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