]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/mainwin.cpp
dhb I've got most of the user interface issues working as I was hoping to see
[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    QTreeWidgetItem *item, *topItem;
69
70    /* Create console tree stacked widget item */
71    m_console = new Console(stackedWidget);
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    /* The top tree item representing the director */
79    topItem = createTopPage(dir->name());
80    topItem->setIcon(0, QIcon(QString::fromUtf8("images/server.png")));
81    /* Create Tree Widget Item */
82    item = createPage("Console", topItem);
83    m_console->setTreeItem(item);
84    /* Append to bstacklist */
85    m_bstacklist.append(m_console);
86    /* Set BatStack m_treeItem */
87    m_console->SetBSTreeWidgetItem(item);
88    QBrush redBrush(Qt::red);
89    item->setForeground(0, redBrush);
90
91    /* Now with the console created, on with the rest, these are easy */
92    /* All should be
93  * 1. create tree widget item
94  * 2. create object passing pointer to tree widget item (modified constructors to pass QTreeWidget pointers)
95  * 3. append to stacklist  */ 
96
97    /* brestore */
98    item = createPage("brestore", topItem);
99    m_brestore = new bRestore(stackedWidget,item);
100    m_bstacklist.append(m_brestore);
101
102    /* lastly for now, the medialist */
103    item = createPage("Storage Tree", topItem );
104    m_medialist = new MediaList(stackedWidget, m_console, item);
105    m_bstacklist.append(m_medialist);
106
107    /* Iterate through and add to the stack */
108    for ( QList<BatStack*>::iterator bstackItem = m_bstacklist.begin(); bstackItem != m_bstacklist.end(); ++bstackItem ) {
109       (*bstackItem)->AddTostack();
110    }
111
112    treeWidget->expandItem(topItem);
113    stackedWidget->setCurrentIndex(0);
114 }
115
116 /* Create a root Tree Widget */
117 QTreeWidgetItem *MainWin::createTopPage(char *name )
118 {
119    QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
120    item->setText(0, name);
121    return item;
122 }
123
124 /* Create A Tree Widget Item which will be associated with a Page in the stacked widget */
125 QTreeWidgetItem *MainWin::createPage(char *name, QTreeWidgetItem *parent)
126 {
127    QTreeWidgetItem *item = new QTreeWidgetItem(parent);
128    item->setText(0, name);
129    return item;
130 }
131
132 /*
133  * Handle up and down arrow keys for the command line
134  *  history.
135  */
136 void MainWin::keyPressEvent(QKeyEvent *event)
137 {
138    if (m_cmd_history.size() == 0) {
139       event->ignore();
140       return;
141    }
142    switch (event->key()) {
143    case Qt::Key_Down:
144       if (m_cmd_last < 0 || m_cmd_last >= (m_cmd_history.size()-1)) {
145          event->ignore();
146          return;
147       }
148       m_cmd_last++;
149       break;
150    case Qt::Key_Up:
151       if (m_cmd_last == 0) {
152          event->ignore();
153          return;
154       }
155       if (m_cmd_last < 0 || m_cmd_last > (m_cmd_history.size()-1)) {
156          m_cmd_last = m_cmd_history.size() - 1;
157       } else {
158          m_cmd_last--;
159       }
160       break;
161    default:
162       event->ignore();
163       return;
164    }
165    lineEdit->setText(m_cmd_history[m_cmd_last]);
166 }
167
168 void MainWin::createConnections()
169 {
170    /* Connect signals to slots */
171    connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(input_line()));
172    connect(actionAbout_bat, SIGNAL(triggered()), this, SLOT(about()));
173
174 /*   connect(treeWidget, SIGNAL(itemActivated(QTreeWidgetItem *, int)), this, 
175            SLOT(treeItemClicked(QTreeWidgetItem *, int)));
176    connect(treeWidget, SIGNAL(itemPressed(QTreeWidgetItem *, int)), this, 
177            SLOT(treeItemClicked(QTreeWidgetItem *, int)));  Commented out because it was getting to clicked multiple times*/
178    connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, 
179            SLOT(treeItemClicked(QTreeWidgetItem *, int)));
180    connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, 
181            SLOT(treeItemDoubleClicked(QTreeWidgetItem *, int)));
182
183    connect(actionQuit, SIGNAL(triggered()), app, SLOT(closeAllWindows()));
184    connect(actionConnect, SIGNAL(triggered()), m_console, SLOT(connect()));
185    connect(actionStatusDir, SIGNAL(triggered()), m_console, SLOT(status_dir()));
186    connect(actionSelectFont, SIGNAL(triggered()), m_console, SLOT(set_font()));
187    connect(actionLabel, SIGNAL(triggered()), this,  SLOT(labelDialogClicked()));
188    connect(actionRun, SIGNAL(triggered()), this,  SLOT(runDialogClicked()));
189    connect(actionRestore, SIGNAL(triggered()), this,  SLOT(restoreDialogClicked()));
190 }
191
192 /* 
193  * Reimplementation of QWidget closeEvent virtual function   
194  */
195 void MainWin::closeEvent(QCloseEvent *event)
196 {
197    writeSettings();
198    m_console->writeSettings();
199    m_console->terminate();
200    event->accept();
201 }
202
203 void MainWin::writeSettings()
204 {
205    QSettings settings("bacula.org", "bat");
206
207    settings.beginGroup("MainWin");
208    settings.setValue("winSize", size());
209    settings.setValue("winPos", pos());
210    settings.endGroup();
211 }
212
213 void MainWin::readSettings()
214
215    QSettings settings("bacula.org", "bat");
216
217    settings.beginGroup("MainWin");
218    resize(settings.value("winSize", QSize(1041, 801)).toSize());
219    move(settings.value("winPos", QPoint(200, 150)).toPoint());
220    settings.endGroup();
221 }
222
223 void MainWin::treeItemClicked(QTreeWidgetItem *item, int column)
224 {
225    /* Iterate through and find the tree widget item clicked */
226    column+=0;
227    QList<BatStack*>::iterator bstackItem;
228    bstackItem = m_bstacklist.begin();
229    while ( bstackItem != m_bstacklist.end() ){
230       if ( item == (*bstackItem)->m_treeItem ) {
231          int stackindex=stackedWidget->indexOf( *bstackItem );
232          if( stackindex >= 0 ){
233             stackedWidget->setCurrentIndex(stackindex);
234          }
235       }
236       ++bstackItem;
237    }
238 }
239
240 /*
241  */
242 void MainWin::treeItemDoubleClicked(QTreeWidgetItem *item, int column)
243 {
244    /* Iterate through and find the tree widget item double clicked */
245    column+=0;
246    QList<BatStack*>::iterator bstackItem;
247    bstackItem = m_bstacklist.begin();
248    while ( bstackItem != m_bstacklist.end() ){
249       if ( item == (*bstackItem)->m_treeItem ) {
250          /* This could be a call a virtual function ?? or just popup a popup menu for an action */
251          if ( (*bstackItem)->isStacked() == true ){
252             m_bstackpophold=*bstackItem;
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             (*bstackItem)->Togglestack();
258          }
259       }
260       ++bstackItem;
261    }
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 }