Use task manager on most GS actions

This commit is contained in:
Arnaud Bienner 2011-11-23 01:07:40 +01:00
parent a047a30265
commit cc20e6b6ae
7 changed files with 164 additions and 45 deletions

View File

@ -24,11 +24,13 @@ Closure::Closure(QObject* sender,
QObject* receiver,
const char* slot,
const ClosureArgumentWrapper* val0,
const ClosureArgumentWrapper* val1)
const ClosureArgumentWrapper* val1,
const ClosureArgumentWrapper* val2)
: QObject(receiver),
callback_(NULL),
val0_(val0),
val1_(val1) {
val1_(val1),
val2_(val2) {
const QMetaObject* meta_receiver = receiver->metaObject();
QByteArray normalised_slot = QMetaObject::normalizedSignature(slot + 1);
@ -61,7 +63,8 @@ void Closure::Invoked() {
slot_.invoke(
parent(),
val0_ ? val0_->arg() : QGenericArgument(),
val1_ ? val1_->arg() : QGenericArgument());
val1_ ? val1_->arg() : QGenericArgument(),
val2_ ? val2_->arg() : QGenericArgument());
}
deleteLater();
}

View File

@ -55,7 +55,8 @@ class Closure : public QObject, boost::noncopyable {
Closure(QObject* sender, const char* signal,
QObject* receiver, const char* slot,
const ClosureArgumentWrapper* val0 = 0,
const ClosureArgumentWrapper* val1 = 0);
const ClosureArgumentWrapper* val1 = 0,
const ClosureArgumentWrapper* val2 = 0);
Closure(QObject* sender, const char* signal,
std::tr1::function<void()> callback);
@ -72,6 +73,7 @@ class Closure : public QObject, boost::noncopyable {
boost::scoped_ptr<const ClosureArgumentWrapper> val0_;
boost::scoped_ptr<const ClosureArgumentWrapper> val1_;
boost::scoped_ptr<const ClosureArgumentWrapper> val2_;
};
#define C_ARG(type, data) new ClosureArgument<type>(data)
@ -107,4 +109,18 @@ Closure* NewClosure(
C_ARG(T0, val0), C_ARG(T1, val1));
}
template <typename T0, typename T1, typename T2>
Closure* NewClosure(
QObject* sender,
const char* signal,
QObject* receiver,
const char* slot,
const T0& val0,
const T1& val1,
const T2& val2) {
return new Closure(
sender, signal, receiver, slot,
C_ARG(T0, val0), C_ARG(T1, val1), C_ARG(T2, val2));
}
#endif // CLOSURE_H

View File

