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);
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");
364 gtk_combo_set_popdown_strings(GTK_COMBO(combo), dirs);
365 gtk_widget_show(dir_dialog);
369 gchar *ecmd = gtk_editable_get_chars((GtkEditable *)dir_select, 0, -1);
370 dir = (DIRRES *)GetResWithName(R_DIRECTOR, ecmd);
372 g_free(ecmd); /* release director name string */
378 gtk_widget_destroy(dir_dialog);
381 /* Just take the first Director */
383 dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
391 memset(&jcr, 0, sizeof(jcr));
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);
396 while (gtk_events_pending()) { /* fully paint screen */
397 gtk_main_iteration();
399 UA_sock = bnet_connect(NULL, 5, 15, "Director daemon", dir->address,
400 NULL, dir->DIRport, 0);
401 if (UA_sock == NULL) {
405 jcr.dir_bsock = UA_sock;
407 /* If cons==NULL, default console will be used */
408 CONRES *cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)NULL);
410 if (!authenticate_director(&jcr, dir, cons)) {
411 set_text(UA_sock->msg, UA_sock->msglen);
415 set_status(" Initializing ...");
417 bnet_fsend(UA_sock, "autodisplay on");
419 /* Read and display all initial messages */
420 while (bnet_recv(UA_sock) > 0) {
421 set_text(UA_sock->msg, UA_sock->msglen);
425 while (gtk_events_pending()) {
426 gtk_main_iteration();
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");
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);
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);
451 set_status(" Connected");
455 void write_director(const gchar *msg)
459 set_status(_(" Processing command ..."));
460 UA_sock->msglen = strlen(msg);
461 pm_strcpy(&UA_sock->msg, msg);
464 if (strcmp(msg, ".quit") == 0 || strcmp(msg, ".exit") == 0) {
465 disconnect_from_director((gpointer)NULL);
471 void read_director(gpointer data, gint fd, GdkInputCondition condition)
475 if (!UA_sock || UA_sock->fd != fd) {
478 stat = bnet_recv(UA_sock);
484 set_text(UA_sock->msg, UA_sock->msglen);
487 if (is_bnet_stop(UA_sock)) { /* error or term request */
491 /* Must be a signal -- either do something or ignore it */
492 if (UA_sock->msglen == BNET_PROMPT) {
494 set_status(_(" At prompt waiting for input ..."));
496 if (UA_sock->msglen == BNET_EOD) {
504 void start_director_reader(gpointer data)
507 if (director_reader_running || !UA_sock) {
510 tag = gdk_input_add(UA_sock->fd, GDK_INPUT_READ, read_director, NULL);
511 director_reader_running = true;
514 void stop_director_reader(gpointer data)
516 if (!director_reader_running) {
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;
535 /* Cleanup and then exit */
536 void terminate_console(int sig)
538 static int already_here = FALSE;
540 if (already_here) /* avoid recursive temination problems */
543 disconnect_from_director((gpointer)NULL);
549 /* Buffer approx 2000 lines -- assume 60 chars/line */
550 #define MAX_TEXT_CHARS (2000 * 60)
551 static int text_chars = 0;
553 static void truncate_text_chars()
555 GtkTextBuffer *textbuf;
556 GtkTextIter iter, iter2;
558 int del_chars = MAX_TEXT_CHARS / 4;
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);
570 void set_textf(const char *fmt, ...)
575 va_start(arg_ptr, fmt);
576 len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
581 void set_text(const char *buf, int len)
583 GtkTextBuffer *textbuf;
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);
592 if (text_chars > MAX_TEXT_CHARS) {
593 truncate_text_chars();
595 set_scroll_bar_to_end();
598 void set_statusf(const char *fmt, ...)
603 va_start(arg_ptr, fmt);
604 len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
606 gtk_label_set_text(GTK_LABEL(status1), buf);
607 // set_scroll_bar_to_end();
611 void set_status_ready()
613 gtk_label_set_text(GTK_LABEL(status1), " Ready");
615 // set_scroll_bar_to_end();
618 void set_status(const char *buf)
620 gtk_label_set_text(GTK_LABEL(status1), buf);
621 // set_scroll_bar_to_end();
625 static void set_scroll_bar_to_end(void)
627 GtkTextBuffer* textbuf = NULL;
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"),