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