git repos / PlatformFolders

blame: sago/platform_folders.cpp

normal view · raw

1213f3f3 sago007 2015-09-19 19:24 1
/*
1213f3f3 sago007 2015-09-19 19:24 2
  Its is under the MIT license, to encourage reuse by cut-and-paste.
1213f3f3 sago007 2015-09-19 19:24 3
3b191916 sago007 2016-02-16 17:08 4
  The original files are hosted here: https://github.com/sago007/PlatformFolders
3b191916 sago007 2016-02-16 17:08 5
1213f3f3 sago007 2015-09-19 19:24 6
  Copyright (c) 2015 Poul Sander
1213f3f3 sago007 2015-09-19 19:24 7
1213f3f3 sago007 2015-09-19 19:24 8
  Permission is hereby granted, free of charge, to any person
1213f3f3 sago007 2015-09-19 19:24 9
  obtaining a copy of this software and associated documentation files
1213f3f3 sago007 2015-09-19 19:24 10
  (the "Software"), to deal in the Software without restriction,
1213f3f3 sago007 2015-09-19 19:24 11
  including without limitation the rights to use, copy, modify, merge,
1213f3f3 sago007 2015-09-19 19:24 12
  publish, distribute, sublicense, and/or sell copies of the Software,
1213f3f3 sago007 2015-09-19 19:24 13
  and to permit persons to whom the Software is furnished to do so,
1213f3f3 sago007 2015-09-19 19:24 14
  subject to the following conditions:
1213f3f3 sago007 2015-09-19 19:24 15
1213f3f3 sago007 2015-09-19 19:24 16
  The above copyright notice and this permission notice shall be
1213f3f3 sago007 2015-09-19 19:24 17
  included in all copies or substantial portions of the Software.
1213f3f3 sago007 2015-09-19 19:24 18
1213f3f3 sago007 2015-09-19 19:24 19
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1213f3f3 sago007 2015-09-19 19:24 20
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1213f3f3 sago007 2015-09-19 19:24 21
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1213f3f3 sago007 2015-09-19 19:24 22
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
1213f3f3 sago007 2015-09-19 19:24 23
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
1213f3f3 sago007 2015-09-19 19:24 24
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1213f3f3 sago007 2015-09-19 19:24 25
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1213f3f3 sago007 2015-09-19 19:24 26
  SOFTWARE.
1213f3f3 sago007 2015-09-19 19:24 27
*/
1213f3f3 sago007 2015-09-19 19:24 28
1213f3f3 sago007 2015-09-19 19:24 29
#include "platform_folders.h"
1213f3f3 sago007 2015-09-19 19:24 30
#include <iostream>
1213f3f3 sago007 2015-09-19 19:24 31
#include <stdexcept>
1213f3f3 sago007 2015-09-19 19:24 32
#include <string.h>
1213f3f3 sago007 2015-09-19 19:24 33
#include <stdio.h>
1213f3f3 sago007 2015-09-19 19:24 34
#include <cstdlib>
1213f3f3 sago007 2015-09-19 19:24 35
1213f3f3 sago007 2015-09-19 19:24 36
#if defined(_WIN32)
1213f3f3 sago007 2015-09-19 19:24 37
#include <windows.h>
1213f3f3 sago007 2015-09-19 19:24 38
#include <shlobj.h>
1213f3f3 sago007 2015-09-19 19:24 39
1213f3f3 sago007 2015-09-19 19:24 40
#define strtok_r strtok_s
1213f3f3 sago007 2015-09-19 19:24 41
1213f3f3 sago007 2015-09-19 19:24 42
static std::string GetWindowsFolder(int folderId, const char* errorMsg) {
1213f3f3 sago007 2015-09-19 19:24 43
	char szPath[MAX_PATH];
1213f3f3 sago007 2015-09-19 19:24 44
	szPath[0] = 0;
63627d7a sago007 2015-09-20 18:08 45
	if ( !SUCCEEDED( SHGetFolderPathA( NULL, folderId, NULL, 0, szPath ) ) )
1213f3f3 sago007 2015-09-19 19:24 46
	{
1213f3f3 sago007 2015-09-19 19:24 47
		throw std::runtime_error(errorMsg);
1213f3f3 sago007 2015-09-19 19:24 48
	}
1213f3f3 sago007 2015-09-19 19:24 49
	return szPath;
1213f3f3 sago007 2015-09-19 19:24 50
}
1213f3f3 sago007 2015-09-19 19:24 51
1213f3f3 sago007 2015-09-19 19:24 52
static std::string GetAppData() {
1213f3f3 sago007 2015-09-19 19:24 53
	return GetWindowsFolder(CSIDL_APPDATA, "RoamingAppData could not be found");
1213f3f3 sago007 2015-09-19 19:24 54
}
1213f3f3 sago007 2015-09-19 19:24 55
1213f3f3 sago007 2015-09-19 19:24 56
static std::string GetAppDataCommon() {
1213f3f3 sago007 2015-09-19 19:24 57
	return GetWindowsFolder(CSIDL_COMMON_APPDATA, "Common appdata could not be found");
1213f3f3 sago007 2015-09-19 19:24 58
}
1213f3f3 sago007 2015-09-19 19:24 59
1213f3f3 sago007 2015-09-19 19:24 60
static std::string GetAppDataLocal() {
1213f3f3 sago007 2015-09-19 19:24 61
	return GetWindowsFolder(CSIDL_LOCAL_APPDATA, "LocalAppData could not be found");
1213f3f3 sago007 2015-09-19 19:24 62
}
1eaaab72 sago007 2015-10-26 17:23 63
#elif defined(__APPLE__)
1eaaab72 sago007 2015-10-26 17:23 64
#include <CoreServices/CoreServices.h>
1eaaab72 sago007 2015-10-26 17:23 65
1eaaab72 sago007 2015-10-26 17:23 66
static std::string GetMacFolder(OSType folderType, const char* errorMsg) {
1eaaab72 sago007 2015-10-26 17:23 67
	std::string ret;
1eaaab72 sago007 2015-10-26 17:23 68
	FSRef ref;
1eaaab72 sago007 2015-10-26 17:23 69
    char path[PATH_MAX];
1eaaab72 sago007 2015-10-26 17:23 70
    OSStatus err = FSFindFolder( kUserDomain, folderType, kCreateFolder, &ref );
1eaaab72 sago007 2015-10-26 17:23 71
	if (err != noErr) {
1eaaab72 sago007 2015-10-26 17:23 72
		throw std::runtime_error(errorMsg);
1eaaab72 sago007 2015-10-26 17:23 73
	}
1eaaab72 sago007 2015-10-26 17:23 74
    FSRefMakePath( &ref, (UInt8*)&path, PATH_MAX );
1eaaab72 sago007 2015-10-26 17:23 75
	ret = path;
1eaaab72 sago007 2015-10-26 17:23 76
	return ret;
1eaaab72 sago007 2015-10-26 17:23 77
}
1eaaab72 sago007 2015-10-26 17:23 78
1213f3f3 sago007 2015-09-19 19:24 79
#else
1213f3f3 sago007 2015-09-19 19:24 80
#include <map>
1213f3f3 sago007 2015-09-19 19:24 81
#include <fstream>
dcb0fec2 sago007 2015-09-21 17:50 82
#include <pwd.h>
dcb0fec2 sago007 2015-09-21 17:50 83
#include <unistd.h>
dcb0fec2 sago007 2015-09-21 17:50 84
#include <sys/types.h>
1213f3f3 sago007 2015-09-19 19:24 85
//Typically Linux. For easy reading the comments will just say Linux but should work with most *nixes
1213f3f3 sago007 2015-09-19 19:24 86
1213f3f3 sago007 2015-09-19 19:24 87
static void throwOnRelative(const char* envName, const char* envValue) {
1213f3f3 sago007 2015-09-19 19:24 88
	if (envValue[0] != '/') {
1213f3f3 sago007 2015-09-19 19:24 89
		char buffer[200];
1213f3f3 sago007 2015-09-19 19:24 90
		snprintf(buffer, sizeof(buffer), "Environment \"%s\" does not start with an '/'. XDG specifies that the value must be absolute. The current value is: \"%s\"", envName, envValue);
1213f3f3 sago007 2015-09-19 19:24 91
		throw std::runtime_error(buffer);
1213f3f3 sago007 2015-09-19 19:24 92
	}
1213f3f3 sago007 2015-09-19 19:24 93
}
1213f3f3 sago007 2015-09-19 19:24 94
dcb0fec2 sago007 2015-09-21 17:50 95
/**
dcb0fec2 sago007 2015-09-21 17:50 96
 * Retrives the effective user's home dir. 
dcb0fec2 sago007 2015-09-21 17:50 97
 * If the user is running as root we ignore the HOME environment. It works badly with sudo. 
dcb0fec2 sago007 2015-09-21 17:50 98
 * Writing to $HOME as root implies security concerns that a multiplatform program cannot be assumed to handle.
dcb0fec2 sago007 2015-09-21 17:50 99
 * @return The home directory. HOME environment is respected for non-root users if it exists. 
dcb0fec2 sago007 2015-09-21 17:50 100
 */
