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->setCurrentIndex(0);
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 one of the first level pages */
249 if( getFromHash(item) ){
250 Pages* page = getFromHash(item);
251 int stackindex=stackedWidget->indexOf(page);
253 if( stackindex >= 0 ){
254 stackedWidget->setCurrentIndex(0);
255 stackedWidget->setCurrentWidget(page);
257 /* run the virtual function in case this class overrides it */
258 page->PgSeltreeWidgetClicked();
263 * This subroutine is called with an item in the Page Selection window
266 void MainWin::treeItemDoubleClicked(QTreeWidgetItem * /*item*/, int /*column*/)
271 * Called with a change of the highlighed tree widget item in the page selector.
273 void MainWin::treeItemChanged(QTreeWidgetItem *currentitem, QTreeWidgetItem *previousitem)
275 /* The Previous item */
278 /* Is this one of the first level pages */
279 if( getFromHash(previousitem) ){
280 Pages* page = getFromHash(previousitem);
281 treeWidget->removeAction(actionToggleDock);
282 foreach( QAction* pageaction, page->m_contextActions ){
283 treeWidget->removeAction(pageaction);
288 /* Is this one of the first level pages */
289 if( getFromHash(currentitem) ){
290 Pages* page = getFromHash(currentitem);
291 int stackindex = stackedWidget->indexOf(page);
293 /* Is this page currently on the stack */
294 if( stackindex >= 0 ){
295 /* put this page on the top of the stack */
296 stackedWidget->setCurrentIndex(stackindex);
298 setContextMenuDockText(page, currentitem);
300 treeWidget->addAction(actionToggleDock);
302 /* Add the actions to the Page Selectors tree widget that are part of the
303 * current items list of desired actions regardless of whether on top of stack*/
304 treeWidget->addActions(page->m_contextActions);
308 void MainWin::labelDialogClicked()
310 new labelDialog(m_console);
313 void MainWin::runDialogClicked()
315 new runDialog(m_console);
318 void MainWin::restoreDialogClicked()
320 new prerestoreDialog(m_console);
326 * The user just finished typing a line in the command line edit box
328 void MainWin::input_line()
330 QString cmdStr = lineEdit->text(); /* Get the text */
331 lineEdit->clear(); /* clear the lineEdit box */
332 if (m_console->is_connected()) {
333 m_console->display_text(cmdStr + "\n");
334 m_console->write_dir(cmdStr.toUtf8().data()); /* send to dir */
336 set_status("Director not connected. Click on connect button.");
338 m_cmd_history.append(cmdStr);
343 void MainWin::about()
345 QMessageBox::about(this, tr("About bat"),
346 tr("<br><h2>bat 0.2, by Kern Sibbald</h2>"
347 "<p>Copyright © " BYEAR " Free Software Foundation Europe e.V."
348 "<p>The <b>bat</b> is an administrative console"
349 " interface to the Director."));
352 void MainWin::set_statusf(const char *fmt, ...)
357 va_start(arg_ptr, fmt);
358 len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
363 void MainWin::set_status_ready()
365 set_status(" Ready");
368 void MainWin::set_status(const char *buf)
370 statusBar()->showMessage(buf);
374 * Function to respond to the button bar button to undock
376 void MainWin::undockWindowButton()
378 Pages* page = (Pages*)stackedWidget->currentWidget();
379 page->togglePageDocking();
380 /* The window has been undocked, lets change the context menu */
381 setContextMenuDockText();
385 * Function to respond to action on page selector context menu to toggle the
386 * dock status of the window associated with the page selectors current
389 void MainWin::toggleDockContextWindow()
391 QTreeWidgetItem *currentitem = treeWidget->currentItem();
393 /* Is this one of the first level pages */
394 if( getFromHash(currentitem) ){
395 Pages* page = getFromHash(currentitem);
396 page->togglePageDocking();
397 if ( page->isDocked() ){
398 stackedWidget->setCurrentWidget(page);
400 /* Toggle the menu item. The window's dock status has been toggled */
401 setContextMenuDockText(page, currentitem);
406 * Function to set the text of the toggle dock context menu when page and
407 * widget item are NOT known. This is an overoaded funciton.
408 * It is called from MainWin::undockWindowButton, it is not intended to change
409 * for the top pages tree widget, it is for the currently active tree widget
410 * item. Which is why the page is not passed.
412 void MainWin::setContextMenuDockText()
414 QTreeWidgetItem *currentitem = treeWidget->currentItem();
416 /* Is this one of the first level pages */
417 if( getFromHash(currentitem) ){
418 Pages* page = getFromHash(currentitem);
419 setContextMenuDockText(page, currentitem);
424 * Function to set the text of the toggle dock context menu when page and
425 * widget item are known. This is the more commonly used.
427 void MainWin::setContextMenuDockText(Pages* page, QTreeWidgetItem* item)
429 QString docktext("");
430 if( page->isDocked() ){
431 docktext += "UnDock ";
433 docktext += "ReDock ";
435 docktext += item->text(0) += " Window";
437 actionToggleDock->setText(docktext);
438 setTreeWidgetItemDockColor(page, item);
442 * Function to set the color of the tree widget item based on whether it is
445 void MainWin::setTreeWidgetItemDockColor(Pages* page, QTreeWidgetItem* item)
447 if( item->text(0) != "Console" ){
448 if( page->isDocked() ){
449 /* Set the brush to blue if undocked */
450 QBrush blackBrush(Qt::black);
451 item->setForeground(0, blackBrush);
453 /* Set the brush back to black if docked */
454 QBrush blueBrush(Qt::blue);
455 item->setForeground(0, blueBrush);
461 * Overload of previous function, use treeindex to get item from page
462 * This is called when an undocked window is closed.
464 void MainWin::setTreeWidgetItemDockColor(Pages* page)
466 QTreeWidgetItem* item = getFromHash(page);
468 setTreeWidgetItemDockColor(page, item);
473 * This function is called when the stack item is changed. Call
474 * the virtual function here. Avoids a window being undocked leaving
475 * a window at the top of the stack unpopulated.
477 void MainWin::stackItemChanged(int)
479 Pages* page = (Pages*)stackedWidget->currentWidget();
480 /* run the virtual function in case this class overrides it */
481 page->currentStackItem();
485 * Function to simplify insertion of QTreeWidgetItem <-> Page association
486 * into a double direction hash.
488 void MainWin::hashInsert(QTreeWidgetItem *item, Pages *page)
490 m_pagehash.insert(item, page);
491 m_widgethash.insert(page, item);
495 * Function to retrieve a Page* when the item in the page selector's tree is
498 Pages* MainWin::getFromHash(QTreeWidgetItem *item)
500 return m_pagehash.value(item);
504 * Function to retrieve the page selectors tree widget item when the page is
507 QTreeWidgetItem* MainWin::getFromHash(Pages *page)
509 return m_widgethash.value(page);