1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2013-2013 Bareos GmbH & Co. KG
5 
6    This program is Free Software; you can redistribute it and/or
7    modify it under the terms of version three of the GNU Affero General Public
8    License as published by the Free Software Foundation and included
9    in the file LICENSE.
10 
11    This program is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14    Affero General Public License for more details.
15 
16    You should have received a copy of the GNU Affero General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19    02110-1301, USA.
20 */
21 
22 #include "mainwindow.h"
23 #include "ui_mainwindow.h"
24 #include "systemtrayicon.h"
25 
26 #include <QDebug>
27 #include <QPlainTextEdit>
28 #include <QTimer>
29 #include <QThread>
30 
31 #include "tray-monitor.h"
32 #include "monitoritemthread.h"
33 #include "monitoritem.h"
34 #include "monitortab.h"
35 
36 MainWindow* MainWindow::mainWindowSingleton = NULL;
37 bool MainWindow::already_destroyed = false;
38 
MainWindow(QWidget * parent)39 MainWindow::MainWindow(QWidget* parent)
40     : QMainWindow(parent)
41     , ui(new Ui::MainWindow)
42     , monitorTabMap(new QMap<QString, MonitorTab*>)
43     , systemTrayIcon(new SystemTrayIcon(this))
44 {
45   /* Init the SystemTrayIcon first to have all its signals
46    * configured to be auto-connected via connectSlotsByName(MainWindow)
47    * during ui->setupUi(this). */
48   Q_ASSERT(systemTrayIcon->objectName() == "SystemTrayIcon");
49 
50   // This will setup the tab-window and auto-connect signals and slots.
51   ui->setupUi(this);
52   setWindowTitle(tr("Bareos Tray Monitor"));
53   setWindowIcon(QIcon(":images/bareos_1.png"));
54   ui->pushButton_Close->setIcon(QIcon(":images/f.png"));
55 
56   // Prepare the tabWidget
57   while (ui->tabWidget->count()) { ui->tabWidget->removeTab(0); }
58 
59   nTabs = 100;
60   bRefs = new bool[nTabs];
61   for (int i = 0; i < nTabs; i++) bRefs[i] = true;
62 
63 
64   // Now show the tray icon, but leave the MainWindow hidden.
65   systemTrayIcon->show();
66 }
67 
~MainWindow()68 MainWindow::~MainWindow()
69 {
70   delete ui;
71   delete monitorTabMap;
72   delete bRefs;
73 }
74 
instance()75 MainWindow* MainWindow::instance()
76 {
77   // improve that the MainWindow is created
78   // and deleted only once during program execution
79   Q_ASSERT(!already_destroyed);
80 
81   if (!mainWindowSingleton) { mainWindowSingleton = new MainWindow; }
82 
83   return mainWindowSingleton;
84 }
85 
destruct()86 void MainWindow::destruct()
87 {
88   if (mainWindowSingleton) {
89     delete mainWindowSingleton;
90     mainWindowSingleton = NULL;
91   }
92 }
93 
addTabs(QStringList tabRefs)94 void MainWindow::addTabs(QStringList tabRefs)
95 {
96   tabs = tabRefs;  //
97   nTabs = tabRefs.size();
98 
99   for (int i = 0; i < tabRefs.count(); i++) {
100     MonitorTab* tab = new MonitorTab(tabRefs[i], this);
101     monitorTabMap->insert(tabRefs[i], tab);  // tabRefs[i] used as reference
102     ui->tabWidget->addTab(tab->getTabWidget(), tabRefs[i]);
103   }
104 }
105 
on_TrayMenu_About_triggered()106 void MainWindow::on_TrayMenu_About_triggered()
107 {
108   QString fmt =
109       QString(
110           "<br><h2>Bareos Tray Monitor %1</h2>"
111           "<p>For more information, see: www.bareos.com"
112           "<p>Copyright &copy; 2004-2011 Free Software Foundation Europe e.V."
113           "<p>Copyright &copy; 2011-2012 Planets Communications B.V."
114           "<p>Copyright &copy; 2013-%2 Bareos GmbH & Co. KG"
115           "<p>BAREOS &reg; is a registered trademark of Bareos GmbH & Co. KG"
116           "<p>Licensed under GNU AGPLv3.")
117           .arg(kBareosVersionStrings.Full)
118           .arg(kBareosVersionStrings.Year);
119 
120   QMessageBox::about(this, tr("About Bareos Tray Monitor"), fmt);
121 }
122 
on_TrayMenu_Quit_triggered()123 void MainWindow::on_TrayMenu_Quit_triggered() { QApplication::quit(); }
124 
on_TrayMenu_Display_triggered()125 void MainWindow::on_TrayMenu_Display_triggered()
126 {
127   if (isVisible()) {
128     hide();
129   } else {
130     show();
131     raise();
132     emit refreshItems();
133   }
134 }
135 
on_SystemTrayIcon_activated(QSystemTrayIcon::ActivationReason r)136 void MainWindow::on_SystemTrayIcon_activated(
137     QSystemTrayIcon::ActivationReason r)
138 {
139   if (r == QSystemTrayIcon::Trigger) { on_TrayMenu_Display_triggered(); }
140 }
141 
on_pushButton_Close_clicked()142 void MainWindow::on_pushButton_Close_clicked() { hide(); }
143 
getTextEdit(const QString & tabRef)144 QPlainTextEdit* MainWindow::getTextEdit(const QString& tabRef)
145 {
146   MonitorTab* tab = monitorTabMap->value(tabRef);
147 
148   return (tab) ? tab->getTextEdit() : 0;
149 }
150 
onClearText(const QString & tabRef)151 void MainWindow::onClearText(const QString& tabRef)
152 {
153   QPlainTextEdit* w = getTextEdit(tabRef);
154 
155   if (w) { w->clear(); }
156 }
157 
onAppendText(QString tabRef,QString line)158 void MainWindow::onAppendText(QString tabRef, QString line)
159 {
160   QPlainTextEdit* w = getTextEdit(tabRef);
161 
162   if (w) { w->appendPlainText(line); }
163 }
164 
onShowStatusbarMessage(QString message)165 void MainWindow::onShowStatusbarMessage(QString message)
166 {
167   ui->statusbar->showMessage(message, 3000);
168 }
169 
onStatusChanged(const QString & tabRef,int state)170 void MainWindow::onStatusChanged(const QString& tabRef, int state)
171 {
172   int n = tabs.indexOf(tabRef);
173 
174   MonitorTab* tab = monitorTabMap->value(tabRef);
175 
176   if (tab) {
177     int idx = ui->tabWidget->indexOf(tab->getTabWidget());
178     if (idx < 0) { return; }
179 
180     switch (state) {
181       case MonitorItem::Error:
182 
183         bRefs[n] = false;
184 
185         systemTrayIcon->setNewIcon(2);  // red Icon on Error
186         ui->tabWidget->setTabIcon(idx, QIcon(":images/W.png"));
187         break;
188 
189       case MonitorItem::Running:
190 
191         bRefs[n] = true;
192         if (bRefs[(n + 1) % nTabs] && bRefs[(n + 2) % nTabs])  // if all Tabs OK
193           systemTrayIcon->setNewIcon(0);  // shows blue Icon
194 
195         ui->tabWidget->setTabIcon(idx, QIcon());
196         break;
197 
198       default:
199         break;
200     } /* switch(state) */
201   }   /* if (tab) */
202 }
203 
onFdJobIsRunning(bool running)204 void MainWindow::onFdJobIsRunning(bool running)
205 {
206   systemTrayIcon->animateIcon(running);
207 }
208