1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-07 14:46:33 +02:00

Refactor the resolver system a bit so we have real objects instead of paths

This commit is contained in:
Leo Franchi
2011-09-09 18:16:18 -04:00
parent 6ec2fae320
commit dbb9d14deb
12 changed files with 237 additions and 141 deletions

View File

@@ -161,7 +161,8 @@ AtticaManager::payloadFetched()
if( !resolverPath.isEmpty() ) if( !resolverPath.isEmpty() )
{ {
TomahawkApp::instance()->enableScriptResolver( resolverPath ); // Do the install / add to tomahawk
TomahawkApp::instance()->addScriptResolver( resolverPath, true );
m_resolverStates[ resolverId ] = Installed; m_resolverStates[ resolverId ] = Installed;
TomahawkSettings::instance()->setAtticaResolverState( resolverId, Installed ); TomahawkSettings::instance()->setAtticaResolverState( resolverId, Installed );
emit resolverInstalled( resolverId ); emit resolverInstalled( resolverId );
@@ -243,34 +244,63 @@ AtticaManager::extractPayload( const QString& filename, const QString& resolverI
return QString( QFile( resolverDir.absolutePath() + "/contents/code/main.js" ).fileName() ); return QString( QFile( resolverDir.absolutePath() + "/contents/code/main.js" ).fileName() );
} }
void
AtticaManager::uninstallResolver( const QString& pathToResolver )
{
// User manually removed a resolver not through attica dialog, simple remove
QRegExp r( ".*([^/]*)/contents/code/main.js" );
r.indexIn( pathToResolver );
const QString& atticaId = r.cap( 1 );
tDebug() << "Got resolver ID to remove:" << atticaId;
if ( !atticaId.isEmpty() ) // this is an attica-installed resolver, mark as uninstalled
{
foreach ( const Content& resolver, m_resolvers )
{
if ( resolver.id() == atticaId ) // this is the one
{
m_resolverStates[ atticaId ] = Uninstalled;
TomahawkSettings::instance()->setAtticaResolverState( atticaId, Uninstalled );
doResolverRemove( atticaId );
}
}
}
}
void void
AtticaManager::uninstallResolver( const Content& resolver ) AtticaManager::uninstallResolver( const Content& resolver )
{ {
TomahawkApp::instance()->disableScriptResolver( resolver.id() );
m_resolverStates[ resolver.id() ] = Uninstalled;
TomahawkSettings::instance()->setAtticaResolverState( resolver.id(), Uninstalled );
emit resolverUninstalled( resolver.id() ); emit resolverUninstalled( resolver.id() );
emit resolverStateChanged( resolver.id() ); emit resolverStateChanged( resolver.id() );
TomahawkApp::instance()->removeScriptResolver( pathFromId( resolver.id() ) );
m_resolverStates[ resolver.id() ] = Uninstalled;
TomahawkSettings::instance()->setAtticaResolverState( resolver.id(), Uninstalled );
doResolverRemove( resolver.id() );
}
void
AtticaManager::doResolverRemove( const QString& id ) const
{
// uninstalling is easy... just delete it! :) // uninstalling is easy... just delete it! :)
QDir resolverDir = TomahawkUtils::appDataDir(); QDir resolverDir = TomahawkUtils::appDataDir();
if ( !resolverDir.cd( QString( "atticaresolvers/%1" ).arg( resolver.id() ) ) ) if ( !resolverDir.cd( QString( "atticaresolvers/%1" ).arg( id ) ) )
return; return;
if ( resolver.id().isEmpty() ) if ( id.isEmpty() )
return; return;
// sanity check // sanity check
if ( !resolverDir.absolutePath().contains( "atticaresolvers" ) || if ( !resolverDir.absolutePath().contains( "atticaresolvers" ) ||
!resolverDir.absolutePath().contains( resolver.id() ) ) !resolverDir.absolutePath().contains( id ) )
return; return;
removeDirectory( resolverDir.absolutePath() ); removeDirectory( resolverDir.absolutePath() );
} }
// taken from util/fileutils.cpp in kdevplatform // taken from util/fileutils.cpp in kdevplatform
bool bool
AtticaManager::removeDirectory( const QString& dir ) const AtticaManager::removeDirectory( const QString& dir ) const

View File

@@ -63,6 +63,7 @@ public:
void installResolver( const Attica::Content& resolver ); void installResolver( const Attica::Content& resolver );
void uninstallResolver( const Attica::Content& resolver ); void uninstallResolver( const Attica::Content& resolver );
void uninstallResolver( const QString& pathToResolver );
QString pathFromId( const QString& resolverId ) const; QString pathFromId( const QString& resolverId ) const;
signals: signals:
@@ -80,6 +81,7 @@ private slots:
private: private:
QString extractPayload( const QString& filename, const QString& resolverId ) const; QString extractPayload( const QString& filename, const QString& resolverId ) const;
void doResolverRemove( const QString& id ) const;
bool removeDirectory( const QString& dir ) const; bool removeDirectory( const QString& dir ) const;
Attica::ProviderManager m_manager; Attica::ProviderManager m_manager;

View File

@@ -54,6 +54,12 @@ public slots:
virtual void resolve( const Tomahawk::query_ptr& query ) = 0; virtual void resolve( const Tomahawk::query_ptr& query ) = 0;
}; };
/**
* Generic resolver object, used to manage a resolver that Tomahawk knows about
*
* You *must* start() a resolver after creating an ExternalResolver in order to use it,
* otherwise it will not do anything.
*/
class DLLEXPORT ExternalResolver : public Resolver class DLLEXPORT ExternalResolver : public Resolver
{ {
Q_OBJECT Q_OBJECT
@@ -73,8 +79,10 @@ public:
virtual void reload() {} // Reloads from file (especially useful to check if file now exists) virtual void reload() {} // Reloads from file (especially useful to check if file now exists)
virtual ErrorState error() const; virtual ErrorState error() const;
virtual bool running() const = 0;
public slots: public slots:
virtual void start() = 0;
virtual void stop() = 0; virtual void stop() = 0;
signals: signals:

View File

@@ -163,7 +163,7 @@ ScriptEngine::javaScriptConsoleMessage( const QString& message, int lineNumber,
QtScriptResolver::QtScriptResolver( const QString& scriptPath ) QtScriptResolver::QtScriptResolver( const QString& scriptPath )
: Tomahawk::ExternalResolver( scriptPath ) : Tomahawk::ExternalResolver( scriptPath )
, m_ready( false ) , m_ready( false )
, m_stopped( false ) , m_stopped( true )
, m_error( Tomahawk::ExternalResolver::NoError ) , m_error( Tomahawk::ExternalResolver::NoError )
, m_resolverHelper( new QtScriptResolverHelper( scriptPath, this ) ) , m_resolverHelper( new QtScriptResolverHelper( scriptPath, this ) )
{ {
@@ -179,7 +179,6 @@ QtScriptResolver::QtScriptResolver( const QString& scriptPath )
{ {
init(); init();
} }
} }
@@ -189,6 +188,11 @@ QtScriptResolver::~QtScriptResolver()
delete m_engine; delete m_engine;
} }
bool
QtScriptResolver::running() const
{
return m_ready && !m_stopped;
}
void void
QtScriptResolver::reload() QtScriptResolver::reload()
@@ -247,7 +251,16 @@ QtScriptResolver::init()
qDebug() << "JS" << filePath() << "READY," << "name" << m_name << "weight" << m_weight << "timeout" << m_timeout; qDebug() << "JS" << filePath() << "READY," << "name" << m_name << "weight" << m_weight << "timeout" << m_timeout;
m_ready = true; m_ready = true;
}
void
QtScriptResolver::start()
{
m_stopped = false;
if ( m_ready )
Tomahawk::Pipeline::instance()->addResolver( this ); Tomahawk::Pipeline::instance()->addResolver( this );
else
init();
} }
@@ -359,7 +372,8 @@ void
QtScriptResolver::stop() QtScriptResolver::stop()
{ {
m_stopped = true; m_stopped = true;
emit finished(); Tomahawk::Pipeline::instance()->removeResolver( this );
emit stopped();
} }

