1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-21 16:29:43 +01:00
This commit is contained in:
Leo Franchi 2012-04-30 08:33:33 -04:00
parent 17bcaf7369
commit 319a03b791
8 changed files with 115 additions and 70 deletions

View File

@ -32,7 +32,7 @@ QPixmap* SpotifyPlaylistUpdater::s_typePixmap = 0;
#endif
Tomahawk::PlaylistUpdaterInterface*
SpotifyUpdaterFactory::create( const Tomahawk::playlist_ptr& pl, const QString &key )
SpotifyUpdaterFactory::create( const Tomahawk::playlist_ptr& pl, const QVariantHash &settings )
{
if ( !m_account )
{
@ -54,9 +54,9 @@ SpotifyUpdaterFactory::create( const Tomahawk::playlist_ptr& pl, const QString &
}
// Register the updater with the account
const QString spotifyId = TomahawkSettings::instance()->value( QString( "%1/spotifyId" ).arg( key ) ).toString();
const QString latestRev = TomahawkSettings::instance()->value( QString( "%1/latestrev" ).arg( key ) ).toString();
const bool sync = TomahawkSettings::instance()->value( QString( "%1/sync" ).arg( key ) ).toBool();
const QString spotifyId = settings.value( "spotifyId" ).toString();
const QString latestRev = settings.value( "%1/latestrev" ).toString();
const bool sync = settings.value( "%1/sync" ).toBool();
Q_ASSERT( !spotifyId.isEmpty() );
SpotifyPlaylistUpdater* updater = new SpotifyPlaylistUpdater( m_account.data(), latestRev, spotifyId, pl );
@ -89,6 +89,8 @@ SpotifyPlaylistUpdater::init()
connect( playlist().data(), SIGNAL( renamed( QString, QString ) ), this, SLOT( tomahawkPlaylistRenamed( QString, QString ) ) );
connect( playlist().data(), SIGNAL( revisionLoaded( Tomahawk::PlaylistRevision ) ), this, SLOT( playlistRevisionLoaded() ), Qt::QueuedConnection ); // Queued so that in Playlist.cpp:443 we let the playlist clear its own queue first
// TODO reorders in a playlist
saveToSettings();
}
@ -122,12 +124,8 @@ SpotifyPlaylistUpdater::remove( bool askToDeletePlaylist )
void
SpotifyPlaylistUpdater::removeFromSettings( const QString& group ) const
SpotifyPlaylistUpdater::aboutToDelete()
{
TomahawkSettings::instance()->remove( QString( "%1/latestrev" ).arg( group ) );
TomahawkSettings::instance()->remove( QString( "%1/sync" ).arg( group ) );
TomahawkSettings::instance()->remove( QString( "%1/spotifyId" ).arg( group ) );
if ( m_sync )
{
if ( QThread::currentThread() != QApplication::instance()->thread() )
@ -173,11 +171,15 @@ SpotifyPlaylistUpdater::playlistRevisionLoaded()
void
SpotifyPlaylistUpdater::saveToSettings( const QString& group ) const
SpotifyPlaylistUpdater::saveToSettings()
{
TomahawkSettings::instance()->setValue( QString( "%1/latestrev" ).arg( group ), m_latestRev );
TomahawkSettings::instance()->setValue( QString( "%1/sync" ).arg( group ), m_sync );
TomahawkSettings::instance()->setValue( QString( "%1/spotifyId" ).arg( group ), m_spotifyId );
QVariantHash s = settings();
s[ "latestrev" ] = m_latestRev;
s[ "sync" ] = m_sync;
s[ "spotifyId" ] = m_spotifyId;
saveSettings( s );
}
@ -214,6 +216,7 @@ SpotifyPlaylistUpdater::setSync( bool sync )
m_sync = sync;
saveToSettings();
emit changed();
}
@ -543,6 +546,9 @@ SpotifyPlaylistUpdater::onTracksInsertedReturn( const QString& msgType, const QV
if ( changed.size() > 0 )
playlist()->updateEntries( uuid(), playlist()->currentrevision(), changed );
// Update with latest rev when/if we use it
// saveToSettings();
}

View File

@ -67,9 +67,9 @@ public slots:
void tomahawkTracksRemoved( const QList<Tomahawk::query_ptr>& );
void tomahawkTracksMoved( const QList<Tomahawk::plentry_ptr>& ,int );
void tomahawkPlaylistRenamed( const QString&, const QString& );
protected:
virtual void removeFromSettings(const QString& group) const;
virtual void saveToSettings(const QString& group) const;
void aboutToDelete();
private slots:
// SpotifyResolver message handlers, all take msgtype, msg as argument
@ -82,6 +82,8 @@ private slots:
void playlistRevisionLoaded();
private:
void init();
void saveToSettings();
/// Finds the nearest spotify id from pos to the beginning of the playlist
QString nearestSpotifyTrack( const QList< Tomahawk::plentry_ptr >& entries, int pos );
QVariantList plentryToVariant( const QList< Tomahawk::plentry_ptr >& entries );
@ -109,7 +111,7 @@ class SpotifyUpdaterFactory : public Tomahawk::PlaylistUpdaterFactory
public:
SpotifyUpdaterFactory() {}
virtual Tomahawk::PlaylistUpdaterInterface* create( const Tomahawk::playlist_ptr& pl, const QString& key );
virtual Tomahawk::PlaylistUpdaterInterface* create( const Tomahawk::playlist_ptr& pl, const QVariantHash& settings );
virtual QString type() const { return "spotify"; }
private:

View File

@ -45,7 +45,7 @@ operator<<(QDataStream& out, const PlaylistUpdaterInterface::SerializedUpdaters&
foreach( const QString& key, updaters.keys() )
{
PlaylistUpdaterInterface::SerializedUpdater updater = updaters[ key ];
out << key << updater.type << updater.sync << updater.customData;
out << key << updater.type << updater.customData;
}
return out;
}
@ -66,9 +66,8 @@ operator>>(QDataStream& in, PlaylistUpdaterInterface::SerializedUpdaters& update
qint32 state, userRating;
in >> key;
in >> type;
in >> sync;
in >> customData;
updaters[ key ] = PlaylistUpdaterInterface::SerializedUpdater( type, sync, customData );
updaters[ key ] = PlaylistUpdaterInterface::SerializedUpdater( type, customData );
}
return in;
@ -1219,3 +1218,26 @@ TomahawkSettings::setImportXspfPath( const QString& path )
{
setValue( "importXspfPath", path );
}
PlaylistUpdaterInterface::SerializedUpdaters
TomahawkSettings::playlistUpdaters() const
{
return value( "playlistupdaters" ).value< PlaylistUpdaterInterface::SerializedUpdaters >();
}
void
TomahawkSettings::setPlaylistUpdaters( const PlaylistUpdaterInterface::SerializedUpdaters& updaters )
{
setValue( "playlistupdaters", QVariant::fromValue< PlaylistUpdaterInterface::SerializedUpdaters >( updaters ) );
}
void
TomahawkSettings::registerCustomSettingsHandlers()
{
qRegisterMetaType< Tomahawk::PlaylistUpdaterInterface::SerializedUpdater >( "Tomahawk::PlaylistUpdaterInterface::SerializedUpdater" );
qRegisterMetaType< Tomahawk::PlaylistUpdaterInterface::SerializedUpdaters >( "Tomahawk::PlaylistUpdaterInterface::SerializedUpdaters" );
qRegisterMetaTypeStreamOperators< Tomahawk::PlaylistUpdaterInterface::SerializedUpdaters >( "Tomahawk::PlaylistUpdaterInterface::SerializedUpdaters" );
}

View File

@ -23,6 +23,8 @@
#include "Playlist.h"
#include "playlist/PlaylistUpdaterInterface.h"
#include <QSettings>
#include <QtNetwork/QNetworkProxy>
@ -201,6 +203,9 @@ public:
void setImportXspfPath( const QString& path );
QString importXspfPath() const;
Tomahawk::PlaylistUpdaterInterface::SerializedUpdaters playlistUpdaters() const;
void setPlaylistUpdaters( const Tomahawk::PlaylistUpdaterInterface::SerializedUpdaters& updaters );
static void registerCustomSettingsHandlers();
signals:

View File

@ -35,21 +35,22 @@ PlaylistUpdaterInterface*
PlaylistUpdaterInterface::loadForPlaylist( const playlist_ptr& pl )
{
TomahawkSettings* s = TomahawkSettings::instance();
const QString key = QString( "playlistupdaters/%1" ).arg( pl->guid() );
if ( s->contains( QString( "%1/type" ).arg( key ) ) )
const SerializedUpdaters updaters = s->playlistUpdaters();
if ( updaters.contains( pl->guid() ) )
{
// Ok, we have one we can try to load
const QString type = s->value( QString( "%1/type" ).arg( key ) ).toString();
PlaylistUpdaterInterface* updater = 0;
const SerializedUpdater info = updaters[ pl->guid() ];
if ( !s_factories.contains( type ) )
if ( !s_factories.contains( info.type ) )
{
Q_ASSERT( false );
// You forgot to register your new updater type with the factory....
return 0;
}
updater = s_factories[ type ]->create( pl, key );
updater = s_factories[ info.type ]->create( pl, info.customData );
return updater;
}
@ -72,13 +73,19 @@ PlaylistUpdaterInterface::PlaylistUpdaterInterface( const playlist_ptr& pl )
void
PlaylistUpdaterInterface::save()
{
if ( m_playlist.isNull() )
return;
TomahawkSettings* s = TomahawkSettings::instance();
const QString key = QString( "playlistupdaters/%1" ).arg( m_playlist->guid() );
if ( !s->contains( QString( "%1/type" ).arg( key ) ) )
{
s->setValue( QString( "%1/type" ).arg( key ), type() );
}
saveToSettings( key );
SerializedUpdaters updaters = s->playlistUpdaters();
SerializedUpdater updater = updaters.value( m_playlist ->guid() );
updater.type = type();
updater.customData = m_extraData;
updaters[ m_playlist->guid() ] = updater;
s->setPlaylistUpdaters( updaters );
}
void
@ -88,9 +95,27 @@ PlaylistUpdaterInterface::remove()
return;
TomahawkSettings* s = TomahawkSettings::instance();
const QString key = QString( "playlistupdaters/%1" ).arg( m_playlist->guid() );
removeFromSettings( key );
s->remove( QString( "%1/type" ).arg( key ) );
SerializedUpdaters updaters = s->playlistUpdaters();
if ( updaters.remove( m_playlist->guid() ) )
s->setPlaylistUpdaters( updaters );
aboutToDelete();
deleteLater();
}
QVariantHash
PlaylistUpdaterInterface::settings() const
{
return m_extraData;
}
void
PlaylistUpdaterInterface::saveSettings( const QVariantHash& settings )
{
m_extraData = settings;
save();
}

View File

@ -46,10 +46,10 @@ 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 ) {}
SerializedUpdater( const QString& t, const QVariantHash cd ) : type( t ), customData( cd ) {}
SerializedUpdater() {}
};
typedef QHash< QString, SerializedUpdater > SerializedUpdaters;
@ -89,11 +89,14 @@ public slots:
void save();
protected:
virtual void saveToSettings( const QString& group ) const = 0;
virtual void removeFromSettings( const QString& group ) const = 0;
virtual void aboutToDelete() {}
QVariantHash settings() const;
void saveSettings( const QVariantHash& settings );
private:
playlist_ptr m_playlist;
QVariantHash m_extraData;
static QMap< QString, PlaylistUpdaterFactory* > s_factories;
};
@ -106,9 +109,12 @@ public:
virtual ~PlaylistUpdaterFactory() {}
virtual QString type() const = 0;
virtual PlaylistUpdaterInterface* create( const playlist_ptr&, const QString& settingsKey ) = 0;
virtual PlaylistUpdaterInterface* create( const playlist_ptr&, const QVariantHash& settings ) = 0;
};
}
Q_DECLARE_METATYPE( Tomahawk::PlaylistUpdaterInterface::SerializedUpdater );
Q_DECLARE_METATYPE( Tomahawk::PlaylistUpdaterInterface::SerializedUpdaters );
#endif // PLAYLISTUPDATERINTERFACE_H

