Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qstandardpaths_win.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qstandardpaths.h"
5
6#include <qdir.h>
7#include <qstringlist.h>
8
9#ifndef QT_BOOTSTRAPPED
10#include <qcoreapplication.h>
11#endif
12
13#include <qt_windows.h>
14#include <shlobj.h>
15#include <intshcut.h>
16#include <qvarlengtharray.h>
17
18#ifndef QT_NO_STANDARDPATHS
19
21
22using namespace Qt::StringLiterals;
23
28
33
40
41static void appendOrganizationAndApp(QString &path) // Courtesy qstandardpaths_unix.cpp
42{
43#ifndef QT_BOOTSTRAPPED
45 if (!org.isEmpty())
46 path += u'/' + org;
48 if (!appName.isEmpty())
49 path += u'/' + appName;
50#else // !QT_BOOTSTRAPPED
52#endif
53}
54
55static inline void appendTestMode(QString &path)
56{
58 path += "/qttest"_L1;
59}
60
62{
63 // same as GetCurrentProcessToken()
64 const auto process_token = HANDLE(quintptr(-4));
65
66 QVarLengthArray<char,256> token_info_buf(256);
67 auto* token_info = reinterpret_cast<TOKEN_MANDATORY_LABEL*>(token_info_buf.data());
68 DWORD token_info_length = token_info_buf.size();
69 if (!GetTokenInformation(process_token, TokenIntegrityLevel, token_info, token_info_length, &token_info_length)) {
70 // grow buffer and retry GetTokenInformation
71 token_info_buf.resize(token_info_length);
72 token_info = reinterpret_cast<TOKEN_MANDATORY_LABEL*>(token_info_buf.data());
73 if (!GetTokenInformation(process_token, TokenIntegrityLevel, token_info, token_info_length, &token_info_length))
74 return false; // assume "normal" process
75 }
76
77 // The GetSidSubAuthorityCount return-code is undefined on failure, so
78 // there's no point in checking before dereferencing
79 DWORD integrity_level = *GetSidSubAuthority(token_info->Label.Sid, *GetSidSubAuthorityCount(token_info->Label.Sid) - 1);
80 return (integrity_level < SECURITY_MANDATORY_MEDIUM_RID);
81}
82
83// Map QStandardPaths::StandardLocation to KNOWNFOLDERID of SHGetKnownFolderPath()
85{
86 // folders for medium & high integrity processes
87 static const GUID folderIds[] = {
88 FOLDERID_Desktop, // DesktopLocation
89 FOLDERID_Documents, // DocumentsLocation
90 FOLDERID_Fonts, // FontsLocation
91 FOLDERID_Programs, // ApplicationsLocation
92 FOLDERID_Music, // MusicLocation
93 FOLDERID_Videos, // MoviesLocation
94 FOLDERID_Pictures, // PicturesLocation
95 GUID(), GUID(), // TempLocation/HomeLocation
96 FOLDERID_LocalAppData, // AppLocalDataLocation ("Local" path)
97 GUID(), // CacheLocation
98 FOLDERID_LocalAppData, // GenericDataLocation ("Local" path)
99 GUID(), // RuntimeLocation
100 FOLDERID_LocalAppData, // ConfigLocation ("Local" path)
101 FOLDERID_Downloads, // DownloadLocation
102 GUID(), // GenericCacheLocation
103 FOLDERID_LocalAppData, // GenericConfigLocation ("Local" path)
104 FOLDERID_RoamingAppData,// AppDataLocation ("Roaming" path)
105 FOLDERID_LocalAppData, // AppConfigLocation ("Local" path)
106 FOLDERID_Public, // PublicShareLocation
107 FOLDERID_Templates, // TemplatesLocation
108 GUID(), // StateLocation
109 GUID(), // GenericStateLocation
110 };
111 static_assert(sizeof(folderIds) / sizeof(folderIds[0]) == size_t(QStandardPaths::GenericStateLocation + 1));
112
113 // folders for low integrity processes
114 static const GUID folderIds_li[] = {
115 FOLDERID_Desktop, // DesktopLocation
116 FOLDERID_Documents, // DocumentsLocation
117 FOLDERID_Fonts, // FontsLocation
118 FOLDERID_Programs, // ApplicationsLocation
119 FOLDERID_Music, // MusicLocation
120 FOLDERID_Videos, // MoviesLocation
121 FOLDERID_Pictures, // PicturesLocation
122 GUID(), GUID(), // TempLocation/HomeLocation
123 FOLDERID_LocalAppDataLow,// AppLocalDataLocation ("Local" path)
124 GUID(), // CacheLocation
125 FOLDERID_LocalAppDataLow,// GenericDataLocation ("Local" path)
126 GUID(), // RuntimeLocation
127 FOLDERID_LocalAppDataLow,// ConfigLocation ("Local" path)
128 FOLDERID_Downloads, // DownloadLocation
129 GUID(), // GenericCacheLocation
130 FOLDERID_LocalAppDataLow,// GenericConfigLocation ("Local" path)
131 FOLDERID_RoamingAppData, // AppDataLocation ("Roaming" path)
132 FOLDERID_LocalAppDataLow,// AppConfigLocation ("Local" path)
133 FOLDERID_Public, // PublicShareLocation
134 FOLDERID_Templates, // TemplatesLocation
135 GUID(), // StateLocation
136 GUID(), // GenericStateLocation
137 };
138 static_assert(sizeof(folderIds_li) == sizeof(folderIds));
139
140 static bool low_integrity_process = isProcessLowIntegrity();
141 if (size_t(type) < sizeof(folderIds) / sizeof(folderIds[0]))
142 return low_integrity_process ? folderIds_li[type] : folderIds[type];
143 return GUID();
144}
145
146// Convenience for SHGetKnownFolderPath().
147static QString sHGetKnownFolderPath(const GUID &clsid)
148{
150 LPWSTR path;
151 if (Q_LIKELY(SUCCEEDED(SHGetKnownFolderPath(clsid, KF_FLAG_DONT_VERIFY, 0, &path)))) {
153 CoTaskMemFree(path);
154 }
155 return result;
156}
157
159{
161 switch (type) {
162 case CacheLocation:
163 // Although Microsoft has a Cache key it is a pointer to IE's cache, not a cache
164 // location for everyone. Most applications seem to be using a
165 // cache directory located in their AppData directory
167 if (!result.isEmpty()) {
170 result += "/cache"_L1;
171 }
172 break;
173
176 if (!result.isEmpty()) {
178 result += "/cache"_L1;
179 }
180 break;
181
182 case RuntimeLocation:
183 case HomeLocation:
185 break;
186
187 case TempLocation:
189 break;
190
191 case StateLocation:
193 if (!result.isEmpty()) {
196 result += "/State"_L1;
197 }
198 break;
199
202 if (!result.isEmpty()) {
204 result += "/State"_L1;
205 }
206 break;
207
208 default:
210 if (!result.isEmpty() && isConfigLocation(type)) {
214 }
215 break;
216 }
217 return result;
218}
219
220#ifndef QT_BOOTSTRAPPED
221extern QString qAppFileName();
222#endif
223
225{
226 QStringList dirs;
227 const QString localDir = writableLocation(type);
228 if (!localDir.isEmpty())
229 dirs.append(localDir);
230
231 // type-specific handling goes here
232 if (isConfigLocation(type)) {
233 QString programData = sHGetKnownFolderPath(FOLDERID_ProgramData);
234 if (!programData.isEmpty()) {
236 appendOrganizationAndApp(programData);
237 dirs.append(programData);
238 }
239#ifndef QT_BOOTSTRAPPED
240 // Note: QCoreApplication::applicationDirPath(), while static, requires
241 // an application instance. But we might need to resolve the standard
242 // locations earlier than that, so we fall back to qAppFileName().
243 QString applicationDirPath = qApp ? QCoreApplication::applicationDirPath()
245 dirs.append(applicationDirPath);
246 const QString dataDir = applicationDirPath + "/data"_L1;
247 dirs.append(dataDir);
248
250 QString appDataDir = dataDir;
251 appendOrganizationAndApp(appDataDir);
252 if (appDataDir != dataDir)
253 dirs.append(appDataDir);
254 }
255#endif // !QT_BOOTSTRAPPED
256 } // isConfigLocation()
257
258 return dirs;
259}
260
262
263#endif // QT_NO_STANDARDPATHS
QString organizationName
the name of the organization that wrote this application
static QString applicationDirPath()
Returns the directory that contains the application executable.
QString applicationName
the name of this application
static QString fromNativeSeparators(const QString &pathName)
Definition qdir.cpp:962
static QString tempPath()
Returns the absolute canonical path of the system's temporary directory.
Definition qdir.cpp:2133
static QString homePath()
Returns the absolute path of the user's home directory.
Definition qdir.cpp:2103
static bool isTestModeEnabled()
static QStringList standardLocations(StandardLocation type)
static QString writableLocation(StandardLocation type)
StandardLocation
This enum describes the different locations that can be queried using methods such as QStandardPaths:...
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
static QString fromWCharArray(const wchar_t *string, qsizetype size=-1)
Definition qstring.h:1309
QString & append(QChar c)
Definition qstring.cpp:3252
Combined button and popup list for selecting options.
void * HANDLE
#define Q_LIKELY(x)
#define qApp
GLenum type
GLsizei const GLchar *const * path
GLuint64EXT * result
[6]
static void appendOrganizationAndApp(QString &path)
static GUID writableSpecialFolderId(QStandardPaths::StandardLocation type)
static bool isGenericConfigLocation(QStandardPaths::StandardLocation type)
QString qAppFileName()
static bool isProcessLowIntegrity()
static void appendTestMode(QString &path)
static void appendOrganizationAndApp(QString &path)
static bool isConfigLocation(QStandardPaths::StandardLocation type)
static QString sHGetKnownFolderPath(const GUID &clsid)
static QString convertCharArray(const wchar_t *path)
#define Q_UNUSED(x)
size_t quintptr
Definition qtypes.h:167