~online-accounts/libaccounts-qt/13.04

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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
/* vi: set et sw=4 ts=4 cino=t0,(0: */
/*
 * This file is part of libaccounts-qt
 *
 * Copyright (C) 2009-2011 Nokia Corporation.
 * Copyright (C) 2012 Canonical Ltd.
 *
 * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * 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 "account.h"
#include "manager.h"
#include "utils.h"

#undef signals
#include <libaccounts-glib/ag-manager.h>
#include <libaccounts-glib/ag-account.h>

namespace Accounts {

/*!
 * @class Watch
 * @headerfile account.h Accounts/Account
 *
 * @brief Monitors an account key or group of keys.
 *
 * @details A watch is created via the Account::watch method and is a simple
 * object which will emit the notify() signal when the value of the key (or
 * group) that it is monitoring is changed.
 */

/*!
 * @class Account
 * @headerfile account.h Accounts/Account
 *
 * @brief The Account class provides an interface to account settings.
 *
 * @details The Account class is used to access the account and service settings.
 * This class has no constructor, therefore to instantiate one Account object
 * one has to either use the Manager::createAccount method (to create a new
 * empty account) or Manager::account (to load an existing account).
 *
 * @attention Do not call setParent() on an account object; the Account class
 * assumes that the parent is always the Manager, and changing it will have
 * unpredictable results.
 *
 * Most of the methods in the Account class act on the selected service: for
 * example, calling setEnabled(false) on the NULL service (this is the service
 * to be used for changing settings globally on the account) will disable the
 * account, while the code
 * \code
 * account->selectService(myService);
 * account->setEnabled(false);
 * \endcode
 * will disable the "myService" service.
 *
 * All changes made on an account (including deletion) are not stored until
 * sync() is called.
 */

class Account::Private
{
public:
    Private():
        m_account(0),
        m_cancellable(g_cancellable_new())
    {
    }

    ~Private()
    {
        g_cancellable_cancel(m_cancellable);
        g_object_unref(m_cancellable);
        m_cancellable = NULL;
    }

    AgAccount *m_account;  //real account
    GCancellable *m_cancellable;
    QString prefix;

    static void on_display_name_changed(Account *self);
    static void on_enabled(Account *self, const gchar *service_name,
                           gboolean enabled);
    static void account_store_cb(AgAccount *account,
                                 GAsyncResult *res,
                                 Account *self);
    static void on_deleted(Account *self);
};

class Watch::Private
{
public:
    static void account_notify_cb(AgAccount *account, const gchar *key,
                                  Watch *self);
};
} //namespace Accounts


using namespace Accounts;

static QChar slash = QChar::fromLatin1('/');

/*!
 * @fn Watch::notify(const char *key)
 *
 * Emitted when the value of the keys monitored by this watch change.
 * @param key The string that was used to create this watch. Note that if
 * this watch is monitor multiple keys this param only identifies their
 * common prefix, and not the actual key being changed.
 */

Watch::Watch(QObject *parent):
    QObject(parent)
{
}

Watch::~Watch()
{
    TRACE();
    Account *account = qobject_cast<Account *>(QObject::parent());
    /* The destructor of Account deletes the child Watches before detaching
     * them, so here account should always be not NULL */
    Q_ASSERT(account != NULL);
    ag_account_remove_watch(account->d->m_account, watch);
}

void Account::Private::on_display_name_changed(Account *self)
{
    TRACE();
    const gchar *name = ag_account_get_display_name(self->d->m_account);

    emit self->displayNameChanged(UTF8(name));
}

void Account::Private::on_enabled(Account *self, const gchar *service_name,
                                  gboolean enabled)
{
    TRACE();

    emit self->enabledChanged(UTF8(service_name), enabled);
}

void Account::Private::on_deleted(Account *self)
{
    TRACE();

    emit self->removed();
}

/*!
 * @fn Account::error(Accounts::Error error)
 *
 * Emitted when an error occurs.
 */

Account::Account(AgAccount *account, QObject *parent):
    QObject(parent),
    d(new Private)
{
    TRACE();
    d->m_account = account;
    g_object_ref(account);

    g_signal_connect_swapped(account, "display-name-changed",
                             G_CALLBACK(&Private::on_display_name_changed),
                             this);
    g_signal_connect_swapped(account, "enabled",
                             G_CALLBACK(&Private::on_enabled), this);
    g_signal_connect_swapped(account, "deleted",
                             G_CALLBACK(&Private::on_deleted), this);
}

