1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-21 05:11:44 +02:00

Added ConfigStorage and LocalConfigStorage, preparation for Telepathy.

This commit is contained in:
Teo Mrnjavac
2013-06-02 17:41:08 +02:00
parent 09a20e98d1
commit 63f9168b4a
11 changed files with 327 additions and 98 deletions

View File

@@ -52,7 +52,7 @@ public:
QString description() const { return tr( "Log on to your Jabber/XMPP account to connect to your friends" ); }
QString factoryId() const { return "xmppaccount"; }
QPixmap icon() const { return QPixmap( ":/xmpp-account/xmpp-icon.png" ); }
AccountTypes types() const { return AccountTypes( SipType | StatusPushType ); };
AccountTypes types() const { return AccountTypes( SipType | StatusPushType ); }
Account* createAccount( const QString& pluginId = QString() );
};

View File

@@ -219,6 +219,7 @@ list(APPEND libSources
accounts/AccountFactoryWrapper.cpp
accounts/AccountFactoryWrapperDelegate.cpp
accounts/AccountConfigWidget.cpp
accounts/LocalConfigStorage.cpp
accounts/spotify/SpotifyAccount.cpp
accounts/spotify/SpotifyAccountConfig.cpp

View File

@@ -2,6 +2,7 @@
*
* Copyright 2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2011, Leo Franchi <lfranchi@kde.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
@@ -19,9 +20,9 @@
#include "Account.h"
#include "TomahawkSettings.h"
#include "AccountManager.h"
#include "CredentialsManager.h"
#include "ConfigStorage.h"
namespace Tomahawk
{
@@ -51,10 +52,11 @@ accountTypeToString( AccountType type )
Account::Account( const QString& accountId )
: QObject()
, m_enabled( false )
, m_accountId( accountId )
{
connect( this, SIGNAL( error( int, QString ) ), this, SLOT( onError( int,QString ) ) );
m_cfg.enabled = false;
connect( this, SIGNAL( error( int, QString ) ), this, SLOT( onError( int, QString ) ) );
connect( this, SIGNAL( connectionStateChanged( Tomahawk::Accounts::Account::ConnectionState ) ) , this, SLOT( onConnectionStateChanged( Tomahawk::Accounts::Account::ConnectionState ) ) );
loadFromConfig( accountId );
@@ -132,18 +134,7 @@ Account::onConnectionStateChanged( Account::ConnectionState )
void
Account::syncConfig()
{
TomahawkSettings* s = TomahawkSettings::instance();
s->beginGroup( "accounts/" + m_accountId );
s->setValue( "accountfriendlyname", m_accountFriendlyName );
s->setValue( "enabled", m_enabled );
s->setValue( "configuration", m_configuration );
s->setValue( "acl", m_acl );
s->setValue( "types", m_types );
s->endGroup();
s->sync();
CredentialsManager* c = AccountManager::instance()->credentialsManager();
c->setCredentials( "Tomahawk", m_accountId, m_credentials );
AccountManager::instance()->configStorageForAccount( m_accountId )->save( m_accountId, m_cfg );
}
@@ -151,35 +142,15 @@ void
Account::loadFromConfig( const QString& accountId )
{
m_accountId = accountId;
TomahawkSettings* s = TomahawkSettings::instance();
s->beginGroup( "accounts/" + m_accountId );
m_accountFriendlyName = s->value( "accountfriendlyname", QString() ).toString();
m_enabled = s->value( "enabled", false ).toBool();
m_configuration = s->value( "configuration", QVariantHash() ).toHash();
m_acl = s->value( "acl", QVariantMap() ).toMap();
m_types = s->value( "types", QStringList() ).toStringList();
s->endGroup();
CredentialsManager* c = AccountManager::instance()->credentialsManager();
m_credentials = c->credentials( "Tomahawk", m_accountId );
AccountManager::instance()->configStorageForAccount( m_accountId )->load( m_accountId, m_cfg );
}
void
Account::removeFromConfig()
{
TomahawkSettings* s = TomahawkSettings::instance();
s->beginGroup( "accounts/" + m_accountId );
s->remove( "accountfriendlyname" );
s->remove( "enabled" );
s->remove( "configuration" );
s->remove( "acl" );
s->remove( "types" );
s->endGroup();
s->remove( "accounts/" + m_accountId );
CredentialsManager* c = AccountManager::instance()->credentialsManager();
c->setCredentials( "Tomahawk", m_accountId, QVariantHash() );
AccountManager::instance()->configStorageForAccount( m_accountId )->remove( m_accountId );
}
@@ -187,15 +158,15 @@ void
Account::setTypes( AccountTypes types )
{
QMutexLocker locker( &m_mutex );
m_types = QStringList();
m_cfg.types = QStringList();
if ( types & InfoType )
m_types << "InfoType";
m_cfg.types << "InfoType";
if ( types & SipType )
m_types << "SipType";
m_cfg.types << "SipType";
if ( types & ResolverType )
m_types << "ResolverType";
m_cfg.types << "ResolverType";
if ( types & StatusPushType )
m_types << "StatusPushType";
m_cfg.types << "StatusPushType";
syncConfig();
}
@@ -205,13 +176,13 @@ Account::types() const
{
QMutexLocker locker( &m_mutex );
AccountTypes types;
if ( m_types.contains( "InfoType" ) )
if ( m_cfg.types.contains( "InfoType" ) )
types |= InfoType;
if ( m_types.contains( "SipType" ) )
if ( m_cfg.types.contains( "SipType" ) )
types |= SipType;
if ( m_types.contains( "ResolverType" ) )
if ( m_cfg.types.contains( "ResolverType" ) )
types |= ResolverType;
if ( m_types.contains( "StatusPushType" ) )
if ( m_cfg.types.contains( "StatusPushType" ) )
types |= StatusPushType;
return types;

View File

@@ -2,6 +2,7 @@
*
* Copyright 2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2011, Leo Franchi <lfranchi@kde.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
@@ -41,6 +42,8 @@ namespace Tomahawk
namespace Accounts
{
class ConfigStorage;
enum AccountType
{
NoType = 0x00,
@@ -66,18 +69,28 @@ class DLLEXPORT Account : public QObject
Q_OBJECT
public:
struct Configuration
{
QString accountFriendlyName;
bool enabled;
QVariantHash configuration;
QVariantMap acl;
QStringList types;
QVariantHash credentials;
};
enum AuthErrorCode { AuthError, ConnectionError };
enum ConnectionState { Disconnected, Connecting, Connected, Disconnecting };
explicit Account( const QString &accountId );
explicit Account(const QString &accountId);
virtual ~Account();
QString accountServiceName() const { QMutexLocker locker( &m_mutex ); return m_accountServiceName; } // e.g. "Twitter", "Last.fm"
QString accountFriendlyName() const { QMutexLocker locker( &m_mutex ); return m_accountFriendlyName; } // e.g. screen name on the service, JID, etc.
bool enabled() const { QMutexLocker locker( &m_mutex ); return m_enabled; }
QString accountFriendlyName() const { QMutexLocker locker( &m_mutex ); return m_cfg.accountFriendlyName; } // e.g. screen name on the service, JID, etc.
bool enabled() const { QMutexLocker locker( &m_mutex ); return m_cfg.enabled; }
QString accountId() const { QMutexLocker locker( &m_mutex ); return m_accountId; }
QVariantHash configuration() const { QMutexLocker locker( &m_mutex ); return m_configuration; }
QVariantHash configuration() const { QMutexLocker locker( &m_mutex ); return m_cfg.configuration; }
/**
* Configuration widgets can have a "dataError( bool )" signal to enable/disable the OK button in their wrapper dialogs.
@@ -94,9 +107,9 @@ public:
virtual void saveConfig() {} // called when the widget has been edited. save values from config widget, call sync() to write to disk account generic settings
QVariantHash credentials() const { QMutexLocker locker( &m_mutex ); return m_credentials; }
QVariantHash credentials() const { QMutexLocker locker( &m_mutex ); return m_cfg.credentials; }
QVariantMap acl() const { QMutexLocker locker( &m_mutex ); return m_acl; }
QVariantMap acl() const { QMutexLocker locker( &m_mutex ); return m_cfg.acl; }
virtual ConnectionState connectionState() const = 0;
virtual bool isAuthenticated() const = 0;
@@ -113,15 +126,16 @@ public:
AccountTypes types() const;
void setAccountServiceName( const QString &serviceName ) { QMutexLocker locker( &m_mutex ); m_accountServiceName = serviceName; }
void setAccountFriendlyName( const QString &friendlyName ) { QMutexLocker locker( &m_mutex ); m_accountFriendlyName = friendlyName; }
void setEnabled( bool enabled ) { QMutexLocker locker( &m_mutex ); m_enabled = enabled; }
void setAccountFriendlyName( const QString &friendlyName ) { QMutexLocker locker( &m_mutex ); m_cfg.accountFriendlyName = friendlyName; }
void setEnabled( bool enabled ) { QMutexLocker locker( &m_mutex ); m_cfg.enabled = enabled; }
void setAccountId( const QString &accountId ) { QMutexLocker locker( &m_mutex ); m_accountId = accountId; }
void setCredentials( const QVariantHash &credentialHash ) { QMutexLocker locker( &m_mutex ); m_credentials = credentialHash; }
void setConfiguration( const QVariantHash &configuration ) { QMutexLocker locker( &m_mutex ); m_configuration = configuration; }
void setAcl( const QVariantMap &acl ) { QMutexLocker locker( &m_mutex ); m_acl = acl; }
void setCredentials( const QVariantHash &credentialHash ) { QMutexLocker locker( &m_mutex ); m_cfg.credentials = credentialHash; }
void setConfiguration( const QVariantHash &configuration ) { QMutexLocker locker( &m_mutex ); m_cfg.configuration = configuration; }
void setAcl( const QVariantMap &acl ) { QMutexLocker locker( &m_mutex ); m_cfg.acl = acl; }
void setTypes( AccountTypes types );
void sync() { QMutexLocker locker( &m_mutex ); syncConfig(); };
void sync() { QMutexLocker locker( &m_mutex ); syncConfig(); }
/**
* Removes all the settings held in the config file for this account instance
@@ -150,15 +164,12 @@ private slots:
private:
QString m_accountServiceName;
QString m_accountFriendlyName;
QString m_cachedError;
bool m_enabled;
QString m_accountId;
QVariantHash m_credentials;
QVariantHash m_configuration;
QVariantMap m_acl;
QStringList m_types;
mutable QMutex m_mutex;
Account::Configuration m_cfg;
};
class DLLEXPORT AccountFactory : public QObject

View File

@@ -32,6 +32,7 @@
#include "ResolverAccount.h"
#include "SourceList.h"
#include "TomahawkSettings.h"
#include "LocalConfigStorage.h"
#include <QtCore/QLibrary>
#include <QtCore/QDir>
@@ -284,38 +285,42 @@ AccountManager::toggleAccountsConnected()
void
AccountManager::loadFromConfig()
{
QStringList accountIds = TomahawkSettings::instance()->accounts();
m_creds = new CredentialsManager( this );
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;
m_creds = new CredentialsManager( this );
NewClosure( m_creds, SIGNAL( ready() ),
this, SLOT( finishLoadingFromConfig( QStringList ) ), accountIds );
CredentialsManager::Service tomahawkSvc;
tomahawkSvc.name = "Tomahawk"; //the string where we store in QtKeychain
tomahawkSvc.keys = accountIds;
QList< CredentialsManager::Service > svcs;
svcs << tomahawkSvc;
m_creds->loadCredentials( svcs );
this, SLOT( finishLoadingFromConfig() ) );
m_creds->loadCredentials();
}
void
AccountManager::finishLoadingFromConfig( const QStringList& accountIds )
AccountManager::finishLoadingFromConfig()
{
qDebug() << "LOADING ALL ACCOUNTS" << accountIds;
foreach ( const QString& accountId, accountIds )
foreach ( ConfigStorage* cs, m_configStorageById )
{
QString pluginFactory = factoryFromId( accountId );
if ( m_accountFactories.contains( pluginFactory ) )
QStringList accountIds = cs->accountIds();
qDebug() << "LOADING ALL ACCOUNTS FOR STORAGE" << cs->metaObject()->className()
<< ":" << accountIds;
foreach ( const QString& accountId, accountIds )
{
Account* account = loadPlugin( accountId );
addAccount( account );
QString pluginFactory = factoryFromId( accountId );
if ( m_accountFactories.contains( pluginFactory ) )
{
Account* account = loadPlugin( accountId );
addAccount( account );
}
}
}
m_readyForSip = true;
emit readyForSip(); //we have to yield to TomahawkApp because we don't know if Servent is ready
}
@@ -453,6 +458,18 @@ AccountManager::zeroconfAccount() const
}
ConfigStorage*
AccountManager::configStorageForAccount( const QString& accountId )
{
foreach ( ConfigStorage* cs, m_configStorageById )
{
if ( cs->accountIds().contains( accountId ) )
return cs;
}
return 0;
}
void
AccountManager::hookupAccount( Account* account ) const
{

View File

@@ -95,6 +95,7 @@ public:
bool isReady() const { return m_completelyReady; }
CredentialsManager* credentialsManager() const { return m_creds; }
ConfigStorage* configStorageForAccount( const QString& accountId );
public slots:
void connectAll();
@@ -119,7 +120,7 @@ private slots:
void init();
void onStateChanged( Tomahawk::Accounts::Account::ConnectionState state );
void onError( int code, const QString& msg );
void finishLoadingFromConfig( const QStringList& accountIds );
void finishLoadingFromConfig();
void onSettingsChanged();
@@ -129,7 +130,7 @@ private:
void loadPluginFactory( const QString &path );
QString factoryFromId( const QString& accountId ) const;
Account* loadPlugin( const QString &accountId );
Account* loadPlugin( const QString& accountId );
void hookupAccount( Account* ) const;
CredentialsManager* m_creds;
@@ -145,6 +146,8 @@ private:
QHash< QString, AccountFactory* > m_accountFactories;
QList< AccountFactory* > m_factoriesForFilesytem;
QMap< QString, ConfigStorage* > m_configStorageById;
static AccountManager* s_instance;
};

View File

@@ -0,0 +1,54 @@
/* === 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/>.
*/
#ifndef CONFIGSTORAGE_H
#define CONFIGSTORAGE_H
#include "TomahawkSettings.h"
#include "Account.h"
namespace Tomahawk
{
namespace Accounts
{
class ConfigStorage : public QObject
{
public:
explicit ConfigStorage( QObject* parent )
: QObject( parent )
{}
virtual ~ConfigStorage() {}
virtual QString id() const = 0;
virtual QStringList accountIds() const = 0;
virtual void save( const QString& accountId, const Account::Configuration& cfg ) = 0;
virtual void load( const QString& accountId, Account::Configuration& cfg ) = 0;
virtual void remove( const QString& accountId ) = 0;
};
} //namespace Accounts
} //namespace Tomahawk
#endif // CONFIGSTORAGE_H

View File

@@ -66,14 +66,26 @@ CredentialsManager::CredentialsManager( QObject* parent )
void
CredentialsManager::loadCredentials( QList< Service > keysByService )
CredentialsManager::addService( const QString& service , const QStringList& accountIds )
{
foreach ( const Service svc, keysByService )
if ( m_services.contains( service ) )
m_services.remove( service );
m_services.insert( service, accountIds );
}
void
CredentialsManager::loadCredentials()
{
for ( QHash< QString, QStringList >::const_iterator it = m_services.constBegin();
it != m_services.constEnd(); ++it )
{
tDebug() << Q_FUNC_INFO << "keys for service" << svc.name << ":" << svc.keys;
foreach ( QString key, svc.keys )
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( svc.name, this );
QKeychain::ReadPasswordJob* j = new QKeychain::ReadPasswordJob( svcName, this );
j->setKey( key );
j->setAutoDelete( false );
#if defined( Q_OS_UNIX ) && !defined( Q_OS_MAC )

View File

@@ -64,15 +64,11 @@ class CredentialsManager : public QObject
{
Q_OBJECT
public:
struct Service
{
QString name;
QStringList keys;
};
explicit CredentialsManager( QObject* parent = 0 );
void loadCredentials( QList< Service > keysByService );
void addService( const QString& service, const QStringList& accountIds );
void loadCredentials();
QList< CredentialsStorageKey > keys() const;
@@ -88,6 +84,7 @@ private slots:
void keychainJobFinished( QKeychain::Job* );
private:
QHash< QString, QStringList > m_services;
QHash< CredentialsStorageKey, QVariantHash > m_credentials;
QList< QKeychain::ReadPasswordJob* > m_readJobs;
QMutex m_mutex;

View File

@@ -0,0 +1,108 @@
/* === 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 "LocalConfigStorage.h"
#include "Account.h"
#include "AccountManager.h"
#include "CredentialsManager.h"
namespace Tomahawk
{
namespace Accounts
{
LocalConfigStorage::LocalConfigStorage( QObject* parent )
: ConfigStorage( parent )
, m_credentialsServiceName( "Tomahawk" )
{
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()
AccountManager::instance()->credentialsManager()->addService( m_credentialsServiceName,
m_accountIds );
}
QStringList
LocalConfigStorage::accountIds() const
{
return m_accountIds;
}
void
LocalConfigStorage::save( const QString& accountId, const Account::Configuration& cfg )
{
TomahawkSettings* s = TomahawkSettings::instance();
s->beginGroup( "accounts/" + accountId );
s->setValue( "accountfriendlyname", cfg.accountFriendlyName );
s->setValue( "enabled", cfg.enabled );
s->setValue( "configuration", cfg.configuration );
s->setValue( "acl", cfg.acl );
s->setValue( "types", cfg.types );
s->endGroup();
s->sync();
CredentialsManager* c = AccountManager::instance()->credentialsManager();
c->setCredentials( m_credentialsServiceName, accountId, cfg.credentials );
if ( !m_accountIds.contains( accountId ) )
m_accountIds.append( accountId );
}
void
LocalConfigStorage::load( const QString& accountId, Account::Configuration& cfg )
{
TomahawkSettings* s = TomahawkSettings::instance();
s->beginGroup( "accounts/" + accountId );
cfg.accountFriendlyName = s->value( "accountfriendlyname", QString() ).toString();
cfg.enabled = s->value( "enabled", false ).toBool();
cfg.configuration = s->value( "configuration", QVariantHash() ).toHash();
cfg.acl = s->value( "acl", QVariantMap() ).toMap();
cfg.types = s->value( "types", QStringList() ).toStringList();
s->endGroup();
CredentialsManager* c = AccountManager::instance()->credentialsManager();
cfg.credentials = c->credentials( m_credentialsServiceName, accountId );
}
void
LocalConfigStorage::remove( const QString& accountId )
{
TomahawkSettings* s = TomahawkSettings::instance();
s->beginGroup( "accounts/" + accountId );
s->remove( "accountfriendlyname" );
s->remove( "enabled" );
s->remove( "configuration" );
s->remove( "acl" );
s->remove( "types" );
s->endGroup();
s->remove( "accounts/" + accountId );
CredentialsManager* c = AccountManager::instance()->credentialsManager();
c->setCredentials( m_credentialsServiceName, accountId, QVariantHash() );
}
}
}

View File

@@ -0,0 +1,55 @@
/* === 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/>.
*/
#ifndef LOCALCONFIGSTORAGE_H
#define LOCALCONFIGSTORAGE_H
#include "ConfigStorage.h"
namespace Tomahawk
{
namespace Accounts
{
class LocalConfigStorage : public ConfigStorage
{
Q_OBJECT
public:
explicit LocalConfigStorage( QObject* parent = 0 );
QString id() const { return "localconfigstorage"; }
QStringList accountIds() const;
virtual void save( const QString& accountId, const Account::Configuration& cfg );
virtual void load( const QString& accountId, Account::Configuration& cfg );
virtual void remove( const QString& accountId );
private:
const QString m_credentialsServiceName;
QStringList m_accountIds;
static LocalConfigStorage* s_instance;
};
} //namespace Accounts
} //namespace Tomahawk
#endif // LOCALCONFIGSTORAGE_H