65209bcced6840c73164b5ec67b96b43123e970e
[profile/ivi/ofono-qt.git] / lib / ofonovoicecallmanager.cpp
1 /*
2  * This file is part of ofono-qt
3  *
4  * Copyright (C) 2010-2011 Nokia Corporation and/or its subsidiary(-ies).
5  *
6  * Contact: Alexander Kanavin <alex.kanavin@gmail.com>
7  *
8  * Portions of this file are Copyright (C) 2011 Intel Corporation
9  * Contact: Shane Bryan <shane.bryan@linux.intel.com>
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * version 2.1 as published by the Free Software Foundation.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301 USA
24  *
25  */
26
27 #include <QtDBus/QtDBus>
28 #include <QtCore/QObject>
29
30 #include "ofonovoicecallmanager.h"
31 #include "ofonointerface.h"
32
33 #define DIAL_TIMEOUT 30000
34 #define TONE_TIMEOUT 10000
35 #define TRANSFER_TIMEOUT 20000
36 #define SWAP_TIMEOUT 20000
37 #define HANGUP_TIMEOUT 30000
38 #define HOLD_TIMEOUT 30000
39 #define PRIVATE_CHAT_TIMEOUT 30000
40 #define CREATE_MULTIPARTY_TIMEOUT 30000
41
42 QDBusArgument &operator<<(QDBusArgument &argument, const OfonoVoiceCallManagerStruct &call)
43 {
44     argument.beginStructure();
45     argument << call.path << call.properties;
46     argument.endStructure();
47     return argument;
48 }
49
50 const QDBusArgument &operator>>(const QDBusArgument &argument, OfonoVoiceCallManagerStruct &call)
51 {
52     argument.beginStructure();
53     argument >> call.path >> call.properties;
54     argument.endStructure();
55     return argument;
56 }
57
58 OfonoVoiceCallManager::OfonoVoiceCallManager(OfonoModem::SelectionSetting modemSetting, const QString &modemPath, QObject *parent)
59     : OfonoModemInterface(modemSetting, modemPath, "org.ofono.VoiceCallManager", OfonoGetAllOnStartup, parent)
60 {
61     qDBusRegisterMetaType<OfonoVoiceCallManagerStruct>();
62     qDBusRegisterMetaType<OfonoVoiceCallManagerList>();
63
64     m_calllist = getCallList();
65
66     connect(m_if, SIGNAL(propertyChanged(const QString&, const QVariant&)), 
67             this, SLOT(propertyChanged(const QString&, const QVariant&)));
68     connect(this, SIGNAL(validityChanged(bool)),
69             this, SLOT(validityChanged(bool)));
70     connect(modem(), SIGNAL(pathChanged(QString)), this, SLOT(pathChanged(const QString&)));
71
72     connectDbusSignals(path());
73 }
74
75 OfonoVoiceCallManager::~OfonoVoiceCallManager()
76 {
77 }
78
79
80 void OfonoVoiceCallManager::validityChanged(bool /*validity*/)
81 {
82     m_calllist = getCallList();
83 }
84
85 void OfonoVoiceCallManager::pathChanged(const QString& path)
86 {
87     connectDbusSignals(path);
88 }
89
90 QStringList OfonoVoiceCallManager::getCallList()
91 {
92     QDBusReply<OfonoVoiceCallManagerList> reply;
93     OfonoVoiceCallManagerList calls;
94
95     QDBusMessage request;
96     QStringList messageList;
97
98     qDBusRegisterMetaType<OfonoVoiceCallManagerStruct>();
99     qDBusRegisterMetaType<OfonoVoiceCallManagerList>();
100
101     request = QDBusMessage::createMethodCall("org.ofono",
102                                              path(), m_if->ifname(),
103                                              "GetCalls");
104     reply = QDBusConnection::systemBus().call(request);
105
106     calls = reply;
107     foreach(OfonoVoiceCallManagerStruct call, calls) {
108         messageList<< call.path.path();
109     }
110     return messageList;
111 }
112
113 void OfonoVoiceCallManager::connectDbusSignals(const QString& path)
114 {
115     QDBusConnection::systemBus().disconnect("org.ofono", QString(), m_if->ifname(),
116                                          "CallAdded", this,
117                                          SLOT(callAddedChanged(const QDBusObjectPath&, const QVariantMap&)));
118
119     QDBusConnection::systemBus().disconnect("org.ofono", QString(), m_if->ifname(),
120                                          "CallRemoved", this,
121                                          SLOT(callRemovedChanged(const QDBusObjectPath&)));
122
123     QDBusConnection::systemBus().connect("org.ofono", path, m_if->ifname(),
124                                          "CallAdded", this,
125                                          SLOT(callAddedChanged(const QDBusObjectPath&, const QVariantMap&)));
126
127     QDBusConnection::systemBus().connect("org.ofono", path, m_if->ifname(),
128                                          "CallRemoved", this,
129                                          SLOT(callRemovedChanged(const QDBusObjectPath&)));
130 }
131
132 void OfonoVoiceCallManager::dial(const QString &number, const QString &callerid_hide)
133 {
134     QDBusMessage request;
135     request = QDBusMessage::createMethodCall("org.ofono",
136                                              path(), m_if->ifname(),
137                                              "Dial");
138     QList<QVariant>arg;
139     arg.append(QVariant(number));
140     arg.append(QVariant(callerid_hide));
141     request.setArguments(arg);
142     QDBusConnection::systemBus().callWithCallback(request, this,
143                                         SLOT(dialResp()),
144                                         SLOT(dialErr(const QDBusError&)),
145                                         DIAL_TIMEOUT);
146 }
147
148 void OfonoVoiceCallManager::hangupAll()
149 {
150     QDBusMessage request;
151     request = QDBusMessage::createMethodCall("org.ofono",
152                                              path(), m_if->ifname(),
153                                              "HangupAll");
154
155     QDBusConnection::systemBus().callWithCallback(request, this,
156                                         SLOT(hangupAllResp()),
157                                         SLOT(hangupAllErr(const QDBusError&)),
158                                         HANGUP_TIMEOUT);
159 }
160
161 void OfonoVoiceCallManager::sendTones(const QString &tonestring)
162 {
163     QDBusMessage request;
164     request = QDBusMessage::createMethodCall("org.ofono",
165                                              path(), m_if->ifname(),
166                                              "SendTones");
167     QList<QVariant>arg;
168     arg.append(QVariant(tonestring));
169     request.setArguments(arg);
170
171     QDBusConnection::systemBus().callWithCallback(request, this,
172                                         SLOT(sendTonesResp()),
173                                         SLOT(sendTonesErr(const QDBusError&)),
174                                         (TONE_TIMEOUT*tonestring.length()));
175 }
176
177 void OfonoVoiceCallManager::transfer()
178 {
179     QDBusMessage request;
180     request = QDBusMessage::createMethodCall("org.ofono",
181                                              path(), m_if->ifname(),
182                                              "Transfer");
183
184     QDBusConnection::systemBus().callWithCallback(request, this,
185                                         SLOT(transferResp()),
186                                         SLOT(transferErr(const QDBusError&)),
187                                         TRANSFER_TIMEOUT);
188 }
189
190 void OfonoVoiceCallManager::swapCalls()
191 {
192     QDBusMessage request;
193     request = QDBusMessage::createMethodCall("org.ofono",
194                                              path(), m_if->ifname(),
195                                              "SwapCalls");
196
197     QDBusConnection::systemBus().callWithCallback(request, this,
198                                         SLOT(swapCallsResp()),
199                                         SLOT(swapCallsErr(const QDBusError&)),
200                                         SWAP_TIMEOUT);
201 }
202
203 void OfonoVoiceCallManager::releaseAndAnswer()
204 {
205     QDBusMessage request;
206     request = QDBusMessage::createMethodCall("org.ofono",
207                                              path(), m_if->ifname(),
208                                              "ReleaseAndAnswer");
209
210     QDBusConnection::systemBus().callWithCallback(request, this,
211                                         SLOT(releaseAndAnswerResp()),
212                                         SLOT(releaseAndAnswerErr(const QDBusError&)),
213                                         HANGUP_TIMEOUT);
214 }
215
216 void OfonoVoiceCallManager::holdAndAnswer()
217 {
218     QDBusMessage request;
219     request = QDBusMessage::createMethodCall("org.ofono",
220                                              path(), m_if->ifname(),
221                                              "HoldAndAnswer");
222
223     QDBusConnection::systemBus().callWithCallback(request, this,
224                                         SLOT(holdAndAnswerResp()),
225                                         SLOT(holdAndAnswerErr(const QDBusError&)),
226                                         HOLD_TIMEOUT);
227 }
228
229 void OfonoVoiceCallManager::privateChat(const QString &call)
230 {
231     QDBusMessage request;
232     request = QDBusMessage::createMethodCall("org.ofono",
233                                              path(), m_if->ifname(),
234                                              "PrivateChat");
235
236     QList<QVariant>arg;
237     arg.append(qVariantFromValue(QDBusObjectPath(call)));
238     request.setArguments(arg);
239     QDBusConnection::systemBus().callWithCallback(request, this,
240                                         SLOT(privateChatResp()),
241                                         SLOT(privateChatErr(const QDBusError&)),
242                                         PRIVATE_CHAT_TIMEOUT);
243 }
244
245 void OfonoVoiceCallManager::createMultiparty()
246 {
247     QDBusMessage request;
248     request = QDBusMessage::createMethodCall("org.ofono",
249                                              path(), m_if->ifname(),
250                                              "CreateMultiparty");
251
252     QDBusConnection::systemBus().callWithCallback(request, this,
253                                         SLOT(createMultipartyResp()),
254                                         SLOT(createMultipartyErr(const QDBusError&)),
255                                         CREATE_MULTIPARTY_TIMEOUT);
256 }
257
258 void OfonoVoiceCallManager::hangupMultiparty()
259 {
260     QDBusMessage request;
261     request = QDBusMessage::createMethodCall("org.ofono",
262                                              path(), m_if->ifname(),
263                                              "HangupMultiparty");
264
265     QDBusConnection::systemBus().callWithCallback(request, this,
266                                         SLOT(hangupMultipartyResp()),
267                                         SLOT(hangupMultipartyErr(const QDBusError&)),
268                                         HANGUP_TIMEOUT);
269 }
270
271 void OfonoVoiceCallManager::hangupMultipartyResp()
272 {
273     emit hangupMultipartyComplete(TRUE);
274 }
275
276 void OfonoVoiceCallManager::hangupMultipartyErr(const QDBusError &error)
277 {
278     m_if->setError(error.name(), error.message());
279     emit hangupMultipartyComplete(FALSE);
280 }
281
282 void OfonoVoiceCallManager::createMultipartyResp()
283 {
284     emit createMultipartyComplete(TRUE);
285 }
286
287 void OfonoVoiceCallManager::createMultipartyErr(const QDBusError &error)
288 {
289     m_if->setError(error.name(), error.message());
290     emit createMultipartyComplete(FALSE);
291 }
292
293 void OfonoVoiceCallManager::privateChatResp()
294 {
295     emit privateChatComplete(TRUE);
296 }
297
298 void OfonoVoiceCallManager::privateChatErr(const QDBusError &error)
299 {
300     m_if->setError(error.name(), error.message());
301     emit privateChatComplete(FALSE);
302 }
303
304 void OfonoVoiceCallManager::holdAndAnswerResp()
305 {
306     emit holdAndAnswerComplete(TRUE);
307 }
308
309 void OfonoVoiceCallManager::holdAndAnswerErr(const QDBusError &error)
310 {
311     m_if->setError(error.name(), error.message());
312     emit holdAndAnswerComplete(FALSE);
313 }
314
315 void OfonoVoiceCallManager::releaseAndAnswerResp()
316 {
317     emit releaseAndAnswerComplete(TRUE);
318 }
319
320 void OfonoVoiceCallManager::releaseAndAnswerErr(const QDBusError &error)
321 {
322     m_if->setError(error.name(), error.message());
323     emit releaseAndAnswerComplete(FALSE);
324 }
325
326 void OfonoVoiceCallManager::swapCallsResp()
327 {
328     emit swapCallsComplete(TRUE);
329 }
330
331 void OfonoVoiceCallManager::swapCallsErr(const QDBusError &error)
332 {
333     m_if->setError(error.name(), error.message());
334     emit swapCallsComplete(FALSE);
335 }
336
337 void OfonoVoiceCallManager::dialResp()
338 {
339     emit dialComplete(TRUE);
340 }
341
342 void OfonoVoiceCallManager::dialErr(const QDBusError &error)
343 {
344     m_if->setError(error.name(), error.message());
345     emit dialComplete(FALSE);
346 }
347
348 void OfonoVoiceCallManager::hangupAllResp()
349 {
350     emit hangupAllComplete(TRUE);
351 }
352
353 void OfonoVoiceCallManager::hangupAllErr(const QDBusError &error)
354 {
355     m_if->setError(error.name(), error.message());
356     emit hangupAllComplete(FALSE);
357 }
358 void OfonoVoiceCallManager::sendTonesResp()
359 {
360     emit sendTonesComplete(TRUE);
361 }
362
363 void OfonoVoiceCallManager::sendTonesErr(const QDBusError &error)
364 {
365     m_if->setError(error.name(), error.message());
366     emit sendTonesComplete(FALSE);
367 }
368
369 void OfonoVoiceCallManager::transferResp()
370 {
371     emit transferComplete(TRUE);
372 }
373
374 void OfonoVoiceCallManager::transferErr(const QDBusError &error)
375 {
376     m_if->setError(error.name(), error.message());
377     emit transferComplete(FALSE);
378 }
379
380 QStringList OfonoVoiceCallManager::emergencyNumbers() const
381 {
382     return m_if->properties()["EmergencyNumbers"].value<QStringList>();
383 }
384
385 void OfonoVoiceCallManager::propertyChanged(const QString &property, const QVariant &value)
386 {
387     if (property == "EmergencyNumbers") {       
388         emit emergencyNumbersChanged(value.value<QStringList>());
389     }
390 }
391
392 QStringList OfonoVoiceCallManager::getCalls() const
393 {
394     return m_calllist;
395 }
396
397 void OfonoVoiceCallManager::callAddedChanged(const QDBusObjectPath &path, const QVariantMap& /*values*/)
398 {
399     m_calllist << path.path();
400     emit callAdded(path.path());
401 }
402
403 void OfonoVoiceCallManager::callRemovedChanged(const QDBusObjectPath &path)
404 {
405     m_calllist.removeAll(path.path());
406     emit callRemoved(path.path());
407 }