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 m_pagehash.insert(item, m_console);
88 m_widgethash.insert(m_console, item);
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_pagehash.insert(item, brestore);
108 m_widgethash.insert(brestore, item);
111 /* lastly for now, the medialist */
112 item=createPage("Media", topItem );
113 MediaList* medialist=new MediaList(stackedWidget, m_console);
114 m_pagehash.insert(item, medialist);
115 m_widgethash.insert(medialist, item);
117 /* Iterate through and add to the stack */
118 foreach(Pages *page, m_pagehash)
121 treeWidget->expandItem(topItem);
122 stackedWidget->setCurrentIndex(0);
125 /* Create a root Tree Widget */
126 QTreeWidgetItem *MainWin::createTopPage(char *name)
128 QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
129 item->setText(0, name);
133 /* Create A Tree Widget Item which will be associated with a Page in the stacked widget */
134 QTreeWidgetItem *MainWin::createPage(char *name, QTreeWidgetItem *parent)
136 QTreeWidgetItem *item = new QTreeWidgetItem(parent);
137 item->setText(0, name);
142 * Handle up and down arrow keys for the command line
145 void MainWin::keyPressEvent(QKeyEvent *event)
147 if (m_cmd_history.size() == 0) {
151 switch (event->key()) {
153 if (m_cmd_last < 0 || m_cmd_last >= (m_cmd_history.size()-1)) {
160 if (m_cmd_last == 0) {
164 if (m_cmd_last < 0 || m_cmd_last > (m_cmd_history.size()-1)) {
165 m_cmd_last = m_cmd_history.size() - 1;
174 lineEdit->setText(m_cmd_history[m_cmd_last]);
177 void MainWin::createConnections()
179 /* Connect signals to slots */
180 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(input_line()));
181 connect(actionAbout_bat, SIGNAL(triggered()), this, SLOT(about()));
184 connect(treeWidget, SIGNAL(itemActivated(QTreeWidgetItem *, int)), this,
185 SLOT(treeItemClicked(QTreeWidgetItem *, int)));
186 connect(treeWidget, SIGNAL(itemPressed(QTreeWidgetItem *, int)), this,
187 SLOT(treeItemClicked(QTreeWidgetItem *, int)));
189 connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this,
190 SLOT(treeItemClicked(QTreeWidgetItem *, int)));
191 connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this,
192 SLOT(treeItemDoubleClicked(QTreeWidgetItem *, int)));
193 connect(treeWidget, SIGNAL(
194 currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
195 this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
196 connect(stackedWidget, SIGNAL(currentChanged(int)),
197 this, SLOT(stackItemChanged(int)));
199 connect(actionQuit, SIGNAL(triggered()), app, SLOT(closeAllWindows()));
200 connect(actionConnect, SIGNAL(triggered()), m_console, SLOT(connect()));
201 connect(actionStatusDir, SIGNAL(triggered()), m_console, SLOT(status_dir()));
202 connect(actionSelectFont, SIGNAL(triggered()), m_console, SLOT(set_font()));
203 connect(actionLabel, SIGNAL(triggered()), this, SLOT(labelDialogClicked()));
204 connect(actionRun, SIGNAL(triggered()), this, SLOT(runDialogClicked()));
205 connect(actionRestore, SIGNAL(triggered()), this, SLOT(restoreDialogClicked()));
206 connect(actionUndock, SIGNAL(triggered()), this, SLOT(undockWindowButton()));
207 connect(actionToggleDock, SIGNAL(triggered()), this, SLOT(toggleDockContextWindow()));
211 * Reimplementation of QWidget closeEvent virtual function
213 void MainWin::closeEvent(QCloseEvent *event)
216 m_console->writeSettings();
217 m_console->terminate();
219 foreach(Pages *page, m_pagehash){
220 if( !page->isDocked() )
225 void MainWin::writeSettings()
227 QSettings settings("bacula.org", "bat");
229 settings.beginGroup("MainWin");
230 settings.setValue("winSize", size());
231 settings.setValue("winPos", pos());
235 void MainWin::readSettings()
237 QSettings settings("bacula.org", "bat");
239 settings.beginGroup("MainWin");
240 resize(settings.value("winSize", QSize(1041, 801)).toSize());
241 move(settings.value("winPos", QPoint(200, 150)).toPoint());
246 * This subroutine is called with an item in the Page Selection window
249 void MainWin::treeItemClicked(QTreeWidgetItem *item, int /*column*/)
251 /* Is this one of the first level pages */
252 if( m_pagehash.value(item) ){
253 Pages* page = m_pagehash.value(item);
254 int stackindex=stackedWidget->indexOf(page);
256 if( stackindex >= 0 ){
257 stackedWidget->setCurrentIndex(0);
258 stackedWidget->setCurrentWidget(page);
260 /* run the virtual function in case this class overrides it */
261 page->PgSeltreeWidgetClicked();
266 * This subroutine is called with an item in the Page Selection window
269 void MainWin::treeItemDoubleClicked(QTreeWidgetItem * /*item*/, int /*column*/)
274 * Called with a change of the highlighed tree widget item in the page selector.
276 void MainWin::treeItemChanged(QTreeWidgetItem *currentitem, QTreeWidgetItem *previousitem)
278 /* The Previous item */
281 /* Is this one of the first level pages */
282 if( m_pagehash.value(previousitem) ){
283 Pages* page = m_pagehash.value(previousitem);
284 treeWidget->removeAction(actionToggleDock);
285 foreach( QAction* pageaction, page->m_contextActions ){
286 treeWidget->removeAction(pageaction);
291 /* Is this one of the first level pages */
292 if( m_pagehash.value(currentitem) ){
293 Pages* page = m_pagehash.value(currentitem);
294 int stackindex = stackedWidget->indexOf(page);
296 /* Is this page currently on the stack */
297 if( stackindex >= 0 ){
298 /* put this page on the top of the stack */
299 stackedWidget->setCurrentIndex(stackindex);
301 setContextMenuDockText(page, currentitem);
303 treeWidget->addAction(actionToggleDock);
305 /* Add the actions to the Page Selectors tree widget that are part of the
306 * current items list of desired actions regardless of whether on top of stack*/
307 treeWidget->addActions(page->m_contextActions);
311 void MainWin::labelDialogClicked()
313 new labelDialog(m_console);
316 void MainWin::runDialogClicked()
318 new runDialog(m_console);
321 void MainWin::restoreDialogClicked()
323 new prerestoreDialog(m_console);
329 * The user just finished typing a line in the command line edit box
331 void MainWin::input_line()
333 QString cmdStr = lineEdit->text(); /* Get the text */
334 lineEdit->clear(); /* clear the lineEdit box */
335 if (m_console->is_connected()) {
336 m_console->display_text(cmdStr + "\n");
337 m_console->write_dir(cmdStr.toUtf8().data()); /* send to dir */
339 set_status("Director not connected. Click on connect button.");
341 m_cmd_history.append(cmdStr);
346 void MainWin::about()
348 QMessageBox::about(this, tr("About bat"),
349 tr("<br><h2>bat 0.2, by Kern Sibbald</h2>"
350 "<p>Copyright © " BYEAR " Free Software Foundation Europe e.V."
351 "<p>The <b>bat</b> is an administrative console"
352 " interface to the Director."));
355 void MainWin::set_statusf(const char *fmt, ...)
360 va_start(arg_ptr, fmt);
361 len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
366 void MainWin::set_status_ready()
368 set_status(" Ready");
371 void MainWin::set_status(const char *buf)
373 statusBar()->showMessage(buf);
377 * Function to respond to the button bar button to undock
379 void MainWin::undockWindowButton()
381 Pages* page = (Pages*)stackedWidget->currentWidget();
382 page->togglePageDocking();
383 /* The window has been undocked, lets change the context menu */
384 setContextMenuDockText();
388 * Function to respond to action on page selector context menu to toggle the
389 * dock status of the window associated with the page selectors current
392 void MainWin::toggleDockContextWindow()
394 QTreeWidgetItem *currentitem = treeWidget->currentItem();
396 /* Is this one of the first level pages */
397 if( m_pagehash.value(currentitem) ){
398 Pages* page = m_pagehash.value(currentitem);
399 page->togglePageDocking();
400 if ( page->isDocked() ){
401 stackedWidget->setCurrentWidget(page);
403 /* Toggle the menu item. The window's dock status has been toggled */
404 setContextMenuDockText(page, currentitem);
409 * Function to set the text of the toggle dock context menu when page and
410 * widget item are NOT known. This is an overoaded funciton.
411 * It is called from MainWin::undockWindowButton, it is not intended to change
412 * for the top pages tree widget, it is for the currently active tree widget
413 * item. Which is why the page is not passed.
415 void MainWin::setContextMenuDockText()
417 QTreeWidgetItem *currentitem = treeWidget->currentItem();
419 /* Is this one of the first level pages */
420 if( m_pagehash.value(currentitem) ){
421 Pages* page = m_pagehash.value(currentitem);
422 setContextMenuDockText(page, currentitem);
427 * Function to set the text of the toggle dock context menu when page and
428 * widget item are known. This is the more commonly used.
430 void MainWin::setContextMenuDockText( Pages* page, QTreeWidgetItem* item )
432 QString docktext("");
433 if( page->isDocked() ){
434 docktext += "UnDock ";
436 docktext += "ReDock ";
438 docktext += item->text(0) += " Window";
440 actionToggleDock->setText(docktext);
441 setTreeWidgetItemDockColor(page, item);
445 * Function to set the color of the tree widget item based on whether it is
448 void MainWin::setTreeWidgetItemDockColor( Pages* page, QTreeWidgetItem* item )
450 if( item->text(0) != "Console" ){
451 if( page->isDocked() ){
452 /* Set the brush to blue if undocked */
453 QBrush blackBrush(Qt::black);
454 item->setForeground(0, blackBrush);
456 /* Set the brush back to black if docked */
457 QBrush blueBrush(Qt::blue);
458 item->setForeground(0, blueBrush);
463 void MainWin::stackItemChanged(int)
465 Pages* page = (Pages*)stackedWidget->currentWidget();
466 /* run the virtual function in case this class overrides it */
467 page->currentStackItem();