2007-03-16 [wwp] 2.8.1cvs19
[claws.git] / src / action.c
index dc1090559bc71816c98d0f253c913690fd679245..d807eecea39fd235d9d966689f8437d632afa89c 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2003 Hiroyuki Yamamoto & The Sylpheed Claws Team
+ * Copyright (C) 1999-2007 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
@@ -14,7 +14,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
 #include "defs.h"
 
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <gtk/gtk.h>
 #include <gdk/gdkkeysyms.h>
 #ifdef GDK_WINDOWING_X11
-#      include <gdk/gdkx.h>
+#  include <gdk/gdkx.h>
 #endif /* GDK_WINDOWING_X11 */
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
 #include <sys/types.h>
-#include <sys/wait.h>
+#if HAVE_SYS_WAIT_H
+#  include <sys/wait.h>
+#endif
 #include <signal.h>
 #include <unistd.h>
 
-#include "intl.h"
 #include "utils.h"
 #include "gtkutils.h"
 #include "manage_window.h"
@@ -52,6 +54,7 @@
 #include "textview.h"
 #include "matcher_parser.h" /* CLAWS */
 #include "filtering.h"
+#include "procheader.h"
 
 typedef struct _Children               Children;
 typedef struct _ChildInfo              ChildInfo;
@@ -97,6 +100,10 @@ struct _ChildInfo
        gint             new_out;
 
        GString         *output;
