git repos / blockattack-game

blame: source/code/sago/platform_folders.cpp

normal view · raw

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