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