mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-11 00:24:12 +02:00
Pass around scriptobject_ptr instead of ScriptObject*
This commit is contained in:
@@ -55,6 +55,7 @@ namespace Tomahawk
|
||||
class GeneratorInterface;
|
||||
class PeerInfo;
|
||||
class DatabaseCommand;
|
||||
class ScriptObject;
|
||||
|
||||
typedef QSharedPointer<Collection> collection_ptr;
|
||||
typedef QSharedPointer<Playlist> playlist_ptr;
|
||||
@@ -82,6 +83,11 @@ namespace Tomahawk
|
||||
typedef QSharedPointer<DynamicControl> dyncontrol_ptr;
|
||||
typedef QSharedPointer<GeneratorInterface> geninterface_ptr;
|
||||
|
||||
|
||||
// Scripting
|
||||
typedef QSharedPointer< ScriptObject > scriptobject_ptr;
|
||||
typedef QWeakPointer< ScriptObject > scriptobject_wptr;
|
||||
|
||||
// let's keep these typesafe, they are different kinds of GUID:
|
||||
typedef QString QID; //query id
|
||||
typedef QString RID; //result id
|
||||
|
@@ -53,7 +53,7 @@ JSAccount::setResolver( JSResolver* resolver )
|
||||
|
||||
|
||||
void
|
||||
JSAccount::scriptPluginFactory( const QString& type, ScriptObject* object )
|
||||
JSAccount::scriptPluginFactory( const QString& type, const scriptobject_ptr& object )
|
||||
{
|
||||
if ( type == "resolver" )
|
||||
{
|
||||
@@ -151,7 +151,7 @@ JSAccount::startJob( ScriptJob* scriptJob )
|
||||
|
||||
|
||||
const QVariant
|
||||
JSAccount::syncInvoke( ScriptObject* scriptObject, const QString& methodName, const QVariantMap& arguments )
|
||||
JSAccount::syncInvoke( const scriptobject_ptr& scriptObject, const QString& methodName, const QVariantMap& arguments )
|
||||
{
|
||||
QString eval = QString(
|
||||
"Tomahawk.PluginManager.invokeSync("
|
||||
|
@@ -45,7 +45,7 @@ public:
|
||||
JSAccount( const QString& name );
|
||||
|
||||
void startJob( ScriptJob* scriptJob ) override;
|
||||
const QVariant syncInvoke( ScriptObject* scriptObject, const QString& methodName, const QVariantMap& arguments ) override;
|
||||
const QVariant syncInvoke( const scriptobject_ptr& scriptObject, const QString& methodName, const QVariantMap& arguments ) override;
|
||||
|
||||
const QString name() const;
|
||||
|
||||
@@ -70,7 +70,7 @@ public:
|
||||
void addToJavaScriptWindowObject( const QString& name, QObject* object );
|
||||
|
||||
void setResolver( JSResolver* resolver );
|
||||
void scriptPluginFactory( const QString& type, ScriptObject* object ) override;
|
||||
void scriptPluginFactory( const QString& type, const scriptobject_ptr& object ) override;
|
||||
|
||||
static QString serializeQVariantMap(const QVariantMap& map);
|
||||
|
||||
|
@@ -59,7 +59,7 @@ using namespace Tomahawk;
|
||||
|
||||
JSResolver::JSResolver( const QString& accountId, const QString& scriptPath, const QStringList& additionalScriptPaths )
|
||||
: Tomahawk::ExternalResolverGui( scriptPath )
|
||||
, ScriptPlugin( nullptr )
|
||||
, ScriptPlugin( scriptobject_ptr() )
|
||||
, d_ptr( new JSResolverPrivate( this, accountId, scriptPath, additionalScriptPaths ) )
|
||||
{
|
||||
Q_D( JSResolver );
|
||||
|
@@ -20,6 +20,7 @@
|
||||
|
||||
#include "ScriptObject.h"
|
||||
#include "../utils/Logger.h"
|
||||
#include "../Typedefs.h"
|
||||
|
||||
// TODO: register factory methods instead of hardcoding all plugin types in here
|
||||
#include "../utils/LinkGenerator.h"
|
||||
@@ -46,7 +47,7 @@ requestIdGenerator()
|
||||
|
||||
|
||||
ScriptJob*
|
||||
ScriptAccount::invoke( ScriptObject* scriptObject, const QString& methodName, const QVariantMap& arguments )
|
||||
ScriptAccount::invoke( const scriptobject_ptr& scriptObject, const QString& methodName, const QVariantMap& arguments )
|
||||
{
|
||||
QString requestId = requestIdGenerator();
|
||||
|
||||
@@ -88,11 +89,12 @@ ScriptAccount::reportScriptJobResult( const QVariantMap& result )
|
||||
void
|
||||
ScriptAccount::registerScriptPlugin( const QString& type, const QString& objectId )
|
||||
{
|
||||
ScriptObject* object = m_objects.value( objectId );
|
||||
scriptobject_ptr object = m_objects.value( objectId );
|
||||
if( !object )
|
||||
{
|
||||
object = new ScriptObject( objectId, this );
|
||||
connect( object, SIGNAL( destroyed( QObject* ) ), SLOT( onScriptObjectDeleted( QObject* ) ) );
|
||||
object = scriptobject_ptr( new ScriptObject( objectId, this ), &ScriptObject::deleteLater );
|
||||
object->setWeakRef( object.toWeakRef() );
|
||||
connect( object.data(), SIGNAL( destroyed( QObject* ) ), SLOT( onScriptObjectDeleted() ) );
|
||||
m_objects.insert( objectId, object );
|
||||
}
|
||||
|
||||
@@ -101,14 +103,21 @@ ScriptAccount::registerScriptPlugin( const QString& type, const QString& objectI
|
||||
|
||||
|
||||
void
|
||||
ScriptAccount::onScriptObjectDeleted( QObject* scriptObject )
|
||||
ScriptAccount::onScriptObjectDeleted()
|
||||
{
|
||||
m_objects.remove( m_objects.key( static_cast< ScriptObject* >( scriptObject ) ) );
|
||||
foreach( const scriptobject_ptr& object, m_objects.values() )
|
||||
{
|
||||
if ( object.isNull() )
|
||||
{
|
||||
m_objects.remove( m_objects.key( object ) );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ScriptAccount::scriptPluginFactory( const QString& type, ScriptObject* object )
|
||||
ScriptAccount::scriptPluginFactory( const QString& type, const scriptobject_ptr& object )
|
||||
{
|
||||
if ( type == "linkGenerator" )
|
||||
{
|
||||
|
@@ -21,6 +21,8 @@
|
||||
#ifndef TOMAHAWK_SCRIPTACCOUNT_H
|
||||
#define TOMAHAWK_SCRIPTACCOUNT_H
|
||||
|
||||
#include "../Typedefs.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QVariantMap>
|
||||
|
||||
@@ -42,25 +44,25 @@ public:
|
||||
ScriptAccount( const QString& name );
|
||||
virtual ~ScriptAccount() {}
|
||||
|
||||
ScriptJob* invoke( ScriptObject* scriptObject, const QString& methodName, const QVariantMap& arguments );
|
||||
virtual const QVariant syncInvoke( ScriptObject* scriptObject, const QString& methodName, const QVariantMap& arguments ) = 0;
|
||||
ScriptJob* invoke( const scriptobject_ptr& scriptObject, const QString& methodName, const QVariantMap& arguments );
|
||||
virtual const QVariant syncInvoke( const scriptobject_ptr& scriptObject, const QString& methodName, const QVariantMap& arguments ) = 0;
|
||||
|
||||
virtual void startJob( ScriptJob* scriptJob ) = 0;
|
||||
|
||||
void reportScriptJobResult( const QVariantMap& result );
|
||||
void registerScriptPlugin( const QString& type, const QString& objectId );
|
||||
|
||||
virtual void scriptPluginFactory( const QString& type, ScriptObject* object );
|
||||
virtual void scriptPluginFactory( const QString& type, const scriptobject_ptr& object );
|
||||
|
||||
private slots:
|
||||
void onJobDeleted( const QString& jobId );
|
||||
|
||||
void onScriptObjectDeleted( QObject* scriptObject );
|
||||
void onScriptObjectDeleted();
|
||||
|
||||
private: // TODO: pimple, might be renamed before tho
|
||||
QString m_name;
|
||||
QHash< QString, ScriptJob* > m_jobs;
|
||||
QHash< QString, ScriptObject* > m_objects;
|
||||
QHash< QString, scriptobject_ptr > m_objects;
|
||||
};
|
||||
|
||||
} // ns: Tomahawk
|
||||
|
@@ -28,7 +28,7 @@
|
||||
|
||||
using namespace Tomahawk;
|
||||
|
||||
ScriptInfoPlugin::ScriptInfoPlugin( ScriptObject* scriptObject, const QString& name )
|
||||
ScriptInfoPlugin::ScriptInfoPlugin( const scriptobject_ptr& scriptObject, const QString& name )
|
||||
: d_ptr( new ScriptInfoPluginPrivate( this ) )
|
||||
, ScriptPlugin( scriptObject )
|
||||
{
|
||||
@@ -40,7 +40,7 @@ ScriptInfoPlugin::ScriptInfoPlugin( ScriptObject* scriptObject, const QString& n
|
||||
|
||||
setFriendlyName( QString( "ScriptInfoPlugin: %1" ).arg( name ) );
|
||||
|
||||
connect( scriptObject, SIGNAL( destroyed( QObject* ) ), SLOT( onScriptObjectDeleted() ) );
|
||||
connect( scriptObject.data(), SIGNAL( destroyed( QObject* ) ), SLOT( onScriptObjectDeleted() ) );
|
||||
}
|
||||
|
||||
|
||||
|
@@ -41,7 +41,7 @@ public:
|
||||
/**
|
||||
* @param id unique identifier to identify an infoplugin in its scope
|
||||
*/
|
||||
ScriptInfoPlugin( ScriptObject* scriptObject, const QString& name );
|
||||
ScriptInfoPlugin( const scriptobject_ptr& scriptObject, const QString& name );
|
||||
virtual ~ScriptInfoPlugin();
|
||||
|
||||
protected slots:
|
||||
|
@@ -24,8 +24,8 @@
|
||||
|
||||
using namespace Tomahawk;
|
||||
|
||||
ScriptJob::ScriptJob( const QString& id, ScriptObject* scriptObject, const QString& methodName, const QVariantMap& arguments )
|
||||
: QObject( scriptObject->thread() == QThread::currentThread() ? scriptObject : nullptr )
|
||||
ScriptJob::ScriptJob( const QString& id, const scriptobject_ptr& scriptObject, const QString& methodName, const QVariantMap& arguments )
|
||||
: QObject( scriptObject->thread() == QThread::currentThread() ? scriptObject.data() : nullptr )
|
||||
, m_error( false )
|
||||
, m_id( id )
|
||||
, m_scriptObject( scriptObject )
|
||||
@@ -54,7 +54,7 @@ ScriptJob::error() const
|
||||
}
|
||||
|
||||
|
||||
ScriptObject*
|
||||
const scriptobject_ptr
|
||||
ScriptJob::scriptObject() const
|
||||
{
|
||||
return m_scriptObject;
|
||||
|
@@ -19,6 +19,8 @@
|
||||
#ifndef TOMAHAWK_SCRIPTJOB_H
|
||||
#define TOMAHAWK_SCRIPTJOB_H
|
||||
|
||||
#include "../Typedefs.h"
|
||||
|
||||
#include <QVariantMap>
|
||||
#include <QObject>
|
||||
|
||||
@@ -34,7 +36,7 @@ class DLLEXPORT ScriptJob : public QObject
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ScriptJob( const QString& id, ScriptObject* scriptObject, const QString& methodName, const QVariantMap& arguments = QVariantMap() );
|
||||
ScriptJob( const QString& id, const scriptobject_ptr& scriptObject, const QString& methodName, const QVariantMap& arguments = QVariantMap() );
|
||||
virtual ~ScriptJob();
|
||||
|
||||
virtual void start();
|
||||
@@ -42,7 +44,7 @@ public:
|
||||
bool error() const;
|
||||
|
||||
const QString id() const;
|
||||
ScriptObject* scriptObject() const;
|
||||
const scriptobject_ptr scriptObject() const;
|
||||
const QString methodName() const;
|
||||
const QVariantMap arguments() const;
|
||||
|
||||
@@ -60,7 +62,7 @@ protected:
|
||||
// TODO: pimple
|
||||
bool m_error;
|
||||
QString m_id;
|
||||
ScriptObject* m_scriptObject;
|
||||
scriptobject_ptr m_scriptObject;
|
||||
QVariantMap m_data;
|
||||
QString m_methodName;
|
||||
QVariantMap m_arguments;
|
||||
|
@@ -24,8 +24,8 @@
|
||||
|
||||
using namespace Tomahawk;
|
||||
|
||||
ScriptLinkGeneratorPlugin::ScriptLinkGeneratorPlugin( ScriptObject* scriptObject )
|
||||
: QObject( scriptObject )
|
||||
ScriptLinkGeneratorPlugin::ScriptLinkGeneratorPlugin( const scriptobject_ptr& scriptObject )
|
||||
: QObject( scriptObject.data() )
|
||||
, ScriptPlugin( scriptObject )
|
||||
, Utils::LinkGeneratorPlugin()
|
||||
, d_ptr( new ScriptLinkGeneratorPluginPrivate( this, scriptObject ) )
|
||||
|
@@ -37,7 +37,7 @@ class DLLEXPORT ScriptLinkGeneratorPlugin : public QObject, public ScriptPlugin,
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ScriptLinkGeneratorPlugin( ScriptObject* scriptObject );
|
||||
ScriptLinkGeneratorPlugin( const scriptobject_ptr& scriptObject );
|
||||
virtual ~ScriptLinkGeneratorPlugin();
|
||||
|
||||
ScriptJob* openLink( const QString& title, const QString& artist, const QString& album ) const override;
|
||||
|
@@ -27,7 +27,7 @@ namespace Tomahawk
|
||||
class ScriptLinkGeneratorPluginPrivate
|
||||
{
|
||||
public:
|
||||
ScriptLinkGeneratorPluginPrivate( ScriptLinkGeneratorPlugin* q, ScriptObject* scriptObject )
|
||||
ScriptLinkGeneratorPluginPrivate( ScriptLinkGeneratorPlugin* q, const scriptobject_ptr& scriptObject )
|
||||
: q_ptr ( q )
|
||||
, scriptObject( scriptObject )
|
||||
{
|
||||
@@ -36,7 +36,7 @@ public:
|
||||
Q_DECLARE_PUBLIC ( ScriptLinkGeneratorPlugin )
|
||||
|
||||
private:
|
||||
ScriptObject* scriptObject;
|
||||
scriptobject_ptr scriptObject;
|
||||
};
|
||||
|
||||
} // ns: Tomahawk
|
||||
|
@@ -31,6 +31,24 @@ ScriptObject::ScriptObject( const QString& id, ScriptAccount* parent )
|
||||
|
||||
ScriptObject::~ScriptObject()
|
||||
{
|
||||
//TODO: Album clears the ownRef wptr explicitly ... why?
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ScriptObject::setWeakRef(const scriptobject_wptr& weakRef)
|
||||
{
|
||||
Q_D( ScriptObject );
|
||||
d->ownRef = weakRef;
|
||||
}
|
||||
|
||||
|
||||
const scriptobject_wptr
|
||||
ScriptObject::weakRef() const
|
||||
{
|
||||
Q_D( const ScriptObject );
|
||||
|
||||
return d->ownRef;
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +57,7 @@ ScriptObject::invoke( const QString& methodName, const QVariantMap& arguments )
|
||||
{
|
||||
Q_D( ScriptObject );
|
||||
|
||||
return d->scriptAccount->invoke( this, methodName, arguments );
|
||||
return d->scriptAccount->invoke( weakRef().toStrongRef(), methodName, arguments );
|
||||
}
|
||||
|
||||
|
||||
@@ -48,7 +66,7 @@ ScriptObject::syncInvoke(const QString& methodName, const QVariantMap& arguments
|
||||
{
|
||||
Q_D( ScriptObject );
|
||||
|
||||
return d->scriptAccount->syncInvoke( this, methodName, arguments );
|
||||
return d->scriptAccount->syncInvoke( weakRef().toStrongRef(), methodName, arguments );
|
||||
}
|
||||
|
||||
|
||||
|
@@ -19,6 +19,8 @@
|
||||
#ifndef TOMAHAWK_SCRIPTOBJECT_H
|
||||
#define TOMAHAWK_SCRIPTOBJECT_H
|
||||
|
||||
#include "../Typedefs.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QVariantMap>
|
||||
|
||||
@@ -40,6 +42,9 @@ public:
|
||||
ScriptObject( const QString& id, ScriptAccount* parent );
|
||||
virtual ~ScriptObject();
|
||||
|
||||
void setWeakRef( const scriptobject_wptr& weakRef );
|
||||
const scriptobject_wptr weakRef() const;
|
||||
|
||||
ScriptJob* invoke( const QString& methodName, const QVariantMap& arguments = QVariantMap() );
|
||||
|
||||
/**
|
||||
|
@@ -40,6 +40,7 @@ public:
|
||||
private:
|
||||
QString id;
|
||||
ScriptAccount* scriptAccount;
|
||||
scriptobject_wptr ownRef;
|
||||
};
|
||||
|
||||
} // ns: Tomahawk
|
||||
|
@@ -20,7 +20,7 @@
|
||||
|
||||
using namespace Tomahawk;
|
||||
|
||||
ScriptPlugin::ScriptPlugin( ScriptObject* object )
|
||||
ScriptPlugin::ScriptPlugin( const scriptobject_ptr& object )
|
||||
: m_scriptObject( object )
|
||||
{
|
||||
}
|
||||
@@ -32,7 +32,7 @@ ScriptPlugin::~ScriptPlugin()
|
||||
}
|
||||
|
||||
|
||||
ScriptObject*
|
||||
const scriptobject_ptr
|
||||
ScriptPlugin::scriptObject() const
|
||||
{
|
||||
return m_scriptObject;
|
||||
|
@@ -32,13 +32,13 @@ class ScriptObject;
|
||||
class DLLEXPORT ScriptPlugin
|
||||
{
|
||||
public:
|
||||
ScriptPlugin( ScriptObject* object );
|
||||
ScriptPlugin( const scriptobject_ptr& object );
|
||||
virtual ~ScriptPlugin();
|
||||
|
||||
ScriptObject* scriptObject() const;
|
||||
const scriptobject_ptr scriptObject() const;
|
||||
|
||||
protected: // TODO: pimple
|
||||
QPointer< ScriptObject > m_scriptObject;
|
||||
scriptobject_ptr m_scriptObject;
|
||||
|
||||
};
|
||||
|
||||
|
@@ -17,9 +17,11 @@
|
||||
*/
|
||||
#include "SyncScriptJob.h"
|
||||
|
||||
#include "../Typedefs.h"
|
||||
#include "ScriptObject.h"
|
||||
|
||||
Tomahawk::SyncScriptJob::SyncScriptJob( const QVariantMap& resultData )
|
||||
: ScriptJob( QString(), nullptr, QString() )
|
||||
: ScriptJob( QString(), scriptobject_ptr(), QString() )
|
||||
{
|
||||
m_data = resultData;
|
||||
}
|
||||
|
Reference in New Issue
Block a user