Clementine-audio-player-Mac.../src/internet/groovesharkservice.h

323 lines
12 KiB
C
Raw Normal View History

/* This file is part of Clementine.
Copyright 2011, David Sansome <me@davidsansome.com>
Clementine is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Clementine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GROOVESHARKSERVICE_H
#define GROOVESHARKSERVICE_H
#include "internetmodel.h"
#include "internetservice.h"
#include <QSslError>
class GroovesharkUrlHandler;
class NetworkAccessManager;
class Playlist;
class SearchBoxWidget;
class QMenu;
class QNetworkReply;
class QNetworkRequest;
class QSortFilterProxyModel;
class GroovesharkService : public InternetService {
Q_OBJECT
public:
GroovesharkService(Application* app, InternetModel *parent);
~GroovesharkService();
enum Role {
Role_UserPlaylistId = InternetModel::RoleCount,
Role_PlaylistType
};
enum PlaylistType {
UserPlaylist = Qt::UserRole,
// Favorites and Library list are like playlists, but we want to do special
// treatments in some cases
2011-11-25 01:00:58 +01:00
UserFavorites,
UserLibrary,
2011-11-25 01:00:58 +01:00
SubscribedPlaylist
};
// Values are persisted - don't change.
enum LoginState {
LoginState_LoggedIn = 1,
LoginState_AuthFailed = 2,
LoginState_NoPremium = 3,
LoginState_OtherError = 4
};
// Internet Service methods
QStandardItem* CreateRootItem();
void LazyPopulate(QStandardItem *parent);
void ItemDoubleClicked(QStandardItem* item);
2011-11-29 13:57:35 +01:00
smart_playlists::GeneratorPtr CreateGenerator(QStandardItem* item);
void DropMimeData(const QMimeData* data, const QModelIndex& index);
QList<QAction*> playlistitem_actions(const Song& song);
void ShowContextMenu(const QPoint& global_pos);
QWidget* HeaderWidget() const;
// User should be logged in to be able to generate streaming urls
2011-11-29 23:52:19 +01:00
QUrl GetStreamingUrlFromSongId(const QString& song_id, const QString& artist_id,
QString* server_id, QString* stream_key,
qint64* length_nanosec);
void Login(const QString& username, const QString& password);
void Logout();
bool IsLoggedIn() const { return !session_id_.isEmpty(); }
2011-09-24 12:06:31 +02:00
void RetrieveUserPlaylists();
2011-10-29 20:42:25 +02:00
void RetrieveUserFavorites();
void RetrieveUserLibrarySongs();
2011-11-23 01:07:40 +01:00
void RetrievePopularSongs();
2011-11-19 18:56:29 +01:00
void RetrievePopularSongsMonth();
void RetrievePopularSongsToday();
2011-11-25 01:00:58 +01:00
void RetrieveSubscribedPlaylists();
2011-11-29 13:57:35 +01:00
void RetrieveAutoplayTags();
void SetPlaylistSongs(int playlist_id, const QList<int>& songs_ids);
void RemoveFromPlaylist(int playlist_id, const QList<int>& songs_ids_to_remove);
// Refresh playlist_id playlist , or create it if it doesn't exist
void RefreshPlaylist(int playlist_id);
void DeletePlaylist(int playlist_id);
2011-11-28 21:59:25 +01:00
void RenamePlaylist(int playlist_id);
void AddUserFavoriteSong(int song_id);
2012-05-11 21:46:07 +02:00
void RemoveFromFavorites(const QList<int>& songs_ids_to_remove);
2012-07-21 18:31:08 +02:00
void AddUserLibrarySongs(const QList<int>& songs_ids);
void RemoveFromLibrary(const QList<int>& songs_ids_to_remove);
void GetSongUrlToShare(int song_id);
void GetPlaylistUrlToShare(int playlist_id);
2011-11-29 13:57:35 +01:00
// Start autoplay for the given tag_id, fill the autoplay_state, returns a
// first song to play
Song StartAutoplayTag(int tag_id, QVariantMap& autoplay_state);
2011-11-29 23:52:19 +01:00
Song StartAutoplay(QVariantMap& autoplay_state);
2011-11-29 13:57:35 +01:00
// Get another autoplay song. autoplay_state is the autoplay_state received from StartAutoplayTag
Song GetAutoplaySong(QVariantMap& autoplay_state);
void MarkStreamKeyOver30Secs(const QString& stream_key, const QString& server_id);
void MarkSongComplete(const QString& song_id, const QString& stream_key, const QString& server_id);
2011-09-24 12:06:31 +02:00
// Persisted in the settings and updated on each Login().
LoginState login_state() const { return login_state_; }
const QString& session_id() { return session_id_; }
2011-09-30 15:35:09 +02:00
int SimpleSearch(const QString& query);
2011-10-04 18:26:40 +02:00
int SearchAlbums(const QString& query);
void GetAlbumSongs(quint64 album_id);
static const char* kServiceName;
static const char* kSettingsGroup;
signals:
void LoginFinished(bool success);
2011-09-30 15:35:09 +02:00
void SimpleSearchResults(int id, SongList songs);
// AlbumSearchResult emits the search id and the Grooveshark ids of the
// albums found. Albums' songs will be loaded asynchronously and
// AlbumSongsLoaded will be emitted, containing the actual Albums' songs.
void AlbumSearchResult(int id, QList<quint64> albums_ids);
void AlbumSongsLoaded(quint64 id, SongList songs);
public slots:
void Search(const QString& text, bool now = false);
void ShowConfig();
protected:
2011-09-24 12:06:31 +02:00
struct PlaylistInfo {
PlaylistInfo() {}
PlaylistInfo(int id, QString name, QStandardItem* item = NULL)
2011-11-25 01:00:58 +01:00
: id_(id), name_(name), item_(item) {}
2011-09-24 12:06:31 +02:00
bool operator< (const PlaylistInfo other) const {
return name_.localeAwareCompare(other.name_) < 0;
}
2011-09-24 12:06:31 +02:00
int id_;
QString name_;
QStandardItem* item_;
QList<int> songs_ids_;
2011-09-24 12:06:31 +02:00
};
private slots:
void SessionCreated(QNetworkReply* reply);
void DoSearch();
void SearchSongsFinished(QNetworkReply* reply);
void SimpleSearchFinished(QNetworkReply* reply, int id);
2011-10-04 18:26:40 +02:00
void SearchAlbumsFinished(QNetworkReply* reply, int id);
void GetAlbumSongsFinished(QNetworkReply* reply, quint64 album_id);
void Authenticated(QNetworkReply* reply);
void UserPlaylistsRetrieved(QNetworkReply* reply);
2011-11-23 01:07:40 +01:00
void UserFavoritesRetrieved(QNetworkReply* reply, int task_id);
void UserLibrarySongsRetrieved(QNetworkReply* reply, int task_id);
2011-11-19 18:56:29 +01:00
void PopularSongsMonthRetrieved(QNetworkReply* reply);
void PopularSongsTodayRetrieved(QNetworkReply* reply);
2011-11-25 01:00:58 +01:00
void SubscribedPlaylistsRetrieved(QNetworkReply* reply);
2011-11-29 13:57:35 +01:00
void AutoplayTagsRetrieved(QNetworkReply* reply);
void PlaylistSongsRetrieved(QNetworkReply* reply, int playlist_id);
2011-11-23 01:07:40 +01:00
void PlaylistSongsSet(QNetworkReply* reply, int playlist_id, int task_id);
void CreateNewPlaylist();
void NewPlaylistCreated(QNetworkReply* reply, const QString& name);
void DeleteCurrentPlaylist();
2011-11-28 21:59:25 +01:00
void RenameCurrentPlaylist();
void PlaylistDeleted(QNetworkReply* reply, int playlist_id);
2011-11-28 21:59:25 +01:00
void PlaylistRenamed(QNetworkReply* reply, int playlist_id, const QString& new_name);
void AddCurrentSongToUserFavorites() { AddUserFavoriteSong(current_song_id_); }
void AddCurrentSongToUserLibrary() { AddUserLibrarySongs(QList<int>() << current_song_id_); }
void AddCurrentSongToPlaylist(QAction* action);
2011-11-23 01:07:40 +01:00
void UserFavoriteSongAdded(QNetworkReply* reply, int task_id);
void UserLibrarySongAdded(QNetworkReply* reply, int task_id);
void GetCurrentSongUrlToShare();
void SongUrlToShareReceived(QNetworkReply* reply);
void GetCurrentPlaylistUrlToShare();
void PlaylistUrlToShareReceived(QNetworkReply* reply);
void RemoveCurrentFromPlaylist();
void RemoveCurrentFromFavorites();
void RemoveCurrentFromLibrary();
void SongsRemovedFromFavorites(QNetworkReply* reply, int task_id);
void SongsRemovedFromLibrary(QNetworkReply* reply, int task_id);
void StreamMarked(QNetworkReply* reply);
void SongMarkedAsComplete(QNetworkReply* reply);
void RequestSslErrors(const QList<QSslError>& errors);
void Homepage();
2011-12-22 20:45:44 +01:00
// Refresh all Grooveshark's items, and re-fill them
void RefreshItems();
2011-09-30 15:35:09 +02:00
private:
void EnsureMenuCreated();
void EnsureItemsCreated();
2011-12-22 20:45:44 +01:00
void RemoveItems();
void EnsureConnected();
void ClearSearchResults();
// Create a playlist item, with data set as excepted. Doesn't fill the item
// with songs rows.
QStandardItem* CreatePlaylistItem(const QString& playlist_name, int playlist_id);
void AuthenticateSession();
void InitCountry();
// Create a request for the given method, with the given params.
// If need_authentication is true, add session_id to params.
// Returns the reply object created
QNetworkReply* CreateRequest(
const QString& method_name,
const QList<QPair<QString, QVariant> >& params,
bool use_https = false);
2011-11-29 13:57:35 +01:00
// Convenient function which block until 'reply' replies, or timeout after 10
// seconds. Returns false if reply has timeouted
bool WaitForReply(QNetworkReply* reply);
void ShowUrlBox(const QString& title, const QString& url);
// Convenient function for extracting result from reply
QVariantMap ExtractResult(QNetworkReply* reply);
2011-11-29 13:57:35 +01:00
// Convenient function for extracting songs from grooveshark result. result
// should be the "result" field of most Grooveshark replies
2011-09-24 12:06:31 +02:00
SongList ExtractSongs(const QVariantMap& result);
2011-11-29 13:57:35 +01:00
// Convenient function for extracting song from grooveshark result.
// result_song should be the song field ('song', 'nextSong', ...) of the
// Grooveshark reply
Song ExtractSong(const QVariantMap& result_song);
// Convenient functions for extracting Grooveshark songs ids
QList<int> ExtractSongsIds(const QVariantMap& result);
QList<int> ExtractSongsIds(const QList<QUrl>& urls);
int ExtractSongId(const QUrl& url); // Returns 0 if url is not a Grooveshark url
// Convenient function for extracting basic playlist info (only 'id' and
// 'name': QStandardItem still need to be created), and sort them by name
QList<PlaylistInfo> ExtractPlaylistInfo(const QVariantMap& result);
void ResetSessionId();
GroovesharkUrlHandler* url_handler_;
2011-09-24 12:06:31 +02:00
QString pending_search_;
2011-09-30 15:35:09 +02:00
int next_pending_search_id_;
QSet<int> pending_retrieve_playlists_;
2011-09-24 12:06:31 +02:00
QMap<int, PlaylistInfo> playlists_;
2011-11-25 01:00:58 +01:00
QMap<int, PlaylistInfo> subscribed_playlists_;
QStandardItem* root_;
QStandardItem* search_;
2011-11-19 18:56:29 +01:00
QStandardItem* popular_month_;
QStandardItem* popular_today_;
2011-11-29 13:57:35 +01:00
QStandardItem* stations_;
2011-11-29 23:52:19 +01:00
QStandardItem* grooveshark_radio_;
QStandardItem* favorites_;
// Grooveshark Library (corresponds to Grooveshark 'MyMusic' actually, but
// called 'Library' in the API).
// Nothing to do with Clementine's local library
QStandardItem* library_;
QStandardItem* playlists_parent_;
QStandardItem* subscribed_playlists_parent_;
NetworkAccessManager* network_;
QMenu* context_menu_;
// IDs kept when showing menu, to know what the user has clicked on, to be
// able to perform actions on corresponding items
int current_song_id_;
int current_playlist_id_;
QAction* create_playlist_;
QAction* delete_playlist_;
2011-11-28 21:59:25 +01:00
QAction* rename_playlist_;
QAction* remove_from_playlist_;
QAction* remove_from_favorites_;
QAction* remove_from_library_;
QAction* get_url_to_share_song_;
QAction* get_url_to_share_playlist_;
QList<QAction*> playlistitem_actions_;
SearchBoxWidget* search_box_;
QTimer* search_delay_;
QNetworkReply* last_search_reply_;
QString username_;
QString password_; // In fact, password's md5 hash
QString user_id_;
QString session_id_;
QMap<QString, QVariant> country_;
2011-11-29 23:52:19 +01:00
// The last artists and songs ids th users has listened to. Used for autoplay
QList<int> last_artists_ids_;
QList<int> last_songs_ids_;
QByteArray api_key_;
LoginState login_state_;
2011-11-23 01:07:40 +01:00
// Tasks' ids: we need to keep them in mind to be able to update task status
// on each step
int task_popular_id_;
int task_playlists_id_;
int task_search_id_;
static const char* kUrl;
static const char* kUrlCover;
static const char* kHomepage;
static const int kSongSearchLimit;
static const int kSongSimpleSearchLimit;
static const int kAlbumSearchLimit;
static const int kSearchDelayMsec;
static const char* kApiKey;
static const char* kApiSecret;
};
#endif // GROOVESHARKSERVICE_H