2011-10-17 [colin] 3.7.10cvs31
[claws.git] / src / action.c
index 60655d7cbd91ad06e811180f854ff3e7608b53a3..ee0b6f0ff7ccbd8baeb936a49dec76e16430c516 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2009 Hiroyuki Yamamoto & The Claws Mail Team
+ * Copyright (C) 1999-2011 Hiroyuki Yamamoto & The Claws Mail Team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -89,6 +89,7 @@ struct _ChildInfo
        Children        *children;
        gchar           *cmd;
        pid_t            pid;
+       gint             next_sig;
        gint             chld_in;
        gint             chld_out;
        gint             chld_err;
@@ -179,13 +180,13 @@ static gint io_dialog_key_pressed_cb      (GtkWidget      *widget,
 
 static void catch_output               (gpointer                data,
                                         gint                    source,
-                                        GdkInputCondition       cond);
+                                        GIOCondition            cond);
 static void catch_input                        (gpointer                data, 
                                         gint                    source,
-                                        GdkInputCondition       cond);
+                                        GIOCondition            cond);
 static void catch_status               (gpointer                data,
                                         gint                    source,
-                                        GdkInputCondition       cond);
+                                        GIOCondition            cond);
 
 static gchar *get_user_string          (const gchar    *action,
                                         ActionType      type);
@@ -475,15 +476,22 @@ void action_update_compose_menu(GtkUIManager *ui_manager,
 
 static GtkWidget *find_item_in_menu(GtkWidget *menu, gchar *name)
 {
-       GList *amenu = GTK_MENU_SHELL(menu)->children;
+       GList *children = gtk_container_get_children(GTK_CONTAINER(GTK_MENU_SHELL(menu)));
+       GList *amenu = children;
        const gchar *existing_name;
        while (amenu) {
                GtkWidget *item = GTK_WIDGET(amenu->data);
                if ((existing_name = g_object_get_data(G_OBJECT(item), "s_name")) != NULL &&
                    !strcmp2(name, existing_name))
+               {
+                       g_list_free(children);
                         return item;
+               }
                amenu = amenu->next;
        }
+
+       g_list_free(children);
+
        return NULL;
 }
 
@@ -897,7 +905,7 @@ static gboolean execute_actions(gchar *action, GSList *msg_list,
                        child_info->data = data;
                        child_info->tag_status = 
                                claws_input_add(child_info->chld_status,
-                                             GDK_INPUT_READ,
+                                             G_IO_IN | G_IO_HUP | G_IO_ERR,
                                              catch_status, child_info,
                                              FALSE);
                }
@@ -947,13 +955,10 @@ static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
 
        pid = fork();
        if (pid == 0) { /* Child */
+               int r = 0;
                if (setpgid(0, 0))
                        perror("setpgid");
 
-#ifdef GDK_WINDOWING_X11
-               (void)close(ConnectionNumber(gdk_display_get_default()));
-#endif /* GDK_WINDOWING_X11 */
-
                gch_pid = fork();
 
                if (gch_pid == 0) {
@@ -965,21 +970,24 @@ static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
                                    (ACTION_PIPE_IN |
                                     ACTION_USER_IN |
                                     ACTION_USER_HIDDEN_IN)) {
-                                       (void)close(fileno(stdin));
-                                       (void)dup  (chld_in[0]);
+                                       r |= close(fileno(stdin));
+                                       (void) dup  (chld_in[0]);
                                }
-                               (void)close(chld_in[0]);
-                               (void)close(chld_in[1]);
-
-                               (void)close(fileno(stdout));
-                               (void)dup  (chld_out[1]);
-                               (void)close(chld_out[0]);
-                               (void)close(chld_out[1]);
-
-                               (void)close(fileno(stderr));
-                               (void)dup  (chld_err[1]);
-                               (void)close(chld_err[0]);
-                               (void)close(chld_err[1]);
+                               r |= close(chld_in[0]);
+                               r |= close(chld_in[1]);
+
+                               r |= close(fileno(stdout));
+                               (void) dup  (chld_out[1]);
+                               r |= close(chld_out[0]);
+                               r |= close(chld_out[1]);
+
+                               r |= close(fileno(stderr));
+                               (void) dup  (chld_err[1]);
+                               r |= close(chld_err[0]);
+                               r |= close(chld_err[1]);
+
+                               if (r != 0)
+                                       debug_print("%s(%d)", strerror(errno), errno);
                        }
 
                        cmdline[0] = "sh";
@@ -999,24 +1007,29 @@ static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
                        _exit(1);
                } else if (gch_pid < (pid_t) 0) { /* Fork error */
                        if (sync)
-                               (void)write(chld_status[1], "1\n", 2);
+                               r = write(chld_status[1], "1\n", 2);
+                       if (r != 0)
+                               debug_print("%s(%d)", strerror(errno), errno);
                        perror("fork");
                        _exit(1);
                } else { /* Child */
                        if (sync) {
-                               (void)close(chld_in[0]);
-                               (void)close(chld_in[1]);
-                               (void)close(chld_out[0]);
-                               (void)close(chld_out[1]);
-                               (void)close(chld_err[0]);
-                               (void)close(chld_err[1]);
-                               (void)close(chld_status[0]);
+                               r |= close(chld_in[0]);
+                               r |= close(chld_in[1]);
+                               r |= close(chld_out[0]);
+                               r |= close(chld_out[1]);
+                               r |= close(chld_err[0]);
+                               r |= close(chld_err[1]);
+                               r |= close(chld_status[0]);
 
                                debug_print("Child: waiting for grandchild\n");
-                               waitpid(gch_pid, NULL, 0);
+                               r |= waitpid(gch_pid, NULL, 0);
                                debug_print("Child: grandchild ended\n");
-                               (void)write(chld_status[1], "0\n", 2);
-                               (void)close(chld_status[1]);
+                               r |= write(chld_status[1], "0\n", 2);
+                               r |= close(chld_status[1]);
+
+                               if (r != 0)
+                                       debug_print("%s(%d)", strerror(errno), errno);
                        }
                        _exit(0);
                }
