source: src-qt4/life-preserver/lp-tray/LPTray.cpp @ cbe0295

releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1
Last change on this file since cbe0295 was cbe0295, checked in by Ken Moore <ken@…>, 9 months ago

Add a popup-message ppolicy setting to the life preserver tray so that you can select to show popups for all messages, errors only, or none. This will not touch "critical" messages, which will cause an actual window to appear that requires attention.

  • Property mode set to 100644
File size: 7.8 KB
Line 
1#include "LPTray.h"
2
3//PUBLIC
4LPTray::LPTray() : QSystemTrayIcon(){
5  qDebug() << "Starting up Life-preserver Tray...";
6  //Start up the log file watcher and connect the signals/slots
7  watcher = new LPWatcher();
8        connect(watcher,SIGNAL(MessageAvailable(QString)),this,SLOT(watcherMessage(QString)) );
9        connect(watcher,SIGNAL(StatusUpdated()),this,SLOT(watcherMessage()) );
10  //Load the tray settings file
11  settings = new QSettings(QSettings::UserScope,"PCBSD", "life-preserver-tray");
12  //Create the notification option widgets
13  nShowAll = new QRadioButton(tr("Show all"));
14        naAll = new QWidgetAction(this);
15        naAll->setDefaultWidget(nShowAll);
16  nShowError = new QRadioButton(tr("Warnings Only"));
17        naErr = new QWidgetAction(this);
18        naErr->setDefaultWidget(nShowError);
19  nShowNone = new QRadioButton(tr("None"));
20        naNone = new QWidgetAction(this);
21        naNone->setDefaultWidget(nShowNone);
22  //Create notification menu
23  notificationMenu = new QMenu(tr("Popup Settings"));
24        notificationMenu->setIcon( QIcon(":/images/configure.png") );
25        notificationMenu->addAction(naAll);
26        notificationMenu->addAction(naErr);
27        notificationMenu->addAction(naNone);
28  //Activate the proper notification setting widget
29  QString popset = settings->value("popup-policy", "").toString();
30  qDebug() << "Current Popup Policy:" << popset;
31  if(popset=="all"){ nShowAll->setChecked(true); popupPolicy = 2; }
32  else if(popset=="errors"){ nShowError->setChecked(true); popupPolicy = 1; }
33  else if(popset=="none"){ nShowNone->setChecked(true); popupPolicy = 0; }
34  else{ 
35    nShowError->setChecked(true);
36    settings->setValue("popup-policy","errors"); //save the proper setting
37    popupPolicy = 1;
38  }
39  //Now connect the popup settings signals/slots
40  connect(nShowAll, SIGNAL(clicked()), this, SLOT(changePopupPolicy()) );
41  connect(nShowError, SIGNAL(clicked()), this, SLOT(changePopupPolicy()) );
42  connect(nShowNone, SIGNAL(clicked()), this, SLOT(changePopupPolicy()) );
43  //Setup the context menu
44  menu = new QMenu;
45        menu->addAction(QIcon(":/images/tray-icon-idle.png"),tr("Open Life Preserver"),this,SLOT(startGUI()) );
46        menu->addSeparator();
47        menu->addAction(QIcon(":/images/backup-failed.png"),tr("View Messages"),this,SLOT(startMessageDialog()) );
48        menu->addMenu(notificationMenu);
49        menu->addAction(QIcon(":/images/refresh.png"),tr("Refresh Tray"),this,SLOT(refreshStatus()) );
50        menu->addSeparator();
51        menu->addAction(QIcon(":/images/application-exit.png"),tr("Close Tray"),this,SLOT(slotClose()) );
52  this->setContextMenu(menu);
53  //Setup initial icon for the tray
54  this->setIcon( QIcon(":/images/tray-icon-idle.png") );
55  //Create the messages GUI
56  msgdlg = new LPMessages();
57  //connect other signals/slots
58  connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(slotTrayClicked(QSystemTrayIcon::ActivationReason)) );
59  //Start up the watcher
60  watcher->start();
61  updateTrayIcon();
62  updateToolTip();
63}
64
65LPTray::~LPTray(){
66  watcher->stop();
67  delete watcher;
68  delete msgdlg;
69}
70
71// ===============
72//  PRIVATE FUNCTIONS
73// ===============
74void LPTray::updateTrayIcon(){
75  if( watcher->hasError() ){
76    //Errors - show that attention is required
77    this->setIcon( QIcon(":/images/tray-icon-error.png") );
78  }else if( watcher->isRunning() ){
79    //Show the working icon
80    this->setIcon( QIcon(":/images/tray-icon-working.png") );
81  }else{
82    //Show the idle icon
83    this->setIcon( QIcon(":/images/tray-icon-idle.png") );
84  }
85 
86}
87
88void LPTray::updateToolTip(){
89  QStringList info = watcher->getAllCurrentMessages();
90  this->setToolTip( info.join("\n") ); 
91}
92
93// ===============
94//     PRIVATE SLOTS
95// ===============
96void LPTray::watcherMessage(QString type){
97  qDebug() << "New Watcher Message:" << type;
98  QStringList info;
99  if(type=="message" &&  popupPolicy > 1){
100    //Show the message pop-up
101    info << "time" << "message";
102    info = watcher->getMessages(type,info);
103    if(!info.isEmpty()){
104      this->showMessage( info[0], info[1], QSystemTrayIcon::Information, 5000);
105    }
106  }else if(type=="replication"){
107    info << "id" << "time" << "message";
108    info = watcher->getMessages(type,info);
109    if(info.isEmpty()){ return; }
110    if(info[0] == "STARTED" && popupPolicy > 1 ){
111      this->showMessage( info[1], info[2], QSystemTrayIcon::Information, 5000);
112    }else if(info[0] == "FINISHED" && popupPolicy > 1){
113      this->showMessage( info[1], info[2], QSystemTrayIcon::Information, 5000);
114    }else if(info[0] == "ERROR" && popupPolicy > 0){
115      this->showMessage( info[1], info[2], QSystemTrayIcon::Warning, 10000);
116    }
117   
118  }else if(type=="critical"){
119    info << "time" << "message" << "device";
120    info = watcher->getMessages(type,info);
121    if(!info.isEmpty()){
122      QString msg = QString(tr("Time: %1")).arg(info[0]);
123      //message/device information can have more than one listed
124      QStringList devs = info[2].split(":::");     
125      QStringList msgs = info[1].split(":::");
126      for(int i=0; i<devs.length(); i++){
127        msg.append("\n"+QString(tr("%1: %2")).arg(devs[i],msgs[i]) );
128      }
129      QString title = tr("System Alert");
130      QMessageBox::warning(0, title ,msg);
131    }
132  }else if(type=="mirror"){
133         
134  }else if(type=="scrub" || type =="resilver"){
135    info << "id" << "time" << "message";
136    info = watcher->getMessages(type, info);
137    if(!info.isEmpty()){
138      if(info[0]=="ERROR" && popupPolicy > 0 ){ 
139        this->showMessage(info[1], info[2], QSystemTrayIcon::Warning, 5000);
140      }else if(popupPolicy > 1){ 
141        this->showMessage(info[1], info[2], QSystemTrayIcon::Information, 5000); 
142      }
143    }
144  }
145  //Update the tray icon
146  updateTrayIcon();
147  //Update the tooltip
148  updateToolTip();
149}
150
151void LPTray::slotTrayClicked(QSystemTrayIcon::ActivationReason reason){
152  if(reason == QSystemTrayIcon::Trigger || reason == QSystemTrayIcon::Context){
153    this->contextMenu()->popup(QCursor::pos());
154  }
155}
156
157void LPTray::slotClose(){
158  exit(0);
159}
160
161void LPTray::slotSingleInstance(){
162  this->show();
163  if(msgdlg->isVisible()){ 
164    msgdlg->raise(); 
165    msgdlg->show();
166  }else if(!watcher->initPhase()){
167    startGUI();
168  }
169}
170
171void LPTray::startGUI(){
172  //Start up the GUI
173  QProcess::startDetached("pc-su life-preserver");
174}
175
176void LPTray::startMessageDialog(){
177  //Get all the available messages from the watcher
178  QStringList msgs, errs, logs; //message variables to fill
179  //Cycle through the watcher types and get any messages
180  QStringList types; types << "message" << "replication" << "critical" << "mirror" << "resilver" << "scrub";
181  QStringList infoL; infoL << "id" << "timestamp" << "device" << "message" << "files";
182  for(int i=0; i<types.length(); i++){
183    QStringList info = watcher->getMessages(types[i], infoL);
184    if(info.isEmpty()){continue;}
185    //Take into account multiple devices/messages per type
186    QStringList devs=info[2].split(":::"); QStringList msgs=info[3].split(":::");
187    for(int j=0; j<devs.length(); j++){
188      if(info[0] == "ERROR"){
189        errs << info[1] + " (" + devs[j] + "): " + msgs[j];
190      }else if(!info[0].isEmpty()){
191        msgs << info[1] + " (" + devs[j] + "): " + msgs[j];
192      }
193    }
194    if(!info[4].isEmpty()){
195      //allow for multiple log files
196      logs << info[4].split(":::"); // In format:  "example logfile </var/logs/example.log>"
197    }
198  }
199  //Now start up the dialog
200  msgdlg->setMessages( msgs.join("\n"), errs.join("\n") );
201  msgdlg->setLogFiles(logs);
202  msgdlg->raise();
203  msgdlg->show();
204}
205
206void LPTray::refreshStatus(){
207  //Manually trigger the watcher to update status notifications
208  watcher->refresh();
209}
210
211void LPTray::changePopupPolicy(){
212  QString policy;
213  if(nShowAll->isChecked()){
214    policy = "all";
215    popupPolicy = 2;
216  }else if(nShowError->isChecked()){
217    policy = "errors";
218    popupPolicy = 1;
219  }else if(nShowNone->isChecked()){
220    policy = "none";
221    popupPolicy = 0;
222  }else{
223    policy = "errors";
224    popupPolicy = 1;
225  }
226  qDebug() << "Change Popup Policy:" << policy;
227  settings->setValue("popup-policy", policy);
228}
Note: See TracBrowser for help on using the repository browser.