/*!
 * Destroys the current account object and free all resources.
 */
Account::~Account()
{
    TRACE();

    QObjectList list = children();
    for (int i = 0; i < list.count(); i++)
    {
        QObject *o = list.at(i);
        if (qobject_cast<Watch *>(o))
            delete o;
    }

    g_signal_handlers_disconnect_by_func
        (d->m_account, (void *)&Private::on_display_name_changed, this);
    g_signal_handlers_disconnect_by_func
        (d->m_account, (void *)&Private::on_enabled, this);
    g_signal_handlers_disconnect_by_func
        (d->m_account, (void *)&Private::on_deleted, this);
    g_object_unref(d->m_account);
    delete d;
    d = 0;
}

/*!
 * Returns the AccountId of this account (0 if the account has not yet been
 * stored into the database).
 */
AccountId Account::id() const
{
    return d->m_account ? d->m_account->id : 0;
}

/*!
 * Returns the Manager.
 */
Manager *Account::manager() const
{
    return qobject_cast<Manager *>(QObject::parent());
}

/*!
 * Checks whether the account supports the given service.
 */
bool Account::supportsService(const QString &serviceType) const
{
    TRACE() << serviceType;

    return ag_account_supports_service(d->m_account,
                                       serviceType.toUtf8().constData());
}

/*!
 * Returns a list of services supported by this account. If the manager was
 * constructed with given service type only the services which supports the
 * service type will be returned.
 *
 * This is currently computed by returning all services having the same
 * provider as the account.
 */
ServiceList Account::services(const QString &serviceType) const
{
    TRACE() << serviceType;

    GList *list;
    if (serviceType.isEmpty()) {
        list = ag_account_list_services(d->m_account);
    } else {
        list = ag_account_list_services_by_type(d->m_account,
            serviceType.toUtf8().constData());
    }

    /* convert glist -> ServiceList */
    ServiceList servList;
    GList *iter;
    for (iter = list; iter; iter = iter->next)
    {
        AgService *service = (AgService*)iter->data;
        servList.append(Service(service, StealReference));
    }

    g_list_free(list);

    return servList;
}

/*!
 * Returns a list of enabled services supported by this account. If the manager
 * was constructed with given service type only the services which supports the
 * service type will be returned.
 */
ServiceList Account::enabledServices() const
{
    GList *list;
    list = ag_account_list_enabled_services(d->m_account);

    /* convert glist -> ServiceList */
    ServiceList servList;
    GList *iter;
    for (iter = list; iter; iter = g_list_next(iter))
    {
        AgService *service = (AgService*)iter->data;
        servList.append(Service(service, StealReference));
    }

    g_list_free(list);

    return servList;
}

/*!
 * Checks whether the account or selected service is enabled.
 *
 * This method operates on the currently selected service or
 * globally, if none selected.
 */
bool Account::enabled() const
{
    return ag_account_get_enabled(d->m_account);
}

/*!
 * Enables/disables the account or selected service.
 * The change will be written only when sync() is called.
 *
 * This method operates on the currently selected service or
 * globally, if none selected.
 */
void Account::setEnabled(bool enabled)
{
    ag_account_set_enabled(d->m_account, enabled);
}

/*!
 * Returns the display name of the account.
 *
 * This method operates on the currently selected service.
 */
QString Account::displayName() const
{
    return UTF8(ag_account_get_display_name(d->m_account));
}

/*!
 * Changes the display name of the account.
 * The change will be written only when sync() is called.
 */
void Account::setDisplayName(const QString &displayName)
{
    ag_account_set_display_name(d->m_account,
                                displayName.toUtf8().constData());
}

/*!
 * Returns the name of the provider of the account.
 */
QString Account::providerName() const
{
    return UTF8(ag_account_get_provider_name(d->m_account));
}

/*!
 * Select the Service for the subsequent operations.
 * @param service The Service to select. If this is invalid, the global
 * account settings will be selected.
 */
void Account::selectService(const Service &service)
{
    AgService *agService = NULL;

    if (service.isValid())
        agService = service.service();

    ag_account_select_service(d->m_account, agService);
    d->prefix = QString();
}

/*!
 * @return The currently selected service.
 */
Service Account::selectedService() const
{
    AgService *agService = ag_account_get_selected_service(d->m_account);
    return Service(agService);
}

/*!
 * Returns all keys in the current group.
 *
 * This method operates on the currently selected service.
 */
