File: call-stream.cpp

package info (click to toggle)
telepathy-qt 0.9.1-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 38,764 kB
  • sloc: cpp: 59,914; xml: 34,543; ansic: 21,194; python: 3,370; sh: 118; makefile: 20
file content (499 lines) | stat: -rw-r--r-- 17,424 bytes parent folder | download | duplicates (5)
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
/*
 * This file is part of TelepathyQt
 *
 * @copyright Copyright (C) 2010-2012 Collabora Ltd. <http://www.collabora.co.uk/>
 * @copyright Copyright (C) 2012 Nokia Corporation
 * @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/CallStream>

#include "TelepathyQt/_gen/call-stream.moc.hpp"

#include "TelepathyQt/_gen/cli-call-stream-body.hpp"
#include "TelepathyQt/_gen/cli-call-stream.moc.hpp"

#include <TelepathyQt/debug-internal.h>

#include <TelepathyQt/CallChannel>
#include <TelepathyQt/Connection>
#include <TelepathyQt/ConnectionLowlevel>
#include <TelepathyQt/ContactManager>
#include <TelepathyQt/DBus>
#include <TelepathyQt/PendingContacts>
#include <TelepathyQt/PendingVoid>
#include <TelepathyQt/PendingVariantMap>
#include <TelepathyQt/ReadinessHelper>

namespace Tp
{

struct TP_QT_NO_EXPORT CallStream::Private
{
    Private(CallStream *parent, const CallContentPtr &content);

    static void introspectMainProperties(Private *self);

    void processRemoteMembersChanged();

    struct RemoteMembersChangedInfo;

    // Public object
    CallStream *parent;

    WeakPtr<CallContent> content;

    // Mandatory proxies
    Client::CallStreamInterface *streamInterface;

    ReadinessHelper *readinessHelper;

    // Introspection
    uint localSendingState;
    ContactSendingStateMap remoteMembers;
    QHash<uint, ContactPtr> remoteMembersContacts;
    bool canRequestReceiving;
    QQueue< QSharedPointer<RemoteMembersChangedInfo> > remoteMembersChangedQueue;
    QSharedPointer<RemoteMembersChangedInfo> currentRemoteMembersChangedInfo;
};

struct TP_QT_NO_EXPORT CallStream::Private::RemoteMembersChangedInfo
{
    RemoteMembersChangedInfo(const ContactSendingStateMap &updates,
            const HandleIdentifierMap &identifiers,
            const UIntList &removed,
            const CallStateReason &reason)
        : updates(updates),
          identifiers(identifiers),
          removed(removed),
          reason(reason)
    {
    }

    static QSharedPointer<RemoteMembersChangedInfo> create(
            const ContactSendingStateMap &updates,
            const HandleIdentifierMap &identifiers,
            const UIntList &removed,
            const CallStateReason &reason)
    {
        RemoteMembersChangedInfo *info = new RemoteMembersChangedInfo(
                updates, identifiers, removed, reason);
        return QSharedPointer<RemoteMembersChangedInfo>(info);
    }

    ContactSendingStateMap updates;
    HandleIdentifierMap identifiers;
    UIntList removed;
    CallStateReason reason;
};

CallStream::Private::Private(CallStream *parent, const CallContentPtr &content)
    : parent(parent),
      content(content.data()),
      streamInterface(parent->interface<Client::CallStreamInterface>()),
      readinessHelper(parent->readinessHelper()),
      localSendingState(SendingStateNone),
      canRequestReceiving(true)
{
    ReadinessHelper::Introspectables introspectables;

    ReadinessHelper::Introspectable introspectableCore(
        QSet<uint>() << 0,                                                        // makesSenseForStatuses
        Features(),                                                           // dependsOnFeatures
        QStringList(),                                                            // dependsOnInterfaces
        (ReadinessHelper::IntrospectFunc) &Private::introspectMainProperties,
        this);
    introspectables[FeatureCore] = introspectableCore;

    readinessHelper->addIntrospectables(introspectables);
    readinessHelper->becomeReady(FeatureCore);
}

void CallStream::Private::introspectMainProperties(CallStream::Private *self)
{
    CallStream *parent = self->parent;

    parent->connect(self->streamInterface,
            SIGNAL(LocalSendingStateChanged(uint,Tp::CallStateReason)),
            SLOT(onLocalSendingStateChanged(uint,Tp::CallStateReason)));
    parent->connect(self->streamInterface,
            SIGNAL(RemoteMembersChanged(Tp::ContactSendingStateMap,Tp::HandleIdentifierMap,Tp::UIntList,Tp::CallStateReason)),
            SLOT(onRemoteMembersChanged(Tp::ContactSendingStateMap,Tp::HandleIdentifierMap,Tp::UIntList,Tp::CallStateReason)));

    parent->connect(self->streamInterface->requestAllProperties(),
            SIGNAL(finished(Tp::PendingOperation*)),
            SLOT(gotMainProperties(Tp::PendingOperation*)));
}

void CallStream::Private::processRemoteMembersChanged()
{
    if (currentRemoteMembersChangedInfo) { // currently building contacts
        return;
    }

    if (remoteMembersChangedQueue.isEmpty()) {
        if (!parent->isReady(FeatureCore)) {
            readinessHelper->setIntrospectCompleted(FeatureCore, true);
        }
        return;
    }

    currentRemoteMembersChangedInfo = remoteMembersChangedQueue.dequeue();

    QSet<uint> pendingRemoteMembers;
    for (ContactSendingStateMap::const_iterator i = currentRemoteMembersChangedInfo->updates.constBegin();
            i != currentRemoteMembersChangedInfo->updates.constEnd(); ++i) {
        pendingRemoteMembers.insert(i.key());
    }

    foreach(uint i, currentRemoteMembersChangedInfo->removed) {
        pendingRemoteMembers.insert(i);
    }

    if (!pendingRemoteMembers.isEmpty()) {
        ConnectionPtr connection = parent->content()->channel()->connection();
        connection->lowlevel()->injectContactIds(currentRemoteMembersChangedInfo->identifiers);

        ContactManagerPtr contactManager = connection->contactManager();
        PendingContacts *contacts = contactManager->contactsForHandles(
                pendingRemoteMembers.toList());
        parent->connect(contacts,
                SIGNAL(finished(Tp::PendingOperation*)),
                SLOT(gotRemoteMembersContacts(Tp::PendingOperation*)));
    } else {
        currentRemoteMembersChangedInfo.clear();
        processRemoteMembersChanged();
    }
}

/**
 * \class CallStream
 * \ingroup clientchannel
 * \headerfile TelepathyQt/call-stream.h <TelepathyQt/CallStream>
 *
 * \brief The CallStream class provides an object representing a Telepathy
 * Call.Stream.
 *
 * Instances of this class cannot be constructed directly; the only way to get
 * one is via CallContent.
 *
 * See \ref async_model
 */

