summaryrefslogtreecommitdiff
path: root/TelepathyQt4/account-set.cpp
blob: 5fbd0305ba5e86632a61b1bd85de2f9931e30da2 (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
/*
 * This file is part of TelepathyQt4
 *
 * Copyright (C) 2010 Collabora Ltd. <http://www.collabora.co.uk/>
 * Copyright (C) 2010 Nokia Corporation
 *
 * 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 <TelepathyQt4/AccountSet>
#include "TelepathyQt4/account-set-internal.h"

#include "TelepathyQt4/_gen/account-set.moc.hpp"
#include "TelepathyQt4/_gen/account-set-internal.moc.hpp"

#include "TelepathyQt4/debug-internal.h"

#include <TelepathyQt4/Account>
#include <TelepathyQt4/AccountFilter>
#include <TelepathyQt4/AccountManager>
#include <TelepathyQt4/ConnectionCapabilities>
#include <TelepathyQt4/ConnectionManager>

namespace Tp
{

AccountSet::Private::Private(AccountSet *parent,
        const AccountManagerPtr &accountManager,
        const AccountFilterConstPtr &filter)
    : parent(parent),
      accountManager(accountManager),
      filter(filter),
      ready(false)
{
    init();
}

AccountSet::Private::Private(AccountSet *parent,
        const AccountManagerPtr &accountManager,
        const QVariantMap &filterMap)
    : parent(parent),
      accountManager(accountManager),
      ready(false)
{
    AccountPropertyFilterPtr propertyFilter = AccountPropertyFilter::create();
    for (QVariantMap::const_iterator i = filterMap.constBegin();
            i != filterMap.constEnd(); ++i) {
        propertyFilter->addProperty(i.key(), i.value());
    }
    filter = AccountFilterPtr::dynamicCast(propertyFilter);
    init();
}

void AccountSet::Private::init()
{
    if (filter->isValid()) {
        connectSignals();
        insertAccounts();
        ready = true;
    }
}

void AccountSet::Private::connectSignals()
{
    parent->connect(accountManager.data(),
            SIGNAL(newAccount(Tp::AccountPtr)),
            SLOT(onNewAccount(Tp::AccountPtr)));
}

void AccountSet::Private::insertAccounts()
{
    foreach (const Tp::AccountPtr &account, accountManager->allAccounts()) {
        insertAccount(account);
    }
}

void AccountSet::Private::insertAccount(const Tp::AccountPtr &account)
{
    QString accountPath = account->objectPath();
    Q_ASSERT(!wrappers.contains(accountPath));
    wrapAccount(account);
    filterAccount(account);
}

void AccountSet::Private::removeAccount(const Tp::AccountPtr &account)
{
    QString accountPath = account->objectPath();
    Q_ASSERT(wrappers.contains(accountPath));
    accounts.remove(accountPath);

    AccountWrapper *wrapper = wrappers.take(accountPath);
    Q_ASSERT(wrapper->disconnect(parent));
    wrapper->deleteLater();

    emit parent->accountRemoved(account);
}

void AccountSet::Private::wrapAccount(const AccountPtr &account)
{
    AccountWrapper *wrapper = new AccountWrapper(account, parent);
    parent->connect(wrapper,
            SIGNAL(accountRemoved(Tp::AccountPtr)),
            SLOT(onAccountRemoved(Tp::AccountPtr)));
    parent->connect(wrapper,
            SIGNAL(accountPropertyChanged(Tp::AccountPtr,QString)),
            SLOT(onAccountChanged(Tp::AccountPtr)));
    parent->connect(wrapper,
            SIGNAL(accountCapabilitiesChanged(Tp::AccountPtr,Tp::ConnectionCapabilities)),
            SLOT(onAccountChanged(Tp::AccountPtr)));
    wrappers.insert(account->objectPath(), wrapper);
}

void AccountSet::Private::filterAccount(const AccountPtr &account)
{
    QString accountPath = account->objectPath();
    Q_ASSERT(wrappers.contains(accountPath));
    AccountWrapper *wrapper = wrappers[accountPath];

    /* account changed, let's check if it matches filter */
    if (accountMatchFilter(wrapper)) {
        if (!accounts.contains(account->objectPath())) {
            accounts.insert(account->objectPath(), account);
            if (ready) {
                emit parent->accountAdded(account);
            }
        }
    } else {
        if (accounts.contains(account->objectPath())) {
            accounts.remove(account->objectPath());
            if (ready) {
                emit parent->accountRemoved(account);
            }
        }
    }
}

