accounts-qt 1.10

- 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
192 Manager::~Manager()
193 {
194 g_signal_handlers_disconnect_by_func
195 (d->m_manager, (void *)&Private::on_enabled_event, this);
196 g_signal_handlers_disconnect_by_func
197 (d->m_manager, (void *)&Private::on_account_updated, this);
198 g_signal_handlers_disconnect_by_func
199 (d->m_manager, (void *)&Private::on_account_deleted, this);
200 g_signal_handlers_disconnect_by_func
201 (d->m_manager, (void *)&Private::on_account_created, this);
202 g_object_unref(d->m_manager);
203
204 delete d;
205 d = 0;
206 }
207
220 Account *Manager::account(const AccountId &id) const
221 {
222 Account *account = d->m_accounts.value(id, 0);
223 if (account == 0) {
224 /* Create a new account object */
225 account = Account::fromId(const_cast<Manager*>(this), id,
226 const_cast<Manager*>(this));
227 d->m_accounts[id] = account;
228 }
229 return account;
230 }
231
241 AccountIdList Manager::accountList(const QString &serviceType) const
242 {
243 GList *list = NULL;
244
245 if (serviceType.isEmpty()) {
246 list = ag_manager_list(d->m_manager);
247 } else {
248 list = ag_manager_list_by_service_type(d->m_manager,
249 serviceType.toUtf8().constData());
250 }
251
252 /* convert glist -> AccountIdList */
253 AccountIdList idList;
254 GList *iter;
255
256 for (iter = list; iter; iter = g_list_next(iter))
257 {
258 idList.append((AccountId)GPOINTER_TO_INT(iter->data));
259 }
260
261 ag_manager_list_free(list);
262
263 return idList;
264 }
265
276 AccountIdList Manager::accountListEnabled(const QString &serviceType) const
277 {
278 GList *list = NULL;
279 if (serviceType.isEmpty()) {
280 list = ag_manager_list_enabled(d->m_manager);
281 } else {
282 list = ag_manager_list_enabled_by_service_type(d->m_manager,
283 serviceType.toUtf8().constData());
284 }
285
286 /* convert glist -> AccountIdList */
287 AccountIdList idList;
288 GList *iter;
289
290 for (iter = list; iter; iter = g_list_next(iter))
291 {
292 idList.append((AccountId)GPOINTER_TO_INT(iter->data));
293 }
294
295 ag_manager_list_free(list);
296
297 return idList;
298 }
299
306 Account *Manager::createAccount(const QString &providerName)
307 {
308 return new Account(this, providerName, this);
309 }
310
317 Service Manager::service(const QString &serviceName) const
318 {
319 AgService *service =
320 ag_manager_get_service(d->m_manager,
321 serviceName.toUtf8().constData());
322 return Service(service, StealReference);
323 }
324
335 ServiceList Manager::serviceList(const QString &serviceType) const
336 {
337 GList *list;
338
339 if (serviceType.isEmpty()) {
340 list = ag_manager_list_services(d->m_manager);
341 } else {
342 list = ag_manager_list_services_by_type(d->m_manager,
343 serviceType.toUtf8().constData());
344 }
345
346 /* convert glist -> ServiceList */
347 ServiceList servList;
348 GList *iter;
349
350 for (iter = list; iter; iter = g_list_next(iter))
351 {
352 AgService *service = (AgService*)iter->data;
353 servList.append(Service(service, StealReference));
354 }
355
356 g_list_free(list);
357
358 return servList;
359 }
360
367 Provider Manager::provider(const QString &providerName) const
368 {
369 AgProvider *provider;
370
371 provider = ag_manager_get_provider(d->m_manager,
372 providerName.toUtf8().constData());
373 return Provider(provider, StealReference);
374 }
375
381 ProviderList Manager::providerList() const
382 {
383 GList *list;
384
385 list = ag_manager_list_providers(d->m_manager);
386
387 /* convert glist -> ProviderList */
388 ProviderList provList;
389 GList *iter;
390
391 for (iter = list; iter; iter = g_list_next(iter))
392 {
393 AgProvider *provider = (AgProvider*)iter->data;
394 provList.append(Provider(provider, StealReference));
395 }
396
397 g_list_free(list);
398
399 return provList;
400 }
401
408 ServiceType Manager::serviceType(const QString &name) const
409 {
410 AgServiceType *type;
411 type = ag_manager_load_service_type(d->m_manager,
412 name.toUtf8().constData());
413 return ServiceType(type, StealReference);
414 }
415
423 Application Manager::application(const QString &applicationName) const
424 {
425 QByteArray ba = applicationName.toUtf8();
426 AgApplication *application =
427 ag_manager_get_application(d->m_manager, ba.constData());
428 return Application(application);
429 }
430
437 ApplicationList Manager::applicationList(const Service &service) const
438 {
439 ApplicationList ret;
440 GList *applications, *list;
441
442 applications = ag_manager_list_applications_by_service(d->m_manager,
443 service.service());
444 for (list = applications; list != NULL; list = list->next) {
445 AgApplication *application = (AgApplication *)list->data;
446 ret.append(Application(application));
447 }
448 g_list_free (applications);
449 return ret;
450 }
451
457 QString Manager::serviceType() const
458 {
459 return UTF8(ag_manager_get_service_type (d->m_manager));
460 }
461
471 void Manager::setTimeout(quint32 timeout)
472 {
473 ag_manager_set_db_timeout(d->m_manager, timeout);
474 }
475
480 quint32 Manager::timeout()
481 {
482 return ag_manager_get_db_timeout(d->m_manager);
483 }
484
489 void Manager::setAbortOnTimeout(bool abort)
490 {
491 ag_manager_set_abort_on_db_timeout(d->m_manager, abort);
492 }
493
498 bool Manager::abortOnTimeout() const
499 {
500 return ag_manager_get_abort_on_db_timeout(d->m_manager);
501 }
502
512 Error Manager::lastError() const
513 {
514 return d->lastError;
515 }
516
Accounts::Manager::createAccount
Account * createAccount(const QString &providerName)
Creates a new account.
Definition: manager.cpp:306
Accounts::Manager::account
Account * account(const AccountId &id) const
Loads an account from the database.
Definition: manager.cpp:220
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:489
Accounts::Manager::applicationList
ApplicationList applicationList(const Service &service) const
List the registered applications which support the given service.
Definition: manager.cpp:437
Accounts::Provider
Representation of an account provider.
Definition: provider.h:48
Accounts::Manager::abortOnTimeout
bool abortOnTimeout() const
Definition: manager.cpp:498
Accounts::Manager::~Manager
~Manager()
Destructor.
Definition: manager.cpp:192
Accounts::Manager::accountList
AccountIdList accountList(const QString &serviceType=QString::null) const
Lists the accounts which support the requested service.
Definition: manager.cpp:241
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::serviceType
QString serviceType() const
Gets the service type if given in manager constructor.
Definition: manager.cpp:457
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:276
Accounts::Manager::setTimeout
void setTimeout(quint32 timeout)
Sets the timeout for database operations.
Definition: manager.cpp:471
Accounts::Manager::serviceList
ServiceList serviceList(const QString &serviceType=QString::null) const
Gets the service list.
Definition: manager.cpp:335
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:317
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:367
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:512
Accounts::Manager::timeout
quint32 timeout()
Gets the database timeout.
Definition: manager.cpp:480
Accounts::Manager::application
Application application(const QString &applicationName) const
Get an object representing an application.
Definition: manager.cpp:423
Accounts::Manager::providerList
ProviderList providerList() const
Gets a provider list.
Definition: manager.cpp:381