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