]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/console/console.cpp
3127c9c8d198bb73ed1feabba3aba5cd28278fcf
[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 "bat.h"
37 #include "console.h"
38
39 Console::Console()
40 {
41    QTreeWidgetItem *item, *topItem;
42    QTreeWidget *treeWidget = mainWin->treeWidget;
43
44    m_sock = NULL;
45    m_textEdit = mainWin->textEdit;   /* our console screen */
46
47    /* Just take the first Director */
48    LockRes();
49    m_dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
50    UnlockRes();
51
52    /* Dummy setup of treeWidget */
53    treeWidget->clear();
54    treeWidget->setColumnCount(1);
55    treeWidget->setHeaderLabel("Selection");
56    topItem = new QTreeWidgetItem(treeWidget);
57    topItem->setText(0, m_dir->name());
58    item = new QTreeWidgetItem(topItem);
59    item->setText(0, "Console");
60    item->setText(1, "0");
61    item = new QTreeWidgetItem(topItem);
62    item->setText(0, "Restore");
63    item->setText(1, "1");
64    treeWidget->expandItem(topItem);
65 }
66
67 /*
68  * Connect to Director. If there are more than one, put up
69  * a modal dialog so that the user chooses one.
70  */
71 void Console::connect()
72 {
73    JCR jcr;
74
75    m_textEdit = mainWin->textEdit;   /* our console screen */
76
77    if (!m_dir) {          
78       set_text("No Director to connect to.\n");
79       return;
80    }
81    if (m_sock) {
82       set_text("Already connected.\n");
83       return;
84    }
85
86    memset(&jcr, 0, sizeof(jcr));
87
88    set_statusf(_(" Connecting to Director %s:%d"), m_dir->address, m_dir->DIRport);
89    set_textf(_("Connecting to Director %s:%d\n\n"), m_dir->address, m_dir->DIRport);
90
91    /* Give GUI a chance */
92    app->processEvents();
93    
94    LockRes();
95    /* If cons==NULL, default console will be used */
96    CONRES *cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)NULL);
97    UnlockRes();
98
99    m_sock = bnet_connect(NULL, 5, 15, _("Director daemon"), m_dir->address,
100                           NULL, m_dir->DIRport, 0);
101    if (m_sock == NULL) {
102       set_text("Connection failed\n");
103       return;
104    }
105
106    jcr.dir_bsock = m_sock;
107
108    if (!authenticate_director(&jcr, m_dir, cons)) {
109       set_text(m_sock->msg);
110       return;
111    }
112
113    /* Give GUI a chance */
114    app->processEvents();
115
116    set_status(_(" Initializing ..."));
117
118    bnet_fsend(m_sock, "autodisplay on");
119
120    /* Read and display all initial messages */
121    while (bnet_recv(m_sock) > 0) {
122       set_text(m_sock->msg);
123    }
124
125    /* Give GUI a chance */
126    app->processEvents();
127
128    /* Query Directory for .jobs, .clients, .filesets, .msgs,
129     *  .pools, .storage, .types, .levels, ...
130     */
131
132    set_status(_(" Connected"));
133    set_text("Connected\n");
134
135    return;
136 }
137
138 void Console::set_textf(const char *fmt, ...)
139 {
140    va_list arg_ptr;
141    char buf[1000];
142    int len;
143    va_start(arg_ptr, fmt);
144    len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
145    va_end(arg_ptr);
146    m_textEdit->append(buf);
147 }
148
149 void Console::set_text(const char *buf)
150 {
151    m_textEdit->append(buf);
152 }
153
154 void Console::set_statusf(const char *fmt, ...)
155 {
156    va_list arg_ptr;
157    char buf[1000];
158    int len;
159    va_start(arg_ptr, fmt);
160    len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
161    va_end(arg_ptr);
162    set_status(buf);
163 }
164
165 void Console::set_status_ready()
166 {
167    mainWin->statusBar()->showMessage("Ready");
168 // ready = true;
169 }
170
171 void Console::set_status(const char *buf)
172 {
173    mainWin->statusBar()->showMessage(buf);
174    set_text(buf);
175 // ready = false;
176 }
177
178
179 #ifdef xxx
180 void write_director(const gchar *msg)
181 {
182    if (UA_sock) {
183       at_prompt = false;
184       set_status(_(" Processing command ..."));
185       UA_sock->msglen = strlen(msg);
186       pm_strcpy(&UA_sock->msg, msg);
187       bnet_send(UA_sock);
188    }
189    if (strcmp(msg, ".quit") == 0 || strcmp(msg, ".exit") == 0) {
190       disconnect_from_director((gpointer)NULL);
191       gtk_main_quit();
192    }
193 }
194
195 extern "C"
196 void read_director(gpointer data, gint fd, GdkInputCondition condition)
197 {
198    int stat;
199
200    if (!UA_sock || UA_sock->fd != fd) {
201       return;
202    }
203    stat = bnet_recv(UA_sock);
204    if (stat >= 0) {
205       if (at_prompt) {
206          set_text("\n", 1);
207          at_prompt = false;
208       }
209       set_text(UA_sock->msg, UA_sock->msglen);
210       return;
211    }
212    if (is_bnet_stop(UA_sock)) {         /* error or term request */
213       gtk_main_quit();
214       return;
215    }
216    /* Must be a signal -- either do something or ignore it */
217    if (UA_sock->msglen == BNET_PROMPT) {
218       at_prompt = true;
219       set_status(_(" At prompt waiting for input ..."));
220    }
221    if (UA_sock->msglen == BNET_EOD) {
222       set_status_ready();
223    }
224    return;
225 }
226
227 static gint tag;
228
229 void start_director_reader(gpointer data)
230 {
231
232    if (director_reader_running || !UA_sock) {
233       return;
234    }
235    tag = gdk_input_add(UA_sock->fd, GDK_INPUT_READ, read_director, NULL);
236    director_reader_running = true;
237 }
238
239 void stop_director_reader(gpointer data)
240 {
241    if (!director_reader_running) {
242       return;
243    }
244    gdk_input_remove(tag);
245    director_reader_running = false;
246 }
247 #endif