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

work on multiple updaters

This commit is contained in:
Leo Franchi 2012-04-29 13:40:05 -04:00
parent c0e627b7c6
commit 17bcaf7369
20 changed files with 265 additions and 111 deletions

View File

@ -123,8 +123,18 @@ SpotifyAccount::aboutToShow( QAction* action, const playlist_ptr& playlist )
return;
// If it's not being synced, allow the option to sync
SpotifyPlaylistUpdater* updater = qobject_cast< SpotifyPlaylistUpdater* >( playlist->updater() );
if ( !updater || !updater->sync() )
bool found = false;
SmartPointerList<PlaylistUpdaterInterface> updaters = playlist->updaters();
foreach ( PlaylistUpdaterInterface* updater, updaters )
{
if ( SpotifyPlaylistUpdater* spotifyUpdater = qobject_cast< SpotifyPlaylistUpdater* >( updater ) )
{
if ( spotifyUpdater->sync() )
found = true;
}
}
if ( !found )
{
action->setText( tr( "Sync with Spotify" ) );
}
@ -152,7 +162,15 @@ SpotifyAccount::syncActionTriggered( bool checked )
return;
}
SpotifyPlaylistUpdater* updater = qobject_cast< SpotifyPlaylistUpdater* >( playlist->updater() );
SpotifyPlaylistUpdater* updater = 0;
SmartPointerList<PlaylistUpdaterInterface> updaters = playlist->updaters();
foreach ( PlaylistUpdaterInterface* u, updaters )
{
if ( SpotifyPlaylistUpdater* spotifyUpdater = qobject_cast< SpotifyPlaylistUpdater* >( u ) )
{
updater = spotifyUpdater;
}
}
if ( !updater )
{

View File

@ -270,8 +270,11 @@ void
Playlist::reportDeleted( const Tomahawk::playlist_ptr& self )
{
Q_ASSERT( self.data() == this );
if ( !m_updater.isNull() )
m_updater.data()->remove();
if ( !m_updaters.isEmpty() )
{
foreach( PlaylistUpdaterInterface* updater, m_updaters )
updater->remove();
}
m_deleted = true;
m_source->collection()->deletePlaylist( self );
@ -280,25 +283,24 @@ Playlist::reportDeleted( const Tomahawk::playlist_ptr& self )
}
void
Playlist::setUpdater( PlaylistUpdaterInterface* pluinterface )
Playlist::addUpdater( PlaylistUpdaterInterface* updater )
{
if ( !m_updater.isNull() )
disconnect( m_updater.data(), SIGNAL( changed() ), this, SIGNAL( changed() ) );
m_updaters << updater;
m_updater = QWeakPointer< PlaylistUpdaterInterface >( pluinterface );
connect( m_updater.data(), SIGNAL( changed() ), this, SIGNAL( changed() ), Qt::UniqueConnection );
connect( m_updater.data(), SIGNAL( destroyed( QObject* ) ), this, SLOT( updaterDestroyed() ), Qt::QueuedConnection );
connect( updater, SIGNAL( changed() ), this, SIGNAL( changed() ), Qt::UniqueConnection );
connect( updater, SIGNAL( destroyed( QObject* ) ), this, SIGNAL( changed() ), Qt::QueuedConnection );
emit changed();
}
void
Playlist::updaterDestroyed()
Playlist::removeUpdater( PlaylistUpdaterInterface* updater )
{
m_updater.clear();
emit changed();
m_updaters.removeAll( updater );
disconnect( updater, SIGNAL( changed() ), this, SIGNAL( changed() ) );
disconnect( updater, SIGNAL( destroyed( QObject* ) ), this, SIGNAL( changed() ) );
}

View File

@ -34,6 +34,7 @@
#include "Query.h"
#include "DllMacro.h"
#include "utils/SmartPointerList.h"
class DatabaseCommand_LoadAllPlaylists;
class DatabaseCommand_LoadAllSortedPlaylists;
@ -188,8 +189,10 @@ public:
QList<plentry_ptr> entriesFromQueries( const QList<Tomahawk::query_ptr>& queries, bool clearFirst = false );
void setUpdater( PlaylistUpdaterInterface* pluinterface );
PlaylistUpdaterInterface* updater() const { return m_updater.data(); }
void addUpdater( PlaylistUpdaterInterface* updater );
void removeUpdater( PlaylistUpdaterInterface* updater );
SmartPointerList<PlaylistUpdaterInterface> updaters() const { return m_updaters; }
Tomahawk::playlistinterface_ptr playlistInterface();
@ -278,7 +281,6 @@ protected:
private slots:
void onResultsFound( const QList<Tomahawk::result_ptr>& results );
void onResolvingFinished();
void updaterDestroyed();
private:
Playlist();
@ -300,7 +302,7 @@ private:
QQueue<RevisionQueueItem> m_revisionQueue;
QQueue<RevisionQueueItem> m_updateQueue;
QWeakPointer<PlaylistUpdaterInterface> m_updater;
SmartPointerList<PlaylistUpdaterInterface> m_updaters;
bool m_locallyChanged;
bool m_deleted;

View File

@ -30,11 +30,51 @@
#include "database/DatabaseCommand_UpdateSearchIndex.h"
#include "database/Database.h"
#include "PlaylistUpdaterInterface.h"
using namespace Tomahawk;
TomahawkSettings* TomahawkSettings::s_instance = 0;
inline QDataStream&
operator<<(QDataStream& out, const PlaylistUpdaterInterface::SerializedUpdaters& updaters)
{
out << TOMAHAWK_SETTINGS_VERSION;
out << (quint32)updaters.count();
foreach( const QString& key, updaters.keys() )
{
PlaylistUpdaterInterface::SerializedUpdater updater = updaters[ key ];
out << key << updater.type << updater.sync << updater.customData;
}
return out;
}
inline QDataStream&
operator>>(QDataStream& in, PlaylistUpdaterInterface::SerializedUpdaters& updaters)
{
quint32 count = 0, version = 0;
in >> version;
in >> count;
for ( uint i = 0; i < count; i++ )
{
QString key, type;
bool sync;
QVariantHash customData;
qint32 state, userRating;
in >> key;
in >> type;
in >> sync;
in >> customData;
updaters[ key ] = PlaylistUpdaterInterface::SerializedUpdater( type, sync, customData );
}
return in;
}
TomahawkSettings*
TomahawkSettings::instance()
{
@ -444,6 +484,11 @@ TomahawkSettings::doUpgrade( int oldVersion, int newVersion )
setValue( "allaccounts", allAccounts );
endGroup();
}
else if ( oldVersion == 9 )
{
// Upgrade single-updater-per-playlist to list-per-playlist
}
}

