File: cdtpcontact.cpp

package info (click to toggle)
contactsd 1.4.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,392 kB
  • sloc: cpp: 8,862; ansic: 3,793; sh: 361; xml: 75; python: 22; makefile: 13
file content (430 lines) | stat: -rw-r--r-- 12,385 bytes parent folder | download
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
/** This file is part of Contacts daemon
 **
 ** Copyright (c) 2010-2011 Nokia Corporation and/or its subsidiary(-ies).
 **
 ** Contact:  Nokia Corporation (info@qt.nokia.com)
 **
 ** GNU Lesser General Public License Usage
 ** This file may be used under the terms of the GNU Lesser General Public License
 ** version 2.1 as published by the Free Software Foundation and appearing in the
 ** file LICENSE.LGPL included in the packaging of this file.  Please review the
 ** following information to ensure the GNU Lesser General Public License version
 ** 2.1 requirements will be met:
 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 **
 ** In addition, as a special exception, Nokia gives you certain additional rights.
 ** These rights are described in the Nokia Qt LGPL Exception version 1.1, included
 ** in the file LGPL_EXCEPTION.txt in this package.
 **
 ** Other Usage
 ** Alternatively, this file may be used in accordance with the terms and
 ** conditions contained in a signed written agreement between you and Nokia.
 **/

#include <TelepathyQt/AvatarData>
#include <TelepathyQt/ContactCapabilities>

#include "cdtpaccount.h"
#include "cdtpcontact.h"
#include "debug.h"

///////////////////////////////////////////////////////////////////////////////

class CDTpContact::InfoData : public QSharedData
{
public:
    InfoData();

    QString alias;
    Tp::Presence presence;
    int capabilities;
    QString avatarPath;
    QString largeAvatarPath;
    QString squareAvatarPath;
    Tp::Contact::PresenceState subscriptionState;
    Tp::Contact::PresenceState publishState;
    Tp::ContactInfoFieldList infoFields;
    bool isSubscriptionStateKnown : 1;
    bool isPublishStateKnown : 1;
    bool isContactInfoKnown : 1;
    bool isVisible : 1;
};

CDTpContact::InfoData::InfoData()
    : isSubscriptionStateKnown(false)
    , isPublishStateKnown(false)
    , isContactInfoKnown(false)
    , isVisible(false)
{}

///////////////////////////////////////////////////////////////////////////////

static CDTpContact::Info::Capabilities makeInfoCaps(const Tp::CapabilitiesBase &capabilities)
{
    CDTpContact::Info::Capabilities caps = 0;

    if (capabilities.textChats()) {
        caps |= CDTpContact::Info::TextChats;
    }
    if (capabilities.streamedMediaCalls()) {
        caps |= CDTpContact::Info::StreamedMediaCalls;
    }
    if (capabilities.streamedMediaAudioCalls()) {
        caps |= CDTpContact::Info::StreamedMediaAudioCalls;
    }
    if (capabilities.streamedMediaVideoCalls()) {
        caps |= CDTpContact::Info::StreamedMediaAudioVideoCalls;
    }
    if (capabilities.upgradingStreamedMediaCalls()) {
        caps |= CDTpContact::Info::UpgradingStreamMediaCalls;
    }
    if (capabilities.fileTransfers()) {
        caps |= CDTpContact::Info::FileTransfers;
    }

    return caps;
}

CDTpContact::Info::Info()
    :d(new CDTpContact::InfoData)
{
}

CDTpContact::Info::Info(const CDTpContact *contact)
    : d(new CDTpContact::InfoData)
{
    const Tp::ContactPtr c = contact->contact();

    d->alias = c->alias();
    d->presence = c->presence();
    d->capabilities = makeInfoCaps(c->capabilities());
    d->avatarPath = c->avatarData().fileName;
    d->subscriptionState = c->subscriptionState();
    d->publishState = c->publishState();
    d->infoFields = c->infoFields().allFields();
    d->isSubscriptionStateKnown = c->isSubscriptionStateKnown();
    d->isPublishStateKnown = c->isPublishStateKnown();
    d->isContactInfoKnown = c->isContactInfoKnown();
    d->isVisible = contact->isVisible();
}

CDTpContact::Info::Info(const CDTpContact::Info &other)
    : d(other.d)
{
}

CDTpContact::Info& CDTpContact::Info::operator=(const CDTpContact::Info &other)
{
    return (d = other.d, *this);
}

CDTpContact::Info::~Info()
{
}

