diff --git a/CMakeLists.txt b/CMakeLists.txt index 6cfd0c791..b4df5a4cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -116,7 +116,9 @@ if(${CMAKE_BUILD_TYPE} MATCHES "Release") endif(${CMAKE_BUILD_TYPE} MATCHES "Release") # Use protobuf-lite if it's available +set(USE_PROTOBUF_LITE ON CACHE BOOL "Use protobuf-lite instead of protobuf if available") find_library(PROTOBUF_LITE_LIBRARY protobuf-lite) + if(NOT PROTOBUF_LITE_LIBRARY) # Lucid doesn't have a .so symlink find_file(PROTOBUF_LITE_LIBRARY diff --git a/cmake/SpotifyVersion.cmake b/cmake/SpotifyVersion.cmake index a5830f830..1dd209d34 100644 --- a/cmake/SpotifyVersion.cmake +++ b/cmake/SpotifyVersion.cmake @@ -1,2 +1,2 @@ # Increment this whenever the user needs to download a new blob -set(SPOTIFY_BLOB_VERSION 4) +set(SPOTIFY_BLOB_VERSION 5) diff --git a/spotifyblob/blob/spotifyclient.cpp b/spotifyblob/blob/spotifyclient.cpp index b94c16311..1e2778908 100644 --- a/spotifyblob/blob/spotifyclient.cpp +++ b/spotifyblob/blob/spotifyclient.cpp @@ -83,8 +83,8 @@ SpotifyClient::SpotifyClient(QObject* parent) events_timer_->setSingleShot(true); connect(events_timer_, SIGNAL(timeout()), SLOT(ProcessEvents())); - connect(handler_, SIGNAL(MessageArrived(protobuf::SpotifyMessage)), - SLOT(HandleMessage(protobuf::SpotifyMessage))); + connect(handler_, SIGNAL(MessageArrived(spotify_pb::SpotifyMessage)), + SLOT(HandleMessage(spotify_pb::SpotifyMessage))); connect(protocol_socket_, SIGNAL(disconnected()), QCoreApplication::instance(), SLOT(quit())); } @@ -108,7 +108,7 @@ void SpotifyClient::Init(quint16 port) { void SpotifyClient::LoggedInCallback(sp_session* session, sp_error error) { SpotifyClient* me = reinterpret_cast(sp_session_userdata(session)); const bool success = error == SP_ERROR_OK; - protobuf::LoginResponse_Error error_code = protobuf::LoginResponse_Error_Other; + spotify_pb::LoginResponse_Error error_code = spotify_pb::LoginResponse_Error_Other; if (!success) { qLog(Warning) << "Failed to login" << sp_error_message(error); @@ -116,13 +116,13 @@ void SpotifyClient::LoggedInCallback(sp_session* session, sp_error error) { switch (error) { case SP_ERROR_BAD_USERNAME_OR_PASSWORD: - error_code = protobuf::LoginResponse_Error_BadUsernameOrPassword; + error_code = spotify_pb::LoginResponse_Error_BadUsernameOrPassword; break; case SP_ERROR_USER_BANNED: - error_code = protobuf::LoginResponse_Error_UserBanned; + error_code = spotify_pb::LoginResponse_Error_UserBanned; break; case SP_ERROR_USER_NEEDS_PREMIUM : - error_code = protobuf::LoginResponse_Error_UserNeedsPremium; + error_code = spotify_pb::LoginResponse_Error_UserNeedsPremium; break; } @@ -150,9 +150,12 @@ void SpotifyClient::LogMessageCallback(sp_session* session, const char* data) { qLog(Debug) << "libspotify:" << QString::fromUtf8(data).trimmed(); } -void SpotifyClient::Search(const protobuf::SearchRequest& req) { +void SpotifyClient::Search(const spotify_pb::SearchRequest& req) { sp_search* search = sp_search_create( - session_, req.query().c_str(), 0, req.limit(), 0, 0, 0, 0, + session_, req.query().c_str(), + 0, req.limit(), + 0, req.limit_album(), + 0, 0, // artists &SearchCompleteCallback, this); pending_searches_[search] = req; @@ -167,11 +170,11 @@ void SpotifyClient::SearchCompleteCallback(sp_search* result, void* userdata) { } // Take the request out of the queue - protobuf::SearchRequest req = me->pending_searches_.take(result); + spotify_pb::SearchRequest req = me->pending_searches_.take(result); // Prepare the response - protobuf::SpotifyMessage message; - protobuf::SearchResponse* response = message.mutable_search_response(); + spotify_pb::SpotifyMessage message; + spotify_pb::SearchResponse* response = message.mutable_search_response(); *response->mutable_request() = req; @@ -186,12 +189,19 @@ void SpotifyClient::SearchCompleteCallback(sp_search* result, void* userdata) { } // Get the list of tracks from the search - const int count = sp_search_num_tracks(result); + int count = sp_search_num_tracks(result); for (int i=0 ; iConvertTrack(track, response->add_result()); } + // Get the albums from the search + count = sp_search_num_albums(result); + for (int i=0 ; iConvertAlbum(album, response->add_album()); + } + // 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)); @@ -200,9 +210,9 @@ void SpotifyClient::SearchCompleteCallback(sp_search* result, void* userdata) { sp_search_release(result); } -void SpotifyClient::HandleMessage(const protobuf::SpotifyMessage& message) { +void SpotifyClient::HandleMessage(const spotify_pb::SpotifyMessage& message) { if (message.has_login_request()) { - const protobuf::LoginRequest& r = message.login_request(); + const spotify_pb::LoginRequest& r = message.login_request(); Login(QStringFromStdString(r.username()), QStringFromStdString(r.password())); } else if (message.has_load_playlist_request()) { LoadPlaylist(message.load_playlist_request()); @@ -221,7 +231,7 @@ void SpotifyClient::Login(const QString& username, const QString& password) { sp_error error = sp_session_create(&spotify_config_, &session_); if (error != SP_ERROR_OK) { qLog(Warning) << "Failed to create session" << sp_error_message(error); - SendLoginCompleted(false, sp_error_message(error), protobuf::LoginResponse_Error_Other); + SendLoginCompleted(false, sp_error_message(error), spotify_pb::LoginResponse_Error_Other); return; } @@ -231,10 +241,10 @@ void SpotifyClient::Login(const QString& username, const QString& password) { } void SpotifyClient::SendLoginCompleted(bool success, const QString& error, - protobuf::LoginResponse_Error error_code) { - protobuf::SpotifyMessage message; + spotify_pb::LoginResponse_Error error_code) { + spotify_pb::SpotifyMessage message; - protobuf::LoginResponse* response = message.mutable_login_response(); + spotify_pb::LoginResponse* response = message.mutable_login_response(); response->set_success(success); response->set_error(DataCommaSizeFromQString(error)); @@ -282,8 +292,8 @@ void SpotifyClient::PlaylistRemovedCallback(sp_playlistcontainer* pc, sp_playlis } void SpotifyClient::SendPlaylistList() { - protobuf::SpotifyMessage message; - protobuf::Playlists* response = message.mutable_playlists_updated(); + spotify_pb::SpotifyMessage message; + spotify_pb::Playlists* response = message.mutable_playlists_updated(); sp_playlistcontainer* container = sp_session_playlistcontainer(session_); if (!container) { @@ -310,7 +320,7 @@ void SpotifyClient::SendPlaylistList() { continue; } - protobuf::Playlists::Playlist* msg = response->add_playlist(); + spotify_pb::Playlists::Playlist* msg = response->add_playlist(); msg->set_index(i); msg->set_name(sp_playlist_name(playlist)); @@ -329,18 +339,18 @@ void SpotifyClient::SendPlaylistList() { handler_->SendMessage(message); } -sp_playlist* SpotifyClient::GetPlaylist(protobuf::PlaylistType type, int user_index) { +sp_playlist* SpotifyClient::GetPlaylist(spotify_pb::PlaylistType type, int user_index) { sp_playlist* playlist = NULL; switch (type) { - case protobuf::Inbox: + case spotify_pb::Inbox: playlist = sp_session_inbox_create(session_); break; - case protobuf::Starred: + case spotify_pb::Starred: playlist = sp_session_starred_create(session_); break; - case protobuf::UserPlaylist: { + case spotify_pb::UserPlaylist: { sp_playlistcontainer* pc = sp_session_playlistcontainer(session_); if (pc && user_index <= sp_playlistcontainer_num_playlists(pc)) { @@ -356,7 +366,7 @@ sp_playlist* SpotifyClient::GetPlaylist(protobuf::PlaylistType type, int user_in return playlist; } -void SpotifyClient::LoadPlaylist(const protobuf::LoadPlaylistRequest& req) { +void SpotifyClient::LoadPlaylist(const spotify_pb::LoadPlaylistRequest& req) { PendingLoadPlaylist pending_load; pending_load.request_ = req; pending_load.playlist_ = GetPlaylist(req.type(), req.user_playlist_index()); @@ -366,8 +376,8 @@ void SpotifyClient::LoadPlaylist(const protobuf::LoadPlaylistRequest& req) { if (!pending_load.playlist_) { qLog(Warning) << "Invalid playlist requested or not logged in"; - protobuf::SpotifyMessage message; - protobuf::LoadPlaylistResponse* response = message.mutable_load_playlist_response(); + spotify_pb::SpotifyMessage message; + spotify_pb::LoadPlaylistResponse* response = message.mutable_load_playlist_response(); *response->mutable_request() = req; handler_->SendMessage(message); return; @@ -379,7 +389,7 @@ void SpotifyClient::LoadPlaylist(const protobuf::LoadPlaylistRequest& req) { PlaylistStateChangedForLoadPlaylist(pending_load.playlist_, this); } -void SpotifyClient::SyncPlaylist(const protobuf::SyncPlaylistRequest& req) { +void SpotifyClient::SyncPlaylist(const spotify_pb::SyncPlaylistRequest& req) { sp_playlist* playlist = GetPlaylist(req.request().type(), req.request().user_playlist_index()); // The playlist should already be loaded. @@ -430,8 +440,8 @@ void SpotifyClient::PlaylistStateChangedForLoadPlaylist(sp_playlist* pl, void* u } // Everything is loaded so send the response protobuf and unref everything. - protobuf::SpotifyMessage message; - protobuf::LoadPlaylistResponse* response = message.mutable_load_playlist_response(); + spotify_pb::SpotifyMessage message; + spotify_pb::LoadPlaylistResponse* response = message.mutable_load_playlist_response(); *response->mutable_request() = pending_load->request_; foreach (sp_track* track, pending_load->tracks_) { @@ -454,7 +464,7 @@ void SpotifyClient::PlaylistStateChangedForGetPlaylists(sp_playlist* pl, void* u me->SendPlaylistList(); } -void SpotifyClient::ConvertTrack(sp_track* track, protobuf::Track* pb) { +void SpotifyClient::ConvertTrack(sp_track* track, spotify_pb::Track* pb) { sp_album* album = sp_track_album(track); pb->set_starred(sp_track_is_starred(session_, track)); @@ -487,6 +497,36 @@ void SpotifyClient::ConvertTrack(sp_track* track, protobuf::Track* pb) { pb->set_uri(uri); } +void SpotifyClient::ConvertAlbum(sp_album* album, spotify_pb::Track* pb) { + 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( + reinterpret_cast(sp_album_cover(album)), + kSpotifyImageIDSize); + const QString art_id_b64 = QString::fromAscii(art_id.toBase64()); + pb->set_album_art_id(DataCommaSizeFromQString(art_id_b64)); + + // URI - Blugh + char uri[256]; + sp_link* link = sp_link_create_from_album(album); + sp_link_as_string(link, uri, sizeof(uri)); + sp_link_release(link); + + pb->set_uri(uri); +} + void SpotifyClient::MetadataUpdatedCallback(sp_session* session) { SpotifyClient* me = reinterpret_cast(sp_session_userdata(session)); @@ -596,7 +636,7 @@ void SpotifyClient::OfflineStatusUpdatedCallback(sp_session* session) { int download_progress = me->GetDownloadProgress(playlist); if (download_progress != -1) { - me->SendDownloadProgress(protobuf::UserPlaylist, i, download_progress); + me->SendDownloadProgress(spotify_pb::UserPlaylist, i, download_progress); } } @@ -605,7 +645,7 @@ void SpotifyClient::OfflineStatusUpdatedCallback(sp_session* session) { sp_playlist_release(inbox); if (download_progress != -1) { - me->SendDownloadProgress(protobuf::Inbox, -1, download_progress); + me->SendDownloadProgress(spotify_pb::Inbox, -1, download_progress); } sp_playlist* starred = sp_session_starred_create(session); @@ -613,14 +653,14 @@ void SpotifyClient::OfflineStatusUpdatedCallback(sp_session* session) { sp_playlist_release(starred); if (download_progress != -1) { - me->SendDownloadProgress(protobuf::Starred, -1, download_progress); + me->SendDownloadProgress(spotify_pb::Starred, -1, download_progress); } } void SpotifyClient::SendDownloadProgress( - protobuf::PlaylistType type, int index, int download_progress) { - protobuf::SpotifyMessage message; - protobuf::SyncPlaylistProgress* progress = message.mutable_sync_playlist_progress(); + spotify_pb::PlaylistType type, int index, int download_progress) { + spotify_pb::SpotifyMessage message; + spotify_pb::SyncPlaylistProgress* progress = message.mutable_sync_playlist_progress(); progress->mutable_request()->set_type(type); if (index != -1) { progress->mutable_request()->set_user_playlist_index(index); @@ -645,7 +685,7 @@ int SpotifyClient::GetDownloadProgress(sp_playlist* playlist) { return -1; } -void SpotifyClient::StartPlayback(const protobuf::PlaybackRequest& req) { +void SpotifyClient::StartPlayback(const spotify_pb::PlaybackRequest& req) { // Get a link object from the URI sp_link* link = sp_link_create_from_string(req.track_uri().c_str()); if (!link) { @@ -714,8 +754,8 @@ void SpotifyClient::TryPlaybackAgain(const PendingPlaybackRequest& req) { } void SpotifyClient::SendPlaybackError(const QString& error) { - protobuf::SpotifyMessage message; - protobuf::PlaybackError* msg = message.mutable_playback_error(); + spotify_pb::SpotifyMessage message; + spotify_pb::PlaybackError* msg = message.mutable_playback_error(); msg->set_error(DataCommaSizeFromQString(error)); handler_->SendMessage(message); @@ -744,8 +784,8 @@ void SpotifyClient::LoadImage(const QString& id_b64) { << kSpotifyImageIDSize << "bytes):" << id_b64; // Send an error response straight away - protobuf::SpotifyMessage message; - protobuf::ImageResponse* msg = message.mutable_image_response(); + spotify_pb::SpotifyMessage message; + spotify_pb::ImageResponse* msg = message.mutable_image_response(); msg->set_id(DataCommaSizeFromQString(id_b64)); handler_->SendMessage(message); return; @@ -793,8 +833,8 @@ void SpotifyClient::TryImageAgain(sp_image* image) { const void* data = sp_image_data(image, &size); // Send the response - protobuf::SpotifyMessage message; - protobuf::ImageResponse* msg = message.mutable_image_response(); + spotify_pb::SpotifyMessage message; + spotify_pb::ImageResponse* msg = message.mutable_image_response(); msg->set_id(DataCommaSizeFromQString(req->id_b64_)); if (data && size) { msg->set_data(data, size); diff --git a/spotifyblob/blob/spotifyclient.h b/spotifyblob/blob/spotifyclient.h index ca421328f..c67822b92 100644 --- a/spotifyblob/blob/spotifyclient.h +++ b/spotifyblob/blob/spotifyclient.h @@ -47,13 +47,13 @@ public: void Init(quint16 port); private slots: - void HandleMessage(const protobuf::SpotifyMessage& message); + void HandleMessage(const spotify_pb::SpotifyMessage& message); void ProcessEvents(); void MediaSocketDisconnected(); private: void SendLoginCompleted(bool success, const QString& error, - protobuf::LoginResponse_Error error_code); + spotify_pb::LoginResponse_Error error_code); void SendPlaybackError(const QString& error); // Spotify session callbacks. @@ -94,28 +94,30 @@ private: // Request handlers. void Login(const QString& username, const QString& password); - void Search(const protobuf::SearchRequest& req); - void LoadPlaylist(const protobuf::LoadPlaylistRequest& req); - void SyncPlaylist(const protobuf::SyncPlaylistRequest& req); - void StartPlayback(const protobuf::PlaybackRequest& req); + void Search(const spotify_pb::SearchRequest& req); + void LoadPlaylist(const spotify_pb::LoadPlaylistRequest& req); + void SyncPlaylist(const spotify_pb::SyncPlaylistRequest& req); + void StartPlayback(const spotify_pb::PlaybackRequest& req); void LoadImage(const QString& id_b64); void SendPlaylistList(); - void ConvertTrack(sp_track* track, protobuf::Track* pb); + void ConvertTrack(sp_track* track, spotify_pb::Track* pb); + void ConvertAlbum(sp_album* album, spotify_pb::Track* pb); + // Gets the appropriate sp_playlist* but does not load it. - sp_playlist* GetPlaylist(protobuf::PlaylistType type, int user_index); + sp_playlist* GetPlaylist(spotify_pb::PlaylistType type, int user_index); private: struct PendingLoadPlaylist { - protobuf::LoadPlaylistRequest request_; + spotify_pb::LoadPlaylistRequest request_; sp_playlist* playlist_; QList tracks_; bool offline_sync; }; struct PendingPlaybackRequest { - protobuf::PlaybackRequest request_; + spotify_pb::PlaybackRequest request_; sp_link* link_; sp_track* track_; @@ -134,7 +136,7 @@ private: void TryPlaybackAgain(const PendingPlaybackRequest& req); void TryImageAgain(sp_image* image); int GetDownloadProgress(sp_playlist* playlist); - void SendDownloadProgress(protobuf::PlaylistType type, int index, int download_progress); + void SendDownloadProgress(spotify_pb::PlaylistType type, int index, int download_progress); QByteArray api_key_; @@ -155,7 +157,7 @@ private: QList pending_playback_requests_; QList pending_image_requests_; QMap image_callbacks_registered_; - QMap pending_searches_; + QMap pending_searches_; int media_length_msec_; }; diff --git a/spotifyblob/common/CMakeLists.txt b/spotifyblob/common/CMakeLists.txt index 0dddd8d1a..3bba1d61f 100644 --- a/spotifyblob/common/CMakeLists.txt +++ b/spotifyblob/common/CMakeLists.txt @@ -27,11 +27,11 @@ add_library(clementine-spotifyblob-messages STATIC ) # Use protobuf-lite if it's available -if(PROTOBUF_LITE_LIBRARY) +if(PROTOBUF_LITE_LIBRARY AND USE_PROTOBUF_LITE) set(protobuf ${PROTOBUF_LITE_LIBRARY}) -else(PROTOBUF_LITE_LIBRARY) +else(PROTOBUF_LITE_LIBRARY AND USE_PROTOBUF_LITE) set(protobuf ${PROTOBUF_LIBRARY}) -endif(PROTOBUF_LITE_LIBRARY) +endif(PROTOBUF_LITE_LIBRARY AND USE_PROTOBUF_LITE) target_link_libraries(clementine-spotifyblob-messages ${protobuf} diff --git a/spotifyblob/common/spotifymessagehandler.cpp b/spotifyblob/common/spotifymessagehandler.cpp index 72e8867b7..fe43d79e5 100644 --- a/spotifyblob/common/spotifymessagehandler.cpp +++ b/spotifyblob/common/spotifymessagehandler.cpp @@ -51,7 +51,7 @@ void SpotifyMessageHandler::DeviceReadyRead() { // Did we get everything? if (buffer_.size() == expected_length_) { // Parse the message - protobuf::SpotifyMessage message; + spotify_pb::SpotifyMessage message; if (!message.ParseFromArray(buffer_.data().constData(), buffer_.size())) { qLog(Error) << "Malformed protobuf message"; device_->close(); @@ -69,7 +69,7 @@ void SpotifyMessageHandler::DeviceReadyRead() { } } -void SpotifyMessageHandler::SendMessage(const protobuf::SpotifyMessage& message) { +void SpotifyMessageHandler::SendMessage(const spotify_pb::SpotifyMessage& message) { std::string data(message.SerializeAsString()); QDataStream s(device_); diff --git a/spotifyblob/common/spotifymessagehandler.h b/spotifyblob/common/spotifymessagehandler.h index d67dce1a5..1c0a57b13 100644 --- a/spotifyblob/common/spotifymessagehandler.h +++ b/spotifyblob/common/spotifymessagehandler.h @@ -25,7 +25,7 @@ #include #include -namespace protobuf { +namespace spotify_pb { class SpotifyMessage; } @@ -42,10 +42,10 @@ class SpotifyMessageHandler : public QObject { public: SpotifyMessageHandler(QIODevice* device, QObject* parent); - void SendMessage(const protobuf::SpotifyMessage& message); + void SendMessage(const spotify_pb::SpotifyMessage& message); signals: - void MessageArrived(const protobuf::SpotifyMessage& message); + void MessageArrived(const spotify_pb::SpotifyMessage& message); private slots: void DeviceReadyRead(); diff --git a/spotifyblob/common/spotifymessages.proto b/spotifyblob/common/spotifymessages.proto index d6bc3f023..18d12d16a 100644 --- a/spotifyblob/common/spotifymessages.proto +++ b/spotifyblob/common/spotifymessages.proto @@ -19,9 +19,9 @@ // compatible. -package protobuf; +package spotify_pb; -option optimize_for = LITE_RUNTIME; +//option optimize_for = LITE_RUNTIME; message LoginRequest { @@ -106,6 +106,7 @@ message PlaybackError { message SearchRequest { required string query = 1; optional int32 limit = 2 [default = 250]; + optional int32 limit_album = 3 [default = 0]; } message SearchResponse { @@ -114,6 +115,8 @@ message SearchResponse { optional int32 total_tracks = 3; optional string did_you_mean = 4; optional string error = 5; + + repeated Track album = 6; } message ImageRequest { diff --git a/src/globalsearch/spotifysearchprovider.cpp b/src/globalsearch/spotifysearchprovider.cpp index 3e21420e7..9f5680f6f 100644 --- a/src/globalsearch/spotifysearchprovider.cpp +++ b/src/globalsearch/spotifysearchprovider.cpp @@ -40,8 +40,8 @@ SpotifyServer* SpotifySearchProvider::server() { return NULL; server_ = service_->server(); - connect(server_, SIGNAL(SearchResults(protobuf::SearchResponse)), - SLOT(SearchFinishedSlot(protobuf::SearchResponse))); + connect(server_, SIGNAL(SearchResults(spotify_pb::SearchResponse)), + SLOT(SearchFinishedSlot(spotify_pb::SearchResponse))); connect(server_, SIGNAL(ImageLoaded(QString,QImage)), SLOT(ArtLoadedSlot(QString,QImage))); connect(server_, SIGNAL(destroyed()), SLOT(ServerDestroyed())); @@ -65,11 +65,11 @@ void SpotifySearchProvider::SearchAsync(int id, const QString& query) { state.tokens_ = TokenizeQuery(query); const QString query_string = state.tokens_.join(" "); - s->Search(query_string, 25); + s->Search(query_string, 5, 5); queries_[query_string] = state; } -void SpotifySearchProvider::SearchFinishedSlot(const protobuf::SearchResponse& response) { +void SpotifySearchProvider::SearchFinishedSlot(const spotify_pb::SearchResponse& response) { QString query_string = QString::fromUtf8(response.request().query().c_str()); QMap::iterator it = queries_.find(query_string); if (it == queries_.end()) @@ -79,8 +79,8 @@ void SpotifySearchProvider::SearchFinishedSlot(const protobuf::SearchResponse& r queries_.erase(it); ResultList ret; - for (int i = 0; i < response.result_size(); ++i) { - const protobuf::Track& track = response.result(i); + for (int i=0; i < response.result_size() ; ++i) { + const spotify_pb::Track& track = response.result(i); Result result(this); result.type_ = Result::Type_Track; @@ -90,6 +90,19 @@ void SpotifySearchProvider::SearchFinishedSlot(const protobuf::SearchResponse& r ret << result; } + for (int i=0 ; inextPendingConnection(); handler_ = new SpotifyMessageHandler(protocol_socket_, this); - connect(handler_, SIGNAL(MessageArrived(protobuf::SpotifyMessage)), - SLOT(HandleMessage(protobuf::SpotifyMessage))); + connect(handler_, SIGNAL(MessageArrived(spotify_pb::SpotifyMessage)), + SLOT(HandleMessage(spotify_pb::SpotifyMessage))); qLog(Info) << "Connection from port" << protocol_socket_->peerPort(); // Send any login messages that were queued before the client connected - foreach (const protobuf::SpotifyMessage& message, queued_login_messages_) { + foreach (const spotify_pb::SpotifyMessage& message, queued_login_messages_) { SendMessage(message); } queued_login_messages_.clear(); } -void SpotifyServer::SendMessage(const protobuf::SpotifyMessage& message) { +void SpotifyServer::SendMessage(const spotify_pb::SpotifyMessage& message) { const bool is_login_message = message.has_login_request(); - QList* queue = + QList* queue = is_login_message ? &queued_login_messages_ : &queued_messages_; if (!protocol_socket_ || (!is_login_message && !logged_in_)) { @@ -76,23 +76,23 @@ void SpotifyServer::SendMessage(const protobuf::SpotifyMessage& message) { } void SpotifyServer::Login(const QString& username, const QString& password) { - protobuf::SpotifyMessage message; + spotify_pb::SpotifyMessage message; - protobuf::LoginRequest* request = message.mutable_login_request(); + spotify_pb::LoginRequest* request = message.mutable_login_request(); request->set_username(DataCommaSizeFromQString(username)); request->set_password(DataCommaSizeFromQString(password)); SendMessage(message); } -void SpotifyServer::HandleMessage(const protobuf::SpotifyMessage& message) { +void SpotifyServer::HandleMessage(const spotify_pb::SpotifyMessage& message) { if (message.has_login_response()) { - const protobuf::LoginResponse& response = message.login_response(); + const spotify_pb::LoginResponse& response = message.login_response(); logged_in_ = response.success(); if (response.success()) { // Send any messages that were queued before the client logged in - foreach (const protobuf::SpotifyMessage& message, queued_messages_) { + foreach (const spotify_pb::SpotifyMessage& message, queued_messages_) { SendMessage(message); } queued_messages_.clear(); @@ -103,18 +103,18 @@ void SpotifyServer::HandleMessage(const protobuf::SpotifyMessage& message) { } else if (message.has_playlists_updated()) { emit PlaylistsUpdated(message.playlists_updated()); } else if (message.has_load_playlist_response()) { - const protobuf::LoadPlaylistResponse& response = message.load_playlist_response(); + const spotify_pb::LoadPlaylistResponse& response = message.load_playlist_response(); switch (response.request().type()) { - case protobuf::Inbox: + case spotify_pb::Inbox: emit InboxLoaded(response); break; - case protobuf::Starred: + case spotify_pb::Starred: emit StarredLoaded(response); break; - case protobuf::UserPlaylist: + case spotify_pb::UserPlaylist: emit UserPlaylistLoaded(response); break; } @@ -123,7 +123,7 @@ void SpotifyServer::HandleMessage(const protobuf::SpotifyMessage& message) { } else if (message.has_search_response()) { emit SearchResults(message.search_response()); } else if (message.has_image_response()) { - const protobuf::ImageResponse& response = message.image_response(); + const spotify_pb::ImageResponse& response = message.image_response(); const QString id = QStringFromStdString(response.id()); if (response.has_data()) { @@ -137,9 +137,9 @@ void SpotifyServer::HandleMessage(const protobuf::SpotifyMessage& message) { } } -void SpotifyServer::LoadPlaylist(protobuf::PlaylistType type, int index) { - protobuf::SpotifyMessage message; - protobuf::LoadPlaylistRequest* req = message.mutable_load_playlist_request(); +void SpotifyServer::LoadPlaylist(spotify_pb::PlaylistType type, int index) { + spotify_pb::SpotifyMessage message; + spotify_pb::LoadPlaylistRequest* req = message.mutable_load_playlist_request(); req->set_type(type); if (index != -1) { @@ -150,9 +150,9 @@ void SpotifyServer::LoadPlaylist(protobuf::PlaylistType type, int index) { } void SpotifyServer::SyncPlaylist( - protobuf::PlaylistType type, int index, bool offline) { - protobuf::SpotifyMessage message; - protobuf::SyncPlaylistRequest* req = message.mutable_sync_playlist_request(); + spotify_pb::PlaylistType type, int index, bool offline) { + spotify_pb::SpotifyMessage message; + spotify_pb::SyncPlaylistRequest* req = message.mutable_sync_playlist_request(); req->mutable_request()->set_type(type); if (index != -1) { req->mutable_request()->set_user_playlist_index(index); @@ -163,52 +163,53 @@ void SpotifyServer::SyncPlaylist( } void SpotifyServer::SyncInbox() { - SyncPlaylist(protobuf::Inbox, -1, true); + SyncPlaylist(spotify_pb::Inbox, -1, true); } void SpotifyServer::SyncStarred() { - SyncPlaylist(protobuf::Starred, -1, true); + SyncPlaylist(spotify_pb::Starred, -1, true); } void SpotifyServer::SyncUserPlaylist(int index) { Q_ASSERT(index >= 0); - SyncPlaylist(protobuf::UserPlaylist, index, true); + SyncPlaylist(spotify_pb::UserPlaylist, index, true); } void SpotifyServer::LoadInbox() { - LoadPlaylist(protobuf::Inbox); + LoadPlaylist(spotify_pb::Inbox); } void SpotifyServer::LoadStarred() { - LoadPlaylist(protobuf::Starred); + LoadPlaylist(spotify_pb::Starred); } void SpotifyServer::LoadUserPlaylist(int index) { Q_ASSERT(index >= 0); - LoadPlaylist(protobuf::UserPlaylist, index); + LoadPlaylist(spotify_pb::UserPlaylist, index); } void SpotifyServer::StartPlayback(const QString& uri, quint16 port) { - protobuf::SpotifyMessage message; - protobuf::PlaybackRequest* req = message.mutable_playback_request(); + spotify_pb::SpotifyMessage message; + spotify_pb::PlaybackRequest* req = message.mutable_playback_request(); req->set_track_uri(DataCommaSizeFromQString(uri)); req->set_media_port(port); SendMessage(message); } -void SpotifyServer::Search(const QString& text, int limit) { - protobuf::SpotifyMessage message; - protobuf::SearchRequest* req = message.mutable_search_request(); +void SpotifyServer::Search(const QString& text, int limit, int limit_album) { + spotify_pb::SpotifyMessage message; + spotify_pb::SearchRequest* req = message.mutable_search_request(); req->set_query(DataCommaSizeFromQString(text)); req->set_limit(limit); + req->set_limit_album(limit_album); SendMessage(message); } void SpotifyServer::LoadImage(const QString& id) { - protobuf::SpotifyMessage message; - protobuf::ImageRequest* req = message.mutable_image_request(); + spotify_pb::SpotifyMessage message; + spotify_pb::ImageRequest* req = message.mutable_image_request(); req->set_id(DataCommaSizeFromQString(id)); SendMessage(message); diff --git a/src/internet/spotifyserver.h b/src/internet/spotifyserver.h index e588416e2..4b17249a7 100644 --- a/src/internet/spotifyserver.h +++ b/src/internet/spotifyserver.h @@ -44,40 +44,40 @@ public: void LoadUserPlaylist(int index); void SyncUserPlaylist(int index); void StartPlayback(const QString& uri, quint16 port); - void Search(const QString& text, int limit); + void Search(const QString& text, int limit, int limit_album = 0); void LoadImage(const QString& id); int server_port() const; signals: void LoginCompleted(bool success, const QString& error, - protobuf::LoginResponse_Error error_code); - void PlaylistsUpdated(const protobuf::Playlists& playlists); + spotify_pb::LoginResponse_Error error_code); + void PlaylistsUpdated(const spotify_pb::Playlists& playlists); - void StarredLoaded(const protobuf::LoadPlaylistResponse& response); - void InboxLoaded(const protobuf::LoadPlaylistResponse& response); - void UserPlaylistLoaded(const protobuf::LoadPlaylistResponse& response); + void StarredLoaded(const spotify_pb::LoadPlaylistResponse& response); + void InboxLoaded(const spotify_pb::LoadPlaylistResponse& response); + void UserPlaylistLoaded(const spotify_pb::LoadPlaylistResponse& response); void PlaybackError(const QString& message); - void SearchResults(const protobuf::SearchResponse& response); + void SearchResults(const spotify_pb::SearchResponse& response); void ImageLoaded(const QString& id, const QImage& image); - void SyncPlaylistProgress(const protobuf::SyncPlaylistProgress& progress); + void SyncPlaylistProgress(const spotify_pb::SyncPlaylistProgress& progress); private slots: void NewConnection(); - void HandleMessage(const protobuf::SpotifyMessage& message); + void HandleMessage(const spotify_pb::SpotifyMessage& message); private: - void LoadPlaylist(protobuf::PlaylistType type, int index = -1); - void SyncPlaylist(protobuf::PlaylistType type, int index, bool offline); - void SendMessage(const protobuf::SpotifyMessage& message); + void LoadPlaylist(spotify_pb::PlaylistType type, int index = -1); + void SyncPlaylist(spotify_pb::PlaylistType type, int index, bool offline); + void SendMessage(const spotify_pb::SpotifyMessage& message); QTcpServer* server_; QTcpSocket* protocol_socket_; SpotifyMessageHandler* handler_; bool logged_in_; - QList queued_login_messages_; - QList queued_messages_; + QList queued_login_messages_; + QList queued_messages_; }; #endif // SPOTIFYSERVER_H diff --git a/src/internet/spotifyservice.cpp b/src/internet/spotifyservice.cpp index 966b7d781..ddbedcb38 100644 --- a/src/internet/spotifyservice.cpp +++ b/src/internet/spotifyservice.cpp @@ -130,7 +130,7 @@ void SpotifyService::Login(const QString& username, const QString& password) { } void SpotifyService::LoginCompleted(bool success, const QString& error, - protobuf::LoginResponse_Error error_code) { + spotify_pb::LoginResponse_Error error_code) { if (login_task_id_) { model()->task_manager()->SetTaskFinished(login_task_id_); login_task_id_ = 0; @@ -142,15 +142,15 @@ void SpotifyService::LoginCompleted(bool success, const QString& error, QMessageBox::warning(NULL, tr("Spotify login error"), error, QMessageBox::Close); switch (error_code) { - case protobuf::LoginResponse_Error_BadUsernameOrPassword: + case spotify_pb::LoginResponse_Error_BadUsernameOrPassword: login_state_ = LoginState_BadCredentials; break; - case protobuf::LoginResponse_Error_UserBanned: + case spotify_pb::LoginResponse_Error_UserBanned: login_state_ = LoginState_Banned; break; - case protobuf::LoginResponse_Error_UserNeedsPremium: + case spotify_pb::LoginResponse_Error_UserNeedsPremium: login_state_ = LoginState_NoPremium; break; @@ -192,24 +192,24 @@ void SpotifyService::EnsureServerCreated(const QString& username, delete server_; server_ = new SpotifyServer(this); - connect(server_, SIGNAL(LoginCompleted(bool,QString,protobuf::LoginResponse_Error)), - SLOT(LoginCompleted(bool,QString,protobuf::LoginResponse_Error))); - connect(server_, SIGNAL(PlaylistsUpdated(protobuf::Playlists)), - SLOT(PlaylistsUpdated(protobuf::Playlists))); - connect(server_, SIGNAL(InboxLoaded(protobuf::LoadPlaylistResponse)), - SLOT(InboxLoaded(protobuf::LoadPlaylistResponse))); - connect(server_, SIGNAL(StarredLoaded(protobuf::LoadPlaylistResponse)), - SLOT(StarredLoaded(protobuf::LoadPlaylistResponse))); - connect(server_, SIGNAL(UserPlaylistLoaded(protobuf::LoadPlaylistResponse)), - SLOT(UserPlaylistLoaded(protobuf::LoadPlaylistResponse))); + connect(server_, SIGNAL(LoginCompleted(bool,QString,spotify_pb::LoginResponse_Error)), + SLOT(LoginCompleted(bool,QString,spotify_pb::LoginResponse_Error))); + connect(server_, SIGNAL(PlaylistsUpdated(spotify_pb::Playlists)), + SLOT(PlaylistsUpdated(spotify_pb::Playlists))); + connect(server_, SIGNAL(InboxLoaded(spotify_pb::LoadPlaylistResponse)), + SLOT(InboxLoaded(spotify_pb::LoadPlaylistResponse))); + connect(server_, SIGNAL(StarredLoaded(spotify_pb::LoadPlaylistResponse)), + SLOT(StarredLoaded(spotify_pb::LoadPlaylistResponse))); + connect(server_, SIGNAL(UserPlaylistLoaded(spotify_pb::LoadPlaylistResponse)), + SLOT(UserPlaylistLoaded(spotify_pb::LoadPlaylistResponse))); connect(server_, SIGNAL(PlaybackError(QString)), SIGNAL(StreamError(QString))); - connect(server_, SIGNAL(SearchResults(protobuf::SearchResponse)), - SLOT(SearchResults(protobuf::SearchResponse))); + connect(server_, SIGNAL(SearchResults(spotify_pb::SearchResponse)), + SLOT(SearchResults(spotify_pb::SearchResponse))); connect(server_, SIGNAL(ImageLoaded(QString,QImage)), SLOT(ImageLoaded(QString,QImage))); - connect(server_, SIGNAL(SyncPlaylistProgress(protobuf::SyncPlaylistProgress)), - SLOT(SyncPlaylistProgress(protobuf::SyncPlaylistProgress))); + connect(server_, SIGNAL(SyncPlaylistProgress(spotify_pb::SyncPlaylistProgress)), + SLOT(SyncPlaylistProgress(spotify_pb::SyncPlaylistProgress))); server_->Init(); @@ -292,7 +292,7 @@ void SpotifyService::BlobDownloadFinished() { EnsureServerCreated(); } -void SpotifyService::PlaylistsUpdated(const protobuf::Playlists& response) { +void SpotifyService::PlaylistsUpdated(const spotify_pb::Playlists& response) { if (login_task_id_) { model()->task_manager()->SetTaskFinished(login_task_id_); login_task_id_ = 0; @@ -332,7 +332,7 @@ void SpotifyService::PlaylistsUpdated(const protobuf::Playlists& response) { playlists_.clear(); for (int i=0 ; isetData(Type_UserPlaylist, InternetModel::Role_Type); @@ -344,13 +344,13 @@ void SpotifyService::PlaylistsUpdated(const protobuf::Playlists& response) { } } -bool SpotifyService::DoPlaylistsDiffer(const protobuf::Playlists& response) { +bool SpotifyService::DoPlaylistsDiffer(const spotify_pb::Playlists& response) { if (playlists_.count() != response.playlist_size()) { return true; } for (int i=0 ; ihasChildren()) item->removeRows(0, item->rowCount()); @@ -408,7 +408,7 @@ void SpotifyService::FillPlaylist(QStandardItem* item, const protobuf::LoadPlayl } } -void SpotifyService::SongFromProtobuf(const protobuf::Track& track, Song* song) { +void SpotifyService::SongFromProtobuf(const spotify_pb::Track& track, Song* song) { song->set_rating(track.starred() ? 1.0 : 0.0); song->set_title(QStringFromStdString(track.title())); song->set_album(QStringFromStdString(track.album())); @@ -505,7 +505,7 @@ void SpotifyService::DoSearch() { } } -void SpotifyService::SearchResults(const protobuf::SearchResponse& response) { +void SpotifyService::SearchResults(const spotify_pb::SearchResponse& response) { if (QStringFromStdString(response.request().query()) != pending_search_) { qLog(Debug) << "Old search result for" << QStringFromStdString(response.request().query()) @@ -585,17 +585,17 @@ void SpotifyService::ImageLoaded(const QString& id, const QImage& image) { } void SpotifyService::SyncPlaylistProgress( - const protobuf::SyncPlaylistProgress& progress) { + const spotify_pb::SyncPlaylistProgress& progress) { qLog(Debug) << "Sync progress:" << progress.sync_progress(); int task_id = -1; switch (progress.request().type()) { - case protobuf::Inbox: + case spotify_pb::Inbox: task_id = inbox_sync_id_; break; - case protobuf::Starred: + case spotify_pb::Starred: task_id = starred_sync_id_; break; - case protobuf::UserPlaylist: { + case spotify_pb::UserPlaylist: { QMap::const_iterator it = playlist_sync_ids_.constFind( progress.request().user_playlist_index()); if (it != playlist_sync_ids_.constEnd()) { @@ -613,7 +613,7 @@ void SpotifyService::SyncPlaylistProgress( model()->task_manager()->SetTaskProgress(task_id, progress.sync_progress(), 100); if (progress.sync_progress() == 100) { model()->task_manager()->SetTaskFinished(task_id); - if (progress.request().type() == protobuf::UserPlaylist) { + if (progress.request().type() == spotify_pb::UserPlaylist) { playlist_sync_ids_.remove(task_id); } } diff --git a/src/internet/spotifyservice.h b/src/internet/spotifyservice.h index 8e9458351..eb30d0e57 100644 --- a/src/internet/spotifyservice.h +++ b/src/internet/spotifyservice.h @@ -70,7 +70,7 @@ public: // Persisted in the settings and updated on each Login(). LoginState login_state() const { return login_state_; } - static void SongFromProtobuf(const protobuf::Track& track, Song* song); + static void SongFromProtobuf(const spotify_pb::Track& track, Song* song); signals: void BlobStateChanged(); @@ -84,23 +84,23 @@ private: void EnsureServerCreated(const QString& username = QString(), const QString& password = QString()); void StartBlobProcess(); - void FillPlaylist(QStandardItem* item, const protobuf::LoadPlaylistResponse& response); + void FillPlaylist(QStandardItem* item, const spotify_pb::LoadPlaylistResponse& response); void EnsureMenuCreated(); QStandardItem* PlaylistBySpotifyIndex(int index) const; - bool DoPlaylistsDiffer(const protobuf::Playlists& response); + bool DoPlaylistsDiffer(const spotify_pb::Playlists& response); private slots: void BlobProcessError(QProcess::ProcessError error); void LoginCompleted(bool success, const QString& error, - protobuf::LoginResponse_Error error_code); - void PlaylistsUpdated(const protobuf::Playlists& response); - void InboxLoaded(const protobuf::LoadPlaylistResponse& response); - void StarredLoaded(const protobuf::LoadPlaylistResponse& response); - void UserPlaylistLoaded(const protobuf::LoadPlaylistResponse& response); - void SearchResults(const protobuf::SearchResponse& response); + spotify_pb::LoginResponse_Error error_code); + void PlaylistsUpdated(const spotify_pb::Playlists& response); + void InboxLoaded(const spotify_pb::LoadPlaylistResponse& response); + void StarredLoaded(const spotify_pb::LoadPlaylistResponse& response); + void UserPlaylistLoaded(const spotify_pb::LoadPlaylistResponse& response); + void SearchResults(const spotify_pb::SearchResponse& response); void ImageLoaded(const QString& id, const QImage& image); - void SyncPlaylistProgress(const protobuf::SyncPlaylistProgress& progress); + void SyncPlaylistProgress(const spotify_pb::SyncPlaylistProgress& progress); void OpenSearchTab(); void DoSearch(); diff --git a/src/resolvers/spotifyresolver.cpp b/src/resolvers/spotifyresolver.cpp index 83a2c7a25..0429a9520 100644 --- a/src/resolvers/spotifyresolver.cpp +++ b/src/resolvers/spotifyresolver.cpp @@ -10,8 +10,8 @@ SpotifyResolver::SpotifyResolver(SpotifyServer* spotify, QObject* parent) : Resolver(parent), spotify_(spotify), next_id_(0) { - connect(spotify_, SIGNAL(SearchResults(protobuf::SearchResponse)), - SLOT(SearchFinished(protobuf::SearchResponse))); + connect(spotify_, SIGNAL(SearchResults(spotify_pb::SearchResponse)), + SLOT(SearchFinished(spotify_pb::SearchResponse))); } int SpotifyResolver::ResolveSong(const Song& song) { @@ -29,7 +29,7 @@ int SpotifyResolver::ResolveSong(const Song& song) { return id; } -void SpotifyResolver::SearchFinished(const protobuf::SearchResponse& response) { +void SpotifyResolver::SearchFinished(const spotify_pb::SearchResponse& response) { QString query_string = QString::fromUtf8(response.request().query().c_str()); qLog(Debug) << query_string; QMap::iterator it = queries_.find(query_string); @@ -42,7 +42,7 @@ void SpotifyResolver::SearchFinished(const protobuf::SearchResponse& response) { SongList songs; for (int i = 0; i < response.result_size(); ++i) { - const protobuf::Track& track = response.result(i); + const spotify_pb::Track& track = response.result(i); Song song; SpotifyService::SongFromProtobuf(track, &song); songs << song; diff --git a/src/resolvers/spotifyresolver.h b/src/resolvers/spotifyresolver.h index 2d7ec7a7e..dc56952b7 100644 --- a/src/resolvers/spotifyresolver.h +++ b/src/resolvers/spotifyresolver.h @@ -5,8 +5,8 @@ #include "resolver.h" -namespace protobuf { -class SearchResponse; +namespace spotify_pb { + class SearchResponse; } class SpotifyServer; @@ -21,7 +21,7 @@ class SpotifyResolver : public Resolver { void ResolveFinished(int id, SongList songs); private slots: - void SearchFinished(const protobuf::SearchResponse& response); + void SearchFinished(const spotify_pb::SearchResponse& response); private: SpotifyServer* spotify_;