@ -80,6 +80,21 @@ void TaskManager::SetTaskProgress(int id, int progress, int max) {
emit TasksChanged();
}
void TaskManager::IncreaseTaskProgress(int id, int progress, int max) {
{
QMutexLocker l(&mutex_);
if (!tasks_.contains(id))
return;
Task& t = tasks_[id];
t.progress += progress;
if (max)
t.progress_max = max;
}
emit TasksChanged();
}
void TaskManager::SetTaskFinished(int id) {
bool resume_library_watchers = false;
@ -105,3 +120,12 @@ void TaskManager::SetTaskFinished(int id) {
if (resume_library_watchers)
emit ResumeLibraryWatchers();
}
int TaskManager::GetTaskProgress(int id) {
{
QMutexLocker l(&mutex_);
if (!tasks_.contains(id))
return 0;
return tasks_[id].progress;
}
}

View File

@ -42,7 +42,9 @@ public:
int StartTask(const QString& name);
void SetTaskBlocksLibraryScans(int id);
void SetTaskProgress(int id, int progress, int max = 0);
void IncreaseTaskProgress(int id, int progress, int max = 0);
void SetTaskFinished(int id);
int GetTaskProgress(int id);
signals:
void TasksChanged();

View File

@ -90,7 +90,10 @@ GroovesharkService::GroovesharkService(InternetModel *parent)
search_delay_(new QTimer(this)),
last_search_reply_(NULL),
api_key_(QByteArray::fromBase64(kApiSecret)),
login_state_(LoginState_OtherError) {
login_state_(LoginState_OtherError),
task_popular_id_(0),
task_playlists_id_(0),
task_search_id_(0) {
model()->player()->RegisterUrlHandler(url_handler_);
model()->player()->playlists()->RegisterSpecialPlaylistType(new GroovesharkSearchPlaylistType(this));
@ -255,6 +258,10 @@ void GroovesharkService::GetAlbumSongsFinished(
}
void GroovesharkService::DoSearch() {
if (!task_search_id_) {
task_search_id_ = model()->task_manager()->StartTask(tr("Searching on Grooveshark"));
}
QList<Param> parameters;
parameters << Param("query", pending_search_)
@ -276,6 +283,8 @@ void GroovesharkService::SearchSongsFinished() {
SongList songs = ExtractSongs(result);
pending_search_playlist_->Clear();
pending_search_playlist_->InsertInternetItems(this, songs);
model()->task_manager()->SetTaskFinished(task_search_id_);
task_search_id_ = 0;
}
void GroovesharkService::InitCountry() {
@ -516,8 +525,7 @@ void GroovesharkService::EnsureItemsCreated() {
RetrieveUserFavorites();
RetrieveUserPlaylists();
RetrievePopularSongsMonth();
RetrievePopularSongsToday();
RetrievePopularSongs();
}
}
@ -541,6 +549,8 @@ QStandardItem* GroovesharkService::CreatePlaylistItem(const QString& playlist_na
}
void GroovesharkService::RetrieveUserPlaylists() {
task_playlists_id_ =
model()->task_manager()->StartTask(tr("Retrieving Grooveshark playlists"));
QNetworkReply* reply = CreateRequest("getUserPlaylists", QList<Param>());
connect(reply, SIGNAL(finished()), SLOT(UserPlaylistsRetrieved()));
@ -610,19 +620,22 @@ void GroovesharkService::PlaylistSongsRetrieved() {
playlist_info.songs_ids_ = ExtractSongsIds(result);
playlist_info.item_ = item;
playlists_.insert(playlist_info.id_, playlist_info);
if (pending_retrieve_playlists_.isEmpty()) {
model()->task_manager()->SetTaskFinished(task_playlists_id_);
}
}
void GroovesharkService::RetrieveUserFavorites() {
int task_id =
model()->task_manager()->StartTask(tr("Retrieving Grooveshark favorites songs"));
QNetworkReply* reply = CreateRequest("getUserFavoriteSongs", QList<Param>());
connect(reply, SIGNAL(finished()), SLOT(UserFavoritesRetrieved()));
NewClosure(reply, SIGNAL(finished()),
this, SLOT(UserFavoritesRetrieved(QNetworkReply*, int)), reply, task_id);
}
void GroovesharkService::UserFavoritesRetrieved() {
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
if (!reply)
return;
void GroovesharkService::UserFavoritesRetrieved(QNetworkReply* reply, int task_id) {
reply->deleteLater();
favorites_->removeRows(0, favorites_->rowCount());
@ -639,6 +652,14 @@ void GroovesharkService::UserFavoritesRetrieved() {
favorites_->appendRow(child);
}
model()->task_manager()->SetTaskFinished(task_id);
}
void GroovesharkService::RetrievePopularSongs() {
task_popular_id_ =
model()->task_manager()->StartTask(tr("Getting Grooveshark popular songs"));
RetrievePopularSongsMonth();
RetrievePopularSongsToday();
}
void GroovesharkService::RetrievePopularSongsMonth() {
@ -662,6 +683,11 @@ void GroovesharkService::PopularSongsMonthRetrieved(QNetworkReply* reply) {
popular_month_->appendRow(child);
}
model()->task_manager()->IncreaseTaskProgress(task_popular_id_, 50, 100);
if (model()->task_manager()->GetTaskProgress(task_popular_id_) >= 100) {
model()->task_manager()->SetTaskFinished(task_popular_id_);
}
}
void GroovesharkService::RetrievePopularSongsToday() {
@ -685,6 +711,11 @@ void GroovesharkService::PopularSongsTodayRetrieved(QNetworkReply* reply) {
popular_today_->appendRow(child);
}
model()->task_manager()->IncreaseTaskProgress(task_popular_id_, 50, 100);
if (model()->task_manager()->GetTaskProgress(task_popular_id_) >= 100) {
model()->task_manager()->SetTaskFinished(task_popular_id_);
}
}
void GroovesharkService::MarkStreamKeyOver30Secs(const QString& stream_key,
@ -875,6 +906,9 @@ void GroovesharkService::AddCurrentSongToPlaylist(QAction* action) {
}
void GroovesharkService::SetPlaylistSongs(int playlist_id, const QList<int>& songs_ids) {
int task_id =
model()->task_manager()->StartTask(tr("Update Grooveshark playlist"));
QList<Param> parameters;
// Convert song ids to QVariant
@ -889,12 +923,13 @@ void GroovesharkService::SetPlaylistSongs(int playlist_id, const QList<int>& son
QNetworkReply* reply = CreateRequest("setPlaylistSongs", parameters);
NewClosure(reply, SIGNAL(finished()),
this, SLOT(PlaylistSongsSet(QNetworkReply*, int)),
reply, playlist_id);
this, SLOT(PlaylistSongsSet(QNetworkReply*, int, int)),
reply, playlist_id, task_id);
}
void GroovesharkService::PlaylistSongsSet(QNetworkReply* reply, int playlist_id) {
void GroovesharkService::PlaylistSongsSet(QNetworkReply* reply, int playlist_id, int task_id) {
reply->deleteLater();
model()->task_manager()->SetTaskFinished(task_id);
QVariantMap result = ExtractResult(reply);
if (!result["success"].toBool()) {
@ -993,16 +1028,18 @@ void GroovesharkService::PlaylistDeleted(QNetworkReply* reply, int playlist_id)
}
void GroovesharkService::AddUserFavoriteSong(int song_id) {
int task_id = model()->task_manager()->StartTask(tr("Adding song to favorites"));
QList<Param> parameters;
parameters << Param("songID", song_id);
QNetworkReply* reply = CreateRequest("addUserFavoriteSong", parameters);
NewClosure(reply, SIGNAL(finished()),
this, SLOT(UserFavoriteSongAdded(QNetworkReply*)),
reply);
this, SLOT(UserFavoriteSongAdded(QNetworkReply*, int)),
reply, task_id);
}
void GroovesharkService::UserFavoriteSongAdded(QNetworkReply* reply) {
void GroovesharkService::UserFavoriteSongAdded(QNetworkReply* reply, int task_id) {
reply->deleteLater();
model()->task_manager()->SetTaskFinished(task_id);
QVariantMap result = ExtractResult(reply);
if (!result["success"].toBool()) {
@ -1049,14 +1086,16 @@ void GroovesharkService::RemoveCurrentFromFavorites() {
}
void GroovesharkService::RemoveFromFavorites(int song_id) {
int task_id = model()->task_manager()->StartTask(tr("Removing song from favorites"));
QList<Param> parameters;
parameters << Param("songIDs", QVariantList() << QVariant(song_id));
QNetworkReply* reply = CreateRequest("removeUserFavoriteSongs", parameters);
NewClosure(reply, SIGNAL(finished()), this,
SLOT(SongRemovedFromFavorites(QNetworkReply*)), reply);
SLOT(SongRemovedFromFavorites(QNetworkReply*, int)), reply, task_id);
}
void GroovesharkService::SongRemovedFromFavorites(QNetworkReply* reply) {
void GroovesharkService::SongRemovedFromFavorites(QNetworkReply* reply, int task_id) {
model()->task_manager()->SetTaskFinished(task_id);
reply->deleteLater();
QVariantMap result = ExtractResult(reply);

View File

@ -78,6 +78,7 @@ class GroovesharkService : public InternetService {
bool IsLoggedIn() const { return !session_id_.isEmpty(); }
void RetrieveUserPlaylists();
void RetrieveUserFavorites();
void RetrievePopularSongs();
void RetrievePopularSongsMonth();
void RetrievePopularSongsToday();
void SetPlaylistSongs(int playlist_id, const QList<int>& songs_ids);
@ -138,23 +139,23 @@ class GroovesharkService : public InternetService {
void GetAlbumSongsFinished(QNetworkReply* reply, int id);
void Authenticated();
void UserPlaylistsRetrieved();
void UserFavoritesRetrieved();
void UserFavoritesRetrieved(QNetworkReply* reply, int task_id);
void PopularSongsMonthRetrieved(QNetworkReply* reply);
void PopularSongsTodayRetrieved(QNetworkReply* reply);
void PlaylistSongsRetrieved();
void PlaylistSongsSet(QNetworkReply* reply, int playlist_id);
void PlaylistSongsSet(QNetworkReply* reply, int playlist_id, int task_id);
void CreateNewPlaylist();
void NewPlaylistCreated(QNetworkReply* reply, const QString& name);
void DeleteCurrentPlaylist();
void PlaylistDeleted(QNetworkReply* reply, int playlist_id);
void AddCurrentSongToUserFavorites() { AddUserFavoriteSong(current_song_id_); }
void AddCurrentSongToPlaylist(QAction* action);
void UserFavoriteSongAdded(QNetworkReply* reply);
void UserFavoriteSongAdded(QNetworkReply* reply, int task_id);
void GetCurrentSongUrlToShare();
void SongUrlToShareReceived(QNetworkReply* reply);
void RemoveCurrentFromPlaylist();
void RemoveCurrentFromFavorites();
void SongRemovedFromFavorites(QNetworkReply* reply);
void SongRemovedFromFavorites(QNetworkReply* reply, int task_id);
void StreamMarked();
void SongMarkedAsComplete();
@ -230,6 +231,12 @@ class GroovesharkService : public InternetService {
LoginState login_state_;
// 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;

View File

@ -419,11 +419,11 @@ msgstr ""
msgid "Add stream..."
msgstr ""
#: internet/groovesharkservice.cpp:802
#: internet/groovesharkservice.cpp:833
msgid "Add to Grooveshark favorites"
msgstr ""
#: internet/groovesharkservice.cpp:808
#: internet/groovesharkservice.cpp:839
msgid "Add to Grooveshark playlists"
msgstr ""
@ -467,6 +467,10 @@ msgstr ""
msgid "Added within three months"
msgstr ""
#: internet/groovesharkservice.cpp:1031
msgid "Adding song to favorites"
msgstr ""
#: ui_libraryfilterwidget.h:130
msgid "Advanced grouping..."
msgstr ""
@ -618,7 +622,7 @@ msgstr ""
msgid "Are you sure you want to delete the \"%1\" preset?"
msgstr ""
#: internet/groovesharkservice.cpp:968
#: internet/groovesharkservice.cpp:1003
msgid "Are you sure you want to delete this playlist?"
msgstr ""
@ -972,7 +976,7 @@ msgstr ""
msgid "Composer"
msgstr ""
#: internet/groovesharkservice.cpp:481
#: internet/groovesharkservice.cpp:490
msgid "Configure Grooveshark..."
msgstr ""
@ -1025,7 +1029,7 @@ msgstr ""
msgid "Convert any music that the device can't play"
msgstr ""
#: internet/groovesharkservice.cpp:855
#: internet/groovesharkservice.cpp:886
msgid "Copy to clipboard"
msgstr ""
@ -1104,7 +1108,7 @@ msgstr ""
msgid "Covers from %1"
msgstr ""
#: internet/groovesharkservice.cpp:466 internet/groovesharkservice.cpp:920
#: internet/groovesharkservice.cpp:475 internet/groovesharkservice.cpp:955
msgid "Create a new Grooveshark playlist"
msgstr ""
@ -1244,7 +1248,7 @@ msgstr ""
msgid "Delay between visualizations"
msgstr ""
#: internet/groovesharkservice.cpp:469 internet/groovesharkservice.cpp:967
#: internet/groovesharkservice.cpp:478 internet/groovesharkservice.cpp:1002
msgid "Delete Grooveshark playlist"
msgstr ""
@ -1671,7 +1675,7 @@ msgstr ""
msgid "Fast"
msgstr ""
#: internet/groovesharkservice.cpp:508
#: internet/groovesharkservice.cpp:517
msgid "Favorites"
msgstr ""
@ -1846,10 +1850,14 @@ msgstr ""
msgid "Genre"
msgstr ""
#: internet/groovesharkservice.cpp:819
#: internet/groovesharkservice.cpp:850
msgid "Get an URL to share this Grooveshark song"
msgstr ""
#: internet/groovesharkservice.cpp:660
msgid "Getting Grooveshark popular songs"
msgstr ""
#: internet/somafmservice.cpp:94
msgid "Getting channels"
msgstr ""
@ -1892,11 +1900,11 @@ msgstr ""
msgid "Grooveshark"
msgstr ""
#: internet/groovesharkservice.cpp:393
#: internet/groovesharkservice.cpp:402
msgid "Grooveshark login error"
msgstr ""
#: internet/groovesharkservice.cpp:851
#: internet/groovesharkservice.cpp:882
msgid "Grooveshark song's URL"
msgstr ""
@ -2091,7 +2099,7 @@ msgstr ""
msgid "Invalid session key"
msgstr ""
#: internet/groovesharkservice.cpp:386
#: internet/groovesharkservice.cpp:395
msgid "Invalid username and/or password"
msgstr ""
@ -2559,7 +2567,7 @@ msgstr ""
msgid "My Recommendations"
msgstr ""
#: internet/groovesharkservice.cpp:921 ui/equalizer.cpp:172
#: internet/groovesharkservice.cpp:956 ui/equalizer.cpp:172
#: ui_deviceproperties.h:369 ui_magnatunedownloaddialog.h:135
#: ui_wizardfinishpage.h:84 ui_globalshortcutssettingspage.h:174
msgid "Name"
@ -2937,11 +2945,11 @@ msgstr ""
msgid "Pop"
msgstr ""
#: internet/groovesharkservice.cpp:494
#: internet/groovesharkservice.cpp:503
msgid "Popular songs of the Month"
msgstr ""
#: internet/groovesharkservice.cpp:501
#: internet/groovesharkservice.cpp:510
msgid "Popular songs today"
msgstr ""
@ -3153,11 +3161,11 @@ msgstr ""
msgid "Remove folder"
msgstr ""
#: internet/groovesharkservice.cpp:476
#: internet/groovesharkservice.cpp:485
msgid "Remove from favorites"
msgstr ""
#: internet/groovesharkservice.cpp:473 ui_mainwindow.h:674
#: internet/groovesharkservice.cpp:482 ui_mainwindow.h:674
msgid "Remove from playlist"
msgstr ""
@ -3165,6 +3173,10 @@ msgstr ""
msgid "Remove playlist"
msgstr ""
#: internet/groovesharkservice.cpp:1089
msgid "Removing song from favorites"
msgstr ""
#: playlist/playlisttabbar.cpp:131
msgid "Rename playlist"
msgstr ""
@ -3239,6 +3251,14 @@ msgstr ""
msgid "Results"
msgstr ""
#: internet/groovesharkservice.cpp:631
msgid "Retrieving Grooveshark favorites songs"
msgstr ""
#: internet/groovesharkservice.cpp:553
msgid "Retrieving Grooveshark playlists"
msgstr ""
#: ui/equalizer.cpp:121
msgid "Rock"
msgstr ""
@ -3312,11 +3332,11 @@ msgid "Search"
msgstr ""
#: internet/groovesharksearchplaylisttype.cpp:32
#: internet/groovesharkservice.cpp:737
#: internet/groovesharkservice.cpp:768
msgid "Search Grooveshark"
msgstr ""
#: internet/groovesharkservice.cpp:479 internet/groovesharkservice.cpp:488
#: internet/groovesharkservice.cpp:488 internet/groovesharkservice.cpp:497
msgid "Search Grooveshark (opens a new tab)"
msgstr ""
@ -3368,6 +3388,10 @@ msgstr ""
msgid "Search terms"
msgstr ""
#: internet/groovesharkservice.cpp:262
msgid "Searching on Grooveshark"
msgstr ""
#: ui_groupbydialog.h:138
msgid "Second level"
msgstr ""
@ -4058,6 +4082,10 @@ msgstr ""
msgid "Unset cover"
msgstr ""
#: internet/groovesharkservice.cpp:910
msgid "Update Grooveshark playlist"
msgstr ""
#: ui_mainwindow.h:688
msgid "Update changed library folders"
msgstr ""
@ -4132,7 +4160,7 @@ msgstr ""
msgid "Used"
msgstr ""
#: internet/groovesharkservice.cpp:389
#: internet/groovesharkservice.cpp:398
#, qt-format
msgid "User %1 doesn't have a Grooveshark Anywhere account"
msgstr ""