This repository has been archived by the owner on Dec 4, 2020. It is now read-only.
/
LPTray.cpp
169 lines (156 loc) · 5.29 KB
/
LPTray.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
#include "LPTray.h"
//PUBLIC
LPTray::LPTray() : QSystemTrayIcon(){
//Start up the log file watcher and connect the signals/slots
watcher = new LPWatcher();
connect(watcher,SIGNAL(MessageAvailable(QString)),this,SLOT(watcherMessage(QString)) );
connect(watcher,SIGNAL(StatusUpdated()),this,SLOT(watcherMessage()) );
//Setup the context menu
menu = new QMenu;
menu->addAction(QIcon(":/images/backup-failed.png"),tr("View Messages"),this,SLOT(startMessageDialog()) );
menu->addAction(QIcon(":/images/tray-icon-idle.png"),tr("Start Application UI"),this,SLOT(startGUI()) );
menu->addSeparator();
menu->addAction(QIcon(":/images/application-exit.png"),tr("Close Life Preserver Tray"),this,SLOT(slotClose()) );
this->setContextMenu(menu);
//Setup initial icon for the tray
this->setIcon( QIcon(":/images/tray-icon-idle.png") );
//Create the configuration GUI
GUI = new mainUI();
msgdlg = new LPMessages();
//connect other signals/slots
connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(slotTrayClicked(QSystemTrayIcon::ActivationReason)) );
//Start up the watcher
watcher->start();
updateTrayIcon();
updateToolTip();
}
LPTray::~LPTray(){
watcher->stop();
delete watcher;
delete GUI;
delete msgdlg;
}
// ===============
// PRIVATE FUNCTIONS
// ===============
void LPTray::updateTrayIcon(){
if( watcher->hasError() ){
//Errors - show that attention is required
this->setIcon( QIcon(":/images/tray-icon-error.png") );
}else if( watcher->isRunning() ){
//Show the working icon
this->setIcon( QIcon(":/images/tray-icon-working.png") );
}else{
//Show the idle icon
this->setIcon( QIcon(":/images/tray-icon-idle.png") );
}
}
void LPTray::updateToolTip(){
QStringList info = watcher->getAllCurrentMessages();
this->setToolTip( info.join("\n") );
}
// ===============
// PRIVATE SLOTS
// ===============
void LPTray::watcherMessage(QString type){
qDebug() << "New Watcher Message:" << type;
QStringList info;
if(type=="message"){
//Show the message pop-up
info << "time" << "message";
info = watcher->getMessages(type,info);
if(!info.isEmpty()){
this->showMessage( info[0], info[1], QSystemTrayIcon::Information, 5000);
}
}else if(type=="replication"){
info << "id" << "time" << "message";
info = watcher->getMessages(type,info);
if(info.isEmpty()){ return; }
if(info[0] == "STARTED"){
this->showMessage( info[1], info[2], QSystemTrayIcon::Information, 5000);
}else if(info[0] == "FINISHED"){
this->showMessage( info[1], info[2], QSystemTrayIcon::Information, 5000);
}else if(info[0] == "ERROR"){
this->showMessage( info[1], info[2], QSystemTrayIcon::Warning, 10000);
}
}else if(type=="critical"){
info << "time" << "message" << "device";
info = watcher->getMessages(type,info);
if(!info.isEmpty()){
QString msg = QString(tr("Time: %1")).arg(info[0]);
//message/device information can have more than one listed
QStringList devs = info[2].split(":::");
QStringList msgs = info[1].split(":::");
for(int i=0; i<devs.length(); i++){
msg.append("\n"+QString(tr("%1: %2")).arg(devs[i],msgs[i]) );
}
QString title = tr("System Alert");
QMessageBox::warning(0, title ,msg);
}
}else if(type=="mirror"){
}else if(type=="scrub" || type =="resilver"){
info << "id" << "time" << "message";
info = watcher->getMessages(type, info);
if(!info.isEmpty()){
if(info[0]=="ERROR"){
this->showMessage(info[1], info[2], QSystemTrayIcon::Warning, 5000);
}else{
this->showMessage(info[1], info[2], QSystemTrayIcon::Information, 5000);
}
}
}
//Update the tray icon
updateTrayIcon();
//Update the tooltip
updateToolTip();
}
void LPTray::slotTrayClicked(QSystemTrayIcon::ActivationReason reason){
if(reason == QSystemTrayIcon::Trigger){
if(GUI->isVisible()){ GUI->hide(); }
else{ startGUI(); }
}else if( reason == QSystemTrayIcon::Context){
this->contextMenu()->popup(QCursor::pos());
}
}
void LPTray::slotClose(){
exit(0);
}
void LPTray::slotSingleInstance(){
this->show();
if(!GUI->isVisible()){ startGUI(); }
else{
GUI->raise();
GUI->show();
}
}
void LPTray::startGUI(){
//Start up the GUI
GUI->setupUI();
GUI->raise();
GUI->show();
}
void LPTray::startMessageDialog(){
//Get all the available messages from the watcher
QStringList msgs, errs, logs; //message variables to fill
//Cycle through the watcher types and get any messages
QStringList types; types << "message" << "replication" << "critical" << "mirror" << "resilver" << "scrub";
QStringList infoL; infoL << "id" << "timestamp" << "device" << "message" << "files";
for(int i=0; i<types.length(); i++){
QStringList info = watcher->getMessages(types[i], infoL);
if(info.isEmpty()){continue;}
if(info[0] == "ERROR"){
errs << info[1] + " (" + info[2] + "): " + info[3];
}else if(!info[0].isEmpty()){
msgs << info[1] + " (" + info[2] + "): " + info[3];
}
if(!info[4].isEmpty()){
logs << info[4]; // In format: "example logfile </var/logs/example.log>"
}
}
//Now start up the dialog
msgdlg->setMessages( msgs.join("\n") );
msgdlg->setErrors( errs.join("\n") );
msgdlg->setLogFiles(logs);
msgdlg->raise();
msgdlg->show();
}