]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/mainwin.cpp
dhb left console.cpp uncompileable. Modified a comment in mainwin.cpp
[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
40 MainWin::MainWin(QWidget *parent) : QMainWindow(parent)
41 {
42
43    mainWin = this;
44    setupUi(this);                     /* Setup UI defined by main.ui (designer) */
45    treeWidget->clear();
46    treeWidget->setColumnCount(1);
47    treeWidget->setHeaderLabel("Select Page");
48    treeWidget->addAction(actionPullWindowOut);
49    treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
50
51    m_pages = 0;
52    createPages();
53
54    resetFocus();
55
56    createConnections();
57
58    this->show();
59
60    readSettings();
61
62    m_console->connect();
63 }
64
65 void MainWin::createPages()
66 {
67    DIRRES *dir;
68    QTreeWidgetItem *item, *topItem;
69
70    /* Create console tree stacked widget item */
71    m_console = new Console(stackedWidget);
72
73    /* Console is special -> needs director*/
74    /* Just take the first Director */
75    LockRes();
76    dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
77    m_console->setDirRes(dir);
78    UnlockRes();
79
80    /* The top tree item representing the director */
81    topItem = createTopPage(dir->name());
82    topItem->setIcon(0, QIcon(QString::fromUtf8("images/server.png")));
83
84    /* Create Tree Widget Item */
85    item = createPage("Console", topItem);
86    m_console->SetPassedValues(stackedWidget, item, m_pages++ );
87
88    /* Append to pageslist */
89    m_pageslist.append(m_console);
90
91    /* Set Color of treeWidgetItem for the console
92    * It will be set to gree in the console class if the connection is made.
93    */
94    QBrush redBrush(Qt::red);
95    item->setForeground(0, redBrush);
96
97    /*
98     * Now with the console created, on with the rest, these are easy   
99     * All should be
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.
104     */
105
106    /* brestore */
107    m_pageslist.append(new bRestore(stackedWidget,
108                       createPage("brestore", topItem), m_pages++ ));
109
110
111    /* lastly for now, the medialist */
112    m_pageslist.append(new MediaList(stackedWidget, m_console, 
113                        createPage("Storage Tree", topItem ), m_pages++));
114
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();
119    }
120
121    treeWidget->expandItem(topItem);
122    stackedWidget->setCurrentIndex(0);
123 }
124
125 /* Create a root Tree Widget */
126 QTreeWidgetItem *MainWin::createTopPage(char *name)
127 {
128    QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
129    item->setText(0, name);
130    return item;
131 }
132
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)
135 {
136    QTreeWidgetItem *item = new QTreeWidgetItem(parent);
137    item->setText(0, name);
138    return item;
139 }
140
141 /*
142  * Handle up and down arrow keys for the command line
143  *  history.
144  */
145 void MainWin::keyPressEvent(QKeyEvent *event)
146 {
147    if (m_cmd_history.size() == 0) {
148       event->ignore();
149       return;
150    }
151    switch (event->key()) {
152    case Qt::Key_Down:
153       if (m_cmd_last < 0 || m_cmd_last >= (m_cmd_history.size()-1)) {
154          event->ignore();
155          return;
156       }
157       m_cmd_last++;
158       break;
159    case Qt::Key_Up:
160       if (m_cmd_last == 0) {
161          event->ignore();
162          return;
163       }
164       if (m_cmd_last < 0 || m_cmd_last > (m_cmd_history.size()-1)) {
165          m_cmd_last = m_cmd_history.size() - 1;
166       } else {
167          m_cmd_last--;
168       }
169       break;
170    default:
171       event->ignore();
172       return;
173    }
174    lineEdit->setText(m_cmd_history[m_cmd_last]);
175 }
176
177 void MainWin::createConnections()
178 {
179    /* Connect signals to slots */
180    connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(input_line()));
181    connect(actionAbout_bat, SIGNAL(triggered()), this, SLOT(about()));
182
183 #ifdef xxx
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)));  
188 #endif
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)));
193
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()));
202 }
203
204 /* 
205  * Reimplementation of QWidget closeEvent virtual function   
206  */
207 void MainWin::closeEvent(QCloseEvent *event)
208 {
209    writeSettings();
210    m_console->writeSettings();
211    m_console->terminate();
212    event->accept();
213 }
214
215 void MainWin::writeSettings()
216 {
217    QSettings settings("bacula.org", "bat");
218
219    settings.beginGroup("MainWin");
220    settings.setValue("winSize", size());
221    settings.setValue("winPos", pos());
222    settings.endGroup();
223 }
224
225 void MainWin::readSettings()
226
227    QSettings settings("bacula.org", "bat");
228
229    settings.beginGroup("MainWin");
230    resize(settings.value("winSize", QSize(1041, 801)).toSize());
231    move(settings.value("winPos", QPoint(200, 150)).toPoint());
232    settings.endGroup();
233 }
234
235 /*
236  * This subroutine is called with an item in the Page Selection window
237  *   is clicked 
238  */
239 void MainWin::treeItemClicked(QTreeWidgetItem *item, int column)
240 {
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]);
244
245    if( stackindex >= 0 ){
246       stackedWidget->setCurrentIndex(stackindex);
247    }
248    /* run the virtual function in case this class overrides it */
249    m_pageslist[treeindex]->PgSeltreeWidgetClicked();
250 }
251
252 /*
253  * This subroutine is called with an item in the Page Selection window
254  *   is double clicked
255  */
256 void MainWin::treeItemDoubleClicked(QTreeWidgetItem *item, int column)
257 {
258    int treeindex = item->data(column, Qt::UserRole).toInt();
259
260    /* Use tree item's Qt::UserRole to get treeindex */
261    if (m_pageslist[treeindex]->isDocked()) {
262       m_pagespophold = m_pageslist[treeindex];
263
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());
269    } else {
270       /* Just pull it back in without prompting */
271       m_pageslist[treeindex]->togglePageDocking();
272    }
273    /* Here is the virtual function so that different classes can do different things */
274    m_pageslist[treeindex]->PgSeltreeWidgetDoubleClicked();
275 }
276
277 void MainWin::labelDialogClicked() 
278 {
279    new labelDialog(m_console);
280 }
281
282 void MainWin::runDialogClicked() 
283 {
284    new runDialog(m_console);
285 }
286
287 void MainWin::restoreDialogClicked() 
288 {
289    new prerestoreDialog(m_console);
290 }
291
292
293
294 /*
295  * The user just finished typing a line in the command line edit box
296  */
297 void MainWin::input_line()
298 {
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 */
304    } else {
305       set_status("Director not connected. Click on connect button.");
306    }
307    m_cmd_history.append(cmdStr);
308    m_cmd_last = -1;
309 }
310
311
312 void MainWin::about()
313 {
314    QMessageBox::about(this, tr("About bat"),
315             tr("<br><h2>bat 0.2, by Kern Sibbald</h2>"
316             "<p>Copyright &copy; " BYEAR " Free Software Foundation Europe e.V."
317             "<p>The <b>bat</b> is an administrative console"
318                " interface to the Director."));
319 }
320
321 void MainWin::set_statusf(const char *fmt, ...)
322 {
323    va_list arg_ptr;
324    char buf[1000];
325    int len;
326    va_start(arg_ptr, fmt);
327    len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
328    va_end(arg_ptr);
329    set_status(buf);
330 }
331
332 void MainWin::set_status_ready()
333 {
334    set_status(" Ready");
335 }
336
337 void MainWin::set_status(const char *buf)
338 {
339    statusBar()->showMessage(buf);
340 }
341
342 void MainWin::undockWindow()
343 {
344    m_pagespophold->togglePageDocking();
345 }
346
347 void MainWin::undockWindowButton()
348 {
349    int curindex = stackedWidget->currentIndex();
350    QList<Pages *>::iterator pagesItem = m_pageslist.begin();
351    
352    while ((pagesItem != m_pageslist.end())){
353       if (curindex == stackedWidget->indexOf(*pagesItem)) {
354           (*pagesItem)->togglePageDocking();
355          break;
356       }
357       ++pagesItem;
358    }
359 }