1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-21 00:09:47 +01:00

Experimenting with ScriptCommands, added ScriptCommand_AllArtists.

This commit is contained in:
Teo Mrnjavac 2013-02-01 19:53:41 +01:00
parent 0e566bc300
commit 1c3941c16b
16 changed files with 373 additions and 48 deletions

View File

@ -89,7 +89,6 @@ set( libGuiSources
resolvers/ExternalResolverGui.cpp
resolvers/ScriptResolver.cpp
resolvers/QtScriptResolver.cpp
resolvers/ScriptCollection.cpp
utils/ImageRegistry.cpp
utils/WidgetDragFilter.cpp
@ -301,8 +300,11 @@ list(APPEND libSources
playlist/dynamic/database/DatabaseControl.cpp
playlist/dynamic/DynamicControl.cpp
resolvers/Resolver.cpp
resolvers/ExternalResolver.cpp
resolvers/Resolver.cpp
resolvers/ScriptCollection.cpp
resolvers/ScriptCommand_AllArtists.cpp
resolvers/ScriptCommandQueue.cpp
sip/SipPlugin.cpp
sip/SipInfo.cpp

View File

@ -0,0 +1,43 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2013, Teo Mrnjavac <teo@kde.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 ARTISTSREQUEST_H
#define ARTISTSREQUEST_H
#include "Typedefs.h"
#include "DllMacro.h"
#include <QList>
namespace Tomahawk
{
class DLLEXPORT ArtistsRequest
{
public:
virtual ~ArtistsRequest() {}
virtual void enqueue() = 0;
protected: //signals
virtual void artists( const QList< Tomahawk::artist_ptr >& ) = 0;
};
} //ns
#endif // ARTISTSREQUEST_H

View File

@ -34,6 +34,7 @@
#include "Typedefs.h"
#include "Playlist.h"
#include "playlist/dynamic/DynamicPlaylist.h"
#include "collection/ArtistsRequest.h"
#include "DllMacro.h"
@ -80,7 +81,7 @@ public:
virtual QList< Tomahawk::dynplaylist_ptr > stations() { return m_stations.values(); }
// Async requests. Emit artists/albums/tracksResult in subclasses when finished.
virtual void artists() = 0;
virtual Tomahawk::ArtistsRequest* requestArtists() = 0;
virtual void albums( const Tomahawk::artist_ptr& artist ) = 0;
virtual void tracks( const Tomahawk::album_ptr& album ) = 0;
@ -88,7 +89,6 @@ public:
unsigned int lastmodified() const { return m_lastmodified; }
signals:
void artistsResult( const QList< Tomahawk::artist_ptr >& artists );
void albumsResult( const QList< Tomahawk::album_ptr >& albums );
void tracksResult( const QList< Tomahawk::query_ptr >& queries );

View File

@ -133,27 +133,17 @@ DatabaseCollection::stations()
}
void
DatabaseCollection::artists()
Tomahawk::ArtistsRequest*
DatabaseCollection::requestArtists()
{
//FIXME: assuming there's only one dbcollection per source, and that this is the one
Tomahawk::collection_ptr thisCollection = source()->dbCollection();
if ( thisCollection->name() != this->name() )
return;
return 0;
DatabaseCommand_AllArtists* cmd = new DatabaseCommand_AllArtists( thisCollection );
Tomahawk::ArtistsRequest* cmd = new DatabaseCommand_AllArtists( thisCollection );
connect( cmd, SIGNAL( artists( QList< Tomahawk::artist_ptr > ) ),
SLOT( onArtistsFetched( QList< Tomahawk::artist_ptr > ) ) );
Database::instance()->enqueue( QSharedPointer<DatabaseCommand>( cmd ) );
}
void
DatabaseCollection::onArtistsFetched( const QList< Tomahawk::artist_ptr >& artists )
{
emit artistsResult( artists );
return cmd;
}

View File

@ -50,7 +50,7 @@ public:
virtual QList< Tomahawk::dynplaylist_ptr > autoPlaylists();
virtual QList< Tomahawk::dynplaylist_ptr > stations();
virtual void artists();
virtual Tomahawk::ArtistsRequest* requestArtists();
virtual void albums( const Tomahawk::artist_ptr& artist );
virtual void tracks( const Tomahawk::album_ptr& album );
@ -59,7 +59,6 @@ public slots:
virtual void removeTracks( const QDir& dir );
private slots:
void onArtistsFetched( const QList< Tomahawk::artist_ptr >& artists );
void onAlbumsFetched( const QList< Tomahawk::album_ptr >& albums, const QVariant& data );
void onTracksFetched( const QList< Tomahawk::query_ptr >& tracks );

View File

@ -23,13 +23,16 @@
#include <QVariantMap>
#include "Artist.h"
#include "collection/ArtistsRequest.h"
#include "collection/Collection.h"
#include "Typedefs.h"
#include "DatabaseCommand.h"
#include "Database.h"
#include "DllMacro.h"
class DLLEXPORT DatabaseCommand_AllArtists : public DatabaseCommand
, public Tomahawk::ArtistsRequest
{
Q_OBJECT
public:
@ -47,6 +50,8 @@ public:
virtual bool doesMutates() const { return false; }
virtual QString commandname() const { return "allartists"; }
virtual void enqueue() { Database::instance()->enqueue( QSharedPointer<DatabaseCommand>( this ) ); }
void setLimit( unsigned int amount ) { m_amount = amount; }
void setSortOrder( DatabaseCommand_AllArtists::SortOrder order ) { m_sortOrder = order; }
void setSortDescending( bool descending ) { m_sortDescending = descending; }

View File

@ -253,9 +253,10 @@ TreeModel::addCollection( const collection_ptr& collection )
m_collection = collection;
connect( m_collection.data(), SIGNAL( artistsResult( QList<Tomahawk::artist_ptr> ) ),
SLOT( onArtistsAdded( QList<Tomahawk::artist_ptr> ) ), Qt::UniqueConnection );
m_collection->artists();
Tomahawk::ArtistsRequest* req = m_collection->requestArtists();
connect( dynamic_cast< QObject* >( req ), SIGNAL( artists( QList< Tomahawk::artist_ptr > ) ),
this, SLOT( onArtistsAdded( QList< Tomahawk::artist_ptr > ) ), Qt::UniqueConnection );
req->enqueue();
connect( collection.data(), SIGNAL( changed() ), SLOT( onCollectionChanged() ), Qt::UniqueConnection );

View File

@ -25,6 +25,8 @@
#include "Query.h"
#include "DllMacro.h"
#include "Resolver.h"
#include "ScriptCommandQueue.h"
#include "ScriptCommand_AllArtists.h"
#include <boost/function.hpp>
@ -45,6 +47,9 @@ class DLLEXPORT ExternalResolver : public Resolver
{
Q_OBJECT
friend class ::ScriptCommandQueue;
friend class ::ScriptCommand_AllArtists;
public:
enum ErrorState {
NoError,
@ -62,7 +67,9 @@ public:
Q_DECLARE_FLAGS( Capabilities, Capability )
Q_FLAGS( Capabilities )
ExternalResolver( const QString& filePath ) { m_filePath = filePath; }
ExternalResolver( const QString& filePath )
: m_commandQueue( new ScriptCommandQueue( this ) )
{ m_filePath = filePath; }
virtual QString filePath() const { return m_filePath; }
@ -74,23 +81,31 @@ public:
virtual Capabilities capabilities() const = 0;
virtual QMap< QString, Tomahawk::collection_ptr > collections() { return m_collections; }
virtual void enqueue( const QSharedPointer< ScriptCommand >& req )
{ m_commandQueue->enqueue( req ); }
public slots:
virtual void start() = 0;
virtual void stop() = 0;
// For ScriptCollection
virtual void artists( const Tomahawk::collection_ptr& collection ) = 0;
virtual void albums( const Tomahawk::collection_ptr& collection, const Tomahawk::artist_ptr& artist ) = 0;
virtual void tracks( const Tomahawk::collection_ptr& collection, const Tomahawk::album_ptr& album ) = 0;
signals:
void changed(); // if config widget was added/removed, name changed, etc
void collectionAdded( const Tomahawk::collection_ptr& collection );
void collectionRemoved( const Tomahawk::collection_ptr& collection );
void artistsFound( const QList< Tomahawk::artist_ptr >& );
void albumsFound( const QList< Tomahawk::album_ptr >& );
void tracksFound( const QList< Tomahawk::query_ptr >& );
protected:
void setFilePath( const QString& path ) { m_filePath = path; }
QMap< QString, Tomahawk::collection_ptr > m_collections;
ScriptCommandQueue* m_commandQueue;
// Should only be called by ScriptCommands
virtual void artists( const Tomahawk::collection_ptr& collection ) = 0;
virtual void albums( const Tomahawk::collection_ptr& collection, const Tomahawk::artist_ptr& artist ) = 0;
virtual void tracks( const Tomahawk::collection_ptr& collection, const Tomahawk::album_ptr& album ) = 0;
private:
QString m_filePath;

View File

@ -156,8 +156,8 @@ QtScriptResolverHelper::addArtistResults( const QVariantMap& results )
tDebug() << Q_FUNC_INFO << "about to push" << artists.count() << "artists";
foreach( const Tomahawk::artist_ptr& artist, artists)
tDebug() << artist->name();
QMetaObject::invokeMethod( collection.data(), "onArtistsFetched", Qt::QueuedConnection,
Q_ARG( QList< Tomahawk::artist_ptr >, artists ) );
emit m_resolver->artistsFound( artists );
}
@ -502,8 +502,7 @@ QtScriptResolver::artists( const Tomahawk::collection_ptr& collection )
if ( !m_collections.contains( collection->name() ) || //if the collection doesn't belong to this resolver
!capabilities().testFlag( Browsable ) ) //or this resolver doesn't even support collections
{
QMetaObject::invokeMethod( collection.data(), "onArtistsFetched", Qt::QueuedConnection,
Q_ARG( QList< Tomahawk::artist_ptr >, QList< Tomahawk::artist_ptr >() ) );
emit artistsFound( QList< Tomahawk::artist_ptr >() );
return;
}

View File

@ -23,6 +23,7 @@
#include "ExternalResolverGui.h"
#include "utils/TomahawkUtilsGui.h"
#include "utils/Logger.h"
#include "resolvers/ScriptCommand_AllArtists.h"
#include <QIcon>
#include <QPainter>
@ -108,31 +109,30 @@ ScriptCollection::bigIcon() const
}
void
ScriptCollection::artists()
Tomahawk::ArtistsRequest*
ScriptCollection::requestArtists()
{
m_resolver->artists( m_resolver->collections().value( name() ) );
Tomahawk::collection_ptr thisCollection = m_resolver->collections().value( name() );
if ( thisCollection->name() != this->name() )
return 0;
Tomahawk::ArtistsRequest* cmd = new ScriptCommand_AllArtists( thisCollection );
return cmd;
}
void
ScriptCollection::albums( const Tomahawk::artist_ptr& artist )
{
m_resolver->albums( m_resolver->collections().value( name() ), artist );
//m_resolver->albums( m_resolver->collections().value( name() ), artist );
}
void
ScriptCollection::tracks( const Tomahawk::album_ptr& album )
{
m_resolver->tracks( m_resolver->collections().value( name() ), album );
}
void
ScriptCollection::onArtistsFetched( const QList<artist_ptr>& artists )
{
emit artistsResult( artists );
//m_resolver->tracks( m_resolver->collections().value( name() ), album );
}

View File

@ -22,6 +22,7 @@
#include "ExternalResolver.h"
#include "collection/Collection.h"
#include "collection/ArtistsRequest.h"
#include "Typedefs.h"
#include "DllMacro.h"
@ -48,12 +49,11 @@ public:
virtual ExternalResolver* resolver() { return m_resolver; }
virtual void artists();
virtual Tomahawk::ArtistsRequest* requestArtists();
virtual void albums( const Tomahawk::artist_ptr& artist );
virtual void tracks( const Tomahawk::album_ptr& album );
private slots:
void onArtistsFetched( const QList< Tomahawk::artist_ptr >& artists );
void onAlbumsFetched( const QList< Tomahawk::album_ptr >& albums );
void onTracksFetched( const QList< Tomahawk::query_ptr >& tracks );

View File

@ -0,0 +1,38 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2013, Teo Mrnjavac <teo@kde.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 SCRIPTCOMMAND_H
#define SCRIPTCOMMAND_H
#include <QObject>
class ScriptCommand : public QObject
{
public:
explicit ScriptCommand( QObject* parent = 0 ) : QObject( parent ) {}
virtual ~ScriptCommand() {}
signals:
virtual void done() = 0;
protected:
friend class ScriptCommandQueue;
virtual void exec() = 0;
};
#endif // SCRIPTCOMMAND_H

View File

@ -0,0 +1,65 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2013, Teo Mrnjavac <teo@kde.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 "ScriptCommandQueue.h"
#include "utils/Closure.h"
#include <QMetaType>
ScriptCommandQueue::ScriptCommandQueue( QObject* parent )
: QObject( parent )
, m_timer( new QTimer( this ) )
{
m_timer->setSingleShot( true );
connect( m_timer, SIGNAL( timeout() ), SLOT( onTimeout() ) );
}
void
ScriptCommandQueue::enqueue( const QSharedPointer< ScriptCommand >& req )
{
m_queue.append( req );
if ( m_queue.count() == 1 )
nextCommand();
}
void
ScriptCommandQueue::nextCommand()
{
if ( m_queue.isEmpty() )
return;
QSharedPointer< ScriptCommand > req = m_queue.first();
NewClosure( req.data(), SIGNAL( done() ),
this, SLOT( onCommandDone( QSharedPointer< ScriptCommand > ) ), req );
req->exec();
}
void
ScriptCommandQueue::onCommandDone( const QSharedPointer< ScriptCommand >& req )
{
m_queue.removeAll( req );
req->deleteLater();
if ( m_queue.count() > 0 )
nextCommand();
}

View File

@ -0,0 +1,48 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2013, Teo Mrnjavac <teo@kde.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 SCRIPTCOMMANDQUEUE_H
#define SCRIPTCOMMANDQUEUE_H
#include "ScriptCommand.h"
#include <QQueue>
#include <QSharedPointer>
#include <QTimer>
#include <QMetaType>
class ScriptCommandQueue : public QObject
{
Q_OBJECT
public:
explicit ScriptCommandQueue( QObject* parent = 0 );
void enqueue( const QSharedPointer< ScriptCommand >& req );
private slots:
void nextCommand();
void onCommandDone( const QSharedPointer< ScriptCommand >& req );
private:
QQueue< QSharedPointer< ScriptCommand > > m_queue;
QTimer* m_timer;
};
Q_DECLARE_METATYPE( QSharedPointer< ScriptCommand > )
#endif // SCRIPTCOMMANDQUEUE_H

View File

@ -0,0 +1,67 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2013, Teo Mrnjavac <teo@kde.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 "ScriptCommand_AllArtists.h"
#include "ExternalResolver.h"
#include "ScriptCollection.h"
ScriptCommand_AllArtists::ScriptCommand_AllArtists( const Tomahawk::collection_ptr& collection,
QObject* parent )
: ScriptCommand( parent )
, m_collection( collection )
{
}
void
ScriptCommand_AllArtists::enqueue()
{
Tomahawk::ScriptCollection* collection = qobject_cast< Tomahawk::ScriptCollection* >( m_collection.data() );
if ( collection == 0 )
{
emit artists( QList< Tomahawk::artist_ptr >() );
return;
}
collection->resolver()->enqueue( QSharedPointer< ScriptCommand >( this ) );
}
void
ScriptCommand_AllArtists::exec()
{
Tomahawk::ScriptCollection* collection = qobject_cast< Tomahawk::ScriptCollection* >( m_collection.data() );
if ( collection == 0 )
{
emit artists( QList< Tomahawk::artist_ptr >() );
return;
}
connect( collection->resolver(), SIGNAL( artistsFound( QList< Tomahawk::artist_ptr > ) ),
this, SLOT( onResolverDone( QList< Tomahawk::artist_ptr > ) ) );
collection->resolver()->artists( m_collection );
}
void ScriptCommand_AllArtists::onResolverDone( const QList< Tomahawk::artist_ptr >& a )
{
emit artists( a );
emit done();
}

View File

@ -0,0 +1,53 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2013, Teo Mrnjavac <teo@kde.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 SCRIPTCOMMAND_ALLARTISTS_H
#define SCRIPTCOMMAND_ALLARTISTS_H
#include "collection/ArtistsRequest.h"
#include "collection/Collection.h"
#include "resolvers/ScriptCommand.h"
#include <QObject>
class ScriptCommand_AllArtists : public ScriptCommand
, public Tomahawk::ArtistsRequest
{
Q_OBJECT
public:
explicit ScriptCommand_AllArtists( const Tomahawk::collection_ptr& collection,
QObject* parent = 0 );
virtual ~ScriptCommand_AllArtists() {}
virtual void enqueue();
signals:
void artists( const QList< Tomahawk::artist_ptr >& );
void done();
protected:
virtual void exec();
private slots:
void onResolverDone( const QList< Tomahawk::artist_ptr >& );
private:
Tomahawk::collection_ptr m_collection;
};
#endif // SCRIPTCOMMAND_ALLARTISTS_H