[PVR][Estuary] Timer settings dialog: Show client name in timer type selection dialog...
[xbmc.git] / xbmc / Util.h
blobfe677484812c595e233ca98edb62ad50a34b2c0b
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 "MediaSource.h" // Definition of VECSOURCES
12 #include "utils/Digest.h"
14 #include <climits>
15 #include <cmath>
16 #include <stdint.h>
17 #include <string.h>
18 #include <vector>
20 // A list of filesystem types for LegalPath/FileName
21 #define LEGAL_NONE 0
22 #define LEGAL_WIN32_COMPAT 1
23 #define LEGAL_FATX 2
25 class CFileItem;
26 class CFileItemList;
27 class CURL;
29 struct ExternalStreamInfo
31 std::string name;
32 std::string language;
33 unsigned int flag = 0;
36 class CUtil
38 CUtil() = delete;
39 public:
40 static void CleanString(const std::string& strFileName,
41 std::string& strTitle,
42 std::string& strTitleAndYear,
43 std::string& strYear,
44 bool bRemoveExtension = false,
45 bool bCleanChars = true);
46 static std::string GetTitleFromPath(const CURL& url, bool bIsFolder = false);
47 static std::string GetTitleFromPath(const std::string& strFileNameAndPath, bool bIsFolder = false);
48 static void GetQualifiedFilename(const std::string &strBasePath, std::string &strFilename);
49 static void RunShortcut(const char* szPath);
50 static std::string GetHomePath(
51 const std::string& strTarget = "KODI_HOME"); // default target is "KODI_HOME"
52 static bool ExcludeFileOrFolder(const std::string& strFileOrFolder, const std::vector<std::string>& regexps);
53 static void GetFileAndProtocol(const std::string& strURL, std::string& strDir);
54 static int GetDVDIfoTitle(const std::string& strPathFile);
56 static bool IsPicture(const std::string& strFile);
57 /// Get resolved filesystem location of splash image
58 static std::string GetSplashPath();
60 /*! \brief retrieve MD5sum of a file
61 \param strPath - path to the file to MD5sum
62 \return md5 sum of the file
64 static std::string GetFileDigest(const std::string& strPath, KODI::UTILITY::CDigest::Type type);
65 static bool GetDirectoryName(const std::string& strFileName, std::string& strDescription);
66 static void GetDVDDriveIcon(const std::string& strPath, std::string& strIcon);
67 static void RemoveTempFiles();
69 static void ClearSubtitles();
70 static void ScanForExternalSubtitles(const std::string& strMovie, std::vector<std::string>& vecSubtitles );
72 /** \brief Retrieves stream info of external associated files, e.g., subtitles, for a given video.
73 * \param[in] videoPath The full path of the video file.
74 * \param[in] associatedFile A file that provides additional streams for the given video file.
75 * \return stream info for the given associatedFile
77 static ExternalStreamInfo GetExternalStreamDetailsFromFilename(const std::string& videoPath, const std::string& associatedFile);
78 static bool FindVobSubPair( const std::vector<std::string>& vecSubtitles, const std::string& strIdxPath, std::string& strSubPath );
79 static bool IsVobSub(const std::vector<std::string>& vecSubtitles, const std::string& strSubPath);
80 static std::string GetVobSubSubFromIdx(const std::string& vobSubIdx);
81 static std::string GetVobSubIdxFromSub(const std::string& vobSub);
83 /** \brief Retrieves paths of external audio files for a given video.
84 * \param[in] videoPath The full path of the video file.
85 * \param[out] vecAudio A vector containing the full paths of all found external audio files.
87 static void ScanForExternalAudio(const std::string& videoPath, std::vector<std::string>& vecAudio);
88 static int64_t ToInt64(uint32_t high, uint32_t low);
89 static std::string GetNextFilename(const std::string &fn_template, int max);
90 static std::string GetNextPathname(const std::string &path_template, int max);
91 static void StatToStatI64(struct _stati64 *result, struct stat *stat);
92 static void StatToStat64(struct __stat64 *result, const struct stat *stat);
93 static void Stat64ToStatI64(struct _stati64 *result, struct __stat64 *stat);
94 static void StatI64ToStat64(struct __stat64 *result, struct _stati64 *stat);
95 static void Stat64ToStat(struct stat *result, struct __stat64 *stat);
96 #ifdef TARGET_WINDOWS
97 static void Stat64ToStat64i32(struct _stat64i32 *result, struct __stat64 *stat);
98 #endif
99 static bool CreateDirectoryEx(const std::string& strPath);
101 #ifdef TARGET_WINDOWS
102 static std::string MakeLegalFileName(const std::string &strFile, int LegalType=LEGAL_WIN32_COMPAT);
103 static std::string MakeLegalPath(const std::string &strPath, int LegalType=LEGAL_WIN32_COMPAT);
104 #else
105 static std::string MakeLegalFileName(const std::string &strFile, int LegalType=LEGAL_NONE);
106 static std::string MakeLegalPath(const std::string &strPath, int LegalType=LEGAL_NONE);
107 #endif
108 static std::string ValidatePath(const std::string &path, bool bFixDoubleSlashes = false); ///< return a validated path, with correct directory separators.
111 * \brief Check if a filename contains a supported font extension.
112 * \param filename The filename to check
113 * \return True if it is supported, otherwise false
115 static bool IsSupportedFontExtension(const std::string& fileName);
117 /*! \brief Split a comma separated parameter list into separate parameters.
118 Takes care of the case where we may have a quoted string containing commas, or we may
119 have a function (i.e. parentheses) with multiple parameters as a single parameter.
123 foo, bar(param1, param2), foo
125 will return:
127 "foo", "bar(param1, param2)", and "foo".
129 \param paramString the string to break up
130 \param parameters the returned parameters
132 static void SplitParams(const std::string& paramString, std::vector<std::string>& parameters);
133 static int GetMatchingSource(const std::string& strPath, VECSOURCES& VECSOURCES, bool& bIsSourceName);
134 static std::string TranslateSpecialSource(const std::string &strSpecial);
135 static void DeleteDirectoryCache(const std::string &prefix = "");
136 static void DeleteMusicDatabaseDirectoryCache();
137 static void DeleteVideoDatabaseDirectoryCache();
138 static std::string MusicPlaylistsLocation();
139 static std::string VideoPlaylistsLocation();
141 static void GetSkinThemes(std::vector<std::string>& vecTheme);
142 static void GetRecursiveListing(const std::string& strPath, CFileItemList& items, const std::string& strMask, unsigned int flags = 0 /* DIR_FLAG_DEFAULTS */);
143 static void GetRecursiveDirsListing(const std::string& strPath, CFileItemList& items, unsigned int flags = 0 /* DIR_FLAG_DEFAULTS */);
144 static void ForceForwardSlashes(std::string& strPath);
146 static double AlbumRelevance(const std::string& strAlbumTemp1, const std::string& strAlbum1, const std::string& strArtistTemp1, const std::string& strArtist1);
147 static bool MakeShortenPath(std::string StrInput, std::string& StrOutput, size_t iTextMaxLength);
148 /*! \brief Checks whether the supplied path supports Write file operations (e.g. Rename, Delete, ...)
150 \param strPath the path to be checked
152 \return true if Write file operations are supported, false otherwise
154 static bool SupportsWriteFileOperations(const std::string& strPath);
155 /*! \brief Checks whether the supplied path supports Read file operations (e.g. Copy, ...)
157 \param strPath the path to be checked
159 \return true if Read file operations are supported, false otherwise
161 static bool SupportsReadFileOperations(const std::string& strPath);
162 static std::string GetDefaultFolderThumb(const std::string &folderThumb);
164 static void InitRandomSeed();
166 // Get decimal integer representation of roman digit, ivxlcdm are valid
167 // return 0 for other chars;
168 static int LookupRomanDigit(char roman_digit);
169 // Translate a string of roman numerals to decimal a decimal integer
170 // return -1 on error, valid range is 1-3999
171 static int TranslateRomanNumeral(const char* roman_numeral);
173 #if defined(TARGET_POSIX) && !defined(TARGET_DARWIN_TVOS)
175 // Forks to execute a shell command.
177 static bool Command(const std::vector<std::string>& arrArgs, bool waitExit = false);
180 // Forks to execute an unparsed shell command line.
182 static bool RunCommandLine(const std::string& cmdLine, bool waitExit = false);
183 #endif
184 static std::string ResolveExecutablePath();
185 static std::string GetFrameworksPath(bool forPython = false);
187 static bool CanBindPrivileged();
188 static bool ValidatePort(int port);
191 * \brief Thread-safe random number generation
193 static int GetRandomNumber();
195 static int64_t ConvertSecsToMilliSecs(double secs) { return static_cast<int64_t>(secs * 1000); }
196 static double ConvertMilliSecsToSecs(int64_t offset) { return offset / 1000.0; }
197 static int64_t ConvertMilliSecsToSecsInt(int64_t offset) { return offset / 1000; }
198 static int64_t ConvertMilliSecsToSecsIntRounded(int64_t offset) { return ConvertMilliSecsToSecsInt(offset + 499); }
200 /** \brief Copy files from the application bundle over to the user data directory in Application Support/Kodi.
202 static void CopyUserDataIfNeeded(const std::string& strPath,
203 const std::string& file,
204 const std::string& destname = "");
206 #if !defined(TARGET_WINDOWS)
207 private:
208 static unsigned int s_randomSeed;
209 #endif
211 protected:
212 /** \brief Retrieves the base path and the filename of a given video.
213 * \param[in] videoPath The full path of the video file.
214 * \param[out] basePath The base path of the given video.
215 * \param[out] videoFileName The file name of the given video..
217 static void GetVideoBasePathAndFileName(const std::string& videoPath,
218 std::string& basePath,
219 std::string& videoFileName);
221 /** \brief Retrieves FileItems that could contain associated files of a given video.
222 * \param[in] videoPath The full path of the video file.
223 * \param[in] item_exts A | separated string of extensions specifying the associated files.
224 * \param[in] sub_dirs A vector of sub directory names to look for.
225 * \param[out] items A List of FileItems to scan for associated files.
227 static void GetItemsToScan(const std::string& videoPath,
228 const std::string& item_exts,
229 const std::vector<std::string>& sub_dirs,
230 CFileItemList& items);
232 /** \brief Searches for associated files of a given video.
233 * \param[in] videoName The name of the video file.
234 * \param[in] items A List of FileItems to scan for associated files.
235 * \param[in] item_exts A vector of extensions specifying the associated files.
236 * \param[out] associatedFiles A vector containing the full paths of all found associated files.
238 static void ScanPathsForAssociatedItems(const std::string& videoName,
239 const CFileItemList& items,
240 const std::vector<std::string>& item_exts,
241 std::vector<std::string>& associatedFiles);
243 /** \brief Searches in an archive for associated files of a given video.
244 * \param[in] strArchivePath The full path of the archive.
245 * \param[in] videoNameNoExt The filename of the video without extension for which associated files should be retrieved.
246 * \param[in] item_exts A vector of extensions specifying the associated files.
247 * \param[out] associatedFiles A vector containing the full paths of all found associated files.
249 static int ScanArchiveForAssociatedItems(const std::string& strArchivePath,
250 const std::string& videoNameNoExt,
251 const std::vector<std::string>& item_exts,
252 std::vector<std::string>& associatedFiles);