From 85de845113c8ac6f1b7eadce10d3420207b0102f Mon Sep 17 00:00:00 2001 From: Matthieu Bruel Date: Fri, 20 Nov 2020 15:23:04 +0100 Subject: [PATCH] properly hack Clementine behaviour to make sure the OUR last active song is replayed (STOP then PAUSE) as Clementine would start the one from it's current opened tab + fix potential crash accessing wrong song index --- src/ClementineRemote.cpp | 23 ++++++++++++++++++++++- src/ClementineRemote.h | 7 ++++--- src/ConnectionWorker.cpp | 16 ++++------------ 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/ClementineRemote.cpp b/src/ClementineRemote.cpp index 9546e01..1a61dd5 100644 --- a/src/ClementineRemote.cpp +++ b/src/ClementineRemote.cpp @@ -127,7 +127,8 @@ ClementineRemote::ClementineRemote(QObject *parent): #ifdef __USE_CONNECTION_THREAD__ _secureUserMsg(), #endif - _userMsg() + _userMsg(), + _forceRePlayActiveSong(false) { setObjectName(sAppName); @@ -221,6 +222,7 @@ void ClementineRemote::close() void ClementineRemote::clearData(const QString &reason) { _initialized = false; + _forceRePlayActiveSong = false; emit _plOpenedModel->preClearPlaylists(_playlistsOpened.size() - 1 ); qDeleteAll(_playlistsOpened); @@ -452,6 +454,16 @@ void ClementineRemote::parseMessage(const QByteArray &data) qDebug() << "[MsgType::PLAY]"; _clemState = pb::remote::EngineState::Playing; emit updateEngineState(); + if (_forceRePlayActiveSong) + { + // Hack to make sure last played song will be played + // as Clementine would start the one from it's current opened tab + qDebug() << "Force replay OUR active song: " << _activeSongAndPlaylistIndexes.first + << " (playlist: " << _activeSongAndPlaylistIndexes.second << ")"; + _connection->sendChangeSong(_activeSongAndPlaylistIndexes.first, _activeSongAndPlaylistIndexes.second); + _forceRePlayActiveSong = false; + } + break; case pb::remote::PAUSE: qDebug() << "[MsgType::PAUSE]"; @@ -985,6 +997,15 @@ void ClementineRemote::doSendInsertUrls(qint32 playlistID, const QString &newPla releaseUserMutex(); } +void ClementineRemote::shallForceRePlayActiveSong() +{ + if (_previousClemState == pb::remote::EngineState::Idle) + { + _activeSongAndPlaylistIndexes = {_activeSongIndex, _activePlaylistId}; + _forceRePlayActiveSong = true; + } +} + //////////////////////////////// diff --git a/src/ClementineRemote.h b/src/ClementineRemote.h index 9bc9c33..0ff576e 100644 --- a/src/ClementineRemote.h +++ b/src/ClementineRemote.h @@ -146,6 +146,9 @@ class ClementineRemote : public QObject, public Singleton #endif pb::remote::Message _userMsg; + bool _forceRePlayActiveSong; + QPair _activeSongAndPlaylistIndexes; + private: ClementineRemote(QObject *parent = nullptr); @@ -208,7 +211,6 @@ class ClementineRemote : public QObject, public Singleton inline Q_INVOKABLE void saveConnectionInSettings(const QString &host, const QString &port, const QString &pass); inline Q_INVOKABLE qint32 playerState() const; - inline qint32 playerPreviousState() const; inline Q_INVOKABLE bool isPlaying() const; inline Q_INVOKABLE bool isPaused() const; @@ -313,7 +315,7 @@ class ClementineRemote : public QObject, public Singleton void doSendSongsToDownload(); void doSendInsertUrls(qint32 playlistID, const QString &newPlaylistName); - + void shallForceRePlayActiveSong(); //////////////////////////////// /// Radio methods @@ -599,7 +601,6 @@ void ClementineRemote::saveConnectionInSettings(const QString &host, const QStri } qint32 ClementineRemote::playerState() const { return _clemState; } -qint32 ClementineRemote::playerPreviousState() const { return _previousClemState; } bool ClementineRemote::isPlaying() const { return _clemState == pb::remote::EngineState::Playing; } bool ClementineRemote::isPaused() const { return _clemState == pb::remote::EngineState::Paused; } diff --git a/src/ConnectionWorker.cpp b/src/ConnectionWorker.cpp index 99624ae..6e5f4bd 100644 --- a/src/ConnectionWorker.cpp +++ b/src/ConnectionWorker.cpp @@ -175,16 +175,10 @@ void ConnectionWorker::onSetEngineState(qint32 state) break; } + if (msg.type() == pb::remote::PLAY) + _remote->shallForceRePlayActiveSong(); sendDataToServer(msg); - - // Hack to make sure last played song will be resumed - if (_remote->playerPreviousState() == pb::remote::EngineState::Idle && msg.type() == pb::remote::PLAY ) - { - qDebug() << "[ConnectionWorker::onSetEngineState] try to play previous song: " << _remote->currentSong().title; - sendChangeSong(static_cast(_remote->currentSongIndex()), _remote->activePlaylistID()); - } - } void ConnectionWorker::onShuffle(ushort mode) @@ -440,17 +434,15 @@ void ConnectionWorker::onInsertUrls(qint32 playlistID, const QString &newPlaylis void ConnectionWorker::sendChangeSong(int songIndex, qint32 playlistID) { - const RemoteSong &song = _remote->playlistSong(songIndex); - pb::remote::Message msg; msg.set_type(pb::remote::CHANGE_SONG); pb::remote::RequestChangeSong *request = msg.mutable_request_change_song(); if (playlistID != -1) request->set_playlist_id(playlistID); - qDebug() << "[ConnectionWorker::onChangeToSong] idx: " << songIndex << " : "<< song.str(); + qDebug() << "[ConnectionWorker::onChangeToSong] playlistID: " << playlistID << ", songIndex: " << songIndex; - request->set_song_index(song.index); + request->set_song_index(songIndex); sendDataToServer(msg); }