]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/mainwin.cpp
3400c83760217a0b7c214ada0650961d6fcba087
[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 /*   connect(treeWidget, SIGNAL(itemActivated(QTreeWidgetItem *, int)), this, 
182            SLOT(treeItemClicked(QTreeWidgetItem *, int)));
183    connect(treeWidget, SIGNAL(itemPressed(QTreeWidgetItem *, int)), this, 
184            SLOT(treeItemClicked(QTreeWidgetItem *, int)));  Commented out because it was getting to clicked multiple times*/
185    connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, 
186            SLOT(treeItemClicked(QTreeWidgetItem *, int)));
187    connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, 
188            SLOT(treeItemDoubleClicked(QTreeWidgetItem *, int)));
189
190    connect(actionQuit, SIGNAL(triggered()), app, SLOT(closeAllWindows()));
191    connect(actionConnect, SIGNAL(triggered()), m_console, SLOT(connect()));
192    connect(actionStatusDir, SIGNAL(triggered()), m_console, SLOT(status_dir()));
193    connect(actionSelectFont, SIGNAL(triggered()), m_console, SLOT(set_font()));
194    connect(actionLabel, SIGNAL(triggered()), this,  SLOT(labelDialogClicked()));
195    connect(actionRun, SIGNAL(triggered()), this,  SLOT(runDialogClicked()));
196    connect(actionRestore, SIGNAL(triggered()), this,  SLOT(restoreDialogClicked()));
197    connect(actionPullWindowOut, SIGNAL(triggered()), this,  SLOT(pullWindowOutButton()));
198 }
199
200 /* 
201  * Reimplementation of QWidget closeEvent virtual function   
202  */
203 void MainWin::closeEvent(QCloseEvent *event)
204 {
205    writeSettings();
206    m_console->writeSettings();
207    m_console->terminate();
208    event->accept();
209 }
210
211 void MainWin::writeSettings()
212 {
213    QSettings settings("bacula.org", "bat");
214
215    settings.beginGroup("MainWin");
216    settings.setValue("winSize", size());
217    settings.setValue("winPos", pos());
218    settings.endGroup();
219 }
220
221 void MainWin::readSettings()
222
223    QSettings settings("bacula.org", "bat");
224
225    settings.beginGroup("MainWin");
226    resize(settings.value("winSize", QSize(1041, 801)).toSize());
227    move(settings.value("winPos", QPoint(200, 150)).toPoint());
228    settings.endGroup();
229 }
230
231 void MainWin::treeItemClicked(QTreeWidgetItem *item, int column)
232 {
233    (void)column;
234
235    /* Use tree item's Qt::UserRole to get treeindex */
236    int treeindex = item->data(column, Qt::UserRole).toInt();
237    int stackindex=stackedWidget->indexOf(m_bstacklist[treeindex]);
238
239    if( stackindex >= 0 ){
240       stackedWidget->setCurrentIndex(stackindex);
241    }
242    /* run the virtual function in case this class overrides it */
243    m_bstacklist[treeindex]->PgSeltreeWidgetClicked();
244 }
245
246 /*
247  */
248 void MainWin::treeItemDoubleClicked(QTreeWidgetItem *item, int column)
249 {
250    (void)column;
251    int treeindex = item->data(column, Qt::UserRole).toInt();
252
253    /* Use tree item's Qt::UserRole to get treeindex */
254    if ( m_bstacklist[treeindex]->isStacked() == true ){
255       m_bstackpophold=m_bstacklist[treeindex];
256       /* Create a popup menu before pulling window out */
257       QMenu *popup = new QMenu( treeWidget );
258       connect(popup->addAction("Pull Window Out"), SIGNAL(triggered()), this, SLOT(pullWindowOut()));
259       popup->exec(QCursor::pos());
260    } else {
261       /* Just pull it back in without prompting */
262       m_bstacklist[treeindex]->Togglestack();
263    }
264    /* Here is the virtual function so that different classes can do different things */
265    m_bstacklist[treeindex]->PgSeltreeWidgetDoubleClicked();
266 }
267
268 void MainWin::labelDialogClicked() 
269 {
270    new labelDialog(m_console);
271 }
272
273 void MainWin::runDialogClicked() 
274 {
275    new runDialog(m_console);
276 }
277
278 void MainWin::restoreDialogClicked() 
279 {
280    new prerestoreDialog(m_console);
281 }
282
283
284
285 /*
286  * The user just finished typing a line in the command line edit box
287  */
288 void MainWin::input_line()
289 {
290    QString cmdStr = lineEdit->text();    /* Get the text */
291    lineEdit->clear();                    /* clear the lineEdit box */
292    if (m_console->is_connected()) {
293       m_console->display_text(cmdStr + "\n");
294       m_console->write_dir(cmdStr.toUtf8().data());         /* send to dir */
295    } else {
296       set_status("Director not connected. Click on connect button.");
297    }
298    m_cmd_history.append(cmdStr);
299    m_cmd_last = -1;
300 }
301
302
303 void MainWin::about()
304 {
305    QMessageBox::about(this, tr("About bat"),
306             tr("<br><h2>bat 0.2, by Kern Sibbald</h2>"
307             "<p>Copyright &copy; " BYEAR " Free Software Foundation Europe e.V."
308             "<p>The <b>bat</b> is an administrative console"
309                " interface to the Director."));
310 }
311
312 void MainWin::set_statusf(const char *fmt, ...)
313 {
314    va_list arg_ptr;
315    char buf[1000];
316    int len;
317    va_start(arg_ptr, fmt);
318    len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
319    va_end(arg_ptr);
320    set_status(buf);
321 }
322
323 void MainWin::set_status_ready()
324 {
325    set_status(" Ready");
326 }
327
328 void MainWin::set_status(const char *buf)
329 {
330    statusBar()->showMessage(buf);
331 }
332
333 void MainWin::pullWindowOut()
334 {
335    m_bstackpophold->Togglestack();
336 }
337
338 void MainWin::pullWindowOutButton()
339 {
340    int curindex = stackedWidget->currentIndex();
341    QList<BatStack*>::iterator bstackItem = m_bstacklist.begin();
342    
343    while ((bstackItem != m_bstacklist.end())){
344       if (curindex == stackedWidget->indexOf(*bstackItem)) {
345           (*bstackItem)->Togglestack();
346          break;
347       }
348       ++bstackItem;
349    }
350 }