View File

@@ -128,14 +128,16 @@ public:
virtual void saveConfig(); virtual void saveConfig();
virtual ExternalResolver::ErrorState error() const; virtual ExternalResolver::ErrorState error() const;
virtual bool running() const;
virtual void reload(); virtual void reload();
public slots: public slots:
virtual void resolve( const Tomahawk::query_ptr& query ); virtual void resolve( const Tomahawk::query_ptr& query );
virtual void stop(); virtual void stop();
virtual void start();
signals: signals:
void finished(); void stopped();
private: private:
void init(); void init();

View File

@@ -36,10 +36,10 @@ ScriptResolver::ScriptResolver( const QString& exe )
, m_num_restarts( 0 ) , m_num_restarts( 0 )
, m_msgsize( 0 ) , m_msgsize( 0 )
, m_ready( false ) , m_ready( false )
, m_stopped( false ) , m_stopped( true )
, m_error( Tomahawk::ExternalResolver::NoError ) , m_error( Tomahawk::ExternalResolver::NoError )
{ {
tLog() << Q_FUNC_INFO << "Loading script resolver:" << exe; tLog() << Q_FUNC_INFO << "Created script resolver:" << exe;
connect( &m_proc, SIGNAL( readyReadStandardError() ), SLOT( readStderr() ) ); connect( &m_proc, SIGNAL( readyReadStandardError() ), SLOT( readStderr() ) );
connect( &m_proc, SIGNAL( readyReadStandardOutput() ), SLOT( readStdout() ) ); connect( &m_proc, SIGNAL( readyReadStandardOutput() ), SLOT( readStdout() ) );
connect( &m_proc, SIGNAL( finished( int, QProcess::ExitStatus ) ), SLOT( cmdExited( int, QProcess::ExitStatus ) ) ); connect( &m_proc, SIGNAL( finished( int, QProcess::ExitStatus ) ), SLOT( cmdExited( int, QProcess::ExitStatus ) ) );
@@ -73,6 +73,16 @@ ScriptResolver::~ScriptResolver()
delete m_configWidget.data(); delete m_configWidget.data();
} }
void
ScriptResolver::start()
{
m_stopped = false;
if ( m_ready )
Tomahawk::Pipeline::instance()->addResolver( this );
else
sendConfig();
}
void void
ScriptResolver::sendConfig() ScriptResolver::sendConfig()
@@ -116,6 +126,11 @@ ScriptResolver::reload()
} }
} }
bool
ScriptResolver::running() const
{
return !m_stopped;
}
void void
ScriptResolver::readStderr() ScriptResolver::readStderr()
@@ -123,7 +138,6 @@ ScriptResolver::readStderr()
tLog() << "SCRIPT_STDERR" << filePath() << m_proc.readAllStandardError(); tLog() << "SCRIPT_STDERR" << filePath() << m_proc.readAllStandardError();
} }
ScriptResolver::ErrorState ScriptResolver::ErrorState
ScriptResolver::error() const ScriptResolver::error() const
{ {
@@ -255,7 +269,7 @@ ScriptResolver::cmdExited( int code, QProcess::ExitStatus status )
if ( m_stopped ) if ( m_stopped )
{ {
tLog() << "*** Script resolver stopped "; tLog() << "*** Script resolver stopped ";
emit finished(); emit stopped();
return; return;
} }
@@ -310,6 +324,8 @@ ScriptResolver::doSetup( const QVariantMap& m )
qDebug() << "SCRIPT" << filePath() << "READY," << "name" << m_name << "weight" << m_weight << "timeout" << m_timeout; qDebug() << "SCRIPT" << filePath() << "READY," << "name" << m_name << "weight" << m_weight << "timeout" << m_timeout;
m_ready = true; m_ready = true;
if ( !m_stopped )
Tomahawk::Pipeline::instance()->addResolver( this ); Tomahawk::Pipeline::instance()->addResolver( this );
} }
@@ -363,5 +379,5 @@ void
ScriptResolver::stop() ScriptResolver::stop()
{ {
m_stopped = true; m_stopped = true;
m_proc.kill(); Tomahawk::Pipeline::instance()->removeResolver( this );
} }

