2 Bacula® - The Network Backup Solution
4 Copyright (C) 2007-2007 Free Software Foundation Europe e.V.
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 two of the GNU General Public
10 License as published by the Free Software Foundation plus additions
11 that are listed in the file LICENSE.
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.
18 You should have received a copy of the GNU 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
23 Bacula® is a registered trademark of John Walker.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
32 * Main Window control for bat (qt-console)
34 * Kern Sibbald, January MMVII
39 #include "joblist/joblist.h"
40 #include "storage/storage.h"
41 #include "fileset/fileset.h"
42 #include "label/label.h"
45 #include "restore/restore.h"
46 #include "medialist/medialist.h"
47 #include "joblist/joblist.h"
48 #include "clients/clients.h"
50 MainWin::MainWin(QWidget *parent) : QMainWindow(parent)
54 setupUi(this); /* Setup UI defined by main.ui (designer) */
56 treeWidget->setColumnCount(1);
57 treeWidget->setHeaderLabel("Select Page");
58 treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
70 foreach(Console *console, m_consoleHash){
73 m_currentConsole = (Console*)getFromHash(m_firstItem);
74 m_currentConsole->setCurrent();
76 * I'd like to turn this into a debug item
77 * DIRRES* dirres = m_currentConsole->getDirRes();
78 * printf("Setting initial window to %s\n", dirres->name());
82 void MainWin::createPages()
85 QTreeWidgetItem *item, *topItem;
89 foreach_res(dir, R_DIRECTOR) {
91 /* Create console tree stacked widget item */
92 m_currentConsole = new Console(stackedWidget);
93 m_currentConsole->setDirRes(dir);
94 m_currentConsole->readSettings();
96 /* The top tree item representing the director */
97 topItem = createTopPage(dir->name());
98 topItem->setIcon(0, QIcon(":images/server.png"));
99 /* Set background to grey for ease of identification of inactive dirfector */
100 QBrush greyBrush(Qt::lightGray);
101 topItem->setBackground(0, greyBrush);
102 m_currentConsole->setDirectorTreeItem(topItem);
103 m_consoleHash.insert(topItem, m_currentConsole);
105 /* Create Tree Widget Item */
106 item = createPage("Console", topItem);
107 if (!m_firstItem){ m_firstItem = item; }
109 /* insert the cosole and tree widget item into the hashes */
110 hashInsert(item, m_currentConsole);
112 /* Set Color of treeWidgetItem for the console
113 * It will be set to green in the console class if the connection is made.
115 QBrush redBrush(Qt::red);
116 item->setForeground(0, redBrush);
117 m_currentConsole->dockPage();
119 /* create instances of the rest of the classes that will by default exist
120 * under each director */
121 // createPagebRestore();
122 createPageMediaList();
123 QString emptymedia(""), emptyclient("");
124 createPageJobList(emptymedia, emptyclient, NULL);
129 treeWidget->expandItem(topItem);
130 stackedWidget->setCurrentWidget(m_currentConsole);
136 * create an instance of the the brestore class on the stack
138 void MainWin::createPagebRestore()
140 bRestore* brestore = new bRestore();
141 brestore->dockPage();
145 * create an instance of the the medialist class on the stack
147 void MainWin::createPageMediaList()
149 MediaList* medialist = new MediaList();
150 medialist->dockPage();
154 * create an instance of the the joblist class on the stack
156 void MainWin::createPageJobList(QString &media, QString &client,
157 QTreeWidgetItem *parentTreeWidgetItem)
159 QTreeWidgetItem *holdItem;
161 /* save current tree widget item in case query produces no results */
162 holdItem = treeWidget->currentItem();
163 JobList* joblist = new JobList(media, client, parentTreeWidgetItem);
165 /* If this is a query of jobs on a specific media */
166 if ((media != "") || (client != "")) {
167 joblist->setCurrent();
168 /* did query produce results, if not close window and set back to hold */
169 if (joblist->m_resultCount == 0) {
170 joblist->closeStackPage();
171 treeWidget->setCurrentItem(holdItem);
177 * create an instance of the the Clients class on the stack
179 void MainWin::createPageClients()
181 Clients* clients = new Clients();
186 * create an instance of the the storage class on the stack
188 void MainWin::createPageStorage()
190 Storage* storage = new Storage();
195 * create an instance of the the fileset class on the stack
197 void MainWin::createPageFileSet()
199 FileSet* fileset = new FileSet();
203 /* Create a root Tree Widget */
204 QTreeWidgetItem *MainWin::createTopPage(char *name)
206 QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
207 item->setText(0, name);
211 /* Create A Tree Widget Item which will be associated with a Page in the stacked widget */
212 QTreeWidgetItem *MainWin::createPage(char *name, QTreeWidgetItem *parent)
214 QTreeWidgetItem *item = new QTreeWidgetItem(parent);
215 item->setText(0, name);
220 * Handle up and down arrow keys for the command line
223 void MainWin::keyPressEvent(QKeyEvent *event)
225 if (m_cmd_history.size() == 0) {
229 switch (event->key()) {
231 if (m_cmd_last < 0 || m_cmd_last >= (m_cmd_history.size()-1)) {
238 if (m_cmd_last == 0) {
242 if (m_cmd_last < 0 || m_cmd_last > (m_cmd_history.size()-1)) {
243 m_cmd_last = m_cmd_history.size() - 1;
252 lineEdit->setText(m_cmd_history[m_cmd_last]);
255 void MainWin::createConnections()
257 /* Connect signals to slots */
258 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(input_line()));
259 connect(actionAbout_bat, SIGNAL(triggered()), this, SLOT(about()));
260 connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this,
261 SLOT(treeItemClicked(QTreeWidgetItem *, int)));
262 connect(treeWidget, SIGNAL(
263 currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
264 this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
265 connect(stackedWidget, SIGNAL(currentChanged(int)),
266 this, SLOT(stackItemChanged(int)));
267 connect(actionQuit, SIGNAL(triggered()), app, SLOT(closeAllWindows()));
268 connect(actionLabel, SIGNAL(triggered()), this, SLOT(labelButtonClicked()));
269 connect(actionRun, SIGNAL(triggered()), this, SLOT(runButtonClicked()));
270 connect(actionRestore, SIGNAL(triggered()), this, SLOT(restoreButtonClicked()));
271 connect(actionUndock, SIGNAL(triggered()), this, SLOT(undockWindowButton()));
272 connect(actionToggleDock, SIGNAL(triggered()), this, SLOT(toggleDockContextWindow()));
273 connect(actionClosePage, SIGNAL(triggered()), this, SLOT(closePage()));
274 connect(actionPreferences, SIGNAL(triggered()), this, SLOT(setPreferences()));
278 * Reimplementation of QWidget closeEvent virtual function
280 void MainWin::closeEvent(QCloseEvent *event)
283 foreach(Console *console, m_consoleHash){
284 console->writeSettings();
285 console->terminate();
288 foreach(Pages *page, m_pagehash) {
289 if (!page->isDocked())
294 void MainWin::writeSettings()
296 QSettings settings("bacula.org", "bat");
298 settings.beginGroup("MainWin");
299 settings.setValue("winSize", size());
300 settings.setValue("winPos", pos());
304 void MainWin::readSettings()
306 QSettings settings("bacula.org", "bat");
308 settings.beginGroup("MainWin");
309 resize(settings.value("winSize", QSize(1041, 801)).toSize());
310 move(settings.value("winPos", QPoint(200, 150)).toPoint());
315 * This subroutine is called with an item in the Page Selection window
318 void MainWin::treeItemClicked(QTreeWidgetItem *item, int /*column*/)
320 /* Is this a page that has been inserted into the hash */
321 if (getFromHash(item)) {
322 Pages* page = getFromHash(item);
323 int stackindex=stackedWidget->indexOf(page);
325 if (stackindex >= 0) {
326 stackedWidget->setCurrentWidget(page);
328 /* run the virtual function in case this class overrides it */
329 page->PgSeltreeWidgetClicked();
334 * Called with a change of the highlighed tree widget item in the page selector.
336 void MainWin::treeItemChanged(QTreeWidgetItem *currentitem, QTreeWidgetItem *previousitem)
338 Pages *previousPage, *nextPage;
339 Console *previousConsole, *nextConsole;
341 /* first determine the next item */
343 /* knowing the treeWidgetItem, get the page from the hash */
344 nextPage = getFromHash(currentitem);
345 nextConsole = m_consoleHash.value(currentitem);
346 /* Is this a page that has been inserted into the hash */
348 nextConsole = nextPage->console();
349 /* then is it a treeWidgetItem representing a director */
350 } else if (nextConsole) {
351 /* let the next page BE the console */
352 nextPage = nextConsole;
354 /* Should never get here */
359 /* The Previous item */
361 /* this condition prevents a segfault. The first time there is no previousitem*/
363 /* knowing the treeWidgetItem, get the page from the hash */
364 previousPage = getFromHash(previousitem);
365 previousConsole = m_consoleHash.value(previousitem);
367 previousConsole = previousPage->console();
368 } else if (previousConsole) {
369 previousPage = previousConsole;
371 if ((previousPage) || (previousConsole)) {
372 if (nextConsole != previousConsole) {
373 /* remove connections to the current console */
374 disconnect(actionConnect, SIGNAL(triggered()), previousConsole, SLOT(connect()));
375 disconnect(actionStatusDir, SIGNAL(triggered()), previousConsole, SLOT(status_dir()));
376 disconnect(actionMessages, SIGNAL(triggered()), previousConsole, SLOT(messages()));
377 disconnect(actionSelectFont, SIGNAL(triggered()), previousConsole, SLOT(set_font()));
378 QTreeWidgetItem *dirItem = previousConsole->directorTreeItem();
379 QBrush greyBrush(Qt::lightGray);
380 dirItem->setBackground(0, greyBrush);
382 /* make sure the close window and toggle dock options are removed */
383 treeWidget->removeAction(actionClosePage);
384 treeWidget->removeAction(actionToggleDock);
385 /* Is this a page that has been inserted into the hash */
387 foreach(QAction* pageaction, previousPage->m_contextActions) {
388 treeWidget->removeAction(pageaction);
394 /* process the current (next) item */
396 if ((nextPage) || (nextConsole)) {
397 if (nextConsole != previousConsole) {
398 /* make connections to the current console */
399 m_currentConsole = nextConsole;
400 connect(actionConnect, SIGNAL(triggered()), m_currentConsole, SLOT(connect()));
401 connect(actionSelectFont, SIGNAL(triggered()), m_currentConsole, SLOT(set_font()));
402 connect(actionStatusDir, SIGNAL(triggered()), m_currentConsole, SLOT(status_dir()));
403 connect(actionMessages, SIGNAL(triggered()), m_currentConsole, SLOT(messages()));
404 /* Set director's tree widget background to magenta for ease of identification */
405 QTreeWidgetItem *dirItem = m_currentConsole->directorTreeItem();
406 QBrush magentaBrush(Qt::magenta);
407 dirItem->setBackground(0, magentaBrush);
409 /* set the value for the currently active console */
410 int stackindex = stackedWidget->indexOf(nextPage);
412 /* Is this page currently on the stack or is it undocked */
413 if (stackindex >= 0) {
414 /* put this page on the top of the stack */
415 stackedWidget->setCurrentIndex(stackindex);
417 /* it is undocked, raise it to the front */
420 /* for the page selectors menu action to dock or undock, set the text */
421 nextPage->setContextMenuDockText();
423 treeWidget->addAction(actionToggleDock);
424 /* if this page is closeable, then add that action */
425 if (nextPage->isCloseable()) {
426 treeWidget->addAction(actionClosePage);
429 /* Add the actions to the Page Selectors tree widget that are part of the
430 * current items list of desired actions regardless of whether on top of stack*/
431 treeWidget->addActions(nextPage->m_contextActions);
435 void MainWin::labelButtonClicked()
440 void MainWin::runButtonClicked()
445 void MainWin::restoreButtonClicked()
447 new prerestorePage();
451 * The user just finished typing a line in the command line edit box
453 void MainWin::input_line()
455 QString cmdStr = lineEdit->text(); /* Get the text */
456 lineEdit->clear(); /* clear the lineEdit box */
457 if (m_currentConsole->is_connected()) {
458 m_currentConsole->display_text(cmdStr + "\n");
459 m_currentConsole->write_dir(cmdStr.toUtf8().data()); /* send to dir */
461 set_status("Director not connected. Click on connect button.");
463 m_cmd_history.append(cmdStr);
465 if (treeWidget->currentItem() != getFromHash(m_currentConsole))
466 m_currentConsole->setCurrent();
470 void MainWin::about()
472 QMessageBox::about(this, tr("About bat"),
473 tr("<br><h2>bat 1.0, by Dirk H Bartley and Kern Sibbald</h2>"
474 "<p>Copyright © " BYEAR " Free Software Foundation Europe e.V."
475 "<p>The <b>bat</b> is an administrative console"
476 " interface to the Director."));
479 void MainWin::set_statusf(const char *fmt, ...)
484 va_start(arg_ptr, fmt);
485 len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
490 void MainWin::set_status_ready()
492 set_status(" Ready");
495 void MainWin::set_status(const char *buf)
497 statusBar()->showMessage(buf);
501 * Function to respond to the button bar button to undock
503 void MainWin::undockWindowButton()
505 Pages* page = (Pages*)stackedWidget->currentWidget();
506 page->togglePageDocking();
510 * Function to respond to action on page selector context menu to toggle the
511 * dock status of the window associated with the page selectors current
514 void MainWin::toggleDockContextWindow()
516 QTreeWidgetItem *currentitem = treeWidget->currentItem();
518 /* Is this a page that has been inserted into the hash */
519 if (getFromHash(currentitem)) {
520 Pages* page = getFromHash(currentitem);
521 page->togglePageDocking();
526 * This function is called when the stack item is changed. Call
527 * the virtual function here. Avoids a window being undocked leaving
528 * a window at the top of the stack unpopulated.
530 void MainWin::stackItemChanged(int)
532 Pages* page = (Pages*)stackedWidget->currentWidget();
533 /* run the virtual function in case this class overrides it */
534 page->currentStackItem();
538 * Function to simplify insertion of QTreeWidgetItem <-> Page association
539 * into a double direction hash.
541 void MainWin::hashInsert(QTreeWidgetItem *item, Pages *page)
543 m_pagehash.insert(item, page);
544 m_widgethash.insert(page, item);
548 * Function to simplify removal of QTreeWidgetItem <-> Page association
549 * into a double direction hash.
551 void MainWin::hashRemove(QTreeWidgetItem *item, Pages *page)
553 /* I had all sorts of return status checking code here. Do we have a log
554 * level capability in bat. I would have left it in but it used printf's
555 * and it should really be some kind of log level facility ???
556 * ******FIXME********/
557 m_pagehash.remove(item);
558 m_widgethash.remove(page);
562 * Function to retrieve a Page* when the item in the page selector's tree is
565 Pages* MainWin::getFromHash(QTreeWidgetItem *item)
567 return m_pagehash.value(item);
571 * Function to retrieve the page selectors tree widget item when the page is
574 QTreeWidgetItem* MainWin::getFromHash(Pages *page)
576 return m_widgethash.value(page);
580 * Function to respond to action on page selector context menu to close the
583 void MainWin::closePage()
585 QTreeWidgetItem *currentitem = treeWidget->currentItem();
587 /* Is this a page that has been inserted into the hash */
588 if (getFromHash(currentitem)) {
589 Pages* page = getFromHash(currentitem);
590 if (page->isCloseable()) {
591 page->closeStackPage();
596 /* Quick function to return the current console */
597 Console *MainWin::currentConsole()
599 return m_currentConsole;
601 /* Quick function to return the tree item for the director */
602 QTreeWidgetItem *MainWin::currentTopItem()
604 return m_currentConsole->directorTreeItem();
607 /* Preferences menu item clicked */
608 void MainWin::setPreferences()
611 prefs.commDebug->setCheckState(g_commDebug ? Qt::Checked : Qt::Unchecked);
612 prefs.displayAll->setCheckState(g_displayAll ? Qt::Checked : Qt::Unchecked);
616 /* Preferences dialog */
617 prefsDialog::prefsDialog()
622 void prefsDialog::accept()
625 g_commDebug = this->commDebug->checkState() == Qt::Checked;
626 g_displayAll = this->displayAll->checkState() == Qt::Checked;
629 void prefsDialog::reject()
632 mainWin->set_status("Canceled");