]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/gnome2-console/console.c
Add extern C to callback functions
[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) {
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       gtk_combo_set_popdown_strings(GTK_COMBO(combo), dirs);   
365       gtk_widget_show(dir_dialog);
366       gtk_main();
367
368       if (reply == OK) {
369          gchar *ecmd = gtk_editable_get_chars((GtkEditable *)dir_select, 0, -1);
370          dir = (DIRRES *)GetResWithName(R_DIRECTOR, ecmd);
371          if (ecmd) {
372             g_free(ecmd);             /* release director name string */
373          }
374       }
375       if (dirs) {
376          g_free(dirs);
377       }
378       gtk_widget_destroy(dir_dialog);
379       dir_dialog = NULL;
380    } else {
381       /* Just take the first Director */
382       LockRes();
383       dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
384       UnlockRes();
385    }
386
387    if (!dir) {
388       return 0;
389    }
390
391    memset(&jcr, 0, sizeof(jcr));
392    
393    set_statusf(_(" Connecting to Director %s:%d"), dir->address,dir->DIRport);
394    set_textf(_("Connecting to Director %s:%d\n\n"), dir->address,dir->DIRport);
395
396    while (gtk_events_pending()) {     /* fully paint screen */
397       gtk_main_iteration();
398    }
399    UA_sock = bnet_connect(NULL, 5, 15, "Director daemon", dir->address, 
400                           NULL, dir->DIRport, 0);
401    if (UA_sock == NULL) {
402       return 0;
403    }
404    
405    jcr.dir_bsock = UA_sock;
406    LockRes();
407    /* If cons==NULL, default console will be used */
408    CONRES *cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)NULL);
409    UnlockRes();
410    if (!authenticate_director(&jcr, dir, cons)) {
411       set_text(UA_sock->msg, UA_sock->msglen);
412       return 0;
413    }
414
415    set_status(" Initializing ...");
416
417    bnet_fsend(UA_sock, "autodisplay on");
418
419    /* Read and display all initial messages */
420    while (bnet_recv(UA_sock) > 0) {
421       set_text(UA_sock->msg, UA_sock->msglen);
422    }
423
424    /* Paint changes */
425    while (gtk_events_pending()) {
426       gtk_main_iteration();
427    }
428
429    /* Fill the run_dialog combo boxes */
430    job_list      = get_and_fill_combo(run_dialog, "combo_job", ".jobs");
431    client_list   = get_and_fill_combo(run_dialog, "combo_client", ".clients");
432    fileset_list  = get_and_fill_combo(run_dialog, "combo_fileset", ".filesets");
433    messages_list = get_and_fill_combo(run_dialog, "combo_messages", ".msgs");
434    pool_list     = get_and_fill_combo(run_dialog, "combo_pool", ".pools");
435    storage_list  = get_and_fill_combo(run_dialog, "combo_storage", ".storage");
436    type_list     = get_and_fill_combo(run_dialog, "combo_type", ".types");
437    level_list    = get_and_fill_combo(run_dialog, "combo_level", ".levels");
438
439    /* Fill the label dialog combo boxes */
440    fill_combo(label_dialog, "label_combo_storage", storage_list);
441    fill_combo(label_dialog, "label_combo_pool", pool_list);
442
443
444    /* Fill the restore_dialog combo boxes */
445    fill_combo(restore_dialog, "combo_restore_job", job_list);
446    fill_combo(restore_dialog, "combo_restore_client", client_list);
447    fill_combo(restore_dialog, "combo_restore_fileset", fileset_list);
448    fill_combo(restore_dialog, "combo_restore_pool", pool_list);
449    fill_combo(restore_dialog, "combo_restore_storage", storage_list);
450
451    set_status(" Connected");
452    return 1;
453 }
454
455 void write_director(const gchar *msg)
456 {
457    if (UA_sock) {
458       at_prompt = false;
459       set_status(_(" Processing command ..."));
460       UA_sock->msglen = strlen(msg);
461       pm_strcpy(&UA_sock->msg, msg);
462       bnet_send(UA_sock);
463    }
464    if (strcmp(msg, ".quit") == 0 || strcmp(msg, ".exit") == 0) {
465       disconnect_from_director((gpointer)NULL);
466       gtk_main_quit();
467    }
468 }
469
470 extern "C"
471 void read_director(gpointer data, gint fd, GdkInputCondition condition)
472 {
473    int stat;
474
475    if (!UA_sock || UA_sock->fd != fd) {
476       return;
477    }
478    stat = bnet_recv(UA_sock);
479    if (stat >= 0) {
480       if (at_prompt) {
481          set_text("\n", 1);
482          at_prompt = false;
483       }
484       set_text(UA_sock->msg, UA_sock->msglen);
485       return;
486    }
487    if (is_bnet_stop(UA_sock)) {         /* error or term request */
488       gtk_main_quit();
489       return;
490    }
491    /* Must be a signal -- either do something or ignore it */
492    if (UA_sock->msglen == BNET_PROMPT) {
493       at_prompt = true;
494       set_status(_(" At prompt waiting for input ..."));
495    }
496    if (UA_sock->msglen == BNET_EOD) {
497       set_status_ready();
498    }
499    return;
500 }
501
502 static gint tag;
503
504 void start_director_reader(gpointer data)
505 {
506
507    if (director_reader_running || !UA_sock) {
508       return;
509    }
510    tag = gdk_input_add(UA_sock->fd, GDK_INPUT_READ, read_director, NULL);
511    director_reader_running = true;
512 }
513
514 void stop_director_reader(gpointer data)
515 {
516    if (!director_reader_running) {
517       return;
518    }
519    gdk_input_remove(tag);
520    gdk_input_remove(tag);
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    director_reader_running = false;
531 }
532
533
534
535 /* Cleanup and then exit */
536 void terminate_console(int sig)
537 {
538    static int already_here = FALSE;
539
540    if (already_here)                  /* avoid recursive temination problems */
541       exit(1);
542    already_here = TRUE;
543    disconnect_from_director((gpointer)NULL);
544    gtk_main_quit();
545    exit(0);
546 }
547
548
549 /* Buffer approx 2000 lines -- assume 60 chars/line */
550 #define MAX_TEXT_CHARS   (2000 * 60)
551 static int text_chars = 0;
552
553 static void truncate_text_chars()
554 {
555    GtkTextBuffer *textbuf;
556    GtkTextIter iter, iter2;
557    guint len;
558    int del_chars = MAX_TEXT_CHARS / 4;
559
560    textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text1));
561    len = gtk_text_buffer_get_char_count(textbuf);
562    gtk_text_buffer_get_iter_at_offset (textbuf, &iter, 0);
563    gtk_text_buffer_get_iter_at_offset (textbuf, &iter2, del_chars);
564    gtk_text_buffer_delete (textbuf, &iter, &iter2);
565    text_chars -= del_chars;
566    len = gtk_text_buffer_get_char_count(textbuf);
567    gtk_text_iter_set_offset(&iter, len);
568 }
569
570 void set_textf(const char *fmt, ...)
571 {
572    va_list arg_ptr;
573    char buf[1000];
574    int len;
575    va_start(arg_ptr, fmt);
576    len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
577    va_end(arg_ptr);
578    set_text(buf, len);
579 }
580
581 void set_text(const char *buf, int len)
582 {
583    GtkTextBuffer *textbuf;
584    GtkTextIter iter;
585    guint buf_len;
586
587    textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text1));
588    buf_len = gtk_text_buffer_get_char_count(textbuf);
589    gtk_text_buffer_get_iter_at_offset(textbuf, &iter, buf_len - 1);
590    gtk_text_buffer_insert(textbuf, &iter, buf, -1);
591    text_chars += len;
592    if (text_chars > MAX_TEXT_CHARS) {
593       truncate_text_chars();
594    }
595    set_scroll_bar_to_end();
596 }
597
598 void set_statusf(const char *fmt, ...)
599 {
600    va_list arg_ptr;
601    char buf[1000];
602    int len;
603    va_start(arg_ptr, fmt);
604    len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
605    va_end(arg_ptr);
606    gtk_label_set_text(GTK_LABEL(status1), buf);
607 // set_scroll_bar_to_end();
608    ready = false;
609 }
610
611 void set_status_ready()    
612 {
613    gtk_label_set_text(GTK_LABEL(status1), " Ready");
614    ready = true;
615 // set_scroll_bar_to_end();
616 }
617
618 void set_status(const char *buf)
619 {
620    gtk_label_set_text(GTK_LABEL(status1), buf);
621 // set_scroll_bar_to_end();
622    ready = false;
623 }
624
625 static void set_scroll_bar_to_end(void)
626 {
627    GtkTextBuffer* textbuf = NULL;
628    GtkTextIter iter;
629    guint buf_len;
630
631    textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text1));
632    buf_len = gtk_text_buffer_get_char_count(textbuf);
633    gtk_text_buffer_get_iter_at_offset(textbuf, &iter, buf_len - 1);
634    gtk_text_iter_set_offset(&iter, buf_len);
635    gtk_text_buffer_place_cursor(textbuf, &iter);
636    gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(text1),
637               gtk_text_buffer_get_mark(textbuf, "insert"), 
638               0, TRUE, 0.0, 1.0);
639 }