/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. #ifndef TOMAHAWK_INFOSYSTEM_H
  20. #define TOMAHAWK_INFOSYSTEM_H
  21. #include "DllMacro.h"
  22. #include "utils/TomahawkUtils.h"
  23. #include "Typedefs.h"
  24. #include "TomahawkPlugin.h"
  25. #include <QCryptographicHash>
  26. #include <QMap>
  27. #include <QObject>
  28. #include <QPointer>
  29. #include <QSet>
  30. #include <QStringList>
  31. #include <QThread>
  32. #include <QVariant>
  33. class QNetworkAccessManager;
  34. class DiagnosticsDialog;
  35. namespace Tomahawk {
  36. namespace InfoSystem {
  37. class InfoSystemCache;
  38. class InfoSystemWorker;
  39. enum PushInfoFlags { // must be powers of 2
  40. PushNoFlag = 1,
  41. PushShortUrlFlag = 2
  42. };
  43. struct DLLEXPORT InfoRequestData {
  44. quint64 requestId;
  45. quint64 internalId; //do not assign to this; it may get overwritten by the InfoSystem
  46. QString caller;
  47. Tomahawk::InfoSystem::InfoType type;
  48. QVariant input;
  49. QVariantMap customData;
  50. uint timeoutMillis;
  51. bool allSources;
  52. InfoRequestData();
  53. InfoRequestData( const quint64 rId, const QString& callr, const Tomahawk::InfoSystem::InfoType typ, const QVariant& inputvar, const QVariantMap& custom );
  54. private:
  55. void init( const QString& callr, const InfoType typ, const QVariant& inputvar, const QVariantMap& custom);
  56. };
  57. struct InfoPushData {
  58. QString caller;
  59. InfoType type;
  60. QVariant input;
  61. PushInfoFlags pushFlags;
  62. PushInfoPair infoPair;
  63. InfoPushData()
  64. : caller( QString() )
  65. , type( Tomahawk::InfoSystem::InfoNoInfo )
  66. , input( QVariant() )
  67. , pushFlags( Tomahawk::InfoSystem::PushNoFlag )
  68. , infoPair( Tomahawk::InfoSystem::PushInfoPair( QVariantMap(), QVariant() ) )
  69. {}
  70. InfoPushData( const QString& callr, const Tomahawk::InfoSystem::InfoType typ, const QVariant& inputvar, const Tomahawk::InfoSystem::PushInfoFlags pflags )
  71. : caller( callr )
  72. , type( typ )
  73. , input( inputvar )
  74. , pushFlags( pflags )
  75. , infoPair( Tomahawk::InfoSystem::PushInfoPair( QVariantMap(), QVariant() ) )
  76. {}
  77. };
  78. class DLLEXPORT InfoPlugin : public QObject
  79. {
  80. Q_OBJECT
  81. public:
  82. /**
  83. * @brief Creates the plugin. Do *not* perform any network-based setup tasks here; defer that to init(), which will be called automatically.
  84. *
  85. **/
  86. InfoPlugin();
  87. virtual ~InfoPlugin();
  88. void setFriendlyName( const QString& friendlyName );
  89. virtual const QString friendlyName() const;
  90. QSet< InfoType > supportedGetTypes() const { return m_supportedGetTypes; }
  91. QSet< InfoType > supportedPushTypes() const { return m_supportedPushTypes; }
  92. signals:
  93. void getCachedInfo( Tomahawk::InfoSystem::InfoStringHash criteria, qint64 newMaxAge, Tomahawk::InfoSystem::InfoRequestData requestData );
  94. void info( Tomahawk::InfoSystem::InfoRequestData requestData, QVariant output );
  95. void updateCache( Tomahawk::InfoSystem::InfoStringHash criteria, qint64 maxAge, Tomahawk::InfoSystem::InfoType type, QVariant output );
  96. protected slots:
  97. /**
  98. * @brief Called after the plugin has been moved to the appropriate thread. Do network-based setup tasks here.
  99. *
  100. * @return void
  101. **/
  102. virtual void init() = 0;
  103. virtual void getInfo( Tomahawk::InfoSystem::InfoRequestData requestData ) = 0;
  104. virtual void pushInfo( Tomahawk::InfoSystem::InfoPushData pushData ) = 0;
  105. virtual void notInCacheSlot( Tomahawk::InfoSystem::InfoStringHash criteria, Tomahawk::InfoSystem::InfoRequestData requestData ) = 0;
  106. protected:
  107. InfoType m_type;
  108. QString m_friendlyName;
  109. QSet< InfoType > m_supportedGetTypes;
  110. QSet< InfoType > m_supportedPushTypes;
  111. private:
  112. friend class InfoSystem;
  113. };
  114. class DLLEXPORT InfoSystemCacheThread : public QThread
  115. {
  116. Q_OBJECT
  117. public:
  118. InfoSystemCacheThread( QObject* parent );
  119. virtual ~InfoSystemCacheThread();
  120. void run();
  121. private:
  122. friend class InfoSystem;
  123. InfoSystemCache* cache() const;
  124. QPointer< InfoSystemCache > m_cache;
  125. };
  126. class DLLEXPORT InfoSystemWorkerThread : public QThread
  127. {
  128. Q_OBJECT
  129. public:
  130. InfoSystemWorkerThread( QObject* parent );
  131. virtual ~InfoSystemWorkerThread();
  132. void run();
  133. private:
  134. friend class ::DiagnosticsDialog;
  135. friend class InfoSystem;
  136. InfoSystemWorker* worker() const;
  137. QPointer< InfoSystemWorker > m_worker;
  138. };
  139. class DLLEXPORT InfoSystem : public QObject
  140. {
  141. Q_OBJECT
  142. public:
  143. static InfoSystem* instance();
  144. InfoSystem( QObject* parent );
  145. ~InfoSystem();
  146. bool getInfo( const InfoRequestData& requestData );
  147. //WARNING: if changing timeoutMillis above, also change in below function in .cpp file
  148. bool getInfo( const QString& caller, const QVariantMap& customData, const InfoTypeMap& inputMap, const InfoTimeoutMap& timeoutMap = InfoTimeoutMap(), bool allSources = false );
  149. bool pushInfo( InfoPushData pushData );
  150. bool pushInfo( const QString& caller, const InfoTypeMap& input, const PushInfoFlags pushFlags );
  151. const InfoTypeSet& supportedGetTypes() const { return m_supportedGetTypes; }
  152. const InfoTypeSet& supportedPushTypes() const { return m_supportedPushTypes; }
  153. QPointer< QThread > workerThread() const;
  154. public slots:
  155. void addInfoPlugin( Tomahawk::InfoSystem::InfoPluginPtr plugin );
  156. void removeInfoPlugin( Tomahawk::InfoSystem::InfoPluginPtr plugin );
  157. signals:
  158. void info( Tomahawk::InfoSystem::InfoRequestData requestData, QVariant output );
  159. void finished( QString target );
  160. void finished( QString target, Tomahawk::InfoSystem::InfoType type );
  161. void ready();
  162. void updatedSupportedGetTypes( Tomahawk::InfoSystem::InfoTypeSet supportedTypes );
  163. void updatedSupportedPushTypes( Tomahawk::InfoSystem::InfoTypeSet supportedTypes );
  164. private slots:
  165. void init();
  166. void receiveUpdatedSupportedGetTypes( Tomahawk::InfoSystem::InfoTypeSet supportedTypes );
  167. void receiveUpdatedSupportedPushTypes( Tomahawk::InfoSystem::InfoTypeSet supportedTypes );
  168. private:
  169. bool m_inited;
  170. InfoSystemCacheThread* m_infoSystemCacheThreadController;
  171. InfoSystemWorkerThread* m_infoSystemWorkerThreadController;
  172. InfoTypeSet m_supportedGetTypes;
  173. InfoTypeSet m_supportedPushTypes;
  174. static InfoSystem* s_instance;
  175. };
  176. }
  177. }
  178. inline uint qHash( Tomahawk::InfoSystem::InfoStringHash hash )
  179. {
  180. QCryptographicHash md5( QCryptographicHash::Md5 );
  181. QStringList keys = hash.keys();
  182. keys.sort();
  183. foreach( QString key, keys )
  184. {
  185. md5.addData( key.toUtf8() );
  186. md5.addData( hash[key].toUtf8() );
  187. }
  188. QString hexData = md5.result();
  189. uint returnval = 0;
  190. foreach( uint val, hexData.toUcs4() )
  191. returnval += val;
  192. return returnval;
  193. }
  194. Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoRequestData )
  195. Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoPushData )
  196. Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoStringHash )
  197. Q_DECLARE_METATYPE( Tomahawk::InfoSystem::PushInfoPair )
  198. Q_DECLARE_METATYPE( Tomahawk::InfoSystem::PushInfoFlags )
  199. Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoType )
  200. Q_DECLARE_METATYPE( QList< Tomahawk::InfoSystem::InfoStringHash > )
  201. Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoPluginPtr )
  202. Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoPlugin* )
  203. Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoTypeSet )
  204. Q_DECLARE_INTERFACE( Tomahawk::InfoSystem::InfoPlugin, "tomahawk.InfoPlugin/1.0" )
  205. #endif // TOMAHAWK_INFOSYSTEM_H