diff --git a/src/libtomahawk/accounts/Account.h b/src/libtomahawk/accounts/Account.h index 73a7dedee..93fcda654 100644 --- a/src/libtomahawk/accounts/Account.h +++ b/src/libtomahawk/accounts/Account.h @@ -180,6 +180,10 @@ public: virtual AccountTypes types() const = 0; virtual Account* createAccount( const QString& accountId = QString() ) = 0; + + /// If this resolver type accepts this path on disk (For general and special resolver accounts) + virtual bool acceptsPath( const QString& path ) const { return false; } + virtual Account* createFromPath( const QString& path ) { return 0; } }; }; diff --git a/src/libtomahawk/accounts/AccountManager.cpp b/src/libtomahawk/accounts/AccountManager.cpp index ae0b96f99..df8177bac 100644 --- a/src/libtomahawk/accounts/AccountManager.cpp +++ b/src/libtomahawk/accounts/AccountManager.cpp @@ -22,6 +22,7 @@ #include "sourcelist.h" #include "ResolverAccount.h" #include "LastFmAccount.h" +#include "SpotifyAccount.h" #include #include @@ -59,9 +60,14 @@ AccountManager::AccountManager( QObject *parent ) // We include the resolver factory manually, not in a plugin ResolverAccountFactory* f = new ResolverAccountFactory(); m_accountFactories[ f->factoryId() ] = f; + registerAccountFactoryForFilesystem( f ); LastFmAccountFactory* l = new LastFmAccountFactory(); m_accountFactories[ l->factoryId() ] = l; + + SpotifyAccountFactory* s = new SpotifyAccountFactory; + m_accountFactories[ s->factoryId() ] = s; + registerAccountFactoryForFilesystem( s ); } @@ -331,6 +337,30 @@ AccountManager::removeAccount( Account* account ) } +Account* +AccountManager::accountFromPath( const QString& accountPath ) +{ + foreach ( AccountFactory* factory, m_factoriesForFilesytem ) + { + if ( factory->acceptsPath( accountPath ) ) + { + return factory->createFromPath( accountPath ); + } + } + + Q_ASSERT_X( false, "Shouldn't have had no account factory accepting a path.. at least ResolverAccount!!", ""); + return 0; +} + + +void +AccountManager::registerAccountFactoryForFilesystem( AccountFactory* factory ) +{ + m_factoriesForFilesytem.prepend( factory ); +} + + + void AccountManager::hookupAccount( Account* account ) const { diff --git a/src/libtomahawk/accounts/AccountManager.h b/src/libtomahawk/accounts/AccountManager.h index 969edd27f..acec261d9 100644 --- a/src/libtomahawk/accounts/AccountManager.h +++ b/src/libtomahawk/accounts/AccountManager.h @@ -61,6 +61,21 @@ public: QList< Account* > accounts() const { return m_accounts; }; QList< Account* > accounts( Tomahawk::Accounts::AccountType type ) const { return m_accountsByAccountType[ type ]; } + /** + * Returns a new Account for a certain path on disk. This will go through all on-disk resolver account providers + * to find the most specific account that matches this. + * + * The fallback is ResolverAccount, which handles our generic external script resolvers. + */ + Account* accountFromPath( const QString& path ); + + /** + * Registers an account factory as being able to "handle" resolvers on disk. When accountFromPath is called + * AccountManager will go through all registered account factories in order until it finds one that can handle the path. + * This is searched in LIFO order. + */ + void registerAccountFactoryForFilesystem( AccountFactory* factory ); + public slots: void connectAll(); void disconnectAll(); @@ -97,6 +112,7 @@ private: QHash< AccountType, QList< Account* > > m_accountsByAccountType; QHash< QString, AccountFactory* > m_accountFactories; + QList< AccountFactory* > m_factoriesForFilesytem; static AccountManager* s_instance; }; diff --git a/src/libtomahawk/accounts/ResolverAccount.cpp b/src/libtomahawk/accounts/ResolverAccount.cpp index 95acbe284..76efe47fd 100644 --- a/src/libtomahawk/accounts/ResolverAccount.cpp +++ b/src/libtomahawk/accounts/ResolverAccount.cpp @@ -45,6 +45,13 @@ ResolverAccountFactory::createAccount( const QString& accountId ) } +Account* +ResolverAccountFactory::createFromPath( const QString& path ) +{ + return createFromPath( path, false ); +} + + Account* ResolverAccountFactory::createFromPath( const QString& path, bool isAttica ) { diff --git a/src/libtomahawk/accounts/ResolverAccount.h b/src/libtomahawk/accounts/ResolverAccount.h index e02a7bfe1..db33f157d 100644 --- a/src/libtomahawk/accounts/ResolverAccount.h +++ b/src/libtomahawk/accounts/ResolverAccount.h @@ -44,6 +44,10 @@ public: // Used to create a new resolver from a script on disk, either chosen by // the user, or installed from synchrotron + virtual bool acceptsPath( const QString& path ) const { return true; } // This is the catch-all filesystem account + virtual Account* createFromPath( const QString& path ); + + // Internal use static Account* createFromPath( const QString& path, bool isAttica ); }; diff --git a/src/libtomahawk/accounts/SpotifyAccount.cpp b/src/libtomahawk/accounts/SpotifyAccount.cpp index e9b1dc8f1..24a52c081 100644 --- a/src/libtomahawk/accounts/SpotifyAccount.cpp +++ b/src/libtomahawk/accounts/SpotifyAccount.cpp @@ -35,6 +35,22 @@ SpotifyAccountFactory::createAccount( const QString& accountId ) return new SpotifyAccount( accountId ); } + +bool +SpotifyAccountFactory::acceptsPath( const QString& path ) const +{ + QFileInfo info( path ); + return info.baseName().startsWith( "spotify_" ); +} + + +Account* +SpotifyAccountFactory::createFromPath( const QString& path ) +{ + return new SpotifyAccount( generateId( "spotifyaccount" ), path ); +} + + QPixmap SpotifyAccountFactory::icon() const { @@ -52,6 +68,13 @@ SpotifyAccount::SpotifyAccount( const QString& accountId ) } +SpotifyAccount::SpotifyAccount( const QString& accountId, const QString& path ) + : ResolverAccount( accountId, path ) +{ + +} + + QPixmap SpotifyAccount::icon() const { diff --git a/src/libtomahawk/accounts/SpotifyAccount.h b/src/libtomahawk/accounts/SpotifyAccount.h index 687315a96..67bd97883 100644 --- a/src/libtomahawk/accounts/SpotifyAccount.h +++ b/src/libtomahawk/accounts/SpotifyAccount.h @@ -44,6 +44,9 @@ public: virtual QString factoryId() const { return "spotifyaccount"; } virtual QString prettyName() const { return "Spotify"; } + virtual bool acceptsPath( const QString& path ) const; + virtual Account* createFromPath( const QString& path ); + virtual AccountTypes types() const { return AccountTypes( ResolverType ); } virtual bool allowUserCreation() const { return false; } virtual QPixmap icon() const; @@ -56,6 +59,7 @@ class SpotifyAccount : public ResolverAccount Q_OBJECT public: SpotifyAccount( const QString& accountId ); + SpotifyAccount( const QString& accountId, const QString& path ); virtual ~SpotifyAccount() {} virtual QPixmap icon() const; diff --git a/src/settingsdialog.cpp b/src/settingsdialog.cpp index 62e03e36f..66f3acdb4 100644 --- a/src/settingsdialog.cpp +++ b/src/settingsdialog.cpp @@ -396,7 +396,8 @@ SettingsDialog::installFromFile() if( !resolver.isEmpty() ) { - Account* acct = ResolverAccountFactory::createFromPath( resolver, false ); + Account* acct = AccountManager::instance()->accountFromPath( resolver ); + AccountManager::instance()->addAccount( acct ); TomahawkSettings::instance()->addAccount( acct->accountId() ); AccountManager::instance()->enableAccount( acct );