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