-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrayIcon.cpp
More file actions
164 lines (126 loc) · 4.9 KB
/
Copy pathTrayIcon.cpp
File metadata and controls
164 lines (126 loc) · 4.9 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
#include "TrayIcon.h"
#include "MSettings.h"
#include <QPainter>
#include <QMessageBox>
#include <QApplication>
#include <QCursor>
#include <QUrl>
#include <QDesktopWidget>
#include <QDesktopServices>
#include <QDir>
#include <QProcess>
#include <QCoreApplication>
#include <QIcon>
#include <QMenu>
#include <QTimer>
#include <QAction>
#include <QDebug>
TrayIcon::TrayIcon(QObject *parent) :
QSystemTrayIcon(parent),
routerProc(this),
taskSchedulerProc(this)
{
setIcon(QPixmap(":/icons/tray.png"));
setToolTip("Anime DB");
appDirPath = QCoreApplication::applicationDirPath() + "/";
addr = MSettings::value("addr", "0.0.0.0").toString();
port = MSettings::value("port", "56780").toString();
pathToPhp = MSettings::value("php", "./bin\\php\\php.exe").toString();
if (pathToPhp.startsWith("./")) {
pathToPhp = QDir::cleanPath(appDirPath + pathToPhp);
}
QRegExp pathChecking("[^A-Za-z0-9\\.\\:\\/\\[\\]\\(\\)\\-\\_\\\\]",
Qt::CaseInsensitive);
/* Checking the app path */;
if (appDirPath.contains(pathChecking)) {
QMessageBox::critical(0, tr("Error. Anime DB"),
tr("The application can not be run in the current folder. Move it to another folder, for example, in the folder: C:\\"));
QTimer::singleShot(10, qApp, SLOT(quit()));
} else {
QString lastAppPath = MSettings::value("lastAppPath", appDirPath).toString();
if (lastAppPath != appDirPath) {
clearCash();
MSettings::setValue("lastAppPath", appDirPath);
}
}
QMenu * menu = new QMenu();
setContextMenu(menu);
QAction * action;
action = new QAction(tr("About"), this);
connect(action, SIGNAL(triggered()), this, SLOT(aboutSlot()));
menu->addAction(action);
action = new QAction(tr("Exit"), this);
connect(action, SIGNAL(triggered()), QCoreApplication::instance(),
SLOT(quit()));
menu->addAction(action);
connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this,
SLOT(activatedSlot(QSystemTrayIcon::ActivationReason)));
leftMenu = new QMenu();
action = new QAction(tr("Start"), this);
connect(action, SIGNAL(triggered()), this, SLOT(startSlot()));
leftMenu->addAction(action);
action = new QAction(tr("Stop"), this);
connect(action, SIGNAL(triggered()), this, SLOT(stopSlot()));
leftMenu->addAction(action);
action = new QAction(tr("Restart"), this);
connect(action, SIGNAL(triggered()), this, SLOT(restartSlot()));
leftMenu->addAction(action);
QTimer::singleShot(1000, this, SLOT(startSlot()));
}
void TrayIcon::startSlot() {
lunchProcesses();
openUrl(QString("http://%1:%2/").arg("localhost").arg(port));
}
void TrayIcon::stopSlot() {
routerProc.kill();
taskSchedulerProc.kill();
}
void TrayIcon::restartSlot() {
stopSlot();
QTimer::singleShot(1000, this, SLOT(lunchProcesses()));
}
void TrayIcon::activatedSlot(QSystemTrayIcon::ActivationReason reason) {
if (reason == 3) { // left click on the icon.
leftMenu->show();
QPoint cursor = QCursor::pos();
cursor.ry() -= leftMenu->rect().height();
int overrade = cursor.x() + leftMenu->rect().width()
- QApplication::desktop()->width();
if (overrade > 0)
cursor.rx() -= overrade;
if(cursor.y() - leftMenu->rect().height() < 0)
cursor.ry() += leftMenu->rect().height();
leftMenu->move(cursor);
}
}
void TrayIcon::aboutSlot() {
openUrl("http://anime-db.org/");
}
void TrayIcon::clearCash() {
QStringList params;
params << QDir::cleanPath(appDirPath + "./app/console");
params << "cache:clear";
params << "--env=prod";
params << "--no-debug";
if (QProcess::execute(pathToPhp, params)) {
QMessageBox::critical(0, tr("Error. Anime DB"),
tr("Could not clear application cache. Delete the folder app/cache/ and try again."));
QTimer::singleShot(100, qApp, SLOT(quit()));
}
}
void TrayIcon::lunchProcesses() {
if (routerProc.state() != QProcess::NotRunning
|| taskSchedulerProc.state() != QProcess::NotRunning)
return;
QString routerProcCommand = pathToPhp + " -S " + addr + ":" + port +
" -t " + QDir::cleanPath(appDirPath + "./web") + " " +
QDir::cleanPath(appDirPath + "./app/router.php") + " >nul 2>&1";
QString taskSchedulerProcCommand = pathToPhp + " -f " +
QDir::cleanPath(appDirPath + "./app/console") +
" animedb:task-scheduler";
routerProc.start(routerProcCommand);
taskSchedulerProc.start(taskSchedulerProcCommand);
}
void TrayIcon::openUrl(QString url) {
QDesktopServices::openUrl(QUrl(url));
}