PageRenderTime 234ms CodeModel.GetById 100ms app.highlight 19ms RepoModel.GetById 112ms app.codeStats 0ms

/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
 21#ifndef ACCOUNT_H
 22#define ACCOUNT_H
 23
 24#include "Typedefs.h"
 25#include "DllMacro.h"
 26
 27#include <QObject>
 28#include <QVariantMap>
 29#include <QWidget>
 30#include <QIcon>
 31#include <QString>
 32#include <QUuid>
 33#include <QMutex>
 34
 35
 36class SipPlugin;
 37class AccountConfigWidget;
 38
 39namespace Tomahawk
 40{
 41
 42namespace Accounts
 43{
 44
 45class ConfigStorage;
 46
 47enum AccountType
 48{
 49    NoType = 0x00,
 50
 51    InfoType = 0x01,
 52    SipType = 0x02,
 53    ResolverType = 0x04,
 54    StatusPushType = 0x08
 55};
 56
 57// ATTENTION: keep in sync with tomahawk.js
 58enum ConfigTestResultType
 59{
 60    ConfigTestResultOther = 0,
 61    ConfigTestResultSuccess = 1,
 62    ConfigTestResultLogout = 2,
 63    ConfigTestResultCommunicationError = 3,
 64    ConfigTestResultInvalidCredentials = 4,
 65    ConfigTestResultInvalidAccount = 5,
 66    ConfigTestResultPlayingElsewhere = 6,
 67    ConfigTestResultAccountExpired = 7
 68};
 69
 70DLLEXPORT QString accountTypeToString( AccountType type );
 71
 72Q_DECLARE_FLAGS( AccountTypes, AccountType )
 73
 74inline QString generateId( const QString& factoryId )
 75{
 76    QString uniq = QUuid::createUuid().toString().mid( 1, 8 );
 77    return factoryId + "_" + uniq;
 78}
 79
 80class DLLEXPORT Account : public QObject
 81{
 82    Q_OBJECT
 83
 84public:
 85    struct Configuration
 86    {
 87        QString accountFriendlyName;
 88        bool enabled;
 89        QVariantHash configuration;
 90        QVariantMap acl;
 91        QStringList types;
 92        QVariantMap credentials;
 93    };
 94
 95    enum AuthErrorCode { AuthError, ConnectionError };
 96    enum ConnectionState { Disconnected, Connecting, Connected, Disconnecting };
 97
 98    explicit Account( const QString& accountId );
 99    virtual ~Account();
100
101    QString accountServiceName() const { QMutexLocker locker( &m_mutex ); return m_accountServiceName; } // e.g. "Twitter", "Last.fm"
102    QString accountFriendlyName() const { QMutexLocker locker( &m_mutex ); return m_cfg.accountFriendlyName; } // e.g. screen name on the service, JID, etc.
103    bool enabled() const { QMutexLocker locker( &m_mutex ); return m_cfg.enabled; }
104    QString accountId() const { QMutexLocker locker( &m_mutex ); return m_accountId; }
105
106    QVariantHash configuration() const { QMutexLocker locker( &m_mutex ); return m_cfg.configuration; }
107
108    /**
109     * Configuration widgets can have a "dataError( bool )" signal to enable/disable the OK button in their wrapper dialogs.
110     */
111    virtual AccountConfigWidget* configurationWidget() = 0;
112    virtual QWidget* aboutWidget() { return 0; }
113    virtual QWidget* aclWidget() = 0;
114    virtual QPixmap icon() const = 0;
115
116    virtual QString description() const { return QString(); }
117    virtual QString author() const { return QString(); }
118    virtual QString version() const { return QString(); }
119
120    virtual void saveConfig() {} // called when the widget has been edited. save values from config widget, call sync() to write to disk account generic settings
121
122    QVariantMap credentials() const { QMutexLocker locker( &m_mutex ); return m_cfg.credentials; }
123
124    QVariantMap acl() const { QMutexLocker locker( &m_mutex ); return m_cfg.acl; }
125
126    virtual ConnectionState connectionState() const = 0;
127    virtual bool isAuthenticated() const = 0;
128
129    virtual QString errorMessage() const { QMutexLocker locker( &m_mutex ); return m_cachedError; }
130
131    virtual Tomahawk::InfoSystem::InfoPluginPtr infoPlugin() = 0;
132    virtual SipPlugin* sipPlugin( bool create = true ) = 0;
133
134    // Some accounts cannot be enabled if authentication fails. Return true after failing to authenticate
135    // if this is the case, and the account will not be enabled
136    virtual bool preventEnabling() const { return false; }
137
138    AccountTypes types() const;
139
140    void setAccountServiceName( const QString &serviceName ) { QMutexLocker locker( &m_mutex ); m_accountServiceName = serviceName; }
141    void setAccountFriendlyName( const QString &friendlyName )  { QMutexLocker locker( &m_mutex ); m_cfg.accountFriendlyName = friendlyName; }
142    void setEnabled( bool enabled ) { QMutexLocker locker( &m_mutex ); m_cfg.enabled = enabled; }
143    void setAccountId( const QString &accountId )  { QMutexLocker locker( &m_mutex ); m_accountId = accountId; }
144    void setCredentials( const QVariantMap &credentialHash ) { QMutexLocker locker( &m_mutex ); m_cfg.credentials = credentialHash; }
145    void setConfiguration( const QVariantHash &configuration ) { QMutexLocker locker( &m_mutex ); m_cfg.configuration = configuration; }
146    void setAcl( const QVariantMap &acl ) { QMutexLocker locker( &m_mutex ); m_cfg.acl = acl; }
147
148    void setTypes( AccountTypes types );
149
150    void sync() { QMutexLocker locker( &m_mutex ); syncConfig(); }
151
152    /**
153     * Removes all the settings held in the config file for this account instance
154     *
155     * Re-implement if you have saved additional files or config settings outside the built-in ones
156     */
157    virtual void removeFromConfig();
158
159    virtual void testConfig();
160
161public slots:
162    virtual void authenticate() = 0;
163    virtual void deauthenticate() = 0;
164
165signals:
166    void error( int errorId, const QString& errorStr );
167    void connectionStateChanged( Tomahawk::Accounts::Account::ConnectionState state );
168
169    void configurationChanged();
170    void configTestResult( int, const QString& = QString() );
171
172protected:
173    virtual void loadFromConfig( const QString &accountId );
174    virtual void syncConfig();
175
176private slots:
177    void onConnectionStateChanged( Tomahawk::Accounts::Account::ConnectionState );
178    void onError( int, const QString& );
179
180private:
181    QString m_accountServiceName;
182    QString m_cachedError;
183    QString m_accountId;
184
185    mutable QMutex m_mutex;
186
187    Account::Configuration m_cfg;
188};
189
190class DLLEXPORT AccountFactory : public QObject
191{
192    Q_OBJECT
193public:
194    AccountFactory() {}
195    virtual ~AccountFactory() {}
196
197    // display name for plugin
198    virtual QString prettyName() const = 0;
199    // internal name
200    virtual QString factoryId() const = 0;
201    // description to be shown when user views a list of account types
202    virtual QString description() const = 0;
203    // if the user can create multiple
204    virtual bool isUnique() const { return false; }
205
206    virtual QPixmap icon() const { return QPixmap(); }
207    virtual bool allowUserCreation() const { return true; }
208
209    // What are the supported types for accounts this factory creates?
210    virtual AccountTypes types() const = 0;
211
212    virtual Account* createAccount( const QString& accountId = QString() ) = 0;
213
214    /// If this resolver type accepts this path on disk (For general and special resolver accounts)
215    virtual bool acceptsPath( const QString& ) const { return false; }
216    virtual Account* createFromPath( const QString& ) { return 0; }
217};
218
219}
220
221}
222
223Q_DECLARE_INTERFACE( Tomahawk::Accounts::AccountFactory, "tomahawk.AccountFactory/1.0" )
224
225Q_DECLARE_METATYPE( Tomahawk::Accounts::Account* )
226Q_DECLARE_METATYPE( QList< Tomahawk::Accounts::Account* > )
227Q_DECLARE_METATYPE( Tomahawk::Accounts::AccountTypes )
228
229#endif