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