strawberry-audio-player-win.../src/core/taskmanager.cpp

146 lines
3.1 KiB
C++
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
2021-03-20 21:14:47 +01:00
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
2018-02-27 18:06:05 +01:00
*
* Strawberry 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.
*
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#include "config.h"
2021-06-21 19:52:37 +02:00
#include <algorithm>
#include <QObject>
#include <QMutex>
#include <QList>
#include <QString>
2018-02-27 18:06:05 +01:00
#include "taskmanager.h"
TaskManager::TaskManager(QObject *parent) : QObject(parent), next_task_id_(1) {}
int TaskManager::StartTask(const QString &name) {
Task t;
t.name = name;
t.progress = 0;
t.progress_max = 0;
t.blocks_collection_scans = false;
{
QMutexLocker l(&mutex_);
t.id = next_task_id_++;
tasks_[t.id] = t;
}
emit TasksChanged();
return t.id;
}
QList<TaskManager::Task> TaskManager::GetTasks() {
QList<TaskManager::Task> ret;
{
QMutexLocker l(&mutex_);
ret = tasks_.values();
}
return ret;
}
void TaskManager::SetTaskBlocksCollectionScans(const int id) {
2018-02-27 18:06:05 +01:00
{
QMutexLocker l(&mutex_);
if (!tasks_.contains(id)) return;
Task &t = tasks_[id];
t.blocks_collection_scans = true;
}
emit TasksChanged();
emit PauseCollectionWatchers();
}
2021-10-30 02:21:29 +02:00
void TaskManager::SetTaskProgress(const int id, const quint64 progress, const quint64 max) {
2018-02-27 18:06:05 +01:00
{
QMutexLocker l(&mutex_);
if (!tasks_.contains(id)) return;
Task &t = tasks_[id];
t.progress = progress;
2021-06-22 13:54:58 +02:00
if (max > 0) t.progress_max = max;
2018-02-27 18:06:05 +01:00
}
emit TasksChanged();
}
2021-10-30 02:21:29 +02:00
void TaskManager::IncreaseTaskProgress(const int id, const quint64 progress, const quint64 max) {
2018-02-27 18:06:05 +01:00
{
QMutexLocker l(&mutex_);
if (!tasks_.contains(id)) return;
Task &t = tasks_[id];
t.progress += progress;
2021-06-22 13:54:58 +02:00
if (max > 0) t.progress_max = max;
2018-02-27 18:06:05 +01:00
}
emit TasksChanged();
}
void TaskManager::SetTaskFinished(const int id) {
2018-02-27 18:06:05 +01:00
bool resume_collection_watchers = false;
{
QMutexLocker l(&mutex_);
if (!tasks_.contains(id)) return;
if (tasks_[id].blocks_collection_scans) {
resume_collection_watchers = true;
2021-03-21 04:47:11 +01:00
QList<Task> tasks = tasks_.values();
2021-06-21 19:52:37 +02:00
if (std::any_of(tasks.begin(), tasks.end(), [id](const Task &task) { return task.id != id && task.blocks_collection_scans; })) {
resume_collection_watchers = false;
2018-02-27 18:06:05 +01:00
}
2021-06-21 19:52:37 +02:00
2018-02-27 18:06:05 +01:00
}
tasks_.remove(id);
}
emit TasksChanged();
if (resume_collection_watchers) emit ResumeCollectionWatchers();
}
2021-10-30 02:21:29 +02:00
quint64 TaskManager::GetTaskProgress(int id) {
2018-02-27 18:06:05 +01:00
{
QMutexLocker l(&mutex_);
if (!tasks_.contains(id)) return 0;
return tasks_[id].progress;
}
}