summaryrefslogtreecommitdiff
path: root/TelepathyQt/protocol-info.cpp
blob: 7eb2fb43febc92ac64b8050e8c11b2736cef3770 (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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/**
 * This file is part of TelepathyQt
 *
 * @copyright Copyright (C) 2010 Collabora Ltd. <http://www.collabora.co.uk/>
 * @copyright Copyright (C) 2010 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/ProtocolInfo>

#include <TelepathyQt/ConnectionCapabilities>
#include <TelepathyQt/ConnectionManager>
#include <TelepathyQt/PendingFailure>
#include <TelepathyQt/PendingString>

namespace Tp
{

struct TP_QT_NO_EXPORT ProtocolInfo::Private : public QSharedData
{
    Private()
        : dbusConnection(QDBusConnection::sessionBus()), // make the compiler happy
          addressingIface(0)
    {
    }

    Private(const ConnectionManagerPtr &cm, const QString &name)
        : dbusConnection(cm->dbusConnection()),
          busName(cm->busName()),
          cmName(cm->name()),
          name(name),
          iconName(QString(QLatin1String("im-%1")).arg(name)),
          addressingIface(0)
    {
        QString escapedProtocolName = name;
        escapedProtocolName.replace(QLatin1Char('-'), QLatin1Char('_'));
        objectPath = QString(QLatin1String("%1/%2")).arg(cm->objectPath()).arg(escapedProtocolName);
    }

    ~Private()
    {
        delete addressingIface;
    }

    Client::ProtocolInterfaceAddressingInterface *addressingInterface()
    {
        if (!addressingIface) {
            addressingIface = new Client::ProtocolInterfaceAddressingInterface(
                    dbusConnection, busName, objectPath);
        }

        return addressingIface;
    }

    QDBusConnection dbusConnection;
    QString busName;
    QString objectPath;
    QString cmName;
    QString name;
    ProtocolParameterList params;
    ConnectionCapabilities caps;
    QString vcardField;
    QString englishName;
    QString iconName;
    PresenceSpecList statuses;
    AvatarSpec avatarRequirements;
    QStringList addressableVCardFields;
    QStringList addressableUriSchemes;

    Client::ProtocolInterfaceAddressingInterface *addressingIface;
};

/**
 * \class ProtocolInfo
 * \ingroup clientcm
 * \headerfile TelepathyQt/protocol-info.h <TelepathyQt/ProtocolInfo>
 *
 * \brief The ProtocolInfo class represents a <a
 * href="http://telepathy.freedesktop.org/spec/Protocol.html">Telepathy Protocol</a>.
 */

ProtocolInfo::ProtocolInfo()
{
}

/**
 * Construct a new ProtocolInfo object.
 *
 * \param cm Connection manager owning this ProtocolInfo.
 * \param name Protocol name.
 */
ProtocolInfo::ProtocolInfo(const ConnectionManagerPtr &cm, const QString &name)
    : mPriv(new Private(cm, name))
{
}

ProtocolInfo::ProtocolInfo(const ProtocolInfo &other)
    : mPriv(other.mPriv)
{
}

/**
 * Class destructor.
 */
ProtocolInfo::~ProtocolInfo()
{
}

ProtocolInfo &ProtocolInfo::operator=(const ProtocolInfo &other)
{
    this->mPriv = other.mPriv;
    return *this;
}

/**
 * Return the short name of the connection manager (e.g. "gabble") for this protocol.
 *
 * \return The name of the connection manager for this protocol.
 */
QString ProtocolInfo::cmName() const
{
    if (!isValid()) {
        return QString();
    }

    return mPriv->cmName;
}

/**
 * Return the string identifying this protocol as described in the \telepathy_spec
 * (e.g. "jabber").
 *
 * This identifier is not intended to be displayed to users directly; user
 * interfaces are responsible for mapping them to localized strings.
 *
 * \return A string identifying this protocol.
 */
QString ProtocolInfo::name() const
{
    if (!isValid()) {
        return QString();
    }

    return mPriv->name;
}

/**
 * Return all supported parameters for this protocol. The parameters' names
 * may either be the well-known strings specified by the \telepathy_spec
 * (e.g. "account" and "password"), or implementation-specific strings.
 *
 * \return A list of parameters for this protocol.
 */
ProtocolParameterList ProtocolInfo::parameters() const
{
    if (!isValid()) {
        return ProtocolParameterList();
    }

    return mPriv->params;
}

/**
 * Return whether a given parameter can be passed to the connection
 * manager when creating a connection to this protocol.
 *
 * \param name The name of a parameter.
 * \return true if the given parameter exists.
 */
bool ProtocolInfo::hasParameter(const QString &name) const
{
    if (!isValid()) {
        return false;
    }

    foreach (const ProtocolParameter &param, mPriv->params) {
        if (param.name() == name) {
            return true;
        }
    }
    return false;
}

/**
 * Return whether it might be possible to register new accounts on this
 * protocol, by setting the special parameter named
 * <code>register</code> to <code>true</code>.
 *
 * \return The same thing as hasParameter("register").
 * \sa hasParameter()
 */
bool ProtocolInfo::canRegister() const
{
    if (!isValid()) {
        return false;
    }

    return hasParameter(QLatin1String("register"));
}

/**
 * Return the capabilities that are expected to be available from a connection
 * to this protocol, i.e. those for which Connection::createChannel() can
 * reasonably be expected to succeed.
 * User interfaces can use this information to show or hide UI components.
 *
 * @return An object representing the capabilities expected to be available from
 *         a connection to this protocol.
 */
ConnectionCapabilities ProtocolInfo::capabilities() const
{
    if (!isValid()) {
        return ConnectionCapabilities();
    }

    return mPriv->caps;
}

/**
 * Return the name of the most common vcard field used for this protocol's
 * contact identifiers, normalized to lower case.
 *
 * One valid use of this field is to answer the question: given a contact's
 * vcard containing an X-JABBER field, how can you communicate with the contact?
 * By iterating through protocols looking for an x-jabber VCardField, one can
 * build up a list of protocols that handle x-jabber, then offer the user a list
 * of accounts for those protocols and/or the option to create a new account for
 * one of those protocols.
 * It is not necessarily valid to interpret contacts' identifiers as values of
 * this vcard field. For instance, telepathy-sofiasip supports contacts whose
 * identifiers are of the form sip:jenny@example.com or tel:8675309, which would
 * not normally both be represented by any single vcard field.
 *
 * \return The most common vcard field used for this protocol's contact
 *         identifiers, or an empty string if there is no such field.
 */
QString ProtocolInfo::vcardField() const
{
    if (!isValid()) {
        return QString();
    }

    return mPriv->vcardField;
}

/**
 * Return the English-language name of this protocol, such as "AIM" or "Yahoo!".
 *
 * The name can be used as a fallback if an application doesn't have a localized name for this
 * protocol.
 *
 * If the manager file or the CM service doesn't specify the english name, it is inferred from this
 * protocol name, such that for example "google-talk" becomes "Google Talk", but "local-xmpp"
 * becomes "Local Xmpp".
 *
 * \return An English-language name for this protocol.
 */
QString ProtocolInfo::englishName() const
{
    if (!isValid()) {
        return QString();
    }

    return mPriv->englishName;
}

/**
 * Return the name of an icon for this protocol in the system's icon theme, such as "im-msn".
 *
 * If the manager file or the CM service doesn't specify the icon name, "im-<protocolname>" is
 * assumed.
 *
 * \return The likely name of an icon for this protocol.
 */
QString ProtocolInfo::iconName() const
{
    if (!isValid()) {
        return QString();
    }

    return mPriv->iconName;
}

/**
 * Return a list of PresenceSpec representing the possible presence statuses
 * from a connection to this protocol.
 *
 * \return A list of PresenceSpec representing the possible presence statuses
 *         from a connection to this protocol.
 */
PresenceSpecList ProtocolInfo::allowedPresenceStatuses() const
{
    if (!isValid()) {
        return PresenceSpecList();
    }

    return mPriv->statuses;
}

/**
 * Return the requirements (size limits, supported MIME types, etc)
 * for avatars used on to this protocol.
 *
 * \return The requirements for avatars used on this protocol.
 */
AvatarSpec ProtocolInfo::avatarRequirements() const
{
    if (!isValid()) {
        return AvatarSpec();
    }

    return mPriv->avatarRequirements;
}

/**
 * Return the vcard fields that can be used to request a contact with on this protocol,
 * normalized to lower case.
 *
 * \return The vcard fields normalized to lower case.
 * \sa addressableUriSchemes()
 */
QStringList ProtocolInfo::addressableVCardFields() const
{
    if (!isValid()) {
        return QStringList();
    }

    return mPriv->addressableVCardFields;
}

/**
 * Return the URI schemes that are supported by this protocol.
 *
 * \return The URI schemes.
 * \sa addressableVCardFields()
 */
QStringList ProtocolInfo::addressableUriSchemes() const
{
    if (!isValid()) {
        return QStringList();
    }

    return mPriv->addressableUriSchemes;
}

/**
 * Attempt to normalize the given \a vcardAddress.
 *
 * For example, a vcard TEL field formatted as +1 (206) 555 1234,
 * could be normalized to +12065551234.
 *
 * If a vcard address X would be normalized to Y, a successful ContactManager
 * contact request using ContactManager::contactsForVCardAddresses() for
 * vcard address X would result in a contact with Y reported as an
 * address that can identify it in Contact::vcardAddresses().
 *
 * \param vcardField The vcard field the \a vcardAddress belongs to.
 * \param vcardAddress The address to normalize.
 * \return A PendingString which will emit PendingString::finished
 *         when the address has been normalized or an error occurred.
 * \sa normalizeContactUri()
 */
PendingString *ProtocolInfo::normalizeVCardAddress(const QString &vcardField,
        const QString &vcardAddress)
{
    if (!isValid()) {
        return new PendingString(TP_QT_ERROR_NOT_AVAILABLE,
                QLatin1String("Protocol object is invalid"));
    }

    Client::ProtocolInterfaceAddressingInterface *iface = mPriv->addressingInterface();
    if (!iface->isValid()) {
        // cm is still valid but no Protocol object found
        return new PendingString(TP_QT_ERROR_NOT_IMPLEMENTED,
                QLatin1String("ConnectionManager does not support Protocol.I.Addressing"));
    }

    return new PendingString(iface->NormalizeVCardAddress(vcardField, vcardAddress),
            SharedPtr<RefCounted>());
}

/**
 * Attempt to normalize the given contact \a uri.
 *
 * If the URI has extra information beyond what's necessary to identify a particular contact, such
 * as an XMPP resource or an action to carry out, this extra information wil be removed.
 *
 * An example would be xmpp:romeo@Example.Com/Empathy?message;body=Hello, which would be normalized
 * to xmpp:romeo@example.com.
 *
 * If a URI address X would be normalized to Y, a successful ContactManager
 * contact request using ContactManager::contactsForUris() for
 * URI address X would result in a contact with Y reported as an
 * address that can identify it in Contact::uris().
 *
 * \param uri The URI to normalize.
 * \return A PendingString which will emit PendingString::finished
 *         when the \a uri has been normalized or an error occurred.
 * \sa normalizeVCardAddress()
 */
PendingString *ProtocolInfo::normalizeContactUri(const QString &uri)
{
    if (!isValid()) {
        return new PendingString(TP_QT_ERROR_NOT_AVAILABLE,
                QLatin1String("Protocol object is invalid"));
    }

    Client::ProtocolInterfaceAddressingInterface *iface = mPriv->addressingInterface();
    if (!iface->isValid()) {
        // cm is still valid but no Protocol object found
        return new PendingString(TP_QT_ERROR_NOT_IMPLEMENTED,
                QLatin1String("ConnectionManager does not support Protocol.I.Addressing"));
    }

    return new PendingString(iface->NormalizeContactURI(uri),
            SharedPtr<RefCounted>());
}

void ProtocolInfo::addParameter(const ParamSpec &spec)
{
    if (!isValid()) {
        mPriv = new Private;
    }

    QVariant defaultValue;
    if (spec.flags & ConnMgrParamFlagHasDefault) {
        defaultValue = spec.defaultValue.variant();
    }

    uint flags = spec.flags;
    if (spec.name.endsWith(QLatin1String("password"))) {
        flags |= ConnMgrParamFlagSecret;
    }

    ProtocolParameter param(spec.name,
            QDBusSignature(spec.signature),
            (ConnMgrParamFlag) flags,
            defaultValue);
    mPriv->params.append(param);
}

void ProtocolInfo::setVCardField(const QString &vcardField)
{
    if (!isValid()) {
        mPriv = new Private;
    }

    mPriv->vcardField = vcardField;
}

void ProtocolInfo::setEnglishName(const QString &englishName)
{
    if (!isValid()) {
        mPriv = new Private;
    }

    mPriv->englishName = englishName;
}

void ProtocolInfo::setIconName(const QString &iconName)
{
    if (!isValid()) {
        mPriv = new Private;
    }

    mPriv->iconName = iconName;
}

void ProtocolInfo::setRequestableChannelClasses(
        const RequestableChannelClassList &caps)
{
    if (!isValid()) {
        mPriv = new Private;
    }

    mPriv->caps.updateRequestableChannelClasses(caps);
}

void ProtocolInfo::setAllowedPresenceStatuses(const PresenceSpecList &statuses)
{
    if (!isValid()) {
        mPriv = new Private;
    }

    mPriv->statuses = statuses;
}

void ProtocolInfo::setAvatarRequirements(const AvatarSpec &avatarRequirements)
{
    if (!isValid()) {
        mPriv = new Private;
    }

    mPriv->avatarRequirements = avatarRequirements;
}

void ProtocolInfo::setAddressableVCardFields(const QStringList &vcardFields)
{
    if (!isValid()) {
        mPriv = new Private;
    }

    mPriv->addressableVCardFields = vcardFields;
}

void ProtocolInfo::setAddressableUriSchemes(const QStringList &uriSchemes)
{
    if (!isValid()) {
        mPriv = new Private;
    }

    mPriv->addressableUriSchemes = uriSchemes;
}

} // Tp