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