mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-06 14:16:32 +02:00
* Threaded QtScriptResolver so it doesn't block the GUI thread anymore.
This commit is contained in:
@@ -10,11 +10,15 @@
|
|||||||
QtScriptResolver::QtScriptResolver( const QString& scriptPath )
|
QtScriptResolver::QtScriptResolver( const QString& scriptPath )
|
||||||
: Tomahawk::ExternalResolver( scriptPath )
|
: Tomahawk::ExternalResolver( scriptPath )
|
||||||
, m_engine( new ScriptEngine( this ) )
|
, m_engine( new ScriptEngine( this ) )
|
||||||
|
, m_thread( new QThread( this ) )
|
||||||
, m_ready( false )
|
, m_ready( false )
|
||||||
, m_stopped( false )
|
, m_stopped( false )
|
||||||
{
|
{
|
||||||
qDebug() << Q_FUNC_INFO << scriptPath;
|
qDebug() << Q_FUNC_INFO << scriptPath;
|
||||||
|
|
||||||
|
m_engine->moveToThread( m_thread );
|
||||||
|
m_thread->start();
|
||||||
|
|
||||||
QFile scriptFile( scriptPath );
|
QFile scriptFile( scriptPath );
|
||||||
scriptFile.open( QIODevice::ReadOnly );
|
scriptFile.open( QIODevice::ReadOnly );
|
||||||
m_engine->mainFrame()->setHtml( "<html><body></body></html>" );
|
m_engine->mainFrame()->setHtml( "<html><body></body></html>" );
|
||||||
@@ -47,13 +51,13 @@ QtScriptResolver::~QtScriptResolver()
|
|||||||
void
|
void
|
||||||
QtScriptResolver::resolve( const Tomahawk::query_ptr& query )
|
QtScriptResolver::resolve( const Tomahawk::query_ptr& query )
|
||||||
{
|
{
|
||||||
if ( QThread::currentThread() != thread() )
|
QMetaObject::invokeMethod( m_engine, "resolve", Qt::QueuedConnection, Q_ARG( Tomahawk::query_ptr, query ) );
|
||||||
{
|
}
|
||||||
qDebug() << "Reinvoking in correct thread:" << Q_FUNC_INFO;
|
|
||||||
QMetaObject::invokeMethod( this, "resolve", Qt::QueuedConnection, Q_ARG( Tomahawk::query_ptr, query ) );
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
ScriptEngine::resolve( const Tomahawk::query_ptr& query )
|
||||||
|
{
|
||||||
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( "'", "\\'" ) )
|
||||||
@@ -63,7 +67,7 @@ QtScriptResolver::resolve( const Tomahawk::query_ptr& query )
|
|||||||
|
|
||||||
QList< Tomahawk::result_ptr > results;
|
QList< Tomahawk::result_ptr > results;
|
||||||
|
|
||||||
QVariantMap m = m_engine->mainFrame()->evaluateJavaScript( eval ).toMap();
|
QVariantMap m = mainFrame()->evaluateJavaScript( eval ).toMap();
|
||||||
qDebug() << "JavaScript Result:" << m;
|
qDebug() << "JavaScript Result:" << m;
|
||||||
|
|
||||||
const QString qid = m.value( "qid" ).toString();
|
const QString qid = m.value( "qid" ).toString();
|
||||||
@@ -83,9 +87,9 @@ QtScriptResolver::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)weight() / 100.0 ) );
|
rp->setScore( m.value( "score" ).toFloat() * ( (float)m_parent->weight() / 100.0 ) );
|
||||||
rp->setRID( uuid() );
|
rp->setRID( uuid() );
|
||||||
rp->setFriendlySource( m_name );
|
rp->setFriendlySource( m_parent->name() );
|
||||||
|
|
||||||
rp->setMimetype( m.value( "mimetype" ).toString() );
|
rp->setMimetype( m.value( "mimetype" ).toString() );
|
||||||
if ( rp->mimetype().isEmpty() )
|
if ( rp->mimetype().isEmpty() )
|
||||||
|
@@ -11,25 +11,32 @@
|
|||||||
#include <QtWebKit/QWebPage>
|
#include <QtWebKit/QWebPage>
|
||||||
#include <QtWebKit/QWebFrame>
|
#include <QtWebKit/QWebFrame>
|
||||||
|
|
||||||
|
class QtScriptResolver;
|
||||||
|
|
||||||
class ScriptEngine : public QWebPage
|
class ScriptEngine : public QWebPage
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit ScriptEngine( QObject* parent )
|
explicit ScriptEngine( QtScriptResolver* parent )
|
||||||
: QWebPage( parent )
|
: QWebPage( (QObject*)parent )
|
||||||
|
, m_parent( parent )
|
||||||
{}
|
{}
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
|
void resolve( const Tomahawk::query_ptr& query );
|
||||||
|
|
||||||
bool shouldInterruptJavaScript()
|
bool shouldInterruptJavaScript()
|
||||||
{
|
{
|
||||||
QApplication::processEvents( QEventLoop::AllEvents, 42 );
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void javaScriptConsoleMessage( const QString & message, int lineNumber, const QString & sourceID )
|
virtual void javaScriptConsoleMessage( const QString & message, int lineNumber, const QString & sourceID )
|
||||||
{ qDebug() << "JAVASCRIPT ERROR:" << message << lineNumber << sourceID; }
|
{ qDebug() << "JAVASCRIPT ERROR:" << message << lineNumber << sourceID; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
QtScriptResolver* m_parent;
|
||||||
};
|
};
|
||||||
|
|
||||||
class QtScriptResolver : public Tomahawk::ExternalResolver
|
class QtScriptResolver : public Tomahawk::ExternalResolver
|
||||||
@@ -53,6 +60,7 @@ private slots:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
ScriptEngine* m_engine;
|
ScriptEngine* m_engine;
|
||||||
|
QThread* m_thread;
|
||||||
|
|
||||||
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