summaryrefslogtreecommitdiff
path: root/TelepathyQt/base-call.cpp
blob: 2d7d151c974bb71c3c6c9a5b3e265ebd6feb52fc (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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/**
 * This file is part of TelepathyQt
 *
 * @copyright Copyright (C) 2013 Matthias Gehre <gehre.matthias@gmail.com>
 * @copyright Copyright 2013 Canonical Ltd.
 * @license LGPL 2.1
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <TelepathyQt/BaseCall>
#include <TelepathyQt/BaseChannel>
#include "TelepathyQt/base-call-internal.h"

#include "TelepathyQt/_gen/base-call.moc.hpp"
#include "TelepathyQt/_gen/base-call-internal.moc.hpp"

#include "TelepathyQt/debug-internal.h"

#include <TelepathyQt/BaseConnection>
#include <TelepathyQt/Constants>
#include <TelepathyQt/DBusObject>
#include <TelepathyQt/Utils>
#include <TelepathyQt/AbstractProtocolInterface>
#include <QString>
#include <QVariantMap>

namespace Tp
{

/**
 * \class AbstractCallContentInterface
 * \ingroup servicechannel
 * \headerfile TelepathyQt/base-call.h <TelepathyQt/BaseCall>
 *
 * \brief Base class for all the CallContent object interface implementations.
 */

AbstractCallContentInterface::AbstractCallContentInterface(const QString &interfaceName)
    : AbstractDBusServiceInterface(interfaceName)
{
}

AbstractCallContentInterface::~AbstractCallContentInterface()
{
}

BaseCallContent::Adaptee::Adaptee(const QDBusConnection &dbusConnection,
                              BaseCallContent *content)
    : QObject(content),
      mContent(content)
{
    debug() << "Creating service::CallContentAdaptor for " << content->dbusObject();
    mAdaptor = new Service::CallContentAdaptor(dbusConnection, this, content->dbusObject());
}

QStringList BaseCallContent::Adaptee::interfaces() const
{
    QStringList ret;
    foreach(const AbstractCallContentInterfacePtr & iface, mContent->interfaces()) {
        ret << iface->interfaceName();
    }
    ret << TP_QT_IFACE_PROPERTIES;
    return ret;
}

BaseCallContent::Adaptee::~Adaptee()
{
}

void BaseCallContent::Adaptee::remove(const Tp::Service::CallContentAdaptor::RemoveContextPtr &context)
{
    context->setFinished();
}

struct TP_QT_NO_EXPORT BaseCallContent::Private {
    Private(BaseCallContent *parent,
            const QDBusConnection &dbusConnection,
            BaseChannel* channel,
            const QString &name,
            const Tp::MediaStreamType &type,
            const Tp::MediaStreamDirection &direction)
        : parent(parent),
          channel(channel),
          name(name),
          type(type),
          disposition(Tp::CallContentDispositionNone),
          direction(direction),
          adaptee(new BaseCallContent::Adaptee(dbusConnection, parent)) {
    }

    BaseCallContent *parent;
    BaseChannel *channel;

    QString name;
    Tp::MediaStreamType type;
    Tp::CallContentDisposition disposition;
    Tp::ObjectPathList streams;

    Tp::MediaStreamDirection direction;

    QHash<QString, AbstractCallContentInterfacePtr> interfaces;
    BaseCallContent::Adaptee *adaptee;
};

BaseCallContent::BaseCallContent(const QDBusConnection &dbusConnection,
                         BaseChannel* channel,
                         const QString &name,
                         const Tp::MediaStreamType &type,
                         const Tp::MediaStreamDirection &direction)
    : DBusService(dbusConnection),
      mPriv(new Private(this, dbusConnection, channel,
                        name, type, direction))
{
}

BaseCallContent::~BaseCallContent()
{
    delete mPriv;
}

QVariantMap BaseCallContent::immutableProperties() const
{
    QVariantMap map;
    map.insert(TP_QT_IFACE_CALL_CONTENT + QLatin1String(".Interfaces"),
               QVariant::fromValue(mPriv->adaptee->interfaces()));
    map.insert(TP_QT_IFACE_CALL_CONTENT + QLatin1String(".Name"),
               QVariant::fromValue(mPriv->adaptee->name()));
    map.insert(TP_QT_IFACE_CALL_CONTENT + QLatin1String(".Type"),
               QVariant::fromValue((uint)mPriv->adaptee->type()));
    map.insert(TP_QT_IFACE_CALL_CONTENT + QLatin1String(".Disposition"),
               QVariant::fromValue((uint)mPriv->adaptee->disposition()));
    return map;
}

QString BaseCallContent::name() const {
    return mPriv->name;
}

Tp::MediaStreamType BaseCallContent::type() const {
    return mPriv->type;
}

Tp::CallContentDisposition BaseCallContent::disposition() const {
    return mPriv->disposition;
}

Tp::ObjectPathList BaseCallContent::streams() const {
    return mPriv->streams;
}

QString BaseCallContent::uniqueName() const
{
    return QString(QLatin1String("_%1")).arg((quintptr) this, 0, 16);
}

bool BaseCallContent::registerObject(DBusError *error)
{
    if (isRegistered()) {
        return true;
    }

    QString name = mPriv->name;
    QString busName = mPriv->channel->busName();
    QString objectPath = QString(QLatin1String("%1/%2"))
                         .arg(mPriv->channel->objectPath(), name);
    debug() << "Registering Content: busName: " << busName << " objectName: " << objectPath;
    DBusError _error;

    debug() << "CallContent: registering interfaces  at " << dbusObject();
    foreach(const AbstractCallContentInterfacePtr & iface, mPriv->interfaces) {
        if (!iface->registerInterface(dbusObject())) {
            // lets not fail if an optional interface fails registering, lets warn only
            warning() << "Unable to register interface" << iface->interfaceName();
        }
    }

    bool ret = registerObject(busName, objectPath, &_error);
    if (!ret && error) {
        error->set(_error.name(), _error.message());
    }
    return ret;
}

/**
 * Reimplemented from DBusService.
 */
bool BaseCallContent::registerObject(const QString &busName,
                                 const QString &objectPath, DBusError *error)
{
    return DBusService::registerObject(busName, objectPath, error);
}

QList<AbstractCallContentInterfacePtr> BaseCallContent::interfaces() const
{
    return mPriv->interfaces.values();
}

bool BaseCallContent::plugInterface(const AbstractCallContentInterfacePtr &interface)
{
    if (isRegistered()) {
        warning() << "Unable to plug protocol interface " << interface->interfaceName() <<
                  "- protocol already registered";
        return false;
    }

    if (interface->isRegistered()) {
        warning() << "Unable to plug protocol interface" << interface->interfaceName() <<
                  "- interface already registered";
        return false;
    }

    if (mPriv->interfaces.contains(interface->interfaceName())) {
        warning() << "Unable to plug protocol interface" << interface->interfaceName() <<
                  "- another interface with same name already plugged";
        return false;
    }

    debug() << "Interface" << interface->interfaceName() << "plugged";
    mPriv->interfaces.insert(interface->interfaceName(), interface);
    return true;
}

// Call.I.Mute
BaseCallMuteInterface::Adaptee::Adaptee(BaseCallMuteInterface *interface)
    : QObject(interface),
      mInterface(interface)
{
}

BaseCallMuteInterface::Adaptee::~Adaptee()
{
}

struct TP_QT_NO_EXPORT BaseCallMuteInterface::Private {
    Private(BaseCallMuteInterface *parent, Tp::LocalMuteState state)
        : state(state),
          adaptee(new BaseCallMuteInterface::Adaptee(parent)) {
    }

    SetMuteStateCallback setMuteStateCB;
    Tp::LocalMuteState state;
    BaseCallMuteInterface::Adaptee *adaptee;
};

void BaseCallMuteInterface::Adaptee::requestMuted(bool mute, const Tp::Service::CallInterfaceMuteAdaptor::RequestMutedContextPtr &context)
{
    if (!mInterface->mPriv->setMuteStateCB.isValid()) {
        context->setFinishedWithError(TP_QT_ERROR_NOT_IMPLEMENTED, QLatin1String("Not implemented"));
        return;
    }

    Tp::LocalMuteState state = Tp::LocalMuteStateUnmuted;
    if (mute) {
        state = Tp::LocalMuteStateMuted;
    }

    DBusError error;
    mInterface->mPriv->setMuteStateCB(state, &error);
    if (error.isValid()) {
        context->setFinishedWithError(error.name(), error.message());
        return;
    }
    context->setFinished();
}


/**
 * \class BaseCallMuteInterface
 * \ingroup servicechannel
 * \headerfile TelepathyQt/base-call.h <TelepathyQt/BaseCall>
 *
 * \brief Base class for implementations of Call.Interface.Mute
 *
 */

/**
 * Class constructor.
 */
BaseCallMuteInterface::BaseCallMuteInterface()
    : AbstractChannelInterface(TP_QT_IFACE_CALL_INTERFACE_MUTE),
      mPriv(new Private(this, Tp::LocalMuteStateUnmuted))
{
}

Tp::LocalMuteState BaseCallMuteInterface::localMuteState() const
{
    return mPriv->state;
}

void BaseCallMuteInterface::setMuteState(const Tp::LocalMuteState &state)
{
    if (mPriv->state != state) {
        mPriv->state = state;
        QMetaObject::invokeMethod(mPriv->adaptee, "muteStateChanged", Q_ARG(uint, state));
    }
}

void BaseCallMuteInterface::setSetMuteStateCallback(const SetMuteStateCallback &cb)
{
    mPriv->setMuteStateCB = cb;
}

/**
 * Class destructor.
 */
BaseCallMuteInterface::~BaseCallMuteInterface()
{
    delete mPriv;
}

/**
 * Return the immutable properties of this interface.
 *
 * Immutable properties cannot change after the interface has been registered
 * on a service on the bus with registerInterface().
 *
 * \return The immutable properties of this interface.
 */
QVariantMap BaseCallMuteInterface::immutableProperties() const
{
    QVariantMap map;
    return map;
}

void BaseCallMuteInterface::createAdaptor()
{
    (void) new Service::CallInterfaceMuteAdaptor(dbusObject()->dbusConnection(),
            mPriv->adaptee, dbusObject());
}

// Call.Content.Interface.DTMF
BaseCallContentDTMFInterface::Adaptee::Adaptee(BaseCallContentDTMFInterface *interface)
    : QObject(interface),
      mInterface(interface)
{
}

BaseCallContentDTMFInterface::Adaptee::~Adaptee()
{
}

struct TP_QT_NO_EXPORT BaseCallContentDTMFInterface::Private {
    Private(BaseCallContentDTMFInterface *parent)
        : currentlySendingTones(false),
          adaptee(new BaseCallContentDTMFInterface::Adaptee(parent)) {
    }

    StartToneCallback startToneCB;
    StopToneCallback stopToneCB;
    MultipleTonesCallback multipleTonesCB;
    bool currentlySendingTones;
    QString deferredTones;
    BaseCallContentDTMFInterface::Adaptee *adaptee;
};

void BaseCallContentDTMFInterface::Adaptee::startTone(uchar event, const Tp::Service::CallContentInterfaceDTMFAdaptor::StartToneContextPtr &context)
{
    if (!mInterface->mPriv->startToneCB.isValid()) {
        context->setFinishedWithError(TP_QT_ERROR_NOT_IMPLEMENTED, QLatin1String("Not implemented"));
        return;
    }

    DBusError error;
    mInterface->mPriv->startToneCB(event, &error);
    if (error.isValid()) {
        context->setFinishedWithError(error.name(), error.message());
        return;
    }
    context->setFinished();
}

void BaseCallContentDTMFInterface::Adaptee::stopTone(const Tp::Service::CallContentInterfaceDTMFAdaptor::StopToneContextPtr &context)
{
    if (!mInterface->mPriv->stopToneCB.isValid()) {
        context->setFinishedWithError(TP_QT_ERROR_NOT_IMPLEMENTED, QLatin1String("Not implemented"));
        return;
    }

    DBusError error;
    mInterface->mPriv->stopToneCB(&error);
    if (error.isValid()) {
        context->setFinishedWithError(error.name(), error.message());
        return;
    }
    context->setFinished();
}


void BaseCallContentDTMFInterface::Adaptee::multipleTones(const QString& tones, const Tp::Service::CallContentInterfaceDTMFAdaptor::MultipleTonesContextPtr &context)
{
    if (!mInterface->mPriv->multipleTonesCB.isValid()) {
        context->setFinishedWithError(TP_QT_ERROR_NOT_IMPLEMENTED, QLatin1String("Not implemented"));
        return;
    }

    DBusError error;
    mInterface->mPriv->multipleTonesCB(tones, &error);
    if (error.isValid()) {
        context->setFinishedWithError(error.name(), error.message());
        return;
    }
    context->setFinished();
}

/**
 * \class BaseCallContentDTMFInterface
 * \ingroup servicechannel
 * \headerfile TelepathyQt/base-call.h <TelepathyQt/BaseCall>
 *
 * \brief Base class for implementations of Call.Content.Interface.DTMF
 *
 */

/**
 * Class constructor.
 */
BaseCallContentDTMFInterface::BaseCallContentDTMFInterface()
    : AbstractCallContentInterface(TP_QT_IFACE_CALL_CONTENT_INTERFACE_DTMF),
      mPriv(new Private(this))
{
}

bool BaseCallContentDTMFInterface::currentlySendingTones() const
{
    return mPriv->currentlySendingTones;
}

void BaseCallContentDTMFInterface::setCurrentlySendingTones(bool sending)
{
    mPriv->currentlySendingTones = sending;
}

QString BaseCallContentDTMFInterface::deferredTones() const
{
    return mPriv->deferredTones;
}

void BaseCallContentDTMFInterface::setDeferredTones(const QString &tones)
{
    mPriv->deferredTones = tones;
}


void BaseCallContentDTMFInterface::setStartToneCallback(const StartToneCallback &cb)
{
    mPriv->startToneCB = cb;
}

void BaseCallContentDTMFInterface::setStopToneCallback(const StopToneCallback &cb)
{
    mPriv->stopToneCB = cb;
}


void BaseCallContentDTMFInterface::setMultipleTonesCallback(const MultipleTonesCallback &cb)
{
    mPriv->multipleTonesCB = cb;
}

/**
 * Class destructor.
 */
BaseCallContentDTMFInterface::~BaseCallContentDTMFInterface()
{
    delete mPriv;
}

/**
 * Return the immutable properties of this interface.
 *
 * Immutable properties cannot change after the interface has been registered
 * on a service on the bus with registerInterface().
 *
 * \return The immutable properties of this interface.
 */
QVariantMap BaseCallContentDTMFInterface::immutableProperties() const
{
    QVariantMap map;
    return map;
}

void BaseCallContentDTMFInterface::createAdaptor()
{
    (void) new Service::CallContentInterfaceDTMFAdaptor(dbusObject()->dbusConnection(),
            mPriv->adaptee, dbusObject());
}

}