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
40 MainWin::MainWin(QWidget *parent) : QMainWindow(parent)
44 setupUi(this); /* Setup UI defined by main.ui (designer) */
46 treeWidget->setColumnCount(1);
47 treeWidget->setHeaderLabel("Select Page");
48 treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
63 void MainWin::createPages()
66 QTreeWidgetItem *item, *topItem;
68 /* Create console tree stacked widget item */
69 m_console = new Console(stackedWidget);
71 /* Console is special -> needs director*/
72 /* Just take the first Director */
74 dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
75 m_console->setDirRes(dir);
78 /* The top tree item representing the director */
79 topItem = createTopPage(dir->name());
80 topItem->setIcon(0, QIcon(QString::fromUtf8("images/server.png")));
82 /* Create Tree Widget Item */
83 item = createPage("Console", topItem);
84 m_console->setTreeItem(item);
86 /* Append to pagelist */
87 hashInsert(item, m_console);
89 /* Set Color of treeWidgetItem for the console
90 * It will be set to gree in the console class if the connection is made.
92 QBrush redBrush(Qt::red);
93 item->setForeground(0, redBrush);
96 * Now with the console created, on with the rest, these are easy
98 * 1. create tree widget item
99 * 2. create object passing pointer to tree widget item (modified constructors to pass QTreeWidget pointers)
100 * 3. append to stackhash
104 item=createPage("brestore", topItem);
105 bRestore* brestore=new bRestore(stackedWidget);
106 hashInsert(item, brestore);
109 /* lastly for now, the medialist */
110 item=createPage("Media", topItem );
111 MediaList* medialist=new MediaList(stackedWidget, m_console);
112 hashInsert(item, medialist);
114 /* Iterate through and add to the stack */
115 foreach(Pages *page, m_pagehash)
118 treeWidget->expandItem(topItem);
119 stackedWidget->setCurrentWidget(m_console);
122 /* Create a root Tree Widget */
123 QTreeWidgetItem *MainWin::createTopPage(char *name)
125 QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
126 item->setText(0, name);
130 /* Create A Tree Widget Item which will be associated with a Page in the stacked widget */
131 QTreeWidgetItem *MainWin::createPage(char *name, QTreeWidgetItem *parent)
133 QTreeWidgetItem *item = new QTreeWidgetItem(parent);
134 item->setText(0, name);
139 * Handle up and down arrow keys for the command line
142 void MainWin::keyPressEvent(QKeyEvent *event)
144 if (m_cmd_history.size() == 0) {
148 switch (event->key()) {
150 if (m_cmd_last < 0 || m_cmd_last >= (m_cmd_history.size()-1)) {
157 if (m_cmd_last == 0) {
161 if (m_cmd_last < 0 || m_cmd_last > (m_cmd_history.size()-1)) {
162 m_cmd_last = m_cmd_history.size() - 1;
171 lineEdit->setText(m_cmd_history[m_cmd_last]);
174 void MainWin::createConnections()
176 /* Connect signals to slots */
177 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(input_line()));
178 connect(actionAbout_bat, SIGNAL(triggered()), this, SLOT(about()));
181 connect(treeWidget, SIGNAL(itemActivated(QTreeWidgetItem *, int)), this,
182 SLOT(treeItemClicked(QTreeWidgetItem *, int)));
183 connect(treeWidget, SIGNAL(itemPressed(QTreeWidgetItem *, int)), this,
184 SLOT(treeItemClicked(QTreeWidgetItem *, int)));
186 connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this,
187 SLOT(treeItemClicked(QTreeWidgetItem *, int)));
188 connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this,
189 SLOT(treeItemDoubleClicked(QTreeWidgetItem *, int)));
190 connect(treeWidget, SIGNAL(
191 currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
192 this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
193 connect(stackedWidget, SIGNAL(currentChanged(int)),
194 this, SLOT(stackItemChanged(int)));
196 connect(actionQuit, SIGNAL(triggered()), app, SLOT(closeAllWindows()));
197 connect(actionConnect, SIGNAL(triggered()), m_console, SLOT(connect()));
198 connect(actionStatusDir, SIGNAL(triggered()), m_console, SLOT(status_dir()));
199 connect(actionSelectFont, SIGNAL(triggered()), m_console, SLOT(set_font()));
200 connect(actionLabel, SIGNAL(triggered()), this, SLOT(labelDialogClicked()));
201 connect(actionRun, SIGNAL(triggered()), this, SLOT(runDialogClicked()));
202 connect(actionRestore, SIGNAL(triggered()), this, SLOT(restoreDialogClicked()));
203 connect(actionUndock, SIGNAL(triggered()), this, SLOT(undockWindowButton()));
204 connect(actionToggleDock, SIGNAL(triggered()), this, SLOT(toggleDockContextWindow()));
208 * Reimplementation of QWidget closeEvent virtual function
210 void MainWin::closeEvent(QCloseEvent *event)
213 m_console->writeSettings();
214 m_console->terminate();
216 foreach(Pages *page, m_pagehash){
217 if( !page->isDocked() )
222 void MainWin::writeSettings()
224 QSettings settings("bacula.org", "bat");
226 settings.beginGroup("MainWin");
227 settings.setValue("winSize", size());
228 settings.setValue("winPos", pos());
232 void MainWin::readSettings()
234 QSettings settings("bacula.org", "bat");
236 settings.beginGroup("MainWin");
237 resize(settings.value("winSize", QSize(1041, 801)).toSize());
238 move(settings.value("winPos", QPoint(200, 150)).toPoint());
243 * This subroutine is called with an item in the Page Selection window
246 void MainWin::treeItemClicked(QTreeWidgetItem *item, int /*column*/)
248 /* Is this a page that has been inserted into the hash */
249 if( getFromHash(item) ){
250 Pages* page = getFromHash(item);
251 int stackindex=stackedWidget->indexOf(page);
253 if( stackindex >= 0 ){
254 stackedWidget->setCurrentWidget(page);
256 /* run the virtual function in case this class overrides it */
257 page->PgSeltreeWidgetClicked();
262 * This subroutine is called with an item in the Page Selection window
265 void MainWin::treeItemDoubleClicked(QTreeWidgetItem * /*item*/, int /*column*/)
270 * Called with a change of the highlighed tree widget item in the page selector.
272 void MainWin::treeItemChanged(QTreeWidgetItem *currentitem, QTreeWidgetItem *previousitem)
274 /* The Previous item */
277 /* Is this a page that has been inserted into the hash */
278 if( getFromHash(previousitem) ){
279 Pages* page = getFromHash(previousitem);
280 treeWidget->removeAction(actionToggleDock);
281 foreach( QAction* pageaction, page->m_contextActions ){
282 treeWidget->removeAction(pageaction);
287 /* Is this a page that has been inserted into the hash */
288 if( getFromHash(currentitem) ){
289 Pages* page = getFromHash(currentitem);
290 int stackindex = stackedWidget->indexOf(page);
292 /* Is this page currently on the stack */
293 if( stackindex >= 0 ){
294 /* put this page on the top of the stack */
295 stackedWidget->setCurrentIndex(stackindex);
297 /* it is undocked, raise it to the front */
300 setContextMenuDockText(page, currentitem);
302 treeWidget->addAction(actionToggleDock);
304 /* Add the actions to the Page Selectors tree widget that are part of the
305 * current items list of desired actions regardless of whether on top of stack*/
306 treeWidget->addActions(page->m_contextActions);
310 void MainWin::labelDialogClicked()
312 new labelDialog(m_console);
315 void MainWin::runDialogClicked()
317 new runDialog(m_console);
320 void MainWin::restoreDialogClicked()
322 new prerestoreDialog(m_console);
328 * The user just finished typing a line in the command line edit box
330 void MainWin::input_line()
332 QString cmdStr = lineEdit->text(); /* Get the text */
333 lineEdit->clear(); /* clear the lineEdit box */
334 if (m_console->is_connected()) {
335 m_console->display_text(cmdStr + "\n");
336 m_console->write_dir(cmdStr.toUtf8().data()); /* send to dir */
338 set_status("Director not connected. Click on connect button.");
340 m_cmd_history.append(cmdStr);
345 void MainWin::about()
347 QMessageBox::about(this, tr("About bat"),
348 tr("<br><h2>bat 0.2, by Kern Sibbald</h2>"
349 "<p>Copyright © " BYEAR " Free Software Foundation Europe e.V."
350 "<p>The <b>bat</b> is an administrative console"
351 " interface to the Director."));
354 void MainWin::set_statusf(const char *fmt, ...)
359 va_start(arg_ptr, fmt);
360 len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
365 void MainWin::set_status_ready()
367 set_status(" Ready");
370 void MainWin::set_status(const char *buf)
372 statusBar()->showMessage(buf);
376 * Function to respond to the button bar button to undock
378 void MainWin::undockWindowButton()
380 Pages* page = (Pages*)stackedWidget->currentWidget();
381 page->togglePageDocking();
382 /* The window has been undocked, lets change the context menu */
383 setContextMenuDockText();
387 * Function to respond to action on page selector context menu to toggle the
388 * dock status of the window associated with the page selectors current
391 void MainWin::toggleDockContextWindow()
393 QTreeWidgetItem *currentitem = treeWidget->currentItem();
395 /* Is this a page that has been inserted into the hash */
396 if( getFromHash(currentitem) ){
397 Pages* page = getFromHash(currentitem);
398 page->togglePageDocking();
399 if ( page->isDocked() ){
400 stackedWidget->setCurrentWidget(page);
402 /* Toggle the menu item. The window's dock status has been toggled */
403 setContextMenuDockText(page, currentitem);
408 * Function to set the text of the toggle dock context menu when page and
409 * widget item are NOT known. This is an overoaded funciton.
410 * It is called from MainWin::undockWindowButton, it is not intended to change
411 * for the top pages tree widget, it is for the currently active tree widget
412 * item. Which is why the page is not passed.
414 void MainWin::setContextMenuDockText()
416 QTreeWidgetItem *currentitem = treeWidget->currentItem();
418 /* Is this a page that has been inserted into the hash */
419 if( getFromHash(currentitem) ){
420 Pages* page = getFromHash(currentitem);
421 setContextMenuDockText(page, currentitem);
426 * Function to set the text of the toggle dock context menu when page and
427 * widget item are known. This is the more commonly used.
429 void MainWin::setContextMenuDockText(Pages* page, QTreeWidgetItem* item)
431 QString docktext("");
432 if( page->isDocked() ){
433 docktext += "UnDock ";
435 docktext += "ReDock ";
437 docktext += item->text(0) += " Window";
439 actionToggleDock->setText(docktext);
440 setTreeWidgetItemDockColor(page, item);
444 * Function to set the color of the tree widget item based on whether it is
447 void MainWin::setTreeWidgetItemDockColor(Pages* page, QTreeWidgetItem* item)
449 if( item->text(0) != "Console" ){
450 if( page->isDocked() ){
451 /* Set the brush to blue if undocked */
452 QBrush blackBrush(Qt::black);
453 item->setForeground(0, blackBrush);
455 /* Set the brush back to black if docked */
456 QBrush blueBrush(Qt::blue);
457 item->setForeground(0, blueBrush);
463 * Overload of previous function, use treeindex to get item from page
464 * This is called when an undocked window is closed.
466 void MainWin::setTreeWidgetItemDockColor(Pages* page)
468 QTreeWidgetItem* item = getFromHash(page);
470 setTreeWidgetItemDockColor(page, item);
475 * This function is called when the stack item is changed. Call
476 * the virtual function here. Avoids a window being undocked leaving
477 * a window at the top of the stack unpopulated.
479 void MainWin::stackItemChanged(int)
481 Pages* page = (Pages*)stackedWidget->currentWidget();
482 /* run the virtual function in case this class overrides it */
483 page->currentStackItem();
487 * Function to simplify insertion of QTreeWidgetItem <-> Page association
488 * into a double direction hash.
490 void MainWin::hashInsert(QTreeWidgetItem *item, Pages *page)
492 m_pagehash.insert(item, page);
493 m_widgethash.insert(page, item);
497 * Function to retrieve a Page* when the item in the page selector's tree is
500 Pages* MainWin::getFromHash(QTreeWidgetItem *item)
502 return m_pagehash.value(item);
506 * Function to retrieve the page selectors tree widget item when the page is
509 QTreeWidgetItem* MainWin::getFromHash(Pages *page)
511 return m_widgethash.value(page);