]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/gnome2-console/console.c
- Modify the depend section of each Makefile.in to reference
[bacula/bacula] / bacula / src / gnome2-console / console.c
index 56b5253aeda0f903374edfde7314d51bb13f0fe2..c4c6fa80974ae7e3a119c2cdb90f6614bf7ffcd0 100644 (file)
@@ -8,7 +8,7 @@
  */
 
 /*
-   Copyright (C) 2002 Kern Sibbald and John Walker
+   Copyright (C) 2002-2004 Kern Sibbald and John Walker
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
 #include "support.h"
 
 /* Imported functions */
-int authenticate_director(JCR *jcr, DIRRES *director);
+int authenticate_director(JCR *jcr, DIRRES *director, CONRES *cons);
+void select_restore_setup();
        
 /* Exported variables */
-GtkWidget *app1;            /* application window */
+GtkWidget *console;   /* application window */
 GtkWidget *text1;           /* text window */
 GtkWidget *entry1;          /* entry box */
 GtkWidget *status1;         /* status bar */
@@ -44,17 +45,15 @@ GtkWidget *scroll1;      /* main scroll bar */
 GtkWidget *run_dialog;      /* run dialog */
 GtkWidget *dir_dialog;      /* director selection dialog */
 GtkWidget *restore_dialog;   /* restore dialog */
-GtkWidget *restore_files;    /* restore files dialog */
+GtkWidget *restore_file_selection;
 GtkWidget *dir_select;
 GtkWidget *about1;          /* about box */
 GtkWidget *label_dialog;
 GdkFont   *text_font = NULL;
 PangoFontDescription *font_desc;
-GtkAdjustment *vadj;
 pthread_mutex_t cmd_mutex = PTHREAD_MUTEX_INITIALIZER;
 pthread_cond_t cmd_wait;
 char cmd[1000];
-int cmd_ready = FALSE;
 int reply;
 BSOCK *UA_sock = NULL;
 GList *job_list, *client_list, *fileset_list;
@@ -62,19 +61,23 @@ GList *messages_list, *pool_list, *storage_list;
 GList *type_list, *level_list;
 
 /* Forward referenced functions */
-static void terminate_console(int sig);
-static gint message_handler(gpointer data);
-static int initial_connect_to_director(gpointer data);
+void terminate_console(int sig);
+
+extern "C" {
+    static gint message_handler(gpointer data);
+    static int initial_connect_to_director(gpointer data);
+}
+
+static void set_scroll_bar_to_end(void);
 
 /* Static variables */
 static char *configfile = NULL;
 static DIRRES *dir; 
-static CONRES *con; 
 static int ndir;
-static int director_reader_running = FALSE;
-static int at_prompt = FALSE;
-static int ready = FALSE;
-static int quit = FALSE;
+static bool director_reader_running = false;
+static bool at_prompt = false;
+static bool ready = false;
+static bool quit = false;
 static guint initial;
 
 #define CONFIG_FILE "./gnome-console.conf"   /* default configuration file */
