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.
9 #include "VideoLibraryQueue.h"
11 #include "GUIUserMessages.h"
12 #include "ServiceBroker.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"
26 CVideoLibraryQueue::CVideoLibraryQueue()
27 : CJobQueue(false, 1, CJob::PRIORITY_LOW
),
31 CVideoLibraryQueue::~CVideoLibraryQueue()
33 std::unique_lock
<CCriticalSection
> lock(m_critical
);
37 CVideoLibraryQueue
& CVideoLibraryQueue::GetInstance()
39 static CVideoLibraryQueue 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
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())
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())
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())
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
)
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
);
93 // we can't perform a modal library cleaning if other jobs are running
99 cleaningJob
->DoWork();
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
118 CVideoLibraryCleaningJob
cleaningJob(paths
, true);
119 cleaningJob
.DoWork();
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
,
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
146 CVideoLibraryRefreshingJob
refreshingJob(std::move(item
), forceRefresh
, refreshAll
);
148 bool result
= refreshingJob
.DoModal();
154 void CVideoLibraryQueue::MarkAsWatched(const std::shared_ptr
<CFileItem
>& item
, bool watched
)
159 AddJob(new CVideoLibraryMarkWatchedJob(item
, watched
));
162 void CVideoLibraryQueue::ResetResumePoint(const std::shared_ptr
<CFileItem
>& item
)
167 AddJob(new CVideoLibraryResetResumePointJob(item
));
170 void CVideoLibraryQueue::AddJob(CVideoLibraryJob
*job
)
175 std::unique_lock
<CCriticalSection
> lock(m_critical
);
176 if (!CJobQueue::AddJob(job
))
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
;
186 m_jobs
.insert(std::make_pair(jobType
, jobs
));
189 jobsIt
->second
.insert(job
);
192 void CVideoLibraryQueue::CancelJob(CVideoLibraryJob
*job
)
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()
201 if (job
->GetType() != NULL
)
202 jobType
= job
->GetType();
204 // check if the job supports cancellation and cancel it
205 if (job
->CanBeCancelled())
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
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
)
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
);