PageRenderTime 94ms CodeModel.GetById 40ms app.highlight 34ms RepoModel.GetById 16ms app.codeStats 1ms

/src/libtomahawk/infosystem/InfoSystem.h

http://github.com/tomahawk-player/tomahawk
C Header | 273 lines | 177 code | 68 blank | 28 comment | 0 complexity | 796f4c4fda326cbd71585f392f3883a8 MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4 *   Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org>
  5 *
  6 *   Tomahawk is free software: you can redistribute it and/or modify
  7 *   it under the terms of the GNU General Public License as published by
  8 *   the Free Software Foundation, either version 3 of the License, or
  9 *   (at your option) any later version.
 10 *
 11 *   Tomahawk is distributed in the hope that it will be useful,
 12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14 *   GNU General Public License for more details.
 15 *
 16 *   You should have received a copy of the GNU General Public License
 17 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 18 */
 19
 20#ifndef TOMAHAWK_INFOSYSTEM_H
 21#define TOMAHAWK_INFOSYSTEM_H
 22
 23#include "DllMacro.h"
 24#include "utils/TomahawkUtils.h"
 25#include "Typedefs.h"
 26#include "TomahawkPlugin.h"
 27
 28#include <QCryptographicHash>
 29#include <QMap>
 30#include <QObject>
 31#include <QPointer>
 32#include <QSet>
 33#include <QStringList>
 34#include <QThread>
 35#include <QVariant>
 36
 37class QNetworkAccessManager;
 38class DiagnosticsDialog;
 39
 40namespace Tomahawk {
 41
 42namespace InfoSystem {
 43
 44class InfoSystemCache;
 45class InfoSystemWorker;
 46
 47enum PushInfoFlags { // must be powers of 2
 48    PushNoFlag = 1,
 49    PushShortUrlFlag = 2
 50};
 51
 52
 53struct DLLEXPORT InfoRequestData {
 54    quint64 requestId;
 55    quint64 internalId; //do not assign to this; it may get overwritten by the InfoSystem
 56    QString caller;
 57    Tomahawk::InfoSystem::InfoType type;
 58    QVariant input;
 59    QVariantMap customData;
 60    uint timeoutMillis;
 61    bool allSources;
 62
 63    InfoRequestData();
 64
 65    InfoRequestData( const quint64 rId, const QString& callr, const Tomahawk::InfoSystem::InfoType typ, const QVariant& inputvar, const QVariantMap& custom );
 66
 67private:
 68    void init( const QString& callr, const InfoType typ, const QVariant& inputvar, const QVariantMap& custom);
 69};
 70
 71
 72struct InfoPushData {
 73    QString caller;
 74    InfoType type;
 75    QVariant input;
 76    PushInfoFlags pushFlags;
 77    PushInfoPair infoPair;
 78
 79    InfoPushData()
 80        : caller( QString() )
 81        , type( Tomahawk::InfoSystem::InfoNoInfo )
 82        , input( QVariant() )
 83        , pushFlags( Tomahawk::InfoSystem::PushNoFlag )
 84        , infoPair( Tomahawk::InfoSystem::PushInfoPair( QVariantMap(), QVariant() ) )
 85        {}
 86
 87    InfoPushData( const QString& callr, const Tomahawk::InfoSystem::InfoType typ, const QVariant& inputvar, const Tomahawk::InfoSystem::PushInfoFlags pflags )
 88        : caller( callr )
 89        , type( typ )
 90        , input( inputvar )
 91        , pushFlags( pflags )
 92        , infoPair( Tomahawk::InfoSystem::PushInfoPair( QVariantMap(), QVariant() ) )
 93        {}
 94
 95};
 96
 97
 98class DLLEXPORT InfoPlugin : public QObject
 99{
100    Q_OBJECT
101
102public:
103    /**
104     * @brief Creates the plugin. Do *not* perform any network-based setup tasks here; defer that to init(), which will be called automatically.
105     *
106     **/
107    InfoPlugin();
108
109    virtual ~InfoPlugin();
110
111    void setFriendlyName( const QString& friendlyName );
112    virtual const QString friendlyName() const;
113
114    QSet< InfoType > supportedGetTypes() const { return m_supportedGetTypes; }
115    QSet< InfoType > supportedPushTypes() const { return m_supportedPushTypes; }
116
117signals:
118    void getCachedInfo( Tomahawk::InfoSystem::InfoStringHash criteria, qint64 newMaxAge, Tomahawk::InfoSystem::InfoRequestData requestData );
119    void info( Tomahawk::InfoSystem::InfoRequestData requestData, QVariant output );
120
121    void updateCache( Tomahawk::InfoSystem::InfoStringHash criteria, qint64 maxAge, Tomahawk::InfoSystem::InfoType type, QVariant output );
122
123protected slots:
124
125    /**
126     * @brief Called after the plugin has been moved to the appropriate thread. Do network-based setup tasks here.
127     *
128     * @return void
129     **/
130    virtual void init() = 0;
131
132    virtual void getInfo( Tomahawk::InfoSystem::InfoRequestData requestData ) = 0;
133    virtual void pushInfo( Tomahawk::InfoSystem::InfoPushData pushData ) = 0;
134    virtual void notInCacheSlot( Tomahawk::InfoSystem::InfoStringHash criteria, Tomahawk::InfoSystem::InfoRequestData requestData ) = 0;
135
136protected:
137    InfoType m_type;
138    QString m_friendlyName;
139    QSet< InfoType > m_supportedGetTypes;
140    QSet< InfoType > m_supportedPushTypes;
141
142private:
143    friend class InfoSystem;
144};
145
146
147class DLLEXPORT InfoSystemCacheThread : public QThread
148{
149    Q_OBJECT
150
151public:
152    InfoSystemCacheThread( QObject* parent );
153    virtual ~InfoSystemCacheThread();
154
155    void run();
156
157private:
158    friend class InfoSystem;
159    InfoSystemCache* cache() const;
160
161    QPointer< InfoSystemCache > m_cache;
162};
163
164
165class DLLEXPORT InfoSystemWorkerThread : public QThread
166{
167    Q_OBJECT
168
169public:
170    InfoSystemWorkerThread( QObject* parent );
171    virtual ~InfoSystemWorkerThread();
172
173    void run();
174
175private:
176    friend class ::DiagnosticsDialog;
177    friend class InfoSystem;
178    InfoSystemWorker* worker() const;
179
180    QPointer< InfoSystemWorker > m_worker;
181};
182
183
184class DLLEXPORT InfoSystem : public QObject
185{
186    Q_OBJECT
187
188public:
189    static InfoSystem* instance();
190
191    InfoSystem( QObject* parent );
192    ~InfoSystem();
193
194    bool getInfo( const InfoRequestData& requestData );
195    //WARNING: if changing timeoutMillis above, also change in below function in .cpp file
196    bool getInfo( const QString& caller, const QVariantMap& customData, const InfoTypeMap& inputMap, const InfoTimeoutMap& timeoutMap = InfoTimeoutMap(), bool allSources = false );
197    bool pushInfo( InfoPushData pushData );
198    bool pushInfo( const QString& caller, const InfoTypeMap& input, const PushInfoFlags pushFlags );
199
200    const InfoTypeSet& supportedGetTypes() const { return m_supportedGetTypes; }
201    const InfoTypeSet& supportedPushTypes() const { return m_supportedPushTypes; }
202
203    QPointer< QThread > workerThread() const;
204
205public slots:
206    void addInfoPlugin( Tomahawk::InfoSystem::InfoPluginPtr plugin );
207    void removeInfoPlugin( Tomahawk::InfoSystem::InfoPluginPtr plugin );
208
209signals:
210    void info( Tomahawk::InfoSystem::InfoRequestData requestData, QVariant output );
211    void finished( QString target );
212    void finished( QString target, Tomahawk::InfoSystem::InfoType type );
213    void ready();
214
215    void updatedSupportedGetTypes( Tomahawk::InfoSystem::InfoTypeSet supportedTypes );
216    void updatedSupportedPushTypes( Tomahawk::InfoSystem::InfoTypeSet supportedTypes );
217
218private slots:
219    void init();
220    void receiveUpdatedSupportedGetTypes( Tomahawk::InfoSystem::InfoTypeSet supportedTypes );
221    void receiveUpdatedSupportedPushTypes( Tomahawk::InfoSystem::InfoTypeSet supportedTypes );
222
223private:
224    bool m_inited;
225    InfoSystemCacheThread* m_infoSystemCacheThreadController;
226    InfoSystemWorkerThread* m_infoSystemWorkerThreadController;
227
228    InfoTypeSet m_supportedGetTypes;
229    InfoTypeSet m_supportedPushTypes;
230
231    static InfoSystem* s_instance;
232};
233
234}
235
236}
237
238
239inline uint qHash( Tomahawk::InfoSystem::InfoStringHash hash )
240{
241    QCryptographicHash md5( QCryptographicHash::Md5 );
242    QStringList keys = hash.keys();
243    keys.sort();
244    foreach( QString key, keys )
245    {
246        md5.addData( key.toUtf8() );
247        md5.addData( hash[key].toUtf8() );
248    }
249
250    QString hexData = md5.result();
251
252    uint returnval = 0;
253
254    foreach( uint val, hexData.toUcs4() )
255        returnval += val;
256
257    return returnval;
258}
259
260Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoRequestData )
261Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoPushData )
262Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoStringHash )
263Q_DECLARE_METATYPE( Tomahawk::InfoSystem::PushInfoPair )
264Q_DECLARE_METATYPE( Tomahawk::InfoSystem::PushInfoFlags )
265Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoType )
266Q_DECLARE_METATYPE( QList< Tomahawk::InfoSystem::InfoStringHash > )
267Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoPluginPtr )
268Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoPlugin* )
269Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoTypeSet )
270
271Q_DECLARE_INTERFACE( Tomahawk::InfoSystem::InfoPlugin, "tomahawk.InfoPlugin/1.0" )
272
273#endif // TOMAHAWK_INFOSYSTEM_H