QStringList Account::allKeys() const
{
    QStringList allKeys;
    AgAccountSettingIter iter;
    const gchar *key;
    GVariant *val;

    /* iterate the settings */
    QByteArray tmp = d->prefix.toLatin1();
    ag_account_settings_iter_init(d->m_account, &iter, tmp.constData());
    while (ag_account_settings_iter_get_next(&iter, &key, &val))
    {
        allKeys.append(QString(ASCII(key)));
    }
    return allKeys;
}

/*!
 * Enters a group. This method never fails.
 * @param prefix
 *
 * This method operates on the currently selected service.
 */
void Account::beginGroup(const QString &prefix)
{
    d->prefix += prefix + slash;
}

/*!
 * Returns all the groups which are direct children of the current group.
 *
 * This method operates on the currently selected service.
 */
QStringList Account::childGroups() const
{
    QStringList groups, all_keys;

    all_keys = allKeys();
    foreach (QString key, all_keys)
    {
        if (key.contains(slash)) {
            QString group = key.section(slash, 0, 0);
            if (!groups.contains(group))
                groups.append(group);
        }
    }
    return groups;
}

/*!
 * Return all the keys which are direct children of the current group.
 *
 * This method operates on the currently selected service.
 */
QStringList Account::childKeys() const
{
    QStringList keys, all_keys;

    all_keys = allKeys();
    foreach (QString key, all_keys)
    {
        if (!key.contains(slash))
            keys.append(key);
    }
    return keys;
}

/*!
 * Removes all the keys in the currently selected service.
 * @see remove(const QString &key)
 */
void Account::clear()
{
    /* clear() must ignore the group: so, temporarily reset it and call
     * remove("") */
    QString saved_prefix = d->prefix;
    d->prefix = QString();
    remove(QString());
    d->prefix = saved_prefix;
}

/*!
 * Checks whether the given key is in the current group.
 * @param key The key name of the settings.
 *
 * This method operates on the currently selected service.
 */
bool Account::contains(const QString &key) const
{
    return childKeys().contains(key);
}

/*!
 * Exits a group.
 *
 * This method operates on the currently selected service.
 */
void Account::endGroup()
{
    d->prefix = d->prefix.section(slash, 0, -3,
                                  QString::SectionIncludeTrailingSep);
    if (d->prefix[0] == slash) d->prefix.remove(0, 1);
}

/*!
 * Returns the name of the current group.
 *
 * This method operates on the currently selected service.
 */
QString Account::group() const
{
    if (d->prefix.endsWith(slash))
        return d->prefix.left(d->prefix.size() - 1);
    return d->prefix;
}

/*!
 * Checks whether the account is writable. This always returns true.
 */
bool Account::isWritable() const
{
    return true;
}

/*!
 * Removes the given key. If the key is the empty string, all keys in the
 * current group are removed.
 * @param key The key name of the settings.
 *
 * This method operates on the currently selected service.
 */
void Account::remove(const QString &key)
{
    if (key.isEmpty())
    {
        /* delete all keys in the group */
        QStringList keys = allKeys();
        foreach (QString key, keys)
        {
            if (!key.isEmpty())
                remove(key);
        }
    }
    else
    {
        QString full_key = d->prefix + key;
        QByteArray tmpkey = full_key.toLatin1();
        ag_account_set_variant(d->m_account, tmpkey.constData(), NULL);
    }
}

/*!
 * Changes the value of an account setting.
 * @param key The key name of the setting.
 * @param value The new value.
 *
 * This method operates on the currently selected service.
 */
void Account::setValue(const QString &key, const QVariant &value)
{
    TRACE();

    GVariant *variant = qVariantToGVariant(value);
    if (variant == 0) {
        return;
    }

    QString full_key = d->prefix + key;
    QByteArray tmpkey = full_key.toLatin1();
    ag_account_set_variant(d->m_account, tmpkey.constData(), variant);
}

void Account::Private::account_store_cb(AgAccount *account,
                                        GAsyncResult *res,
                                        Account *self)
{
    TRACE() << "Saved accunt ID:" << account->id;

    GError *error = NULL;
    ag_account_store_finish(account, res, &error);
    if (error) {
        if (error->domain == G_IO_ERROR &&
            error->code == G_IO_ERROR_CANCELLED) {
            TRACE() << "Account destroyed, operation cancelled";
        } else {
            emit self->error(Error(error));
        }
        g_error_free(error);
    } else {
        emit self->synced();
    }
}

