account-service.cpp
/* vi: set et sw=4 ts=4 cino=t0,(0: */
/*
* This file is part of libaccounts-qt
*
* Copyright (C) 2009-2010 Nokia Corporation.
* Copyright (C) 2013-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 "account-service.h"
#include "manager.h"
#include "utils.h"
#include <QPointer>
#include <libaccounts-glib/ag-account.h>
#include <libaccounts-glib/ag-account-service.h>
#include <libaccounts-glib/ag-auth-data.h>
namespace Accounts
{
class AccountServicePrivate
{
Q_DECLARE_PUBLIC(AccountService)
public:
AccountServicePrivate(Account *account,
const Service &service,
AccountService *accountService);
~AccountServicePrivate();
private:
static void onEnabled(AccountService *accountService, gboolean isEnabled);
static void onChanged(AccountService *accountService);
ServiceList m_serviceList;
AgAccountService *m_accountService;
QPointer<Account> m_account;
QString prefix;
mutable AccountService *q_ptr;
};
} // namespace
using namespace Accounts;
static QChar slash = QChar::fromLatin1('/');
AccountServicePrivate::AccountServicePrivate(Account *account,
const Service &service,
AccountService *accountService):
m_account(account),
q_ptr(accountService)
{
m_accountService = ag_account_service_new(account->account(),
service.service());
g_signal_connect_swapped(m_accountService, "enabled",
G_CALLBACK(&onEnabled), accountService);
g_signal_connect_swapped(m_accountService, "changed",
G_CALLBACK(&onChanged), accountService);
}
AccountServicePrivate::~AccountServicePrivate()
{
Q_Q(AccountService);
g_signal_handlers_disconnect_by_func(m_accountService,
(void *)&onEnabled, q);
g_signal_handlers_disconnect_by_func(m_accountService,
(void *)&onChanged, q);
g_object_unref(m_accountService);
m_accountService = nullptr;
}
void AccountServicePrivate::onEnabled(AccountService *accountService,
gboolean isEnabled)
{
Q_EMIT accountService->enabled(isEnabled);
}
void AccountServicePrivate::onChanged(AccountService *accountService)
{
Q_EMIT accountService->changed();
}
AccountService::AccountService(Account *account, const Service &service):
QObject(nullptr),
d_ptr(new AccountServicePrivate(account, service, this))
{
}
AccountService::AccountService(Account *account, const Service &service,
QObject *parent):
QObject(parent),
d_ptr(new AccountServicePrivate(account, service, this))
{
}
AccountService::~AccountService()
{
delete d_ptr;
}
Account *AccountService::account() const
{
Q_D(const AccountService);
return d->m_account;
}
Service AccountService::service() const
{
Q_D(const AccountService);
AgService *service = ag_account_service_get_service(d->m_accountService);
return Service(service);
}
bool AccountService::enabled() const
{
return isEnabled();
}
bool AccountService::isEnabled() const
{
Q_D(const AccountService);
return ag_account_service_get_enabled(d->m_accountService);
}
QStringList AccountService::allKeys() const
{
Q_D(const AccountService);
QStringList allKeys;
AgAccountSettingIter iter;
const gchar *key;
GVariant *val;
/* iterate the settings */
QByteArray tmp = d->prefix.toLatin1();
ag_account_service_settings_iter_init(d->m_accountService,
&iter, tmp.constData());
while (ag_account_settings_iter_get_next(&iter, &key, &val))
{
allKeys.append(ASCII(key));
}
return allKeys;
}
void AccountService::beginGroup(const QString &prefix)
{
Q_D(AccountService);
d->prefix += prefix + slash;
}
QStringList AccountService::childGroups() const
{
QStringList groups, all_keys;
all_keys = allKeys();
Q_FOREACH (const QString &key, all_keys)
{
if (key.contains(slash)) {
QString group = key.section(slash, 0, 0);
if (!groups.contains(group))
groups.append(group);
}
}
return groups;
}
QStringList AccountService::childKeys() const
{
QStringList keys, all_keys;
all_keys = allKeys();
Q_FOREACH (const QString &key, all_keys)
{
if (!key.contains(slash))
keys.append(key);
}
return keys;
}
void AccountService::clear()
{
Q_D(AccountService);
/* clear() must ignore the group: so, temporarily reset it and call
* remove("") */
QString saved_prefix = d->prefix;
d->prefix = QString();
remove(QString());
d->prefix = saved_prefix;
}
bool AccountService::contains(const QString &key) const
{
return childKeys().contains(key);
}
void AccountService::endGroup()
{
Q_D(AccountService);
d->prefix = d->prefix.section(slash, 0, -3,
QString::SectionIncludeTrailingSep);
if (d->prefix[0] == slash) d->prefix.remove(0, 1);
}
QString AccountService::group() const
{
Q_D(const AccountService);
if (d->prefix.endsWith(slash))
return d->prefix.left(d->prefix.size() - 1);
return d->prefix;
}
void AccountService::remove(const QString &key)
{
Q_D(AccountService);
if (key.isEmpty())
{
/* delete all keys in the group */
QStringList keys = allKeys();
Q_FOREACH (const QString &key, keys)
{
if (!key.isEmpty())
remove(key);
}
}
else
{
QString full_key = d->prefix + key;
QByteArray tmpkey = full_key.toLatin1();
ag_account_service_set_variant(d->m_accountService,
tmpkey.constData(),
NULL);
}
}
void AccountService::setValue(const QString &key, const QVariant &value)
{
Q_D(AccountService);
GVariant *variant = qVariantToGVariant(value);
if (variant == nullptr) {
return;
}
QString full_key = d->prefix + key;
QByteArray tmpkey = full_key.toLatin1();
ag_account_service_set_variant(d->m_accountService,
tmpkey.constData(),
variant);
}
void AccountService::setValue(const char *key, const QVariant &value)
{
setValue(ASCII(key), value);
}
QVariant AccountService::value(const QString &key,
const QVariant &defaultValue,
SettingSource *source) const
{
Q_D(const AccountService);
QString full_key = d->prefix + key;
QByteArray ba = full_key.toLatin1();
AgSettingSource settingSource;
GVariant *variant =
ag_account_service_get_variant(d->m_accountService,
ba.constData(),
&settingSource);
if (source != nullptr) {
switch (settingSource) {
case AG_SETTING_SOURCE_ACCOUNT: *source = ACCOUNT; break;
case AG_SETTING_SOURCE_PROFILE: *source = TEMPLATE; break;
default: *source = NONE; break;
}
}
return (variant != nullptr) ? gVariantToQVariant(variant) : defaultValue;
}
QVariant AccountService::value(const QString &key, SettingSource *source) const
{
return value(key, QVariant(), source);
}
QVariant AccountService::value(const char *key, SettingSource *source) const
{
return value(ASCII(key), source);
}
QStringList AccountService::changedFields() const
{
Q_D(const AccountService);
gchar **changedFields =
ag_account_service_get_changed_fields(d->m_accountService);
QStringList keyList;
if (changedFields == nullptr)
return keyList;
gchar **keys = changedFields;
while (*keys != nullptr) {
keyList.append(QString(ASCII(*keys)));
keys++;
}
g_strfreev(changedFields);
return keyList;
}
AuthData AccountService::authData() const
{
Q_D(const AccountService);
AgAuthData *agAuthData =
ag_account_service_get_auth_data(d->m_accountService);
AuthData authData(agAuthData);
ag_auth_data_unref(agAuthData);
return authData;
}
Accounts::AccountService::AccountService
AccountService(Account *account, const Service &service)
Constructor.
Definition: account-service.cpp:175
Accounts::AuthData
Information for account authentication.
Definition: auth-data.h:50
Accounts::AccountService::childKeys
QStringList childKeys() const
Return all the keys which are direct children of the current group.
Definition: account-service.cpp:295
Accounts::AccountService::endGroup
void endGroup()
Exit a group.
Definition: account-service.cpp:335
Accounts::AccountService::isEnabled
bool isEnabled() const
Check whether the account service is enabled.
Definition: account-service.cpp:235
Accounts::AccountService::value
QVariant value(const QString &key, const QVariant &defaultValue, SettingSource *source=nullptr) const
Retrieves the value of an account setting, as a QVariant.
Definition: account-service.cpp:419
Accounts::AccountService::group
QString group() const
Return the name of the current group.
Definition: account-service.cpp:346
Accounts::AccountService::clear
void clear()
Remove all the keys.
Definition: account-service.cpp:312
Accounts::AccountService::enabled
bool enabled() const
Check whether the account service is enabled.
Definition: account-service.cpp:227
Accounts::AccountService::changedFields
QStringList changedFields() const
This method should be called only in the context of a handler of the AccountService::changed() signal...
Definition: account-service.cpp:467
Accounts::AccountService::account
Account * account() const
Return the Account.
Definition: account-service.cpp:205
Accounts::AccountService::~AccountService
virtual ~AccountService()
Destructor.
Definition: account-service.cpp:197
Accounts::AccountService::remove
void remove(const QString &key)
Remove the given key.
Definition: account-service.cpp:359
Accounts::Service
Representation of an account service.
Definition: service.h:49
Accounts::AccountService::service
Service service() const
Return the Service.
Definition: account-service.cpp:214
Accounts::AccountService::contains
bool contains(const QString &key) const
Check whether the given key is in the current group.
Definition: account-service.cpp:327
Accounts::AccountService
Account settings for a specific service.
Definition: account-service.h:40
Accounts::AccountService::beginGroup
void beginGroup(const QString &prefix)
Enter a group.
Definition: account-service.cpp:267
Accounts::AccountService::changed
void changed()
Emitted when some setting has changed on the account service.
Accounts::AccountService::childGroups
QStringList childGroups() const
Return all the groups which are direct children of the current group.
Definition: account-service.cpp:276
Accounts::AccountService::authData
AuthData authData() const
Read the authentication data stored in the account (merging the service-specific settings with the gl...
Definition: account-service.cpp:497
Accounts::AccountService::allKeys
QStringList allKeys() const
Return all the keys in the current group.
Definition: account-service.cpp:244