mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-04 21:27:58 +02:00
Allow registering C++-plugins from JS, make them usable and use the new api in ScriptLinkGeneratorPlugin
This commit is contained in:
@@ -590,3 +590,31 @@ Tomahawk.setTimeout = Tomahawk.setTimeout || window.setTimeout;
|
||||
Tomahawk.setInterval = Tomahawk.setInterval || window.setInterval;
|
||||
Tomahawk.base64Decode = function(a) { return window.atob(a); };
|
||||
Tomahawk.base64Encode = function(b) { return window.btoa(b); };
|
||||
|
||||
Tomahawk.PluginManager = {
|
||||
objects: {},
|
||||
identifyObject: function (object) {
|
||||
if( object.id === undefined ) {
|
||||
// FIXME: get a proper unique id
|
||||
object.id = "foobar";
|
||||
}
|
||||
|
||||
return object.id;
|
||||
},
|
||||
registerPlugin: function (type, object) {
|
||||
this.objects[this.identifyObject(object)] = object;
|
||||
|
||||
Tomahawk.registerScriptPlugin(type, object.id);
|
||||
},
|
||||
|
||||
invoke: function (requestId, objectId, methodName, params ) {
|
||||
this.objects[objectId][methodName](params).then(function (result) {
|
||||
Tomahawk.reportScriptJobResults({
|
||||
requestId: requestId,
|
||||
data: result
|
||||
});
|
||||
},function (error) {
|
||||
Tomahawk.reportScriptJobResults({error: error});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
@@ -92,6 +92,9 @@ set( libGuiSources
|
||||
resolvers/JSPlugin.cpp
|
||||
resolvers/ScriptJob.cpp
|
||||
resolvers/SyncScriptJob.cpp
|
||||
resolvers/ScriptObject.cpp
|
||||
resolvers/ScriptLinkGeneratorPlugin.cpp
|
||||
resolvers/ScriptPlugin.cpp
|
||||
|
||||
utils/DpiScaler.cpp
|
||||
utils/ImageRegistry.cpp
|
||||
|
@@ -21,6 +21,8 @@
|
||||
#include "../utils/Json.h"
|
||||
#include "../utils/Logger.h"
|
||||
#include "ScriptEngine.h"
|
||||
#include "ScriptJob.h"
|
||||
#include "ScriptObject.h"
|
||||
|
||||
#include <QWebFrame>
|
||||
#include <QFile>
|
||||
@@ -100,3 +102,24 @@ JSPlugin::loadScripts( const QStringList& paths )
|
||||
loadScript( path );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
JSPlugin::startJob( ScriptJob* scriptJob )
|
||||
{
|
||||
QString eval = QString(
|
||||
"Tomahawk.PluginManager.invoke("
|
||||
"'%1'," // requestId
|
||||
"'%2'," // objectId
|
||||
"'%3'," // methodName
|
||||
"%4" // arguments
|
||||
");"
|
||||
).arg( scriptJob->id() )
|
||||
.arg( scriptJob->scriptObject()->id() )
|
||||
.arg( scriptJob->methodName() )
|
||||
.arg( serializeQVariantMap( scriptJob->arguments() ) );
|
||||
|
||||
tLog() << Q_FUNC_INFO << eval;
|
||||
|
||||
evaluateJavaScript( eval );
|
||||
}
|
||||
|
@@ -35,13 +35,15 @@ namespace Tomahawk
|
||||
//TODO: pimple
|
||||
class ScriptEngine;
|
||||
|
||||
class DLLEXPORT JSPlugin : public QObject, public ScriptPlugin
|
||||
class DLLEXPORT JSPlugin : public ScriptPlugin
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
JSPlugin();
|
||||
|
||||
void startJob( ScriptJob* scriptJob ) override;
|
||||
|
||||
/**
|
||||
* Evaluate JavaScript on the WebKit thread
|
||||
*/
|
||||
|
@@ -401,6 +401,20 @@ JSResolverHelper::reportCapabilities( const QVariant& v )
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
JSResolverHelper::reportScriptJobResults( const QVariantMap& result )
|
||||
{
|
||||
m_resolver->d_func()->scriptPlugin->reportScriptJobResult( result );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
JSResolverHelper::registerScriptPlugin(const QString& type, const QString& objectId)
|
||||
{
|
||||
m_resolver->d_func()->scriptPlugin->registerScriptPlugin( type, objectId );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
JSResolverHelper::tracksAdded( const QList<query_ptr>&, const ModelMode, const collection_ptr&)
|
||||
{
|
||||
|
@@ -143,6 +143,10 @@ public slots:
|
||||
|
||||
void reportCapabilities( const QVariant& capabilities );
|
||||
|
||||
void reportScriptJobResults( const QVariantMap& result );
|
||||
|
||||
void registerScriptPlugin( const QString& type, const QString& objectId );
|
||||
|
||||
private slots:
|
||||
void gotStreamUrl( IODeviceCallback callback, NetworkReply* reply );
|
||||
void tracksAdded( const QList<Tomahawk::query_ptr>& tracks, const Tomahawk::ModelMode, const Tomahawk::collection_ptr& collection );
|
||||
|
@@ -17,25 +17,63 @@
|
||||
*/
|
||||
|
||||
#include "ScriptJob.h"
|
||||
#include "ScriptObject.h"
|
||||
#include <QMetaObject>
|
||||
|
||||
using namespace Tomahawk;
|
||||
|
||||
ScriptJob::ScriptJob( ScriptObject* scriptObject, const QString& method, const QVariantMap& parameters )
|
||||
: QObject()
|
||||
ScriptJob::ScriptJob( const QString& id, ScriptObject* scriptObject, const QString& methodName, const QVariantMap& arguments )
|
||||
: QObject( scriptObject )
|
||||
, m_id( id )
|
||||
, m_scriptObject( scriptObject )
|
||||
, m_methodName( methodName )
|
||||
, m_arguments( arguments )
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
ScriptJob::~ScriptJob()
|
||||
{
|
||||
//FIXME: probably not necessary if we change the inheritance order
|
||||
if ( !m_id.isEmpty() )
|
||||
{
|
||||
Q_ASSERT( m_scriptObject );
|
||||
m_scriptObject->removeJob( this );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ScriptJob::start()
|
||||
{
|
||||
m_scriptObject->startJob( this );
|
||||
}
|
||||
|
||||
|
||||
ScriptObject*
|
||||
ScriptJob::scriptObject() const
|
||||
{
|
||||
return m_scriptObject;
|
||||
}
|
||||
|
||||
|
||||
const QString
|
||||
ScriptJob::id() const
|
||||
{
|
||||
return m_id;
|
||||
}
|
||||
|
||||
|
||||
const QString
|
||||
ScriptJob::methodName() const
|
||||
{
|
||||
return m_methodName;
|
||||
}
|
||||
|
||||
|
||||
const QVariantMap
|
||||
ScriptJob::arguments() const
|
||||
{
|
||||
return m_arguments;
|
||||
}
|
||||
|
||||
|
||||
|
@@ -31,17 +31,20 @@ class ScriptObject;
|
||||
|
||||
class DLLEXPORT ScriptJob : public QObject
|
||||
{
|
||||
friend class ScriptObject;
|
||||
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ScriptJob( ScriptObject* scriptObject, const QString& method, const QVariantMap& parameters = QVariantMap() );
|
||||
ScriptJob( const QString& id, ScriptObject* scriptObject, const QString& methodName, const QVariantMap& arguments = QVariantMap() );
|
||||
virtual ~ScriptJob();
|
||||
|
||||
virtual void start();
|
||||
|
||||
protected slots:
|
||||
const QString id() const;
|
||||
ScriptObject* scriptObject() const;
|
||||
const QString methodName() const;
|
||||
const QVariantMap arguments() const;
|
||||
|
||||
public slots:
|
||||
void reportResults( const QVariantMap& data );
|
||||
void reportFailure();
|
||||
|
||||
@@ -51,7 +54,11 @@ signals:
|
||||
|
||||
protected:
|
||||
// TODO: pimple
|
||||
QString m_id;
|
||||
ScriptObject* m_scriptObject;
|
||||
QVariantMap m_data;
|
||||
QString m_methodName;
|
||||
QVariantMap m_arguments;
|
||||
};
|
||||
|
||||
}
|
||||
|
83
src/libtomahawk/resolvers/ScriptLinkGeneratorPlugin.cpp
Normal file
83
src/libtomahawk/resolvers/ScriptLinkGeneratorPlugin.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
/* === 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 "ScriptLinkGeneratorPlugin_p.h"
|
||||
|
||||
#include "ScriptObject.h"
|
||||
|
||||
#include "../Artist.h"
|
||||
#include "../Album.h"
|
||||
|
||||
using namespace Tomahawk;
|
||||
|
||||
ScriptLinkGeneratorPlugin::ScriptLinkGeneratorPlugin( ScriptObject* scriptObject )
|
||||
: Utils::LinkGeneratorPlugin()
|
||||
, d_ptr( new ScriptLinkGeneratorPluginPrivate( this, scriptObject ) )
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
ScriptLinkGeneratorPlugin::~ScriptLinkGeneratorPlugin()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
ScriptJob*
|
||||
ScriptLinkGeneratorPlugin::openLink( const QString& title, const QString& artist, const QString& album ) const
|
||||
{
|
||||
Q_D( const ScriptLinkGeneratorPlugin );
|
||||
|
||||
QVariantMap arguments;
|
||||
arguments[ "title" ] = QVariant( title );
|
||||
arguments[ "artist" ] = QVariant( artist );
|
||||
arguments[ "album" ] = QVariant( album );
|
||||
|
||||
return d->scriptObject->invoke( "generateQueryLink", arguments );
|
||||
}
|
||||
|
||||
|
||||
ScriptJob*
|
||||
ScriptLinkGeneratorPlugin::openLink( const artist_ptr& artist ) const
|
||||
{
|
||||
Q_D( const ScriptLinkGeneratorPlugin );
|
||||
|
||||
// TODO: create proper serializer for QObjects
|
||||
QVariantMap arguments;
|
||||
arguments[ "name" ] = QVariant( artist->name() );
|
||||
|
||||
return d->scriptObject->invoke( "generateArtistLink", arguments );
|
||||
}
|
||||
|
||||
|
||||
ScriptJob*
|
||||
ScriptLinkGeneratorPlugin::openLink( const album_ptr& album ) const
|
||||
{
|
||||
Q_D( const ScriptLinkGeneratorPlugin );
|
||||
|
||||
// TODO: create proper serializer for QObjects
|
||||
QVariantMap arguments;
|
||||
arguments[ "name" ] = QVariant( album->name() );
|
||||
|
||||
return d->scriptObject->invoke( "generateAlbumLink", arguments );
|
||||
}
|
||||
|
||||
|
||||
ScriptJob*
|
||||
ScriptLinkGeneratorPlugin::openLink( const dynplaylist_ptr& playlist ) const
|
||||
{
|
||||
return nullptr;
|
||||
}
|
53
src/libtomahawk/resolvers/ScriptLinkGeneratorPlugin.h
Normal file
53
src/libtomahawk/resolvers/ScriptLinkGeneratorPlugin.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/* === 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_SCRIPTLINKGENERATORPLUGIN_H
|
||||
#define TOMAHAWK_SCRIPTLINKGENERATORPLUGIN_H
|
||||
|
||||
#include "../utils/LinkGeneratorPlugin.h"
|
||||
|
||||
#include "DllMacro.h"
|
||||
|
||||
|
||||
namespace Tomahawk
|
||||
{
|
||||
|
||||
class ScriptObject;
|
||||
class ScriptLinkGeneratorPluginPrivate;
|
||||
|
||||
class DLLEXPORT ScriptLinkGeneratorPlugin : QObject, public Utils::LinkGeneratorPlugin
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ScriptLinkGeneratorPlugin( ScriptObject* scriptObject );
|
||||
virtual ~ScriptLinkGeneratorPlugin();
|
||||
|
||||
ScriptJob* openLink( const QString& title, const QString& artist, const QString& album ) const override;
|
||||
ScriptJob* openLink( const artist_ptr& artist ) const override;
|
||||
ScriptJob* openLink( const album_ptr& album ) const override;
|
||||
ScriptJob* openLink( const dynplaylist_ptr& playlist ) const override;
|
||||
|
||||
private:
|
||||
Q_DECLARE_PRIVATE( ScriptLinkGeneratorPlugin )
|
||||
QScopedPointer<ScriptLinkGeneratorPluginPrivate> d_ptr;
|
||||
};
|
||||
|
||||
}; // ns: Tomahawk
|
||||
|
||||
#endif // TOMAHAWK_SCRIPTLINKGENERATORPLUGIN_H
|
45
src/libtomahawk/resolvers/ScriptLinkGeneratorPlugin_p.h
Normal file
45
src/libtomahawk/resolvers/ScriptLinkGeneratorPlugin_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_SCRIPTLINKGENERATORPLUGIN_P_H
|
||||
#define TOMAHAWK_SCRIPTLINKGENERATORPLUGIN_P_H
|
||||
|
||||
#include "ScriptLinkGeneratorPlugin.h"
|
||||
|
||||
namespace Tomahawk
|
||||
{
|
||||
|
||||
class ScriptLinkGeneratorPluginPrivate
|
||||
{
|
||||
friend class ScriptLinkGeneratorPlugin;
|
||||
public:
|
||||
ScriptLinkGeneratorPluginPrivate( ScriptLinkGeneratorPlugin* q, ScriptObject* scriptObject )
|
||||
: q_ptr ( q )
|
||||
, scriptObject( scriptObject )
|
||||
{
|
||||
}
|
||||
ScriptLinkGeneratorPlugin* q_ptr;
|
||||
Q_DECLARE_PUBLIC ( ScriptLinkGeneratorPlugin )
|
||||
|
||||
private:
|
||||
ScriptObject* scriptObject;
|
||||
};
|
||||
|
||||
} // ns: Tomahawk
|
||||
|
||||
#endif // TOMAHAWK_SCRIPTLINKGENERATORPLUGIN_P_H
|
76
src/libtomahawk/resolvers/ScriptObject.cpp
Normal file
76
src/libtomahawk/resolvers/ScriptObject.cpp
Normal file
@@ -0,0 +1,76 @@
|
||||
/* === 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 "ScriptObject_p.h"
|
||||
|
||||
#include "ScriptPlugin.h"
|
||||
|
||||
|
||||
using namespace Tomahawk;
|
||||
|
||||
ScriptObject::ScriptObject( ScriptPlugin* parent )
|
||||
: QObject( parent )
|
||||
, d_ptr( new ScriptObjectPrivate( this, parent ))
|
||||
{
|
||||
Q_D( ScriptObject );
|
||||
|
||||
// TODO:retrieve id from ScriptPlugin
|
||||
d->id = "foobar";
|
||||
}
|
||||
|
||||
|
||||
ScriptObject::~ScriptObject()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
ScriptJob*
|
||||
ScriptObject::invoke( const QString& methodName, const QVariantMap& arguments )
|
||||
{
|
||||
Q_D( ScriptObject );
|
||||
|
||||
return d->scriptPlugin->invoke( this, methodName, arguments );
|
||||
}
|
||||
|
||||
|
||||
QString
|
||||
ScriptObject::id() const
|
||||
{
|
||||
Q_D( const ScriptObject );
|
||||
|
||||
return d->id;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ScriptObject::startJob( ScriptJob* scriptJob )
|
||||
{
|
||||
Q_D( const ScriptObject );
|
||||
|
||||
d->scriptPlugin->startJob( scriptJob );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ScriptObject::removeJob( ScriptJob* scriptJob )
|
||||
{
|
||||
Q_D( const ScriptObject );
|
||||
|
||||
Q_ASSERT( d->scriptPlugin );
|
||||
d->scriptPlugin->removeJob( scriptJob );
|
||||
}
|
||||
|
57
src/libtomahawk/resolvers/ScriptObject.h
Normal file
57
src/libtomahawk/resolvers/ScriptObject.h
Normal file
@@ -0,0 +1,57 @@
|
||||
/* === 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_SCRIPTOBJECT_H
|
||||
#define TOMAHAWK_SCRIPTOBJECT_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QVariantMap>
|
||||
|
||||
#include "DllMacro.h"
|
||||
|
||||
namespace Tomahawk
|
||||
{
|
||||
|
||||
class ScriptPlugin;
|
||||
class ScriptObjectPrivate;
|
||||
class ScriptJob;
|
||||
|
||||
class DLLEXPORT ScriptObject : public QObject
|
||||
{
|
||||
friend class JSPlugin;
|
||||
friend class ScriptJob;
|
||||
|
||||
public:
|
||||
ScriptObject( ScriptPlugin* parent );
|
||||
virtual ~ScriptObject();
|
||||
|
||||
ScriptJob* invoke( const QString& methodName, const QVariantMap& arguments );
|
||||
|
||||
protected:
|
||||
QString id() const;
|
||||
|
||||
void startJob( ScriptJob* scriptJob );
|
||||
void removeJob( ScriptJob* scriptJob );
|
||||
private:
|
||||
Q_DECLARE_PRIVATE( ScriptObject )
|
||||
QScopedPointer<ScriptObjectPrivate> d_ptr;
|
||||
};
|
||||
|
||||
}; // ns: Tomahawk
|
||||
|
||||
#endif // TOMAHAWK_SCRIPTOBJECT_H
|
46
src/libtomahawk/resolvers/ScriptObject_p.h
Normal file
46
src/libtomahawk/resolvers/ScriptObject_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_SCRIPTOBJECT_P_H
|
||||
#define TOMAHAWK_SCRIPTOBJECT_P_H
|
||||
|
||||
#include "ScriptObject.h"
|
||||
|
||||
namespace Tomahawk
|
||||
{
|
||||
|
||||
class ScriptObjectPrivate
|
||||
{
|
||||
friend class ScriptObject;
|
||||
public:
|
||||
ScriptObjectPrivate( ScriptObject* q, ScriptPlugin* scriptPlugin )
|
||||
: q_ptr ( q )
|
||||
, scriptPlugin( scriptPlugin )
|
||||
{
|
||||
}
|
||||
ScriptObject* q_ptr;
|
||||
Q_DECLARE_PUBLIC ( ScriptObject )
|
||||
|
||||
private:
|
||||
QString id;
|
||||
ScriptPlugin* scriptPlugin;
|
||||
};
|
||||
|
||||
} // ns: Tomahawk
|
||||
|
||||
#endif // TOMAHAWK_SCRIPTOBJECT_P_H
|
103
src/libtomahawk/resolvers/ScriptPlugin.cpp
Normal file
103
src/libtomahawk/resolvers/ScriptPlugin.cpp
Normal file
@@ -0,0 +1,103 @@
|
||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||
*
|
||||
* Copyright (C) 2011 Leo Franchi <lfranchi@kde.org>
|
||||
* Copyright (C) 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 2 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 "ScriptPlugin.h"
|
||||
|
||||
#include "ScriptObject.h"
|
||||
#include "../utils/Logger.h"
|
||||
|
||||
|
||||
// TODO: register factory methods instead of hardcoding all plugin types in here
|
||||
#include "../utils/LinkGenerator.h"
|
||||
#include "ScriptLinkGeneratorPlugin.h"
|
||||
|
||||
using namespace Tomahawk;
|
||||
|
||||
static QString
|
||||
requestIdGenerator()
|
||||
{
|
||||
//FIXME: create a proper requestId
|
||||
return "somerequestId";
|
||||
}
|
||||
|
||||
|
||||
ScriptJob*
|
||||
ScriptPlugin::invoke( ScriptObject* scriptObject, const QString& methodName, const QVariantMap& arguments )
|
||||
{
|
||||
QString requestId = requestIdGenerator();
|
||||
|
||||
ScriptJob* job = new ScriptJob( requestId, scriptObject, methodName, arguments );
|
||||
m_jobs.insert( requestId, job );
|
||||
|
||||
return job;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ScriptPlugin::removeJob( ScriptJob* job )
|
||||
{
|
||||
m_jobs.remove( job->id() );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ScriptPlugin::reportScriptJobResult( const QVariantMap& result )
|
||||
{
|
||||
tLog() << Q_FUNC_INFO << result;
|
||||
const QString requestId = result[ "requestId" ].toString();
|
||||
Q_ASSERT( !requestId.isEmpty() );
|
||||
|
||||
ScriptJob* job = m_jobs.value( requestId );
|
||||
Q_ASSERT( job );
|
||||
|
||||
// got a successful job result
|
||||
if ( result[ "error"].isNull() )
|
||||
{
|
||||
const QVariantMap data = result[ "data" ].toMap();
|
||||
|
||||
job->reportResults( data );
|
||||
}
|
||||
else
|
||||
{
|
||||
job->reportFailure();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ScriptPlugin::registerScriptPlugin( const QString& type, const QString& objectId )
|
||||
{
|
||||
ScriptObject* object = m_objects.value( objectId );
|
||||
if( !object )
|
||||
{
|
||||
object = new ScriptObject( this );
|
||||
m_objects.insert( objectId, object );
|
||||
}
|
||||
|
||||
if ( type == "linkGenerator" )
|
||||
{
|
||||
tLog() << "Got link generator plugin";
|
||||
ScriptLinkGeneratorPlugin* lgp = new ScriptLinkGeneratorPlugin( object );
|
||||
Utils::LinkGenerator::instance()->addPlugin( lgp );
|
||||
}
|
||||
else
|
||||
{
|
||||
tLog() << "This plugin type is not handled by Tomahawk";
|
||||
Q_ASSERT( false );
|
||||
}
|
||||
}
|
@@ -22,15 +22,36 @@
|
||||
#define TOMAHAWK_SCRIPTPLUGIN_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QVariantMap>
|
||||
|
||||
//TODO: pimple
|
||||
#include <QHash>
|
||||
|
||||
#include "../DllMacro.h"
|
||||
|
||||
namespace Tomahawk {
|
||||
|
||||
class DLLEXPORT ScriptPlugin
|
||||
class ScriptObject;
|
||||
class ScriptJob;
|
||||
|
||||
class DLLEXPORT ScriptPlugin : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
virtual ~ScriptPlugin() {}
|
||||
|
||||
ScriptJob* invoke( ScriptObject* scriptObject, const QString& methodName, const QVariantMap& arguments );
|
||||
virtual void startJob( ScriptJob* scriptJob ) = 0;
|
||||
void removeJob( ScriptJob* );
|
||||
|
||||
void reportScriptJobResult( const QVariantMap& result );
|
||||
void registerScriptPlugin( const QString& type, const QString& objectId );
|
||||
|
||||
|
||||
private: // TODO: pimple, might be renamed before tho
|
||||
QHash< QString, ScriptJob* > m_jobs;
|
||||
QHash< QString, ScriptObject* > m_objects;
|
||||
};
|
||||
|
||||
} // ns: Tomahawk
|
||||
|
@@ -19,7 +19,7 @@
|
||||
|
||||
|
||||
Tomahawk::SyncScriptJob::SyncScriptJob( const QVariantMap& resultData )
|
||||
: ScriptJob( nullptr, "nomethod")
|
||||
: ScriptJob( QString(), nullptr, QString() )
|
||||
{
|
||||
m_data = resultData;
|
||||
}
|
||||
|
Reference in New Issue
Block a user