View File

@ -20,7 +20,6 @@
#include "Playlist.h"
#include "utils/XspfLoader.h"
#include "TomahawkSettings.h"
#include "Pipeline.h"
#include "utils/TomahawkUtils.h"
@ -33,11 +32,11 @@
using namespace Tomahawk;
PlaylistUpdaterInterface*
XspfUpdaterFactory::create( const playlist_ptr &pl, const QString& settingsKey )
XspfUpdaterFactory::create( const playlist_ptr &pl, const QVariantHash& settings )
{
const bool autoUpdate = TomahawkSettings::instance()->value( QString( "%1/autoupdate" ).arg( settingsKey ) ).toBool();
const int interval = TomahawkSettings::instance()->value( QString( "%1/interval" ).arg( settingsKey ) ).toInt();
const QString url = TomahawkSettings::instance()->value( QString( "%1/xspfurl" ).arg( settingsKey ) ).toString();
const bool autoUpdate = settings.value( "autoupdate" ).toBool();
const int interval = settings.value( "interval" ).toInt();
const QString url = settings.value( "xspfurl" ).toString();
XspfUpdater* updater = new XspfUpdater( pl, interval, autoUpdate, url );
@ -113,24 +112,6 @@ XspfUpdater::playlistLoaded( const QList<Tomahawk::query_ptr>& newEntries )
}
void
XspfUpdater::saveToSettings( const QString& group ) const
{
TomahawkSettings::instance()->setValue( QString( "%1/autoupdate" ).arg( group ), m_autoUpdate );
TomahawkSettings::instance()->setValue( QString( "%1/interval" ).arg( group ), m_timer->interval() );
TomahawkSettings::instance()->setValue( QString( "%1/xspfurl" ).arg( group ), m_url );
}
void
XspfUpdater::removeFromSettings( const QString& group ) const
{
TomahawkSettings::instance()->remove( QString( "%1/autoupdate" ).arg( group ) );
TomahawkSettings::instance()->remove( QString( "%1/interval" ).arg( group ) );
TomahawkSettings::instance()->remove( QString( "%1/xspfurl" ).arg( group ) );
}
void
XspfUpdater::setAutoUpdate( bool autoUpdate )
{
@ -141,8 +122,9 @@ XspfUpdater::setAutoUpdate( bool autoUpdate )
else
m_timer->stop();
const QString key = QString( "playlistupdaters/%1/autoupdate" ).arg( playlist()->guid() );
TomahawkSettings::instance()->setValue( key, m_autoUpdate );
QVariantHash s = settings();
s[ "autoupdate" ] = m_autoUpdate;
saveSettings( s );
// Update immediately as well
if ( m_autoUpdate )
@ -152,8 +134,9 @@ XspfUpdater::setAutoUpdate( bool autoUpdate )
void
XspfUpdater::setInterval( int intervalMsecs )
{
const QString key = QString( "playlistupdaters/%1/interval" ).arg( playlist()->guid() );
TomahawkSettings::instance()->setValue( key, intervalMsecs );
QVariantHash s = settings();
s[ "interval" ] = intervalMsecs;
saveSettings( s );
if ( !m_timer )
m_timer = new QTimer( this );

View File

@ -52,10 +52,6 @@ public slots:
void updateNow();
void setAutoUpdate( bool autoUpdate );
protected:
void saveToSettings( const QString& group ) const;
void removeFromSettings(const QString& group) const;
private slots:
void playlistLoaded( const QList<Tomahawk::query_ptr> & );
@ -75,7 +71,7 @@ public:
XspfUpdaterFactory() {}
virtual QString type() const { return "xspf"; }
virtual PlaylistUpdaterInterface* create( const playlist_ptr& pl, const QString& settingsKey );
virtual PlaylistUpdaterInterface* create( const playlist_ptr& pl, const QVariantHash& settings );
};
}