accounts-qt: Accounts/manager.cpp Source File

accounts-qt 1.12

- Main Page
- Related Pages
- Classes
- Files

- File List

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-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 "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 == 0);
98 Q_ASSERT(m_manager == 0);
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 != 0) {
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 != 0) {
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
217 Manager::~Manager()
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 = 0;
231 }
232
245 Account *Manager::account(const AccountId &id) const
246 {
247 Account *account = d->m_accounts.value(id, 0);
248 if (account == 0) {
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
392 Provider Manager::provider(const QString &providerName) const
393 {
394 AgProvider *provider;
395
396 provider = ag_manager_get_provider(d->m_manager,
397 providerName.toUtf8().constData());
398 return Provider(provider, StealReference);
399 }
400
406 ProviderList Manager::providerList() const
407 {
408 GList *list;
409
410 list = ag_manager_list_providers(d->m_manager);
411
412 /* convert glist -> ProviderList */
413 ProviderList provList;
414 GList *iter;
415
416 for (iter = list; iter; iter = g_list_next(iter))
417 {
418 AgProvider *provider = (AgProvider*)iter->data;
419 provList.append(Provider(provider, StealReference));
420 }
421
422 g_list_free(list);
423
424 return provList;
425 }
426
433 ServiceType Manager::serviceType(const QString &name) const
434 {
435 AgServiceType *type;
436 type = ag_manager_load_service_type(d->m_manager,
437 name.toUtf8().constData());
438 return ServiceType(type, StealReference);
439 }
440
448 Application Manager::application(const QString &applicationName) const
449 {
450 QByteArray ba = applicationName.toUtf8();
451 AgApplication *application =
452 ag_manager_get_application(d->m_manager, ba.constData());
453 return Application(application);
454 }
455
462 ApplicationList Manager::applicationList(const Service &service) const
463 {
464 ApplicationList ret;
465 GList *applications, *list;
466
467 applications = ag_manager_list_applications_by_service(d->m_manager,
468 service.service());
469 for (list = applications; list != NULL; list = list->next) {
470 AgApplication *application = (AgApplication *)list->data;
471 ret.append(Application(application));
472 }
473 g_list_free (applications);
474 return ret;
475 }
476
482 QString Manager::serviceType() const
483 {
484 return UTF8(ag_manager_get_service_type (d->m_manager));
485 }
486
496 void Manager::setTimeout(quint32 timeout)
497 {
498 ag_manager_set_db_timeout(d->m_manager, timeout);
499 }
500
505 quint32 Manager::timeout()
506 {
507 return ag_manager_get_db_timeout(d->m_manager);
508 }
509
514 void Manager::setAbortOnTimeout(bool abort)
515 {
516 ag_manager_set_abort_on_db_timeout(d->m_manager, abort);
517 }
518
523 bool Manager::abortOnTimeout() const
524 {
525 return ag_manager_get_abort_on_db_timeout(d->m_manager);
526 }
527
531 Manager::Options Manager::options() const
532 {
533 bool useDBus = true;
534 g_object_get(d->m_manager,
535 "use-dbus", &useDBus,
536 NULL);
537
538 Options opts;
539 if (!useDBus) {
540 opts |= DisableNotifications;
541 }
542 return opts;
543 }
544
554 Error Manager::lastError() const
555 {
556 return d->lastError;
557 }
558
Accounts::Manager::createAccount
Account * createAccount(const QString &providerName)
Creates a new account.
Definition: manager.cpp:331
Accounts::Manager::account
Account * account(const AccountId &id) const
Loads an account from the database.
Definition: manager.cpp:245
Accounts::Manager
Manager of accounts, services and providers.
Definition: manager.h:51
Accounts::Manager::setAbortOnTimeout
void setAbortOnTimeout(bool abort)
Sets whether to abort the application when a database timeout occurs.
Definition: manager.cpp:514
Accounts::Manager::applicationList
ApplicationList applicationList(const Service &service) const
List the registered applications which support the given service.
Definition: manager.cpp:462
Accounts::Provider
Representation of an account provider.
Definition: provider.h:48
Accounts::Manager::abortOnTimeout
bool abortOnTimeout() const
Definition: manager.cpp:523
Accounts::Manager::~Manager
~Manager()
Destructor.
Definition: manager.cpp:217
Accounts::Manager::accountList
AccountIdList accountList(const QString &serviceType=QString::null) const
Lists the accounts which support the requested service.
Definition: manager.cpp:266
Accounts::ServiceType
Representation of an account service type.
Definition: service-type.h:49
Accounts::Service
Representation of an account service.
Definition: service.h:48
Accounts::Manager::DisableNotifications
Definition: manager.h:61
Accounts::Manager::serviceType
QString serviceType() const
Gets the service type if given in manager constructor.
Definition: manager.cpp:482
Accounts::Manager::accountListEnabled
AccountIdList accountListEnabled(const QString &serviceType=QString::null) const
Lists the enabled accounts which support the requested service that also must be enabled.
Definition: manager.cpp:301
Accounts::Manager::setTimeout
void setTimeout(quint32 timeout)
Sets the timeout for database operations.
Definition: manager.cpp:496
Accounts::Manager::serviceList
ServiceList serviceList(const QString &serviceType=QString::null) const
Gets the service list.
Definition: manager.cpp:360
Accounts::Error
Base object definition for accounts error handling.
Definition: error.h:42
Accounts::Manager::service
Service service(const QString &serviceName) const
Gets an object representing a service.
Definition: manager.cpp:342
Accounts::Error::DatabaseLocked
Definition: error.h:54
Accounts::Manager::Manager
Manager(QObject *parent=0)
Constructor.
Definition: manager.cpp:146
Accounts::Manager::provider
Provider provider(const QString &providerName) const
Gets an object representing a provider.
Definition: manager.cpp:392
Accounts::Application
Information on the client applications of libaccounts.
Definition: application.h:40
Accounts::Manager::lastError
Error lastError() const
Gets the last error.
Definition: manager.cpp:554
Accounts::Manager::timeout
quint32 timeout()
Gets the database timeout.
Definition: manager.cpp:505
Accounts::Manager::options
Options options() const
Definition: manager.cpp:531
Accounts::Manager::application
Application application(const QString &applicationName) const
Get an object representing an application.
Definition: manager.cpp:448
Accounts::Manager::providerList
ProviderList providerList() const
Gets a provider list.
Definition: manager.cpp:406