@@ -82,6 +85,7 @@ static guint initial;
 static void usage()
 {
    fprintf(stderr, _(
+"Copyright (C) 2002-2004 Kern Sibbald and John Walker\n"
 "\nVersion: " VERSION " (" BDATE ") %s %s %s\n\n"
 "Usage: gnome-console [-s] [-c config_file] [-d debug_level] [config_file]\n"
 "       -c <file>   set configuration file to file\n"
@@ -106,7 +110,8 @@ int main(int argc, char *argv[])
    int no_signals = TRUE;
    int test_config = FALSE;
    int gargc = 1;
-   char *gargv[2] = {"gnome-console", NULL};
+   const char *gargv[2] = {"gnome-console", NULL};
+   CONFONTRES *con_font;
 
    init_stack_dump();
    my_name_is(argc, argv, "gnome-console");
@@ -128,30 +133,29 @@ int main(int argc, char *argv[])
 
    while ((ch = getopt(argc, argv, "bc:d:r:st?")) != -1) {
       switch (ch) {
-         case 'c':                    /* configuration file */
-           if (configfile != NULL)
-              free(configfile);
-           configfile = bstrdup(optarg);
-           break;
-
-         case 'd':
-           debug_level = atoi(optarg);
-           if (debug_level <= 0)
-              debug_level = 1;
-           break;
-
-         case 's':                    /* turn off signals */
-           no_signals = TRUE;
-           break;
-
-         case 't':
-           test_config = TRUE;
-           break;
-
-         case '?':
-        default:
-           usage();
-
+      case 'c':                    /* configuration file */
+        if (configfile != NULL)
+           free(configfile);
+        configfile = bstrdup(optarg);
+        break;
+
+      case 'd':
+        debug_level = atoi(optarg);
+        if (debug_level <= 0)
+           debug_level = 1;
+        break;
+
+      case 's':                    /* turn off signals */
+        no_signals = TRUE;
+        break;
+
+      case 't':
+        test_config = TRUE;
+        break;
+
+      case '?':
+      default:
+        usage();
       }  
    }
    argc -= optind;
@@ -174,7 +178,7 @@ int main(int argc, char *argv[])
 
    LockRes();
    ndir = 0;
-   for (dir=NULL; (dir = (DIRRES *)GetNextRes(R_DIRECTOR, (RES *)dir)); ) {
+   foreach_res(dir, R_DIRECTOR) {
       ndir++;
    }
    UnlockRes();
@@ -183,26 +187,23 @@ int main(int argc, char *argv[])
 Without that I don't how to speak to the Director :-(\n"), configfile);
    }
 
-   if (test_config) {
-      terminate_console(0);
-      exit(0);
-   }
 
 
-   app1 = create_app1();
-   gtk_window_set_default_size(GTK_WINDOW(app1), 800, 700);
+   console = create_console();
+   gtk_window_set_default_size(GTK_WINDOW(console), 800, 700);
    run_dialog = create_RunDialog();
    label_dialog = create_label_dialog();
-   restore_dialog = create_restore_dialog();
-   restore_files  = create_restore_files();
+   restore_dialog = create_RestoreDialog();
    about1 = create_about1();
 
-   gtk_widget_show(app1);
+   text1 = lookup_widget(console, "text1");
+   entry1 = lookup_widget(console, "entry1");
+   status1 = lookup_widget(console, "status1");
+   scroll1 = lookup_widget(console, "scroll1");
+
+   select_restore_setup();
 
-   text1 = lookup_widget(app1, "text1");
-   entry1 = lookup_widget(app1, "entry1");
-   status1 = lookup_widget(app1, "status1");
-   scroll1 = lookup_widget(app1, "scroll1");
+   gtk_widget_show(console);
 
 /*
  * Thanks to Phil Stracchino for providing the font configuration code.
@@ -214,36 +215,44 @@ Without that I don't how to speak to the Director :-(\n"), configfile);
  */
 
    LockRes();
-   for (con = NULL; (con = (CONRES *)GetNextRes(R_CONSOLE, (RES *)con)); ) {
-       text_font = gdk_font_load(con->fontface);
+   foreach_res(con_font, R_CONSOLE_FONT) {
+       if (!con_font->fontface) {
+          Dmsg1(400, "No fontface for %s\n", con_font->hdr.name);
+         continue;
+       }
+       text_font = gdk_font_load(con_font->fontface);
        if (text_font == NULL) {
-           Dmsg2(404, "Load of requested ConsoleFont \"%s\" (%s) failed!\n",
-                 con->hdr.name, con->fontface);
+           Dmsg2(400, "Load of requested ConsoleFont \"%s\" (%s) failed!\n",
+                 con_font->hdr.name, con_font->fontface);
        } else {
-           Dmsg2(404, "ConsoleFont \"%s\" (%s) loaded.\n",
-                 con->hdr.name, con->fontface);
+           Dmsg2(400, "ConsoleFont \"%s\" (%s) loaded.\n",
+                 con_font->hdr.name, con_font->fontface);
           break;
        }          
    }
    UnlockRes();
 
    if (text_font == NULL) {
-       Dmsg1(100, "Attempting to load fallback font %s\n",
+       Dmsg1(400, "Attempting to load fallback font %s\n",
               "-misc-fixed-medium-r-normal-*-*-130-*-*-c-*-iso8859-1");
        text_font = gdk_font_load("-misc-fixed-medium-r-normal-*-*-130-*-*-c-*-iso8859-1");
    }
    font_desc = pango_font_description_from_string("LucidaTypewriter 9");
-   gtk_widget_modify_font (app1, font_desc);
-   gtk_widget_modify_font (text1, font_desc);
-   gtk_widget_modify_font (entry1, font_desc);
-   gtk_widget_modify_font (status1, font_desc);
-   pango_font_description_free (font_desc);
+   gtk_widget_modify_font(console, font_desc);
+   gtk_widget_modify_font(text1, font_desc);
+   gtk_widget_modify_font(entry1, font_desc);
+   gtk_widget_modify_font(status1, font_desc);
+   pango_font_description_free(font_desc);
 
+   if (test_config) {
+      terminate_console(0);
+      exit(0);
+   }
 
    initial = gtk_timeout_add(100, initial_connect_to_director, (gpointer)NULL);
 
    gtk_main();
-   quit = TRUE;
+   quit = true;
    disconnect_from_director((gpointer)NULL);
    return 0;
 }
@@ -262,8 +271,9 @@ static gint message_handler(gpointer data)
 
 int disconnect_from_director(gpointer data)
 {
-   if (!quit)
+   if (!quit) {
       set_status(_(" Not Connected"));
+   }
    if (UA_sock) {
       bnet_sig(UA_sock, BNET_TERMINATE); /* send EOF */
       bnet_close(UA_sock);
@@ -303,7 +313,7 @@ static GList *get_list(char *cmd)
    
 }
 
-static GList *get_and_fill_combo(GtkWidget *dialog, char *combo_name, char *cmd)
+static GList *get_and_fill_combo(GtkWidget *dialog, const char *combo_name, const char *cm)
 {
    GtkWidget *combo;
    GList *options;
@@ -316,12 +326,12 @@ static GList *get_and_fill_combo(GtkWidget *dialog, char *combo_name, char *cmd)
    return options;
 }
 
-static void fill_combo(GtkWidget *dialog, char *combo_name, GList *options)
+static void fill_combo(GtkWidget *dialog, const char *combo_name, GList *options)
 {
    GtkWidget *combo;
 
    combo = lookup_widget(dialog, combo_name);
-   if (combo) {
+   if (combo && options) {
       gtk_combo_set_popdown_strings(GTK_COMBO(combo), options);
    }
    return;
@@ -336,7 +346,6 @@ int connect_to_director(gpointer data)
 {
    GList *dirs = NULL;
    GtkWidget *combo;
-   char buf[1000];
    JCR jcr;
 
 
@@ -346,18 +355,16 @@ int connect_to_director(gpointer data)
 
    if (ndir > 1) {
       LockRes();
-      for (dir = NULL; (dir = (DIRRES *)GetNextRes(R_DIRECTOR, (RES *)dir)); ) {
-         sprintf(buf, "%s at %s:%d", dir->hdr.name, dir->address,
-           dir->DIRport);
-         printf("%s\n", buf);
+      foreach_res(dir, R_DIRECTOR) {
         dirs = g_list_append(dirs, dir->hdr.name);
       }
       UnlockRes();
       dir_dialog = create_SelectDirectorDialog();
       combo = lookup_widget(dir_dialog, "combo1");
       dir_select = lookup_widget(dir_dialog, "dirselect");
-      gtk_combo_set_popdown_strings(GTK_COMBO(combo), dirs);   
-      printf("dialog run\n");
+      if (dirs) {
+        gtk_combo_set_popdown_strings(GTK_COMBO(combo), dirs);   
+      }
       gtk_widget_show(dir_dialog);
       gtk_main();
 
@@ -381,18 +388,17 @@ int connect_to_director(gpointer data)
    }
 
    if (!dir) {
-      printf("dir is NULL\n");
       return 0;
    }
 
    memset(&jcr, 0, sizeof(jcr));
    
-   set_statusf(_(" Connecting to Director %s:%d\n"), dir->address,dir->DIRport);
-   set_textf(_("Connecting to Director %s:%d\n"), dir->address,dir->DIRport);
+   set_statusf(_(" Connecting to Director %s:%d"), dir->address,dir->DIRport);
+   set_textf(_("Connecting to Director %s:%d\n\n"), dir->address,dir->DIRport);
 
-// while (gtk_events_pending()) {     /* fully paint screen */
-//    gtk_main_iteration();
-// }
+   while (gtk_events_pending()) {     /* fully paint screen */
+      gtk_main_iteration();
+   }
    UA_sock = bnet_connect(NULL, 5, 15, "Director daemon", dir->address, 
                          NULL, dir->DIRport, 0);
    if (UA_sock == NULL) {
@@ -400,7 +406,11 @@ int connect_to_director(gpointer data)
    }
    
    jcr.dir_bsock = UA_sock;
-   if (!authenticate_director(&jcr, dir)) {
+   LockRes();
+   /* If cons==NULL, default console will be used */
+   CONRES *cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)NULL);
+   UnlockRes();
+   if (!authenticate_director(&jcr, dir, cons)) {
       set_text(UA_sock->msg, UA_sock->msglen);
       return 0;
    }
@@ -429,17 +439,26 @@ int connect_to_director(gpointer data)
    type_list     = get_and_fill_combo(run_dialog, "combo_type", ".types");
    level_list    = get_and_fill_combo(run_dialog, "combo_level", ".levels");
 
+   /* Fill the label dialog combo boxes */
    fill_combo(label_dialog, "label_combo_storage", storage_list);
    fill_combo(label_dialog, "label_combo_pool", pool_list);
 
+
+   /* Fill the restore_dialog combo boxes */
+   fill_combo(restore_dialog, "combo_restore_job", job_list);
+   fill_combo(restore_dialog, "combo_restore_client", client_list);
+   fill_combo(restore_dialog, "combo_restore_fileset", fileset_list);
+   fill_combo(restore_dialog, "combo_restore_pool", pool_list);
+   fill_combo(restore_dialog, "combo_restore_storage", storage_list);
+
    set_status(" Connected");
    return 1;
 }
 
-void write_director(gchar *msg)
+void write_director(const gchar *msg)
 {
    if (UA_sock) {
-      at_prompt = FALSE;
+      at_prompt = false;
       set_status(_(" Processing command ..."));
       UA_sock->msglen = strlen(msg);
       pm_strcpy(&UA_sock->msg, msg);
@@ -451,6 +470,7 @@ void write_director(gchar *msg)
    }
 }
 
+extern "C"
 void read_director(gpointer data, gint fd, GdkInputCondition condition)
 {
    int stat;
@@ -462,7 +482,7 @@ void read_director(gpointer data, gint fd, GdkInputCondition condition)
    if (stat >= 0) {
       if (at_prompt) {
          set_text("\n", 1);
-        at_prompt = FALSE;
+        at_prompt = false;
       }
       set_text(UA_sock->msg, UA_sock->msglen);
       return;
@@ -473,7 +493,7 @@ void read_director(gpointer data, gint fd, GdkInputCondition condition)
    }
    /* Must be a signal -- either do something or ignore it */
    if (UA_sock->msglen == BNET_PROMPT) {
-      at_prompt = TRUE;
+      at_prompt = true;
       set_status(_(" At prompt waiting for input ..."));
    }
    if (UA_sock->msglen == BNET_EOD) {
@@ -490,9 +510,8 @@ void start_director_reader(gpointer data)
    if (director_reader_running || !UA_sock) {
       return;
    }
-   director_reader_running = TRUE;
-
    tag = gdk_input_add(UA_sock->fd, GDK_INPUT_READ, read_director, NULL);
+   director_reader_running = true;
 }
 
 void stop_director_reader(gpointer data)
@@ -501,13 +520,23 @@ void stop_director_reader(gpointer data)
       return;
    }
    gdk_input_remove(tag);
-   director_reader_running = FALSE;
+   gdk_input_remove(tag);
+   gdk_input_remove(tag);
+   gdk_input_remove(tag);
+   gdk_input_remove(tag);
+   gdk_input_remove(tag);
+   gdk_input_remove(tag);
+   gdk_input_remove(tag);
+   gdk_input_remove(tag);
+   gdk_input_remove(tag);
+   gdk_input_remove(tag);
+   director_reader_running = false;
 }
 
 
 
 /* Cleanup and then exit */
-static void terminate_console(int sig)
+void terminate_console(int sig)
 {
    static int already_here = FALSE;
 
@@ -539,26 +568,20 @@ static void truncate_text_chars()
    text_chars -= del_chars;
    len = gtk_text_buffer_get_char_count(textbuf);
    gtk_text_iter_set_offset(&iter, len);
-
-/*
-   gtk_text_set_point(GTK_TEXT(text1), del_chars);
-   gtk_text_backward_delete(GTK_TEXT(text1), del_chars);
-   text_chars -= del_chars;
-   gtk_text_set_point(GTK_TEXT(text1), text_chars);
- */
 }
 
-void set_textf(char *fmt, ...)
+void set_textf(const char *fmt, ...)
 {
    va_list arg_ptr;
    char buf[1000];
    int len;
    va_start(arg_ptr, fmt);
    len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
+   va_end(arg_ptr);
    set_text(buf, len);
 }
 
-void set_text(char *buf, int len)
+void set_text(const char *buf, int len)
 {
    GtkTextBuffer *textbuf;
    GtkTextIter iter;
@@ -568,42 +591,52 @@ void set_text(char *buf, int len)
    buf_len = gtk_text_buffer_get_char_count(textbuf);
    gtk_text_buffer_get_iter_at_offset(textbuf, &iter, buf_len - 1);
    gtk_text_buffer_insert(textbuf, &iter, buf, -1);
-/*
-   gtk_text_insert(GTK_TEXT(text1), text_font, NULL, NULL, buf, -1);
- */
    text_chars += len;
    if (text_chars > MAX_TEXT_CHARS) {
       truncate_text_chars();
    }
-   buf_len = gtk_text_buffer_get_char_count(textbuf);
-   gtk_text_iter_set_offset(&iter, buf_len);
-   /*
-    * Force the scroll bars to the bottom so that most
-    * recent text is on screen.
-    */
-   vadj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(scroll1));
-   gtk_adjustment_set_value(vadj, vadj->upper);
+   set_scroll_bar_to_end();
 }
 
-void set_statusf(char *fmt, ...)
+void set_statusf(const char *fmt, ...)
 {
    va_list arg_ptr;
    char buf[1000];
    int len;
    va_start(arg_ptr, fmt);
    len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
+   va_end(arg_ptr);
    gtk_label_set_text(GTK_LABEL(status1), buf);
-   ready = FALSE;
+// set_scroll_bar_to_end();
+   ready = false;
 }
 
 void set_status_ready()    
 {
    gtk_label_set_text(GTK_LABEL(status1), " Ready");
-   ready = TRUE;
+   ready = true;
+// set_scroll_bar_to_end();
 }
 
-void set_status(char *buf)
+void set_status(const char *buf)
 {
    gtk_label_set_text(GTK_LABEL(status1), buf);
-   ready = FALSE;
+// set_scroll_bar_to_end();
+   ready = false;
+}
+
+static void set_scroll_bar_to_end(void)
+{
+   GtkTextBuffer* textbuf = NULL;
+   GtkTextIter iter;
+   guint buf_len;
+
+   textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text1));
+   buf_len = gtk_text_buffer_get_char_count(textbuf);
+   gtk_text_buffer_get_iter_at_offset(textbuf, &iter, buf_len - 1);
+   gtk_text_iter_set_offset(&iter, buf_len);
+   gtk_text_buffer_place_cursor(textbuf, &iter);
+   gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(text1),
+              gtk_text_buffer_get_mark(textbuf, "insert"), 
+             0, TRUE, 0.0, 1.0);
 }