CDTpContact::Changes CDTpContact::Info::diff(const CDTpContact::Info &other) const
{
    Changes changes = 0;

    if (d->alias != other.d->alias)
        changes |= CDTpContact::Alias;

    // We only compare the relevant fields (status is not saved in Tracker, and isValid is irrelevant)
    if (d->presence.type() != other.d->presence.type()
     || d->presence.statusMessage() != other.d->presence.statusMessage())
        changes |= CDTpContact::Presence;

    if (d->capabilities != other.d->capabilities)
        changes |= CDTpContact::Capabilities;

    if (d->avatarPath != other.d->avatarPath)
        changes |= CDTpContact::DefaultAvatar;

    if (d->largeAvatarPath != other.d->largeAvatarPath)
        changes |= CDTpContact::LargeAvatar;

    if (d->squareAvatarPath != other.d->squareAvatarPath)
        changes |= CDTpContact::SquareAvatar;

    if (d->isSubscriptionStateKnown != other.d->isSubscriptionStateKnown
     || d->isPublishStateKnown != other.d->isPublishStateKnown
     || d->subscriptionState != other.d->subscriptionState
     || d->publishState != other.d->publishState)
        changes |= CDTpContact::Authorization;

    if (other.d->isContactInfoKnown
     && d->infoFields != other.d->infoFields)
        changes |= CDTpContact::Information;

    if (d->isVisible != other.d->isVisible)
        changes |= CDTpContact::Visibility;

    return changes;
}

///////////////////////////////////////////////////////////////////////////////

CDTpContact::CDTpContact(Tp::ContactPtr contact, CDTpAccount *accountWrapper)
    : QObject(),
      mContact(contact),
      mAccountWrapper(accountWrapper),
      mRemoved(false),
      mQueuedChanges(0)
{
    mQueuedChangesTimer.setInterval(0);
    mQueuedChangesTimer.setSingleShot(true);
    connect(&mQueuedChangesTimer, SIGNAL(timeout()), SLOT(onQueuedChangesTimeout()));

    updateVisibility();

    connect(contact.data(),
            SIGNAL(aliasChanged(const QString &)),
            SLOT(onContactAliasChanged()));
    connect(contact.data(),
            SIGNAL(presenceChanged(const Tp::Presence &)),
            SLOT(onContactPresenceChanged()));
    connect(contact.data(),
            SIGNAL(capabilitiesChanged(const Tp::ContactCapabilities &)),
            SLOT(onContactCapabilitiesChanged()));
    connect(contact.data(),
            SIGNAL(avatarDataChanged(const Tp::AvatarData &)),
            SLOT(onContactAvatarDataChanged()));
    connect(contact.data(),
            SIGNAL(subscriptionStateChanged(Tp::Contact::PresenceState)),
            SLOT(onContactAuthorizationChanged()));
    connect(contact.data(),
            SIGNAL(publishStateChanged(Tp::Contact::PresenceState, const QString &)),
            SLOT(onContactAuthorizationChanged()));
    connect(contact.data(),
            SIGNAL(infoFieldsChanged(const Tp::Contact::InfoFields &)),
            SLOT(onContactInfoChanged()));
    connect(contact.data(),
            SIGNAL(blockStatusChanged(bool)),
            SLOT(onBlockStatusChanged()));
}

CDTpContact::~CDTpContact()
{
}

CDTpAccountPtr CDTpContact::accountWrapper() const
{
    return CDTpAccountPtr(mAccountWrapper.data());
}

bool CDTpContact::isAvatarKnown() const
{
    if (!mContact->isAvatarTokenKnown()) {
        return false;
    }

    /* If we have a token but not an avatar filename, that probably means the
     * avatar data is being requested and we'll get an update later. */
    if (!mContact->avatarToken().isEmpty() &&
        mContact->avatarData().fileName.isEmpty()) {
        return false;
    }

    return true;
}

bool CDTpContact::isInformationKnown() const
{
    return mContact->isContactInfoKnown();
}

CDTpContact::Info CDTpContact::info() const
{
    return Info(this);
}

void CDTpContact::setLargeAvatarPath(const QString &path)
{
    mLargeAvatarPath = path;
    emitChanged(LargeAvatar);
}

void CDTpContact::setSquareAvatarPath(const QString &path)
{
    mSquareAvatarPath = path;
    emitChanged(SquareAvatar);
}

void CDTpContact::onContactAliasChanged()
{
    emitChanged(Alias);
}

void CDTpContact::onContactPresenceChanged()
{
    emitChanged(Presence);
}

void CDTpContact::onContactCapabilitiesChanged()
{
    emitChanged(Capabilities);
}

void CDTpContact::onContactAvatarDataChanged()
{
    emitChanged(DefaultAvatar);
}

