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
|
#include "settingsutilities.h"
#include "expanderwidget.h"
#include <utility.h>
using namespace MOBase;
bool shouldLogSetting(const QString& displayName)
{
// don't log Geometry/ and Widgets/, too noisy and not very useful
static const QStringList ignorePrefixes = {"Geometry/", "Widgets/"};
for (auto&& prefix : ignorePrefixes) {
if (displayName.startsWith(prefix, Qt::CaseInsensitive)) {
return false;
}
}
return true;
}
void logRemoval(const QString& name)
{
if (!shouldLogSetting(name)) {
return;
}
log::debug("setting '{}' removed", name);
}
QString settingName(const QString& section, const QString& key)
{
if (section.isEmpty()) {
return key;
} else if (key.isEmpty()) {
return section;
} else {
if (section.compare("General", Qt::CaseInsensitive) == 0) {
return key;
} else {
return section + "/" + key;
}
}
}
void removeImpl(QSettings& settings, const QString& displayName, const QString& section,
const QString& key)
{
if (key.isEmpty()) {
if (!settings.childGroups().contains(section, Qt::CaseInsensitive)) {
// not there
return;
}
} else {
if (!settings.contains(settingName(section, key))) {
// not there
return;
}
}
logRemoval(displayName);
settings.remove(settingName(section, key));
}
void remove(QSettings& settings, const QString& section, const QString& key)
{
removeImpl(settings, settingName(section, key), section, key);
}
void removeSection(QSettings& settings, const QString& section)
{
removeImpl(settings, section, section, "");
}
ScopedGroup::ScopedGroup(QSettings& s, const QString& name)
: m_settings(s), m_name(name)
{
m_settings.beginGroup(m_name);
}
ScopedGroup::~ScopedGroup()
{
m_settings.endGroup();
}
void ScopedGroup::remove(const QString& key)
{
removeImpl(m_settings, settingName(m_name, key), "", key);
}
QStringList ScopedGroup::keys() const
{
return m_settings.childKeys();
}
ScopedReadArray::ScopedReadArray(QSettings& s, const QString& section)
: m_settings(s), m_count(0)
{
m_count = m_settings.beginReadArray(section);
}
ScopedReadArray::~ScopedReadArray()
{
m_settings.endArray();
}
int ScopedReadArray::count() const
{
return m_count;
}
QStringList ScopedReadArray::keys() const
{
return m_settings.childKeys();
}
ScopedWriteArray::ScopedWriteArray(QSettings& s, const QString& section,
std::size_t size)
: m_settings(s), m_section(section), m_i(0)
{
m_settings.beginWriteArray(section, size == NoSize ? -1 : static_cast<int>(size));
}
ScopedWriteArray::~ScopedWriteArray()
{
m_settings.endArray();
}
void ScopedWriteArray::next()
{
m_settings.setArrayIndex(m_i);
++m_i;
}
QString widgetNameWithTopLevel(const QWidget* widget)
{
QStringList components;
auto* tl = widget->window();
if (tl == widget) {
// this is a top level widget, such as a dialog
components.push_back(widget->objectName());
} else {
// this is a widget
const auto toplevelName = tl->objectName();
if (!toplevelName.isEmpty()) {
components.push_back(toplevelName);
}
const auto widgetName = widget->objectName();
if (!widgetName.isEmpty()) {
components.push_back(widgetName);
}
}
if (components.isEmpty()) {
// can't do much
return "unknown_widget";
}
return components.join("_");
}
QString widgetName(const QMainWindow* w)
{
return w->objectName();
}
QString widgetName(const QHeaderView* w)
{
return widgetNameWithTopLevel(w->parentWidget());
}
QString widgetName(const ExpanderWidget* w)
{
return widgetNameWithTopLevel(w->button());
}
QString widgetName(const QWidget* w)
{
return widgetNameWithTopLevel(w);
}
QString dockSettingName(const QDockWidget* dock)
{
return "MainWindow_docks_" + dock->objectName() + "_size";
}
QString indexSettingName(const QWidget* widget)
{
return widgetNameWithTopLevel(widget) + "_index";
}
QString checkedSettingName(const QAbstractButton* b)
{
return widgetNameWithTopLevel(b) + "_checked";
}
void warnIfNotCheckable(const QAbstractButton* b)
{
if (!b->isCheckable()) {
log::warn("button '{}' used in the settings as a checkbox or radio button "
"but is not checkable",
b->objectName());
}
}
QString credentialName(const QString& key)
{
return "ModOrganizer2_" + key;
}
bool deleteWindowsCredential(const QString& key)
{
const auto credName = credentialName(key);
if (!CredDeleteW(credName.toStdWString().c_str(), CRED_TYPE_GENERIC, 0)) {
const auto e = GetLastError();
// not an error if the key already doesn't exist, and don't log it because
// it happens all the time when the settings dialog is closed since it
// doesn't check first
if (e == ERROR_NOT_FOUND) {
return true;
}
log::error("failed to delete windows credential {}, {}", credName,
formatSystemMessage(e));
return false;
}
log::debug("deleted windows credential {}", credName);
return true;
}
bool addWindowsCredential(const QString& key, const QString& data)
{
const auto credName = credentialName(key);
const auto wname = credName.toStdWString();
const auto wdata = data.toStdWString();
const auto* blob = reinterpret_cast<const BYTE*>(wdata.data());
const auto blobSize = wdata.size() * sizeof(decltype(wdata)::value_type);
CREDENTIALW cred = {};
cred.Flags = 0;
cred.Type = CRED_TYPE_GENERIC;
cred.TargetName = const_cast<wchar_t*>(wname.c_str());
cred.CredentialBlob = const_cast<BYTE*>(blob);
cred.CredentialBlobSize = static_cast<DWORD>(blobSize);
cred.Persist = CRED_PERSIST_LOCAL_MACHINE;
if (!CredWriteW(&cred, 0)) {
const auto e = GetLastError();
log::error("failed to delete windows credential {}, {}", credName,
formatSystemMessage(e));
return false;
}
log::debug("set windows credential {}", credName);
return true;
}
struct CredentialFreer
{
void operator()(CREDENTIALW* c)
{
if (c) {
CredFree(c);
}
}
};
using CredentialPtr = std::unique_ptr<CREDENTIALW, CredentialFreer>;
QString getWindowsCredential(const QString& key)
{
const QString credName = credentialName(key);
CREDENTIALW* rawCreds = nullptr;
const auto ret =
CredReadW(credName.toStdWString().c_str(), CRED_TYPE_GENERIC, 0, &rawCreds);
CredentialPtr creds(rawCreds);
if (!ret) {
const auto e = GetLastError();
if (e != ERROR_NOT_FOUND) {
log::error("failed to retrieve windows credential {}: {}", credName,
formatSystemMessage(e));
}
return {};
}
QString value;
if (creds->CredentialBlob) {
value =
QString::fromWCharArray(reinterpret_cast<const wchar_t*>(creds->CredentialBlob),
creds->CredentialBlobSize / sizeof(wchar_t));
}
return value;
}
bool setWindowsCredential(const QString& key, const QString& data)
{
if (data.isEmpty()) {
return deleteWindowsCredential(key);
} else {
return addWindowsCredential(key, data);
}
}
|