From 08158ca405d9e9504c6e5cd2761ff6ae4e2f29dd Mon Sep 17 00:00:00 2001 From: Teo Mrnjavac Date: Tue, 11 Jun 2013 18:08:45 +0200 Subject: [PATCH] ConfigStorage is now in charge of initializing its own account credentials in CredentialsManager. --- src/libtomahawk/CMakeLists.txt | 1 + src/libtomahawk/accounts/AccountManager.cpp | 12 +--- src/libtomahawk/accounts/ConfigStorage.cpp | 41 +++++++++++++ src/libtomahawk/accounts/ConfigStorage.h | 11 ++-- .../accounts/CredentialsManager.cpp | 57 +++++++++++-------- src/libtomahawk/accounts/CredentialsManager.h | 21 ++++--- .../accounts/LocalConfigStorage.cpp | 21 ++++++- src/libtomahawk/accounts/LocalConfigStorage.h | 4 ++ .../telepathy/TelepathyConfigStorage.cpp | 1 - 9 files changed, 118 insertions(+), 51 deletions(-) create mode 100644 src/libtomahawk/accounts/ConfigStorage.cpp diff --git a/src/libtomahawk/CMakeLists.txt b/src/libtomahawk/CMakeLists.txt index 8dd9992db..21777479a 100644 --- a/src/libtomahawk/CMakeLists.txt +++ b/src/libtomahawk/CMakeLists.txt @@ -219,6 +219,7 @@ list(APPEND libSources accounts/AccountFactoryWrapper.cpp accounts/AccountFactoryWrapperDelegate.cpp accounts/AccountConfigWidget.cpp + accounts/ConfigStorage.cpp accounts/LocalConfigStorage.cpp accounts/spotify/SpotifyAccount.cpp diff --git a/src/libtomahawk/accounts/AccountManager.cpp b/src/libtomahawk/accounts/AccountManager.cpp index 7b758e085..ec8776a13 100644 --- a/src/libtomahawk/accounts/AccountManager.cpp +++ b/src/libtomahawk/accounts/AccountManager.cpp @@ -286,19 +286,13 @@ void AccountManager::loadFromConfig() { m_creds = new CredentialsManager( this ); - ConfigStorage* configStorage; - configStorage = new LocalConfigStorage( this ); //registers with CredentialsManager in the ctor + ConfigStorage* configStorage = new LocalConfigStorage( this ); //registers with CredentialsManager in the ctor m_configStorageById.insert( configStorage->id(), configStorage ); - QStringList accountIds; - foreach ( ConfigStorage* cs, m_configStorageById ) - accountIds << cs->accountIds(); - qDebug() << "LOADING ALL CREDENTIALS" << accountIds; - - NewClosure( m_creds, SIGNAL( ready() ), + //TODO: when we get more than one CS, hook them all up to continue with account loading + NewClosure( configStorage, SIGNAL( ready() ), this, SLOT( finishLoadingFromConfig() ) ); - m_creds->loadCredentials(); } diff --git a/src/libtomahawk/accounts/ConfigStorage.cpp b/src/libtomahawk/accounts/ConfigStorage.cpp new file mode 100644 index 000000000..8ee9fee76 --- /dev/null +++ b/src/libtomahawk/accounts/ConfigStorage.cpp @@ -0,0 +1,41 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, Teo Mrnjavac + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ + +#include "ConfigStorage.h" + + + +namespace Tomahawk +{ + +namespace Accounts +{ + +ConfigStorage::ConfigStorage( QObject* parent ) + : QObject( parent ) +{ +} + + +ConfigStorage::~ConfigStorage() +{ +} + + +} //ns +} //ns diff --git a/src/libtomahawk/accounts/ConfigStorage.h b/src/libtomahawk/accounts/ConfigStorage.h index ad95fe5a7..69a531186 100644 --- a/src/libtomahawk/accounts/ConfigStorage.h +++ b/src/libtomahawk/accounts/ConfigStorage.h @@ -29,14 +29,13 @@ namespace Tomahawk namespace Accounts { -class ConfigStorage : public QObject +class DLLEXPORT ConfigStorage : public QObject { + Q_OBJECT public: - explicit ConfigStorage( QObject* parent ) - : QObject( parent ) - {} + explicit ConfigStorage( QObject* parent = 0 ); - virtual ~ConfigStorage() {} + virtual ~ConfigStorage(); virtual QString id() const = 0; @@ -46,6 +45,8 @@ public: virtual void load( const QString& accountId, Account::Configuration& cfg ) = 0; virtual void remove( const QString& accountId ) = 0; +signals: + void ready(); //emit this when done with whatever it is that needs to be initialized }; } //namespace Accounts diff --git a/src/libtomahawk/accounts/CredentialsManager.cpp b/src/libtomahawk/accounts/CredentialsManager.cpp index 3f0be86d1..8daed33b7 100644 --- a/src/libtomahawk/accounts/CredentialsManager.cpp +++ b/src/libtomahawk/accounts/CredentialsManager.cpp @@ -71,44 +71,53 @@ CredentialsManager::addService( const QString& service , const QStringList& acco if ( m_services.contains( service ) ) m_services.remove( service ); m_services.insert( service, accountIds ); + loadCredentials( service ); } void -CredentialsManager::loadCredentials() +CredentialsManager::loadCredentials( const QString &service ) { - for ( QHash< QString, QStringList >::const_iterator it = m_services.constBegin(); - it != m_services.constEnd(); ++it ) + + const QStringList& accountIds = m_services.value( service ); + tDebug() << Q_FUNC_INFO << "keys for service" << service << ":" << accountIds; + foreach ( QString key, accountIds ) { - const QString& svcName = it.key(); - const QStringList& accountIds = it.value(); - tDebug() << Q_FUNC_INFO << "keys for service" << svcName << ":" << accountIds; - foreach ( QString key, accountIds ) - { - QKeychain::ReadPasswordJob* j = new QKeychain::ReadPasswordJob( svcName, this ); - j->setKey( key ); - j->setAutoDelete( false ); + QKeychain::ReadPasswordJob* j = new QKeychain::ReadPasswordJob( service, this ); + j->setKey( key ); + j->setAutoDelete( false ); #if defined( Q_OS_UNIX ) && !defined( Q_OS_MAC ) - j->setInsecureFallback( true ); + j->setInsecureFallback( true ); #endif - connect( j, SIGNAL( finished( QKeychain::Job* ) ), - SLOT( keychainJobFinished( QKeychain::Job* ) ) ); - m_readJobs << j; - j->start(); - tDebug() << "Launching QtKeychain readJob for" << key; - } + connect( j, SIGNAL( finished( QKeychain::Job* ) ), + SLOT( keychainJobFinished( QKeychain::Job* ) ) ); + m_readJobs[ service ] << j; + j->start(); + tDebug() << "Launching QtKeychain readJob for" << key; } } -QList< CredentialsStorageKey > -CredentialsManager::keys() const +QStringList +CredentialsManager::keys( const QString& service ) const { - QList< CredentialsStorageKey > keys = m_credentials.keys(); + QStringList keys; + foreach ( const CredentialsStorageKey& k, m_credentials.keys() ) + { + if ( k.service() == service ) + keys << k.key(); + } return keys; } +QStringList +CredentialsManager::services() const +{ + return m_services.keys(); +} + + QVariantHash CredentialsManager::credentials( const CredentialsStorageKey& key ) const { @@ -197,11 +206,11 @@ CredentialsManager::keychainJobFinished( QKeychain::Job* j ) tDebug() << "QtKeychain readJob for" << readJob->key() << "finished with error:" << j->error() << j->errorString(); } - m_readJobs.removeOne( readJob ); + m_readJobs[ readJob->service() ].removeOne( readJob ); - if ( m_readJobs.isEmpty() ) + if ( m_readJobs[ readJob->service() ].isEmpty() ) { - emit ready(); + emit serviceReady( readJob->service() ); } } else if ( QKeychain::WritePasswordJob* writeJob = qobject_cast< QKeychain::WritePasswordJob* >( j ) ) diff --git a/src/libtomahawk/accounts/CredentialsManager.h b/src/libtomahawk/accounts/CredentialsManager.h index eaf7b4f46..5e21b4da8 100644 --- a/src/libtomahawk/accounts/CredentialsManager.h +++ b/src/libtomahawk/accounts/CredentialsManager.h @@ -55,10 +55,6 @@ private: /** * @brief The CredentialsManager class holds an in-memory cache of whatever credentials are stored * in the system's QtKeychain-accessible credentials storage. - * After instantiating the class, loadCredentials should be called, and this is the only time a read - * operation from QtKeychain is performed. When CredentialsManager emits ready(), it can be used for - * all other operations. The only QtKeychain operations performed at any time after startup are - * write and delete. * This ensures an illusion of synchronous operations for Tomahawk's Account classes, even though all * QtKeychain jobs are async. */ @@ -70,25 +66,28 @@ public: void addService( const QString& service, const QStringList& accountIds ); - void loadCredentials(); + QStringList keys( const QString& service ) const; + QStringList services() const; - QList< CredentialsStorageKey > keys() const; - - QVariantHash credentials( const CredentialsStorageKey& key ) const; QVariantHash credentials( const QString& serviceName, const QString& key ) const; - void setCredentials( const CredentialsStorageKey& key, const QVariantHash& value ); void setCredentials( const QString& serviceName, const QString& key, const QVariantHash& value ); signals: - void ready(); + void serviceReady( const QString& service ); private slots: + void loadCredentials( const QString& service ); + void keychainJobFinished( QKeychain::Job* ); +protected: + QVariantHash credentials( const CredentialsStorageKey& key ) const; + void setCredentials( const CredentialsStorageKey& key, const QVariantHash& value ); + private: QHash< QString, QStringList > m_services; QHash< CredentialsStorageKey, QVariantHash > m_credentials; - QList< QKeychain::ReadPasswordJob* > m_readJobs; + QHash< QString, QList< QKeychain::ReadPasswordJob* > > m_readJobs; QMutex m_mutex; }; diff --git a/src/libtomahawk/accounts/LocalConfigStorage.cpp b/src/libtomahawk/accounts/LocalConfigStorage.cpp index b1581b978..878e722cd 100644 --- a/src/libtomahawk/accounts/LocalConfigStorage.cpp +++ b/src/libtomahawk/accounts/LocalConfigStorage.cpp @@ -21,6 +21,7 @@ #include "Account.h" #include "AccountManager.h" #include "CredentialsManager.h" +#include "utils/Logger.h" namespace Tomahawk { @@ -36,9 +37,27 @@ LocalConfigStorage::LocalConfigStorage( QObject* parent ) m_accountIds = TomahawkSettings::instance()->accounts(); // tell CredentialsManager which account ids it will be writing credentials for and in which svc - // so it can preload them when we call CM::loadCredentials() + + CredentialsManager* cm = AccountManager::instance()->credentialsManager(); + connect( cm, SIGNAL( serviceReady( QString ) ), + this, SLOT( onCredentialsManagerReady( QString ) ) ); AccountManager::instance()->credentialsManager()->addService( m_credentialsServiceName, m_accountIds ); + + tDebug() << Q_FUNC_INFO << "LOADING ALL CREDENTIALS FOR SERVICE" << m_credentialsServiceName << m_accountIds; +} + + +void +LocalConfigStorage::onCredentialsManagerReady( const QString& service ) +{ + if ( service != m_credentialsServiceName ) + return; + + //no need to listen for it any more + disconnect( this, SLOT( onCredentialsManagerReady( QString ) ) ); + + emit ready(); } diff --git a/src/libtomahawk/accounts/LocalConfigStorage.h b/src/libtomahawk/accounts/LocalConfigStorage.h index 31f04d0b0..3540bebd9 100644 --- a/src/libtomahawk/accounts/LocalConfigStorage.h +++ b/src/libtomahawk/accounts/LocalConfigStorage.h @@ -41,6 +41,10 @@ public: virtual void load( const QString& accountId, Account::Configuration& cfg ); virtual void remove( const QString& accountId ); + +private slots: + void onCredentialsManagerReady( const QString& service ); + private: const QString m_credentialsServiceName; QStringList m_accountIds; diff --git a/src/libtomahawk/accounts/configstorage/telepathy/TelepathyConfigStorage.cpp b/src/libtomahawk/accounts/configstorage/telepathy/TelepathyConfigStorage.cpp index acf2cb582..ecae779bf 100644 --- a/src/libtomahawk/accounts/configstorage/telepathy/TelepathyConfigStorage.cpp +++ b/src/libtomahawk/accounts/configstorage/telepathy/TelepathyConfigStorage.cpp @@ -37,7 +37,6 @@ TelepathyConfigStorage::TelepathyConfigStorage( QObject* parent ) { tDebug() << Q_FUNC_INFO; - // tell CredentialsManager which account ids it will be writing credentials for and in which svc // so it can preload them when we call CM::loadCredentials() AccountManager::instance()->credentialsManager()->addService( m_credentialsServiceName,