2011-04-25 21:16:26 +02:00
|
|
|
/* This file is part of Clementine.
|
2014-12-17 19:02:21 +01:00
|
|
|
Copyright 2011-2012, 2014, John Maguire <john.maguire@gmail.com>
|
|
|
|
Copyright 2011-2012, 2014, David Sansome <me@davidsansome.com>
|
|
|
|
Copyright 2014, Arnaud Bienner <arnaud.bienner@gmail.com>
|
|
|
|
Copyright 2014, pie.or.paj <pie.or.paj@gmail.com>
|
|
|
|
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
2011-04-25 21:16:26 +02:00
|
|
|
|
|
|
|
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 "spotifyserver.h"
|
|
|
|
|
|
|
|
#include <QTcpServer>
|
|
|
|
#include <QTcpSocket>
|
2011-11-28 19:11:09 +01:00
|
|
|
#include <QTimer>
|
2014-09-22 22:40:21 +02:00
|
|
|
#include <QUrl>
|
2011-04-25 21:16:26 +02:00
|
|
|
|
2014-12-19 00:40:30 +01:00
|
|
|
#include "core/closure.h"
|
|
|
|
#include "core/logging.h"
|
|
|
|
|
|
|
|
#include "spotifymessages.pb.h"
|
|
|
|
|
2011-04-25 21:16:26 +02:00
|
|
|
SpotifyServer::SpotifyServer(QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: AbstractMessageHandler<pb::spotify::Message>(nullptr, parent),
|
|
|
|
server_(new QTcpServer(this)),
|
|
|
|
logged_in_(false) {
|
2011-04-25 21:16:26 +02:00
|
|
|
connect(server_, SIGNAL(newConnection()), SLOT(NewConnection()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpotifyServer::Init() {
|
|
|
|
if (!server_->listen(QHostAddress::LocalHost)) {
|
|
|
|
qLog(Error) << "Couldn't open server socket" << server_->errorString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
int SpotifyServer::server_port() const { return server_->serverPort(); }
|
2011-04-25 21:16:26 +02:00
|
|
|
|
|
|
|
void SpotifyServer::NewConnection() {
|
2012-01-08 00:30:33 +01:00
|
|
|
QTcpSocket* socket = server_->nextPendingConnection();
|
|
|
|
SetDevice(socket);
|
2011-04-27 18:38:28 +02:00
|
|
|
|
2012-01-08 00:30:33 +01:00
|
|
|
qLog(Info) << "Connection from port" << socket->peerPort();
|
2011-04-25 21:16:26 +02:00
|
|
|
|
2011-04-26 15:42:58 +02:00
|
|
|
// Send any login messages that were queued before the client connected
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const pb::spotify::Message& message : queued_login_messages_) {
|
2012-01-08 00:30:33 +01:00
|
|
|
SendOrQueueMessage(message);
|
2011-04-26 15:42:58 +02:00
|
|
|
}
|
|
|
|
queued_login_messages_.clear();
|
2012-01-08 00:30:33 +01:00
|
|
|
|
|
|
|
// Don't take any more connections from clients
|
|
|
|
disconnect(server_, SIGNAL(newConnection()), this, 0);
|
2011-04-25 21:16:26 +02:00
|
|
|
}
|
|
|
|
|
2012-01-08 00:30:33 +01:00
|
|
|
void SpotifyServer::SendOrQueueMessage(const pb::spotify::Message& message) {
|
2011-04-26 15:42:58 +02:00
|
|
|
const bool is_login_message = message.has_login_request();
|
2011-04-25 21:16:26 +02:00
|
|
|
|
2012-01-08 00:31:20 +01:00
|
|
|
QList<pb::spotify::Message>* queue =
|
2011-04-26 15:42:58 +02:00
|
|
|
is_login_message ? &queued_login_messages_ : &queued_messages_;
|
2011-04-25 21:16:26 +02:00
|
|
|
|
2012-01-08 00:30:33 +01:00
|
|
|
if (!device_ || (!is_login_message && !logged_in_)) {
|
2011-04-26 15:42:58 +02:00
|
|
|
queue->append(message);
|
|
|
|
} else {
|
2012-01-08 00:30:33 +01:00
|
|
|
SendMessage(message);
|
2011-04-26 15:42:58 +02:00
|
|
|
}
|
2011-04-25 21:16:26 +02:00
|
|
|
}
|
|
|
|
|
2011-11-27 18:29:33 +01:00
|
|
|
void SpotifyServer::Login(const QString& username, const QString& password,
|
2014-02-07 16:34:20 +01:00
|
|
|
pb::spotify::Bitrate bitrate,
|
|
|
|
bool volume_normalisation) {
|
2012-01-08 00:31:20 +01:00
|
|
|
pb::spotify::Message message;
|
2011-04-25 21:16:26 +02:00
|
|
|
|
2012-01-08 00:26:27 +01:00
|
|
|
pb::spotify::LoginRequest* request = message.mutable_login_request();
|
2011-04-26 15:42:58 +02:00
|
|
|
request->set_username(DataCommaSizeFromQString(username));
|
2011-11-24 12:17:19 +01:00
|
|
|
if (!password.isEmpty()) {
|
|
|
|
request->set_password(DataCommaSizeFromQString(password));
|
|
|
|
}
|
2011-11-27 18:29:33 +01:00
|
|
|
request->mutable_playback_settings()->set_bitrate(bitrate);
|
2014-02-07 16:34:20 +01:00
|
|
|
request->mutable_playback_settings()->set_volume_normalisation(
|
|
|
|
volume_normalisation);
|
2011-11-27 18:29:33 +01:00
|
|
|
|
2012-01-08 00:30:33 +01:00
|
|
|
SendOrQueueMessage(message);
|
2011-11-27 18:29:33 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SpotifyServer::SetPlaybackSettings(pb::spotify::Bitrate bitrate,
|
|
|
|
bool volume_normalisation) {
|
2012-01-08 00:31:20 +01:00
|
|
|
pb::spotify::Message message;
|
2011-11-27 18:29:33 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
pb::spotify::PlaybackSettings* request =
|
|
|
|
message.mutable_set_playback_settings_request();
|
2011-11-27 18:29:33 +01:00
|
|
|
request->set_bitrate(bitrate);
|
|
|
|
request->set_volume_normalisation(volume_normalisation);
|
2011-04-25 21:16:26 +02:00
|
|
|
|
2012-01-08 00:30:33 +01:00
|
|
|
SendOrQueueMessage(message);
|
2011-04-25 21:16:26 +02:00
|
|
|
}
|
|
|
|
|
2012-01-08 00:30:33 +01:00
|
|
|
void SpotifyServer::MessageArrived(const pb::spotify::Message& message) {
|
2011-04-25 21:16:26 +02:00
|
|
|
if (message.has_login_response()) {
|
2012-01-08 00:26:27 +01:00
|
|
|
const pb::spotify::LoginResponse& response = message.login_response();
|
2011-04-26 15:42:58 +02:00
|
|
|
logged_in_ = response.success();
|
|
|
|
|
2011-04-30 14:31:20 +02:00
|
|
|
if (response.success()) {
|
2011-04-26 15:42:58 +02:00
|
|
|
// Send any messages that were queued before the client logged in
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const pb::spotify::Message& message : queued_messages_) {
|
2012-01-08 00:30:33 +01:00
|
|
|
SendOrQueueMessage(message);
|
2011-04-26 15:42:58 +02:00
|
|
|
}
|
|
|
|
queued_messages_.clear();
|
2011-04-25 21:16:26 +02:00
|
|
|
}
|
2011-04-26 15:42:58 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
emit LoginCompleted(response.success(),
|
|
|
|
QStringFromStdString(response.error()),
|
2011-08-27 23:01:28 +02:00
|
|
|
response.error_code());
|
2011-04-26 15:42:58 +02:00
|
|
|
} else if (message.has_playlists_updated()) {
|
|
|
|
emit PlaylistsUpdated(message.playlists_updated());
|
2011-04-26 19:06:36 +02:00
|
|
|
} else if (message.has_load_playlist_response()) {
|
2014-02-07 16:34:20 +01:00
|
|
|
const pb::spotify::LoadPlaylistResponse& response =
|
|
|
|
message.load_playlist_response();
|
2011-04-26 19:06:36 +02:00
|
|
|
|
|
|
|
switch (response.request().type()) {
|
2012-01-08 00:26:27 +01:00
|
|
|
case pb::spotify::Inbox:
|
2011-04-26 19:06:36 +02:00
|
|
|
emit InboxLoaded(response);
|
|
|
|
break;
|
|
|
|
|
2012-01-08 00:26:27 +01:00
|
|
|
case pb::spotify::Starred:
|
2011-04-26 19:06:36 +02:00
|
|
|
emit StarredLoaded(response);
|
|
|
|
break;
|
|
|
|
|
2012-01-08 00:26:27 +01:00
|
|
|
case pb::spotify::UserPlaylist:
|
2011-04-26 19:06:36 +02:00
|
|
|
emit UserPlaylistLoaded(response);
|
|
|
|
break;
|
|
|
|
}
|
2011-04-27 18:38:28 +02:00
|
|
|
} else if (message.has_playback_error()) {
|
|
|
|
emit PlaybackError(QStringFromStdString(message.playback_error().error()));
|
|
|
|
} else if (message.has_search_response()) {
|
|
|
|
emit SearchResults(message.search_response());
|
2011-04-29 15:41:42 +02:00
|
|
|
} else if (message.has_image_response()) {
|
2012-01-08 00:26:27 +01:00
|
|
|
const pb::spotify::ImageResponse& response = message.image_response();
|
2011-04-29 15:41:42 +02:00
|
|
|
const QString id = QStringFromStdString(response.id());
|
|
|
|
|
|
|
|
if (response.has_data()) {
|
2014-02-07 16:34:20 +01:00
|
|
|
emit ImageLoaded(
|
|
|
|
id, QImage::fromData(
|
|
|
|
QByteArray(response.data().data(), response.data().size())));
|
2011-04-29 15:41:42 +02:00
|
|
|
} else {
|
|
|
|
emit ImageLoaded(id, QImage());
|
|
|
|
}
|
2011-05-25 16:22:49 +02:00
|
|
|
} else if (message.has_sync_playlist_progress()) {
|
|
|
|
emit SyncPlaylistProgress(message.sync_playlist_progress());
|
2011-08-29 04:26:59 +02:00
|
|
|
} else if (message.has_browse_album_response()) {
|
|
|
|
emit AlbumBrowseResults(message.browse_album_response());
|
2012-06-12 15:53:23 +02:00
|
|
|
} else if (message.has_browse_toplist_response()) {
|
|
|
|
emit ToplistBrowseResults(message.browse_toplist_response());
|
2011-04-26 19:06:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-08 00:26:27 +01:00
|
|
|
void SpotifyServer::LoadPlaylist(pb::spotify::PlaylistType type, int index) {
|
2012-01-08 00:31:20 +01:00
|
|
|
pb::spotify::Message message;
|
2014-02-07 16:34:20 +01:00
|
|
|
pb::spotify::LoadPlaylistRequest* req =
|
|
|
|
message.mutable_load_playlist_request();
|
2011-04-26 19:06:36 +02:00
|
|
|
|
|
|
|
req->set_type(type);
|
|
|
|
if (index != -1) {
|
|
|
|
req->set_user_playlist_index(index);
|
2011-04-25 21:16:26 +02:00
|
|
|
}
|
2011-04-26 19:06:36 +02:00
|
|
|
|
2012-01-08 00:30:33 +01:00
|
|
|
SendOrQueueMessage(message);
|
2011-04-26 19:06:36 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SpotifyServer::SyncPlaylist(pb::spotify::PlaylistType type, int index,
|
|
|
|
bool offline) {
|
2012-01-08 00:31:20 +01:00
|
|
|
pb::spotify::Message message;
|
2014-02-07 16:34:20 +01:00
|
|
|
pb::spotify::SyncPlaylistRequest* req =
|
|
|
|
message.mutable_sync_playlist_request();
|
2011-05-25 16:22:49 +02:00
|
|
|
req->mutable_request()->set_type(type);
|
|
|
|
if (index != -1) {
|
|
|
|
req->mutable_request()->set_user_playlist_index(index);
|
|
|
|
}
|
|
|
|
req->set_offline_sync(offline);
|
|
|
|
|
2012-01-08 00:30:33 +01:00
|
|
|
SendOrQueueMessage(message);
|
2011-05-25 16:22:49 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SpotifyServer::SyncInbox() { SyncPlaylist(pb::spotify::Inbox, -1, true); }
|
2011-05-25 16:22:49 +02:00
|
|
|
|
|
|
|
void SpotifyServer::SyncStarred() {
|
2012-01-08 00:26:27 +01:00
|
|
|
SyncPlaylist(pb::spotify::Starred, -1, true);
|
2011-05-25 16:22:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpotifyServer::SyncUserPlaylist(int index) {
|
|
|
|
Q_ASSERT(index >= 0);
|
2012-01-08 00:26:27 +01:00
|
|
|
SyncPlaylist(pb::spotify::UserPlaylist, index, true);
|
2011-05-25 16:22:49 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SpotifyServer::LoadInbox() { LoadPlaylist(pb::spotify::Inbox); }
|
2011-04-26 19:06:36 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SpotifyServer::LoadStarred() { LoadPlaylist(pb::spotify::Starred); }
|
2011-04-26 19:06:36 +02:00
|
|
|
|
|
|
|
void SpotifyServer::LoadUserPlaylist(int index) {
|
2011-05-25 16:22:49 +02:00
|
|
|
Q_ASSERT(index >= 0);
|
2012-01-08 00:26:27 +01:00
|
|
|
LoadPlaylist(pb::spotify::UserPlaylist, index);
|
2011-04-25 21:16:26 +02:00
|
|
|
}
|
2011-04-26 20:39:38 +02:00
|
|
|
|
2015-01-14 22:49:36 +01:00
|
|
|
void SpotifyServer::AddSongsToStarred(const QList<QUrl>& songs_urls) {
|
|
|
|
AddSongsToPlaylist(pb::spotify::Starred, songs_urls);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpotifyServer::AddSongsToUserPlaylist(int playlist_index,
|
|
|
|
const QList<QUrl>& songs_urls) {
|
|
|
|
AddSongsToPlaylist(pb::spotify::UserPlaylist, songs_urls, playlist_index);
|
|
|
|
}
|
|
|
|
|
2015-01-14 22:55:20 +01:00
|
|
|
void SpotifyServer::AddSongsToPlaylist(
|
|
|
|
const pb::spotify::PlaylistType playlist_type,
|
|
|
|
const QList<QUrl>& songs_urls, int playlist_index) {
|
2014-09-22 22:40:21 +02:00
|
|
|
pb::spotify::Message message;
|
|
|
|
pb::spotify::AddTracksToPlaylistRequest* req =
|
|
|
|
message.mutable_add_tracks_to_playlist();
|
2015-01-14 22:49:36 +01:00
|
|
|
req->set_playlist_type(playlist_type);
|
2014-09-22 22:40:21 +02:00
|
|
|
req->set_playlist_index(playlist_index);
|
|
|
|
for (const QUrl& song_url : songs_urls) {
|
|
|
|
req->add_track_uri(DataCommaSizeFromQString(song_url.toString()));
|
|
|
|
}
|
|
|
|
SendOrQueueMessage(message);
|
|
|
|
}
|
|
|
|
|
2015-01-14 22:55:20 +01:00
|
|
|
void SpotifyServer::RemoveSongsFromStarred(
|
|
|
|
const QList<int>& songs_indices_to_remove) {
|
2015-01-14 22:49:36 +01:00
|
|
|
RemoveSongsFromPlaylist(pb::spotify::Starred, songs_indices_to_remove);
|
|
|
|
}
|
|
|
|
|
2015-01-14 22:55:20 +01:00
|
|
|
void SpotifyServer::RemoveSongsFromUserPlaylist(
|
|
|
|
int playlist_index, const QList<int>& songs_indices_to_remove) {
|
|
|
|
RemoveSongsFromPlaylist(pb::spotify::UserPlaylist, songs_indices_to_remove,
|
|
|
|
playlist_index);
|
2015-01-14 22:49:36 +01:00
|
|
|
}
|
|
|
|
|
2014-09-28 02:00:32 +02:00
|
|
|
void SpotifyServer::RemoveSongsFromPlaylist(
|
2015-01-14 22:49:36 +01:00
|
|
|
const pb::spotify::PlaylistType playlist_type,
|
|
|
|
const QList<int>& songs_indices_to_remove, int playlist_index) {
|
2014-09-28 02:00:32 +02:00
|
|
|
pb::spotify::Message message;
|
|
|
|
pb::spotify::RemoveTracksFromPlaylistRequest* req =
|
|
|
|
message.mutable_remove_tracks_from_playlist();
|
2015-01-14 22:49:36 +01:00
|
|
|
req->set_playlist_type(playlist_type);
|
|
|
|
if (playlist_type == pb::spotify::UserPlaylist) {
|
|
|
|
req->set_playlist_index(playlist_index);
|
|
|
|
}
|
2014-09-28 02:00:32 +02:00
|
|
|
for (int song_index : songs_indices_to_remove) {
|
|
|
|
req->add_track_index(song_index);
|
|
|
|
}
|
|
|
|
SendOrQueueMessage(message);
|
|
|
|
}
|
|
|
|
|
2011-04-26 20:39:38 +02:00
|
|
|
void SpotifyServer::StartPlayback(const QString& uri, quint16 port) {
|
2012-01-08 00:31:20 +01:00
|
|
|
pb::spotify::Message message;
|
2012-01-08 00:26:27 +01:00
|
|
|
pb::spotify::PlaybackRequest* req = message.mutable_playback_request();
|
2011-04-26 20:39:38 +02:00
|
|
|
|
|
|
|
req->set_track_uri(DataCommaSizeFromQString(uri));
|
|
|
|
req->set_media_port(port);
|
2012-01-08 00:30:33 +01:00
|
|
|
SendOrQueueMessage(message);
|
2011-04-26 20:39:38 +02:00
|
|
|
}
|
2011-04-27 18:38:28 +02:00
|
|
|
|
2014-09-21 16:13:00 +02:00
|
|
|
void SpotifyServer::Seek(qint64 offset_nsec) {
|
2012-01-08 00:31:20 +01:00
|
|
|
pb::spotify::Message message;
|
2012-01-08 00:26:27 +01:00
|
|
|
pb::spotify::SeekRequest* req = message.mutable_seek_request();
|
2011-09-25 20:24:44 +02:00
|
|
|
|
2014-09-21 16:13:00 +02:00
|
|
|
req->set_offset_nsec(offset_nsec);
|
2012-01-08 00:30:33 +01:00
|
|
|
SendOrQueueMessage(message);
|
2011-09-25 20:24:44 +02:00
|
|
|
}
|
|
|
|
|
2011-08-29 03:00:59 +02:00
|
|
|
void SpotifyServer::Search(const QString& text, int limit, int limit_album) {
|
2012-01-08 00:31:20 +01:00
|
|
|
pb::spotify::Message message;
|
2012-01-08 00:26:27 +01:00
|
|
|
pb::spotify::SearchRequest* req = message.mutable_search_request();
|
2011-04-27 18:38:28 +02:00
|
|
|
|
|
|
|
req->set_query(DataCommaSizeFromQString(text));
|
|
|
|
req->set_limit(limit);
|
2011-08-29 03:00:59 +02:00
|
|
|
req->set_limit_album(limit_album);
|
2012-01-08 00:30:33 +01:00
|
|
|
SendOrQueueMessage(message);
|
2011-04-27 18:38:28 +02:00
|
|
|
}
|
2011-04-29 15:41:42 +02:00
|
|
|
|
|
|
|
void SpotifyServer::LoadImage(const QString& id) {
|
2012-01-08 00:31:20 +01:00
|
|
|
pb::spotify::Message message;
|
2012-01-08 00:26:27 +01:00
|
|
|
pb::spotify::ImageRequest* req = message.mutable_image_request();
|
2011-04-29 15:41:42 +02:00
|
|
|
|
|
|
|
req->set_id(DataCommaSizeFromQString(id));
|
2012-01-08 00:30:33 +01:00
|
|
|
SendOrQueueMessage(message);
|
2011-04-29 15:41:42 +02:00
|
|
|
}
|
2011-08-29 04:26:59 +02:00
|
|
|
|
|
|
|
void SpotifyServer::AlbumBrowse(const QString& uri) {
|
2012-01-08 00:31:20 +01:00
|
|
|
pb::spotify::Message message;
|
2012-01-08 00:26:27 +01:00
|
|
|
pb::spotify::BrowseAlbumRequest* req = message.mutable_browse_album_request();
|
2011-08-29 04:26:59 +02:00
|
|
|
|
|
|
|
req->set_uri(DataCommaSizeFromQString(uri));
|
2012-01-08 00:30:33 +01:00
|
|
|
SendOrQueueMessage(message);
|
2011-08-29 04:26:59 +02:00
|
|
|
}
|
2012-06-12 15:53:23 +02:00
|
|
|
|
|
|
|
void SpotifyServer::LoadToplist() {
|
|
|
|
pb::spotify::Message message;
|
2014-02-07 16:34:20 +01:00
|
|
|
pb::spotify::BrowseToplistRequest* req =
|
|
|
|
message.mutable_browse_toplist_request();
|
2012-06-12 15:53:23 +02:00
|
|
|
req->set_type(pb::spotify::BrowseToplistRequest::Tracks);
|
|
|
|
req->set_region(pb::spotify::BrowseToplistRequest::Everywhere);
|
|
|
|
|
|
|
|
SendOrQueueMessage(message);
|
|
|
|
}
|
2014-09-06 19:21:23 +02:00
|
|
|
|
|
|
|
void SpotifyServer::SetPaused(const bool paused) {
|
|
|
|
pb::spotify::Message message;
|
|
|
|
pb::spotify::PauseRequest* req = message.mutable_pause_request();
|
|
|
|
req->set_paused(paused);
|
|
|
|
SendOrQueueMessage(message);
|
|
|
|
}
|