From 8401befe6b30674721518ef18cc17cc03cd2908d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Lindstr=C3=B6m?= Date: Tue, 12 Jun 2012 23:04:33 +0200 Subject: [PATCH] Enable Subscribing on SpotifyPlaylists --- .../accounts/spotify/SpotifyAccount.cpp | 124 ++++++++++++++++-- .../accounts/spotify/SpotifyAccount.h | 31 +++-- .../spotify/SpotifyPlaylistUpdater.cpp | 23 +++- .../accounts/spotify/SpotifyPlaylistUpdater.h | 5 + src/libtomahawk/utils/SpotifyParser.cpp | 15 +++ src/libtomahawk/utils/SpotifyParser.h | 4 +- 6 files changed, 179 insertions(+), 23 deletions(-) diff --git a/src/libtomahawk/accounts/spotify/SpotifyAccount.cpp b/src/libtomahawk/accounts/spotify/SpotifyAccount.cpp index d786be550..0b79e985d 100644 --- a/src/libtomahawk/accounts/spotify/SpotifyAccount.cpp +++ b/src/libtomahawk/accounts/spotify/SpotifyAccount.cpp @@ -47,6 +47,7 @@ using namespace Accounts; static QPixmap* s_icon = 0; +SpotifyAccount* SpotifyAccount::s_instance = 0; #ifdef Q_OS_MAC static QString s_resolverId = "spotify-osx"; @@ -81,10 +82,10 @@ SpotifyAccount::SpotifyAccount( const QString& accountId ) : CustomAtticaAccount( accountId ) , m_preventEnabling( false ) { + s_instance = this; init(); } - SpotifyAccount::~SpotifyAccount() { clearUser(); @@ -346,9 +347,12 @@ SpotifyAccount::aboutToShow( QAction* action, const playlist_ptr& playlist ) if ( !m_customActions.contains( action ) ) return; + // If it's not being synced, allow the option to sync bool found = false; bool manuallyDisabled = false; + bool canSubscribe = false; + bool isSubscribed = false; QList updaters = playlist->updaters(); foreach ( PlaylistUpdaterInterface* updater, updaters ) { @@ -358,21 +362,42 @@ SpotifyAccount::aboutToShow( QAction* action, const playlist_ptr& playlist ) if ( !spotifyUpdater->sync() ) manuallyDisabled = true; + canSubscribe = spotifyUpdater->canSubscribe(); + isSubscribed = spotifyUpdater->subscribed(); + qDebug() << "canSub " << canSubscribe << " is Sub " << isSubscribed << "isSibb " << spotifyUpdater->subscribed(); } } if ( !found ) { action->setText( tr( "Sync with Spotify" ) ); + action->setProperty( "type", Sync ); } - else if ( manuallyDisabled ) + + else if ( manuallyDisabled && !canSubscribe ) { action->setText( tr( "Re-enable syncing with Spotify" ) ); + action->setProperty( "type", Sync ); + } + else if ( canSubscribe && !isSubscribed ) + { + action->setText( tr( "Subscribe with Spotify" ) ); + action->setProperty( "type", Subscribe ); } else { - action->setText( tr( "Stop syncing with Spotify" ) ); + if ( canSubscribe ) + { + action->setText( tr( "Stop Subscribing with Spotify" ) ); + action->setProperty( "type", UnSubscribe); + } + else + { + action->setText( tr( "Stop syncing with Spotify" ) ); + action->setProperty( "type", UnSync); + } } + } @@ -431,21 +456,95 @@ SpotifyAccount::syncActionTriggered( bool checked ) } } - Q_ASSERT( info ); if ( info ) + { + qDebug() << "Found info!"; info->sync = !updater->sync(); + info->subscribe = !updater->sync(); + } + else if( action->property("type").toInt() == Subscribe | UnSubscribe ) + { + qDebug() << "ADDING PLAYLIST INFO!"; + info = new SpotifyPlaylistInfo( playlist->title(), updater->spotifyId(), updater->spotifyId(), true, false ); + } + + Q_ASSERT( info ); if ( m_configWidget.data() ) m_configWidget.data()->setPlaylists( m_allSpotifyPlaylists ); - if ( !updater->sync() ) + + switch( action->property("type").toInt() ) { - startPlaylistSync( info ); - } - else - { - stopPlaylistSync( info, true ); + case Sync: + startPlaylistSync( info ); + break; + case UnSync: + stopPlaylistSync( info, true ); + break; + case Subscribe: + qDebug() << "Got subscribe!"; + startPlaylistSubscribe( info ); + break; + case UnSubscribe: + qDebug() << "Got UNsubscribe!"; + stopPlaylistSubscribe( info ); + break; + default: + qDebug() << "I DONNO WHAT TO DO"; + break; + } + + + } +} + + +void +SpotifyAccount::stopPlaylistSubscribe( SpotifyPlaylistInfo* playlist ) +{ + + qDebug() << Q_FUNC_INFO; + + if ( !playlist ) + return; + + QVariantMap msg; + msg[ "_msgtype" ] = "setSubscription"; + msg[ "playlistid" ] = playlist->plid; + msg[ "subscribed" ] = false; + + sendMessage( msg, 0, "setSubscription" ); + + if ( m_updaters.contains( playlist->plid ) ) + { + SpotifyPlaylistUpdater* updater = m_updaters[ playlist->plid ]; + updater->setSubscribe( false ); + updater->save(); + } +} + +void +SpotifyAccount::startPlaylistSubscribe( SpotifyPlaylistInfo* playlist ) +{ + qDebug() << Q_FUNC_INFO; + + if ( !playlist ) + return; + + QVariantMap msg; + msg[ "_msgtype" ] = "setSubscription"; + msg[ "playlistid" ] = playlist->plid; + msg[ "subscribed" ] = true; + + sendMessage( msg, 0, "setSubscription" ); + + if ( m_updaters.contains( playlist->plid ) ) + { + SpotifyPlaylistUpdater* updater = m_updaters[ playlist->plid ]; + updater->setSubscribe( true ); + updater->save(); } } @@ -495,13 +594,13 @@ SpotifyAccount::resolverMessage( const QString &msgType, const QVariantMap &msg const QString plid = plMap.value( "id" ).toString(); const QString revid = plMap.value( "revid" ).toString(); const bool sync = plMap.value( "sync" ).toBool(); - + const bool subscribe = plMap.value( "subscribe" ).toBool(); if ( name.isNull() || plid.isNull() || revid.isNull() ) { qDebug() << "Did not get name and plid and revid for spotify playlist:" << name << plid << revid << plMap; continue; } - m_allSpotifyPlaylists << new SpotifyPlaylistInfo( name, plid, revid, sync ); + m_allSpotifyPlaylists << new SpotifyPlaylistInfo( name, plid, revid, sync, subscribe ); } if ( !m_configWidget.isNull() ) @@ -796,7 +895,6 @@ SpotifyAccount::startPlaylistSync( SpotifyPlaylistInfo* playlist ) sendMessage( msg, this, "startPlaylistSyncWithPlaylist" ); } - void SpotifyAccount::startPlaylistSyncWithPlaylist( const QString& msgType, const QVariantMap& msg ) { diff --git a/src/libtomahawk/accounts/spotify/SpotifyAccount.h b/src/libtomahawk/accounts/spotify/SpotifyAccount.h index f31d97309..05ca87787 100644 --- a/src/libtomahawk/accounts/spotify/SpotifyAccount.h +++ b/src/libtomahawk/accounts/spotify/SpotifyAccount.h @@ -42,13 +42,13 @@ class SpotifyAccountConfig; // metadata for a playlist struct SpotifyPlaylistInfo { QString name, plid, revid; - bool sync, changed; + bool sync, changed, subscribe; - SpotifyPlaylistInfo( const QString& nname, const QString& pid, const QString& rrevid, bool ssync ) - : name( nname ), plid( pid ), revid( rrevid ), sync( ssync ), changed( false ) {} + SpotifyPlaylistInfo( const QString& nname, const QString& pid, const QString& rrevid, bool ssync, bool ssubscribe ) + : name( nname ), plid( pid ), revid( rrevid ), sync( ssync ), changed( false ), subscribe( ssubscribe ) {} - SpotifyPlaylistInfo() : sync( false ), changed( false ) {} + SpotifyPlaylistInfo() : sync( false ), changed( false ), subscribe( false ) {} }; @@ -78,6 +78,10 @@ public: SpotifyAccount( const QString& accountId, const QString& path ); virtual ~SpotifyAccount(); + static SpotifyAccount* instance() { + return s_instance; + } + virtual QPixmap icon() const; virtual QWidget* configurationWidget(); virtual QWidget* aboutWidget(); @@ -94,12 +98,9 @@ public: virtual bool preventEnabling() const { return m_preventEnabling; } QString sendMessage( const QVariantMap& msg, QObject* receiver = 0, const QString& slot = QString() ); - void registerUpdaterForPlaylist( const QString& plId, SpotifyPlaylistUpdater* updater ); void unregisterUpdater( const QString& plid ); - bool deleteOnUnsync() const; - void setManualResolverPath( const QString& resolverPath ); public slots: @@ -133,9 +134,21 @@ private: void startPlaylistSync( SpotifyPlaylistInfo* playlist ); void stopPlaylistSync( SpotifyPlaylistInfo* playlist, bool forceDontDelete = false ); void fetchFullPlaylist( SpotifyPlaylistInfo* playlist ); - void setSyncForPlaylist( const QString& spotifyPlaylistId, bool sync ); + void stopPlaylistSubscribe( SpotifyPlaylistInfo* playlist ); + void startPlaylistSubscribe( SpotifyPlaylistInfo* playlist ); + + enum Actions{ + + None = 0x01, + Subscribe = 0x02, + Sync = 0x04, + UnSubscribe = 0x06, + UnSync = 0x08 + }; + + Actions m_action; void createActions(); void removeActions(); @@ -155,6 +168,8 @@ private: SmartPointerList< QAction > m_customActions; friend class ::SpotifyPlaylistUpdater; + + static SpotifyAccount* s_instance; }; } diff --git a/src/libtomahawk/accounts/spotify/SpotifyPlaylistUpdater.cpp b/src/libtomahawk/accounts/spotify/SpotifyPlaylistUpdater.cpp index 64bdda98f..ec544b89b 100644 --- a/src/libtomahawk/accounts/spotify/SpotifyPlaylistUpdater.cpp +++ b/src/libtomahawk/accounts/spotify/SpotifyPlaylistUpdater.cpp @@ -74,6 +74,7 @@ SpotifyPlaylistUpdater::SpotifyPlaylistUpdater( SpotifyAccount* acct, const QStr , m_spotifyId( spotifyId ) , m_blockUpdatesForNextRevision( false ) , m_sync( false ) + , m_subscribe( false ) { init(); } @@ -194,13 +195,14 @@ SpotifyPlaylistUpdater::type() const QPixmap SpotifyPlaylistUpdater::typeIcon() const { + if ( !s_typePixmap ) { QPixmap pm( RESPATH "images/spotify-logo.png" ); s_typePixmap = new QPixmap( pm.scaled( 32, 32, Qt::KeepAspectRatio, Qt::SmoothTransformation ) ); } - if ( !m_sync ) + if( !m_sync ) return QPixmap(); return *s_typePixmap; @@ -220,6 +222,17 @@ SpotifyPlaylistUpdater::setSync( bool sync ) emit changed(); } +void +SpotifyPlaylistUpdater::setSubscribe( bool sub ) +{ + if ( m_subscribe == sub ) + return; + + m_subscribe = sub; + + saveToSettings(); + emit changed(); +} bool SpotifyPlaylistUpdater::sync() const @@ -227,6 +240,14 @@ SpotifyPlaylistUpdater::sync() const return m_sync; } +bool +SpotifyPlaylistUpdater::canSubscribe() +{ + + qDebug() << " canSubscribe " << m_spotify.data()->credentials().value( "username" ).toString() << m_spotifyId; + return !m_spotifyId.contains( m_spotify.data()->credentials().value( "username" ).toString() ); + +} void SpotifyPlaylistUpdater::spotifyTracksAdded( const QVariantList& tracks, const QString& startPosId, const QString& newRev, const QString& oldRev ) diff --git a/src/libtomahawk/accounts/spotify/SpotifyPlaylistUpdater.h b/src/libtomahawk/accounts/spotify/SpotifyPlaylistUpdater.h index 896e507d3..cabfb68b5 100644 --- a/src/libtomahawk/accounts/spotify/SpotifyPlaylistUpdater.h +++ b/src/libtomahawk/accounts/spotify/SpotifyPlaylistUpdater.h @@ -53,6 +53,9 @@ public: bool sync() const; void setSync( bool sync ); + bool canSubscribe(); + void setSubscribe( bool sub ); + bool subscribed() { return m_subscribe; } QString spotifyId() const { return m_spotifyId; } @@ -99,6 +102,8 @@ private: bool m_blockUpdatesForNextRevision; bool m_sync; + bool m_subscribe; + bool m_canSubscribe; QQueue<_detail::Closure*> m_queuedOps; #ifndef ENABLE_HEADLESS diff --git a/src/libtomahawk/utils/SpotifyParser.cpp b/src/libtomahawk/utils/SpotifyParser.cpp index b03276412..0e968b190 100644 --- a/src/libtomahawk/utils/SpotifyParser.cpp +++ b/src/libtomahawk/utils/SpotifyParser.cpp @@ -116,6 +116,7 @@ SpotifyParser::lookupSpotifyBrowse( const QString& linkRaw ) type = DropJob::Track; QUrl url; + m_browseId = browseUri; if( type != DropJob::Artist ) url = QUrl( QString( SPOTIFY_PLAYLIST_API_URL "/browse/%1" ).arg( browseUri ) ); @@ -302,6 +303,8 @@ SpotifyParser::checkBrowseFinished() if( m_createNewPlaylist && !m_tracks.isEmpty() ) { + + m_playlist = Playlist::create( SourceList::instance()->getLocal(), uuid(), m_title, @@ -309,6 +312,18 @@ SpotifyParser::checkBrowseFinished() m_creator, false, m_tracks ); + + if( Tomahawk::Accounts::SpotifyAccount::instance() != 0 ) + { + + qDebug() << "FOUND SPOTIFY ACCOUNT!!" << Tomahawk::Accounts::SpotifyAccount::instance()->credentials().value("username") << Tomahawk::Accounts::SpotifyAccount::instance()->isAuthenticated(); + SpotifyPlaylistUpdater* updater = new SpotifyPlaylistUpdater( Tomahawk::Accounts::SpotifyAccount::instance(), m_browseId, m_browseId, m_playlist ); + updater->setSync( true ); + updater->setSubscribe( false ); + Tomahawk::Accounts::SpotifyAccount::instance()->registerUpdaterForPlaylist( m_browseId, updater); + + } + connect( m_playlist.data(), SIGNAL( revisionLoaded( Tomahawk::PlaylistRevision ) ), this, SLOT( playlistCreated() ) ); return; } diff --git a/src/libtomahawk/utils/SpotifyParser.h b/src/libtomahawk/utils/SpotifyParser.h index 7f5c65b61..0cc5ea23e 100644 --- a/src/libtomahawk/utils/SpotifyParser.h +++ b/src/libtomahawk/utils/SpotifyParser.h @@ -28,6 +28,8 @@ #include #include #include +#include "accounts/spotify/SpotifyAccount.h" +#include "accounts/spotify/SpotifyPlaylistUpdater.h" #define SPOTIFY_PLAYLIST_API_URL "http://spotikea.tomahawk-player.org" @@ -84,7 +86,7 @@ private: QString m_title, m_info, m_creator; Tomahawk::playlist_ptr m_playlist; DropJobNotifier* m_browseJob; - + QString m_browseId; static QPixmap* s_pixmap; };