]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/mainwin.cpp
Small cleanup in qt-console
[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 }
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->SetPassedValues(stackedWidget, item, m_pages++ );
86
87    /* Append to bstacklist */
88    m_bstacklist.append(m_console);
89
90    /* Set BatStack m_treeItem */
91    QBrush redBrush(Qt::red);
92    item->setForeground(0, redBrush);
93
94    /*
95     * Now with the console created, on with the rest, these are easy   
96     * All should be
97     * 1. create tree widget item
98     * 2. create object passing pointer to tree widget item (modified constructors to pass QTreeWidget pointers)
99     * 3. append to stacklist
100     * And it can even be done in one line.
101     */
102
103    /* brestore */
104    m_bstacklist.append(new bRestore(stackedWidget, 
105                        createPage("brestore", topItem), m_pages++ ));
106
107    /* lastly for now, the medialist */
108    m_bstacklist.append(new MediaList(stackedWidget, m_console, 
109                        createPage("Storage Tree", topItem ), m_pages++));
110
111    /* Iterate through and add to the stack */
112    for (QList<BatStack*>::iterator bstackItem = m_bstacklist.begin(); 
113          bstackItem != m_bstacklist.end(); ++bstackItem ) {
114       (*bstackItem)->AddTostack();
115    }
116
117    treeWidget->expandItem(topItem);
118    stackedWidget->setCurrentIndex(0);
119 }
120
121 /* Create a root Tree Widget */
122 QTreeWidgetItem *MainWin::createTopPage(char *name )
123 {
124    QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
125    item->setText(0, name);
126    return item;
127 }
128
129 /* Create A Tree Widget Item which will be associated with a Page in the stacked widget */
130 QTreeWidgetItem *MainWin::createPage(char *name, QTreeWidgetItem *parent)
131 {
132    QTreeWidgetItem *item = new QTreeWidgetItem(parent);
133    item->setText(0, name);
134    return item;
135 }
136
137 /*
138  * Handle up and down arrow keys for the command line
139  *  history.
140  */
141 void MainWin::keyPressEvent(QKeyEvent *event)
142 {
143    if (m_cmd_history.size() == 0) {
144       event->ignore();
145       return;
146    }
147    switch (event->key()) {
148    case Qt::Key_Down:
149       if (m_cmd_last < 0 || m_cmd_last >= (m_cmd_history.size()-1)) {
150          event->ignore();
151          return;
152       }
153       m_cmd_last++;
154       break;
155    case Qt::Key_Up:
156       if (m_cmd_last == 0) {
157          event->ignore();
158          return;
159       }
160       if (m_cmd_last < 0 || m_cmd_last > (m_cmd_history.size()-1)) {
161          m_cmd_last = m_cmd_history.size() - 1;
162       } else {
163          m_cmd_last--;
164       }
165       break;
166    default:
167       event->ignore();
168       return;
169    }
170    lineEdit->setText(m_cmd_history[m_cmd_last]);
171 }
172
173 void MainWin::createConnections()
174 {
175    /* Connect signals to slots */
176    connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(input_line()));
177    connect(actionAbout_bat, SIGNAL(triggered()), this, SLOT(about()));
178
179 /*   connect(treeWidget, SIGNAL(itemActivated(QTreeWidgetItem *, int)), this, 
180            SLOT(treeItemClicked(QTreeWidgetItem *, int)));
181    connect(treeWidget, SIGNAL(itemPressed(QTreeWidgetItem *, int)), this, 
182            SLOT(treeItemClicked(QTreeWidgetItem *, int)));  Commented out because it was getting to clicked multiple times*/
183    connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, 
184            SLOT(treeItemClicked(QTreeWidgetItem *, int)));
185    connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, 
186            SLOT(treeItemDoubleClicked(QTreeWidgetItem *, int)));
187
188    connect(actionQuit, SIGNAL(triggered()), app, SLOT(closeAllWindows()));
189    connect(actionConnect, SIGNAL(triggered()), m_console, SLOT(connect()));
190    connect(actionStatusDir, SIGNAL(triggered()), m_console, SLOT(status_dir()));
191    connect(actionSelectFont, SIGNAL(triggered()), m_console, SLOT(set_font()));
192    connect(actionLabel, SIGNAL(triggered()), this,  SLOT(labelDialogClicked()));
193    connect(actionRun, SIGNAL(triggered()), this,  SLOT(runDialogClicked()));
194    connect(actionRestore, SIGNAL(triggered()), this,  SLOT(restoreDialogClicked()));
195    connect(actionPullWindowOut, SIGNAL(triggered()), this,  SLOT(pullWindowOutButton()));
196 }
197
198 /* 
199  * Reimplementation of QWidget closeEvent virtual function   
200  */
201 void MainWin::closeEvent(QCloseEvent *event)
202 {
203    writeSettings();
204    m_console->writeSettings();
205    m_console->terminate();
206    event->accept();
207 }
208
209 void MainWin::writeSettings()
210 {
211    QSettings settings("bacula.org", "bat");
212
213    settings.beginGroup("MainWin");
214    settings.setValue("winSize", size());
215    settings.setValue("winPos", pos());
216    settings.endGroup();
217 }
218
219 void MainWin::readSettings()
220
221    QSettings settings("bacula.org", "bat");
222
223    settings.beginGroup("MainWin");
224    resize(settings.value("winSize", QSize(1041, 801)).toSize());
225    move(settings.value("winPos", QPoint(200, 150)).toPoint());
226    settings.endGroup();
227 }
228
229 void MainWin::treeItemClicked(QTreeWidgetItem *item, int column)
230 {
231    column+=0;
232    /* Use tree item's Qt::UserRole to get treeindex */
233    int treeindex = item->data(column, Qt::UserRole).toInt();
234    int stackindex=stackedWidget->indexOf( m_bstacklist[treeindex] );
235    if( stackindex >= 0 ){
236       stackedWidget->setCurrentIndex(stackindex);
237    }
238    /* run the virtual function in case this class overrides it */
239    m_bstacklist[treeindex]->PgSeltreeWidgetClicked();
240 }
241
242 /*
243  */
244 void MainWin::treeItemDoubleClicked(QTreeWidgetItem *item, int column)
245 {
246    (void)column;
247    int treeindex = item->data(column, Qt::UserRole).toInt();
248
249    /* Use tree item's Qt::UserRole to get treeindex */
250    if ( m_bstacklist[treeindex]->isStacked() == true ){
251       m_bstackpophold=m_bstacklist[treeindex];
252       /* Create a popup menu before pulling window out */
253       QMenu *popup = new QMenu( treeWidget );
254       connect(popup->addAction("Pull Window Out"), SIGNAL(triggered()), this, SLOT(pullWindowOut()));
255       popup->exec(QCursor::pos());
256    } else {
257       /* Just pull it back in without prompting */
258       m_bstacklist[treeindex]->Togglestack();
259    }
260    /* Here is the virtual function so that different classes can do different things */
261    m_bstacklist[treeindex]->PgSeltreeWidgetDoubleClicked();
262 }
263
264 void MainWin::labelDialogClicked() 
265 {
266    new labelDialog(m_console);
267 }
268
269 void MainWin::runDialogClicked() 
270 {
271    new runDialog(m_console);
272 }
273
274 void MainWin::restoreDialogClicked() 
275 {
276    new prerestoreDialog(m_console);
277 }
278
279
280
281 /*
282  * The user just finished typing a line in the command line edit box
283  */
284 void MainWin::input_line()
285 {
286    QString cmdStr = lineEdit->text();    /* Get the text */
287    lineEdit->clear();                    /* clear the lineEdit box */
288    if (m_console->is_connected()) {
289       m_console->display_text(cmdStr + "\n");
290       m_console->write_dir(cmdStr.toUtf8().data());         /* send to dir */
291    } else {
292       set_status("Director not connected. Click on connect button.");
293    }
294    m_cmd_history.append(cmdStr);
295    m_cmd_last = -1;
296 }
297
298
299 void MainWin::about()
300 {
301    QMessageBox::about(this, tr("About bat"),
302             tr("<br><h2>bat 0.2, by Kern Sibbald</h2>"
303             "<p>Copyright &copy; " BYEAR " Free Software Foundation Europe e.V."
304             "<p>The <b>bat</b> is an administrative console"
305                " interface to the Director."));
306 }
307
308 void MainWin::set_statusf(const char *fmt, ...)
309 {
310    va_list arg_ptr;
311    char buf[1000];
312    int len;
313    va_start(arg_ptr, fmt);
314    len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
315    va_end(arg_ptr);
316    set_status(buf);
317 }
318
319 void MainWin::set_status_ready()
320 {
321    set_status(" Ready");
322 }
323
324 void MainWin::set_status(const char *buf)
325 {
326    statusBar()->showMessage(buf);
327 }
328
329 void MainWin::pullWindowOut()
330 {
331    m_bstackpophold->Togglestack();
332 }
333
334 void MainWin::pullWindowOutButton()
335 {
336    int curindex = stackedWidget->currentIndex();
337    QList<BatStack*>::iterator bstackItem = m_bstacklist.begin();
338    
339    while ((bstackItem != m_bstacklist.end())){
340       if (curindex == stackedWidget->indexOf(*bstackItem)) {
341           (*bstackItem)->Togglestack();
342          break;
343       }
344       ++bstackItem;
345    }
346 }