1 /* This file is part of Clementine.
2    Copyright 2010-2011, David Sansome <me@davidsansome.com>
3    Copyright 2011, Arnaud Bienner <arnaud.bienner@gmail.com>
4    Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
5    Copyright 2014, John Maguire <john.maguire@gmail.com>
6 
7    Clementine is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11 
12    Clementine is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with Clementine.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "taskmanager.h"
22 
TaskManager(QObject * parent)23 TaskManager::TaskManager(QObject* parent) : QObject(parent), next_task_id_(1) {}
24 
StartTask(const QString & name)25 int TaskManager::StartTask(const QString& name) {
26   Task t;
27   t.name = name;
28   t.progress = 0;
29   t.progress_max = 0;
30   t.blocks_library_scans = false;
31 
32   {
33     QMutexLocker l(&mutex_);
34     t.id = next_task_id_++;
35     tasks_[t.id] = t;
36   }
37 
38   emit TasksChanged();
39   return t.id;
40 }
41 
GetTasks()42 QList<TaskManager::Task> TaskManager::GetTasks() {
43   QList<TaskManager::Task> ret;
44 
45   {
46     QMutexLocker l(&mutex_);
47     ret = tasks_.values();
48   }
49 
50   return ret;
51 }
52 
SetTaskBlocksLibraryScans(int id)53 void TaskManager::SetTaskBlocksLibraryScans(int id) {
54   {
55     QMutexLocker l(&mutex_);
56     if (!tasks_.contains(id)) return;
57 
58     Task& t = tasks_[id];
59     t.blocks_library_scans = true;
60   }
61 
62   emit TasksChanged();
63   emit PauseLibraryWatchers();
64 }
65 
SetTaskProgress(int id,int progress,int max)66 void TaskManager::SetTaskProgress(int id, int progress, int max) {
67   {
68     QMutexLocker l(&mutex_);
69     if (!tasks_.contains(id)) return;
70 
71     Task& t = tasks_[id];
72     t.progress = progress;
73     if (max) t.progress_max = max;
74   }
75 
76   emit TasksChanged();
77 }
78 
IncreaseTaskProgress(int id,int progress,int max)79 void TaskManager::IncreaseTaskProgress(int id, int progress, int max) {
80   {
81     QMutexLocker l(&mutex_);
82     if (!tasks_.contains(id)) return;
83 
84     Task& t = tasks_[id];
85     t.progress += progress;
86     if (max) t.progress_max = max;
87   }
88 
89   emit TasksChanged();
90 }
91 
SetTaskFinished(int id)92 void TaskManager::SetTaskFinished(int id) {
93   bool resume_library_watchers = false;
94 
95   {
96     QMutexLocker l(&mutex_);
97     if (!tasks_.contains(id)) return;
98 
99     if (tasks_[id].blocks_library_scans) {
100       resume_library_watchers = true;
101       for (const Task& task : tasks_.values()) {
102         if (task.id != id && task.blocks_library_scans) {
103           resume_library_watchers = false;
104           break;
105         }
106       }
107     }
108 
109     tasks_.remove(id);
110   }
111 
112   emit TasksChanged();
113   if (resume_library_watchers) emit ResumeLibraryWatchers();
114 }
115 
GetTaskProgress(int id)116 int TaskManager::GetTaskProgress(int id) {
117   {
118     QMutexLocker l(&mutex_);
119     if (!tasks_.contains(id)) return 0;
120     return tasks_[id].progress;
121   }
122 }
123