1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-07-31 03:10:12 +02:00

Add Telepathy magic to TelepathyConfigStorage

This commit is contained in:
Teo Mrnjavac
2013-06-16 14:08:56 +02:00
parent 2db2cdb996
commit 530a838a1f
5 changed files with 197 additions and 55 deletions

View File

@@ -118,14 +118,14 @@ CredentialsManager::services() const
} }
QVariantHash QVariant
CredentialsManager::credentials( const CredentialsStorageKey& key ) const CredentialsManager::credentials( const CredentialsStorageKey& key ) const
{ {
return m_credentials.value( key ); return m_credentials.value( key );
} }
QVariantHash QVariant
CredentialsManager::credentials( const QString& serviceName, const QString& key ) const CredentialsManager::credentials( const QString& serviceName, const QString& key ) const
{ {
return credentials( CredentialsStorageKey( serviceName, key ) ); return credentials( CredentialsStorageKey( serviceName, key ) );
@@ -133,12 +133,14 @@ CredentialsManager::credentials( const QString& serviceName, const QString& key
void void
CredentialsManager::setCredentials( const CredentialsStorageKey& csKey, const QVariantHash& value ) CredentialsManager::setCredentials( const CredentialsStorageKey& csKey, const QVariant& value, bool tryToWriteAsString )
{ {
QMutexLocker locker( &m_mutex ); QMutexLocker locker( &m_mutex );
QKeychain::Job* j; 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 if ( !m_credentials.contains( csKey ) ) //if we don't have any credentials for this key, we bail
return; return;
@@ -156,15 +158,26 @@ CredentialsManager::setCredentials( const CredentialsStorageKey& csKey, const QV
m_credentials.insert( csKey, value ); m_credentials.insert( csKey, value );
QByteArray data;
{
QDataStream ds( &data, QIODevice::WriteOnly );
ds << value;
}
QKeychain::WritePasswordJob* wj = new QKeychain::WritePasswordJob( csKey.service(), this ); QKeychain::WritePasswordJob* wj = new QKeychain::WritePasswordJob( csKey.service(), this );
wj->setKey( csKey.key() ); 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; j = wj;
} }
@@ -181,7 +194,14 @@ CredentialsManager::setCredentials( const CredentialsStorageKey& csKey, const QV
void void
CredentialsManager::setCredentials( const QString& serviceName, const QString& key, const QVariantHash& value ) 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"; tDebug() << "QtKeychain readJob for" << readJob->key() << "finished without errors";
QVariantHash creds; QVariant creds;
QDataStream dataStream( readJob->binaryData() ); if ( !readJob->textData().isEmpty() )
dataStream >> creds; {
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 ); m_credentials.insert( CredentialsStorageKey( readJob->service(), readJob->key() ), creds );
} }

View File

@@ -69,8 +69,9 @@ public:
QStringList keys( const QString& service ) const; QStringList keys( const QString& service ) const;
QStringList services() 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 QVariantHash& value );
void setCredentials( const QString& serviceName, const QString& key, const QString& value );
signals: signals:
void serviceReady( const QString& service ); void serviceReady( const QString& service );
@@ -81,12 +82,12 @@ private slots:
void keychainJobFinished( QKeychain::Job* ); void keychainJobFinished( QKeychain::Job* );
protected: protected:
QVariantHash credentials( const CredentialsStorageKey& key ) const; QVariant credentials( const CredentialsStorageKey& key ) const;
void setCredentials( const CredentialsStorageKey& key, const QVariantHash& value ); void setCredentials( const CredentialsStorageKey& key, const QVariant& value, bool tryToWriteAsString = false );
private: private:
QHash< QString, QStringList > m_services; QHash< QString, QStringList > m_services;
QHash< CredentialsStorageKey, QVariantHash > m_credentials; QHash< CredentialsStorageKey, QVariant > m_credentials;
QHash< QString, QList< QKeychain::ReadPasswordJob* > > m_readJobs; QHash< QString, QList< QKeychain::ReadPasswordJob* > > m_readJobs;
QMutex m_mutex; QMutex m_mutex;
}; };

View File

@@ -107,7 +107,9 @@ LocalConfigStorage::load( const QString& accountId, Account::Configuration& cfg
s->endGroup(); s->endGroup();
CredentialsManager* c = AccountManager::instance()->credentialsManager(); 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();
} }

View File