View File

@@ -39,6 +39,7 @@ public:
explicit ScriptResolver( const QString& exe ); explicit ScriptResolver( const QString& exe );
virtual ~ScriptResolver(); virtual ~ScriptResolver();
virtual QString name() const { return m_name; } virtual QString name() const { return m_name; }
virtual unsigned int weight() const { return m_weight; } virtual unsigned int weight() const { return m_weight; }
virtual unsigned int preference() const { return m_preference; } virtual unsigned int preference() const { return m_preference; }
@@ -50,12 +51,14 @@ public:
virtual ExternalResolver::ErrorState error() const; virtual ExternalResolver::ErrorState error() const;
virtual void reload(); virtual void reload();
virtual bool running() const;
signals: signals:
void finished(); void stopped();
public slots: public slots:
virtual void stop(); virtual void stop();
virtual void resolve( const Tomahawk::query_ptr& query ); virtual void resolve( const Tomahawk::query_ptr& query );
virtual void start();
private slots: private slots:
void readStderr(); void readStderr();

View File

@@ -1,6 +1,6 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> === /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
* *
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org> * Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
* *
* Tomahawk is free software: you can redistribute it and/or modify * Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@@ -27,22 +27,9 @@
#include "utils/logger.h" #include "utils/logger.h"
ResolversModel::ResolversModel( const QStringList& allResolvers, const QStringList& enabledResolvers, QObject* parent ) ResolversModel::ResolversModel( QObject* parent )
: QAbstractListModel( parent ) : QAbstractListModel( parent )
, m_allResolvers( allResolvers )
, m_enabledResolvers( enabledResolvers )
{ {
// do some sanity checking just in case
bool changed = false;
foreach( const QString& l, m_enabledResolvers ) {
if( !m_allResolvers.contains( l ) ) {
m_enabledResolvers.removeAll( l );
changed = true;
}
}
if( changed )
TomahawkSettings::instance()->setEnabledScriptResolvers( m_enabledResolvers );
addInstalledResolvers(); addInstalledResolvers();
} }
@@ -55,33 +42,25 @@ ResolversModel::~ResolversModel()
QVariant QVariant
ResolversModel::data( const QModelIndex& index, int role ) const ResolversModel::data( const QModelIndex& index, int role ) const
{ {
if( !index.isValid() ) if( !index.isValid() || !hasIndex( index.row(), index.column(), QModelIndex() ) )
return QVariant(); return QVariant();
Tomahawk::ExternalResolver* res = TomahawkApp::instance()->scriptResolvers().at( index.row() );
switch( role ) switch( role )
{ {
case Qt::DisplayRole: case Qt::DisplayRole:
case ResolversModel::ResolverName: case ResolversModel::ResolverName:
{ return res->name();
QFileInfo info( m_allResolvers.at( index.row() ) );
return info.baseName();
}
case ResolversModel::ResolverPath: case ResolversModel::ResolverPath:
return m_allResolvers.at( index.row() ); return res->filePath();
case ResolversModel::HasConfig: case ResolversModel::HasConfig:
if( Tomahawk::ExternalResolver* r = TomahawkApp::instance()->resolverForPath( m_allResolvers.at( index.row() ) ) ) // if we have one, it means we are loaded too! return res->configUI() != 0;
return r->configUI() != 0;
return false;
case ResolversModel::ErrorState: case ResolversModel::ErrorState:
if( Tomahawk::ExternalResolver* r = TomahawkApp::instance()->resolverForPath( m_allResolvers.at( index.row() ) ) ) // if we have one, it means we are loaded too! return res->error();
return r->error();
else if( !QFile::exists( m_allResolvers.at( index.row() ) ) )
return Tomahawk::ExternalResolver::FileNotFound;
return Tomahawk::ExternalResolver::NoError;
case Qt::CheckStateRole: case Qt::CheckStateRole:
return m_enabledResolvers.contains( m_allResolvers.at( index.row() ) ) ? Qt::Checked : Qt::Unchecked; return res->running() ? Qt::Checked : Qt::Unchecked;
case Qt::ToolTipRole: case Qt::ToolTipRole:
return m_allResolvers.at( index.row() ); return res->filePath();
default: default:
return QVariant(); return QVariant();
} }
@@ -90,37 +69,35 @@ ResolversModel::data( const QModelIndex& index, int role ) const
bool bool
ResolversModel::setData( const QModelIndex& index, const QVariant& value, int role ) ResolversModel::setData( const QModelIndex& index, const QVariant& value, int role )
{ {
Tomahawk::ExternalResolver* r = TomahawkApp::instance()->resolverForPath( m_allResolvers.at( index.row() ) ); if ( !hasIndex( index.row(), index.column(), QModelIndex() ) )
return false;
Tomahawk::ExternalResolver* r = TomahawkApp::instance()->scriptResolvers().at( index.row() );
if ( r && r->error() == Tomahawk::ExternalResolver::FileNotFound ) // give it a shot to see if the user manually fixed paths if ( r && r->error() == Tomahawk::ExternalResolver::FileNotFound ) // give it a shot to see if the user manually fixed paths
{ {
r->reload(); r->reload();
if( r->error() == Tomahawk::ExternalResolver::FileNotFound ) // Nope, no luck. Doesn't exist on disk, don't let user mess with it if( r->error() == Tomahawk::ExternalResolver::FileNotFound ) // Nope, no luck. Doesn't exist on disk, don't let user mess with it
return false; return false;
} else if( !r && !QFile::exists( index.data( ResolverPath ).toString() ) ) { }
else if ( !r && !QFile::exists( r->filePath() ) )
{
return false; return false;
} }
if( role == Qt::CheckStateRole ) { if ( role == Qt::CheckStateRole )
{
Qt::CheckState state = static_cast< Qt::CheckState >( value.toInt() ); Qt::CheckState state = static_cast< Qt::CheckState >( value.toInt() );
QString resolver = m_allResolvers.at( index.row() );
if( state == Qt::Checked && !m_enabledResolvers.contains( resolver ) ) { if ( state == Qt::Checked && !r->running() ) {
m_enabledResolvers.append( resolver ); r->start();
}
else if ( state == Qt::Unchecked )
{
r->stop();
}
TomahawkApp::instance()->enableScriptResolver( resolver );
emit dataChanged( index, index ); emit dataChanged( index, index );
if( Tomahawk::ExternalResolver* res = TomahawkApp::instance()->resolverForPath( resolver ) ) {
connect( res, SIGNAL( changed() ), this, SLOT( resolverChanged() ) );
}
} else if( state == Qt::Unchecked ) {
m_enabledResolvers.removeAll( resolver );
TomahawkApp::instance()->disableScriptResolver( resolver );
}
dataChanged( index, index );
return true; return true;
} }
return false; return false;
@@ -131,7 +108,7 @@ int
ResolversModel::rowCount( const QModelIndex& parent ) const ResolversModel::rowCount( const QModelIndex& parent ) const
{ {
Q_UNUSED( parent ); Q_UNUSED( parent );
return m_allResolvers.size(); return APP->scriptResolvers().count();
} }
int int
@@ -151,52 +128,50 @@ ResolversModel::flags( const QModelIndex& index ) const
void void
ResolversModel::addResolver( const QString& resolver, bool enable ) ResolversModel::addResolver( const QString& resolver, bool enable )
{ {
beginInsertRows( QModelIndex(), m_allResolvers.count(), m_allResolvers.count() ); const int count = rowCount( QModelIndex() );
m_allResolvers << resolver; beginInsertRows( QModelIndex(), count, count );
if( enable ) Tomahawk::ExternalResolver* res = APP->addScriptResolver( resolver, enable );
m_enabledResolvers << resolver;
if( Tomahawk::ExternalResolver* res = TomahawkApp::instance()->resolverForPath( resolver ) ) {
qDebug() << "Added resolver with config and stuff:" << res->configUI();
connect( res, SIGNAL( changed() ), this, SLOT( resolverChanged() ) ); connect( res, SIGNAL( changed() ), this, SLOT( resolverChanged() ) );
} else
qDebug() << "No resolver object for path yet:" << resolver;
endInsertRows(); endInsertRows();
} }
void
ResolversModel::atticaResolverInstalled( const QString& resolverId )
{
Tomahawk::ExternalResolver* r = APP->resolverForPath( AtticaManager::instance()->pathFromId( resolverId ) );
if ( !r )
return;
const int idx = APP->scriptResolvers().indexOf( r );
if ( idx >= 0 )
{
beginInsertRows( QModelIndex(), idx, idx );
endInsertRows();
}
}
void void
ResolversModel::removeResolver( const QString& resolver ) ResolversModel::removeResolver( const QString& resolver )
{ {
for( int i = 0; i < m_allResolvers.count(); i++ ) { const int idx = APP->scriptResolvers().indexOf( APP->resolverForPath( resolver ) );
if( m_allResolvers.at( i ) == resolver ) { if ( idx < 0 )
beginRemoveRows( QModelIndex(), i, i ); return;
m_allResolvers.takeAt( i );
beginRemoveRows( QModelIndex(), idx, idx );
APP->removeScriptResolver( resolver );
endRemoveRows(); endRemoveRows();
} }
}
m_enabledResolvers.removeAll( resolver );
}
QStringList
ResolversModel::allResolvers() const
{
return m_allResolvers;
}
QStringList
ResolversModel::enabledResolvers() const
{
return m_enabledResolvers;
}
void void
ResolversModel::resolverChanged() ResolversModel::resolverChanged()
{ {
Q_ASSERT( qobject_cast< Tomahawk::ExternalResolver* >( sender() ) );
Tomahawk::ExternalResolver* res = qobject_cast< Tomahawk::ExternalResolver* >( sender() ); Tomahawk::ExternalResolver* res = qobject_cast< Tomahawk::ExternalResolver* >( sender() );
Q_ASSERT( res );
if ( APP->scriptResolvers().contains( res ) )
{
qDebug() << "Got resolverChanged signal, does it have a config UI yet?" << res->configUI(); qDebug() << "Got resolverChanged signal, does it have a config UI yet?" << res->configUI();
if( m_enabledResolvers.contains( res->filePath() ) ) { const QModelIndex idx = index( APP->scriptResolvers().indexOf( res ), 0, QModelIndex() );
QModelIndex idx = index( m_allResolvers.indexOf( res->filePath() ), 0, QModelIndex() );
emit dataChanged( idx, idx ); emit dataChanged( idx, idx );
} }
} }
@@ -220,10 +195,30 @@ ResolversModel::addInstalledResolvers()
foreach ( QString fileName, pluginDir.entryList( QStringList() << "*_tomahawkresolver*", QDir::Files ) ){ foreach ( QString fileName, pluginDir.entryList( QStringList() << "*_tomahawkresolver*", QDir::Files ) ){
if ( fileName.contains( "_tomahawkresolver" ) ) { if ( fileName.contains( "_tomahawkresolver" ) ) {
const QString path = pluginDir.absoluteFilePath( fileName ); const QString path = pluginDir.absoluteFilePath( fileName );
if( !m_allResolvers.contains( path ) ) { bool found = false;
m_allResolvers.append( path ); foreach ( Tomahawk::ExternalResolver* res, APP->scriptResolvers() )
{
if ( res->filePath() == path )
found = true;
}
if ( !found ) {
APP->addScriptResolver( path, false );
} }
} }
} }
} }
} }
void
ResolversModel::saveScriptResolvers()
{
QStringList enabled, all;
foreach ( Tomahawk::ExternalResolver* res, APP->scriptResolvers() )
{
all << res->filePath();
if ( res->running() )
enabled << res->filePath();
}
TomahawkSettings::instance()->setAllScriptResolvers( all );
TomahawkSettings::instance()->setEnabledScriptResolvers( enabled );
}

