diff --git a/src/libtomahawk/resolvers/qtscriptresolver.cpp b/src/libtomahawk/resolvers/qtscriptresolver.cpp index 607662e60..6b3f4be07 100644 --- a/src/libtomahawk/resolvers/qtscriptresolver.cpp +++ b/src/libtomahawk/resolvers/qtscriptresolver.cpp @@ -28,6 +28,9 @@ #include #include "utils/logger.h" +#include +#include +#include // FIXME: bloody hack, remove this for 0.3 // this one adds new functionality to old resolvers @@ -106,6 +109,7 @@ QtScriptResolverHelper::log( const QString& message ) void QtScriptResolverHelper::addTrackResults( const QVariantMap& results ) { + qDebug() << "Resolver reporting results:" << results; QList< Tomahawk::result_ptr > tracks = m_resolver->parseResultVariantList( results.value("results").toList() ); QString qid = results.value("qid").toString(); @@ -152,6 +156,30 @@ QtScriptResolverHelper::md5( const QByteArray& input ) return QString::fromLatin1( digest.toHex() ); } +void +QtScriptResolverHelper::addCustomUrlHandler( const QString& protocol, const QString& callbackFuncName ) +{ + boost::function(Tomahawk::result_ptr)> fac = boost::bind( &QtScriptResolverHelper::customIODeviceFactory, this, _1 ); + Servent::instance()->registerIODeviceFactory( protocol, fac ); + + m_urlCallback = callbackFuncName; +} + +QSharedPointer< QIODevice > +QtScriptResolverHelper::customIODeviceFactory( const Tomahawk::result_ptr& result ) +{ + QString getUrl = QString( "Tomahawk.resolver.instance.%1( '%2' );" ).arg( m_urlCallback ) + .arg( QString( QUrl( result->url() ).toEncoded() ) ); + + QString urlStr = m_resolver->m_engine->mainFrame()->evaluateJavaScript( getUrl ).toString(); + QUrl url = QUrl::fromEncoded( urlStr.toUtf8() ); + QNetworkRequest req( url ); + tDebug() << "Creating a QNetowrkReply with url:" << req.url().toString(); + QNetworkReply* reply = TomahawkUtils::nam()->get( req ); + return QSharedPointer( reply, &QObject::deleteLater ); +} + + void ScriptEngine::javaScriptConsoleMessage( const QString& message, int lineNumber, const QString& sourceID ) { diff --git a/src/libtomahawk/resolvers/qtscriptresolver.h b/src/libtomahawk/resolvers/qtscriptresolver.h index e281ad9ac..673b648ee 100644 --- a/src/libtomahawk/resolvers/qtscriptresolver.h +++ b/src/libtomahawk/resolvers/qtscriptresolver.h @@ -51,6 +51,9 @@ public: Q_INVOKABLE QString hmac( const QByteArray& key, const QByteArray& input ); Q_INVOKABLE QString md5( const QByteArray& input ); + Q_INVOKABLE void addCustomUrlHandler( const QString& protocol, const QString& callbackFuncName ); + + QSharedPointer customIODeviceFactory( const Tomahawk::result_ptr& result ); public slots: QByteArray readRaw( const QString& fileName ); QString readBase64( const QString& fileName ); @@ -64,8 +67,9 @@ public slots: void addTrackResults( const QVariantMap& results ); + private: - QString m_scriptPath; + QString m_scriptPath, m_urlCallback; QVariantMap m_resolverConfig; QtScriptResolver* m_resolver; #ifdef QCA2_FOUND @@ -115,7 +119,7 @@ class DLLEXPORT QtScriptResolver : public Tomahawk::ExternalResolver { Q_OBJECT -friend class QtScriptResolverHelper; +friend class ::QtScriptResolverHelper; public: explicit QtScriptResolver( const QString& scriptPath );