/**
 * Feature representing the core that needs to become ready to make the
 * CallStream object usable.
 *
 * Note that this feature must be enabled in order to use most CallStream
 * methods. See specific methods documentation for more details.
 *
 * When calling isReady(), becomeReady(), this feature is implicitly added
 * to the requested features.
 */
const Feature CallStream::FeatureCore = Feature(QLatin1String(CallStream::staticMetaObject.className()), 0);

/**
 * Construct a new CallStream object.
 *
 * \param content The content owning this call stream.
 * \param objectPath The object path of this call stream.
 */
CallStream::CallStream(const CallContentPtr &content, const QDBusObjectPath &objectPath)
    : StatefulDBusProxy(content->dbusConnection(), content->busName(),
            objectPath.path(), FeatureCore),
      OptionalInterfaceFactory<CallStream>(this),
      mPriv(new Private(this, content))
{
}

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

/**
 * Return the content owning this call stream.
 *
 * \return The content owning this call stream.
 */
CallContentPtr CallStream::content() const
{
    return CallContentPtr(mPriv->content);
}

/**
 * Returns whether the user can request that a remote contact starts
 * sending on this stream. Not all protocols allow the user to ask
 * the other side to start sending media.
 *
 * \return true if the user can request that a remote contact starts
 * sending on this stream, or false otherwise.
 * \sa requestReceiving()
 */
bool CallStream::canRequestReceiving() const
{
    return mPriv->canRequestReceiving;
}

/**
 * Return the contacts whose the call stream is with.
 *
 * \return The contacts whose the call stream is with.
 * \sa remoteMembersRemoved()
 */
Contacts CallStream::remoteMembers() const
{
    return mPriv->remoteMembersContacts.values().toSet();
}

/**
 * Return the call stream local sending state.
 *
 * \return The call stream local sending state.
 * \sa localSendingStateChanged()
 */
