]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/mainwin.cpp
306aea39d6372b34068f70fc496d0574769ddbcc
[bacula/bacula] / bacula / src / qt-console / mainwin.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-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    mainWin = this;
43    setupUi(this);                     /* Setup UI defined by main.ui (designer) */
44
45    m_console = new Console(stackedWidget);
46    stackedWidget->setCurrentIndex(0);
47
48    resetFocus();
49
50    createConnections();
51
52    this->show();
53
54    readSettings();
55
56    m_console->connect();
57 }
58
59 void MainWin::resetFocus()
60 {  
61    lineEdit->setFocus();
62 }   
63
64 void MainWin::keyPressEvent(QKeyEvent *event)
65 {
66    if (m_cmd_history.size() == 0) {
67       event->ignore();
68       return;
69    }
70    switch (event->key()) {
71    case Qt::Key_Down:
72 //    Dmsg0(000, "Down key pressed\n");
73       if (m_cmd_last < 0 || m_cmd_last >= (m_cmd_history.size()-1)) {
74          event->ignore();
75          return;
76       }
77       m_cmd_last++;
78       break;
79    case Qt::Key_Up:
80 //    Dmsg0(000, "Up key pressed\n");
81       if (m_cmd_last == 0) {
82          event->ignore();
83          return;
84       }
85       if (m_cmd_last < 0 || m_cmd_last > (m_cmd_history.size()-1)) {
86          m_cmd_last = m_cmd_history.size() - 1;
87       } else {
88          m_cmd_last--;
89       }
90       break;
91    default:
92       event->ignore();
93       return;
94    }
95    lineEdit->setText(m_cmd_history[m_cmd_last]);
96 }
97
98 void MainWin::createConnections()
99 {
100    /* Connect signals to slots */
101    connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(input_line()));
102    connect(actionAbout_bat, SIGNAL(triggered()), this, SLOT(about()));
103
104    connect(treeWidget, SIGNAL(itemActivated(QTreeWidgetItem *, int)), this, 
105            SLOT(treeItemClicked(QTreeWidgetItem *, int)));
106    connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, 
107            SLOT(treeItemClicked(QTreeWidgetItem *, int)));
108    connect(treeWidget, SIGNAL(itemPressed(QTreeWidgetItem *, int)), this, 
109            SLOT(treeItemClicked(QTreeWidgetItem *, int)));
110    connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, 
111            SLOT(treeItemDoubleClicked(QTreeWidgetItem *, int)));
112
113    connect(actionQuit, SIGNAL(triggered()), app, SLOT(closeAllWindows()));
114    connect(actionConnect, SIGNAL(triggered()), m_console, SLOT(connect()));
115    connect(actionStatusDir, SIGNAL(triggered()), m_console, SLOT(status_dir()));
116    connect(actionSelectFont, SIGNAL(triggered()), m_console, SLOT(set_font()));
117    connect(actionLabel, SIGNAL(triggered()), this,  SLOT(labelDialogClicked()));
118    connect(actionRun, SIGNAL(triggered()), this,  SLOT(runDialogClicked()));
119    connect(actionRestore, SIGNAL(triggered()), this,  SLOT(restoreDialogClicked()));
120 }
121
122 /* 
123  * Reimplementation of QWidget closeEvent virtual function   
124  */
125 void MainWin::closeEvent(QCloseEvent *event)
126 {
127    writeSettings();
128    m_console->writeSettings();
129    m_console->terminate();
130    event->accept();
131 }
132
133 void MainWin::writeSettings()
134 {
135    QSettings settings("bacula.org", "bat");
136
137    settings.beginGroup("MainWin");
138    settings.setValue("winSize", size());
139    settings.setValue("winPos", pos());
140    settings.endGroup();
141 }
142
143 void MainWin::readSettings()
144
145    QSettings settings("bacula.org", "bat");
146
147    settings.beginGroup("MainWin");
148    resize(settings.value("winSize", QSize(1041, 801)).toSize());
149    move(settings.value("winPos", QPoint(200, 150)).toPoint());
150    settings.endGroup();
151 }
152
153 void MainWin::treeItemClicked(QTreeWidgetItem *item, int column)
154 {
155    (void)column;
156    int index = item->text(1).toInt();
157    if (index >= 0 && index < 4) {
158       stackedWidget->setCurrentIndex(index);
159    }
160 }
161
162 /*
163  */
164 void MainWin::treeItemDoubleClicked(QTreeWidgetItem *item, int column)
165 {
166    (void)column;
167    int index = item->text(1).toInt();
168    /* ***FIXME**** make this automatic */
169    if (index >= 0 && index < 4) {
170       stackedWidget->setCurrentIndex(index);
171    }
172 }
173
174 void MainWin::labelDialogClicked() 
175 {
176    new labelDialog(m_console);
177 }
178
179 void MainWin::runDialogClicked() 
180 {
181    new runDialog(m_console);
182 }
183
184 void MainWin::restoreDialogClicked() 
185 {
186    new prerestoreDialog(m_console);
187 }
188
189
190
191 /*
192  * The user just finished typing a line in the command line edit box
193  */
194 void MainWin::input_line()
195 {
196    QString cmdStr = lineEdit->text();    /* Get the text */
197    lineEdit->clear();                    /* clear the lineEdit box */
198    if (m_console->is_connected()) {
199       m_console->display_text(cmdStr + "\n");
200       m_console->write_dir(cmdStr.toUtf8().data());         /* send to dir */
201    } else {
202       set_status("Director not connected. Click on connect button.");
203    }
204    m_cmd_history.append(cmdStr);
205    m_cmd_last = -1;
206 }
207
208
209 void MainWin::about()
210 {
211    QMessageBox::about(this, tr("About bat"),
212             tr("<br><h2>bat 0.1</h2>"
213             "<p>Copyright &copy; " BYEAR " Free Software Foundation Europe e.V."
214             "<p>The <b>bat</b> is an administrative console"
215                " interface to the Director."));
216 }
217
218 void MainWin::set_statusf(const char *fmt, ...)
219 {
220    va_list arg_ptr;
221    char buf[1000];
222    int len;
223    va_start(arg_ptr, fmt);
224    len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
225    va_end(arg_ptr);
226    set_status(buf);
227 // set_scroll_bar_to_end();
228 }
229
230 void MainWin::set_status_ready()
231 {
232    set_status(" Ready");
233 // set_scroll_bar_to_end();
234 }
235
236 void MainWin::set_status(const char *buf)
237 {
238    statusBar()->showMessage(buf);
239 // ready = false;
240 }