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