]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/tray-monitor/tray-ui.h
Tweak date format in tray monitor
[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->setDisplayFormat("yyyy-MM-dd hh:mm:ss");
109       dateTimeEdit->setDateTime(dt.currentDateTime());
110       fill(messagesCombo, res.messages_list);
111       messagesCombo->setEnabled(false);
112       job_name_change(0);
113       connect(jobCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(job_name_change(int)));
114       connect(cancelButton, SIGNAL(pressed()), this, SLOT(deleteLater()));
115       connect(okButton, SIGNAL(pressed()), this, SLOT(okButtonPushed()));
116       show();
117    }
118
119 private slots:
120
121    void okButtonPushed()
122    {
123       QString cmd;
124       cmd = "run";
125       if (jobCombo->isEnabled()) {
126          cmd += " job=\"" + jobCombo->currentText() + "\"" ;
127       }
128       if (filesetCombo->isEnabled()) {
129          cmd += " fileset=\"" + filesetCombo->currentText() + "\"";
130       }
131       cmd += " level=\"" + levelCombo->currentText() + "\"";
132       if (clientCombo->isEnabled()) {
133          cmd += " client=\"" + clientCombo->currentText() + "\"" ;
134       }
135       if (poolCombo->isEnabled()) {
136          cmd += " pool=\"" + poolCombo->currentText() + "\"";
137       }
138       if (storageCombo->isEnabled()) {
139          cmd += " storage=\"" + storageCombo->currentText() + "\"";
140       }
141       cmd += " priority=\"" + QString().setNum(prioritySpin->value()) + "\"";
142       cmd += " when=\"" + dateTimeEdit->dateTime().toString("yyyy-MM-dd hh:mm:ss") + "\"";
143 #ifdef xxx
144       " messages=\"" << messagesCombo->currentText() << "\"";
145      /* FIXME when there is an option to modify the messages resoruce associated
146       * with a  job */
147 #endif
148       cmd += " yes";
149       qDebug() << cmd;
150       item->D_sock->fsend("%s", cmd.toUtf8().data());
151       QString output;
152       while(bnet_recv(item->D_sock) >= 0) {output += item->D_sock->msg;}
153       QMessageBox msgBox;
154       msgBox.setText(output);
155       msgBox.exec();
156       deleteLater();
157    }
158
159    void job_name_change(int)
160    {
161       job_defaults job_defs;
162       job_defs.job_name = jobCombo->currentText();
163
164       if (item->get_job_defaults(job_defs)) {
165          typeLabel->setText("<H3>"+job_defs.type+"</H3>");
166          filesetCombo->setCurrentIndex(filesetCombo->findText(job_defs.fileset_name, Qt::MatchExactly));
167          levelCombo->setCurrentIndex(levelCombo->findText(job_defs.level, Qt::MatchExactly));
168          clientCombo->setCurrentIndex(clientCombo->findText(job_defs.client_name, Qt::MatchExactly));
169          poolCombo->setCurrentIndex(poolCombo->findText(job_defs.pool_name, Qt::MatchExactly));
170          storageCombo->setCurrentIndex(storageCombo->findText(job_defs.store_name, Qt::MatchExactly));
171          messagesCombo->setCurrentIndex(messagesCombo->findText(job_defs.messages_name, Qt::MatchExactly));
172
173       } else {
174
175       }
176    }
177 };
178
179 void refresh_item();
180 void dotest();
181
182 class TrayUI: public QMainWindow
183 {
184    Q_OBJECT
185
186 public:
187     QWidget *centralwidget;
188     QTabWidget *tabWidget;
189     QStatusBar *statusbar;
190     QHash<QString, QPlainTextEdit*> hash;
191     monitoritem* director;
192
193     QSystemTrayIcon *tray;
194     QSpinBox *spinRefresh;
195     QTimer *timer;
196
197     QPlainTextEdit *getTextEdit(char *title)
198     {
199        return hash.value(QString(title));
200     }
201
202     void clearText(char *title) 
203     {
204        QPlainTextEdit *w = getTextEdit(title);
205        if (!w) {
206           return;
207        }
208        w->clear();
209     }
210
211     void appendText(char *title, char *line) 
212     {
213        QPlainTextEdit *w = getTextEdit(title);
214        if (!w) {
215           return;
216        }
217        w->appendPlainText(QString(line));
218     }
219
220     void addDirector(monitoritem *item)
221     {
222        director = item;
223     }
224
225     void addTab(char *title)
226     {
227        QString t = QString(title);
228        QWidget *tab = new QWidget();
229        QVBoxLayout *vLayout = new QVBoxLayout(tab);
230        QPlainTextEdit *plainTextEdit = new QPlainTextEdit(tab);
231        plainTextEdit->setObjectName(t);
232        plainTextEdit->setReadOnly(true);
233        plainTextEdit->setFont(QFont("courier"));
234        vLayout->addWidget(plainTextEdit);
235        hash.insert(t, plainTextEdit);
236        tabWidget->addTab(tab, t);
237     }
238
239     void startTimer()
240     {
241        if (!timer) {
242           timer = new QTimer(this);
243           connect(timer, SIGNAL(timeout()), this, SLOT(refresh_screen()));
244        }
245        timer->start(spinRefresh->value()*1000);
246     }
247
248     void setupUi(QMainWindow *TrayMonitor)
249     {
250         timer = NULL;
251         director = NULL;
252         if (TrayMonitor->objectName().isEmpty())
253             TrayMonitor->setObjectName(QString::fromUtf8("TrayMonitor"));
254         TrayMonitor->setWindowIcon(QIcon(":/images/cartridge1.png")); 
255         TrayMonitor->resize(789, 595);
256         centralwidget = new QWidget(TrayMonitor);
257         centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
258         QVBoxLayout *verticalLayout = new QVBoxLayout(centralwidget);
259         verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
260         tabWidget = new QTabWidget(centralwidget);
261         tabWidget->setObjectName(QString::fromUtf8("tabWidget"));
262         tabWidget->setTabPosition(QTabWidget::North);
263         tabWidget->setTabShape(QTabWidget::Rounded);
264         tabWidget->setTabsClosable(false);
265         verticalLayout->addWidget(tabWidget);
266
267         QDialogButtonBox *buttonBox = new QDialogButtonBox(centralwidget);
268         buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
269         buttonBox->setStandardButtons(QDialogButtonBox::Close);
270         connect(buttonBox, SIGNAL(rejected()), this, SLOT(cb_show()));
271
272         TrayMonitor->setCentralWidget(centralwidget);
273         statusbar = new QStatusBar(TrayMonitor);
274         statusbar->setObjectName(QString::fromUtf8("statusbar"));
275         TrayMonitor->setStatusBar(statusbar);
276
277         QHBoxLayout *hLayout = new QHBoxLayout();
278         QLabel *refreshlabel = new QLabel(centralwidget);
279         refreshlabel->setText("Refresh:");
280         hLayout->addWidget(refreshlabel);
281         spinRefresh = new QSpinBox(centralwidget);
282         QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
283         sizePolicy.setHorizontalStretch(0);
284         sizePolicy.setVerticalStretch(0);
285         sizePolicy.setHeightForWidth(spinRefresh->sizePolicy().hasHeightForWidth());
286         spinRefresh->setSizePolicy(sizePolicy);
287         spinRefresh->setMinimum(1);
288         spinRefresh->setMaximum(600);
289         spinRefresh->setSingleStep(10);
290         spinRefresh->setValue(60);
291         hLayout->addWidget(spinRefresh);
292         hLayout->addWidget(buttonBox);
293
294         verticalLayout->addLayout(hLayout);
295
296         tray = new QSystemTrayIcon(TrayMonitor);
297         QMenu* stmenu = new QMenu(TrayMonitor);
298
299         QAction *actShow = new QAction(QApplication::translate("TrayMonitor",
300                                "Display",
301                                 0, QApplication::UnicodeUTF8),TrayMonitor);
302         QAction* actQuit = new QAction(QApplication::translate("TrayMonitor",
303                                "Quit",
304                                 0, QApplication::UnicodeUTF8),TrayMonitor);
305         QAction* actAbout = new QAction(QApplication::translate("TrayMonitor",
306                                "About",
307                                 0, QApplication::UnicodeUTF8),TrayMonitor);
308         QAction* actRun = new QAction(QApplication::translate("TrayMonitor",
309                                "Run...",
310                                 0, QApplication::UnicodeUTF8),TrayMonitor);
311         stmenu->addAction(actShow);
312         stmenu->addAction(actRun);
313         stmenu->addAction(actAbout);
314         stmenu->addSeparator();
315         stmenu->addAction(actQuit);
316         
317         connect(actRun, SIGNAL(triggered()), this, SLOT(cb_run()));
318         connect(actShow, SIGNAL(triggered()), this, SLOT(cb_show()));
319         connect(actQuit, SIGNAL(triggered()), this, SLOT(cb_quit()));
320         connect(actAbout, SIGNAL(triggered()), this, SLOT(cb_about()));
321         connect(spinRefresh, SIGNAL(valueChanged(int)), this, SLOT(cb_refresh(int)));
322         connect(tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
323                 this, SLOT(cb_trayIconActivated(QSystemTrayIcon::ActivationReason)));
324
325         tray->setContextMenu(stmenu);
326         QIcon icon(":/images/cartridge1.png");
327         tray->setIcon(icon);
328         tray->setToolTip(QString("Bacula Tray Monitor"));
329         tray->show();
330
331         retranslateUi(TrayMonitor);
332         QMetaObject::connectSlotsByName(TrayMonitor);
333     } // setupUi
334
335     void retranslateUi(QMainWindow *TrayMonitor)
336     {
337        TrayMonitor->setWindowTitle(QApplication::translate("TrayMonitor", "Bacula Tray Monitor", 0, QApplication::UnicodeUTF8));
338     } // retranslateUi
339
340 private slots:
341     void cb_quit() {
342        QApplication::quit();
343     }
344
345     void cb_refresh(int val) {
346        if (timer) {
347           timer->setInterval(val*1000);
348        }
349     }
350
351     void cb_about() {
352        QMessageBox::about(this, "Bacula Tray Monitor", "Bacula Tray Monitor\n"
353                           "For more information, see: www.baculasystems.com\n"
354                           "Copyright (C) 1999-2010, Bacula Systems(R) SA\n"
355                           "Licensed under GNU AGPLv3.");
356     }
357
358     void cb_run() {
359        if (director) {
360           RunDlg *runbox = new RunDlg(director);
361           runbox->show();
362        }
363     }
364
365     void cb_trayIconActivated(QSystemTrayIcon::ActivationReason r) {
366        if (r == QSystemTrayIcon::Trigger) {
367           cb_show();
368        }
369     }
370
371     void refresh_screen() {
372 //       qDebug() << "refresh_screen()";
373        if (isVisible()) {
374           refresh_item();
375 //          qDebug() << "  -> OK";
376        }
377     }
378
379     void cb_show() {
380        if (isVisible()) {
381           hide();
382        } else {
383           refresh_item();
384           show();
385        }
386     }
387 };
388
389
390 #endif  /* TRAYUI_H */