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->addAction(actionUndock);
49 treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
65 void MainWin::createPages()
68 QTreeWidgetItem *item, *topItem;
70 /* Create console tree stacked widget item */
71 m_console = new Console(stackedWidget);
73 /* Console is special -> needs director*/
74 /* Just take the first Director */
76 dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
77 m_console->setDirRes(dir);
80 /* The top tree item representing the director */
81 topItem = createTopPage(dir->name());
82 topItem->setData(0, Qt::UserRole, QVariant(m_pages));
84 topItem->setIcon(0, QIcon(QString::fromUtf8("images/server.png")));
86 /* Create Tree Widget Item */
87 item = createPage("Console", topItem);
88 m_console->SetPassedValues(stackedWidget, item, m_pages );
90 /* Append to pagelist */
91 m_pagehash.insert(m_pages, m_console);
93 /* Set Color of treeWidgetItem for the console
94 * It will be set to gree in the console class if the connection is made.
96 QBrush redBrush(Qt::red);
97 item->setForeground(0, redBrush);
100 * Now with the console created, on with the rest, these are easy
102 * 1. create tree widget item
103 * 2. create object passing pointer to tree widget item (modified constructors to pass QTreeWidget pointers)
104 * 3. append to stackhash
109 item=createPage("brestore", topItem);
110 bRestore* brestore=new bRestore(stackedWidget, item, m_pages);
111 m_pagehash.insert(m_pages, brestore);
114 /* lastly for now, the medialist */
116 item=createPage("Media", topItem );
117 MediaList* medialist=new MediaList(stackedWidget, m_console, item, m_pages);
118 m_pagehash.insert(m_pages, medialist);
120 /* Iterate through and add to the stack */
121 foreach (Pages *page, m_pagehash)
124 treeWidget->expandItem(topItem);
125 stackedWidget->setCurrentIndex(0);
128 /* Create a root Tree Widget */
129 QTreeWidgetItem *MainWin::createTopPage(char *name)
131 QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
132 item->setText(0, name);
136 /* Create A Tree Widget Item which will be associated with a Page in the stacked widget */
137 QTreeWidgetItem *MainWin::createPage(char *name, QTreeWidgetItem *parent)
139 QTreeWidgetItem *item = new QTreeWidgetItem(parent);
140 item->setText(0, name);
145 * Handle up and down arrow keys for the command line
148 void MainWin::keyPressEvent(QKeyEvent *event)
150 if (m_cmd_history.size() == 0) {
154 switch (event->key()) {
156 if (m_cmd_last < 0 || m_cmd_last >= (m_cmd_history.size()-1)) {
163 if (m_cmd_last == 0) {
167 if (m_cmd_last < 0 || m_cmd_last > (m_cmd_history.size()-1)) {
168 m_cmd_last = m_cmd_history.size() - 1;
177 lineEdit->setText(m_cmd_history[m_cmd_last]);
180 void MainWin::createConnections()
182 /* Connect signals to slots */
183 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(input_line()));
184 connect(actionAbout_bat, SIGNAL(triggered()), this, SLOT(about()));
187 connect(treeWidget, SIGNAL(itemActivated(QTreeWidgetItem *, int)), this,
188 SLOT(treeItemClicked(QTreeWidgetItem *, int)));
189 connect(treeWidget, SIGNAL(itemPressed(QTreeWidgetItem *, int)), this,
190 SLOT(treeItemClicked(QTreeWidgetItem *, int)));
192 connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this,
193 SLOT(treeItemClicked(QTreeWidgetItem *, int)));
194 connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this,
195 SLOT(treeItemDoubleClicked(QTreeWidgetItem *, int)));
196 connect(treeWidget, SIGNAL(
197 currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
198 this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
199 connect(stackedWidget, SIGNAL(currentChanged(int)),
200 this, SLOT(stackItemChanged(int)));
202 connect(actionQuit, SIGNAL(triggered()), app, SLOT(closeAllWindows()));
203 connect(actionConnect, SIGNAL(triggered()), m_console, SLOT(connect()));
204 connect(actionStatusDir, SIGNAL(triggered()), m_console, SLOT(status_dir()));
205 connect(actionSelectFont, SIGNAL(triggered()), m_console, SLOT(set_font()));
206 connect(actionLabel, SIGNAL(triggered()), this, SLOT(labelDialogClicked()));
207 connect(actionRun, SIGNAL(triggered()), this, SLOT(runDialogClicked()));
208 connect(actionRestore, SIGNAL(triggered()), this, SLOT(restoreDialogClicked()));
209 connect(actionUndock, SIGNAL(triggered()), this, SLOT(undockWindowButton()));
210 connect(actionToggleDock, SIGNAL(triggered()), this, SLOT(toggleDockContextWindow()));
214 * Reimplementation of QWidget closeEvent virtual function
216 void MainWin::closeEvent(QCloseEvent *event)
219 m_console->writeSettings();
220 m_console->terminate();
224 void MainWin::writeSettings()
226 QSettings settings("bacula.org", "bat");
228 settings.beginGroup("MainWin");
229 settings.setValue("winSize", size());
230 settings.setValue("winPos", pos());
234 void MainWin::readSettings()
236 QSettings settings("bacula.org", "bat");
238 settings.beginGroup("MainWin");
239 resize(settings.value("winSize", QSize(1041, 801)).toSize());
240 move(settings.value("winPos", QPoint(200, 150)).toPoint());
245 * This subroutine is called with an item in the Page Selection window
248 void MainWin::treeItemClicked(QTreeWidgetItem *item, int column)
250 /* Use tree item's Qt::UserRole to get treeindex */
251 int treeindex = item->data(column, Qt::UserRole).toInt();
253 /* Is this one of the first level pages */
254 if( m_pagehash.value(treeindex) ){
255 Pages* page = m_pagehash.value(treeindex);
256 int stackindex=stackedWidget->indexOf(page);
258 if( stackindex >= 0 ){
259 stackedWidget->setCurrentIndex(0);
260 stackedWidget->setCurrentWidget(page);
262 /* run the virtual function in case this class overrides it */
264 page->PgSeltreeWidgetClicked();
270 * This subroutine is called with an item in the Page Selection window
273 void MainWin::treeItemDoubleClicked(QTreeWidgetItem * /*item*/, int /*column*/)
278 * Called with a change of the highlighed tree widget item in the page selector.
281 void MainWin::treeItemChanged(QTreeWidgetItem *currentitem, QTreeWidgetItem *previousitem)
284 /* The Previous item */
286 /* Use tree item's Qt::UserRole to get treeindex now for the previousitem */
288 treeindex = previousitem->data(0, Qt::UserRole).toInt();
289 /* Is this one of the first level pages */
290 if( m_pagehash.value(treeindex) ){
291 Pages* page = m_pagehash.value(treeindex);
292 treeWidget->removeAction(actionToggleDock);
293 foreach( QAction* pageaction, page->m_contextActions ){
294 treeWidget->removeAction(pageaction);
299 /* Use tree item's Qt::UserRole to get treeindex */
300 treeindex = currentitem->data(0, Qt::UserRole).toInt();
302 /* Is this one of the first level pages */
303 if( m_pagehash.value(treeindex) ){
304 Pages* page = m_pagehash.value(treeindex);
305 int stackindex = stackedWidget->indexOf(page);
307 /* Is this page currently on the stack */
308 if( stackindex >= 0 ){
309 /* put this page on the top of the stack */
310 stackedWidget->setCurrentIndex(stackindex);
312 setContextMenuDockText(page, currentitem);
314 treeWidget->addAction(actionToggleDock);
316 /* Add the actions to the Page Selectors tree widget that are part of the
317 * current items list of desired actions regardless of whether on top of stack*/
318 treeWidget->addActions(page->m_contextActions);
322 void MainWin::labelDialogClicked()
324 new labelDialog(m_console);
327 void MainWin::runDialogClicked()
329 new runDialog(m_console);
332 void MainWin::restoreDialogClicked()
334 new prerestoreDialog(m_console);
340 * The user just finished typing a line in the command line edit box
342 void MainWin::input_line()
344 QString cmdStr = lineEdit->text(); /* Get the text */
345 lineEdit->clear(); /* clear the lineEdit box */
346 if (m_console->is_connected()) {
347 m_console->display_text(cmdStr + "\n");
348 m_console->write_dir(cmdStr.toUtf8().data()); /* send to dir */
350 set_status("Director not connected. Click on connect button.");
352 m_cmd_history.append(cmdStr);
357 void MainWin::about()
359 QMessageBox::about(this, tr("About bat"),
360 tr("<br><h2>bat 0.2, by Kern Sibbald</h2>"
361 "<p>Copyright © " BYEAR " Free Software Foundation Europe e.V."
362 "<p>The <b>bat</b> is an administrative console"
363 " interface to the Director."));
366 void MainWin::set_statusf(const char *fmt, ...)
371 va_start(arg_ptr, fmt);
372 len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
377 void MainWin::set_status_ready()
379 set_status(" Ready");
382 void MainWin::set_status(const char *buf)
384 statusBar()->showMessage(buf);
388 * Function to respond to the button bar button to undock
390 void MainWin::undockWindowButton()
392 Pages* page = (Pages*)stackedWidget->currentWidget();
393 page->togglePageDocking();
394 /* The window has been undocked, lets change the context menu */
395 setContextMenuDockText();
399 * Function to respond to action on page selector context menu to toggle the
400 * dock status of the window associated with the page selectors current
403 void MainWin::toggleDockContextWindow()
405 QTreeWidgetItem *currentitem = treeWidget->currentItem();
407 /* Use tree item's Qt::UserRole to get treeindex */
408 int treeindex = currentitem->data(0, Qt::UserRole).toInt();
410 /* Is this one of the first level pages */
411 if( m_pagehash.value(treeindex) ){
412 Pages* page = m_pagehash.value(treeindex);
413 page->togglePageDocking();
414 if ( page->isDocked() ){
415 stackedWidget->setCurrentWidget(page);
417 /* Toggle the menu item. The window's dock status has been toggled */
418 setContextMenuDockText(page, currentitem);
423 * Function to set the text of the toggle dock context menu when page and
424 * widget item are NOT known. This is an overoaded funciton.
425 * It is called from MainWin::undockWindowButton, it is not intended to change
426 * for the top pages tree widget, it is for the currently active tree widget
427 * item. Which is why the page is not passed.
429 void MainWin::setContextMenuDockText()
431 QTreeWidgetItem *currentitem = treeWidget->currentItem();
433 /* Use tree item's Qt::UserRole to get treeindex */
434 int treeindex = currentitem->data(0, Qt::UserRole).toInt();
436 /* Is this one of the first level pages */
437 if( m_pagehash.value(treeindex) ){
438 Pages* page = m_pagehash.value(treeindex);
439 setContextMenuDockText(page, currentitem);
444 * Function to set the text of the toggle dock context menu when page and
445 * widget item are known. This is the more commonly used.
447 void MainWin::setContextMenuDockText( Pages* page, QTreeWidgetItem* item )
449 QString docktext("");
450 if( page->isDocked() ){
451 docktext += "UnDock ";
453 docktext += "ReDock ";
455 docktext += item->text(0) += " Window";
457 actionToggleDock->setText(docktext);
460 void MainWin::stackItemChanged(int)
462 Pages* page = (Pages*)stackedWidget->currentWidget();
463 /* run the virtual function in case this class overrides it */
464 page->currentStackItem();