source: src-qt4/life-preserver/LPTray.cpp @ e0c368d

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2
Last change on this file since e0c368d was e0c368d, checked in by Ken Moore <ken@…>, 10 months ago

Create a new "LPMessages" dialog class and integrate it into the tray application. This will show the full messages that are available, as well as display the contents of any known log/error files that the watcher is aware of.

  • Property mode set to 100644
File size: 5.3 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/backup-failed.png"),tr("View Messages"),this,SLOT(startMessageDialog()) );
12        menu->addAction(QIcon(":/images/tray-icon-idle.png"),tr("Start Application UI"),this,SLOT(startGUI()) );
13        menu->addSeparator();
14        menu->addAction(QIcon(":/images/application-exit.png"),tr("Close Life Preserver Tray"),this,SLOT(slotClose()) );
15  this->setContextMenu(menu);
16  //Setup initial icon for the tray
17  this->setIcon( QIcon(":/images/tray-icon-idle.png") );
18  //Create the configuration GUI
19  GUI = new mainUI();
20  msgdlg = new LPMessages();
21  //connect other signals/slots
22  connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(slotTrayClicked(QSystemTrayIcon::ActivationReason)) );
23  //Start up the watcher
24  watcher->start();
25  updateTrayIcon();
26  updateToolTip();
27}
28
29LPTray::~LPTray(){
30  watcher->stop();
31  delete watcher;
32  delete GUI;
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){ 
118    if(GUI->isVisible()){ GUI->hide(); }
119    else{ startGUI(); }
120  }else if( reason == QSystemTrayIcon::Context){
121    this->contextMenu()->popup(QCursor::pos());
122  }
123}
124
125void LPTray::slotClose(){
126  exit(0);
127}
128
129void LPTray::slotSingleInstance(){
130  this->show();
131  if(!GUI->isVisible()){ startGUI(); }
132  else{ 
133    GUI->raise(); 
134    GUI->show();
135  }
136}
137
138void LPTray::startGUI(){
139  //Start up the GUI
140    GUI->setupUI();
141    GUI->raise();
142    GUI->show();
143}
144
145void LPTray::startMessageDialog(){
146  //Get all the available messages from the watcher
147  QStringList msgs, errs, logs; //message variables to fill
148  //Cycle through the watcher types and get any messages
149  QStringList types; types << "message" << "replication" << "critical" << "mirror" << "resilver" << "scrub";
150  QStringList infoL; infoL << "id" << "timestamp" << "device" << "message" << "files";
151  for(int i=0; i<types.length(); i++){
152    QStringList info = watcher->getMessages(types[i], infoL);
153    if(info.isEmpty()){continue;}
154    if(info[0] == "ERROR"){
155      errs << info[1] + " (" + info[2] + "): " + info[3];
156    }else if(!info[0].isEmpty()){
157      msgs << info[1] + " (" + info[2] + "): " + info[3];
158    }
159    if(!info[4].isEmpty()){
160      logs << info[4]; // In format:  "example logfile </var/logs/example.log>"
161    }
162  }
163  //Now start up the dialog
164  msgdlg->setMessages( msgs.join("\n") );
165  msgdlg->setErrors( errs.join("\n") );
166  msgdlg->setLogFiles(logs);
167  msgdlg->raise();
168  msgdlg->show();
169}
Note: See TracBrowser for help on using the repository browser.