]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/mainwin.cpp
Tweak names
[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    treeWidget->addAction(actionPullWindowOut);
50    treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
51
52    m_pages = 0;
53    createPages();
54
55    resetFocus();
56
57    createConnections();
58
59    this->show();
60
61    readSettings();
62
63    m_console->connect();
64 }
65
66 void MainWin::createPages()
67 {
68    DIRRES *dir;
69    QTreeWidgetItem *item, *topItem;
70
71    /* Create console tree stacked widget item */
72    m_console = new Console(stackedWidget);
73
74    /* Console is special -> needs director*/
75    /* Just take the first Director */
76    LockRes();
77    dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
78    m_console->setDirRes(dir);
79    UnlockRes();
80
81    /* The top tree item representing the director */
82    topItem = createTopPage(dir->name());
83    topItem->setIcon(0, QIcon(QString::fromUtf8("images/server.png")));
84
85    /* Create Tree Widget Item */
86    item = createPage("Console", topItem);
87    m_console->SetPassedValues(stackedWidget, item, m_pages++ );
88
89    /* Append to bstacklist */
90    m_bstacklist.append(m_console);
91
92    /* Set BatStack m_treeItem */
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 stacklist
102     * And it can even be done in one line.
103     */
104
105    /* brestore */
106    m_bstacklist.append(new bRestore(stackedWidget, 
107                        createPage("brestore", topItem), m_pages++ ));
108
109    /* lastly for now, the medialist */
110    m_bstacklist.append(new MediaList(stackedWidget, m_console, 
111                        createPage("Storage Tree", topItem ), m_pages++));
112
113    /* Iterate through and add to the stack */
114    for (QList<BatStack*>::iterator bstackItem = m_bstacklist.begin(); 
115          bstackItem != m_bstacklist.end(); ++bstackItem ) {
116       (*bstackItem)->AddTostack();
117    }
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
192    connect(actionQuit, SIGNAL(triggered()), app, SLOT(closeAllWindows()));
193    connect(actionConnect, SIGNAL(triggered()), m_console, SLOT(connect()));
194    connect(actionStatusDir, SIGNAL(triggered()), m_console, SLOT(status_dir()));
195    connect(actionSelectFont, SIGNAL(triggered()), m_console, SLOT(set_font()));
196    connect(actionLabel, SIGNAL(triggered()), this,  SLOT(labelDialogClicked()));
197    connect(actionRun, SIGNAL(triggered()), this,  SLOT(runDialogClicked()));
198    connect(actionRestore, SIGNAL(triggered()), this,  SLOT(restoreDialogClicked()));
199    connect(actionPullWindowOut, SIGNAL(triggered()), this,  SLOT(floatWindowButton()));
200 }
201
202 /* 
203  * Reimplementation of QWidget closeEvent virtual function   
204  */
205 void MainWin::closeEvent(QCloseEvent *event)
206 {
207    writeSettings();
208    m_console->writeSettings();
209    m_console->terminate();
210    event->accept();
211 }
212
213 void MainWin::writeSettings()
214 {
215    QSettings settings("bacula.org", "bat");
216
217    settings.beginGroup("MainWin");
218    settings.setValue("winSize", size());
219    settings.setValue("winPos", pos());
220    settings.endGroup();
221 }
222
223 void MainWin::readSettings()
224
225    QSettings settings("bacula.org", "bat");
226
227    settings.beginGroup("MainWin");
228    resize(settings.value("winSize", QSize(1041, 801)).toSize());
229    move(settings.value("winPos", QPoint(200, 150)).toPoint());
230    settings.endGroup();
231 }
232
233 /*
234  * This subroutine is called with an item in the Page Selection window
235  *   is clicked 
236  */
237 void MainWin::treeItemClicked(QTreeWidgetItem *item, int column)
238 {
239    /* Use tree item's Qt::UserRole to get treeindex */
240    int treeindex = item->data(column, Qt::UserRole).toInt();
241    int stackindex=stackedWidget->indexOf(m_bstacklist[treeindex]);
242
243    if( stackindex >= 0 ){
244       stackedWidget->setCurrentIndex(stackindex);
245    }
246    /* run the virtual function in case this class overrides it */
247    m_bstacklist[treeindex]->PgSeltreeWidgetClicked();
248 }
249
250 /*
251  * This subroutine is called with an item in the Page Selection window
252  *   is double clicked
253  */
254 void MainWin::treeItemDoubleClicked(QTreeWidgetItem *item, int column)
255 {
256    int treeindex = item->data(column, Qt::UserRole).toInt();
257
258    /* Use tree item's Qt::UserRole to get treeindex */
259    if ( m_bstacklist[treeindex]->isStacked() == true ){
260       m_bstackpophold=m_bstacklist[treeindex];
261
262       /* Create a popup menu before floating window */
263       QMenu *popup = new QMenu( treeWidget );
264       connect(popup->addAction("Float Window"), SIGNAL(triggered()), this, 
265               SLOT(floatWindow()));
266       popup->exec(QCursor::pos());
267    } else {
268       /* Just pull it back in without prompting */
269       m_bstacklist[treeindex]->Togglestack();
270    }
271    /* Here is the virtual function so that different classes can do different things */
272    m_bstacklist[treeindex]->PgSeltreeWidgetDoubleClicked();
273 }
274
275 void MainWin::labelDialogClicked() 
276 {
277    new labelDialog(m_console);
278 }
279
280 void MainWin::runDialogClicked() 
281 {
282    new runDialog(m_console);
283 }
284
285 void MainWin::restoreDialogClicked() 
286 {
287    new prerestoreDialog(m_console);
288 }
289
290
291
292 /*
293  * The user just finished typing a line in the command line edit box
294  */
295 void MainWin::input_line()
296 {
297    QString cmdStr = lineEdit->text();    /* Get the text */
298    lineEdit->clear();                    /* clear the lineEdit box */
299    if (m_console->is_connected()) {
300       m_console->display_text(cmdStr + "\n");
301       m_console->write_dir(cmdStr.toUtf8().data());         /* send to dir */
302    } else {
303       set_status("Director not connected. Click on connect button.");
304    }
305    m_cmd_history.append(cmdStr);
306    m_cmd_last = -1;
307 }
308
309
310 void MainWin::about()
311 {
312    QMessageBox::about(this, tr("About bat"),
313             tr("<br><h2>bat 0.2, by Kern Sibbald</h2>"
314             "<p>Copyright &copy; " BYEAR " Free Software Foundation Europe e.V."
315             "<p>The <b>bat</b> is an administrative console"
316                " interface to the Director."));
317 }
318
319 void MainWin::set_statusf(const char *fmt, ...)
320 {
321    va_list arg_ptr;
322    char buf[1000];
323    int len;
324    va_start(arg_ptr, fmt);
325    len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
326    va_end(arg_ptr);
327    set_status(buf);
328 }
329
330 void MainWin::set_status_ready()
331 {
332    set_status(" Ready");
333 }
334
335 void MainWin::set_status(const char *buf)
336 {
337    statusBar()->showMessage(buf);
338 }
339
340 void MainWin::floatWindow()
341 {
342    m_bstackpophold->Togglestack();
343 }
344
345 void MainWin::floatWindowButton()
346 {
347    int curindex = stackedWidget->currentIndex();
348    QList<BatStack*>::iterator bstackItem = m_bstacklist.begin();
349    
350    while ((bstackItem != m_bstacklist.end())){
351       if (curindex == stackedWidget->indexOf(*bstackItem)) {
352           (*bstackItem)->Togglestack();
353          break;
354       }
355       ++bstackItem;
356    }
357 }