bool AccountSet::Private::accountMatchFilter(AccountWrapper *wrapper)
{
    if (!filter) {
        return true;
    }

    return filter->matches(wrapper->account());
}

AccountSet::Private::AccountWrapper::AccountWrapper(
        const AccountPtr &account, QObject *parent)
    : QObject(parent),
      mAccount(account)
{
    connect(account.data(),
            SIGNAL(removed()),
            SLOT(onAccountRemoved()));
    connect(account.data(),
            SIGNAL(propertyChanged(QString)),
            SLOT(onAccountPropertyChanged(QString)));
    connect(account.data(),
            SIGNAL(capabilitiesChanged(Tp::ConnectionCapabilities)),
            SLOT(onAccountCapalitiesChanged(Tp::ConnectionCapabilities)));
}

AccountSet::Private::AccountWrapper::~AccountWrapper()
{
}

void AccountSet::Private::AccountWrapper::onAccountRemoved()
{
    emit accountRemoved(mAccount);
}

void AccountSet::Private::AccountWrapper::onAccountPropertyChanged(
        const QString &propertyName)
{
    emit accountPropertyChanged(mAccount, propertyName);
}

void AccountSet::Private::AccountWrapper::onAccountCapalitiesChanged(
        const ConnectionCapabilities &caps)
{
    emit accountCapabilitiesChanged(mAccount, caps);
}

/**
 * \class AccountSet
 * \ingroup clientaccount
 * \headerfile TelepathyQt4/account-set.h <TelepathyQt4/AccountSet>
 *
 * \brief The AccountSet class provides an object representing a set of
 * Telepathy accounts filtered by a given criteria.
 *
 * AccountSet is automatically updated whenever accounts that match the given
 * criteria are added, removed or updated.
 *
 * \section account_set_usage_sec Usage
 *
 * \subsection account_set_create_sec Creating an AccountSet object
 *
 * The easiest way to create AccountSet objects is through AccountManager. One
 * can just use the AccountManager convenience methods such as
 * AccountManager::validAccountsSet() to get a set of account objects
 * representing valid accounts.
 *
 * For example:
 *
 * \code
 *
 * class MyClass : public QObject
 * {
 *     QOBJECT
 *
 * public:
 *     MyClass(QObject *parent = 0);
 *     ~MyClass() { }
 *
 * private Q_SLOTS:
 *     void onAccountManagerReady(Tp::PendingOperation *);
 *     void onValidAccountAdded(const Tp::AccountPtr &);
 *     void onValidAccountRemoved(const Tp::AccountPtr &);
 *
 * private:
 *     AccountManagerPtr am;
 *     AccountSetPtr validAccountsSet;
 * };
 *
 * MyClass::MyClass(QObject *parent)
 *     : QObject(parent)
 *       am(AccountManager::create())
 * {
 *     connect(am->becomeReady(),
 *             SIGNAL(finished(Tp::PendingOperation*)),
 *             SLOT(onAccountManagerReady(Tp::PendingOperation*)));
 * }
 *
 * void MyClass::onAccountManagerReady(Tp::PendingOperation *op)
 * {
 *     if (op->isError()) {
 *         qWarning() << "Account manager cannot become ready:" <<
 *             op->errorName() << "-" << op->errorMessage();
 *         return;
 *     }
 *
 *     validAccountsSet = am->validAccountsSet();
 *     connect(validAccountsSet.data(),
 *             SIGNAL(accountAdded(const Tp::AccountPtr &)),
 *             SLOT(onValidAccountAdded(const Tp::AccountPtr &)));
 *     connect(validAccountsSet.data(),
 *             SIGNAL(accountRemoved(const Tp::AccountPtr &)),
 *             SLOT(onValidAccountRemoved(const Tp::AccountPtr &)));
 *
 *     QList<AccountPtr> accounts = validAccountsSet->accounts();
 *     // do something with accounts
 * }
 *
 * void MyClass::onValidAccountAdded(const Tp::AccountPtr &account)
 * {
 *     // do something with account
 * }
 *
 * void MyClass::onValidAccountRemoved(const Tp::AccountPtr &account)
 * {
 *     // do something with account
 * }
 *
 * \endcode
 *
 * You can also define your own filter using AccountManager::filterAccounts:
 *
 * \code
 *
 * void MyClass::onAccountManagerReady(Tp::PendingOperation *op)
 * {
 *     ...
 *
 *     AccountPropertyFilterPtr filter = AccountPropertyFilter::create();
 *     filter->addProperty(QLatin1String("protocolName"), QLatin1String("jabber"));
 *     filter->addProperty(QLatin1String("enabled"), true);
 *
 *     AccountSetPtr filteredAccountSet = am->filterAccounts(filter);
 *     // connect to AccountSet::accountAdded/accountRemoved signals
 *     QList<AccountPtr> accounts = filteredAccountSet->accounts();
 *     // do something with accounts
 *
 *     ....
 * }
 *
 * \endcode
 *
 * Note that for AccountSet to property work with AccountCapabilityFilter
 * objects, the feature Account::FeatureCapabilities need to be enabled in all
 * accounts return by the AccountManager passed as param in the constructor.
 * The easiest way to do this is to enable AccountManager feature
 * AccountManager::FeatureFilterByCapabilities.
 *
 * AccountSet can also be instantiated directly, but when doing it,
 * the AccountManager object passed as param in the constructor must be ready
 * for AccountSet properly work.
 */

