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