-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
paths.cpp
280 lines (210 loc) · 7.32 KB
/
paths.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
Copyright (C) 2016 - 2018 by Iris Morelle <shadowm2006@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "desktop/paths.hpp"
#include "game_config.hpp"
#include "filesystem.hpp"
#include "gettext.hpp"
#include "log.hpp"
#include "preferences/general.hpp"
#include "serialization/unicode.hpp"
#if !defined(_WIN32) && !defined(__APPLE__)
#include <boost/filesystem.hpp>
#endif
#ifndef _WIN32
// For username stuff on Unix:
#include <pwd.h>
#include <sys/types.h>
#else // _WIN32
#ifndef UNICODE
#define UNICODE
#endif
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shlobj.h>
#endif
static lg::log_domain log_desktop("desktop");
#define ERR_DU LOG_STREAM(err, log_desktop)
#define LOG_DU LOG_STREAM(info, log_desktop)
#define DBG_DU LOG_STREAM(debug, log_desktop)
namespace desktop
{
namespace
{
void enumerate_storage_devices(std::vector<path_info>& res)
{
#ifdef _WIN32
const DWORD drive_table = GetLogicalDrives();
for(unsigned n = 0; n < 26; ++n) {
if((drive_table >> n) & 1) {
std::string u8drive = "A:";
u8drive[0] += n;
LOG_DU << "enumerate_win32_drives(): " << u8drive << " is reported to be present\n";
wchar_t drive[] = L"A:\\";
drive[0] += n;
const DWORD label_bufsize = MAX_PATH + 1;
wchar_t label[label_bufsize] { 0 };
if(GetVolumeInformation(drive, label, label_bufsize, nullptr, nullptr, nullptr, nullptr, 0) == 0) {
// Probably an empty removable drive, just ignore it and carry on.
const DWORD err = GetLastError();
LOG_DU << "enumerate_win32_drives(): GetVolumeInformation() failed (" << err << ")\n";
continue;
}
// Trailing slash so that we don't get compatibility per-drive working dirs
// involved in path resolution.
res.push_back({u8drive, unicode_cast<std::string>(std::wstring{label}), u8drive + '\\'});
}
}
#elif defined(__APPLE__)
// Probably as unreliable as /media|/mnt on other platforms, not worth
// examining in detail.
res.push_back({{ N_("filesystem_path_system^Volumes"), GETTEXT_DOMAIN }, "", "/Volumes"});
#else
namespace bsys = boost::system;
namespace bfs = boost::filesystem;
// These are either used as mount points themselves, or host mount points. The
// reasoning here is that if any or all of them are non-empty, they are
// probably used for _something_ that might be of interest to the user (if not
// directly and actively controlled by the user themselves).
static const std::vector<std::string> candidates { "/media", "/mnt" };
for(const auto& mnt : candidates) {
bsys::error_code e;
try {
if(bfs::is_directory(mnt, e) && !bfs::is_empty(mnt, e) && !e) {
DBG_DU << "enumerate_mount_parents(): " << mnt << " appears to be a non-empty dir\n";
res.push_back({mnt, "", mnt});
}
}
catch(...) {
//bool is_empty(const path& p, system::error_code& ec) might throw.
//For example if you have no permission on that directory. Don't list the file in that case.
DBG_DU << "caught exception in enumerate_storage_devices\n";
}
}
#endif
}
bool have_path(const std::vector<path_info>& pathset, const std::string& path)
{
for(const auto& pinfo : pathset) {
if(pinfo.path == path) {
return true;
}
}
return false;
}
inline std::string pretty_path(const std::string& path)
{
return filesystem::normalize_path(path, true, true);
}
inline config get_bookmarks_config()
{
const config& cfg = preferences::get_child("dir_bookmarks");
return cfg ? cfg : config{};
}
inline void commit_bookmarks_config(config& cfg)
{
preferences::set_child("dir_bookmarks", cfg);
}
} // unnamed namespace
std::string user_profile_dir()
{
#ifndef _WIN32
// TODO: The filesystem API uses $HOME for this purpose, which may be
// overridden or missing. Not sure which one really makes more sense
// for us here.
const passwd* const pwd = getpwuid(geteuid());
if(!pwd || !pwd->pw_dir || !*pwd->pw_dir) {
return "";
}
return pwd->pw_dir;
#else // _WIN32
wchar_t profile_path[MAX_PATH];
HRESULT res = SHGetFolderPath(nullptr, CSIDL_PROFILE, nullptr, SHGFP_TYPE_CURRENT, profile_path);
return res != S_OK ? "" : unicode_cast<std::string>(std::wstring{profile_path});
#endif // _WIN32
}
std::string path_info::display_name() const
{
return label.empty() ? name : label + " (" + name + ")";
}
std::ostream& operator<<(std::ostream& os, const path_info& pinf)
{
return os << pinf.name << " [" << pinf.label << "] - " << pinf.path;
}
std::vector<path_info> game_paths(unsigned path_types)
{
static const std::string& game_bin_dir = pretty_path(filesystem::get_exe_dir());
static const std::string& game_data_dir = pretty_path(game_config::path);
static const std::string& game_user_data_dir = pretty_path(filesystem::get_user_data_dir());
static const std::string& game_user_pref_dir = pretty_path(filesystem::get_user_config_dir());
std::vector<path_info> res;
if(path_types & GAME_BIN_DIR && !have_path(res, game_bin_dir)) {
res.push_back({{ N_("filesystem_path_game^Game executables"), GETTEXT_DOMAIN }, "", game_bin_dir});
}
if(path_types & GAME_CORE_DATA_DIR && !have_path(res, game_data_dir)) {
res.push_back({{ N_("filesystem_path_game^Game data"), GETTEXT_DOMAIN }, "", game_data_dir});
}
if(path_types & GAME_USER_DATA_DIR && !have_path(res, game_user_data_dir)) {
res.push_back({{ N_("filesystem_path_game^User data"), GETTEXT_DOMAIN }, "", game_user_data_dir});
}
if(path_types & GAME_USER_PREFS_DIR && !have_path(res, game_user_pref_dir)) {
res.push_back({{ N_("filesystem_path_game^User preferences"), GETTEXT_DOMAIN }, "", game_user_pref_dir});
}
return res;
}
std::vector<path_info> system_paths(unsigned path_types)
{
static const std::string& home_dir = user_profile_dir();
std::vector<path_info> res;
if(path_types & SYSTEM_USER_PROFILE && !home_dir.empty()) {
res.push_back({{ N_("filesystem_path_system^Home"), GETTEXT_DOMAIN }, "", home_dir});
}
if(path_types & SYSTEM_ALL_DRIVES) {
enumerate_storage_devices(res);
}
#ifndef _WIN32
if(path_types & SYSTEM_ROOTFS) {
res.push_back({{ N_("filesystem_path_system^Root"), GETTEXT_DOMAIN }, "", "/"});
}
#endif
return res;
}
unsigned add_user_bookmark(const std::string& label, const std::string& path)
{
config cfg = get_bookmarks_config();
config& bookmark_cfg = cfg.add_child("bookmark");
bookmark_cfg["label"] = label;
bookmark_cfg["path"] = path;
commit_bookmarks_config(cfg);
return cfg.child_count("bookmark");
}
void remove_user_bookmark(unsigned index)
{
config cfg = get_bookmarks_config();
const unsigned prev_size = cfg.child_count("bookmark");
if(index < prev_size) {
cfg.remove_child("bookmark", index);
}
commit_bookmarks_config(cfg);
}
std::vector<bookmark_info> user_bookmarks()
{
const config& cfg = get_bookmarks_config();
std::vector<bookmark_info> res;
if(cfg.has_child("bookmark")) {
for(const config& bookmark_cfg : cfg.child_range("bookmark")) {
res.push_back({ bookmark_cfg["label"], bookmark_cfg["path"] });
}
}
return res;
}
} // namespace desktop