/**
 * Construct a new AccountSet object.
 *
 * \param accountManager An account manager object used to filter accounts.
 *                       The account manager object must be ready.
 * \param filter The desired filter.
 */
AccountSet::AccountSet(const AccountManagerPtr &accountManager,
        const AccountFilterConstPtr &filter)
    : Object(),
      mPriv(new Private(this, accountManager, filter))
{
}

/**
 * Construct a new AccountSet object.
 *
 * The \a filter must contain Account property names and values as map items.
 *
 * \param accountManager An account manager object used to filter accounts.
 *                       The account manager object must be ready.
 * \param filter The desired filter.
 */
AccountSet::AccountSet(const AccountManagerPtr &accountManager,
        const QVariantMap &filter)
    : Object(),
      mPriv(new Private(this, accountManager, filter))
{
}

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

/**
 * Return the account manager object used to filter accounts.
 *
 * \return The AccountManager object used to filter accounts.
 */
AccountManagerPtr AccountSet::accountManager() const
{
    return mPriv->accountManager;
}

/**
 * Return the filter used to filter accounts.
 *
 * \return The filter used to filter accounts.
 */
AccountFilterConstPtr AccountSet::filter() const
{
    return mPriv->filter;
}

/**
 * Return a list of account objects that match filter.
 *
 * \return A list of account objects that match filter.
 */
QList<AccountPtr> AccountSet::accounts() const
{
    return mPriv->accounts.values();
}

/**
 * \fn void AccountSet::accountAdded(const Tp::AccountPtr &account);
 *
 * This signal is emitted whenever an account that matches filter is added to
 * this set.
 *
 * \param account The account that was added to this set.
 */

/**
 * \fn void AccountSet::accountRemoved(const Tp::AccountPtr &account);
 *
 * This signal is emitted whenever an account that matches filter is removed
 * from this set.
 *
 * \param account The account that was removed from this set.
 */

void AccountSet::onNewAccount(const AccountPtr &account)
{
    mPriv->insertAccount(account);
}

void AccountSet::onAccountRemoved(const AccountPtr &account)
{
    mPriv->removeAccount(account);
}

void AccountSet::onAccountChanged(const AccountPtr &account)
{
    mPriv->filterAccount(account);
}

} // Tp