| 1 | /* |
| 2 | Its is under the MIT license, to encourage reuse by cut-and-paste. |
| 3 | |
| 4 | The original files are hosted here: https://github.com/sago007/PlatformFolders |
| 5 | |
| 6 | Copyright (c) 2015 Poul Sander |
| 7 | |
| 8 | Permission is hereby granted, free of charge, to any person |
| 9 | obtaining a copy of this software and associated documentation files |
| 10 | (the "Software"), to deal in the Software without restriction, |
| 11 | including without limitation the rights to use, copy, modify, merge, |
| 12 | publish, distribute, sublicense, and/or sell copies of the Software, |
| 13 | and to permit persons to whom the Software is furnished to do so, |
| 14 | subject to the following conditions: |
| 15 | |
| 16 | The above copyright notice and this permission notice shall be |
| 17 | included in all copies or substantial portions of the Software. |
| 18 | |
| 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 22 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 23 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 24 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 25 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | #ifndef SAGO_PLATFORM_FOLDERS_H |
| 30 | #define SAGO_PLATFORM_FOLDERS_H |
| 31 | |
| 32 | #include <vector> |
| 33 | #include <string> |
| 34 | |
| 35 | /** |
| 36 | * The namespace I use for common function. Nothing special about it. |
| 37 | */ |
| 38 | namespace sago { |
| 39 | |
| 40 | #ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 41 | namespace internal { |
| 42 | #if !defined(_WIN32) && !defined(__APPLE__) |
| 43 | void appendExtraFoldersTokenizer(const char* envName, const char* envValue, std::vector<std::string>& folders); |
| 44 | #endif |
| 45 | #ifdef _WIN32 |
| 46 | std::string win32_utf16_to_utf8(const wchar_t* wstr); |
| 47 | #endif |
| 48 | } |
| 49 | #endif //DOXYGEN_SHOULD_SKIP_THIS |
| 50 | |
| 51 | /** |
| 52 | * Retrives the base folder for storing data files. |
| 53 | * You must add the program name yourself like this: |
| 54 | * @code{.cpp} |
| 55 | * string data_home = getDataHome()+"/My Program Name/"; |
| 56 | * @endcode |
| 57 | * On Windows this defaults to %APPDATA% (Roaming profile) |
| 58 | * On Linux this defaults to ~/.local/share but can be configured by the user |
| 59 | * @return The base folder for storing program data. |
| 60 | */ |
| 61 | std::string getDataHome(); |
| 62 | |
| 63 | /** |
| 64 | * Retrives the base folder for storing config files. |
| 65 | * You must add the program name yourself like this: |
| 66 | * @code{.cpp} |
| 67 | * string data_home = getConfigHome()+"/My Program Name/"; |
| 68 | * @endcode |
| 69 | * On Windows this defaults to %APPDATA% (Roaming profile) |
| 70 | * On Linux this defaults to ~/.config but can be configured by the user |
| 71 | * @return The base folder for storing config data. |
| 72 | */ |
| 73 | std::string getConfigHome(); |
| 74 | |
| 75 | /** |
| 76 | * Retrives the base folder for storing cache files. |
| 77 | * You must add the program name yourself like this: |
| 78 | * @code{.cpp} |
| 79 | * string data_home = getCacheDir()+"/My Program Name/cache/"; |
| 80 | * @endcode |
| 81 | * On Windows this defaults to %APPDATALOCAL% |
| 82 | * On Linux this defaults to ~/.cache but can be configured by the user |
| 83 | * Note that it is recommended to append "cache" after the program name to prevent conflicting with "StateDir" under Windows |
| 84 | * @return The base folder for storing data that do not need to be backed up and might be deleted. |
| 85 | */ |
| 86 | std::string getCacheDir(); |
| 87 | |
| 88 | /** |
| 89 | * Retrives the base folder used for state files. |
| 90 | * You must add the program name yourself like this: |
| 91 | * @code{.cpp} |
| 92 | * string data_home = getStateDir()+"/My Program Name/"; |
| 93 | * @endcode |
| 94 | * On Windows this defaults to %APPDATALOCAL% |
| 95 | * On Linux this defaults to ~/.local/state but can be configured by the user |
| 96 | * On OS X this is the same as getDataHome() |
| 97 | * @return The base folder for storing data that do not need to be backed up but should not be reguarly deleted either. |
| 98 | */ |
| 99 | std::string getStateDir(); |
| 100 | |
| 101 | /** |
| 102 | * This will append extra folders that your program should be looking for data files in. |
| 103 | * This does not normally include the path returned by GetDataHome(). |
| 104 | * If you want all the folders you should do something like: |
| 105 | * @code{.cpp} |
| 106 | * vector<string> folders; |
| 107 | * folders.push_back(getDataHome()); |
| 108 | * appendAdditionalDataDirectories(folders); |
| 109 | * for (string s& : folders) { |
| 110 | * s+="/My Program Name/"; |
| 111 | * } |
| 112 | * @endcode |
| 113 | * You must apply "/My Program Name/" to all the strings. |
| 114 | * The string at the lowest index has the highest priority. |
| 115 | * @param homes A vector that extra folders will be appended to. |
| 116 | */ |
| 117 | void appendAdditionalDataDirectories(std::vector<std::string>& homes); |
| 118 | |
| 119 | /** |
| 120 | * This will append extra folders that your program should be looking for config files in. |
| 121 | * This does not normally include the path returned by GetConfigHome(). |
| 122 | * If you want all the folders you should do something like: |
| 123 | * @code{.cpp} |
| 124 | * std::vector<std::string> folders; |
| 125 | * folders.push_back(sago::getConfigHome()); |
| 126 | * sago::appendAdditionalConfigDirectories(folders); |
| 127 | * for (std::string s& : folders) { |
| 128 | * s+="/My Program Name/"; |
| 129 | * } |
| 130 | * @endcode |
| 131 | * You must apply "/My Program Name/" to all the strings. |
| 132 | * The string at the lowest index has the highest priority. |
| 133 | * @param homes A vector that extra folders will be appended to. |
| 134 | */ |
| 135 | void appendAdditionalConfigDirectories(std::vector<std::string>& homes); |
| 136 | |
| 137 | /** |
| 138 | * The folder that represents the desktop. |
| 139 | * Normally you should try not to use this folder. |
| 140 | * @return Absolute path to the user's desktop |
| 141 | */ |
| 142 | std::string getDesktopFolder(); |
| 143 | |
| 144 | /** |
| 145 | * The folder to store user documents to |
| 146 | * @return Absolute path to the "Documents" folder |
| 147 | */ |
| 148 | std::string getDocumentsFolder(); |
| 149 | |
| 150 | /** |
| 151 | * The folder where files are downloaded. |
| 152 | * @return Absolute path to the folder where files are downloaded to. |
| 153 | */ |
| 154 | std::string getDownloadFolder(); |
| 155 | |
| 156 | /** |
| 157 | * The folder where files are downloaded. |
| 158 | * @note This is provided for backward compatibility. Use getDownloadFolder instead. |
| 159 | * @return Absolute path to the folder where files are downloaded to. |
| 160 | */ |
| 161 | std::string getDownloadFolder1(); |
| 162 | |
| 163 | /** |
| 164 | * The folder for storing the user's pictures. |
| 165 | * @return Absolute path to the "Picture" folder |
| 166 | */ |
| 167 | std::string getPicturesFolder(); |
| 168 | |
| 169 | /** |
| 170 | * This returns the folder that can be used for sharing files with other users on the same system. |
| 171 | * @return Absolute path to the "Public" folder |
| 172 | */ |
| 173 | std::string getPublicFolder(); |
| 174 | |
| 175 | /** |
| 176 | * The folder where music is stored |
| 177 | * @return Absolute path to the music folder |
| 178 | */ |
| 179 | std::string getMusicFolder(); |
| 180 | |
| 181 | /** |
| 182 | * The folder where video is stored |
| 183 | * @return Absolute path to the video folder |
| 184 | */ |
| 185 | std::string getVideoFolder(); |
| 186 | |
| 187 | /** |
| 188 | * A base folder for storing saved games. |
| 189 | * You must add the program name to it like this: |
| 190 | * @code{.cpp} |
| 191 | * string saved_games_folder = sago::getSaveGamesFolder1()+"/My Program Name/"; |
| 192 | * @endcode |
| 193 | * @note Windows: This is an XP compatible version and returns the path to "My Games" in Documents. Vista and later has an official folder. |
| 194 | * @note Linux: XDF does not define a folder for saved games. This will just return the same as GetDataHome() |
| 195 | * @return The folder base folder for storing save games. |
| 196 | */ |
| 197 | std::string getSaveGamesFolder1(); |
| 198 | |
| 199 | /** |
| 200 | * A base folder for storing saved games. |
| 201 | * You must add the program name to it like this: |
| 202 | * @code{.cpp} |
| 203 | * string saved_games_folder = sago::getSaveGamesFolder2()+"/My Program Name/"; |
| 204 | * @endcode |
| 205 | * @note PlatformFolders provide different folders to for saved games as not all operating systems has support for Saved Games yet. |
| 206 | * It is recommended to pick the highest number (currently getSaveGamesFolder2) at the time your product enters production and stick with it |
| 207 | * @note Windows: This returns the "Saved Games" folder. This folder exist in Vista and later |
| 208 | * @note Linux: XDF does not define a folder for saved games. This will just return the same as GetDataHome() |
| 209 | * @return The folder base folder for storing save games. |
| 210 | */ |
| 211 | std::string getSaveGamesFolder2(); |
| 212 | |
| 213 | #ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 214 | |
| 215 | /** |
| 216 | * This class contains methods for finding the system depended special folders. |
| 217 | * For Windows these folders are either by convention or given by CSIDL. |
| 218 | * For Linux XDG convention is used. |
| 219 | * The Linux version has very little error checking and assumes that the config is correct |
| 220 | */ |
| 221 | class PlatformFolders { |
| 222 | public: |
| 223 | PlatformFolders(); |
| 224 | ~PlatformFolders(); |
| 225 | /** |
| 226 | * The folder that represents the desktop. |
| 227 | * Normally you should try not to use this folder. |
| 228 | * @return Absolute path to the user's desktop |
| 229 | */ |
| 230 | std::string getDesktopFolder() const; |
| 231 | /** |
| 232 | * The folder to store user documents to |
| 233 | * @return Absolute path to the "Documents" folder |
| 234 | */ |
| 235 | std::string getDocumentsFolder() const; |
| 236 | /** |
| 237 | * The folder for storing the user's pictures. |
| 238 | * @return Absolute path to the "Picture" folder |
| 239 | */ |
| 240 | std::string getPicturesFolder() const; |
| 241 | /** |
| 242 | * Use sago::getPublicFolder() instead! |
| 243 | */ |
| 244 | std::string getPublicFolder() const; |
| 245 | /** |
| 246 | * The folder where files are downloaded. |
| 247 | * @note Windows: This version is XP compatible and returns the Desktop. Vista and later has a dedicated folder. |
| 248 | * @return Absolute path to the folder where files are downloaded to. |
| 249 | */ |
| 250 | std::string getDownloadFolder1() const; |
| 251 | /** |
| 252 | * The folder where music is stored |
| 253 | * @return Absolute path to the music folder |
| 254 | */ |
| 255 | std::string getMusicFolder() const; |
| 256 | /** |
| 257 | * The folder where video is stored |
| 258 | * @return Absolute path to the video folder |
| 259 | */ |
| 260 | std::string getVideoFolder() const; |
| 261 | /** |
| 262 | * The base folder for storing saved games. |
| 263 | * You must add the program name to it like this: |
| 264 | * @code{.cpp} |
| 265 | * PlatformFolders pf; |
| 266 | * string saved_games_folder = pf.getSaveGamesFolder1()+"/My Program Name/"; |
| 267 | * @endcode |
| 268 | * @note Windows: This is an XP compatible version and returns the path to "My Games" in Documents. Vista and later has an official folder. |
| 269 | * @note Linux: XDF does not define a folder for saved games. This will just return the same as GetDataHome() |
| 270 | * @return The folder base folder for storing save games. |
| 271 | */ |
| 272 | std::string getSaveGamesFolder1() const; |
| 273 | private: |
| 274 | PlatformFolders(const PlatformFolders&); |
| 275 | PlatformFolders& operator=(const PlatformFolders&); |
| 276 | #if !defined(_WIN32) && !defined(__APPLE__) |
| 277 | struct PlatformFoldersData; |
| 278 | PlatformFoldersData* data; |
| 279 | #endif |
| 280 | }; |
| 281 | |
| 282 | #endif // skip doxygen |
| 283 | |
| 284 | |
| 285 | } //namespace sago |
| 286 | |
| 287 | #endif /* PLATFORM_FOLDERS_H */ |