-
Notifications
You must be signed in to change notification settings - Fork 1
/
window.cpp
executable file
·199 lines (169 loc) · 5.54 KB
/
window.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
#include "window.h"
#include <QProcess>
#include <QtGui>
#include <QRegExp>
class PingThread : public QThread {
public:
Window *window;
protected:
void run() {
QStringList list;
QString text;
QProcess ping;
QStringList result;
QRegExp rx("(\\d+)% loss");
while(true) {
if(window->stop) {
window->stop = false;
window->StartButton->setEnabled(true);
window->StopButton->setEnabled(false);
window->IPInput->setEnabled(true);
break;
}
#ifdef _WIN32
ping.start("ping.exe " + window->IPInput->text() + " -n 10");
#else
ping.start("ping " + window->IPInput->text() + " -c 10");
#endif
ping.waitForFinished();
text = ping.readAll();
if (!text.isEmpty()) {
list = text.split("\n");
if(list.length() < 5) {
window->stop = true;
} else {
result = list.filter("unreachable");
if(result.length() > 0) {
window->stop = true;
} else {
result = list.filter("loss");
if(result.length() > 0) {
if(rx.indexIn(result[0], 0) != -1) {
if(rx.cap(1) == "100") {
window->stop = true;
}
}
}
}
}
}
ping.deleteLater();
}
}
};
Window::Window()
{
QDialog::setWindowFlags(Qt::WindowStaysOnTopHint| Qt::WindowCloseButtonHint | Qt::Tool);
stop = false;
createMessageGroupBox();
createActions();
createTrayIcon();
connect(StartButton, SIGNAL(clicked()), this, SLOT(startPing()));
connect(StopButton, SIGNAL(clicked()), this, SLOT(stopPing()));
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(messageGroupBox);
setLayout(mainLayout);
setWindowTitle(tr("Pinger v.1.2"));
resize(215, 100);
QIcon icon = QIcon(":/images/trash.svg");
trayIcon->setIcon(icon);
setWindowIcon(icon);
trayIcon->show();
trayIcon->setToolTip(tr("Pinger v.1.2"));
setVisible(true);
}
void Window::setVisible(bool visible)
{
minimizeAction->setEnabled(visible);
restoreAction->setEnabled(isMaximized() || !visible);
QDialog::setVisible(visible);
}
void Window::closeEvent(QCloseEvent *event)
{
if (trayIcon->isVisible()) {
hide();
event->ignore();
}
}
void Window::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason) {
case QSystemTrayIcon::Trigger:
case QSystemTrayIcon::DoubleClick:
if (this->isVisible()) {
hide();
} else {
show();
setVisible(true);
}
break;
case QSystemTrayIcon::MiddleClick:
default:
;
}
}
void Window::startPing()
{
// Save ip data
QFile file("./ip-address.txt");
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
if(file.isWritable()) {
const char *ip_address = IPInput->text().toAscii().data();
file.write(ip_address, qstrlen(ip_address));
}
}
file.close();
stop = false;
PingThread *t = new PingThread;
t->window = this;
t->start();
StartButton->setEnabled(false);
StopButton->setEnabled(true);
IPInput->setEnabled(false);
}
void Window::stopPing()
{
StopButton->setEnabled(false);
stop = true;
}
void Window::createMessageGroupBox()
{
messageGroupBox = new QGroupBox(tr("Settings"));
IPLabel = new QLabel(tr("IP-address:"));
IPInput = new QLineEdit();//tr("192.168.100.250")
QFile file("./ip-address.txt");
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
IPInput->setText(file.readAll());
}
StartButton = new QPushButton(tr("Start"));
StopButton = new QPushButton(tr("Stop"));
StartButton->setDefault(true);
StopButton->setEnabled(false);
QGridLayout *messageLayout = new QGridLayout;
messageLayout->addWidget(IPLabel, 1, 0);
messageLayout->addWidget(IPInput, 1, 1);
messageLayout->addWidget(StartButton, 2, 0);
messageLayout->addWidget(StopButton, 2, 1);
messageGroupBox->setLayout(messageLayout);
}
void Window::createActions()
{
minimizeAction = new QAction(tr("Mi&nimize"), this);
connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
restoreAction = new QAction(tr("&Restore"), this);
connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
}
void Window::createTrayIcon()
{
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(minimizeAction);
trayIconMenu->addAction(restoreAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
}