2011-04-26 13:43:13 +00:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2011, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// 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 19:16:26 +00:00
|
|
|
#ifndef SPOTIFYCLIENT_H
|
|
|
|
#define SPOTIFYCLIENT_H
|
|
|
|
|
2011-04-26 17:06:36 +00:00
|
|
|
#include "spotifymessages.pb.h"
|
2011-04-26 13:42:58 +00:00
|
|
|
#include "spotifymessageutils.h"
|
|
|
|
|
2011-04-25 19:16:26 +00:00
|
|
|
#include <QObject>
|
|
|
|
|
2011-04-26 13:42:58 +00:00
|
|
|
#include <libspotify/api.h>
|
|
|
|
|
2011-04-25 19:16:26 +00:00
|
|
|
class QTcpSocket;
|
2011-04-26 13:42:58 +00:00
|
|
|
class QTimer;
|
2011-04-25 19:16:26 +00:00
|
|
|
|
|
|
|
class ResponseMessage;
|
|
|
|
|
2011-04-26 13:42:58 +00:00
|
|
|
class SpotifyClient : public QObject, protected SpotifyMessageUtils {
|
2011-04-25 19:16:26 +00:00
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
SpotifyClient(QObject* parent = 0);
|
2011-04-26 13:42:58 +00:00
|
|
|
~SpotifyClient();
|
2011-04-25 19:16:26 +00:00
|
|
|
|
|
|
|
void Init(quint16 port);
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void SocketReadyRead();
|
2011-04-26 13:42:58 +00:00
|
|
|
void ProcessEvents();
|
2011-04-26 17:06:46 +00:00
|
|
|
void MediaSocketDisconnected();
|
2011-04-25 19:16:26 +00:00
|
|
|
|
|
|
|
private:
|
2011-04-26 13:42:58 +00:00
|
|
|
void SendLoginCompleted(bool success, const QString& error);
|
2011-04-26 17:06:46 +00:00
|
|
|
void SendPlaybackError(const QString& error);
|
2011-04-26 13:42:58 +00:00
|
|
|
|
|
|
|
// Spotify session callbacks.
|
|
|
|
static void LoggedInCallback(sp_session* session, sp_error error);
|
|
|
|
static void NotifyMainThreadCallback(sp_session* session);
|
|
|
|
static void LogMessageCallback(sp_session* session, const char* data);
|
|
|
|
static void SearchCompleteCallback(sp_search* result, void* userdata);
|
2011-04-26 17:06:36 +00:00
|
|
|
static void MetadataUpdatedCallback(sp_session* session);
|
2011-04-26 17:06:46 +00:00
|
|
|
static int MusicDeliveryCallback(
|
|
|
|
sp_session* session, const sp_audioformat* format,
|
|
|
|
const void* frames, int num_frames);
|
|
|
|
static void EndOfTrackCallback(sp_session* session);
|
|
|
|
static void StreamingErrorCallback(sp_session* session, sp_error error);
|
2011-04-26 13:42:58 +00:00
|
|
|
|
|
|
|
// Spotify playlist container callbacks.
|
|
|
|
static void PlaylistAddedCallback(
|
|
|
|
sp_playlistcontainer* pc, sp_playlist* playlist,
|
|
|
|
int position, void* userdata);
|
|
|
|
static void PlaylistRemovedCallback(
|
|
|
|
sp_playlistcontainer* pc, sp_playlist* playlist,
|
|
|
|
int position, void* userdata);
|
|
|
|
static void PlaylistMovedCallback(
|
|
|
|
sp_playlistcontainer* pc, sp_playlist* playlist,
|
|
|
|
int position, int new_position, void* userdata);
|
|
|
|
static void PlaylistContainerLoadedCallback(
|
|
|
|
sp_playlistcontainer* pc, void* userdata);
|
|
|
|
|
2011-04-26 17:06:36 +00:00
|
|
|
// Spotify playlist callbacks - when loading a playlist
|
|
|
|
static void PlaylistStateChanged(sp_playlist* pl, void* userdata);
|
|
|
|
|
2011-04-26 13:42:58 +00:00
|
|
|
// Request handlers.
|
|
|
|
void Login(const QString& username, const QString& password);
|
|
|
|
void GetPlaylists();
|
|
|
|
void Search(const QString& query);
|
2011-04-26 17:06:36 +00:00
|
|
|
void LoadPlaylist(const protobuf::LoadPlaylistRequest& req);
|
2011-04-26 17:06:46 +00:00
|
|
|
void StartPlayback(const protobuf::PlaybackRequest& req);
|
2011-04-26 17:06:36 +00:00
|
|
|
|
|
|
|
void ConvertTrack(sp_track* track, protobuf::Track* pb);
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct PendingLoadPlaylist {
|
|
|
|
protobuf::LoadPlaylistRequest request_;
|
|
|
|
|
|
|
|
sp_playlist* playlist_;
|
|
|
|
|
|
|
|
QList<sp_track*> tracks_;
|
|
|
|
};
|
2011-04-25 19:16:26 +00:00
|
|
|
|
2011-04-26 13:43:13 +00:00
|
|
|
QByteArray api_key_;
|
|
|
|
|
2011-04-26 17:06:46 +00:00
|
|
|
QTcpSocket* protocol_socket_;
|
|
|
|
QTcpSocket* media_socket_;
|
2011-04-26 13:42:58 +00:00
|
|
|
|
|
|
|
sp_session_config spotify_config_;
|
|
|
|
sp_session_callbacks spotify_callbacks_;
|
|
|
|
sp_playlistcontainer_callbacks playlistcontainer_callbacks_;
|
2011-04-26 17:06:36 +00:00
|
|
|
sp_playlist_callbacks load_playlist_callbacks_;
|
2011-04-26 13:42:58 +00:00
|
|
|
sp_session* session_;
|
|
|
|
|
|
|
|
QTimer* events_timer_;
|
2011-04-26 17:06:36 +00:00
|
|
|
|
|
|
|
QList<PendingLoadPlaylist> pending_load_playlists_;
|
2011-04-26 17:06:46 +00:00
|
|
|
|
|
|
|
int media_length_msec_;
|
2011-04-25 19:16:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // SPOTIFYCLIENT_H
|