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