/src/libtomahawk/accounts/Account.h

http://github.com/tomahawk-player/tomahawk · C Header · 229 lines · 139 code · 54 blank · 36 comment · 0 complexity · f5eb5e23e6113fbeaacce31687a93e17 MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4. * Copyright 2011, Leo Franchi <lfranchi@kde.org>
  5. * Copyright 2013, Teo Mrnjavac <teo@kde.org>
  6. *
  7. * Tomahawk is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Tomahawk is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef ACCOUNT_H
  21. #define ACCOUNT_H
  22. #include "Typedefs.h"
  23. #include "DllMacro.h"
  24. #include <QObject>
  25. #include <QVariantMap>
  26. #include <QWidget>
  27. #include <QIcon>
  28. #include <QString>
  29. #include <QUuid>
  30. #include <QMutex>
  31. class SipPlugin;
  32. class AccountConfigWidget;
  33. namespace Tomahawk
  34. {
  35. namespace Accounts
  36. {
  37. class ConfigStorage;
  38. enum AccountType
  39. {
  40. NoType = 0x00,
  41. InfoType = 0x01,
  42. SipType = 0x02,
  43. ResolverType = 0x04,
  44. StatusPushType = 0x08
  45. };
  46. // ATTENTION: keep in sync with tomahawk.js
  47. enum ConfigTestResultType
  48. {
  49. ConfigTestResultOther = 0,
  50. ConfigTestResultSuccess = 1,
  51. ConfigTestResultLogout = 2,
  52. ConfigTestResultCommunicationError = 3,
  53. ConfigTestResultInvalidCredentials = 4,
  54. ConfigTestResultInvalidAccount = 5,
  55. ConfigTestResultPlayingElsewhere = 6,
  56. ConfigTestResultAccountExpired = 7
  57. };
  58. DLLEXPORT QString accountTypeToString( AccountType type );
  59. Q_DECLARE_FLAGS( AccountTypes, AccountType )
  60. inline QString generateId( const QString& factoryId )
  61. {
  62. QString uniq = QUuid::createUuid().toString().mid( 1, 8 );
  63. return factoryId + "_" + uniq;
  64. }
  65. class DLLEXPORT Account : public QObject
  66. {
  67. Q_OBJECT
  68. public:
  69. struct Configuration
  70. {
  71. QString accountFriendlyName;
  72. bool enabled;
  73. QVariantHash configuration;
  74. QVariantMap acl;
  75. QStringList types;
  76. QVariantMap credentials;
  77. };
  78. enum AuthErrorCode { AuthError, ConnectionError };
  79. enum ConnectionState { Disconnected, Connecting, Connected, Disconnecting };
  80. explicit Account( const QString& accountId );
  81. virtual ~Account();
  82. QString accountServiceName() const { QMutexLocker locker( &m_mutex ); return m_accountServiceName; } // e.g. "Twitter", "Last.fm"
  83. QString accountFriendlyName() const { QMutexLocker locker( &m_mutex ); return m_cfg.accountFriendlyName; } // e.g. screen name on the service, JID, etc.
  84. bool enabled() const { QMutexLocker locker( &m_mutex ); return m_cfg.enabled; }
  85. QString accountId() const { QMutexLocker locker( &m_mutex ); return m_accountId; }
  86. QVariantHash configuration() const { QMutexLocker locker( &m_mutex ); return m_cfg.configuration; }
  87. /**
  88. * Configuration widgets can have a "dataError( bool )" signal to enable/disable the OK button in their wrapper dialogs.
  89. */
  90. virtual AccountConfigWidget* configurationWidget() = 0;
  91. virtual QWidget* aboutWidget() { return 0; }
  92. virtual QWidget* aclWidget() = 0;
  93. virtual QPixmap icon() const = 0;
  94. virtual QString description() const { return QString(); }
  95. virtual QString author() const { return QString(); }
  96. virtual QString version() const { return QString(); }
  97. virtual void saveConfig() {} // called when the widget has been edited. save values from config widget, call sync() to write to disk account generic settings
  98. QVariantMap credentials() const { QMutexLocker locker( &m_mutex ); return m_cfg.credentials; }
  99. QVariantMap acl() const { QMutexLocker locker( &m_mutex ); return m_cfg.acl; }
  100. virtual ConnectionState connectionState() const = 0;
  101. virtual bool isAuthenticated() const = 0;
  102. virtual QString errorMessage() const { QMutexLocker locker( &m_mutex ); return m_cachedError; }
  103. virtual Tomahawk::InfoSystem::InfoPluginPtr infoPlugin() = 0;
  104. virtual SipPlugin* sipPlugin( bool create = true ) = 0;
  105. // Some accounts cannot be enabled if authentication fails. Return true after failing to authenticate
  106. // if this is the case, and the account will not be enabled
  107. virtual bool preventEnabling() const { return false; }
  108. AccountTypes types() const;
  109. void setAccountServiceName( const QString &serviceName ) { QMutexLocker locker( &m_mutex ); m_accountServiceName = serviceName; }
  110. void setAccountFriendlyName( const QString &friendlyName ) { QMutexLocker locker( &m_mutex ); m_cfg.accountFriendlyName = friendlyName; }
  111. void setEnabled( bool enabled ) { QMutexLocker locker( &m_mutex ); m_cfg.enabled = enabled; }
  112. void setAccountId( const QString &accountId ) { QMutexLocker locker( &m_mutex ); m_accountId = accountId; }
  113. void setCredentials( const QVariantMap &credentialHash ) { QMutexLocker locker( &m_mutex ); m_cfg.credentials = credentialHash; }
  114. void setConfiguration( const QVariantHash &configuration ) { QMutexLocker locker( &m_mutex ); m_cfg.configuration = configuration; }
  115. void setAcl( const QVariantMap &acl ) { QMutexLocker locker( &m_mutex ); m_cfg.acl = acl; }
  116. void setTypes( AccountTypes types );
  117. void sync() { QMutexLocker locker( &m_mutex ); syncConfig(); }
  118. /**
  119. * Removes all the settings held in the config file for this account instance
  120. *
  121. * Re-implement if you have saved additional files or config settings outside the built-in ones
  122. */
  123. virtual void removeFromConfig();
  124. virtual void testConfig();
  125. public slots:
  126. virtual void authenticate() = 0;
  127. virtual void deauthenticate() = 0;
  128. signals:
  129. void error( int errorId, const QString& errorStr );
  130. void connectionStateChanged( Tomahawk::Accounts::Account::ConnectionState state );
  131. void configurationChanged();
  132. void configTestResult( int, const QString& = QString() );
  133. protected:
  134. virtual void loadFromConfig( const QString &accountId );
  135. virtual void syncConfig();
  136. private slots:
  137. void onConnectionStateChanged( Tomahawk::Accounts::Account::ConnectionState );
  138. void onError( int, const QString& );
  139. private:
  140. QString m_accountServiceName;
  141. QString m_cachedError;
  142. QString m_accountId;
  143. mutable QMutex m_mutex;
  144. Account::Configuration m_cfg;
  145. };
  146. class DLLEXPORT AccountFactory : public QObject
  147. {
  148. Q_OBJECT
  149. public:
  150. AccountFactory() {}
  151. virtual ~AccountFactory() {}
  152. // display name for plugin
  153. virtual QString prettyName() const = 0;
  154. // internal name
  155. virtual QString factoryId() const = 0;
  156. // description to be shown when user views a list of account types
  157. virtual QString description() const = 0;
  158. // if the user can create multiple
  159. virtual bool isUnique() const { return false; }
  160. virtual QPixmap icon() const { return QPixmap(); }
  161. virtual bool allowUserCreation() const { return true; }
  162. // What are the supported types for accounts this factory creates?
  163. virtual AccountTypes types() const = 0;
  164. virtual Account* createAccount( const QString& accountId = QString() ) = 0;
  165. /// If this resolver type accepts this path on disk (For general and special resolver accounts)
  166. virtual bool acceptsPath( const QString& ) const { return false; }
  167. virtual Account* createFromPath( const QString& ) { return 0; }
  168. };
  169. }
  170. }
  171. Q_DECLARE_INTERFACE( Tomahawk::Accounts::AccountFactory, "tomahawk.AccountFactory/1.0" )
  172. Q_DECLARE_METATYPE( Tomahawk::Accounts::Account* )
  173. Q_DECLARE_METATYPE( QList< Tomahawk::Accounts::Account* > )
  174. Q_DECLARE_METATYPE( Tomahawk::Accounts::AccountTypes )
  175. #endif