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
39 #include "medialist/medialist.h"
41 MainWin::MainWin(QWidget *parent) : QMainWindow(parent)
45 setupUi(this); /* Setup UI defined by main.ui (designer) */
47 treeWidget->setColumnCount(1);
48 treeWidget->setHeaderLabel("Select Page");
62 m_medialist->populateTree();
65 void MainWin::createPages()
68 QTreeWidgetItem *item, *topItem;
70 /* Create console tree stacked widget item */
71 m_console = new Console(stackedWidget);
72 /* Console is special needs director*/
73 /* Just take the first Director */
75 dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
76 m_console->setDirRes(dir);
78 /* The top tree item representing the director */
79 topItem = createTopPage(dir->name(), false);
80 topItem->setIcon(0, QIcon(QString::fromUtf8("images/server.png")));
81 /* Create Tree Widget Item */
82 item = createPage("Console", topItem, true);
83 /* Append to bstacklist */
84 m_bstacklist.append(m_console);
85 /* FIXME Associate tree item with console, may not be needed. */
86 m_console->setTreeItem(item);
87 /* Set BatStack m_treeItem */
88 m_console->SetBSTreeWidgetItem(item);
89 QBrush redBrush(Qt::red);
90 item->setForeground(0, redBrush);
92 /* Now with the console created, on with the rest, these are easy */
94 * 1. create tree widget item
95 * 2. create object passing pointer to tree widget item (modified constructors to pass QTreeWidget pointers)
96 * 3. append to stacklist */
99 item = createPage("brestore", topItem, true);
100 m_brestore = new bRestore(stackedWidget,item);
101 m_bstacklist.append(m_brestore);
103 /* lastly for now, the medialist */
104 item = createPage("Storage Tree", topItem, true);
105 m_medialist = new MediaList(stackedWidget, m_console, item);
106 m_bstacklist.append(m_medialist);
108 /* Iterate through and add to the stack */
109 for ( QList<BatStack*>::iterator bstackItem = m_bstacklist.begin(); bstackItem != m_bstacklist.end(); ++bstackItem ) {
110 (*bstackItem)->AddTostack();
113 treeWidget->expandItem(topItem);
114 stackedWidget->setCurrentIndex(0);
117 /* FIXME These Two are not needed any more because UserRole is not used. */
118 /* Create a root Tree Widget */
119 QTreeWidgetItem *MainWin::createTopPage(char *name, bool canDisplay)
121 QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
122 item->setText(0, name);
124 item->setData(0, Qt::UserRole, QVariant(m_pages++));
126 item->setData(0, Qt::UserRole, QVariant(-1));
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, bool canDisplay)
134 QTreeWidgetItem *item = new QTreeWidgetItem(parent);
135 item->setText(0, name);
137 item->setData(0, Qt::UserRole, QVariant(m_pages++));
139 item->setData(0, Qt::UserRole, QVariant(-1));
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()));
186 connect(treeWidget, SIGNAL(itemActivated(QTreeWidgetItem *, int)), this,
187 SLOT(treeItemClicked(QTreeWidgetItem *, int)));
188 connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this,
189 SLOT(treeItemClicked(QTreeWidgetItem *, int)));
190 connect(treeWidget, SIGNAL(itemPressed(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()));
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());
235 void MainWin::treeItemClicked(QTreeWidgetItem *item, int column)
237 /* Iterate through and find the tree widget item clicked */
239 for ( QList<BatStack*>::iterator bstackItem = m_bstacklist.begin(); bstackItem != m_bstacklist.end(); ++bstackItem ) {
240 if ( item == (*bstackItem)->m_treeItem ) {
241 int stackindex=stackedWidget->indexOf( *bstackItem );
242 if( stackindex >= 0 ){
243 stackedWidget->setCurrentIndex(stackindex);
251 void MainWin::treeItemDoubleClicked(QTreeWidgetItem *item, int column)
253 /* Iterate through and find the tree widget item double clicked */
255 for ( QList<BatStack*>::iterator bstackItem = m_bstacklist.begin(); bstackItem != m_bstacklist.end(); ++bstackItem ) {
256 if ( item == (*bstackItem)->m_treeItem ) {
257 (*bstackItem)->Togglestack();
262 void MainWin::labelDialogClicked()
264 new labelDialog(m_console);
267 void MainWin::runDialogClicked()
269 new runDialog(m_console);
272 void MainWin::restoreDialogClicked()
274 new prerestoreDialog(m_console);
280 * The user just finished typing a line in the command line edit box
282 void MainWin::input_line()
284 QString cmdStr = lineEdit->text(); /* Get the text */
285 lineEdit->clear(); /* clear the lineEdit box */
286 if (m_console->is_connected()) {
287 m_console->display_text(cmdStr + "\n");
288 m_console->write_dir(cmdStr.toUtf8().data()); /* send to dir */
290 set_status("Director not connected. Click on connect button.");
292 m_cmd_history.append(cmdStr);
297 void MainWin::about()
299 QMessageBox::about(this, tr("About bat"),
300 tr("<br><h2>bat 0.2, by Kern Sibbald</h2>"
301 "<p>Copyright © " BYEAR " Free Software Foundation Europe e.V."
302 "<p>The <b>bat</b> is an administrative console"
303 " interface to the Director."));
306 void MainWin::set_statusf(const char *fmt, ...)
311 va_start(arg_ptr, fmt);
312 len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
317 void MainWin::set_status_ready()
319 set_status(" Ready");
322 void MainWin::set_status(const char *buf)
324 statusBar()->showMessage(buf);