2011-04-25 21:16:26 +02:00
|
|
|
/* This file is part of Clementine.
|
2011-04-26 15:43:13 +02:00
|
|
|
Copyright 2011, David Sansome <me@davidsansome.com>
|
2011-04-25 21:16:26 +02:00
|
|
|
|
2011-04-26 15:43:13 +02:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
2011-04-25 21:16:26 +02:00
|
|
|
|
2011-04-26 15:43:13 +02:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-25 21:16:26 +02:00
|
|
|
|
2011-04-26 15:43:13 +02:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
2011-04-25 21:16:26 +02:00
|
|
|
*/
|
|
|
|
|
2011-04-26 15:43:13 +02:00
|
|
|
// Note: this file is licensed under the Apache License instead of GPL because
|
|
|
|
// it is used by the Spotify blob which links against libspotify and is not GPL
|
|
|
|
// compatible.
|
|
|
|
|
2011-04-25 21:16:26 +02:00
|
|
|
#include "spotifyclient.h"
|
2014-04-29 14:11:52 +02:00
|
|
|
|
2011-05-25 16:29:10 +02:00
|
|
|
#include <QCoreApplication>
|
2011-04-26 15:42:58 +02:00
|
|
|
#include <QDir>
|
2011-04-25 21:16:26 +02:00
|
|
|
#include <QHostAddress>
|
|
|
|
#include <QTcpSocket>
|
2011-04-26 15:42:58 +02:00
|
|
|
#include <QTimer>
|
2020-09-18 16:15:19 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <memory>
|
2011-04-25 21:16:26 +02:00
|
|
|
|
2014-04-29 14:11:52 +02:00
|
|
|
#include "core/arraysize.h"
|
|
|
|
#include "core/logging.h"
|
2014-09-21 16:13:00 +02:00
|
|
|
#include "core/timeconstants.h"
|
2014-04-29 14:11:52 +02:00
|
|
|
#include "mediapipeline.h"
|
2020-09-18 16:15:19 +02:00
|
|
|
#include "spotify_utilities.h"
|
2014-04-29 14:11:52 +02:00
|
|
|
#include "spotifykey.h"
|
|
|
|
#include "spotifymessages.pb.h"
|
|
|
|
|
2011-04-29 15:41:42 +02:00
|
|
|
const int SpotifyClient::kSpotifyImageIDSize = 20;
|
2011-09-25 20:24:44 +02:00
|
|
|
const int SpotifyClient::kWaveHeaderSize = 44;
|
2011-04-29 15:41:42 +02:00
|
|
|
|
2011-04-25 21:16:26 +02:00
|
|
|
SpotifyClient::SpotifyClient(QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: AbstractMessageHandler<pb::spotify::Message>(nullptr, parent),
|
|
|
|
api_key_(QByteArray::fromBase64(kSpotifyApiKey)),
|
|
|
|
protocol_socket_(new QTcpSocket(this)),
|
|
|
|
session_(nullptr),
|
|
|
|
events_timer_(new QTimer(this)) {
|
2012-01-08 00:30:33 +01:00
|
|
|
SetDevice(protocol_socket_);
|
|
|
|
|
2011-04-26 15:42:58 +02:00
|
|
|
memset(&spotify_callbacks_, 0, sizeof(spotify_callbacks_));
|
|
|
|
memset(&spotify_config_, 0, sizeof(spotify_config_));
|
2014-02-07 16:34:20 +01:00
|
|
|
memset(&playlistcontainer_callbacks_, 0,
|
|
|
|
sizeof(playlistcontainer_callbacks_));
|
2011-04-27 18:38:28 +02:00
|
|
|
memset(&get_playlists_callbacks_, 0, sizeof(get_playlists_callbacks_));
|
2011-04-26 19:06:36 +02:00
|
|
|
memset(&load_playlist_callbacks_, 0, sizeof(load_playlist_callbacks_));
|
2011-04-26 15:42:58 +02:00
|
|
|
|
|
|
|
spotify_callbacks_.logged_in = &LoggedInCallback;
|
|
|
|
spotify_callbacks_.notify_main_thread = &NotifyMainThreadCallback;
|
|
|
|
spotify_callbacks_.log_message = &LogMessageCallback;
|
2011-04-26 19:06:36 +02:00
|
|
|
spotify_callbacks_.metadata_updated = &MetadataUpdatedCallback;
|
2011-04-26 19:06:46 +02:00
|
|
|
spotify_callbacks_.music_delivery = &MusicDeliveryCallback;
|
|
|
|
spotify_callbacks_.end_of_track = &EndOfTrackCallback;
|
|
|
|
spotify_callbacks_.streaming_error = &StreamingErrorCallback;
|
2011-05-25 16:22:49 +02:00
|
|
|
spotify_callbacks_.offline_status_updated = &OfflineStatusUpdatedCallback;
|
2011-11-24 13:40:05 +01:00
|
|
|
spotify_callbacks_.connection_error = &ConnectionErrorCallback;
|
|
|
|
spotify_callbacks_.message_to_user = &UserMessageCallback;
|
2011-11-24 14:19:10 +01:00
|
|
|
spotify_callbacks_.start_playback = &StartPlaybackCallback;
|
|
|
|
spotify_callbacks_.stop_playback = &StopPlaybackCallback;
|
2011-11-24 13:40:05 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
playlistcontainer_callbacks_.container_loaded =
|
|
|
|
&PlaylistContainerLoadedCallback;
|
2011-04-26 15:42:58 +02:00
|
|
|
playlistcontainer_callbacks_.playlist_added = &PlaylistAddedCallback;
|
|
|
|
playlistcontainer_callbacks_.playlist_moved = &PlaylistMovedCallback;
|
|
|
|
playlistcontainer_callbacks_.playlist_removed = &PlaylistRemovedCallback;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
get_playlists_callbacks_.playlist_state_changed =
|
|
|
|
&PlaylistStateChangedForGetPlaylists;
|
2011-04-27 18:38:28 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
load_playlist_callbacks_.playlist_state_changed =
|
|
|
|
&PlaylistStateChangedForLoadPlaylist;
|
2011-04-26 19:06:36 +02:00
|
|
|
|
2011-07-07 16:54:22 +02:00
|
|
|
QString cache = utilities::GetCacheDirectory();
|
|
|
|
qLog(Debug) << "Using:" << cache << "for Spotify cache";
|
2011-11-24 13:40:05 +01:00
|
|
|
QString settings_dir = utilities::GetSettingsDirectory();
|
|
|
|
qLog(Debug) << "Using:" << settings_dir << "for Spotify settings";
|
2011-07-07 16:54:22 +02:00
|
|
|
|
2011-04-26 15:42:58 +02:00
|
|
|
spotify_config_.api_version = SPOTIFY_API_VERSION; // From libspotify/api.h
|
2011-11-24 13:40:05 +01:00
|
|
|
spotify_config_.cache_location = strdup(cache.toUtf8().constData());
|
|
|
|
spotify_config_.settings_location = strdup(settings_dir.toUtf8().constData());
|
2011-04-26 15:43:13 +02:00
|
|
|
spotify_config_.application_key = api_key_.constData();
|
|
|
|
spotify_config_.application_key_size = api_key_.size();
|
2011-04-26 15:42:58 +02:00
|
|
|
spotify_config_.callbacks = &spotify_callbacks_;
|
|
|
|
spotify_config_.userdata = this;
|
|
|
|
spotify_config_.user_agent = "Clementine Player";
|
|
|
|
|
|
|
|
events_timer_->setSingleShot(true);
|
|
|
|
connect(events_timer_, SIGNAL(timeout()), SLOT(ProcessEvents()));
|
|
|
|
|
2011-05-25 16:29:10 +02:00
|
|
|
connect(protocol_socket_, SIGNAL(disconnected()),
|
|
|
|
QCoreApplication::instance(), SLOT(quit()));
|
2011-04-25 21:16:26 +02:00
|
|
|
}
|
|
|
|
|
2011-04-26 15:42:58 +02:00
|
|
|
SpotifyClient::~SpotifyClient() {
|
|
|
|
if (session_) {
|
|
|
|
sp_session_release(session_);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(const_cast<char*>(spotify_config_.cache_location));
|
|
|
|
free(const_cast<char*>(spotify_config_.settings_location));
|
|
|
|
}
|
|
|
|
|
2011-04-25 21:16:26 +02:00
|
|
|
void SpotifyClient::Init(quint16 port) {
|
2011-04-26 19:06:36 +02:00
|
|
|
qLog(Debug) << "Connecting to port" << port;
|
2011-04-25 21:16:26 +02:00
|
|
|
|
2011-04-26 19:06:46 +02:00
|
|
|
protocol_socket_->connectToHost(QHostAddress::LocalHost, port);
|
2011-04-25 21:16:26 +02:00
|
|
|
}
|
|
|
|
|
2011-04-26 15:42:58 +02:00
|
|
|
void SpotifyClient::LoggedInCallback(sp_session* session, sp_error error) {
|
2014-02-07 16:34:20 +01:00
|
|
|
SpotifyClient* me =
|
|
|
|
reinterpret_cast<SpotifyClient*>(sp_session_userdata(session));
|
2011-04-26 15:42:58 +02:00
|
|
|
const bool success = error == SP_ERROR_OK;
|
2014-02-07 16:34:20 +01:00
|
|
|
pb::spotify::LoginResponse_Error error_code =
|
|
|
|
pb::spotify::LoginResponse_Error_Other;
|
2011-04-26 15:42:58 +02:00
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
qLog(Warning) << "Failed to login" << sp_error_message(error);
|
|
|
|
}
|
|
|
|
|
2011-08-27 23:01:28 +02:00
|
|
|
switch (error) {
|
2014-02-07 16:34:20 +01:00
|
|
|
case SP_ERROR_BAD_USERNAME_OR_PASSWORD:
|
|
|
|
error_code = pb::spotify::LoginResponse_Error_BadUsernameOrPassword;
|
|
|
|
break;
|
|
|
|
case SP_ERROR_USER_BANNED:
|
|
|
|
error_code = pb::spotify::LoginResponse_Error_UserBanned;
|
|
|
|
break;
|
|
|
|
case SP_ERROR_USER_NEEDS_PREMIUM:
|
|
|
|
error_code = pb::spotify::LoginResponse_Error_UserNeedsPremium;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error_code = pb::spotify::LoginResponse_Error_Other;
|
|
|
|
break;
|
2011-08-27 23:01:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
me->SendLoginCompleted(success, sp_error_message(error), error_code);
|
2011-04-26 15:42:58 +02:00
|
|
|
|
|
|
|
if (success) {
|
2014-02-07 16:34:20 +01:00
|
|
|
sp_playlistcontainer_add_callbacks(sp_session_playlistcontainer(session),
|
|
|
|
&me->playlistcontainer_callbacks_, me);
|
2012-03-27 23:01:37 +02:00
|
|
|
sp_session_flush_caches(me->session_);
|
2011-04-26 15:42:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpotifyClient::NotifyMainThreadCallback(sp_session* session) {
|
2014-02-07 16:34:20 +01:00
|
|
|
SpotifyClient* me =
|
|
|
|
reinterpret_cast<SpotifyClient*>(sp_session_userdata(session));
|
2011-04-26 15:42:58 +02:00
|
|
|
QMetaObject::invokeMethod(me, "ProcessEvents", Qt::QueuedConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpotifyClient::ProcessEvents() {
|
|
|
|
int next_timeout_ms;
|
|
|
|
sp_session_process_events(session_, &next_timeout_ms);
|
|
|
|
events_timer_->start(next_timeout_ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpotifyClient::LogMessageCallback(sp_session* session, const char* data) {
|
2011-04-26 19:06:36 +02:00
|
|
|
qLog(Debug) << "libspotify:" << QString::fromUtf8(data).trimmed();
|
2011-04-26 15:42:58 +02:00
|
|
|
}
|
2011-04-25 21:16:26 +02:00
|
|
|
|
2012-01-08 00:26:27 +01:00
|
|
|
void SpotifyClient::Search(const pb::spotify::SearchRequest& req) {
|
2014-02-07 16:34:20 +01:00
|
|
|
sp_search* search =
|
|
|
|
sp_search_create(session_, req.query().c_str(), 0, req.limit(), 0,
|
|
|
|
req.limit_album(), 0, 0, // artists
|
|
|
|
0, 0, // playlists
|
|
|
|
SP_SEARCH_STANDARD, &SearchCompleteCallback, this);
|
2011-04-27 18:38:28 +02:00
|
|
|
|
|
|
|
pending_searches_[search] = req;
|
2011-04-26 15:42:58 +02:00
|
|
|
}
|
2011-04-25 21:16:26 +02:00
|
|
|
|
2011-04-26 15:42:58 +02:00
|
|
|
void SpotifyClient::SearchCompleteCallback(sp_search* result, void* userdata) {
|
2011-04-27 18:38:28 +02:00
|
|
|
SpotifyClient* me = reinterpret_cast<SpotifyClient*>(userdata);
|
|
|
|
|
|
|
|
if (!me->pending_searches_.contains(result)) {
|
|
|
|
qLog(Warning) << "SearchComplete called with unknown search";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-17 16:57:34 +02:00
|
|
|
// If there were any album results then we need to resolve those before
|
|
|
|
// we can send our response.
|
|
|
|
const int count = sp_search_num_albums(result);
|
|
|
|
if (count != 0) {
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < count; ++i) {
|
2011-09-17 16:57:34 +02:00
|
|
|
sp_album* album = sp_search_album(result, i);
|
2014-02-07 16:34:20 +01:00
|
|
|
sp_albumbrowse* browse = sp_albumbrowse_create(
|
|
|
|
me->session_, album, &SearchAlbumBrowseComplete, me);
|
2011-09-17 16:57:34 +02:00
|
|
|
|
|
|
|
me->pending_search_album_browse_responses_[browse] = result;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
me->SendSearchResponse(result);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SpotifyClient::SearchAlbumBrowseComplete(sp_albumbrowse* result,
|
|
|
|
void* userdata) {
|
2011-09-17 16:57:34 +02:00
|
|
|
SpotifyClient* me = reinterpret_cast<SpotifyClient*>(userdata);
|
|
|
|
|
|
|
|
if (!me->pending_search_album_browse_responses_.contains(result)) {
|
|
|
|
qLog(Warning) << "SearchAlbumBrowseComplete called with unknown result";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sp_search* search = me->pending_search_album_browse_responses_.take(result);
|
|
|
|
me->pending_search_album_browses_[search].append(result);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (me->pending_search_album_browses_[search].count() >=
|
|
|
|
sp_search_num_albums(search)) {
|
2011-09-17 16:57:34 +02:00
|
|
|
me->SendSearchResponse(search);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpotifyClient::SendSearchResponse(sp_search* result) {
|
2011-04-27 18:38:28 +02:00
|
|
|
// Take the request out of the queue
|
2012-01-08 00:26:27 +01:00
|
|
|
pb::spotify::SearchRequest req = pending_searches_.take(result);
|
2011-04-27 18:38:28 +02:00
|
|
|
|
|
|
|
// Prepare the response
|
2012-01-08 00:31:20 +01:00
|
|
|
pb::spotify::Message message;
|
2012-01-08 00:26:27 +01:00
|
|
|
pb::spotify::SearchResponse* response = message.mutable_search_response();
|
2011-04-27 18:38:28 +02:00
|
|
|
|
|
|
|
*response->mutable_request() = req;
|
|
|
|
|
|
|
|
// Check for errors
|
2011-04-26 15:42:58 +02:00
|
|
|
sp_error error = sp_search_error(result);
|
|
|
|
if (error != SP_ERROR_OK) {
|
2011-04-27 18:38:28 +02:00
|
|
|
response->set_error(sp_error_message(error));
|
|
|
|
|
2012-01-08 00:30:33 +01:00
|
|
|
SendMessage(message);
|
2011-04-26 15:42:58 +02:00
|
|
|
sp_search_release(result);
|
2011-04-25 21:16:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-27 18:38:28 +02:00
|
|
|
// Get the list of tracks from the search
|
2011-08-29 03:00:59 +02:00
|
|
|
int count = sp_search_num_tracks(result);
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < count; ++i) {
|
2011-04-27 18:38:28 +02:00
|
|
|
sp_track* track = sp_search_track(result, i);
|
2011-09-17 16:57:34 +02:00
|
|
|
ConvertTrack(track, response->add_result());
|
2011-04-26 15:42:58 +02:00
|
|
|
}
|
|
|
|
|
2011-09-17 16:57:34 +02:00
|
|
|
// Get the albums from the search. All these should be resolved by now.
|
|
|
|
QList<sp_albumbrowse*> browses = pending_search_album_browses_.take(result);
|
2014-02-10 14:29:07 +01:00
|
|
|
for (sp_albumbrowse* browse : browses) {
|
2011-09-17 16:57:34 +02:00
|
|
|
sp_album* album = sp_albumbrowse_album(browse);
|
2012-01-08 00:26:27 +01:00
|
|
|
pb::spotify::Album* msg = response->add_album();
|
2011-09-18 01:06:07 +02:00
|
|
|
|
|
|
|
ConvertAlbum(album, msg->mutable_metadata());
|
|
|
|
ConvertAlbumBrowse(browse, msg->mutable_metadata());
|
|
|
|
|
|
|
|
// Add all tracks
|
|
|
|
const int tracks = sp_albumbrowse_num_tracks(browse);
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < tracks; ++i) {
|
2011-09-18 01:06:07 +02:00
|
|
|
ConvertTrack(sp_albumbrowse_track(browse, i), msg->add_track());
|
|
|
|
}
|
|
|
|
|
2011-09-17 16:57:34 +02:00
|
|
|
sp_albumbrowse_release(browse);
|
2011-08-29 03:00:59 +02:00
|
|
|
}
|
|
|
|
|
2011-04-27 18:38:28 +02:00
|
|
|
// Add other data to the response
|
|
|
|
response->set_total_tracks(sp_search_total_tracks(result));
|
|
|
|
response->set_did_you_mean(sp_search_did_you_mean(result));
|
|
|
|
|
2012-01-08 00:30:33 +01:00
|
|
|
SendMessage(message);
|
2011-04-26 15:42:58 +02:00
|
|
|
sp_search_release(result);
|
|
|
|
}
|
2011-04-25 21:16:26 +02:00
|
|
|
|
2012-01-08 00:30:33 +01:00
|
|
|
void SpotifyClient::MessageArrived(const pb::spotify::Message& message) {
|
2011-04-25 21:16:26 +02:00
|
|
|
if (message.has_login_request()) {
|
2011-11-27 18:29:33 +01:00
|
|
|
Login(message.login_request());
|
2011-04-26 19:06:36 +02:00
|
|
|
} else if (message.has_load_playlist_request()) {
|
|
|
|
LoadPlaylist(message.load_playlist_request());
|
2011-04-26 19:06:46 +02:00
|
|
|
} else if (message.has_playback_request()) {
|
|
|
|
StartPlayback(message.playback_request());
|
2011-09-25 20:24:44 +02:00
|
|
|
} else if (message.has_seek_request()) {
|
2014-09-21 16:13:00 +02:00
|
|
|
Seek(message.seek_request().offset_nsec());
|
2011-04-27 18:38:28 +02:00
|
|
|
} else if (message.has_search_request()) {
|
|
|
|
Search(message.search_request());
|
2011-04-29 15:41:42 +02:00
|
|
|
} else if (message.has_image_request()) {
|
|
|
|
LoadImage(QStringFromStdString(message.image_request().id()));
|
2011-05-25 16:22:49 +02:00
|
|
|
} else if (message.has_sync_playlist_request()) {
|
|
|
|
SyncPlaylist(message.sync_playlist_request());
|
2011-08-29 04:26:59 +02:00
|
|
|
} else if (message.has_browse_album_request()) {
|
|
|
|
BrowseAlbum(QStringFromStdString(message.browse_album_request().uri()));
|
2011-11-27 18:29:33 +01:00
|
|
|
} else if (message.has_set_playback_settings_request()) {
|
|
|
|
SetPlaybackSettings(message.set_playback_settings_request());
|
2012-06-12 15:34:23 +02:00
|
|
|
} else if (message.has_browse_toplist_request()) {
|
|
|
|
BrowseToplist(message.browse_toplist_request());
|
2014-09-06 19:21:23 +02:00
|
|
|
} else if (message.has_pause_request()) {
|
|
|
|
SetPaused(message.pause_request());
|
2014-09-22 22:40:21 +02:00
|
|
|
} else if (message.has_add_tracks_to_playlist()) {
|
|
|
|
AddTracksToPlaylist(message.add_tracks_to_playlist());
|
2014-09-28 02:00:32 +02:00
|
|
|
} else if (message.has_remove_tracks_from_playlist()) {
|
|
|
|
RemoveTracksFromPlaylist(message.remove_tracks_from_playlist());
|
2011-04-26 15:42:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SpotifyClient::SetPlaybackSettings(
|
|
|
|
const pb::spotify::PlaybackSettings& req) {
|
2011-11-27 18:29:33 +01:00
|
|
|
sp_bitrate bitrate = SP_BITRATE_320k;
|
|
|
|
switch (req.bitrate()) {
|
2014-02-07 16:34:20 +01:00
|
|
|
case pb::spotify::Bitrate96k:
|
|
|
|
bitrate = SP_BITRATE_96k;
|
|
|
|
break;
|
|
|
|
case pb::spotify::Bitrate160k:
|
|
|
|
bitrate = SP_BITRATE_160k;
|
|
|
|
break;
|
|
|
|
case pb::spotify::Bitrate320k:
|
|
|
|
bitrate = SP_BITRATE_320k;
|
|
|
|
break;
|
2011-11-27 18:29:33 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
qLog(Debug) << "Setting playback settings: bitrate" << bitrate
|
|
|
|
<< "normalisation" << req.volume_normalisation();
|
2011-11-27 18:29:33 +01:00
|
|
|
|
|
|
|
sp_session_preferred_bitrate(session_, bitrate);
|
|
|
|
sp_session_preferred_offline_bitrate(session_, bitrate, false);
|
|
|
|
sp_session_set_volume_normalization(session_, req.volume_normalisation());
|
|
|
|
}
|
|
|
|
|
2012-01-08 00:26:27 +01:00
|
|
|
void SpotifyClient::Login(const pb::spotify::LoginRequest& req) {
|
2011-04-26 15:42:58 +02:00
|
|
|
sp_error error = sp_session_create(&spotify_config_, &session_);
|
|
|
|
if (error != SP_ERROR_OK) {
|
|
|
|
qLog(Warning) << "Failed to create session" << sp_error_message(error);
|
2014-02-07 16:34:20 +01:00
|
|
|
SendLoginCompleted(false, sp_error_message(error),
|
|
|
|
pb::spotify::LoginResponse_Error_Other);
|
2011-04-26 15:42:58 +02:00
|
|
|
return;
|
2011-04-25 21:16:26 +02:00
|
|
|
}
|
2011-04-26 15:42:58 +02:00
|
|
|
|
2011-11-27 18:29:33 +01:00
|
|
|
SetPlaybackSettings(req.playback_settings());
|
2011-09-01 21:56:40 +02:00
|
|
|
|
2011-11-27 18:29:33 +01:00
|
|
|
if (req.password().empty()) {
|
2011-11-24 12:17:19 +01:00
|
|
|
sp_error error = sp_session_relogin(session_);
|
|
|
|
if (error != SP_ERROR_OK) {
|
|
|
|
qLog(Warning) << "Tried to relogin but no stored credentials";
|
|
|
|
SendLoginCompleted(false, sp_error_message(error),
|
2012-01-08 00:26:27 +01:00
|
|
|
pb::spotify::LoginResponse_Error_ReloginFailed);
|
2011-11-24 12:17:19 +01:00
|
|
|
}
|
|
|
|
} else {
|
2014-02-07 16:34:20 +01:00
|
|
|
sp_session_login(session_, req.username().c_str(), req.password().c_str(),
|
|
|
|
true, // Remember the password.
|
2014-02-06 16:49:49 +01:00
|
|
|
nullptr);
|
2011-11-24 12:17:19 +01:00
|
|
|
}
|
2011-04-25 21:16:26 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SpotifyClient::SendLoginCompleted(
|
|
|
|
bool success, const QString& error,
|
|
|
|
pb::spotify::LoginResponse_Error error_code) {
|
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::LoginResponse* response = message.mutable_login_response();
|
2011-04-26 15:42:58 +02:00
|
|
|
response->set_success(success);
|
|
|
|
response->set_error(DataCommaSizeFromQString(error));
|
|
|
|
|
2011-08-27 23:01:28 +02:00
|
|
|
if (!success) {
|
|
|
|
response->set_error_code(error_code);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SpotifyClient::PlaylistContainerLoadedCallback(sp_playlistcontainer* pc,
|
|
|
|
void* userdata) {
|
2011-04-26 15:42:58 +02:00
|
|
|
SpotifyClient* me = reinterpret_cast<SpotifyClient*>(userdata);
|
2011-04-27 18:38:28 +02:00
|
|
|
|
|
|
|
// Install callbacks on all the playlists
|
2015-01-14 22:55:20 +01:00
|
|
|
sp_playlist_add_callbacks(sp_session_starred_create(me->session_),
|
|
|
|
&me->get_playlists_callbacks_, me);
|
2011-04-27 18:38:28 +02:00
|
|
|
const int count = sp_playlistcontainer_num_playlists(pc);
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < count; ++i) {
|
2011-04-27 18:38:28 +02:00
|
|
|
sp_playlist* playlist = sp_playlistcontainer_playlist(pc, i);
|
|
|
|
sp_playlist_add_callbacks(playlist, &me->get_playlists_callbacks_, me);
|
|
|
|
}
|
|
|
|
|
|
|
|
me->SendPlaylistList();
|
2011-04-25 21:16:26 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SpotifyClient::PlaylistAddedCallback(sp_playlistcontainer* pc,
|
|
|
|
sp_playlist* playlist, int position,
|
|
|
|
void* userdata) {
|
2011-04-26 15:42:58 +02:00
|
|
|
SpotifyClient* me = reinterpret_cast<SpotifyClient*>(userdata);
|
2011-04-27 18:38:28 +02:00
|
|
|
|
|
|
|
// Install callbacks on this playlist
|
|
|
|
sp_playlist_add_callbacks(playlist, &me->get_playlists_callbacks_, me);
|
|
|
|
|
|
|
|
me->SendPlaylistList();
|
2011-04-26 15:42:58 +02:00
|
|
|
}
|
2011-04-25 21:16:26 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SpotifyClient::PlaylistMovedCallback(sp_playlistcontainer* pc,
|
|
|
|
sp_playlist* playlist, int position,
|
|
|
|
int new_position, void* userdata) {
|
2011-04-26 15:42:58 +02:00
|
|
|
SpotifyClient* me = reinterpret_cast<SpotifyClient*>(userdata);
|
2011-04-27 18:38:28 +02:00
|
|
|
me->SendPlaylistList();
|
2011-04-26 15:42:58 +02:00
|
|
|
}
|
2011-04-25 21:16:26 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SpotifyClient::PlaylistRemovedCallback(sp_playlistcontainer* pc,
|
|
|
|
sp_playlist* playlist, int position,
|
|
|
|
void* userdata) {
|
2011-04-26 15:42:58 +02:00
|
|
|
SpotifyClient* me = reinterpret_cast<SpotifyClient*>(userdata);
|
2011-04-27 18:38:28 +02:00
|
|
|
|
|
|
|
// Remove callbacks from this playlist
|
|
|
|
sp_playlist_remove_callbacks(playlist, &me->get_playlists_callbacks_, me);
|
|
|
|
|
|
|
|
me->SendPlaylistList();
|
2011-04-26 15:42:58 +02:00
|
|
|
}
|
|
|
|
|
2011-04-27 18:38:28 +02:00
|
|
|
void SpotifyClient::SendPlaylistList() {
|
2012-01-08 00:31:20 +01:00
|
|
|
pb::spotify::Message message;
|
2012-01-08 00:26:27 +01:00
|
|
|
pb::spotify::Playlists* response = message.mutable_playlists_updated();
|
2011-04-26 15:42:58 +02:00
|
|
|
|
|
|
|
sp_playlistcontainer* container = sp_session_playlistcontainer(session_);
|
|
|
|
if (!container) {
|
2014-02-06 16:49:49 +01:00
|
|
|
qLog(Warning) << "sp_session_playlistcontainer returned nullptr";
|
2011-04-26 15:42:58 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int count = sp_playlistcontainer_num_playlists(container);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < count; ++i) {
|
2011-04-26 15:42:58 +02:00
|
|
|
const int type = sp_playlistcontainer_playlist_type(container, i);
|
|
|
|
sp_playlist* playlist = sp_playlistcontainer_playlist(container, i);
|
2011-04-27 18:38:28 +02:00
|
|
|
const bool is_loaded = sp_playlist_is_loaded(playlist);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
qLog(Debug) << "Got playlist" << i << is_loaded << type
|
|
|
|
<< sp_playlist_name(playlist);
|
2011-04-26 15:42:58 +02:00
|
|
|
|
2011-04-27 18:38:28 +02:00
|
|
|
if (!is_loaded) {
|
2017-01-12 19:00:32 +01:00
|
|
|
qLog(Info) << "Playlist is not loaded yet, jump to the next one...";
|
|
|
|
continue;
|
2011-04-27 18:38:28 +02:00
|
|
|
}
|
2011-04-26 15:42:58 +02:00
|
|
|
|
|
|
|
if (type != SP_PLAYLIST_TYPE_PLAYLIST) {
|
|
|
|
// Just ignore folders for now
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-01-08 00:26:27 +01:00
|
|
|
pb::spotify::Playlists::Playlist* msg = response->add_playlist();
|
2011-04-26 15:42:58 +02:00
|
|
|
msg->set_index(i);
|
|
|
|
msg->set_name(sp_playlist_name(playlist));
|
2014-09-22 22:40:21 +02:00
|
|
|
sp_user* playlist_owner = sp_playlist_owner(playlist);
|
|
|
|
msg->set_is_mine(sp_session_user(session_) == playlist_owner);
|
|
|
|
msg->set_owner(sp_user_display_name(playlist_owner));
|
2011-05-25 16:22:49 +02:00
|
|
|
|
|
|
|
sp_playlist_offline_status offline_status =
|
|
|
|
sp_playlist_get_offline_status(session_, playlist);
|
|
|
|
const bool is_offline = offline_status == SP_PLAYLIST_OFFLINE_STATUS_YES;
|
|
|
|
msg->set_is_offline(is_offline);
|
|
|
|
if (offline_status == SP_PLAYLIST_OFFLINE_STATUS_DOWNLOADING) {
|
|
|
|
msg->set_download_progress(
|
|
|
|
sp_playlist_get_offline_download_completed(session_, playlist));
|
|
|
|
} else if (offline_status == SP_PLAYLIST_OFFLINE_STATUS_WAITING) {
|
|
|
|
msg->set_download_progress(0);
|
|
|
|
}
|
2014-09-22 22:40:21 +02:00
|
|
|
msg->set_nb_tracks(sp_playlist_num_tracks(playlist));
|
2014-12-27 18:05:04 +01:00
|
|
|
// URI - Blugh
|
|
|
|
char uri[256];
|
|
|
|
sp_link* link = sp_link_create_from_playlist(playlist);
|
|
|
|
sp_link_as_string(link, uri, arraysize(uri));
|
|
|
|
sp_link_release(link);
|
|
|
|
msg->set_uri(uri);
|
2011-04-26 15:42:58 +02:00
|
|
|
}
|
2011-04-25 21:16:26 +02:00
|
|
|
|
2012-01-08 00:30:33 +01:00
|
|
|
SendMessage(message);
|
2011-04-25 21:16:26 +02:00
|
|
|
}
|
2011-04-26 19:06:36 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
sp_playlist* SpotifyClient::GetPlaylist(pb::spotify::PlaylistType type,
|
|
|
|
int user_index) {
|
2014-02-06 16:49:49 +01:00
|
|
|
sp_playlist* playlist = nullptr;
|
2011-05-25 16:22:49 +02:00
|
|
|
switch (type) {
|
2012-01-08 00:26:27 +01:00
|
|
|
case pb::spotify::Inbox:
|
2011-05-25 16:22:49 +02:00
|
|
|
playlist = sp_session_inbox_create(session_);
|
2011-04-26 19:06:36 +02:00
|
|
|
break;
|
|
|
|
|
2012-01-08 00:26:27 +01:00
|
|
|
case pb::spotify::Starred:
|
2011-05-25 16:22:49 +02:00
|
|
|
playlist = sp_session_starred_create(session_);
|
2011-04-26 19:06:36 +02:00
|
|
|
break;
|
|
|
|
|
2012-01-08 00:26:27 +01:00
|
|
|
case pb::spotify::UserPlaylist: {
|
2011-04-26 19:06:36 +02:00
|
|
|
sp_playlistcontainer* pc = sp_session_playlistcontainer(session_);
|
|
|
|
|
2011-05-25 16:22:49 +02:00
|
|
|
if (pc && user_index <= sp_playlistcontainer_num_playlists(pc)) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (sp_playlistcontainer_playlist_type(pc, user_index) ==
|
|
|
|
SP_PLAYLIST_TYPE_PLAYLIST) {
|
2011-05-25 16:22:49 +02:00
|
|
|
playlist = sp_playlistcontainer_playlist(pc, user_index);
|
|
|
|
sp_playlist_add_ref(playlist);
|
2011-04-26 19:06:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-05-25 16:22:49 +02:00
|
|
|
return playlist;
|
|
|
|
}
|
|
|
|
|
2012-01-08 00:26:27 +01:00
|
|
|
void SpotifyClient::LoadPlaylist(const pb::spotify::LoadPlaylistRequest& req) {
|
2011-05-25 16:22:49 +02:00
|
|
|
PendingLoadPlaylist pending_load;
|
|
|
|
pending_load.request_ = req;
|
|
|
|
pending_load.playlist_ = GetPlaylist(req.type(), req.user_playlist_index());
|
2011-04-26 19:06:36 +02:00
|
|
|
|
|
|
|
// A null playlist might mean the user wasn't logged in, or an invalid
|
|
|
|
// playlist index was requested, so we'd better return an error straight away.
|
|
|
|
if (!pending_load.playlist_) {
|
|
|
|
qLog(Warning) << "Invalid playlist requested or not logged in";
|
|
|
|
|
2012-01-08 00:31:20 +01:00
|
|
|
pb::spotify::Message message;
|
2014-02-07 16:34:20 +01:00
|
|
|
pb::spotify::LoadPlaylistResponse* response =
|
|
|
|
message.mutable_load_playlist_response();
|
2011-04-26 19:06:36 +02:00
|
|
|
*response->mutable_request() = req;
|
2012-01-08 00:30:33 +01:00
|
|
|
SendMessage(message);
|
2011-04-26 19:06:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
sp_playlist_add_callbacks(pending_load.playlist_, &load_playlist_callbacks_,
|
|
|
|
this);
|
2011-04-26 19:06:36 +02:00
|
|
|
pending_load_playlists_ << pending_load;
|
|
|
|
|
2011-04-27 18:38:28 +02:00
|
|
|
PlaylistStateChangedForLoadPlaylist(pending_load.playlist_, this);
|
2011-04-26 19:06:36 +02:00
|
|
|
}
|
|
|
|
|
2012-01-08 00:26:27 +01:00
|
|
|
void SpotifyClient::SyncPlaylist(const pb::spotify::SyncPlaylistRequest& req) {
|
2014-02-07 16:34:20 +01:00
|
|
|
sp_playlist* playlist =
|
|
|
|
GetPlaylist(req.request().type(), req.request().user_playlist_index());
|
2011-05-25 16:22:49 +02:00
|
|
|
|
|
|
|
// The playlist should already be loaded.
|
|
|
|
sp_playlist_set_offline_mode(session_, playlist, req.offline_sync());
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SpotifyClient::PlaylistStateChangedForLoadPlaylist(sp_playlist* pl,
|
|
|
|
void* userdata) {
|
2011-04-26 19:06:36 +02:00
|
|
|
SpotifyClient* me = reinterpret_cast<SpotifyClient*>(userdata);
|
|
|
|
|
|
|
|
// If the playlist isn't loaded yet we have to wait
|
|
|
|
if (!sp_playlist_is_loaded(pl)) {
|
|
|
|
qLog(Debug) << "Playlist isn't loaded yet, waiting";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find this playlist's pending load object
|
|
|
|
int pending_load_index = -1;
|
2014-02-06 16:49:49 +01:00
|
|
|
PendingLoadPlaylist* pending_load = nullptr;
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < me->pending_load_playlists_.count(); ++i) {
|
2011-04-26 19:06:36 +02:00
|
|
|
if (me->pending_load_playlists_[i].playlist_ == pl) {
|
|
|
|
pending_load_index = i;
|
|
|
|
pending_load = &me->pending_load_playlists_[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pending_load) {
|
|
|
|
qLog(Warning) << "Playlist not found in pending load list";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the playlist was just loaded then get all its tracks and ref them
|
|
|
|
if (pending_load->tracks_.isEmpty()) {
|
|
|
|
const int count = sp_playlist_num_tracks(pl);
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < count; ++i) {
|
2011-04-26 19:06:36 +02:00
|
|
|
sp_track* track = sp_playlist_track(pl, i);
|
|
|
|
sp_track_add_ref(track);
|
|
|
|
pending_load->tracks_ << track;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If any of the tracks aren't loaded yet we have to wait
|
2014-02-10 14:29:07 +01:00
|
|
|
for (sp_track* track : pending_load->tracks_) {
|
2011-04-26 19:06:36 +02:00
|
|
|
if (!sp_track_is_loaded(track)) {
|
|
|
|
qLog(Debug) << "One or more tracks aren't loaded yet, waiting";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Everything is loaded so send the response protobuf and unref everything.
|
2012-01-08 00:31:20 +01:00
|
|
|
pb::spotify::Message message;
|
2014-02-07 16:34:20 +01:00
|
|
|
pb::spotify::LoadPlaylistResponse* response =
|
|
|
|
message.mutable_load_playlist_response();
|
2011-04-26 19:06:36 +02:00
|
|
|
|
2011-12-21 12:14:34 +01:00
|
|
|
// For some reason, we receive the starred tracks in reverse order but not
|
|
|
|
// other playlists.
|
2012-01-08 00:26:27 +01:00
|
|
|
if (pending_load->request_.type() == pb::spotify::Starred) {
|
2014-02-07 16:34:20 +01:00
|
|
|
std::reverse(pending_load->tracks_.begin(), pending_load->tracks_.end());
|
2011-12-21 12:14:34 +01:00
|
|
|
}
|
|
|
|
|
2011-04-26 19:06:36 +02:00
|
|
|
*response->mutable_request() = pending_load->request_;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (sp_track* track : pending_load->tracks_) {
|
2011-04-26 19:06:36 +02:00
|
|
|
me->ConvertTrack(track, response->add_track());
|
|
|
|
sp_track_release(track);
|
|
|
|
}
|
2012-01-08 00:30:33 +01:00
|
|
|
me->SendMessage(message);
|
2011-04-26 19:06:36 +02:00
|
|
|
|
|
|
|
// Unref the playlist and remove our callbacks
|
|
|
|
sp_playlist_remove_callbacks(pl, &me->load_playlist_callbacks_, me);
|
|
|
|
sp_playlist_release(pl);
|
|
|
|
|
|
|
|
// Remove the pending load object
|
|
|
|
me->pending_load_playlists_.removeAt(pending_load_index);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SpotifyClient::PlaylistStateChangedForGetPlaylists(sp_playlist* pl,
|
|
|
|
void* userdata) {
|
2011-04-27 18:38:28 +02:00
|
|
|
SpotifyClient* me = reinterpret_cast<SpotifyClient*>(userdata);
|
|
|
|
|
|
|
|
me->SendPlaylistList();
|
|
|
|
}
|
|
|
|
|
2014-09-22 22:40:21 +02:00
|
|
|
void SpotifyClient::AddTracksToPlaylist(
|
|
|
|
const pb::spotify::AddTracksToPlaylistRequest& req) {
|
|
|
|
// Get the playlist we want to update
|
2015-01-14 22:55:20 +01:00
|
|
|
sp_playlist* playlist =
|
|
|
|
GetPlaylist(req.playlist_type(), req.playlist_index());
|
2014-09-22 22:40:21 +02:00
|
|
|
if (!playlist) {
|
2015-01-14 22:55:20 +01:00
|
|
|
qLog(Error) << "Playlist " << req.playlist_type() << ","
|
|
|
|
<< req.playlist_index() << "not found";
|
2014-09-22 22:40:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the tracks we want to add
|
2020-09-18 16:15:19 +02:00
|
|
|
std::unique_ptr<sp_track*[]> tracks_array(
|
|
|
|
new sp_track*[req.track_uri_size()]);
|
2014-09-22 22:40:21 +02:00
|
|
|
for (int i = 0; i < req.track_uri_size(); ++i) {
|
|
|
|
sp_link* track_link = sp_link_create_from_string(req.track_uri(i).c_str());
|
|
|
|
sp_track* track = sp_link_as_track(track_link);
|
|
|
|
sp_track_add_ref(track);
|
|
|
|
sp_link_release(track_link);
|
|
|
|
if (!track) {
|
2014-10-15 21:57:57 +02:00
|
|
|
qLog(Error) << "Track" << QString::fromStdString(req.track_uri(i))
|
|
|
|
<< "not found";
|
2014-09-22 22:40:21 +02:00
|
|
|
}
|
|
|
|
tracks_array[i] = track;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actually add the tracks to the playlist
|
2014-10-15 21:57:57 +02:00
|
|
|
if (sp_playlist_add_tracks(playlist, tracks_array.get(), req.track_uri_size(),
|
2014-09-22 22:40:21 +02:00
|
|
|
0 /* TODO: don't insert at a hardcoded position */,
|
|
|
|
session_) != SP_ERROR_OK) {
|
|
|
|
qLog(Error) << "Error when adding tracks!";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean everything
|
|
|
|
for (int i = 0; i < req.track_uri_size(); ++i) {
|
|
|
|
sp_track_release(tracks_array[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 02:00:32 +02:00
|
|
|
void SpotifyClient::RemoveTracksFromPlaylist(
|
|
|
|
const pb::spotify::RemoveTracksFromPlaylistRequest& req) {
|
|
|
|
// Get the playlist we want to update
|
|
|
|
sp_playlist* playlist =
|
2015-01-14 22:49:36 +01:00
|
|
|
GetPlaylist(req.playlist_type(), req.playlist_index());
|
2014-09-28 02:00:32 +02:00
|
|
|
if (!playlist) {
|
2015-01-14 22:55:20 +01:00
|
|
|
qLog(Error) << "Playlist " << req.playlist_type() << ","
|
|
|
|
<< req.playlist_index() << "not found";
|
2014-09-28 02:00:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the position of the tracks we want to remove
|
2014-10-15 21:57:57 +02:00
|
|
|
std::unique_ptr<int[]> tracks_indices_array(new int[req.track_index_size()]);
|
2014-09-28 02:00:32 +02:00
|
|
|
for (int i = 0; i < req.track_index_size(); ++i) {
|
|
|
|
tracks_indices_array[i] = req.track_index(i);
|
|
|
|
}
|
|
|
|
|
2015-01-14 22:49:36 +01:00
|
|
|
// WTF: sp_playlist_remove_tracks indexes start from the end for starred
|
|
|
|
// playlist, not from the beginning like other playlists: reverse them
|
|
|
|
if (req.playlist_type() == pb::spotify::Starred) {
|
|
|
|
int num_tracks = sp_playlist_num_tracks(playlist);
|
|
|
|
for (int i = 0; i < req.track_index_size(); i++) {
|
|
|
|
tracks_indices_array[i] = num_tracks - tracks_indices_array[i] - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 02:00:32 +02:00
|
|
|
if (sp_playlist_remove_tracks(playlist, tracks_indices_array.get(),
|
2014-10-15 21:57:57 +02:00
|
|
|
req.track_index_size()) != SP_ERROR_OK) {
|
2014-09-28 02:00:32 +02:00
|
|
|
qLog(Error) << "Error when removing tracks!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-08 00:26:27 +01:00
|
|
|
void SpotifyClient::ConvertTrack(sp_track* track, pb::spotify::Track* pb) {
|
2011-04-26 19:06:36 +02:00
|
|
|
sp_album* album = sp_track_album(track);
|
|
|
|
|
|
|
|
pb->set_starred(sp_track_is_starred(session_, track));
|
|
|
|
pb->set_title(sp_track_name(track));
|
|
|
|
pb->set_album(sp_album_name(album));
|
|
|
|
pb->set_year(sp_album_year(album));
|
|
|
|
pb->set_duration_msec(sp_track_duration(track));
|
|
|
|
pb->set_popularity(sp_track_popularity(track));
|
|
|
|
pb->set_disc(sp_track_disc(track));
|
|
|
|
pb->set_track(sp_track_index(track));
|
|
|
|
|
2011-04-29 15:41:42 +02:00
|
|
|
// Album art
|
2014-02-07 16:34:20 +01:00
|
|
|
const QByteArray art_id(reinterpret_cast<const char*>(sp_album_cover(
|
|
|
|
sp_track_album(track), SP_IMAGE_SIZE_LARGE)),
|
|
|
|
kSpotifyImageIDSize);
|
2015-06-13 23:01:52 +02:00
|
|
|
const QString art_id_b64 = QString::fromLatin1(art_id.toBase64());
|
2011-04-29 15:41:42 +02:00
|
|
|
pb->set_album_art_id(DataCommaSizeFromQString(art_id_b64));
|
|
|
|
|
|
|
|
// Artists
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < sp_track_num_artists(track); ++i) {
|
2011-04-26 19:06:36 +02:00
|
|
|
pb->add_artist(sp_artist_name(sp_track_artist(track, i)));
|
|
|
|
}
|
|
|
|
|
2011-04-29 15:41:42 +02:00
|
|
|
// URI - Blugh
|
2011-04-26 19:06:36 +02:00
|
|
|
char uri[256];
|
|
|
|
sp_link* link = sp_link_create_from_track(track, 0);
|
2014-04-29 14:11:52 +02:00
|
|
|
sp_link_as_string(link, uri, arraysize(uri));
|
2011-04-26 19:06:36 +02:00
|
|
|
sp_link_release(link);
|
|
|
|
|
|
|
|
pb->set_uri(uri);
|
|
|
|
}
|
|
|
|
|
2012-01-08 00:26:27 +01:00
|
|
|
void SpotifyClient::ConvertAlbum(sp_album* album, pb::spotify::Track* pb) {
|
2011-08-29 03:00:59 +02:00
|
|
|
pb->set_album(sp_album_name(album));
|
|
|
|
pb->set_year(sp_album_year(album));
|
|
|
|
pb->add_artist(sp_artist_name(sp_album_artist(album)));
|
|
|
|
|
|
|
|
// These fields were required in a previous version so need to set them again
|
|
|
|
// now.
|
|
|
|
pb->mutable_title();
|
|
|
|
pb->set_duration_msec(-1);
|
|
|
|
pb->set_popularity(-1);
|
|
|
|
pb->set_disc(-1);
|
|
|
|
pb->set_track(-1);
|
|
|
|
pb->set_starred(false);
|
|
|
|
|
|
|
|
// Album art
|
|
|
|
const QByteArray art_id(
|
2014-02-07 16:34:20 +01:00
|
|
|
reinterpret_cast<const char*>(sp_album_cover(album, SP_IMAGE_SIZE_LARGE)),
|
|
|
|
kSpotifyImageIDSize);
|
2015-06-13 23:01:52 +02:00
|
|
|
const QString art_id_b64 = QString::fromLatin1(art_id.toBase64());
|
2011-08-29 03:00:59 +02:00
|
|
|
pb->set_album_art_id(DataCommaSizeFromQString(art_id_b64));
|
|
|
|
|
|
|
|
// URI - Blugh
|
|
|
|
char uri[256];
|
|
|
|
sp_link* link = sp_link_create_from_album(album);
|
2014-04-29 14:11:52 +02:00
|
|
|
sp_link_as_string(link, uri, arraysize(uri));
|
2011-08-29 03:00:59 +02:00
|
|
|
sp_link_release(link);
|
|
|
|
|
|
|
|
pb->set_uri(uri);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SpotifyClient::ConvertAlbumBrowse(sp_albumbrowse* browse,
|
|
|
|
pb::spotify::Track* pb) {
|
2011-09-17 16:57:34 +02:00
|
|
|
pb->set_track(sp_albumbrowse_num_tracks(browse));
|
|
|
|
}
|
|
|
|
|
2011-04-26 19:06:36 +02:00
|
|
|
void SpotifyClient::MetadataUpdatedCallback(sp_session* session) {
|
2014-02-07 16:34:20 +01:00
|
|
|
SpotifyClient* me =
|
|
|
|
reinterpret_cast<SpotifyClient*>(sp_session_userdata(session));
|
2011-04-26 19:06:36 +02:00
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const PendingLoadPlaylist& load : me->pending_load_playlists_) {
|
2011-04-27 18:38:28 +02:00
|
|
|
PlaylistStateChangedForLoadPlaylist(load.playlist_, me);
|
2011-04-26 19:06:36 +02:00
|
|
|
}
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const PendingPlaybackRequest& playback :
|
|
|
|
me->pending_playback_requests_) {
|
2011-04-28 17:11:10 +02:00
|
|
|
me->TryPlaybackAgain(playback);
|
|
|
|
}
|
2011-04-26 19:06:36 +02:00
|
|
|
}
|
2011-04-26 19:06:46 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
int SpotifyClient::MusicDeliveryCallback(sp_session* session,
|
|
|
|
const sp_audioformat* format,
|
|
|
|
const void* frames, int num_frames) {
|
|
|
|
SpotifyClient* me =
|
|
|
|
reinterpret_cast<SpotifyClient*>(sp_session_userdata(session));
|
2011-04-26 19:06:46 +02:00
|
|
|
|
2011-11-28 19:11:09 +01:00
|
|
|
if (!me->media_pipeline_) {
|
2011-04-26 19:06:46 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-25 20:24:44 +02:00
|
|
|
if (num_frames == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-28 19:11:09 +01:00
|
|
|
if (!me->media_pipeline_->is_initialised()) {
|
|
|
|
if (!me->media_pipeline_->Init(format->sample_rate, format->channels)) {
|
|
|
|
qLog(Warning) << "Failed to intitialise media pipeline";
|
|
|
|
sp_session_player_unload(me->session_);
|
|
|
|
me->media_pipeline_.reset();
|
|
|
|
return 0;
|
|
|
|
}
|
2011-04-26 19:06:46 +02:00
|
|
|
}
|
|
|
|
|
2011-11-28 19:28:28 +01:00
|
|
|
if (!me->media_pipeline_->is_accepting_data()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
me->media_pipeline_->WriteData(reinterpret_cast<const char*>(frames),
|
|
|
|
num_frames * format->channels * 2);
|
2011-09-25 20:24:44 +02:00
|
|
|
|
2011-11-28 19:11:09 +01:00
|
|
|
return num_frames;
|
2011-04-26 19:06:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpotifyClient::EndOfTrackCallback(sp_session* session) {
|
2014-02-07 16:34:20 +01:00
|
|
|
SpotifyClient* me =
|
|
|
|
reinterpret_cast<SpotifyClient*>(sp_session_userdata(session));
|
2011-04-26 19:06:46 +02:00
|
|
|
|
2011-11-28 19:11:09 +01:00
|
|
|
me->media_pipeline_.reset();
|
2011-04-26 19:06:46 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SpotifyClient::StreamingErrorCallback(sp_session* session,
|
|
|
|
sp_error error) {
|
|
|
|
SpotifyClient* me =
|
|
|
|
reinterpret_cast<SpotifyClient*>(sp_session_userdata(session));
|
2011-04-26 19:06:46 +02:00
|
|
|
|
2011-11-28 19:11:09 +01:00
|
|
|
me->media_pipeline_.reset();
|
2011-04-26 19:06:46 +02:00
|
|
|
|
|
|
|
// Send the error
|
|
|
|
me->SendPlaybackError(QString::fromUtf8(sp_error_message(error)));
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SpotifyClient::ConnectionErrorCallback(sp_session* session,
|
|
|
|
sp_error error) {
|
2011-11-24 13:40:05 +01:00
|
|
|
qLog(Debug) << Q_FUNC_INFO << sp_error_message(error);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SpotifyClient::UserMessageCallback(sp_session* session,
|
|
|
|
const char* message) {
|
2011-11-24 13:40:05 +01:00
|
|
|
qLog(Debug) << Q_FUNC_INFO << message;
|
|
|
|
}
|
|
|
|
|
2011-11-24 14:19:10 +01:00
|
|
|
void SpotifyClient::StartPlaybackCallback(sp_session* session) {
|
|
|
|
qLog(Debug) << Q_FUNC_INFO;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpotifyClient::StopPlaybackCallback(sp_session* session) {
|
|
|
|
qLog(Debug) << Q_FUNC_INFO;
|
|
|
|
}
|
|
|
|
|
2011-05-25 16:22:49 +02:00
|
|
|
void SpotifyClient::OfflineStatusUpdatedCallback(sp_session* session) {
|
2014-02-07 16:34:20 +01:00
|
|
|
SpotifyClient* me =
|
|
|
|
reinterpret_cast<SpotifyClient*>(sp_session_userdata(session));
|
2011-05-25 16:22:49 +02:00
|
|
|
sp_playlistcontainer* container = sp_session_playlistcontainer(session);
|
|
|
|
if (!container) {
|
2014-02-06 16:49:49 +01:00
|
|
|
qLog(Warning) << "sp_session_playlistcontainer returned nullptr";
|
2011-05-25 16:22:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int count = sp_playlistcontainer_num_playlists(container);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
const sp_playlist_type type =
|
|
|
|
sp_playlistcontainer_playlist_type(container, i);
|
2011-05-25 16:22:49 +02:00
|
|
|
sp_playlist* playlist = sp_playlistcontainer_playlist(container, i);
|
|
|
|
|
|
|
|
if (type != SP_PLAYLIST_TYPE_PLAYLIST) {
|
|
|
|
// Just ignore folders for now
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int download_progress = me->GetDownloadProgress(playlist);
|
|
|
|
if (download_progress != -1) {
|
2012-01-08 00:26:27 +01:00
|
|
|
me->SendDownloadProgress(pb::spotify::UserPlaylist, i, download_progress);
|
2011-05-25 16:22:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sp_playlist* inbox = sp_session_inbox_create(session);
|
|
|
|
int download_progress = me->GetDownloadProgress(inbox);
|
|
|
|
sp_playlist_release(inbox);
|
|
|
|
|
|
|
|
if (download_progress != -1) {
|
2012-01-08 00:26:27 +01:00
|
|
|
me->SendDownloadProgress(pb::spotify::Inbox, -1, download_progress);
|
2011-05-25 16:22:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sp_playlist* starred = sp_session_starred_create(session);
|
|
|
|
download_progress = me->GetDownloadProgress(starred);
|
|
|
|
sp_playlist_release(starred);
|
|
|
|
|
|
|
|
if (download_progress != -1) {
|
2012-01-08 00:26:27 +01:00
|
|
|
me->SendDownloadProgress(pb::spotify::Starred, -1, download_progress);
|
2011-05-25 16:22:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SpotifyClient::SendDownloadProgress(pb::spotify::PlaylistType type,
|
|
|
|
int index, int download_progress) {
|
2012-01-08 00:31:20 +01:00
|
|
|
pb::spotify::Message message;
|
2014-02-07 16:34:20 +01:00
|
|
|
pb::spotify::SyncPlaylistProgress* progress =
|
|
|
|
message.mutable_sync_playlist_progress();
|
2011-05-25 16:22:49 +02:00
|
|
|
progress->mutable_request()->set_type(type);
|
|
|
|
if (index != -1) {
|
|
|
|
progress->mutable_request()->set_user_playlist_index(index);
|
|
|
|
}
|
|
|
|
progress->set_sync_progress(download_progress);
|
2012-01-08 00:30:33 +01:00
|
|
|
SendMessage(message);
|
2011-05-25 16:22:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int SpotifyClient::GetDownloadProgress(sp_playlist* playlist) {
|
|
|
|
sp_playlist_offline_status status =
|
2014-02-07 16:34:20 +01:00
|
|
|
sp_playlist_get_offline_status(session_, playlist);
|
2011-05-25 16:22:49 +02:00
|
|
|
switch (status) {
|
|
|
|
case SP_PLAYLIST_OFFLINE_STATUS_NO:
|
|
|
|
return -1;
|
|
|
|
case SP_PLAYLIST_OFFLINE_STATUS_YES:
|
|
|
|
return 100;
|
|
|
|
case SP_PLAYLIST_OFFLINE_STATUS_DOWNLOADING:
|
|
|
|
return sp_playlist_get_offline_download_completed(session_, playlist);
|
|
|
|
case SP_PLAYLIST_OFFLINE_STATUS_WAITING:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-01-08 00:26:27 +01:00
|
|
|
void SpotifyClient::StartPlayback(const pb::spotify::PlaybackRequest& req) {
|
2011-04-26 19:06:46 +02:00
|
|
|
// Get a link object from the URI
|
|
|
|
sp_link* link = sp_link_create_from_string(req.track_uri().c_str());
|
|
|
|
if (!link) {
|
|
|
|
SendPlaybackError("Invalid Spotify URI");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the track from the link
|
|
|
|
sp_track* track = sp_link_as_track(link);
|
|
|
|
if (!track) {
|
|
|
|
SendPlaybackError("Spotify URI was not a track");
|
2011-04-28 17:11:10 +02:00
|
|
|
sp_link_release(link);
|
2011-04-26 19:06:46 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-28 17:11:10 +02:00
|
|
|
PendingPlaybackRequest pending_playback;
|
|
|
|
pending_playback.request_ = req;
|
|
|
|
pending_playback.link_ = link;
|
|
|
|
pending_playback.track_ = track;
|
|
|
|
|
|
|
|
pending_playback_requests_ << pending_playback;
|
|
|
|
|
|
|
|
TryPlaybackAgain(pending_playback);
|
|
|
|
}
|
|
|
|
|
2014-09-21 16:13:00 +02:00
|
|
|
void SpotifyClient::Seek(qint64 offset_nsec) {
|
2014-10-14 10:25:39 +02:00
|
|
|
// TODO
|
|
|
|
qLog(Error) << "TODO seeking";
|
2011-09-25 20:24:44 +02:00
|
|
|
}
|
|
|
|
|
2011-04-28 17:11:10 +02:00
|
|
|
void SpotifyClient::TryPlaybackAgain(const PendingPlaybackRequest& req) {
|
|
|
|
// If the track was not loaded then we have to come back later
|
|
|
|
if (!sp_track_is_loaded(req.track_)) {
|
|
|
|
qLog(Debug) << "Playback track not loaded yet, will try again later";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-26 19:06:46 +02:00
|
|
|
// Load the track
|
2011-04-28 17:11:10 +02:00
|
|
|
sp_error error = sp_session_player_load(session_, req.track_);
|
2011-04-26 19:06:46 +02:00
|
|
|
if (error != SP_ERROR_OK) {
|
2011-04-28 17:11:10 +02:00
|
|
|
SendPlaybackError("Spotify playback error: " +
|
|
|
|
QString::fromUtf8(sp_error_message(error)));
|
|
|
|
sp_link_release(req.link_);
|
2015-03-14 15:26:44 +01:00
|
|
|
|
|
|
|
// Remove this from the pending list now
|
|
|
|
pending_playback_requests_.removeAll(req);
|
2011-04-26 19:06:46 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the media socket
|
2011-11-28 19:11:09 +01:00
|
|
|
media_pipeline_.reset(new MediaPipeline(req.request_.media_port(),
|
|
|
|
sp_track_duration(req.track_)));
|
2011-04-26 20:39:38 +02:00
|
|
|
|
2011-04-28 17:11:10 +02:00
|
|
|
qLog(Info) << "Starting playback of uri" << req.request_.track_uri().c_str()
|
|
|
|
<< "to port" << req.request_.media_port();
|
2011-04-26 19:06:46 +02:00
|
|
|
|
|
|
|
// Start playback
|
|
|
|
sp_session_player_play(session_, true);
|
|
|
|
|
2011-04-28 17:11:10 +02:00
|
|
|
sp_link_release(req.link_);
|
2015-03-13 14:12:19 +01:00
|
|
|
|
|
|
|
// Remove this from the pending list now
|
|
|
|
pending_playback_requests_.removeAll(req);
|
2011-04-26 19:06:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpotifyClient::SendPlaybackError(const QString& error) {
|
2012-01-08 00:31:20 +01:00
|
|
|
pb::spotify::Message message;
|
2012-01-08 00:26:27 +01:00
|
|
|
pb::spotify::PlaybackError* msg = message.mutable_playback_error();
|
2011-04-26 19:06:46 +02:00
|
|
|
|
|
|
|
msg->set_error(DataCommaSizeFromQString(error));
|
2012-01-08 00:30:33 +01:00
|
|
|
SendMessage(message);
|
2011-04-26 19:06:46 +02:00
|
|
|
}
|
|
|
|
|
2011-04-29 15:41:42 +02:00
|
|
|
void SpotifyClient::LoadImage(const QString& id_b64) {
|
2015-06-13 23:01:52 +02:00
|
|
|
QByteArray id = QByteArray::fromBase64(id_b64.toLatin1());
|
2011-04-29 15:41:42 +02:00
|
|
|
if (id.length() != kSpotifyImageIDSize) {
|
|
|
|
qLog(Warning) << "Invalid image ID (did not decode to"
|
|
|
|
<< kSpotifyImageIDSize << "bytes):" << id_b64;
|
|
|
|
|
|
|
|
// Send an error response straight away
|
2012-01-08 00:31:20 +01:00
|
|
|
pb::spotify::Message message;
|
2012-01-08 00:26:27 +01:00
|
|
|
pb::spotify::ImageResponse* msg = message.mutable_image_response();
|
2011-04-29 15:41:42 +02:00
|
|
|
msg->set_id(DataCommaSizeFromQString(id_b64));
|
2012-01-08 00:30:33 +01:00
|
|
|
SendMessage(message);
|
2011-04-29 15:41:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PendingImageRequest pending_load;
|
|
|
|
pending_load.id_ = id;
|
|
|
|
pending_load.id_b64_ = id_b64;
|
2014-02-07 16:34:20 +01:00
|
|
|
pending_load.image_ =
|
|
|
|
sp_image_create(session_, reinterpret_cast<const byte*>(id.constData()));
|
2011-04-29 15:41:42 +02:00
|
|
|
pending_image_requests_ << pending_load;
|
|
|
|
|
|
|
|
if (!image_callbacks_registered_[pending_load.image_]) {
|
|
|
|
sp_image_add_load_callback(pending_load.image_, &ImageLoaded, this);
|
|
|
|
}
|
2014-02-07 16:34:20 +01:00
|
|
|
image_callbacks_registered_[pending_load.image_]++;
|
2011-04-29 15:41:42 +02:00
|
|
|
|
|
|
|
TryImageAgain(pending_load.image_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpotifyClient::TryImageAgain(sp_image* image) {
|
|
|
|
if (!sp_image_is_loaded(image)) {
|
|
|
|
qLog(Debug) << "Image not loaded, will try again later";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the pending request for this image
|
|
|
|
int index = -1;
|
2014-02-06 16:49:49 +01:00
|
|
|
PendingImageRequest* req = nullptr;
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < pending_image_requests_.count(); ++i) {
|
2011-04-29 15:41:42 +02:00
|
|
|
if (pending_image_requests_[i].image_ == image) {
|
|
|
|
index = i;
|
|
|
|
req = &pending_image_requests_[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index == -1) {
|
|
|
|
qLog(Warning) << "Image not found in pending load list";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the image data
|
|
|
|
size_t size = 0;
|
|
|
|
const void* data = sp_image_data(image, &size);
|
|
|
|
|
|
|
|
// Send the response
|
2012-01-08 00:31:20 +01:00
|
|
|
pb::spotify::Message message;
|
2012-01-08 00:26:27 +01:00
|
|
|
pb::spotify::ImageResponse* msg = message.mutable_image_response();
|
2011-04-29 15:41:42 +02:00
|
|
|
msg->set_id(DataCommaSizeFromQString(req->id_b64_));
|
|
|
|
if (data && size) {
|
|
|
|
msg->set_data(data, size);
|
|
|
|
}
|
2012-01-08 00:30:33 +01:00
|
|
|
SendMessage(message);
|
2011-04-29 15:41:42 +02:00
|
|
|
|
|
|
|
// Free stuff
|
2014-02-07 16:34:20 +01:00
|
|
|
image_callbacks_registered_[image]--;
|
2011-08-29 02:24:32 +02:00
|
|
|
|
|
|
|
// TODO: memory leak?
|
|
|
|
// sp_image_remove_load_callback(image, &ImageLoaded, this);
|
|
|
|
image_callbacks_registered_.remove(image);
|
2011-04-29 15:41:42 +02:00
|
|
|
|
|
|
|
sp_image_release(image);
|
|
|
|
pending_image_requests_.removeAt(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpotifyClient::ImageLoaded(sp_image* image, void* userdata) {
|
|
|
|
SpotifyClient* me = reinterpret_cast<SpotifyClient*>(userdata);
|
|
|
|
me->TryImageAgain(image);
|
|
|
|
}
|
2011-08-29 04:26:59 +02:00
|
|
|
|
|
|
|
void SpotifyClient::BrowseAlbum(const QString& uri) {
|
|
|
|
// Get a link object from the URI
|
|
|
|
sp_link* link = sp_link_create_from_string(uri.toStdString().c_str());
|
|
|
|
if (!link) {
|
|
|
|
SendPlaybackError("Invalid Album URI");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-17 16:57:34 +02:00
|
|
|
// Get the album from the link
|
2011-08-29 04:26:59 +02:00
|
|
|
sp_album* album = sp_link_as_album(link);
|
|
|
|
if (!album) {
|
|
|
|
SendPlaybackError("Spotify URI was not an album");
|
|
|
|
sp_link_release(link);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sp_albumbrowse* browse =
|
|
|
|
sp_albumbrowse_create(session_, album, &AlbumBrowseComplete, this);
|
|
|
|
pending_album_browses_[browse] = uri;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SpotifyClient::AlbumBrowseComplete(sp_albumbrowse* result,
|
|
|
|
void* userdata) {
|
2011-08-29 04:26:59 +02:00
|
|
|
SpotifyClient* me = reinterpret_cast<SpotifyClient*>(userdata);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!me->pending_album_browses_.contains(result)) return;
|
2011-08-29 04:26:59 +02:00
|
|
|
|
|
|
|
QString uri = me->pending_album_browses_.take(result);
|
|
|
|
|
2012-01-08 00:31:20 +01:00
|
|
|
pb::spotify::Message message;
|
2014-02-07 16:34:20 +01:00
|
|
|
pb::spotify::BrowseAlbumResponse* msg =
|
|
|
|
message.mutable_browse_album_response();
|
2011-08-29 04:26:59 +02:00
|
|
|
|
|
|
|
msg->set_uri(DataCommaSizeFromQString(uri));
|
|
|
|
|
|
|
|
const int count = sp_albumbrowse_num_tracks(result);
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < count; ++i) {
|
2011-08-29 04:26:59 +02:00
|
|
|
me->ConvertTrack(sp_albumbrowse_track(result, i), msg->add_track());
|
|
|
|
}
|
|
|
|
|
2012-01-08 00:30:33 +01:00
|
|
|
me->SendMessage(message);
|
2011-09-17 16:57:34 +02:00
|
|
|
sp_albumbrowse_release(result);
|
2011-08-29 04:26:59 +02:00
|
|
|
}
|
2012-01-08 17:34:34 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SpotifyClient::BrowseToplist(
|
|
|
|
const pb::spotify::BrowseToplistRequest& req) {
|
2012-06-12 15:34:23 +02:00
|
|
|
sp_toplistbrowse* browse = sp_toplistbrowse_create(
|
2014-02-07 16:34:20 +01:00
|
|
|
session_, SP_TOPLIST_TYPE_TRACKS, // TODO: Support albums and artists.
|
|
|
|
SP_TOPLIST_REGION_EVERYWHERE, // TODO: Support other regions.
|
|
|
|
nullptr, &ToplistBrowseComplete, this);
|
2012-06-12 15:34:23 +02:00
|
|
|
pending_toplist_browses_[browse] = req;
|
|
|
|
}
|
|
|
|
|
2014-09-06 19:21:23 +02:00
|
|
|
void SpotifyClient::SetPaused(const pb::spotify::PauseRequest& req) {
|
|
|
|
sp_session_player_play(session_, !req.paused());
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SpotifyClient::ToplistBrowseComplete(sp_toplistbrowse* result,
|
|
|
|
void* userdata) {
|
2012-06-12 15:34:23 +02:00
|
|
|
SpotifyClient* me = reinterpret_cast<SpotifyClient*>(userdata);
|
|
|
|
|
|
|
|
qLog(Debug) << "Toplist browse request took:"
|
2014-02-07 16:34:20 +01:00
|
|
|
<< sp_toplistbrowse_backend_request_duration(result) << "ms";
|
2012-06-12 15:34:23 +02:00
|
|
|
|
|
|
|
if (!me->pending_toplist_browses_.contains(result)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
const pb::spotify::BrowseToplistRequest& request =
|
|
|
|
me->pending_toplist_browses_.take(result);
|
2012-06-12 15:34:23 +02:00
|
|
|
|
|
|
|
pb::spotify::Message message;
|
2014-02-07 16:34:20 +01:00
|
|
|
pb::spotify::BrowseToplistResponse* msg =
|
|
|
|
message.mutable_browse_toplist_response();
|
2012-06-12 15:34:23 +02:00
|
|
|
msg->mutable_request()->CopyFrom(request);
|
|
|
|
|
|
|
|
const int count = sp_toplistbrowse_num_tracks(result);
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
me->ConvertTrack(sp_toplistbrowse_track(result, i), msg->add_track());
|
|
|
|
}
|
|
|
|
|
|
|
|
me->SendMessage(message);
|
|
|
|
sp_toplistbrowse_release(result);
|
|
|
|
}
|
|
|
|
|
2012-01-22 19:42:40 +01:00
|
|
|
void SpotifyClient::DeviceClosed() {
|
|
|
|
AbstractMessageHandler<pb::spotify::Message>::DeviceClosed();
|
2012-01-08 17:34:34 +01:00
|
|
|
|
|
|
|
qApp->exit();
|
|
|
|
}
|