summaryrefslogtreecommitdiff
path: root/rockworkd/platformintegration/sailfish/voicecallmanager.cpp
blob: afb36291083e6252b4521714b65a1a36a1b80907 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include "voicecallmanager.h"

#include <QDebug>
#include <QTimer>
#include <QDBusInterface>
#include <QDBusPendingReply>

class VoiceCallManagerPrivate
{
    Q_DECLARE_PUBLIC(VoiceCallManager)

public:
    VoiceCallManagerPrivate(VoiceCallManager *q)
        : q_ptr(q),
          interface(NULL),
          activeVoiceCall(NULL),
          connected(false)
    { /*...*/ }

    VoiceCallManager *q_ptr;

    QDBusInterface *interface;

    QList<VoiceCallHandler*> voicecalls;
    QHash<QString,VoiceCallProviderData> providers;

    VoiceCallHandler* activeVoiceCall;

    bool connected;
};

VoiceCallManager::VoiceCallManager(Settings *settings, QObject *parent)
    : QObject(parent), l(metaObject()->className()), d_ptr(new VoiceCallManagerPrivate(this)), settings(settings)
{
    this->initialize();
}

VoiceCallManager::~VoiceCallManager()
{
    Q_D(VoiceCallManager);
    delete d;
}

void VoiceCallManager::initialize(bool notifyError)
{
    Q_D(VoiceCallManager);
    bool success = false;

    delete d->interface;
    d->interface = new QDBusInterface("org.nemomobile.voicecall",
                                      "/",
                                      "org.nemomobile.voicecall.VoiceCallManager",
                                      QDBusConnection::sessionBus(),
                                      this);

    if(d->interface->isValid())
    {
        success = true;
        success &= (bool)QObject::connect(d->interface, SIGNAL(error(QString)), SIGNAL(error(QString)));
        success &= (bool)QObject::connect(d->interface, SIGNAL(voiceCallsChanged()), SLOT(onVoiceCallsChanged()));
        success &= (bool)QObject::connect(d->interface, SIGNAL(providersChanged()), SLOT(onProvidersChanged()));
        success &= (bool)QObject::connect(d->interface, SIGNAL(activeVoiceCallChanged()), SLOT(onActiveVoiceCallChanged()));
        success &= (bool)QObject::connect(d->interface, SIGNAL(audioModeChanged()), SIGNAL(audioModeChanged()));
        success &= (bool)QObject::connect(d->interface, SIGNAL(audioRoutedChanged()), SIGNAL(audioRoutedChanged()));
        success &= (bool)QObject::connect(d->interface, SIGNAL(microphoneMutedChanged()), SIGNAL(microphoneMutedChanged()));
        success &= (bool)QObject::connect(d->interface, SIGNAL(speakerMutedChanged()), SIGNAL(speakerMutedChanged()));

        onVoiceCallsChanged();
        onActiveVoiceCallChanged();
    }

    if(!(d->connected = success))
    {
        QTimer::singleShot(2000, this, SLOT(initialize()));
        if(notifyError) emit this->error("Failed to connect to VCM D-Bus service.");
    }
}

QDBusInterface* VoiceCallManager::interface() const
{
    Q_D(const VoiceCallManager);
    return d->interface;
}

VoiceCallHandlerList VoiceCallManager::voiceCalls() const
{
    Q_D(const VoiceCallManager);
    return d->voicecalls;
}

VoiceCallProviderHash VoiceCallManager::providers() const
{
    Q_D(const VoiceCallManager);
    return d->providers;
}

QString VoiceCallManager::defaultProviderId() const
{
    Q_D(const VoiceCallManager);
    if(d->providers.count() == 0) {
        qCDebug(l) << Q_FUNC_INFO << "No provider added";
        return QString::null;
    }

    QStringList keys = d->providers.keys();
    qSort(keys);

    VoiceCallProviderData provider = d->providers.value(keys.value(0));
    return provider.id;
}

VoiceCallHandler* VoiceCallManager::activeVoiceCall() const
{
    Q_D(const VoiceCallManager);
    return d->activeVoiceCall;
}

QString VoiceCallManager::audioMode() const
{
    Q_D(const VoiceCallManager);
    return d->interface->property("audioMode").toString();
}

bool VoiceCallManager::isAudioRouted() const
{
    Q_D(const VoiceCallManager);
    return d->interface->property("isAudioRouted").toBool();
}

bool VoiceCallManager::isMicrophoneMuted() const
{
    Q_D(const VoiceCallManager);
    return d->interface->property("isMicrophoneMuted").toBool();
}

bool VoiceCallManager::isSpeakerMuted() const
{
    Q_D(const VoiceCallManager);
    return d->interface->property("isSpeakerMuted").toBool();
}

void VoiceCallManager::dial(const QString &provider, const QString &msisdn)
{
    Q_D(VoiceCallManager);
    QDBusPendingCall call = d->interface->asyncCall("dial", provider, msisdn);

    QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this);
    QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), SLOT(onPendingCallFinished(QDBusPendingCallWatcher*)));
}

