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 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 stacklist
103 * And it can even be done in one line.
107 m_pageslist.append(new bRestore(stackedWidget,
108 createPage("brestore", topItem), m_pages++ ));
111 /* lastly for now, the medialist */
112 m_pageslist.append(new MediaList(stackedWidget, m_console,
113 createPage("Storage Tree", topItem ), m_pages++));
115 /* Iterate through and add to the stack */
116 for (QList<Pages*>::iterator pagesItem = m_pageslist.begin();
117 pagesItem != m_pageslist.end(); ++pagesItem ) {
118 (*pagesItem)->dockPage();
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)));
194 connect(actionQuit, SIGNAL(triggered()), app, SLOT(closeAllWindows()));
195 connect(actionConnect, SIGNAL(triggered()), m_console, SLOT(connect()));
196 connect(actionStatusDir, SIGNAL(triggered()), m_console, SLOT(status_dir()));
197 connect(actionSelectFont, SIGNAL(triggered()), m_console, SLOT(set_font()));
198 connect(actionLabel, SIGNAL(triggered()), this, SLOT(labelDialogClicked()));
199 connect(actionRun, SIGNAL(triggered()), this, SLOT(runDialogClicked()));
200 connect(actionRestore, SIGNAL(triggered()), this, SLOT(restoreDialogClicked()));
201 connect(actionPullWindowOut, SIGNAL(triggered()), this, SLOT(undockWindowButton()));
205 * Reimplementation of QWidget closeEvent virtual function
207 void MainWin::closeEvent(QCloseEvent *event)
210 m_console->writeSettings();
211 m_console->terminate();
215 void MainWin::writeSettings()
217 QSettings settings("bacula.org", "bat");
219 settings.beginGroup("MainWin");
220 settings.setValue("winSize", size());
221 settings.setValue("winPos", pos());
225 void MainWin::readSettings()
227 QSettings settings("bacula.org", "bat");
229 settings.beginGroup("MainWin");
230 resize(settings.value("winSize", QSize(1041, 801)).toSize());
231 move(settings.value("winPos", QPoint(200, 150)).toPoint());
236 * This subroutine is called with an item in the Page Selection window
239 void MainWin::treeItemClicked(QTreeWidgetItem *item, int column)
241 /* Use tree item's Qt::UserRole to get treeindex */
242 int treeindex = item->data(column, Qt::UserRole).toInt();
243 int stackindex=stackedWidget->indexOf(m_pageslist[treeindex]);
245 if( stackindex >= 0 ){
246 stackedWidget->setCurrentIndex(stackindex);
248 /* run the virtual function in case this class overrides it */
249 m_pageslist[treeindex]->PgSeltreeWidgetClicked();
253 * This subroutine is called with an item in the Page Selection window
256 void MainWin::treeItemDoubleClicked(QTreeWidgetItem *item, int column)
258 int treeindex = item->data(column, Qt::UserRole).toInt();
260 /* Use tree item's Qt::UserRole to get treeindex */
261 if (m_pageslist[treeindex]->isDocked()) {
262 m_pagespophold = m_pageslist[treeindex];
264 /* Create a popup menu before floating window */
265 QMenu *popup = new QMenu( treeWidget );
266 connect(popup->addAction("Undock Window"), SIGNAL(triggered()), this,
267 SLOT(undockWindow()));
268 popup->exec(QCursor::pos());
270 /* Just pull it back in without prompting */
271 m_pageslist[treeindex]->togglePageDocking();
273 /* Here is the virtual function so that different classes can do different things */
274 m_pageslist[treeindex]->PgSeltreeWidgetDoubleClicked();
277 void MainWin::labelDialogClicked()
279 new labelDialog(m_console);
282 void MainWin::runDialogClicked()
284 new runDialog(m_console);
287 void MainWin::restoreDialogClicked()
289 new prerestoreDialog(m_console);
295 * The user just finished typing a line in the command line edit box
297 void MainWin::input_line()
299 QString cmdStr = lineEdit->text(); /* Get the text */
300 lineEdit->clear(); /* clear the lineEdit box */
301 if (m_console->is_connected()) {
302 m_console->display_text(cmdStr + "\n");
303 m_console->write_dir(cmdStr.toUtf8().data()); /* send to dir */
305 set_status("Director not connected. Click on connect button.");
307 m_cmd_history.append(cmdStr);
312 void MainWin::about()
314 QMessageBox::about(this, tr("About bat"),
315 tr("<br><h2>bat 0.2, by Kern Sibbald</h2>"
316 "<p>Copyright © " BYEAR " Free Software Foundation Europe e.V."
317 "<p>The <b>bat</b> is an administrative console"
318 " interface to the Director."));
321 void MainWin::set_statusf(const char *fmt, ...)
326 va_start(arg_ptr, fmt);
327 len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
332 void MainWin::set_status_ready()
334 set_status(" Ready");
337 void MainWin::set_status(const char *buf)
339 statusBar()->showMessage(buf);
342 void MainWin::undockWindow()
344 m_pagespophold->togglePageDocking();
347 void MainWin::undockWindowButton()
349 int curindex = stackedWidget->currentIndex();
350 QList<Pages *>::iterator pagesItem = m_pageslist.begin();
352 while ((pagesItem != m_pageslist.end())){
353 if (curindex == stackedWidget->indexOf(*pagesItem)) {
354 (*pagesItem)->togglePageDocking();