3 * Bacula GNOME Console interface to the Director
5 * Kern Sibbald, March MMII
11 Copyright (C) 2002 Kern Sibbald and John Walker
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.
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.
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.
31 #include "interface.h"
34 /* Imported functions */
35 int authenticate_director(JCR *jcr, DIRRES *director, CONRES *cons);
36 void select_restore_setup();
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;
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;
63 /* Forward referenced functions */
64 void terminate_console(int sig);
67 static gint message_handler(gpointer data);
68 static int initial_connect_to_director(gpointer data);
71 static void set_scroll_bar_to_end(void);
73 /* Static variables */
74 static char *configfile = NULL;
77 static bool director_reader_running = false;
78 static bool at_prompt = false;
79 static bool ready = false;
80 static bool quit = false;
83 #define CONFIG_FILE "./gnome-console.conf" /* default configuration file */
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"
93 " -t test - read configuration and exit\n"
94 " -? print this message.\n"
95 "\n"), HOST_OS, DISTNAME, DISTVER);
101 /*********************************************************************
103 * Main Bacula GNOME Console -- User Interface Program
106 int main(int argc, char *argv[])
109 int no_signals = TRUE;
110 int test_config = FALSE;
112 const char *gargv[2] = {"gnome-console", NULL};
113 CONFONTRES *con_font;
116 my_name_is(argc, argv, "gnome-console");
117 init_msg(NULL, NULL);
118 working_directory = "/tmp";
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);
126 if ((stat=pthread_cond_init(&cmd_wait, NULL)) != 0) {
127 Emsg1(M_ABORT, 0, _("Pthread cond init error = %s\n"),
131 gnome_init("bacula", VERSION, gargc, (char **)&gargv);
133 while ((ch = getopt(argc, argv, "bc:d:r:st?")) != -1) {
135 case 'c': /* configuration file */
136 if (configfile != NULL)
138 configfile = bstrdup(optarg);
142 debug_level = atoi(optarg);
143 if (debug_level <= 0)
147 case 's': /* turn off signals */
165 init_signals(terminate_console);
172 if (configfile == NULL) {
173 configfile = bstrdup(CONFIG_FILE);
176 parse_config(configfile);
180 foreach_res(dir, R_DIRECTOR) {
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);
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();
198 text1 = lookup_widget(console, "text1");
199 entry1 = lookup_widget(console, "entry1");
200 status1 = lookup_widget(console, "status1");
201 scroll1 = lookup_widget(console, "scroll1");
203 select_restore_setup();
205 gtk_widget_show(console);
208 * Thanks to Phil Stracchino for providing the font configuration code.
210 text_font = gdk_font_load("-misc-fixed-medium-r-normal-*-*-130-*-*-c-*-koi8-r");
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!
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);
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);
227 Dmsg2(400, "ConsoleFont \"%s\" (%s) loaded.\n",
228 con_font->hdr.name, con_font->fontface);
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");
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);
247 terminate_console(0);
251 initial = gtk_timeout_add(100, initial_connect_to_director, (gpointer)NULL);
255 disconnect_from_director((gpointer)NULL);
260 * Every 5 seconds, ask the Director for our
263 static gint message_handler(gpointer data)
265 if (ready && UA_sock) {
266 bnet_fsend(UA_sock, ".messages");
271 int disconnect_from_director(gpointer data)
274 set_status(_(" Not Connected"));
277 bnet_sig(UA_sock, BNET_TERMINATE); /* send EOF */
285 * Called just after the main loop is started to allow
286 * us to connect to the Director.
288 static int initial_connect_to_director(gpointer data)
290 gtk_timeout_remove(initial);
291 if (connect_to_director(data)) {
292 start_director_reader(data);
294 gtk_timeout_add(5000, message_handler, (gpointer)NULL);
298 static GList *get_list(char *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);
315 static GList *get_and_fill_combo(GtkWidget *dialog, const char *combo_name, const char *cm)
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);
328 static void fill_combo(GtkWidget *dialog, const char *combo_name, GList *options)
332 combo = lookup_widget(dialog, combo_name);
333 if (combo && options) {
334 gtk_combo_set_popdown_strings(GTK_COMBO(combo), options);
341 * Connect to Director. If there are more than one, put up
342 * a modal dialog so that the user chooses one.
344 int connect_to_director(gpointer data)
357 foreach_res(dir, R_DIRECTOR) {
358 dirs = g_list_append(dirs, dir->hdr.name);
361 dir_dialog = create_SelectDirectorDialog();
362 combo = lookup_widget(dir_dialog, "combo1");
363 dir_select = lookup_widget(dir_dialog, "dirselect");
365 gtk_combo_set_popdown_strings(GTK_COMBO(combo), dirs);
367 gtk_widget_show(dir_dialog);
371 gchar *ecmd = gtk_editable_get_chars((GtkEditable *)dir_select, 0, -1);
372 dir = (DIRRES *)GetResWithName(R_DIRECTOR, ecmd);
374 g_free(ecmd); /* release director name string */
380 gtk_widget_destroy(dir_dialog);
383 /* Just take the first Director */
385 dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
393 memset(&jcr, 0, sizeof(jcr));
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);
398 while (gtk_events_pending()) { /* fully paint screen */
399 gtk_main_iteration();
401 UA_sock = bnet_connect(NULL, 5, 15, "Director daemon", dir->address,
402 NULL, dir->DIRport, 0);
403 if (UA_sock == NULL) {
407 jcr.dir_bsock = UA_sock;
409 /* If cons==NULL, default console will be used */
410 CONRES *cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)NULL);
412 if (!authenticate_director(&jcr, dir, cons)) {
413 set_text(UA_sock->msg, UA_sock->msglen);
417 set_status(" Initializing ...");
419 bnet_fsend(UA_sock, "autodisplay on");
421 /* Read and display all initial messages */
422 while (bnet_recv(UA_sock) > 0) {
423 set_text(UA_sock->msg, UA_sock->msglen);
427 while (gtk_events_pending()) {
428 gtk_main_iteration();
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");
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);
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);
453 set_status(" Connected");
457 void write_director(const gchar *msg)
461 set_status(_(" Processing command ..."));
462 UA_sock->msglen = strlen(msg);
463 pm_strcpy(&UA_sock->msg, msg);
466 if (strcmp(msg, ".quit") == 0 || strcmp(msg, ".exit") == 0) {
467 disconnect_from_director((gpointer)NULL);
473 void read_director(gpointer data, gint fd, GdkInputCondition condition)
477 if (!UA_sock || UA_sock->fd != fd) {
480 stat = bnet_recv(UA_sock);
486 set_text(UA_sock->msg, UA_sock->msglen);
489 if (is_bnet_stop(UA_sock)) { /* error or term request */
493 /* Must be a signal -- either do something or ignore it */
494 if (UA_sock->msglen == BNET_PROMPT) {
496 set_status(_(" At prompt waiting for input ..."));
498 if (UA_sock->msglen == BNET_EOD) {
506 void start_director_reader(gpointer data)
509 if (director_reader_running || !UA_sock) {
512 tag = gdk_input_add(UA_sock->fd, GDK_INPUT_READ, read_director, NULL);
513 director_reader_running = true;
516 void stop_director_reader(gpointer data)
518 if (!director_reader_running) {
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;
537 /* Cleanup and then exit */
538 void terminate_console(int sig)
540 static int already_here = FALSE;
542 if (already_here) /* avoid recursive temination problems */
545 disconnect_from_director((gpointer)NULL);
551 /* Buffer approx 2000 lines -- assume 60 chars/line */
552 #define MAX_TEXT_CHARS (2000 * 60)
553 static int text_chars = 0;
555 static void truncate_text_chars()
557 GtkTextBuffer *textbuf;
558 GtkTextIter iter, iter2;
560 int del_chars = MAX_TEXT_CHARS / 4;
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);
572 void set_textf(const char *fmt, ...)
577 va_start(arg_ptr, fmt);
578 len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
583 void set_text(const char *buf, int len)
585 GtkTextBuffer *textbuf;
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);
594 if (text_chars > MAX_TEXT_CHARS) {
595 truncate_text_chars();
597 set_scroll_bar_to_end();
600 void set_statusf(const char *fmt, ...)
605 va_start(arg_ptr, fmt);
606 len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
608 gtk_label_set_text(GTK_LABEL(status1), buf);
609 // set_scroll_bar_to_end();
613 void set_status_ready()
615 gtk_label_set_text(GTK_LABEL(status1), " Ready");
617 // set_scroll_bar_to_end();
620 void set_status(const char *buf)
622 gtk_label_set_text(GTK_LABEL(status1), buf);
623 // set_scroll_bar_to_end();
627 static void set_scroll_bar_to_end(void)
629 GtkTextBuffer* textbuf = NULL;
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"),