Merge branch 'master' of ssh://drop.maemo.org/git/vicar
[vicar] / src / vicar-telepathy / cpp / 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 #include <logutility.h>
33
34 #include <QtCore/QDebug>
35
36 namespace
37 {
38 static const QString protocol_name("tel");
39 }
40
41 class ConnectionManagerPrivate
42 {
43 public:
44     ConnectionManagerPrivate(ConnectionManager * parent) :
45         adaptor(new ConnectionManagerAdaptor(parent)),
46 <<<<<<< HEAD
47         logUtility(new LogUtility(parent))
48 =======
49         logUtility(new LogUtility("/var/log/vicar/vicar.log",parent))
50 >>>>>>> 74800375ecf7f41e290cf7cc7fa9ee8b230be68e
51     {
52         Q_ASSERT(0 != adaptor);
53         activeConnection = 0;
54     }
55
56     ~ConnectionManagerPrivate() { delete(adaptor);}
57     ConnectionManagerAdaptor * const adaptor;
58     LogUtility * const logUtility;
59     Connection * activeConnection;
60 };
61
62 // ---------------------------------------------------------------------------
63
64 ConnectionManager::ConnectionManager(QObject * parent) :
65     QObject(parent),
66     d(new ConnectionManagerPrivate(this))
67 { Q_ASSERT(0 != d); }
68
69 ConnectionManager::~ConnectionManager()
70 { delete(d); }
71
72 org::freedesktop::Telepathy::ParameterDefinitionList
73 ConnectionManager::GetParameters(const QString &proto)
74 {
75
76     QString strMessage;
77     Q_ASSERT(!proto.isEmpty());    
78     Q_UNUSED(proto);
79     strMessage = "VICAR: ConnectionManager::GetParameters(const QString &prot)";
80 <<<<<<< HEAD
81 =======
82     qDebug() << strMessage;
83 >>>>>>> 74800375ecf7f41e290cf7cc7fa9ee8b230be68e
84     d->logUtility->logMessage(strMessage);
85     org::freedesktop::Telepathy::ParameterDefinitionList result;
86     org::freedesktop::Telepathy::ParameterDefinition param;
87
88     //TODO - Match the parameters with telepathy-ring?
89
90     // Attention! Default constructed QDBusVariants cause havok on the D-Bus!
91     param.name = "com.nokia.Telepathy.Connection.Interface.GSM.IMSI";
92     param.flags = Register;
93     param.signature = "s";
94     param.defaultValue = QDBusVariant(QString());
95     result.append(param);
96
97     param.name = "com.nokia.Telepathy.Connection.Interface.GSM.Privacy";
98     param.flags = Register|hasDefault;
99     param.signature = "s";
100     param.defaultValue = QDBusVariant(QString());
101     result.append(param);
102
103     param.name = "com.nokia.Telepathy.Connection.Interface.GSM.SMSServiceCentre";
104     param.flags = Register;
105     param.signature = "s";
106     param.defaultValue = QDBusVariant(QString());
107     result.append(param);
108
109     param.name = "com.nokia.Telepathy.Connection.Interface.GSM.SMSValidityPeriod";
110     param.flags = Register|hasDefault;
111     param.signature = "u";
112     param.defaultValue = QDBusVariant(0);
113     result.append(param);
114
115     param.name = "account";
116     param.flags = None;
117     param.signature = "s";
118     param.defaultValue = QDBusVariant(QString());
119     result.append(param);
120
121     param.name = "password";
122     param.flags = None;
123     param.signature = "s";
124     param.defaultValue = QDBusVariant(QString());
125     result.append(param);
126
127     return result;
128 }
129
130 QStringList ConnectionManager::ListProtocols()
131 {
132     QString strMessage = "VICaR ConnectionManager::ListProtocols()";
133 <<<<<<< HEAD
134 =======
135     qDebug() << strMessage;
136 >>>>>>> 74800375ecf7f41e290cf7cc7fa9ee8b230be68e
137     d->logUtility->logMessage(strMessage);
138     return QStringList(protocol_name);
139 }
140
141 QString ConnectionManager::RequestConnection(const QString & proto,
142                                              QVariantMap parameters,
143                                              QDBusObjectPath & object_path)
144 {
145     QString strMessage =  "VICaR CM: Connection Requested for protocol "+proto;
146 <<<<<<< HEAD
147 =======
148     qDebug() << strMessage;
149 >>>>>>> 74800375ecf7f41e290cf7cc7fa9ee8b230be68e
150     d->logUtility->logMessage(strMessage);
151     QString connection_service;    
152     //object_path = QDBusObjectPath();
153
154     if (proto != protocol_name)
155     {
156         /*
157         sendErrorReply("org.freedesktop.Telepathy.Error.NotImplemented",
158                        "VICaR - Unable to create Connection. Requested protocol is not implemented.");
159         */
160         strMessage =  "VICaR CM::RequestConnection: proto mismatch.";
161 <<<<<<< HEAD
162         d->logUtility->logMessage(strMessage);
163         return connection_service;
164     }
165     if (d->activeConnection != 0){
166         strMessage =  "VICaR CM::RequestConnection: An active connection already exists at "+d->activeConnection->serviceName();
167         d->logUtility->logMessage(strMessage);
168
169         object_path = d->activeConnection->objectPath();
170         connection_service = d->activeConnection->serviceName();
171         //emit NewConnection(connection_service, object_path, "tel"); //Just in case, emit the NewConnection again
172         return connection_service;
173     }
174 =======
175         qDebug() << strMessage;
176         d->logUtility->logMessage(strMessage);
177         return connection_service;
178     }
179     if (d->activeConnection != 0){
180         strMessage =  "VICaR CM::RequestConnection: An active connection already exists at "+d->activeConnection->serviceName();
181         qDebug() << strMessage;
182         d->logUtility->logMessage(strMessage);
183
184         object_path = d->activeConnection->objectPath();
185         connection_service = d->activeConnection->serviceName();
186         //emit NewConnection(connection_service, object_path, "tel"); //Just in case, emit the NewConnection again
187         return connection_service;
188     }
189 >>>>>>> 74800375ecf7f41e290cf7cc7fa9ee8b230be68e
190
191     QString imsi;
192     QString privacy;
193     QString smsServiceCenter;
194     uint smsValidityPeriod(0);
195     QString account;
196     QString password;
197
198     // read parameters:
199     QString param;
200     foreach (param, parameters.keys())
201     {
202         if ("com.nokia.Telepathy.Connection.Interface.GSM.IMSI" == param)
203         { imsi = parameters[param].toString(); }
204         else if ("com.nokia.Telepathy.Connection.Interface.GSM.Privacy" == param)
205         { privacy = parameters[param].toString(); }
206         else if ("com.nokia.Telepathy.Connection.Interface.GSM.SMSServiceCentre" == param)
207         { smsServiceCenter = parameters[param].toString(); }
208         else if ("com.nokia.Telepathy.Connection.Interface.GSM.SMSValidityPeriod" == param)
209         { smsValidityPeriod = parameters[param].toInt(); }
210         else if ("account" == param)
211         { account = parameters[param].toString(); }
212         else if ("password" == param)
213         { password = parameters[param].toString(); }
214         else
215         {
216             /*
217             sendErrorReply("org.freedesktop.Telepathy.Error.InvalidArgument",
218                            "VICaR - Unable to create Connection. Invalid parameters specified.");
219             */
220             strMessage =  "VICaR CM::RequestConnection: invalid parameter" + param + "found.";
221 <<<<<<< HEAD
222 =======
223             qDebug() << strMessage;
224 >>>>>>> 74800375ecf7f41e290cf7cc7fa9ee8b230be68e
225             d->logUtility->logMessage(strMessage);
226             return connection_service;
227         }
228     }
229
230     strMessage =  "DEBUG CM: Trying to create new connection with account "+account;
231 <<<<<<< HEAD
232 =======
233     qDebug() << strMessage;
234 >>>>>>> 74800375ecf7f41e290cf7cc7fa9ee8b230be68e
235     d->logUtility->logMessage(strMessage);
236
237     Connection * new_connection = new Connection(account, this);
238     Q_ASSERT(0 != new_connection);
239
240     strMessage =  "DEBUG CM: Trying to register new connection";
241 <<<<<<< HEAD
242 =======
243     qDebug() << strMessage;
244 >>>>>>> 74800375ecf7f41e290cf7cc7fa9ee8b230be68e
245     d->logUtility->logMessage(strMessage);
246
247     if (!new_connection->registerObject())
248     {
249         strMessage = "VICaR CM: Error while registering Connection object with DBus.";
250 <<<<<<< HEAD
251 =======
252         qDebug() << strMessage;
253 >>>>>>> 74800375ecf7f41e290cf7cc7fa9ee8b230be68e
254         d->logUtility->logMessage(strMessage);
255         new_connection->deleteLater();
256         return QString();
257     }
258
259     strMessage =  "VICaR CM: New Connection Created. Status is " + QString(new_connection->GetStatus());
260 <<<<<<< HEAD
261 =======
262     qDebug() << strMessage;
263 >>>>>>> 74800375ecf7f41e290cf7cc7fa9ee8b230be68e
264     d->logUtility->logMessage(strMessage);
265
266
267     object_path = new_connection->objectPath();
268     connection_service = new_connection->serviceName();
269
270     strMessage =  "VICaR CM: Emitting New Connection Signal";
271 <<<<<<< HEAD
272 =======
273     qDebug() << strMessage;
274 >>>>>>> 74800375ecf7f41e290cf7cc7fa9ee8b230be68e
275     d->logUtility->logMessage(strMessage);
276
277     emit NewConnection(connection_service, object_path, "tel");
278
279     //Set ActiveConnection - We only need one active connection at a time
280     d->activeConnection = new_connection;
281     return new_connection->serviceName();
282 }