mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-17 19:37:09 +02:00
Add (lazy) loading for Playlists from JSResolvers
This commit is contained in:
@@ -322,6 +322,7 @@ list(APPEND libSources
|
||||
network/ConnectionManager.cpp
|
||||
|
||||
playlist/PlaylistUpdaterInterface.cpp
|
||||
playlist/PlaylistTemplate.cpp
|
||||
playlist/dynamic/DynamicPlaylist.cpp
|
||||
playlist/dynamic/GeneratorFactory.cpp
|
||||
playlist/dynamic/GeneratorInterface.cpp
|
||||
|
@@ -43,6 +43,7 @@ namespace Tomahawk
|
||||
class Playlist;
|
||||
class PlaylistEntry;
|
||||
class PlaylistInterface;
|
||||
class PlaylistTemplate;
|
||||
class DynamicPlaylist;
|
||||
class Query;
|
||||
class Result;
|
||||
@@ -58,6 +59,7 @@ namespace Tomahawk
|
||||
typedef QSharedPointer<Playlist> playlist_ptr;
|
||||
typedef QSharedPointer<PlaylistEntry> plentry_ptr;
|
||||
typedef QSharedPointer<PlaylistInterface> playlistinterface_ptr;
|
||||
typedef QSharedPointer<PlaylistTemplate> playlisttemplate_ptr;
|
||||
typedef QSharedPointer<DynamicPlaylist> dynplaylist_ptr;
|
||||
typedef QSharedPointer<Query> query_ptr;
|
||||
typedef QWeakPointer<Query> query_wptr;
|
||||
|
55
src/libtomahawk/playlist/PlaylistTemplate.cpp
Normal file
55
src/libtomahawk/playlist/PlaylistTemplate.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||
*
|
||||
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
|
||||
*
|
||||
* 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 "PlaylistTemplate_p.h"
|
||||
|
||||
#include "Source.h"
|
||||
|
||||
Tomahawk::PlaylistTemplate::PlaylistTemplate(const Tomahawk::source_ptr& author, const QString &guid, const QString &title, const QString &info, const QString &creator, bool shared, const QList<Tomahawk::query_ptr> &queries)
|
||||
: QObject( 0 )
|
||||
, d_ptr( new PlaylistTemplatePrivate( this, author, guid, title, info, creator, shared, queries ) )
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Tomahawk::PlaylistTemplate::~PlaylistTemplate()
|
||||
{
|
||||
tLog() << Q_FUNC_INFO;
|
||||
delete d_ptr;
|
||||
}
|
||||
|
||||
|
||||
Tomahawk::playlist_ptr
|
||||
Tomahawk::PlaylistTemplate::get()
|
||||
{
|
||||
Q_D( PlaylistTemplate );
|
||||
|
||||
if ( d->playlist.isNull() )
|
||||
{
|
||||
// First try to load the playlist if already exists
|
||||
d->playlist = Playlist::get( d->guid );
|
||||
}
|
||||
|
||||
if ( d->playlist.isNull() )
|
||||
{
|
||||
// This playlist does not exist yet, so create it.
|
||||
d->playlist = Playlist::create( d->author, d->guid, d->title, d->info, d->creator, d->shared, d->queries );
|
||||
}
|
||||
|
||||
return d->playlist;
|
||||
}
|
57
src/libtomahawk/playlist/PlaylistTemplate.h
Normal file
57
src/libtomahawk/playlist/PlaylistTemplate.h
Normal file
@@ -0,0 +1,57 @@
|
||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||
*
|
||||
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
|
||||
*
|
||||
* 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 PLAYLISTTEMPLATE_H
|
||||
#define PLAYLISTTEMPLATE_H
|
||||
|
||||
#include "DllMacro.h"
|
||||
#include "Typedefs.h"
|
||||
|
||||
namespace Tomahawk
|
||||
{
|
||||
|
||||
class PlaylistTemplatePrivate;
|
||||
|
||||
class DLLEXPORT PlaylistTemplate : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit PlaylistTemplate( const source_ptr& author,
|
||||
const QString& guid,
|
||||
const QString& title,
|
||||
const QString& info,
|
||||
const QString& creator,
|
||||
bool shared,
|
||||
const QList<Tomahawk::query_ptr>& queries = QList<Tomahawk::query_ptr>());
|
||||
virtual ~PlaylistTemplate();
|
||||
|
||||
/**
|
||||
* Create or get the playlist for this template.
|
||||
*/
|
||||
Tomahawk::playlist_ptr get();
|
||||
|
||||
protected:
|
||||
PlaylistTemplatePrivate* d_ptr;
|
||||
|
||||
private:
|
||||
Q_DECLARE_PRIVATE( PlaylistTemplate )
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // PLAYLISTTEMPLATE_H
|
70
src/libtomahawk/playlist/PlaylistTemplate_p.h
Normal file
70
src/libtomahawk/playlist/PlaylistTemplate_p.h
Normal file
@@ -0,0 +1,70 @@
|
||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||
*
|
||||
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
|
||||
*
|
||||
* 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 PLAYLISTTEMPLATE_P_H
|
||||
#define PLAYLISTTEMPLATE_P_H
|
||||
|
||||
#include "playlist/PlaylistTemplate.h"
|
||||
|
||||
#include "Playlist.h"
|
||||
#include "utils/Logger.h"
|
||||
|
||||
namespace Tomahawk
|
||||
{
|
||||
|
||||
class PlaylistTemplatePrivate
|
||||
{
|
||||
public:
|
||||
PlaylistTemplatePrivate( PlaylistTemplate* q,
|
||||
const source_ptr& _author,
|
||||
const QString& _guid,
|
||||
const QString& _title,
|
||||
const QString& _info,
|
||||
const QString& _creator,
|
||||
bool _shared,
|
||||
const QList<Tomahawk::query_ptr>& _queries )
|
||||
: q_ptr( q )
|
||||
, author( _author )
|
||||
, guid( _guid )
|
||||
, title( _title )
|
||||
, info( _info )
|
||||
, creator( _creator )
|
||||
, shared( _shared )
|
||||
, queries( _queries )
|
||||
{
|
||||
}
|
||||
|
||||
Q_DECLARE_PUBLIC( PlaylistTemplate )
|
||||
PlaylistTemplate* q_ptr;
|
||||
|
||||
private:
|
||||
source_ptr author;
|
||||
QString guid;
|
||||
QString title;
|
||||
QString info;
|
||||
QString creator;
|
||||
bool shared;
|
||||
QList<Tomahawk::query_ptr> queries;
|
||||
playlist_ptr playlist;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Q_DECLARE_METATYPE( QSharedPointer< Tomahawk::PlaylistTemplate > )
|
||||
|
||||
#endif // PLAYLISTTEMPLATE_P_H
|
@@ -21,13 +21,18 @@
|
||||
|
||||
#include "JSResolverHelper.h"
|
||||
|
||||
#include "database/Database.h"
|
||||
#include "database/DatabaseImpl.h"
|
||||
#include "playlist/PlaylistTemplate.h"
|
||||
#include "resolvers/ScriptEngine.h"
|
||||
#include "network/Servent.h"
|
||||
#include "utils/Logger.h"
|
||||
|
||||
#include "config.h"
|
||||
#include "JSResolver_p.h"
|
||||
#include "Pipeline.h"
|
||||
#include "Result.h"
|
||||
#include "SourceList.h"
|
||||
|
||||
#include <boost/bind.hpp>
|
||||
#include <QFile>
|
||||
@@ -35,6 +40,8 @@
|
||||
#include <QtCrypto>
|
||||
#include <QWebFrame>
|
||||
|
||||
using namespace Tomahawk;
|
||||
|
||||
JSResolverHelper::JSResolverHelper( const QString& scriptPath, JSResolver* parent )
|
||||
: QObject( parent )
|
||||
, m_urlCallbackIsAsync( false )
|
||||
@@ -215,31 +222,81 @@ JSResolverHelper::addAlbumTrackResults( const QVariantMap& results )
|
||||
}
|
||||
|
||||
|
||||
query_ptr
|
||||
JSResolverHelper::parseTrack( const QVariantMap& track )
|
||||
{
|
||||
QString title = track.value( "title" ).toString();
|
||||
QString artist = track.value( "artist" ).toString();
|
||||
QString album = track.value( "album" ).toString();
|
||||
if ( title.isEmpty() || artist.isEmpty() )
|
||||
{
|
||||
return query_ptr();
|
||||
}
|
||||
|
||||
Tomahawk::query_ptr query = Tomahawk::Query::get( artist, title, album );
|
||||
QString resultHint = track.value( "hint" ).toString();
|
||||
if ( !resultHint.isEmpty() )
|
||||
{
|
||||
query->setResultHint( resultHint );
|
||||
query->setSaveHTTPResultHint( true );
|
||||
}
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
void
|
||||
JSResolverHelper::addUrlResult( const QString& url, const QVariantMap& result )
|
||||
{
|
||||
QString type = result.value( "type" ).toString();
|
||||
if ( type == "track" )
|
||||
{
|
||||
QString title = result.value( "title" ).toString();
|
||||
QString artist = result.value( "artist" ).toString();
|
||||
QString album = result.value( "album" ).toString();
|
||||
tLog( LOGVERBOSE ) << m_resolver->name() << "Got track for url" << url << title << artist << album;
|
||||
if ( title.isEmpty() || artist.isEmpty() )
|
||||
Tomahawk::query_ptr query = parseTrack( result );
|
||||
if ( query.isNull() )
|
||||
{
|
||||
// A valid track result shoud have non-empty title and artist.
|
||||
tLog() << Q_FUNC_INFO << m_resolver->name() << "Got empty track information for " << url;
|
||||
emit m_resolver->informationFound( url, QSharedPointer<QObject>() );
|
||||
}
|
||||
|
||||
Tomahawk::query_ptr query = Tomahawk::Query::get( artist, title, album );
|
||||
QString resultHint = result.value( "hint" ).toString();
|
||||
if ( !resultHint.isEmpty() )
|
||||
else
|
||||
{
|
||||
query->setResultHint( resultHint );
|
||||
query->setSaveHTTPResultHint( true );
|
||||
emit m_resolver->informationFound( url, query.objectCast<QObject>() );
|
||||
}
|
||||
emit m_resolver->informationFound( url, query.objectCast<QObject>() );
|
||||
}
|
||||
else if ( type == "playlist" )
|
||||
{
|
||||
QString guid = result.value( "guid" ).toString();
|
||||
Q_ASSERT( !guid.isEmpty() );
|
||||
// Append nodeid to guid to make it globally unique.
|
||||
guid += Tomahawk::Database::instance()->impl()->dbid();
|
||||
|
||||
// Do we already have this playlist loaded?
|
||||
{
|
||||
playlist_ptr playlist = Playlist::get( guid );
|
||||
if ( !playlist.isNull() )
|
||||
{
|
||||
emit m_resolver->informationFound( url, playlist.objectCast<QObject>() );
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Get all information to build a new playlist but do not build it until we know,
|
||||
// if it is really handled as a playlist and not as a set of tracks.
|
||||
Tomahawk::source_ptr source = SourceList::instance()->getLocal();
|
||||
const QString title = result.value( "title" ).toString();
|
||||
const QString info = result.value( "info" ).toString();
|
||||
const QString creator = result.value( "creator" ).toString();
|
||||
QList<query_ptr> queries;
|
||||
foreach( QVariant track, result.value( "tracks" ).toList() )
|
||||
{
|
||||
query_ptr query = parseTrack( track.toMap() );
|
||||
if ( !query.isNull() )
|
||||
{
|
||||
queries << query;
|
||||
}
|
||||
}
|
||||
tLog( LOGVERBOSE ) << Q_FUNC_INFO << m_resolver->name() << "Got playlist for " << url;
|
||||
playlisttemplate_ptr pltemplate( new PlaylistTemplate( source, guid, title, info, creator, false, queries ) );
|
||||
emit m_resolver->informationFound( url, pltemplate.objectCast<QObject>() );
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@@ -75,7 +75,9 @@ public slots:
|
||||
void reportCapabilities( const QVariant& capabilities );
|
||||
|
||||
private:
|
||||
Tomahawk::query_ptr parseTrack( const QVariantMap& track );
|
||||
void returnStreamUrl( const QString& streamUrl, boost::function< void( QSharedPointer< QIODevice >& ) > callback );
|
||||
|
||||
QString m_scriptPath, m_urlCallback;
|
||||
QHash< QString, boost::function< void( QSharedPointer< QIODevice >& ) > > m_streamCallbacks;
|
||||
bool m_urlCallbackIsAsync;
|
||||
|
@@ -55,11 +55,12 @@
|
||||
#include "accounts/spotify/SpotifyAccount.h"
|
||||
#include "accounts/spotify/SpotifyPlaylistUpdater.h"
|
||||
#include "accounts/AccountManager.h"
|
||||
#include "audio/AudioEngine.h"
|
||||
#include "database/Database.h"
|
||||
#include "database/DatabaseCollection.h"
|
||||
#include "database/DatabaseCommand_CollectionStats.h"
|
||||
#include "database/DatabaseResolver.h"
|
||||
#include "audio/AudioEngine.h"
|
||||
#include "playlist/PlaylistTemplate.h"
|
||||
#include "jobview/ErrorStatusMessage.h"
|
||||
#include "jobview/JobStatusModel.h"
|
||||
#include "jobview/JobStatusView.h"
|
||||
@@ -401,6 +402,7 @@ TomahawkApp::registerMetaTypes()
|
||||
qRegisterMetaType< Tomahawk::dyncontrol_ptr >("Tomahawk::dyncontrol_ptr");
|
||||
qRegisterMetaType< Tomahawk::playlist_ptr >("Tomahawk::playlist_ptr");
|
||||
qRegisterMetaType< Tomahawk::playlistinterface_ptr >("Tomahawk::playlistinterface_ptr");
|
||||
qRegisterMetaType< Tomahawk::playlisttemplate_ptr >("Tomahawk::playlisttemplate_ptr");
|
||||
qRegisterMetaType< Tomahawk::dynplaylist_ptr >("Tomahawk::dynplaylist_ptr");
|
||||
qRegisterMetaType< Tomahawk::geninterface_ptr >("Tomahawk::geninterface_ptr");
|
||||
qRegisterMetaType< Tomahawk::PlaybackLog >("Tomahawk::PlaybackLog");
|
||||
@@ -750,6 +752,13 @@ TomahawkApp::informationForUrl( const QString& url, const QSharedPointer<QObject
|
||||
return;
|
||||
}
|
||||
|
||||
Tomahawk::playlisttemplate_ptr pltemplate = information.objectCast<Tomahawk::PlaylistTemplate>();
|
||||
if ( !pltemplate.isNull() )
|
||||
{
|
||||
ViewManager::instance()->show( pltemplate->get() );
|
||||
return;
|
||||
}
|
||||
|
||||
// Try to interpret as Track/Query
|
||||
Tomahawk::query_ptr query = information.objectCast<Tomahawk::Query>();
|
||||
if ( !query.isNull() )
|
||||
|
Reference in New Issue
Block a user