/*!
 * Retrieves the value of an account setting, as a QVariant.
 * @param key The key whose value must be retrieved.
 * @param defaultValue Value returned if the key is unset.
 * @param source Indicates whether the value comes from the account, the
 * service template or was unset.
 * @see valueAsString
 * @see valueAsInt
 * @see valueAsBool
 *
 * @return The value associated to \a key.
 *
 * This method operates on the currently selected service.
 */
QVariant Account::value(const QString &key, const QVariant &defaultValue,
                        SettingSource *source) const
{
    QString full_key = d->prefix + key;
    QByteArray ba = full_key.toLatin1();
    AgSettingSource settingSource;
    GVariant *variant =
        ag_account_get_variant(d->m_account, ba.constData(), &settingSource);
    if (source != 0) {
        switch (settingSource) {
        case AG_SETTING_SOURCE_ACCOUNT: *source = ACCOUNT; break;
        case AG_SETTING_SOURCE_PROFILE: *source = TEMPLATE; break;
        default: *source = NONE; break;
        }
    }

    return (variant != 0) ? gVariantToQVariant(variant) : defaultValue;
}

/*!
 * Retrieves the value of an account setting, as a QVariant.
 * @param key The key whose value must be retrieved.
 * @param value A QVariant initialized to the expected type of the value.
 * @see valueAsString
 * @see valueAsInt
 * @see valueAsBool
 *
 * @return Whether the value comes from the account, the service template
 * or was unset.
 *
 * This method operates on the currently selected service.
 * @deprecated Use value(const QString &key, const QVariant &defaultValue,
 * SettingSource *source) const instead.
 */
SettingSource Account::value(const QString &key, QVariant &value) const
{
    SettingSource source;
    QVariant variant = this->value(key, QVariant(), &source);
    if (variant.isValid()) {
        if (value.type() != variant.type()) {
            if (!variant.convert(value.type())) source = NONE;
        }
        value = variant;
    }

    return source;
}

/*!
 * Gets an account setting as a string.
 * @param key The key whose value must be retrieved.
 * @param default_value Value returned if the key is unset.
 * @param source Indicates whether the value comes from the account, the
 * service template or was unset.
 *
 * This method operates on the currently selected service.
 */
QString Account::valueAsString(const QString &key,
                               QString default_value,
                               SettingSource *source) const
{
    QVariant var = default_value;
    SettingSource src = value(key, var);
    if (source)
        *source = src;
    return var.toString();
}

/*!
 * Gets an account setting as an integer.
 * @param key The key whose value must be retrieved.
 * @param default_value Value returned if the key is unset.
 * @param source Indicates whether the value comes from the account, the
 * service template or was unset.
 *
 * This method operates on the currently selected service.
 */
int Account::valueAsInt(const QString &key,
                        int default_value,
                        SettingSource *source) const
{
    QVariant var = default_value;
    SettingSource src = value(key, var);
    if (source)
        *source = src;
    return var.toInt();
}

/*!
 * Gets an account setting as an unsigned long integer.
 * @param key The key of which value must be retrieved.
 * @param default_value Value returned if the key is unset.
 * @param source Indicates whether the value comes from the account, the
 * service template or was unset.
 *
 * This method operates on the currently selected service.
 */
quint64 Account::valueAsUInt64(const QString &key,
                        quint64 default_value,
                        SettingSource *source) const
{
    QVariant var = default_value;
    SettingSource src = value(key, var);
    if (source)
        *source = src;
    return var.toULongLong();
}

/*!
 * Gets an account setting as a boolean.
 * @param key The key whose value must be retrieved.
 * @param default_value Value returned if the key is unset.
 * @param source Indicates whether the value comes from the account, the
 * service template or was unset.
 *
 * This method operates on the currently selected service.
 */
bool Account::valueAsBool(const QString &key,
                          bool default_value,
                          SettingSource *source) const
{
    QVariant var = default_value;
    SettingSource src = value(key, var);
    if (source)
        *source = src;
    return var.toBool();
}

void Watch::Private::account_notify_cb(AgAccount *account, const gchar *key,
                                       Watch *watch)
{
    emit watch->notify(key);

    Q_UNUSED(account);
}

/*!
 * Installs a key or group watch.
 *
 * @param key The key to watch; if %NULL, watches the currently selected
 * group.
 *
 * @return A watch object.
 *
 * This method operates on the currently selected service.
 */
