1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-01 03:40:16 +02:00

Allow script resolvers to register a url handler

This commit is contained in:
Leo Franchi
2011-09-22 22:34:09 -04:00
parent e0d63535a6
commit 624c05bdf4
2 changed files with 34 additions and 2 deletions

View File

@@ -28,6 +28,9 @@
#include <QCryptographicHash> #include <QCryptographicHash>
#include "utils/logger.h" #include "utils/logger.h"
#include <network/servent.h>
#include <QNetworkRequest>
#include <QNetworkReply>
// FIXME: bloody hack, remove this for 0.3 // FIXME: bloody hack, remove this for 0.3
// this one adds new functionality to old resolvers // this one adds new functionality to old resolvers
@@ -106,6 +109,7 @@ QtScriptResolverHelper::log( const QString& message )
void void
QtScriptResolverHelper::addTrackResults( const QVariantMap& results ) QtScriptResolverHelper::addTrackResults( const QVariantMap& results )
{ {
qDebug() << "Resolver reporting results:" << results;
QList< Tomahawk::result_ptr > tracks = m_resolver->parseResultVariantList( results.value("results").toList() ); QList< Tomahawk::result_ptr > tracks = m_resolver->parseResultVariantList( results.value("results").toList() );
QString qid = results.value("qid").toString(); QString qid = results.value("qid").toString();
@@ -152,6 +156,30 @@ QtScriptResolverHelper::md5( const QByteArray& input )
return QString::fromLatin1( digest.toHex() ); return QString::fromLatin1( digest.toHex() );
} }
void
QtScriptResolverHelper::addCustomUrlHandler( const QString& protocol, const QString& callbackFuncName )
{
boost::function<QSharedPointer<QIODevice>(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<QIODevice>( reply, &QObject::deleteLater );
}
void void
ScriptEngine::javaScriptConsoleMessage( const QString& message, int lineNumber, const QString& sourceID ) ScriptEngine::javaScriptConsoleMessage( const QString& message, int lineNumber, const QString& sourceID )
{ {

View File

@@ -51,6 +51,9 @@ public:
Q_INVOKABLE QString hmac( const QByteArray& key, const QByteArray& input ); Q_INVOKABLE QString hmac( const QByteArray& key, const QByteArray& input );
Q_INVOKABLE QString md5( const QByteArray& input ); Q_INVOKABLE QString md5( const QByteArray& input );
Q_INVOKABLE void addCustomUrlHandler( const QString& protocol, const QString& callbackFuncName );
QSharedPointer<QIODevice> customIODeviceFactory( const Tomahawk::result_ptr& result );
public slots: public slots:
QByteArray readRaw( const QString& fileName ); QByteArray readRaw( const QString& fileName );
QString readBase64( const QString& fileName ); QString readBase64( const QString& fileName );
@@ -64,8 +67,9 @@ public slots:
void addTrackResults( const QVariantMap& results ); void addTrackResults( const QVariantMap& results );
private: private:
QString m_scriptPath; QString m_scriptPath, m_urlCallback;
QVariantMap m_resolverConfig; QVariantMap m_resolverConfig;
QtScriptResolver* m_resolver; QtScriptResolver* m_resolver;
#ifdef QCA2_FOUND #ifdef QCA2_FOUND
@@ -115,7 +119,7 @@ class DLLEXPORT QtScriptResolver : public Tomahawk::ExternalResolver
{ {
Q_OBJECT Q_OBJECT
friend class QtScriptResolverHelper; friend class ::QtScriptResolverHelper;
public: public:
explicit QtScriptResolver( const QString& scriptPath ); explicit QtScriptResolver( const QString& scriptPath );