1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/*
* Copyright (C) 2017-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "MusicLibraryQueue.h"
#include "GUIUserMessages.h"
#include "ServiceBroker.h"
#include "Util.h"
#include "dialogs/GUIDialogProgress.h"
#include "guilib/GUIComponent.h"
#include "guilib/GUIWindowManager.h"
#include "music/infoscanner/MusicInfoScanner.h"
#include "music/jobs/MusicLibraryCleaningJob.h"
#include "music/jobs/MusicLibraryExportJob.h"
#include "music/jobs/MusicLibraryImportJob.h"
#include "music/jobs/MusicLibraryJob.h"
#include "music/jobs/MusicLibraryScanningJob.h"
#include "settings/Settings.h"
#include "settings/SettingsComponent.h"
#include "utils/Variant.h"
#include <mutex>
#include <utility>
CMusicLibraryQueue::CMusicLibraryQueue()
: CJobQueue(false, 1, CJob::PRIORITY_LOW),
m_jobs()
{ }
CMusicLibraryQueue::~CMusicLibraryQueue()
{
std::unique_lock<CCriticalSection> lock(m_critical);
m_jobs.clear();
}
CMusicLibraryQueue& CMusicLibraryQueue::GetInstance()
{
static CMusicLibraryQueue s_instance;
return s_instance;
}
void CMusicLibraryQueue::ExportLibrary(const CLibExportSettings& settings, bool showDialog /* = false */)
{
CGUIDialogProgress* progress = NULL;
if (showDialog)
{
progress = CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogProgress>(WINDOW_DIALOG_PROGRESS);
if (progress)
{
progress->SetHeading(CVariant{ 20196 }); //"Export music library"
progress->SetText(CVariant{ 650 }); //"Exporting"
progress->SetPercentage(0);
progress->Open();
progress->ShowProgressBar(true);
}
}
CMusicLibraryExportJob* exportJob = new CMusicLibraryExportJob(settings, progress);
if (showDialog)
{
AddJob(exportJob);
// Wait for export to complete or be canceled, but render every 10ms so that the
// pointer movements work on dialog even when export is reporting progress infrequently
if (progress)
progress->Wait();
}
else
{
m_modal = true;
exportJob->DoWork();
delete exportJob;
m_modal = false;
Refresh();
}
}
void CMusicLibraryQueue::ImportLibrary(const std::string& xmlFile, bool showDialog /* = false */)
{
CGUIDialogProgress* progress = nullptr;
if (showDialog)
{
progress = CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogProgress>(WINDOW_DIALOG_PROGRESS);
if (progress)
{
progress->SetHeading(CVariant{ 20197 }); //"Import music library"
progress->SetText(CVariant{ 649 }); //"Importing"
progress->SetLine(1, CVariant{ 330 }); //"This could take some time"
progress->SetLine(2, CVariant{ "" });
progress->SetPercentage(0);
progress->Open();
progress->ShowProgressBar(true);
}
}
CMusicLibraryImportJob* importJob = new CMusicLibraryImportJob(xmlFile, progress);
if (showDialog)
{
AddJob(importJob);
// Wait for import to complete or be canceled, but render every 10ms so that the
// pointer movements work on dialog even when import is reporting progress infrequently
if (progress)
progress->Wait();
}
else
{
m_modal = true;
importJob->DoWork();
delete importJob;
m_modal = false;
Refresh();
}
}
void CMusicLibraryQueue::ScanLibrary(const std::string& strDirectory,
int flags /* = 0 */,
bool showProgress /* = true */)
{
if (flags == MUSIC_INFO::CMusicInfoScanner::SCAN_NORMAL)
{
if (CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(
CSettings::SETTING_MUSICLIBRARY_DOWNLOADINFO))
flags |= MUSIC_INFO::CMusicInfoScanner::SCAN_ONLINE;
}
if (!showProgress || CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(
CSettings::SETTING_MUSICLIBRARY_BACKGROUNDUPDATE))
flags |= MUSIC_INFO::CMusicInfoScanner::SCAN_BACKGROUND;
AddJob(new CMusicLibraryScanningJob(strDirectory, flags, showProgress));
}
void CMusicLibraryQueue::StartAlbumScan(const std::string & strDirectory, bool refresh)
{
int flags = MUSIC_INFO::CMusicInfoScanner::SCAN_ALBUMS;
if (refresh)
flags |= MUSIC_INFO::CMusicInfoScanner::SCAN_RESCAN;
AddJob(new CMusicLibraryScanningJob(strDirectory, flags, true));
}
void CMusicLibraryQueue::StartArtistScan(const std::string& strDirectory, bool refresh)
{
int flags = MUSIC_INFO::CMusicInfoScanner::SCAN_ARTISTS;
if (refresh)
flags |= MUSIC_INFO::CMusicInfoScanner::SCAN_RESCAN;
AddJob(new CMusicLibraryScanningJob(strDirectory, flags, true));
}
bool CMusicLibraryQueue::IsScanningLibrary() const
{
// check if the library is being cleaned synchronously
if (m_cleaning)
return true;
// check if the library is being scanned asynchronously
MusicLibraryJobMap::const_iterator scanningJobs = m_jobs.find("MusicLibraryScanningJob");
if (scanningJobs != m_jobs.end() && !scanningJobs->second.empty())
return true;
// check if the library is being cleaned asynchronously
MusicLibraryJobMap::const_iterator cleaningJobs = m_jobs.find("MusicLibraryCleaningJob");
if (cleaningJobs != m_jobs.end() && !cleaningJobs->second.empty())
return true;
return false;
}
void CMusicLibraryQueue::StopLibraryScanning()
{
std::unique_lock<CCriticalSection> lock(m_critical);
MusicLibraryJobMap::const_iterator scanningJobs = m_jobs.find("MusicLibraryScanningJob");
if (scanningJobs == m_jobs.end())
return;
// get a copy of the scanning jobs because CancelJob() will modify m_scanningJobs
MusicLibraryJobs tmpScanningJobs(scanningJobs->second.begin(), scanningJobs->second.end());
// cancel all scanning jobs
for (const auto& job : tmpScanningJobs)
CancelJob(job);
Refresh();
}
void CMusicLibraryQueue::CleanLibrary(bool showDialog /* = false */)
{
CGUIDialogProgress* progress = NULL;
if (showDialog)
{
progress = CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogProgress>(WINDOW_DIALOG_PROGRESS);
if (progress)
{
progress->SetHeading(CVariant{ 700 });
progress->SetPercentage(0);
progress->Open();
progress->ShowProgressBar(true);
}
}
CMusicLibraryCleaningJob* cleaningJob = new CMusicLibraryCleaningJob(progress);
AddJob(cleaningJob);
// Wait for cleaning to complete or be canceled, but render every 20ms so that the
// pointer movements work on dialog even when cleaning is reporting progress infrequently
if (progress)
progress->Wait(20);
}
void CMusicLibraryQueue::AddJob(CMusicLibraryJob *job)
{
if (job == NULL)
return;
std::unique_lock<CCriticalSection> lock(m_critical);
if (!CJobQueue::AddJob(job))
return;
// add the job to our list of queued/running jobs
std::string jobType = job->GetType();
MusicLibraryJobMap::iterator jobsIt = m_jobs.find(jobType);
if (jobsIt == m_jobs.end())
{
MusicLibraryJobs jobs;
jobs.insert(job);
m_jobs.insert(std::make_pair(jobType, jobs));
}
else
jobsIt->second.insert(job);
}
void CMusicLibraryQueue::CancelJob(CMusicLibraryJob *job)
{
if (job == NULL)
return;
std::unique_lock<CCriticalSection> lock(m_critical);
// remember the job type needed later because the job might be deleted
// in the call to CJobQueue::CancelJob()
std::string jobType;
if (job->GetType() != NULL)
jobType = job->GetType();
// check if the job supports cancellation and cancel it
if (job->CanBeCancelled())
job->Cancel();
// remove the job from the job queue
CJobQueue::CancelJob(job);
// remove the job from our list of queued/running jobs
MusicLibraryJobMap::iterator jobsIt = m_jobs.find(jobType);
if (jobsIt != m_jobs.end())
jobsIt->second.erase(job);
}
void CMusicLibraryQueue::CancelAllJobs()
{
std::unique_lock<CCriticalSection> lock(m_critical);
CJobQueue::CancelJobs();
// remove all scanning jobs
m_jobs.clear();
}
bool CMusicLibraryQueue::IsRunning() const
{
return CJobQueue::IsProcessing() || m_modal;
}
void CMusicLibraryQueue::Refresh()
{
CUtil::DeleteMusicDatabaseDirectoryCache();
CGUIMessage msg(GUI_MSG_NOTIFY_ALL, 0, 0, GUI_MSG_UPDATE);
CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(msg);
}
void CMusicLibraryQueue::OnJobComplete(unsigned int jobID, bool success, CJob *job)
{
if (success)
{
if (QueueEmpty())
Refresh();
}
{
std::unique_lock<CCriticalSection> lock(m_critical);
// remove the job from our list of queued/running jobs
MusicLibraryJobMap::iterator jobsIt = m_jobs.find(job->GetType());
if (jobsIt != m_jobs.end())
jobsIt->second.erase(static_cast<CMusicLibraryJob*>(job));
}
return CJobQueue::OnJobComplete(jobID, success, job);
}
|