+       void (*callback)(void *data);
+       void *data;
+
+       GSList          *msginfo_list;
 };
 
 static void action_update_menu         (GtkItemFactory *ifactory,
@@ -123,7 +130,9 @@ static gboolean execute_actions             (gchar          *action,
                                         GSList         *msg_list, 
                                         GtkWidget      *text,
                                         gint            body_pos,
-                                        MimeInfo       *partinfo);
+                                        MimeInfo       *partinfo, 
+                                        void (*callback)(void *data),
+                                        void *data);
 
 static gchar *parse_action_cmd         (gchar          *action,
                                         MsgInfo        *msginfo,
@@ -335,20 +344,31 @@ static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
 {
        gchar *filename;
+       gchar *p, *q;
+       gchar escape_ch[] = "\\ ";
 
        g_return_val_if_fail(msginfo, FALSE);
 
        filename = procmsg_get_message_file(msginfo);
 
-       if (filename) {
-               g_string_append(cmd, filename);
-               g_free(filename);
-       } else {
+       if (!filename) {
                alertpanel_error(_("Could not get message file %d"),
                                 msginfo->msgnum);
                return FALSE;
        }
 
+       p = filename;
+       while ((q = strpbrk(p, "$\"`'\\ \t*?[]&|;<>()!#~")) != NULL) {
+               escape_ch[1] = *q;
+               *q = '\0';
+               g_string_append(cmd, p);
+               g_string_append(cmd, escape_ch);
+               p = q + 1;
+       }
+       g_string_append(cmd, p);
+
+       g_free(filename);
+
        return TRUE;
 }
 
@@ -380,7 +400,7 @@ static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
        g_free(filename);
 
        if (ret < 0) {
-               alertpanel_error(_("Can't get part of multipart message"));
+               alertpanel_error(_("Can't get part of multipart message: %s"), strerror(-ret));
                g_free(part_filename);
                return FALSE;
        }
@@ -460,8 +480,9 @@ static void action_update_menu(GtkItemFactory *ifactory,
                action   = g_strdup((gchar *)cur->data);
                action_p = strstr(action, ": ");
                if (action_p && action_p[2] &&
-                   action_get_type(&action_p[2]) != ACTION_ERROR) {
-                       action_p[0] = 0x00;
+                   (action_get_type(&action_p[2]) != ACTION_ERROR) &&
+                   (action[0] != '/')) {
+                       action_p[0] = '\0';
                        menu_path = g_strdup_printf("%s/%s", branch_path,
                                                    action);
                        ifentry.path = menu_path;
@@ -498,7 +519,8 @@ static void compose_actions_execute_cb(Compose *compose, guint action_nb,
                return;
        }
 
-       execute_actions(action, NULL, compose->text, 0, NULL);
+       execute_actions(action, NULL, compose->text, 0, NULL, 
+               compose_action_cb, compose);
 }
 
 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
@@ -560,7 +582,8 @@ static void message_actions_execute(MessageView *msgview, guint action_nb,
                 * filtering */
                execute_filtering_actions(action, msg_list);
        else
-               execute_actions(action, msg_list, text, body_pos, partinfo);
+               execute_actions(action, msg_list, text, body_pos, partinfo,
+                       NULL, NULL);
 }
 
 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
@@ -568,7 +591,14 @@ static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
        GSList *action_list, *p;
        const gchar *sbegin, *send;
        gchar *action_string;
-       
+       SummaryView *summaryview = NULL;
+       MainWindow *mainwin = NULL;
+
+       if (mainwindow_get_mainwindow()) {
+               summaryview = mainwindow_get_mainwindow()->summaryview;
+               mainwin = mainwindow_get_mainwindow();
+       }
+
        if (NULL == (sbegin = strstr2(action, "%as{")))
                return FALSE;
        sbegin += sizeof "%as{" - 1;
@@ -577,13 +607,41 @@ static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
        action_string = g_strndup(sbegin, send - sbegin);
        
        action_list = matcher_parser_get_action_list(action_string);
+       if (action_list == NULL) {
+               gchar *tmp = g_strdup(action_string);
+
+               g_strstrip(tmp);
+               if (*tmp == '\0')
+                       alertpanel_error(_("There is no filtering action set"));
+               else
+                       alertpanel_error(_("Invalid filtering action(s):\n%s"), tmp);
+               g_free(action_string);
+               g_free(tmp);
+               return FALSE;
+       }
        g_free(action_string);
-       if (action_list == NULL) return FALSE;
        
        /* apply actions on each message info */
-       for (p = msglist; p && p->data; p = g_slist_next(p))
+       for (p = msglist; p && p->data; p = g_slist_next(p)) {
                filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
-               
+       }
+
+       if (summaryview) {
+               summary_lock(summaryview);                              
+               main_window_cursor_wait(mainwin);               
+               summary_freeze(summaryview);    
+               folder_item_update_freeze();                            
+       }
+
+       filtering_move_and_copy_msgs(msglist);
+
+       if (summaryview) {
+               folder_item_update_thaw();                              
+               summary_thaw(summaryview);              
+               main_window_cursor_normal(mainwin);     
+               summary_unlock(summaryview);                            
+               summary_show(summaryview, summaryview->folder_item);
+       }
        for (p = action_list; p; p = g_slist_next(p))
                if (p->data) filteringaction_free(p->data);     
        g_slist_free(action_list);              
@@ -592,7 +650,8 @@ static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
 
 static gboolean execute_actions(gchar *action, GSList *msg_list,
                                GtkWidget *text,
-                               gint body_pos, MimeInfo *partinfo)
+                               gint body_pos, MimeInfo *partinfo,
+                               void (*callback)(void *data), void *data)
 {
        GSList *children_list = NULL;
        gint is_ok  = TRUE;
@@ -637,18 +696,17 @@ static gboolean execute_actions(gchar *action, GSList *msg_list,
                GtkTextBuffer *textbuf;
 
                textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
-               is_selection = gtk_text_buffer_get_selection_bounds(textbuf,
-                                                                   &start_iter,
-                                                                   &end_iter);
+               is_selection = gtk_text_buffer_get_selection_bounds
+                       (textbuf, &start_iter, &end_iter);
                if (!is_selection) {
-                       gtk_text_buffer_get_start_iter(textbuf, &start_iter);
+                       gtk_text_buffer_get_iter_at_offset
+                               (textbuf, &start_iter, body_pos);
                        gtk_text_buffer_get_end_iter(textbuf, &end_iter);
                }
-               msg_str = gtk_text_buffer_get_text(textbuf,
-                                                  &start_iter, &end_iter,
-                                                  FALSE);
+               msg_str = gtk_text_buffer_get_text
+                       (textbuf, &start_iter, &end_iter, FALSE);
                if (is_selection)
-                       sel_str = g_strdup (msg_str);
+                       sel_str = g_strdup(msg_str);
        }
 
        if (action_type & ACTION_USER_STR) {
@@ -704,6 +762,10 @@ static gboolean execute_actions(gchar *action, GSList *msg_list,
                                break;
                        }
                        if ((child_info = fork_child(cmd, msg_str, children))) {
+                               /* Pass msginfo to catch_status () */
+                               if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
+                                       child_info->msginfo_list = 
+                                               g_slist_append (NULL, msginfo);
                                children_list = g_slist_append(children_list,
                                                               child_info);
                        }
@@ -714,8 +776,11 @@ static gboolean execute_actions(gchar *action, GSList *msg_list,
                                       user_str, user_hidden_str, sel_str);
                if (cmd) {
                        if ((child_info = fork_child(cmd, msg_str, children))) {
+                               if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
+                                       child_info->msginfo_list = 
+                                               g_slist_copy (msg_list);
                                children_list = g_slist_append(children_list,
-                                                              child_info);
+                                                               child_info);
                        }
                        g_free(cmd);
                } else
@@ -739,6 +804,8 @@ static gboolean execute_actions(gchar *action, GSList *msg_list,
 
                for (cur = children_list; cur; cur = cur->next) {
                        child_info = (ChildInfo *) cur->data;
+                       child_info->callback = callback;
+                       child_info->data = data;
                        child_info->tag_status = 
                                gdk_input_add(child_info->chld_status,
                                              GDK_INPUT_READ,
@@ -747,18 +814,19 @@ static gboolean execute_actions(gchar *action, GSList *msg_list,
 
                create_io_dialog(children);
        }
-
        return is_ok;
 }
 
 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
                             Children *children)
 {
+#ifdef G_OS_UNIX
        gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
-       gchar *cmdline[4];
+       gchar *cmdline[4], *ret_str;
        pid_t pid, gch_pid;
        ChildInfo *child_info;
        gint sync;
+       gssize by_written = 0, by_read = 0;
 
        sync = !(children->action_type & ACTION_ASYNC);
 
@@ -785,6 +853,7 @@ static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
        }
 
        debug_print("Forking child and grandchild.\n");
+       debug_print("Executing: /bin/sh -c %s\n", cmd);
 
        pid = fork();
        if (pid == 0) { /* Child */
@@ -825,11 +894,18 @@ static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
 
                        cmdline[0] = "sh";
                        cmdline[1] = "-c";
-                       cmdline[2] = cmd;
-                       cmdline[3] = 0;
+                       ret_str = g_locale_from_utf8(cmd, strlen(cmd),
+                                                    &by_read, &by_written,
+                                                    NULL);
+                       if (ret_str && by_written)
+                               cmdline[2] = ret_str;
+                       else
+                               cmdline[2] = cmd;
+                       cmdline[3] = NULL;
                        execvp("/bin/sh", cmdline);
 
                        perror("execvp");
+                       g_free(ret_str);
                        _exit(1);
                } else if (gch_pid < (pid_t) 0) { /* Fork error */
                        if (sync)
@@ -846,7 +922,7 @@ static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
                                close(chld_err[1]);
                                close(chld_status[0]);
 
-                               debug_print("Child: Waiting for grandchild\n");
+                               debug_print("Child: waiting for grandchild\n");
                                waitpid(gch_pid, NULL, 0);
                                debug_print("Child: grandchild ended\n");
                                write(chld_status[1], "0\n", 2);
@@ -902,7 +978,13 @@ static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
                return child_info;
 
        if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
-               write(chld_in[1], msg_str, strlen(msg_str));
+               ret_str = g_locale_from_utf8(msg_str, strlen(msg_str),
+                                            &by_read, &by_written, NULL);
+               if (ret_str && by_written) {
+                       write(chld_in[1], ret_str, strlen(ret_str));
+                       g_free(ret_str);
+               } else
+                       write(chld_in[1], msg_str, strlen(msg_str));
                if (!(children->action_type &
                      (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
                        close(chld_in[1]);
@@ -910,10 +992,14 @@ static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
        }
 
        return child_info;
+#else
+       return NULL;
+#endif /* G_OS_UNIX */
 }
 
 static void kill_children_cb(GtkWidget *widget, gpointer data)
 {
+#ifdef G_OS_UNIX
        GSList *cur;
        Children *children = (Children *) data;
        ChildInfo *child_info;
@@ -924,6 +1010,7 @@ static void kill_children_cb(GtkWidget *widget, gpointer data)
                if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
                        perror("kill");
        }
+#endif /* G_OS_UNIX */
 }
 
 static gint wait_for_children(Children *children)
@@ -953,11 +1040,8 @@ static gint wait_for_children(Children *children)
        if (children->nb)
                return FALSE;
 
-       if (!children->dialog) {
+       if (!children->dialog)
                free_children(children);
-       } else if (!children->output) {
-               gtk_widget_destroy(children->dialog);
-       }
 
        return FALSE;
 }
@@ -970,7 +1054,6 @@ static void send_input(GtkWidget *w, gpointer data)
        child_info->tag_in = gdk_input_add(child_info->chld_in,
                                           GDK_INPUT_WRITE,
                                           catch_input, children);
-       gtk_widget_set_sensitive(children->input_hbox, FALSE);
 }
 
 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
@@ -996,7 +1079,8 @@ 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)
+       if (event && (event->keyval == GDK_Escape ||
+                     event->keyval == GDK_Return))
                hide_io_dialog_cb(widget, data);
        return TRUE;
 }
@@ -1028,6 +1112,8 @@ static void childinfo_close_pipes(ChildInfo *child_info)
 static void free_children(Children *children)
 {
        ChildInfo *child_info;
+       void (*callback)(void *data) = NULL;
+       void *data = NULL;
 
        debug_print("Freeing children data %p\n", children);
 
@@ -1037,8 +1123,14 @@ static void free_children(Children *children)
                g_free(child_info->cmd);
                g_string_free(child_info->output, TRUE);
                children->list = g_slist_remove(children->list, child_info);
+               callback = child_info->callback;
+               data = child_info->data;
                g_free(child_info);
        }
+
+       if (callback)
+               callback(data);
+       
        g_free(children);
 }
 
