2011-04-25 21:16:26 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2010, David Sansome <me@davidsansome.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 "spotifyserver.h"
|
2011-11-28 19:11:09 +01:00
|
|
|
#include "core/closure.h"
|
2011-04-25 21:16:26 +02:00
|
|
|
#include "core/logging.h"
|
|
|
|
|
2012-01-06 22:27:02 +01:00
|
|
|
#include "spotifymessages.pb.h"
|
2011-04-25 21:16:26 +02:00
|
|
|
|
|
|
|
#include <QTcpServer>
|
|
|
|
#include <QTcpSocket>
|
2011-11-28 19:11:09 +01:00
|
|
|
#include <QTimer>
|
2011-04-25 21:16:26 +02:00
|
|
|
|
|
|
|
SpotifyServer::SpotifyServer(QObject* parent)
|
2012-01-08 00:30:33 +01:00
|
|
|
: AbstractMessageHandler<pb::spotify::Message>(NULL, parent),
|
2011-04-25 21:16:26 +02:00
|
|
|
server_(new QTcpServer(this)),
|
2011-04-26 15:42:58 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int SpotifyServer::server_port() const {
|
|
|
|
return server_->serverPort();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2012-01-08 00:31:20 +01:00
|
|
|
foreach (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,
|
2012-01-08 00:26:27 +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);
|
|
|
|
request->mutable_playback_settings()->set_volume_normalisation(volume_normalisation);
|
|
|
|
|
2012-01-08 00:30:33 +01:00
|
|
|
SendOrQueueMessage(message);
|
2011-11-27 18:29:33 +01:00
|
|
|
}
|
|
|
|
|
2012-01-08 00:26:27 +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
|
|
|
|
2012-01-08 00:26:27 +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
|
2012-01-08 00:31:20 +01:00
|
|
|
foreach (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
|
|
|
|
2011-08-27 23:01:28 +02:00
|
|
|
emit LoginCompleted(response.success(), QStringFromStdString(response.error()),
|
|
|
|
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()) {
|
2012-01-08 00:26:27 +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()) {
|
|
|
|
emit ImageLoaded(id, QImage::fromData(QByteArray(
|
|
|
|
response.data().data(), response.data().size())));
|
|
|
|
} 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;
|
2012-01-08 00:26:27 +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
|
|
|
}
|
|
|
|
|
2011-05-25 16:22:49 +02:00
|
|
|
void SpotifyServer::SyncPlaylist(
|
2012-01-08 00:26:27 +01:00
|
|
|
pb::spotify::PlaylistType type, int index, bool offline) {
|
2012-01-08 00:31:20 +01:00
|
|
|
pb::spotify::Message message;
|
2012-01-08 00:26:27 +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
|
|
|
}
|
|
|
|
|
|
|
|
void SpotifyServer::SyncInbox() {
|
2012-01-08 00:26:27 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-04-26 19:06:36 +02:00
|
|
|
void SpotifyServer::LoadInbox() {
|
2012-01-08 00:26:27 +01:00
|
|
|
LoadPlaylist(pb::spotify::Inbox);
|
2011-04-26 19:06:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpotifyServer::LoadStarred() {
|
2012-01-08 00:26:27 +01:00
|
|
|
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
|
|
|
|
2011-11-28 19:11:09 +01:00
|
|
|
void SpotifyServer::StartPlaybackLater(const QString& uri, quint16 port) {
|
|
|
|
QTimer* timer = new QTimer(this);
|
|
|
|
connect(timer, SIGNAL(timeout()), timer, SLOT(deleteLater()));
|
|
|
|
|
|
|
|
timer->start(100); // lol
|
|
|
|
NewClosure(timer, SIGNAL(timeout()),
|
|
|
|
this, SLOT(StartPlayback(QString,quint16)),
|
|
|
|
uri, port);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2011-09-25 20:24:44 +02:00
|
|
|
void SpotifyServer::Seek(qint64 offset_bytes) {
|
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
|
|
|
|
|
|
|
req->set_offset_bytes(offset_bytes);
|
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;
|
|
|
|
pb::spotify::BrowseToplistRequest* req = message.mutable_browse_toplist_request();
|
|
|
|
req->set_type(pb::spotify::BrowseToplistRequest::Tracks);
|
|
|
|
req->set_region(pb::spotify::BrowseToplistRequest::Everywhere);
|
|
|
|
|
|
|
|
SendOrQueueMessage(message);
|
|
|
|
}
|