-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
75 lines (62 loc) · 2.66 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
#include "taskbarmenu.h"
#include <QApplication>
#include <QWidgetAction>
#include <QMenu>
#include <QSystemTrayIcon>
#include <QIcon>
#include <QMessageBox>
#include <QFile>
#include <QSettings>
#include <QStandardPaths>
#include <QFileInfo>
int main(int argc, char *argv[])
{
// Init app that do not quit without opened window
QApplication a(argc, argv);
a.setQuitOnLastWindowClosed(false);
// Warn if the OS do not support system tray, then exit
if(!QSystemTrayIcon::isSystemTrayAvailable())
{
QMessageBox errorMessage(QMessageBox::Critical,
"System not supported",
"The operating system do not provide any system tray or task bar.\n\nThe application will now exit."
);
errorMessage.exec();
return(0);
}
// Configre the app, essentialy for QSettings
QCoreApplication::setOrganizationName("Sylvain-Mariel");
QCoreApplication::setOrganizationDomain("sylvain-mariel.fr");
QCoreApplication::setApplicationName("TaskBarSearch");
// Init settings
#ifdef Q_OS_WIN
// Special feature on Windows : load default settings if there is no settings.
if(!QFile::exists(QCoreApplication::applicationDirPath()+"/settings.ini"))
{
QFile::copy(QCoreApplication::applicationDirPath()+"/default.ini", QCoreApplication::applicationDirPath()+"/settings.ini");
}
QSettings settings(QCoreApplication::applicationDirPath()+"/settings.ini", QSettings::IniFormat);
// Speacial feature on Windows : overwrite auto startup setting depending on the file existance
settings.setValue("autoStartup",
QFile::exists(
QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation)
+ "/Startup/"
+ QFileInfo( QCoreApplication::applicationFilePath() ).fileName()+".lnk"));
#else
// Other OS than Windows : just create settings wherever the OS want
QSettings settings;
#endif
settings.sync();
// Create a sub-element of a menu that contain the custom UI
QWidgetAction *trayWidget = new QWidgetAction(0);
trayWidget->setDefaultWidget(new TaskBarMenu());
// Create a menu that contain the sub-element
QMenu *trayMenu = new QMenu();
trayMenu->addAction(trayWidget);
// Create a tray icon that show the menu on a right click
QSystemTrayIcon *trayIcon = new QSystemTrayIcon();
trayIcon->setContextMenu(trayMenu);
trayIcon->setIcon(QIcon(":/img/icons/icon.16.png"));
trayIcon->show();
return a.exec();
}