2013-01-03 21:40:47 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2012, Andreas Muttscheller <asfa194@gmail.com>
|
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "incomingdataparser.h"
|
2013-04-14 00:58:49 +02:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2013-01-03 21:40:47 +01:00
|
|
|
#include "core/logging.h"
|
|
|
|
#include "engines/enginebase.h"
|
2013-05-07 17:57:53 +02:00
|
|
|
#include "internet/internetmodel.h"
|
2013-01-03 21:40:47 +01:00
|
|
|
#include "playlist/playlistmanager.h"
|
2013-01-15 14:43:02 +01:00
|
|
|
#include "playlist/playlistsequence.h"
|
2013-04-13 11:57:05 +02:00
|
|
|
#include "playlist/playlist.h"
|
2013-01-03 21:40:47 +01:00
|
|
|
|
2013-05-07 19:09:31 +02:00
|
|
|
#ifdef HAVE_LIBLASTFM
|
|
|
|
# include "internet/lastfmservice.h"
|
|
|
|
#endif
|
|
|
|
|
2013-01-03 21:40:47 +01:00
|
|
|
IncomingDataParser::IncomingDataParser(Application* app)
|
|
|
|
:app_(app)
|
|
|
|
{
|
|
|
|
// Connect all the signals
|
|
|
|
// due the player is in a different thread, we cannot access these functions directly
|
|
|
|
connect(this, SIGNAL(Play()),
|
|
|
|
app_->player(), SLOT(Play()));
|
|
|
|
connect(this, SIGNAL(PlayPause()),
|
|
|
|
app_->player(), SLOT(PlayPause()));
|
|
|
|
connect(this, SIGNAL(Pause()),
|
|
|
|
app_->player(), SLOT(Pause()));
|
|
|
|
connect(this, SIGNAL(Stop()),
|
|
|
|
app_->player(), SLOT(Stop()));
|
2013-07-20 19:38:37 +02:00
|
|
|
connect(this, SIGNAL(StopAfterCurrent()),
|
|
|
|
app_->player(), SLOT(StopAfterCurrent()));
|
2013-01-03 21:40:47 +01:00
|
|
|
connect(this, SIGNAL(Next()),
|
|
|
|
app_->player(), SLOT(Next()));
|
|
|
|
connect(this, SIGNAL(Previous()),
|
|
|
|
app_->player(), SLOT(Previous()));
|
|
|
|
connect(this, SIGNAL(SetVolume(int)),
|
|
|
|
app_->player(), SLOT(SetVolume(int)));
|
|
|
|
connect(this, SIGNAL(PlayAt(int,Engine::TrackChangeFlags,bool)),
|
|
|
|
app_->player(), SLOT(PlayAt(int,Engine::TrackChangeFlags,bool)));
|
2013-01-23 20:02:12 +01:00
|
|
|
connect(this, SIGNAL(SeekTo(int)),
|
|
|
|
app_->player(), SLOT(SeekTo(int)));
|
2013-01-16 19:26:33 +01:00
|
|
|
|
2013-01-03 21:40:47 +01:00
|
|
|
connect(this, SIGNAL(SetActivePlaylist(int)),
|
|
|
|
app_->playlist_manager(), SLOT(SetActivePlaylist(int)));
|
2013-01-09 20:07:28 +01:00
|
|
|
connect(this, SIGNAL(ShuffleCurrent()),
|
|
|
|
app_->playlist_manager(), SLOT(ShuffleCurrent()));
|
2013-01-15 14:43:02 +01:00
|
|
|
connect(this, SIGNAL(SetRepeatMode(PlaylistSequence::RepeatMode)),
|
|
|
|
app_->playlist_manager()->sequence(),
|
|
|
|
SLOT(SetRepeatMode(PlaylistSequence::RepeatMode)));
|
|
|
|
connect(this, SIGNAL(SetShuffleMode(PlaylistSequence::ShuffleMode)),
|
|
|
|
app_->playlist_manager()->sequence(),
|
|
|
|
SLOT(SetShuffleMode(PlaylistSequence::ShuffleMode)));
|
2013-07-31 19:06:29 +02:00
|
|
|
connect(this, SIGNAL(InsertUrls(int, const QList<QUrl>&, int, bool, bool)),
|
|
|
|
app_->playlist_manager(),
|
|
|
|
SLOT(InsertUrls(int, const QList<QUrl>&, int, bool, bool)));
|
|
|
|
connect(this, SIGNAL(RemoveSongs(int, const QList<int>&)),
|
|
|
|
app_->playlist_manager(),
|
|
|
|
SLOT(RemoveItemsWithoutUndo(int, const QList<int>&)));
|
2013-04-20 16:11:57 +02:00
|
|
|
connect(this, SIGNAL(Open(int)),
|
|
|
|
app_->playlist_manager(), SLOT(Open(int)));
|
|
|
|
connect(this, SIGNAL(Close(int)),
|
|
|
|
app_->playlist_manager(), SLOT(Close(int)));
|
2013-05-07 17:57:53 +02:00
|
|
|
|
2013-09-22 12:06:19 +02:00
|
|
|
connect(this, SIGNAL(RateCurrentSong(double)),
|
|
|
|
app_->playlist_manager(), SLOT(RateCurrentSong(double)));
|
2013-08-15 20:52:12 +02:00
|
|
|
|
2013-05-07 19:09:31 +02:00
|
|
|
#ifdef HAVE_LIBLASTFM
|
2013-05-07 17:57:53 +02:00
|
|
|
connect(this, SIGNAL(Love()),
|
|
|
|
InternetModel::Service<LastFMService>(), SLOT(Love()));
|
|
|
|
connect(this, SIGNAL(Ban()),
|
|
|
|
InternetModel::Service<LastFMService>(), SLOT(Ban()));
|
2013-05-07 19:09:31 +02:00
|
|
|
#endif
|
2013-01-03 21:40:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
IncomingDataParser::~IncomingDataParser() {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IncomingDataParser::close_connection() {
|
|
|
|
return close_connection_;
|
|
|
|
}
|
|
|
|
|
2013-01-23 20:02:12 +01:00
|
|
|
void IncomingDataParser::Parse(const pb::remote::Message& msg) {
|
2013-01-03 21:40:47 +01:00
|
|
|
close_connection_ = false;
|
|
|
|
|
2013-07-12 12:31:27 +02:00
|
|
|
RemoteClient* client = qobject_cast<RemoteClient*>(sender());
|
|
|
|
|
2013-01-03 21:40:47 +01:00
|
|
|
// Now check what's to do
|
2013-01-15 13:05:43 +01:00
|
|
|
switch (msg.type()) {
|
2013-08-01 18:13:14 +02:00
|
|
|
case pb::remote::CONNECT:
|
|
|
|
ClientConnect(msg);
|
|
|
|
break;
|
|
|
|
case pb::remote::DISCONNECT:
|
|
|
|
close_connection_ = true;
|
|
|
|
break;
|
|
|
|
case pb::remote::REQUEST_PLAYLISTS:
|
|
|
|
SendPlaylists(msg);
|
|
|
|
break;
|
|
|
|
case pb::remote::REQUEST_PLAYLIST_SONGS:
|
|
|
|
GetPlaylistSongs(msg);
|
|
|
|
break;
|
|
|
|
case pb::remote::SET_VOLUME:
|
|
|
|
emit SetVolume(msg.request_set_volume().volume());
|
|
|
|
break;
|
|
|
|
case pb::remote::PLAY:
|
|
|
|
emit Play();
|
|
|
|
break;
|
|
|
|
case pb::remote::PLAYPAUSE:
|
|
|
|
emit PlayPause();
|
|
|
|
break;
|
|
|
|
case pb::remote::PAUSE:
|
|
|
|
emit Pause();
|
|
|
|
break;
|
|
|
|
case pb::remote::STOP:
|
|
|
|
emit Stop();
|
|
|
|
break;
|
|
|
|
case pb::remote::STOP_AFTER:
|
|
|
|
emit StopAfterCurrent();
|
|
|
|
break;
|
|
|
|
case pb::remote::NEXT:
|
|
|
|
emit Next();
|
|
|
|
break;
|
|
|
|
case pb::remote::PREVIOUS:
|
|
|
|
emit Previous();
|
|
|
|
break;
|
|
|
|
case pb::remote::CHANGE_SONG:
|
|
|
|
ChangeSong(msg);
|
|
|
|
break;
|
|
|
|
case pb::remote::SHUFFLE_PLAYLIST:
|
|
|
|
emit ShuffleCurrent();
|
|
|
|
break;
|
|
|
|
case pb::remote::REPEAT:
|
|
|
|
SetRepeatMode(msg.repeat());
|
|
|
|
break;
|
|
|
|
case pb::remote::SHUFFLE:
|
|
|
|
SetShuffleMode(msg.shuffle());
|
|
|
|
break;
|
2013-01-23 20:02:12 +01:00
|
|
|
case pb::remote::SET_TRACK_POSITION:
|
2013-08-01 18:13:14 +02:00
|
|
|
emit SeekTo(msg.request_set_track_position().position());
|
|
|
|
break;
|
|
|
|
case pb::remote::INSERT_URLS:
|
|
|
|
InsertUrls(msg);
|
|
|
|
break;
|
|
|
|
case pb::remote::REMOVE_SONGS:
|
|
|
|
RemoveSongs(msg);
|
|
|
|
break;
|
2013-04-20 16:11:57 +02:00
|
|
|
case pb::remote::OPEN_PLAYLIST:
|
2013-08-01 18:13:14 +02:00
|
|
|
OpenPlaylist(msg);
|
|
|
|
break;
|
2013-04-20 16:11:57 +02:00
|
|
|
case pb::remote::CLOSE_PLAYLIST:
|
2013-08-01 18:13:14 +02:00
|
|
|
ClosePlaylist(msg);
|
|
|
|
break;
|
|
|
|
case pb::remote::LOVE:
|
|
|
|
emit Love();
|
|
|
|
break;
|
|
|
|
case pb::remote::BAN:
|
|
|
|
emit Ban();
|
|
|
|
break;
|
|
|
|
case pb::remote::GET_LYRICS:
|
|
|
|
emit GetLyrics();
|
|
|
|
break;
|
2013-07-12 12:31:27 +02:00
|
|
|
case pb::remote::DOWNLOAD_SONGS:
|
2013-08-01 18:13:14 +02:00
|
|
|
emit SendSongs(msg.request_download_songs(), client);
|
|
|
|
break;
|
2013-07-23 16:59:50 +02:00
|
|
|
case pb::remote::SONG_OFFER_RESPONSE:
|
2013-08-01 18:13:14 +02:00
|
|
|
emit ResponseSongOffer(client, msg.response_song_offer().accepted());
|
|
|
|
break;
|
|
|
|
case pb::remote::GET_LIBRARY:
|
|
|
|
emit SendLibrary(client);
|
|
|
|
break;
|
2013-08-15 20:52:12 +02:00
|
|
|
case pb::remote::RATE_SONG:
|
|
|
|
RateSong(msg);
|
|
|
|
break;
|
2013-01-03 21:40:47 +01:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-15 13:05:43 +01:00
|
|
|
void IncomingDataParser::GetPlaylistSongs(const pb::remote::Message& msg) {
|
|
|
|
emit SendPlaylistSongs(msg.request_playlist_songs().id());
|
2013-01-03 21:40:47 +01:00
|
|
|
}
|
|
|
|
|
2013-01-15 13:05:43 +01:00
|
|
|
void IncomingDataParser::ChangeSong(const pb::remote::Message& msg) {
|
2013-01-03 21:40:47 +01:00
|
|
|
// Get the first entry and check if there is a song
|
2013-01-15 13:05:43 +01:00
|
|
|
const pb::remote::RequestChangeSong& request = msg.request_change_song();
|
2013-01-03 21:40:47 +01:00
|
|
|
|
|
|
|
// Check if we need to change the playlist
|
2013-01-15 13:05:43 +01:00
|
|
|
if (request.playlist_id() != app_->playlist_manager()->active_id()) {
|
|
|
|
emit SetActivePlaylist(request.playlist_id());
|
2013-01-03 21:40:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Play the selected song
|
2013-01-15 13:05:43 +01:00
|
|
|
emit PlayAt(request.song_index(), Engine::Manual, false);
|
2013-01-03 21:40:47 +01:00
|
|
|
}
|
2013-01-15 14:43:02 +01:00
|
|
|
|
|
|
|
void IncomingDataParser::SetRepeatMode(const pb::remote::Repeat& repeat) {
|
|
|
|
switch (repeat.repeat_mode()) {
|
2013-08-01 18:13:14 +02:00
|
|
|
case pb::remote::Repeat_Off:
|
|
|
|
emit SetRepeatMode(PlaylistSequence::Repeat_Off);
|
|
|
|
break;
|
|
|
|
case pb::remote::Repeat_Track:
|
|
|
|
emit SetRepeatMode(PlaylistSequence::Repeat_Track);
|
|
|
|
break;
|
|
|
|
case pb::remote::Repeat_Album:
|
|
|
|
emit SetRepeatMode(PlaylistSequence::Repeat_Album);
|
|
|
|
break;
|
|
|
|
case pb::remote::Repeat_Playlist:
|
|
|
|
emit SetRepeatMode(PlaylistSequence::Repeat_Playlist);
|
|
|
|
break;
|
|
|
|
default: break;
|
2013-01-15 14:43:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IncomingDataParser::SetShuffleMode(const pb::remote::Shuffle& shuffle) {
|
|
|
|
switch (shuffle.shuffle_mode()) {
|
2013-08-01 18:13:14 +02:00
|
|
|
case pb::remote::Shuffle_Off:
|
|
|
|
emit SetShuffleMode(PlaylistSequence::Shuffle_Off);
|
|
|
|
break;
|
|
|
|
case pb::remote::Shuffle_All:
|
|
|
|
emit SetShuffleMode(PlaylistSequence::Shuffle_All);
|
|
|
|
break;
|
|
|
|
case pb::remote::Shuffle_InsideAlbum:
|
|
|
|
emit SetShuffleMode(PlaylistSequence::Shuffle_InsideAlbum);
|
|
|
|
break;
|
|
|
|
case pb::remote::Shuffle_Albums:
|
|
|
|
emit SetShuffleMode(PlaylistSequence::Shuffle_Albums);
|
|
|
|
break;
|
|
|
|
default: break;
|
2013-01-15 14:43:02 +01:00
|
|
|
}
|
|
|
|
}
|
2013-03-27 16:54:02 +01:00
|
|
|
|
2013-04-13 11:57:05 +02:00
|
|
|
void IncomingDataParser::InsertUrls(const pb::remote::Message& msg) {
|
|
|
|
const pb::remote::RequestInsertUrls& request = msg.request_insert_urls();
|
|
|
|
|
|
|
|
// Extract urls
|
|
|
|
QList<QUrl> urls;
|
2013-04-14 00:58:49 +02:00
|
|
|
for (auto it = request.urls().begin(); it != request.urls().end(); ++it) {
|
|
|
|
urls << QUrl(QString::fromStdString(*it));
|
2013-04-13 11:57:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert the urls
|
2013-07-31 19:06:29 +02:00
|
|
|
emit InsertUrls(request.playlist_id(), urls, request.position(),
|
|
|
|
request.play_now(), request.enqueue());
|
2013-04-13 11:57:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IncomingDataParser::RemoveSongs(const pb::remote::Message& msg) {
|
2013-10-31 19:52:02 +01:00
|
|
|
const pb::remote::RequestRemoveSongs& request = msg.request_remove_songs();
|
2013-04-13 11:57:05 +02:00
|
|
|
|
2013-10-31 19:52:02 +01:00
|
|
|
// Extract urls
|
|
|
|
QList<int> songs;
|
|
|
|
for (int i = 0; i<request.songs().size();i++) {
|
|
|
|
songs.append(request.songs(i));
|
|
|
|
}
|
2013-04-13 11:57:05 +02:00
|
|
|
|
2013-10-31 19:52:02 +01:00
|
|
|
// Insert the urls
|
|
|
|
emit RemoveSongs(request.playlist_id(), songs);
|
2013-04-13 11:57:05 +02:00
|
|
|
}
|
|
|
|
|
2013-07-12 12:41:45 +02:00
|
|
|
void IncomingDataParser::ClientConnect(const pb::remote::Message& msg) {
|
2013-07-12 12:31:27 +02:00
|
|
|
|
2013-03-27 16:54:02 +01:00
|
|
|
// Always sned the Clementine infos
|
|
|
|
emit SendClementineInfo();
|
|
|
|
|
|
|
|
// Check if we should send the first data
|
|
|
|
if (!msg.request_connect().has_send_playlist_songs() // legacy
|
|
|
|
|| msg.request_connect().send_playlist_songs()) {
|
|
|
|
emit SendFirstData(true);
|
|
|
|
} else {
|
|
|
|
emit SendFirstData(false);
|
|
|
|
}
|
|
|
|
}
|
2013-04-16 13:57:04 +02:00
|
|
|
|
|
|
|
void IncomingDataParser::SendPlaylists(const pb::remote::Message &msg) {
|
|
|
|
if (!msg.has_request_playlists()
|
|
|
|
|| !msg.request_playlists().include_closed()) {
|
|
|
|
emit SendAllActivePlaylists();
|
|
|
|
} else {
|
|
|
|
emit SendAllPlaylists();
|
|
|
|
}
|
|
|
|
}
|
2013-04-20 16:11:57 +02:00
|
|
|
|
|
|
|
void IncomingDataParser::OpenPlaylist(const pb::remote::Message &msg) {
|
|
|
|
emit Open(msg.request_open_playlist().playlist_id());
|
|
|
|
}
|
|
|
|
|
|
|
|
void IncomingDataParser::ClosePlaylist(const pb::remote::Message &msg) {
|
|
|
|
emit Close(msg.request_close_playlist().playlist_id());
|
|
|
|
}
|
2013-08-15 20:52:12 +02:00
|
|
|
|
|
|
|
void IncomingDataParser::RateSong(const pb::remote::Message &msg) {
|
2013-09-22 12:06:19 +02:00
|
|
|
double rating = (double) msg.request_rate_song().rating();
|
2013-08-15 20:52:12 +02:00
|
|
|
emit RateCurrentSong(rating);
|
|
|
|
}
|