Release version 0.6-2
[vicar] / src / vicar-telepathy / src / connectionmanager.cpp
1 /*
2 @version: 0.6
3 @author: Sudheer K. <scifi1947 at gmail.com>
4 @license: GNU General Public License
5
6 Based on Telepathy-SNOM with copyright notice below.
7 */
8
9 /*
10  * Telepathy SNOM VoIP phone connection manager
11  * Copyright (C) 2006 by basyskom GmbH
12  *  @author Tobias Hunger <info@basyskom.de>
13  *
14  * This library is free software; you can redisQObject::tribute it and/or
15  * modify it under the terms of the GNU Lesser General Public
16  * License version 2.1 as published by the Free Software Foundation.
17  *
18  * This library is disQObject::tributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public
24  * License along with this library; if not, write to the
25  * Free Software Foundation, Inc.,
26  * 51 Franklin SQObject::treet, Fifth Floor, Boston, MA  02110-1301  USA
27  */
28
29 #include "connectionmanager.h"
30 #include "connectionmanageradaptor.h"
31 #include "connection.h"
32
33 #include <QtCore/QDebug>
34
35 namespace
36 {
37 static const QString protocol_name("tel");
38 }
39
40 class ConnectionManagerPrivate
41 {
42 public:
43     ConnectionManagerPrivate(ConnectionManager * parent) :
44         adaptor(new ConnectionManagerAdaptor(parent))
45     { Q_ASSERT(0 != adaptor); }
46
47     ~ConnectionManagerPrivate() { delete(adaptor);}
48
49     ConnectionManagerAdaptor * const adaptor;
50 };
51
52 // ---------------------------------------------------------------------------
53
54 ConnectionManager::ConnectionManager(QObject * parent) :
55     QObject(parent),
56     d(new ConnectionManagerPrivate(this))
57 { Q_ASSERT(0 != d); }
58
59 ConnectionManager::~ConnectionManager()
60 { delete(d); }
61
62 org::freedesktop::Telepathy::ParameterDefinitionList
63 ConnectionManager::GetParameters(const QString &proto)
64 {
65     Q_ASSERT(!proto.isEmpty());
66     qDebug() << "VICAR: ConnectionManager::GetParameters(const QString &prot)";
67     org::freedesktop::Telepathy::ParameterDefinitionList result;
68     org::freedesktop::Telepathy::ParameterDefinition param;
69
70     // Attention! Default constructed QDBusVariants cause havok on the D-Bus!
71     param.name = "com.nokia.Telepathy.Connection.Interface.GSM.IMSI";
72     param.flags = Register;
73     param.signature = "s";
74     //param.defaultValue = QDBusVariant(QString());
75     result.append(param);
76
77     param.name = "com.nokia.Telepathy.Connection.Interface.GSM.Privacy";
78     param.flags = Register|hasDefault;
79     param.signature = "s";
80     param.defaultValue = QDBusVariant(QString());
81     result.append(param);
82
83     param.name = "com.nokia.Telepathy.Connection.Interface.GSM.SMSServiceCentre";
84     param.flags = Register;
85     param.signature = "s";
86     //param.defaultValue = QDBusVariant(QString());
87     result.append(param);
88
89     param.name = "com.nokia.Telepathy.Connection.Interface.GSM.SMSValidityPeriod";
90     param.flags = Register|hasDefault;
91     param.signature = "u";
92     param.defaultValue = QDBusVariant(0);
93     result.append(param);
94
95     param.name = "account";
96     param.flags = None;
97     param.signature = "s";
98     //param.defaultValue = QDBusVariant(QString());
99     result.append(param);
100
101     param.name = "password";
102     param.flags = None;
103     param.signature = "s";
104     //param.defaultValue = QDBusVariant(QString());
105     result.append(param);
106
107     return result;
108 }
109
110 QStringList ConnectionManager::ListProtocols()
111 {
112     qDebug() << "VICaR ConnectionManager::ListProtocols()";
113     return QStringList(protocol_name);
114 }
115
116 QString ConnectionManager::RequestConnection(const QString & proto,
117                                              QVariantMap parameters,
118                                              QDBusObjectPath & object_path)
119 {
120     qDebug() << "VICaR CM: Connection Requested...";
121     QString connection_service;
122     object_path = QDBusObjectPath();
123
124     if (proto != protocol_name)
125     {
126         /*
127         sendErrorReply("org.freedesktop.Telepathy.Error.NotImplemented",
128                        "VICaR - Unable to create Connection. Requested protocol is not implemented.");
129         */
130         qDebug() << "VICaR CM::RequestConnection: proto mismatch.";
131         return connection_service;
132     }
133
134
135     QString imsi;
136     QString privacy;
137     QString smsServiceCenter;
138     uint smsValidityPeriod(0);
139     QString account;
140     QString password;
141
142     // read parameters:
143     QString param;
144     foreach (param, parameters.keys())
145     {
146         if ("com.nokia.Telepathy.Connection.Interface.GSM.IMSI" == param)
147         { imsi = parameters[param].toString(); }
148         else if ("com.nokia.Telepathy.Connection.Interface.GSM.Privacy" == param)
149         { privacy = parameters[param].toString(); }
150         else if ("com.nokia.Telepathy.Connection.Interface.GSM.SMSServiceCentre" == param)
151         { smsServiceCenter = parameters[param].toString(); }
152         else if ("com.nokia.Telepathy.Connection.Interface.GSM.SMSValidityPeriod" == param)
153         { smsValidityPeriod = parameters[param].toInt(); }
154         else if ("account" == param)
155         { account = parameters[param].toString(); }
156         else if ("password" == param)
157         { password = parameters[param].toString(); }
158         else
159         {
160             /*
161             sendErrorReply("org.freedesktop.Telepathy.Error.InvalidArgument",
162                            "VICaR - Unable to create Connection. Invalid parameters specified.");
163             */
164             qDebug() << "VICaR CM::RequestConnection: invalid parameter" << param << "found.";
165             return connection_service;
166         }
167     }
168
169     Connection * new_connection = new Connection(account, this);
170     Q_ASSERT(0 != new_connection);
171
172     if (!new_connection->registerObject())
173     {
174         qDebug() << "VICaR CM: Error while registering Connection object with DBus.";
175         delete new_connection;
176         return QString();
177     }
178
179     qDebug() << "VICaR CM: New Connection Created. Status is "<< new_connection->GetStatus();
180
181     object_path = new_connection->objectPath();
182     connection_service = new_connection->serviceName();
183
184     emit NewConnection(connection_service, object_path, "tel");
185
186     return new_connection->serviceName();
187 }