1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-01-18 23:17:59 +01:00

Create ScriptPluginFactory template and port ScripCollection

This commit is contained in:
Dominik Schmidt 2015-01-10 00:42:09 +01:00
parent 25ba94f183
commit 551949a07f
8 changed files with 297 additions and 62 deletions

View File

@ -346,12 +346,16 @@ list(APPEND libSources
resolvers/ExternalResolver.cpp
resolvers/Resolver.cpp
resolvers/ScriptCollection.cpp
resolvers/ScriptCommand_AllArtists.cpp
resolvers/ScriptCommand_AllAlbums.cpp
resolvers/ScriptCommand_AllTracks.cpp
resolvers/ScriptCommand_LookupUrl.cpp
resolvers/ScriptCommandQueue.cpp
resolvers/ScriptPluginFactory.cpp
# ScriptPlugins
resolvers/ScriptCollection.cpp
resolvers/plugins/ScriptCollectionFactory.cpp
sip/SipPlugin.cpp
sip/SipInfo.cpp

View File

@ -309,6 +309,8 @@ JSResolver::start()
Tomahawk::Pipeline::instance()->addResolver( this );
else
init();
scriptAccount()->start();
}

View File

@ -22,19 +22,18 @@
#include "../utils/Logger.h"
#include "../Typedefs.h"
#include "plugins/ScriptCollectionFactory.h"
// TODO: register factory methods instead of hardcoding all plugin types in here
#include "../utils/LinkGenerator.h"
#include "ScriptLinkGeneratorPlugin.h"
#include "ScriptInfoPlugin.h"
#include "SourceList.h"
#include "ScriptCollection.h"
// TODO:
#include "../Result.h"
#include "../Track.h"
#include <QTime>
#include <QFileInfo>
using namespace Tomahawk;
@ -42,17 +41,40 @@ using namespace Tomahawk;
ScriptAccount::ScriptAccount( const QString& name )
: QObject()
, m_name( name )
, m_stopped( false )
, m_collectionFactory( new ScriptCollectionFactory() )
{
}
ScriptAccount::~ScriptAccount()
{
delete m_collectionFactory;
}
void
ScriptAccount::start()
{
m_stopped = false;
m_collectionFactory->addAllPlugins();
}
void
ScriptAccount::stop()
{
foreach( const QWeakPointer< ScriptCollection >& collection, m_collections.hash().values() )
{
unregisterScriptPlugin( "collection", collection.data()->scriptObject()->id() );
}
m_stopped = true;
m_collectionFactory->removeAllPlugins();
}
bool
ScriptAccount::isStopped()
{
return m_stopped;
}
@ -169,11 +191,7 @@ ScriptAccount::unregisterScriptPlugin( const QString& type, const QString& objec
if ( type == "collection" )
{
collection_ptr collection = scriptCollection( objectId );
if ( !collection.isNull() )
{
SourceList::instance()->removeScriptCollection( collection );
}
m_collectionFactory->unregisterPlugin( object );
}
else
{
@ -220,51 +238,7 @@ ScriptAccount::scriptPluginFactory( const QString& type, const scriptobject_ptr&
}
else if( type == "collection" )
{
if ( !scriptCollection( object->id() ).isNull() )
return;
const QVariantMap collectionInfo = object->syncInvoke( "collection" ).toMap();
if ( collectionInfo.isEmpty() ||
!collectionInfo.contains( "prettyname" ) ||
!collectionInfo.contains( "description" ) )
return;
const QString prettyname = collectionInfo.value( "prettyname" ).toString();
const QString desc = collectionInfo.value( "description" ).toString();
// at this point we assume that all the tracks browsable through a resolver belong to the local source
Tomahawk::ScriptCollection* sc = new Tomahawk::ScriptCollection( object, SourceList::instance()->getLocal(), this );
QSharedPointer<ScriptCollection> collection( sc );
collection->setWeakRef( collection.toWeakRef() );
sc->setServiceName( prettyname );
sc->setDescription( desc );
if ( collectionInfo.contains( "trackcount" ) ) //a resolver might not expose this
{
bool ok = false;
int trackCount = collectionInfo.value( "trackcount" ).toInt( &ok );
if ( ok )
sc->setTrackCount( trackCount );
}
if ( collectionInfo.contains( "iconfile" ) )
{
QString iconPath = QFileInfo( filePath() ).path() + "/"
+ collectionInfo.value( "iconfile" ).toString();
QPixmap iconPixmap;
bool ok = iconPixmap.load( iconPath );
if ( ok && !iconPixmap.isNull() )
sc->setIcon( iconPixmap );
}
SourceList::instance()->addScriptCollection( collection );
sc->fetchIcon( collectionInfo.value( "iconurl" ).toString() );
m_collections.insert( object->id(), collection );
m_collectionFactory->registerPlugin( object, this );
}
else
{
@ -366,5 +340,5 @@ ScriptAccount::parseResultVariantList( const QVariantList& reslist )
const QSharedPointer< ScriptCollection >
ScriptAccount::scriptCollection( const QString& id ) const
{
return m_collections.hash().value( id );
return m_collectionFactory->scriptPlugins().value( id );
}

View File

@ -32,13 +32,13 @@
#include <QHash>
#include <QPixmap>
#include "../DllMacro.h"
namespace Tomahawk {
class ScriptObject;
class ScriptJob;
class ScriptCollectionFactory;
class DLLEXPORT ScriptAccount : public QObject
{
@ -46,10 +46,13 @@ class DLLEXPORT ScriptAccount : public QObject
public:
ScriptAccount( const QString& name );
virtual ~ScriptAccount() {}
virtual ~ScriptAccount();
void start();
void stop();
bool isStopped();
const QString name() const;
void setIcon( const QPixmap& icon );
@ -82,9 +85,11 @@ private: // TODO: pimple, might be renamed before tho
QString m_name;
QPixmap m_icon;
QString m_filePath;
bool m_stopped;
QHash< QString, ScriptJob* > m_jobs;
QHash< QString, scriptobject_ptr > m_objects;
Utils::WeakObjectHash< ScriptCollection > m_collections;
ScriptCollectionFactory* m_collectionFactory; // port to QScopedPointer when pimple'd
};
} // ns: Tomahawk

View File

@ -0,0 +1,18 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright (C) 2015 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 "ScriptPluginFactory.h"

View File

@ -0,0 +1,109 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright (C) 2015 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/>.
*/
#pragma once
#ifndef TOMAHAWK_SCRIPTPLUGINFACTORY_H
#define TOMAHAWK_SCRIPTPLUGINFACTORY_H
#include "../Typedefs.h"
#include "ScriptPlugin.h"
#include "ScriptAccount.h"
namespace Tomahawk
{
class ScriptAccount;
class ScriptCollection;
template <class T>
class ScriptPluginFactory
{
public:
void registerPlugin( const scriptobject_ptr& object, ScriptAccount* scriptAccount )
{
if ( !m_scriptPlugins.value( object->id() ).isNull() )
return;
QSharedPointer< T > scriptPlugin = createPlugin( object, scriptAccount );
if ( !scriptPlugin.isNull() )
{
m_scriptPlugins.insert( object->id(), scriptPlugin );
if( !scriptAccount->isStopped() )
{
addPlugin( scriptPlugin );
}
}
}
void unregisterPlugin( const scriptobject_ptr& object )
{
QSharedPointer< T > scriptPlugin = m_scriptPlugins.value( object->id() );
if ( !scriptPlugin.isNull() )
{
removePlugin( scriptPlugin );
}
}
virtual const QSharedPointer<T> createPlugin( const scriptobject_ptr&, ScriptAccount* )
{
return QSharedPointer<T>();
}
void addAllPlugins() const
{
foreach( const QWeakPointer< T >& scriptPlugin, m_scriptPlugins.values() )
{
addPlugin( scriptPlugin );
}
}
virtual void addPlugin( const QSharedPointer< T >& scriptPlugin ) const
{
}
void removeAllPlugins() const
{
foreach( const QWeakPointer< T >& scriptPlugin, m_scriptPlugins.values() )
{
removePlugin( scriptPlugin );
}
}
virtual void removePlugin( const QSharedPointer<T>& scriptPlugin ) const
{
}
const QHash< QString, QSharedPointer< T > > scriptPlugins() const
{
return m_scriptPlugins;
}
private:
QHash< QString, QSharedPointer< T > > m_scriptPlugins;
};
} // ns: Tomahawk
#endif // TOMAHAWK_SCRIPTPLUGINFACTORY_H

View File

@ -0,0 +1,80 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright (C) 2015 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 "ScriptCollectionFactory.h"
#include "SourceList.h"
#include "../ScriptAccount.h"
#include <QFileInfo>
using namespace Tomahawk;
void ScriptCollectionFactory::addPlugin( const QSharedPointer<ScriptCollection>& collection ) const
{
SourceList::instance()->addScriptCollection( collection );
}
void ScriptCollectionFactory::removePlugin( const QSharedPointer<ScriptCollection>& collection ) const
{
SourceList::instance()->removeScriptCollection( collection );
}
const QSharedPointer< ScriptCollection > ScriptCollectionFactory::createPlugin( const scriptobject_ptr& object, ScriptAccount* scriptAccount )
{
const QVariantMap collectionInfo = object->syncInvoke( "collection" ).toMap();
if ( collectionInfo.isEmpty() ||
!collectionInfo.contains( "prettyname" ) ||
!collectionInfo.contains( "description" ) )
return QSharedPointer< ScriptCollection >();
const QString prettyname = collectionInfo.value( "prettyname" ).toString();
const QString desc = collectionInfo.value( "description" ).toString();
// at this point we assume that all the tracks browsable through a resolver belong to the local source
Tomahawk::ScriptCollection* sc = new Tomahawk::ScriptCollection( object, SourceList::instance()->getLocal(), scriptAccount );
QSharedPointer<ScriptCollection> collection( sc );
collection->setWeakRef( collection.toWeakRef() );
sc->setServiceName( prettyname );
sc->setDescription( desc );
if ( collectionInfo.contains( "trackcount" ) ) //a resolver might not expose this
{
bool ok = false;
int trackCount = collectionInfo.value( "trackcount" ).toInt( &ok );
if ( ok )
sc->setTrackCount( trackCount );
}
if ( collectionInfo.contains( "iconfile" ) )
{
QString iconPath = QFileInfo( scriptAccount->filePath() ).path() + "/"
+ collectionInfo.value( "iconfile" ).toString();
QPixmap iconPixmap;
bool ok = iconPixmap.load( iconPath );
if ( ok && !iconPixmap.isNull() )
sc->setIcon( iconPixmap );
}
sc->fetchIcon( collectionInfo.value( "iconurl" ).toString() );
return collection;
}

View File

@ -0,0 +1,43 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright (C) 2015 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/>.
*/
#pragma once
#ifndef TOMAHAWK_SCRIPTCOLLECTIONFACTORY_H
#define TOMAHAWK_SCRIPTCOLLECTIONFACTORY_H
#include "Typedefs.h"
#include "../ScriptPluginFactory.h"
#include "../ScriptCollection.h"
namespace Tomahawk
{
class ScriptAccount;
class ScriptCollection;
class DLLEXPORT ScriptCollectionFactory : public ScriptPluginFactory< ScriptCollection >
{
const QSharedPointer< ScriptCollection > createPlugin( const scriptobject_ptr&, ScriptAccount* ) override;
void addPlugin( const QSharedPointer< ScriptCollection >& scriptPlugin ) const override;
void removePlugin( const QSharedPointer< ScriptCollection >& scriptPlugin ) const override;
};
} // ns: Tomahawk
#endif // TOMAHAWK_SCRIPTCOLLECTIONFACTORY_H