1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-04-14 13:01:53 +02:00

Load and create dynamic playlists, wohoo

This commit is contained in:
Leo Franchi 2010-12-08 23:18:35 -05:00
parent 73af178418
commit 6ae5a5d798
13 changed files with 395 additions and 81 deletions

View File

@ -93,6 +93,8 @@ SET( tomahawkSources ${tomahawkSources}
database/databasecommand_setdynamicplaylistrevision.cpp
database/databasecommand_createdynamicplaylist.cpp
database/databasecommand_loaddynamicplaylist.cpp
database/databasecommand_loadalldynamicplaylists.cpp
database/databasecommand_deletedynamicplaylist.cpp
database/databasecollection.cpp
dynamic/dynamicplaylist.cpp
@ -216,6 +218,8 @@ SET( tomahawkHeaders ${tomahawkHeaders}
database/databasecommand_setdynamicplaylistrevision.h
database/databasecommand_createdynamicplaylist.h
database/databasecommand_loaddynamicplaylist.h
database/databasecommand_deletedynamicplaylist.h
database/databasecommand_loadalldynamicplaylists.h
database/databasecommand_renameplaylist.h
database/databasecommand_loadops.h
database/databasecommand_updatesearchindex.h

View File

@ -5,6 +5,7 @@
#include "databasecommand_alltracks.h"
#include "databasecommand_addfiles.h"
#include "databasecommand_loadallplaylists.h"
#include "databasecommand_loadalldynamicplaylists.h"
using namespace Tomahawk;
@ -31,12 +32,12 @@ void
DatabaseCollection::loadDynamicPlaylists()
{
qDebug() << Q_FUNC_INFO;
// DatabaseCommand_LoadAllDynamicPlaylists* cmd = new DatabaseCommand_LoadAllDynamicPlaylists( source() );
//
// connect( cmd, SIGNAL( done( const QList<Tomahawk::dynplaylist_ptr>& ) ),
// SLOT( setDynamicPlaylists( const QList<Tomahawk::dynplaylist_ptr>& ) ) );
//
// TomahawkApp::instance()->database()->enqueue( QSharedPointer<DatabaseCommand>( cmd ) );
DatabaseCommand_LoadAllDynamicPlaylists* cmd = new DatabaseCommand_LoadAllDynamicPlaylists( source() );
connect( cmd, SIGNAL( done( const QList<Tomahawk::dynplaylist_ptr>& ) ),
SLOT( setDynamicPlaylists( const QList<Tomahawk::dynplaylist_ptr>& ) ) );
TomahawkApp::instance()->database()->enqueue( QSharedPointer<DatabaseCommand>( cmd ) );
}

View File

@ -1,6 +1,7 @@
#include "databasecommand_createdynamicplaylist.h"
#include <QSqlQuery>
#include <QSqlDriver>
#include "tomahawk/tomahawkapp.h"
#include "dynamic/dynamicplaylist.h"
@ -34,17 +35,13 @@ DatabaseCommand_CreateDynamicPlaylist::exec( DatabaseImpl* lib )
Q_ASSERT( !source().isNull() );
DatabaseCommand_CreatePlaylist::exec( lib );
qDebug() << "Created normal playlist, now creating additional dynamic info!" << m_playlist.isNull();
qDebug() << "Created normal playlist, now creating additional dynamic info!";
TomahawkSqlQuery cre = lib->newquery();
cre.prepare( "INSERT INTO dynamic_playlist( guid, pltype, plmode ) "
"VALUES( ?, ?, ? )" );
cre.bindValue( 0, m_playlist->guid() );
cre.bindValue( 1, m_playlist->type() );
cre.bindValue( 2, m_playlist->mode() );
qDebug() << "CREATE DYNPLAYLIST:" << cre.boundValues();
qDebug() << "open driver?" << cre.driver()->isOpen();
cre.prepare( QString( "INSERT INTO dynamic_playlist( guid, pltype, plmode ) "
"VALUES( '%1', '%2', %3 )" ).arg( m_playlist->guid() ).arg( m_playlist->type() ).arg( m_playlist->mode() ) );
cre.exec();
// save the controls

View File

@ -0,0 +1,57 @@
/****************************************************************************************
* Copyright (c) 2010 Leo Franchi <lfranchi@kde.org> *
* *
* This program 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. *
* *
* This program 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 *
* this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************************/
#include "databasecommand_deletedynamicplaylist.h"
#include <QSqlQuery>
#include "tomahawk/tomahawkapp.h"
using namespace Tomahawk;
DatabaseCommand_DeleteDynamicPlaylist::DatabaseCommand_DeleteDynamicPlaylist( const source_ptr& source, const QString& playlistguid )
: DatabaseCommand_DeletePlaylist( source, playlistguid )
{
}
void
DatabaseCommand_DeleteDynamicPlaylist::exec( DatabaseImpl* lib )
{
qDebug() << Q_FUNC_INFO;
DatabaseCommand_DeletePlaylist::exec( lib );
TomahawkSqlQuery cre = lib->newquery();
cre.prepare( "DELETE FROM dynamic_playlist WHERE guid = :id" );
cre.bindValue( ":id", m_playlistguid );
cre.exec();
}
void
DatabaseCommand_DeleteDynamicPlaylist::postCommitHook()
{
qDebug() << Q_FUNC_INFO << "..reporting..";
dynplaylist_ptr playlist = source()->collection()->dynamicPlaylist( m_playlistguid );
Q_ASSERT( !playlist.isNull() );
playlist->reportDeleted( playlist );
if( source()->isLocal() )
APP->servent().triggerDBSync();
}

View File

@ -0,0 +1,45 @@
/****************************************************************************************
* Copyright (c) 2010 Leo Franchi <lfranchi@kde.org> *
* *
* This program 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. *
* *
* This program 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 *
* this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************************/
#ifndef DATABASECOMMAND_DELETEDYNAMICPLAYLIST_H
#define DATABASECOMMAND_DELETEDYNAMICPLAYLIST_H
#include "databaseimpl.h"
#include "databasecommand_deleteplaylist.h"
#include "tomahawk/source.h"
#include "tomahawk/typedefs.h"
class DatabaseCommand_DeleteDynamicPlaylist : public DatabaseCommand_DeletePlaylist
{
Q_OBJECT
public:
explicit DatabaseCommand_DeleteDynamicPlaylist( QObject* parent = 0 )
: DatabaseCommand_DeletePlaylist( parent )
{}
explicit DatabaseCommand_DeleteDynamicPlaylist( const Tomahawk::source_ptr& source, const QString& playlistguid );
QString commandname() const { return "deletedynamicplaylist"; }
virtual void exec( DatabaseImpl* lib );
virtual void postCommitHook();
virtual bool doesMutates() const { return true; }
private:
QString m_playlistguid;
};
#endif // DATABASECOMMAND_DELETEDYNAMICPLAYLIST_H

View File

@ -0,0 +1,55 @@
/****************************************************************************************
* Copyright (c) 2010 Leo Franchi <lfranchi@kde.org> *
* *
* This program 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. *
* *
* This program 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 *
* this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************************/
#include "databasecommand_loadalldynamicplaylists.h"
#include <QSqlQuery>
#include "dynamic/dynamicplaylist.h"
#include "databaseimpl.h"
using namespace Tomahawk;
void DatabaseCommand_LoadAllDynamicPlaylists::exec( DatabaseImpl* dbi )
{
TomahawkSqlQuery query = dbi->newquery();
query.exec( QString( "SELECT playlist.guid as guid, title, info, creator, lastmodified, shared, currentrevision, dynamic_playlist.pltype "
"FROM playlist, dynamic_playlist WHERE source %1 AND dynplaylist AND playlist.guid = dynamic_playlist.guid" )
.arg( source()->isLocal() ? "IS NULL" :
QString( "=%1" ).arg( source()->id() )
) );
QList<dynplaylist_ptr> plists;
while ( query.next() )
{
dynplaylist_ptr p( new DynamicPlaylist( source(), //src
query.value(6).toString(), //current rev
query.value(1).toString(), //title
query.value(2).toString(), //info
query.value(3).toString(), //creator
query.value(7).toString(), // dynamic type
query.value(5).toBool(), //shared
query.value(4).toInt(), //lastmod
query.value(0).toString() //GUID
) );
plists.append( p );
}
emit done( plists );
}

View File

@ -0,0 +1,42 @@
/****************************************************************************************
* Copyright (c) 2010 Leo Franchi <lfranchi@kde.org> *
* *
* This program 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. *
* *
* This program 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 *
* this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************************/
#ifndef DATABASECOMMAND_LOADALLDYNAMICPLAYLISTS_H
#define DATABASECOMMAND_LOADALLDYNAMICPLAYLISTS_H
#include <QObject>
#include <QVariantMap>
#include "databasecommand.h"
#include "tomahawk/typedefs.h"
class DatabaseCommand_LoadAllDynamicPlaylists : public DatabaseCommand
{
Q_OBJECT
public:
explicit DatabaseCommand_LoadAllDynamicPlaylists( const Tomahawk::source_ptr& s, QObject* parent = 0 )
: DatabaseCommand( s, parent )
{}
virtual void exec( DatabaseImpl* );
virtual bool doesMutates() const { return false; }
virtual QString commandname() const { return "loadalldynamicplaylists"; }
signals:
void done( const QList<Tomahawk::dynplaylist_ptr>& playlists );
};
#endif // DATABASECOMMAND_ADDFILES_H

View File

@ -64,8 +64,8 @@ CREATE TABLE IF NOT EXISTS playlist (
dynplaylist BOOLEAN DEFAULT false
);
--INSERT INTO playlist(guid, title, info, currentrevision)
-- VALUES('playlistguid-1','Test Playlist','this playlist automatically created and used for testing','revisionguid-1');
INSERT INTO playlist(guid, title, info, currentrevision, dynplaylist)
VALUES('dynamic_playlist-guid-1','Test Dynamic Playlist','this playlist automatically created and used for testing','revisionguid-1', 1);
CREATE TABLE IF NOT EXISTS playlist_item (
guid TEXT PRIMARY KEY,
@ -98,12 +98,20 @@ CREATE TABLE IF NOT EXISTS playlist_revision (
previous_revision TEXT REFERENCES playlist_revision(guid) DEFERRABLE INITIALLY DEFERRED
);
INSERT INTO playlist_revision(guid, playlist, entries)
VALUES('revisionguid-1', 'playlistguid-1', '["itemguid-2","itemguid-1","itemguid-3"]');
CREATE TABLE IF NOT EXISTS dynamic_playlist (
guid TEXT PRIMARY KEY,
pltype TEXT, -- the generator type
plmode INTEGER -- the mode of this playlist
);
INSERT INTO dynamic_playlist(guid, pltype, plmode)
VALUES('dynamic_playlist-guid-1', 'echonest', 0);
INSERT INTO dynamic_playlist(guid, pltype, plmode)
VALUES('dynamic_playlist-guid-2', 'echonest', 0);
-- list of controls in each playlist. each control saves a selectedType, a match, and an input
CREATE TABLE IF NOT EXISTS dynamic_playlist_controls (
id TEXT PRIMARY KEY,
@ -112,6 +120,10 @@ CREATE TABLE IF NOT EXISTS dynamic_playlist_controls (
match TEXT,
input TEXT
);
INSERT INTO dynamic_playlist_controls(id, playlist, selectedType, match, input)
VALUES('controlid-1', 'dynamic_playlist-guid-1', "artist", 0, "FooArtist" );
CREATE TABLE IF NOT EXISTS dynamic_playlist_revision (
guid TEXT PRIMARY KEY,
@ -120,8 +132,8 @@ CREATE TABLE IF NOT EXISTS dynamic_playlist_revision (
pltype TEXT REFERENCES dynamic_playlist( pltype ) ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED
);
--INSERT INTO playlist_revision(guid, playlist, entries)
-- VALUES('revisionguid-1', 'playlistguid-1', '["itemguid-2","itemguid-1","itemguid-3"]');
INSERT INTO dynamic_playlist_revision(guid, controls, plmode, pltype)
VALUES('revisionguid-1', '["controlid-1"]', 0, "echonest");
-- the trigram search indexes

View File

@ -1,5 +1,5 @@
/*
This file was automatically generated from schema.sql on Mon Dec 6 22:41:52 EST 2010.
This file was automatically generated from schema.sql on Wed Dec 8 22:37:14 EST 2010.
*/
static const char * tomahawk_schema_sql =
@ -52,6 +52,8 @@ static const char * tomahawk_schema_sql =
" currentrevision TEXT REFERENCES playlist_revision(guid) DEFERRABLE INITIALLY DEFERRED,"
" dynplaylist BOOLEAN DEFAULT false"
");"
"INSERT INTO playlist(guid, title, info, currentrevision, dynplaylist)"
"VALUES('dynamic_playlist-guid-1','Test Dynamic Playlist','this playlist automatically created and used for testing','revisionguid-1', 1);"
"CREATE TABLE IF NOT EXISTS playlist_item ("
" guid TEXT PRIMARY KEY,"
" playlist TEXT NOT NULL REFERENCES playlist(guid) ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED,"
@ -73,11 +75,17 @@ static const char * tomahawk_schema_sql =
" timestamp INTEGER NOT NULL DEFAULT 0,"
" previous_revision TEXT REFERENCES playlist_revision(guid) DEFERRABLE INITIALLY DEFERRED"
");"
"INSERT INTO playlist_revision(guid, playlist, entries)"
" VALUES('revisionguid-1', 'playlistguid-1', '[\"itemguid-2\",\"itemguid-1\",\"itemguid-3\"]');"
"CREATE TABLE IF NOT EXISTS dynamic_playlist ("
" guid TEXT PRIMARY KEY,"
" pltype TEXT, "
" plmode INTEGER "
");"
"INSERT INTO dynamic_playlist(guid, pltype, plmode)"
" VALUES('dynamic_playlist-guid-1', 'echonest', 0);"
"INSERT INTO dynamic_playlist(guid, pltype, plmode)"
" VALUES('dynamic_playlist-guid-2', 'echonest', 0);"
"CREATE TABLE IF NOT EXISTS dynamic_playlist_controls ("
" id TEXT PRIMARY KEY,"
" playlist TEXT NOT NULL REFERENCES playlist(guid) ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED,"
@ -85,6 +93,8 @@ static const char * tomahawk_schema_sql =
" match TEXT,"
" input TEXT"
");"
"INSERT INTO dynamic_playlist_controls(id, playlist, selectedType, match, input)"
" VALUES('controlid-1', 'dynamic_playlist-guid-1', \"artist\", 0, \"FooArtist\" );"
""
"CREATE TABLE IF NOT EXISTS dynamic_playlist_revision ("
" guid TEXT PRIMARY KEY,"
@ -92,6 +102,8 @@ static const char * tomahawk_schema_sql =
" plmode INTEGER REFERENCES dynamic_playlist( plmode ) ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED,"
" pltype TEXT REFERENCES dynamic_playlist( pltype ) ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED"
");"
"INSERT INTO dynamic_playlist_revision(guid, controls, plmode, pltype)"
" VALUES('revisionguid-1', '[\"controlid-1\"]', 0, \"echonest\");"
"CREATE TABLE IF NOT EXISTS artist_search_index ("
" ngram TEXT NOT NULL,"
" id INTEGER NOT NULL REFERENCES artist(id) ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED,"

View File

@ -25,6 +25,10 @@
#include "tomahawk/typedefs.h"
#include "dynamic/dynamiccontrol.h"
class DatabaseCommand_LoadAllDynamicPlaylists;
class DatabaseCommand_SetDynamicPlaylistRevision;
class DatabaseCommand_CreateDynamicPlaylist;
namespace Tomahawk {
/**
@ -57,7 +61,11 @@ class DynamicPlaylist : public Playlist
Q_PROPERTY( GeneratorMode mode WRITE setMode READ mode )
Q_PROPERTY( QString type WRITE setType READ type )
friend class ::DatabaseCommand_LoadAllDynamicPlaylists;
friend class ::DatabaseCommand_SetDynamicPlaylistRevision;
friend class ::DatabaseCommand_CreateDynamicPlaylist;
public:
virtual ~DynamicPlaylist();

View File

@ -285,11 +285,11 @@ Playlist::setNewRevision( const QString& rev,
//qDebug() << "counters:" << neworderedguids.count() << entriesmap.count() << addedmap.count();
foreach( const QString& id, neworderedguids )
{
//qDebug() << "id:" << id;
//qDebug() << "newordered:" << neworderedguids.count() << neworderedguids;
//qDebug() << "entriesmap:" << entriesmap.count() << entriesmap;
//qDebug() << "addedmap:" << addedmap.count() << addedmap;
//qDebug() << "m_entries" << m_entries;
qDebug() << "id:" << id;
qDebug() << "newordered:" << neworderedguids.count() << neworderedguids;
qDebug() << "entriesmap:" << entriesmap.count() << entriesmap;
qDebug() << "addedmap:" << addedmap.count() << addedmap;
qDebug() << "m_entries" << m_entries;
if( entriesmap.contains( id ) )
{

View File

@ -9,6 +9,14 @@
using namespace Tomahawk;
static inline QList< playlist_ptr > dynListToPlaylists( const QList< Tomahawk::dynplaylist_ptr >& list )
{
QList< playlist_ptr > newptrs;
foreach( const dynplaylist_ptr& pl, list ) {
newptrs << pl.staticCast<Playlist>();
}
return newptrs;
}
SourceTreeItem::SourceTreeItem( const source_ptr& source, QObject* parent )
: QObject( parent )
@ -23,12 +31,18 @@ SourceTreeItem::SourceTreeItem( const source_ptr& source, QObject* parent )
if ( !source.isNull() )
{
onPlaylistsAdded( source->collection()->playlists() );
onDynamicPlaylistsAdded( source->collection()->dynamicPlaylists() );
connect( source->collection().data(), SIGNAL( playlistsAdded( QList<Tomahawk::playlist_ptr> ) ),
SLOT( onPlaylistsAdded( QList<Tomahawk::playlist_ptr> ) ) );
connect( source->collection().data(), SIGNAL( playlistsDeleted( QList<Tomahawk::playlist_ptr> ) ),
SLOT( onPlaylistsDeleted( QList<Tomahawk::playlist_ptr> ) ) );
connect( source->collection().data(), SIGNAL( dynamicPlaylistsAdded( QList<Tomahawk::dynplaylist_ptr> ) ),
SLOT( onDynamicPlaylistsAdded( QList<Tomahawk::dynplaylist_ptr> ) ) );
connect( source->collection().data(), SIGNAL( dynamicPlaylistsDeleted( QList<Tomahawk::dynplaylist_ptr> ) ),
SLOT( onDynamicPlaylistsDeleted( QList<Tomahawk::dynplaylist_ptr> ) ) );
}
m_widget = new SourceTreeItemWidget( source );
@ -66,81 +80,132 @@ SourceTreeItem::onOffline()
void
SourceTreeItem::onPlaylistsAdded( const QList<playlist_ptr>& playlists )
{
// const-ness is important for getting the right pointer!
foreach( const playlist_ptr& p, playlists )
{
m_playlists.append( p );
qlonglong ptr = qlonglong( &m_playlists.last() );
qDebug() << "Playlist added:" << p->title() << p->creator() << p->info() << ptr;
connect( p.data(), SIGNAL( revisionLoaded( Tomahawk::PlaylistRevision ) ),
SLOT( onPlaylistLoaded( Tomahawk::PlaylistRevision ) ),
Qt::QueuedConnection);
QStandardItem* subitem = new QStandardItem( p->title() );
subitem->setIcon( QIcon( RESPATH "images/playlist-icon.png" ) );
subitem->setEditable( false );
subitem->setEnabled( false );
subitem->setData( ptr, Qt::UserRole + 3 );
subitem->setData( 1, Qt::UserRole + 1 );
subitem->setData( (qlonglong)this, Qt::UserRole + 2 );
m_columns.at( 0 )->appendRow( subitem );
((QTreeView*)parent()->parent())->expandAll();
p->loadRevision();
}
playlistsAdded( playlists, false );
}
void
SourceTreeItem::onPlaylistsDeleted( const QList<playlist_ptr>& playlists )
{
// const-ness is important for getting the right pointer!
foreach( const playlist_ptr& p, playlists )
{
qlonglong ptr = qlonglong( p.data() );
qDebug() << "Playlist removed:" << p->title() << p->creator() << p->info() << ptr;
QStandardItem* item = m_columns.at( 0 );
int rows = item->rowCount();
for ( int i = rows - 1; i >= 0; i-- )
{
QStandardItem* pi = item->child( i );
qlonglong piptr = pi->data( Qt::UserRole + 3 ).toLongLong();
playlist_ptr* pl = reinterpret_cast<playlist_ptr*>(piptr);
int type = pi->data( Qt::UserRole + 1 ).toInt();
if ( type == 1 && ptr == qlonglong( pl->data() ) )
{
m_playlists.removeAll( p );
item->removeRow( i );
}
}
}
playlistsDeleted( playlists, false );
}
void
SourceTreeItem::onPlaylistLoaded( Tomahawk::PlaylistRevision revision )
{
qlonglong ptr = qlonglong( sender() );
//qDebug() << "sender ptr:" << ptr;
playlistLoaded( revision, false );
}
void SourceTreeItem::onDynamicPlaylistsAdded( const QList< dynplaylist_ptr >& playlists )
{
playlistsAdded( dynListToPlaylists( playlists ), true );
}
void SourceTreeItem::onDynamicPlaylistsDeleted( const QList< dynplaylist_ptr >& playlists )
{
playlistsDeleted( dynListToPlaylists( playlists ), true );
}
void SourceTreeItem::onDynamicPlaylistsLoaded( DynamicPlaylistRevision revision )
{
playlistLoaded( revision, true );
}
void
SourceTreeItem::playlistLoaded( PlaylistRevision revision, bool dynamic )
{
qlonglong ptr = reinterpret_cast<qlonglong>( sender() );
//qDebug() << "sender ptr:" << ptr;
QStandardItem* item = m_columns.at( 0 );
int rows = item->rowCount();
for ( int i = 0; i < rows; i++ )
{
QStandardItem* pi = item->child( i );
qlonglong piptr = pi->data( Qt::UserRole + 3 ).toLongLong();
qlonglong piptr = pi->data( PlaylistPointer ).toLongLong();
playlist_ptr* pl = reinterpret_cast<playlist_ptr*>(piptr);
int type = pi->data( Qt::UserRole + 1 ).toInt();
int type = pi->data( Type ).toInt();
if ( type == 1 && ptr == qlonglong( pl->data() ) )
{
//qDebug() << "Found playlist!";
pi->setEnabled( true );
m_current_revisions.insert( pl->data()->guid(), revision.revisionguid );
if( dynamic )
m_current_dynamic_revisions.insert( pl->data()->guid(), revision.revisionguid );
else
m_current_revisions.insert( pl->data()->guid(), revision.revisionguid );
}
}
}
void
SourceTreeItem::playlistsAdded( const QList< playlist_ptr >& playlists, bool dynamic )
{
// const-ness is important for getting the right pointer!
foreach( const playlist_ptr& p, playlists )
{
qlonglong ptr;
if( dynamic )
{
m_dynplaylists.append( p.staticCast<Tomahawk::DynamicPlaylist>() );
ptr = reinterpret_cast<qlonglong>( &m_dynplaylists.last() );
connect( p.staticCast<Tomahawk::DynamicPlaylist>().data(), SIGNAL( revisionLoaded( Tomahawk::DynamicPlaylistRevision ) ),
SLOT( onDynamicPlaylistLoaded( Tomahawk::DynamicPlaylistRevision ) ),
Qt::QueuedConnection);
} else
{
m_playlists.append( p );
ptr = reinterpret_cast<qlonglong>( &m_playlists.last() );
connect( p.data(), SIGNAL( revisionLoaded( Tomahawk::PlaylistRevision ) ),
SLOT( onPlaylistLoaded( Tomahawk::PlaylistRevision ) ),
Qt::QueuedConnection);
}
qDebug() << "Playlist added:" << p->title() << p->creator() << p->info() << ptr << dynamic;
QStandardItem* subitem = new QStandardItem( p->title() );
subitem->setIcon( QIcon( RESPATH "images/playlist-icon.png" ) );
subitem->setEditable( false );
subitem->setEnabled( false );
subitem->setData( ptr, PlaylistPointer );
subitem->setData( 1, Type );
subitem->setData( (qlonglong)this, SourceItemPointer );
m_columns.at( 0 )->appendRow( subitem );
Q_ASSERT( qobject_cast<QTreeView*>((parent()->parent()) ) );
qobject_cast<QTreeView*>((parent()->parent()))->expandAll();
p->loadRevision();
}
}
void
SourceTreeItem::playlistsDeleted( const QList< playlist_ptr >& playlists, bool dynamic )
{
// const-ness is important for getting the right pointer!
foreach( const playlist_ptr& p, playlists )
{
qlonglong ptr = qlonglong( p.data() );
qDebug() << "Playlist removed:" << p->title() << p->creator() << p->info() << ptr << dynamic;
QStandardItem* item = m_columns.at( 0 );
int rows = item->rowCount();
for ( int i = rows - 1; i >= 0; i-- )
{
QStandardItem* pi = item->child( i );
qlonglong piptr = pi->data( PlaylistPointer ).toLongLong();
playlist_ptr* pl = reinterpret_cast<playlist_ptr*>(piptr);
int type = pi->data( Type ).toInt();
if ( type == 1 && ptr == qlonglong( pl->data() ) )
{
if( dynamic )
m_dynplaylists.removeAll( p.staticCast<Tomahawk::DynamicPlaylist>() );
else
m_playlists.removeAll( p );
item->removeRow( i );
}
}
}
}

View File

@ -6,11 +6,18 @@
#include "tomahawk/typedefs.h"
#include "sourcetreeitemwidget.h"
#include "dynamic/dynamicplaylist.h"
class SourceTreeItem : public QObject
{
Q_OBJECT
enum PlaylistItemType {
Type = Qt::UserRole + 1,
SourceItemPointer = Qt::UserRole + 2,
PlaylistPointer = Qt::UserRole + 3
};
public:
explicit SourceTreeItem( const Tomahawk::source_ptr& source, QObject* parent );
virtual ~SourceTreeItem();
@ -39,15 +46,24 @@ private slots:
void onPlaylistsAdded( const QList<Tomahawk::playlist_ptr>& playlists );
void onPlaylistsDeleted( const QList<Tomahawk::playlist_ptr>& playlists );
void onPlaylistLoaded( Tomahawk::PlaylistRevision revision );
void onDynamicPlaylistsAdded( const QList<Tomahawk::dynplaylist_ptr>& playlists );
void onDynamicPlaylistsDeleted( const QList<Tomahawk::dynplaylist_ptr>& playlists );
void onDynamicPlaylistsLoaded( Tomahawk::DynamicPlaylistRevision revision );
private:
void playlistsAdded( const QList<Tomahawk::playlist_ptr>& playlists, bool dynamic );
void playlistsDeleted( const QList<Tomahawk::playlist_ptr>& playlists, bool dynamic );
void playlistLoaded( Tomahawk::PlaylistRevision revision, bool dynamic );
QList<QStandardItem*> m_columns;
Tomahawk::source_ptr m_source;
SourceTreeItemWidget* m_widget;
QList<Tomahawk::playlist_ptr> m_playlists;
QList<Tomahawk::dynplaylist_ptr> m_dynplaylists;
// playist->guid() -> currently loaded revision
QMap<QString,QString> m_current_revisions;
QMap<QString,QString> m_current_dynamic_revisions;
};
#endif // SOURCETREEITEM_H