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
|
/*
* Copyright (C) 2012-2014 Canonical, Ltd.
*
* Authors:
* Gustavo Pichorim Boiko <gustavo.boiko@canonical.com>
* Tiago Salem Herrmann <tiago.herrmann@canonical.com>
*
* This file is part of telephony-service.
*
* telephony-service 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; version 3.
*
* telephony-service 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, see <http://www.gnu.org/licenses/>.
*/
#include "callhandler.h"
#include "phoneutils.h"
#include "telepathyhelper.h"
#include "accountentry.h"
#include "tonegenerator.h"
#include "greetercontacts.h"
#include <TelepathyQt/ContactManager>
#include <TelepathyQt/PendingContacts>
#include <TelepathyQt/PendingChannelRequest>
#define TELEPATHY_MUTE_IFACE "org.freedesktop.Telepathy.Call1.Interface.Mute"
#define DBUS_PROPERTIES_IFACE "org.freedesktop.DBus.Properties"
typedef QMap<QString, QVariant> dbusQMap;
Q_DECLARE_METATYPE(dbusQMap)
CallHandler *CallHandler::instance()
{
static CallHandler *self = new CallHandler();
return self;
}
QVariantMap CallHandler::getCallProperties(const QString &objectPath)
{
QVariantMap properties;
Tp::CallChannelPtr channel = callFromObjectPath(objectPath);
if (!channel) {
return properties;
}
QVariant property = channel->property("timestamp");
if (property.isValid()) {
properties["timestamp"] = property;
}
property = channel->property("activeTimestamp");
if (property.isValid()) {
properties["activeTimestamp"] = property;
}
property = channel->property("dtmfString");
if (property.isValid()) {
properties["dtmfString"] = property;
}
return properties;
}
bool CallHandler::hasCalls() const
{
bool hasActiveCalls = false;
Q_FOREACH(const Tp::CallChannelPtr channel, mCallChannels) {
AccountEntry *accountEntry = TelepathyHelper::instance()->accountForConnection(channel->connection());
bool incoming = channel->initiatorContact() != accountEntry->account()->connection()->selfContact();
bool dialing = !incoming && (channel->callState() == Tp::CallStateInitialised);
bool active = channel->callState() == Tp::CallStateActive;
if (dialing || active) {
hasActiveCalls = true;
break;
}
}
return hasActiveCalls;
}
CallHandler::CallHandler(QObject *parent)
: QObject(parent),
mHangupRequested(false)
{
}
void CallHandler::startCall(const QString &targetId, const QString &accountId)
{
// Request the contact to start audio call
AccountEntry *accountEntry = TelepathyHelper::instance()->accountForId(accountId);
if (!accountEntry) {
return;
}
Tp::ConnectionPtr connection = accountEntry->account()->connection();
if (!connection) {
return;
}
connect(connection->contactManager()->contactsForIdentifiers(QStringList() << targetId),
SIGNAL(finished(Tp::PendingOperation*)),
SLOT(onContactsAvailable(Tp::PendingOperation*)));
}
void CallHandler::hangUpCall(const QString &objectPath)
{
Tp::CallChannelPtr channel = callFromObjectPath(objectPath);
if (channel.isNull()) {
return;
}
Tp::PendingOperation *pending = channel->hangup();
mClosingChannels[pending] = channel;
connect(pending,
SIGNAL(finished(Tp::PendingOperation*)),
SLOT(onCallHangupFinished(Tp::PendingOperation*)));
}
void CallHandler::setHold(const QString &objectPath, bool hold)
{
Tp::CallChannelPtr channel = callFromObjectPath(objectPath);
if (channel.isNull()) {
return;
}
Tp::PendingOperation *op = channel->requestHold(hold);
connect(op, &Tp::PendingOperation::finished, [this, objectPath, op] {
if (op->isError()) {
Q_EMIT callHoldingFailed(objectPath);
}
});
}
void CallHandler::setMuted(const QString &objectPath, bool muted)
{
Tp::CallChannelPtr channel = callFromObjectPath(objectPath);
if (channel.isNull()) {
return;
}
// FIXME: replace by a proper TpQt implementation of mute
QDBusInterface muteInterface(channel->busName(), channel->objectPath(), TELEPATHY_MUTE_IFACE);
muteInterface.call("RequestMuted", muted);
}
void CallHandler::setActiveAudioOutput(const QString &objectPath, const QString &id)
{
Tp::CallChannelPtr channel = callFromObjectPath(objectPath);
QDBusInterface audioOutputsInterface(channel->busName(), channel->objectPath(), CANONICAL_TELEPHONY_AUDIOOUTPUTS_IFACE);
audioOutputsInterface.call("SetActiveAudioOutput", id);
}
void CallHandler::sendDTMF(const QString &objectPath, const QString &key)
{
bool ok;
Tp::DTMFEvent event = (Tp::DTMFEvent)key.toInt(&ok);
if (!ok) {
if (!key.compare("*")) {
event = Tp::DTMFEventAsterisk;
} else if (!key.compare("#")) {
event = Tp::DTMFEventHash;
} else {
qWarning() << "Tone not recognized. DTMF failed";
return;
}
}
/*
* play locally (via tone generator) only if we are on a call, or if this is
* dialpad sounds
*/
if (GreeterContacts::instance()->dialpadSoundsEnabled() &&
!GreeterContacts::instance()->silentMode() && objectPath.isEmpty()
|| !objectPath.isEmpty()) {
ToneGenerator::instance()->playDTMFTone((uint)event);
}
Tp::CallChannelPtr channel = callFromObjectPath(objectPath);
if (channel.isNull()) {
return;
}
// save the dtmfString to send to clients that request it
QString dtmfString = channel->property("dtmfString").toString();
dtmfString += key;
channel->setProperty("dtmfString", dtmfString);
Q_FOREACH(const Tp::CallContentPtr &content, channel->contents()) {
if (content->supportsDTMF()) {
/* send DTMF to network (via telepathy and oFono) */
content->startDTMFTone(event);
}
}
Q_EMIT callPropertiesChanged(channel->objectPath(), getCallProperties(channel->objectPath()));
}
void CallHandler::createConferenceCall(const QStringList &objectPaths)
{
QList<Tp::ChannelPtr> calls;
AccountEntry *accountEntry = 0;
Q_FOREACH(const QString &objectPath, objectPaths) {
Tp::CallChannelPtr call = callFromObjectPath(objectPath);
if (!call) {
qWarning() << "Could not find a call channel for objectPath:" << objectPath;
return;
}
if (!accountEntry) {
accountEntry = TelepathyHelper::instance()->accountForConnection(call->connection());
}
// make sure all call channels belong to the same connection
if (call->connection() != accountEntry->account()->connection()) {
qWarning() << "It is not possible to merge channels from different accounts.";
return;
}
calls.append(call);
}
if (calls.isEmpty() || !accountEntry) {
qWarning() << "The list of calls was empty. Failed to create a conference.";
return;
}
// there is no need to check the pending request. The new channel will arrive at some point.
Tp::PendingChannelRequest *pcr = accountEntry->account()->createConferenceCall(calls, QStringList(), QDateTime::currentDateTime(),
TP_QT_IFACE_CLIENT + ".TelephonyServiceHandler");
connect(pcr, &Tp::PendingChannelRequest::finished, [this, pcr] {
Q_EMIT conferenceCallRequestFinished(!pcr->isError());
});
}
void CallHandler::mergeCall(const QString &conferenceObjectPath, const QString &callObjectPath)
{
Tp::CallChannelPtr conferenceChannel = callFromObjectPath(conferenceObjectPath);
Tp::CallChannelPtr callChannel = callFromObjectPath(callObjectPath);
if (!conferenceChannel || !callChannel || !conferenceChannel->isConference()) {
qWarning() << "No valid channels found.";
return;
}
// there is no need to check for the result here.
conferenceChannel->conferenceMergeChannel(callChannel);
}
void CallHandler::splitCall(const QString &objectPath)
{
Tp::CallChannelPtr channel = callFromObjectPath(objectPath);
if (!channel) {
return;
}
// we don't need to check the result of the operation here
channel->conferenceSplitChannel();
}
void CallHandler::onCallChannelAvailable(Tp::CallChannelPtr channel)
{
QDBusInterface callChannelIface(channel->busName(), channel->objectPath(), DBUS_PROPERTIES_IFACE);
QDBusMessage reply = callChannelIface.call("GetAll", CANONICAL_TELEPHONY_AUDIOOUTPUTS_IFACE);
QVariantList args = reply.arguments();
QMap<QString, QVariant> map = qdbus_cast<QMap<QString, QVariant> >(args[0]);
channel->setProperty("timestamp", QDateTime::currentDateTimeUtc());
if (channel->callState() == Tp::CallStateActive) {
channel->setProperty("activeTimestamp", QDateTime::currentDateTimeUtc());
}
connect(channel.data(),
SIGNAL(invalidated(Tp::DBusProxy*,QString,QString)),
SLOT(onCallChannelInvalidated()));
connect(channel.data(),
SIGNAL(callStateChanged(Tp::CallState)),
SLOT(onCallStateChanged(Tp::CallState)));
mCallChannels.append(channel);
Q_EMIT callPropertiesChanged(channel->objectPath(), getCallProperties(channel->objectPath()));
}
void CallHandler::onContactsAvailable(Tp::PendingOperation *op)
{
Tp::PendingContacts *pc = qobject_cast<Tp::PendingContacts*>(op);
if (!pc) {
qCritical() << "The pending object is not a Tp::PendingContacts";
return;
}
AccountEntry *accountEntry = TelepathyHelper::instance()->accountForConnection(pc->manager()->connection());
// start call to the contacts
Q_FOREACH(Tp::ContactPtr contact, pc->contacts()) {
accountEntry->account()->ensureAudioCall(contact, QLatin1String("audio"), QDateTime::currentDateTime(), TP_QT_IFACE_CLIENT + ".TelephonyServiceHandler");
// hold the ContactPtr to make sure its refcounting stays bigger than 0
mContacts[contact->id()] = contact;
}
}
void CallHandler::onCallHangupFinished(Tp::PendingOperation *op)
{
if (!mClosingChannels.contains(op)) {
qCritical() << "Channel for pending hangup not found:" << op;
return;
}
// Do NOT request the channel closing at this point. It will get closed automatically.
// if you request it to be closed, the CallStateEnded will never be reached and the UI
// and logging will be broken.
Tp::CallChannelPtr channel = mClosingChannels.take(op);
if (mCallChannels.count() == 1) {
mHangupRequested = true;
}
}
void CallHandler::onCallChannelInvalidated()
{
Tp::CallChannelPtr channel(qobject_cast<Tp::CallChannel*>(sender()));
if (channel.isNull()) {
return;
}
mCallChannels.removeAll(channel);
if (mCallChannels.isEmpty() && !mHangupRequested) {
ToneGenerator::instance()->playCallEndedTone();
}
mHangupRequested = false;
}
void CallHandler::onCallStateChanged(Tp::CallState state)
{
Tp::CallChannelPtr channel(qobject_cast<Tp::CallChannel*>(sender()));
if (!channel) {
return;
}
switch (state) {
case Tp::CallStateActive:
channel->setProperty("activeTimestamp", QDateTime::currentDateTimeUtc());
Q_EMIT callPropertiesChanged(channel->objectPath(), getCallProperties(channel->objectPath()));
break;
}
}
Tp::CallChannelPtr CallHandler::existingCall(const QString &targetId)
{
Tp::CallChannelPtr channel;
Q_FOREACH(const Tp::CallChannelPtr &ch, mCallChannels) {
if (ch->isConference()) {
continue;
}
AccountEntry *account = TelepathyHelper::instance()->accountForConnection(ch->connection());
if (!account) {
continue;
}
if (account->compareIds(ch->targetContact()->id(), targetId)) {
channel = ch;
break;
}
}
return channel;
}
Tp::CallChannelPtr CallHandler::callFromObjectPath(const QString &objectPath)
{
Tp::CallChannelPtr channel;
Q_FOREACH(const Tp::CallChannelPtr &ch, mCallChannels) {
if (ch->objectPath() == objectPath) {
channel = ch;
break;
}
}
return channel;
}
|