From b6ba7b92bc41f0c6658ecc6262fb1c588e506326 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Lindstr=C3=B6m?= Date: Thu, 16 Aug 2012 21:29:45 +0200 Subject: [PATCH] Enable collaborative spotify playlists - Needs better nameing --- .../accounts/spotify/SpotifyAccount.cpp | 94 +++++++++++++------ .../accounts/spotify/SpotifyAccount.h | 5 +- .../spotify/SpotifyPlaylistUpdater.cpp | 2 - .../accounts/spotify/SpotifyPlaylistUpdater.h | 1 - .../playlist/PlaylistUpdaterInterface.h | 2 + src/sourcetree/items/PlaylistItems.cpp | 21 +++++ src/sourcetree/items/PlaylistItems.h | 1 + 7 files changed, 93 insertions(+), 33 deletions(-) diff --git a/src/libtomahawk/accounts/spotify/SpotifyAccount.cpp b/src/libtomahawk/accounts/spotify/SpotifyAccount.cpp index c5a065097..aa1695889 100644 --- a/src/libtomahawk/accounts/spotify/SpotifyAccount.cpp +++ b/src/libtomahawk/accounts/spotify/SpotifyAccount.cpp @@ -531,9 +531,9 @@ SpotifyAccount::aboutToShow( QAction* action, const playlist_ptr& playlist ) // If the user is owner of current playlist, enable collaboration options if ( actionType == Collaborate ) { - if( found && owner && !manuallyDisabled ) + if ( found && owner && !manuallyDisabled ) { - if( !collaborative ) + if ( !collaborative ) action->setText( tr( "Enable Spotify collaborations" ) ); else action->setText( tr( "Disable Spotify collaborations" ) ); @@ -545,7 +545,8 @@ SpotifyAccount::aboutToShow( QAction* action, const playlist_ptr& playlist ) SpotifyPlaylistUpdater* -SpotifyAccount::getPlaylistUpdater( const playlist_ptr plptr ){ +SpotifyAccount::getPlaylistUpdater( const playlist_ptr plptr ) +{ SpotifyPlaylistUpdater* updater = 0; QList updaters = plptr->updaters(); @@ -559,42 +560,68 @@ SpotifyAccount::getPlaylistUpdater( const playlist_ptr plptr ){ return updater; } -void -SpotifyAccount::subscribeActionTriggered( bool ) +SpotifyPlaylistUpdater* +SpotifyAccount::getPlaylistUpdater( QObject *sender ) { - const playlist_ptr playlist = playlistFromAction( qobject_cast< QAction* >( sender() ) ); + + if ( !sender ) + { + tLog() << "uuh noo, null sender!"; + return 0; + } + + QAction* senderAction = qobject_cast< QAction* >( sender ); + + if ( !senderAction ) + { + tLog() << "uuh noo, null action!"; + return 0; + } + + const playlist_ptr playlist = playlistFromAction( senderAction ); if ( playlist.isNull() ) { - qWarning() << "Got context menu spotify subscribe action triggered, but invalid playlist payload!"; + qWarning() << "Got context menu spotify action " << senderAction->text() << "triggered, but invalid playlist payload!"; Q_ASSERT( false ); - return; + return 0; } - SpotifyPlaylistUpdater *updater = getPlaylistUpdater( playlist ); + SpotifyPlaylistUpdater* updater = 0; + QList updaters = playlist->updaters(); + foreach ( PlaylistUpdaterInterface* u, updaters ) + { + if ( SpotifyPlaylistUpdater* spotifyUpdater = qobject_cast< SpotifyPlaylistUpdater* >( u ) ) + { + updater = spotifyUpdater; + } + } + return updater; +} + +void +SpotifyAccount::subscribeActionTriggered( bool ) +{ + SpotifyPlaylistUpdater* updater = getPlaylistUpdater( sender() ); Q_ASSERT( updater ); if ( !updater ) return; + Q_ASSERT( updater->playlist() ); + if ( !updater->playlist() ) + return; + // Toggle subscription status - setSubscribedForPlaylist( playlist, !updater->subscribed() ); + setSubscribedForPlaylist( updater->playlist(), !updater->subscribed() ); } void SpotifyAccount::collaborateActionTriggered( bool ) { - const playlist_ptr playlist = playlistFromAction( qobject_cast< QAction* >( sender() ) ); - if ( playlist.isNull() ) - { - qWarning() << "Got context menu spotify collaboration action triggered, but invalid playlist payload!"; - Q_ASSERT( false ); - return; - } - - SpotifyPlaylistUpdater *updater = getPlaylistUpdater( playlist ); + SpotifyPlaylistUpdater* updater = getPlaylistUpdater( sender() ); if ( !updater ) { @@ -606,7 +633,7 @@ SpotifyAccount::collaborateActionTriggered( bool ) SpotifyPlaylistInfo* info = m_allSpotifyPlaylists.value( updater->spotifyId(), 0 ); Q_ASSERT( info ); - if( info->isOwner ) + if ( info->isOwner ) { tLog() << info->name << info->isOwner << info->plid << updater->owner() << updater->collaborative(); QVariantMap msg; @@ -616,7 +643,6 @@ SpotifyAccount::collaborateActionTriggered( bool ) sendMessage( msg, this ); updater->setCollaborative( !updater->collaborative() ); - } else tLog() << "cant set collab for this pl, not owner!?" << info->name << info->plid; @@ -628,7 +654,7 @@ SpotifyAccount::collaborateActionTriggered( bool ) void SpotifyAccount::syncActionTriggered( bool ) { - const playlist_ptr playlist = playlistFromAction( qobject_cast< QAction* >( sender() ) ); + const playlist_ptr playlist = playlistFromAction( sender() ); if ( playlist.isNull() ) { @@ -637,7 +663,7 @@ SpotifyAccount::syncActionTriggered( bool ) return; } - SpotifyPlaylistUpdater *updater = getPlaylistUpdater( playlist ); + SpotifyPlaylistUpdater* updater = getPlaylistUpdater( playlist ); if ( !updater || updater->canSubscribe() ) { @@ -690,7 +716,7 @@ SpotifyAccount::syncActionTriggered( bool ) void SpotifyAccount::setSubscribedForPlaylist( const playlist_ptr& playlist, bool subscribed ) { - SpotifyPlaylistUpdater *updater = getPlaylistUpdater( playlist ); + SpotifyPlaylistUpdater* updater = getPlaylistUpdater( playlist ); if ( !updater ) { @@ -736,14 +762,26 @@ SpotifyAccount::setSubscribedForPlaylist( const playlist_ptr& playlist, bool sub playlist_ptr -SpotifyAccount::playlistFromAction( QAction* action ) const +SpotifyAccount::playlistFromAction( QObject* action ) const { - Q_ASSERT( action ); + if ( !action ) + { + tLog() << "uuh noo, null sender!"; + return playlist_ptr(); + } - if ( !action || !m_customActions.contains( action ) ) + QAction* senderAction = qobject_cast< QAction* >( action ); + + if ( !senderAction ) + { + tLog() << "uuh noo, null action!"; + return playlist_ptr(); + } + + if ( !senderAction || !m_customActions.contains( senderAction ) ) return playlist_ptr(); - return action->property( "payload" ).value< playlist_ptr >(); + return senderAction->property( "payload" ).value< playlist_ptr >(); } diff --git a/src/libtomahawk/accounts/spotify/SpotifyAccount.h b/src/libtomahawk/accounts/spotify/SpotifyAccount.h index e93ceee5d..777695fdb 100644 --- a/src/libtomahawk/accounts/spotify/SpotifyAccount.h +++ b/src/libtomahawk/accounts/spotify/SpotifyAccount.h @@ -103,7 +103,7 @@ public: void registerUpdaterForPlaylist( const QString& plId, SpotifyPlaylistUpdater* updater ); - void registerPlaylistInfo(const QString& name, const QString& plid, const QString &revid, const bool sync, const bool subscribed , const bool owner = false); + void registerPlaylistInfo( const QString& name, const QString& plid, const QString &revid, const bool sync, const bool subscribed , const bool owner = false); void registerPlaylistInfo( SpotifyPlaylistInfo* info ); void unregisterUpdater( const QString& plid ); @@ -157,8 +157,9 @@ private: void createActions(); void removeActions(); - playlist_ptr playlistFromAction( QAction* action ) const; + playlist_ptr playlistFromAction( QObject* action ) const; SpotifyPlaylistUpdater* getPlaylistUpdater( const playlist_ptr plptr); + SpotifyPlaylistUpdater* getPlaylistUpdater( QObject* sender ); static SpotifyAccount* s_instance; QWeakPointer m_configWidget; diff --git a/src/libtomahawk/accounts/spotify/SpotifyPlaylistUpdater.cpp b/src/libtomahawk/accounts/spotify/SpotifyPlaylistUpdater.cpp index e6b6c9251..3b8c71ac8 100644 --- a/src/libtomahawk/accounts/spotify/SpotifyPlaylistUpdater.cpp +++ b/src/libtomahawk/accounts/spotify/SpotifyPlaylistUpdater.cpp @@ -22,7 +22,6 @@ #include "accounts/AccountManager.h" #include "SpotifyAccount.h" #include "utils/TomahawkUtils.h" - #include using namespace Tomahawk; @@ -212,7 +211,6 @@ SpotifyPlaylistUpdater::typeIcon() const } #endif - void SpotifyPlaylistUpdater::setSync( bool sync ) { diff --git a/src/libtomahawk/accounts/spotify/SpotifyPlaylistUpdater.h b/src/libtomahawk/accounts/spotify/SpotifyPlaylistUpdater.h index 656a49aa7..2bc606386 100644 --- a/src/libtomahawk/accounts/spotify/SpotifyPlaylistUpdater.h +++ b/src/libtomahawk/accounts/spotify/SpotifyPlaylistUpdater.h @@ -49,7 +49,6 @@ public: #ifndef ENABLE_HEADLESS virtual QWidget* configurationWidget() const { return 0; } - virtual QPixmap typeIcon() const; #endif diff --git a/src/libtomahawk/playlist/PlaylistUpdaterInterface.h b/src/libtomahawk/playlist/PlaylistUpdaterInterface.h index 1d569e1a2..53a2fe2ad 100644 --- a/src/libtomahawk/playlist/PlaylistUpdaterInterface.h +++ b/src/libtomahawk/playlist/PlaylistUpdaterInterface.h @@ -84,6 +84,8 @@ public: virtual bool canSubscribe() const { return false; } virtual bool subscribed() const { return false; } virtual void setSubscribed( bool ) {} + virtual void setCollaborative( bool ) {} + virtual bool collaborative() const { return false; } // The int data value associated with each question must be unique across *all* playlist updaters, // as setQuestionResults is called with all questions from all updaters. diff --git a/src/sourcetree/items/PlaylistItems.cpp b/src/sourcetree/items/PlaylistItems.cpp index 3630b1f0d..12d27fa8e 100644 --- a/src/sourcetree/items/PlaylistItems.cpp +++ b/src/sourcetree/items/PlaylistItems.cpp @@ -291,6 +291,25 @@ PlaylistItem::onUpdated() emit updated(); } +bool +PlaylistItem::collaborative() const +{ + Q_ASSERT( !m_playlist.isNull() ); + + if ( m_playlist->updaters().isEmpty() ) + return false; + + bool collaborative = false; + + foreach ( PlaylistUpdaterInterface* updater, m_playlist->updaters() ) + { + if( !updater->collaborative() ) + continue; + collaborative = updater->collaborative(); + } + + return collaborative; +} bool PlaylistItem::createOverlay() @@ -302,6 +321,7 @@ PlaylistItem::createOverlay() m_showSubscribed = false; m_canSubscribe = false; + foreach ( PlaylistUpdaterInterface* updater, m_playlist->updaters() ) { if ( updater->canSubscribe() ) @@ -349,6 +369,7 @@ PlaylistItem::createOverlay() overlayRect.moveLeft( 0 ); } + p.end(); m_overlaidIcon.addPixmap( base ); diff --git a/src/sourcetree/items/PlaylistItems.h b/src/sourcetree/items/PlaylistItems.h index e1bc3ea6e..7f346b0f5 100644 --- a/src/sourcetree/items/PlaylistItems.h +++ b/src/sourcetree/items/PlaylistItems.h @@ -47,6 +47,7 @@ public: bool subscribed() const { return m_showSubscribed; } QPixmap subscribedIcon() const { return m_showSubscribed ? m_subscribedOnIcon : m_subscribedOffIcon; } void setSubscribed( bool subscribed ); + bool collaborative() const; public slots: virtual void activate();