diff --git a/src/TomahawkApp.cpp b/src/TomahawkApp.cpp index cc0a7d5f5..252108f02 100644 --- a/src/TomahawkApp.cpp +++ b/src/TomahawkApp.cpp @@ -201,8 +201,8 @@ TomahawkApp::init() m_scanManager = QPointer( new ScanManager( this ) ); #ifndef ENABLE_HEADLESS - Pipeline::instance()->addExternalResolverFactory( boost::bind( &QtScriptResolver::factory, _1 ) ); - Pipeline::instance()->addExternalResolverFactory( boost::bind( &ScriptResolver::factory, _1 ) ); + Pipeline::instance()->addExternalResolverFactory( boost::bind( &QtScriptResolver::factory, _1, _2 ) ); + Pipeline::instance()->addExternalResolverFactory( boost::bind( &ScriptResolver::factory, _1, _2 ) ); new ActionCollection( this ); connect( ActionCollection::instance()->getAction( "quit" ), SIGNAL( triggered() ), SLOT( quit() ), Qt::UniqueConnection ); diff --git a/src/libtomahawk/Pipeline.cpp b/src/libtomahawk/Pipeline.cpp index e4e105d34..44b93812c 100644 --- a/src/libtomahawk/Pipeline.cpp +++ b/src/libtomahawk/Pipeline.cpp @@ -137,13 +137,13 @@ Pipeline::addExternalResolverFactory( ResolverFactoryFunc resolverFactory ) Tomahawk::ExternalResolver* -Pipeline::addScriptResolver( const QString& path ) +Pipeline::addScriptResolver( const QString& path, const QStringList& additionalScriptPaths ) { ExternalResolver* res = 0; foreach ( ResolverFactoryFunc factory, m_resolverFactories ) { - res = factory( path ); + res = factory( path, additionalScriptPaths ); if ( !res ) continue; diff --git a/src/libtomahawk/Pipeline.h b/src/libtomahawk/Pipeline.h index a1dcb7258..e08f9412c 100644 --- a/src/libtomahawk/Pipeline.h +++ b/src/libtomahawk/Pipeline.h @@ -36,7 +36,7 @@ namespace Tomahawk { class Resolver; class ExternalResolver; -typedef boost::function ResolverFactoryFunc; +typedef boost::function ResolverFactoryFunc; class DLLEXPORT Pipeline : public QObject { @@ -58,7 +58,7 @@ public: void reportArtists( QID qid, const QList< artist_ptr >& artists ); void addExternalResolverFactory( ResolverFactoryFunc resolverFactory ); - Tomahawk::ExternalResolver* addScriptResolver( const QString& scriptPath ); + Tomahawk::ExternalResolver* addScriptResolver( const QString& scriptPath, const QStringList& additionalScriptPaths = QStringList() ); void stopScriptResolver( const QString& scriptPath ); void removeScriptResolver( const QString& scriptPath ); QList< QPointer< ExternalResolver > > scriptResolvers() const { return m_scriptResolvers; } diff --git a/src/libtomahawk/Typedefs.h b/src/libtomahawk/Typedefs.h index daf5a98f2..f54aa57c8 100644 --- a/src/libtomahawk/Typedefs.h +++ b/src/libtomahawk/Typedefs.h @@ -89,7 +89,7 @@ namespace Tomahawk }; class ExternalResolver; - typedef boost::function ResolverFactoryFunc; + typedef boost::function ResolverFactoryFunc; namespace PlaylistModes { enum RepeatMode { NoRepeat, RepeatOne, RepeatAll }; diff --git a/src/libtomahawk/accounts/ResolverAccount.cpp b/src/libtomahawk/accounts/ResolverAccount.cpp index 2e1f0655f..a3591caea 100644 --- a/src/libtomahawk/accounts/ResolverAccount.cpp +++ b/src/libtomahawk/accounts/ResolverAccount.cpp @@ -63,15 +63,23 @@ Account* ResolverAccountFactory::createFromPath( const QString& path, const QString& factory, bool isAttica ) { qDebug() << "Creating ResolverAccount from path:" << path << "is attica" << isAttica; + + const QFileInfo pathInfo( path ); + if ( isAttica ) { - QFileInfo info( path ); - return new AtticaResolverAccount( generateId( factory ), path, info.baseName() ); + QVariantHash configuration; + QDir dir = pathInfo.absoluteDir();//assume we are in the code directory of a bundle + if ( dir.cdUp() && dir.cdUp() ) //go up twice to the content dir, if any + { + QString metadataFilePath = dir.absoluteFilePath( "metadata.json" ); + configuration = metadataFromJsonFile( metadataFilePath ); + } + return new AtticaResolverAccount( generateId( factory ), path, pathInfo.baseName(), configuration ); } else //on filesystem, but it could be a bundle or a legacy resolver file { QString realPath( path ); - const QFileInfo pathInfo( path ); QVariantHash configuration; @@ -135,6 +143,16 @@ ResolverAccountFactory::metadataFromJsonFile( const QString& path ) QFileInfo fi( path ); result[ "path" ] = fi.absoluteDir().absoluteFilePath( manifest[ "main" ].toString() ); //this is our path to the JS } + if ( !manifest[ "scripts" ].isNull() ) + { + QStringList scripts; + foreach ( QString s, manifest[ "scripts" ].toStringList() ) + { + QFileInfo fi( path ); + scripts << fi.absoluteDir().absoluteFilePath( s ); + } + result[ "scripts" ] = scripts; + } } } //TODO: correct baseName and rename directory maybe? @@ -161,6 +179,7 @@ ResolverAccount::ResolverAccount( const QString& accountId, const QString& path, { QVariantHash configuration( initialConfiguration ); configuration[ "path" ] = path; + setConfiguration( configuration ); init( path ); @@ -200,7 +219,13 @@ ResolverAccount::hookupResolver() { tDebug() << "Hooking up resolver:" << configuration().value( "path" ).toString() << enabled(); - m_resolver = QPointer< ExternalResolverGui >( qobject_cast< ExternalResolverGui* >( Pipeline::instance()->addScriptResolver( configuration().value( "path" ).toString() ) ) ); + QString mainScriptPath = configuration().value( "path" ).toString(); + QStringList additionalPaths; + if ( configuration().contains( "scripts" ) ) + additionalPaths = configuration().value( "scripts" ).toStringList(); + + Tomahawk::ExternalResolver* er = Pipeline::instance()->addScriptResolver( mainScriptPath, additionalPaths ); + m_resolver = QPointer< ExternalResolverGui >( qobject_cast< ExternalResolverGui* >( er ) ); connect( m_resolver.data(), SIGNAL( changed() ), this, SLOT( resolverChanged() ) ); // What resolver do we have here? Should only be types that are 'real' resolvers @@ -341,8 +366,8 @@ AtticaResolverAccount::AtticaResolverAccount( const QString& accountId ) } -AtticaResolverAccount::AtticaResolverAccount( const QString& accountId, const QString& path, const QString& atticaId ) - : ResolverAccount( accountId, path ) +AtticaResolverAccount::AtticaResolverAccount( const QString& accountId, const QString& path, const QString& atticaId, const QVariantHash& initialConfiguration ) + : ResolverAccount( accountId, path, initialConfiguration ) , m_atticaId( atticaId ) { QVariantHash conf = configuration(); diff --git a/src/libtomahawk/accounts/ResolverAccount.h b/src/libtomahawk/accounts/ResolverAccount.h index 9f25f2ade..5906aad4c 100644 --- a/src/libtomahawk/accounts/ResolverAccount.h +++ b/src/libtomahawk/accounts/ResolverAccount.h @@ -133,7 +133,7 @@ private slots: void loadIcon(); private: // Created by factory, when user installs a new resolver - AtticaResolverAccount( const QString& accountId, const QString& path, const QString& atticaId ); + AtticaResolverAccount( const QString& accountId, const QString& path, const QString& atticaId, const QVariantHash& initialConfiguration = QVariantHash() ); void init(); diff --git a/src/libtomahawk/resolvers/QtScriptResolver.cpp b/src/libtomahawk/resolvers/QtScriptResolver.cpp index 01146f701..ba991f089 100644 --- a/src/libtomahawk/resolvers/QtScriptResolver.cpp +++ b/src/libtomahawk/resolvers/QtScriptResolver.cpp @@ -343,12 +343,13 @@ ScriptEngine::javaScriptConsoleMessage( const QString& message, int lineNumber, } -QtScriptResolver::QtScriptResolver( const QString& scriptPath ) +QtScriptResolver::QtScriptResolver( const QString& scriptPath, const QStringList& additionalScriptPaths ) : Tomahawk::ExternalResolverGui( scriptPath ) , m_ready( false ) , m_stopped( true ) , m_error( Tomahawk::ExternalResolver::NoError ) , m_resolverHelper( new QtScriptResolverHelper( scriptPath, this ) ) + , m_requiredScriptPaths( additionalScriptPaths ) { tLog() << Q_FUNC_INFO << "Loading JS resolver:" << scriptPath; @@ -379,14 +380,14 @@ QtScriptResolver::~QtScriptResolver() } -Tomahawk::ExternalResolver* QtScriptResolver::factory( const QString& scriptPath ) +Tomahawk::ExternalResolver* QtScriptResolver::factory( const QString& scriptPath, const QStringList& additionalScriptPaths ) { ExternalResolver* res = 0; const QFileInfo fi( scriptPath ); if ( fi.suffix() == "js" || fi.suffix() == "script" ) { - res = new QtScriptResolver( scriptPath ); + res = new QtScriptResolver( scriptPath, additionalScriptPaths ); tLog() << Q_FUNC_INFO << scriptPath << "Loaded."; } @@ -438,6 +439,21 @@ QtScriptResolver::init() m_engine->mainFrame()->evaluateJavaScript( jslib.readAll() ); jslib.close(); + // add resolver dependencies, if any + foreach ( QString s, m_requiredScriptPaths ) + { + QFile reqFile( s ); + if( !reqFile.open( QIODevice::ReadOnly ) ) + { + qWarning() << "Failed to read contents of file:" << s << reqFile.errorString(); + return; + } + const QByteArray reqContents = reqFile.readAll(); + + m_engine->setScriptPath( s ); + m_engine->mainFrame()->evaluateJavaScript( reqContents ); + } + // add resolver m_engine->setScriptPath( filePath() ); m_engine->mainFrame()->evaluateJavaScript( scriptContents ); diff --git a/src/libtomahawk/resolvers/QtScriptResolver.h b/src/libtomahawk/resolvers/QtScriptResolver.h index 4156bbe71..9a63af7e7 100644 --- a/src/libtomahawk/resolvers/QtScriptResolver.h +++ b/src/libtomahawk/resolvers/QtScriptResolver.h @@ -148,9 +148,9 @@ Q_OBJECT friend class ::QtScriptResolverHelper; public: - explicit QtScriptResolver( const QString& scriptPath ); + explicit QtScriptResolver( const QString& scriptPath, const QStringList& additionalScriptPaths = QStringList() ); virtual ~QtScriptResolver(); - static ExternalResolver* factory( const QString& scriptPath ); + static ExternalResolver* factory( const QString& scriptPath, const QStringList& additionalScriptPaths = QStringList() ); virtual Capabilities capabilities() const { return m_capabilities; } @@ -219,6 +219,7 @@ private: QtScriptResolverHelper* m_resolverHelper; QPointer< AccountConfigWidget > m_configWidget; QList< QVariant > m_dataWidgets; + QStringList m_requiredScriptPaths; }; #endif // QTSCRIPTRESOLVER_H diff --git a/src/libtomahawk/resolvers/ScriptResolver.cpp b/src/libtomahawk/resolvers/ScriptResolver.cpp index 1df21ba09..3561f4f91 100644 --- a/src/libtomahawk/resolvers/ScriptResolver.cpp +++ b/src/libtomahawk/resolvers/ScriptResolver.cpp @@ -96,8 +96,10 @@ ScriptResolver::~ScriptResolver() Tomahawk::ExternalResolver* -ScriptResolver::factory( const QString& exe ) +ScriptResolver::factory( const QString& exe, const QStringList& unused ) { + Q_UNUSED( unused ) + ExternalResolver* res = 0; const QFileInfo fi( exe ); diff --git a/src/libtomahawk/resolvers/ScriptResolver.h b/src/libtomahawk/resolvers/ScriptResolver.h index 14372a669..0e0de460f 100644 --- a/src/libtomahawk/resolvers/ScriptResolver.h +++ b/src/libtomahawk/resolvers/ScriptResolver.h @@ -40,7 +40,7 @@ Q_OBJECT public: explicit ScriptResolver( const QString& exe ); virtual ~ScriptResolver(); - static ExternalResolver* factory( const QString& exe ); + static ExternalResolver* factory( const QString& exe, const QStringList& ); virtual QString name() const { return m_name; } virtual QPixmap icon() const { return m_icon; }