1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
|
/* 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.h>
namespace Accounts
{
/*!
* @class AccountService
* @headerfile account-service.h Accounts/AccountService
*
* @brief Account settings for a specific service
*
* @details The AccountService class provides access to the account settings
* for a specific service type. It is meant to be easier to use than the
* Account class because it hides the complexity of the account structure and
* gives access to only the limited subset of account settings which are
* relevant to a service.
*
* To get an AccountService one can use the Manager methods accountServices()
* or enabledAccountServices(), which both return a QList of account services.
* Note that if the Manager was instantiated for a specific service type, these
* lists will contain only those account services matching that service type.
* The AccountService can also be instantiated with its AccountService(Account
* *account, Service *service) constructor: this is useful if one already has
* an Account instance.
*
* This is intended to be a convenient wrapper over the accounts settings
* specific for a service; as such, it doesn't offer all the editing
* possibilities offered by the Account class, such as enabling the service
* itself: these operations should ideally not be performed by consumer
* applications, but by the account editing UI only.
*
* Example code:
* @code
* // Instantiate an account manager interested in e-mail services only.
* Accounts::Manager *manager = new Accounts::Manager("e-mail");
*
* // Get the list of enabled AccountService objects of type e-mail.
* Accounts::AccountServiceList services = manager->enabledAccountServices();
*
* // Loop through the account services and do something useful with them.
* foreach (Accounts::AccountService service, services) {
* QString server = service.value("pop3/hostname").toString();
* int port = service.value("pop3/port").toInt();
*
* // Suppose that the e-mail address is stored in the global account
* // settings; let's get it from there:
* QString fromAddress = service.account()->valueAsString("username");
*
* ...
* }
* @endcode
*
* @note User applications (with the notable exception of the accounts editing
* application) should never use account services which are not enabled, and
* should stop using an account when the account service becomes disabled. The
* latter can be done by connecting to the changed() signal and checking if
* isEnabled() still returns true.
* @note Note that if the account gets deleted, it will always get disabled
* first; so, there is no need to connect to the Account::removed() signal; one
* can just monitor the changed() signal from the AccountService objects.
*/
/*!
* @fn AccountService::enabled(bool isEnabled)
*
* Emitted when the enabledness state of the account service has changed.
*/
/*!
* @fn AccountService::changed()
* Emitted when some setting has changed on the account service.
* You can use the changedFields() method to retrieve the list of the
* settings which have changed.
*/
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();
}
/*!
* Constructor.
* @param account An Account.
* @param service A Service supported by the account.
*/
AccountService::AccountService(Account *account, const Service &service):
QObject(nullptr),
d_ptr(new AccountServicePrivate(account, service, this))
{
}
/*!
* Constructor.
* @param account An Account.
* @param service A Service supported by the account.
* @param parent The parent object.
*/
AccountService::AccountService(Account *account, const Service &service,
QObject *parent):
QObject(parent),
d_ptr(new AccountServicePrivate(account, service, this))
{
}
/*!
* Destructor.
*/
AccountService::~AccountService()
{
delete d_ptr;
}
/*!
* Return the Account. Do not delete this object explicitly.
*/
Account *AccountService::account() const
{
Q_D(const AccountService);
return d->m_account;
}
/*!
* Return the Service.
*/
Service AccountService::service() const
{
Q_D(const AccountService);
AgService *service = ag_account_service_get_service(d->m_accountService);
return Service(service);
}
/*!
* Check whether the account service is enabled.
* @note this is just a wrapper of isEnabled
* @see isEnabled()
* @deprecated use isEnabled instead
*/
bool AccountService::enabled() const
{
return isEnabled();
}
/*!
* Check whether the account service is enabled.
*/
bool AccountService::isEnabled() const
{
Q_D(const AccountService);
return ag_account_service_get_enabled(d->m_accountService);
}
/*!
* Return all the keys in the current group.
*/
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;
}
/*!
* Enter a group. This method never fails.
* @param prefix
*/
void AccountService::beginGroup(const QString &prefix)
{
Q_D(AccountService);
d->prefix += prefix + slash;
}
/*!
* Return all the groups which are direct children of the current group.
*/
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;
}
/*!
* Return all the keys which are direct children of the current group.
*/
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;
}
/*!
* Remove all the keys.
* @see remove(const QString &key)
*/
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;
}
/*!
* Check whether the given key is in the current group.
* @param key The key name of the setting.
*/
bool AccountService::contains(const QString &key) const
{
return childKeys().contains(key);
}
/*!
* Exit a group.
*/
void AccountService::endGroup()
{
Q_D(AccountService);
d->prefix = d->prefix.section(slash, 0, -3,
QString::SectionIncludeTrailingSep);
if (!d->prefix.isEmpty() && d->prefix[0] == slash) d->prefix.remove(0, 1);
}
/*!
* Return the name of the current group.
*/
QString AccountService::group() const
{
Q_D(const AccountService);
if (d->prefix.endsWith(slash))
return d->prefix.left(d->prefix.size() - 1);
return d->prefix;
}
/*!
* Remove the given key. If the key is the empty string, all keys in the
* current group are removed.
* @param key The key name of the setting.
*/
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);
}
}
/*!
* Change the value of an account setting.
* @param key The name of the setting.
* @param value The new value of the setting.
*/
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);
}
/*!
* Retrieves the value of an account setting, as a QVariant.
* @param key The key whose value must be retrieved.
* @param defaultValue Value returned if the key is unset.
* @param source Indicates whether the value comes from the account, the
* service template or was unset.
*
* @return The value associated to \a key.
*
* This method operates on the currently selected service.
*/
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;
}
/*!
* Retrieves the value of an account setting.
* @param key The key whose value must be retrieved
* @param source Indicates whether the value comes from the account, the
* service template or was unset.
*
* Returns the value of the setting, or an invalid QVariant if unset.
*/
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);
}
/*!
* This method should be called only in the context of a handler of the
* AccountService::changed() signal, and can be used to retrieve the set of
* changes.
*
* @return a QStringList of the keys which have changed.
*/
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;
}
/*!
* Read the authentication data stored in the account (merging the
* service-specific settings with the global account settings).
* The method and mechanism are read from the "auth/method" and
* "auth/mechanism" keys, respectively. The authentication parameters are
* found under the "auth/<method>/<mechanism>/" group.
*
* @return an AuthData object, describing the authentication settings.
*/
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;
}
|