]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/console/console.cpp
Toggle connect/disconnect icon
[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    /* ***FIXME*** make this configurable */
53    font.setFamily("Courier");
54    font.setFixedPitch(true);
55    font.setPointSize(10);
56    m_textEdit->setFont(font);
57
58    /* Just take the first Director */
59    LockRes();
60    m_dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
61    UnlockRes();
62
63    /* Dummy setup of treeWidget */
64    treeWidget->clear();
65    treeWidget->setColumnCount(1);
66    treeWidget->setHeaderLabel("Selection");
67    topItem = new QTreeWidgetItem(treeWidget);
68    topItem->setText(0, m_dir->name());
69    item = new QTreeWidgetItem(topItem);
70    item->setText(0, "Console");
71    item->setText(1, "0");
72    item = new QTreeWidgetItem(topItem);
73    item->setText(0, "Restore");
74    item->setText(1, "1");
75    treeWidget->expandItem(topItem);
76 }
77
78 /*
79  * Connect to Director. If there are more than one, put up
80  * a modal dialog so that the user chooses one.
81  */
82 void Console::connect()
83 {
84    JCR jcr;
85
86    m_textEdit = mainWin->textEdit;   /* our console screen */
87
88    if (!m_dir) {          
89       mainWin->set_status("No Director found.");
90       return;
91    }
92    if (m_sock) {
93       mainWin->set_status("Already connected.");
94       return;
95    }
96
97    memset(&jcr, 0, sizeof(jcr));
98
99    mainWin->set_statusf(_(" Connecting to Director %s:%d"), m_dir->address, m_dir->DIRport);
100    set_textf(_("Connecting to Director %s:%d\n\n"), m_dir->address, m_dir->DIRport);
101
102    /* Give GUI a chance */
103    app->processEvents();
104    
105    LockRes();
106    /* If cons==NULL, default console will be used */
107    CONRES *cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)NULL);
108    UnlockRes();
109
110    m_sock = bnet_connect(NULL, 5, 15, _("Director daemon"), m_dir->address,
111                           NULL, m_dir->DIRport, 0);
112    if (m_sock == NULL) {
113       mainWin->set_status("Connection failed");
114       return;
115    } else {
116       mainWin->actionConnect->setIcon(QIcon(QString::fromUtf8("images/connected.png")));
117    }
118
119
120    jcr.dir_bsock = m_sock;
121
122    if (!authenticate_director(&jcr, m_dir, cons)) {
123       set_text(m_sock->msg);
124       return;
125    }
126
127    /* Give GUI a chance */
128    app->processEvents();
129
130    mainWin->set_status(_(" Initializing ..."));
131
132    bnet_fsend(m_sock, "autodisplay on");
133
134    /* Set up input notifier */
135    m_notifier = new QSocketNotifier(m_sock->fd, QSocketNotifier::Read, 0);
136    QObject::connect(m_notifier, SIGNAL(activated(int)), this, SLOT(read_dir(int)));
137
138    /* Give GUI a chance */
139    app->processEvents();
140
141    /*  *** FIXME *** 
142     * Query Directory for .jobs, .clients, .filesets, .msgs,
143     *  .pools, .storage, .types, .levels, ...
144     */
145
146    mainWin->set_status(_(" Connected"));
147    return;
148 }
149 #ifdef xxx
150     QByteArray bytes = m_Bash->readAllStandardOutput();
151     QStringList lines = QString(bytes).split("\n");
152     foreach (QString line, lines) {
153         m_Logw->append(line);
154     }
155 #endif
156
157 void Console::set_textf(const char *fmt, ...)
158 {
159    va_list arg_ptr;
160    char buf[1000];
161    int len;
162    va_start(arg_ptr, fmt);
163    len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
164    va_end(arg_ptr);
165    set_text(buf);
166 }
167
168 void Console::set_text(const QString buf)
169 {
170    m_cursor->movePosition(QTextCursor::End);
171    m_cursor->insertText(buf);
172    m_textEdit->moveCursor(QTextCursor::End);
173    m_textEdit->ensureCursorVisible();
174 }
175
176
177 void Console::set_text(const char *buf)
178 {
179    m_cursor->movePosition(QTextCursor::End);
180    m_cursor->insertText(buf);
181    m_textEdit->moveCursor(QTextCursor::End);
182    m_textEdit->ensureCursorVisible();
183 }
184
185
186 void Console::write_dir(const char *msg)
187 {
188    if (m_sock) {
189       m_at_prompt = false;
190       mainWin->set_status(_(" Processing command ..."));
191       QApplication::setOverrideCursor(Qt::WaitCursor);
192       m_sock->msglen = strlen(msg);
193       pm_strcpy(&m_sock->msg, msg);
194       bnet_send(m_sock);
195    } else {
196       mainWin->set_status(" Director not connected. Click on connect button.");
197       mainWin->actionConnect->setIcon(QIcon(QString::fromUtf8("images/disconnected.png")));
198    }
199 }
200
201 void Console::read_dir(int fd)
202 {
203    int stat;
204    (void)fd;
205
206    if (!m_sock) {
207       return;
208    }
209    stat = bnet_recv(m_sock);
210    if (stat >= 0) {
211       if (m_at_prompt) {
212          set_text("\n");
213          m_at_prompt = false;
214       }
215       set_text(m_sock->msg);
216       return;
217    }
218    if (is_bnet_stop(m_sock)) {         /* error or term request */
219       bnet_close(m_sock);
220       m_sock = NULL;
221       mainWin->actionConnect->setIcon(QIcon(QString::fromUtf8("images/disconnected.png")));
222       m_notifier->setEnabled(false);
223       delete m_notifier;
224       m_notifier = NULL;
225       mainWin->set_status(_(" Director disconnected."));
226       QApplication::restoreOverrideCursor();
227       return;
228    }
229    /* Must be a signal -- either do something or ignore it */
230    if (m_sock->msglen == BNET_PROMPT) {
231       m_at_prompt = true;
232       mainWin->set_status(_(" At prompt waiting for input ..."));
233       QApplication::restoreOverrideCursor();
234    }
235    if (m_sock->msglen == BNET_EOD) {
236       mainWin->set_status_ready();
237       QApplication::restoreOverrideCursor();
238    }
239    return;
240 }
241
242 #ifdef xxx
243
244 static gint tag;
245
246 void start_director_reader(gpointer data)
247 {
248
249    if (director_reader_running || !UA_sock) {
250       return;
251    }
252    tag = gdk_input_add(UA_sock->fd, GDK_INPUT_READ, read_director, NULL);
253    director_reader_running = true;
254 }
255
256 void stop_director_reader(gpointer data)
257 {
258    if (!director_reader_running) {
259       return;
260    }
261    gdk_input_remove(tag);
262    director_reader_running = false;
263 }
264 #endif