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