Tidy up some remote control protobuf usage and style quirks.

This commit is contained in:
John Maguire 2013-01-15 13:05:43 +01:00
parent 8cb0b3d349
commit 06568248fc
5 changed files with 100 additions and 149 deletions

View File

@ -25,8 +25,8 @@ enum MsgType {
RANDOM = 28; RANDOM = 28;
// Messages send from server to client // Messages send from server to client
INFOS = 40; INFO = 40;
CURRENT_METAINFOS = 41; CURRENT_METAINFO = 41;
PLAYLISTS = 42; PLAYLISTS = 42;
PLAYLIST_SONGS = 43; PLAYLIST_SONGS = 43;
ENGINE_STATE_CHANGED = 44; ENGINE_STATE_CHANGED = 44;
@ -86,23 +86,6 @@ enum ShuffleMode {
Shuffle_Albums = 3; Shuffle_Albums = 3;
} }
// Message with unknown content
// Shouldn't be used anywhere
message Unknown {
}
// This message is sent when a client connects to clementine
message Connect {
}
// This message is sent when a client disconnects from clementine
message Disconnect {
}
// Client requests all playlists
message RequestPlaylists {
}
// A Client requests songs from a specific playlist // A Client requests songs from a specific playlist
message RequestPlaylistSongs { message RequestPlaylistSongs {
optional int32 id = 1; optional int32 id = 1;
@ -121,15 +104,6 @@ message RequestSetVolume {
optional int32 volume = 1; optional int32 volume = 1;
} }
// Controlmessages
message Play {}
message PlayPause {}
message Pause {}
message Stop {}
message Next {}
message Previous {}
message ShufflePlaylist {}
// Repeat and Random messages // Repeat and Random messages
message Repeat { message Repeat {
optional RepeatMode repeat_mode = 1; optional RepeatMode repeat_mode = 1;
@ -140,9 +114,10 @@ message Shuffle {
} }
// Response from server // Response from server
// General infos // General info
message ResponseClementineInfo { message ResponseClementineInfo {
optional string version = 1; optional string version = 1;
optional EngineState state = 2;
} }
// The current song played // The current song played
@ -158,7 +133,6 @@ message ResponsePlaylists {
// A list of songs in a playlist // A list of songs in a playlist
message ResponsePlaylistSongs { message ResponsePlaylistSongs {
optional Playlist requested_playlist = 1; optional Playlist requested_playlist = 1;
repeated SongMetadata song = 2;
} }
// The current state of the play engine // The current state of the play engine
@ -166,33 +140,19 @@ message ResponseEngineStateChanged {
optional EngineState state = 1; optional EngineState state = 1;
} }
// Empty Keep Alive telegram
message KeepAlive {}
// The message itself // The message itself
message Message { message Message {
optional int32 version = 1 [default=1]; optional int32 version = 1 [default=1];
optional MsgType msgType = 2 [default=UNKNOWN]; // What data is in the message? optional MsgType type = 2 [default=UNKNOWN]; // What data is in the message?
optional Unknown unknown = 10; optional RequestPlaylistSongs request_playlist_songs = 10;
optional Connect connect = 11; optional RequestChangeSong request_change_song = 11;
optional Disconnect disconnect = 12; optional RequestSetVolume request_set_volume = 12;
optional RequestPlaylists request_playlist = 13; optional Repeat repeat = 13;
optional RequestPlaylistSongs request_playlist_songs = 14; optional Shuffle shuffle = 14;
optional RequestChangeSong request_change_song = 15; optional ResponseClementineInfo response_clementine_info = 15;
optional RequestSetVolume request_set_volume = 16; optional ResponseCurrentMetadata response_current_metadata = 16;
optional Play play = 17; optional ResponsePlaylists response_playlists = 17;
optional PlayPause play_pause = 18; optional ResponsePlaylistSongs response_playlist_songs = 18;
optional Pause pause = 19; optional ResponseEngineStateChanged response_engine_state_changed = 19;
optional Stop stop = 20;
optional Next next = 21;
optional Previous previous = 22;
optional ShufflePlaylist shuffle_playlist = 23;
optional Repeat repeat = 24;
optional Shuffle shuffle = 25;
optional ResponseClementineInfo response_clementine_info = 26;
optional ResponseCurrentMetadata response_current_metadata = 27;
optional ResponsePlaylists response_playlists = 28;
optional ResponsePlaylistSongs response_playlist_songs = 29;
optional ResponseEngineStateChanged response_engine_state_changed = 30;
} }

View File

@ -65,17 +65,17 @@ void IncomingDataParser::Parse(const QByteArray& data) {
} }
// Now check what's to do // Now check what's to do
switch (msg.msgtype()) { switch (msg.type()) {
case pb::remote::CONNECT: emit SendClementineInfos(); case pb::remote::CONNECT: emit SendClementineInfo();
emit SendFirstData(); emit SendFirstData();
break; break;
case pb::remote::DISCONNECT: close_connection_ = true; case pb::remote::DISCONNECT: close_connection_ = true;
break; break;
case pb::remote::REQUEST_PLAYLISTS: emit SendAllPlaylists(); case pb::remote::REQUEST_PLAYLISTS: emit SendAllPlaylists();
break; break;
case pb::remote::REQUEST_PLAYLIST_SONGS: GetPlaylistSongs(&msg); case pb::remote::REQUEST_PLAYLIST_SONGS: GetPlaylistSongs(msg);
break; break;
case pb::remote::SET_VOLUME: emit SetVolume(msg.volume()); case pb::remote::SET_VOLUME: emit SetVolume(msg.request_set_volume().volume());
break; break;
case pb::remote::PLAY: emit Play(); case pb::remote::PLAY: emit Play();
break; break;
@ -87,47 +87,29 @@ void IncomingDataParser::Parse(const QByteArray& data) {
break; break;
case pb::remote::NEXT: emit Next(); case pb::remote::NEXT: emit Next();
break; break;
case pb::remote::PREV: emit Previous(); case pb::remote::PREVIOUS: emit Previous();
break; break;
case pb::remote::CHANGE_SONG: ChangeSong(&msg); case pb::remote::CHANGE_SONG: ChangeSong(msg);
break; break;
case pb::remote::TOGGLE_SHUFFLE: emit ShuffleCurrent(); case pb::remote::SHUFFLE_PLAYLIST: emit ShuffleCurrent();
break; break;
default: break; default: break;
} }
} }
void IncomingDataParser::GetPlaylistSongs(pb::remote::Message* msg) { void IncomingDataParser::GetPlaylistSongs(const pb::remote::Message& msg) {
// Check if we got a playlist emit SendPlaylistSongs(msg.request_playlist_songs().id());
if (msg->playlists_size() == 0)
{
return;
}
// Get the first entry and send the songs
pb::remote::Playlist playlist = msg->playlists(0);
emit SendPlaylistSongs(playlist.id());
} }
void IncomingDataParser::ChangeSong(pb::remote::Message* msg) { void IncomingDataParser::ChangeSong(const pb::remote::Message& msg) {
// Check if we got a song
if (msg->playlists_size() == 0) {
return;
}
// Get the first entry and check if there is a song // Get the first entry and check if there is a song
pb::remote::Playlist playlist = msg->playlists(0); const pb::remote::RequestChangeSong& request = msg.request_change_song();
if (playlist.songs_size() == 0) {
return;
}
pb::remote::SongMetadata song = playlist.songs(0);
// Check if we need to change the playlist // Check if we need to change the playlist
if (playlist.id() != app_->playlist_manager()->active_id()) { if (request.playlist_id() != app_->playlist_manager()->active_id()) {
emit SetActivePlaylist(playlist.id()); emit SetActivePlaylist(request.playlist_id());
} }
// Play the selected song // Play the selected song
emit PlayAt(song.index(), Engine::Manual, false); emit PlayAt(request.song_index(), Engine::Manual, false);
} }

