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(actionPullWindowOut);
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->setIcon(0, QIcon(QString::fromUtf8("images/server.png")));
84 /* Create Tree Widget Item */
85 item = createPage("Console", topItem);
86 m_console->SetPassedValues(stackedWidget, item, m_pages );
88 /* Append to pagelist */
89 m_pagelist.insert(m_pages, m_console);
91 /* Set Color of treeWidgetItem for the console
92 * It will be set to gree in the console class if the connection is made.
94 QBrush redBrush(Qt::red);
95 item->setForeground(0, redBrush);
98 * Now with the console created, on with the rest, these are easy
100 * 1. create tree widget item
101 * 2. create object passing pointer to tree widget item (modified constructors to pass QTreeWidget pointers)
102 * 3. append to stackhash
107 item=createPage("brestore", topItem);
108 bRestore* brestore=new bRestore(stackedWidget, item, m_pages);
109 m_pagelist.insert(m_pages, brestore);
112 /* lastly for now, the medialist */
114 item=createPage("Media", topItem );
115 MediaList* medialist=new MediaList(stackedWidget, m_console, item, m_pages);
116 m_pagelist.insert(m_pages, medialist);
118 /* Iterate through and add to the stack */
119 foreach (Pages *page, m_pagelist)
122 treeWidget->expandItem(topItem);
123 stackedWidget->setCurrentIndex(0);
126 /* Create a root Tree Widget */
127 QTreeWidgetItem *MainWin::createTopPage(char *name)
129 QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
130 item->setText(0, name);
134 /* Create A Tree Widget Item which will be associated with a Page in the stacked widget */
135 QTreeWidgetItem *MainWin::createPage(char *name, QTreeWidgetItem *parent)
137 QTreeWidgetItem *item = new QTreeWidgetItem(parent);
138 item->setText(0, name);
143 * Handle up and down arrow keys for the command line
146 void MainWin::keyPressEvent(QKeyEvent *event)
148 if (m_cmd_history.size() == 0) {
152 switch (event->key()) {
154 if (m_cmd_last < 0 || m_cmd_last >= (m_cmd_history.size()-1)) {
161 if (m_cmd_last == 0) {
165 if (m_cmd_last < 0 || m_cmd_last > (m_cmd_history.size()-1)) {
166 m_cmd_last = m_cmd_history.size() - 1;
175 lineEdit->setText(m_cmd_history[m_cmd_last]);
178 void MainWin::createConnections()
180 /* Connect signals to slots */
181 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(input_line()));
182 connect(actionAbout_bat, SIGNAL(triggered()), this, SLOT(about()));
185 connect(treeWidget, SIGNAL(itemActivated(QTreeWidgetItem *, int)), this,
186 SLOT(treeItemClicked(QTreeWidgetItem *, int)));
187 connect(treeWidget, SIGNAL(itemPressed(QTreeWidgetItem *, int)), this,
188 SLOT(treeItemClicked(QTreeWidgetItem *, int)));
190 connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this,
191 SLOT(treeItemClicked(QTreeWidgetItem *, int)));
192 connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this,
193 SLOT(treeItemDoubleClicked(QTreeWidgetItem *, int)));
195 connect(actionQuit, SIGNAL(triggered()), app, SLOT(closeAllWindows()));
196 connect(actionConnect, SIGNAL(triggered()), m_console, SLOT(connect()));
197 connect(actionStatusDir, SIGNAL(triggered()), m_console, SLOT(status_dir()));
198 connect(actionSelectFont, SIGNAL(triggered()), m_console, SLOT(set_font()));
199 connect(actionLabel, SIGNAL(triggered()), this, SLOT(labelDialogClicked()));
200 connect(actionRun, SIGNAL(triggered()), this, SLOT(runDialogClicked()));
201 connect(actionRestore, SIGNAL(triggered()), this, SLOT(restoreDialogClicked()));
202 connect(actionPullWindowOut, SIGNAL(triggered()), this, SLOT(undockWindowButton()));
206 * Reimplementation of QWidget closeEvent virtual function
208 void MainWin::closeEvent(QCloseEvent *event)
211 m_console->writeSettings();
212 m_console->terminate();
216 void MainWin::writeSettings()
218 QSettings settings("bacula.org", "bat");
220 settings.beginGroup("MainWin");
221 settings.setValue("winSize", size());
222 settings.setValue("winPos", pos());
226 void MainWin::readSettings()
228 QSettings settings("bacula.org", "bat");
230 settings.beginGroup("MainWin");
231 resize(settings.value("winSize", QSize(1041, 801)).toSize());
232 move(settings.value("winPos", QPoint(200, 150)).toPoint());
237 * This subroutine is called with an item in the Page Selection window
240 void MainWin::treeItemClicked(QTreeWidgetItem *item, int column)
242 /* Use tree item's Qt::UserRole to get treeindex */
243 int treeindex = item->data(column, Qt::UserRole).toInt();
244 int stackindex=stackedWidget->indexOf(m_pagelist.value(treeindex));
246 if( stackindex >= 0 ){
247 stackedWidget->setCurrentIndex(stackindex);
249 /* run the virtual function in case this class overrides it */
250 m_pagelist.value(treeindex)->PgSeltreeWidgetClicked();
254 * This subroutine is called with an item in the Page Selection window
257 void MainWin::treeItemDoubleClicked(QTreeWidgetItem *item, int column)
259 int treeindex = item->data(column, Qt::UserRole).toInt();
261 /* Use tree item's Qt::UserRole to get treeindex */
262 if (m_pagelist.value(treeindex)->isDocked()) {
263 m_pagespophold = m_pagelist.value(treeindex);
265 /* Create a popup menu before floating window */
266 QMenu *popup = new QMenu( treeWidget );
267 connect(popup->addAction("Undock Window"), SIGNAL(triggered()), this,
268 SLOT(undockWindow()));
269 popup->exec(QCursor::pos());
271 /* Just pull it back in without prompting */
272 m_pagelist.value(treeindex)->togglePageDocking();
274 /* Here is the virtual function so that different classes can do different things */
275 m_pagelist.value(treeindex)->PgSeltreeWidgetDoubleClicked();
278 void MainWin::labelDialogClicked()
280 new labelDialog(m_console);
283 void MainWin::runDialogClicked()
285 new runDialog(m_console);
288 void MainWin::restoreDialogClicked()
290 new prerestoreDialog(m_console);
296 * The user just finished typing a line in the command line edit box
298 void MainWin::input_line()
300 QString cmdStr = lineEdit->text(); /* Get the text */
301 lineEdit->clear(); /* clear the lineEdit box */
302 if (m_console->is_connected()) {
303 m_console->display_text(cmdStr + "\n");
304 m_console->write_dir(cmdStr.toUtf8().data()); /* send to dir */
306 set_status("Director not connected. Click on connect button.");
308 m_cmd_history.append(cmdStr);
313 void MainWin::about()
315 QMessageBox::about(this, tr("About bat"),
316 tr("<br><h2>bat 0.2, by Kern Sibbald</h2>"
317 "<p>Copyright © " BYEAR " Free Software Foundation Europe e.V."
318 "<p>The <b>bat</b> is an administrative console"
319 " interface to the Director."));
322 void MainWin::set_statusf(const char *fmt, ...)
327 va_start(arg_ptr, fmt);
328 len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
333 void MainWin::set_status_ready()
335 set_status(" Ready");
338 void MainWin::set_status(const char *buf)
340 statusBar()->showMessage(buf);
343 void MainWin::undockWindow()
345 m_pagespophold->togglePageDocking();
348 void MainWin::undockWindowButton()
350 Pages* page = (Pages*)stackedWidget->currentWidget();
351 page->togglePageDocking();