@@ -1049,10 +1141,18 @@ static void update_io_dialog(Children *children)
        debug_print("Updating actions input/output dialog.\n");
 
        if (children->progress_bar) {
-               gtk_progress_configure(GTK_PROGRESS(children->progress_bar),
-                               children->initial_nb -children->nb,
-                               0.0, children->initial_nb);
-       }
+               gchar *text;
+               
+               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"), 
+                                      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);
@@ -1068,19 +1168,17 @@ static void update_io_dialog(Children *children)
 
        if (children->output) {
                GtkWidget *text = children->text;
-               gchar *caption;
-               ChildInfo *child_info;
                GtkTextBuffer *textbuf;
                GtkTextIter iter, start_iter, end_iter;
-               GdkFont *font;
+               gchar *caption;
+               ChildInfo *child_info;
 
-               font = g_object_get_data(G_OBJECT(children->dialog), 
-                                        "s_txtfont");
                gtk_widget_show(children->scrolledwin);
-               textbuf = gtk_text_view_get_buffer (GTK_TEXT_VIEW(text));
-               gtk_text_buffer_get_start_iter (textbuf, &start_iter);
-               gtk_text_buffer_get_end_iter (textbuf, &end_iter);
-               iter = start_iter;
+               textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
+               gtk_text_buffer_get_bounds(textbuf, &start_iter, &end_iter);
+               gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
+               gtk_text_buffer_get_start_iter(textbuf, &iter);
+
                for (cur = children->list; cur; cur = cur->next) {
                        child_info = (ChildInfo *)cur->data;
                        if (child_info->pid)
@@ -1092,8 +1190,7 @@ static void update_io_dialog(Children *children)
                                        (_("--- Ended: %s\n"),
                                         child_info->cmd);
 
-                       gtk_text_buffer_insert(textbuf, &iter,
-                                              caption, -1);
+                       gtk_text_buffer_insert(textbuf, &iter, caption, -1);
                        gtk_text_buffer_insert(textbuf, &iter,
                                               child_info->output->str, -1);
                        g_free(caption);
@@ -1116,7 +1213,6 @@ static void create_io_dialog(Children *children)
        GtkWidget *progress_bar = NULL;
        GtkWidget *abort_button;
        GtkWidget *close_button;
-       GdkFont   *output_font;
 
        debug_print("Creating action IO dialog\n");
 
@@ -1144,13 +1240,30 @@ static void create_io_dialog(Children *children)
 
        scrolledwin = gtk_scrolled_window_new(NULL, NULL);
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
-                                      GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
+                                      GTK_POLICY_AUTOMATIC,
+                                      GTK_POLICY_AUTOMATIC);
+       gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
+                                           GTK_SHADOW_IN);
        gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