SendingState CallStream::localSendingState() const
{
    return (SendingState) mPriv->localSendingState;
}

/**
 * Return the call stream remote sending state for a given \a contact.
 *
 * \return The call stream remote sending state for a contact.
 * \sa remoteSendingStateChanged()
 */
SendingState CallStream::remoteSendingState(const ContactPtr &contact) const
{
    if (!contact) {
        return SendingStateNone;
    }

    for (ContactSendingStateMap::const_iterator i = mPriv->remoteMembers.constBegin();
            i != mPriv->remoteMembers.constEnd(); ++i) {
        uint handle = i.key();
        SendingState sendingState = (SendingState) i.value();

        if (handle == contact->handle()[0]) {
            return sendingState;
        }
    }

    return SendingStateNone;
}

/**
 * Request that media starts or stops being sent on this call stream.
 *
 * \return A PendingOperation which will emit PendingOperation::finished
 *         when the call has finished.
 * \sa localSendingStateChanged()
 */
PendingOperation *CallStream::requestSending(bool send)
{
    return new PendingVoid(mPriv->streamInterface->SetSending(send), CallStreamPtr(this));
}

/**
 * Request that a remote \a contact stops or starts sending on this call stream.
 *
 * \return A PendingOperation which will emit PendingOperation::finished
 *         when the call has finished.
 * \sa remoteSendingStateChanged()
 */
PendingOperation *CallStream::requestReceiving(const ContactPtr &contact, bool receive)
{
    if (!contact) {
        return new PendingFailure(TP_QT_ERROR_INVALID_ARGUMENT,
                QLatin1String("Invalid contact"), CallStreamPtr(this));
    } else if (!mPriv->canRequestReceiving && receive) {
        return new PendingFailure(TP_QT_ERROR_NOT_IMPLEMENTED,
                QLatin1String("Requesting the other side to start sending media "
                              "is not allowed by this protocol"),
                CallStreamPtr(this));
    }

    return new PendingVoid(mPriv->streamInterface->RequestReceiving(contact->handle()[0], receive),
            CallStreamPtr(this));
}

void CallStream::gotMainProperties(PendingOperation *op)
{
    if (op->isError()) {
        warning().nospace() << "CallStreamInterface::requestAllProperties() failed with " <<
            op->errorName() << ": " << op->errorMessage();
        mPriv->readinessHelper->setIntrospectCompleted(FeatureCore, false,
            op->errorName(), op->errorMessage());
        return;
    }

    debug() << "Got reply to CallStreamInterface::requestAllProperties()";

    PendingVariantMap *pvm = qobject_cast<PendingVariantMap*>(op);
    Q_ASSERT(pvm);

    QVariantMap props = pvm->result();

    mPriv->canRequestReceiving = qdbus_cast<bool>(props[QLatin1String("CanRequestReceiving")]);
    mPriv->localSendingState = qdbus_cast<uint>(props[QLatin1String("LocalSendingState")]);

    ContactSendingStateMap remoteMembers =
        qdbus_cast<ContactSendingStateMap>(props[QLatin1String("RemoteMembers")]);
    HandleIdentifierMap remoteMemberIdentifiers =
        qdbus_cast<HandleIdentifierMap>(props[QLatin1String("RemoteMemberIdentifiers")]);

    mPriv->remoteMembersChangedQueue.enqueue(Private::RemoteMembersChangedInfo::create(
                remoteMembers, remoteMemberIdentifiers, UIntList(), CallStateReason()));
    mPriv->processRemoteMembersChanged();
}

