]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/console/console.cpp
kes Clarify some stored/acquire messages to indicate if the
[bacula/bacula] / bacula / src / qt-console / console / console.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  *  Console Class
31  *
32  *   Kern Sibbald, January MMVI
33  *
34  */ 
35
36 #include <QAbstractEventDispatcher>
37 #include "bat.h"
38 #include "console.h"
39 #include "restore.h"
40
41 Console::Console(QStackedWidget *parent)
42 {
43    QFont font;
44    QTreeWidgetItem *item, *topItem;
45    QTreeWidget *treeWidget = mainWin->treeWidget;
46
47    setupUi(this);
48    parent->addWidget(this);
49    m_sock = NULL;
50    m_at_prompt = false;
51    m_textEdit = textEdit;   /* our console screen */
52    m_cursor = new QTextCursor(m_textEdit->document());
53    mainWin->actionConnect->setIcon(QIcon(QString::fromUtf8("images/disconnected.png")));
54
55    bRestore *restore = new bRestore(parent);
56    restore->setupUi(restore);
57    parent->addWidget(restore);
58
59    /* Just take the first Director */
60    LockRes();
61    m_dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
62    UnlockRes();
63
64    /* Dummy setup of treeWidget */
65    treeWidget->clear();
66    treeWidget->setColumnCount(1);
67    treeWidget->setHeaderLabel("Selection");
68    topItem = new QTreeWidgetItem(treeWidget);
69    topItem->setText(0, m_dir->name());
70    topItem->setIcon(0, QIcon(QString::fromUtf8("images/server.png")));
71    item = new QTreeWidgetItem(topItem);
72    m_consoleItem = item;
73    item->setText(0, "Console");
74    item->setText(1, "0");
75    QBrush redBrush(Qt::red);
76    item->setForeground(0, redBrush);
77    item = new QTreeWidgetItem(topItem);
78    item->setText(0, "Restore");
79    item->setText(1, "1");
80    treeWidget->expandItem(topItem);
81
82    readSettings();
83
84 }
85
86 /*
87  * Connect to Director. If there are more than one, put up
88  * a modal dialog so that the user chooses one.
89  */
90 void Console::connect()
91 {
92    JCR jcr;
93
94    m_textEdit = textEdit;   /* our console screen */
95
96    if (!m_dir) {          
97       mainWin->set_status("No Director found.");
98       return;
99    }
100    if (m_sock) {
101       mainWin->set_status("Already connected.");
102       return;
103    }
104
105    memset(&jcr, 0, sizeof(jcr));
106
107    mainWin->set_statusf(_(" Connecting to Director %s:%d"), m_dir->address, m_dir->DIRport);
108    set_textf(_("Connecting to Director %s:%d\n\n"), m_dir->address, m_dir->DIRport);
109
110    /* Give GUI a chance */
111    app->processEvents();
112    
113    LockRes();
114    /* If cons==NULL, default console will be used */
115    CONRES *cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)NULL);
116    UnlockRes();
117
118    m_sock = bnet_connect(NULL, 5, 15, _("Director daemon"), m_dir->address,
119                           NULL, m_dir->DIRport, 0);
120    if (m_sock == NULL) {
121       mainWin->set_status("Connection failed");
122       return;
123    } else {
124       /* Update page selector to green to indicate that Console is connected */
125       mainWin->actionConnect->setIcon(QIcon(QString::fromUtf8("images/connected.png")));
126       QBrush greenBrush(Qt::green);
127       m_consoleItem->setForeground(0, greenBrush);
128    }
129
130    jcr.dir_bsock = m_sock;
131
132    if (!authenticate_director(&jcr, m_dir, cons)) {
133       set_text(m_sock->msg);
134       return;
135    }
136
137    /* Give GUI a chance */
138    app->processEvents();
139
140    mainWin->set_status(_(" Initializing ..."));
141
142    bnet_fsend(m_sock, "autodisplay on");
143
144    /* Set up input notifier */
145    m_notifier = new QSocketNotifier(m_sock->fd, QSocketNotifier::Read, 0);
146    QObject::connect(m_notifier, SIGNAL(activated(int)), this, SLOT(read_dir(int)));
147
148    /* Give GUI a chance */
149    app->processEvents();
150
151    /*  *** FIXME *** 
152     * Query Directory for .jobs, .clients, .filesets, .msgs,
153     *  .pools, .storage, .types, .levels, ...
154     */
155
156    mainWin->set_status(_(" Connected"));
157    return;
158 }
159
160
161 void Console::writeSettings()
162 {
163    QFont font = get_font();
164
165    QSettings settings("bacula.org", "bat");
166    settings.beginGroup("Console");
167    settings.setValue("consoleFont", font.family());
168    settings.setValue("consolePointSize", font.pointSize());
169    settings.setValue("consoleFixedPitch", font.fixedPitch());
170    settings.endGroup();
171 }
172
173 void Console::readSettings()
174
175    QFont font = get_font();
176
177    QSettings settings("bacula.org", "bat");
178    settings.beginGroup("Console");
179    font.setFamily(settings.value("consoleFont", "Courier").value<QString>());
180    font.setPointSize(settings.value("consolePointSize", 10).toInt());
181    font.setFixedPitch(settings.value("consoleFixedPitch", true).toBool());
182    settings.endGroup();
183    m_textEdit->setFont(font);
184 }
185
186 void Console::set_font()
187 {
188    bool ok;
189    QFont font = QFontDialog::getFont(&ok, QFont(m_textEdit->font()), this);
190    if (ok) {
191       m_textEdit->setFont(font);
192    }
193 }
194
195 const QFont Console::get_font()
196 {
197    return m_textEdit->font();
198 }
199
200
201 void Console::status_dir()
202 {
203    write_dir("status dir\n");
204 }
205
206 void Console::set_textf(const char *fmt, ...)
207 {
208    va_list arg_ptr;
209    char buf[1000];
210    int len;
211    va_start(arg_ptr, fmt);
212    len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
213    va_end(arg_ptr);
214    set_text(buf);
215 }
216
217 void Console::set_text(const QString buf)
218 {
219    m_cursor->movePosition(QTextCursor::End);
220    m_cursor->insertText(buf);
221 }
222
223
224 void Console::set_text(const char *buf)
225 {
226    m_cursor->movePosition(QTextCursor::End);
227    m_cursor->insertText(buf);
228 }
229
230 /* Position cursor to end of screen */
231 void Console::update_cursor()
232 {
233    QApplication::restoreOverrideCursor();
234    m_textEdit->moveCursor(QTextCursor::End);
235    m_textEdit->ensureCursorVisible();
236 }
237
238
239 /* Send a command to the Director */
240 void Console::write_dir(const char *msg)
241 {
242    if (m_sock) {
243       m_at_prompt = false;
244       mainWin->set_status(_(" Processing command ..."));
245       QApplication::setOverrideCursor(Qt::WaitCursor);
246       m_sock->msglen = strlen(msg);
247       pm_strcpy(&m_sock->msg, msg);
248       bnet_send(m_sock);
249    } else {
250       mainWin->set_status(" Director not connected. Click on connect button.");
251       mainWin->actionConnect->setIcon(QIcon(QString::fromUtf8("images/disconnected.png")));
252       QBrush redBrush(Qt::red);
253       m_consoleItem->setForeground(0, redBrush);
254    }
255 }
256
257 /* Called by signal when the Director has output for us */
258 void Console::read_dir(int fd)
259 {
260    int stat;
261    (void)fd;
262
263    if (!m_sock) {
264       return;
265    }
266    stat = bnet_recv(m_sock);
267    if (stat >= 0) {
268       if (m_at_prompt) {
269          set_text("\n");
270          m_at_prompt = false;
271       }
272       set_text(m_sock->msg);
273       return;
274    }
275    if (is_bnet_stop(m_sock)) {         /* error or term request */
276       bnet_close(m_sock);
277       m_sock = NULL;
278       mainWin->actionConnect->setIcon(QIcon(QString::fromUtf8("images/disconnected.png")));
279       QBrush redBrush(Qt::red);
280       m_consoleItem->setForeground(0, redBrush);
281       m_notifier->setEnabled(false);
282       delete m_notifier;
283       m_notifier = NULL;
284       mainWin->set_status(_(" Director disconnected."));
285       QApplication::restoreOverrideCursor();
286       return;
287    }
288    /* Must be a signal -- either do something or ignore it */
289    if (m_sock->msglen == BNET_PROMPT) {
290       m_at_prompt = true;
291       mainWin->set_status(_(" At prompt waiting for input ..."));
292       update_cursor();
293    }
294    if (m_sock->msglen == BNET_EOD) {
295       mainWin->set_status_ready();
296       update_cursor();
297    }
298    return;
299 }