]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/gnome2-console/console.c
Finish new restore tree code
[bacula/bacula] / bacula / src / gnome2-console / console.c
1 /*
2  *
3  *   Bacula GNOME Console interface to the Director
4  *
5  *     Kern Sibbald, March MMII
6  *     
7  *     Version $Id$
8  */
9
10 /*
11    Copyright (C) 2002 Kern Sibbald and John Walker
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License
15    as published by the Free Software Foundation; either version 2
16    of the License, or (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21    GNU General Public License for more details.
22
23    You should have received a copy of the GNU General Public License
24    along with this program; if not, write to the Free Software
25    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #include "bacula.h"
29 #include "console.h"
30
31 #include "interface.h"
32 #include "support.h"
33
34 /* Imported functions */
35 int authenticate_director(JCR *jcr, DIRRES *director, CONRES *cons);
36 void select_restore_setup();
37        
38 /* Exported variables */
39 GtkWidget *console;   /* application window */
40 GtkWidget *text1;            /* text window */
41 GtkWidget *entry1;           /* entry box */
42 GtkWidget *status1;          /* status bar */
43 GtkWidget *combo1;           /* director combo */
44 GtkWidget *scroll1;          /* main scroll bar */
45 GtkWidget *run_dialog;       /* run dialog */
46 GtkWidget *dir_dialog;       /* director selection dialog */
47 GtkWidget *restore_dialog;   /* restore dialog */
48 GtkWidget *restore_file_selection;
49 GtkWidget *dir_select;
50 GtkWidget *about1;           /* about box */
51 GtkWidget *label_dialog;
52 GdkFont   *text_font = NULL;
53 PangoFontDescription *font_desc;
54 pthread_mutex_t cmd_mutex = PTHREAD_MUTEX_INITIALIZER;
55 pthread_cond_t  cmd_wait;
56 char cmd[1000];
57 int reply;
58 BSOCK *UA_sock = NULL;
59 GList *job_list, *client_list, *fileset_list;
60 GList *messages_list, *pool_list, *storage_list;
61 GList *type_list, *level_list;
62
63 /* Forward referenced functions */
64 void terminate_console(int sig);
65
66 extern "C" {
67     static gint message_handler(gpointer data);
68     static int initial_connect_to_director(gpointer data);
69 }
70
71 static void set_scroll_bar_to_end(void);
72
73 /* Static variables */
74 static char *configfile = NULL;
75 static DIRRES *dir; 
76 static int ndir;
77 static bool director_reader_running = false;
78 static bool at_prompt = false;
79 static bool ready = false;
80 static bool quit = false;
81 static guint initial;
82
83 #define CONFIG_FILE "./gnome-console.conf"   /* default configuration file */
84
85 static void usage()
86 {
87    fprintf(stderr, _(
88 "\nVersion: " VERSION " (" BDATE ") %s %s %s\n\n"
89 "Usage: gnome-console [-s] [-c config_file] [-d debug_level] [config_file]\n"
90 "       -c <file>   set configuration file to file\n"
91 "       -dnn        set debug level to nn\n"
92 "       -s          no signals\n"
93 "       -t          test - read configuration and exit\n"
94 "       -?          print this message.\n"
95 "\n"), HOST_OS, DISTNAME, DISTVER);
96
97    exit(1);
98 }
99
100
101 /*********************************************************************
102  *
103  *         Main Bacula GNOME Console -- User Interface Program
104  *
105  */
106 int main(int argc, char *argv[])
107 {
108    int ch, stat;
109    int no_signals = TRUE;
110    int test_config = FALSE;
111    int gargc = 1;
112    const char *gargv[2] = {"gnome-console", NULL};
113    CONFONTRES *con_font;
114
115    init_stack_dump();
116    my_name_is(argc, argv, "gnome-console");
117    init_msg(NULL, NULL);
118    working_directory  = "/tmp";
119
120    struct sigaction sigignore;
121    sigignore.sa_flags = 0;
122    sigignore.sa_handler = SIG_IGN;       
123    sigfillset(&sigignore.sa_mask);
124    sigaction(SIGPIPE, &sigignore, NULL);
125
126    if ((stat=pthread_cond_init(&cmd_wait, NULL)) != 0) {
127       Emsg1(M_ABORT, 0, _("Pthread cond init error = %s\n"),
128          strerror(stat));
129    }
130
131    gnome_init("bacula", VERSION, gargc, (char **)&gargv);
132
133    while ((ch = getopt(argc, argv, "bc:d:r:st?")) != -1) {
134       switch (ch) {
135       case 'c':                    /* configuration file */
136          if (configfile != NULL)
137             free(configfile);
138          configfile = bstrdup(optarg);
139          break;
140
141       case 'd':
142          debug_level = atoi(optarg);
143          if (debug_level <= 0)
144             debug_level = 1;
145          break;
146
147       case 's':                    /* turn off signals */
148          no_signals = TRUE;
149          break;
150
151       case 't':
152          test_config = TRUE;
153          break;
154
155       case '?':
156       default:
157          usage();
158       }  
159    }
160    argc -= optind;
161    argv += optind;
162
163
164    if (!no_signals) {
165       init_signals(terminate_console);
166    }
167
168    if (argc) {
169       usage();
170    }
171
172    if (configfile == NULL) {
173       configfile = bstrdup(CONFIG_FILE);
174    }
175
176    parse_config(configfile);
177
178    LockRes();
179    ndir = 0;
180    foreach_res(dir, R_DIRECTOR) {
181       ndir++;
182    }
183    UnlockRes();
184    if (ndir == 0) {
185       Emsg1(M_ERROR_TERM, 0, _("No director resource defined in %s\n\
186 Without that I don't how to speak to the Director :-(\n"), configfile);
187    }
188
189
190
191    console = create_console();
192    gtk_window_set_default_size(GTK_WINDOW(console), 800, 700);
193    run_dialog = create_RunDialog();
194    label_dialog = create_label_dialog();
195    restore_dialog = create_RestoreDialog();
196    about1 = create_about1();
197
198    text1 = lookup_widget(console, "text1");
199    entry1 = lookup_widget(console, "entry1");
200    status1 = lookup_widget(console, "status1");
201    scroll1 = lookup_widget(console, "scroll1");
202
203    select_restore_setup();
204
205    gtk_widget_show(console);
206
207 /*
208  * Thanks to Phil Stracchino for providing the font configuration code.
209  * original default:
210    text_font = gdk_font_load("-misc-fixed-medium-r-normal-*-*-130-*-*-c-*-koi8-r");
211  * this works for me:
212    text_font = gdk_font_load("-Bigelow & Holmes-lucida console-medium-r-semi condensed-*-12-0-100-100-m-0-iso8859-1");
213  * and, new automagic:font specification!
214  */
215
216    LockRes();
217    foreach_res(con_font, R_CONSOLE_FONT) {
218        if (!con_font->fontface) {
219           Dmsg1(400, "No fontface for %s\n", con_font->hdr.name);
220           continue;
221        }
222        text_font = gdk_font_load(con_font->fontface);
223        if (text_font == NULL) {
224            Dmsg2(400, "Load of requested ConsoleFont \"%s\" (%s) failed!\n",
225                   con_font->hdr.name, con_font->fontface);
226        } else {
227            Dmsg2(400, "ConsoleFont \"%s\" (%s) loaded.\n",
228                   con_font->hdr.name, con_font->fontface);
229            break;
230        }           
231    }
232    UnlockRes();
233
234    if (text_font == NULL) {
235        Dmsg1(400, "Attempting to load fallback font %s\n",
236               "-misc-fixed-medium-r-normal-*-*-130-*-*-c-*-iso8859-1");
237        text_font = gdk_font_load("-misc-fixed-medium-r-normal-*-*-130-*-*-c-*-iso8859-1");
238    }
239    font_desc = pango_font_description_from_string("LucidaTypewriter 9");
240    gtk_widget_modify_font(console, font_desc);
241    gtk_widget_modify_font(text1, font_desc);
242    gtk_widget_modify_font(entry1, font_desc);
243    gtk_widget_modify_font(status1, font_desc);
244    pango_font_description_free(font_desc);
245
246    if (test_config) {
247       terminate_console(0);
248       exit(0);
249    }
250
251    initial = gtk_timeout_add(100, initial_connect_to_director, (gpointer)NULL);
252
253    gtk_main();
254    quit = true;
255    disconnect_from_director((gpointer)NULL);
256    return 0;
257 }
258
259 /*
260  * Every 5 seconds, ask the Director for our
261  *  messages.
262  */
263 static gint message_handler(gpointer data)
264 {
265    if (ready && UA_sock) {
266       bnet_fsend(UA_sock, ".messages");
267    }
268    return TRUE;
269 }
270
271 int disconnect_from_director(gpointer data)
272 {
273    if (!quit) {
274       set_status(_(" Not Connected"));
275    }
276    if (UA_sock) {
277       bnet_sig(UA_sock, BNET_TERMINATE); /* send EOF */
278       bnet_close(UA_sock);
279       UA_sock = NULL;
280    }
281    return 1;
282 }
283
284 /*
285  * Called just after the main loop is started to allow
286  *  us to connect to the Director.
287  */
288 static int initial_connect_to_director(gpointer data)
289 {
290    gtk_timeout_remove(initial);
291    if (connect_to_director(data)) {
292       start_director_reader(data);
293    }
294    gtk_timeout_add(5000, message_handler, (gpointer)NULL);
295    return TRUE;
296 }
297
298 static GList *get_list(char *cmd)
299 {
300    GList *options;
301    char *msg;
302
303    options = NULL;
304    write_director(cmd);
305    while (bnet_recv(UA_sock) > 0) {
306       strip_trailing_junk(UA_sock->msg);
307       msg = (char *)malloc(strlen(UA_sock->msg) + 1);
308       strcpy(msg, UA_sock->msg);
309       options = g_list_append(options, msg);
310    }
311    return options;
312    
313 }
314
315 static GList *get_and_fill_combo(GtkWidget *dialog, const char *combo_name, const char *cm)
316 {
317    GtkWidget *combo;
318    GList *options;
319
320    combo = lookup_widget(dialog, combo_name);
321    options = get_list(cmd);
322    if (combo && options) {
323       gtk_combo_set_popdown_strings(GTK_COMBO(combo), options);
324    }
325    return options;
326 }
327
328 static void fill_combo(GtkWidget *dialog, const char *combo_name, GList *options)
329 {
330    GtkWidget *combo;
331
332    combo = lookup_widget(dialog, combo_name);
333    if (combo && options) {
334       gtk_combo_set_popdown_strings(GTK_COMBO(combo), options);
335    }
336    return;
337 }
338
339
340 /*
341  * Connect to Director. If there are more than one, put up
342  * a modal dialog so that the user chooses one.
343  */
344 int connect_to_director(gpointer data)
345 {
346    GList *dirs = NULL;
347    GtkWidget *combo;
348    JCR jcr;
349
350
351    if (UA_sock) {
352       return 0;
353    }
354
355    if (ndir > 1) {
356       LockRes();
357       foreach_res(dir, R_DIRECTOR) {
358          dirs = g_list_append(dirs, dir->hdr.name);
359       }
360       UnlockRes();
361       dir_dialog = create_SelectDirectorDialog();
362       combo = lookup_widget(dir_dialog, "combo1");
363       dir_select = lookup_widget(dir_dialog, "dirselect");
364       if (dirs) {
365          gtk_combo_set_popdown_strings(GTK_COMBO(combo), dirs);   
366       }
367       gtk_widget_show(dir_dialog);
368       gtk_main();
369
370       if (reply == OK) {
371          gchar *ecmd = gtk_editable_get_chars((GtkEditable *)dir_select, 0, -1);
372          dir = (DIRRES *)GetResWithName(R_DIRECTOR, ecmd);
373          if (ecmd) {
374             g_free(ecmd);             /* release director name string */
375          }
376       }
377       if (dirs) {
378          g_free(dirs);
379       }
380       gtk_widget_destroy(dir_dialog);
381       dir_dialog = NULL;
382    } else {
383       /* Just take the first Director */
384       LockRes();
385       dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
386       UnlockRes();
387    }
388
389    if (!dir) {
390       return 0;
391    }
392
393    memset(&jcr, 0, sizeof(jcr));
394    
395    set_statusf(_(" Connecting to Director %s:%d"), dir->address,dir->DIRport);
396    set_textf(_("Connecting to Director %s:%d\n\n"), dir->address,dir->DIRport);
397
398    while (gtk_events_pending()) {     /* fully paint screen */
399       gtk_main_iteration();
400    }
401    UA_sock = bnet_connect(NULL, 5, 15, "Director daemon", dir->address, 
402                           NULL, dir->DIRport, 0);
403    if (UA_sock == NULL) {
404       return 0;
405    }
406    
407    jcr.dir_bsock = UA_sock;
408    LockRes();
409    /* If cons==NULL, default console will be used */
410    CONRES *cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)NULL);
411    UnlockRes();
412    if (!authenticate_director(&jcr, dir, cons)) {
413       set_text(UA_sock->msg, UA_sock->msglen);
414       return 0;
415    }
416
417    set_status(" Initializing ...");
418
419    bnet_fsend(UA_sock, "autodisplay on");
420
421    /* Read and display all initial messages */
422    while (bnet_recv(UA_sock) > 0) {
423       set_text(UA_sock->msg, UA_sock->msglen);
424    }
425
426    /* Paint changes */
427    while (gtk_events_pending()) {
428       gtk_main_iteration();
429    }
430
431    /* Fill the run_dialog combo boxes */
432    job_list      = get_and_fill_combo(run_dialog, "combo_job", ".jobs");
433    client_list   = get_and_fill_combo(run_dialog, "combo_client", ".clients");
434    fileset_list  = get_and_fill_combo(run_dialog, "combo_fileset", ".filesets");
435    messages_list = get_and_fill_combo(run_dialog, "combo_messages", ".msgs");
436    pool_list     = get_and_fill_combo(run_dialog, "combo_pool", ".pools");
437    storage_list  = get_and_fill_combo(run_dialog, "combo_storage", ".storage");
438    type_list     = get_and_fill_combo(run_dialog, "combo_type", ".types");
439    level_list    = get_and_fill_combo(run_dialog, "combo_level", ".levels");
440
441    /* Fill the label dialog combo boxes */
442    fill_combo(label_dialog, "label_combo_storage", storage_list);
443    fill_combo(label_dialog, "label_combo_pool", pool_list);
444
445
446    /* Fill the restore_dialog combo boxes */
447    fill_combo(restore_dialog, "combo_restore_job", job_list);
448    fill_combo(restore_dialog, "combo_restore_client", client_list);
449    fill_combo(restore_dialog, "combo_restore_fileset", fileset_list);
450    fill_combo(restore_dialog, "combo_restore_pool", pool_list);
451    fill_combo(restore_dialog, "combo_restore_storage", storage_list);
452
453    set_status(" Connected");
454    return 1;
455 }
456
457 void write_director(const gchar *msg)
458 {
459    if (UA_sock) {
460       at_prompt = false;
461       set_status(_(" Processing command ..."));
462       UA_sock->msglen = strlen(msg);
463       pm_strcpy(&UA_sock->msg, msg);
464       bnet_send(UA_sock);
465    }
466    if (strcmp(msg, ".quit") == 0 || strcmp(msg, ".exit") == 0) {
467       disconnect_from_director((gpointer)NULL);
468       gtk_main_quit();
469    }
470 }
471
472 extern "C"
473 void read_director(gpointer data, gint fd, GdkInputCondition condition)
474 {
475    int stat;
476
477    if (!UA_sock || UA_sock->fd != fd) {
478       return;
479    }
480    stat = bnet_recv(UA_sock);
481    if (stat >= 0) {
482       if (at_prompt) {
483          set_text("\n", 1);
484          at_prompt = false;
485       }
486       set_text(UA_sock->msg, UA_sock->msglen);
487       return;
488    }
489    if (is_bnet_stop(UA_sock)) {         /* error or term request */
490       gtk_main_quit();
491       return;
492    }
493    /* Must be a signal -- either do something or ignore it */
494    if (UA_sock->msglen == BNET_PROMPT) {
495       at_prompt = true;
496       set_status(_(" At prompt waiting for input ..."));
497    }
498    if (UA_sock->msglen == BNET_EOD) {
499       set_status_ready();
500    }
501    return;
502 }
503
504 static gint tag;
505
506 void start_director_reader(gpointer data)
507 {
508
509    if (director_reader_running || !UA_sock) {
510       return;
511    }
512    tag = gdk_input_add(UA_sock->fd, GDK_INPUT_READ, read_director, NULL);
513    director_reader_running = true;
514 }
515
516 void stop_director_reader(gpointer data)
517 {
518    if (!director_reader_running) {
519       return;
520    }
521    gdk_input_remove(tag);
522    gdk_input_remove(tag);
523    gdk_input_remove(tag);
524    gdk_input_remove(tag);
525    gdk_input_remove(tag);
526    gdk_input_remove(tag);
527    gdk_input_remove(tag);
528    gdk_input_remove(tag);
529    gdk_input_remove(tag);
530    gdk_input_remove(tag);
531    gdk_input_remove(tag);
532    director_reader_running = false;
533 }
534
535
536
537 /* Cleanup and then exit */
538 void terminate_console(int sig)
539 {
540    static int already_here = FALSE;
541
542    if (already_here)                  /* avoid recursive temination problems */
543       exit(1);
544    already_here = TRUE;
545    disconnect_from_director((gpointer)NULL);
546    gtk_main_quit();
547    exit(0);
548 }
549
550
551 /* Buffer approx 2000 lines -- assume 60 chars/line */
552 #define MAX_TEXT_CHARS   (2000 * 60)
553 static int text_chars = 0;
554
555 static void truncate_text_chars()
556 {
557    GtkTextBuffer *textbuf;
558    GtkTextIter iter, iter2;
559    guint len;
560    int del_chars = MAX_TEXT_CHARS / 4;
561
562    textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text1));
563    len = gtk_text_buffer_get_char_count(textbuf);
564    gtk_text_buffer_get_iter_at_offset (textbuf, &iter, 0);
565    gtk_text_buffer_get_iter_at_offset (textbuf, &iter2, del_chars);
566    gtk_text_buffer_delete (textbuf, &iter, &iter2);
567    text_chars -= del_chars;
568    len = gtk_text_buffer_get_char_count(textbuf);
569    gtk_text_iter_set_offset(&iter, len);
570 }
571
572 void set_textf(const char *fmt, ...)
573 {
574    va_list arg_ptr;
575    char buf[1000];
576    int len;
577    va_start(arg_ptr, fmt);
578    len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
579    va_end(arg_ptr);
580    set_text(buf, len);
581 }
582
583 void set_text(const char *buf, int len)
584 {
585    GtkTextBuffer *textbuf;
586    GtkTextIter iter;
587    guint buf_len;
588
589    textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text1));
590    buf_len = gtk_text_buffer_get_char_count(textbuf);
591    gtk_text_buffer_get_iter_at_offset(textbuf, &iter, buf_len - 1);
592    gtk_text_buffer_insert(textbuf, &iter, buf, -1);
593    text_chars += len;
594    if (text_chars > MAX_TEXT_CHARS) {
595       truncate_text_chars();
596    }
597    set_scroll_bar_to_end();
598 }
599
600 void set_statusf(const char *fmt, ...)
601 {
602    va_list arg_ptr;
603    char buf[1000];
604    int len;
605    va_start(arg_ptr, fmt);
606    len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
607    va_end(arg_ptr);
608    gtk_label_set_text(GTK_LABEL(status1), buf);
609 // set_scroll_bar_to_end();
610    ready = false;
611 }
612
613 void set_status_ready()    
614 {
615    gtk_label_set_text(GTK_LABEL(status1), " Ready");
616    ready = true;
617 // set_scroll_bar_to_end();
618 }
619
620 void set_status(const char *buf)
621 {
622    gtk_label_set_text(GTK_LABEL(status1), buf);
623 // set_scroll_bar_to_end();
624    ready = false;
625 }
626
627 static void set_scroll_bar_to_end(void)
628 {
629    GtkTextBuffer* textbuf = NULL;
630    GtkTextIter iter;
631    guint buf_len;
632
633    textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text1));
634    buf_len = gtk_text_buffer_get_char_count(textbuf);
635    gtk_text_buffer_get_iter_at_offset(textbuf, &iter, buf_len - 1);
636    gtk_text_iter_set_offset(&iter, buf_len);
637    gtk_text_buffer_place_cursor(textbuf, &iter);
638    gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(text1),
639               gtk_text_buffer_get_mark(textbuf, "insert"), 
640               0, TRUE, 0.0, 1.0);
641 }