From 530a838a1f40d853297e96336e8e1f57c9961d20 Mon Sep 17 00:00:00 2001 From: Teo Mrnjavac Date: Sun, 16 Jun 2013 14:08:56 +0200 Subject: [PATCH] Add Telepathy magic to TelepathyConfigStorage --- .../accounts/CredentialsManager.cpp | 59 +++++-- src/libtomahawk/accounts/CredentialsManager.h | 9 +- .../accounts/LocalConfigStorage.cpp | 4 +- .../telepathy/TelepathyConfigStorage.cpp | 165 ++++++++++++++---- .../telepathy/TelepathyConfigStorage.h | 15 ++ 5 files changed, 197 insertions(+), 55 deletions(-) diff --git a/src/libtomahawk/accounts/CredentialsManager.cpp b/src/libtomahawk/accounts/CredentialsManager.cpp index 8daed33b7..d0f4802ed 100644 --- a/src/libtomahawk/accounts/CredentialsManager.cpp +++ b/src/libtomahawk/accounts/CredentialsManager.cpp @@ -118,14 +118,14 @@ CredentialsManager::services() const } -QVariantHash +QVariant CredentialsManager::credentials( const CredentialsStorageKey& key ) const { return m_credentials.value( key ); } -QVariantHash +QVariant CredentialsManager::credentials( const QString& serviceName, const QString& key ) const { return credentials( CredentialsStorageKey( serviceName, key ) ); @@ -133,12 +133,14 @@ CredentialsManager::credentials( const QString& serviceName, const QString& key void -CredentialsManager::setCredentials( const CredentialsStorageKey& csKey, const QVariantHash& value ) +CredentialsManager::setCredentials( const CredentialsStorageKey& csKey, const QVariant& value, bool tryToWriteAsString ) { QMutexLocker locker( &m_mutex ); QKeychain::Job* j; - if ( value.isEmpty() ) + if ( value.isNull() || + ( value.type() == QVariant::Hash && value.toHash().isEmpty() ) || + ( value.type() == QVariant::String && value.toString().isEmpty() ) ) { if ( !m_credentials.contains( csKey ) ) //if we don't have any credentials for this key, we bail return; @@ -156,15 +158,26 @@ CredentialsManager::setCredentials( const CredentialsStorageKey& csKey, const QV m_credentials.insert( csKey, value ); - QByteArray data; - { - QDataStream ds( &data, QIODevice::WriteOnly ); - ds << value; - } - QKeychain::WritePasswordJob* wj = new QKeychain::WritePasswordJob( csKey.service(), this ); wj->setKey( csKey.key() ); - wj->setBinaryData( data ); + + Q_ASSERT( value.type() == QVariant::String || value.type() == QVariant::Hash ); + + if ( tryToWriteAsString && value.type() == QVariant::String ) + { + wj->setTextData( value.toString() ); + } + else if ( value.type() == QVariant::Hash ) + { + QByteArray data; + { + QDataStream ds( &data, QIODevice::WriteOnly ); + ds << value.toHash(); + } + + wj->setBinaryData( data ); + } + j = wj; } @@ -181,7 +194,14 @@ CredentialsManager::setCredentials( const CredentialsStorageKey& csKey, const QV void CredentialsManager::setCredentials( const QString& serviceName, const QString& key, const QVariantHash& value ) { - setCredentials( CredentialsStorageKey( serviceName, key ), value ); + setCredentials( CredentialsStorageKey( serviceName, key ), QVariant( value ) ); +} + + +void +CredentialsManager::setCredentials( const QString& serviceName, const QString& key, const QString& value ) +{ + setCredentials( CredentialsStorageKey( serviceName, key ), QVariant( value ), true ); } @@ -195,9 +215,18 @@ CredentialsManager::keychainJobFinished( QKeychain::Job* j ) { tDebug() << "QtKeychain readJob for" << readJob->key() << "finished without errors"; - QVariantHash creds; - QDataStream dataStream( readJob->binaryData() ); - dataStream >> creds; + QVariant creds; + if ( !readJob->textData().isEmpty() ) + { + creds = QVariant( readJob->textData() ); + } + else //must be a QVH + { + QDataStream dataStream( readJob->binaryData() ); + QVariantHash hash; + dataStream >> hash; + creds = QVariant( hash ); + } m_credentials.insert( CredentialsStorageKey( readJob->service(), readJob->key() ), creds ); } diff --git a/src/libtomahawk/accounts/CredentialsManager.h b/src/libtomahawk/accounts/CredentialsManager.h index 5e21b4da8..851e8d9b6 100644 --- a/src/libtomahawk/accounts/CredentialsManager.h +++ b/src/libtomahawk/accounts/CredentialsManager.h @@ -69,8 +69,9 @@ public: QStringList keys( const QString& service ) const; QStringList services() const; - QVariantHash credentials( const QString& serviceName, const QString& key ) const; + QVariant credentials( const QString& serviceName, const QString& key ) const; //returns QString or QVH void setCredentials( const QString& serviceName, const QString& key, const QVariantHash& value ); + void setCredentials( const QString& serviceName, const QString& key, const QString& value ); signals: void serviceReady( const QString& service ); @@ -81,12 +82,12 @@ private slots: void keychainJobFinished( QKeychain::Job* ); protected: - QVariantHash credentials( const CredentialsStorageKey& key ) const; - void setCredentials( const CredentialsStorageKey& key, const QVariantHash& value ); + QVariant credentials( const CredentialsStorageKey& key ) const; + void setCredentials( const CredentialsStorageKey& key, const QVariant& value, bool tryToWriteAsString = false ); private: QHash< QString, QStringList > m_services; - QHash< CredentialsStorageKey, QVariantHash > m_credentials; + QHash< CredentialsStorageKey, QVariant > m_credentials; 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 254ac1c79..51e6e8d79 100644 --- a/src/libtomahawk/accounts/LocalConfigStorage.cpp +++ b/src/libtomahawk/accounts/LocalConfigStorage.cpp @@ -107,7 +107,9 @@ LocalConfigStorage::load( const QString& accountId, Account::Configuration& cfg s->endGroup(); CredentialsManager* c = AccountManager::instance()->credentialsManager(); - cfg.credentials = c->credentials( m_credentialsServiceName, accountId ); + QVariant credentials = c->credentials( m_credentialsServiceName, accountId ); + if ( credentials.type() == QVariant::Hash ) + cfg.credentials = credentials.toHash(); } diff --git a/src/libtomahawk/accounts/configstorage/telepathy/TelepathyConfigStorage.cpp b/src/libtomahawk/accounts/configstorage/telepathy/TelepathyConfigStorage.cpp index 222072fd1..2e1e7b9c8 100644 --- a/src/libtomahawk/accounts/configstorage/telepathy/TelepathyConfigStorage.cpp +++ b/src/libtomahawk/accounts/configstorage/telepathy/TelepathyConfigStorage.cpp @@ -23,18 +23,20 @@ #include "accounts/CredentialsManager.h" #include "utils/Logger.h" +#include +#include +#include +#include + #include -namespace Tomahawk -{ -namespace Accounts -{ +//NOTE: Both Tomahawk::Accounts and Tp have class names Account and AccountManager. -TelepathyConfigStorage::TelepathyConfigStorage( QObject* parent ) - : ConfigStorage( parent ) +Tomahawk::Accounts::TelepathyConfigStorage::TelepathyConfigStorage( QObject* parent ) + : Tomahawk::Accounts::ConfigStorage( parent ) , m_credentialsServiceName( "telepathy-kde" ) { tDebug() << Q_FUNC_INFO; @@ -42,25 +44,92 @@ TelepathyConfigStorage::TelepathyConfigStorage( QObject* parent ) void -TelepathyConfigStorage::init() +Tomahawk::Accounts::TelepathyConfigStorage::init() { + + m_tpam = Tp::AccountManager::create(); + connect( m_tpam->becomeReady(), SIGNAL( finished( Tp::PendingOperation* ) ), + this, SLOT( onTpAccountManagerReady( Tp::PendingOperation* ) ) ); +} + + +void +Tomahawk::Accounts::TelepathyConfigStorage::onTpAccountManagerReady( Tp::PendingOperation* op ) +{ + if ( op->isError() ) + { + tDebug() << "Telepathy AccountManager cannot become ready:" + << op->errorName() << "-" << op->errorMessage(); + emit ready(); //we bail, this CS is ready to provide 0 accounts + return; + } + + QStringList keychainIds; + foreach ( const Tp::AccountPtr& acc, m_tpam->validAccounts()->accounts() ) + { + if ( acc->protocolName() == "jabber" ) + { + m_accountIds << acc->objectPath(); + keychainIds << acc->uniqueIdentifier(); + } + } + // 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 ); - QTimer::singleShot( 0, this, SIGNAL( ready() ) ); + + CredentialsManager* cm = AccountManager::instance()->credentialsManager(); + connect( cm, SIGNAL( serviceReady( QString ) ), + this, SLOT( onCredentialsManagerReady( QString ) ) ); + Tomahawk::Accounts::AccountManager::instance()->credentialsManager()->addService( m_credentialsServiceName, + keychainIds ); + tDebug() << Q_FUNC_INFO << "LOADING ALL CREDENTIALS FOR SERVICE" << m_credentialsServiceName << m_accountIds << keychainIds; +} + + +void +Tomahawk::Accounts::TelepathyConfigStorage::onCredentialsManagerReady( const QString& service ) +{ + if ( service != m_credentialsServiceName ) + return; + + //no need to listen for it any more + disconnect( this, SLOT( onCredentialsManagerReady( QString ) ) ); + + for ( int i = 0; i < m_accountIds.length(); ++i ) + { + m_accountIds[ i ] = telepathyPathToAccountId( m_accountIds[ i ] ); + } + + emit ready(); +} + + +QString +Tomahawk::Accounts::TelepathyConfigStorage::telepathyPathToAccountId( const QString& objectPath ) +{ + return QString( "xmppaccount_" ) + objectPath; +} + + +QString +Tomahawk::Accounts::TelepathyConfigStorage::accountIdToTelepathyPath( const QString& accountId ) +{ + QString prefix( "xmppaccount_" ); + QString r = accountId; + if ( r.startsWith( prefix ) ) + r.remove( 0, prefix.length() ); + return r; } QStringList -TelepathyConfigStorage::accountIds() const +Tomahawk::Accounts::TelepathyConfigStorage::accountIds() const { return m_accountIds; } void -TelepathyConfigStorage::save( const QString& accountId, const Account::Configuration& cfg ) +Tomahawk::Accounts::TelepathyConfigStorage::save( const QString& accountId, const Account::Configuration& cfg ) { // TomahawkSettings* s = TomahawkSettings::instance(); // s->beginGroup( "accounts/" + accountId ); @@ -81,7 +150,7 @@ TelepathyConfigStorage::save( const QString& accountId, const Account::Configura void -TelepathyConfigStorage::load( const QString& accountId, Account::Configuration& cfg ) +Tomahawk::Accounts::TelepathyConfigStorage::load( const QString& accountId, Account::Configuration& cfg ) { // TomahawkSettings* s = TomahawkSettings::instance(); // s->beginGroup( "accounts/" + accountId ); @@ -92,33 +161,59 @@ TelepathyConfigStorage::load( const QString& accountId, Account::Configuration& // cfg.types = s->value( "types", QStringList() ).toStringList(); // s->endGroup(); - CredentialsManager* c = AccountManager::instance()->credentialsManager(); - QString prefix( "tomahawkaccount_" ); - QString idInKeychain = accountId; - if ( idInKeychain.startsWith( prefix ) ) - idInKeychain.remove( 0, prefix.length() ); - cfg.credentials = c->credentials( m_credentialsServiceName, idInKeychain ); + Tp::AccountPtr account = m_tpam->accountForObjectPath( accountIdToTelepathyPath( accountId ) ); + + cfg.accountFriendlyName = account->normalizedName(); + + cfg.enabled = true; + cfg.acl = QVariantMap(); + + QStringList types; + types << "SipType"; + cfg.types = types; + + if ( !account->parameters()[ "port" ].isNull() ) + cfg.configuration[ "port" ] = account->parameters()[ "port" ].toString(); + else + cfg.configuration[ "port" ] = "5222"; + + if ( !account->parameters()[ "server" ].isNull() ) + cfg.configuration[ "server" ] = account->parameters()[ "server" ].toString(); + + if ( !account->parameters()[ "require-encryption" ].isNull() ) + cfg.configuration[ "enforcesecure" ] = account->parameters()[ "require-encryption" ].toBool(); + + cfg.configuration[ "publishtracks" ] = true; + + Tomahawk::Accounts::CredentialsManager* c = Tomahawk::Accounts::AccountManager::instance()->credentialsManager(); + cfg.credentials = QVariantHash(); + + if ( !account->parameters()[ "account" ].isNull() ) + cfg.credentials[ "username" ] = account->parameters()[ "account" ].toString(); + else + cfg.credentials[ "username" ] = account->normalizedName(); + + QVariant credentials = c->credentials( m_credentialsServiceName, account->uniqueIdentifier() ); + if ( credentials.type() == QVariant::String ) + cfg.credentials[ "password" ] = credentials.toString(); } void -TelepathyConfigStorage::remove( const QString& accountId ) +Tomahawk::Accounts::TelepathyConfigStorage::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 ); +// 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() ); -} - -} +// Tomahawk::Accounts::CredentialsManager* c = Tomahawk::Accounts::AccountManager::instance()->credentialsManager(); +// c->setCredentials( m_credentialsServiceName, account->uniqueIdentifier(), QVariantHash() ); } Q_EXPORT_PLUGIN2( Tomahawk::Accounts::ConfigStorage, Tomahawk::Accounts::TelepathyConfigStorage ) diff --git a/src/libtomahawk/accounts/configstorage/telepathy/TelepathyConfigStorage.h b/src/libtomahawk/accounts/configstorage/telepathy/TelepathyConfigStorage.h index 362ab885d..3c063d43f 100644 --- a/src/libtomahawk/accounts/configstorage/telepathy/TelepathyConfigStorage.h +++ b/src/libtomahawk/accounts/configstorage/telepathy/TelepathyConfigStorage.h @@ -22,6 +22,13 @@ #include "accounts/ConfigStorageDllMacro.h" #include "accounts/ConfigStorage.h" +#include + +namespace Tp +{ +class PendingOperation; +} + namespace Tomahawk { @@ -45,9 +52,17 @@ public: virtual void load( const QString& accountId, Account::Configuration& cfg ); virtual void remove( const QString& accountId ); +private slots: + void onTpAccountManagerReady( Tp::PendingOperation* op ); + void onCredentialsManagerReady( const QString& service ); + private: + QString telepathyPathToAccountId( const QString& objectPath ); + QString accountIdToTelepathyPath( const QString& accountId ); + const QString m_credentialsServiceName; QStringList m_accountIds; + Tp::AccountManagerPtr m_tpam; static TelepathyConfigStorage* s_instance; };