void VoiceCallManager::hangupAll()
{
    foreach (VoiceCallHandler* handler, voiceCalls()) {
        handler->hangup();
    }
}

void VoiceCallManager::silenceRingtone()
{
    Q_D(const VoiceCallManager);
    QDBusPendingCall call = d->interface->asyncCall("silenceRingtone");
    QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this);
    QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), SLOT(onPendingSilenceFinished(QDBusPendingCallWatcher*)));
}

/*
  - Use of method calls instead of property setters to allow status checking.
 */
bool VoiceCallManager::setAudioMode(const QString &mode)
{
    Q_D(const VoiceCallManager);
    QDBusPendingReply<bool> reply = d->interface->call("setAudioMode", mode);
    return reply.isError() ? false : reply.value();
}

bool VoiceCallManager::setAudioRouted(bool on)
{
    Q_D(const VoiceCallManager);
    QDBusPendingReply<bool> reply = d->interface->call("setAudioRouted", on);
    return reply.isError() ? false : reply.value();
}

bool VoiceCallManager::setMuteMicrophone(bool on)
{
    Q_D(VoiceCallManager);
    QDBusPendingReply<bool> reply = d->interface->call("setMuteMicrophone", on);
    return reply.isError() ? false : reply.value();
}

bool VoiceCallManager::setMuteSpeaker(bool on)
{
    Q_D(VoiceCallManager);
    QDBusPendingReply<bool> reply = d->interface->call("setMuteSpeaker", on);
    return reply.isError() ? false : reply.value();
}

void VoiceCallManager::onVoiceCallsChanged()
{
    Q_D(VoiceCallManager);
    QStringList nIds = d->interface->property("voiceCalls").toStringList();
    QStringList oIds;

    QStringList added;
    QStringList removed;

    // Map current call handlers to handler ids for easy indexing.
    foreach(VoiceCallHandler *handler, d->voicecalls)
    {
        oIds.append(handler->handlerId());
    }

    // Index new handlers to be added.
    foreach(QString nId, nIds)
    {
        if(!oIds.contains(nId)) added.append(nId);
    }

    // Index old handlers to be removed.
    foreach(QString oId, oIds)
    {
        if(!nIds.contains(oId)) removed.append(oId);
    }

    // Remove handlers that need to be removed.
    foreach(QString removeId, removed)
    {
        for (int i = 0; i < d->voicecalls.count(); ++i) {
            VoiceCallHandler *handler = d->voicecalls.at(i);
            if(handler->handlerId() == removeId)
            {
                handler->disconnect(this);
                d->voicecalls.removeAt(i);
                handler->deleteLater();
                break;
            }
        }
    }

    // Add handlers that need to be added.
    foreach(QString addId, added)
    {
        VoiceCallHandler *handler = new VoiceCallHandler(addId, this);
        d->voicecalls.append(handler);
    }

    emit this->voiceCallsChanged();
}

void VoiceCallManager::onProvidersChanged()
{
    Q_D(VoiceCallManager);
    d->providers.clear();
    foreach(QString provider, d->interface->property("providers").toStringList())
    {
        QStringList parts = provider.split(':');
        d->providers.insert(parts.first(), VoiceCallProviderData(parts.first(),
                                                                 parts.last(),
                                                                 parts.first()));
    }

    emit this->providersChanged();
}

void VoiceCallManager::onActiveVoiceCallChanged()
{
    Q_D(VoiceCallManager);
    QString voiceCallId = d->interface->property("activeVoiceCall").toString();

    if(d->voicecalls.count() == 0 || voiceCallId.isNull() || voiceCallId.isEmpty())
    {
        d->activeVoiceCall = NULL;
    }
    else
    {
        bool found = false;
        d->activeVoiceCall = NULL;
        foreach(VoiceCallHandler* handler, d->voicecalls)
        {
            if(handler->handlerId() == voiceCallId)
            {
                d->activeVoiceCall = handler;
                found = true;
            }
            if(!found) d->activeVoiceCall = NULL;
        }
    }

    emit this->activeVoiceCallChanged();
}

void VoiceCallManager::onPendingCallFinished(QDBusPendingCallWatcher *watcher)
{
    QDBusPendingReply<bool> reply = *watcher;

    if (reply.isError()) {
        emit this->error(reply.error().message());
    } else {
        qCDebug(l) << QString("Received successful reply for member: ") + reply.reply().member();
    }

    watcher->deleteLater();
}

void VoiceCallManager::onPendingSilenceFinished(QDBusPendingCallWatcher *watcher)
{
    QDBusPendingReply<> reply = *watcher;

    if (reply.isError()) {
        emit this->error(reply.error().message());
    } else {
        qCDebug(l) << QString("Received successful reply for member: ") + reply.reply().member();
    }

    watcher->deleteLater();
}