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