libaccounts-qt  1.1
account-service.cpp
00001 /* vi: set et sw=4 ts=4 cino=t0,(0: */
00002 /*
00003  * This file is part of libaccounts-qt
00004  *
00005  * Copyright (C) 2009-2010 Nokia Corporation.
00006  *
00007  * Contact: Alberto Mardegan <alberto.mardegan@nokia.com>
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public License
00011  * version 2.1 as published by the Free Software Foundation.
00012  *
00013  * This library is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00021  * 02110-1301 USA
00022  */
00023 
00024 #include "account-service.h"
00025 #include "manager.h"
00026 #include "utils.h"
00027 #include <libaccounts-glib/ag-account.h>
00028 #include <libaccounts-glib/ag-account-service.h>
00029 #include <libaccounts-glib/ag-auth-data.h>
00030 
00031 namespace Accounts
00032 {
00033 
00105 class AccountServicePrivate
00106 {
00107     Q_DECLARE_PUBLIC(AccountService)
00108 
00109 public:
00110     AccountServicePrivate(Account *account,
00111                           Service *service,
00112                           AccountService *accountService);
00113     ~AccountServicePrivate();
00114 
00115 private:
00116     static void onEnabled(AccountService *accountService, gboolean isEnabled);
00117     static void onChanged(AccountService *accountService);
00118 
00119     ServiceList m_serviceList;
00120     AgAccountService *m_accountService;
00121     Manager *m_manager;
00122     QString prefix;
00123     mutable AccountService *q_ptr;
00124 };
00125 
00126 } // namespace
00127 
00128 using namespace Accounts;
00129 
00130 static QChar slash = QChar::fromLatin1('/');
00131 
00132 AccountServicePrivate::AccountServicePrivate(Account *account,
00133                                              Service *service,
00134                                              AccountService *accountService):
00135     m_manager(account->manager()),
00136     q_ptr(accountService)
00137 {
00138     m_accountService = ag_account_service_new(account->account(),
00139                                               service->service());
00140     g_signal_connect_swapped(m_accountService, "enabled",
00141                              G_CALLBACK(&onEnabled), accountService);
00142     g_signal_connect_swapped(m_accountService, "changed",
00143                              G_CALLBACK(&onChanged), accountService);
00144 }
00145 
00146 AccountServicePrivate::~AccountServicePrivate()
00147 {
00148     Q_Q(AccountService);
00149     g_signal_handlers_disconnect_by_func(m_accountService,
00150                                          (void *)&onEnabled, q);
00151     g_signal_handlers_disconnect_by_func(m_accountService,
00152                                          (void *)&onChanged, q);
00153     g_object_unref(m_accountService);
00154     m_accountService = 0;
00155 }
00156 
00157 void AccountServicePrivate::onEnabled(AccountService *accountService,
00158                                       gboolean isEnabled)
00159 {
00160     TRACE();
00161 
00162     Q_EMIT accountService->enabled(isEnabled);
00163 }
00164 
00165 void AccountServicePrivate::onChanged(AccountService *accountService)
00166 {
00167     TRACE();
00168 
00169     Q_EMIT accountService->changed();
00170 }
00171 
00177 AccountService::AccountService(Account *account, Service *service):
00178     d_ptr(new AccountServicePrivate(account, service, this))
00179 {
00180 }
00181 
00185 AccountService::~AccountService()
00186 {
00187     delete d_ptr;
00188 }
00189 
00193 Account *AccountService::account() const
00194 {
00195     Q_D(const AccountService);
00196     AgAccount *account = ag_account_service_get_account(d->m_accountService);
00197     AgAccountId account_id = account->id;
00198 
00199     return d->m_manager->account(account_id);
00200 }
00201 
00205 Service *AccountService::service() const
00206 {
00207     Q_D(const AccountService);
00208     AgService *service = ag_account_service_get_service(d->m_accountService);
00209     return d->m_manager->serviceInstance(service);
00210 }
00211 
00215 bool AccountService::enabled() const
00216 {
00217     Q_D(const AccountService);
00218     return ag_account_service_get_enabled(d->m_accountService);
00219 }
00220 
00224 QStringList AccountService::allKeys() const
00225 {
00226     Q_D(const AccountService);
00227     QStringList allKeys;
00228     AgAccountSettingIter iter;
00229     const gchar *key;
00230     const GValue *val;
00231 
00232     /* iterate the settings */
00233     QByteArray tmp = d->prefix.toLatin1();
00234     ag_account_service_settings_iter_init(d->m_accountService,
00235                                           &iter, tmp.constData());
00236     while (ag_account_service_settings_iter_next(&iter, &key, &val))
00237     {
00238         allKeys.append(ASCII(key));
00239     }
00240     return allKeys;
00241 }
00242 
00247 void AccountService::beginGroup(const QString &prefix)
00248 {
00249     Q_D(AccountService);
00250     d->prefix += prefix + slash;
00251 }
00252 
00256 QStringList AccountService::childGroups() const
00257 {
00258     QStringList groups, all_keys;
00259 
00260     all_keys = allKeys();
00261     foreach (QString key, all_keys)
00262     {
00263         if (key.contains(slash)) {
00264             QString group = key.section(slash, 0, 0);
00265             if (!groups.contains(group))
00266                 groups.append(group);
00267         }
00268     }
00269     return groups;
00270 }
00271 
00275 QStringList AccountService::childKeys() const
00276 {
00277     QStringList keys, all_keys;
00278 
00279     all_keys = allKeys();
00280     foreach (QString key, all_keys)
00281     {
00282         if (!key.contains(slash))
00283             keys.append(key);
00284     }
00285     return keys;
00286 }
00287 
00292 void AccountService::clear()
00293 {
00294     Q_D(AccountService);
00295     /* clear() must ignore the group: so, temporarily reset it and call
00296      * remove("") */
00297     QString saved_prefix = d->prefix;
00298     d->prefix = QString();
00299     remove(QString());
00300     d->prefix = saved_prefix;
00301 }
00302 
00307 bool AccountService::contains(const QString &key) const
00308 {
00309     return childKeys().contains(key);
00310 }
00311 
00315 void AccountService::endGroup()
00316 {
00317     Q_D(AccountService);
00318     d->prefix = d->prefix.section(slash, 0, -3,
00319                                   QString::SectionIncludeTrailingSep);
00320     if (d->prefix[0] == slash) d->prefix.remove(0, 1);
00321 }
00322 
00326 QString AccountService::group() const
00327 {
00328     Q_D(const AccountService);
00329     if (d->prefix.endsWith(slash))
00330         return d->prefix.left(d->prefix.size() - 1);
00331     return d->prefix;
00332 }
00333 
00339 void AccountService::remove(const QString &key)
00340 {
00341     Q_D(AccountService);
00342     if (key.isEmpty())
00343     {
00344         /* delete all keys in the group */
00345         QStringList keys = allKeys();
00346         foreach (QString key, keys)
00347         {
00348             if (!key.isEmpty())
00349                 remove(key);
00350         }
00351     }
00352     else
00353     {
00354         QString full_key = d->prefix + key;
00355         QByteArray tmpkey = full_key.toLatin1();
00356         ag_account_service_set_value(d->m_accountService,
00357                                      tmpkey.constData(),
00358                                      NULL);
00359     }
00360 }
00361 
00367 void AccountService::setValue(const QString &key, const QVariant &value)
00368 {
00369     Q_D(AccountService);
00370     TRACE();
00371     GValue val= {0, {{0}}};
00372 
00373     if (!variantToGValue(value, &val)) {
00374         return;
00375     }
00376 
00377     QString full_key = d->prefix + key;
00378     QByteArray tmpkey = full_key.toLatin1();
00379     ag_account_service_set_value(d->m_accountService,
00380                                  tmpkey.constData(),
00381                                  &val);
00382     g_value_unset(&val);
00383 }
00384 
00385 void AccountService::setValue(const char *key, const QVariant &value)
00386 {
00387     setValue(ASCII(key), value);
00388 }
00389 
00398 QVariant AccountService::value(const QString &key, SettingSource *source) const
00399 {
00400     Q_D(const AccountService);
00401     GValue val= {0, {{0}}};
00402     g_value_init(&val, G_TYPE_STRING);
00403     QString full_key = d->prefix + key;
00404     QByteArray ba = full_key.toLatin1();
00405     AgSettingSource agSource =
00406         ag_account_service_get_value(d->m_accountService,
00407                                      ba.constData(), &val);
00408     if (source != 0) {
00409         switch (agSource) {
00410         case AG_SETTING_SOURCE_ACCOUNT: *source = ACCOUNT; break;
00411         case AG_SETTING_SOURCE_PROFILE: *source = TEMPLATE; break;
00412         default: *source = NONE; break;
00413         }
00414     }
00415 
00416     QVariant variant;
00417     if (agSource == AG_SETTING_SOURCE_NONE)
00418         return variant;
00419 
00420     variant = UTF8(g_value_get_string(&val));
00421     g_value_unset(&val);
00422 
00423     return variant;
00424 }
00425 
00426 QVariant AccountService::value(const char *key, SettingSource *source) const
00427 {
00428     return value(ASCII(key), source);
00429 }
00430 
00438 QStringList AccountService::changedFields() const
00439 {
00440     Q_D(const AccountService);
00441 
00442     gchar **changedFields =
00443         ag_account_service_get_changed_fields(d->m_accountService);
00444 
00445     QStringList keyList;
00446     if (changedFields == 0)
00447         return keyList;
00448 
00449     gchar **keys = changedFields;
00450     while (*keys != 0) {
00451         keyList.append(QString(ASCII(*keys)));
00452         keys++;
00453     }
00454 
00455     g_strfreev(changedFields);
00456     return keyList;
00457 }
00458 
00468 AuthData AccountService::authData() const
00469 {
00470     Q_D(const AccountService);
00471 
00472     AgAuthData *agAuthData =
00473         ag_account_service_get_auth_data(d->m_accountService);
00474     return AuthData(agAuthData);
00475 }