PageRenderTime 35ms CodeModel.GetById 16ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 19#ifndef ATTICAMANAGER_H
 20#define ATTICAMANAGER_H
 21
 22#include <QObject>
 23#include <QHash>
 24#include <QPixmap>
 25#include <QNetworkReply>
 26
 27#include "DllMacro.h"
 28#include "accounts/Account.h"
 29
 30#include <attica/provider.h>
 31#include <attica/providermanager.h>
 32#include <attica/content.h>
 33
 34namespace Tomahawk {
 35namespace Accounts {
 36class AtticaResolverAccount;
 37}
 38}
 39
 40class BinaryInstallerHelper;
 41
 42class DLLEXPORT AtticaManager : public QObject
 43{
 44    Q_OBJECT
 45public:
 46    enum ResolverState {
 47        Uninstalled = 0,
 48        Installing,
 49        Installed,
 50        NeedsUpgrade,
 51        Upgrading,
 52        Failed
 53    };
 54
 55    struct Resolver {
 56        QString version, scriptPath;
 57        int userRating; // 0-100
 58        ResolverState state;
 59        QPixmap* pixmap;
 60        bool binary;
 61
 62        // internal
 63        bool pixmapDirty;
 64
 65        Resolver( const QString& v, const QString& path, int userR, ResolverState s, bool isBinary )
 66            : version( v ), scriptPath( path ), userRating( userR ), state( s ), pixmap( 0 ), binary( isBinary ), pixmapDirty( false )
 67        {
 68        }
 69        Resolver() : userRating( -1 ), state( Uninstalled ), pixmap( 0 ), binary( false ), pixmapDirty( false ) {}
 70    };
 71
 72    typedef QHash< QString, AtticaManager::Resolver > StateHash;
 73
 74    static AtticaManager* instance()
 75    {
 76        if ( !s_instance )
 77            s_instance = new AtticaManager();
 78
 79        return s_instance;
 80    }
 81
 82    /**
 83     * Delete the AtticaManager if initialised.
 84     *
 85     * Note that <code>delete AtticaManager::instance()</code> will create an
 86     * instance if none existed before.
 87     */
 88    static void deleteInstace()
 89    {
 90        if ( s_instance )
 91        {
 92            delete s_instance;
 93        }
 94    }
 95
 96    explicit AtticaManager ( QObject* parent = 0 );
 97    virtual ~AtticaManager();
 98
 99    bool resolversLoaded() const;
100
101    Attica::Content::List resolvers() const;
102    Attica::Content resolverForId( const QString& id ) const;
103
104    ResolverState resolverState( const Attica::Content& resolver ) const;
105    QPixmap iconForResolver( const Attica::Content& id ); // Looks up in icon cache
106
107    void uninstallResolver( const Attica::Content& resolver );
108    void uninstallResolver( const QString& pathToResolver );
109    QString pathFromId( const QString& resolverId ) const;
110
111    void uploadRating( const Attica::Content& c );
112    bool userHasRated( const Attica::Content& c ) const;
113
114    /**
115      If the resolver coming from libattica has a native custom c++ account
116      as well. For example the last.fm & spotify accounts.
117      */
118    bool hasCustomAccountForAttica( const QString& id ) const;
119    Tomahawk::Accounts::Account* customAccountForAttica( const QString& id ) const;
120    void registerCustomAccount( const QString& atticaId, Tomahawk::Accounts::Account* account );
121
122    AtticaManager::Resolver resolverData( const QString& atticaId ) const;
123
124public slots:
125    void installResolver( const Attica::Content& resolver, bool autoCreateAccount = true );
126    void installResolverWithHandler( const Attica::Content& resolver, Tomahawk::Accounts::AtticaResolverAccount* handler );
127
128    void upgradeResolver( const Attica::Content& resolver );
129
130signals:
131    void resolversLoaded( const Attica::Content::List& resolvers );
132
133    void resolverStateChanged( const QString& resolverId );
134    void resolverInstalled( const QString& resolverId );
135    void resolverUninstalled( const QString& resolverId );
136    void resolverInstallationFailed( const QString& resolverId );
137
138    void resolverIconUpdated( const QString& resolverId );
139
140    void startedInstalling( const QString& resolverId );
141
142private slots:
143    void providerFetched( QNetworkReply* reply );
144    void providerError( QNetworkReply::NetworkError );
145    void providerAdded( const Attica::Provider& );
146    void categoriesReturned( Attica::BaseJob* );
147    void resolversList( Attica::BaseJob* );
148    void binaryResolversList( Attica::BaseJob* );
149    void resolverDownloadFinished( QNetworkReply* );
150    void payloadFetched();
151
152    void loadPixmapsFromCache();
153    void savePixmapsToCache();
154    void resolverIconFetched();
155
156    void syncServerData();
157
158private:
159    void doResolverRemove( const QString& id ) const;
160    void doInstallResolver(  const Attica::Content& resolver, bool autoCreate, Tomahawk::Accounts::AtticaResolverAccount* handler );
161    void fetchMissingIcons();
162    QString hostname() const;
163
164    Attica::ProviderManager m_manager;
165
166    Attica::Provider m_resolverProvider;
167    Attica::Content::List m_resolvers;
168    StateHash m_resolverStates;
169
170    int m_resolverJobsLoaded;
171    QMap< QString, Tomahawk::Accounts::Account* > m_customAccounts;
172
173    static AtticaManager* s_instance;
174
175    friend class ::BinaryInstallerHelper;
176};
177
178class DLLEXPORT CustomAtticaAccount : public Tomahawk::Accounts::Account
179{
180    Q_OBJECT
181public:
182    virtual ~CustomAtticaAccount() {}
183
184    virtual Attica::Content atticaContent() const = 0;
185
186protected:
187    // No, you can't.
188    CustomAtticaAccount( const QString& id ) : Tomahawk::Accounts::Account( id ) {}
189};
190
191Q_DECLARE_METATYPE( Attica::Content );
192#endif // ATTICAMANAGER_H