forked from sailfishos/voicecall
/
telepathyprovider.cpp
419 lines (349 loc) · 14.5 KB
/
telepathyprovider.cpp
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
* This file is a part of the Voice Call Manager project
*
* Copyright (C) 2011-2015 Tom Swindell <tom.swindell@jollamobile.com>
*
* 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 "telepathyprovider.h"
#include "callchannelhandler.h"
#include "streamchannelhandler.h"
#include <TelepathyQt/CallChannel>
#include <TelepathyQt/StreamedMediaChannel>
#include <TelepathyQt/PendingReady>
#include <TelepathyQt/PendingChannel>
#include <TelepathyQt/PendingChannelRequest>
class TelepathyProviderPrivate
{
Q_DECLARE_PUBLIC(TelepathyProvider)
public:
TelepathyProviderPrivate(Tp::AccountPtr a, VoiceCallManagerInterface *m, TelepathyProvider *q)
: q_ptr(q), manager(m), account(a),
tpChannelRequest(NULL)
{ /* ... */ }
TelepathyProvider *q_ptr;
VoiceCallManagerInterface *manager;
Tp::AccountPtr account;
QString errorString;
QHash<QString,BaseChannelHandler*> voiceCalls;
Tp::PendingChannelRequest *tpChannelRequest;
bool shouldForceReconnect() const;
};
TelepathyProvider::TelepathyProvider(Tp::AccountPtr account, VoiceCallManagerInterface *manager, QObject *parent)
: AbstractVoiceCallProvider(parent),
d_ptr(new TelepathyProviderPrivate(account, manager, this))
{
TRACE
QObject::connect(account.data()->becomeReady(), SIGNAL(finished(Tp::PendingOperation*)), SLOT(onAccountBecomeReady(Tp::PendingOperation*)));
}
TelepathyProvider::~TelepathyProvider()
{
TRACE
delete d_ptr;
}
QString TelepathyProvider::errorString() const
{
TRACE
Q_D(const TelepathyProvider);
return d->errorString;
}
QString TelepathyProvider::providerId() const
{
TRACE
Q_D(const TelepathyProvider);
return d->account->objectPath();
}
QString TelepathyProvider::providerType() const
{
TRACE
Q_D(const TelepathyProvider);
return d->account.data()->protocolName();
}
QList<AbstractVoiceCallHandler*> TelepathyProvider::voiceCalls() const
{
TRACE
Q_D(const TelepathyProvider);
QList<AbstractVoiceCallHandler*> calls;
foreach (BaseChannelHandler *handler, d->voiceCalls)
calls << handler;
return calls;
}
BaseChannelHandler *TelepathyProvider::voiceCall(const QString &handlerId) const
{
TRACE
Q_D(const TelepathyProvider);
return d->voiceCalls.value(handlerId);
}
BaseChannelHandler *TelepathyProvider::voiceCall(Tp::ChannelPtr channel) const
{
Q_D(const TelepathyProvider);
foreach (BaseChannelHandler *handler, d->voiceCalls) {
if (handler->channel()->objectPath() == channel->objectPath()) {
return handler;
}
}
return 0;
}
bool TelepathyProvider::dial(const QString &msisdn)
{
TRACE
Q_D(TelepathyProvider);
if (d->tpChannelRequest) {
d->errorString = "Can't initiate a call when one is pending!";
WARNING_T("%s", qPrintable(d->errorString));
emit this->error(d->errorString);
return false;
}
if (d->account->protocolName() == "sip" || d->account->protocolName() == "jabber") {
d->tpChannelRequest = d->account->ensureAudioCall(msisdn, QString(), QDateTime::currentDateTime(),
TP_QT_IFACE_CLIENT + ".voicecall");
} else if (d->account->protocolName() == "tel") {
d->tpChannelRequest = d->account->ensureStreamedMediaAudioCall(msisdn, QDateTime::currentDateTime(),
TP_QT_IFACE_CLIENT + ".voicecall");
} else {
d->errorString = "Attempting to dial an unknown protocol";
WARNING_T("%s", qPrintable(d->errorString));
emit this->error(d->errorString);
return false;
}
QObject::connect(d->tpChannelRequest,
SIGNAL(finished(Tp::PendingOperation*)),
SLOT(onPendingRequestFinished(Tp::PendingOperation*)));
QObject::connect(d->tpChannelRequest,
SIGNAL(channelRequestCreated(Tp::ChannelRequestPtr)),
SLOT(onChannelRequestCreated(Tp::ChannelRequestPtr)));
return true;
}
bool TelepathyProvider::createConference(Tp::ChannelPtr channel1, Tp::ChannelPtr channel2)
{
TRACE
Q_D(TelepathyProvider);
if (d->tpChannelRequest) {
d->errorString = "Can't initiate a call when one is pending!";
WARNING_T("%s", qPrintable(d->errorString));
emit this->error(d->errorString);
return false;
}
if (d->account->protocolName() == "sip") {
WARNING_T("Conference calls not supported for SIP protocol");
} else if (d->account->protocolName() == "tel") {
QList<Tp::ChannelPtr> channels;
channels << channel1 << channel2;
WARNING_T("Create conference call");
d->tpChannelRequest = d->account->createConferenceStreamedMediaCall(channels, QStringList(), QDateTime::currentDateTime(), TP_QT_IFACE_CLIENT + ".voicecall");
} else {
d->errorString = "Attempting to create conference on an unknown protocol";
WARNING_T("%s", qPrintable(d->errorString));
emit this->error(d->errorString);
return false;
}
QObject::connect(d->tpChannelRequest,
SIGNAL(finished(Tp::PendingOperation*)),
SLOT(onPendingRequestFinished(Tp::PendingOperation*)));
QObject::connect(d->tpChannelRequest,
SIGNAL(channelRequestCreated(Tp::ChannelRequestPtr)),
SLOT(onChannelRequestCreated(Tp::ChannelRequestPtr)));
return true;
}
void TelepathyProvider::updateConferenceHoldState()
{
Q_D(TelepathyProvider);
BaseChannelHandler *confHandler = conferenceHandler();
if (confHandler) {
AbstractVoiceCallHandler::VoiceCallStatus status = AbstractVoiceCallHandler::STATUS_NULL;
for (auto it = d->voiceCalls.begin(); it != d->voiceCalls.end(); ++it) {
BaseChannelHandler *callHandler = *it;
if (callHandler != confHandler && callHandler->parentHandlerId() == confHandler->handlerId()) {
if (status == AbstractVoiceCallHandler::STATUS_NULL)
status = callHandler->status();
else if (status != callHandler->status())
return;
}
}
if (status != AbstractVoiceCallHandler::STATUS_NULL) {
StreamChannelHandler *streamHandler = qobject_cast<StreamChannelHandler*>(confHandler);
if (streamHandler)
streamHandler->getHoldState();
}
}
}
void TelepathyProvider::onAccountBecomeReady(Tp::PendingOperation *op)
{
TRACE
Q_D(TelepathyProvider);
if (op->isError()) {
WARNING_T("Operation failed: %s: %s", qPrintable(op->errorName()), qPrintable(op->errorMessage()));
d->errorString = QString("Telepathy Operation Failed: %1 - %2").arg(op->errorName(), op->errorMessage());
emit this->error(d->errorString);
return;
}
DEBUG_T("Account %s became ready.", qPrintable(d->account.data()->uniqueIdentifier()));
QObject::connect(d->account.data(), SIGNAL(stateChanged(bool)), SLOT(onAccountAvailabilityChanged()));
QObject::connect(d->account.data(), SIGNAL(onlinenessChanged(bool)), SLOT(onAccountAvailabilityChanged()));
QObject::connect(d->account.data(), SIGNAL(connectionStatusChanged(Tp::ConnectionStatus)), SLOT(onAccountAvailabilityChanged()));
this->onAccountAvailabilityChanged();
}
void TelepathyProvider::onAccountAvailabilityChanged()
{
TRACE
Q_D(TelepathyProvider);
if (d->account.data()->isEnabled() && d->account.data()->isOnline()
&& d->account.data()->connectionStatus() == Tp::ConnectionStatusConnected) {
/* Test for ring */
if (d->shouldForceReconnect()) {
// If telepathy-ring, set anonymity properties here
auto conn = d->account->connection();
auto anon = new Tp::Client::ConnectionInterfaceAnonymityInterface(Tp::Client::ConnectionInterface(conn->busName(), conn->objectPath()));
qDebug("Setting anonymity modes!");
auto pending = anon->setPropertyAnonymityModes(2);
QObject::connect(pending,
SIGNAL(finished(Tp::PendingOperation*)),
SLOT(onPendingRequestFinished(Tp::PendingOperation*)));
auto pending2 = anon->setPropertyAnonymityMandatory(false);
QObject::connect(pending2,
SIGNAL(finished(Tp::PendingOperation*)),
SLOT(onPendingRequestFinished(Tp::PendingOperation*)));
}
d->manager->appendProvider(this);
} else {
d->manager->removeProvider(this);
if (d->shouldForceReconnect()) {
WARNING_T("Forcing account %s back online immediately", qPrintable(d->account.data()->uniqueIdentifier()));
d->account->setRequestedPresence(Tp::Presence::available());
}
}
}
void TelepathyProvider::createHandler(Tp::ChannelPtr ch, const QDateTime &userActionTime)
{
TRACE
Q_D(TelepathyProvider);
BaseChannelHandler *handler = 0;
DEBUG_T("\tProcessing channel: %s", qPrintable(ch->objectPath()));
Tp::CallChannelPtr callChannel = Tp::CallChannelPtr::dynamicCast(ch);
if (callChannel && !callChannel.isNull()) {
DEBUG_T("Found CallChannel interface.");
handler = new CallChannelHandler(d->manager->generateHandlerId(), callChannel, userActionTime, this);
}
Tp::StreamedMediaChannelPtr streamChannel = Tp::StreamedMediaChannelPtr::dynamicCast(ch);
if (streamChannel && !streamChannel.isNull()) {
DEBUG_T("Found StreamedMediaChannel interface.");
handler = new StreamChannelHandler(d->manager->generateHandlerId(), streamChannel, userActionTime, this);
connect(handler, &BaseChannelHandler::channelMerged, this, &TelepathyProvider::onChannelMerged);
connect(handler, &BaseChannelHandler::channelRemoved, this, &TelepathyProvider::onChannelRemoved);
connect(streamChannel.data(), &Tp::Channel::conferenceChannelMerged, this, &TelepathyProvider::onChannelMerged);
connect(streamChannel.data(), &Tp::Channel::conferenceChannelRemoved,
this, [this](const Tp::ChannelPtr &channel, const Tp::Channel::GroupMemberChangeDetails &) {
onChannelRemoved(channel);
});
}
if (!handler)
return;
d->voiceCalls.insert(handler->handlerId(), handler);
QObject::connect(handler, SIGNAL(error(QString)), SIGNAL(error(QString)));
QObject::connect(handler, SIGNAL(invalidated(QString,QString)), SLOT(onHandlerInvalidated(QString,QString)));
emit this->voiceCallAdded(handler);
emit this->voiceCallsChanged();
}
BaseChannelHandler *TelepathyProvider::conferenceHandler() const
{
Q_D(const TelepathyProvider);
for (auto it = d->voiceCalls.begin(); it != d->voiceCalls.end(); ++it) {
if ((*it)->isMultiparty()) {
return *it;
}
}
return 0;
}
void TelepathyProvider::onPendingRequestFinished(Tp::PendingOperation *op)
{
TRACE
Q_D(TelepathyProvider);
if (op != d->tpChannelRequest)
return;
if (op->isError()) {
WARNING_T("Operation failed: %s: %s", qPrintable(op->errorName()), qPrintable(op->errorMessage()));
d->errorString = QString("Telepathy Operation Failed: %1 - %2").arg(op->errorName(), op->errorMessage());
emit this->error(d->errorString);
}
d->tpChannelRequest = NULL;
}
void TelepathyProvider::onChannelRequestCreated(const Tp::ChannelRequestPtr &request)
{
TRACE
// There is no need to watch for success; the channel will be delivered to the handler.
// pendingRequestFinished (emitted after the request succeeds) will clean up the rest.
connect(request.data(), SIGNAL(failed(QString,QString)),
SLOT(onDialFailed(QString,QString)));
}
void TelepathyProvider::onDialFailed(const QString &errorName, const QString &errorMessage)
{
TRACE
Q_D(TelepathyProvider);
WARNING_T("Operation failed: %s: %s", qPrintable(errorName), qPrintable(errorMessage));
d->errorString = QString("Telepathy Operation Failed: %1 - %2").arg(errorName, errorMessage);
emit this->error(d->errorString);
// onPendingRequestFinished will clean up the request
}
void TelepathyProvider::onHandlerInvalidated(const QString &errorName, const QString &errorMessage)
{
TRACE
Q_D(TelepathyProvider);
BaseChannelHandler *handler = qobject_cast<BaseChannelHandler*>(QObject::sender());
d->voiceCalls.remove(handler->handlerId());
emit this->voiceCallRemoved(handler->handlerId());
emit this->voiceCallsChanged();
handler->deleteLater();
if (!errorName.isEmpty() || !errorMessage.isEmpty()) {
WARNING_T("Handler invalidated: %s: %s", qPrintable(errorName), qPrintable(errorMessage));
d->errorString = QString("Telepathy Handler Invalidated: %1 - %2").arg(errorName, errorMessage);
emit this->error(d->errorString);
}
}
void TelepathyProvider::onChannelMerged(Tp::ChannelPtr channel)
{
TRACE
BaseChannelHandler *confHandler = conferenceHandler();
if (!confHandler) {
WARNING_T("Channel merged, but no conference call exists");
return;
}
BaseChannelHandler *callHandler = voiceCall(channel);
if (!callHandler) {
WARNING_T("No call handler exists for: %s", qPrintable(channel->objectPath()));
return;
}
confHandler->addChildCall(callHandler);
}
void TelepathyProvider::onChannelRemoved(Tp::ChannelPtr channel)
{
TRACE
BaseChannelHandler *callHandler = voiceCall(channel);
if (!callHandler) {
WARNING_T("No call handler exists for: %s", qPrintable(channel->objectPath()));
return;
}
BaseChannelHandler *confHandler = conferenceHandler();
if (!confHandler) {
WARNING_T("Channel removed, but no conference call exists");
return;
}
confHandler->removeChildCall(callHandler);
}
bool TelepathyProviderPrivate::shouldForceReconnect() const
{
return account->cmName() == "ring";
}