Merge pull request #26126 from stephan49/fix-pipewire-unlock-error
[xbmc.git] / xbmc / video / VideoInfoScanner.h
blob633ec29415ae59c44b73d1eeb304e83e631c3fac
1 /*
2 * Copyright (C) 2005-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 #pragma once
11 #include "InfoScanner.h"
12 #include "VideoDatabase.h"
13 #include "addons/Scraper.h"
14 #include "guilib/GUIListItem.h"
16 #include <set>
17 #include <string>
18 #include <vector>
20 class CAdvancedSettings;
21 class CRegExp;
22 class CFileItem;
23 class CFileItemList;
25 namespace KODI::VIDEO
27 class IVideoInfoTagLoader;
29 typedef struct SScanSettings
31 SScanSettings()
33 parent_name = false;
34 parent_name_root = false;
35 noupdate = false;
36 exclude = false;
37 m_allExtAudio = false;
38 recurse = 1;
40 bool parent_name; /* use the parent dirname as name of lookup */
41 bool parent_name_root; /* use the name of directory where scan started as name for files in that dir */
42 int recurse; /* recurse into sub folders (indicate levels) */
43 bool noupdate; /* exclude from update library function */
44 bool exclude; /* exclude this path from scraping */
45 bool m_allExtAudio; /* treat all audio files in video directory as external tracks */
46 } SScanSettings;
48 class CVideoInfoScanner : public CInfoScanner
50 public:
51 CVideoInfoScanner();
52 ~CVideoInfoScanner() override;
54 /*! \brief Scan a folder using the background scanner
55 \param strDirectory path to scan
56 \param scanAll whether to scan everything not already scanned (regardless of whether the user normally doesn't want a folder scanned.) Defaults to false.
58 void Start(const std::string& strDirectory, bool scanAll = false);
59 void Stop();
61 /*! \brief Add an item to the database.
62 \param pItem item to add to the database.
63 \param content content type of the item.
64 \param videoFolder whether the video is represented by a folder (single movie per folder). Defaults to false.
65 \param useLocal whether to use local information for artwork etc.
66 \param showInfo pointer to CVideoInfoTag details for the show if this is an episode. Defaults to NULL.
67 \param libraryImport Whether this call belongs to a full library import or not. Defaults to false.
68 \return database id of the added item, or -1 on failure.
70 long AddVideo(CFileItem *pItem, const CONTENT_TYPE &content, bool videoFolder = false, bool useLocal = true, const CVideoInfoTag *showInfo = NULL, bool libraryImport = false);
72 /*! \brief Retrieve information for a list of items and add them to the database.
73 \param items list of items to retrieve info for.
74 \param bDirNames whether we should use folder or file names for lookups.
75 \param content type of content to retrieve.
76 \param useLocal should local data (.nfo and art) be used. Defaults to true.
77 \param pURL an optional URL to use to retrieve online info. Defaults to NULL.
78 \param fetchEpisodes whether we are fetching episodes with shows. Defaults to true.
79 \param pDlgProgress progress dialog to update and check for cancellation during processing. Defaults to NULL.
80 \return true if we successfully found information for some items, false otherwise
82 bool RetrieveVideoInfo(CFileItemList& items, bool bDirNames, CONTENT_TYPE content, bool useLocal = true, CScraperUrl *pURL = NULL, bool fetchEpisodes = true, CGUIDialogProgress* pDlgProgress = NULL);
84 static void ApplyThumbToFolder(const std::string &folder, const std::string &imdbThumb);
85 static bool DownloadFailed(CGUIDialogProgress* pDlgProgress);
87 /*! \brief Retrieve any artwork associated with an item
88 \param pItem item to find artwork for.
89 \param content content type of the item.
90 \param bApplyToDir whether we should apply any thumbs to a folder. Defaults to false.
91 \param useLocal whether we should use local thumbs. Defaults to true.
92 \param actorArtPath the path to search for actor thumbs. Defaults to empty.
94 void GetArtwork(CFileItem *pItem, const CONTENT_TYPE &content, bool bApplyToDir=false, bool useLocal=true, const std::string &actorArtPath = "");
96 /*! \brief Get season thumbs for a tvshow.
97 All seasons (regardless of whether the user has episodes) are added to the art map.
98 \param show tvshow info tag
99 \param art artwork map to which season thumbs are added.
100 \param useLocal whether to use local thumbs, defaults to true
102 static void GetSeasonThumbs(const CVideoInfoTag &show, std::map<int, std::map<std::string, std::string> > &art, const std::vector<std::string> &artTypes, bool useLocal = true);
103 static std::string GetImage(const CScraperUrl::SUrlEntry &image, const std::string& itemPath);
105 bool EnumerateEpisodeItem(const CFileItem *item, EPISODELIST& episodeList);
107 static std::string GetMovieSetInfoFolder(const std::string& setTitle);
109 protected:
110 virtual void Process();
111 bool DoScan(const std::string& strDirectory) override;
113 InfoRet RetrieveInfoForTvShow(CFileItem* pItem,
114 bool bDirNames,
115 ADDON::ScraperPtr& scraper,
116 bool useLocal,
117 CScraperUrl* pURL,
118 bool fetchEpisodes,
119 CGUIDialogProgress* pDlgProgress);
120 InfoRet RetrieveInfoForMovie(CFileItem* pItem,
121 bool bDirNames,
122 ADDON::ScraperPtr& scraper,
123 bool useLocal,
124 CScraperUrl* pURL,
125 CGUIDialogProgress* pDlgProgress);
126 InfoRet RetrieveInfoForMusicVideo(CFileItem* pItem,
127 bool bDirNames,
128 ADDON::ScraperPtr& scraper,
129 bool useLocal,
130 CScraperUrl* pURL,
131 CGUIDialogProgress* pDlgProgress);
132 InfoRet RetrieveInfoForEpisodes(CFileItem* item,
133 long showID,
134 const ADDON::ScraperPtr& scraper,
135 bool useLocal,
136 CGUIDialogProgress* progress = nullptr);
138 /*! \brief Update the progress bar with the heading and line and check for cancellation
139 \param progress CGUIDialogProgress bar
140 \param heading string id of heading
141 \param line1 string to set for the first line
142 \return true if the user has cancelled the scanner, false otherwise
144 bool ProgressCancelled(CGUIDialogProgress* progress, int heading, const std::string &line1);
146 /*! \brief Find a url for the given video using the given scraper
147 \param title title of the video to lookup
148 \param year year of the video to lookup
149 \param scraper scraper to use for the lookup
150 \param url [out] returned url from the scraper
151 \param progress CGUIDialogProgress bar
152 \return >0 on success, <0 on failure (cancellation), and 0 on no info found
154 int FindVideo(const std::string &title, int year, const ADDON::ScraperPtr &scraper, CScraperUrl &url, CGUIDialogProgress *progress);
156 /*! \brief Find a url for the given video using the given scraper
157 \param item the video to lookup
158 \param scraper scraper to use for the lookup
159 \param url [out] returned url from the scraper
160 \param progress CGUIDialogProgress bar
161 \return >0 on success, <0 on failure (cancellation), and 0 on no info found
163 int FindVideoUsingTag(CFileItem& item, const ADDON::ScraperPtr &scraper, CScraperUrl &url, CGUIDialogProgress *progress);
165 /*! \brief Retrieve detailed information for an item from an online source, optionally supplemented with local data
166 @todo sort out some better return codes.
167 \param pItem item to retrieve online details for.
168 \param uniqueIDs Unique IDs for additional information for scrapers.
169 \param url URL to use to retrieve online details.
170 \param scraper Scraper that handles parsing the online data.
171 \param nfoFile if set, we override the online data with the locally supplied data. Defaults to NULL.
172 \param pDialog progress dialog to update and check for cancellation during processing. Defaults to NULL.
173 \return true if information is found, false if an error occurred, the lookup was cancelled, or no information was found.
175 bool GetDetails(CFileItem* pItem,
176 const std::unordered_map<std::string, std::string>& uniqueIDs,
177 CScraperUrl& url,
178 const ADDON::ScraperPtr& scraper,
179 VIDEO::IVideoInfoTagLoader* nfoFile = nullptr,
180 CGUIDialogProgress* pDialog = nullptr);
182 /*! \brief Extract episode and season numbers from a processed regexp
183 \param reg Regular expression object with at least 2 matches
184 \param episodeInfo Episode information to fill in.
185 \param defaultSeason Season to use if not found in reg.
186 \return true on success (2 matches), false on failure (fewer than 2 matches)
188 bool GetEpisodeAndSeasonFromRegExp(CRegExp &reg, EPISODE &episodeInfo, int defaultSeason);
190 /*! \brief Extract episode air-date from a processed regexp
191 \param reg Regular expression object with at least 3 matches
192 \param episodeInfo Episode information to fill in.
193 \return true on success (3 matches), false on failure (fewer than 3 matches)
195 bool GetAirDateFromRegExp(CRegExp &reg, EPISODE &episodeInfo);
197 /*! \brief Extract episode title from a processed regexp
198 \param reg Regular expression object with at least 1 match
199 \param episodeInfo Episode information to fill in.
200 \return true on success (1 match), false on failure (no matches)
202 bool GetEpisodeTitleFromRegExp(CRegExp& reg, EPISODE& episodeInfo);
204 /*! \brief Fetch thumbs for actors
205 Updates each actor with their thumb (local or online)
206 \param actors - vector of SActorInfo
207 \param strPath - path on filesystem to look for local thumbs
209 void FetchActorThumbs(std::vector<SActorInfo>& actors, const std::string& strPath);
211 static int GetPathHash(const CFileItemList &items, std::string &hash);
213 /*! \brief Retrieve a "fast" hash of the given directory (if available)
214 Performs a stat() on the directory, and uses modified time to create a "fast"
215 hash of the folder. If no modified time is available, the create time is used,
216 and if neither are available, an empty hash is returned.
217 In case exclude from scan expressions are present, the string array will be appended
218 to the md5 hash to ensure we're doing a re-scan whenever the user modifies those.
219 \param directory folder to hash
220 \param excludes string array of exclude expressions
221 \return the md5 hash of the folder"
223 std::string GetFastHash(const std::string &directory, const std::vector<std::string> &excludes) const;
225 /*! \brief Retrieve a "fast" hash of the given directory recursively (if available)
226 Performs a stat() on the directory, and uses modified time to create a "fast"
227 hash of each folder. If no modified time is available, the create time is used,
228 and if neither are available, an empty hash is returned.
229 In case exclude from scan expressions are present, the string array will be appended
230 to the md5 hash to ensure we're doing a re-scan whenever the user modifies those.
231 \param directory folder to hash (recursively)
232 \param excludes string array of exclude expressions
233 \return the md5 hash of the folder
235 std::string GetRecursiveFastHash(const std::string &directory, const std::vector<std::string> &excludes) const;
237 /*! \brief Decide whether a folder listing could use the "fast" hash
238 Fast hashing can be done whenever the folder contains no scannable subfolders, as the
239 fast hash technique uses modified time to determine when folder content changes, which
240 is generally not propagated up the directory tree.
241 \param items the directory listing
242 \param excludes string array of exclude expressions
243 \return true if this directory listing can be fast hashed, false otherwise
245 bool CanFastHash(const CFileItemList &items, const std::vector<std::string> &excludes) const;
247 /*! \brief Process a series folder, filling in episode details and adding them to the database.
248 @todo Ideally we would return InfoRet:HAVE_ALREADY if we don't have to update any episodes
249 and we should return InfoRet::NOT_FOUND only if no information is found for any of
250 the episodes. InfoRet::ADDED then indicates we've added one or more episodes.
251 \param files the episode files to process.
252 \param scraper scraper to use for finding online info
253 \param showInfo information for the show.
254 \param pDlgProcess progress dialog to update during processing. Defaults to NULL.
255 \return InfoRet::ERROR on failure, InfoRet::CANCELLED on cancellation,
256 InfoRet::NOT_FOUND if an episode isn't found, or InfoRet::ADDED if all episodes are added.
258 InfoRet OnProcessSeriesFolder(EPISODELIST& files,
259 const ADDON::ScraperPtr& scraper,
260 bool useLocal,
261 const CVideoInfoTag& showInfo,
262 CGUIDialogProgress* pDlgProgress = nullptr);
264 bool EnumerateSeriesFolder(CFileItem* item, EPISODELIST& episodeList);
265 bool ProcessItemByVideoInfoTag(const CFileItem *item, EPISODELIST &episodeList);
267 bool AddVideoExtras(CFileItemList& items, const CONTENT_TYPE& content, const std::string& path);
268 bool ProcessVideoVersion(VideoDbContentType itemType, int dbId);
270 std::pair<InfoType, std::unique_ptr<IVideoInfoTagLoader>> ReadInfoTag(
271 CFileItem& item, const ADDON::ScraperPtr& scraper, bool lookInFolder, bool resetTag);
273 bool m_bStop;
274 bool m_scanAll;
275 bool m_ignoreVideoVersions{false};
276 bool m_ignoreVideoExtras{false};
277 std::string m_strStartDir;
278 CVideoDatabase m_database;
279 std::set<std::string> m_pathsToCount;
280 std::set<int> m_pathsToClean;
281 std::shared_ptr<CAdvancedSettings> m_advancedSettings;
282 CVideoDatabase::ScraperCache m_scraperCache;
284 private:
285 static void AddLocalItemArtwork(CGUIListItem::ArtMap& itemArt,
286 const std::vector<std::string>& wantedArtTypes, const std::string& itemPath,
287 bool addAll, bool exactName);
289 /*! \brief Retrieve the art type for an image from the given size.
290 \param width the width of the image.
291 \param height the height of the image.
292 \return "poster" if the aspect ratio is at most 4:5, "banner" if the aspect ratio
293 is at least 1:4, "thumb" otherwise.
295 static std::string GetArtTypeFromSize(unsigned int width, unsigned int height);
297 } // namespace KODI::VIDEO