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 pageslist */
89 m_pageslist.append(m_console);
91 /* Set BatStack m_treeItem */
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 stacklist
101 * And it can even be done in one line.
105 m_pageslist.append(new bRestore(stackedWidget,
106 createPage("brestore", topItem), m_pages++ ));
109 /* lastly for now, the medialist */
110 m_pageslist.append(new MediaList(stackedWidget, m_console,
111 createPage("Storage Tree", topItem ), m_pages++));
113 /* Iterate through and add to the stack */
114 for (QList<Pages*>::iterator pagesItem = m_pageslist.begin();
115 pagesItem != m_pageslist.end(); ++pagesItem ) {
116 (*pagesItem)->dockPage();
119 treeWidget->expandItem(topItem);
120 stackedWidget->setCurrentIndex(0);
123 /* Create a root Tree Widget */
124 QTreeWidgetItem *MainWin::createTopPage(char *name)
126 QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
127 item->setText(0, name);
131 /* Create A Tree Widget Item which will be associated with a Page in the stacked widget */
132 QTreeWidgetItem *MainWin::createPage(char *name, QTreeWidgetItem *parent)
134 QTreeWidgetItem *item = new QTreeWidgetItem(parent);
135 item->setText(0, name);
140 * Handle up and down arrow keys for the command line
143 void MainWin::keyPressEvent(QKeyEvent *event)
145 if (m_cmd_history.size() == 0) {
149 switch (event->key()) {
151 if (m_cmd_last < 0 || m_cmd_last >= (m_cmd_history.size()-1)) {
158 if (m_cmd_last == 0) {
162 if (m_cmd_last < 0 || m_cmd_last > (m_cmd_history.size()-1)) {
163 m_cmd_last = m_cmd_history.size() - 1;
172 lineEdit->setText(m_cmd_history[m_cmd_last]);
175 void MainWin::createConnections()
177 /* Connect signals to slots */
178 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(input_line()));
179 connect(actionAbout_bat, SIGNAL(triggered()), this, SLOT(about()));
182 connect(treeWidget, SIGNAL(itemActivated(QTreeWidgetItem *, int)), this,
183 SLOT(treeItemClicked(QTreeWidgetItem *, int)));
184 connect(treeWidget, SIGNAL(itemPressed(QTreeWidgetItem *, int)), this,
185 SLOT(treeItemClicked(QTreeWidgetItem *, int)));
187 connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this,
188 SLOT(treeItemClicked(QTreeWidgetItem *, int)));
189 connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this,
190 SLOT(treeItemDoubleClicked(QTreeWidgetItem *, int)));
192 connect(actionQuit, SIGNAL(triggered()), app, SLOT(closeAllWindows()));
193 connect(actionConnect, SIGNAL(triggered()), m_console, SLOT(connect()));
194 connect(actionStatusDir, SIGNAL(triggered()), m_console, SLOT(status_dir()));
195 connect(actionSelectFont, SIGNAL(triggered()), m_console, SLOT(set_font()));
196 connect(actionLabel, SIGNAL(triggered()), this, SLOT(labelDialogClicked()));
197 connect(actionRun, SIGNAL(triggered()), this, SLOT(runDialogClicked()));
198 connect(actionRestore, SIGNAL(triggered()), this, SLOT(restoreDialogClicked()));
199 connect(actionPullWindowOut, SIGNAL(triggered()), this, SLOT(undockWindowButton()));
203 * Reimplementation of QWidget closeEvent virtual function
205 void MainWin::closeEvent(QCloseEvent *event)
208 m_console->writeSettings();
209 m_console->terminate();
213 void MainWin::writeSettings()
215 QSettings settings("bacula.org", "bat");
217 settings.beginGroup("MainWin");
218 settings.setValue("winSize", size());
219 settings.setValue("winPos", pos());
223 void MainWin::readSettings()
225 QSettings settings("bacula.org", "bat");
227 settings.beginGroup("MainWin");
228 resize(settings.value("winSize", QSize(1041, 801)).toSize());
229 move(settings.value("winPos", QPoint(200, 150)).toPoint());
234 * This subroutine is called with an item in the Page Selection window
237 void MainWin::treeItemClicked(QTreeWidgetItem *item, int column)
239 /* Use tree item's Qt::UserRole to get treeindex */
240 int treeindex = item->data(column, Qt::UserRole).toInt();
241 int stackindex=stackedWidget->indexOf(m_pageslist[treeindex]);
243 if( stackindex >= 0 ){
244 stackedWidget->setCurrentIndex(stackindex);
246 /* run the virtual function in case this class overrides it */
247 m_pageslist[treeindex]->PgSeltreeWidgetClicked();
251 * This subroutine is called with an item in the Page Selection window
254 void MainWin::treeItemDoubleClicked(QTreeWidgetItem *item, int column)
256 int treeindex = item->data(column, Qt::UserRole).toInt();
258 /* Use tree item's Qt::UserRole to get treeindex */
259 if (m_pageslist[treeindex]->isDocked()) {
260 m_pagespophold = m_pageslist[treeindex];
262 /* Create a popup menu before floating window */
263 QMenu *popup = new QMenu( treeWidget );
264 connect(popup->addAction("Undock Window"), SIGNAL(triggered()), this,
265 SLOT(undockWindow()));
266 popup->exec(QCursor::pos());
268 /* Just pull it back in without prompting */
269 m_pageslist[treeindex]->togglePageDocking();
271 /* Here is the virtual function so that different classes can do different things */
272 m_pageslist[treeindex]->PgSeltreeWidgetDoubleClicked();
275 void MainWin::labelDialogClicked()
277 new labelDialog(m_console);
280 void MainWin::runDialogClicked()
282 new runDialog(m_console);
285 void MainWin::restoreDialogClicked()
287 new prerestoreDialog(m_console);
293 * The user just finished typing a line in the command line edit box
295 void MainWin::input_line()
297 QString cmdStr = lineEdit->text(); /* Get the text */
298 lineEdit->clear(); /* clear the lineEdit box */
299 if (m_console->is_connected()) {
300 m_console->display_text(cmdStr + "\n");
301 m_console->write_dir(cmdStr.toUtf8().data()); /* send to dir */
303 set_status("Director not connected. Click on connect button.");
305 m_cmd_history.append(cmdStr);
310 void MainWin::about()
312 QMessageBox::about(this, tr("About bat"),
313 tr("<br><h2>bat 0.2, by Kern Sibbald</h2>"
314 "<p>Copyright © " BYEAR " Free Software Foundation Europe e.V."
315 "<p>The <b>bat</b> is an administrative console"
316 " interface to the Director."));
319 void MainWin::set_statusf(const char *fmt, ...)
324 va_start(arg_ptr, fmt);
325 len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
330 void MainWin::set_status_ready()
332 set_status(" Ready");
335 void MainWin::set_status(const char *buf)
337 statusBar()->showMessage(buf);
340 void MainWin::undockWindow()
342 m_pagespophold->togglePageDocking();
345 void MainWin::undockWindowButton()
347 int curindex = stackedWidget->currentIndex();
348 QList<Pages *>::iterator pagesItem = m_pageslist.begin();
350 while ((pagesItem != m_pageslist.end())){
351 if (curindex == stackedWidget->indexOf(*pagesItem)) {
352 (*pagesItem)->togglePageDocking();