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");
49 treeWidget->addAction(actionPullWindowOut);
50 treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
66 void MainWin::createPages()
69 QTreeWidgetItem *item, *topItem;
71 /* Create console tree stacked widget item */
72 m_console = new Console(stackedWidget);
74 /* Console is special -> needs director*/
75 /* Just take the first Director */
77 dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
78 m_console->setDirRes(dir);
81 /* The top tree item representing the director */
82 topItem = createTopPage(dir->name());
83 topItem->setIcon(0, QIcon(QString::fromUtf8("images/server.png")));
85 /* Create Tree Widget Item */
86 item = createPage("Console", topItem);
87 m_console->SetPassedValues(stackedWidget, item, m_pages++ );
89 /* Append to bstacklist */
90 m_bstacklist.append(m_console);
92 /* Set BatStack m_treeItem */
93 QBrush redBrush(Qt::red);
94 item->setForeground(0, redBrush);
97 * Now with the console created, on with the rest, these are easy
99 * 1. create tree widget item
100 * 2. create object passing pointer to tree widget item (modified constructors to pass QTreeWidget pointers)
101 * 3. append to stacklist
102 * And it can even be done in one line.
106 m_bstacklist.append(new bRestore(stackedWidget,
107 createPage("brestore", topItem), m_pages++ ));
109 /* lastly for now, the medialist */
110 m_bstacklist.append(new MediaList(stackedWidget, m_console,
111 createPage("Storage Tree", topItem ), m_pages++));
113 /* Iterate through and add to the stack */
114 for (QList<BatStack*>::iterator bstackItem = m_bstacklist.begin();
115 bstackItem != m_bstacklist.end(); ++bstackItem ) {
116 (*bstackItem)->AddTostack();
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()));
181 /* connect(treeWidget, SIGNAL(itemActivated(QTreeWidgetItem *, int)), this,
182 SLOT(treeItemClicked(QTreeWidgetItem *, int)));
183 connect(treeWidget, SIGNAL(itemPressed(QTreeWidgetItem *, int)), this,
184 SLOT(treeItemClicked(QTreeWidgetItem *, int))); Commented out because it was getting to clicked multiple times*/
185 connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this,
186 SLOT(treeItemClicked(QTreeWidgetItem *, int)));
187 connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this,
188 SLOT(treeItemDoubleClicked(QTreeWidgetItem *, int)));
190 connect(actionQuit, SIGNAL(triggered()), app, SLOT(closeAllWindows()));
191 connect(actionConnect, SIGNAL(triggered()), m_console, SLOT(connect()));
192 connect(actionStatusDir, SIGNAL(triggered()), m_console, SLOT(status_dir()));
193 connect(actionSelectFont, SIGNAL(triggered()), m_console, SLOT(set_font()));
194 connect(actionLabel, SIGNAL(triggered()), this, SLOT(labelDialogClicked()));
195 connect(actionRun, SIGNAL(triggered()), this, SLOT(runDialogClicked()));
196 connect(actionRestore, SIGNAL(triggered()), this, SLOT(restoreDialogClicked()));
197 connect(actionPullWindowOut, SIGNAL(triggered()), this, SLOT(pullWindowOutButton()));
201 * Reimplementation of QWidget closeEvent virtual function
203 void MainWin::closeEvent(QCloseEvent *event)
206 m_console->writeSettings();
207 m_console->terminate();
211 void MainWin::writeSettings()
213 QSettings settings("bacula.org", "bat");
215 settings.beginGroup("MainWin");
216 settings.setValue("winSize", size());
217 settings.setValue("winPos", pos());
221 void MainWin::readSettings()
223 QSettings settings("bacula.org", "bat");
225 settings.beginGroup("MainWin");
226 resize(settings.value("winSize", QSize(1041, 801)).toSize());
227 move(settings.value("winPos", QPoint(200, 150)).toPoint());
231 void MainWin::treeItemClicked(QTreeWidgetItem *item, int column)
235 /* Use tree item's Qt::UserRole to get treeindex */
236 int treeindex = item->data(column, Qt::UserRole).toInt();
237 int stackindex=stackedWidget->indexOf(m_bstacklist[treeindex]);
239 if( stackindex >= 0 ){
240 stackedWidget->setCurrentIndex(stackindex);
242 /* run the virtual function in case this class overrides it */
243 m_bstacklist[treeindex]->PgSeltreeWidgetClicked();
248 void MainWin::treeItemDoubleClicked(QTreeWidgetItem *item, int column)
251 int treeindex = item->data(column, Qt::UserRole).toInt();
253 /* Use tree item's Qt::UserRole to get treeindex */
254 if ( m_bstacklist[treeindex]->isStacked() == true ){
255 m_bstackpophold=m_bstacklist[treeindex];
256 /* Create a popup menu before pulling window out */
257 QMenu *popup = new QMenu( treeWidget );
258 connect(popup->addAction("Pull Window Out"), SIGNAL(triggered()), this, SLOT(pullWindowOut()));
259 popup->exec(QCursor::pos());
261 /* Just pull it back in without prompting */
262 m_bstacklist[treeindex]->Togglestack();
264 /* Here is the virtual function so that different classes can do different things */
265 m_bstacklist[treeindex]->PgSeltreeWidgetDoubleClicked();
268 void MainWin::labelDialogClicked()
270 new labelDialog(m_console);
273 void MainWin::runDialogClicked()
275 new runDialog(m_console);
278 void MainWin::restoreDialogClicked()
280 new prerestoreDialog(m_console);
286 * The user just finished typing a line in the command line edit box
288 void MainWin::input_line()
290 QString cmdStr = lineEdit->text(); /* Get the text */
291 lineEdit->clear(); /* clear the lineEdit box */
292 if (m_console->is_connected()) {
293 m_console->display_text(cmdStr + "\n");
294 m_console->write_dir(cmdStr.toUtf8().data()); /* send to dir */
296 set_status("Director not connected. Click on connect button.");
298 m_cmd_history.append(cmdStr);
303 void MainWin::about()
305 QMessageBox::about(this, tr("About bat"),
306 tr("<br><h2>bat 0.2, by Kern Sibbald</h2>"
307 "<p>Copyright © " BYEAR " Free Software Foundation Europe e.V."
308 "<p>The <b>bat</b> is an administrative console"
309 " interface to the Director."));
312 void MainWin::set_statusf(const char *fmt, ...)
317 va_start(arg_ptr, fmt);
318 len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
323 void MainWin::set_status_ready()
325 set_status(" Ready");
328 void MainWin::set_status(const char *buf)
330 statusBar()->showMessage(buf);
333 void MainWin::pullWindowOut()
335 m_bstackpophold->Togglestack();
338 void MainWin::pullWindowOutButton()
340 int curindex = stackedWidget->currentIndex();
341 QList<BatStack*>::iterator bstackItem = m_bstacklist.begin();
343 while ((bstackItem != m_bstacklist.end())){
344 if (curindex == stackedWidget->indexOf(*bstackItem)) {
345 (*bstackItem)->Togglestack();