@@ -1047,6 +1060,7 @@ static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
        child_info->children    = children;
 
        child_info->pid         = pid;
+       child_info->next_sig    = SIGTERM;
        child_info->cmd         = g_strdup(cmd);
        child_info->new_out     = FALSE;
        child_info->output      = g_string_new(NULL);
@@ -1058,9 +1072,9 @@ static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
        child_info->chld_err    = chld_err[0];
        child_info->chld_status = chld_status[0];
        child_info->tag_in      = -1;
-       child_info->tag_out     = claws_input_add(chld_out[0], GDK_INPUT_READ,
+       child_info->tag_out     = claws_input_add(chld_out[0], G_IO_IN | G_IO_HUP | G_IO_ERR,
                                                catch_output, child_info, FALSE);
-       child_info->tag_err     = claws_input_add(chld_err[0], GDK_INPUT_READ,
+       child_info->tag_err     = claws_input_add(chld_err[0], G_IO_IN | G_IO_HUP | G_IO_ERR,
                                                catch_output, child_info, FALSE);
 
        if (!(children->action_type &
@@ -1068,17 +1082,20 @@ static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
                return child_info;
 
        if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
+               int r;
                ret_str = g_locale_from_utf8(msg_str, strlen(msg_str),
                                             &by_read, &by_written, NULL);
                if (ret_str && by_written) {
-                       (void)write(chld_in[1], ret_str, strlen(ret_str));
+                       r = write(chld_in[1], ret_str, strlen(ret_str));
                        g_free(ret_str);
                } else
-                       (void)write(chld_in[1], msg_str, strlen(msg_str));
+                       r = write(chld_in[1], msg_str, strlen(msg_str));
                if (!(children->action_type &
                      (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
-                       (void)close(chld_in[1]);
+                       r = close(chld_in[1]);
                child_info->chld_in = -1; /* No more input */
+               if (r != 0)
+                       debug_print("%s(%d)", strerror(errno), errno);
        }
 
        return child_info;
@@ -1097,8 +1114,9 @@ static void kill_children_cb(GtkWidget *widget, gpointer data)
        for (cur = children->list; cur; cur = cur->next) {
                child_info = (ChildInfo *)(cur->data);
                debug_print("Killing child group id %d\n", child_info->pid);
-               if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
+               if (child_info->pid && kill(-child_info->pid, child_info->next_sig) < 0)
                        perror("kill");
+               child_info->next_sig = SIGKILL;
        }
 #endif /* G_OS_UNIX */
 }
@@ -1140,7 +1158,7 @@ static void send_input(GtkWidget *w, gpointer data)
        ChildInfo *child_info = (ChildInfo *) children->list->data;
 
        child_info->tag_in = claws_input_add(child_info->chld_in,
-                                          GDK_INPUT_WRITE,
+                                          G_IO_OUT | G_IO_ERR,
                                           catch_input, children, FALSE);
 }
 
@@ -1167,9 +1185,9 @@ static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
                                     gpointer data)
 {
-       if (event && (event->keyval == GDK_Escape ||
-                     event->keyval == GDK_Return ||
-                         event->keyval == GDK_KP_Enter))
+       if (event && (event->keyval == GDK_KEY_Escape ||
+                     event->keyval == GDK_KEY_Return ||
+                         event->keyval == GDK_KEY_KP_Enter))
                hide_io_dialog_cb(widget, data);
        return TRUE;
 }
@@ -1231,17 +1249,23 @@ static void update_io_dialog(Children *children)
 
        if (children->progress_bar) {
                gchar *text;
+#ifdef GENERIC_UMPC
+               /* use a more compact format */
+               const gchar *format = "%s %d/%d";
+#else
+               const gchar *format = "%s %d / %d";
+#endif
                
                gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar),
                                                  (children->initial_nb == 0) ? 0 :
                                              (gdouble) (children->initial_nb - children->nb) /
                                              (gdouble) children->initial_nb);
-               text = g_strdup_printf("%s %d/%d", _("Completed"), 
+               text = g_strdup_printf(format, _("Completed"), 
                                       children->initial_nb - children->nb,
                                       children->initial_nb);
                gtk_progress_bar_set_text(GTK_PROGRESS_BAR(children->progress_bar), text);
                g_free(text);
-       }                                             
+       }
 
        if (!children->nb) {
                gtk_widget_set_sensitive(children->abort_btn, FALSE);
@@ -1307,7 +1331,7 @@ static void create_io_dialog(Children *children)
 
        dialog = gtk_dialog_new();
        gtk_container_set_border_width
-               (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
+               (GTK_CONTAINER(gtk_dialog_get_action_area(GTK_DIALOG(dialog))), 5);
        gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
        gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
        gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
@@ -1319,7 +1343,8 @@ static void create_io_dialog(Children *children)
                         children);
 
        vbox = gtk_vbox_new(FALSE, 8);
-       gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
+       gtk_container_add(GTK_CONTAINER(
+                               gtk_dialog_get_content_area(GTK_DIALOG(dialog))), vbox);
        gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
        gtk_widget_show(vbox);
 
@@ -1387,12 +1412,25 @@ static void create_io_dialog(Children *children)
        }
 
        if (children->initial_nb > 1) {
-               gchar * text;
+               gchar *text;
+#ifdef GENERIC_UMPC
+               /* use a more compact format */
+               const gchar *format = "%s 0/%d\n";
+#else
+               const gchar *format = "%s 0 / %d\n";
+#endif
                
                progress_bar = gtk_progress_bar_new();
+#if !GTK_CHECK_VERSION(3, 0, 0)
                gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
                                GTK_PROGRESS_LEFT_TO_RIGHT);
-               text = g_strdup_printf("%s 0/%d\n", _("Completed"), 
+#else
+               gtk_orientable_set_orientation(GTK_PROGRESS_BAR(progress_bar),
+                               GTK_ORIENTATION_HORIZONTAL);
+               gtk_progress_bar_set_inverted(GTK_PROGRESS_BAR(progress_bar),
+                               FALSE);
+#endif
+               text = g_strdup_printf(format, _("Completed"), 
                                       children->initial_nb);
                gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar),
                                          text);
