mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-26 15:25:04 +02:00
Add JavaScript InfoPlugins
This commit is contained in:
@@ -84,6 +84,8 @@ set( libGuiSources
|
||||
|
||||
resolvers/ExternalResolverGui.cpp
|
||||
resolvers/ScriptResolver.cpp
|
||||
resolvers/JSInfoPlugin.cpp
|
||||
resolvers/JSInfoSystemHelper.cpp
|
||||
resolvers/JSResolver.cpp
|
||||
resolvers/JSResolverHelper.cpp
|
||||
resolvers/ScriptEngine.cpp
|
||||
|
236
src/libtomahawk/resolvers/JSInfoPlugin.cpp
Normal file
236
src/libtomahawk/resolvers/JSInfoPlugin.cpp
Normal file
@@ -0,0 +1,236 @@
|
||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||
*
|
||||
* Copyright 2014, Dominik Schmidt <domme@tomahawk-player.org>
|
||||
*
|
||||
* Tomahawk is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Tomahawk is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "JSInfoPlugin_p.h"
|
||||
|
||||
#include "JSResolver.h"
|
||||
#include "Typedefs.h"
|
||||
|
||||
#include "../utils/Logger.h"
|
||||
#include "../utils/Json.h"
|
||||
|
||||
JSInfoPlugin::JSInfoPlugin( int id, JSResolver *resolver )
|
||||
: d_ptr( new JSInfoPluginPrivate( this, id, resolver ) )
|
||||
{
|
||||
Q_ASSERT( resolver );
|
||||
|
||||
// read in supported GetTypes and PushTypes
|
||||
m_supportedGetTypes = parseSupportedTypes( callMethodOnInfoPlugin( "supportedGetTypes" ) );
|
||||
m_supportedPushTypes = parseSupportedTypes( callMethodOnInfoPlugin( "supportedPushTypes" ) );
|
||||
|
||||
setFriendlyName( QString( "JSInfoPlugin: %1" ).arg( resolver->name() ) );
|
||||
}
|
||||
|
||||
|
||||
JSInfoPlugin::~JSInfoPlugin()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
JSInfoPlugin::init()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
JSInfoPlugin::getInfo( Tomahawk::InfoSystem::InfoRequestData requestData )
|
||||
{
|
||||
Q_D( JSInfoPlugin );
|
||||
|
||||
d->requestDataCache[ requestData.requestId ] = requestData;
|
||||
|
||||
QString eval = QString("_getInfo(%1, %2, %3, %4)")
|
||||
.arg( d->id ) // infoPluginId
|
||||
.arg( requestData.requestId ) // requestId
|
||||
.arg( requestData.type ) // type
|
||||
.arg( serializeQVariantMap( convertInfoStringHashToQVariantMap( requestData.input.value<Tomahawk::InfoSystem::InfoStringHash>() ) ) ); // infoHash
|
||||
|
||||
callMethodOnInfoPlugin( eval );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
JSInfoPlugin::pushInfo( Tomahawk::InfoSystem::InfoPushData pushData )
|
||||
{
|
||||
Q_D( JSInfoPlugin );
|
||||
|
||||
QString eval = QString( "pushInfo({ type: %1, pushFlags: %2, input: %3, additionalInput: %4})" )
|
||||
.arg( pushData.type )
|
||||
.arg( pushData.pushFlags )
|
||||
.arg( serializeQVariantMap ( pushData.infoPair.second.toMap() ) )
|
||||
.arg( serializeQVariantMap( pushData.infoPair.first ) );
|
||||
|
||||
callMethodOnInfoPlugin( eval );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
JSInfoPlugin::notInCacheSlot( Tomahawk::InfoSystem::InfoStringHash criteria, Tomahawk::InfoSystem::InfoRequestData requestData )
|
||||
{
|
||||
Q_D( JSInfoPlugin );
|
||||
|
||||
d->requestDataCache[ requestData.requestId ] = requestData;
|
||||
d->criteriaCache[ requestData.requestId ] = criteria;
|
||||
|
||||
|
||||
QString eval = QString( "_notInCache(%1, %2, %3, %4)" )
|
||||
.arg( d->id )
|
||||
.arg( requestData.requestId )
|
||||
.arg( requestData.type )
|
||||
.arg( serializeQVariantMap( convertInfoStringHashToQVariantMap( criteria ) ) );
|
||||
|
||||
callMethodOnInfoPlugin( eval );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
JSInfoPlugin::addInfoRequestResult ( int requestId, qint64 maxAge, const QVariantMap& returnedData )
|
||||
{
|
||||
Q_D( JSInfoPlugin );
|
||||
|
||||
// retrieve requestData from cache and delete it
|
||||
Tomahawk::InfoSystem::InfoRequestData requestData = d->requestDataCache[ requestId ];
|
||||
d->requestDataCache.remove( requestId );
|
||||
|
||||
emit info( requestData, returnedData );
|
||||
|
||||
// retrieve criteria from cache and delete it
|
||||
Tomahawk::InfoSystem::InfoStringHash criteria = d->criteriaCache[ requestId ];
|
||||
d->criteriaCache.remove( requestId );
|
||||
|
||||
emit updateCache( criteria, maxAge, requestData.type, returnedData );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
JSInfoPlugin::emitGetCachedInfo ( int requestId, const QVariantMap& criteria, int newMaxAge )
|
||||
{
|
||||
Q_D( JSInfoPlugin );
|
||||
|
||||
emit getCachedInfo( convertQVariantMapToInfoStringHash( criteria ), newMaxAge, d->requestDataCache[ requestId ]);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
JSInfoPlugin::emitInfo ( int requestId, const QVariantMap& output )
|
||||
{
|
||||
Q_D( JSInfoPlugin );
|
||||
|
||||
emit info( d->requestDataCache[ requestId ], output );
|
||||
}
|
||||
|
||||
|
||||
QString
|
||||
JSInfoPlugin::serviceGetter() const
|
||||
{
|
||||
Q_D( const JSInfoPlugin );
|
||||
|
||||
return QString("Tomahawk.InfoSystem.getInfoPlugin(%1)").arg( d->id );
|
||||
}
|
||||
|
||||
|
||||
QVariant
|
||||
JSInfoPlugin::callMethodOnInfoPlugin ( const QString& scriptSource )
|
||||
{
|
||||
Q_D( JSInfoPlugin );
|
||||
|
||||
QString eval = QString( "%1.%2" ).arg( serviceGetter() ).arg( scriptSource );
|
||||
|
||||
tLog() << Q_FUNC_INFO << eval;
|
||||
|
||||
return d->resolver->evaluateJavaScript( eval );
|
||||
}
|
||||
|
||||
QSet< Tomahawk::InfoSystem::InfoType >
|
||||
JSInfoPlugin::parseSupportedTypes ( const QVariant& variant )
|
||||
{
|
||||
QVariantList list = variant.toList();
|
||||
|
||||
QSet < Tomahawk::InfoSystem::InfoType > results;
|
||||
foreach( const QVariant& type, list )
|
||||
{
|
||||
bool ok;
|
||||
int intType = type.toInt( &ok );
|
||||
if ( ok )
|
||||
{
|
||||
results.insert( static_cast< Tomahawk::InfoSystem::InfoType >( intType ) );
|
||||
}
|
||||
tLog() << type << intType;
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
|
||||
QString
|
||||
JSInfoPlugin::serializeQVariantMap ( const QVariantMap& map )
|
||||
{
|
||||
QVariantMap localMap = map;
|
||||
|
||||
foreach( const QString& key, localMap.keys() )
|
||||
{
|
||||
QVariant currentVariant = localMap[ key ];
|
||||
|
||||
// strip unserializable types - at least QJson needs this, check with QtJson
|
||||
if( currentVariant.canConvert<QImage>() )
|
||||
{
|
||||
localMap.remove( key );
|
||||
}
|
||||
|
||||
// convert InfoStringHash to QVariantMap
|
||||
if( currentVariant.canConvert< Tomahawk::InfoSystem::InfoStringHash >() )
|
||||
{
|
||||
Tomahawk::InfoSystem::InfoStringHash currentHash = currentVariant.value< Tomahawk::InfoSystem::InfoStringHash >();
|
||||
localMap[ key ] = convertInfoStringHashToQVariantMap( currentHash );
|
||||
}
|
||||
}
|
||||
|
||||
QByteArray serialized = TomahawkUtils::toJson( localMap );
|
||||
|
||||
return QString("JSON.parse('%1')").arg( QString::fromUtf8( serialized ) );
|
||||
}
|
||||
|
||||
|
||||
QVariantMap
|
||||
JSInfoPlugin::convertInfoStringHashToQVariantMap ( const Tomahawk::InfoSystem::InfoStringHash& hash )
|
||||
{
|
||||
QVariantMap map;
|
||||
|
||||
foreach( const QString& key, hash.keys() )
|
||||
{
|
||||
map[key] = QVariant::fromValue< QString >( hash.value( key ) );
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
|
||||
Tomahawk::InfoSystem::InfoStringHash
|
||||
JSInfoPlugin::convertQVariantMapToInfoStringHash ( const QVariantMap& map )
|
||||
{
|
||||
Tomahawk::InfoSystem::InfoStringHash hash;
|
||||
|
||||
foreach( const QString& key, map.keys() )
|
||||
{
|
||||
hash.insert( key, map[ key ].toString() );
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
69
src/libtomahawk/resolvers/JSInfoPlugin.h
Normal file
69
src/libtomahawk/resolvers/JSInfoPlugin.h
Normal file
@@ -0,0 +1,69 @@
|
||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||
*
|
||||
* Copyright 2014, Dominik Schmidt <domme@tomahawk-player.org>
|
||||
*
|
||||
* Tomahawk is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Tomahawk is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef TOMAHAWK_JSINFOPLUGIN_H
|
||||
#define TOMAHAWK_JSINFOPLUGIN_H
|
||||
|
||||
#include "../infosystem/InfoSystem.h"
|
||||
|
||||
#include "DllMacro.h"
|
||||
|
||||
|
||||
class JSResolver;
|
||||
class JSInfoPluginPrivate;
|
||||
|
||||
|
||||
class DLLEXPORT JSInfoPlugin : public Tomahawk::InfoSystem::InfoPlugin
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
/**
|
||||
* @param id unique identifier to identify an infoplugin in its scope
|
||||
*/
|
||||
JSInfoPlugin( int id, JSResolver* resolver );
|
||||
virtual ~JSInfoPlugin();
|
||||
|
||||
|
||||
void addInfoRequestResult( int requestId, qint64 maxAge, const QVariantMap& returnedData );
|
||||
void emitGetCachedInfo( int requestId, const QVariantMap& criteria, int newMaxAge );
|
||||
void emitInfo( int requestId, const QVariantMap& output );
|
||||
|
||||
protected slots:
|
||||
void init() override;
|
||||
|
||||
void getInfo( Tomahawk::InfoSystem::InfoRequestData requestData ) override;
|
||||
void pushInfo( Tomahawk::InfoSystem::InfoPushData pushData ) override;
|
||||
void notInCacheSlot( Tomahawk::InfoSystem::InfoStringHash criteria, Tomahawk::InfoSystem::InfoRequestData requestData ) override;
|
||||
|
||||
protected:
|
||||
// TODO: create JSPlugin base class and move these methods there
|
||||
QString serviceGetter() const; // = 0
|
||||
QVariant callMethodOnInfoPlugin( const QString& scriptSource );
|
||||
|
||||
private:
|
||||
static QSet< Tomahawk::InfoSystem::InfoType > parseSupportedTypes(const QVariant& variant);
|
||||
static QString serializeQVariantMap(const QVariantMap& map);
|
||||
static QVariantMap convertInfoStringHashToQVariantMap(const Tomahawk::InfoSystem::InfoStringHash& hash);
|
||||
static Tomahawk::InfoSystem::InfoStringHash convertQVariantMapToInfoStringHash( const QVariantMap& map );
|
||||
|
||||
Q_DECLARE_PRIVATE( JSInfoPlugin )
|
||||
QScopedPointer<JSInfoPluginPrivate> d_ptr;
|
||||
};
|
||||
|
||||
#endif // TOMAHAWK_JSINFOPLUGIN_H
|
46
src/libtomahawk/resolvers/JSInfoPlugin_p.h
Normal file
46
src/libtomahawk/resolvers/JSInfoPlugin_p.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||
*
|
||||
* Copyright 2014, Dominik Schmidt <domme@tomahawk-player.org>
|
||||
*
|
||||
* Tomahawk is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Tomahawk is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef TOMAHAWK_JSINFOPLUGIN_P_H
|
||||
#define TOMAHAWK_JSINFOPLUGIN_P_H
|
||||
|
||||
#include "JSInfoPlugin.h"
|
||||
|
||||
class JSInfoPluginPrivate
|
||||
{
|
||||
friend class ::JSInfoPlugin;
|
||||
public:
|
||||
JSInfoPluginPrivate( JSInfoPlugin* q, int id, JSResolver* resolver )
|
||||
: q_ptr ( q )
|
||||
, id( id )
|
||||
, resolver( resolver )
|
||||
{
|
||||
}
|
||||
JSInfoPlugin* q_ptr;
|
||||
Q_DECLARE_PUBLIC ( JSInfoPlugin )
|
||||
|
||||
private:
|
||||
int id;
|
||||
JSResolver* resolver;
|
||||
|
||||
QMap< int, Tomahawk::InfoSystem::InfoRequestData > requestDataCache;
|
||||
QMap< int, Tomahawk::InfoSystem::InfoStringHash > criteriaCache;
|
||||
};
|
||||
|
||||
|
||||
#endif // TOMAHAWK_JSINFOPLUGIN_P_H
|
118
src/libtomahawk/resolvers/JSInfoSystemHelper.cpp
Normal file
118
src/libtomahawk/resolvers/JSInfoSystemHelper.cpp
Normal file
@@ -0,0 +1,118 @@
|
||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||
*
|
||||
* Copyright 2014, Dominik Schmidt <domme@tomahawk-player.org>
|
||||
*
|
||||
* Tomahawk is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Tomahawk is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "JSInfoSystemHelper_p.h"
|
||||
#include "JSInfoPlugin.h"
|
||||
|
||||
#include "../utils/Logger.h"
|
||||
|
||||
#include <QMetaMethod>
|
||||
|
||||
|
||||
JSInfoSystemHelper::JSInfoSystemHelper( JSResolver* parent )
|
||||
: QObject( parent )
|
||||
, d_ptr( new JSInfoSystemHelperPrivate( this, parent ) )
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
JSInfoSystemHelper::~JSInfoSystemHelper()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
QStringList JSInfoSystemHelper::requiredScriptPaths() const
|
||||
{
|
||||
return QStringList()
|
||||
<< RESPATH "js/es6-promise-2.0.0.min.js"
|
||||
<< RESPATH "js/tomahawk-infosystem.js";
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
JSInfoSystemHelper::nativeAddInfoPlugin ( int id )
|
||||
{
|
||||
Q_D( JSInfoSystemHelper );
|
||||
|
||||
// create infoplugin instance
|
||||
JSInfoPlugin* jsInfoPlugin = new JSInfoPlugin( id, d->resolver );
|
||||
Tomahawk::InfoSystem::InfoPluginPtr infoPlugin( jsInfoPlugin );
|
||||
|
||||
// move it to infosystem thread
|
||||
infoPlugin->moveToThread( Tomahawk::InfoSystem::InfoSystem::instance()->workerThread().data() );
|
||||
|
||||
// add it to local list of infoplugins
|
||||
d->infoPlugins[id] = jsInfoPlugin;
|
||||
|
||||
// add it to infosystem
|
||||
Tomahawk::InfoSystem::InfoSystem::instance()->addInfoPlugin( infoPlugin );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
JSInfoSystemHelper::nativeRemoveInfoPlugin ( int id )
|
||||
{
|
||||
Q_UNUSED( id );
|
||||
tLog() << "Removing Info plugins from JS is not implemented yet";
|
||||
Q_ASSERT( false );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
JSInfoSystemHelper::nativeAddInfoRequestResult ( int infoPluginId, int requestId, int maxAge, const QVariantMap& returnedData )
|
||||
{
|
||||
Q_D( JSInfoSystemHelper );
|
||||
|
||||
if ( !d->infoPlugins[ infoPluginId ] )
|
||||
{
|
||||
Q_ASSERT( d->infoPlugins[ infoPluginId ] );
|
||||
return;
|
||||
}
|
||||
|
||||
d->infoPlugins[ infoPluginId ]->addInfoRequestResult( requestId, maxAge, returnedData );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
JSInfoSystemHelper::nativeGetCachedInfo ( int infoPluginId, int requestId, int newMaxAge, const QVariantMap& criteria )
|
||||
{
|
||||
Q_D( JSInfoSystemHelper );
|
||||
|
||||
if ( !d->infoPlugins[ infoPluginId ] )
|
||||
{
|
||||
Q_ASSERT( d->infoPlugins[ infoPluginId ] );
|
||||
return;
|
||||
}
|
||||
|
||||
d->infoPlugins[ infoPluginId ]->emitGetCachedInfo( requestId, criteria, newMaxAge );
|
||||
|
||||
}
|
||||
|
||||
void JSInfoSystemHelper::nativeDataError ( int infoPluginId, int requestId )
|
||||
{
|
||||
Q_D( JSInfoSystemHelper );
|
||||
|
||||
if ( !d->infoPlugins[ infoPluginId ] )
|
||||
{
|
||||
Q_ASSERT( d->infoPlugins[ infoPluginId ] );
|
||||
return;
|
||||
}
|
||||
|
||||
d->infoPlugins[ infoPluginId ]->emitInfo( requestId, QVariantMap() );
|
||||
}
|
||||
|
50
src/libtomahawk/resolvers/JSInfoSystemHelper.h
Normal file
50
src/libtomahawk/resolvers/JSInfoSystemHelper.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||
*
|
||||
* Copyright 2014, Dominik Schmidt <domme@tomahawk-player.org>
|
||||
*
|
||||
* Tomahawk is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Tomahawk is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef TOMAHAWK_JSINFOSYSTEMHELPER_H
|
||||
#define TOMAHAWK_JSINFOSYSTEMHELPER_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class JSResolver;
|
||||
class JSInfoSystemHelperPrivate;
|
||||
|
||||
class JSInfoSystemHelper : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
JSInfoSystemHelper( JSResolver* parent );
|
||||
~JSInfoSystemHelper();
|
||||
|
||||
QStringList requiredScriptPaths() const;
|
||||
|
||||
Q_INVOKABLE void nativeAddInfoPlugin( int id );
|
||||
Q_INVOKABLE void nativeRemoveInfoPlugin( int id );
|
||||
|
||||
Q_INVOKABLE void nativeAddInfoRequestResult( int infoPluginId, int requestId, int maxAge, const QVariantMap& returnedData );
|
||||
Q_INVOKABLE void nativeGetCachedInfo( int infoPluginId, int requestId, int newMaxAge, const QVariantMap& criteria);
|
||||
Q_INVOKABLE void nativeDataError( int infoPluginId, int requestId );
|
||||
|
||||
|
||||
private:
|
||||
Q_DECLARE_PRIVATE( JSInfoSystemHelper )
|
||||
QScopedPointer<JSInfoSystemHelperPrivate> d_ptr;
|
||||
};
|
||||
|
||||
#endif // TOMAHAWK_JSINFOSYSTEMHELPER_H
|
45
src/libtomahawk/resolvers/JSInfoSystemHelper_p.h
Normal file
45
src/libtomahawk/resolvers/JSInfoSystemHelper_p.h
Normal file
@@ -0,0 +1,45 @@
|
||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||
*
|
||||
* Copyright 2014, Dominik Schmidt <domme@tomahawk-player.org>
|
||||
*
|
||||
* Tomahawk is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Tomahawk is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef TOMAHAWK_JSINFOSYSTEMHELPER_P_H
|
||||
#define TOMAHAWK_JSINFOSYSTEMHELPER_P_H
|
||||
|
||||
#include "JSResolver.h"
|
||||
#include "JSInfoSystemHelper.h"
|
||||
|
||||
class JSInfoSystemHelperPrivate
|
||||
{
|
||||
friend class ::JSInfoSystemHelper;
|
||||
public:
|
||||
JSInfoSystemHelperPrivate( JSInfoSystemHelper* q, JSResolver* resolver )
|
||||
: q_ptr ( q )
|
||||
, resolver ( resolver )
|
||||
{
|
||||
}
|
||||
|
||||
JSInfoSystemHelper* q_ptr;
|
||||
Q_DECLARE_PUBLIC ( JSInfoSystemHelper )
|
||||
|
||||
private:
|
||||
JSResolver* resolver;
|
||||
QMap<int,JSInfoPlugin*> infoPlugins;
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif // TOMAHAWK_JSINFOSYSTEMHELPER_P_H
|
@@ -42,6 +42,7 @@
|
||||
#include "TomahawkSettings.h"
|
||||
#include "TomahawkVersion.h"
|
||||
#include "Track.h"
|
||||
#include "JSInfoPlugin.h"
|
||||
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
@@ -204,57 +205,44 @@ JSResolver::init()
|
||||
|
||||
d->engine->mainFrame()->setHtml( "<html><body></body></html>", QUrl( "file:///invalid/file/for/security/policy" ) );
|
||||
|
||||
// add c++ part of tomahawk javascript library
|
||||
d->engine->mainFrame()->addToJavaScriptWindowObject( "Tomahawk", d->resolverHelper );
|
||||
|
||||
// Load CrytoJS
|
||||
{
|
||||
d->engine->setScriptPath( "cryptojs-core.js" );
|
||||
QFile jslib( RESPATH "js/cryptojs-core.js" );
|
||||
jslib.open( QIODevice::ReadOnly );
|
||||
d->engine->mainFrame()->evaluateJavaScript( jslib.readAll() );
|
||||
jslib.close();
|
||||
}
|
||||
// tomahawk.js
|
||||
{
|
||||
// add c++ part of tomahawk javascript library
|
||||
d->engine->mainFrame()->addToJavaScriptWindowObject( "Tomahawk", d->resolverHelper );
|
||||
|
||||
// Load CrytoJS core
|
||||
loadScript( RESPATH "js/cryptojs-core.js" );
|
||||
|
||||
// Load CryptoJS modules
|
||||
QStringList jsfiles;
|
||||
jsfiles << "*.js";
|
||||
QDir cryptojs( RESPATH "js/cryptojs" );
|
||||
foreach ( QString jsfile, cryptojs.entryList( jsfiles ) )
|
||||
{
|
||||
d->engine->setScriptPath( RESPATH "js/cryptojs/" + jsfile );
|
||||
QFile jslib( RESPATH "js/cryptojs/" + jsfile );
|
||||
jslib.open( QIODevice::ReadOnly );
|
||||
d->engine->mainFrame()->evaluateJavaScript( jslib.readAll() );
|
||||
jslib.close();
|
||||
loadScript( RESPATH "js/cryptojs/" + jsfile );
|
||||
}
|
||||
|
||||
// Load tomahawk.js
|
||||
loadScript( RESPATH "js/tomahawk.js" );
|
||||
}
|
||||
|
||||
// tomahawk-infosystem.js
|
||||
{
|
||||
// Load the tomahawk javascript utilities
|
||||
d->engine->setScriptPath( "tomahawk.js" );
|
||||
QFile jslib( RESPATH "js/tomahawk.js" );
|
||||
jslib.open( QIODevice::ReadOnly );
|
||||
d->engine->mainFrame()->evaluateJavaScript( jslib.readAll() );
|
||||
jslib.close();
|
||||
// add c++ part of tomahawk infosystem bindings as Tomahawk.InfoSystem
|
||||
d->engine->mainFrame()->addToJavaScriptWindowObject( "_TomahawkInfoSystem", d->infoSystemHelper );
|
||||
d->engine->mainFrame()->evaluateJavaScript( "Tomahawk.InfoSystem = _TomahawkInfoSystem;" );
|
||||
|
||||
// add deps
|
||||
loadScripts( d->infoSystemHelper->requiredScriptPaths() );
|
||||
}
|
||||
|
||||
// add resolver dependencies, if any
|
||||
foreach ( const QString& s, d->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();
|
||||
loadScripts( d->requiredScriptPaths );
|
||||
|
||||
d->engine->setScriptPath( s );
|
||||
d->engine->mainFrame()->evaluateJavaScript( reqContents );
|
||||
}
|
||||
|
||||
// add resolver
|
||||
d->engine->setScriptPath( filePath() );
|
||||
d->engine->mainFrame()->evaluateJavaScript( scriptContents );
|
||||
loadScript( filePath() );
|
||||
|
||||
// init resolver
|
||||
resolverInit();
|
||||
@@ -485,6 +473,16 @@ JSResolver::lookupUrl( const QString& url )
|
||||
}
|
||||
|
||||
|
||||
QVariant
|
||||
JSResolver::evaluateJavaScript ( const QString& scriptSource )
|
||||
{
|
||||
Q_D( JSResolver );
|
||||
|
||||
return d->engine->mainFrame()->evaluateJavaScript( scriptSource );
|
||||
}
|
||||
|
||||
|
||||
|
||||
Tomahawk::ExternalResolver::ErrorState
|
||||
JSResolver::error() const
|
||||
{
|
||||
@@ -943,3 +941,36 @@ JSResolver::resolverCollections()
|
||||
// Then when there's callbacks from a resolver, it sends source name, collection id
|
||||
// + data.
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
JSResolver::loadScript ( const QString& path )
|
||||
{
|
||||
Q_D( JSResolver );
|
||||
|
||||
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();
|
||||
|
||||
d->engine->setScriptPath( path );
|
||||
d->engine->mainFrame()->evaluateJavaScript( contents );
|
||||
|
||||
file.close();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
JSResolver::loadScripts ( const QStringList& paths )
|
||||
{
|
||||
foreach ( const QString& path, paths )
|
||||
{
|
||||
loadScript( path );
|
||||
}
|
||||
}
|
||||
|
@@ -27,6 +27,7 @@
|
||||
#include "ExternalResolverGui.h"
|
||||
#include "Typedefs.h"
|
||||
|
||||
class JSInfoPlugin;
|
||||
class JSResolverHelper;
|
||||
class JSResolverPrivate;
|
||||
class ScriptEngine;
|
||||
@@ -60,6 +61,8 @@ public:
|
||||
|
||||
bool canParseUrl( const QString& url, UrlType type ) override;
|
||||
|
||||
QVariant evaluateJavaScript( const QString& scriptSource );
|
||||
|
||||
public slots:
|
||||
void resolve( const Tomahawk::query_ptr& query ) override;
|
||||
void stop() override;
|
||||
@@ -88,6 +91,8 @@ private:
|
||||
void fillDataInWidgets( const QVariantMap& data );
|
||||
void onCapabilitiesChanged( Capabilities capabilities );
|
||||
void loadCollections();
|
||||
void loadScript( const QString& path );
|
||||
void loadScripts( const QStringList& paths );
|
||||
|
||||
// encapsulate javascript calls
|
||||
QVariantMap resolverSettings();
|
||||
|
@@ -125,7 +125,7 @@ JSResolverHelper::resolverData()
|
||||
void
|
||||
JSResolverHelper::log( const QString& message )
|
||||
{
|
||||
tLog() << m_scriptPath << ":" << message;
|
||||
tLog() << "JAVASCRIPT:" << m_scriptPath << ":" << message;
|
||||
}
|
||||
|
||||
|
||||
|
@@ -25,6 +25,7 @@
|
||||
#include "JSResolver.h"
|
||||
|
||||
#include "JSResolverHelper.h"
|
||||
#include "JSInfoSystemHelper.h"
|
||||
#include "database/fuzzyindex/FuzzyIndex.h"
|
||||
|
||||
class JSResolverPrivate
|
||||
@@ -38,6 +39,8 @@ public:
|
||||
, stopped( true )
|
||||
, error( Tomahawk::ExternalResolver::NoError )
|
||||
, resolverHelper( new JSResolverHelper( scriptPath, q ) )
|
||||
// TODO: be smarter about this, only instantiate this if the resolver supports infoplugins
|
||||
, infoSystemHelper( new JSInfoSystemHelper( q ) )
|
||||
, requiredScriptPaths( additionalScriptPaths )
|
||||
{
|
||||
}
|
||||
@@ -58,6 +61,7 @@ private:
|
||||
Tomahawk::ExternalResolver::ErrorState error;
|
||||
|
||||
JSResolverHelper* resolverHelper;
|
||||
JSInfoSystemHelper* infoSystemHelper;
|
||||
QScopedPointer<FuzzyIndex> fuzzyIndex;
|
||||
QPointer< AccountConfigWidget > configWidget;
|
||||
QList< QVariant > dataWidgets;
|
||||
|
Reference in New Issue
Block a user