2011-09-02 00:28:11 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2010, 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"
|
|
|
|
|
2011-09-20 00:26:24 +02:00
|
|
|
class GrooveSharkUrlHandler;
|
2011-09-02 00:28:11 +02:00
|
|
|
class NetworkAccessManager;
|
|
|
|
class Playlist;
|
|
|
|
class QMenu;
|
|
|
|
class QSortFilterProxyModel;
|
|
|
|
class QNetworkRequest;
|
|
|
|
|
|
|
|
class GrooveSharkService : public InternetService {
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
GrooveSharkService(InternetModel *parent);
|
|
|
|
~GrooveSharkService();
|
|
|
|
|
|
|
|
enum Type {
|
2011-09-24 12:06:31 +02:00
|
|
|
Type_SearchResults = InternetModel::TypeCount,
|
|
|
|
Type_UserPlaylist,
|
|
|
|
Type_Track
|
2011-09-02 00:28:11 +02:00
|
|
|
};
|
2011-09-13 22:32:10 +02:00
|
|
|
|
|
|
|
// Values are persisted - don't change.
|
|
|
|
enum LoginState {
|
|
|
|
LoginState_LoggedIn = 1,
|
|
|
|
LoginState_AuthFailed = 2,
|
|
|
|
LoginState_NoPremium = 3,
|
|
|
|
LoginState_OtherError = 4
|
|
|
|
};
|
|
|
|
|
2011-09-02 00:28:11 +02:00
|
|
|
// Internet Service methods
|
|
|
|
QStandardItem* CreateRootItem();
|
|
|
|
void LazyPopulate(QStandardItem *parent);
|
|
|
|
|
|
|
|
void ItemDoubleClicked(QStandardItem* item);
|
|
|
|
void ShowContextMenu(const QModelIndex& index, const QPoint& global_pos);
|
|
|
|
|
|
|
|
void Search(const QString& text, Playlist* playlist, bool now = false);
|
2011-09-20 00:26:24 +02:00
|
|
|
// User should be logged in to be able to generate streaming urls
|
|
|
|
QUrl GetStreamingUrlFromSongId(const QString& song_id);
|
2011-09-13 22:32:10 +02:00
|
|
|
void Login(const QString& username, const QString& password);
|
|
|
|
void Logout();
|
|
|
|
bool IsLoggedIn() { return !session_id_.isEmpty(); }
|
2011-09-24 12:06:31 +02:00
|
|
|
void RetrieveUserPlaylists();
|
|
|
|
|
2011-09-13 22:32:10 +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-02 00:28:11 +02:00
|
|
|
|
|
|
|
static const char* kServiceName;
|
2011-09-13 22:32:10 +02:00
|
|
|
static const char* kSettingsGroup;
|
2011-09-02 00:28:11 +02:00
|
|
|
static const char* kUrl;
|
2011-09-28 21:42:39 +02:00
|
|
|
static const char* kUrlCover;
|
2011-09-02 00:28:11 +02:00
|
|
|
|
|
|
|
static const int kSongSearchLimit;
|
2011-09-20 19:12:06 +02:00
|
|
|
static const int kSearchDelayMsec;
|
2011-09-02 00:28:11 +02:00
|
|
|
|
|
|
|
static const char* kApiKey;
|
|
|
|
static const char* kApiSecret;
|
|
|
|
|
2011-09-13 22:32:10 +02:00
|
|
|
signals:
|
|
|
|
void LoginFinished(bool success);
|
|
|
|
|
2011-09-02 00:28:11 +02:00
|
|
|
protected:
|
|
|
|
QModelIndex GetCurrentIndex();
|
|
|
|
|
2011-09-24 12:06:31 +02:00
|
|
|
struct PlaylistInfo {
|
|
|
|
PlaylistInfo() {}
|
|
|
|
PlaylistInfo(int id, QString name)
|
|
|
|
: id_(id), name_(name) {}
|
|
|
|
|
|
|
|
int id_;
|
|
|
|
QString name_;
|
|
|
|
};
|
|
|
|
|
2011-09-02 00:28:11 +02:00
|
|
|
private slots:
|
|
|
|
void UpdateTotalSongCount(int count);
|
|
|
|
|
2011-09-20 19:29:16 +02:00
|
|
|
void ShowConfig();
|
2011-09-13 22:32:10 +02:00
|
|
|
void SessionCreated();
|
2011-09-20 19:29:16 +02:00
|
|
|
void OpenSearchTab();
|
2011-09-20 19:12:06 +02:00
|
|
|
void DoSearch();
|
2011-09-02 00:28:11 +02:00
|
|
|
void SearchSongsFinished();
|
2011-09-13 22:32:10 +02:00
|
|
|
void Authenticated();
|
2011-09-24 12:06:31 +02:00
|
|
|
void UserPlaylistsRetrieved();
|
|
|
|
void PlaylistSongsRetrieved();
|
2011-09-02 00:28:11 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
void EnsureMenuCreated();
|
2011-09-22 19:54:59 +02:00
|
|
|
void EnsureItemsCreated();
|
2011-09-02 00:28:11 +02:00
|
|
|
void EnsureConnected();
|
|
|
|
|
2011-09-13 22:32:10 +02:00
|
|
|
void AuthenticateSession();
|
2011-09-20 00:26:24 +02:00
|
|
|
void InitCountry();
|
2011-09-02 00:28:11 +02:00
|
|
|
|
|
|
|
// 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
|
2011-09-20 00:26:24 +02:00
|
|
|
QNetworkReply* CreateRequest(const QString& method_name, const QList<QPair<QString, QVariant> > params,
|
2011-09-13 22:32:10 +02:00
|
|
|
bool need_authentication = false,
|
|
|
|
bool use_https = false);
|
2011-09-20 00:26:24 +02:00
|
|
|
// Convenient function for extracting result from reply
|
2011-09-13 22:32:10 +02:00
|
|
|
QVariantMap ExtractResult(QNetworkReply* reply);
|
2011-09-24 12:06:31 +02:00
|
|
|
// Convenient function for extracting songs from grooveshark result
|
|
|
|
SongList ExtractSongs(const QVariantMap& result);
|
2011-09-13 22:32:10 +02:00
|
|
|
void ResetSessionId();
|
|
|
|
|
2011-09-02 00:28:11 +02:00
|
|
|
|
2011-09-20 00:26:24 +02:00
|
|
|
GrooveSharkUrlHandler* url_handler_;
|
2011-09-24 12:06:31 +02:00
|
|
|
|
2011-09-20 19:12:06 +02:00
|
|
|
QString pending_search_;
|
2011-09-02 00:28:11 +02:00
|
|
|
Playlist* pending_search_playlist_;
|
|
|
|
|
2011-09-24 12:06:31 +02:00
|
|
|
QMap<QNetworkReply*, PlaylistInfo> pending_retrieve_playlists_;
|
|
|
|
|
2011-09-02 00:28:11 +02:00
|
|
|
QStandardItem* root_;
|
|
|
|
QStandardItem* search_;
|
|
|
|
|
|
|
|
NetworkAccessManager* network_;
|
|
|
|
|
|
|
|
QMenu* context_menu_;
|
|
|
|
QModelIndex context_item_;
|
|
|
|
|
2011-09-20 19:12:06 +02:00
|
|
|
QTimer* search_delay_;
|
|
|
|
QNetworkReply* last_search_reply_;
|
|
|
|
|
2011-09-13 22:32:10 +02:00
|
|
|
QString username_;
|
|
|
|
QString password_; // In fact, password's md5 hash
|
|
|
|
QString user_id_;
|
2011-09-02 00:28:11 +02:00
|
|
|
QString session_id_;
|
2011-09-20 00:26:24 +02:00
|
|
|
QMap<QString, QVariant> country_;
|
2011-09-02 00:28:11 +02:00
|
|
|
QByteArray api_key_;
|
|
|
|
|
2011-09-13 22:32:10 +02:00
|
|
|
LoginState login_state_;
|
2011-09-02 00:28:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // GROOVESHARKSERVICE_H
|