/src/libtomahawk/resolvers/qtscriptresolver.h

http://github.com/tomahawk-player/tomahawk · C Header · 184 lines · 123 code · 41 blank · 20 comment · 0 complexity · 636452652b7eed91586879b04e7b880a 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, Leo Franchi <lfranchi@kde.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 QTSCRIPTRESOLVER_H
  20. #define QTSCRIPTRESOLVER_H
  21. #include "ExternalResolverGui.h"
  22. #include "Query.h"
  23. #include "utils/TomahawkUtils.h"
  24. #include "config.h"
  25. #include <QtCore/QDir>
  26. #include <QtCore/QFile>
  27. #include <QtCore/QThread>
  28. #include <QtWebKit/QWebPage>
  29. #include <QtWebKit/QWebFrame>
  30. #ifdef QCA2_FOUND
  31. #include <QtCrypto>
  32. #endif
  33. #include "DllMacro.h"
  34. class QtScriptResolver;
  35. class DLLEXPORT QtScriptResolverHelper : public QObject
  36. {
  37. Q_OBJECT
  38. public:
  39. QtScriptResolverHelper( const QString& scriptPath, QtScriptResolver* parent );
  40. void setResolverConfig( const QVariantMap& config );
  41. // Return a HMAC (md5) signature of the input text with the desired key
  42. Q_INVOKABLE QString hmac( const QByteArray& key, const QByteArray& input );
  43. Q_INVOKABLE QString md5( const QByteArray& input );
  44. Q_INVOKABLE void addCustomUrlHandler( const QString& protocol, const QString& callbackFuncName );
  45. Q_INVOKABLE QByteArray base64Encode( const QByteArray& input );
  46. Q_INVOKABLE QByteArray base64Decode( const QByteArray& input );
  47. QSharedPointer<QIODevice> customIODeviceFactory( const Tomahawk::result_ptr& result );
  48. public slots:
  49. QByteArray readRaw( const QString& fileName );
  50. QString readBase64( const QString& fileName );
  51. QString readCompressed( const QString& fileName );
  52. QString compress( const QString& data );
  53. QVariantMap resolverData();
  54. void log( const QString& message );
  55. bool fakeEnv() { return false; }
  56. void addTrackResults( const QVariantMap& results );
  57. private:
  58. QString m_scriptPath, m_urlCallback;
  59. QVariantMap m_resolverConfig;
  60. QtScriptResolver* m_resolver;
  61. #ifdef QCA2_FOUND
  62. QCA::Initializer m_qcaInit;
  63. #endif
  64. };
  65. class DLLEXPORT ScriptEngine : public QWebPage
  66. {
  67. Q_OBJECT
  68. public:
  69. explicit ScriptEngine( QtScriptResolver* parent )
  70. : QWebPage( (QObject*) parent )
  71. , m_parent( parent )
  72. {
  73. settings()->setAttribute( QWebSettings::OfflineStorageDatabaseEnabled, true );
  74. settings()->setOfflineStoragePath( TomahawkUtils::appDataDir().path() );
  75. settings()->setAttribute(QWebSettings::LocalStorageEnabled, true);
  76. settings()->setLocalStoragePath( TomahawkUtils::appDataDir().path() );
  77. settings()->setAttribute( QWebSettings::LocalStorageDatabaseEnabled, true );
  78. settings()->setAttribute( QWebSettings::LocalContentCanAccessFileUrls, true );
  79. settings()->setAttribute( QWebSettings::LocalContentCanAccessRemoteUrls, true );
  80. }
  81. void setScriptPath( const QString& scriptPath )
  82. {
  83. m_scriptPath = scriptPath;
  84. }
  85. public slots:
  86. bool shouldInterruptJavaScript()
  87. {
  88. return true;
  89. }
  90. protected:
  91. virtual void javaScriptConsoleMessage( const QString& message, int lineNumber, const QString& sourceID );
  92. private:
  93. QtScriptResolver* m_parent;
  94. QString m_scriptPath;
  95. };
  96. class DLLEXPORT QtScriptResolver : public Tomahawk::ExternalResolverGui
  97. {
  98. Q_OBJECT
  99. friend class ::QtScriptResolverHelper;
  100. public:
  101. explicit QtScriptResolver( const QString& scriptPath );
  102. virtual ~QtScriptResolver();
  103. static ExternalResolver* factory( const QString& scriptPath );
  104. virtual QString name() const { return m_name; }
  105. virtual QPixmap icon() const { return m_icon; }
  106. virtual unsigned int weight() const { return m_weight; }
  107. virtual unsigned int timeout() const { return m_timeout; }
  108. virtual QWidget* configUI() const;
  109. virtual void saveConfig();
  110. virtual ExternalResolver::ErrorState error() const;
  111. virtual bool running() const;
  112. virtual void reload();
  113. virtual void setIcon( const QPixmap& icon ) { m_icon = icon; }
  114. public slots:
  115. virtual void resolve( const Tomahawk::query_ptr& query );
  116. virtual void stop();
  117. virtual void start();
  118. signals:
  119. void stopped();
  120. private:
  121. void init();
  122. void loadUi();
  123. QWidget* findWidget( QWidget* widget, const QString& objectName );
  124. void setWidgetData( const QVariant& value, QWidget* widget, const QString& property );
  125. QVariant widgetData( QWidget* widget, const QString& property );
  126. QVariantMap loadDataFromWidgets();
  127. void fillDataInWidgets( const QVariantMap& data );
  128. // encapsulate javascript calls
  129. QVariantMap resolverSettings();
  130. QVariantMap resolverUserConfig();
  131. QVariantMap resolverInit();
  132. QList< Tomahawk::result_ptr > parseResultVariantList( const QVariantList& reslist );
  133. ScriptEngine* m_engine;
  134. QString m_name;
  135. QPixmap m_icon;
  136. unsigned int m_weight, m_timeout;
  137. bool m_ready, m_stopped;
  138. ExternalResolver::ErrorState m_error;
  139. QtScriptResolverHelper* m_resolverHelper;
  140. QWeakPointer< QWidget > m_configWidget;
  141. QList< QVariant > m_dataWidgets;
  142. };
  143. #endif // QTSCRIPTRESOLVER_H