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
41 MainWin::MainWin(QWidget *parent) : QMainWindow(parent)
45 setupUi(this); /* Setup UI defined by main.ui (designer) */
47 treeWidget->setColumnCount(1);
48 treeWidget->setHeaderLabel("Select Page");
49 treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
64 void MainWin::createPages()
67 QTreeWidgetItem *item, *topItem;
69 /* Create console tree stacked widget item */
70 m_console = new Console(stackedWidget);
72 /* Console is special -> needs director*/
73 /* Just take the first Director */
75 dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
76 m_console->setDirRes(dir);
79 /* The top tree item representing the director */
80 topItem = createTopPage(dir->name());
81 topItem->setIcon(0, QIcon(QString::fromUtf8("images/server.png")));
83 /* Create Tree Widget Item */
84 item = createPage("Console", topItem);
85 m_console->setTreeItem(item);
87 /* Append to pagelist */
88 m_treeindex.insert(item, m_console);
90 /* Set Color of treeWidgetItem for the console
91 * It will be set to gree in the console class if the connection is made.
93 QBrush redBrush(Qt::red);
94 item->setForeground(0, redBrush);
97 * Now with the console created, on with the rest, these are easy
99 * 1. create tree widget item
100 * 2. create object passing pointer to tree widget item (modified constructors to pass QTreeWidget pointers)
101 * 3. append to stackhash
105 item=createPage("brestore", topItem);
106 bRestore* brestore=new bRestore(stackedWidget);
107 m_treeindex.insert(item, brestore);
110 /* lastly for now, the medialist */
111 item=createPage("Media", topItem );
112 MediaList* medialist=new MediaList(stackedWidget, m_console);
113 m_treeindex.insert(item, medialist);
115 /* Iterate through and add to the stack */
116 foreach(Pages *page, m_treeindex.m_pagehash)
119 treeWidget->expandItem(topItem);
120 stackedWidget->setCurrentIndex(0);
123 /* Create a root Tree Widget */
124 QTreeWidgetItem *MainWin::createTopPage(char *name)
126 QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
127 item->setText(0, name);
131 /* Create A Tree Widget Item which will be associated with a Page in the stacked widget */
132 QTreeWidgetItem *MainWin::createPage(char *name, QTreeWidgetItem *parent)
134 QTreeWidgetItem *item = new QTreeWidgetItem(parent);
135 item->setText(0, name);
140 * Handle up and down arrow keys for the command line
143 void MainWin::keyPressEvent(QKeyEvent *event)
145 if (m_cmd_history.size() == 0) {
149 switch (event->key()) {
151 if (m_cmd_last < 0 || m_cmd_last >= (m_cmd_history.size()-1)) {
158 if (m_cmd_last == 0) {
162 if (m_cmd_last < 0 || m_cmd_last > (m_cmd_history.size()-1)) {
163 m_cmd_last = m_cmd_history.size() - 1;
172 lineEdit->setText(m_cmd_history[m_cmd_last]);
175 void MainWin::createConnections()
177 /* Connect signals to slots */
178 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(input_line()));
179 connect(actionAbout_bat, SIGNAL(triggered()), this, SLOT(about()));
182 connect(treeWidget, SIGNAL(itemActivated(QTreeWidgetItem *, int)), this,
183 SLOT(treeItemClicked(QTreeWidgetItem *, int)));
184 connect(treeWidget, SIGNAL(itemPressed(QTreeWidgetItem *, int)), this,
185 SLOT(treeItemClicked(QTreeWidgetItem *, int)));
187 connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this,
188 SLOT(treeItemClicked(QTreeWidgetItem *, int)));
189 connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this,
190 SLOT(treeItemDoubleClicked(QTreeWidgetItem *, int)));
191 connect(treeWidget, SIGNAL(
192 currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
193 this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
194 connect(stackedWidget, SIGNAL(currentChanged(int)),
195 this, SLOT(stackItemChanged(int)));
197 connect(actionQuit, SIGNAL(triggered()), app, SLOT(closeAllWindows()));
198 connect(actionConnect, SIGNAL(triggered()), m_console, SLOT(connect()));
199 connect(actionStatusDir, SIGNAL(triggered()), m_console, SLOT(status_dir()));
200 connect(actionSelectFont, SIGNAL(triggered()), m_console, SLOT(set_font()));
201 connect(actionLabel, SIGNAL(triggered()), this, SLOT(labelDialogClicked()));
202 connect(actionRun, SIGNAL(triggered()), this, SLOT(runDialogClicked()));
203 connect(actionRestore, SIGNAL(triggered()), this, SLOT(restoreDialogClicked()));
204 connect(actionUndock, SIGNAL(triggered()), this, SLOT(undockWindowButton()));
205 connect(actionToggleDock, SIGNAL(triggered()), this, SLOT(toggleDockContextWindow()));
209 * Reimplementation of QWidget closeEvent virtual function
211 void MainWin::closeEvent(QCloseEvent *event)
214 m_console->writeSettings();
215 m_console->terminate();
217 foreach(Pages *page, m_treeindex.m_pagehash){
218 if( !page->isDocked() )
223 void MainWin::writeSettings()
225 QSettings settings("bacula.org", "bat");
227 settings.beginGroup("MainWin");
228 settings.setValue("winSize", size());
229 settings.setValue("winPos", pos());
233 void MainWin::readSettings()
235 QSettings settings("bacula.org", "bat");
237 settings.beginGroup("MainWin");
238 resize(settings.value("winSize", QSize(1041, 801)).toSize());
239 move(settings.value("winPos", QPoint(200, 150)).toPoint());
244 * This subroutine is called with an item in the Page Selection window
247 void MainWin::treeItemClicked(QTreeWidgetItem *item, int /*column*/)
249 /* Is this one of the first level pages */
250 if( m_treeindex.value(item) ){
251 Pages* page = m_treeindex.value(item);
252 int stackindex=stackedWidget->indexOf(page);
254 if( stackindex >= 0 ){
255 stackedWidget->setCurrentIndex(0);
256 stackedWidget->setCurrentWidget(page);
258 /* run the virtual function in case this class overrides it */
259 page->PgSeltreeWidgetClicked();
264 * This subroutine is called with an item in the Page Selection window
267 void MainWin::treeItemDoubleClicked(QTreeWidgetItem * /*item*/, int /*column*/)
272 * Called with a change of the highlighed tree widget item in the page selector.
274 void MainWin::treeItemChanged(QTreeWidgetItem *currentitem, QTreeWidgetItem *previousitem)
276 /* The Previous item */
279 /* Is this one of the first level pages */
280 if( m_treeindex.value(previousitem) ){
281 Pages* page = m_treeindex.value(previousitem);
282 treeWidget->removeAction(actionToggleDock);
283 foreach( QAction* pageaction, page->m_contextActions ){
284 treeWidget->removeAction(pageaction);
289 /* Is this one of the first level pages */
290 if( m_treeindex.value(currentitem) ){
291 Pages* page = m_treeindex.value(currentitem);
292 int stackindex = stackedWidget->indexOf(page);
294 /* Is this page currently on the stack */
295 if( stackindex >= 0 ){
296 /* put this page on the top of the stack */
297 stackedWidget->setCurrentIndex(stackindex);
299 setContextMenuDockText(page, currentitem);
301 treeWidget->addAction(actionToggleDock);
303 /* Add the actions to the Page Selectors tree widget that are part of the
304 * current items list of desired actions regardless of whether on top of stack*/
305 treeWidget->addActions(page->m_contextActions);
309 void MainWin::labelDialogClicked()
311 new labelDialog(m_console);
314 void MainWin::runDialogClicked()
316 new runDialog(m_console);
319 void MainWin::restoreDialogClicked()
321 new prerestoreDialog(m_console);
327 * The user just finished typing a line in the command line edit box
329 void MainWin::input_line()
331 QString cmdStr = lineEdit->text(); /* Get the text */
332 lineEdit->clear(); /* clear the lineEdit box */
333 if (m_console->is_connected()) {
334 m_console->display_text(cmdStr + "\n");
335 m_console->write_dir(cmdStr.toUtf8().data()); /* send to dir */
337 set_status("Director not connected. Click on connect button.");
339 m_cmd_history.append(cmdStr);
344 void MainWin::about()
346 QMessageBox::about(this, tr("About bat"),
347 tr("<br><h2>bat 0.2, by Kern Sibbald</h2>"
348 "<p>Copyright © " BYEAR " Free Software Foundation Europe e.V."
349 "<p>The <b>bat</b> is an administrative console"
350 " interface to the Director."));
353 void MainWin::set_statusf(const char *fmt, ...)
358 va_start(arg_ptr, fmt);
359 len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
364 void MainWin::set_status_ready()
366 set_status(" Ready");
369 void MainWin::set_status(const char *buf)
371 statusBar()->showMessage(buf);
375 * Function to respond to the button bar button to undock
377 void MainWin::undockWindowButton()
379 Pages* page = (Pages*)stackedWidget->currentWidget();
380 page->togglePageDocking();
381 /* The window has been undocked, lets change the context menu */
382 setContextMenuDockText();
386 * Function to respond to action on page selector context menu to toggle the
387 * dock status of the window associated with the page selectors current
390 void MainWin::toggleDockContextWindow()
392 QTreeWidgetItem *currentitem = treeWidget->currentItem();
394 /* Is this one of the first level pages */
395 if( m_treeindex.value(currentitem) ){
396 Pages* page = m_treeindex.value(currentitem);
397 page->togglePageDocking();
398 if ( page->isDocked() ){
399 stackedWidget->setCurrentWidget(page);
401 /* Toggle the menu item. The window's dock status has been toggled */
402 setContextMenuDockText(page, currentitem);
407 * Function to set the text of the toggle dock context menu when page and
408 * widget item are NOT known. This is an overoaded funciton.
409 * It is called from MainWin::undockWindowButton, it is not intended to change
410 * for the top pages tree widget, it is for the currently active tree widget
411 * item. Which is why the page is not passed.
413 void MainWin::setContextMenuDockText()
415 QTreeWidgetItem *currentitem = treeWidget->currentItem();
417 /* Is this one of the first level pages */
418 if( m_treeindex.value(currentitem) ){
419 Pages* page = m_treeindex.value(currentitem);
420 setContextMenuDockText(page, currentitem);
425 * Function to set the text of the toggle dock context menu when page and
426 * widget item are known. This is the more commonly used.
428 void MainWin::setContextMenuDockText(Pages* page, QTreeWidgetItem* item)
430 QString docktext("");
431 if( page->isDocked() ){
432 docktext += "UnDock ";
434 docktext += "ReDock ";
436 docktext += item->text(0) += " Window";
438 actionToggleDock->setText(docktext);
439 setTreeWidgetItemDockColor(page, item);
443 * Function to set the color of the tree widget item based on whether it is
446 void MainWin::setTreeWidgetItemDockColor(Pages* page, QTreeWidgetItem* item)
448 if( item->text(0) != "Console" ){
449 if( page->isDocked() ){
450 /* Set the brush to blue if undocked */
451 QBrush blackBrush(Qt::black);
452 item->setForeground(0, blackBrush);
454 /* Set the brush back to black if docked */
455 QBrush blueBrush(Qt::blue);
456 item->setForeground(0, blueBrush);
462 * Overload of previous function, use treeindex to get item from page
463 * This is called when an undocked window is closed.
465 void MainWin::setTreeWidgetItemDockColor(Pages* page)
467 QTreeWidgetItem* item = m_treeindex.value(page);
469 setTreeWidgetItemDockColor(page, item);
474 * This function is called when the stack item is changed. Call
475 * the virtual function here. Avoids a window being undocked leaving
476 * a window at the top of the stack unpopulated.
478 void MainWin::stackItemChanged(int)
480 Pages* page = (Pages*)stackedWidget->currentWidget();
481 /* run the virtual function in case this class overrides it */
482 page->currentStackItem();