accounts-qt  1.7
account.cpp
1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /*
3  * This file is part of libaccounts-qt
4  *
5  * Copyright (C) 2009-2011 Nokia Corporation.
6  * Copyright (C) 2012-2013 Canonical Ltd.
7  *
8  * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * version 2.1 as published by the Free Software Foundation.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22  * 02110-1301 USA
23  */
24 
25 #include "account.h"
26 #include "manager.h"
27 #include "manager_p.h"
28 #include "utils.h"
29 
30 #include <QPointer>
31 #include <libaccounts-glib/ag-account.h>
32 
33 namespace Accounts {
34 
75 class Account::Private
76 {
77 public:
78  Private(Manager *manager, const QString &providerName, Account *account);
79  Private(Manager *manager, AgAccount *agAccount);
80 
81  ~Private()
82  {
83  g_cancellable_cancel(m_cancellable);
84  g_object_unref(m_cancellable);
85  m_cancellable = NULL;
86  }
87 
88  void init(Account *account);
89 
90  QPointer<Manager> m_manager;
91  AgAccount *m_account; //real account
92  GCancellable *m_cancellable;
93  QString prefix;
94 
95  static void on_display_name_changed(Account *self);
96  static void on_enabled(Account *self, const gchar *service_name,
97  gboolean enabled);
98  static void account_store_cb(AgAccount *account,
99  GAsyncResult *res,
100  Account *self);
101  static void on_deleted(Account *self);
102 };
103 
104 class Watch::Private
105 {
106 public:
107  static void account_notify_cb(AgAccount *account, const gchar *key,
108  Watch *self);
109 };
110 } //namespace Accounts
111 
112 
113 using namespace Accounts;
114 
115 static QChar slash = QChar::fromLatin1('/');
116 
126 Watch::Watch(QObject *parent):
127  QObject(parent)
128 {
129 }
130 
131 Watch::~Watch()
132 {
133  Account *account = qobject_cast<Account *>(QObject::parent());
134  /* The destructor of Account deletes the child Watches before detaching
135  * them, so here account should always be not NULL */
136  Q_ASSERT(account != NULL);
137  ag_account_remove_watch(account->d->m_account, watch);
138 }
139 
140 Account::Private::Private(Manager *manager, const QString &providerName,
141  Account *account):
142  m_manager(manager),
143  m_cancellable(g_cancellable_new())
144 {
145  m_account = ag_manager_create_account(manager->d->m_manager,
146  providerName.toUtf8().constData());
147  init(account);
148 }
149 
150 Account::Private::Private(Manager *manager, AgAccount *agAccount):
151  m_manager(manager),
152  m_account(agAccount),
153  m_cancellable(g_cancellable_new())
154 {
155 }
156 
157 void Account::Private::init(Account *account)
158 {
159  if (m_account == 0) return;
160  g_signal_connect_swapped(m_account, "display-name-changed",
161  G_CALLBACK(&Private::on_display_name_changed),
162  account);
163  g_signal_connect_swapped(m_account, "enabled",
164  G_CALLBACK(&Private::on_enabled), account);
165  g_signal_connect_swapped(m_account, "deleted",
166  G_CALLBACK(&Private::on_deleted), account);
167 }
168 
169 void Account::Private::on_display_name_changed(Account *self)
170 {
171  const gchar *name = ag_account_get_display_name(self->d->m_account);
172 
173  Q_EMIT self->displayNameChanged(UTF8(name));
174 }
175 
176 void Account::Private::on_enabled(Account *self, const gchar *service_name,
177  gboolean enabled)
178 {
179  Q_EMIT self->enabledChanged(UTF8(service_name), enabled);
180 }
181 
182 void Account::Private::on_deleted(Account *self)
183 {
184  TRACE();
185 
186  Q_EMIT self->removed();
187 }
188 
204 Account::Account(Manager *manager, const QString &providerName,
205  QObject *parent):
206  QObject(parent),
207  d(new Private(manager, providerName, this))
208 {
209 }
210 
211 Account::Account(Private *d, QObject *parent):
212  QObject(parent),
213  d(d)
214 {
215  d->init(this);
216 }
217 
227 Account *Account::fromId(Manager *manager, AccountId id, QObject *parent)
228 {
229  GError *error = 0;
230  AgAccount *account = ag_manager_load_account(manager->d->m_manager, id,
231  &error);
232  if (account == 0) {
233  Q_ASSERT(error != 0);
234  manager->d->lastError = Error(error);
235  g_error_free(error);
236  return 0;
237  }
238  Q_ASSERT(error == 0);
239  return new Account(new Private(manager, account), parent);
240 }
241 
246 {
247  QObjectList list = children();
248  for (int i = 0; i < list.count(); i++)
249  {
250  QObject *o = list.at(i);
251  if (qobject_cast<Watch *>(o))
252  delete o;
253  }
254 
255  g_signal_handlers_disconnect_by_func
256  (d->m_account, (void *)&Private::on_display_name_changed, this);
257  g_signal_handlers_disconnect_by_func
258  (d->m_account, (void *)&Private::on_enabled, this);
259  g_signal_handlers_disconnect_by_func
260  (d->m_account, (void *)&Private::on_deleted, this);
261  g_object_unref(d->m_account);
262  delete d;
263  d = 0;
264 }
265 
270 AccountId Account::id() const
271 {
272  return d->m_account ? d->m_account->id : 0;
273 }
274 
279 {
280  return d->m_manager;
281 }
282 
286 bool Account::supportsService(const QString &serviceType) const
287 {
288  return ag_account_supports_service(d->m_account,
289  serviceType.toUtf8().constData());
290 }
291 
300 ServiceList Account::services(const QString &serviceType) const
301 {
302  GList *list;
303  if (serviceType.isEmpty()) {
304  list = ag_account_list_services(d->m_account);
305  } else {
306  list = ag_account_list_services_by_type(d->m_account,
307  serviceType.toUtf8().constData());
308  }
309 
310  /* convert glist -> ServiceList */
311  ServiceList servList;
312  GList *iter;
313  for (iter = list; iter; iter = iter->next)
314  {
315  AgService *service = (AgService*)iter->data;
316  servList.append(Service(service, StealReference));
317  }
318 
319  g_list_free(list);
320 
321  return servList;
322 }
323 
329 ServiceList Account::enabledServices() const
330 {
331  GList *list;
332  list = ag_account_list_enabled_services(d->m_account);
333 
334  /* convert glist -> ServiceList */
335  ServiceList servList;
336  GList *iter;
337  for (iter = list; iter; iter = g_list_next(iter))
338  {
339  AgService *service = (AgService*)iter->data;
340  servList.append(Service(service, StealReference));
341  }
342 
343  g_list_free(list);
344 
345  return servList;
346 }
347 
354 bool Account::enabled() const
355 {
356  return ag_account_get_enabled(d->m_account);
357 }
358 
366 void Account::setEnabled(bool enabled)
367 {
368  ag_account_set_enabled(d->m_account, enabled);
369 }
370 
376 QString Account::displayName() const
377 {
378  return UTF8(ag_account_get_display_name(d->m_account));
379 }
380 
385 void Account::setDisplayName(const QString &displayName)
386 {
387  ag_account_set_display_name(d->m_account,
388  displayName.toUtf8().constData());
389 }
390 
394 QString Account::providerName() const
395 {
396  return UTF8(ag_account_get_provider_name(d->m_account));
397 }
398 
403 {
404  return manager()->provider(providerName());
405 }
406 
412 void Account::selectService(const Service &service)
413 {
414  AgService *agService = NULL;
415 
416  if (service.isValid())
417  agService = service.service();
418 
419  ag_account_select_service(d->m_account, agService);
420  d->prefix = QString();
421 }
422 
427 {
428  AgService *agService = ag_account_get_selected_service(d->m_account);
429  return Service(agService);
430 }
431 
437 QStringList Account::allKeys() const
438 {
439  QStringList allKeys;
440  AgAccountSettingIter iter;
441  const gchar *key;
442  GVariant *val;
443 
444  /* iterate the settings */
445  QByteArray tmp = d->prefix.toLatin1();
446  ag_account_settings_iter_init(d->m_account, &iter, tmp.constData());
447  while (ag_account_settings_iter_get_next(&iter, &key, &val))
448  {
449  allKeys.append(QString(ASCII(key)));
450  }
451  return allKeys;
452 }
453 
460 void Account::beginGroup(const QString &prefix)
461 {
462  d->prefix += prefix + slash;
463 }
464 
470 QStringList Account::childGroups() const
471 {
472  QStringList groups, all_keys;
473 
474  all_keys = allKeys();
475  Q_FOREACH (QString key, all_keys)
476  {
477  if (key.contains(slash)) {
478  QString group = key.section(slash, 0, 0);
479  if (!groups.contains(group))
480  groups.append(group);
481  }
482  }
483  return groups;
484 }
485 
491 QStringList Account::childKeys() const
492 {
493  QStringList keys, all_keys;
494 
495  all_keys = allKeys();
496  Q_FOREACH (QString key, all_keys)
497  {
498  if (!key.contains(slash))
499  keys.append(key);
500  }
501  return keys;
502 }
503 
509 {
510  /* clear() must ignore the group: so, temporarily reset it and call
511  * remove("") */
512  QString saved_prefix = d->prefix;
513  d->prefix = QString();
514  remove(QString());
515  d->prefix = saved_prefix;
516 }
517 
524 bool Account::contains(const QString &key) const
525 {
526  return childKeys().contains(key);
527 }
528 
535 {
536  d->prefix = d->prefix.section(slash, 0, -3,
537  QString::SectionIncludeTrailingSep);
538  if (d->prefix[0] == slash) d->prefix.remove(0, 1);
539 }
540 
546 QString Account::group() const
547 {
548  if (d->prefix.endsWith(slash))
549  return d->prefix.left(d->prefix.size() - 1);
550  return d->prefix;
551 }
552 
557 {
558  return true;
559 }
560 
568 void Account::remove(const QString &key)
569 {
570  if (key.isEmpty())
571  {
572  /* delete all keys in the group */
573  QStringList keys = allKeys();
574  Q_FOREACH (QString key, keys)
575  {
576  if (!key.isEmpty())
577  remove(key);
578  }
579  }
580  else
581  {
582  QString full_key = d->prefix + key;
583  QByteArray tmpkey = full_key.toLatin1();
584  ag_account_set_variant(d->m_account, tmpkey.constData(), NULL);
585  }
586 }
587 
595 void Account::setValue(const QString &key, const QVariant &value)
596 {
597  GVariant *variant = qVariantToGVariant(value);
598  if (variant == 0) {
599  return;
600  }
601 
602  QString full_key = d->prefix + key;
603  QByteArray tmpkey = full_key.toLatin1();
604  ag_account_set_variant(d->m_account, tmpkey.constData(), variant);
605 }
606 
607 void Account::Private::account_store_cb(AgAccount *account,
608  GAsyncResult *res,
609  Account *self)
610 {
611  TRACE() << "Saved accunt ID:" << account->id;
612 
613  GError *error = NULL;
614  ag_account_store_finish(account, res, &error);
615  if (error) {
616  if (error->domain == G_IO_ERROR &&
617  error->code == G_IO_ERROR_CANCELLED) {
618  TRACE() << "Account destroyed, operation cancelled";
619  } else {
620  Q_EMIT self->error(Error(error));
621  }
622  g_error_free(error);
623  } else {
624  Q_EMIT self->synced();
625  }
626 }
627 
642 QVariant Account::value(const QString &key, const QVariant &defaultValue,
643  SettingSource *source) const
644 {
645  QString full_key = d->prefix + key;
646  QByteArray ba = full_key.toLatin1();
647  AgSettingSource settingSource;
648  GVariant *variant =
649  ag_account_get_variant(d->m_account, ba.constData(), &settingSource);
650  if (source != 0) {
651  switch (settingSource) {
652  case AG_SETTING_SOURCE_ACCOUNT: *source = ACCOUNT; break;
653  case AG_SETTING_SOURCE_PROFILE: *source = TEMPLATE; break;
654  default: *source = NONE; break;
655  }
656  }
657 
658  return (variant != 0) ? gVariantToQVariant(variant) : defaultValue;
659 }
660 
676 SettingSource Account::value(const QString &key, QVariant &value) const
677 {
678  SettingSource source;
679  QVariant variant = this->value(key, QVariant(), &source);
680  if (variant.isValid()) {
681  if (value.type() != variant.type()) {
682  if (!variant.convert(value.type())) source = NONE;
683  }
684  value = variant;
685  }
686 
687  return source;
688 }
689 
699 QString Account::valueAsString(const QString &key,
700  QString default_value,
701  SettingSource *source) const
702 {
703  QVariant var = default_value;
704  SettingSource src = value(key, var);
705  if (source)
706  *source = src;
707  return var.toString();
708 }
709 
719 int Account::valueAsInt(const QString &key,
720  int default_value,
721  SettingSource *source) const
722 {
723  QVariant var = default_value;
724  SettingSource src = value(key, var);
725  if (source)
726  *source = src;
727  return var.toInt();
728 }
729 
739 quint64 Account::valueAsUInt64(const QString &key,
740  quint64 default_value,
741  SettingSource *source) const
742 {
743  QVariant var = default_value;
744  SettingSource src = value(key, var);
745  if (source)
746  *source = src;
747  return var.toULongLong();
748 }
749 
759 bool Account::valueAsBool(const QString &key,
760  bool default_value,
761  SettingSource *source) const
762 {
763  QVariant var = default_value;
764  SettingSource src = value(key, var);
765  if (source)
766  *source = src;
767  return var.toBool();
768 }
769 
770 void Watch::Private::account_notify_cb(AgAccount *account, const gchar *key,
771  Watch *watch)
772 {
773  Q_EMIT watch->notify(key);
774 
775  Q_UNUSED(account);
776 }
777 
788 Watch *Account::watchKey(const QString &key)
789 {
790  AgAccountWatch ag_watch;
791  Watch *watch = new Watch(this);
792 
793  if (!key.isEmpty())
794  {
795  QString full_key = d->prefix + key;
796  ag_watch = ag_account_watch_key
797  (d->m_account, full_key.toLatin1().constData(),
798  (AgAccountNotifyCb)&Watch::Private::account_notify_cb, watch);
799  }
800  else
801  {
802  ag_watch = ag_account_watch_dir
803  (d->m_account, d->prefix.toLatin1().constData(),
804  (AgAccountNotifyCb)&Watch::Private::account_notify_cb, watch);
805  }
806 
807  if (!ag_watch)
808  {
809  delete watch;
810  return NULL;
811  }
812 
813  watch->setWatch(ag_watch);
814  return watch;
815 }
816 
830 {
831  ag_account_store_async(d->m_account,
832  d->m_cancellable,
833  (GAsyncReadyCallback)&Private::account_store_cb,
834  this);
835 }
836 
845 {
846  GError *error = NULL;
847  bool ret;
848 
849  ret = ag_account_store_blocking(d->m_account, &error);
850  if (error)
851  {
852  qWarning() << "Store operation failed: " << error->message;
853  g_error_free(error);
854  }
855 
856  return ret;
857 }
858 
864 {
865  ag_account_delete(d->m_account);
866 }
867 
877 void Account::sign(const QString &key, const char *token)
878 {
879  ag_account_sign (d->m_account, key.toUtf8().constData(), token);
880 }
881 
893 bool Account::verify(const QString &key, const char **token)
894 {
895  return ag_account_verify(d->m_account, key.toUtf8().constData(), token);
896 }
897 
910 bool Account::verifyWithTokens(const QString &key, QList<const char*> tokens)
911 {
912  int tokensCount = tokens.count();
913 
914  const char *tmp[tokensCount + 1];
915 
916  for (int i = 0; i < tokensCount; ++i)
917  {
918  tmp[i] = tokens.at(i);
919  }
920  tmp[tokensCount] = NULL;
921 
922  return ag_account_verify_with_tokens(d->m_account, key.toUtf8().constData(), tmp);
923 }
924 
926 {
927  QString key = ACCOUNTS_KEY_CREDENTIALS_ID;
928  QVariant val(QVariant::Int);
929 
930  if (value(key, val) != NONE)
931  return val.toUInt();
932 
933  uint id = 0;
934  Service service = selectedService();
935  if (service.isValid()) {
936  selectService();
937  if (value(key, val) != NONE)
938  id = val.toUInt();
939  selectService(service);
940  }
941  return id;
942 }
943 
944 AgAccount *Account::account()
945 {
946  return d->m_account;
947 }