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