View File

@@ -1,6 +1,6 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> === /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
* *
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org> * Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
* *
* Tomahawk is free software: you can redistribute it and/or modify * Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@@ -35,7 +35,7 @@ public:
ErrorState = Qt::UserRole + 18 ErrorState = Qt::UserRole + 18
}; };
explicit ResolversModel( const QStringList& allResolvers, const QStringList& enabledResolvers, QObject* parent = 0 ); explicit ResolversModel( QObject* parent = 0 );
virtual ~ResolversModel(); virtual ~ResolversModel();
virtual QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const; virtual QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const;
@@ -45,19 +45,15 @@ public:
virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole); virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole);
void addResolver( const QString& resolver, bool enable = false ); void addResolver( const QString& resolver, bool enable = false );
void atticaResolverInstalled ( const QString& resolverId );
void removeResolver( const QString& resolver ); void removeResolver( const QString& resolver );
QStringList allResolvers() const; void saveScriptResolvers();
QStringList enabledResolvers() const;
private slots: private slots:
void resolverChanged(); void resolverChanged();
private: private:
void addInstalledResolvers(); void addInstalledResolvers();
QStringList m_allResolvers;
QStringList m_enabledResolvers;
}; };
#endif // RESOLVERSMODEL_H #endif // RESOLVERSMODEL_H

