Skip to content

Commit

Permalink
properly hack Clementine behaviour to make sure the OUR last active s…
Browse files Browse the repository at this point in the history
…ong is replayed (STOP then PAUSE)

as Clementine would start the one from it's current opened tab
+ fix potential crash accessing wrong song index
  • Loading branch information
mbruel committed Nov 20, 2020
1 parent cd55f1b commit 85de845
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
23 changes: 22 additions & 1 deletion src/ClementineRemote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ ClementineRemote::ClementineRemote(QObject *parent):
#ifdef __USE_CONNECTION_THREAD__
_secureUserMsg(),
#endif
_userMsg()
_userMsg(),
_forceRePlayActiveSong(false)
{
setObjectName(sAppName);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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]";
Expand Down Expand Up @@ -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;
}
}



////////////////////////////////
Expand Down
7 changes: 4 additions & 3 deletions src/ClementineRemote.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ class ClementineRemote : public QObject, public Singleton<ClementineRemote>
#endif
pb::remote::Message _userMsg;

bool _forceRePlayActiveSong;
QPair<int, int> _activeSongAndPlaylistIndexes;


private:
ClementineRemote(QObject *parent = nullptr);
Expand Down Expand Up @@ -208,7 +211,6 @@ class ClementineRemote : public QObject, public Singleton<ClementineRemote>
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;

Expand Down Expand Up @@ -313,7 +315,7 @@ class ClementineRemote : public QObject, public Singleton<ClementineRemote>
void doSendSongsToDownload();
void doSendInsertUrls(qint32 playlistID, const QString &newPlaylistName);


void shallForceRePlayActiveSong();

////////////////////////////////
/// Radio methods
Expand Down Expand Up @@ -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; }
Expand Down
16 changes: 4 additions & 12 deletions src/ConnectionWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(_remote->currentSongIndex()), _remote->activePlaylistID());
}

}

void ConnectionWorker::onShuffle(ushort mode)
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 85de845

Please sign in to comment.