diff --git a/src/accounts/spotify/SpotifyAccount.cpp b/src/accounts/spotify/SpotifyAccount.cpp index 52f07c37b..554551633 100644 --- a/src/accounts/spotify/SpotifyAccount.cpp +++ b/src/accounts/spotify/SpotifyAccount.cpp @@ -23,6 +23,7 @@ #include "playlist/PlaylistUpdaterInterface.h" #include "sourcelist.h" #include "SpotifyAccountConfig.h" +#include "SpotifyPlaylistUpdater.h" #include "resolvers/scriptresolver.h" #include @@ -80,7 +81,7 @@ SpotifyAccount::SpotifyAccount( const QString& accountId, const QString& path ) void SpotifyAccount::init() { - qRegisterMetaType< Tomahawk::Accounts::SpotifyPlaylist* >( "Tomahawk::Accounts::SpotifyPlaylist*" ); + qRegisterMetaType< Tomahawk::Accounts::SpotifyPlaylistInfo* >( "Tomahawk::Accounts::SpotifyPlaylist*" ); m_spotifyResolver = dynamic_cast< ScriptResolver* >( m_resolver.data() ); @@ -151,7 +152,7 @@ SpotifyAccount::resolverMessage( const QString &msgType, const QVariantMap &msg qDebug() << "Did not get name and plid and revid for spotify playlist:" << name << plid << revid << plMap; continue; } - m_allSpotifyPlaylists << new SpotifyPlaylist( name, plid, revid, sync ); + m_allSpotifyPlaylists << new SpotifyPlaylistInfo( name, plid, revid, sync ); } if ( !m_configWidget.isNull() ) @@ -231,13 +232,21 @@ SpotifyAccount::saveConfig() } m_configWidget.data()->saveSettings(); - foreach ( SpotifyPlaylist* pl, m_allSpotifyPlaylists ) + foreach ( SpotifyPlaylistInfo* pl, m_allSpotifyPlaylists ) { if ( pl->changed ) { pl->changed = false; if ( pl->sync ) - startPlaylistSync( pl ); + { + // Fetch full playlist contents, then begin the sync + QVariantMap msg; + msg[ "_msgtype" ] = "getPlaylist"; + msg[ "playlistid" ] = pl->plid; + msg[ "sync" ] = pl->sync; + + sendMessage( msg, "startPlaylistSyncWithPlaylist" ); + } else stopPlaylistSync( pl ); } @@ -245,6 +254,39 @@ SpotifyAccount::saveConfig() } +void +SpotifyAccount::startPlaylistSyncWithPlaylist( const QString& msgType, const QVariantMap& msg ) +{ + qDebug() << Q_FUNC_INFO << "Got full spotify playlist body, creating a tomahawk playlist and enabling sync!!"; + const QString id = msg.value( "id" ).toString(); + const QString name = msg.value( "name" ).toString(); + + qDebug() << "Starting sync with pl:" << id << name; + QVariantList tracks = msg.value( "tracks" ).toList(); + + // create a list of query/plentries directly + QList< query_ptr > queries; + foreach ( const QVariant& trackV, tracks ) + { + const QVariantMap trackMap = trackV.toMap(); + qDebug() << "Adding spotify track to new tomahawk playlist:" << trackMap.value( "track" ).toString() << trackMap.value( "artist" ).toString() << trackMap.value( "album" ).toString(); + query_ptr q = Query::get( trackMap.value( "artist" ).toString(), trackMap.value( "track" ).toString(), trackMap.value( "album" ).toString(), uuid(), false ); + q->setProperty( "spotifytrackid", trackMap.value( "id" ) ); + + queries << q; + } + + playlist_ptr plPtr = Tomahawk::Playlist::create( SourceList::instance()->getLocal(), + uuid(), + name, + QString(), + QString(), + false, + queries ); + SpotifyPlaylistUpdater* updater = new SpotifyPlaylistUpdater( this, plPtr ); +} + + void SpotifyAccount::addPlaylist( const QString &qid, const QString& title, QList< Tomahawk::query_ptr > tracks ) { @@ -312,14 +354,35 @@ SpotifyAccount::sendMessage( const QVariantMap &m, const QString& slot ) void -SpotifyAccount::startPlaylistSync( SpotifyPlaylist* playlist ) +SpotifyAccount::fetchFullPlaylist( SpotifyPlaylistInfo* playlist ) { } void -SpotifyAccount::stopPlaylistSync( SpotifyPlaylist* playlist ) +SpotifyAccount::startPlaylistSync( SpotifyPlaylistInfo* playlist ) +{ + /** + * Begin syncing a playlist. Two options: + * 1) This is a playlist that has never been synced to tomahawk. Create a new one + * and attach a new SpotifyPlaylistUpdater to it + * 2) This was previously synced, and has since been unsynced. THe playlist is still around + * with an inactive SpotifyPlaylistUpdater, so just enable it and bring it up to date. + */ + if ( m_updaters.contains( playlist->plid ) ) + { + // update and re-sync + } + else + { + + } +} + + +void +SpotifyAccount::stopPlaylistSync( SpotifyPlaylistInfo* playlist ) { } @@ -334,12 +397,3 @@ SpotifyAccount::loadPlaylists() msg[ "_msgtype" ] = "getAllPlaylists"; sendMessage( msg, "allPlaylistsLoaded" ); } - - -// bool -// operator==( Accounts::SpotifyAccount::Sync one, Accounts::SpotifyAccount::Sync two ) -// { -// if( one.id_ == two.id_ ) -// return true; -// return false; -// } diff --git a/src/accounts/spotify/SpotifyAccount.h b/src/accounts/spotify/SpotifyAccount.h index 1badb2f49..f8eac701a 100644 --- a/src/accounts/spotify/SpotifyAccount.h +++ b/src/accounts/spotify/SpotifyAccount.h @@ -25,6 +25,7 @@ #include "sourcelist.h" #include "accounts/ResolverAccount.h" +class SpotifyPlaylistUpdater; class QTimer; class ScriptResolver; @@ -34,16 +35,19 @@ namespace Accounts { class SpotifyAccountConfig; -struct SpotifyPlaylist { +// metadata for a playlist +struct SpotifyPlaylistInfo { QString name, plid, revid; bool sync, changed; - SpotifyPlaylist( const QString& nname, const QString& pid, const QString& rrevid, bool ssync ) + + SpotifyPlaylistInfo( const QString& nname, const QString& pid, const QString& rrevid, bool ssync ) : name( nname ), plid( pid ), revid( rrevid ), sync( ssync ), changed( false ) {} - SpotifyPlaylist() : sync( false ), changed( false ) {} + SpotifyPlaylistInfo() : sync( false ), changed( false ) {} }; + class SpotifyAccountFactory : public AccountFactory { Q_OBJECT @@ -94,13 +98,17 @@ private slots: // SpotifyResolver message handlers, all take msgtype, msg as argument // void ( const QString& msgType, const QVariantMap& msg ); + void startPlaylistSyncWithPlaylist( const QString& msgType, const QVariantMap& msg ); + private: void init(); void loadPlaylists(); void sendMessage( const QVariantMap& msg, const QString& slot ); - void startPlaylistSync( SpotifyPlaylist* playlist ); - void stopPlaylistSync( SpotifyPlaylist* playlist ); + void startPlaylistSync( SpotifyPlaylistInfo* playlist ); + void stopPlaylistSync( SpotifyPlaylistInfo* playlist ); + + void fetchFullPlaylist( SpotifyPlaylistInfo* playlist ); // QList m_syncPlaylists; QWeakPointer m_configWidget; @@ -108,12 +116,15 @@ private: QMap m_qidToSlotMap; - QList< SpotifyPlaylist* > m_allSpotifyPlaylists; + // List of synced spotify playlists in config UI + QList< SpotifyPlaylistInfo* > m_allSpotifyPlaylists; + QHash< QString, SpotifyPlaylistUpdater* > m_updaters; + }; } } -Q_DECLARE_METATYPE( Tomahawk::Accounts::SpotifyPlaylist* ); +Q_DECLARE_METATYPE( Tomahawk::Accounts::SpotifyPlaylistInfo* ); #endif // SpotifyAccount_H diff --git a/src/accounts/spotify/SpotifyAccountConfig.cpp b/src/accounts/spotify/SpotifyAccountConfig.cpp index 4403e2b9e..c55f72189 100644 --- a/src/accounts/spotify/SpotifyAccountConfig.cpp +++ b/src/accounts/spotify/SpotifyAccountConfig.cpp @@ -52,7 +52,7 @@ SpotifyAccountConfig::saveSettings() for( int i = 0; i < m_ui->playlistList->count(); i++ ) { const QListWidgetItem* item = m_ui->playlistList->itemAt( i, 0 ); - SpotifyPlaylist* pl = item->data( Qt::UserRole ).value< SpotifyPlaylist* >(); + SpotifyPlaylistInfo* pl = item->data( Qt::UserRole ).value< SpotifyPlaylistInfo* >(); const bool toSync = ( item->checkState() == Qt::Checked ); if ( pl->sync != toSync ) { @@ -83,13 +83,13 @@ SpotifyAccountConfig::highQuality() const void -SpotifyAccountConfig::setPlaylists( const QList& playlists ) +SpotifyAccountConfig::setPlaylists( const QList& playlists ) { m_ui->playlistList->clear(); - foreach ( SpotifyPlaylist* pl, playlists ) + foreach ( SpotifyPlaylistInfo* pl, playlists ) { QListWidgetItem* item = new QListWidgetItem( pl->name, m_ui->playlistList ); - item->setData( Qt::UserRole, QVariant::fromValue< SpotifyPlaylist* >( pl ) ); + item->setData( Qt::UserRole, QVariant::fromValue< SpotifyPlaylistInfo* >( pl ) ); item->setFlags( Qt::ItemIsUserCheckable | Qt::ItemIsSelectable | Qt::ItemIsEnabled ); item->setCheckState( pl->sync ? Qt::Checked : Qt::Unchecked ); } diff --git a/src/accounts/spotify/SpotifyAccountConfig.h b/src/accounts/spotify/SpotifyAccountConfig.h index 35f9ed559..c49a9b21a 100644 --- a/src/accounts/spotify/SpotifyAccountConfig.h +++ b/src/accounts/spotify/SpotifyAccountConfig.h @@ -32,7 +32,7 @@ namespace Accounts { class SpotifyAccount; -struct SpotifyPlaylist; +struct SpotifyPlaylistInfo; class SpotifyAccountConfig : public QWidget { @@ -44,7 +44,7 @@ public: QString password() const; bool highQuality() const; - void setPlaylists( const QList< SpotifyPlaylist* >& playlists ); + void setPlaylists( const QList< SpotifyPlaylistInfo* >& playlists ); void loadFromConfig(); void saveSettings(); diff --git a/src/accounts/spotify/SpotifyPlaylistUpdater.cpp b/src/accounts/spotify/SpotifyPlaylistUpdater.cpp index 1c7958125..785fdc6eb 100644 --- a/src/accounts/spotify/SpotifyPlaylistUpdater.cpp +++ b/src/accounts/spotify/SpotifyPlaylistUpdater.cpp @@ -18,7 +18,35 @@ #include "SpotifyPlaylistUpdater.h" -SpotifyPlaylistUpdater::SpotifyPlaylistUpdater(const Tomahawk::playlist_ptr& pl): PlaylistUpdaterInterface(pl) +#include "accounts/AccountManager.h" +#include "SpotifyAccount.h" + +using namespace Tomahawk; +using namespace Accounts; + +Tomahawk::PlaylistUpdaterInterface* +SpotifyUpdaterFactory::create( const Tomahawk::playlist_ptr& pl ) +{ + if ( !m_account ) + { + // Find the spotify account + foreach ( Account* account, AccountManager::instance()->accounts() ) + { + if ( SpotifyAccount* spotify = qobject_cast< SpotifyAccount* >( account ) ) + { + m_account = spotify; + break; + } + } + } + + return new SpotifyPlaylistUpdater( m_account, pl ); +} + + +SpotifyPlaylistUpdater::SpotifyPlaylistUpdater( SpotifyAccount* acct, const playlist_ptr& pl ) + : PlaylistUpdaterInterface( pl ) + , m_spotify( acct ) { } diff --git a/src/accounts/spotify/SpotifyPlaylistUpdater.h b/src/accounts/spotify/SpotifyPlaylistUpdater.h index fc29a4d3f..24c39fb30 100644 --- a/src/accounts/spotify/SpotifyPlaylistUpdater.h +++ b/src/accounts/spotify/SpotifyPlaylistUpdater.h @@ -32,7 +32,7 @@ class SpotifyPlaylistUpdater : public Tomahawk::PlaylistUpdaterInterface { Q_OBJECT public: - SpotifyPlaylistUpdater( const Tomahawk::playlist_ptr& pl ); + SpotifyPlaylistUpdater( Tomahawk::Accounts::SpotifyAccount* acct, const Tomahawk::playlist_ptr& pl ); virtual ~SpotifyPlaylistUpdater(); @@ -52,10 +52,13 @@ private: class SpotifyUpdaterFactory : public Tomahawk::PlaylistUpdaterFactory { public: - SpotifyUpdaterFactory() {} + SpotifyUpdaterFactory() : m_account( 0 ) {} - virtual Tomahawk::PlaylistUpdaterInterface* create( const Tomahawk::playlist_ptr& pl ) { return new SpotifyPlaylistUpdater( pl ); } + virtual Tomahawk::PlaylistUpdaterInterface* create( const Tomahawk::playlist_ptr& pl ); virtual QString type() const { return "spotify"; } + +private: + Tomahawk::Accounts::SpotifyAccount* m_account; }; #endif // SPOTIFYPLAYLISTUPDATER_H diff --git a/src/libtomahawk/playlist/playlistitemdelegate.cpp b/src/libtomahawk/playlist/playlistitemdelegate.cpp index eb9016760..9429f7d31 100644 --- a/src/libtomahawk/playlist/playlistitemdelegate.cpp +++ b/src/libtomahawk/playlist/playlistitemdelegate.cpp @@ -200,7 +200,7 @@ PlaylistItemDelegate::paintShort( QPainter* painter, const QStyleOptionViewItem& painter->setPen( opt.palette.text().color() ); QRect ir = r.adjusted( 4, 0, -option.rect.width() + option.rect.height() - 8 + r.left(), 0 ); - + if ( useAvatars ) pixmap = source->avatar( Source::FancyStyle, ir.size() ); else diff --git a/src/libtomahawk/resolvers/qtscriptresolver.cpp b/src/libtomahawk/resolvers/qtscriptresolver.cpp index d518c15f2..38fcff9e4 100644 --- a/src/libtomahawk/resolvers/qtscriptresolver.cpp +++ b/src/libtomahawk/resolvers/qtscriptresolver.cpp @@ -1,6 +1,7 @@ /* === This file is part of Tomahawk Player - === * * Copyright 2010-2011, Christian Muehlhaeuser + * Copyright 2010-2011, Leo Franchi * * Tomahawk is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/libtomahawk/resolvers/qtscriptresolver.h b/src/libtomahawk/resolvers/qtscriptresolver.h index 4006f3221..42769bdbb 100644 --- a/src/libtomahawk/resolvers/qtscriptresolver.h +++ b/src/libtomahawk/resolvers/qtscriptresolver.h @@ -1,6 +1,7 @@ /* === This file is part of Tomahawk Player - === * * Copyright 2010-2011, Christian Muehlhaeuser + * Copyright 2010-2011, Leo Franchi * * Tomahawk is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by