-       gtk_widget_set_size_request(scrolledwin, 480, 200);
+       gtk_widget_set_size_request(scrolledwin, 560, 200);
        gtk_widget_hide(scrolledwin);
 
        text = gtk_text_view_new();
+
+       if (prefs_common.textfont) {
+               PangoFontDescription *font_desc;
+               font_desc = pango_font_description_from_string
+                       (prefs_common.textfont);
+               if (font_desc) {
+                       gtk_widget_modify_font(text, font_desc);
+                       pango_font_description_free(font_desc);
+               }
+       }
+
        gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
+       gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
+       gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
+       gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
        gtk_container_add(GTK_CONTAINER(scrolledwin), text);
        gtk_widget_show(text);
 
@@ -1167,7 +1280,7 @@ static void create_io_dialog(Children *children)
                        gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
                gtk_widget_show(entry);
 
-               send_button = gtk_button_new_with_label(_(" Send "));
+               send_button = gtk_button_new_from_stock(GTK_STOCK_EXECUTE);
                g_signal_connect(G_OBJECT(send_button), "clicked",
                                 G_CALLBACK(send_input), children);
                gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
@@ -1179,26 +1292,22 @@ static void create_io_dialog(Children *children)
        }
 
        if (children->initial_nb > 1) {
+               gchar * text;
+               
                progress_bar = gtk_progress_bar_new();
-               gtk_progress_bar_set_bar_style(GTK_PROGRESS_BAR(progress_bar),
-                               GTK_PROGRESS_CONTINUOUS);
                gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
                                GTK_PROGRESS_LEFT_TO_RIGHT);
