From c5b74c832e94532b4e3895251346cc0b67a13dbd Mon Sep 17 00:00:00 2001 From: John Maguire Date: Mon, 6 Aug 2012 14:00:54 +0200 Subject: [PATCH] Add tasks for indexing Google Drive songs. --- src/core/taskmanager.h | 20 ++++++++++++++++++++ src/internet/googledriveservice.cpp | 14 +++++++++++--- src/internet/googledriveservice.h | 5 ++++- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/core/taskmanager.h b/src/core/taskmanager.h index 3e2f753c1..4a240c1b5 100644 --- a/src/core/taskmanager.h +++ b/src/core/taskmanager.h @@ -36,6 +36,24 @@ public: bool blocks_library_scans; }; + class ScopedTask { + public: + ScopedTask(const int task_id, TaskManager* task_manager) + : task_id_(task_id), + task_manager_(task_manager) { + } + + ~ScopedTask() { + task_manager_->SetTaskFinished(task_id_); + } + + private: + const int task_id_; + TaskManager* task_manager_; + + Q_DISABLE_COPY(ScopedTask); + }; + // Everything here is thread safe QList GetTasks(); @@ -56,6 +74,8 @@ private: QMutex mutex_; QMap tasks_; int next_task_id_; + + Q_DISABLE_COPY(TaskManager); }; #endif // TASKMANAGER_H diff --git a/src/internet/googledriveservice.cpp b/src/internet/googledriveservice.cpp index 1ea995f41..a6111988c 100644 --- a/src/internet/googledriveservice.cpp +++ b/src/internet/googledriveservice.cpp @@ -9,6 +9,7 @@ #include "core/database.h" #include "core/mergedproxymodel.h" #include "core/player.h" +#include "core/taskmanager.h" #include "core/timeconstants.h" #include "globalsearch/globalsearch.h" #include "globalsearch/librarysearchprovider.h" @@ -33,6 +34,7 @@ GoogleDriveService::GoogleDriveService(Application* app, InternetModel* parent) : InternetService(kServiceName, app, parent, parent), root_(NULL), client_(new google_drive::Client(this)), + task_manager_(app->task_manager()), library_sort_model_(new QSortFilterProxyModel(this)) { library_backend_ = new LibraryBackend; library_backend_->moveToThread(app_->database()->thread()); @@ -127,6 +129,9 @@ void GoogleDriveService::MaybeAddFileToDatabase(const google_drive::File& file) return; } + const int task_id = task_manager_->StartTask( + tr("Indexing %1").arg(file.title())); + // Song not in index; tag and add. TagReaderClient::ReplyType* reply = app_->tag_reader_client()->ReadGoogleDrive( file.download_url(), @@ -136,15 +141,18 @@ void GoogleDriveService::MaybeAddFileToDatabase(const google_drive::File& file) client_->access_token()); NewClosure(reply, SIGNAL(Finished(bool)), - this, SLOT(ReadTagsFinished(TagReaderClient::ReplyType*,google_drive::File,QString)), - reply, file, url); + this, SLOT(ReadTagsFinished(TagReaderClient::ReplyType*,google_drive::File,QString,int)), + reply, file, url, task_id); } void GoogleDriveService::ReadTagsFinished(TagReaderClient::ReplyType* reply, const google_drive::File& metadata, - const QString& url) { + const QString& url, + const int task_id) { reply->deleteLater(); + TaskManager::ScopedTask(task_id, task_manager_); + const pb::tagreader::ReadGoogleDriveResponse& msg = reply->message().read_google_drive_response(); if (!msg.has_metadata() || !msg.metadata().filesize()) { diff --git a/src/internet/googledriveservice.h b/src/internet/googledriveservice.h index 7c50d13a7..a253e3fd8 100644 --- a/src/internet/googledriveservice.h +++ b/src/internet/googledriveservice.h @@ -10,6 +10,7 @@ class QStandardItem; class LibraryBackend; class LibraryModel; +class TaskManager; class QSortFilterProxyModel; namespace google_drive { @@ -40,7 +41,8 @@ class GoogleDriveService : public InternetService { void ListFilesFinished(google_drive::ListFilesResponse* response); void ReadTagsFinished(TagReaderClient::ReplyType* reply, const google_drive::File& metadata, - const QString& url); + const QString& url, + const int task_id); private: void Connect(); @@ -53,6 +55,7 @@ class GoogleDriveService : public InternetService { google_drive::Client* client_; NetworkAccessManager network_; + TaskManager* task_manager_; LibraryBackend* library_backend_; LibraryModel* library_model_;