View File

@ -28,7 +28,7 @@
#include "DllMacro.h"
#define TOMAHAWK_SETTINGS_VERSION 9
#define TOMAHAWK_SETTINGS_VERSION 10
/**
* Convenience wrapper around QSettings for tomahawk-specific config
@ -201,6 +201,8 @@ public:
void setImportXspfPath( const QString& path );
QString importXspfPath() const;
static void registerCustomSettingsHandlers();
signals:
void changed();
void recentlyPlayedPlaylistAdded( const Tomahawk::playlist_ptr& playlist );

View File

@ -23,6 +23,38 @@
using namespace Tomahawk;
inline QDataStream& operator<<(QDataStream& out, const AtticaManager::StateHash& states)
{
out << TOMAHAWK_SETTINGS_VERSION;
out << (quint32)states.count();
foreach( const QString& key, states.keys() )
{
AtticaManager::Resolver resolver = states[ key ];
out << key << resolver.version << resolver.scriptPath << (qint32)resolver.state << resolver.userRating;
}
return out;
}
inline QDataStream& operator>>(QDataStream& in, AtticaManager::StateHash& states)
{
quint32 count = 0, version = 0;
in >> version;
in >> count;
for ( uint i = 0; i < count; i++ )
{
QString key, version, scriptPath;
qint32 state, userRating;
in >> key;
in >> version;
in >> scriptPath;
in >> state;
in >> userRating;
states[ key ] = AtticaManager::Resolver( version, scriptPath, userRating, (AtticaManager::ResolverState)state );
}
return in;
}
TomahawkSettingsGui*
TomahawkSettingsGui::instanceGui()
{
@ -91,3 +123,11 @@ TomahawkSettingsGui::removeAtticaResolverState ( const QString& resolver )
resolvers.remove( resolver );
setValue( "script/atticaresolverstates", QVariant::fromValue< AtticaManager::StateHash >( resolvers ) );
}
void
TomahawkSettingsGui::registerCustomSettingsHandlers()
{
qRegisterMetaType< AtticaManager::StateHash >( "AtticaManager::StateHash" );
qRegisterMetaTypeStreamOperators<AtticaManager::StateHash>("AtticaManager::StateHash");
}

View File

@ -47,6 +47,8 @@ public:
void setAtticaResolverState( const QString& resolver, AtticaManager::ResolverState state );
void removeAtticaResolverState( const QString& resolver );
static void registerCustomSettingsHandlers();
};
Q_DECLARE_METATYPE(AtticaManager::StateHash);

View File

@ -745,7 +745,7 @@ ViewManager::updateView()
m_infobar->setVisible( currentPage()->showInfoBar() );
m_infobar->setCaption( currentPage()->title() );
m_infobar->setAutoUpdateInterface( currentPage()->autoUpdateInterface() );
m_infobar->setUpdaters( currentPage()->updaters() );
switch( currentPage()->descriptionType() )
{

View File

@ -25,13 +25,14 @@
#include "Artist.h"
#include "Album.h"
#include "utils/TomahawkUtils.h"
#include "utils/SmartPointerList.h"
#include "playlist/PlaylistUpdaterInterface.h"
#include <QtGui/QPixmap>
namespace Tomahawk
{
class PlaylistUpdaterInterface;
class DLLEXPORT ViewPage
{
@ -69,8 +70,7 @@ public:
virtual bool isTemporaryPage() const { return false; }
virtual bool isBeingPlayed() const { return false; }
virtual bool canAutoUpdate() const { return false; }
virtual PlaylistUpdaterInterface* autoUpdateInterface() const { return 0; }
virtual SmartPointerList<PlaylistUpdaterInterface> updaters() const { return SmartPointerList<PlaylistUpdaterInterface>(); }
/** subclasses implementing ViewPage can emit the following signals:
* nameChanged( const QString& )

View File

@ -1,6 +1,7 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2012, Leo Franchi <lfranchi@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
@ -40,8 +41,6 @@ using namespace Tomahawk;
InfoBar::InfoBar( QWidget* parent )
: QWidget( parent )
, ui( new Ui::InfoBar )
, m_updaterInterface( 0 )
, m_updaterConfiguration( 0 )
, m_queryLabel( 0 )
{
ui->setupUi( this );
@ -202,21 +201,31 @@ InfoBar::setFilterAvailable( bool b )
void
InfoBar::setAutoUpdateInterface( PlaylistUpdaterInterface *interface )
InfoBar::setUpdaters( const SmartPointerList<PlaylistUpdaterInterface>& updaters )
{
if ( m_updaterConfiguration )
m_updaterConfiguration->hide();
QList< QWidget* > newUpdaterWidgets;
foreach ( PlaylistUpdaterInterface* updater, updaters )
{
if ( updater->configurationWidget() )
newUpdaterWidgets << updater->configurationWidget();
}
if ( m_updaterConfiguration && ( interface ? (m_updaterConfiguration != interface->configurationWidget()) : true ) )
ui->horizontalLayout->removeWidget( m_updaterConfiguration );
m_updaterInterface = interface;
m_updaterConfiguration = interface ? interface->configurationWidget() : 0;
foreach ( QWidget* updaterWidget, m_updaterConfigurations )
{
updaterWidget->hide();
if ( !m_updaterInterface || !m_updaterConfiguration )
return;
if ( !newUpdaterWidgets.contains( updaterWidget ) )
{
// Old config widget no longer present, remove it
ui->horizontalLayout->removeWidget( updaterWidget );
}
}
m_updaterConfiguration->setPalette( m_whitePal );
m_updaters = updaters;
m_updaterConfigurations = newUpdaterWidgets;
// Display each new widget in the proper place
int insertIdx = -1; // Ugh, no indexOf for QSpacerItem*
for ( int i = 0; i < ui->horizontalLayout->count(); i++ )
{
@ -227,9 +236,40 @@ InfoBar::setAutoUpdateInterface( PlaylistUpdaterInterface *interface )
}
}
insertIdx++;
ui->horizontalLayout->insertWidget( insertIdx, m_updaterConfiguration );
m_updaterConfiguration->show();
foreach ( QWidget* updaterWidget, m_updaterConfigurations )
{
updaterWidget->setPalette( m_whitePal );
ui->horizontalLayout->insertWidget( insertIdx, updaterWidget );
updaterWidget->show();
}
// if ( m_updaterConfiguration )
// m_updaterConfiguration->hide();
//
// if ( m_updaterConfiguration && ( interface ? (m_updaterConfiguration != interface->configurationWidget()) : true ) )
// ui->horizontalLayout->removeWidget( m_updaterConfiguration );
//
// m_updaterInterface = interface;
// m_updaterConfiguration = interface ? interface->configurationWidget() : 0;
//
// if ( !m_updaterInterface || !m_updaterConfiguration )
// return;
//
// m_updaterConfiguration->setPalette( m_whitePal );
// int insertIdx = -1; // Ugh, no indexOf for QSpacerItem*
// for ( int i = 0; i < ui->horizontalLayout->count(); i++ )
// {
// if ( ui->horizontalLayout->itemAt( i )->spacerItem() == ui->horizontalSpacer_4 )
// {
// insertIdx = i;
// break;
// }
// }
// insertIdx++;
// ui->horizontalLayout->insertWidget( insertIdx, m_updaterConfiguration );
//
// m_updaterConfiguration->show();
}

View File

@ -1,6 +1,7 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2012, Leo Franchi <lfranchi@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
@ -23,6 +24,7 @@
#include "DllMacro.h"
#include "Artist.h"
#include <utils/SmartPointerList.h>
class QueryLabel;
class QCheckBox;
@ -62,7 +64,7 @@ public slots:
void setFilter( const QString& filter );
void setFilterAvailable( bool b );
void setAutoUpdateInterface( Tomahawk::PlaylistUpdaterInterface* interface );
void setUpdaters( const SmartPointerList<Tomahawk::PlaylistUpdaterInterface>& updaters );
signals:
void filterTextChanged( const QString& filter );
@ -82,8 +84,8 @@ private:
QPixmap m_bgTile;
QPalette m_whitePal;
Tomahawk::PlaylistUpdaterInterface* m_updaterInterface;
QWidget* m_updaterConfiguration;
SmartPointerList<Tomahawk::PlaylistUpdaterInterface> m_updaters;;
QList<QWidget*> m_updaterConfigurations;
QSearchField* m_searchWidget;
QueryLabel* m_queryLabel;

View File

@ -63,7 +63,7 @@ PlaylistUpdaterInterface::PlaylistUpdaterInterface( const playlist_ptr& pl )
{
Q_ASSERT( !m_playlist.isNull() );
m_playlist->setUpdater( this );
m_playlist->addUpdater( this );
QTimer::singleShot( 0, this, SLOT( save() ) );
}

View File

@ -43,6 +43,17 @@ class DLLEXPORT PlaylistUpdaterInterface : public QObject
{
Q_OBJECT
public:
// used when loading/saving from settings
struct SerializedUpdater {
QString type;
bool sync;
QVariantHash customData;
SerializedUpdater( const QString& t, bool s, const QVariantHash cd ) : type( t ), sync( s ), customData( cd ) {}
};
typedef QHash< QString, SerializedUpdater > SerializedUpdaters;
explicit PlaylistUpdaterInterface( const playlist_ptr& pl );
virtual ~PlaylistUpdaterInterface(){}

View File

@ -109,23 +109,13 @@ PlaylistView::deleteItems()
}
bool
PlaylistView::canAutoUpdate() const
SmartPointerList<PlaylistUpdaterInterface>
PlaylistView::updaters() const
{
if ( !m_model->playlist().isNull() && m_model->playlist()->updater() )
return true;
if ( !m_model->playlist().isNull() )
return m_model->playlist()->updaters();
return false;
}
PlaylistUpdaterInterface*
PlaylistView::autoUpdateInterface() const
{
if ( !m_model->playlist().isNull() && m_model->playlist()->updater() )
return m_model->playlist()->updater();
return 0;
return SmartPointerList<PlaylistUpdaterInterface>();
}

View File

@ -43,8 +43,7 @@ public:
virtual bool showFilter() const { return true; }
virtual bool canAutoUpdate() const;
virtual Tomahawk::PlaylistUpdaterInterface* autoUpdateInterface() const;
virtual SmartPointerList<Tomahawk::PlaylistUpdaterInterface> updaters() const;
virtual QString title() const { return playlistModel()->title(); }
virtual QString description() const { return m_model->description(); }

View File

@ -57,7 +57,7 @@ XspfUpdater::XspfUpdater( const playlist_ptr& pl, int interval, bool autoUpdate,
#ifndef ENABLE_HEADLESS
m_toggleCheckbox = new QCheckBox( );
m_toggleCheckbox->setText( tr( "Automatically update" ) );
m_toggleCheckbox->setText( tr( "Automatically update from XSPF" ) );
m_toggleCheckbox->setLayoutDirection( Qt::RightToLeft );
m_toggleCheckbox->setChecked( m_autoUpdate );
m_toggleCheckbox->hide();

View File

@ -136,6 +136,11 @@ public:
return *this;
}
bool operator==( const SmartPointerList& that )
{
return QList<T*>::operator==( that );
}
void push_back( T* o )
{
append( o );
@ -180,7 +185,7 @@ public:
using QList<T*>::swap;
using QList<T*>::value;
using QList<T*>::operator!=;
using QList<T*>::operator==;
// using QList<T*>::operator==;
// can't use using directive here since we only want the const versions
typename QList<T*>::const_iterator begin() const { return QList<T*>::constBegin(); }

View File

@ -37,38 +37,6 @@
#include "breakpad/BreakPad.h"
#endif
inline QDataStream& operator<<(QDataStream& out, const AtticaManager::StateHash& states)
{
out << TOMAHAWK_SETTINGS_VERSION;
out << (quint32)states.count();
foreach( const QString& key, states.keys() )
{
AtticaManager::Resolver resolver = states[ key ];
out << key << resolver.version << resolver.scriptPath << (qint32)resolver.state << resolver.userRating;
}
return out;
}
inline QDataStream& operator>>(QDataStream& in, AtticaManager::StateHash& states)
{
quint32 count = 0, version = 0;
in >> version;
in >> count;
for ( uint i = 0; i < count; i++ )
{
QString key, version, scriptPath;
qint32 state, userRating;
in >> key;
in >> version;
in >> scriptPath;
in >> state;
in >> userRating;
states[ key ] = AtticaManager::Resolver( version, scriptPath, userRating, (AtticaManager::ResolverState)state );
}
return in;
}
#ifdef Q_OS_WIN
#include <io.h>
#define argc __argc
@ -115,8 +83,8 @@ main( int argc, char *argv[] )
TomahawkApp a( argc, argv );
// MUST register StateHash ****before*** initing TomahawkSettingsGui as constructor of settings does upgrade before Gui subclass registers type
qRegisterMetaType< AtticaManager::StateHash >( "AtticaManager::StateHash" );
qRegisterMetaTypeStreamOperators<AtticaManager::StateHash>("AtticaManager::StateHash");
TomahawkSettings::registerCustomSettingsHandlers();
TomahawkSettingsGui::registerCustomSettingsHandlers();
#ifdef ENABLE_HEADLESS
new TomahawkSettings( &a );

View File

@ -52,7 +52,7 @@ PlaylistItem::PlaylistItem( SourcesModel* mdl, SourceTreeItem* parent, const pla
if( ViewManager::instance()->pageForPlaylist( pl ) )
model()->linkSourceItemToPage( this, ViewManager::instance()->pageForPlaylist( pl ) );
if ( m_playlist->updater() && !m_playlist->updater()->typeIcon().isNull() )
if ( !m_playlist->updaters().isEmpty() )
createOverlay();
}
@ -251,40 +251,66 @@ PlaylistItem::parsedDroppedTracks( const QList< query_ptr >& tracks )
void
PlaylistItem::onUpdated()
{
if ( m_playlist->updater() && !m_playlist->updater()->typeIcon().isNull() &&
m_overlaidIcon.isNull() ) // create overlay
// No work todo
if ( !m_overlaidIcon.isNull() && m_overlaidUpdaters.operator==( m_playlist->updaters() ) )
{
createOverlay();
emit updated();
return;
}
else if ( !m_playlist->updater() || ( m_playlist->updater()->typeIcon().isNull() && !m_overlaidIcon.isNull() ) )
{
// No longer an updater with an icon
const bool newOverlay = createOverlay();
if ( !newOverlay && !m_overlaidIcon.isNull() )
m_overlaidIcon = QIcon();
}
emit updated();
}
void
bool
PlaylistItem::createOverlay()
{
Q_ASSERT( !m_playlist.isNull() );
Q_ASSERT( m_playlist->updater() );
Q_ASSERT( !m_playlist->updater()->typeIcon().isNull() );
if ( m_playlist->updaters().isEmpty() )
return false;
QList< QPixmap > icons;
foreach ( PlaylistUpdaterInterface* updater, m_playlist->updaters() )
{
if ( !updater->typeIcon().isNull() )
icons << updater->typeIcon();
}
if ( icons.isEmpty() )
return false;
// For now we only support up to 2 overlaid updater icons,
// we need to add smarter scaling etc to manage more at once
if ( icons.size() > 2 )
icons = icons.mid( 0, 2 );
m_overlaidIcon = QIcon();
m_overlaidUpdaters = m_playlist->updaters();
QPixmap base = m_icon.pixmap( 48, 48 );
const QPixmap overlay = m_playlist->updater()->typeIcon();
QPainter p( &base );
const int w = base.width() / 2;
const QRect overlayRect( base.rect().right() - w, base.rect().height() - w, w, w );
p.drawPixmap( overlayRect, overlay );
QRect overlayRect( base.rect().right() - w, base.rect().height() - w, w, w );
foreach ( const QPixmap& overlay, icons )
{
p.drawPixmap( overlayRect, overlay );
// NOTE only works if icons.size == 2 as ensured above
overlayRect.moveLeft( 0 );
}
p.end();
m_overlaidIcon.addPixmap( base );
return true;
}

View File

@ -21,6 +21,7 @@
#include "SourceTreeItem.h"
#include "playlist/dynamic/DynamicPlaylist.h"
#include "utils/SmartPointerList.h"
class PlaylistItem : public SourceTreeItem
{
@ -56,11 +57,12 @@ private slots:
void onUpdated();
private:
void createOverlay();
bool createOverlay();
bool m_loaded;
Tomahawk::playlist_ptr m_playlist;
QIcon m_icon, m_overlaidIcon;
SmartPointerList<Tomahawk::PlaylistUpdaterInterface> m_overlaidUpdaters;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(PlaylistItem::DropTypes)