-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
379 lines (327 loc) · 13.1 KB
/
Utils.cpp
File metadata and controls
379 lines (327 loc) · 13.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
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
#include "AppSettings.h"
#include "SplitWindow.h"
#include "Utils.h"
#include <algorithm>
#include <QApplication>
#include <QDateTime>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMap>
#include <QPainter>
#include <QPalette>
#include <QStandardPaths>
#include <QThread>
#include <QUuid>
#include <QWebEnginePage>
#include <QWebEngineProfile>
#include <QWebEngineProfileBuilder>
std::vector<SplitWindow*> g_windows;
QIcon g_windowDiamondIcon;
QIcon g_windowEmptyIcon;
QIcon g_windowCheckIcon;
QIcon g_windowCheckDiamondIcon;
GroupScope::GroupScope(AppSettings &settings, const QString &path) : s(settings) {
const QStringList parts = path.split('/', Qt::SkipEmptyParts);
for (const QString &p : parts) { s->beginGroup(p); ++depth; }
}
GroupScope::~GroupScope() {
for (int i = 0; i < depth; ++i) s->endGroup();
}
void createWindowMenuIcons() {
QPixmap emptyPix(16, 16);
emptyPix.fill(Qt::transparent);
g_windowEmptyIcon = QIcon(emptyPix);
QPixmap diamondPix(16, 16);
diamondPix.fill(Qt::transparent);
QPainter p(&diamondPix);
p.setRenderHint(QPainter::Antialiasing);
QPen pen(QApplication::palette().color(QPalette::WindowText));
pen.setWidthF(1.25);
p.setPen(pen);
p.setBrush(Qt::NoBrush);
const QPoint center(8, 8);
const int s = 4;
QPolygon poly;
poly << QPoint(center.x(), center.y() - s)
<< QPoint(center.x() + s, center.y())
<< QPoint(center.x(), center.y() + s)
<< QPoint(center.x() - s, center.y());
p.drawPolygon(poly);
p.end();
g_windowDiamondIcon = QIcon(diamondPix);
// Draw a simple checkmark icon for the active window indicator.
QPixmap checkPix(16, 16);
checkPix.fill(Qt::transparent);
{
QPainter pc(&checkPix);
pc.setRenderHint(QPainter::Antialiasing);
QPen penCheck(QApplication::palette().color(QPalette::WindowText));
penCheck.setWidthF(1.6);
penCheck.setCapStyle(Qt::RoundCap);
penCheck.setJoinStyle(Qt::RoundJoin);
pc.setPen(penCheck);
// Simple two-segment check mark
const QPointF p1(4.0, 8.5);
const QPointF p2(7.0, 11.5);
const QPointF p3(12.0, 5.0);
pc.drawLine(p1, p2);
pc.drawLine(p2, p3);
pc.end();
}
g_windowCheckIcon = QIcon(checkPix);
// Composite: check on left, diamond on right so both indicators can
// appear in a single icon column when the window is active and minimized.
QPixmap comboPix(16, 16);
comboPix.fill(Qt::transparent);
{
QPainter p2(&comboPix);
p2.setRenderHint(QPainter::Antialiasing);
// draw check (left side) reusing the check pen
QPen penCheck(QApplication::palette().color(QPalette::WindowText));
penCheck.setWidthF(1.6);
penCheck.setCapStyle(Qt::RoundCap);
penCheck.setJoinStyle(Qt::RoundJoin);
p2.setPen(penCheck);
p2.drawLine(QPointF(3.0, 8.5), QPointF(6.5, 11.5));
p2.drawLine(QPointF(6.5, 11.5), QPointF(10.5, 5.0));
// draw diamond on right (slightly smaller to fit)
QPen penDiamond(QApplication::palette().color(QPalette::WindowText));
penDiamond.setWidthF(1.25);
p2.setPen(penDiamond);
p2.setBrush(Qt::NoBrush);
const QPoint center(12, 8);
const int s = 3;
QPolygon poly2;
poly2 << QPoint(center.x(), center.y() - s)
<< QPoint(center.x() + s, center.y())
<< QPoint(center.x(), center.y() + s)
<< QPoint(center.x() - s, center.y());
p2.drawPolygon(poly2);
p2.end();
}
g_windowCheckDiamondIcon = QIcon(comboPix);
}
void rebuildAllWindowMenus() {
for (SplitWindow *w : g_windows) {
if (w) {
// Ensure window titles reflect current ordering and counts before
// rebuilding each window's Window menu.
w->updateWindowTitle();
w->refreshWindowMenu();
}
}
}
void createAndShowWindow(const QString &initialAddress, const QString &windowId, bool isIncognito) {
QString id = windowId;
// Generate a new ID for windows without one, or for Incognito windows.
// Incognito windows always get a fresh ID and will not restore from AppSettings
// even if an ID is provided, since the constructor skips restoration for Incognito.
if (id.isEmpty() || isIncognito) id = QUuid::createUuid().toString();
// Construct the window with an id. The SplitWindow constructor will
// attempt to restore saved per-window addresses/layout if the id exists
// and the window is not Incognito.
SplitWindow *w = new SplitWindow(id, isIncognito);
qDebug() << "createAndShowWindow: created window id=" << id
<< " initialAddress=" << (initialAddress.isEmpty() ? QString("(none)") : initialAddress)
<< " isIncognito=" << isIncognito;
w->show();
if (!windowId.isEmpty() && !isIncognito) {
// This is a restored window: the constructor already loaded addresses
// and rebuilt sections. Do not reset or override addresses here.
} else if (!initialAddress.isEmpty()) {
// New ephemeral window requested with an initial address: set it.
QMetaObject::invokeMethod(w, [w, initialAddress]() {
w->setFirstFrameAddress(initialAddress);
}, Qt::QueuedConnection);
} else {
// New window without an initial address: ensure a single empty section.
QMetaObject::invokeMethod(w, "resetToSingleEmptySection", Qt::QueuedConnection);
}
// Track the window and remove it from the list when destroyed so we don't keep dangling pointers.
g_windows.push_back(w);
qDebug() << "createAndShowWindow: tracked window id=" << id << " g_windows.count=" << g_windows.size();
// Update the new window's title now that it is tracked in g_windows.
w->updateWindowTitle();
QObject::connect(w, &QObject::destroyed, qApp, [w]() {
g_windows.erase(std::remove_if(g_windows.begin(), g_windows.end(), [w](SplitWindow *x){ return x == w; }), g_windows.end());
rebuildAllWindowMenus();
});
// Ensure all Window menus show the latest list
rebuildAllWindowMenus();
}
void createAndShowIncognitoWindow(const QString &initialAddress) {
createAndShowWindow(initialAddress, QString(), true);
}
void performLegacyMigration() {
// No-op placeholder reserved for future settings schema migrations.
}
// Map of profile name -> profile instance for caching
static QMap<QString, QWebEngineProfile*> g_profileCache;
QWebEngineProfile *getProfileByName(const QString &profileName) {
// Check cache first
if (g_profileCache.contains(profileName)) {
return g_profileCache.value(profileName);
}
const QString dataRoot = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
const QString profileDir = dataRoot + QStringLiteral("/profiles/") + profileName;
const QString cacheDir = profileDir + QStringLiteral("/cache");
QDir().mkpath(profileDir);
QDir().mkpath(cacheDir);
QWebEngineProfileBuilder builder;
builder.setPersistentStoragePath(profileDir);
builder.setCachePath(cacheDir);
builder.setHttpCacheType(QWebEngineProfile::DiskHttpCache);
builder.setPersistentCookiesPolicy(QWebEngineProfile::ForcePersistentCookies);
builder.setPersistentPermissionsPolicy(QWebEngineProfile::PersistentPermissionsPolicy::StoreOnDisk);
QWebEngineProfile *profile = builder.createProfile(QStringLiteral("phraims-") + profileName, qApp);
qDebug() << "getProfileByName: created profile" << profileName << "storage=" << profile->persistentStoragePath()
<< "cache=" << profile->cachePath() << "offTheRecord=" << profile->isOffTheRecord();
// Cache the profile
g_profileCache.insert(profileName, profile);
return profile;
}
QString currentProfileName() {
AppSettings s;
return s->value("currentProfile", QStringLiteral("Default")).toString();
}
void setCurrentProfileName(const QString &profileName) {
AppSettings s;
s->setValue("currentProfile", profileName);
s->sync();
qDebug() << "setCurrentProfileName:" << profileName;
}
QStringList listProfiles() {
const QString dataRoot = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
const QString profilesDir = dataRoot + QStringLiteral("/profiles");
QDir dir(profilesDir);
if (!dir.exists()) {
// If profiles dir doesn't exist yet, return the default profile.
// Note: The Default profile directory is created lazily by getProfileByName()
// on first use, so it may not exist in the filesystem yet.
return QStringList() << QStringLiteral("Default");
}
QStringList profiles = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name);
// Ensure at least "Default" exists in the list even if its directory hasn't
// been created yet (lazy creation). This ensures Default is always available.
if (profiles.isEmpty() || !profiles.contains(QStringLiteral("Default"))) {
profiles.prepend(QStringLiteral("Default"));
}
profiles.sort(Qt::CaseInsensitive);
return profiles;
}
bool isValidProfileName(const QString &name) {
if (name.isEmpty()) return false;
if (name.contains('/') || name.contains('\\')) return false;
return true;
}
bool createProfile(const QString &profileName) {
if (!isValidProfileName(profileName)) return false;
const QString dataRoot = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
const QString profileDir = dataRoot + QStringLiteral("/profiles/") + profileName;
QDir dir;
if (dir.exists(profileDir)) {
qDebug() << "createProfile: profile already exists:" << profileName;
return false;
}
if (!dir.mkpath(profileDir)) {
qWarning() << "createProfile: failed to create directory:" << profileDir;
return false;
}
qDebug() << "createProfile: created profile:" << profileName << "at" << profileDir;
return true;
}
bool renameProfile(const QString &oldName, const QString &newName) {
if (!isValidProfileName(oldName) || !isValidProfileName(newName) || oldName == newName) return false;
const QString dataRoot = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
const QString oldDir = dataRoot + QStringLiteral("/profiles/") + oldName;
const QString newDir = dataRoot + QStringLiteral("/profiles/") + newName;
QDir dir;
if (!dir.exists(oldDir)) {
qDebug() << "renameProfile: old profile doesn't exist:" << oldName;
return false;
}
if (dir.exists(newDir)) {
qDebug() << "renameProfile: new profile name already exists:" << newName;
return false;
}
if (!dir.rename(oldDir, newDir)) {
qWarning() << "renameProfile: failed to rename directory from" << oldDir << "to" << newDir;
return false;
}
// Update cache if the profile is loaded
if (g_profileCache.contains(oldName)) {
QWebEngineProfile *profile = g_profileCache.take(oldName);
g_profileCache.insert(newName, profile);
}
// Update current profile name if it was renamed
if (currentProfileName() == oldName) {
setCurrentProfileName(newName);
}
qDebug() << "renameProfile: renamed profile from" << oldName << "to" << newName;
return true;
}
bool deleteProfile(const QString &profileName) {
if (profileName.isEmpty()) return false;
// Cannot delete if it's the only profile
const QStringList profiles = listProfiles();
if (profiles.size() <= 1) {
qDebug() << "deleteProfile: cannot delete the last profile";
return false;
}
const QString dataRoot = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
const QString profileDir = dataRoot + QStringLiteral("/profiles/") + profileName;
QDir dir(profileDir);
if (!dir.exists()) {
qDebug() << "deleteProfile: profile doesn't exist:" << profileName;
return false;
}
// Remove from cache if loaded
if (g_profileCache.contains(profileName)) {
// Note: We don't explicitly delete the QWebEngineProfile object because
// it's parented to qApp and will be cleaned up on application exit.
// Deleting it prematurely could cause crashes if pages are still using it.
// Design trade-off: During long sessions with frequent profile deletions,
// this could accumulate QWebEngineProfile objects in memory. This is
// acceptable for typical usage patterns where profiles are rarely deleted.
g_profileCache.remove(profileName);
}
// Switch to another profile if this is the current one
if (currentProfileName() == profileName) {
// Find a profile that isn't being deleted
QString newProfile;
for (const QString &p : profiles) {
if (p != profileName) {
newProfile = p;
break;
}
}
// This should always succeed since we checked profiles.size() > 1 above
if (!newProfile.isEmpty()) {
setCurrentProfileName(newProfile);
}
}
// Delete the directory recursively
if (!dir.removeRecursively()) {
qWarning() << "deleteProfile: failed to remove directory:" << profileDir;
return false;
}
qDebug() << "deleteProfile: deleted profile:" << profileName;
return true;
}
QWebEngineProfile *sharedWebEngineProfile() {
// Use the current profile name
return getProfileByName(currentProfileName());
}
QWebEngineProfile *createIncognitoProfile() {
QWebEngineProfileBuilder builder;
// Off-the-record profile: no persistent storage, all data is ephemeral
QWebEngineProfile *profile = builder.createOffTheRecordProfile(qApp);
qDebug() << "createIncognitoProfile: created off-the-record profile"
<< "offTheRecord=" << profile->isOffTheRecord();
return profile;
}