Merge pull request #3182 from ronie/aspects
[vuplus_xbmc] / xbmc / Util.h
1 #pragma once
2 /*
3  *      Copyright (C) 2005-2013 Team XBMC
4  *      http://xbmc.org
5  *
6  *  This Program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2, or (at your option)
9  *  any later version.
10  *
11  *  This Program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with XBMC; see the file COPYING.  If not, see
18  *  <http://www.gnu.org/licenses/>.
19  *
20  */
21
22 #include <climits>
23 #include <cmath>
24 #include <vector>
25 #include <string.h>
26 #include <stdint.h>
27
28 #include "MediaSource.h"
29
30 // A list of filesystem types for LegalPath/FileName
31 #define LEGAL_NONE            0
32 #define LEGAL_WIN32_COMPAT    1
33 #define LEGAL_FATX            2
34
35 namespace XFILE
36 {
37   class IFileCallback;
38 }
39
40 class CFileItem;
41 class CFileItemList;
42 class CURL;
43
44 struct sortstringbyname
45 {
46   bool operator()(const CStdString& strItem1, const CStdString& strItem2)
47   {
48     CStdString strLine1 = strItem1;
49     CStdString strLine2 = strItem2;
50     strLine1 = strLine1.ToLower();
51     strLine2 = strLine2.ToLower();
52     return strcmp(strLine1.c_str(), strLine2.c_str()) < 0;
53   }
54 };
55
56 class CUtil
57 {
58 public:
59   CUtil(void);
60   virtual ~CUtil(void);
61   static void CleanString(const CStdString& strFileName, CStdString& strTitle, CStdString& strTitleAndYear, CStdString& strYear, bool bRemoveExtension = false, bool bCleanChars = true);
62   static CStdString GetTitleFromPath(const CStdString& strFileNameAndPath, bool bIsFolder = false);
63   static void GetQualifiedFilename(const CStdString &strBasePath, CStdString &strFilename);
64   static void RunShortcut(const char* szPath);
65   static void GetHomePath(CStdString& strPath, const CStdString& strTarget = "XBMC_HOME");
66   static bool IsPVR(const CStdString& strFile);
67   static bool IsHTSP(const CStdString& strFile);
68   static bool IsLiveTV(const CStdString& strFile);
69   static bool IsTVRecording(const CStdString& strFile);
70   static bool ExcludeFileOrFolder(const CStdString& strFileOrFolder, const CStdStringArray& regexps);
71   static void GetFileAndProtocol(const CStdString& strURL, CStdString& strDir);
72   static int GetDVDIfoTitle(const CStdString& strPathFile);
73
74   static bool IsPicture(const CStdString& strFile);
75
76   /*! \brief retrieve MD5sum of a file
77    \param strPath - path to the file to MD5sum
78    \return md5 sum of the file
79    */
80   static CStdString GetFileMD5(const CStdString& strPath);
81   static bool GetDirectoryName(const CStdString& strFileName, CStdString& strDescription);
82   static void GetDVDDriveIcon( const CStdString& strPath, CStdString& strIcon );
83   static void RemoveTempFiles();
84   static void ClearTempFonts();
85
86   static void ClearSubtitles();
87   static void ScanForExternalSubtitles(const CStdString& strMovie, std::vector<CStdString>& vecSubtitles );
88   static int ScanArchiveForSubtitles( const CStdString& strArchivePath, const CStdString& strMovieFileNameNoExt, std::vector<CStdString>& vecSubtitles );
89   static bool FindVobSubPair( const std::vector<CStdString>& vecSubtitles, const CStdString& strIdxPath, CStdString& strSubPath );
90   static bool IsVobSub( const std::vector<CStdString>& vecSubtitles, const CStdString& strSubPath );  
91   static int64_t ToInt64(uint32_t high, uint32_t low);
92   static CStdString GetNextFilename(const CStdString &fn_template, int max);
93   static CStdString GetNextPathname(const CStdString &path_template, int max);
94   static void Tokenize(const CStdString& path, std::vector<CStdString>& tokens, const std::string& delimiters);
95   static void StatToStatI64(struct _stati64 *result, 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 CStdString& strPath);
103
104 #ifdef TARGET_WINDOWS
105   static CStdString MakeLegalFileName(const CStdString &strFile, int LegalType=LEGAL_WIN32_COMPAT);
106   static CStdString MakeLegalPath(const CStdString &strPath, int LegalType=LEGAL_WIN32_COMPAT);
107 #else
108   static CStdString MakeLegalFileName(const CStdString &strFile, int LegalType=LEGAL_NONE);
109   static CStdString MakeLegalPath(const CStdString &strPath, int LegalType=LEGAL_NONE);
110 #endif
111   static CStdString ValidatePath(const CStdString &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 CStdString &paramString, std::vector<CStdString> &parameters);
131   static void SplitExecFunction(const CStdString &execString, CStdString &function, std::vector<CStdString> &parameters);
132   static int GetMatchingSource(const CStdString& strPath, VECSOURCES& VECSOURCES, bool& bIsSourceName);
133   static CStdString TranslateSpecialSource(const CStdString &strSpecial);
134   static void DeleteDirectoryCache(const CStdString &prefix = "");
135   static void DeleteMusicDatabaseDirectoryCache();
136   static void DeleteVideoDatabaseDirectoryCache();
137   static CStdString MusicPlaylistsLocation();
138   static CStdString VideoPlaylistsLocation();
139
140   static void GetSkinThemes(std::vector<CStdString>& vecTheme);
141   static void GetRecursiveListing(const CStdString& strPath, CFileItemList& items, const CStdString& strMask, bool bUseFileDirectories=false);
142   static void GetRecursiveDirsListing(const CStdString& strPath, CFileItemList& items);
143   static void ForceForwardSlashes(CStdString& strPath);
144
145   static double AlbumRelevance(const CStdString& strAlbumTemp1, const CStdString& strAlbum1, const CStdString& strArtistTemp1, const CStdString& strArtist1);
146   static bool MakeShortenPath(CStdString StrInput, CStdString& StrOutput, int iTextMaxLength);
147 #ifdef TARGET_WINDOWS
148   static bool AddExtraLongPathPrefix(std::wstring& path);
149   static bool RemoveExtraLongPathPrefix(std::wstring& path);
150 #endif // TARGET_WINDOWS
151   /*! \brief Checks wether the supplied path supports Write file operations (e.g. Rename, Delete, ...)
152
153    \param strPath the path to be checked
154
155    \return true if Write file operations are supported, false otherwise
156    */
157   static bool SupportsWriteFileOperations(const CStdString& strPath);
158   /*! \brief Checks wether the supplied path supports Read file operations (e.g. Copy, ...)
159
160    \param strPath the path to be checked
161
162    \return true if Read file operations are supported, false otherwise
163    */
164   static bool SupportsReadFileOperations(const CStdString& strPath);
165   static CStdString GetDefaultFolderThumb(const CStdString &folderThumb);
166
167 #ifdef UNIT_TESTING
168   static bool TestSplitExec();
169   static bool TestGetQualifiedFilename();
170   static bool TestMakeLegalPath();
171 #endif
172
173   static void InitRandomSeed();
174
175   // Get decimal integer representation of roman digit, ivxlcdm are valid
176   // return 0 for other chars;
177   static int LookupRomanDigit(char roman_digit);
178   // Translate a string of roman numerals to decimal a decimal integer
179   // return -1 on error, valid range is 1-3999
180   static int TranslateRomanNumeral(const char* roman_numeral);
181
182 #ifdef TARGET_POSIX
183   // this will run the command using sudo in a new process.
184   // the user that runs xbmc should be allowed to issue the given sudo command.
185   // in order to allow a user to run sudo without supplying the password you'll need to edit sudoers
186   // # sudo visudo
187   // and add a line at the end defining the user and allowed commands
188   static bool SudoCommand(const CStdString &strCommand);
189
190   //
191   // Forks to execute a shell command.
192   //
193   static bool Command(const CStdStringArray& arrArgs, bool waitExit = false);
194
195   //
196   // Forks to execute an unparsed shell command line.
197   //
198   static bool RunCommandLine(const CStdString& cmdLine, bool waitExit = false);
199 #endif
200   static CStdString ResolveExecutablePath();
201   static CStdString GetFrameworksPath(bool forPython = false);
202
203   static bool CanBindPrivileged();
204   static bool ValidatePort(int port);
205
206   /*!
207    * \brief Thread-safe random number generation
208    */
209   static int GetRandomNumber();
210
211 #if !defined(TARGET_WINDOWS)
212 private:
213   static unsigned int s_randomSeed;
214 #endif
215 };
216
217