]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/mainwin.cpp
3009edf102302d016d58b9b9b8d69f337d846387
[bacula/bacula] / bacula / src / qt-console / mainwin.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2007 Free Software Foundation Europe e.V.
5
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.
12
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.
17
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
21    02110-1301, USA.
22
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.
27 */
28
29 /*
30  *   Version $Id$
31  *
32  *  Main Window control for bat (qt-console)
33  *
34  *   Kern Sibbald, January MMVII
35  *
36  */ 
37
38 #include "bat.h"
39 #include "medialist/medialist.h"
40
41 MainWin::MainWin(QWidget *parent) : QMainWindow(parent)
42 {
43
44    mainWin = this;
45    setupUi(this);                     /* Setup UI defined by main.ui (designer) */
46    treeWidget->clear();
47    treeWidget->setColumnCount(1);
48    treeWidget->setHeaderLabel("Select Page");
49
50    m_pages = 0;
51    createPages();
52
53    resetFocus();
54
55    createConnections();
56
57    this->show();
58
59    readSettings();
60
61    m_console->connect();
62    m_medialist->populateTree();
63 }
64
65 void MainWin::createPages()
66 {
67    DIRRES *dir;
68
69    QTreeWidgetItem *item, *topItem;
70    m_console = new Console(stackedWidget);
71    m_console->AddTostack();
72
73    m_brestore = new bRestore(stackedWidget);
74    m_brestore->AddTostack();
75
76    m_medialist = new MediaList(stackedWidget, m_console);
77    m_medialist->AddTostack();
78
79    /* Just take the first Director */
80    LockRes();
81    dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
82    m_console->setDirRes(dir);
83    UnlockRes();
84
85    topItem = createTopPage(dir->name(), false);
86    topItem->setIcon(0, QIcon(QString::fromUtf8("images/server.png")));
87
88    item = createPage("Console", topItem, true);
89    m_console->setTreeItem(item);
90    QBrush redBrush(Qt::red);
91    item->setForeground(0, redBrush);
92
93    item = createPage("brestore", topItem, true);
94    item = createPage("Storage Tree", topItem, true);
95
96    treeWidget->expandItem(topItem);
97
98    stackedWidget->setCurrentIndex(0);
99 }
100
101 /* Create a root Tree Widget */
102 QTreeWidgetItem *MainWin::createTopPage(char *name, bool canDisplay)
103 {
104    QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
105    item->setText(0, name);
106    if (canDisplay) {
107       item->setData(0, Qt::UserRole, QVariant(m_pages++));
108    } else {
109       item->setData(0, Qt::UserRole, QVariant(-1));
110    }
111    return item;
112 }
113
114 /* Create A Tree Widget Item Representing a Page */
115 QTreeWidgetItem *MainWin::createPage(char *name, QTreeWidgetItem *parent, bool canDisplay)
116 {
117    QTreeWidgetItem *item = new QTreeWidgetItem(parent);
118    item->setText(0, name);
119    if (canDisplay) {
120       item->setData(0, Qt::UserRole, QVariant(m_pages++));
121    } else {
122       item->setData(0, Qt::UserRole, QVariant(-1));
123    }
124    return item;
125 }
126
127
128 /*
129  * Handle up and down arrow keys for the command line
130  *  history.
131  */
132 void MainWin::keyPressEvent(QKeyEvent *event)
133 {
134    if (m_cmd_history.size() == 0) {
135       event->ignore();
136       return;
137    }
138    switch (event->key()) {
139    case Qt::Key_Down:
140       if (m_cmd_last < 0 || m_cmd_last >= (m_cmd_history.size()-1)) {
141          event->ignore();
142          return;
143       }
144       m_cmd_last++;
145       break;
146    case Qt::Key_Up:
147       if (m_cmd_last == 0) {
148          event->ignore();
149          return;
150       }
151       if (m_cmd_last < 0 || m_cmd_last > (m_cmd_history.size()-1)) {
152          m_cmd_last = m_cmd_history.size() - 1;
153       } else {
154          m_cmd_last--;
155       }
156       break;
157    default:
158       event->ignore();
159       return;
160    }
161    lineEdit->setText(m_cmd_history[m_cmd_last]);
162 }
163
164 void MainWin::createConnections()
165 {
166    /* Connect signals to slots */
167    connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(input_line()));
168    connect(actionAbout_bat, SIGNAL(triggered()), this, SLOT(about()));
169
170    connect(treeWidget, SIGNAL(itemActivated(QTreeWidgetItem *, int)), this, 
171            SLOT(treeItemClicked(QTreeWidgetItem *, int)));
172    connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, 
173            SLOT(treeItemClicked(QTreeWidgetItem *, int)));
174    connect(treeWidget, SIGNAL(itemPressed(QTreeWidgetItem *, int)), this, 
175            SLOT(treeItemClicked(QTreeWidgetItem *, int)));
176    connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, 
177            SLOT(treeItemDoubleClicked(QTreeWidgetItem *, int)));
178
179    connect(actionQuit, SIGNAL(triggered()), app, SLOT(closeAllWindows()));
180    connect(actionConnect, SIGNAL(triggered()), m_console, SLOT(connect()));
181    connect(actionStatusDir, SIGNAL(triggered()), m_console, SLOT(status_dir()));
182    connect(actionSelectFont, SIGNAL(triggered()), m_console, SLOT(set_font()));
183    connect(actionLabel, SIGNAL(triggered()), this,  SLOT(labelDialogClicked()));
184    connect(actionRun, SIGNAL(triggered()), this,  SLOT(runDialogClicked()));
185    connect(actionRestore, SIGNAL(triggered()), this,  SLOT(restoreDialogClicked()));
186 }
187
188 /* 
189  * Reimplementation of QWidget closeEvent virtual function   
190  */
191 void MainWin::closeEvent(QCloseEvent *event)
192 {
193    writeSettings();
194    m_console->writeSettings();
195    m_console->terminate();
196    event->accept();
197 }
198
199 void MainWin::writeSettings()
200 {
201    QSettings settings("bacula.org", "bat");
202
203    settings.beginGroup("MainWin");
204    settings.setValue("winSize", size());
205    settings.setValue("winPos", pos());
206    settings.endGroup();
207 }
208
209 void MainWin::readSettings()
210
211    QSettings settings("bacula.org", "bat");
212
213    settings.beginGroup("MainWin");
214    resize(settings.value("winSize", QSize(1041, 801)).toSize());
215    move(settings.value("winPos", QPoint(200, 150)).toPoint());
216    settings.endGroup();
217 }
218
219 void MainWin::treeItemClicked(QTreeWidgetItem *item, int column)
220 {
221    /* There just has to be a more elegant way of doing this
222  * as more and more pages get added, this could get realllly long */
223    int treeindex = item->data(column, Qt::UserRole).toInt();
224    int stackindex=-1;
225    if( treeindex == 0 ) {
226      stackindex=stackedWidget->indexOf( m_console );
227    }
228    if( treeindex == 1 ) {
229      stackindex=stackedWidget->indexOf( m_brestore );
230    }
231    if( treeindex == 2 ) {
232      stackindex=stackedWidget->indexOf( m_medialist );
233    }
234    if( stackindex >= 0 ){
235       stackedWidget->setCurrentIndex(stackindex);
236    }
237 }
238
239 /*
240  */
241 void MainWin::treeItemDoubleClicked(QTreeWidgetItem *item, int column)
242 {
243    /* There just has to be a more elegant way of doing this
244  * as more and more pages get added, this could get realllly long */
245    int treeindex = item->data(column, Qt::UserRole).toInt();
246    if( treeindex == 0 ) {
247      m_console->Togglestack();
248    }
249    if( treeindex == 1 ) {
250      m_brestore->Togglestack();
251    }
252    if( treeindex == 2 ) {
253      m_medialist->Togglestack();
254    }
255 }
256
257 void MainWin::labelDialogClicked() 
258 {
259    new labelDialog(m_console);
260 }
261
262 void MainWin::runDialogClicked() 
263 {
264    new runDialog(m_console);
265 }
266
267 void MainWin::restoreDialogClicked() 
268 {
269    new prerestoreDialog(m_console);
270 }
271
272
273
274 /*
275  * The user just finished typing a line in the command line edit box
276  */
277 void MainWin::input_line()
278 {
279    QString cmdStr = lineEdit->text();    /* Get the text */
280    lineEdit->clear();                    /* clear the lineEdit box */
281    if (m_console->is_connected()) {
282       m_console->display_text(cmdStr + "\n");
283       m_console->write_dir(cmdStr.toUtf8().data());         /* send to dir */
284    } else {
285       set_status("Director not connected. Click on connect button.");
286    }
287    m_cmd_history.append(cmdStr);
288    m_cmd_last = -1;
289 }
290
291
292 void MainWin::about()
293 {
294    QMessageBox::about(this, tr("About bat"),
295             tr("<br><h2>bat 0.2, by Kern Sibbald</h2>"
296             "<p>Copyright &copy; " BYEAR " Free Software Foundation Europe e.V."
297             "<p>The <b>bat</b> is an administrative console"
298                " interface to the Director."));
299 }
300
301 void MainWin::set_statusf(const char *fmt, ...)
302 {
303    va_list arg_ptr;
304    char buf[1000];
305    int len;
306    va_start(arg_ptr, fmt);
307    len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
308    va_end(arg_ptr);
309    set_status(buf);
310 }
311
312 void MainWin::set_status_ready()
313 {
314    set_status(" Ready");
315 }
316
317 void MainWin::set_status(const char *buf)
318 {
319    statusBar()->showMessage(buf);
320 }