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