From 81fdc368a29a2966786b4ef4d4d1d34625d6435a Mon Sep 17 00:00:00 2001 From: Dominik Schmidt Date: Mon, 1 Dec 2014 18:52:49 +0100 Subject: [PATCH] Move JSPlugin implementation to its own file --- src/libtomahawk/resolvers/JSPlugin.cpp | 41 ++++++++++++++++++++++++ src/libtomahawk/resolvers/JSResolver.cpp | 39 ---------------------- 2 files changed, 41 insertions(+), 39 deletions(-) diff --git a/src/libtomahawk/resolvers/JSPlugin.cpp b/src/libtomahawk/resolvers/JSPlugin.cpp index 4affbc10e..e13d94369 100644 --- a/src/libtomahawk/resolvers/JSPlugin.cpp +++ b/src/libtomahawk/resolvers/JSPlugin.cpp @@ -19,9 +19,11 @@ #include "JSPlugin.h" #include "../utils/Json.h" +#include "../utils/Logger.h" #include "ScriptEngine.h" #include +#include using namespace Tomahawk; @@ -59,3 +61,42 @@ JSPlugin::serializeQVariantMap( const QVariantMap& map ) return QString( "JSON.parse('%1')" ).arg( JSPlugin::escape( QString::fromUtf8( serialized ) ) ); } + + +QString +JSPlugin::JSPlugin::escape( const QString& source ) +{ + QString copy = source; + return copy.replace( "\\", "\\\\" ).replace( "'", "\\'" ); +} + + +void +JSPlugin::loadScript( const QString& path ) +{ + QFile file( path ); + + if ( !file.open( QIODevice::ReadOnly ) ) + { + qWarning() << "Failed to read contents of file:" << path << file.errorString(); + Q_ASSERT(false); + return; + } + + const QByteArray contents = file.readAll(); + + m_engine->setScriptPath( path ); + m_engine->mainFrame()->evaluateJavaScript( contents ); + + file.close(); +} + + +void +JSPlugin::loadScripts( const QStringList& paths ) +{ + foreach ( const QString& path, paths ) + { + loadScript( path ); + } +} diff --git a/src/libtomahawk/resolvers/JSResolver.cpp b/src/libtomahawk/resolvers/JSResolver.cpp index d22fc291c..6a1c4b087 100644 --- a/src/libtomahawk/resolvers/JSResolver.cpp +++ b/src/libtomahawk/resolvers/JSResolver.cpp @@ -960,37 +960,6 @@ JSResolver::resolverCollections() } -void -JSPlugin::loadScript( const QString& path ) -{ - QFile file( path ); - - if ( !file.open( QIODevice::ReadOnly ) ) - { - qWarning() << "Failed to read contents of file:" << path << file.errorString(); - Q_ASSERT(false); - return; - } - - const QByteArray contents = file.readAll(); - - m_engine->setScriptPath( path ); - m_engine->mainFrame()->evaluateJavaScript( contents ); - - file.close(); -} - - -void -JSPlugin::loadScripts( const QStringList& paths ) -{ - foreach ( const QString& path, paths ) - { - loadScript( path ); - } -} - - QVariant JSResolver::callOnResolver( const QString& scriptSource ) { @@ -1008,11 +977,3 @@ JSResolver::callOnResolver( const QString& scriptSource ) "}" ).arg( propertyName ).arg( scriptSource ) ); } - - -QString -JSPlugin::JSPlugin::escape( const QString& source ) -{ - QString copy = source; - return copy.replace( "\\", "\\\\" ).replace( "'", "\\'" ); -}