mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-13 09:34:53 +02:00
Replace crappy ConfigStorage deduplication with decent deduplication.
+ const correctness
This commit is contained in:
@@ -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 )
|
||||
|
@@ -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:
|
||||
|
@@ -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 );
|
||||
|
@@ -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 );
|
||||
|
||||
|
||||
|
@@ -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 );
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user