forked from Securepoint/openvpn-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
servicelog.cpp
133 lines (115 loc) · 3.55 KB
/
servicelog.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
#include "servicelog.h"
#include "ui_servicelog.h"
#include "message.h"
#include "servicelogdata.h"
#include "preferences.h"
ServiceLog::ServiceLog(int id) :
QDialog(),
m_ui(new Ui::ServiceLog),
openvpnId(id)
{
m_ui->setupUi(this);
this->setWindowFlags(Qt::Tool);
// Timer für die Aktualisierung aktualisieren
this->timer = new QTimer(this);
this->timer->setInterval(1000);
QObject::connect(this->timer, SIGNAL(timeout()), this, SLOT(refreshData()));
}
ServiceLog::~ServiceLog()
{
delete m_ui;
}
void ServiceLog::changeEvent(QEvent *e)
{
QDialog::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
m_ui->retranslateUi(this);
break;
default:
break;
}
}
void ServiceLog::on_cmdStopLog_clicked()
{
if (this->timer->isActive()) {
this->timer->stop();
m_ui->cmdStopLog->setText(QObject::tr("Start Log"));
} else {
this->timer->start();
m_ui->cmdStopLog->setText(QObject::tr("Stop Log"));
}
}
void ServiceLog::closeEvent(QCloseEvent *event)
{
this->timer->stop();
event->accept();
}
void ServiceLog::showEvent(QShowEvent *e)
{
this->refreshData();
this->timer->start();
m_ui->cmdStopLog->setText(QObject::tr("Stop Log"));
int winW = this->width();
int winH = this->height();
int left (0);
int top (0);
if (Preferences::instance()->isVisible()) {
// Wenn das Hauptfenster offen ist mittig über diesem plazieren
left = Preferences::instance()->geometry().x();
top = Preferences::instance()->geometry().y();
left = left + (Preferences::instance()->geometry().width() - winW) / 2;
//
top = top + (Preferences::instance()->geometry().height() - winH) / 2;
} else {
// Desktop auswerten
top = qApp->desktop()->height();
left = qApp->desktop()->width();
// Die Breite bei virtuellen Desktops vierteln
if (left > 2000 && qApp->desktop()->isVirtualDesktop()) {
left /= 4;
} else {
// Normaler Desktop
left = (left - winH) / 2;
}
// Height
if (top > 2000 && qApp->desktop()->isVirtualDesktop()) {
top /= 4;
} else {
top = (top - winH) / 2;
}
}
// Nun die neuen setzen
this->setGeometry(left, top, winW, winH);
e->accept();
}
void ServiceLog::on_cmdSave_clicked()
{
QString fileName = QFileDialog::getSaveFileName(this, QObject::tr("Save Log"), QApplication::applicationDirPath(), QObject::tr("Log files (*.log *.txt)"));
if (!fileName.isEmpty()) {
// open and write
QFile wFile (fileName);
if (!wFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
Message::error(QObject::tr("Can't open file to write!"));
return;
}
QTextStream out (&wFile);
out << m_ui->memLog->toPlainText();
wFile.close();
}
}
void ServiceLog::refreshData()
{
this->timer->stop();
m_ui->memLog->clear();
m_ui->memLog->appendPlainText(ServiceLogData::instance()->logs(this->openvpnId).join(""));
// Da das TextEdit readonly ist, nehmen wir die scrollbar für den auto scroll. ansonsten ist der cursor besser
QScrollBar *sb = m_ui->memLog->verticalScrollBar();
sb->setValue(sb->maximum());
this->timer->start();
}
void ServiceLog::on_cmdClose_clicked()
{
timer->stop();
this->close();
}