mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-05 13:47:26 +02:00
* Unthread QtScriptResolver. WebKit is too lame for us.
This commit is contained in:
@@ -32,81 +32,11 @@ QtScriptResolver::QtScriptResolver( const QString& scriptPath )
|
|||||||
{
|
{
|
||||||
qDebug() << Q_FUNC_INFO << scriptPath;
|
qDebug() << Q_FUNC_INFO << scriptPath;
|
||||||
|
|
||||||
m_thread = new ScriptThread( scriptPath, this );
|
m_engine = new ScriptEngine( this );
|
||||||
connect( m_thread, SIGNAL( engineFound( QString, unsigned int, unsigned int, unsigned int ) ),
|
QFile scriptFile( scriptPath );
|
||||||
SLOT( onEngineFound( QString, unsigned int, unsigned int, unsigned int ) ) );
|
|
||||||
|
|
||||||
m_thread->start();
|
|
||||||
|
|
||||||
connect( this, SIGNAL( destroyed( QObject* ) ), m_thread, SLOT( deleteLater() ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
QtScriptResolver::~QtScriptResolver()
|
|
||||||
{
|
|
||||||
Tomahawk::Pipeline::instance()->removeResolver( this );
|
|
||||||
delete m_thread;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
QtScriptResolver::resolve( const Tomahawk::query_ptr& query )
|
|
||||||
{
|
|
||||||
m_thread->resolve( query );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
QtScriptResolver::onEngineFound( const QString& name, unsigned int weight, unsigned int timeout, unsigned int preference )
|
|
||||||
{
|
|
||||||
m_name = name;
|
|
||||||
m_weight = weight;
|
|
||||||
m_timeout = timeout;
|
|
||||||
m_preference = preference;
|
|
||||||
|
|
||||||
qDebug() << "QTSCRIPT" << filePath() << "READY," << endl
|
|
||||||
<< "name" << m_name << endl
|
|
||||||
<< "weight" << m_weight << endl
|
|
||||||
<< "timeout" << m_timeout << endl
|
|
||||||
<< "preference" << m_preference;
|
|
||||||
|
|
||||||
m_ready = true;
|
|
||||||
Tomahawk::Pipeline::instance()->addResolver( this );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ScriptThread::ScriptThread( const QString& scriptPath, QtScriptResolver* parent )
|
|
||||||
: QThread()
|
|
||||||
, m_parent( parent )
|
|
||||||
, m_scriptPath( scriptPath )
|
|
||||||
{
|
|
||||||
moveToThread( this );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
ScriptThread::resolve( const Tomahawk::query_ptr& query )
|
|
||||||
{
|
|
||||||
m_engine->resolve( query );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
ScriptThread::run()
|
|
||||||
{
|
|
||||||
QTimer::singleShot( 0, this, SLOT( initEngine() ) );
|
|
||||||
exec();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
ScriptThread::initEngine()
|
|
||||||
{
|
|
||||||
m_engine = new ScriptEngine( m_parent, this );
|
|
||||||
QFile scriptFile( m_scriptPath );
|
|
||||||
if ( !scriptFile.open( QIODevice::ReadOnly ) )
|
if ( !scriptFile.open( QIODevice::ReadOnly ) )
|
||||||
{
|
{
|
||||||
qDebug() << Q_FUNC_INFO << "Failed loading JavaScript resolver:" << m_scriptPath;
|
qDebug() << Q_FUNC_INFO << "Failed loading JavaScript resolver:" << scriptPath;
|
||||||
deleteLater();
|
deleteLater();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -115,32 +45,29 @@ ScriptThread::initEngine()
|
|||||||
m_engine->mainFrame()->evaluateJavaScript( scriptFile.readAll() );
|
m_engine->mainFrame()->evaluateJavaScript( scriptFile.readAll() );
|
||||||
scriptFile.close();
|
scriptFile.close();
|
||||||
|
|
||||||
QString name;
|
|
||||||
unsigned int weight, preference, timeout;
|
|
||||||
QVariantMap m = m_engine->mainFrame()->evaluateJavaScript( "getSettings();" ).toMap();
|
QVariantMap m = m_engine->mainFrame()->evaluateJavaScript( "getSettings();" ).toMap();
|
||||||
name = m.value( "name" ).toString();
|
m_name = m.value( "name" ).toString();
|
||||||
weight = m.value( "weight", 0 ).toUInt();
|
m_weight = m.value( "weight", 0 ).toUInt();
|
||||||
timeout = m.value( "timeout", 25 ).toUInt() * 1000;
|
m_timeout = m.value( "timeout", 25 ).toUInt() * 1000;
|
||||||
preference = m.value( "preference", 0 ).toUInt();
|
m_preference = m.value( "preference", 0 ).toUInt();
|
||||||
|
|
||||||
qDebug() << Q_FUNC_INFO << name << weight << timeout << preference;
|
qDebug() << Q_FUNC_INFO << m_name << m_weight << m_timeout << m_preference;
|
||||||
emit engineFound( name, weight, timeout, preference );
|
|
||||||
|
m_ready = true;
|
||||||
|
Tomahawk::Pipeline::instance()->addResolver( this );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
QtScriptResolver::~QtScriptResolver()
|
||||||
|
{
|
||||||
|
Tomahawk::Pipeline::instance()->removeResolver( this );
|
||||||
|
delete m_engine;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
ScriptEngine::resolve( const Tomahawk::query_ptr& query )
|
QtScriptResolver::resolve( const Tomahawk::query_ptr& query )
|
||||||
{
|
{
|
||||||
if ( QThread::currentThread() != thread() )
|
|
||||||
{
|
|
||||||
// qDebug() << "Reinvoking in correct thread:" << Q_FUNC_INFO;
|
|
||||||
QMetaObject::invokeMethod( this, "resolve",
|
|
||||||
Qt::QueuedConnection,
|
|
||||||
Q_ARG(Tomahawk::query_ptr, query)
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
qDebug() << Q_FUNC_INFO << query->toString();
|
qDebug() << Q_FUNC_INFO << query->toString();
|
||||||
QString eval = QString( "resolve( '%1', '%2', '%3', '%4' );" )
|
QString eval = QString( "resolve( '%1', '%2', '%3', '%4' );" )
|
||||||
.arg( query->id().replace( "'", "\\'" ) )
|
.arg( query->id().replace( "'", "\\'" ) )
|
||||||
@@ -150,7 +77,7 @@ ScriptEngine::resolve( const Tomahawk::query_ptr& query )
|
|||||||
|
|
||||||
QList< Tomahawk::result_ptr > results;
|
QList< Tomahawk::result_ptr > results;
|
||||||
|
|
||||||
QVariantMap m = mainFrame()->evaluateJavaScript( eval ).toMap();
|
QVariantMap m = m_engine->mainFrame()->evaluateJavaScript( eval ).toMap();
|
||||||
qDebug() << "JavaScript Result:" << m;
|
qDebug() << "JavaScript Result:" << m;
|
||||||
|
|
||||||
const QString qid = query->id();
|
const QString qid = query->id();
|
||||||
@@ -169,9 +96,9 @@ ScriptEngine::resolve( const Tomahawk::query_ptr& query )
|
|||||||
rp->setBitrate( m.value( "bitrate" ).toUInt() );
|
rp->setBitrate( m.value( "bitrate" ).toUInt() );
|
||||||
rp->setUrl( m.value( "url" ).toString() );
|
rp->setUrl( m.value( "url" ).toString() );
|
||||||
rp->setSize( m.value( "size" ).toUInt() );
|
rp->setSize( m.value( "size" ).toUInt() );
|
||||||
rp->setScore( m.value( "score" ).toFloat() * ( (float)m_resolver->weight() / 100.0 ) );
|
rp->setScore( m.value( "score" ).toFloat() * ( (float)weight() / 100.0 ) );
|
||||||
rp->setRID( uuid() );
|
rp->setRID( uuid() );
|
||||||
rp->setFriendlySource( m_resolver->name() );
|
rp->setFriendlySource( name() );
|
||||||
|
|
||||||
if ( m.contains( "year" ) )
|
if ( m.contains( "year" ) )
|
||||||
{
|
{
|
||||||
|
@@ -30,7 +30,6 @@
|
|||||||
#include <QtWebKit/QWebPage>
|
#include <QtWebKit/QWebPage>
|
||||||
#include <QtWebKit/QWebFrame>
|
#include <QtWebKit/QWebFrame>
|
||||||
|
|
||||||
class ScriptThread;
|
|
||||||
class QtScriptResolver;
|
class QtScriptResolver;
|
||||||
|
|
||||||
class ScriptEngine : public QWebPage
|
class ScriptEngine : public QWebPage
|
||||||
@@ -38,18 +37,16 @@ class ScriptEngine : public QWebPage
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit ScriptEngine( QtScriptResolver* resolver, ScriptThread* parent )
|
explicit ScriptEngine( QtScriptResolver* parent )
|
||||||
: QWebPage( (QObject*) parent )
|
: QWebPage( (QObject*) parent )
|
||||||
, m_parent( parent )
|
, m_parent( parent )
|
||||||
, m_resolver( resolver )
|
{
|
||||||
{}
|
}
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void resolve( const Tomahawk::query_ptr& query );
|
|
||||||
|
|
||||||
bool shouldInterruptJavaScript()
|
bool shouldInterruptJavaScript()
|
||||||
{
|
{
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
@@ -57,32 +54,7 @@ protected:
|
|||||||
{ qDebug() << "JAVASCRIPT ERROR:" << message << lineNumber << sourceID; }
|
{ qDebug() << "JAVASCRIPT ERROR:" << message << lineNumber << sourceID; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ScriptThread* m_parent;
|
|
||||||
QtScriptResolver* m_resolver;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
class ScriptThread : public QThread
|
|
||||||
{
|
|
||||||
Q_OBJECT
|
|
||||||
|
|
||||||
public:
|
|
||||||
ScriptThread( const QString& scriptPath, QtScriptResolver* parent );
|
|
||||||
|
|
||||||
void run();
|
|
||||||
|
|
||||||
virtual void resolve( const Tomahawk::query_ptr& query );
|
|
||||||
|
|
||||||
signals:
|
|
||||||
void engineFound( const QString& name, unsigned int weight, unsigned int timeout, unsigned int preference );
|
|
||||||
|
|
||||||
private slots:
|
|
||||||
void initEngine();
|
|
||||||
|
|
||||||
private:
|
|
||||||
ScriptEngine* m_engine;
|
|
||||||
QtScriptResolver* m_parent;
|
QtScriptResolver* m_parent;
|
||||||
QString m_scriptPath;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -106,11 +78,8 @@ public slots:
|
|||||||
signals:
|
signals:
|
||||||
void finished();
|
void finished();
|
||||||
|
|
||||||
private slots:
|
|
||||||
void onEngineFound( const QString& name, unsigned int weight, unsigned int timeout, unsigned int preference );
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ScriptThread* m_thread;
|
ScriptEngine* m_engine;
|
||||||
|
|
||||||
QString m_name;
|
QString m_name;
|
||||||
unsigned int m_weight, m_preference, m_timeout;
|
unsigned int m_weight, m_preference, m_timeout;
|
||||||
|
Reference in New Issue
Block a user