accounts-qt  1.16
manager.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-2016 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 "application.h"
26 #include "service.h"
27 #include "manager.h"
28 #include "manager_p.h"
29 
30 #include <libaccounts-glib/ag-account.h>
31 
32 
33 namespace Accounts {
34 
91 } //namespace Accounts
92 
93 using namespace Accounts;
94 
95 void Manager::Private::init(Manager *q, AgManager *manager)
96 {
97  Q_ASSERT(q_ptr == nullptr);
98  Q_ASSERT(m_manager == nullptr);
99 
100  q_ptr = q;
101  m_manager = manager;
102 
103  if (manager) {
104  g_signal_connect_swapped
105  (manager, "account-created",
106  G_CALLBACK(&Private::on_account_created), q);
107  g_signal_connect_swapped
108  (manager, "account-deleted",
109  G_CALLBACK(&Private::on_account_deleted), q);
110  g_signal_connect_swapped
111  (manager, "account-updated",
112  G_CALLBACK(&Private::on_account_updated), q);
113  g_signal_connect_swapped
114  (manager, "enabled-event",
115  G_CALLBACK(&Private::on_enabled_event), q);
116  } else {
117  qWarning() << Q_FUNC_INFO << "Initializing with NULL AgManager!";
118  }
119 }
120 
121 void Manager::Private::on_account_created(Manager *self, AgAccountId id)
122 {
123  Q_EMIT self->accountCreated(id);
124 }
125 
126 void Manager::Private::on_account_deleted(Manager *self, AgAccountId id)
127 {
128  Q_EMIT self->accountRemoved(id);
129 }
130 
131 void Manager::Private::on_account_updated(Manager *self, AgAccountId id)
132 {
133  Q_EMIT self->accountUpdated(id);
134 }
135 
136 void Manager::Private::on_enabled_event(Manager *self, AgAccountId id)
137 {
138  Q_EMIT self->enabledEvent(id);
139 }
140 
146 Manager::Manager(QObject *parent):
147  QObject(parent),
148  d(new Private)
149 {
150  AgManager *manager = ag_manager_new();
151 
152  if (manager != nullptr) {
153  d->init(this, manager);
154  } else {
155  qWarning() << "Manager could not be created. DB is locked";
156  d->lastError = Error::DatabaseLocked;
157  }
158 
159 }
160 
173 Manager::Manager(const QString &serviceType, QObject *parent):
174  QObject(parent),
175  d(new Private)
176 {
177  AgManager *manager =
178  ag_manager_new_for_service_type(serviceType.toUtf8().constData());
179 
180  if (manager != nullptr) {
181  d->init(this, manager);
182  } else {
183  qWarning() << "Manager could not be created, DB is locked";
184  d->lastError = Error::DatabaseLocked;
185  }
186 
187 }
188 
194 Manager::Manager(Options options, QObject *parent):
195  QObject(parent),
196  d(new Private)
197 {
198  bool disableNotifications = options.testFlag(DisableNotifications);
199 
200  GError *error = NULL;
201  AgManager *manager =
202  (AgManager *)g_initable_new(AG_TYPE_MANAGER, NULL, &error,
203  "use-dbus", !disableNotifications,
204  NULL);
205  if (Q_LIKELY(manager)) {
206  d->init(this, manager);
207  } else {
208  qWarning() << "Manager could not be created." << error->message;
209  d->lastError = Error(error);
210  g_error_free(error);
211  }
212 }
213 
218 {
219  g_signal_handlers_disconnect_by_func
220  (d->m_manager, (void *)&Private::on_enabled_event, this);
221  g_signal_handlers_disconnect_by_func
222  (d->m_manager, (void *)&Private::on_account_updated, this);
223  g_signal_handlers_disconnect_by_func
224  (d->m_manager, (void *)&Private::on_account_deleted, this);
225  g_signal_handlers_disconnect_by_func
226  (d->m_manager, (void *)&Private::on_account_created, this);
227  g_object_unref(d->m_manager);
228 
229  delete d;
230  d = nullptr;
231 }
232 
245 Account *Manager::account(const AccountId &id) const
246 {
247  Account *account = d->m_accounts.value(id, 0);
248  if (account == nullptr) {
249  /* Create a new account object */
250  account = Account::fromId(const_cast<Manager*>(this), id,
251  const_cast<Manager*>(this));
252  d->m_accounts[id] = account;
253  }
254  return account;
255 }
256 
266 AccountIdList Manager::accountList(const QString &serviceType) const
267 {
268  GList *list = NULL;
269 
270  if (serviceType.isEmpty()) {
271  list = ag_manager_list(d->m_manager);
272  } else {
273  list = ag_manager_list_by_service_type(d->m_manager,
274  serviceType.toUtf8().constData());
275  }
276 
277  /* convert glist -> AccountIdList */
278  AccountIdList idList;
279  GList *iter;
280 
281  for (iter = list; iter; iter = g_list_next(iter))
282  {
283  idList.append((AccountId)GPOINTER_TO_INT(iter->data));
284  }
285 
286  ag_manager_list_free(list);
287 
288  return idList;
289 }
290 
301 AccountIdList Manager::accountListEnabled(const QString &serviceType) const
302 {
303  GList *list = NULL;
304  if (serviceType.isEmpty()) {
305  list = ag_manager_list_enabled(d->m_manager);
306  } else {
307  list = ag_manager_list_enabled_by_service_type(d->m_manager,
308  serviceType.toUtf8().constData());
309  }
310 
311  /* convert glist -> AccountIdList */
312  AccountIdList idList;
313  GList *iter;
314 
315  for (iter = list; iter; iter = g_list_next(iter))
316  {
317  idList.append((AccountId)GPOINTER_TO_INT(iter->data));
318  }
319 
320  ag_manager_list_free(list);
321 
322  return idList;
323 }
324 
331 Account *Manager::createAccount(const QString &providerName)
332 {
333  return new Account(this, providerName, this);
334 }
335 
342 Service Manager::service(const QString &serviceName) const
343 {
344  AgService *service =
345  ag_manager_get_service(d->m_manager,
346  serviceName.toUtf8().constData());
347  return Service(service, StealReference);
348 }
349 
360 ServiceList Manager::serviceList(const QString &serviceType) const
361 {
362  GList *list;
363 
364  if (serviceType.isEmpty()) {
365  list = ag_manager_list_services(d->m_manager);
366  } else {
367  list = ag_manager_list_services_by_type(d->m_manager,
368  serviceType.toUtf8().constData());
369  }
370 
371  /* convert glist -> ServiceList */
372  ServiceList servList;
373  GList *iter;
374 
375  for (iter = list; iter; iter = g_list_next(iter))
376  {
377  AgService *service = (AgService*)iter->data;
378  servList.append(Service(service, StealReference));
379  }
380 
381  g_list_free(list);
382 
383  return servList;
384 }
385 
393 ServiceList Manager::serviceList(const Application &application) const
394 {
395  GList *list;
396 
397  list = ag_manager_list_services_by_application(d->m_manager,
398  application.application());
399 
400  /* convert glist -> ServiceList */
401  ServiceList servList;
402  GList *iter;
403 
404  for (iter = list; iter; iter = g_list_next(iter))
405  {
406  AgService *service = (AgService*)iter->data;
407  servList.append(Service(service, StealReference));
408  }
409 
410  g_list_free(list);
411 
412  return servList;
413 }
414 
421 Provider Manager::provider(const QString &providerName) const
422 {
423  AgProvider *provider;
424 
425  provider = ag_manager_get_provider(d->m_manager,
426  providerName.toUtf8().constData());
427  return Provider(provider, StealReference);
428 }
429 
435 ProviderList Manager::providerList() const
436 {
437  GList *list;
438 
439  list = ag_manager_list_providers(d->m_manager);
440 
441  /* convert glist -> ProviderList */
442  ProviderList provList;
443  GList *iter;
444 
445  for (iter = list; iter; iter = g_list_next(iter))
446  {
447  AgProvider *provider = (AgProvider*)iter->data;
448  provList.append(Provider(provider, StealReference));
449  }
450 
451  g_list_free(list);
452 
453  return provList;
454 }
455 
462 ServiceType Manager::serviceType(const QString &name) const
463 {
464  AgServiceType *type;
465  type = ag_manager_load_service_type(d->m_manager,
466  name.toUtf8().constData());
467  return ServiceType(type, StealReference);
468 }
469 
477 Application Manager::application(const QString &applicationName) const
478 {
479  QByteArray ba = applicationName.toUtf8();
480  AgApplication *application =
481  ag_manager_get_application(d->m_manager, ba.constData());
482  return Application(application);
483 }
484 
491 ApplicationList Manager::applicationList(const Service &service) const
492 {
493  ApplicationList ret;
494  GList *applications, *list;
495 
496  applications = ag_manager_list_applications_by_service(d->m_manager,
497  service.service());
498  for (list = applications; list != NULL; list = list->next) {
499  AgApplication *application = (AgApplication *)list->data;
500  ret.append(Application(application));
501  }
502  g_list_free (applications);
503  return ret;
504 }
505 
511 QString Manager::serviceType() const
512 {
513  return UTF8(ag_manager_get_service_type (d->m_manager));
514 }
515 
525 void Manager::setTimeout(quint32 timeout)
526 {
527  ag_manager_set_db_timeout(d->m_manager, timeout);
528 }
529 
535 {
536  return ag_manager_get_db_timeout(d->m_manager);
537 }
538 
544 {
545  ag_manager_set_abort_on_db_timeout(d->m_manager, abort);
546 }
547 
553 {
554  return ag_manager_get_abort_on_db_timeout(d->m_manager);
555 }
556 
560 Manager::Options Manager::options() const
561 {
562  gboolean useDBus = true;
563  g_object_get(d->m_manager,
564  "use-dbus", &useDBus,
565  NULL);
566 
567  Options opts;
568  if (!useDBus) {
569  opts |= DisableNotifications;
570  }
571  return opts;
572 }
573 
584 {
585  return d->lastError;
586 }
587 
Accounts::Manager::accountListEnabled
AccountIdList accountListEnabled(const QString &serviceType=QString()) const
Lists the enabled accounts which support the requested service that also must be enabled.
Definition: manager.cpp:301
Accounts::ServiceType
Representation of an account service type.
Definition: service-type.h:50
Accounts::Manager::lastError
Error lastError() const
Gets the last error.
Definition: manager.cpp:583
Accounts::Manager::accountList
AccountIdList accountList(const QString &serviceType=QString()) const
Lists the accounts which support the requested service.
Definition: manager.cpp:266
Accounts::Manager::timeout
quint32 timeout()
Gets the database timeout.
Definition: manager.cpp:534
Accounts::Manager::setTimeout
void setTimeout(quint32 timeout)
Sets the timeout for database operations.
Definition: manager.cpp:525
Accounts::Manager::DisableNotifications
@ DisableNotifications
Definition: manager.h:61
Accounts::Manager::abortOnTimeout
bool abortOnTimeout() const
Definition: manager.cpp:552
Accounts::Manager
Manager of accounts, services and providers.
Definition: manager.h:52
Accounts::Manager::Manager
Manager(QObject *parent=nullptr)
Constructor.
Definition: manager.cpp:146
Accounts::Error
Base object definition for accounts error handling.
Definition: error.h:44
Accounts::Manager::account
Account * account(const AccountId &id) const
Loads an account from the database.
Definition: manager.cpp:245
Accounts::Manager::serviceList
ServiceList serviceList(const QString &serviceType=QString()) const
Gets the service list.
Definition: manager.cpp:360
Accounts::Manager::provider
Provider provider(const QString &providerName) const
Gets an object representing a provider.
Definition: manager.cpp:421
Accounts::Manager::application
Application application(const QString &applicationName) const
Get an object representing an application.
Definition: manager.cpp:477
Accounts::Manager::options
Options options() const
Definition: manager.cpp:560
Accounts::Application
Information on the client applications of libaccounts.
Definition: application.h:42
Accounts::Service
Representation of an account service.
Definition: service.h:49
Accounts::Manager::applicationList
ApplicationList applicationList(const Service &service) const
List the registered applications which support the given service.
Definition: manager.cpp:491
Accounts::Error::DatabaseLocked
@ DatabaseLocked
Definition: error.h:55
Accounts::Provider
Representation of an account provider.
Definition: provider.h:49
Accounts::Manager::~Manager
~Manager()
Destructor.
Definition: manager.cpp:217
Accounts::Manager::providerList
ProviderList providerList() const
Gets a provider list.
Definition: manager.cpp:435
Accounts::Manager::setAbortOnTimeout
void setAbortOnTimeout(bool abort)
Sets whether to abort the application when a database timeout occurs.
Definition: manager.cpp:543
Accounts::Manager::serviceType
QString serviceType() const
Gets the service type if given in manager constructor.
Definition: manager.cpp:511
Accounts::Manager::service
Service service(const QString &serviceName) const
Gets an object representing a service.
Definition: manager.cpp:342
Accounts::Manager::createAccount
Account * createAccount(const QString &providerName)
Creates a new account.
Definition: manager.cpp:331