1213f3f3 sago007 2015-09-19 19:24 101
static std::string getHome() {
1213f3f3 sago007 2015-09-19 19:24 102
	std::string res;
dcb0fec2 sago007 2015-09-21 17:50 103
	int uid = getuid();
dcb0fec2 sago007 2015-09-21 17:50 104
	const char* homeEnv = getenv("HOME");
dcb0fec2 sago007 2015-09-21 17:50 105
	if ( uid != 0 && homeEnv) {
dcb0fec2 sago007 2015-09-21 17:50 106
		//We only acknowlegde HOME if not root.
dcb0fec2 sago007 2015-09-21 17:50 107
		res = homeEnv;
dcb0fec2 sago007 2015-09-21 17:50 108
		return res;
dcb0fec2 sago007 2015-09-21 17:50 109
	}
dcb0fec2 sago007 2015-09-21 17:50 110
	struct passwd *pw = getpwuid(uid);
dcb0fec2 sago007 2015-09-21 17:50 111
	if (!pw) {
dcb0fec2 sago007 2015-09-21 17:50 112
		throw std::runtime_error("Unable to get passwd struct.");
dcb0fec2 sago007 2015-09-21 17:50 113
	}
dcb0fec2 sago007 2015-09-21 17:50 114
	const char* tempRes = pw->pw_dir;
1213f3f3 sago007 2015-09-19 19:24 115
	if (!tempRes) {
dcb0fec2 sago007 2015-09-21 17:50 116
		throw std::runtime_error("User has no home directory");
1213f3f3 sago007 2015-09-19 19:24 117
	}
1213f3f3 sago007 2015-09-19 19:24 118
	res = tempRes;
1213f3f3 sago007 2015-09-19 19:24 119
	return res;
1213f3f3 sago007 2015-09-19 19:24 120
}
1213f3f3 sago007 2015-09-19 19:24 121
1213f3f3 sago007 2015-09-19 19:24 122
static std::string getLinuxFolderDefault(const char* envName, const char* defaultRelativePath) {
1213f3f3 sago007 2015-09-19 19:24 123
	std::string res;
1213f3f3 sago007 2015-09-19 19:24 124
	const char* tempRes = getenv(envName);
1213f3f3 sago007 2015-09-19 19:24 125
	if (tempRes) {
1213f3f3 sago007 2015-09-19 19:24 126
		throwOnRelative(envName, tempRes);
1213f3f3 sago007 2015-09-19 19:24 127
		res = tempRes;
1213f3f3 sago007 2015-09-19 19:24 128
		return res;
1213f3f3 sago007 2015-09-19 19:24 129
	}
1213f3f3 sago007 2015-09-19 19:24 130
	res = getHome() + "/" + defaultRelativePath;
1213f3f3 sago007 2015-09-19 19:24 131
	return res;
1213f3f3 sago007 2015-09-19 19:24 132
}
1213f3f3 sago007 2015-09-19 19:24 133
1213f3f3 sago007 2015-09-19 19:24 134
static void appendExtraFoldersTokenizer(const char* envName, const char* envValue, std::vector<std::string>& folders) {
4c793595 sago007 2016-02-16 17:16 135
	std::vector<char> buffer(envValue, envValue + strlen(envValue));
1213f3f3 sago007 2015-09-19 19:24 136
	char *saveptr;
4c793595 sago007 2016-02-16 17:16 137
	const char* p = strtok_r (buffer.data(), ":", &saveptr);
1213f3f3 sago007 2015-09-19 19:24 138
	while (p != NULL) {
1213f3f3 sago007 2015-09-19 19:24 139
		if (p[0] == '/') {
1213f3f3 sago007 2015-09-19 19:24 140
			folders.push_back(p);
1213f3f3 sago007 2015-09-19 19:24 141
		}
1213f3f3 sago007 2015-09-19 19:24 142
		else {
1213f3f3 sago007 2015-09-19 19:24 143
			//Unless the system is wrongly configured this should never happen... But of course some systems will be incorectly configured.
1213f3f3 sago007 2015-09-19 19:24 144
			//The XDG documentation indicates that the folder should be ignored but that the program should continue.
1213f3f3 sago007 2015-09-19 19:24 145
			std::cerr << "Skipping path \"" << p << "\" in \"" << envName << "\" because it does not start with a \"/\"" << std::endl;
1213f3f3 sago007 2015-09-19 19:24 146
		}
1213f3f3 sago007 2015-09-19 19:24 147
		p = strtok_r (NULL, ":", &saveptr);
1213f3f3 sago007 2015-09-19 19:24 148
	}
1213f3f3 sago007 2015-09-19 19:24 149
}
1213f3f3 sago007 2015-09-19 19:24 150
1213f3f3 sago007 2015-09-19 19:24 151
static void appendExtraFolders(const char* envName, const char* defaultValue, std::vector<std::string>& folders) {
1213f3f3 sago007 2015-09-19 19:24 152
	const char* envValue = getenv(envName);
1213f3f3 sago007 2015-09-19 19:24 153
	if (!envValue) {
1213f3f3 sago007 2015-09-19 19:24 154
		envValue = defaultValue;
1213f3f3 sago007 2015-09-19 19:24 155
	}
1213f3f3 sago007 2015-09-19 19:24 156
	appendExtraFoldersTokenizer(envName, envValue, folders);
1213f3f3 sago007 2015-09-19 19:24 157
}
1213f3f3 sago007 2015-09-19 19:24 158
1213f3f3 sago007 2015-09-19 19:24 159
#endif
1213f3f3 sago007 2015-09-19 19:24 160
1213f3f3 sago007 2015-09-19 19:24 161
1213f3f3 sago007 2015-09-19 19:24 162
namespace sago {
1213f3f3 sago007 2015-09-19 19:24 163
1213f3f3 sago007 2015-09-19 19:24 164
std::string getDataHome() {
1213f3f3 sago007 2015-09-19 19:24 165
#if defined(_WIN32)
1213f3f3 sago007 2015-09-19 19:24 166
	return GetAppData();
1eaaab72 sago007 2015-10-26 17:23 167
#elif defined(__APPLE__)
1eaaab72 sago007 2015-10-26 17:23 168
	return GetMacFolder(kApplicationSupportFolderType, "Failed to find the Application Support Folder");
1213f3f3 sago007 2015-09-19 19:24 169
#else
1213f3f3 sago007 2015-09-19 19:24 170
	return getLinuxFolderDefault("XDG_DATA_HOME", ".local/share");
1213f3f3 sago007 2015-09-19 19:24 171
#endif
1213f3f3 sago007 2015-09-19 19:24 172
}
1213f3f3 sago007 2015-09-19 19:24 173
1213f3f3 sago007 2015-09-19 19:24 174
std::string getConfigHome() {
1213f3f3 sago007 2015-09-19 19:24 175
#if defined(_WIN32)
1213f3f3 sago007 2015-09-19 19:24 176
	return GetAppData();
1eaaab72 sago007 2015-10-26 17:23 177
#elif defined(__APPLE__)
1eaaab72 sago007 2015-10-26 17:23 178
	return GetMacFolder(kApplicationSupportFolderType, "Failed to find the Application Support Folder");
1213f3f3 sago007 2015-09-19 19:24 179
#else
1213f3f3 sago007 2015-09-19 19:24 180
	return getLinuxFolderDefault("XDG_CONFIG_HOME", ".config");
1213f3f3 sago007 2015-09-19 19:24 181
#endif
1213f3f3 sago007 2015-09-19 19:24 182
}
1213f3f3 sago007 2015-09-19 19:24 183
1213f3f3 sago007 2015-09-19 19:24 184
std::string getCacheDir() {
1213f3f3 sago007 2015-09-19 19:24 185
#if defined(_WIN32)
1213f3f3 sago007 2015-09-19 19:24 186
	return GetAppDataLocal();
1eaaab72 sago007 2015-10-26 17:23 187
#elif defined(__APPLE__)
1eaaab72 sago007 2015-10-26 17:23 188
	return GetMacFolder(kCachedDataFolderType, "Failed to find the Application Support Folder");
1213f3f3 sago007 2015-09-19 19:24 189
#else
1213f3f3 sago007 2015-09-19 19:24 190
	return getLinuxFolderDefault("XDG_CONFIG_HOME", ".cache");
1213f3f3 sago007 2015-09-19 19:24 191
#endif
1213f3f3 sago007 2015-09-19 19:24 192
}
1213f3f3 sago007 2015-09-19 19:24 193
1213f3f3 sago007 2015-09-19 19:24 194
void appendAdditionalDataDirectories(std::vector<std::string>& homes) {
1213f3f3 sago007 2015-09-19 19:24 195
#if defined(_WIN32)
1213f3f3 sago007 2015-09-19 19:24 196
	homes.push_back(GetAppDataCommon());
1eaaab72 sago007 2015-10-26 17:23 197
#elif defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 198
#else
1213f3f3 sago007 2015-09-19 19:24 199
	appendExtraFolders("XDG_DATA_DIRS", "/usr/local/share/:/usr/share/", homes);
1213f3f3 sago007 2015-09-19 19:24 200
#endif
1213f3f3 sago007 2015-09-19 19:24 201
}
1213f3f3 sago007 2015-09-19 19:24 202
1213f3f3 sago007 2015-09-19 19:24 203
void appendAdditionalConfigDirectories(std::vector<std::string>& homes) {
1213f3f3 sago007 2015-09-19 19:24 204
#if defined(_WIN32)
1213f3f3 sago007 2015-09-19 19:24 205
	homes.push_back(GetAppDataCommon());
1eaaab72 sago007 2015-10-26 17:23 206
#elif defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 207
#else
1213f3f3 sago007 2015-09-19 19:24 208
	appendExtraFolders("XDG_CONFIG_DIRS", "/etc/xdg", homes);
1213f3f3 sago007 2015-09-19 19:24 209
#endif
1213f3f3 sago007 2015-09-19 19:24 210
}
1213f3f3 sago007 2015-09-19 19:24 211
1213f3f3 sago007 2015-09-19 19:24 212
#if defined(_WIN32)
1eaaab72 sago007 2015-10-26 17:23 213
#elif defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 214
#else
1213f3f3 sago007 2015-09-19 19:24 215
struct PlatformFolders::PlatformFoldersData {
1213f3f3 sago007 2015-09-19 19:24 216
	std::map<std::string, std::string> folders;
1213f3f3 sago007 2015-09-19 19:24 217
};
1213f3f3 sago007 2015-09-19 19:24 218
1213f3f3 sago007 2015-09-19 19:24 219
static void PlatformFoldersAddFromFile(const std::string& filename, std::map<std::string, std::string>& folders) {
1213f3f3 sago007 2015-09-19 19:24 220
	std::ifstream infile(filename.c_str());
1213f3f3 sago007 2015-09-19 19:24 221
	std::string line;
1213f3f3 sago007 2015-09-19 19:24 222
	while (std::getline(infile, line)) {
1213f3f3 sago007 2015-09-19 19:24 223
		if (line.length() == 0 || line.at(0) == '#') {
1213f3f3 sago007 2015-09-19 19:24 224
			continue;
1213f3f3 sago007 2015-09-19 19:24 225
		}
1213f3f3 sago007 2015-09-19 19:24 226
		std::size_t splitPos = line.find("=");
1213f3f3 sago007 2015-09-19 19:24 227
		std::string key = line.substr(0, splitPos);
1213f3f3 sago007 2015-09-19 19:24 228
		std::string value = line.substr(splitPos+2, line.length()-splitPos-3);
1213f3f3 sago007 2015-09-19 19:24 229
		folders[key] = value;
1213f3f3 sago007 2015-09-19 19:24 230
		//std::cout << key << " : " << value << std::endl;
1213f3f3 sago007 2015-09-19 19:24 231
	}
1213f3f3 sago007 2015-09-19 19:24 232
}
1213f3f3 sago007 2015-09-19 19:24 233
1213f3f3 sago007 2015-09-19 19:24 234
static void PlatformFoldersFillData(std::map<std::string, std::string>& folders) {
1213f3f3 sago007 2015-09-19 19:24 235
	folders["XDG_DOCUMENTS_DIR"] = "$HOME/Documents";
1213f3f3 sago007 2015-09-19 19:24 236
	folders["XDG_DESKTOP_DIR"] = "$HOME/Desktop";
1213f3f3 sago007 2015-09-19 19:24 237
	folders["XDG_DOWNLOAD_DIR"] = "$HOME/Downloads";
1213f3f3 sago007 2015-09-19 19:24 238
	folders["XDG_MUSIC_DIR"] = "$HOME/Music";
1213f3f3 sago007 2015-09-19 19:24 239
	folders["XDG_PICTURES_DIR"] = "$HOME/Pictures";
1213f3f3 sago007 2015-09-19 19:24 240
	folders["XDG_PUBLICSHARE_DIR"] = "$HOME/Public";
1213f3f3 sago007 2015-09-19 19:24 241
	folders["XDG_TEMPLATES_DIR"] = "$HOME/.Templates";
1213f3f3 sago007 2015-09-19 19:24 242
	folders["XDG_VIDEOS_DIR"] = "$HOME/Videos";
1213f3f3 sago007 2015-09-19 19:24 243
	PlatformFoldersAddFromFile( getConfigHome()+"/user-dirs.dirs", folders);
1213f3f3 sago007 2015-09-19 19:24 244
	for (std::map<std::string, std::string>::iterator itr = folders.begin() ; itr != folders.end() ; itr ++ ) {
1213f3f3 sago007 2015-09-19 19:24 245
		std::string& value = itr->second;
1213f3f3 sago007 2015-09-19 19:24 246
		if (value.compare(0, 5, "$HOME") == 0) {
1213f3f3 sago007 2015-09-19 19:24 247
			value = getHome() + value.substr(5, std::string::npos);
1213f3f3 sago007 2015-09-19 19:24 248
		}
1213f3f3 sago007 2015-09-19 19:24 249
	}
1213f3f3 sago007 2015-09-19 19:24 250
}
1213f3f3 sago007 2015-09-19 19:24 251
#endif
1213f3f3 sago007 2015-09-19 19:24 252
1213f3f3 sago007 2015-09-19 19:24 253
PlatformFolders::PlatformFolders() {
1213f3f3 sago007 2015-09-19 19:24 254
#if defined(_WIN32)
1eaaab72 sago007 2015-10-26 17:23 255
#elif defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 256
#else
1213f3f3 sago007 2015-09-19 19:24 257
	this->data = new PlatformFolders::PlatformFoldersData();
1213f3f3 sago007 2015-09-19 19:24 258
	try {
1213f3f3 sago007 2015-09-19 19:24 259
		PlatformFoldersFillData(data->folders);
1213f3f3 sago007 2015-09-19 19:24 260
	} catch (...) {
1213f3f3 sago007 2015-09-19 19:24 261
		delete this->data;
1213f3f3 sago007 2015-09-19 19:24 262
		throw;
1213f3f3 sago007 2015-09-19 19:24 263
	}
1213f3f3 sago007 2015-09-19 19:24 264
#endif
1213f3f3 sago007 2015-09-19 19:24 265
}
1213f3f3 sago007 2015-09-19 19:24 266
1213f3f3 sago007 2015-09-19 19:24 267
PlatformFolders::~PlatformFolders() {
1213f3f3 sago007 2015-09-19 19:24 268
#if defined(_WIN32)
1eaaab72 sago007 2015-10-26 17:23 269
#elif defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 270
#else
1213f3f3 sago007 2015-09-19 19:24 271
	delete this->data;
1213f3f3 sago007 2015-09-19 19:24 272
#endif
1213f3f3 sago007 2015-09-19 19:24 273
}
1213f3f3 sago007 2015-09-19 19:24 274
1213f3f3 sago007 2015-09-19 19:24 275
std::string PlatformFolders::getDocumentsFolder() const {
1213f3f3 sago007 2015-09-19 19:24 276
#if defined(_WIN32)
1213f3f3 sago007 2015-09-19 19:24 277
	return GetWindowsFolder(CSIDL_PERSONAL, "Failed to find My Documents folder");
1eaaab72 sago007 2015-10-26 17:23 278
#elif defined(__APPLE__)
1eaaab72 sago007 2015-10-26 17:23 279
	return GetMacFolder(kDocumentsFolderType, "Failed to find Documents Folder");
1213f3f3 sago007 2015-09-19 19:24 280
#else
1213f3f3 sago007 2015-09-19 19:24 281
	return data->folders["XDG_DOCUMENTS_DIR"];
1213f3f3 sago007 2015-09-19 19:24 282
#endif
1213f3f3 sago007 2015-09-19 19:24 283
}
1213f3f3 sago007 2015-09-19 19:24 284
1213f3f3 sago007 2015-09-19 19:24 285
std::string PlatformFolders::getDesktopFolder() const {
1213f3f3 sago007 2015-09-19 19:24 286
#if defined(_WIN32)
1213f3f3 sago007 2015-09-19 19:24 287
	return GetWindowsFolder(CSIDL_DESKTOP, "Failed to find Desktop folder");
1eaaab72 sago007 2015-10-26 17:23 288
#elif defined(__APPLE__)
1eaaab72 sago007 2015-10-26 17:23 289
	return GetMacFolder(kDesktopFolderType, "Failed to find Desktop folder");
1213f3f3 sago007 2015-09-19 19:24 290
#else
1213f3f3 sago007 2015-09-19 19:24 291
	return data->folders["XDG_DESKTOP_DIR"];
1213f3f3 sago007 2015-09-19 19:24 292
#endif
1213f3f3 sago007 2015-09-19 19:24 293
}
1213f3f3 sago007 2015-09-19 19:24 294
1213f3f3 sago007 2015-09-19 19:24 295
std::string PlatformFolders::getPicturesFolder() const {
1213f3f3 sago007 2015-09-19 19:24 296
#if defined(_WIN32)
1213f3f3 sago007 2015-09-19 19:24 297
	return GetWindowsFolder(CSIDL_MYPICTURES, "Failed to find My Pictures folder");
1eaaab72 sago007 2015-10-26 17:23 298
#elif defined(__APPLE__)
1eaaab72 sago007 2015-10-26 17:23 299
	return GetMacFolder(kPictureDocumentsFolderType, "Failed to find Picture folder");
1213f3f3 sago007 2015-09-19 19:24 300
#else
1213f3f3 sago007 2015-09-19 19:24 301
	return data->folders["XDG_PICTURES_DIR"];
1213f3f3 sago007 2015-09-19 19:24 302
#endif
1213f3f3 sago007 2015-09-19 19:24 303
}
1213f3f3 sago007 2015-09-19 19:24 304
1213f3f3 sago007 2015-09-19 19:24 305
std::string PlatformFolders::getDownloadFolder1() const {
1213f3f3 sago007 2015-09-19 19:24 306
#if defined(_WIN32)
1213f3f3 sago007 2015-09-19 19:24 307
	//Pre Vista. Files was downloaded to the desktop
1213f3f3 sago007 2015-09-19 19:24 308
	return GetWindowsFolder(CSIDL_DESKTOP, "Failed to find My Downloads (Desktop) folder");
1eaaab72 sago007 2015-10-26 17:23 309
#elif defined(__APPLE__)
1eaaab72 sago007 2015-10-26 17:23 310
	return GetMacFolder(kDownloadsFolderType, "Failed to find Download folder");
1213f3f3 sago007 2015-09-19 19:24 311
#else
1213f3f3 sago007 2015-09-19 19:24 312
	return data->folders["XDG_DOWNLOAD_DIR"];
1213f3f3 sago007 2015-09-19 19:24 313
#endif
1213f3f3 sago007 2015-09-19 19:24 314
}
1213f3f3 sago007 2015-09-19 19:24 315
1213f3f3 sago007 2015-09-19 19:24 316
std::string PlatformFolders::getMusicFolder() const {
1213f3f3 sago007 2015-09-19 19:24 317
#if defined(_WIN32)
1213f3f3 sago007 2015-09-19 19:24 318
	return GetWindowsFolder(CSIDL_MYMUSIC, "Failed to find My Music folder");
1eaaab72 sago007 2015-10-26 17:23 319
#elif defined(__APPLE__)
1eaaab72 sago007 2015-10-26 17:23 320
	return GetMacFolder(kMusicDocumentsFolderType, "Failed to find Music folder");
1213f3f3 sago007 2015-09-19 19:24 321
#else
1213f3f3 sago007 2015-09-19 19:24 322
	return data->folders["XDG_MUSIC_DIR"];
1213f3f3 sago007 2015-09-19 19:24 323
#endif
1213f3f3 sago007 2015-09-19 19:24 324
}
1213f3f3 sago007 2015-09-19 19:24 325
1213f3f3 sago007 2015-09-19 19:24 326
std::string PlatformFolders::getVideoFolder() const {
1213f3f3 sago007 2015-09-19 19:24 327
#if defined(_WIN32)
1213f3f3 sago007 2015-09-19 19:24 328
	return GetWindowsFolder(CSIDL_MYVIDEO, "Failed to find My Video folder");
1eaaab72 sago007 2015-10-26 17:23 329
#elif defined(__APPLE__)
1eaaab72 sago007 2015-10-26 17:23 330
	return GetMacFolder(kMovieDocumentsFolderType, "Failed to find Movie folder");
1213f3f3 sago007 2015-09-19 19:24 331
#else
1213f3f3 sago007 2015-09-19 19:24 332
	return data->folders["XDG_VIDEOS_DIR"];
1213f3f3 sago007 2015-09-19 19:24 333
#endif
1213f3f3 sago007 2015-09-19 19:24 334
}
1213f3f3 sago007 2015-09-19 19:24 335
1213f3f3 sago007 2015-09-19 19:24 336
std::string PlatformFolders::getSaveGamesFolder1() const {
1213f3f3 sago007 2015-09-19 19:24 337
#if defined(_WIN32)
1213f3f3 sago007 2015-09-19 19:24 338
	//A dedicated Save Games folder was not introduced until Vista. For XP and older save games are most often saved in a normal folder named "My Games".
1213f3f3 sago007 2015-09-19 19:24 339
	//Data that should not be user accessible should be placed under GetDataHome() instead
3984e19e sago007 2015-09-20 11:12 340
	return GetWindowsFolder(CSIDL_PERSONAL, "Failed to find My Documents folder")+"\\My Games";
1eaaab72 sago007 2015-10-26 17:23 341
#elif defined(__APPLE__)
1eaaab72 sago007 2015-10-26 17:23 342
	return GetMacFolder(kApplicationSupportFolderType, "Failed to find Application Support Folder");
1213f3f3 sago007 2015-09-19 19:24 343
#else
1213f3f3 sago007 2015-09-19 19:24 344
	return getDataHome();
1213f3f3 sago007 2015-09-19 19:24 345
#endif
1213f3f3 sago007 2015-09-19 19:24 346
}
1213f3f3 sago007 2015-09-19 19:24 347
1213f3f3 sago007 2015-09-19 19:24 348
1213f3f3 sago007 2015-09-19 19:24 349
1213f3f3 sago007 2015-09-19 19:24 350
}  //namespace sago
1970-01-01 00:00 351