2010-06-23 15:21:30 +02:00
|
|
|
/* This file is part of Clementine.
|
2014-11-02 19:36:21 +01:00
|
|
|
Copyright 2010-2011, David Sansome <me@davidsansome.com>
|
|
|
|
Copyright 2011, Arnaud Bienner <arnaud.bienner@gmail.com>
|
2014-11-02 19:40:52 +01:00
|
|
|
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
2014-11-02 19:36:21 +01:00
|
|
|
Copyright 2014, John Maguire <john.maguire@gmail.com>
|
2010-06-23 15:21:30 +02:00
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "taskmanager.h"
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
TaskManager::TaskManager(QObject* parent) : QObject(parent), next_task_id_(1) {}
|
2010-06-23 15:21:30 +02:00
|
|
|
|
|
|
|
int TaskManager::StartTask(const QString& name) {
|
2011-07-21 01:22:20 +02:00
|
|
|
Task t;
|
2010-06-23 15:21:30 +02:00
|
|
|
t.name = name;
|
|
|
|
t.progress = 0;
|
|
|
|
t.progress_max = 0;
|
2010-06-25 00:59:29 +02:00
|
|
|
t.blocks_library_scans = false;
|
2010-06-23 15:21:30 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
QMutexLocker l(&mutex_);
|
2014-02-07 16:34:20 +01:00
|
|
|
t.id = next_task_id_++;
|
2010-06-23 15:21:30 +02:00
|
|
|
tasks_[t.id] = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit TasksChanged();
|
|
|
|
return t.id;
|
|
|
|
}
|
|
|
|
|
2011-07-21 01:22:20 +02:00
|
|
|
QList<TaskManager::Task> TaskManager::GetTasks() {
|
|
|
|
QList<TaskManager::Task> ret;
|
2010-06-23 15:21:30 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
QMutexLocker l(&mutex_);
|
|
|
|
ret = tasks_.values();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-06-25 00:59:29 +02:00
|
|
|
void TaskManager::SetTaskBlocksLibraryScans(int id) {
|
|
|
|
{
|
|
|
|
QMutexLocker l(&mutex_);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!tasks_.contains(id)) return;
|
2010-06-25 00:59:29 +02:00
|
|
|
|
2011-07-21 01:22:20 +02:00
|
|
|
Task& t = tasks_[id];
|
2010-06-25 00:59:29 +02:00
|
|
|
t.blocks_library_scans = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit TasksChanged();
|
|
|
|
emit PauseLibraryWatchers();
|
|
|
|
}
|
|
|
|
|
2010-06-23 15:21:30 +02:00
|
|
|
void TaskManager::SetTaskProgress(int id, int progress, int max) {
|
|
|
|
{
|
|
|
|
QMutexLocker l(&mutex_);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!tasks_.contains(id)) return;
|
2010-06-23 15:21:30 +02:00
|
|
|
|
2011-07-21 01:22:20 +02:00
|
|
|
Task& t = tasks_[id];
|
2010-06-23 15:21:30 +02:00
|
|
|
t.progress = progress;
|
2014-02-07 16:34:20 +01:00
|
|
|
if (max) t.progress_max = max;
|
2010-06-23 15:21:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
emit TasksChanged();
|
|
|
|
}
|
|
|
|
|
2011-11-23 01:07:40 +01:00
|
|
|
void TaskManager::IncreaseTaskProgress(int id, int progress, int max) {
|
|
|
|
{
|
|
|
|
QMutexLocker l(&mutex_);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!tasks_.contains(id)) return;
|
2011-11-23 01:07:40 +01:00
|
|
|
|
|
|
|
Task& t = tasks_[id];
|
|
|
|
t.progress += progress;
|
2014-02-07 16:34:20 +01:00
|
|
|
if (max) t.progress_max = max;
|
2011-11-23 01:07:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
emit TasksChanged();
|
|
|
|
}
|
|
|
|
|
2010-06-23 15:21:30 +02:00
|
|
|
void TaskManager::SetTaskFinished(int id) {
|
2010-06-25 00:59:29 +02:00
|
|
|
bool resume_library_watchers = false;
|
|
|
|
|
2010-06-23 15:21:30 +02:00
|
|
|
{
|
|
|
|
QMutexLocker l(&mutex_);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!tasks_.contains(id)) return;
|
2010-06-25 00:59:29 +02:00
|
|
|
|
|
|
|
if (tasks_[id].blocks_library_scans) {
|
|
|
|
resume_library_watchers = true;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const Task& task : tasks_.values()) {
|
2010-06-25 00:59:29 +02:00
|
|
|
if (task.id != id && task.blocks_library_scans) {
|
|
|
|
resume_library_watchers = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-23 15:21:30 +02:00
|
|
|
tasks_.remove(id);
|
|
|
|
}
|
2010-06-25 00:59:29 +02:00
|
|
|
|
2010-06-23 15:21:30 +02:00
|
|
|
emit TasksChanged();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (resume_library_watchers) emit ResumeLibraryWatchers();
|
2010-06-23 15:21:30 +02:00
|
|
|
}
|
2011-11-23 01:07:40 +01:00
|
|
|
|
|
|
|
int TaskManager::GetTaskProgress(int id) {
|
|
|
|
{
|
|
|
|
QMutexLocker l(&mutex_);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!tasks_.contains(id)) return 0;
|
2011-11-23 01:07:40 +01:00
|
|
|
return tasks_[id].progress;
|
|
|
|
}
|
|
|
|
}
|