@@ -23,18 +23,20 @@
#include "accounts/CredentialsManager.h" #include "accounts/CredentialsManager.h"
#include "utils/Logger.h" #include "utils/Logger.h"
#include <TelepathyQt/Account>
#include <TelepathyQt/PendingReady>
#include <TelepathyQt/PendingOperation>
#include <TelepathyQt/AccountSet>
#include <QTimer> #include <QTimer>
namespace Tomahawk
{
namespace Accounts //NOTE: Both Tomahawk::Accounts and Tp have class names Account and AccountManager.
{
TelepathyConfigStorage::TelepathyConfigStorage( QObject* parent ) Tomahawk::Accounts::TelepathyConfigStorage::TelepathyConfigStorage( QObject* parent )
: ConfigStorage( parent ) : Tomahawk::Accounts::ConfigStorage( parent )
, m_credentialsServiceName( "telepathy-kde" ) , m_credentialsServiceName( "telepathy-kde" )
{ {
tDebug() << Q_FUNC_INFO; tDebug() << Q_FUNC_INFO;
@@ -42,25 +44,92 @@ TelepathyConfigStorage::TelepathyConfigStorage( QObject* parent )
void 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 // 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, CredentialsManager* cm = AccountManager::instance()->credentialsManager();
m_accountIds ); connect( cm, SIGNAL( serviceReady( QString ) ),
QTimer::singleShot( 0, this, SIGNAL( ready() ) ); 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 QStringList
TelepathyConfigStorage::accountIds() const Tomahawk::Accounts::TelepathyConfigStorage::accountIds() const
{ {
return m_accountIds; return m_accountIds;
} }
void 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(); // TomahawkSettings* s = TomahawkSettings::instance();
// s->beginGroup( "accounts/" + accountId ); // s->beginGroup( "accounts/" + accountId );
@@ -81,7 +150,7 @@ TelepathyConfigStorage::save( const QString& accountId, const Account::Configura
void void
TelepathyConfigStorage::load( const QString& accountId, Account::Configuration& cfg ) Tomahawk::Accounts::TelepathyConfigStorage::load( const QString& accountId, Account::Configuration& cfg )
{ {
// TomahawkSettings* s = TomahawkSettings::instance(); // TomahawkSettings* s = TomahawkSettings::instance();
// s->beginGroup( "accounts/" + accountId ); // s->beginGroup( "accounts/" + accountId );
@@ -92,33 +161,59 @@ TelepathyConfigStorage::load( const QString& accountId, Account::Configuration&
// cfg.types = s->value( "types", QStringList() ).toStringList(); // cfg.types = s->value( "types", QStringList() ).toStringList();
// s->endGroup(); // s->endGroup();
CredentialsManager* c = AccountManager::instance()->credentialsManager(); Tp::AccountPtr account = m_tpam->accountForObjectPath( accountIdToTelepathyPath( accountId ) );
QString prefix( "tomahawkaccount_" );
QString idInKeychain = accountId; cfg.accountFriendlyName = account->normalizedName();
if ( idInKeychain.startsWith( prefix ) )
idInKeychain.remove( 0, prefix.length() ); cfg.enabled = true;
cfg.credentials = c->credentials( m_credentialsServiceName, idInKeychain ); 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 void
TelepathyConfigStorage::remove( const QString& accountId ) Tomahawk::Accounts::TelepathyConfigStorage::remove( const QString& accountId )
{ {
TomahawkSettings* s = TomahawkSettings::instance(); // TomahawkSettings* s = TomahawkSettings::instance();
s->beginGroup( "accounts/" + accountId ); // s->beginGroup( "accounts/" + accountId );
s->remove( "accountfriendlyname" ); // s->remove( "accountfriendlyname" );
s->remove( "enabled" ); // s->remove( "enabled" );
s->remove( "configuration" ); // s->remove( "configuration" );
s->remove( "acl" ); // s->remove( "acl" );
s->remove( "types" ); // s->remove( "types" );
s->endGroup(); // s->endGroup();
s->remove( "accounts/" + accountId ); // s->remove( "accounts/" + accountId );
CredentialsManager* c = AccountManager::instance()->credentialsManager(); // Tomahawk::Accounts::CredentialsManager* c = Tomahawk::Accounts::AccountManager::instance()->credentialsManager();
c->setCredentials( m_credentialsServiceName, accountId, QVariantHash() ); // c->setCredentials( m_credentialsServiceName, account->uniqueIdentifier(), QVariantHash() );
}
}
} }
Q_EXPORT_PLUGIN2( Tomahawk::Accounts::ConfigStorage, Tomahawk::Accounts::TelepathyConfigStorage ) Q_EXPORT_PLUGIN2( Tomahawk::Accounts::ConfigStorage, Tomahawk::Accounts::TelepathyConfigStorage )

View File

@@ -22,6 +22,13 @@
#include "accounts/ConfigStorageDllMacro.h" #include "accounts/ConfigStorageDllMacro.h"
#include "accounts/ConfigStorage.h" #include "accounts/ConfigStorage.h"
#include <TelepathyQt/AccountManager>
namespace Tp
{
class PendingOperation;
}
namespace Tomahawk namespace Tomahawk
{ {
@@ -45,9 +52,17 @@ public:
virtual void load( const QString& accountId, Account::Configuration& cfg ); virtual void load( const QString& accountId, Account::Configuration& cfg );
virtual void remove( const QString& accountId ); virtual void remove( const QString& accountId );
private slots:
void onTpAccountManagerReady( Tp::PendingOperation* op );
void onCredentialsManagerReady( const QString& service );
private: private:
QString telepathyPathToAccountId( const QString& objectPath );
QString accountIdToTelepathyPath( const QString& accountId );
const QString m_credentialsServiceName; const QString m_credentialsServiceName;
QStringList m_accountIds; QStringList m_accountIds;
Tp::AccountManagerPtr m_tpam;
static TelepathyConfigStorage* s_instance; static TelepathyConfigStorage* s_instance;
}; };