manager.cpp
/* 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-2016 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 "application.h"
#include "service.h"
#include "manager.h"
#include "manager_p.h"
#include <libaccounts-glib/ag-account.h>
namespace Accounts {
} //namespace Accounts
using namespace Accounts;
void Manager::Private::init(Manager *q, AgManager *manager)
{
Q_ASSERT(q_ptr == 0);
Q_ASSERT(m_manager == 0);
q_ptr = q;
m_manager = manager;
if (manager) {
g_signal_connect_swapped
(manager, "account-created",
G_CALLBACK(&Private::on_account_created), q);
g_signal_connect_swapped
(manager, "account-deleted",
G_CALLBACK(&Private::on_account_deleted), q);
g_signal_connect_swapped
(manager, "account-updated",
G_CALLBACK(&Private::on_account_updated), q);
g_signal_connect_swapped
(manager, "enabled-event",
G_CALLBACK(&Private::on_enabled_event), q);
} else {
qWarning() << Q_FUNC_INFO << "Initializing with NULL AgManager!";
}
}
void Manager::Private::on_account_created(Manager *self, AgAccountId id)
{
Q_EMIT self->accountCreated(id);
}
void Manager::Private::on_account_deleted(Manager *self, AgAccountId id)
{
Q_EMIT self->accountRemoved(id);
}
void Manager::Private::on_account_updated(Manager *self, AgAccountId id)
{
Q_EMIT self->accountUpdated(id);
}
void Manager::Private::on_enabled_event(Manager *self, AgAccountId id)
{
Q_EMIT self->enabledEvent(id);
}
Manager::Manager(QObject *parent):
QObject(parent),
d(new Private)
{
AgManager *manager = ag_manager_new();
if (manager != 0) {
d->init(this, manager);
} else {
qWarning() << "Manager could not be created. DB is locked";
d->lastError = Error::DatabaseLocked;
}
}
Manager::Manager(const QString &serviceType, QObject *parent):
QObject(parent),
d(new Private)
{
AgManager *manager =
ag_manager_new_for_service_type(serviceType.toUtf8().constData());
if (manager != 0) {
d->init(this, manager);
} else {
qWarning() << "Manager could not be created, DB is locked";
d->lastError = Error::DatabaseLocked;
}
}
Manager::Manager(Options options, QObject *parent):
QObject(parent),
d(new Private)
{
bool disableNotifications = options.testFlag(DisableNotifications);
GError *error = NULL;
AgManager *manager =
(AgManager *)g_initable_new(AG_TYPE_MANAGER, NULL, &error,
"use-dbus", !disableNotifications,
NULL);
if (Q_LIKELY(manager)) {
d->init(this, manager);
} else {
qWarning() << "Manager could not be created." << error->message;
d->lastError = Error(error);
g_error_free(error);
}
}
Manager::~Manager()
{
g_signal_handlers_disconnect_by_func
(d->m_manager, (void *)&Private::on_enabled_event, this);
g_signal_handlers_disconnect_by_func
(d->m_manager, (void *)&Private::on_account_updated, this);
g_signal_handlers_disconnect_by_func
(d->m_manager, (void *)&Private::on_account_deleted, this);
g_signal_handlers_disconnect_by_func
(d->m_manager, (void *)&Private::on_account_created, this);
g_object_unref(d->m_manager);
delete d;
d = 0;
}
Account *Manager::account(const AccountId &id) const
{
Account *account = d->m_accounts.value(id, 0);
if (account == 0) {
/* Create a new account object */
account = Account::fromId(const_cast<Manager*>(this), id,
const_cast<Manager*>(this));
d->m_accounts[id] = account;
}
return account;
}
AccountIdList Manager::accountList(const QString &serviceType) const
{
GList *list = NULL;
if (serviceType.isEmpty()) {
list = ag_manager_list(d->m_manager);
} else {
list = ag_manager_list_by_service_type(d->m_manager,
serviceType.toUtf8().constData());
}
/* convert glist -> AccountIdList */
AccountIdList idList;
GList *iter;
for (iter = list; iter; iter = g_list_next(iter))
{
idList.append((AccountId)GPOINTER_TO_INT(iter->data));
}
ag_manager_list_free(list);
return idList;
}
AccountIdList Manager::accountListEnabled(const QString &serviceType) const
{
GList *list = NULL;
if (serviceType.isEmpty()) {
list = ag_manager_list_enabled(d->m_manager);
} else {
list = ag_manager_list_enabled_by_service_type(d->m_manager,
serviceType.toUtf8().constData());
}
/* convert glist -> AccountIdList */
AccountIdList idList;
GList *iter;
for (iter = list; iter; iter = g_list_next(iter))
{
idList.append((AccountId)GPOINTER_TO_INT(iter->data));
}
ag_manager_list_free(list);
return idList;
}
Account *Manager::createAccount(const QString &providerName)
{
return new Account(this, providerName, this);
}
Service Manager::service(const QString &serviceName) const
{
AgService *service =
ag_manager_get_service(d->m_manager,
serviceName.toUtf8().constData());
return Service(service, StealReference);
}
ServiceList Manager::serviceList(const QString &serviceType) const
{
GList *list;
if (serviceType.isEmpty()) {
list = ag_manager_list_services(d->m_manager);
} else {
list = ag_manager_list_services_by_type(d->m_manager,
serviceType.toUtf8().constData());
}
/* 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;
}
ServiceList Manager::serviceList(const Application &application) const
{
GList *list;
list = ag_manager_list_services_by_application(d->m_manager,
application.application());
/* 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;
}
Provider Manager::provider(const QString &providerName) const
{
AgProvider *provider;
provider = ag_manager_get_provider(d->m_manager,
providerName.toUtf8().constData());
return Provider(provider, StealReference);
}
ProviderList Manager::providerList() const
{
GList *list;
list = ag_manager_list_providers(d->m_manager);
/* convert glist -> ProviderList */
ProviderList provList;
GList *iter;
for (iter = list; iter; iter = g_list_next(iter))
{
AgProvider *provider = (AgProvider*)iter->data;
provList.append(Provider(provider, StealReference));
}
g_list_free(list);
return provList;
}
ServiceType Manager::serviceType(const QString &name) const
{
AgServiceType *type;
type = ag_manager_load_service_type(d->m_manager,
name.toUtf8().constData());
return ServiceType(type, StealReference);
}
Application Manager::application(const QString &applicationName) const
{
QByteArray ba = applicationName.toUtf8();
AgApplication *application =
ag_manager_get_application(d->m_manager, ba.constData());
return Application(application);
}
ApplicationList Manager::applicationList(const Service &service) const
{
ApplicationList ret;
GList *applications, *list;
applications = ag_manager_list_applications_by_service(d->m_manager,
service.service());
for (list = applications; list != NULL; list = list->next) {
AgApplication *application = (AgApplication *)list->data;
ret.append(Application(application));
}
g_list_free (applications);
return ret;
}
QString Manager::serviceType() const
{
return UTF8(ag_manager_get_service_type (d->m_manager));
}
void Manager::setTimeout(quint32 timeout)
{
ag_manager_set_db_timeout(d->m_manager, timeout);
}
quint32 Manager::timeout()
{
return ag_manager_get_db_timeout(d->m_manager);
}
void Manager::setAbortOnTimeout(bool abort)
{
ag_manager_set_abort_on_db_timeout(d->m_manager, abort);
}
bool Manager::abortOnTimeout() const
{
return ag_manager_get_abort_on_db_timeout(d->m_manager);
}
Manager::Options Manager::options() const
{
gboolean useDBus = true;
g_object_get(d->m_manager,
"use-dbus", &useDBus,
NULL);
Options opts;
if (!useDBus) {
opts |= DisableNotifications;
}
return opts;
}
Error Manager::lastError() const
{
return d->lastError;
}
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::Manager::lastError
Error lastError() const
Gets the last error.
Definition: manager.cpp:583
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
Manager(QObject *parent=0)
Constructor.
Definition: manager.cpp:146
Accounts::Manager
Manager of accounts, services and providers.
Definition: manager.h:51
Accounts::Error
Base object definition for accounts error handling.
Definition: error.h:43
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::null) 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:41
Accounts::Service
Representation of an account service.
Definition: service.h:48
Accounts::Manager::applicationList
ApplicationList applicationList(const Service &service) const
List the registered applications which support the given service.
Definition: manager.cpp:491
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::Error::DatabaseLocked
@ DatabaseLocked
Definition: error.h:55
Accounts::Provider
Representation of an account provider.
Definition: provider.h:48
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