View File

@ -17,7 +17,7 @@ public slots:
void Parse(const QByteArray& pb_data); void Parse(const QByteArray& pb_data);
signals: signals:
void SendClementineInfos(); void SendClementineInfo();
void SendFirstData(); void SendFirstData();
void SendAllPlaylists(); void SendAllPlaylists();
void SendPlaylistSongs(int id); void SendPlaylistSongs(int id);
@ -37,8 +37,8 @@ private:
Application* app_; Application* app_;
bool close_connection_; bool close_connection_;
void GetPlaylistSongs(pb::remote::Message* msg); void GetPlaylistSongs(const pb::remote::Message& msg);
void ChangeSong(pb::remote::Message* msg); void ChangeSong(const pb::remote::Message& msg);
}; };
#endif // INCOMINGDATAPARSER_H #endif // INCOMINGDATAPARSER_H

View File

@ -59,23 +59,23 @@ void OutgoingDataCreator::SendDataToClients(pb::remote::Message* msg) {
} }
} }
void OutgoingDataCreator::SendClementineInfos() { void OutgoingDataCreator::SendClementineInfo() {
// Create the general message and set the message type // Create the general message and set the message type
pb::remote::Message msg; pb::remote::Message msg;
msg.set_msgtype(pb::remote::INFOS); msg.set_type(pb::remote::INFO);
// Now add the message specific data // Now add the message specific data
SetEngineState(&msg); pb::remote::ResponseClementineInfo* info =
msg.mutable_response_clementine_info();
SetEngineState(info);
QString version = QString("%1 %2").arg(QCoreApplication::applicationName(), QString version = QString("%1 %2").arg(QCoreApplication::applicationName(),
QCoreApplication::applicationVersion()); QCoreApplication::applicationVersion());
pb::remote::ClementineInfo *info = msg.mutable_info();
info->set_version(version.toAscii()); info->set_version(version.toAscii());
SendDataToClients(&msg); SendDataToClients(&msg);
} }
void OutgoingDataCreator::SetEngineState(pb::remote::Message *msg) { void OutgoingDataCreator::SetEngineState(pb::remote::ResponseClementineInfo* msg) {
switch(app_->player()->GetState()) { switch(app_->player()->GetState()) {
case Engine::Idle: msg->set_state(pb::remote::Idle); case Engine::Idle: msg->set_state(pb::remote::Idle);
break; break;
@ -90,31 +90,33 @@ void OutgoingDataCreator::SetEngineState(pb::remote::Message *msg) {
void OutgoingDataCreator::SendAllPlaylists() { void OutgoingDataCreator::SendAllPlaylists() {
// Get all Playlists // Get all Playlists
QList<Playlist*> playlists = app_->playlist_manager()->GetAllPlaylists(); QList<Playlist*> app_playlists = app_->playlist_manager()->GetAllPlaylists();
QListIterator<Playlist*> i(playlists);
int active_playlist = app_->playlist_manager()->active_id(); int active_playlist = app_->playlist_manager()->active_id();
// Create message // Create message
pb::remote::Message msg; pb::remote::Message msg;
msg.set_msgtype(pb::remote::PLAYLISTS); msg.set_type(pb::remote::PLAYLISTS);
while(i.hasNext()) { pb::remote::ResponsePlaylists* playlists = msg.mutable_response_playlists();
QListIterator<Playlist*> it(app_playlists);
while(it.hasNext()) {
// Get the next Playlist // Get the next Playlist
Playlist* p = i.next(); Playlist* p = it.next();
QString playlist_name = app_->playlist_manager()->GetPlaylistName(p->id()); QString playlist_name = app_->playlist_manager()->GetPlaylistName(p->id());
// Create a new playlist // Create a new playlist
pb::remote::Playlist* playlist = msg.add_playlists(); pb::remote::Playlist* playlist = playlists->add_playlist();
playlist->set_name(playlist_name.toStdString()); playlist->set_name(playlist_name.toStdString());
playlist->set_id(p->id()); playlist->set_id(p->id());
playlist->set_active((p->id() == active_playlist)); playlist->set_active((p->id() == active_playlist));
// TODO: Fill in the song metadata here. playlist->set_item_count(p->rowCount());
} }
SendDataToClients(&msg); SendDataToClients(&msg);
} }
void OutgoingDataCreator::ActiveChanged(Playlist *) { void OutgoingDataCreator::ActiveChanged(Playlist*) {
// When a playlist was changed, send the new list // When a playlist was changed, send the new list
SendAllPlaylists(); SendAllPlaylists();
} }
@ -140,35 +142,40 @@ void OutgoingDataCreator::CurrentSongChanged(const Song& song, const QString& ur
if (!clients_->empty()) { if (!clients_->empty()) {
// Create the message // Create the message
pb::remote::Message msg; pb::remote::Message msg;
msg.set_msgtype(pb::remote::CURRENT_METAINFOS); msg.set_type(pb::remote::CURRENT_METAINFO);
// If there is no song, create an empty node, otherwise fill it with data // If there is no song, create an empty node, otherwise fill it with data
int i = app_->playlist_manager()->active()->current_row(); int i = app_->playlist_manager()->active()->current_row();
CreateSong(msg.mutable_currentsong(), &current_song_, &uri, i); CreateSong(
current_song_, uri, i,
msg.mutable_response_current_metadata()->mutable_song_metadata());
SendDataToClients(&msg); SendDataToClients(&msg);
} }
} }
void OutgoingDataCreator::CreateSong(pb::remote::SongMetadata* song_metadata, void OutgoingDataCreator::CreateSong(
Song* song, const QString* artUri, int index) { const Song& song,
if (song->is_valid()) { const QString& art_uri,
song_metadata->set_id(song->id()); const int index,
pb::remote::SongMetadata* song_metadata) {
if (song.is_valid()) {
song_metadata->set_id(song.id());
song_metadata->set_index(index); song_metadata->set_index(index);
song_metadata->set_title( DataCommaSizeFromQString(song->PrettyTitle())); song_metadata->set_title(DataCommaSizeFromQString(song.PrettyTitle()));
song_metadata->set_artist(DataCommaSizeFromQString(song->artist())); song_metadata->set_artist(DataCommaSizeFromQString(song.artist()));
song_metadata->set_album( DataCommaSizeFromQString(song->album())); song_metadata->set_album(DataCommaSizeFromQString(song.album()));
song_metadata->set_albumartist(DataCommaSizeFromQString(song->albumartist())); song_metadata->set_albumartist(DataCommaSizeFromQString(song.albumartist()));
song_metadata->set_pretty_length(DataCommaSizeFromQString(song->PrettyLength())); song_metadata->set_pretty_length(DataCommaSizeFromQString(song.PrettyLength()));
song_metadata->set_genre(DataCommaSizeFromQString(song->genre())); song_metadata->set_genre(DataCommaSizeFromQString(song.genre()));
song_metadata->set_pretty_year(DataCommaSizeFromQString(song->PrettyYear())); song_metadata->set_pretty_year(DataCommaSizeFromQString(song.PrettyYear()));
song_metadata->set_track(song->track()); song_metadata->set_track(song.track());
song_metadata->set_disc(song->disc()); song_metadata->set_disc(song.disc());
song_metadata->set_playcount(song->playcount()); song_metadata->set_playcount(song.playcount());
// Append coverart // Append coverart
if (!artUri->isEmpty()) { if (!art_uri.isEmpty()) {
QImage orig(QUrl(*artUri).toLocalFile()); QImage orig(QUrl(art_uri).toLocalFile());
QImage small; QImage small;
// Check if we resize the image // Check if we resize the image
if (orig.width() > 1000) { if (orig.width() > 1000) {
@ -184,9 +191,7 @@ void OutgoingDataCreator::CreateSong(pb::remote::SongMetadata* song_metadata,
small.save(&buf, "JPG"); small.save(&buf, "JPG");
// Append the Data in the protocol buffer // Append the Data in the protocol buffer
song_metadata->set_art(data.data(), data.size()); song_metadata->set_art(data.constData(), data.size());
buf.close();
} }
} }
} }
@ -195,8 +200,8 @@ void OutgoingDataCreator::CreateSong(pb::remote::SongMetadata* song_metadata,
void OutgoingDataCreator::VolumeChanged(int volume) { void OutgoingDataCreator::VolumeChanged(int volume) {
// Create the message // Create the message
pb::remote::Message msg; pb::remote::Message msg;
msg.set_msgtype(pb::remote::SET_VOLUME); msg.set_type(pb::remote::SET_VOLUME);
msg.set_volume(volume); msg.mutable_request_set_volume()->set_volume(volume);
SendDataToClients(&msg); SendDataToClients(&msg);
} }
@ -208,23 +213,23 @@ void OutgoingDataCreator::SendPlaylistSongs(int id) {
return; return;
} }
SongList song_list = playlist->GetAllSongs();
QListIterator<Song> i(song_list);
// Create the message and the playlist // Create the message and the playlist
pb::remote::Message msg; pb::remote::Message msg;
msg.set_msgtype(pb::remote::PLAYLIST_SONGS); msg.set_type(pb::remote::PLAYLIST_SONGS);
// Create a new playlist // Create a new playlist
pb::remote::Playlist* pb_playlist = msg.add_playlists(); pb::remote::Playlist* pb_playlist =
msg.mutable_response_playlist_songs()->mutable_requested_playlist();
pb_playlist->set_id(id); pb_playlist->set_id(id);
// Send all songs // Send all songs
int index = 0; int index = 0;
while(i.hasNext()) { SongList song_list = playlist->GetAllSongs();
Song song = i.next(); QListIterator<Song> it(song_list);
while(it.hasNext()) {
Song song = it.next();
QString art = song.art_automatic(); QString art = song.art_automatic();
pb::remote::SongMetadata* pb_song = pb_playlist->add_songs(); pb::remote::SongMetadata* pb_song = pb_playlist->add_songs();
CreateSong(pb_song, &song, &art, index); CreateSong(song, art, index, pb_song);
++index; ++index;
} }
SendDataToClients(&msg); SendDataToClients(&msg);
@ -247,13 +252,13 @@ void OutgoingDataCreator::StateChanged(Engine::State state) {
pb::remote::Message msg; pb::remote::Message msg;
switch (state) { switch (state) {
case Engine::Playing: msg.set_msgtype(pb::remote::PLAY); case Engine::Playing: msg.set_type(pb::remote::PLAY);
break; break;
case Engine::Paused: msg.set_msgtype(pb::remote::PAUSE); case Engine::Paused: msg.set_type(pb::remote::PAUSE);
break; break;
case Engine::Empty: msg.set_msgtype(pb::remote::STOP); // Empty is called when player stopped case Engine::Empty: msg.set_type(pb::remote::STOP); // Empty is called when player stopped
break; break;
default: msg.set_msgtype(pb::remote::STOP); default: msg.set_type(pb::remote::STOP);
break; break;
}; };
@ -262,6 +267,6 @@ void OutgoingDataCreator::StateChanged(Engine::State state) {
void OutgoingDataCreator::SendKeepAlive() { void OutgoingDataCreator::SendKeepAlive() {
pb::remote::Message msg; pb::remote::Message msg;
msg.set_msgtype(pb::remote::KEEP_ALIVE); msg.set_type(pb::remote::KEEP_ALIVE);
SendDataToClients(&msg); SendDataToClients(&msg);
} }

View File

@ -24,7 +24,7 @@ public:
void SetClients(QList<RemoteClient*>* clients); void SetClients(QList<RemoteClient*>* clients);
public slots: public slots:
void SendClementineInfos(); void SendClementineInfo();
void SendAllPlaylists(); void SendAllPlaylists();
void SendFirstData(); void SendFirstData();
void SendPlaylistSongs(int id); void SendPlaylistSongs(int id);
@ -46,8 +46,12 @@ private:
int keep_alive_timeout_; int keep_alive_timeout_;
void SendDataToClients(pb::remote::Message* msg); void SendDataToClients(pb::remote::Message* msg);
void SetEngineState(pb::remote::Message* msg); void SetEngineState(pb::remote::ResponseClementineInfo* msg);
void CreateSong(pb::remote::SongMetadata* song_metadata, Song* song, const QString* art_uri, int index); void CreateSong(
const Song& song,
const QString& art_uri,
const int index,
pb::remote::SongMetadata* song_metadata);
}; };
#endif // OUTGOINGDATACREATOR_H #endif // OUTGOINGDATACREATOR_H