/src/libtomahawk/AtticaManager.h

http://github.com/tomahawk-player/tomahawk · C Header · 192 lines · 122 code · 41 blank · 29 comment · 2 complexity · 8ce949e391e28480988a07e4a91bf18f MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
  4. *
  5. * Tomahawk is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Tomahawk is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef ATTICAMANAGER_H
  19. #define ATTICAMANAGER_H
  20. #include <QObject>
  21. #include <QHash>
  22. #include <QPixmap>
  23. #include <QNetworkReply>
  24. #include "DllMacro.h"
  25. #include "accounts/Account.h"
  26. #include <attica/provider.h>
  27. #include <attica/providermanager.h>
  28. #include <attica/content.h>
  29. namespace Tomahawk {
  30. namespace Accounts {
  31. class AtticaResolverAccount;
  32. }
  33. }
  34. class BinaryInstallerHelper;
  35. class DLLEXPORT AtticaManager : public QObject
  36. {
  37. Q_OBJECT
  38. public:
  39. enum ResolverState {
  40. Uninstalled = 0,
  41. Installing,
  42. Installed,
  43. NeedsUpgrade,
  44. Upgrading,
  45. Failed
  46. };
  47. struct Resolver {
  48. QString version, scriptPath;
  49. int userRating; // 0-100
  50. ResolverState state;
  51. QPixmap* pixmap;
  52. bool binary;
  53. // internal
  54. bool pixmapDirty;
  55. Resolver( const QString& v, const QString& path, int userR, ResolverState s, bool isBinary )
  56. : version( v ), scriptPath( path ), userRating( userR ), state( s ), pixmap( 0 ), binary( isBinary ), pixmapDirty( false )
  57. {
  58. }
  59. Resolver() : userRating( -1 ), state( Uninstalled ), pixmap( 0 ), binary( false ), pixmapDirty( false ) {}
  60. };
  61. typedef QHash< QString, AtticaManager::Resolver > StateHash;
  62. static AtticaManager* instance()
  63. {
  64. if ( !s_instance )
  65. s_instance = new AtticaManager();
  66. return s_instance;
  67. }
  68. /**
  69. * Delete the AtticaManager if initialised.
  70. *
  71. * Note that <code>delete AtticaManager::instance()</code> will create an
  72. * instance if none existed before.
  73. */
  74. static void deleteInstace()
  75. {
  76. if ( s_instance )
  77. {
  78. delete s_instance;
  79. }
  80. }
  81. explicit AtticaManager ( QObject* parent = 0 );
  82. virtual ~AtticaManager();
  83. bool resolversLoaded() const;
  84. Attica::Content::List resolvers() const;
  85. Attica::Content resolverForId( const QString& id ) const;
  86. ResolverState resolverState( const Attica::Content& resolver ) const;
  87. QPixmap iconForResolver( const Attica::Content& id ); // Looks up in icon cache
  88. void uninstallResolver( const Attica::Content& resolver );
  89. void uninstallResolver( const QString& pathToResolver );
  90. QString pathFromId( const QString& resolverId ) const;
  91. void uploadRating( const Attica::Content& c );
  92. bool userHasRated( const Attica::Content& c ) const;
  93. /**
  94. If the resolver coming from libattica has a native custom c++ account
  95. as well. For example the last.fm & spotify accounts.
  96. */
  97. bool hasCustomAccountForAttica( const QString& id ) const;
  98. Tomahawk::Accounts::Account* customAccountForAttica( const QString& id ) const;
  99. void registerCustomAccount( const QString& atticaId, Tomahawk::Accounts::Account* account );
  100. AtticaManager::Resolver resolverData( const QString& atticaId ) const;
  101. public slots:
  102. void installResolver( const Attica::Content& resolver, bool autoCreateAccount = true );
  103. void installResolverWithHandler( const Attica::Content& resolver, Tomahawk::Accounts::AtticaResolverAccount* handler );
  104. void upgradeResolver( const Attica::Content& resolver );
  105. signals:
  106. void resolversLoaded( const Attica::Content::List& resolvers );
  107. void resolverStateChanged( const QString& resolverId );
  108. void resolverInstalled( const QString& resolverId );
  109. void resolverUninstalled( const QString& resolverId );
  110. void resolverInstallationFailed( const QString& resolverId );
  111. void resolverIconUpdated( const QString& resolverId );
  112. void startedInstalling( const QString& resolverId );
  113. private slots:
  114. void providerFetched( QNetworkReply* reply );
  115. void providerError( QNetworkReply::NetworkError );
  116. void providerAdded( const Attica::Provider& );
  117. void categoriesReturned( Attica::BaseJob* );
  118. void resolversList( Attica::BaseJob* );
  119. void binaryResolversList( Attica::BaseJob* );
  120. void resolverDownloadFinished( QNetworkReply* );
  121. void payloadFetched();
  122. void loadPixmapsFromCache();
  123. void savePixmapsToCache();
  124. void resolverIconFetched();
  125. void syncServerData();
  126. private:
  127. void doResolverRemove( const QString& id ) const;
  128. void doInstallResolver( const Attica::Content& resolver, bool autoCreate, Tomahawk::Accounts::AtticaResolverAccount* handler );
  129. void fetchMissingIcons();
  130. QString hostname() const;
  131. Attica::ProviderManager m_manager;
  132. Attica::Provider m_resolverProvider;
  133. Attica::Content::List m_resolvers;
  134. StateHash m_resolverStates;
  135. int m_resolverJobsLoaded;
  136. QMap< QString, Tomahawk::Accounts::Account* > m_customAccounts;
  137. static AtticaManager* s_instance;
  138. friend class ::BinaryInstallerHelper;
  139. };
  140. class DLLEXPORT CustomAtticaAccount : public Tomahawk::Accounts::Account
  141. {
  142. Q_OBJECT
  143. public:
  144. virtual ~CustomAtticaAccount() {}
  145. virtual Attica::Content atticaContent() const = 0;
  146. protected:
  147. // No, you can't.
  148. CustomAtticaAccount( const QString& id ) : Tomahawk::Accounts::Account( id ) {}
  149. };
  150. Q_DECLARE_METATYPE( Attica::Content );
  151. #endif // ATTICAMANAGER_H