1
/* This file is part of Clementine.
2
Copyright 2010, David Sansome <me@davidsansome.com>
4
Clementine is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
9
Clementine is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
18
#include "spotifyserver.h"
19
#include "core/closure.h"
20
#include "core/logging.h"
22
#include "spotifyblob/common/spotifymessages.pb.h"
23
#include "spotifyblob/common/spotifymessagehandler.h"
29
SpotifyServer::SpotifyServer(QObject* parent)
31
server_(new QTcpServer(this)),
32
protocol_socket_(NULL),
36
connect(server_, SIGNAL(newConnection()), SLOT(NewConnection()));
39
void SpotifyServer::Init() {
40
if (!server_->listen(QHostAddress::LocalHost)) {
41
qLog(Error) << "Couldn't open server socket" << server_->errorString();
45
int SpotifyServer::server_port() const {
46
return server_->serverPort();
49
void SpotifyServer::NewConnection() {
50
delete protocol_socket_;
53
protocol_socket_ = server_->nextPendingConnection();
54
handler_ = new SpotifyMessageHandler(protocol_socket_, this);
55
connect(handler_, SIGNAL(MessageArrived(spotify_pb::SpotifyMessage)),
56
SLOT(HandleMessage(spotify_pb::SpotifyMessage)));
58
qLog(Info) << "Connection from port" << protocol_socket_->peerPort();
60
// Send any login messages that were queued before the client connected
61
foreach (const spotify_pb::SpotifyMessage& message, queued_login_messages_) {
64
queued_login_messages_.clear();
67
void SpotifyServer::SendMessage(const spotify_pb::SpotifyMessage& message) {
68
const bool is_login_message = message.has_login_request();
70
QList<spotify_pb::SpotifyMessage>* queue =
71
is_login_message ? &queued_login_messages_ : &queued_messages_;
73
if (!protocol_socket_ || (!is_login_message && !logged_in_)) {
74
queue->append(message);
76
handler_->SendMessage(message);
80
void SpotifyServer::Login(const QString& username, const QString& password,
81
spotify_pb::Bitrate bitrate, bool volume_normalisation) {
82
spotify_pb::SpotifyMessage message;
84
spotify_pb::LoginRequest* request = message.mutable_login_request();
85
request->set_username(DataCommaSizeFromQString(username));
86
if (!password.isEmpty()) {
87
request->set_password(DataCommaSizeFromQString(password));
89
request->mutable_playback_settings()->set_bitrate(bitrate);
90
request->mutable_playback_settings()->set_volume_normalisation(volume_normalisation);
95
void SpotifyServer::SetPlaybackSettings(spotify_pb::Bitrate bitrate, bool volume_normalisation) {
96
spotify_pb::SpotifyMessage message;
98
spotify_pb::PlaybackSettings* request = message.mutable_set_playback_settings_request();
99
request->set_bitrate(bitrate);
100
request->set_volume_normalisation(volume_normalisation);
102
SendMessage(message);
105
void SpotifyServer::HandleMessage(const spotify_pb::SpotifyMessage& message) {
106
if (message.has_login_response()) {
107
const spotify_pb::LoginResponse& response = message.login_response();
108
logged_in_ = response.success();
110
if (response.success()) {
111
// Send any messages that were queued before the client logged in
112
foreach (const spotify_pb::SpotifyMessage& message, queued_messages_) {
113
SendMessage(message);
115
queued_messages_.clear();
118
emit LoginCompleted(response.success(), QStringFromStdString(response.error()),
119
response.error_code());
120
} else if (message.has_playlists_updated()) {
121
emit PlaylistsUpdated(message.playlists_updated());
122
} else if (message.has_load_playlist_response()) {
123
const spotify_pb::LoadPlaylistResponse& response = message.load_playlist_response();
125
switch (response.request().type()) {
126
case spotify_pb::Inbox:
127
emit InboxLoaded(response);
130
case spotify_pb::Starred:
131
emit StarredLoaded(response);
134
case spotify_pb::UserPlaylist:
135
emit UserPlaylistLoaded(response);
138
} else if (message.has_playback_error()) {
139
emit PlaybackError(QStringFromStdString(message.playback_error().error()));
140
} else if (message.has_search_response()) {
141
emit SearchResults(message.search_response());
142
} else if (message.has_image_response()) {
143
const spotify_pb::ImageResponse& response = message.image_response();
144
const QString id = QStringFromStdString(response.id());
146
if (response.has_data()) {
147
emit ImageLoaded(id, QImage::fromData(QByteArray(
148
response.data().data(), response.data().size())));
150
emit ImageLoaded(id, QImage());
152
} else if (message.has_sync_playlist_progress()) {
153
emit SyncPlaylistProgress(message.sync_playlist_progress());
154
} else if (message.has_browse_album_response()) {
155
emit AlbumBrowseResults(message.browse_album_response());
159
void SpotifyServer::LoadPlaylist(spotify_pb::PlaylistType type, int index) {
160
spotify_pb::SpotifyMessage message;
161
spotify_pb::LoadPlaylistRequest* req = message.mutable_load_playlist_request();
165
req->set_user_playlist_index(index);
168
SendMessage(message);
171
void SpotifyServer::SyncPlaylist(
172
spotify_pb::PlaylistType type, int index, bool offline) {
173
spotify_pb::SpotifyMessage message;
174
spotify_pb::SyncPlaylistRequest* req = message.mutable_sync_playlist_request();
175
req->mutable_request()->set_type(type);
177
req->mutable_request()->set_user_playlist_index(index);
179
req->set_offline_sync(offline);
181
SendMessage(message);
184
void SpotifyServer::SyncInbox() {
185
SyncPlaylist(spotify_pb::Inbox, -1, true);
188
void SpotifyServer::SyncStarred() {
189
SyncPlaylist(spotify_pb::Starred, -1, true);
192
void SpotifyServer::SyncUserPlaylist(int index) {
193
Q_ASSERT(index >= 0);
194
SyncPlaylist(spotify_pb::UserPlaylist, index, true);
197
void SpotifyServer::LoadInbox() {
198
LoadPlaylist(spotify_pb::Inbox);
201
void SpotifyServer::LoadStarred() {
202
LoadPlaylist(spotify_pb::Starred);
205
void SpotifyServer::LoadUserPlaylist(int index) {
206
Q_ASSERT(index >= 0);
207
LoadPlaylist(spotify_pb::UserPlaylist, index);
210
void SpotifyServer::StartPlaybackLater(const QString& uri, quint16 port) {
211
QTimer* timer = new QTimer(this);
212
connect(timer, SIGNAL(timeout()), timer, SLOT(deleteLater()));
214
timer->start(100); // lol
215
NewClosure(timer, SIGNAL(timeout()),
216
this, SLOT(StartPlayback(QString,quint16)),
220
void SpotifyServer::StartPlayback(const QString& uri, quint16 port) {
221
spotify_pb::SpotifyMessage message;
222
spotify_pb::PlaybackRequest* req = message.mutable_playback_request();
224
req->set_track_uri(DataCommaSizeFromQString(uri));
225
req->set_media_port(port);
226
SendMessage(message);
229
void SpotifyServer::Seek(qint64 offset_bytes) {
230
spotify_pb::SpotifyMessage message;
231
spotify_pb::SeekRequest* req = message.mutable_seek_request();
233
req->set_offset_bytes(offset_bytes);
234
SendMessage(message);
237
void SpotifyServer::Search(const QString& text, int limit, int limit_album) {
238
spotify_pb::SpotifyMessage message;
239
spotify_pb::SearchRequest* req = message.mutable_search_request();
241
req->set_query(DataCommaSizeFromQString(text));
242
req->set_limit(limit);
243
req->set_limit_album(limit_album);
244
SendMessage(message);
247
void SpotifyServer::LoadImage(const QString& id) {
248
spotify_pb::SpotifyMessage message;
249
spotify_pb::ImageRequest* req = message.mutable_image_request();
251
req->set_id(DataCommaSizeFromQString(id));
252
SendMessage(message);
255
void SpotifyServer::AlbumBrowse(const QString& uri) {
256
spotify_pb::SpotifyMessage message;
257
spotify_pb::BrowseAlbumRequest* req = message.mutable_browse_album_request();
259
req->set_uri(DataCommaSizeFromQString(uri));
260
SendMessage(message);