-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
stdpaths.cpp
380 lines (308 loc) · 10.1 KB
/
stdpaths.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
///////////////////////////////////////////////////////////////////////////////
// Name: src/msw/stdpaths.cpp
// Purpose: wxStandardPaths implementation for Win32
// Author: Vadim Zeitlin
// Created: 2004-10-19
// Copyright: (c) 2004 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
// for compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#if wxUSE_STDPATHS
#include "wx/stdpaths.h"
#ifndef WX_PRECOMP
#include "wx/utils.h"
#endif //WX_PRECOMP
#include "wx/dynlib.h"
#include "wx/filename.h"
#include "wx/msw/private.h"
#include "wx/msw/wrapshl.h"
#include "wx/msw/private/cotaskmemptr.h"
#include <initguid.h>
// ----------------------------------------------------------------------------
// types
// ----------------------------------------------------------------------------
typedef HRESULT (WINAPI *SHGetKnownFolderPath_t)(const GUID&, DWORD, HANDLE, PWSTR *);
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// used in our wxLogTrace messages
#define TRACE_MASK wxT("stdpaths")
#ifndef CSIDL_PERSONAL
#define CSIDL_PERSONAL 0x0005
#endif
// ----------------------------------------------------------------------------
// module globals
// ----------------------------------------------------------------------------
namespace
{
DEFINE_GUID(wxFOLDERID_Downloads,
0x374de290, 0x123f, 0x4565, 0x91, 0x64, 0x39, 0xc4, 0x92, 0x5e, 0x46, 0x7b);
struct ShellFunctions
{
ShellFunctions()
{
pSHGetKnownFolderPath = nullptr;
initialized = false;
}
SHGetKnownFolderPath_t pSHGetKnownFolderPath;
bool initialized;
};
// in spite of using a static variable, this is MT-safe as in the worst case it
// results in initializing the function pointer several times -- but this is
// harmless
ShellFunctions gs_shellFuncs;
// ----------------------------------------------------------------------------
// private functions
// ----------------------------------------------------------------------------
void ResolveShellFunctions()
{
// start with the newest functions, fall back to the oldest ones
// first check for SHGetFolderPath (shell32.dll 5.0)
wxString shellDllName(wxT("shell32"));
wxDynamicLibrary dllShellFunctions( shellDllName );
if ( !dllShellFunctions.IsLoaded() )
{
wxLogTrace(TRACE_MASK, wxT("Failed to load %s.dll"), shellDllName );
}
// don't give errors if the functions are unavailable, we're ready to deal
// with this
wxLogNull noLog;
gs_shellFuncs.pSHGetKnownFolderPath = (SHGetKnownFolderPath_t)
dllShellFunctions.GetSymbol("SHGetKnownFolderPath");
// shell32.dll is going to be unloaded, but it still remains in memory
// because we also link to it statically, so it's ok
gs_shellFuncs.initialized = true;
}
} // anonymous namespace
// ============================================================================
// wxStandardPaths implementation
// ============================================================================
// ----------------------------------------------------------------------------
// private helpers
// ----------------------------------------------------------------------------
/* static */
wxString wxStandardPaths::DoGetDirectory(int csidl)
{
wxString dir;
HRESULT hr = E_FAIL;
hr = ::SHGetFolderPath
(
nullptr, // parent window, not used
csidl,
nullptr, // access token (current user)
SHGFP_TYPE_CURRENT, // current path, not just default value
wxStringBuffer(dir, MAX_PATH)
);
if ( hr == E_FAIL )
{
// directory doesn't exist, maybe we can get its default value?
hr = ::SHGetFolderPath
(
nullptr,
csidl,
nullptr,
SHGFP_TYPE_DEFAULT,
wxStringBuffer(dir, MAX_PATH)
);
}
return dir;
}
wxString wxStandardPaths::DoGetKnownFolder(const GUID& rfid)
{
if (!gs_shellFuncs.initialized)
ResolveShellFunctions();
wxString dir;
if ( gs_shellFuncs.pSHGetKnownFolderPath )
{
wxCoTaskMemPtr<wchar_t> pDir;
HRESULT hr = gs_shellFuncs.pSHGetKnownFolderPath(rfid, 0, 0, &pDir);
if ( SUCCEEDED(hr) )
{
dir = pDir;
}
}
return dir;
}
wxString wxStandardPaths::GetAppDir() const
{
if ( m_appDir.empty() )
{
m_appDir = wxFileName(wxGetFullModuleName()).GetPath();
}
return m_appDir;
}
wxString wxStandardPaths::GetUserDir(Dir userDir) const
{
int csidl;
switch (userDir)
{
case Dir_Cache:
csidl = CSIDL_LOCAL_APPDATA;
break;
case Dir_Config:
csidl = CSIDL_APPDATA;
break;
case Dir_Desktop:
csidl = CSIDL_DESKTOPDIRECTORY;
break;
case Dir_Downloads:
{
csidl = CSIDL_PERSONAL;
// Downloads folder is only available since Vista
wxString dir = DoGetKnownFolder(wxFOLDERID_Downloads);
if ( !dir.empty() )
return dir;
break;
}
case Dir_Music:
csidl = CSIDL_MYMUSIC;
break;
case Dir_Pictures:
csidl = CSIDL_MYPICTURES;
break;
case Dir_Videos:
csidl = CSIDL_MYVIDEO;
break;
default:
csidl = CSIDL_PERSONAL;
break;
}
return DoGetDirectory(csidl);
}
// ----------------------------------------------------------------------------
// MSW-specific functions
// ----------------------------------------------------------------------------
void wxStandardPaths::IgnoreAppSubDir(const wxString& subdirPattern)
{
wxFileName fn = wxFileName::DirName(GetAppDir());
if ( !fn.GetDirCount() )
{
// no last directory to ignore anyhow
return;
}
const wxString lastdir = fn.GetDirs().Last().Lower();
if ( lastdir.Matches(subdirPattern.Lower()) )
{
fn.RemoveLastDir();
// store the cached value so that subsequent calls to GetAppDir() will
// reuse it instead of using just the program binary directory
m_appDir = fn.GetPath();
}
}
void wxStandardPaths::IgnoreAppBuildSubDirs()
{
IgnoreAppSubDir("debug");
IgnoreAppSubDir("release");
// there can also be an architecture-dependent parent directory, ignore it
// as well
#ifdef __WIN64__
IgnoreAppSubDir("Win64");
IgnoreAppSubDir("x64");
IgnoreAppSubDir("x86_64");
IgnoreAppSubDir("ARM64");
#else // __WIN32__
IgnoreAppSubDir("Win32");
IgnoreAppSubDir("x86");
IgnoreAppSubDir("ARM");
#endif // __WIN64__/__WIN32__
wxString compilerPrefix;
#ifdef __VISUALC__
compilerPrefix = "vc";
#elif defined(__GNUG__)
compilerPrefix = "gcc";
#else
return;
#endif
IgnoreAppSubDir(compilerPrefix + "_msw*");
}
void wxStandardPaths::DontIgnoreAppSubDir()
{
// this will force the next call to GetAppDir() to use the program binary
// path as the application directory
m_appDir.clear();
}
/* static */
wxString wxStandardPaths::MSWGetShellDir(int csidl)
{
return DoGetDirectory(csidl);
}
// ----------------------------------------------------------------------------
// public functions
// ----------------------------------------------------------------------------
wxStandardPaths::wxStandardPaths()
{
// make it possible to run uninstalled application from the build directory
IgnoreAppBuildSubDirs();
}
wxString wxStandardPaths::GetExecutablePath() const
{
return wxGetFullModuleName();
}
wxString wxStandardPaths::GetConfigDir() const
{
return AppendAppInfo(DoGetDirectory(CSIDL_COMMON_APPDATA));
}
wxString wxStandardPaths::GetUserConfigDir() const
{
return DoGetDirectory(CSIDL_APPDATA);
}
wxString wxStandardPaths::GetDataDir() const
{
// under Windows each program is usually installed in its own directory and
// so its datafiles are in the same directory as its main executable
return GetAppDir();
}
wxString wxStandardPaths::GetUserDataDir() const
{
return AppendAppInfo(GetUserConfigDir());
}
wxString wxStandardPaths::GetUserLocalDataDir() const
{
return AppendAppInfo(DoGetDirectory(CSIDL_LOCAL_APPDATA));
}
wxString wxStandardPaths::GetPluginsDir() const
{
// there is no standard location for plugins, suppose they're in the same
// directory as the .exe
return GetAppDir();
}
wxString
wxStandardPaths::MakeConfigFileName(const wxString& basename,
ConfigFileConv WXUNUSED(conv)) const
{
wxFileName fn(wxEmptyString, basename);
fn.SetExt(wxT("ini"));
return fn.GetFullName();
}
wxString wxStandardPaths::GetSharedLibrariesDir() const
{
wxFileName fn( GetExecutablePath() );
return fn.GetPath();
}
// ============================================================================
// wxStandardPathsWin16 implementation
// ============================================================================
wxString wxStandardPathsWin16::GetConfigDir() const
{
// this is for compatibility with earlier wxFileConfig versions
// which used the Windows directory for the global files
wxString dir;
if ( !::GetWindowsDirectory(wxStringBuffer(dir, MAX_PATH), MAX_PATH) )
{
wxLogLastError(wxT("GetWindowsDirectory"));
}
return dir;
}
wxString wxStandardPathsWin16::GetUserConfigDir() const
{
// again, for wxFileConfig which uses $HOME for its user config file
return wxGetHomeDir();
}
#endif // wxUSE_STDPATHS