-               gtk_progress_set_activity_mode(GTK_PROGRESS(progress_bar), 
-                               FALSE);
-               gtk_progress_set_format_string(GTK_PROGRESS(progress_bar),
-                               _("Completed %v/%u"));
-               gtk_progress_set_show_text(GTK_PROGRESS(progress_bar), TRUE);
-               gtk_progress_configure(GTK_PROGRESS(progress_bar),
-                               children->initial_nb -children->nb,
-                               0.0, children->initial_nb);
-
+               text = g_strdup_printf("%s 0/%d\n", _("Completed"), 
+                                      children->initial_nb);
+               gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar),
+                                         text);
+               g_free(text);
                gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
                gtk_widget_show(progress_bar);
        }
 
-       gtkut_button_set_create(&hbox, &abort_button, _("Abort"),
-                               &close_button, _("Close"), NULL, NULL);
+       gtkut_stock_button_set_create(&hbox, &abort_button, GTK_STOCK_STOP,
+                                     &close_button, GTK_STOCK_CLOSE, NULL, NULL);
        g_signal_connect(G_OBJECT(abort_button), "clicked",
                         G_CALLBACK(kill_children_cb), children);
        g_signal_connect(G_OBJECT(close_button), "clicked",
@@ -1233,10 +1342,57 @@ static void catch_status(gpointer data, gint source, GdkInputCondition cond)
        c = read(source, &buf, 1);
        debug_print("Child returned %c\n", buf);
 
+#ifdef G_OS_UNIX
        waitpid(-child_info->pid, NULL, 0);
+#endif
        childinfo_close_pipes(child_info);
        child_info->pid = 0;
 
+       if (child_info->children->action_type & (ACTION_SINGLE | ACTION_MULTIPLE)
+           && child_info->msginfo_list) {
+               /* Actions on message *files* might change size and
+               * time stamp, and thus invalidate the cache */
+               SummaryView *summaryview  = NULL;
+               GSList      *cur;
+               MsgInfo     *msginfo, *nmi;     /* newmsginfo */
+               char        *file;
+               gboolean     modified_something = FALSE;
+               FolderItem  *last_item = NULL;
+               if (mainwindow_get_mainwindow ())
+                       summaryview = mainwindow_get_mainwindow ()->summaryview;
+               for (cur = child_info->msginfo_list; cur; cur = cur->next) {
+                       msginfo = (MsgInfo *)cur->data;
+                       if (!(msginfo && /* Stuff used valid? */
+                           msginfo->folder && msginfo->folder->cache)) 
+                               continue;
+                       file = procmsg_get_message_file_path (msginfo);
+                       if (!file) 
+                               continue;
+                       nmi = procheader_parse_file (file, msginfo->flags, TRUE, FALSE);
+                       if (!nmi) 
+                               continue; /* Deleted? */
+                       if (msginfo->mtime != nmi->mtime || msginfo->size != nmi->size) {
+                               nmi->folder = msginfo->folder;
+                               nmi->msgnum = msginfo->msgnum;
+                               msgcache_update_msg (msginfo->folder->cache, nmi);
+                               modified_something = TRUE;
+                               last_item = nmi->folder;
+                       }
+                       procmsg_msginfo_free (nmi);
+                       if (summaryview && summaryview->displayed &&
+                           summaryview->folder_item == msginfo->folder &&
+                           summary_get_msgnum(summaryview, summaryview->displayed) == msginfo->msgnum)
+                               summary_redisplay_msg(summaryview);
+                                       
+               }
+               if (modified_something && last_item && 
+                   summaryview && summaryview->folder_item == last_item) {
+                       summary_show (summaryview, summaryview->folder_item);
+               }
+               g_slist_free (child_info->msginfo_list);
+               child_info->msginfo_list = NULL;
+       }
+
        wait_for_children(child_info->children);
 }
        
