1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-06 14:16:32 +02:00

Enable Subscribing on SpotifyPlaylists

This commit is contained in:
Hugo Lindström
2012-06-12 23:04:33 +02:00
parent 14d7b882fa
commit 8401befe6b
6 changed files with 179 additions and 23 deletions

View File

@@ -47,6 +47,7 @@ using namespace Accounts;
static QPixmap* s_icon = 0; static QPixmap* s_icon = 0;
SpotifyAccount* SpotifyAccount::s_instance = 0;
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
static QString s_resolverId = "spotify-osx"; static QString s_resolverId = "spotify-osx";
@@ -81,10 +82,10 @@ SpotifyAccount::SpotifyAccount( const QString& accountId )
: CustomAtticaAccount( accountId ) : CustomAtticaAccount( accountId )
, m_preventEnabling( false ) , m_preventEnabling( false )
{ {
s_instance = this;
init(); init();
} }
SpotifyAccount::~SpotifyAccount() SpotifyAccount::~SpotifyAccount()
{ {
clearUser(); clearUser();
@@ -346,9 +347,12 @@ SpotifyAccount::aboutToShow( QAction* action, const playlist_ptr& playlist )
if ( !m_customActions.contains( action ) ) if ( !m_customActions.contains( action ) )
return; return;
// If it's not being synced, allow the option to sync // If it's not being synced, allow the option to sync
bool found = false; bool found = false;
bool manuallyDisabled = false; bool manuallyDisabled = false;
bool canSubscribe = false;
bool isSubscribed = false;
QList<PlaylistUpdaterInterface*> updaters = playlist->updaters(); QList<PlaylistUpdaterInterface*> updaters = playlist->updaters();
foreach ( PlaylistUpdaterInterface* updater, updaters ) foreach ( PlaylistUpdaterInterface* updater, updaters )
{ {
@@ -358,21 +362,42 @@ SpotifyAccount::aboutToShow( QAction* action, const playlist_ptr& playlist )
if ( !spotifyUpdater->sync() ) if ( !spotifyUpdater->sync() )
manuallyDisabled = true; manuallyDisabled = true;
canSubscribe = spotifyUpdater->canSubscribe();
isSubscribed = spotifyUpdater->subscribed();
qDebug() << "canSub " << canSubscribe << " is Sub " << isSubscribed << "isSibb " << spotifyUpdater->subscribed();
} }
} }
if ( !found ) if ( !found )
{ {
action->setText( tr( "Sync with Spotify" ) ); 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->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 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 ) if ( info )
{
qDebug() << "Found info!";
info->sync = !updater->sync(); 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() ) if ( m_configWidget.data() )
m_configWidget.data()->setPlaylists( m_allSpotifyPlaylists ); m_configWidget.data()->setPlaylists( m_allSpotifyPlaylists );
if ( !updater->sync() )
switch( action->property("type").toInt() )
{ {
startPlaylistSync( info ); case Sync:
} startPlaylistSync( info );
else break;
{ case UnSync:
stopPlaylistSync( info, true ); 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 plid = plMap.value( "id" ).toString();
const QString revid = plMap.value( "revid" ).toString(); const QString revid = plMap.value( "revid" ).toString();
const bool sync = plMap.value( "sync" ).toBool(); const bool sync = plMap.value( "sync" ).toBool();
const bool subscribe = plMap.value( "subscribe" ).toBool();
if ( name.isNull() || plid.isNull() || revid.isNull() ) if ( name.isNull() || plid.isNull() || revid.isNull() )
{ {
qDebug() << "Did not get name and plid and revid for spotify playlist:" << name << plid << revid << plMap; qDebug() << "Did not get name and plid and revid for spotify playlist:" << name << plid << revid << plMap;
continue; continue;
} }
m_allSpotifyPlaylists << new SpotifyPlaylistInfo( name, plid, revid, sync ); m_allSpotifyPlaylists << new SpotifyPlaylistInfo( name, plid, revid, sync, subscribe );
} }
if ( !m_configWidget.isNull() ) if ( !m_configWidget.isNull() )
@@ -796,7 +895,6 @@ SpotifyAccount::startPlaylistSync( SpotifyPlaylistInfo* playlist )
sendMessage( msg, this, "startPlaylistSyncWithPlaylist" ); sendMessage( msg, this, "startPlaylistSyncWithPlaylist" );
} }
void void
SpotifyAccount::startPlaylistSyncWithPlaylist( const QString& msgType, const QVariantMap& msg ) SpotifyAccount::startPlaylistSyncWithPlaylist( const QString& msgType, const QVariantMap& msg )
{ {

View File

@@ -42,13 +42,13 @@ class SpotifyAccountConfig;
// metadata for a playlist // metadata for a playlist
struct SpotifyPlaylistInfo { struct SpotifyPlaylistInfo {
QString name, plid, revid; QString name, plid, revid;
bool sync, changed; bool sync, changed, subscribe;
SpotifyPlaylistInfo( const QString& nname, const QString& pid, const QString& rrevid, bool ssync ) SpotifyPlaylistInfo( const QString& nname, const QString& pid, const QString& rrevid, bool ssync, bool ssubscribe )
: name( nname ), plid( pid ), revid( rrevid ), sync( ssync ), changed( false ) {} : 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 ); SpotifyAccount( const QString& accountId, const QString& path );
virtual ~SpotifyAccount(); virtual ~SpotifyAccount();
static SpotifyAccount* instance() {
return s_instance;
}
virtual QPixmap icon() const; virtual QPixmap icon() const;
virtual QWidget* configurationWidget(); virtual QWidget* configurationWidget();
virtual QWidget* aboutWidget(); virtual QWidget* aboutWidget();
@@ -94,12 +98,9 @@ public:
virtual bool preventEnabling() const { return m_preventEnabling; } virtual bool preventEnabling() const { return m_preventEnabling; }
QString sendMessage( const QVariantMap& msg, QObject* receiver = 0, const QString& slot = QString() ); QString sendMessage( const QVariantMap& msg, QObject* receiver = 0, const QString& slot = QString() );
void registerUpdaterForPlaylist( const QString& plId, SpotifyPlaylistUpdater* updater ); void registerUpdaterForPlaylist( const QString& plId, SpotifyPlaylistUpdater* updater );
void unregisterUpdater( const QString& plid ); void unregisterUpdater( const QString& plid );
bool deleteOnUnsync() const; bool deleteOnUnsync() const;
void setManualResolverPath( const QString& resolverPath ); void setManualResolverPath( const QString& resolverPath );
public slots: public slots:
@@ -133,9 +134,21 @@ private:
void startPlaylistSync( SpotifyPlaylistInfo* playlist ); void startPlaylistSync( SpotifyPlaylistInfo* playlist );
void stopPlaylistSync( SpotifyPlaylistInfo* playlist, bool forceDontDelete = false ); void stopPlaylistSync( SpotifyPlaylistInfo* playlist, bool forceDontDelete = false );
void fetchFullPlaylist( SpotifyPlaylistInfo* playlist ); void fetchFullPlaylist( SpotifyPlaylistInfo* playlist );
void setSyncForPlaylist( const QString& spotifyPlaylistId, bool sync ); 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 createActions();
void removeActions(); void removeActions();
@@ -155,6 +168,8 @@ private:
SmartPointerList< QAction > m_customActions; SmartPointerList< QAction > m_customActions;
friend class ::SpotifyPlaylistUpdater; friend class ::SpotifyPlaylistUpdater;
static SpotifyAccount* s_instance;
}; };
} }

