-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
349 lines (320 loc) · 11.7 KB
/
Copy pathmain.cpp
File metadata and controls
349 lines (320 loc) · 11.7 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
#include <fstream>
#include <filesystem>
#include "LockedLineEdit.h"
#include "ui/components/list.h"
#include "core/listeners/kb.h"
#include "ui/components/appRow.h"
#include "core/apps/readApps.h"
#include "utils.h"
#include "theme/theme.h"
#include "utils/utils.h"
#include "utils/debug.h"
#include "utils/json.hpp"
#include "core/xdg.h"
#include "core/frequency_store.h"
#include <QApplication>
#include <QIcon>
#include <QLabel>
#include <QLineEdit>
#include <QProcess>
#include <QScreen>
#include <QVBoxLayout>
#include <QWidget>
#include <iostream>
#include <qprocess.h>
#include <qsizepolicy.h>
#include <QRegularExpression>
Qt::WindowFlags devFlags()
{
return Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint;
}
int main(int argc, char *argv[])
{
// Use XDG helpers and FrequencyStore
std::string freqPath = xdg::configPath("dlauncher_freq.txt").string();
// ensure dir exists
xdg::ensureDir(xdg::configPath(""));
// frequency store
FrequencyStore freqStore(freqPath);
Debug::log("Debug run string: " + Debug::generateRandomString());
AppReader appReader;
QApplication app(argc, argv);
ThemeManager themeManager;
const Theme &theme = themeManager.currentTheme();
GlobalEventListener globalKbListener(app);
// By default include all .desktop entries (don't skip NoDisplay/Hidden/OnlyShowIn)
bool includeHidden = false;
bool dumpMode = false;
bool menuMode = false;
bool showSystem = false;
std::string debugAppPattern;
bool rebuildCache = false;
bool dumpInMem = false;
std::vector<std::pair<std::string, std::string>> menuItems; // label, command
for (int i = 1; i < argc; ++i) {
std::string arg(argv[i]);
if (arg == "--include-hidden" || arg == "--show-hidden") includeHidden = true;
else if (arg == "--show-system") showSystem = true;
else if (arg == "--dump") {
// Diagnostic mode: print each .desktop path and whether it would be included
dumpMode = true;
}
else if (arg == "--debug-app" && i + 1 < argc) {
debugAppPattern = argv[++i];
dumpMode = true;
}
else if (arg == "--menu-file" && i + 1 < argc) {
menuMode = true;
std::string path = argv[++i];
std::ifstream mf(path);
if (mf.is_open()) {
std::string line;
while (std::getline(mf, line)) {
if (line.empty()) continue;
// Support separators: "::", '\t', or '|'
std::string label, cmd;
auto pos = line.find("::");
if (pos != std::string::npos) {
label = line.substr(0, pos);
cmd = line.substr(pos + 2);
} else if ((pos = line.find('\t')) != std::string::npos) {
label = line.substr(0, pos);
cmd = line.substr(pos + 1);
} else if ((pos = line.find('|')) != std::string::npos) {
label = line.substr(0, pos);
cmd = line.substr(pos + 1);
} else {
// whole line is command
cmd = line;
label = line;
}
if (!label.empty() && !cmd.empty()) {
menuItems.emplace_back(label, cmd);
}
}
}
} else if (arg == "--menu-item" && i + 1 < argc) {
menuMode = true;
std::string pair = argv[++i];
auto pos = pair.find("::");
if (pos == std::string::npos) pos = pair.find('|');
std::string label, cmd;
if (pos != std::string::npos) {
label = pair.substr(0, pos);
cmd = pair.substr(pos + (pair[pos]==':'?2:1));
} else {
label = pair; cmd = pair;
}
if (!label.empty() && !cmd.empty()) menuItems.emplace_back(label, cmd);
}
else if (arg == "--rebuild-cache") {
rebuildCache = true;
}
else if (arg == "--dump-inmem") {
dumpInMem = true;
}
}
// If rebuildCache is set, disable cache reads so we force a fresh rescan
appReader.SetCacheEnabled(!rebuildCache);
appReader.LoadApps(includeHidden, showSystem);
std::cout << "[DEBUG] Loaded apps (count) = " << appReader.GetAllApps().size() << std::endl;
if (dumpMode) {
// Print diagnostics and exit. If debugAppPattern is set only matching lines are printed.
appReader.DumpAndPrint(includeHidden, showSystem, debugAppPattern);
// Also inspect the in-memory app list we loaded and print any matches so we
// can compare disk scan (DumpAndPrint) vs in-memory filtering.
if (!debugAppPattern.empty()) {
std::string pat = toLower(debugAppPattern);
const auto &all = appReader.GetAllApps();
for (size_t i = 0; i < all.size(); ++i) {
const auto &a = all[i];
std::string line = a.name + "\t" + a.exec;
std::string low = toLower(line);
if (low.find(pat) != std::string::npos) {
std::cout << "[IN-MEM] index=" << i << "\t" << a.name << "\t" << a.exec << "\tNoDisplay=" << (a.noDisplay?"1":"0") << "\tHidden=" << (a.hidden?"1":"0") << std::endl;
}
}
}
return 0;
}
if (rebuildCache) {
// Persist the freshly scanned apps to cache and exit
appReader.SaveCache();
std::cout << "Rebuilt cache (XDG_CACHE_HOME or ~/.cache/dlauncher/apps.cache)." << std::endl;
return 0;
}
if (dumpInMem) {
const auto &all = appReader.GetAllApps();
for (size_t i = 0; i < all.size(); ++i) {
const auto &a = all[i];
std::cout << "INMEM\t" << i << "\t" << a.name << "\t" << a.exec << "\tNoDisplay=" << (a.noDisplay?"1":"0") << "\tHidden=" << (a.hidden?"1":"0") << std::endl;
}
return 0;
}
std::string searchTerm = "";
globalKbListener.registerKeyCallback(Qt::Key_Escape, [&]() {
app.quit();
});
QWidget window;
window.setWindowFlags(devFlags());
window.setFixedSize(theme.windowWidth, theme.windowHeight);
window.setAttribute(Qt::WA_TranslucentBackground);
QPalette pal = window.palette();
pal.setColor(QPalette::Window, theme.backgroundColor);
window.setPalette(pal);
window.setAutoFillBackground(true);
if (theme.windowPosX >= 0 && theme.windowPosY >= 0)
{
window.move(theme.windowPosX, theme.windowPosY);
}
else
{
QRect screenGeometry = QApplication::primaryScreen()->geometry();
int x = (screenGeometry.width() - theme.windowWidth) / 2;
int y = (screenGeometry.height() - theme.windowHeight) / 2;
window.move(x, y);
}
ListView *list = new ListView(&window);
QVBoxLayout *layout = new QVBoxLayout(&window);
LockedLineEdit *input = new LockedLineEdit(&window, list->listWidget);
input->setStyleSheet(QString(R"(
QLineEdit {
background-color: %1;
border: 2px solid %2;
border-radius: 10px;
padding: 10px;
color: %3;
font-size: %4px;
}
)")
.arg(theme.inputBackground.name(QColor::HexArgb))
.arg(theme.inputBorder.name(QColor::HexArgb))
.arg(theme.textColor.name(QColor::HexArgb))
.arg(theme.font.pointSize()));
input->setFont(theme.font);
input->setPlaceholderText("Search...");
input->setFocus();
layout->addWidget(input);
// Create rows lazily from pointers into the canonical app list
if (menuMode) {
for (auto &p : menuItems) {
DesktopApp a;
a.name = p.first;
a.exec = p.second;
list->addRow(new AppRow(list, a));
}
} else {
auto &master = appReader.GetAllApps();
size_t count = master.size();
for (size_t i = 0; i < count && i < 64; ++i) {
list->addRow(new AppRow(list, master[i]));
}
}
QObject::connect(list->listWidget, &QListWidget::currentRowChanged, [&](int row)
{
if (row >= 0 && row < list->listWidget->count()) {
QListWidgetItem *item = list->listWidget->item(row);
AppRow *appRow = dynamic_cast<AppRow *>(list->listWidget->itemWidget(item));
if (appRow) {
std::cout << "Exec: " << appRow->app.exec << std::endl;
}
} });
layout->addWidget(list);
QObject::connect(input, &QLineEdit::textChanged, [&](const QString &text)
{
std::string search = text.toStdString();
list->listWidget->clear();
if (search.empty()) {
std::vector<DesktopApp> allApps = appReader.ReadDesktopApps(64, "");
std::sort(allApps.begin(), allApps.end(), [&](const DesktopApp &a, const DesktopApp &b) {
int fa = freqStore.get(a.exec);
int fb = freqStore.get(b.exec);
if (fa == fb) {
return a.name < b.name;
}
if (fa == 0) return false;
if (fb == 0) return true;
return fa > fb;
});
for (const auto &app : allApps) {
list->addRow(new AppRow(list, app));
}
return;
}
std::vector<const DesktopApp*> filteredAppsPtrs;
for (const auto &app : appReader.GetAllApps()) {
if (similarity(search, app.name) > 0.5 || contains(app.name, search, false) != std::string::npos) {
filteredAppsPtrs.push_back(&app);
}
}
std::sort(filteredAppsPtrs.begin(), filteredAppsPtrs.end(), [&](const DesktopApp *a, const DesktopApp *b) {
int fa = freqStore.get(a->exec);
int fb = freqStore.get(b->exec);
if (fa == fb) {
return a->name < b->name;
}
if (fa == 0) return false;
if (fb == 0) return true;
return fa > fb;
});
for (const auto *appPtr : filteredAppsPtrs) {
list->addRow(new AppRow(list, *appPtr));
} });
QObject::connect(input, &QLineEdit::returnPressed, [&]()
{
int row = list->listWidget->currentRow();
if (row >= 0 && row < list->listWidget->count()) {
QListWidgetItem *item = list->listWidget->item(row);
AppRow *appRow = dynamic_cast<AppRow *>(list->listWidget->itemWidget(item));
if (appRow) {
// Sanitize Exec field: remove desktop entry field codes like %f, %F, %u, %U, %i, %c, %k, etc.
QString rawExec = QString::fromStdString(appRow->app.exec);
rawExec.replace("%%", "%");
// remove single-letter field codes preceded by '%'
QRegularExpression fieldCode("%[fFuUiIcklnNvVmMdD]");
QString cleanedExec = rawExec;
cleanedExec.remove(fieldCode);
auto [program, args, envAssignments] = parseExecCommand(cleanedExec);
if (program.isEmpty()) return;
std::cout << "Launching: " << program.toStdString();
for (const auto &arg : args) std::cout << " " << arg.toStdString();
std::cout << std::endl;
freqStore.inc(appRow->app.exec);
freqStore.save();
if (envAssignments.isEmpty()) {
QProcess::startDetached(program, args);
} else {
// Apply env assignments for this process
QProcess *p = new QProcess();
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
for (const QString &assign : envAssignments) {
auto pos = assign.indexOf('=');
if (pos > 0) {
QString key = assign.left(pos);
QString val = assign.mid(pos+1);
env.insert(key, val);
}
}
p->setProcessEnvironment(env);
p->start(program, args);
p->setParent(nullptr);
}
app.quit();
}
} });
QRect screenGeometry = QApplication::primaryScreen()->geometry();
int x = (screenGeometry.width() - window.width()) / 2;
int y = (screenGeometry.height() - window.height()) / 2;
window.move(x, y);
window.show();
// Safety: ensure initial rows are populated (some environments/layout timing
// issues can leave the list empty). If list is empty, populate from master.
if (!menuMode && list->listWidget->count() == 0) {
auto &master = appReader.GetAllApps();
size_t count = master.size();
for (size_t i = 0; i < count && i < 64; ++i) {
list->addRow(new AppRow(list, master[i]));
}
}
return app.exec();
}