]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/mainwin.cpp
dhb add a test program directory and an example from the qt
[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
69    QTreeWidgetItem *item, *topItem;
70    m_console = new Console(stackedWidget);
71    stackedWidget->addWidget(m_console);
72
73    bRestore *brestore = new bRestore(stackedWidget);
74    stackedWidget->addWidget(brestore);
75
76    m_medialist = new MediaList(stackedWidget, m_console);
77    stackedWidget->addWidget(m_medialist);
78
79    /* Just take the first Director */
80    LockRes();
81    dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
82    m_console->setDirRes(dir);
83    UnlockRes();
84
85    topItem = createTopPage(dir->name(), false);
86    topItem->setIcon(0, QIcon(QString::fromUtf8("images/server.png")));
87
88    item = createPage("Console", topItem, true);
89    m_console->setTreeItem(item);
90    QBrush redBrush(Qt::red);
91    item->setForeground(0, redBrush);
92
93    item = createPage("brestore", topItem, true);
94    item = createPage("MediaList", topItem, true);
95
96    treeWidget->expandItem(topItem);
97
98    stackedWidget->setCurrentIndex(0);
99 }
100
101 QTreeWidgetItem *MainWin::createTopPage(char *name, bool canDisplay)
102 {
103    QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
104    item->setText(0, name);
105    if (canDisplay) {
106       item->setData(0, Qt::UserRole, QVariant(m_pages++));
107    } else {
108       item->setData(0, Qt::UserRole, QVariant(-1));
109    }
110    return item;
111 }
112
113 QTreeWidgetItem *MainWin::createPage(char *name, QTreeWidgetItem *parent, bool canDisplay)
114 {
115    QTreeWidgetItem *item = new QTreeWidgetItem(parent);
116    item->setText(0, name);
117    if (canDisplay) {
118       item->setData(0, Qt::UserRole, QVariant(m_pages++));
119    } else {
120       item->setData(0, Qt::UserRole, QVariant(-1));
121    }
122    return item;
123 }
124
125
126 /*
127  * Handle up and down arrow keys for the command line
128  *  history.
129  */
130 void MainWin::keyPressEvent(QKeyEvent *event)
131 {
132    if (m_cmd_history.size() == 0) {
133       event->ignore();
134       return;
135    }
136    switch (event->key()) {
137    case Qt::Key_Down:
138       if (m_cmd_last < 0 || m_cmd_last >= (m_cmd_history.size()-1)) {
139          event->ignore();
140          return;
141       }
142       m_cmd_last++;
143       break;
144    case Qt::Key_Up:
145       if (m_cmd_last == 0) {
146          event->ignore();
147          return;
148       }
149       if (m_cmd_last < 0 || m_cmd_last > (m_cmd_history.size()-1)) {
150          m_cmd_last = m_cmd_history.size() - 1;
151       } else {
152          m_cmd_last--;
153       }
154       break;
155    default:
156       event->ignore();
157       return;
158    }
159    lineEdit->setText(m_cmd_history[m_cmd_last]);
160 }
161
162 void MainWin::createConnections()
163 {
164    /* Connect signals to slots */
165    connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(input_line()));
166    connect(actionAbout_bat, SIGNAL(triggered()), this, SLOT(about()));
167
168    connect(treeWidget, SIGNAL(itemActivated(QTreeWidgetItem *, int)), this, 
169            SLOT(treeItemClicked(QTreeWidgetItem *, int)));
170    connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, 
171            SLOT(treeItemClicked(QTreeWidgetItem *, int)));
172    connect(treeWidget, SIGNAL(itemPressed(QTreeWidgetItem *, int)), this, 
173            SLOT(treeItemClicked(QTreeWidgetItem *, int)));
174    connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, 
175            SLOT(treeItemDoubleClicked(QTreeWidgetItem *, int)));
176
177    connect(actionQuit, SIGNAL(triggered()), app, SLOT(closeAllWindows()));
178    connect(actionConnect, SIGNAL(triggered()), m_console, SLOT(connect()));
179    connect(actionStatusDir, SIGNAL(triggered()), m_console, SLOT(status_dir()));
180    connect(actionSelectFont, SIGNAL(triggered()), m_console, SLOT(set_font()));
181    connect(actionLabel, SIGNAL(triggered()), this,  SLOT(labelDialogClicked()));
182    connect(actionRun, SIGNAL(triggered()), this,  SLOT(runDialogClicked()));
183    connect(actionRestore, SIGNAL(triggered()), this,  SLOT(restoreDialogClicked()));
184 }
185
186 /* 
187  * Reimplementation of QWidget closeEvent virtual function   
188  */
189 void MainWin::closeEvent(QCloseEvent *event)
190 {
191    writeSettings();
192    m_console->writeSettings();
193    m_console->terminate();
194    event->accept();
195 }
196
197 void MainWin::writeSettings()
198 {
199    QSettings settings("bacula.org", "bat");
200
201    settings.beginGroup("MainWin");
202    settings.setValue("winSize", size());
203    settings.setValue("winPos", pos());
204    settings.endGroup();
205 }
206
207 void MainWin::readSettings()
208
209    QSettings settings("bacula.org", "bat");
210
211    settings.beginGroup("MainWin");
212    resize(settings.value("winSize", QSize(1041, 801)).toSize());
213    move(settings.value("winPos", QPoint(200, 150)).toPoint());
214    settings.endGroup();
215 }
216
217 void MainWin::treeItemClicked(QTreeWidgetItem *item, int column)
218 {
219    int index = item->data(column, Qt::UserRole).toInt();
220    if (index >= 0) {
221       stackedWidget->setCurrentIndex(index);
222    }
223 }
224
225 /*
226  */
227 void MainWin::treeItemDoubleClicked(QTreeWidgetItem *item, int column)
228 {
229    int index = item->data(column, Qt::UserRole).toInt();
230    if (index >= 0) {
231       stackedWidget->setCurrentIndex(index);
232    }
233 }
234
235 void MainWin::labelDialogClicked() 
236 {
237    new labelDialog(m_console);
238 }
239
240 void MainWin::runDialogClicked() 
241 {
242    new runDialog(m_console);
243 }
244
245 void MainWin::restoreDialogClicked() 
246 {
247    new prerestoreDialog(m_console);
248 }
249
250
251
252 /*
253  * The user just finished typing a line in the command line edit box
254  */
255 void MainWin::input_line()
256 {
257    QString cmdStr = lineEdit->text();    /* Get the text */
258    lineEdit->clear();                    /* clear the lineEdit box */
259    if (m_console->is_connected()) {
260       m_console->display_text(cmdStr + "\n");
261       m_console->write_dir(cmdStr.toUtf8().data());         /* send to dir */
262    } else {
263       set_status("Director not connected. Click on connect button.");
264    }
265    m_cmd_history.append(cmdStr);
266    m_cmd_last = -1;
267 }
268
269
270 void MainWin::about()
271 {
272    QMessageBox::about(this, tr("About bat"),
273             tr("<br><h2>bat 0.2, by Kern Sibbald</h2>"
274             "<p>Copyright &copy; " BYEAR " Free Software Foundation Europe e.V."
275             "<p>The <b>bat</b> is an administrative console"
276                " interface to the Director."));
277 }
278
279 void MainWin::set_statusf(const char *fmt, ...)
280 {
281    va_list arg_ptr;
282    char buf[1000];
283    int len;
284    va_start(arg_ptr, fmt);
285    len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
286    va_end(arg_ptr);
287    set_status(buf);
288 }
289
290 void MainWin::set_status_ready()
291 {
292    set_status(" Ready");
293 }
294
295 void MainWin::set_status(const char *buf)
296 {
297    statusBar()->showMessage(buf);
298 }