[PVR][Estuary] Timer settings dialog: Show client name in timer type selection dialog...
[xbmc.git] / xbmc / video / VideoLibraryQueue.cpp
blob65e65bbe1b8441605cc0436ce8783fb570f05e3a
1 /*
2 * Copyright (C) 2014-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "VideoLibraryQueue.h"
11 #include "GUIUserMessages.h"
12 #include "ServiceBroker.h"
13 #include "Util.h"
14 #include "guilib/GUIComponent.h"
15 #include "guilib/GUIWindowManager.h"
16 #include "video/jobs/VideoLibraryCleaningJob.h"
17 #include "video/jobs/VideoLibraryJob.h"
18 #include "video/jobs/VideoLibraryMarkWatchedJob.h"
19 #include "video/jobs/VideoLibraryRefreshingJob.h"
20 #include "video/jobs/VideoLibraryResetResumePointJob.h"
21 #include "video/jobs/VideoLibraryScanningJob.h"
23 #include <mutex>
24 #include <utility>
26 CVideoLibraryQueue::CVideoLibraryQueue()
27 : CJobQueue(false, 1, CJob::PRIORITY_LOW),
28 m_jobs()
29 { }
31 CVideoLibraryQueue::~CVideoLibraryQueue()
33 std::unique_lock<CCriticalSection> lock(m_critical);
34 m_jobs.clear();
37 CVideoLibraryQueue& CVideoLibraryQueue::GetInstance()
39 static CVideoLibraryQueue s_instance;
40 return s_instance;
43 void CVideoLibraryQueue::ScanLibrary(const std::string& directory, bool scanAll /* = false */ , bool showProgress /* = true */)
45 AddJob(new CVideoLibraryScanningJob(directory, scanAll, showProgress));
48 bool CVideoLibraryQueue::IsScanningLibrary() const
50 // check if the library is being cleaned synchronously
51 if (m_cleaning)
52 return true;
54 // check if the library is being scanned asynchronously
55 VideoLibraryJobMap::const_iterator scanningJobs = m_jobs.find("VideoLibraryScanningJob");
56 if (scanningJobs != m_jobs.end() && !scanningJobs->second.empty())
57 return true;
59 // check if the library is being cleaned asynchronously
60 VideoLibraryJobMap::const_iterator cleaningJobs = m_jobs.find("VideoLibraryCleaningJob");
61 if (cleaningJobs != m_jobs.end() && !cleaningJobs->second.empty())
62 return true;
64 return false;
67 void CVideoLibraryQueue::StopLibraryScanning()
69 std::unique_lock<CCriticalSection> lock(m_critical);
70 VideoLibraryJobMap::const_iterator scanningJobs = m_jobs.find("VideoLibraryScanningJob");
71 if (scanningJobs == m_jobs.end())
72 return;
74 // get a copy of the scanning jobs because CancelJob() will modify m_scanningJobs
75 VideoLibraryJobs tmpScanningJobs(scanningJobs->second.begin(), scanningJobs->second.end());
77 // cancel all scanning jobs
78 for (VideoLibraryJobs::const_iterator job = tmpScanningJobs.begin(); job != tmpScanningJobs.end(); ++job)
79 CancelJob(*job);
80 Refresh();
83 bool CVideoLibraryQueue::CleanLibrary(const std::set<int>& paths /* = std::set<int>() */,
84 bool asynchronous /* = true */,
85 CGUIDialogProgressBarHandle* progressBar /* = NULL */)
87 CVideoLibraryCleaningJob* cleaningJob = new CVideoLibraryCleaningJob(paths, progressBar);
89 if (asynchronous)
90 AddJob(cleaningJob);
91 else
93 // we can't perform a modal library cleaning if other jobs are running
94 if (IsRunning())
95 return false;
97 m_modal = true;
98 m_cleaning = true;
99 cleaningJob->DoWork();
101 delete cleaningJob;
102 m_cleaning = false;
103 m_modal = false;
104 Refresh();
107 return true;
110 bool CVideoLibraryQueue::CleanLibraryModal(const std::set<int>& paths /* = std::set<int>() */)
112 // we can't perform a modal library cleaning if other jobs are running
113 if (IsRunning())
114 return false;
116 m_modal = true;
117 m_cleaning = true;
118 CVideoLibraryCleaningJob cleaningJob(paths, true);
119 cleaningJob.DoWork();
120 m_cleaning = false;
121 m_modal = false;
122 Refresh();
124 return true;
127 void CVideoLibraryQueue::RefreshItem(std::shared_ptr<CFileItem> item,
128 bool ignoreNfo /* = false */,
129 bool forceRefresh /* = true */,
130 bool refreshAll /* = false */,
131 const std::string& searchTitle /* = "" */)
133 AddJob(new CVideoLibraryRefreshingJob(std::move(item), forceRefresh, refreshAll, ignoreNfo,
134 searchTitle));
137 bool CVideoLibraryQueue::RefreshItemModal(std::shared_ptr<CFileItem> item,
138 bool forceRefresh /* = true */,
139 bool refreshAll /* = false */)
141 // we can't perform a modal item refresh if other jobs are running
142 if (IsRunning())
143 return false;
145 m_modal = true;
146 CVideoLibraryRefreshingJob refreshingJob(std::move(item), forceRefresh, refreshAll);
148 bool result = refreshingJob.DoModal();
149 m_modal = false;
151 return result;
154 void CVideoLibraryQueue::MarkAsWatched(const std::shared_ptr<CFileItem>& item, bool watched)
156 if (item == NULL)
157 return;
159 AddJob(new CVideoLibraryMarkWatchedJob(item, watched));
162 void CVideoLibraryQueue::ResetResumePoint(const std::shared_ptr<CFileItem>& item)
164 if (item == nullptr)
165 return;
167 AddJob(new CVideoLibraryResetResumePointJob(item));
170 void CVideoLibraryQueue::AddJob(CVideoLibraryJob *job)
172 if (job == NULL)
173 return;
175 std::unique_lock<CCriticalSection> lock(m_critical);
176 if (!CJobQueue::AddJob(job))
177 return;
179 // add the job to our list of queued/running jobs
180 std::string jobType = job->GetType();
181 VideoLibraryJobMap::iterator jobsIt = m_jobs.find(jobType);
182 if (jobsIt == m_jobs.end())
184 VideoLibraryJobs jobs;
185 jobs.insert(job);
186 m_jobs.insert(std::make_pair(jobType, jobs));
188 else
189 jobsIt->second.insert(job);
192 void CVideoLibraryQueue::CancelJob(CVideoLibraryJob *job)
194 if (job == NULL)
195 return;
197 std::unique_lock<CCriticalSection> lock(m_critical);
198 // remember the job type needed later because the job might be deleted
199 // in the call to CJobQueue::CancelJob()
200 std::string jobType;
201 if (job->GetType() != NULL)
202 jobType = job->GetType();
204 // check if the job supports cancellation and cancel it
205 if (job->CanBeCancelled())
206 job->Cancel();
208 // remove the job from the job queue
209 CJobQueue::CancelJob(job);
211 // remove the job from our list of queued/running jobs
212 VideoLibraryJobMap::iterator jobsIt = m_jobs.find(jobType);
213 if (jobsIt != m_jobs.end())
214 jobsIt->second.erase(job);
217 void CVideoLibraryQueue::CancelAllJobs()
219 std::unique_lock<CCriticalSection> lock(m_critical);
220 CJobQueue::CancelJobs();
222 // remove all scanning jobs
223 m_jobs.clear();
226 bool CVideoLibraryQueue::IsRunning() const
228 return CJobQueue::IsProcessing() || m_modal;
231 void CVideoLibraryQueue::Refresh()
233 CUtil::DeleteVideoDatabaseDirectoryCache();
234 CGUIMessage msg(GUI_MSG_NOTIFY_ALL, 0, 0, GUI_MSG_UPDATE);
235 CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(msg);
238 void CVideoLibraryQueue::OnJobComplete(unsigned int jobID, bool success, CJob *job)
240 if (success)
242 if (QueueEmpty())
243 Refresh();
247 std::unique_lock<CCriticalSection> lock(m_critical);
248 // remove the job from our list of queued/running jobs
249 VideoLibraryJobMap::iterator jobsIt = m_jobs.find(job->GetType());
250 if (jobsIt != m_jobs.end())
251 jobsIt->second.erase(static_cast<CVideoLibraryJob*>(job));
254 return CJobQueue::OnJobComplete(jobID, success, job);