From b625b9a2658fd84b6ac1f2f45bcd84020dd12404 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Sun, 3 Apr 2011 10:23:05 +0200 Subject: [PATCH] * Unthread QtScriptResolver. WebKit is too lame for us. --- src/resolvers/qtscriptresolver.cpp | 125 ++++++----------------------- src/resolvers/qtscriptresolver.h | 43 ++-------- 2 files changed, 32 insertions(+), 136 deletions(-) diff --git a/src/resolvers/qtscriptresolver.cpp b/src/resolvers/qtscriptresolver.cpp index a19147b14..4a23439e6 100644 --- a/src/resolvers/qtscriptresolver.cpp +++ b/src/resolvers/qtscriptresolver.cpp @@ -32,81 +32,11 @@ QtScriptResolver::QtScriptResolver( const QString& scriptPath ) { qDebug() << Q_FUNC_INFO << scriptPath; - m_thread = new ScriptThread( scriptPath, this ); - connect( m_thread, SIGNAL( engineFound( QString, unsigned int, unsigned int, unsigned int ) ), - 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 ); + m_engine = new ScriptEngine( this ); + QFile scriptFile( scriptPath ); 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(); return; } @@ -115,42 +45,39 @@ ScriptThread::initEngine() m_engine->mainFrame()->evaluateJavaScript( scriptFile.readAll() ); scriptFile.close(); - QString name; - unsigned int weight, preference, timeout; QVariantMap m = m_engine->mainFrame()->evaluateJavaScript( "getSettings();" ).toMap(); - name = m.value( "name" ).toString(); - weight = m.value( "weight", 0 ).toUInt(); - timeout = m.value( "timeout", 25 ).toUInt() * 1000; - preference = m.value( "preference", 0 ).toUInt(); + m_name = m.value( "name" ).toString(); + m_weight = m.value( "weight", 0 ).toUInt(); + m_timeout = m.value( "timeout", 25 ).toUInt() * 1000; + m_preference = m.value( "preference", 0 ).toUInt(); - qDebug() << Q_FUNC_INFO << name << weight << timeout << preference; - emit engineFound( name, weight, timeout, preference ); + qDebug() << Q_FUNC_INFO << m_name << m_weight << m_timeout << m_preference; + + m_ready = true; + Tomahawk::Pipeline::instance()->addResolver( this ); +} + + +QtScriptResolver::~QtScriptResolver() +{ + Tomahawk::Pipeline::instance()->removeResolver( this ); + delete m_engine; } 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(); QString eval = QString( "resolve( '%1', '%2', '%3', '%4' );" ) - .arg( query->id().replace( "'", "\\'" ) ) - .arg( query->artist().replace( "'", "\\'" ) ) - .arg( query->album().replace( "'", "\\'" ) ) - .arg( query->track().replace( "'", "\\'" ) ); + .arg( query->id().replace( "'", "\\'" ) ) + .arg( query->artist().replace( "'", "\\'" ) ) + .arg( query->album().replace( "'", "\\'" ) ) + .arg( query->track().replace( "'", "\\'" ) ); QList< Tomahawk::result_ptr > results; - QVariantMap m = mainFrame()->evaluateJavaScript( eval ).toMap(); + QVariantMap m = m_engine->mainFrame()->evaluateJavaScript( eval ).toMap(); qDebug() << "JavaScript Result:" << m; const QString qid = query->id(); @@ -169,9 +96,9 @@ ScriptEngine::resolve( const Tomahawk::query_ptr& query ) rp->setBitrate( m.value( "bitrate" ).toUInt() ); rp->setUrl( m.value( "url" ).toString() ); 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->setFriendlySource( m_resolver->name() ); + rp->setFriendlySource( name() ); if ( m.contains( "year" ) ) { diff --git a/src/resolvers/qtscriptresolver.h b/src/resolvers/qtscriptresolver.h index 7c69fe103..a6850ac96 100644 --- a/src/resolvers/qtscriptresolver.h +++ b/src/resolvers/qtscriptresolver.h @@ -30,7 +30,6 @@ #include #include -class ScriptThread; class QtScriptResolver; class ScriptEngine : public QWebPage @@ -38,18 +37,16 @@ class ScriptEngine : public QWebPage Q_OBJECT public: - explicit ScriptEngine( QtScriptResolver* resolver, ScriptThread* parent ) - : QWebPage( (QObject*)parent ) + explicit ScriptEngine( QtScriptResolver* parent ) + : QWebPage( (QObject*) parent ) , m_parent( parent ) - , m_resolver( resolver ) - {} + { + } public slots: - void resolve( const Tomahawk::query_ptr& query ); - bool shouldInterruptJavaScript() { - return false; + return true; } protected: @@ -57,32 +54,7 @@ protected: { qDebug() << "JAVASCRIPT ERROR:" << message << lineNumber << sourceID; } 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; - QString m_scriptPath; }; @@ -106,11 +78,8 @@ public slots: signals: void finished(); -private slots: - void onEngineFound( const QString& name, unsigned int weight, unsigned int timeout, unsigned int preference ); - private: - ScriptThread* m_thread; + ScriptEngine* m_engine; QString m_name; unsigned int m_weight, m_preference, m_timeout;