From 866bf9372153fbdf7f24d557bb857d56da2503c7 Mon Sep 17 00:00:00 2001 From: Leo Franchi Date: Fri, 13 Jul 2012 17:47:44 -0400 Subject: [PATCH] Call sendMessage() with a QueuedConnection as it's cross-thread --- .../accounts/spotify/SpotifyAccount.cpp | 18 +++++++++++------- .../accounts/spotify/SpotifyAccount.h | 10 ++++++---- .../accounts/spotify/SpotifyInfoPlugin.cpp | 16 +++++++--------- .../accounts/spotify/SpotifyInfoPlugin.h | 4 +--- .../spotify/SpotifyPlaylistUpdater.cpp | 8 +++++--- .../accounts/spotify/SpotifyPlaylistUpdater.h | 6 +++--- 6 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/libtomahawk/accounts/spotify/SpotifyAccount.cpp b/src/libtomahawk/accounts/spotify/SpotifyAccount.cpp index a72b828ea..f37c22645 100644 --- a/src/libtomahawk/accounts/spotify/SpotifyAccount.cpp +++ b/src/libtomahawk/accounts/spotify/SpotifyAccount.cpp @@ -564,8 +564,12 @@ SpotifyAccount::resolverMessage( const QString &msgType, const QVariantMap &msg QObject* receiver = m_qidToSlotMap[ qid ].first; QString slot = m_qidToSlotMap[ qid ].second; m_qidToSlotMap.remove( qid ); + + QVariant extraData; + if ( m_qidToExtraData.contains( qid ) ) + extraData = m_qidToExtraData.take( qid ); - QMetaObject::invokeMethod( receiver, slot.toLatin1(), Q_ARG( QString, msgType ), Q_ARG( QVariantMap, msg ) ); + QMetaObject::invokeMethod( receiver, slot.toLatin1(), Q_ARG( QString, msgType ), Q_ARG( QVariantMap, msg ), Q_ARG( QVariant, extraData ) ); } else if ( msgType == "allPlaylists" ) { @@ -911,7 +915,7 @@ SpotifyAccount::startPlaylistSync( SpotifyPlaylistInfo* playlist ) void -SpotifyAccount::startPlaylistSyncWithPlaylist( const QString& msgType, const QVariantMap& msg ) +SpotifyAccount::startPlaylistSyncWithPlaylist( const QString& msgType, const QVariantMap& msg, const QVariant& ) { Q_UNUSED( msgType ); qDebug() << Q_FUNC_INFO << "Got full spotify playlist body, creating a tomahawk playlist and enabling sync!!"; @@ -959,7 +963,7 @@ SpotifyAccount::startPlaylistSyncWithPlaylist( const QString& msgType, const QVa void -SpotifyAccount::playlistCreated( const QString& msgType, const QVariantMap& msg ) +SpotifyAccount::playlistCreated( const QString& msgType, const QVariantMap& msg, const QVariant& ) { Q_UNUSED( msgType ); @@ -990,20 +994,20 @@ SpotifyAccount::playlistCreated( const QString& msgType, const QVariantMap& msg QString -SpotifyAccount::sendMessage( const QVariantMap &m, QObject* obj, const QString& slot ) +SpotifyAccount::sendMessage( const QVariantMap &m, QObject* obj, const QString& slot, const QVariant& extraData ) { QVariantMap msg = m; - QString qid; + const QString qid = uuid(); if ( obj ) { - qid = QUuid::createUuid().toString().replace( "{", "" ).replace( "}", "" ); - m_qidToSlotMap[ qid ] = qMakePair( obj, slot ); msg[ "qid" ] = qid; } + m_qidToExtraData[ qid ] = extraData; + m_spotifyResolver.data()->sendMessage( msg ); return qid; diff --git a/src/libtomahawk/accounts/spotify/SpotifyAccount.h b/src/libtomahawk/accounts/spotify/SpotifyAccount.h index e524baed4..4347d7bb5 100644 --- a/src/libtomahawk/accounts/spotify/SpotifyAccount.h +++ b/src/libtomahawk/accounts/spotify/SpotifyAccount.h @@ -99,7 +99,6 @@ public: virtual SipPlugin* sipPlugin() { return 0; } 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 ); @@ -111,6 +110,8 @@ public: bool loggedIn() const; public slots: + QString sendMessage( const QVariantMap& msg, QObject* receiver = 0, const QString& slot = QString(), const QVariant& extraData = QVariant() ); + void aboutToShow( QAction* action, const Tomahawk::playlist_ptr& playlist ); void syncActionTriggered( bool ); void atticaLoaded(Attica::Content::List); @@ -125,9 +126,9 @@ private slots: void logout(); // 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 ); + // void ( const QString& msgType, const QVariantMap& msg, const QVariant& extraData ); + void startPlaylistSyncWithPlaylist( const QString& msgType, const QVariantMap& msg, const QVariant& extraData ); + void playlistCreated( const QString& msgType, const QVariantMap& msg, const QVariant& extraData ); void delayedInit(); void hookupAfterDeletion( bool autoEnable ); @@ -156,6 +157,7 @@ private: QWeakPointer< InfoSystem::SpotifyInfoPlugin > m_infoPlugin; QMap > m_qidToSlotMap; + QMap m_qidToExtraData; // List of synced spotify playlists in config UI QList< SpotifyPlaylistInfo* > m_allSpotifyPlaylists; diff --git a/src/libtomahawk/accounts/spotify/SpotifyInfoPlugin.cpp b/src/libtomahawk/accounts/spotify/SpotifyInfoPlugin.cpp index 297b93fe2..b16e24351 100644 --- a/src/libtomahawk/accounts/spotify/SpotifyInfoPlugin.cpp +++ b/src/libtomahawk/accounts/spotify/SpotifyInfoPlugin.cpp @@ -103,9 +103,10 @@ SpotifyInfoPlugin::notInCacheSlot( InfoStringHash criteria, InfoRequestData requ message[ "artist" ] = artist; message[ "album" ] = album; - const QString qid = m_account.data()->sendMessage( message, this, "albumListingResult" ); - - m_waitingForResults[ qid ] = requestData; + QMetaObject::invokeMethod( m_account.data(), "sendMessage", Qt::QueuedConnection, Q_ARG( QVariantMap, message ), + Q_ARG( QObject*, this ), + Q_ARG( QString, "albumListingResult" ), + Q_ARG( QVariant, QVariant::fromValue< InfoRequestData >( requestData ) ) ); } break; } @@ -119,15 +120,12 @@ SpotifyInfoPlugin::notInCacheSlot( InfoStringHash criteria, InfoRequestData requ void -SpotifyInfoPlugin::albumListingResult( const QString& msgType, const QVariantMap& msg ) +SpotifyInfoPlugin::albumListingResult( const QString& msgType, const QVariantMap& msg, const QVariant& extraData ) { Q_ASSERT( msg.contains( "qid" ) ); - Q_ASSERT( m_waitingForResults.contains( msg.value( "qid" ).toString() ) ); + Q_ASSERT( extraData.canConvert< InfoRequestData >() ); - if ( !msg.contains( "qid" ) || !m_waitingForResults.contains( msg.value( "qid" ).toString() ) ) - return; - - const InfoRequestData requestData = m_waitingForResults.take( msg.value( "qid" ).toString() ); + const InfoRequestData requestData = extraData.value< InfoRequestData >(); QVariantList tracks = msg.value( "tracks" ).toList(); QStringList trackNameList; diff --git a/src/libtomahawk/accounts/spotify/SpotifyInfoPlugin.h b/src/libtomahawk/accounts/spotify/SpotifyInfoPlugin.h index fafc0cead..97c0b96ad 100644 --- a/src/libtomahawk/accounts/spotify/SpotifyInfoPlugin.h +++ b/src/libtomahawk/accounts/spotify/SpotifyInfoPlugin.h @@ -46,7 +46,7 @@ public: virtual ~SpotifyInfoPlugin(); public slots: - void albumListingResult( const QString& msgType, const QVariantMap& msg ); + void albumListingResult( const QString& msgType, const QVariantMap& msg, const QVariant& extraData ); protected slots: virtual void init() {} @@ -62,8 +62,6 @@ private: void dataError( InfoRequestData ); void trackListResult( const QStringList& trackNameList, const Tomahawk::InfoSystem::InfoRequestData& requestData ); - QHash< QString, InfoRequestData > m_waitingForResults; - QWeakPointer< Tomahawk::Accounts::SpotifyAccount > m_account; }; diff --git a/src/libtomahawk/accounts/spotify/SpotifyPlaylistUpdater.cpp b/src/libtomahawk/accounts/spotify/SpotifyPlaylistUpdater.cpp index 43ee8cf70..7e5c2d236 100644 --- a/src/libtomahawk/accounts/spotify/SpotifyPlaylistUpdater.cpp +++ b/src/libtomahawk/accounts/spotify/SpotifyPlaylistUpdater.cpp @@ -353,6 +353,8 @@ SpotifyPlaylistUpdater::tomahawkPlaylistRenamed(const QString &newT, const QStri msg[ "newTitle" ] = newT; msg[ "oldTitle" ] = oldT; msg[ "playlistid" ] = m_spotifyId; + + // TODO check return value m_spotify.data()->sendMessage( msg, this, "onPlaylistRename" ); } @@ -493,7 +495,7 @@ SpotifyPlaylistUpdater::plentryToVariant( const QList< plentry_ptr >& entries ) void -SpotifyPlaylistUpdater::onTracksInsertedReturn( const QString& msgType, const QVariantMap& msg ) +SpotifyPlaylistUpdater::onTracksInsertedReturn( const QString& msgType, const QVariantMap& msg, const QVariant& ) { const bool success = msg.value( "success" ).toBool(); @@ -577,7 +579,7 @@ SpotifyPlaylistUpdater::tomahawkTracksRemoved( const QList< query_ptr >& tracks void -SpotifyPlaylistUpdater::onTracksRemovedReturn( const QString& msgType, const QVariantMap& msg ) +SpotifyPlaylistUpdater::onTracksRemovedReturn( const QString& msgType, const QVariantMap& msg, const QVariant& ) { const bool success = msg.value( "success" ).toBool(); @@ -626,7 +628,7 @@ SpotifyPlaylistUpdater::tomahawkTracksMoved( const QList< plentry_ptr >& tracks, void -SpotifyPlaylistUpdater::onTracksMovedReturn( const QString& msgType, const QVariantMap& msg ) +SpotifyPlaylistUpdater::onTracksMovedReturn( const QString& msgType, const QVariantMap& msg, const QVariant& ) { const bool success = msg.value( "success" ).toBool(); diff --git a/src/libtomahawk/accounts/spotify/SpotifyPlaylistUpdater.h b/src/libtomahawk/accounts/spotify/SpotifyPlaylistUpdater.h index 896e507d3..3ac899a1d 100644 --- a/src/libtomahawk/accounts/spotify/SpotifyPlaylistUpdater.h +++ b/src/libtomahawk/accounts/spotify/SpotifyPlaylistUpdater.h @@ -74,9 +74,9 @@ protected: private slots: // SpotifyResolver message handlers, all take msgtype, msg as argument - void onTracksInsertedReturn( const QString& msgType, const QVariantMap& msg ); - void onTracksRemovedReturn( const QString& msgType, const QVariantMap& msg ); - void onTracksMovedReturn( const QString& msgType, const QVariantMap& msg ); + void onTracksInsertedReturn( const QString& msgType, const QVariantMap& msg, const QVariant& extraData ); + void onTracksRemovedReturn( const QString& msgType, const QVariantMap& msg, const QVariant& extraData ); + void onTracksMovedReturn( const QString& msgType, const QVariantMap& msg, const QVariant& extraData ); void checkDeleteDialog() const;