PageRenderTime 143ms CodeModel.GetById 70ms app.highlight 14ms RepoModel.GetById 57ms app.codeStats 0ms

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