mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-07-31 03:10:12 +02:00
More API cleanup for QtScriptResolver
This commit is contained in:
@@ -4,6 +4,10 @@ if(window.Tomahawk === undefined)
|
|||||||
{
|
{
|
||||||
alert("PHANTOMJS ENVIRONMENT");
|
alert("PHANTOMJS ENVIRONMENT");
|
||||||
var Tomahawk = {
|
var Tomahawk = {
|
||||||
|
fakeEnv: function()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
},
|
||||||
resolverData: function()
|
resolverData: function()
|
||||||
{
|
{
|
||||||
return {
|
return {
|
||||||
@@ -12,14 +16,34 @@ if(window.Tomahawk === undefined)
|
|||||||
return "/home/tomahawk/resolver.js";
|
return "/home/tomahawk/resolver.js";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
},
|
||||||
|
log: function( message )
|
||||||
|
{
|
||||||
|
console.log( message );
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Tomahawk.resolver = {
|
Tomahawk.resolver = {
|
||||||
scriptPath: Tomahawk.resolverData().scriptPath
|
scriptPath: Tomahawk.resolverData().scriptPath
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Tomahawk.timestamp = function() {
|
||||||
|
return Math.round( new Date()/1000 );
|
||||||
|
}
|
||||||
|
|
||||||
|
Tomahawk.dumpResult = function( result ) {
|
||||||
|
var results = result.results;
|
||||||
|
Tomahawk.log("Dumping " + results.length + " results for query " + result.qid + "...");
|
||||||
|
for(var i=0; i<results.length;i++)
|
||||||
|
{
|
||||||
|
var result = results[i];
|
||||||
|
Tomahawk.log( result.artist + " - " + result.track + " | " + result.url );
|
||||||
|
}
|
||||||
|
|
||||||
|
Tomahawk.log("Done.");
|
||||||
|
}
|
||||||
|
|
||||||
// javascript part of Tomahawk-Object API
|
// javascript part of Tomahawk-Object API
|
||||||
Tomahawk.extend = function(object, members) {
|
Tomahawk.extend = function(object, members) {
|
||||||
@@ -38,6 +62,9 @@ Tomahawk.extend = function(object, members) {
|
|||||||
|
|
||||||
// Resolver BaseObject, inherit it to implement your own resolver
|
// Resolver BaseObject, inherit it to implement your own resolver
|
||||||
var TomahawkResolver = {
|
var TomahawkResolver = {
|
||||||
|
init: function()
|
||||||
|
{
|
||||||
|
},
|
||||||
scriptPath: function()
|
scriptPath: function()
|
||||||
{
|
{
|
||||||
return Tomahawk.resolverData().scriptPath;
|
return Tomahawk.resolverData().scriptPath;
|
||||||
@@ -129,7 +156,11 @@ Tomahawk.valueForSubNode = function(node, tag)
|
|||||||
if(node === undefined)
|
if(node === undefined)
|
||||||
throw new Error("Tomahawk.valueForSubnode: node is undefined!");
|
throw new Error("Tomahawk.valueForSubnode: node is undefined!");
|
||||||
|
|
||||||
return node.getElementsByTagName(tag)[0].textContent;
|
var element = node.getElementsByTagName(tag)[0];
|
||||||
|
if( element === undefined )
|
||||||
|
return undefined;
|
||||||
|
|
||||||
|
return element.textContent;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -267,4 +298,4 @@ Tomahawk.sha256=function(s){
|
|||||||
s = Utf8Encode(s);
|
s = Utf8Encode(s);
|
||||||
return binb2hex(core_sha256(str2binb(s), s.length * chrsz));
|
return binb2hex(core_sha256(str2binb(s), s.length * chrsz));
|
||||||
|
|
||||||
}
|
}
|
@@ -86,6 +86,12 @@ QtScriptResolverHelper::resolverData()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void QtScriptResolverHelper::log(const QString& message)
|
||||||
|
{
|
||||||
|
qDebug() << m_scriptPath << ":" << message;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
QtScriptResolverHelper::setResolverConfig( QVariantMap config )
|
QtScriptResolverHelper::setResolverConfig( QVariantMap config )
|
||||||
{
|
{
|
||||||
@@ -122,11 +128,14 @@ QtScriptResolver::QtScriptResolver( const QString& scriptPath )
|
|||||||
m_engine->mainFrame()->evaluateJavaScript( jslib.readAll() );
|
m_engine->mainFrame()->evaluateJavaScript( jslib.readAll() );
|
||||||
jslib.close();
|
jslib.close();
|
||||||
|
|
||||||
// execute resolver
|
// add resolver
|
||||||
m_engine->setScriptPath( scriptPath );
|
m_engine->setScriptPath( scriptPath );
|
||||||
m_engine->mainFrame()->evaluateJavaScript( scriptFile.readAll() );
|
m_engine->mainFrame()->evaluateJavaScript( scriptFile.readAll() );
|
||||||
scriptFile.close();
|
scriptFile.close();
|
||||||
|
|
||||||
|
// init resolver
|
||||||
|
resolverInit();
|
||||||
|
|
||||||
|
|
||||||
QVariantMap m = resolverSettings();
|
QVariantMap m = resolverSettings();
|
||||||
m_name = m.value( "name" ).toString();
|
m_name = m.value( "name" ).toString();
|
||||||
@@ -167,7 +176,7 @@ QtScriptResolver::resolve( const Tomahawk::query_ptr& query )
|
|||||||
|
|
||||||
if ( !query->isFullTextQuery() )
|
if ( !query->isFullTextQuery() )
|
||||||
{
|
{
|
||||||
eval = QString( "Tomahawk.resolver.instance.resolve( '%1', '%2', '%3', '%4' );" )
|
eval = QString( "Tomahawk.resolver.instance.resolve( '%1', '%4', '%3', '%4' );" )
|
||||||
.arg( query->id().replace( "'", "\\'" ) )
|
.arg( query->id().replace( "'", "\\'" ) )
|
||||||
.arg( query->artist().replace( "'", "\\'" ) )
|
.arg( query->artist().replace( "'", "\\'" ) )
|
||||||
.arg( query->album().replace( "'", "\\'" ) )
|
.arg( query->album().replace( "'", "\\'" ) )
|
||||||
@@ -175,11 +184,9 @@ QtScriptResolver::resolve( const Tomahawk::query_ptr& query )
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
eval = QString( "Tomahawk.resolver.instance.resolve( '%1', '%2', '%3', '%4' );" )
|
eval = QString( "Tomahawk.resolver.instance.search( '%1', '%2' );" )
|
||||||
.arg( query->id().replace( "'", "\\'" ) )
|
.arg( query->id().replace( "'", "\\'" ) )
|
||||||
.arg( query->fullTextQuery().replace( "'", "\\'" ) )
|
.arg( query->fullTextQuery().replace( "'", "\\'" ) );
|
||||||
.arg( QString() )
|
|
||||||
.arg( QString() );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QList< Tomahawk::result_ptr > results;
|
QList< Tomahawk::result_ptr > results;
|
||||||
@@ -415,7 +422,7 @@ QtScriptResolver::fillDataInWidgets( const QVariantMap& data )
|
|||||||
QVariantMap
|
QVariantMap
|
||||||
QtScriptResolver::resolverSettings()
|
QtScriptResolver::resolverSettings()
|
||||||
{
|
{
|
||||||
return m_engine->mainFrame()->evaluateJavaScript( "Tomahawk.resolver.instance.getSettings();" ).toMap();
|
return m_engine->mainFrame()->evaluateJavaScript( "Tomahawk.resolver.instance.settings;" ).toMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -424,3 +431,11 @@ QtScriptResolver::resolverUserConfig()
|
|||||||
{
|
{
|
||||||
return m_engine->mainFrame()->evaluateJavaScript( "Tomahawk.resolver.instance.getUserConfig();" ).toMap();
|
return m_engine->mainFrame()->evaluateJavaScript( "Tomahawk.resolver.instance.getUserConfig();" ).toMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
QVariantMap
|
||||||
|
QtScriptResolver::resolverInit()
|
||||||
|
{
|
||||||
|
return m_engine->mainFrame()->evaluateJavaScript( "Tomahawk.resolver.instance.init();" ).toMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
@@ -49,6 +49,9 @@ public slots:
|
|||||||
QString compress( const QString& data );
|
QString compress( const QString& data );
|
||||||
QVariantMap resolverData();
|
QVariantMap resolverData();
|
||||||
|
|
||||||
|
void log( const QString& message);
|
||||||
|
bool fakeEnv() { return false; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString m_scriptPath;
|
QString m_scriptPath;
|
||||||
QVariantMap m_resolverConfig;
|
QVariantMap m_resolverConfig;
|
||||||
@@ -124,6 +127,7 @@ private:
|
|||||||
// encapsulate javascript calls
|
// encapsulate javascript calls
|
||||||
QVariantMap resolverSettings();
|
QVariantMap resolverSettings();
|
||||||
QVariantMap resolverUserConfig();
|
QVariantMap resolverUserConfig();
|
||||||
|
QVariantMap resolverInit();
|
||||||
|
|
||||||
ScriptEngine* m_engine;
|
ScriptEngine* m_engine;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user