]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/tray-monitor/tray-ui.h
9b774408814536c8cb96d04aea67de4b98632353
[bacula/bacula] / bacula / src / qt-console / tray-monitor / tray-ui.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2011-2011 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation, which is 
11    listed in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24
25    The licensor of Bacula is the Free Software Foundation Europe
26    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
27    Switzerland, email:ftf@fsfeurope.org.
28 */
29
30 #ifndef TRAYUI_H
31 #define TRAYUI_H
32
33 #include <QAction>
34 #include <QApplication>
35 #include <QButtonGroup>
36 #include <QDialogButtonBox>
37 #include <QHeaderView>
38 #include <QMainWindow>
39 #include <QPlainTextEdit>
40 #include <QStatusBar>
41 #include <QTabWidget>
42 #include <QVBoxLayout>
43 #include <QWidget>
44 #include <QLabel>
45 #include <QSpinBox>
46 #include <QMenu>
47 #include <QIcon>
48 #include <QSystemTrayIcon>
49 #include <QTimer>
50 #include <QDebug>
51 #include <QMessageBox>
52 #include <QFont>
53 #include "version.h"
54 #include "ui/ui_run.h"
55 #include "tray-monitor.h"
56
57
58 class RunDlg: public QDialog, public Ui::runForm
59 {
60    Q_OBJECT
61
62 public:
63    monitoritem *item;
64
65    void fill(QComboBox *cb, QStringList &lst) {
66       if (lst.length()) {
67          cb->addItems(lst);
68       } else {
69          cb->setEnabled(false);
70       }
71    }
72    RunDlg(monitoritem *i) {
73       struct resources res;
74       struct job_defaults jdefault;
75       QDateTime dt;
76       item = i;
77
78       qDebug() << "start getting elements";
79       get_list(item, ".jobs type=B", res.job_list);
80       
81       if (res.job_list.length() == 0) {
82          QMessageBox msgBox;
83          msgBox.setText("This restricted console doesn't have access to Backup jobs");
84          msgBox.setIcon(QMessageBox::Warning);
85          msgBox.exec();
86          this->deleteLater();
87          return;
88       }
89
90       get_list(item, ".pools", res.pool_list);
91       get_list(item, ".clients", res.client_list);
92       get_list(item, ".storage", res.storage_list);
93       res.levels << "Full" << "Incremental" << "Differential";
94       get_list(item, ".filesets", res.fileset_list);
95       get_list(item, ".messages", res.messages_list);
96
97       setupUi(this);
98
99       qDebug() << "  -> done";
100       label_5->setVisible(false);
101       bootstrap->setVisible(false);
102       jobCombo->addItems(res.job_list);
103       fill(filesetCombo, res.fileset_list);
104       fill(levelCombo, res.levels);
105       fill(clientCombo, res.client_list);
106       fill(poolCombo, res.pool_list);
107       fill(storageCombo, res.storage_list);
108       dateTimeEdit->setDateTime(dt.currentDateTime());
109       fill(messagesCombo, res.messages_list);
110       messagesCombo->setEnabled(false);
111       job_name_change(0);
112       connect(jobCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(job_name_change(int)));
113       connect(cancelButton, SIGNAL(pressed()), this, SLOT(deleteLater()));
114       connect(okButton, SIGNAL(pressed()), this, SLOT(okButtonPushed()));
115       show();
116    }
117
118 private slots:
119
120    void okButtonPushed()
121    {
122       QString cmd;
123       cmd = "run";
124       if (jobCombo->isEnabled()) {
125          cmd += " job=\"" + jobCombo->currentText() + "\"" ;
126       }
127       if (filesetCombo->isEnabled()) {
128          cmd += " fileset=\"" + filesetCombo->currentText() + "\"";
129       }
130       cmd += " level=\"" + levelCombo->currentText() + "\"";
131       if (clientCombo->isEnabled()) {
132          cmd += " client=\"" + clientCombo->currentText() + "\"" ;
133       }
134       if (poolCombo->isEnabled()) {
135          cmd += " pool=\"" + poolCombo->currentText() + "\"";
136       }
137       if (storageCombo->isEnabled()) {
138          cmd += " storage=\"" + storageCombo->currentText() + "\"";
139       }
140       cmd += " priority=\"" + QString().setNum(prioritySpin->value()) + "\"";
141       cmd += " when=\"" + dateTimeEdit->dateTime().toString("yyyy-MM-dd hh:mm:ss") + "\"";
142 #ifdef xxx
143       " messages=\"" << messagesCombo->currentText() << "\"";
144      /* FIXME when there is an option to modify the messages resoruce associated
145       * with a  job */
146 #endif
147       cmd += " yes";
148       qDebug() << cmd;
149       item->D_sock->fsend("%s", cmd.toUtf8().data());
150       QString output;
151       while(bnet_recv(item->D_sock) >= 0) {output += item->D_sock->msg;}
152       QMessageBox msgBox;
153       msgBox.setText(output);
154       msgBox.exec();
155       deleteLater();
156    }
157
158    void job_name_change(int)
159    {
160       job_defaults job_defs;
161       job_defs.job_name = jobCombo->currentText();
162
163       if (item->get_job_defaults(job_defs)) {
164          typeLabel->setText("<H3>"+job_defs.type+"</H3>");
165          filesetCombo->setCurrentIndex(filesetCombo->findText(job_defs.fileset_name, Qt::MatchExactly));
166          levelCombo->setCurrentIndex(levelCombo->findText(job_defs.level, Qt::MatchExactly));
167          clientCombo->setCurrentIndex(clientCombo->findText(job_defs.client_name, Qt::MatchExactly));
168          poolCombo->setCurrentIndex(poolCombo->findText(job_defs.pool_name, Qt::MatchExactly));
169          storageCombo->setCurrentIndex(storageCombo->findText(job_defs.store_name, Qt::MatchExactly));
170          messagesCombo->setCurrentIndex(messagesCombo->findText(job_defs.messages_name, Qt::MatchExactly));
171
172       } else {
173
174       }
175    }
176 };
177
178 void refresh_item();
179 void dotest();
180
181 class TrayUI: public QMainWindow
182 {
183    Q_OBJECT
184
185 public:
186     QWidget *centralwidget;
187     QTabWidget *tabWidget;
188     QStatusBar *statusbar;
189     QHash<QString, QPlainTextEdit*> hash;
190     monitoritem* director;
191
192     QSystemTrayIcon *tray;
193     QSpinBox *spinRefresh;
194     QTimer *timer;
195
196     QPlainTextEdit *getTextEdit(char *title)
197     {
198        return hash.value(QString(title));
199     }
200
201     void clearText(char *title) 
202     {
203        QPlainTextEdit *w = getTextEdit(title);
204        if (!w) {
205           return;
206        }
207        w->clear();
208     }
209
210     void appendText(char *title, char *line) 
211     {
212        QPlainTextEdit *w = getTextEdit(title);
213        if (!w) {
214           return;
215        }
216        w->appendPlainText(QString(line));
217     }
218
219     void addDirector(monitoritem *item)
220     {
221        director = item;
222     }
223
224     void addTab(char *title)
225     {
226        QString t = QString(title);
227        QWidget *tab = new QWidget();
228        QVBoxLayout *vLayout = new QVBoxLayout(tab);
229        QPlainTextEdit *plainTextEdit = new QPlainTextEdit(tab);
230        plainTextEdit->setObjectName(t);
231        plainTextEdit->setReadOnly(true);
232        plainTextEdit->setFont(QFont("courier"));
233        vLayout->addWidget(plainTextEdit);
234        hash.insert(t, plainTextEdit);
235        tabWidget->addTab(tab, t);
236     }
237
238     void startTimer()
239     {
240        if (!timer) {
241           timer = new QTimer(this);
242           connect(timer, SIGNAL(timeout()), this, SLOT(refresh_screen()));
243        }
244        timer->start(spinRefresh->value()*1000);
245     }
246
247     void setupUi(QMainWindow *TrayMonitor)
248     {
249         timer = NULL;
250         director = NULL;
251         if (TrayMonitor->objectName().isEmpty())
252             TrayMonitor->setObjectName(QString::fromUtf8("TrayMonitor"));
253         TrayMonitor->setWindowIcon(QIcon(":/images/cartridge1.png")); 
254         TrayMonitor->resize(789, 595);
255         centralwidget = new QWidget(TrayMonitor);
256         centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
257         QVBoxLayout *verticalLayout = new QVBoxLayout(centralwidget);
258         verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
259         tabWidget = new QTabWidget(centralwidget);
260         tabWidget->setObjectName(QString::fromUtf8("tabWidget"));
261         tabWidget->setTabPosition(QTabWidget::North);
262         tabWidget->setTabShape(QTabWidget::Rounded);
263         tabWidget->setTabsClosable(false);
264         verticalLayout->addWidget(tabWidget);
265
266         QDialogButtonBox *buttonBox = new QDialogButtonBox(centralwidget);
267         buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
268         buttonBox->setStandardButtons(QDialogButtonBox::Close);
269         connect(buttonBox, SIGNAL(rejected()), this, SLOT(cb_show()));
270
271         TrayMonitor->setCentralWidget(centralwidget);
272         statusbar = new QStatusBar(TrayMonitor);
273         statusbar->setObjectName(QString::fromUtf8("statusbar"));
274         TrayMonitor->setStatusBar(statusbar);
275
276         QHBoxLayout *hLayout = new QHBoxLayout();
277         QLabel *refreshlabel = new QLabel(centralwidget);
278         refreshlabel->setText("Refresh:");
279         hLayout->addWidget(refreshlabel);
280         spinRefresh = new QSpinBox(centralwidget);
281         QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
282         sizePolicy.setHorizontalStretch(0);
283         sizePolicy.setVerticalStretch(0);
284         sizePolicy.setHeightForWidth(spinRefresh->sizePolicy().hasHeightForWidth());
285         spinRefresh->setSizePolicy(sizePolicy);
286         spinRefresh->setMinimum(1);
287         spinRefresh->setMaximum(600);
288         spinRefresh->setSingleStep(10);
289         spinRefresh->setValue(60);
290         hLayout->addWidget(spinRefresh);
291         hLayout->addWidget(buttonBox);
292
293         verticalLayout->addLayout(hLayout);
294
295         tray = new QSystemTrayIcon(TrayMonitor);
296         QMenu* stmenu = new QMenu(TrayMonitor);
297
298         QAction *actShow = new QAction(QApplication::translate("TrayMonitor",
299                                "Display",
300                                 0, QApplication::UnicodeUTF8),TrayMonitor);
301         QAction* actQuit = new QAction(QApplication::translate("TrayMonitor",
302                                "Quit",
303                                 0, QApplication::UnicodeUTF8),TrayMonitor);
304         QAction* actAbout = new QAction(QApplication::translate("TrayMonitor",
305                                "About",
306                                 0, QApplication::UnicodeUTF8),TrayMonitor);
307         QAction* actRun = new QAction(QApplication::translate("TrayMonitor",
308                                "Run...",
309                                 0, QApplication::UnicodeUTF8),TrayMonitor);
310         stmenu->addAction(actShow);
311         stmenu->addAction(actRun);
312         stmenu->addAction(actAbout);
313         stmenu->addSeparator();
314         stmenu->addAction(actQuit);
315         
316         connect(actRun, SIGNAL(triggered()), this, SLOT(cb_run()));
317         connect(actShow, SIGNAL(triggered()), this, SLOT(cb_show()));
318         connect(actQuit, SIGNAL(triggered()), this, SLOT(cb_quit()));
319         connect(actAbout, SIGNAL(triggered()), this, SLOT(cb_about()));
320         connect(spinRefresh, SIGNAL(valueChanged(int)), this, SLOT(cb_refresh(int)));
321         connect(tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
322                 this, SLOT(cb_trayIconActivated(QSystemTrayIcon::ActivationReason)));
323
324         tray->setContextMenu(stmenu);
325         QIcon icon(":/images/cartridge1.png");
326         tray->setIcon(icon);
327         tray->setToolTip(QString("Bacula Tray Monitor"));
328         tray->show();
329
330         retranslateUi(TrayMonitor);
331         QMetaObject::connectSlotsByName(TrayMonitor);
332     } // setupUi
333
334     void retranslateUi(QMainWindow *TrayMonitor)
335     {
336        TrayMonitor->setWindowTitle(QApplication::translate("TrayMonitor", "Bacula Tray Monitor", 0, QApplication::UnicodeUTF8));
337     } // retranslateUi
338
339 private slots:
340     void cb_quit() {
341        QApplication::quit();
342     }
343
344     void cb_refresh(int val) {
345        if (timer) {
346           timer->setInterval(val*1000);
347        }
348     }
349
350     void cb_about() {
351        QMessageBox::about(this, "Bacula Tray Monitor", "Bacula Tray Monitor\n"
352                           "For more information, see: www.baculasystems.com\n"
353                           "Copyright (C) 1999-2010, Bacula Systems(R) SA\n"
354                           "Licensed under GNU AGPLv3.");
355     }
356
357     void cb_run() {
358        if (director) {
359           RunDlg *runbox = new RunDlg(director);
360           runbox->show();
361        }
362     }
363
364     void cb_trayIconActivated(QSystemTrayIcon::ActivationReason r) {
365        if (r == QSystemTrayIcon::Trigger) {
366           cb_show();
367        }
368     }
369
370     void refresh_screen() {
371 //       qDebug() << "refresh_screen()";
372        if (isVisible()) {
373           refresh_item();
374 //          qDebug() << "  -> OK";
375        }
376     }
377
378     void cb_show() {
379        if (isVisible()) {
380           hide();
381        } else {
382           refresh_item();
383           show();
384        }
385     }
386 };
387
388
389 #endif  /* TRAYUI_H */