@@ -1244,18 +1400,29 @@ static void catch_input(gpointer data, gint source, GdkInputCondition cond)
 {
        Children *children = (Children *)data;
        ChildInfo *child_info = (ChildInfo *)children->list->data;
-       gchar *input;
+       gchar *input, *ret_str;
        gint c, count, len;
+       gssize by_read = 0, by_written = 0;
 
        debug_print("Sending input to grand child.\n");
        if (!(cond && GDK_INPUT_WRITE))
                return;
 
+       gtk_widget_set_sensitive(children->input_hbox, FALSE);
+       gtk_widget_grab_focus(children->abort_btn);
+
        gdk_input_remove(child_info->tag_in);
        child_info->tag_in = -1;
 
        input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
                                       0, -1);
+       ret_str = g_locale_from_utf8(input, strlen(input), &by_read,
+                                    &by_written, NULL);
+       if (ret_str && by_written) {
+               g_free(input);
+               input = ret_str;
+       }
+
        len = strlen(input);
        count = 0;
 
@@ -1270,8 +1437,6 @@ static void catch_input(gpointer data, gint source, GdkInputCondition cond)
 
        g_free(input);
 
-       gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
-       gtk_widget_set_sensitive(children->input_hbox, TRUE);
        close(child_info->chld_in);
        child_info->chld_in = -1;
        debug_print("Input to grand child sent.\n");