View File

@@ -1,6 +1,7 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> === /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
* *
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org> * Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
* *
* Tomahawk is free software: you can redistribute it and/or modify * Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@@ -188,7 +189,7 @@ SettingsDialog::SettingsDialog( QWidget *parent )
ResolverConfigDelegate* del = new ResolverConfigDelegate( this ); ResolverConfigDelegate* del = new ResolverConfigDelegate( this );
connect( del, SIGNAL( openConfig( QString ) ), this, SLOT( openResolverConfig( QString ) ) ); connect( del, SIGNAL( openConfig( QString ) ), this, SLOT( openResolverConfig( QString ) ) );
ui->scriptList->setItemDelegate( del ); ui->scriptList->setItemDelegate( del );
m_resolversModel = new ResolversModel( s->allScriptResolvers(), s->enabledScriptResolvers(), this ); m_resolversModel = new ResolversModel( this );
ui->scriptList->setModel( m_resolversModel ); ui->scriptList->setModel( m_resolversModel );
#ifdef LIBATTICA_FOUND #ifdef LIBATTICA_FOUND
@@ -239,8 +240,7 @@ SettingsDialog::~SettingsDialog()
s->setLastFmUsername( ui->lineEditLastfmUsername->text() ); s->setLastFmUsername( ui->lineEditLastfmUsername->text() );
s->setLastFmPassword( ui->lineEditLastfmPassword->text() ); s->setLastFmPassword( ui->lineEditLastfmPassword->text() );
s->setAllScriptResolvers( m_resolversModel->allResolvers() ); m_resolversModel->saveScriptResolvers();
s->setEnabledScriptResolvers( m_resolversModel->enabledResolvers() );
s->applyChanges(); s->applyChanges();
} }
@@ -527,8 +527,8 @@ SettingsDialog::addScriptResolver()
QString resolver = QFileDialog::getOpenFileName( this, tr( "Load script resolver file" ), TomahawkSettings::instance()->scriptDefaultPath() ); QString resolver = QFileDialog::getOpenFileName( this, tr( "Load script resolver file" ), TomahawkSettings::instance()->scriptDefaultPath() );
if( !resolver.isEmpty() ) if( !resolver.isEmpty() )
{ {
TomahawkApp::instance()->enableScriptResolver( resolver );
m_resolversModel->addResolver( resolver, true ); m_resolversModel->addResolver( resolver, true );
QFileInfo resolverAbsoluteFilePath = resolver; QFileInfo resolverAbsoluteFilePath = resolver;
TomahawkSettings::instance()->setScriptDefaultPath( resolverAbsoluteFilePath.absolutePath() ); TomahawkSettings::instance()->setScriptDefaultPath( resolverAbsoluteFilePath.absolutePath() );
} }
@@ -542,9 +542,8 @@ SettingsDialog::removeScriptResolver()
if( !ui->scriptList->selectionModel()->selectedIndexes().isEmpty() ) if( !ui->scriptList->selectionModel()->selectedIndexes().isEmpty() )
{ {
QString resolver = ui->scriptList->selectionModel()->selectedIndexes().first().data( ResolversModel::ResolverPath ).toString(); QString resolver = ui->scriptList->selectionModel()->selectedIndexes().first().data( ResolversModel::ResolverPath ).toString();
AtticaManager::instance()->uninstallResolver( resolver );
m_resolversModel->removeResolver( resolver ); m_resolversModel->removeResolver( resolver );
TomahawkApp::instance()->disableScriptResolver( resolver );
} }
} }
@@ -567,7 +566,7 @@ SettingsDialog::getMoreResolvers()
void void
SettingsDialog::atticaResolverInstalled( const QString& resolverId ) SettingsDialog::atticaResolverInstalled( const QString& resolverId )
{ {
m_resolversModel->addResolver( AtticaManager::instance()->pathFromId( resolverId ), true ); m_resolversModel->atticaResolverInstalled( resolverId );
} }
void void

