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