mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-10 08:04:25 +02:00
Port JSResolver::resolve()
This commit is contained in:
@@ -281,27 +281,23 @@ Tomahawk.Resolver = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
_adapter_resolve: function (qid, artist, album, title) {
|
_adapter_resolve: function (params) {
|
||||||
var that = this;
|
var that = this;
|
||||||
var collectionPromises = [];
|
var collectionPromises = [];
|
||||||
Tomahawk.collections.forEach(function (col) {
|
Tomahawk.collections.forEach(function (col) {
|
||||||
if (col.resolve) {
|
if (col.resolve) {
|
||||||
collectionPromises.push(col.resolve({artist: artist, album: album, track: title}));
|
collectionPromises.push(col.resolve(params));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
RSVP.Promise.all(collectionPromises).then(function (collectionResults) {
|
|
||||||
|
return RSVP.Promise.all(collectionPromises).then(function (collectionResults) {
|
||||||
var merged = [];
|
var merged = [];
|
||||||
return merged.concat.apply(merged, collectionResults);
|
return merged.concat.apply(merged, collectionResults);
|
||||||
}).then(function (collectionResults) {
|
}).then(function (collectionResults) {
|
||||||
RSVP.Promise.resolve(that.resolve({
|
return RSVP.Promise.resolve(that.resolve(params)).then(function (results) {
|
||||||
artist: artist,
|
return {
|
||||||
album: album,
|
|
||||||
track: title
|
|
||||||
})).then(function (results) {
|
|
||||||
Tomahawk.addTrackResults({
|
|
||||||
'qid': qid,
|
|
||||||
'results': that._convertUrls(results.concat(collectionResults))
|
'results': that._convertUrls(results.concat(collectionResults))
|
||||||
});
|
};
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@@ -320,23 +316,22 @@ Tomahawk.Resolver = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
_adapter_search: function (qid, query) {
|
_adapter_search: function (params) {
|
||||||
var that = this;
|
var that = this;
|
||||||
var collectionPromises = [];
|
var collectionPromises = [];
|
||||||
Tomahawk.collections.forEach(function (col) {
|
Tomahawk.collections.forEach(function (col) {
|
||||||
if (col.search) {
|
if (col.search) {
|
||||||
collectionPromises.push(col.search({query: query}));
|
collectionPromises.push(col.search(params));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
RSVP.Promise.all(collectionPromises).then(function (collectionResults) {
|
return RSVP.Promise.all(collectionPromises).then(function (collectionResults) {
|
||||||
var merged = [];
|
var merged = [];
|
||||||
return merged.concat.apply(merged, collectionResults);
|
return merged.concat.apply(merged, collectionResults);
|
||||||
}).then(function (collectionResults) {
|
}).then(function (collectionResults) {
|
||||||
RSVP.Promise.resolve(that.search({query: query})).then(function (results) {
|
return RSVP.Promise.resolve(that.search(params)).then(function (results) {
|
||||||
Tomahawk.addTrackResults({
|
return {
|
||||||
'qid': qid,
|
|
||||||
'results': that._convertUrls(results.concat(collectionResults))
|
'results': that._convertUrls(results.concat(collectionResults))
|
||||||
});
|
};
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@@ -815,7 +810,9 @@ Tomahawk.base64Encode = function (b) {
|
|||||||
return window.btoa(b);
|
return window.btoa(b);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
Tomahawk.PluginManager = {
|
Tomahawk.PluginManager = {
|
||||||
|
wrapperPrefix: '_adapter_',
|
||||||
objects: {},
|
objects: {},
|
||||||
objectCounter: 0,
|
objectCounter: 0,
|
||||||
identifyObject: function (object) {
|
identifyObject: function (object) {
|
||||||
@@ -852,6 +849,12 @@ Tomahawk.PluginManager = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (this.objects[objectId][this.wrapperPrefix + methodName]) {
|
||||||
|
methodName = this.wrapperPrefix + methodName;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
var pluginManager = this;
|
var pluginManager = this;
|
||||||
if (!this.objects[objectId]) {
|
if (!this.objects[objectId]) {
|
||||||
Tomahawk.log("Object not found! objectId: " + objectId + " methodName: " + methodName);
|
Tomahawk.log("Object not found! objectId: " + objectId + " methodName: " + methodName);
|
||||||
|
@@ -44,6 +44,7 @@
|
|||||||
#include "Track.h"
|
#include "Track.h"
|
||||||
#include "ScriptInfoPlugin.h"
|
#include "ScriptInfoPlugin.h"
|
||||||
#include "JSAccount.h"
|
#include "JSAccount.h"
|
||||||
|
#include "ScriptJob.h"
|
||||||
|
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
@@ -250,8 +251,7 @@ JSResolver::init()
|
|||||||
d->scriptAccount->loadScript( filePath() );
|
d->scriptAccount->loadScript( filePath() );
|
||||||
|
|
||||||
// HACK: register resolver object
|
// HACK: register resolver object
|
||||||
d->scriptAccount->evaluateJavaScript( "Tomahawk.PluginManager.registerPlugin('resolver', Tomahawk.resolver.instance);" )
|
d->scriptAccount->evaluateJavaScript( "Tomahawk.PluginManager.registerPlugin('resolver', Tomahawk.resolver.instance);" );
|
||||||
;
|
|
||||||
// init resolver
|
// init resolver
|
||||||
resolverInit();
|
resolverInit();
|
||||||
|
|
||||||
@@ -394,23 +394,28 @@ JSResolver::resolve( const Tomahawk::query_ptr& query )
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString eval;
|
ScriptJob* job = nullptr;
|
||||||
if ( !query->isFullTextQuery() )
|
if ( !query->isFullTextQuery() )
|
||||||
{
|
{
|
||||||
eval = QString( "resolve( '%1', '%2', '%3', '%4' )" )
|
QVariantMap arguments;
|
||||||
.arg( JSAccount::escape( query->id() ) )
|
arguments["artist"] = query->queryTrack()->artist();
|
||||||
.arg( JSAccount::escape( query->queryTrack()->artist() ) )
|
arguments["album"] = query->queryTrack()->album();
|
||||||
.arg( JSAccount::escape( query->queryTrack()->album() ) )
|
arguments["track"] = query->queryTrack()->track();
|
||||||
.arg( JSAccount::escape( query->queryTrack()->track() ) );
|
|
||||||
|
job = scriptObject()->invoke( "resolve", arguments );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
eval = QString( "search( '%1', '%2' )" )
|
QVariantMap arguments;
|
||||||
.arg( JSAccount::escape( query->id() ) )
|
arguments["query"] = query->fullTextQuery();
|
||||||
.arg( JSAccount::escape( query->fullTextQuery() ) );
|
job = scriptObject()->invoke( "search", arguments );
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariantMap m = callOnResolver( eval ).toMap();
|
|
||||||
|
job->setProperty( "qid", query->id() );
|
||||||
|
connect( job, SIGNAL( done( QVariantMap ) ), SLOT( onResolveRequestDone( QVariantMap ) ) );
|
||||||
|
|
||||||
|
job->start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -552,3 +557,32 @@ JSResolver::callOnResolver( const QString& scriptSource )
|
|||||||
"}"
|
"}"
|
||||||
).arg( propertyName ).arg( scriptSource ) );
|
).arg( propertyName ).arg( scriptSource ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
JSResolver::onResolveRequestDone( const QVariantMap& data )
|
||||||
|
{
|
||||||
|
Q_ASSERT( QThread::currentThread() == thread() );
|
||||||
|
Q_D( JSResolver );
|
||||||
|
|
||||||
|
ScriptJob* job = qobject_cast< ScriptJob* >( sender() );
|
||||||
|
if ( job->error() )
|
||||||
|
{
|
||||||
|
// what do here?!
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
QList< Tomahawk::result_ptr > results = scriptAccount()->parseResultVariantList( data.value( "results" ).toList() );
|
||||||
|
|
||||||
|
foreach( const result_ptr& result, results )
|
||||||
|
{
|
||||||
|
result->setResolvedByResolver( this );
|
||||||
|
result->setFriendlySource( name() );
|
||||||
|
}
|
||||||
|
|
||||||
|
Tomahawk::Pipeline::instance()->reportResults( job->property( "qid" ).toString(), this, results );
|
||||||
|
}
|
||||||
|
|
||||||
|
sender()->deleteLater();
|
||||||
|
}
|
||||||
|
@@ -89,6 +89,9 @@ signals:
|
|||||||
protected:
|
protected:
|
||||||
QVariant callOnResolver( const QString& scriptSource );
|
QVariant callOnResolver( const QString& scriptSource );
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void onResolveRequestDone(const QVariantMap& data);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
|
@@ -140,25 +140,6 @@ JSResolverHelper::log( const QString& message )
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
JSResolverHelper::addTrackResults( const QVariantMap& results )
|
|
||||||
{
|
|
||||||
Q_ASSERT( results["results"].toMap().isEmpty() );
|
|
||||||
|
|
||||||
QList< Tomahawk::result_ptr > tracks = m_resolver->scriptAccount()->parseResultVariantList( results.value( "results" ).toList() );
|
|
||||||
|
|
||||||
foreach( const result_ptr& track, tracks )
|
|
||||||
{
|
|
||||||
track->setResolvedByResolver( m_resolver );
|
|
||||||
track->setFriendlySource( m_resolver->name() );
|
|
||||||
}
|
|
||||||
|
|
||||||
QString qid = results.value("qid").toString();
|
|
||||||
|
|
||||||
Tomahawk::Pipeline::instance()->reportResults( qid, m_resolver, tracks );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
query_ptr
|
query_ptr
|
||||||
JSResolverHelper::parseTrack( const QVariantMap& track )
|
JSResolverHelper::parseTrack( const QVariantMap& track )
|
||||||
{
|
{
|
||||||
|
@@ -142,8 +142,6 @@ public slots:
|
|||||||
void log( const QString& message );
|
void log( const QString& message );
|
||||||
bool fakeEnv() { return false; }
|
bool fakeEnv() { return false; }
|
||||||
|
|
||||||
void addTrackResults( const QVariantMap& results );
|
|
||||||
|
|
||||||
void addUrlResult( const QString& url, const QVariantMap& result );
|
void addUrlResult( const QString& url, const QVariantMap& result );
|
||||||
|
|
||||||
void nativeReportCapabilities( const QVariant& capabilities );
|
void nativeReportCapabilities( const QVariant& capabilities );
|
||||||
|
Reference in New Issue
Block a user