Watch *Account::watchKey(const QString &key)
{
    AgAccountWatch ag_watch;
    Watch *watch = new Watch(this);

    if (!key.isEmpty())
    {
        QString full_key = d->prefix + key;
        ag_watch = ag_account_watch_key
            (d->m_account, full_key.toLatin1().constData(),
             (AgAccountNotifyCb)&Watch::Private::account_notify_cb, watch);
    }
    else
    {
        ag_watch = ag_account_watch_dir
            (d->m_account, d->prefix.toLatin1().constData(),
             (AgAccountNotifyCb)&Watch::Private::account_notify_cb, watch);
    }

    if (!ag_watch)
    {
        delete watch;
        return NULL;
    }

    watch->setWatch(ag_watch);
    return watch;
}

/*!
 * Stores all account settings into the database.
 * The signal synced() will be emitted in case of success, or
 * error() in case of failure. No assumption must be made about when these
 * signals will be emitted: if the database is locked, the signals might
 * be emitted asynchronously, whereas if the operation can happen
 * synchronously then the signals can be emitted before this method
 * returns.
 * If for some reason one would want to process the signals asynchronously
 * from the event loop, one can use the Qt::QueuedConnection connection
 * type as last parameter of the QObject::connect call.
 */
void Account::sync()
{
    TRACE();

    ag_account_store_async(d->m_account,
                           d->m_cancellable,
                           (GAsyncReadyCallback)&Private::account_store_cb,
                           this);
}

/*!
 * Blocking version of the sync() method: execution of the current thread
 * will block until the operation has completed.
 * Usage of this method is discouraged, especially for UI applications.
 *
 * @return True on success, false otherwise.
 */
bool Account::syncAndBlock()
{
    TRACE();

    GError *error = NULL;
    bool ret;

    ret = ag_account_store_blocking(d->m_account, &error);
    if (error)
    {
        qWarning() << "Store operation failed: " << error->message;
        g_error_free(error);
    }

    return ret;
}

/*!
 * Marks the account for removal.
 * The account will be deleted only when the sync() method is called.
 */
void Account::remove()
{
    TRACE();
    ag_account_delete(d->m_account);
}

/*!
 * Creates signature of key with given aegis token. The calling application
 * must possess (request) the given aegis token. The account needs to be
 * stored prior to executing this method.
 * @param key The key or the prefix of set of the keys to be signed.
 * @param token The aegis token to be used for signing the key.
 *
 * This method operates on the currently selected service.
 */
void Account::sign(const QString &key, const char *token)
{
    ag_account_sign (d->m_account, key.toUtf8().constData(), token);
}

/*!
 * Verifies if the key is signed and the signature matches the value
 * and provides the aegis token which was used for signing the key.
 *
 * @param key The name of the key or prefix of the keys to be verified.
 * @param token Aegis token to be retrieved.
 *
 * @return True if the key is signed and the signature matches the value.
 *
 * This method operates on the currently selected service.
 */
bool Account::verify(const QString &key, const char **token)
{
    return ag_account_verify(d->m_account, key.toUtf8().constData(), token);
}

/*!
 * Verifies if the key is signed with any of the aegis tokens and the
 * signature is valid.
 *
 * @param key The name of the key or prefix of the keys to be verified.
 * @param tokens Array of aegis tokens.
 *
 * @return True if the key is signed with any of the aegis tokens and
 * the signature is valid.
 *
 * This method operates on the currently selected service.
 */
bool Account::verifyWithTokens(const QString &key, QList<const char*> tokens)
{
    int tokensCount = tokens.count();

    const char *tmp[tokensCount + 1];

    for (int i = 0; i < tokensCount; ++i)
    {
        tmp[i] = tokens.at(i);
    }
    tmp[tokensCount] = NULL;

    return ag_account_verify_with_tokens(d->m_account, key.toUtf8().constData(), tmp);
}

uint Account::credentialsId()
{
    QString key = ACCOUNTS_KEY_CREDENTIALS_ID;
    QVariant val(QVariant::Int);

    if (value(key, val) != NONE)
        return val.toUInt();

    uint id = 0;
    Service service = selectedService();
    if (service.isValid()) {
        selectService();
        if (value(key, val) != NONE)
            id = val.toUInt();
        selectService(service);
    }
    return id;
}

AgAccount *Account::account()
{
    return d->m_account;
}