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

releng/10.0
Last change on this file since 94f8116 was 94f8116, checked in by Kris Moore <kris@…>, 7 months ago

Adjust the life-preserver tray single-instance logic: if the message dialog is currently open, just show/focus on that, otherwise start the process to launch the full GUI.

  • Property mode set to 100644
File size: 5.6 KB
Line 
1#include "LPTray.h"
2
3//PUBLIC
4LPTray::LPTray() : QSystemTrayIcon(){
5  //Start up the log file watcher and connect the signals/slots
6  watcher = new LPWatcher();
7        connect(watcher,SIGNAL(MessageAvailable(QString)),this,SLOT(watcherMessage(QString)) );
8        connect(watcher,SIGNAL(StatusUpdated()),this,SLOT(watcherMessage()) );
9  //Setup the context menu
10  menu = new QMenu;
11        menu->addAction(QIcon(":/images/tray-icon-idle.png"),tr("Open Life Preserver"),this,SLOT(startGUI()) );
12        menu->addSeparator();
13        menu->addAction(QIcon(":/images/backup-failed.png"),tr("View Messages"),this,SLOT(startMessageDialog()) );
14        menu->addAction(QIcon(":/images/refresh.png"),tr("Refresh Tray"),this,SLOT(refreshStatus()) );
15        menu->addSeparator();
16        menu->addAction(QIcon(":/images/application-exit.png"),tr("Close Tray"),this,SLOT(slotClose()) );
17  this->setContextMenu(menu);
18  //Setup initial icon for the tray
19  this->setIcon( QIcon(":/images/tray-icon-idle.png") );
20  //Create the messages GUI
21  msgdlg = new LPMessages();
22  //connect other signals/slots
23  connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(slotTrayClicked(QSystemTrayIcon::ActivationReason)) );
24  //Start up the watcher
25  watcher->start();
26  updateTrayIcon();
27  updateToolTip();
28}
29
30LPTray::~LPTray(){
31  watcher->stop();
32  delete watcher;
33  delete msgdlg;
34}
35
36// ===============
37//  PRIVATE FUNCTIONS
38// ===============
39void LPTray::updateTrayIcon(){
40  if( watcher->hasError() ){
41    //Errors - show that attention is required
42    this->setIcon( QIcon(":/images/tray-icon-error.png") );
43  }else if( watcher->isRunning() ){
44    //Show the working icon
45    this->setIcon( QIcon(":/images/tray-icon-working.png") );
46  }else{
47    //Show the idle icon
48    this->setIcon( QIcon(":/images/tray-icon-idle.png") );
49  }
50 
51}
52
53void LPTray::updateToolTip(){
54  QStringList info = watcher->getAllCurrentMessages();
55  this->setToolTip( info.join("\n") ); 
56}
57
58// ===============
59//     PRIVATE SLOTS
60// ===============
61void LPTray::watcherMessage(QString type){
62  qDebug() << "New Watcher Message:" << type;
63  QStringList info;
64  if(type=="message"){
65    //Show the message pop-up
66    info << "time" << "message";
67    info = watcher->getMessages(type,info);
68    if(!info.isEmpty()){
69      this->showMessage( info[0], info[1], QSystemTrayIcon::Information, 5000);
70    }
71  }else if(type=="replication"){
72    info << "id" << "time" << "message";
73    info = watcher->getMessages(type,info);
74    if(info.isEmpty()){ return; }
75    if(info[0] == "STARTED"){
76      this->showMessage( info[1], info[2], QSystemTrayIcon::Information, 5000);
77    }else if(info[0] == "FINISHED"){
78      this->showMessage( info[1], info[2], QSystemTrayIcon::Information, 5000);
79    }else if(info[0] == "ERROR"){
80      this->showMessage( info[1], info[2], QSystemTrayIcon::Warning, 10000);
81    }
82   
83  }else if(type=="critical"){
84    info << "time" << "message" << "device";
85    info = watcher->getMessages(type,info);
86    if(!info.isEmpty()){
87      QString msg = QString(tr("Time: %1")).arg(info[0]);
88      //message/device information can have more than one listed
89      QStringList devs = info[2].split(":::");     
90      QStringList msgs = info[1].split(":::");
91      for(int i=0; i<devs.length(); i++){
92        msg.append("\n"+QString(tr("%1: %2")).arg(devs[i],msgs[i]) );
93      }
94      QString title = tr("System Alert");
95      QMessageBox::warning(0, title ,msg);
96    }
97  }else if(type=="mirror"){
98         
99  }else if(type=="scrub" || type =="resilver"){
100    info << "id" << "time" << "message";
101    info = watcher->getMessages(type, info);
102    if(!info.isEmpty()){
103      if(info[0]=="ERROR"){ 
104        this->showMessage(info[1], info[2], QSystemTrayIcon::Warning, 5000);
105      }else{ 
106        this->showMessage(info[1], info[2], QSystemTrayIcon::Information, 5000); 
107      }
108    }
109  }
110  //Update the tray icon
111  updateTrayIcon();
112  //Update the tooltip
113  updateToolTip();
114}
115
116void LPTray::slotTrayClicked(QSystemTrayIcon::ActivationReason reason){
117  if(reason == QSystemTrayIcon::Trigger || reason == QSystemTrayIcon::Context){
118    this->contextMenu()->popup(QCursor::pos());
119  }
120}
121
122void LPTray::slotClose(){
123  exit(0);
124}
125
126void LPTray::slotSingleInstance(){
127  this->show();
128  if(msgdlg->isVisible()){ 
129    msgdlg->raise(); 
130    msgdlg->show();
131  }else{
132    startGUI();
133  }
134}
135
136void LPTray::startGUI(){
137  //Start up the GUI
138  QProcess::startDetached("pc-su life-preserver");
139}
140
141void LPTray::startMessageDialog(){
142  //Get all the available messages from the watcher
143  QStringList msgs, errs, logs; //message variables to fill
144  //Cycle through the watcher types and get any messages
145  QStringList types; types << "message" << "replication" << "critical" << "mirror" << "resilver" << "scrub";
146  QStringList infoL; infoL << "id" << "timestamp" << "device" << "message" << "files";
147  for(int i=0; i<types.length(); i++){
148    QStringList info = watcher->getMessages(types[i], infoL);
149    if(info.isEmpty()){continue;}
150    //Take into account multiple devices/messages per type
151    QStringList devs=info[2].split(":::"); QStringList msgs=info[3].split(":::");
152    for(int j=0; j<devs.length(); j++){
153      if(info[0] == "ERROR"){
154        errs << info[1] + " (" + devs[j] + "): " + msgs[j];
155      }else if(!info[0].isEmpty()){
156        msgs << info[1] + " (" + devs[j] + "): " + msgs[j];
157      }
158    }
159    if(!info[4].isEmpty()){
160      //allow for multiple log files
161      logs << info[4].split(":::"); // In format:  "example logfile </var/logs/example.log>"
162    }
163  }
164  //Now start up the dialog
165  msgdlg->setMessages( msgs.join("\n"), errs.join("\n") );
166  msgdlg->setLogFiles(logs);
167  msgdlg->raise();
168  msgdlg->show();
169}
170
171void LPTray::refreshStatus(){
172  //Manually trigger the watcher to update status notifications
173  watcher->refresh();
174}
Note: See TracBrowser for help on using the repository browser.