From 48f2d3b45a370c078fcf783b25df4b256c85621d Mon Sep 17 00:00:00 2001 From: Teo Mrnjavac Date: Tue, 6 Aug 2013 10:32:21 +0200 Subject: [PATCH] Replace crappy ConfigStorage deduplication with decent deduplication. + const correctness --- src/libtomahawk/accounts/AccountManager.cpp | 42 ++++++----- src/libtomahawk/accounts/ConfigStorage.h | 4 +- .../accounts/LocalConfigStorage.cpp | 10 ++- src/libtomahawk/accounts/LocalConfigStorage.h | 4 +- .../telepathy/TelepathyConfigStorage.cpp | 69 +++++++++++++++++-- .../telepathy/TelepathyConfigStorage.h | 7 +- 6 files changed, 107 insertions(+), 29 deletions(-) diff --git a/src/libtomahawk/accounts/AccountManager.cpp b/src/libtomahawk/accounts/AccountManager.cpp index 95376fdb6..03cc18491 100644 --- a/src/libtomahawk/accounts/AccountManager.cpp +++ b/src/libtomahawk/accounts/AccountManager.cpp @@ -258,6 +258,30 @@ AccountManager::finishLoadingFromConfig( const QString& csid ) if ( !m_configStorageLoading.isEmpty() ) return; + // First we prioritize available ConfigStorages + QList< ConfigStorage* > csByPriority; + foreach ( ConfigStorage* cs, m_configStorageById ) + { + int i = 0; + for ( ; i < csByPriority.length(); ++i ) + { + if ( csByPriority.at( i )->priority() > cs->priority() ) + break; + } + csByPriority.insert( i, cs ); + } + + // And we deduplicate + for ( int i = 1; i < csByPriority.length(); ++i ) + { + for ( int j = 0; j < i; ++j ) + { + ConfigStorage* prioritized = csByPriority.value( j ); + ConfigStorage* trimming = csByPriority.value( i ); + trimming->deduplicateFrom( prioritized ); + } + } + foreach ( const ConfigStorage* cs, m_configStorageById ) { QStringList accountIds = cs->accountIds(); @@ -312,24 +336,6 @@ void AccountManager::addAccount( Account* account ) { tDebug() << Q_FUNC_INFO << "adding account plugin" << account->accountId(); - foreach ( Account* a, m_accounts ) - { - if ( a->credentials()["username"] == account->credentials()["username"] ) - { - ConfigStorage* configStorageForA = configStorageForAccount( a->accountId() ); - ConfigStorage* configStorageForNewAccount = configStorageForAccount( account->accountId() ); - - if ( !configStorageForA || !configStorageForNewAccount || configStorageForA->priority() > configStorageForNewAccount->priority() ) - { - removeAccount( a ); - break; - } - else - { - return; - } - } - } m_accounts.append( account ); if ( account->types() & Accounts::SipType ) diff --git a/src/libtomahawk/accounts/ConfigStorage.h b/src/libtomahawk/accounts/ConfigStorage.h index 734bc3a4b..867b91f6a 100644 --- a/src/libtomahawk/accounts/ConfigStorage.h +++ b/src/libtomahawk/accounts/ConfigStorage.h @@ -51,8 +51,10 @@ public: virtual unsigned int priority() const = 0; //LocalConfigStorage has 0, everything else comes later + virtual void deduplicateFrom( const ConfigStorage* other ) = 0; + virtual void save( const QString& accountId, const Account::Configuration& cfg ) = 0; - virtual void load( const QString& accountId, Account::Configuration& cfg ) = 0; + virtual void load( const QString& accountId, Account::Configuration& cfg ) const = 0; virtual void remove( const QString& accountId ) = 0; signals: diff --git a/src/libtomahawk/accounts/LocalConfigStorage.cpp b/src/libtomahawk/accounts/LocalConfigStorage.cpp index aaf490761..73cbf79d3 100644 --- a/src/libtomahawk/accounts/LocalConfigStorage.cpp +++ b/src/libtomahawk/accounts/LocalConfigStorage.cpp @@ -87,6 +87,14 @@ LocalConfigStorage::priority() const } +void +LocalConfigStorage::deduplicateFrom( const ConfigStorage* other) +{ + Q_UNUSED( other ) + // I'm priority 0 so I don't have to deduplicate anything +} + + void LocalConfigStorage::save( const QString& accountId, const Account::Configuration& cfg ) { @@ -109,7 +117,7 @@ LocalConfigStorage::save( const QString& accountId, const Account::Configuration void -LocalConfigStorage::load( const QString& accountId, Account::Configuration& cfg ) +LocalConfigStorage::load( const QString& accountId, Account::Configuration& cfg ) const { TomahawkSettings* s = TomahawkSettings::instance(); s->beginGroup( "accounts/" + accountId ); diff --git a/src/libtomahawk/accounts/LocalConfigStorage.h b/src/libtomahawk/accounts/LocalConfigStorage.h index 55347ea13..9e65da8e3 100644 --- a/src/libtomahawk/accounts/LocalConfigStorage.h +++ b/src/libtomahawk/accounts/LocalConfigStorage.h @@ -41,8 +41,10 @@ public: unsigned int priority() const; + void deduplicateFrom( const ConfigStorage* other ); + virtual void save( const QString& accountId, const Account::Configuration& cfg ); - virtual void load( const QString& accountId, Account::Configuration& cfg ); + virtual void load( const QString& accountId, Account::Configuration& cfg ) const; virtual void remove( const QString& accountId ); diff --git a/src/libtomahawk/accounts/configstorage/telepathy/TelepathyConfigStorage.cpp b/src/libtomahawk/accounts/configstorage/telepathy/TelepathyConfigStorage.cpp index 6f5ececa5..4f2b629fe 100644 --- a/src/libtomahawk/accounts/configstorage/telepathy/TelepathyConfigStorage.cpp +++ b/src/libtomahawk/accounts/configstorage/telepathy/TelepathyConfigStorage.cpp @@ -50,6 +50,8 @@ Tomahawk::Accounts::TelepathyConfigStorage::TelepathyConfigStorage( QObject* par , m_credentialsServiceName( "telepathy-kde" ) { tDebug() << Q_FUNC_INFO; + m_allowedPrefixes << "xmppaccount_" + << "googleaccount_"; loadConfigWidgetPlugins(); } @@ -179,13 +181,10 @@ Tomahawk::Accounts::TelepathyConfigStorage::telepathyPathToAccountId( const QStr QString -Tomahawk::Accounts::TelepathyConfigStorage::accountIdToTelepathyPath( const QString& accountId ) +Tomahawk::Accounts::TelepathyConfigStorage::accountIdToTelepathyPath( const QString& accountId ) const { - QStringList allowedPrefixes; - allowedPrefixes << "xmppaccount_" - << "googleaccount_"; QString r = accountId; - foreach ( QString prefix, allowedPrefixes ) + foreach ( QString prefix, m_allowedPrefixes ) { if ( r.startsWith( prefix ) ) r.remove( 0, prefix.length() ); @@ -208,6 +207,64 @@ Tomahawk::Accounts::TelepathyConfigStorage::priority() const } +void +Tomahawk::Accounts::TelepathyConfigStorage::deduplicateFrom( const Tomahawk::Accounts::ConfigStorage* other ) +{ + tDebug() << "MY ACCOUNTS:" << accountIds(); + tDebug() << "OTHER ACCOUNTS:" << other->accountIds(); + + QSet< QString > toDelete; + foreach ( QString myId, m_accountIds ) + { + QString myUsername; + { + Account::Configuration cfg; + load( myId, cfg ); + myUsername = cfg.credentials[ "username" ].toString(); + } + + if ( myUsername.isEmpty() ) + { + toDelete.insert( myId ); + continue; + } + + foreach ( QString otherId, other->accountIds() ) + { + bool typesMatch = false; + foreach ( QString prefix, m_allowedPrefixes ) + { + if ( otherId.startsWith( prefix ) && myId.startsWith( prefix ) ) + { + typesMatch = true; + break; + } + } + if ( !typesMatch ) + continue; + + QString otherUsername; + { + Account::Configuration cfg; + other->load( otherId, cfg ); + otherUsername = cfg.credentials[ "username" ].toString(); + } + + if ( myUsername == otherUsername ) + { + toDelete.insert( myId ); + break; + } + } + } + + foreach ( QString id, toDelete ) + { + m_accountIds.removeAll( id ); + } +} + + void Tomahawk::Accounts::TelepathyConfigStorage::save( const QString& accountId, const Account::Configuration& cfg ) { @@ -224,7 +281,7 @@ Tomahawk::Accounts::TelepathyConfigStorage::save( const QString& accountId, cons void -Tomahawk::Accounts::TelepathyConfigStorage::load( const QString& accountId, Account::Configuration& cfg ) +Tomahawk::Accounts::TelepathyConfigStorage::load( const QString& accountId, Account::Configuration& cfg ) const { TomahawkSettings* s = TomahawkSettings::instance(); s->beginGroup( "externalaccounts/" + accountId ); diff --git a/src/libtomahawk/accounts/configstorage/telepathy/TelepathyConfigStorage.h b/src/libtomahawk/accounts/configstorage/telepathy/TelepathyConfigStorage.h index d21265fa7..2ae3dc4a7 100644 --- a/src/libtomahawk/accounts/configstorage/telepathy/TelepathyConfigStorage.h +++ b/src/libtomahawk/accounts/configstorage/telepathy/TelepathyConfigStorage.h @@ -57,8 +57,10 @@ public: unsigned int priority() const; + void deduplicateFrom( const ConfigStorage* other ); + virtual void save( const QString& accountId, const Account::Configuration& cfg ); - virtual void load( const QString& accountId, Account::Configuration& cfg ); + virtual void load( const QString& accountId, Account::Configuration& cfg ) const; virtual void remove( const QString& accountId ); private slots: @@ -69,7 +71,7 @@ private: void loadConfigWidgetPlugins(); QString telepathyPathToAccountId( const QString& objectPath, const QString& telepathyServiceName ); - QString accountIdToTelepathyPath( const QString& accountId ); + QString accountIdToTelepathyPath( const QString& accountId ) const; const QString m_credentialsServiceName; QStringList m_accountIds; @@ -77,6 +79,7 @@ private: QList< TelepathyConfigStorageConfigWidgetPlugin* > m_configWidgetPlugins; static TelepathyConfigStorage* s_instance; + QStringList m_allowedPrefixes; }; } //namespace Accounts