]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/mainwin.cpp
ac13a49b028396749b99aa2119fffe9bbdd2e445
[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 "pagehash.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    treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
50
51    createPages();
52
53    resetFocus();
54
55    createConnections();
56
57    this->show();
58
59    readSettings();
60
61    m_console->connect();
62 }
63
64 void MainWin::createPages()
65 {
66    DIRRES *dir;
67    QTreeWidgetItem *item, *topItem;
68
69    /* Create console tree stacked widget item */
70    m_console = new Console(stackedWidget);
71
72    /* Console is special -> needs director*/
73    /* Just take the first Director */
74    LockRes();
75    dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
76    m_console->setDirRes(dir);
77    UnlockRes();
78
79    /* The top tree item representing the director */
80    topItem = createTopPage(dir->name());
81    topItem->setIcon(0, QIcon(QString::fromUtf8("images/server.png")));
82
83    /* Create Tree Widget Item */
84    item = createPage("Console", topItem);
85    m_console->setTreeItem(item);
86
87    /* Append to pagelist */
88    m_treeindex.insert(item, m_console);
89
90    /* Set Color of treeWidgetItem for the console
91    * It will be set to gree in the console class if the connection is made.
92    */
93    QBrush redBrush(Qt::red);
94    item->setForeground(0, redBrush);
95
96    /*
97     * Now with the console created, on with the rest, these are easy   
98     * All should be
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 stackhash
102     */
103
104    /* brestore */
105    item=createPage("brestore", topItem);
106    bRestore* brestore=new bRestore(stackedWidget);
107    m_treeindex.insert(item, brestore);
108
109
110    /* lastly for now, the medialist */
111    item=createPage("Media", topItem );
112    MediaList* medialist=new MediaList(stackedWidget, m_console);
113    m_treeindex.insert(item, medialist);
114
115    /* Iterate through and add to the stack */
116    foreach(Pages *page, m_treeindex.m_pagehash)
117       page->dockPage();
118
119    treeWidget->expandItem(topItem);
120    stackedWidget->setCurrentIndex(0);
121 }
122
123 /* Create a root Tree Widget */
124 QTreeWidgetItem *MainWin::createTopPage(char *name)
125 {
126    QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
127    item->setText(0, name);
128    return item;
129 }
130
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)
133 {
134    QTreeWidgetItem *item = new QTreeWidgetItem(parent);
135    item->setText(0, name);
136    return item;
137 }
138
139 /*
140  * Handle up and down arrow keys for the command line
141  *  history.
142  */
143 void MainWin::keyPressEvent(QKeyEvent *event)
144 {
145    if (m_cmd_history.size() == 0) {
146       event->ignore();
147       return;
148    }
149    switch (event->key()) {
150    case Qt::Key_Down:
151       if (m_cmd_last < 0 || m_cmd_last >= (m_cmd_history.size()-1)) {
152          event->ignore();
153          return;
154       }
155       m_cmd_last++;
156       break;
157    case Qt::Key_Up:
158       if (m_cmd_last == 0) {
159          event->ignore();
160          return;
161       }
162       if (m_cmd_last < 0 || m_cmd_last > (m_cmd_history.size()-1)) {
163          m_cmd_last = m_cmd_history.size() - 1;
164       } else {
165          m_cmd_last--;
166       }
167       break;
168    default:
169       event->ignore();
170       return;
171    }
172    lineEdit->setText(m_cmd_history[m_cmd_last]);
173 }
174
175 void MainWin::createConnections()
176 {
177    /* Connect signals to slots */
178    connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(input_line()));
179    connect(actionAbout_bat, SIGNAL(triggered()), this, SLOT(about()));
180
181 #ifdef xxx
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)));  
186 #endif
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)));
191    connect(treeWidget, SIGNAL(
192            currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
193            this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
194    connect(stackedWidget, SIGNAL(currentChanged(int)),
195            this, SLOT(stackItemChanged(int)));
196
197    connect(actionQuit, SIGNAL(triggered()), app, SLOT(closeAllWindows()));
198    connect(actionConnect, SIGNAL(triggered()), m_console, SLOT(connect()));
199    connect(actionStatusDir, SIGNAL(triggered()), m_console, SLOT(status_dir()));
200    connect(actionSelectFont, SIGNAL(triggered()), m_console, SLOT(set_font()));
201    connect(actionLabel, SIGNAL(triggered()), this,  SLOT(labelDialogClicked()));
202    connect(actionRun, SIGNAL(triggered()), this,  SLOT(runDialogClicked()));
203    connect(actionRestore, SIGNAL(triggered()), this,  SLOT(restoreDialogClicked()));
204    connect(actionUndock, SIGNAL(triggered()), this,  SLOT(undockWindowButton()));
205    connect(actionToggleDock, SIGNAL(triggered()), this,  SLOT(toggleDockContextWindow()));
206 }
207
208 /* 
209  * Reimplementation of QWidget closeEvent virtual function   
210  */
211 void MainWin::closeEvent(QCloseEvent *event)
212 {
213    writeSettings();
214    m_console->writeSettings();
215    m_console->terminate();
216    event->accept();
217    foreach(Pages *page, m_treeindex.m_pagehash){
218       if( !page->isDocked() )
219          page->close();
220    }
221 }
222
223 void MainWin::writeSettings()
224 {
225    QSettings settings("bacula.org", "bat");
226
227    settings.beginGroup("MainWin");
228    settings.setValue("winSize", size());
229    settings.setValue("winPos", pos());
230    settings.endGroup();
231 }
232
233 void MainWin::readSettings()
234
235    QSettings settings("bacula.org", "bat");
236
237    settings.beginGroup("MainWin");
238    resize(settings.value("winSize", QSize(1041, 801)).toSize());
239    move(settings.value("winPos", QPoint(200, 150)).toPoint());
240    settings.endGroup();
241 }
242
243 /*
244  * This subroutine is called with an item in the Page Selection window
245  *   is clicked 
246  */
247 void MainWin::treeItemClicked(QTreeWidgetItem *item, int /*column*/)
248 {
249    /* Is this one of the first level pages */
250    if( m_treeindex.value(item) ){
251       Pages* page = m_treeindex.value(item);
252       int stackindex=stackedWidget->indexOf(page);
253
254       if( stackindex >= 0 ){
255          stackedWidget->setCurrentIndex(0);
256          stackedWidget->setCurrentWidget(page);
257       }
258       /* run the virtual function in case this class overrides it */
259       page->PgSeltreeWidgetClicked();
260    }
261 }
262
263 /*
264  * This subroutine is called with an item in the Page Selection window
265  *   is double clicked
266  */
267 void MainWin::treeItemDoubleClicked(QTreeWidgetItem * /*item*/, int /*column*/)
268 {
269 }
270
271 /*
272  * Called with a change of the highlighed tree widget item in the page selector.
273  */
274 void MainWin::treeItemChanged(QTreeWidgetItem *currentitem, QTreeWidgetItem *previousitem)
275 {
276    /* The Previous item */
277
278    if ( previousitem ){
279       /* Is this one of the first level pages */
280       if( m_treeindex.value(previousitem) ){
281          Pages* page = m_treeindex.value(previousitem);
282          treeWidget->removeAction(actionToggleDock);
283          foreach( QAction* pageaction, page->m_contextActions ){
284             treeWidget->removeAction(pageaction);
285          } 
286       }
287    }
288
289    /* Is this one of the first level pages */
290    if( m_treeindex.value(currentitem) ){
291       Pages* page = m_treeindex.value(currentitem);
292       int stackindex = stackedWidget->indexOf(page);
293    
294       /* Is this page currently on the stack */
295       if( stackindex >= 0 ){
296          /* put this page on the top of the stack */
297          stackedWidget->setCurrentIndex(stackindex);
298       }
299       setContextMenuDockText(page, currentitem);
300
301       treeWidget->addAction(actionToggleDock);
302
303       /* Add the actions to the Page Selectors tree widget that are part of the
304        * current items list of desired actions regardless of whether on top of stack*/
305       treeWidget->addActions(page->m_contextActions);
306    }
307 }
308
309 void MainWin::labelDialogClicked() 
310 {
311    new labelDialog(m_console);
312 }
313
314 void MainWin::runDialogClicked() 
315 {
316    new runDialog(m_console);
317 }
318
319 void MainWin::restoreDialogClicked() 
320 {
321    new prerestoreDialog(m_console);
322 }
323
324
325
326 /*
327  * The user just finished typing a line in the command line edit box
328  */
329 void MainWin::input_line()
330 {
331    QString cmdStr = lineEdit->text();    /* Get the text */
332    lineEdit->clear();                    /* clear the lineEdit box */
333    if (m_console->is_connected()) {
334       m_console->display_text(cmdStr + "\n");
335       m_console->write_dir(cmdStr.toUtf8().data());         /* send to dir */
336    } else {
337       set_status("Director not connected. Click on connect button.");
338    }
339    m_cmd_history.append(cmdStr);
340    m_cmd_last = -1;
341 }
342
343
344 void MainWin::about()
345 {
346    QMessageBox::about(this, tr("About bat"),
347             tr("<br><h2>bat 0.2, by Kern Sibbald</h2>"
348             "<p>Copyright &copy; " BYEAR " Free Software Foundation Europe e.V."
349             "<p>The <b>bat</b> is an administrative console"
350                " interface to the Director."));
351 }
352
353 void MainWin::set_statusf(const char *fmt, ...)
354 {
355    va_list arg_ptr;
356    char buf[1000];
357    int len;
358    va_start(arg_ptr, fmt);
359    len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
360    va_end(arg_ptr);
361    set_status(buf);
362 }
363
364 void MainWin::set_status_ready()
365 {
366    set_status(" Ready");
367 }
368
369 void MainWin::set_status(const char *buf)
370 {
371    statusBar()->showMessage(buf);
372 }
373
374 /*
375  * Function to respond to the button bar button to undock
376  */
377 void MainWin::undockWindowButton()
378 {
379    Pages* page = (Pages*)stackedWidget->currentWidget();
380    page->togglePageDocking();
381    /* The window has been undocked, lets change the context menu */
382    setContextMenuDockText();
383 }
384
385 /*
386  * Function to respond to action on page selector context menu to toggle the 
387  * dock status of the window associated with the page selectors current
388  * tree widget item.
389  */
390 void MainWin::toggleDockContextWindow()
391 {
392    QTreeWidgetItem *currentitem = treeWidget->currentItem();
393    
394    /* Is this one of the first level pages */
395    if( m_treeindex.value(currentitem) ){
396       Pages* page = m_treeindex.value(currentitem);
397       page->togglePageDocking();
398       if ( page->isDocked() ){
399          stackedWidget->setCurrentWidget(page);
400       }
401       /* Toggle the menu item.  The window's dock status has been toggled */
402       setContextMenuDockText(page, currentitem);
403    }
404 }
405
406 /*
407  * Function to set the text of the toggle dock context menu when page and
408  * widget item are NOT known.  This is an overoaded funciton.
409  * It is called from MainWin::undockWindowButton, it is not intended to change
410  * for the top pages tree widget, it is for the currently active tree widget
411  * item.  Which is why the page is not passed.
412  */
413 void MainWin::setContextMenuDockText()
414 {
415    QTreeWidgetItem *currentitem = treeWidget->currentItem();
416    
417    /* Is this one of the first level pages */
418    if( m_treeindex.value(currentitem) ){
419       Pages* page = m_treeindex.value(currentitem);
420       setContextMenuDockText(page, currentitem);
421    }
422 }
423
424 /*
425  * Function to set the text of the toggle dock context menu when page and
426  * widget item are known.  This is the more commonly used.
427  */
428 void MainWin::setContextMenuDockText(Pages* page, QTreeWidgetItem* item)
429 {
430    QString docktext("");
431    if( page->isDocked() ){
432       docktext += "UnDock ";
433    } else {
434       docktext += "ReDock ";
435    }
436    docktext += item->text(0) += " Window";
437    
438    actionToggleDock->setText(docktext);
439    setTreeWidgetItemDockColor(page, item);
440 }
441
442 /*
443  * Function to set the color of the tree widget item based on whether it is
444  * docked or not.
445  */
446 void MainWin::setTreeWidgetItemDockColor(Pages* page, QTreeWidgetItem* item)
447 {
448    if( item->text(0) != "Console" ){
449       if( page->isDocked() ){
450       /* Set the brush to blue if undocked */
451          QBrush blackBrush(Qt::black);
452          item->setForeground(0, blackBrush);
453       } else {
454       /* Set the brush back to black if docked */
455          QBrush blueBrush(Qt::blue);
456          item->setForeground(0, blueBrush);
457       }
458    }
459 }
460
461 /*
462  *  Overload of previous function, use treeindex to get item from page
463  *  This is called when an undocked window is closed.
464  */
465 void MainWin::setTreeWidgetItemDockColor(Pages* page)
466 {
467    QTreeWidgetItem* item = m_treeindex.value(page);
468    if( item ){
469      setTreeWidgetItemDockColor(page, item);
470    }
471 }
472
473 /*
474  * This function is called when the stack item is changed.  Call
475  * the virtual function here.  Avoids a window being undocked leaving
476  * a window at the top of the stack unpopulated.
477  */
478 void MainWin::stackItemChanged(int)
479 {
480    Pages* page = (Pages*)stackedWidget->currentWidget();
481    /* run the virtual function in case this class overrides it */
482    page->currentStackItem();
483 }