View File

@@ -266,10 +266,7 @@ TomahawkApp::~TomahawkApp()
tLog() << "Shutting down Tomahawk..."; tLog() << "Shutting down Tomahawk...";
// stop script resolvers // stop script resolvers
foreach( Tomahawk::ExternalResolver* r, m_scriptResolvers.values() ) qDeleteAll( m_scriptResolvers );
{
delete r;
}
m_scriptResolvers.clear(); m_scriptResolvers.clear();
if ( !m_servent.isNull() ) if ( !m_servent.isNull() )
@@ -449,32 +446,58 @@ TomahawkApp::initPipeline()
Pipeline::instance()->addResolver( new DatabaseResolver( 100 ) ); Pipeline::instance()->addResolver( new DatabaseResolver( 100 ) );
// load script resolvers // load script resolvers
foreach( QString resolver, TomahawkSettings::instance()->enabledScriptResolvers() ) QStringList enabled = TomahawkSettings::instance()->enabledScriptResolvers();
enableScriptResolver( resolver ); foreach ( QString resolver, TomahawkSettings::instance()->allScriptResolvers() )
{
const bool enable = enabled.contains( resolver );
addScriptResolver( resolver, enable );
}
} }
void Tomahawk::ExternalResolver*
TomahawkApp::enableScriptResolver( const QString& path ) TomahawkApp::addScriptResolver( const QString& path, bool start )
{ {
const QFileInfo fi( path ); const QFileInfo fi( path );
ExternalResolver* res = 0;
if ( fi.suffix() == "js" || fi.suffix() == "script" ) if ( fi.suffix() == "js" || fi.suffix() == "script" )
m_scriptResolvers.insert( path, new QtScriptResolver( path ) ); res = new QtScriptResolver( path );
else else
m_scriptResolvers.insert( path, new ScriptResolver( path ) ); res = new ScriptResolver( path );
m_scriptResolvers << res;
if ( start )
res->start();
return res;
} }
void void
TomahawkApp::disableScriptResolver( const QString& path ) TomahawkApp::stopScriptResolver( const QString& path )
{ {
if ( m_scriptResolvers.contains( path ) ) foreach ( ExternalResolver* res, m_scriptResolvers )
{ {
Tomahawk::ExternalResolver* r = m_scriptResolvers.take( path ); if ( res->filePath() == path )
res->stop();
}
}
connect( r, SIGNAL( finished() ), r, SLOT( deleteLater() ) ); void
TomahawkApp::removeScriptResolver( const QString& scriptPath )
{
ExternalResolver* r = 0;
foreach ( ExternalResolver* res, m_scriptResolvers )
{
if ( res->filePath() == scriptPath )
r = res;
}
m_scriptResolvers.removeAll( r );
if ( r )
{
r->stop(); r->stop();
return; connect( r, SIGNAL( stopped() ), r, SLOT( deleteLater() ) );
} }
} }
@@ -482,7 +505,12 @@ TomahawkApp::disableScriptResolver( const QString& path )
Tomahawk::ExternalResolver* Tomahawk::ExternalResolver*
TomahawkApp::resolverForPath( const QString& scriptPath ) TomahawkApp::resolverForPath( const QString& scriptPath )
{ {
return m_scriptResolvers.value( scriptPath, 0 ); foreach ( ExternalResolver* res, m_scriptResolvers )
{
if ( res->filePath() == scriptPath )
return res;
}
return 0;
} }