void CDTpContact::onContactAuthorizationChanged()
{
    emitChanged(Authorization);
}

void CDTpContact::onContactInfoChanged()
{
    emitChanged(Information);
}

void CDTpContact::onBlockStatusChanged()
{
    emitChanged(Blocked);
}

void CDTpContact::emitChanged(CDTpContact::Changes changes)
{
    mQueuedChanges |= changes;

    if (not mQueuedChangesTimer.isActive()) {
        mQueuedChangesTimer.start();
    }
}

void CDTpContact::onQueuedChangesTimeout()
{
    // Check if this change also modified the visibility
    bool wasVisible = mVisible;
    updateVisibility();
    if (mVisible != wasVisible) {
        mQueuedChanges |= Visibility;
    }

    Q_EMIT changed(CDTpContactPtr(this), mQueuedChanges);

    mQueuedChanges = 0;
}

void CDTpContact::updateVisibility()
{
    /* Don't import contacts blocked, removed or incoming auth requests (because
     * user never asked for them). Note that we still import contacts that have
     * publishState==subscribeState==No, because that case happens if we sent an
     * auth request but it got rejected. In the case we received an auth request
     * and we rejected it, with our implementation, the contact is completely
     * removed from the roster so it won't appear here at all (some other IM
     * clients could still keep the contact in the roster with
     * publishState==subscribeState==No, but that's really corner case so we
     * don't care). */
    mVisible = !mRemoved && !mContact->isBlocked() &&
        (mContact->publishState() != Tp::Contact::PresenceStateAsk ||
         mContact->subscriptionState() != Tp::Contact::PresenceStateNo);
}

void CDTpContact::setRemoved(bool value)
{
    mRemoved = value;
    updateVisibility();
}

QDataStream& operator<<(QDataStream &stream, const Tp::Presence &presence)
{
    stream << presence.type();
    stream << presence.status();
    stream << presence.statusMessage();

    return stream;
}

QDataStream& operator<<(QDataStream &stream, const Tp::ContactInfoField &field)
{
    stream << field.fieldName;
    stream << field.parameters;
    stream << field.fieldValue;

    return stream;
}

QDataStream& operator<<(QDataStream &stream, const CDTpContact::Info &info)
{
    stream << info.d->alias;
    stream << info.d->presence;
    stream << info.d->capabilities;
    stream << info.d->avatarPath;
    stream << info.d->largeAvatarPath;
    stream << info.d->squareAvatarPath;
    stream << info.d->isSubscriptionStateKnown;
    stream << uint(info.d->subscriptionState);
    stream << info.d->isPublishStateKnown;
    stream << uint(info.d->publishState);
    stream << info.d->isContactInfoKnown;
    stream << info.d->infoFields;
    stream << info.d->isVisible;

    return stream;
}

QDataStream& operator>>(QDataStream &stream, Tp::Presence &presence)
{
    uint type;
    QString status;
    QString statusMessage;

    stream >> type;
    stream >> status;
    stream >> statusMessage;

    presence.setStatus((Tp::ConnectionPresenceType)type, status, statusMessage);

    return stream;
}

QDataStream& operator>>(QDataStream &stream, Tp::ContactInfoField &field)
{
    stream >> field.fieldName;
    stream >> field.parameters;
    stream >> field.fieldValue;

    return stream;
}

static QDataStream& operator>>(QDataStream &stream, Tp::Contact::PresenceState &presenceState)
{
    uint value;
    stream >> value;
    presenceState = Tp::Contact::PresenceState(value);
    return stream;
}

QDataStream& operator>>(QDataStream &stream, CDTpContact::Info &info)
{
    bool isSubscriptionStateKnown;
    bool isPublishStateKnown;
    bool isContactInfoKnown;
    bool isVisible;

    stream >> info.d->alias;
    stream >> info.d->presence;
    stream >> info.d->capabilities;
    stream >> info.d->avatarPath;
    stream >> info.d->largeAvatarPath;
    stream >> info.d->squareAvatarPath;
    stream >> isSubscriptionStateKnown;
    stream >> info.d->subscriptionState;
    stream >> isPublishStateKnown;
    stream >> info.d->publishState;
    stream >> isContactInfoKnown;
    stream >> info.d->infoFields;
    stream >> isVisible;

    info.d->isSubscriptionStateKnown = isSubscriptionStateKnown;
    info.d->isPublishStateKnown = isPublishStateKnown;
    info.d->isContactInfoKnown = isContactInfoKnown;
    info.d->isVisible = isVisible;

    return stream;
}