/
main.cpp
657 lines (587 loc) · 22.1 KB
/
main.cpp
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/*
* qtmenu-standalone
* ==================
* (C) Will Brokenbourgh
* will_brokenbourgh@yahoo.com
* http://www.pismotek.com/brainout/
* - - - -
* Thank you Jesus! To God be the glory! C programming is fun!! :-D
* - - - -
* Copyright (C) 2014 - Will Brokenbourgh - will_brokenbourgh@yahoo.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QAction *qaAction;
QMenu *menMain = new QMenu();
QMenu *menPref = new QMenu();
QMenu *menAcc = new QMenu();
QMenu *menDevel = new QMenu();
QMenu *menEdu = new QMenu();
QMenu *menGraph = new QMenu();
QMenu *menHam = new QMenu();
QMenu *menInter = new QMenu();
QMenu *menMulti = new QMenu();
QMenu *menOffic = new QMenu();
QMenu *menOth = new QMenu();
QMenu *menSci = new QMenu();
QMenu *menSys = new QMenu();
QDesktopWidget *qDesktop = new QDesktopWidget();
QRect qrDesktop = qDesktop->availableGeometry();
QFile file(QDir::homePath() + "/.qtmenu");
QString strProcess;
QString strForeColor;
QString strBackColor;
int nLeft = -1;
int nTop = -1;
QDir qdDir = QDir(strSysApps);
QFileInfoList qflList = qdDir.entryInfoList();
QString strRunCommand = "fbrun -nearmouse";
QString strExitCommand = "fluxbox-remote quit";
QString strRebootCommand = "sudo reboot";
QString strPoweroffCommand = "sudo poweroff";
QStringList strlistCustoms;
QString strTheme;
bool blShowGNOME = true;
bool blShowLXDE = true;
bool blShowKDE = true;
bool blShowMATE = true;
bool blShowXFCE = true;
QList<MenuListItem> mliList;
MenuListItem mliListTemp;
QMenu *menTemp;
int nCount = qflList.count() - 1;
bool blFirstLoop = true;
bool blHasCustoms = false;
bool blIconHasPath = false;
bool blIconHasExt = false;
// process config file
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
while (!file.atEnd())
{
strProcess = file.readLine().trimmed();
// left positioning
if (strProcess.toLower().startsWith("left=")) {
strProcess.remove(0, 5);
nLeft = strProcess.toInt();
} else
// top positioning
if (strProcess.toLower().startsWith("top=")) {
strProcess.remove(0, 4);
nTop = strProcess.toInt();
} else
// custom menu items
if (strProcess.toLower().startsWith("custom=")) {
strProcess.remove(0, 7);
strlistCustoms.append(strProcess);
} else
// theme
if (strProcess.toLower().startsWith("icontheme=")) {
strProcess.remove(0, 10);
strTheme = strProcess;
} else
// foreground
if (strProcess.toLower().startsWith("fg=")) {
strProcess.remove(0, 3);
if (strProcess != "") {
strForeColor = strProcess;
}
} else
// background
if (strProcess.toLower().startsWith("bg=")) {
strProcess.remove(0, 3);
if (strProcess != "") {
strBackColor = strProcess;
}
} else
// show gnome items?
if (strProcess.toLower().startsWith("showgnomeitems=")) {
strProcess.remove(0, 15);
if (strProcess.contains("true") ) {
blShowGNOME = true;
} else {
blShowGNOME = false;
}
} else
// show lxde items?
if (strProcess.toLower().startsWith("showlxdeitems=")) {
strProcess.remove(0, 14);
if (strProcess.contains("true") ) {
blShowLXDE = true;
} else {
blShowLXDE = false;
}
} else
// show kde items?
if (strProcess.toLower().startsWith("showkdeitems=")) {
strProcess.remove(0, 14);
if (strProcess.contains("true") ) {
blShowKDE = true;
} else {
blShowKDE = false;
}
} else
// show mate items?
if (strProcess.toLower().startsWith("showmateitems=")) {
strProcess.remove(0, 14);
if (strProcess.contains("true") ) {
blShowMATE = true;
} else {
blShowMATE = false;
}
} else
// show xfce items?
if (strProcess.toLower().startsWith("showxfceitems=")) {
strProcess.remove(0, 14);
if (strProcess.contains("true") ) {
blShowXFCE = true;
} else {
blShowXFCE = false;
}
} else
// run command
if (strProcess.toLower().startsWith("runcommand=")) {
strProcess.remove(0, 11);
if (strProcess != "") {
strRunCommand = strProcess;
}
} else
// exit command
if (strProcess.toLower().startsWith("exitcommand=")) {
strProcess.remove(0, 12);
if (strProcess != "") {
strExitCommand = strProcess;
}
} else
// reboot command
if (strProcess.toLower().startsWith("restartcommand=")) {
strProcess.remove(0, 15);
if (strProcess != "") {
strRebootCommand = strProcess;
}
} else
// poweroff command
if (strProcess.toLower().startsWith("poweroffcommand=")) {
strProcess.remove(0, 16);
if (strProcess != "") {
strPoweroffCommand = strProcess;
}
}
}
}
// set icon theme
if (strTheme != "") {
QIcon::setThemeName(strTheme);
} else {
QIcon::setThemeName("hicolor");
}
// custom menu items from config file
for (int n = 0; n < strlistCustoms.count(); n++)
{
MenuListItem mi;
strProcess = strlistCustoms.at(n);
mi.strName = strProcess.section("|", 0, 0);
mi.strExec = strProcess.section("|", 1, 1);
mi.strIcon = strProcess.section("|", 2, 2);
mi.strCategory = "CustomItems";
mliList.append(mi);
blHasCustoms = true;
}
// read desktop files and insert menu items
for (int n = 0; n < nCount;)
{
QFileInfo fileInfo = qflList.at(n);
QFile qfFile(fileInfo.absoluteFilePath());
MenuListItem mi;
bool blNoShow = false;
bool blOkToParse = false;
if (qfFile.open(QIODevice::ReadOnly|QIODevice::Text)) {
while (!qfFile.atEnd())
{
strProcess = qfFile.readLine();
// set parse flag if we are in the Desktop Entry section
if (strProcess.startsWith("[") && strProcess.contains("Desktop Entry")) {
blOkToParse = true;
}
if (blOkToParse) {
// app name
if (strProcess.startsWith("Name=")) {
strProcess.remove(0,5);
mi.strName = strProcess.trimmed().remove("_");
} else
// app icon
if (strProcess.startsWith("Icon=")) {
strProcess.remove(0,5);
mi.strIcon = strProcess.trimmed();
} else
// app exec line
if (strProcess.startsWith("Exec=")) {
strProcess.remove(0,5);
mi.strExec = strProcess.trimmed();
mi.strExec.remove("%F");
mi.strExec.remove("%f");
mi.strExec.remove("%U");
mi.strExec.remove("%u");
} else
// app category
if (strProcess.startsWith("Categories=")) {
if (blFirstLoop) {
strProcess.remove(0,11);
mi.strCategory = strProcess.trimmed();
} else {
mi.strCategory = "Other";
}
} else
// needs terminal?
if (strProcess.startsWith("Terminal=")) {
strProcess.remove(0,9);
if (strProcess.toLower().contains("true")) {
mi.blNeedsTerminal = true;
} else {
mi.blNeedsTerminal = false;
}
} else
// don't display?
if (strProcess.startsWith("NoDisplay=")) {
strProcess.remove(0,10);
if (strProcess.contains("True") ||
strProcess.contains("true")
) {
blNoShow = true;
}
} else
// show gnome stuff?
if (strProcess.contains("OnlyShowIn=GNOME")) {
if (blShowGNOME == true) {
blNoShow = false;
} else {
blNoShow = true;
}
} else
// show lxde stuff?
if (strProcess.contains("OnlyShowIn=LXDE")) {
if (blShowLXDE == true) {
blNoShow = false;
} else {
blNoShow = true;
}
} else
// show kde stuff?
if (strProcess.contains("OnlyShowIn=KDE")) {
if (blShowKDE == true) {
blNoShow = false;
} else {
blNoShow = true;
}
} else
// show mate stuff?
if (strProcess.contains("OnlyShowIn=MATE")) {
if (blShowMATE == true) {
blNoShow = false;
} else {
blNoShow = true;
}
} else
// show xfce stuff?
if (strProcess.contains("OnlyShowIn=XFCE")) {
if (blShowXFCE == true) {
blNoShow = false;
} else {
blNoShow = true;
}
} else
// stop parsing if there are other sections
if (strProcess.startsWith("[") && !strProcess.contains("Desktop Entry")) {
break;
}
}
}
if (mi.strName != "" && !blNoShow) {
if (mi.blNeedsTerminal) {
mi.strExec = "x-terminal-emulator -e " + mi.strExec;
}
mliList.append(mi);
}
}
if (qfFile.isOpen()) {
qfFile.close();
}
// switch to local app folder and start over
if (n == (nCount - 1) && blFirstLoop) {
qdDir = QDir(QDir::homePath() + "/.local/share/applications");
qflList = qdDir.entryInfoList();
nCount = qflList.count() - 1;
n = 0;
blFirstLoop = false;
QApplication::beep();
} else {
n++;
}
}
// sort items alphabetically
qSort(mliList);
// set up category sub-menus
menPref->setIcon(QIcon::fromTheme("preferences-desktop", QIcon::fromTheme("document-send")));
menPref->setTitle("Preferences");
menAcc->setIcon(QIcon::fromTheme("applications-accessories", QIcon::fromTheme("document-send")));
menAcc->setTitle("Accessories");
menDevel->setIcon(QIcon::fromTheme("applications-development", QIcon::fromTheme("document-send")));
menDevel->setTitle("Development");
menEdu->setIcon(QIcon::fromTheme("applications-science", QIcon::fromTheme("document-send")));
menEdu->setTitle("Education");
menGraph->setIcon(QIcon::fromTheme("applications-graphics", QIcon::fromTheme("document-send")));
menGraph->setTitle("Graphics");
menHam->setIcon(QIcon::fromTheme("applications-other", QIcon::fromTheme("document-send")));
menHam->setTitle("Ham Radio");
menInter->setIcon(QIcon::fromTheme("applications-internet", QIcon::fromTheme("document-send")));
menInter->setTitle("Network");
menMulti->setIcon(QIcon::fromTheme("applications-multimedia", QIcon::fromTheme("document-send")));
menMulti->setTitle("Multimedia");
menOffic->setIcon(QIcon::fromTheme("applications-office", QIcon::fromTheme("document-send")));
menOffic->setTitle("Office");
menOth->setIcon(QIcon::fromTheme("applications-other", QIcon::fromTheme("document-send")));
menOth->setTitle("Other");
menSci->setIcon(QIcon::fromTheme("applications-science", QIcon::fromTheme("document-send")));
menSci->setTitle("Science");
menSys->setIcon(QIcon::fromTheme("applications-system", QIcon::fromTheme("document-send")));
menSys->setTitle("System");
// *** collate by standard categories ***
for (int i = mliList.size() - 1; i > -1; --i) {
menTemp = menOth;
mliListTemp = mliList.at(i);
// custom items
if (mliListTemp.strCategory.contains("CustomItems")) {
menTemp = menMain;
} else
// settings / preferences
if (mliListTemp.strCategory.contains("=Settings") ||
mliListTemp.strCategory.contains(";Settings")
) {
menTemp = menPref;
} else
// system
if (mliListTemp.strCategory.contains("System")) {
menTemp = menSys;
} else
// hamradio
if (mliListTemp.strCategory.contains("HamRadio")) {
menTemp = menHam;
} else
// science
if (mliListTemp.strCategory.contains("Science")) {
menTemp = menSci;
} else
// accessories / utility
if (mliListTemp.strCategory.contains("Accessories") ||
mliListTemp.strCategory.contains("Utility")
) {
menTemp = menAcc;
} else
// development / programming
if (mliListTemp.strCategory.contains("Development")) {
menTemp = menDevel;
} else
// education / science
if (mliListTemp.strCategory.contains("Education") ||
mliListTemp.strCategory.contains("Science")
) {
menTemp = menEdu;
} else
// graphics
if (mliListTemp.strCategory.contains("Graphics")) {
menTemp = menGraph;
} else
// internet / network
if (mliListTemp.strCategory.contains("Internet") ||
mliListTemp.strCategory.contains("WebBrowser") ||
mliListTemp.strCategory.contains("Network")
)
{
menTemp = menInter;
} else
// multimedia / sound/video
if (mliListTemp.strCategory.contains("Audio") ||
mliListTemp.strCategory.contains("AudioVideo") ||
mliListTemp.strCategory.contains("Multimedia")
)
{
menTemp = menMulti;
} else
// office / productivity
if (mliListTemp.strCategory.contains("Office") ||
mliListTemp.strCategory.contains("Productivity") ||
mliListTemp.strCategory.contains("WordProcessor")
)
{
menTemp = menOffic;
}
/* insert into menu */
qaAction = new QAction(mliListTemp.strName, NULL);
// figure out icon
blIconHasExt = false;
blIconHasPath = false;
// has path
if (mliListTemp.strIcon.contains("/")) {
blIconHasPath = true;
}
// has extension
if (mliListTemp.strIcon.contains(".png") ||
mliListTemp.strIcon.contains(".xpm") ||
mliListTemp.strIcon.contains(".svg")
) {
blIconHasExt = true;
}
// no path and no extension
if (blIconHasExt == false && blIconHasPath == false) {
qaAction->setIcon(QIcon::fromTheme(mliListTemp.strIcon));
} else
// path
if (blIconHasPath == true) {
qaAction->setIcon(QIcon(mliListTemp.strIcon));
} else
// extension but no path
if (blIconHasExt == true && blIconHasPath == false) {
qaAction->setIcon(QIcon::fromTheme(mliListTemp.strIcon));
}
if (qaAction->icon().isNull()) {
if (file.exists(strSysPix + "/" + mliListTemp.strIcon)) {
qaAction->setIcon(QIcon(strSysPix + "/" + mliListTemp.strIcon));
} else
if (file.exists(strSysIco + "/" + mliListTemp.strIcon + ".png")) {
qaAction->setIcon(QIcon(strSysIco + "/" + mliListTemp.strIcon + ".png"));
} else
if (file.exists(strSysIco + "/hicolor/scalable/apps/" + mliListTemp.strIcon + ".svg")) {
qaAction->setIcon(QIcon(strSysIco + "/hicolor/scalable/apps/" + mliListTemp.strIcon + ".svg"));
} else
if (file.exists(strSysPix + "/" + mliListTemp.strIcon + ".png")) {
qaAction->setIcon(QIcon(strSysPix + "/" + mliListTemp.strIcon + ".png"));
} else
if (file.exists(strSysPix + "/" + mliListTemp.strIcon + ".xpm")) {
qaAction->setIcon(QIcon(strSysPix + "/" + mliListTemp.strIcon + ".xpm"));
} else
if (file.exists(strSysPix + "/" + mliListTemp.strIcon + ".svg")) {
qaAction->setIcon(QIcon(strSysPix + "/" + mliListTemp.strIcon + ".svg"));
}
}
qaAction->setIconVisibleInMenu(true);
qaAction->setData(mliListTemp.strExec);
menTemp->addAction(qaAction);
}
// add separator if there are customs
if (blHasCustoms) {
menMain->addSeparator();
}
// preferences
if (menPref->actions().count() > 0) {
menMain->addMenu(menPref);
menMain->addSeparator();
}
// accessories
if (menAcc->actions().count() > 0) {
menMain->addMenu(menAcc);
}
// development
if (menDevel->actions().count() > 0) {
menMain->addMenu(menDevel);
}
// education
if (menEdu->actions().count() > 0) {
menMain->addMenu(menEdu);
}
// graphics
if (menGraph->actions().count() > 0) {
menMain->addMenu(menGraph);
}
// ham radio
if (menHam->actions().count() > 0) {
menMain->addMenu(menHam);
}
// internet/networking
if (menInter->actions().count() > 0) {
menMain->addMenu(menInter);
}
// multimedia / sound/video
if (menMulti->actions().count() > 0) {
menMain->addMenu(menMulti);
}
// office
if (menOffic->actions().count() > 0) {
menMain->addMenu(menOffic);
}
// other
if (menOth->actions().count() > 0) {
menMain->addMenu(menOth);
}
// science
if (menSci->actions().count() > 0) {
menMain->addMenu(menSci);
}
// system
if (menSys->actions().count() > 0) {
menMain->addMenu(menSys);
}
menMain->addSeparator();
qaAction = new QAction("About...", NULL);
qaAction->setData("...about...");
qaAction->setIcon(QIcon::fromTheme("dialog-information"));
menMain->addAction(qaAction);
menMain->addSeparator();
qaAction = new QAction("Run...", NULL);
qaAction->setData(strRunCommand);
menMain->addAction(qaAction);
menMain->addSeparator();
qaAction = new QAction("Exit", NULL);
qaAction->setData(strExitCommand);
qaAction->setIcon(QIcon::fromTheme("system-log-out"));
menMain->addAction(qaAction);
qaAction = new QAction("Reboot", NULL);
qaAction->setData(strRebootCommand);
qaAction->setIcon(QIcon::fromTheme("view-refresh"));
menMain->addAction(qaAction);
qaAction = new QAction("Shutdown", NULL);
qaAction->setData(strPoweroffCommand);
qaAction->setIcon(QIcon::fromTheme("system-shutdown"));
menMain->addAction(qaAction);
// set correct positioning
if (nLeft == -1) {
nLeft = qrDesktop.left();
}
if (nTop == -1) {
nTop = qrDesktop.bottom();
}
// show menu
qaAction = menMain->exec(QPoint(nLeft, nTop));
// perform/run selected action
if (qaAction != 0) {
QString qsExec = qaAction->data().toString();
if (qsExec == "...about...") {
QMessageBox::about(NULL,
"About Qt Menu - Standalone",
"<h3>Qt Menu - Standalone</h3><br><br>"
"Copyright 2014 - Will Brokenbourgh<br>"
"Blog: http://www.pismotek.com/brainout/<br><br>"
"Version 0.1.1");
} else {
QProcess *qp = new QProcess();
qp->setWorkingDirectory(QDir::homePath());
qp->start(qsExec);
}
}
}