@@ -1280,39 +1445,68 @@ static void catch_input(gpointer data, gint source, GdkInputCondition cond)
 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
 {
        ChildInfo *child_info = (ChildInfo *)data;
-       gint c, i;
+       gint c;
        gchar buf[BUFFSIZE];
 
        debug_print("Catching grand child's output.\n");
        if (child_info->children->action_type &
            (ACTION_PIPE_OUT | ACTION_INSERT)
            && source == child_info->chld_out) {
-               GtkTextView *text = GTK_TEXT_VIEW(child_info->children->msg_text);
-               GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
-               GtkTextIter iter1, iter2;
-               GtkTextMark *mark;
-               mark = gtk_text_buffer_get_insert(textbuf);
-               gtk_text_buffer_get_iter_at_mark(textbuf, &iter1, mark);
-               gtk_text_buffer_get_iter_at_mark(textbuf, &iter2, mark);
+               GtkTextView *text =
+                       GTK_TEXT_VIEW(child_info->children->msg_text);
+               GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
+               GtkTextIter iter;
+               GtkTextMark *mark;
+               gint ins_pos;
+
+               mark = gtk_text_buffer_get_insert(textbuf);
+               gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
+               ins_pos = gtk_text_iter_get_offset(&iter);
 
                while (TRUE) {
+                       gsize bytes_read = 0, bytes_written = 0;
+                       gchar *ret_str;
+
                        c = read(source, buf, sizeof(buf) - 1);
                        if (c == 0)
                                break;
-                       gtk_text_buffer_insert(textbuf, &iter2, buf, c);
+
+                       ret_str = g_locale_to_utf8
+                               (buf, c, &bytes_read, &bytes_written, NULL);
+                       if (ret_str && bytes_written > 0) {
+                               gtk_text_buffer_insert
+                                       (textbuf, &iter, ret_str,
+                                        -1);
+                               g_free(ret_str);
+                       } else
+                               gtk_text_buffer_insert(textbuf, &iter, buf, c);
                }
-               if (child_info->children->is_selection) {
-                       gtk_text_buffer_place_cursor(textbuf, &iter1);
-                       gtk_text_buffer_move_mark_by_name
-                               (textbuf, "selection_bound", &iter2);
+
+               if (child_info->children->is_selection) {
+                       GtkTextIter ins;
+
+                       gtk_text_buffer_get_iter_at_offset
+                               (textbuf, &ins, ins_pos);
+                       gtk_text_buffer_select_range(textbuf, &ins, &iter);
                }
        } else {
                c = read(source, buf, sizeof(buf) - 1);
-               for (i = 0; i < c; i++)
-                       g_string_append_c(child_info->output, buf[i]);
-               if (c > 0)
+               if (c > 0) {
+                       gsize bytes_read = 0, bytes_written = 0;
+                       gchar *ret_str;
+
+                       ret_str = g_locale_to_utf8
+                               (buf, c, &bytes_read, &bytes_written, NULL);
+                       if (ret_str && bytes_written > 0) {
+                               g_string_append_len
+                                       (child_info->output, ret_str,
+                                        bytes_written);
+                               g_free(ret_str);
+                       } else
+                               g_string_append_len(child_info->output, buf, c);
+
                        child_info->new_out = TRUE;
+               }
        }
        if (c == 0) {
                if (source == child_info->chld_out) {
@@ -1340,7 +1534,7 @@ static gchar *get_user_string(const gchar *action, ActionType type)
        case ACTION_USER_HIDDEN_STR:
                message = g_strdup_printf
                        (_("Enter the argument for the following action:\n"
-                          "(`%%h' will be replaced with the argument)\n"
+                          "('%%h' will be replaced with the argument)\n"
                           "  %s"),
                         action);
                user_str = input_dialog_with_invisible
@@ -1349,7 +1543,7 @@ static gchar *get_user_string(const gchar *action, ActionType type)
        case ACTION_USER_STR:
                message = g_strdup_printf
                        (_("Enter the argument for the following action:\n"
-                          "(`%%u' will be replaced with the argument)\n"
+                          "('%%u' will be replaced with the argument)\n"
                           "  %s"),
                         action);
                user_str = input_dialog