1 /*
2  *  Copyright (C) 2017-2018 Team Kodi
3  *  This file is part of Kodi - https://kodi.tv
4  *
5  *  SPDX-License-Identifier: GPL-2.0-or-later
6  *  See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "FileItem.h"
12 #include "settings/LibExportSettings.h"
13 #include "threads/CriticalSection.h"
14 #include "utils/JobManager.h"
15 
16 #include <map>
17 #include <set>
18 
19 class CGUIDialogProgressBarHandle;
20 class CMusicLibraryJob;
21 class CGUIDialogProgress;
22 
23 /*!
24  \brief Queue for music library jobs.
25 
26  The queue can only process a single job at any time and every job will be
27  executed at the lowest priority.
28  */
29 class CMusicLibraryQueue : protected CJobQueue
30 {
31 public:
32   ~CMusicLibraryQueue() override;
33 
34   /*!
35    \brief Gets the singleton instance of the music library queue.
36   */
37   static CMusicLibraryQueue& GetInstance();
38 
39   /*!
40    \brief Enqueue a music library export job.
41    \param[in] settings   Library export settings
42    \param[in] showDialog Show a progress dialog while (asynchronously) exporting, otherwise export in synchronous
43   */
44   void ExportLibrary(const CLibExportSettings& settings, bool showDialog = false);
45 
46   /*!
47   \brief Enqueue a music library import job.
48   \param[in] xmlFile    xml file to import
49   \param[in] showDialog Show a progress dialog while (asynchronously) exporting, otherwise export in synchronous
50   */
51   void ImportLibrary(const std::string& xmlFile, bool showDialog = false);
52 
53   /*!
54    \brief Enqueue a music library update job, scanning tags embedded in music files and optionally scraping additional data.
55    \param[in] strDirectory Directory to scan or "" (empty string) for a global scan.
56    \param[in] flags Flags for controlling the scanning process.  See xbmc/music/infoscanner/MusicInfoScanner.h for possible values.
57    \param[in] showProgress Whether or not to show a progress dialog. Defaults to true
58    */
59   void ScanLibrary(const std::string& strDirectory, int flags = 0, bool showProgress = true);
60 
61   /*!
62    \brief Enqueue an album scraping job fetching additional album data.
63    \param[in] strDirectory Virtual path that identifies which albums to process or "" (empty string) for all albums
64    \param[in] refresh Whether or not to refresh data for albums that have previously been scraped
65   */
66   void StartAlbumScan(const std::string& strDirectory, bool refresh = false);
67 
68   /*!
69    \brief Enqueue an artist scraping job fetching additional artist data.
70    \param[in] strDirectory Virtual path that identifies which artists to process or "" (empty string) for all artists
71    \param[in] refresh Whether or not to refresh data for artists that have previously been scraped
72    */
73   void StartArtistScan(const std::string& strDirectory, bool refresh = false);
74 
75   /*!
76    \brief Check if a library scan or cleaning is in progress.
77    \return True if a scan or clean is in progress, false otherwise
78    */
79   bool IsScanningLibrary() const;
80 
81   /*!
82    \brief Stop and dequeue all scanning jobs.
83    */
84   void StopLibraryScanning();
85 
86   /*!
87    \brief Enqueue an asynchronous library cleaning job.
88    \param[in] showDialog Show a model progress dialog while cleaning. Default is false.
89    */
90   void CleanLibrary(bool showDialog = false);
91 
92   /*!
93    \brief Executes a library cleaning with a modal dialog.
94    However UI rendering of dialog is on same thread as the cleaning process, so mouse movement
95    is stilted and opportunities to cancel the process limited
96    */
97   void CleanLibraryModal();
98 
99   /*!
100    \brief Adds the given job to the queue.
101    \param[in] job Music library job to be queued.
102    */
103   void AddJob(CMusicLibraryJob *job);
104 
105   /*!
106    \brief Cancels the given job and removes it from the queue.
107    \param[in] job Music library job to be canceled and removed from the queue.
108    */
109   void CancelJob(CMusicLibraryJob *job);
110 
111   /*!
112    \brief Cancels all running and queued jobs.
113    */
114   void CancelAllJobs();
115 
116   /*!
117    \brief Whether any jobs are running or not.
118    */
119   bool IsRunning() const;
120 
121 protected:
122   // implementation of IJobCallback
123   void OnJobComplete(unsigned int jobID, bool success, CJob *job) override;
124 
125   /*!
126    \brief Notifies all to refresh the current listings.
127    */
128   void Refresh();
129 
130 private:
131   CMusicLibraryQueue();
132   CMusicLibraryQueue(const CMusicLibraryQueue&);
133   CMusicLibraryQueue const& operator=(CMusicLibraryQueue const&);
134 
135   typedef std::set<CMusicLibraryJob*> MusicLibraryJobs;
136   typedef std::map<std::string, MusicLibraryJobs> MusicLibraryJobMap;
137   MusicLibraryJobMap m_jobs;
138   CCriticalSection m_critical;
139 
140   bool m_modal = false;
141   bool m_exporting = false;
142   bool m_cleaning = false;
143 };
144