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