void CallStream::gotRemoteMembersContacts(PendingOperation *op)
{
    PendingContacts *pc = qobject_cast<PendingContacts *>(op);

    if (!pc->isValid()) {
        warning().nospace() << "Getting contacts failed with " <<
            pc->errorName() << ":" << pc->errorMessage() << ", ignoring";
        mPriv->currentRemoteMembersChangedInfo.clear();
        mPriv->processRemoteMembersChanged();
        return;
    }

    QMap<uint, ContactPtr> removed;

    for (ContactSendingStateMap::const_iterator i =
                mPriv->currentRemoteMembersChangedInfo->updates.constBegin();
            i != mPriv->currentRemoteMembersChangedInfo->updates.constEnd(); ++i) {
        mPriv->remoteMembers.insert(i.key(), i.value());
    }

    foreach (const ContactPtr &contact, pc->contacts()) {
        mPriv->remoteMembersContacts.insert(contact->handle()[0], contact);
    }

    foreach (uint handle, mPriv->currentRemoteMembersChangedInfo->removed) {
        mPriv->remoteMembers.remove(handle);
        if (isReady(FeatureCore) && mPriv->remoteMembersContacts.contains(handle)) {
            removed.insert(handle, mPriv->remoteMembersContacts[handle]);

            // make sure we don't have updates for removed contacts
            mPriv->currentRemoteMembersChangedInfo->updates.remove(handle);
        }
        mPriv->remoteMembersContacts.remove(handle);
    }

    foreach (uint handle, pc->invalidHandles()) {
        mPriv->remoteMembers.remove(handle);
        if (isReady(FeatureCore) && mPriv->remoteMembersContacts.contains(handle)) {
            removed.insert(handle, mPriv->remoteMembersContacts[handle]);

            // make sure we don't have updates for invalid handles
            mPriv->currentRemoteMembersChangedInfo->updates.remove(handle);
        }
        mPriv->remoteMembersContacts.remove(handle);
    }

    if (isReady(FeatureCore)) {
        CallChannelPtr channel(content()->channel());
        QHash<ContactPtr, SendingState> remoteSendingStates;
        for (ContactSendingStateMap::const_iterator i =
                    mPriv->currentRemoteMembersChangedInfo->updates.constBegin();
                i != mPriv->currentRemoteMembersChangedInfo->updates.constEnd(); ++i) {
            uint handle = i.key();
            SendingState sendingState = (SendingState) i.value();

            Q_ASSERT(mPriv->remoteMembersContacts.contains(handle));
            remoteSendingStates.insert(mPriv->remoteMembersContacts[handle], sendingState);

            mPriv->remoteMembers.insert(i.key(), i.value());
        }

        if (!remoteSendingStates.isEmpty()) {
            emit remoteSendingStateChanged(remoteSendingStates,
                    mPriv->currentRemoteMembersChangedInfo->reason);
        }

        if (!removed.isEmpty()) {
            emit remoteMembersRemoved(removed.values().toSet(),
                    mPriv->currentRemoteMembersChangedInfo->reason);
        }
    }

    mPriv->currentRemoteMembersChangedInfo.clear();
    mPriv->processRemoteMembersChanged();
}

void CallStream::onLocalSendingStateChanged(uint state, const CallStateReason &reason)
{
    mPriv->localSendingState = state;
    emit localSendingStateChanged((SendingState) state, reason);
}

void CallStream::onRemoteMembersChanged(const ContactSendingStateMap &updates,
        const HandleIdentifierMap &identifiers,
        const UIntList &removed,
        const CallStateReason &reason)
{
    if (updates.isEmpty() && removed.isEmpty()) {
        debug() << "Received Call::Stream::RemoteMembersChanged with 0 removals and "
            "updates, skipping it";
        return;
    }

    debug() << "Received Call::Stream::RemoteMembersChanged with" << updates.size() <<
        "updated and" << removed.size() << "removed";
    mPriv->remoteMembersChangedQueue.enqueue(
            Private::RemoteMembersChangedInfo::create(updates, identifiers, removed, reason));
    mPriv->processRemoteMembersChanged();
}

/**
 * \fn void CallStream::localSendingStateChanged(Tp::SendingState localSendingState, const Tp::CallStateReason &reason);
 *
 * This signal is emitted when the local sending state of this call stream
 * changes.
 *
 * \param localSendingState The new local sending state of this call stream.
 * \param reason The reason that caused this change
 * \sa localSendingState()
 */

/**
 * \fn void CallStream::remoteSendingStateChanged(const QHash<Tp::ContactPtr, Tp::SendingState> &remoteSendingStates, const Tp::CallStateReason &reason);
 *
 * This signal is emitted when any remote sending state of this call stream
 * changes.
 *
 * \param remoteSendingStates The new remote sending states of this call stream.
 * \param reason The reason that caused these changes
 * \sa remoteSendingState()
 */

/**
 * \fn void CallStream::remoteMembersRemoved(const Tp::Contacts &members, const Tp::CallStateReason &reason);
 *
 * This signal is emitted when one or more members of this stream are removed.
 *
 * \param members The members that were removed from this call stream.
 * \param reason The reason for that caused these removals
 * \sa remoteMembers()
 */

} // Tp