View File

@@ -90,8 +90,11 @@ public:
TomahawkWindow* mainWindow() const { return m_mainwindow; } TomahawkWindow* mainWindow() const { return m_mainwindow; }
#endif #endif
void enableScriptResolver( const QString& scriptPath ); Tomahawk::ExternalResolver* addScriptResolver( const QString& scriptPath, bool start = true );
void disableScriptResolver( const QString& scriptPath ); void stopScriptResolver( const QString& scriptPath );
void removeScriptResolver( const QString& scriptPath );
QList< Tomahawk::ExternalResolver* > scriptResolvers() const { return m_scriptResolvers; }
Tomahawk::ExternalResolver* resolverForPath( const QString& scriptPath ); Tomahawk::ExternalResolver* resolverForPath( const QString& scriptPath );
// PlatformInterface // PlatformInterface
@@ -120,7 +123,7 @@ private:
void loadPlugins(); void loadPlugins();
QList<Tomahawk::collection_ptr> m_collections; QList<Tomahawk::collection_ptr> m_collections;
QHash<QString, Tomahawk::ExternalResolver*> m_scriptResolvers; QList<Tomahawk::ExternalResolver*> m_scriptResolvers;
QWeakPointer<Database> m_database; QWeakPointer<Database> m_database;
QWeakPointer<ScanManager> m_scanManager; QWeakPointer<ScanManager> m_scanManager;