@@ -1412,7 +1450,8 @@ static void create_io_dialog(Children *children)
        if (children->nb)
                gtk_widget_set_sensitive(close_button, FALSE);
 
-       gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
+       gtk_container_add(GTK_CONTAINER(
+                       gtk_dialog_get_action_area(GTK_DIALOG(dialog))), hbox);
 
        children->dialog       = dialog;
        children->scrolledwin  = scrolledwin;
@@ -1426,7 +1465,7 @@ static void create_io_dialog(Children *children)
        gtk_widget_show(dialog);
 }
 
-static void catch_status(gpointer data, gint source, GdkInputCondition cond)
+static void catch_status(gpointer data, gint source, GIOCondition cond)
 {
        ChildInfo *child_info = (ChildInfo *)data;
        gchar buf;
@@ -1494,16 +1533,16 @@ static void catch_status(gpointer data, gint source, GdkInputCondition cond)
        wait_for_children(child_info->children);
 }
        
-static void catch_input(gpointer data, gint source, GdkInputCondition cond)
+static void catch_input(gpointer data, gint source, GIOCondition cond)
 {
        Children *children = (Children *)data;
        ChildInfo *child_info = (ChildInfo *)children->list->data;
        gchar *input, *ret_str;
-       gint c, count, len;
+       gint c, count, len, r;
        gssize by_read = 0, by_written = 0;
 
        debug_print("Sending input to grand child.\n");
-       if (!(cond && GDK_INPUT_WRITE))
+       if (!(cond & (G_IO_OUT | G_IO_ERR)))
                return;
 
        gtk_widget_set_sensitive(children->input_hbox, FALSE);
@@ -1531,16 +1570,18 @@ static void catch_input(gpointer data, gint source, GdkInputCondition cond)
        } while (c >= 0 && count < len);
 
        if (c >= 0)
-               (void)write(child_info->chld_in, "\n", 2);
+               r = write(child_info->chld_in, "\n", 2);
 
        g_free(input);
 
-       (void)close(child_info->chld_in);
+       r = close(child_info->chld_in);
+       if (r != 0)
+               debug_print("%s(%d)", strerror(errno), errno);
        child_info->chld_in = -1;
        debug_print("Input to grand child sent.\n");
 }
 
-static void catch_output(gpointer data, gint source, GdkInputCondition cond)
+static void catch_output(gpointer data, gint source, GIOCondition cond)
 {
        ChildInfo *child_info = (ChildInfo *)data;
        gint c;