mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-10 08:04:25 +02:00
ConfigStorage is now in charge of initializing its own account credentials in CredentialsManager.
This commit is contained in:
@@ -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
|
||||
|
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
41
src/libtomahawk/accounts/ConfigStorage.cpp
Normal file
41
src/libtomahawk/accounts/ConfigStorage.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||
*
|
||||
* Copyright 2013, Teo Mrnjavac <teo@kde.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "ConfigStorage.h"
|
||||
|
||||
|
||||
|
||||
namespace Tomahawk
|
||||
{
|
||||
|
||||
namespace Accounts
|
||||
{
|
||||
|
||||
ConfigStorage::ConfigStorage( QObject* parent )
|
||||
: QObject( parent )
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
ConfigStorage::~ConfigStorage()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
} //ns
|
||||
} //ns
|
@@ -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
|
||||
|
@@ -71,21 +71,19 @@ 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 QString& svcName = it.key();
|
||||
const QStringList& accountIds = it.value();
|
||||
tDebug() << Q_FUNC_INFO << "keys for service" << svcName << ":" << accountIds;
|
||||
|
||||
const QStringList& accountIds = m_services.value( service );
|
||||
tDebug() << Q_FUNC_INFO << "keys for service" << service << ":" << accountIds;
|
||||
foreach ( QString key, accountIds )
|
||||
{
|
||||
QKeychain::ReadPasswordJob* j = new QKeychain::ReadPasswordJob( svcName, this );
|
||||
QKeychain::ReadPasswordJob* j = new QKeychain::ReadPasswordJob( service, this );
|
||||
j->setKey( key );
|
||||
j->setAutoDelete( false );
|
||||
#if defined( Q_OS_UNIX ) && !defined( Q_OS_MAC )
|
||||
@@ -93,22 +91,33 @@ CredentialsManager::loadCredentials()
|
||||
#endif
|
||||
connect( j, SIGNAL( finished( QKeychain::Job* ) ),
|
||||
SLOT( keychainJobFinished( QKeychain::Job* ) ) );
|
||||
m_readJobs << j;
|
||||
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 ) )
|
||||
|
@@ -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;
|
||||
};
|
||||
|
||||
|
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
@@ -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;
|
||||
|
@@ -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,
|
||||
|
Reference in New Issue
Block a user