-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettingsjson.cpp
More file actions
293 lines (254 loc) · 12.1 KB
/
Copy pathsettingsjson.cpp
File metadata and controls
293 lines (254 loc) · 12.1 KB
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
#include "settingsjson.h"
#include <QJsonArray>
#include <QJsonDocument>
#include <QSettings>
#include <algorithm>
namespace {
// Marker identifying our own export files, so an unrelated .json can be
// rejected with a real message instead of importing as "all defaults".
constexpr auto kMarkerKey = "application";
constexpr auto kMarkerValue = "TrackClick";
constexpr auto kVersionKey = "schemaVersion";
// QSettings key holding every profile as one JSON object (see settingsjson.h).
constexpr auto kProfilesKey = "profiles/all";
// ── Readers that fall back to the current value on anything unexpected ───────
// A hand-edited or truncated file must never produce a half-applied config, so
// every read is total: wrong type, missing key or out-of-range value all yield
// the fallback.
bool readBool(const QJsonObject& o, const char* key, bool fallback)
{
const QJsonValue v = o.value(QLatin1String(key));
return v.isBool() ? v.toBool() : fallback;
}
int readInt(const QJsonObject& o, const char* key, int fallback, int lo, int hi)
{
const QJsonValue v = o.value(QLatin1String(key));
if (!v.isDouble()) return fallback;
const int i = v.toInt(fallback);
return (i < lo || i > hi) ? fallback : i;
}
double readDouble(const QJsonObject& o, const char* key, double fallback,
double lo, double hi)
{
const QJsonValue v = o.value(QLatin1String(key));
if (!v.isDouble()) return fallback;
const double d = v.toDouble(fallback);
return (d < lo || d > hi) ? fallback : d;
}
QString readString(const QJsonObject& o, const char* key, const QString& fallback)
{
const QJsonValue v = o.value(QLatin1String(key));
return v.isString() ? v.toString() : fallback;
}
// Enums travel as ints; anything outside the valid range falls back rather than
// producing an enum value with no case in the switches that consume it.
template <typename E>
E readEnum(const QJsonObject& o, const char* key, E fallback, int maxValue)
{
const int i = readInt(o, key, static_cast<int>(fallback), 0, maxValue);
return static_cast<E>(i);
}
QStringList readStringList(const QJsonObject& o, const char* key,
const QStringList& fallback)
{
const QJsonValue v = o.value(QLatin1String(key));
if (!v.isArray()) return fallback;
QStringList out;
for (const QJsonValue& e : v.toArray())
if (e.isString()) out << e.toString();
return out;
}
} // namespace
QJsonObject settingsToJson(const AppSettings& s)
{
QJsonObject o;
o[QLatin1String(kMarkerKey)] = QLatin1String(kMarkerValue);
o[QLatin1String(kVersionKey)] = kSettingsSchemaVersion;
// Dwell
o["dwellMs"] = s.dwellMs;
o["sensitivityPx"] = s.sensitivityPx;
o["scrollRepeat"] = s.scrollRepeat;
o["repeatOnDwell"] = s.repeatOnDwell;
o["autoSelectEnabled"] = s.autoSelectEnabled;
o["returnToLeftAfterClick"] = s.returnToLeftAfterClick;
o["hoverSelectPercent"] = s.hoverSelectPercent;
// Visible buttons
o["showNoClick"] = s.showNoClick;
o["showLeftClick"] = s.showLeftClick;
o["showLeftDouble"] = s.showLeftDouble;
o["showLeftDrag"] = s.showLeftDrag;
o["showRightClick"] = s.showRightClick;
o["showRightDouble"] = s.showRightDouble;
o["showRightDrag"] = s.showRightDrag;
o["showMiddleClick"] = s.showMiddleClick;
o["showMiddleDouble"] = s.showMiddleDouble;
o["showScrollUp"] = s.showScrollUp;
o["showScrollDown"] = s.showScrollDown;
o["showScrollHoriz"] = s.showScrollHoriz;
o["showModCtrl"] = s.showModCtrl;
o["showModAlt"] = s.showModAlt;
o["showModShift"] = s.showModShift;
o["showQuitButton"] = s.showQuitButton;
o["showDwellActiveBtn"] = s.showDwellActiveBtn;
o["showKeyboardButton"] = s.showKeyboardButton;
// Window
o["windowOpacity"] = s.windowOpacity;
o["alwaysOnTop"] = s.alwaysOnTop;
o["startMinimized"] = s.startMinimized;
o["xMinimizesApp"] = s.xMinimizesApp;
o["launchOnStartup"] = s.launchOnStartup;
o["minimalWidth"] = s.minimalWidth;
o["minimalWidthPx"] = s.minimalWidthPx;
// Feedback
o["audioFeedback"] = s.audioFeedback;
o["showClickIndicator"] = s.showClickIndicator;
// Audio click
o["audioClickEnabled"] = s.audioClickEnabled;
o["audioClickThreshold"] = s.audioClickThreshold;
o["audioInputDevice"] = s.audioInputDevice;
// Appearance
o["iconsOnly"] = s.iconsOnly;
o["largeButtons"] = s.largeButtons;
o["buttonLayout"] = static_cast<int>(s.buttonLayout);
o["language"] = s.language;
o["settingsFontScale"] = s.settingsFontScale;
o["buttonOrder"] = QJsonArray::fromStringList(s.buttonOrder);
// Edge lock
o["edgeLock"] = static_cast<int>(s.edgeLock);
o["edgeHide"] = s.edgeHide;
// Hotkeys
QJsonArray hotkeys;
for (const HotkeySlot& h : s.hotkeys) {
QJsonObject ho;
ho["enabled"] = h.enabled;
ho["label"] = h.label;
ho["keySequence"] = h.keySequence;
hotkeys.append(ho);
}
o["hotkeys"] = hotkeys;
// NOTE: usageReporting is intentionally absent — see settingsjson.h.
return o;
}
AppSettings settingsFromJson(const QJsonObject& o, const AppSettings& d)
{
AppSettings s = d;
s.dwellMs = readInt(o, "dwellMs", d.dwellMs, 1, 60000);
s.sensitivityPx = readInt(o, "sensitivityPx", d.sensitivityPx, 0, 1000);
s.scrollRepeat = readInt(o, "scrollRepeat", d.scrollRepeat, 1, 100);
s.repeatOnDwell = readBool(o, "repeatOnDwell", d.repeatOnDwell);
s.autoSelectEnabled = readBool(o, "autoSelectEnabled", d.autoSelectEnabled);
s.returnToLeftAfterClick = readBool(o, "returnToLeftAfterClick", d.returnToLeftAfterClick);
s.hoverSelectPercent = readInt(o, "hoverSelectPercent", d.hoverSelectPercent, 1, 100);
s.showNoClick = readBool(o, "showNoClick", d.showNoClick);
s.showLeftClick = readBool(o, "showLeftClick", d.showLeftClick);
s.showLeftDouble = readBool(o, "showLeftDouble", d.showLeftDouble);
s.showLeftDrag = readBool(o, "showLeftDrag", d.showLeftDrag);
s.showRightClick = readBool(o, "showRightClick", d.showRightClick);
s.showRightDouble = readBool(o, "showRightDouble", d.showRightDouble);
s.showRightDrag = readBool(o, "showRightDrag", d.showRightDrag);
s.showMiddleClick = readBool(o, "showMiddleClick", d.showMiddleClick);
s.showMiddleDouble = readBool(o, "showMiddleDouble", d.showMiddleDouble);
s.showScrollUp = readBool(o, "showScrollUp", d.showScrollUp);
s.showScrollDown = readBool(o, "showScrollDown", d.showScrollDown);
s.showScrollHoriz = readBool(o, "showScrollHoriz", d.showScrollHoriz);
s.showModCtrl = readBool(o, "showModCtrl", d.showModCtrl);
s.showModAlt = readBool(o, "showModAlt", d.showModAlt);
s.showModShift = readBool(o, "showModShift", d.showModShift);
s.showQuitButton = readBool(o, "showQuitButton", d.showQuitButton);
s.showDwellActiveBtn = readBool(o, "showDwellActiveBtn", d.showDwellActiveBtn);
s.showKeyboardButton = readBool(o, "showKeyboardButton", d.showKeyboardButton);
s.windowOpacity = readDouble(o, "windowOpacity", d.windowOpacity, 0.1, 1.0);
s.alwaysOnTop = readBool(o, "alwaysOnTop", d.alwaysOnTop);
s.startMinimized = readBool(o, "startMinimized", d.startMinimized);
s.xMinimizesApp = readBool(o, "xMinimizesApp", d.xMinimizesApp);
s.launchOnStartup = readBool(o, "launchOnStartup", d.launchOnStartup);
s.minimalWidth = readBool(o, "minimalWidth", d.minimalWidth);
s.minimalWidthPx = readInt(o, "minimalWidthPx", d.minimalWidthPx, 0, 10000);
s.audioFeedback = readBool(o, "audioFeedback", d.audioFeedback);
s.showClickIndicator = readBool(o, "showClickIndicator", d.showClickIndicator);
s.audioClickEnabled = readBool(o, "audioClickEnabled", d.audioClickEnabled);
s.audioClickThreshold = readInt(o, "audioClickThreshold", d.audioClickThreshold, 1, 100);
s.audioInputDevice = readString(o, "audioInputDevice", d.audioInputDevice);
s.iconsOnly = readBool(o, "iconsOnly", d.iconsOnly);
s.largeButtons = readBool(o, "largeButtons", d.largeButtons);
s.buttonLayout = readEnum(o, "buttonLayout", d.buttonLayout,
static_cast<int>(ButtonLayout::VerticalTwo));
s.language = readString(o, "language", d.language);
s.settingsFontScale = readInt(o, "settingsFontScale", d.settingsFontScale, 80, 200);
s.buttonOrder = readStringList(o, "buttonOrder", d.buttonOrder);
s.edgeLock = readEnum(o, "edgeLock", d.edgeLock, static_cast<int>(EdgeLock::Right));
s.edgeHide = readBool(o, "edgeHide", d.edgeHide);
const QJsonValue hk = o.value(QLatin1String("hotkeys"));
if (hk.isArray()) {
const QJsonArray arr = hk.toArray();
for (int i = 0; i < 3; ++i) {
if (i >= arr.size() || !arr.at(i).isObject()) continue;
const QJsonObject ho = arr.at(i).toObject();
s.hotkeys[i].enabled = readBool(ho, "enabled", d.hotkeys[i].enabled);
s.hotkeys[i].label = readString(ho, "label", d.hotkeys[i].label);
s.hotkeys[i].keySequence = readString(ho, "keySequence", d.hotkeys[i].keySequence);
}
}
// Privacy choice is never imported — it stays whatever this machine has.
s.usageReporting = d.usageReporting;
return s;
}
bool isSettingsJson(const QJsonObject& o)
{
if (o.value(QLatin1String(kMarkerKey)).toString() != QLatin1String(kMarkerValue))
return false;
const QJsonValue v = o.value(QLatin1String(kVersionKey));
if (!v.isDouble()) return false;
const int ver = v.toInt(-1);
return ver >= 1 && ver <= kSettingsSchemaVersion;
}
// ── Named profiles ───────────────────────────────────────────────────────────
static QJsonObject readProfiles(QSettings& store)
{
const QByteArray raw = store.value(QLatin1String(kProfilesKey)).toString().toUtf8();
if (raw.isEmpty()) return {};
const QJsonDocument doc = QJsonDocument::fromJson(raw);
return doc.isObject() ? doc.object() : QJsonObject{};
}
static void writeProfiles(QSettings& store, const QJsonObject& all)
{
store.setValue(QLatin1String(kProfilesKey),
QString::fromUtf8(QJsonDocument(all).toJson(QJsonDocument::Compact)));
}
QStringList settingsProfileNames(QSettings& store)
{
QStringList names = readProfiles(store).keys();
// Order the way the user reads, not by code point, so "Work" and "work" sit
// together rather than being split by every lowercase name. The tie-break
// on an exact compare keeps the result deterministic: names differing only
// in case compare equal case-insensitively, and std::sort is not stable, so
// without it their order would be unspecified.
std::sort(names.begin(), names.end(), [](const QString& a, const QString& b) {
const int ci = a.compare(b, Qt::CaseInsensitive);
return ci != 0 ? ci < 0 : a.compare(b) < 0;
});
return names;
}
void saveSettingsProfile(QSettings& store, const QString& name, const AppSettings& s)
{
if (name.isEmpty()) return;
QJsonObject all = readProfiles(store);
all[name] = settingsToJson(s);
writeProfiles(store, all);
}
bool loadSettingsProfile(QSettings& store, const QString& name,
const AppSettings& defaults, AppSettings& out)
{
const QJsonObject all = readProfiles(store);
const QJsonValue v = all.value(name);
if (!v.isObject()) return false;
out = settingsFromJson(v.toObject(), defaults);
return true;
}
void deleteSettingsProfile(QSettings& store, const QString& name)
{
QJsonObject all = readProfiles(store);
if (!all.contains(name)) return;
all.remove(name);
writeProfiles(store, all);
}