/
voicecallmanager.cpp
437 lines (367 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
* This file is a part of the Voice Call Manager project
*
* Copyright (C) 2011-2012 Tom Swindell <t.swindell@rubyx.co.uk>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "common.h"
#include "voicecallmanager.h"
#include <QHash>
25
#include <QUuid>
26
27
28
#include <QSettings>
#ifdef WITH_NEMO_DEVICELOCK
29
#include <nemo-devicelock/devicelock.h>
30
#endif
31
32
#ifdef WITH_AUDIOPOLICY
33
#include "audiocallpolicyproxy.h"
34
#endif
35
36
37
class VoiceCallManagerPrivate
{
38
39
Q_DECLARE_PUBLIC(VoiceCallManager)
40
public:
41
VoiceCallManagerPrivate(VoiceCallManager *q)
42
: q_ptr(q), activeVoiceCall(NULL),
43
audioMode("earpiece"), isAudioRouted(false), isMicrophoneMuted(false), isSpeakerMuted(false)
44
45
{/* ... */}
46
VoiceCallManager *q_ptr;
47
48
49
QHash<QString, AbstractVoiceCallProvider*> providers;
50
51
QHash<QString, AbstractVoiceCallHandler*> voiceCalls;
52
53
AbstractVoiceCallHandler *activeVoiceCall;
54
#ifdef WITH_NEMO_DEVICELOCK
55
NemoDeviceLock::DeviceLock deviceLock;
56
57
#endif
58
59
60
61
QString audioMode;
bool isAudioRouted;
bool isMicrophoneMuted;
bool isSpeakerMuted;
62
63
64
65
66
QString errorString;
};
VoiceCallManager::VoiceCallManager(QObject *parent)
67
: VoiceCallManagerInterface(parent), d_ptr(new VoiceCallManagerPrivate(this))
68
69
70
71
72
73
74
{
TRACE
}
VoiceCallManager::~VoiceCallManager()
{
TRACE
75
delete d_ptr;
76
77
78
79
80
}
QString VoiceCallManager::errorString() const
{
TRACE
81
Q_D(const VoiceCallManager);
82
83
84
85
86
return d->errorString;
}
void VoiceCallManager::setError(const QString &errorString)
{
87
88
TRACE
Q_D(VoiceCallManager);
89
90
91
92
93
94
95
96
d->errorString = errorString;
qDebug() << d->errorString;
emit this->error(d->errorString);
}
AbstractVoiceCallHandler* VoiceCallManager::activeVoiceCall() const
{
TRACE
97
Q_D(const VoiceCallManager);
98
99
100
101
102
103
return d->activeVoiceCall;
}
QList<AbstractVoiceCallProvider*> VoiceCallManager::providers() const
{
TRACE
104
Q_D(const VoiceCallManager);
105
106
107
108
109
110
return d->providers.values();
}
void VoiceCallManager::appendProvider(AbstractVoiceCallProvider *provider)
{
TRACE
111
Q_D(VoiceCallManager);
112
113
if(d->providers.contains(provider->providerId())) return;
114
DEBUG_T("VCM: Registering voice call provider: %s", qPrintable(provider->providerId()));
115
116
117
118
119
120
QObject::connect(provider,
SIGNAL(voiceCallAdded(AbstractVoiceCallHandler*)),
SLOT(onVoiceCallAdded(AbstractVoiceCallHandler*)));
QObject::connect(provider,
SIGNAL(voiceCallRemoved(QString)),
SLOT(onVoiceCallRemoved(QString)));
121
122
123
QObject::connect(provider,
SIGNAL(error(QString)),
SLOT(setError(QString)));
124
125
126
127
d->providers.insert(provider->providerId(), provider);
emit this->providersChanged();
emit this->providerAdded(provider);
128
129
130
foreach (AbstractVoiceCallHandler *handler, provider->voiceCalls())
onVoiceCallAdded(handler);
131
132
133
134
135
}
void VoiceCallManager::removeProvider(AbstractVoiceCallProvider *provider)
{
TRACE
136
Q_D(VoiceCallManager);
137
138
if(!d->providers.contains(provider->providerId())) return;
139
DEBUG_T("VCM: Deregistering voice call provider: %s", qPrintable(provider->providerId()));
140
141
142
143
144
145
146
147
QObject::disconnect(provider,
SIGNAL(voiceCallAdded(AbstractVoiceCallHandler*)),
this,
SLOT(onVoiceCallAdded(AbstractVoiceCallHandler*)));
QObject::disconnect(provider,
SIGNAL(voiceCallRemoved(QString)),
this,
SLOT(onVoiceCallRemoved(QString)));
148
149
150
151
QObject::disconnect(provider,
SIGNAL(error(QString)),
this,
SLOT(setError(QString)));
152
153
154
155
156
157
d->providers.remove(provider->providerId());
emit this->providersChanged();
emit this->providerRemoved(provider->providerId());
}
158
159
160
161
162
163
164
QString VoiceCallManager::generateHandlerId()
{
TRACE
QString handlerId = QUuid::createUuid().toString().mid(1, 36); // Remove curly braces.
return handlerId.replace('-', ""); // Remove dashes, (can't have dashes in dbus paths)
}
165
166
167
int VoiceCallManager::voiceCallCount() const
{
TRACE
168
Q_D(const VoiceCallManager);
169
170
171
172
173
174
175
176
177
178
179
180
181
int result = 0;
foreach(AbstractVoiceCallProvider *provider, d->providers.values())
{
result += provider->voiceCalls().length();
}
return result;
}
QList<AbstractVoiceCallHandler*> VoiceCallManager::voiceCalls() const
{
TRACE
182
Q_D(const VoiceCallManager);
183
184
185
186
187
188
189
190
QList<AbstractVoiceCallHandler*> results;
foreach(AbstractVoiceCallProvider *provider, d->providers)
{
results.append(provider->voiceCalls());
}
return results;
191
192
}
193
QString VoiceCallManager::audioMode() const
194
195
{
TRACE
196
Q_D(const VoiceCallManager);
197
return d->audioMode;
198
199
}
200
bool VoiceCallManager::isAudioRouted() const
201
202
{
TRACE
203
Q_D(const VoiceCallManager);
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
return d->isAudioRouted;
}
bool VoiceCallManager::isMicrophoneMuted() const
{
TRACE
Q_D(const VoiceCallManager);
return d->isMicrophoneMuted;
}
bool VoiceCallManager::isSpeakerMuted() const
{
TRACE
Q_D(const VoiceCallManager);
return d->isSpeakerMuted;
}
void VoiceCallManager::setAudioMode(const QString &mode)
{
TRACE
Q_D(VoiceCallManager);
d->audioMode = mode;
emit this->setAudioModeRequested(mode);
227
emit this->audioModeChanged();
228
229
230
231
232
233
234
235
}
void VoiceCallManager::setAudioRouted(bool on)
{
TRACE
Q_D(VoiceCallManager);
d->isAudioRouted = on;
emit this->setAudioRoutedRequested(on);
236
emit this->audioRoutedChanged();
237
238
239
240
241
}
void VoiceCallManager::setMuteMicrophone(bool on)
{
TRACE
242
Q_D(VoiceCallManager);
243
d->isMicrophoneMuted = on;
244
emit this->setMuteMicrophoneRequested(on);
245
emit this->microphoneMutedChanged();
246
247
}
248
void VoiceCallManager::setMuteSpeaker(bool on)
249
250
{
TRACE
251
Q_D(VoiceCallManager);
252
253
254
d->isSpeakerMuted = on;
emit this->setMuteSpeakerRequested(on);
emit this->speakerMutedChanged();
255
256
}
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
void VoiceCallManager::onAudioModeChanged(const QString &mode)
{
TRACE
Q_D(VoiceCallManager);
d->audioMode = mode;
emit this->audioModeChanged();
}
void VoiceCallManager::onAudioRoutedChanged(bool on)
{
TRACE
Q_D(VoiceCallManager);
d->isAudioRouted = on;
emit this->audioRoutedChanged();
}
void VoiceCallManager::onMuteMicrophoneChanged(bool on)
{
TRACE
Q_D(VoiceCallManager);
d->isMicrophoneMuted = on;
emit this->microphoneMutedChanged();
}
void VoiceCallManager::onMuteSpeakerChanged(bool on)
{
TRACE
Q_D(VoiceCallManager);
d->isSpeakerMuted = on;
emit this->speakerMutedChanged();
}
289
290
291
bool VoiceCallManager::dial(const QString &providerId, const QString &msisdn)
{
TRACE
292
Q_D(VoiceCallManager);
293
294
295
296
297
298
299
300
AbstractVoiceCallProvider *provider = d->providers.value(providerId);
if(!provider)
{
this->setError(QString("*** Unable to find voice call provider with id: ") + providerId);
return false;
}
301
return provider->dial(msisdn);
302
303
}
304
305
306
307
308
309
void VoiceCallManager::silenceRingtone()
{
TRACE
emit this->silenceRingtoneRequested();
}
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
void VoiceCallManager::startEventTone(ToneType type, int volume)
{
TRACE
emit this->startEventToneRequested(type, volume);
}
void VoiceCallManager::stopEventTone()
{
TRACE
emit this->stopEventToneRequested();
}
void VoiceCallManager::startDtmfTone(const QString &tone, int volume)
{
TRACE
emit this->startDtmfToneRequested(tone, volume);
}
void VoiceCallManager::stopDtmfTone()
{
TRACE
emit this->stopDtmfToneRequested();
}
334
335
336
void VoiceCallManager::onVoiceCallAdded(AbstractVoiceCallHandler *handler)
{
TRACE
337
338
Q_D(VoiceCallManager);
339
#ifdef WITH_NEMO_DEVICELOCK
340
341
if (!handler->isEmergency()
&& d->deviceLock.state() == NemoDeviceLock::DeviceLock::ManagerLockout) {
342
343
344
#else
if (!handler->isEmergency()) {
#endif
345
346
347
348
349
handler->hangup();
return;
}
350
351
//AudioCallPolicyProxy *pHandler = new AudioCallPolicyProxy(handler, this);
d->voiceCalls.insert(handler->handlerId(), handler);
352
353
emit this->voiceCallAdded(handler);
354
emit this->voiceCallsChanged();
355
356
357
if(!d->activeVoiceCall)
{
358
d->activeVoiceCall = handler;
359
360
361
362
363
364
365
emit this->activeVoiceCallChanged();
}
}
void VoiceCallManager::onVoiceCallRemoved(const QString &handlerId)
{
TRACE
366
Q_D(VoiceCallManager);
367
AbstractVoiceCallHandler *handler = d->voiceCalls.value(handlerId);
368
if (!handler) {
369
DEBUG_T("VCM: attempt to remove unregistered handler: %s", qPrintable(handlerId));
370
371
return;
}
372
373
d->voiceCalls.remove(handlerId);
374
emit this->voiceCallRemoved(handlerId);
375
emit this->voiceCallsChanged();
376
377
if (d->activeVoiceCall && d->activeVoiceCall->handlerId() == handlerId)
378
379
380
381
{
d->activeVoiceCall = NULL;
emit this->activeVoiceCallChanged();
}
382
383
384
385
386
387
388
389
QSettings settings;
settings.beginGroup(QLatin1String("Voice Counters"));
// Update call time statistics
if (handler->isIncoming()) {
int received = settings.value(QLatin1String("Received")).toInt();
received += handler->duration();
390
DEBUG_T("Incoming call ended. Total incoming duration is now %d", received);
391
392
393
394
395
settings.setValue(QLatin1String("Received"), received);
emit totalIncomingCallDurationChanged();
} else {
int dialled = settings.value(QLatin1String("Dialled")).toInt();
dialled += handler->duration();
396
DEBUG_T("Outgoing call ended. Total outgoing duration is now %d", dialled);
397
398
399
400
401
402
settings.setValue(QLatin1String("Dialled"), dialled);
emit totalOutgoingCallDurationChanged();
}
settings.endGroup();
403
handler->deleteLater();
404
}
405
406
407
408
409
410
int VoiceCallManager::totalOutgoingCallDuration() const
{
QSettings settings;
settings.beginGroup(QLatin1String("Voice Counters"));
int dialled = settings.value(QLatin1String("Dialled")).toInt();
411
DEBUG_T("Request for outgoing call duration. Total: %d", dialled);
412
413
414
415
416
417
418
419
return dialled;
}
int VoiceCallManager::totalIncomingCallDuration() const
{
QSettings settings;
settings.beginGroup(QLatin1String("Voice Counters"));
int received = settings.value(QLatin1String("Received")).toInt();
420
DEBUG_T("Request for incoming call duration. Total: %d", received);
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
return received;
}
void VoiceCallManager::resetCallDurationCounters()
{
{
QSettings settings;
settings.beginGroup(QLatin1String("Voice Counters"));
settings.setValue(QLatin1String("Received"), 0);
settings.setValue(QLatin1String("Dialled"), 0);
settings.endGroup();
}
emit totalOutgoingCallDurationChanged();
emit totalIncomingCallDurationChanged();
}