View File

@@ -74,6 +74,7 @@ SpotifyPlaylistUpdater::SpotifyPlaylistUpdater( SpotifyAccount* acct, const QStr
, m_spotifyId( spotifyId ) , m_spotifyId( spotifyId )
, m_blockUpdatesForNextRevision( false ) , m_blockUpdatesForNextRevision( false )
, m_sync( false ) , m_sync( false )
, m_subscribe( false )
{ {
init(); init();
} }
@@ -194,13 +195,14 @@ SpotifyPlaylistUpdater::type() const
QPixmap QPixmap
SpotifyPlaylistUpdater::typeIcon() const SpotifyPlaylistUpdater::typeIcon() const
{ {
if ( !s_typePixmap ) if ( !s_typePixmap )
{ {
QPixmap pm( RESPATH "images/spotify-logo.png" ); QPixmap pm( RESPATH "images/spotify-logo.png" );
s_typePixmap = new QPixmap( pm.scaled( 32, 32, Qt::KeepAspectRatio, Qt::SmoothTransformation ) ); s_typePixmap = new QPixmap( pm.scaled( 32, 32, Qt::KeepAspectRatio, Qt::SmoothTransformation ) );
} }
if ( !m_sync ) if( !m_sync )
return QPixmap(); return QPixmap();
return *s_typePixmap; return *s_typePixmap;
@@ -220,6 +222,17 @@ SpotifyPlaylistUpdater::setSync( bool sync )
emit changed(); emit changed();
} }
void
SpotifyPlaylistUpdater::setSubscribe( bool sub )
{
if ( m_subscribe == sub )
return;
m_subscribe = sub;
saveToSettings();
emit changed();
}
bool bool
SpotifyPlaylistUpdater::sync() const SpotifyPlaylistUpdater::sync() const
@@ -227,6 +240,14 @@ SpotifyPlaylistUpdater::sync() const
return m_sync; 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 void
SpotifyPlaylistUpdater::spotifyTracksAdded( const QVariantList& tracks, const QString& startPosId, const QString& newRev, const QString& oldRev ) SpotifyPlaylistUpdater::spotifyTracksAdded( const QVariantList& tracks, const QString& startPosId, const QString& newRev, const QString& oldRev )

View File

@@ -53,6 +53,9 @@ public:
bool sync() const; bool sync() const;
void setSync( bool sync ); void setSync( bool sync );
bool canSubscribe();
void setSubscribe( bool sub );
bool subscribed() { return m_subscribe; }
QString spotifyId() const { return m_spotifyId; } QString spotifyId() const { return m_spotifyId; }
@@ -99,6 +102,8 @@ private:
bool m_blockUpdatesForNextRevision; bool m_blockUpdatesForNextRevision;
bool m_sync; bool m_sync;
bool m_subscribe;
bool m_canSubscribe;
QQueue<_detail::Closure*> m_queuedOps; QQueue<_detail::Closure*> m_queuedOps;
#ifndef ENABLE_HEADLESS #ifndef ENABLE_HEADLESS

View File

@@ -116,6 +116,7 @@ SpotifyParser::lookupSpotifyBrowse( const QString& linkRaw )
type = DropJob::Track; type = DropJob::Track;
QUrl url; QUrl url;
m_browseId = browseUri;
if( type != DropJob::Artist ) if( type != DropJob::Artist )
url = QUrl( QString( SPOTIFY_PLAYLIST_API_URL "/browse/%1" ).arg( browseUri ) ); url = QUrl( QString( SPOTIFY_PLAYLIST_API_URL "/browse/%1" ).arg( browseUri ) );
@@ -302,6 +303,8 @@ SpotifyParser::checkBrowseFinished()
if( m_createNewPlaylist && !m_tracks.isEmpty() ) if( m_createNewPlaylist && !m_tracks.isEmpty() )
{ {
m_playlist = Playlist::create( SourceList::instance()->getLocal(), m_playlist = Playlist::create( SourceList::instance()->getLocal(),
uuid(), uuid(),
m_title, m_title,
@@ -309,6 +312,18 @@ SpotifyParser::checkBrowseFinished()
m_creator, m_creator,
false, false,
m_tracks ); 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() ) ); connect( m_playlist.data(), SIGNAL( revisionLoaded( Tomahawk::PlaylistRevision ) ), this, SLOT( playlistCreated() ) );
return; return;
} }

View File

@@ -28,6 +28,8 @@
#include <QObject> #include <QObject>
#include <QSet> #include <QSet>
#include <QtCore/QStringList> #include <QtCore/QStringList>
#include "accounts/spotify/SpotifyAccount.h"
#include "accounts/spotify/SpotifyPlaylistUpdater.h"
#define SPOTIFY_PLAYLIST_API_URL "http://spotikea.tomahawk-player.org" #define SPOTIFY_PLAYLIST_API_URL "http://spotikea.tomahawk-player.org"
@@ -84,7 +86,7 @@ private:
QString m_title, m_info, m_creator; QString m_title, m_info, m_creator;
Tomahawk::playlist_ptr m_playlist; Tomahawk::playlist_ptr m_playlist;
DropJobNotifier* m_browseJob; DropJobNotifier* m_browseJob;
QString m_browseId;
static QPixmap* s_pixmap; static QPixmap* s_pixmap;
}; };