From 042b27ea83e777f0e5c0a7d44ef765c53ebb0dc8 Mon Sep 17 00:00:00 2001 From: Leo Franchi Date: Mon, 9 Apr 2012 23:01:03 -0400 Subject: [PATCH] tomahawk side of creating a newly synced playlist --- src/accounts/spotify/SpotifyAccount.cpp | 50 ++++++++++++++++++++++++- src/accounts/spotify/SpotifyAccount.h | 5 ++- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/accounts/spotify/SpotifyAccount.cpp b/src/accounts/spotify/SpotifyAccount.cpp index 13522d3ea..46e05ad29 100644 --- a/src/accounts/spotify/SpotifyAccount.cpp +++ b/src/accounts/spotify/SpotifyAccount.cpp @@ -155,7 +155,17 @@ SpotifyAccount::syncActionTriggered( bool checked ) if ( !updater ) { - // TODO + QVariantMap msg; + msg[ "_msgtype" ] = "createPlaylist"; + msg[ "sync" ] = true; + + QList< query_ptr > queries; + foreach ( const plentry_ptr& ple, playlist->entries() ) + queries << ple->query(); + QVariantList tracks = SpotifyPlaylistUpdater::queriesToVariant( queries ); + + const QString qid = sendMessage( msg, this, "playlistCreated" ); + m_waitingForCreateReply[ qid ] = playlist; } else { @@ -415,6 +425,7 @@ SpotifyAccount::startPlaylistSync( SpotifyPlaylistInfo* playlist ) void SpotifyAccount::startPlaylistSyncWithPlaylist( const QString& msgType, const QVariantMap& msg ) { + Q_UNUSED( msgType ); 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(); @@ -459,19 +470,54 @@ SpotifyAccount::startPlaylistSyncWithPlaylist( const QString& msgType, const QVa void +SpotifyAccount::playlistCreated( const QString& msgType, const QVariantMap& msg ) +{ + Q_UNUSED( msgType ); + + qDebug() << Q_FUNC_INFO << "Got response from our createPlaylist command, now creating updater and attaching"; + const bool success = msg.value( "success" ).toBool(); + + if ( !success ) + { + qWarning() << "Got FAILED return code from spotify resolver createPlaylist command, aborting sync"; + return; + } + + const QString id = msg.value( "playlistid" ).toString(); + const QString revid = msg.value( "playlistid" ).toString(); + const QString qid = msg.value( "qid" ).toString(); + + if ( !m_waitingForCreateReply.contains( qid ) ) + { + qWarning() << "Got a createPlaylist reply for a playlist/qid we were not waiting for :-/ " << qid << m_waitingForCreateReply; + return; + } + + playlist_ptr playlist = m_waitingForCreateReply.take( qid ); + SpotifyPlaylistUpdater* updater = new SpotifyPlaylistUpdater( this, revid, id, playlist ); + updater->setSync( true ); + m_updaters[ id ] = updater; +} + + +QString SpotifyAccount::sendMessage( const QVariantMap &m, QObject* obj, const QString& slot ) { QVariantMap msg = m; + QString qid; if ( obj ) { - const QString qid = QUuid::createUuid().toString().replace( "{", "" ).replace( "}", "" ); + qid = QUuid::createUuid().toString().replace( "{", "" ).replace( "}", "" ); m_qidToSlotMap[ qid ] = qMakePair( obj, slot ); msg[ "qid" ] = qid; + } m_spotifyResolver.data()->sendMessage( msg ); + + return qid; } diff --git a/src/accounts/spotify/SpotifyAccount.h b/src/accounts/spotify/SpotifyAccount.h index 01dbad3fd..4379e6502 100644 --- a/src/accounts/spotify/SpotifyAccount.h +++ b/src/accounts/spotify/SpotifyAccount.h @@ -87,7 +87,7 @@ public: virtual InfoSystem::InfoPlugin* infoPlugin() { return 0; } virtual SipPlugin* sipPlugin() { return 0; } - void 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 unregisterUpdater( const QString& plid ); @@ -104,6 +104,7 @@ 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 ); + void playlistCreated( const QString& msgType, const QVariantMap& msg ); private: void init(); @@ -124,6 +125,8 @@ private: QList< SpotifyPlaylistInfo* > m_allSpotifyPlaylists; QHash< QString, SpotifyPlaylistUpdater* > m_updaters; + QHash< QString, playlist_ptr > m_waitingForCreateReply; + SmartPointerList< QAction > m_customActions; friend class ::SpotifyPlaylistUpdater; };