2006-02-07 [colin] 2.0.0cvs28
[claws.git] / src / action.c
index 1335baeea625882c36f8247100451ab94632765c..88a946e6638db41623cc52bf8007223f00777f9b 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2003 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2006 Hiroyuki Yamamoto & The Sylpheed Claws 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>
-#include <gdk/gdkx.h>
+#ifdef GDK_WINDOWING_X11
+#  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"
@@ -47,8 +51,9 @@
 #include "action.h"
 #include "compose.h"
 #include "procmsg.h"
-#include "gtkstext.h"
 #include "textview.h"
+#include "matcher_parser.h" /* CLAWS */
+#include "filtering.h"
 
 typedef struct _Children               Children;
 typedef struct _ChildInfo              ChildInfo;
@@ -60,25 +65,28 @@ struct _Children
        GtkWidget       *text;
        GtkWidget       *input_entry;
        GtkWidget       *input_hbox;
+       GtkWidget       *progress_bar;
        GtkWidget       *abort_btn;
        GtkWidget       *close_btn;
        GtkWidget       *scrolledwin;
 
        gchar           *action;
+       ActionType       action_type;
        GSList          *list;
        gint             nb;
+       gint             initial_nb;
        gint             open_in;
        gboolean         output;
 
        GtkWidget       *msg_text;
-       GdkFont         *msgfont;
+
+       gboolean         is_selection;
 };
 
 struct _ChildInfo
 {
        Children        *children;
        gchar           *cmd;
-       guint            type;
        pid_t            pid;
        gint             chld_in;
        gint             chld_out;
@@ -91,6 +99,8 @@ struct _ChildInfo
        gint             new_out;
 
        GString         *output;
+       void (*callback)(void *data);
+       void *data;
 };
 
 static void action_update_menu         (GtkItemFactory *ifactory,
@@ -110,12 +120,16 @@ static void message_actions_execute       (MessageView    *msgview,
                                         guint           action_nb,
                                         GSList         *msg_list);
 
+static gboolean execute_filtering_actions(gchar                *action, 
+                                         GSList        *msglist);
+
 static gboolean execute_actions                (gchar          *action, 
                                         GSList         *msg_list, 
                                         GtkWidget      *text,
-                                        GdkFont        *msgfont,
                                         gint            body_pos,
-                                        MimeInfo       *partinfo);
+                                        MimeInfo       *partinfo, 
+                                        void (*callback)(void *data),
+                                        void *data);
 
 static gchar *parse_action_cmd         (gchar          *action,
                                         MsgInfo        *msginfo,
@@ -132,7 +146,6 @@ static gboolean parse_append_msgpart        (GString        *cmd,
                                         MimeInfo       *partinfo);
 
 static ChildInfo *fork_child           (gchar          *cmd,
-                                        gint            action_type,
                                         const gchar    *msg_str,
                                         Children       *children);
 
@@ -190,8 +203,18 @@ ActionType action_get_type(const gchar *action_str)
                return ACTION_ERROR;
 
        while (*p && action_type != ACTION_ERROR) {
-               if (p[0] == '%') {
+               if (p[0] == '%' && p[1]) {
                        switch (p[1]) {
+                       case 'a':
+                               /* CLAWS: filtering action is a mutually exclusive
+                                * action. we can enable others if needed later. we
+                                * add ACTION_SINGLE | ACTION_MULTIPLE so it will
+                                * only be executed from the main window toolbar */
+                               if (p[2] == 's')  /* source messages */
+                                       action_type = ACTION_FILTERING_ACTION 
+                                                   | ACTION_SINGLE 
+                                                   | ACTION_MULTIPLE;
+                               break;
                        case 'f':
                                action_type |= ACTION_SINGLE;
                                break;
@@ -318,20 +341,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;
 }
 
@@ -344,23 +378,8 @@ static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
        gint ret;
 
        if (!partinfo) {
-               FILE *fp;
-#if USE_GPGME
-               if ((fp = procmsg_open_message_decrypted(msginfo, &partinfo))
-                   == NULL) {
-                       alertpanel_error(_("Could not get message file."));
-                       return FALSE;
-               }
-#else
-               if ((fp = procmsg_open_message(msginfo)) == NULL) {
-                       alertpanel_error(_("Could not get message file."));
-                       return FALSE;
-               }
-               partinfo = procmime_scan_mime_header(fp);
-#endif
-               fclose(fp);
+               partinfo = procmime_scan_message(msginfo);
                if (!partinfo) {
-                       procmime_mimeinfo_free_all(partinfo);
                        alertpanel_error(_("Could not get message part."));
                        return FALSE;
                }
@@ -371,7 +390,7 @@ static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
        filename = procmsg_get_message_file_path(msginfo);
        part_filename = procmime_get_tmp_file_name(partinfo);
 
-       ret = procmime_get_part(part_filename, filename, partinfo); 
+       ret = procmime_get_part(part_filename, partinfo);
 
        if (single_part)
                procmime_mimeinfo_free_all(partinfo);
@@ -403,25 +422,32 @@ void actions_execute(gpointer data,
                msgview_actions_execute_cb((MessageView*)data, action_nb, widget);      
 }
 
-void action_update_mainwin_menu(GtkItemFactory *ifactory, MainWindow *mainwin)
+void action_update_mainwin_menu(GtkItemFactory *ifactory,
+                               gchar *branch_path,
+                               MainWindow *mainwin)
 {
-       action_update_menu(ifactory, "/Tools/Actions",
+       action_update_menu(ifactory, branch_path,
                           mainwin_actions_execute_cb, mainwin);
 }
 
-void action_update_msgview_menu(GtkItemFactory *ifactory, MessageView *msgview)
+void action_update_msgview_menu(GtkItemFactory *ifactory,
+                               gchar *branch_path,
+                               MessageView *msgview)
 {
-       action_update_menu(ifactory, "/Tools/Actions",
+       action_update_menu(ifactory, branch_path,
                           msgview_actions_execute_cb, msgview);
 }
 
-void action_update_compose_menu(GtkItemFactory *ifactory, Compose *compose)
+void action_update_compose_menu(GtkItemFactory *ifactory, 
+                               gchar *branch_path,
+                               Compose *compose)
 {
-       action_update_menu(ifactory, "/Tools/Actions",
+       action_update_menu(ifactory, branch_path,
                           compose_actions_execute_cb, compose);
 }
 
-static void action_update_menu(GtkItemFactory *ifactory, gchar *branch_path,
+static void action_update_menu(GtkItemFactory *ifactory, 
+                              gchar *branch_path,
                               gpointer callback, gpointer data)
 {
        GtkWidget *menuitem;
@@ -451,8 +477,9 @@ static void action_update_menu(GtkItemFactory *ifactory, gchar *branch_path,
                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;
@@ -469,7 +496,7 @@ static void compose_actions_execute_cb(Compose *compose, guint action_nb,
                                       GtkWidget *widget)
 {
        gchar *buf, *action;
-       guint action_type;
+       ActionType action_type;
 
        g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
 
@@ -485,11 +512,12 @@ static void compose_actions_execute_cb(Compose *compose, guint action_nb,
        if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
                alertpanel_warning
                        (_("The selected action cannot be used in the compose window\n"
-                          "because it contains %%f, %%F or %%p."));
+                          "because it contains %%f, %%F, %%as or %%p."));
                return;
        }
 
-       execute_actions(action, NULL, compose->text, NULL, 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,
@@ -521,15 +549,15 @@ static void message_actions_execute(MessageView *msgview, guint action_nb,
        gchar *buf;
        gchar *action;
        GtkWidget *text = NULL;
-       GdkFont *msgfont = NULL;
        guint body_pos = 0;
-
+       ActionType action_type;
+       
        g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
 
        buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
 
        g_return_if_fail(buf);
-       g_return_if_fail(action = strstr(buf, ": "));
+       g_return_if_fail((action = strstr(buf, ": ")));
 
        /* Point to the beginning of the command-line */
        action += 2;
@@ -537,31 +565,94 @@ static void message_actions_execute(MessageView *msgview, guint action_nb,
        textview = messageview_get_current_textview(msgview);
        if (textview) {
                text     = textview->text;
-               msgfont  = textview->msgfont;
                body_pos = textview->body_pos;
        }
        partinfo = messageview_get_selected_mime_part(msgview);
 
-       execute_actions(action, msg_list, text, msgfont, body_pos, partinfo);
+       /* this command will alter the message text */
+       action_type = action_get_type(action);
+       if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
+               msgview->filtered = TRUE;
+
+       if (action_type & ACTION_FILTERING_ACTION) 
+               /* CLAWS: most of the above code is not necessary for applying
+                * filtering */
+               execute_filtering_actions(action, msg_list);
+       else
+               execute_actions(action, msg_list, text, body_pos, partinfo,
+                       NULL, NULL);
+}
+
+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;
+       if (NULL == (send = strrchr(sbegin, '}')))
+               return FALSE;
+       action_string = g_strndup(sbegin, send - sbegin);
+       
+       action_list = matcher_parser_get_action_list(action_string);
+       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)) {
+               filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
+               
+       }
+
+       if (summaryview) {
+               summary_lock(summaryview);                              
+               main_window_cursor_wait(mainwin);               
+               gtk_clist_freeze(GTK_CLIST(summaryview->ctree));        
+               folder_item_update_freeze();                            
+       }
+
+       filtering_move_and_copy_msgs(msglist);
+
+       if (summaryview) {
+               folder_item_update_thaw();                              
+               gtk_clist_thaw(GTK_CLIST(summaryview->ctree));          
+               main_window_cursor_normal(mainwin);     
+               summary_unlock(summaryview);                            
+       }
+       for (p = action_list; p; p = g_slist_next(p))
+               if (p->data) filteringaction_free(p->data);     
+       g_slist_free(action_list);              
+       return TRUE;    
 }
 
 static gboolean execute_actions(gchar *action, GSList *msg_list,
-                               GtkWidget *text, GdkFont *msgfont,
-                               gint body_pos, MimeInfo *partinfo)
+                               GtkWidget *text,
+                               gint body_pos, MimeInfo *partinfo,
+                               void (*callback)(void *data), void *data)
 {
        GSList *children_list = NULL;
        gint is_ok  = TRUE;
        gint msg_list_len;
        Children *children;
        ChildInfo *child_info;
-       gint action_type;
+       ActionType action_type;
        MsgInfo *msginfo;
        gchar *cmd;
-       guint start = 0, end = 0;
        gchar *sel_str = NULL;
        gchar *msg_str = NULL;
        gchar *user_str = NULL;
        gchar *user_hidden_str = NULL;
+       GtkTextIter start_iter, end_iter;
+       gboolean is_selection = FALSE;
 
        g_return_val_if_fail(action && *action, FALSE);
 
@@ -587,31 +678,21 @@ static gboolean execute_actions(gchar *action, GSList *msg_list,
                        return FALSE; /* ERR: selection string but no text */
        }
 
-       if (GTK_EDITABLE(text)->has_selection) {
-               start = GTK_EDITABLE(text)->selection_start_pos;
-               end   = GTK_EDITABLE(text)->selection_end_pos;
-               if (start > end) {
-                       guint tmp;
-                       tmp = start;
-                       start = end;
-                       end = tmp;
-               }
+       if (text) {
+               GtkTextBuffer *textbuf;
 
-               if (start == end) {
-                       start = body_pos;
-                       end = gtk_stext_get_length(GTK_STEXT(text));
-                       msg_str = gtk_editable_get_chars(GTK_EDITABLE(text),
-                                                        start, end);
-               } else {
-                       sel_str = gtk_editable_get_chars(GTK_EDITABLE(text),
-                                                        start, end);
-                       msg_str = g_strdup(sel_str);
+               textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
+               is_selection = gtk_text_buffer_get_selection_bounds
+                       (textbuf, &start_iter, &end_iter);
+               if (!is_selection) {
+                       gtk_text_buffer_get_iter_at_offset
+                               (textbuf, &start_iter, body_pos);
+                       gtk_text_buffer_get_end_iter(textbuf, &end_iter);
                }
-       } else {
-               start = body_pos;
-               end = gtk_stext_get_length(GTK_STEXT(text));
-               msg_str = gtk_editable_get_chars(GTK_EDITABLE(text),
-                                                start, end);
+               msg_str = gtk_text_buffer_get_text
+                       (textbuf, &start_iter, &end_iter, FALSE);
+               if (is_selection)
+                       sel_str = g_strdup(msg_str);
        }
 
        if (action_type & ACTION_USER_STR) {
@@ -632,17 +713,18 @@ static gboolean execute_actions(gchar *action, GSList *msg_list,
                }
        }
 
-       if (action_type & ACTION_PIPE_OUT) {
-               gtk_stext_freeze(GTK_STEXT(text));
-               gtk_stext_set_point(GTK_STEXT(text), start);
-               gtk_stext_forward_delete(GTK_STEXT(text), end - start);
-               gtk_stext_thaw(GTK_STEXT(text));
+       if (text && (action_type & ACTION_PIPE_OUT)) {
+               GtkTextBuffer *textbuf;
+               textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
+               gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
        }
 
        children = g_new0(Children, 1);
 
-       children->msg_text = text;
-       children->msgfont = msgfont;
+       children->action      = g_strdup(action);
+       children->action_type = action_type;
+       children->msg_text    = text;
+       children->is_selection = is_selection;
 
        if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
            ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
@@ -665,8 +747,7 @@ static gboolean execute_actions(gchar *action, GSList *msg_list,
                                is_ok  = FALSE; /* ERR: incorrect command */
                                break;
                        }
-                       if ((child_info = fork_child(cmd, action_type, msg_str,
-                                                    children))) {
+                       if ((child_info = fork_child(cmd, msg_str, children))) {
                                children_list = g_slist_append(children_list,
                                                               child_info);
                        }
@@ -676,8 +757,7 @@ static gboolean execute_actions(gchar *action, GSList *msg_list,
                cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
                                       user_str, user_hidden_str, sel_str);
                if (cmd) {
-                       if ((child_info = fork_child(cmd, action_type, msg_str,
-                                                    children))) {
+                       if ((child_info = fork_child(cmd, msg_str, children))) {
                                children_list = g_slist_append(children_list,
                                                               child_info);
                        }
@@ -693,17 +773,18 @@ static gboolean execute_actions(gchar *action, GSList *msg_list,
 
        if (!children_list) {
                 /* If not waiting for children, return */
-               g_free(children);
+               free_children(children);
        } else {
                GSList *cur;
 
-               children->action  = g_strdup(action);
-               children->dialog  = NULL;
-               children->list    = children_list;
-               children->nb      = g_slist_length(children_list);
+               children->list        = children_list;
+               children->nb          = g_slist_length(children_list);
+               children->initial_nb  = children->nb;
 
                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,
@@ -712,20 +793,21 @@ static gboolean execute_actions(gchar *action, GSList *msg_list,
 
                create_io_dialog(children);
        }
-
        return is_ok;
 }
 
-static ChildInfo *fork_child(gchar *cmd, gint action_type,
-                            const gchar *msg_str, Children *children)
+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 = !(action_type & ACTION_ASYNC);
+       sync = !(children->action_type & ACTION_ASYNC);
 
        chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
                = chld_err[1] = chld_status[0] = chld_status[1] = -1;
@@ -750,13 +832,16 @@ static ChildInfo *fork_child(gchar *cmd, gint action_type,
        }
 
        debug_print("Forking child and grandchild.\n");
+       debug_print("Executing: /bin/sh -c %s\n", cmd);
 
        pid = fork();
        if (pid == 0) { /* Child */
                if (setpgid(0, 0))
                        perror("setpgid");
 
+#ifdef GDK_WINDOWING_X11
                close(ConnectionNumber(gdk_display));
+#endif /* GDK_WINDOWING_X11 */
 
                gch_pid = fork();
 
@@ -765,7 +850,7 @@ static ChildInfo *fork_child(gchar *cmd, gint action_type,
                                perror("setpgid");
 
                        if (sync) {
-                               if (action_type &
+                               if (children->action_type &
                                    (ACTION_PIPE_IN |
                                     ACTION_USER_IN |
                                     ACTION_USER_HIDDEN_IN)) {
@@ -788,11 +873,18 @@ static ChildInfo *fork_child(gchar *cmd, gint action_type,
 
                        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)
@@ -832,7 +924,7 @@ static ChildInfo *fork_child(gchar *cmd, gint action_type,
        }
 
        close(chld_in[0]);
-       if (!(action_type &
+       if (!(children->action_type &
              (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
                close(chld_in[1]);
        close(chld_out[1]);
@@ -845,11 +937,10 @@ static ChildInfo *fork_child(gchar *cmd, gint action_type,
 
        child_info->pid         = pid;
        child_info->cmd         = g_strdup(cmd);
-       child_info->type        = action_type;
        child_info->new_out     = FALSE;
        child_info->output      = g_string_new(NULL);
        child_info->chld_in     =
-               (action_type &
+               (children->action_type &
                 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
                        ? chld_in [1] : -1;
        child_info->chld_out    = chld_out[0];
@@ -861,21 +952,33 @@ static ChildInfo *fork_child(gchar *cmd, gint action_type,
        child_info->tag_err     = gdk_input_add(chld_err[0], GDK_INPUT_READ,
                                                catch_output, child_info);
 
-       if (!(action_type & (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
+       if (!(children->action_type &
+             (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
                return child_info;
 
-       if ((action_type & ACTION_PIPE_IN) && msg_str) {
-               write(chld_in[1], msg_str, strlen(msg_str));
-               if (!(action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
+       if ((children->action_type & ACTION_PIPE_IN) && 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]);
                child_info->chld_in = -1; /* No more input */
        }
 
        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;
@@ -886,6 +989,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)
@@ -947,8 +1051,9 @@ static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
        Children *children = (Children *)data;
 
        if (!children->nb) {
-               gtk_signal_disconnect_by_data(GTK_OBJECT(children->dialog),
-                                             children);
+               g_signal_handlers_disconnect_matched
+                       (G_OBJECT(children->dialog), G_SIGNAL_MATCH_DATA,
+                        0, 0, NULL, NULL, children);
                gtk_widget_destroy(children->dialog);
                free_children(children);
        }
@@ -964,34 +1069,50 @@ static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
 
 static void childinfo_close_pipes(ChildInfo *child_info)
 {
+       /* stdout and stderr pipes are guaranteed to be removed by
+        * their handler, but in case where we receive child exit notification
+        * before grand-child's pipes closing signals, we check them and close
+        * them if necessary
+        */
        if (child_info->tag_in > 0)
                gdk_input_remove(child_info->tag_in);
-       gdk_input_remove(child_info->tag_out);
-       gdk_input_remove(child_info->tag_err);
+       if (child_info->tag_out > 0)
+               gdk_input_remove(child_info->tag_out);
+       if (child_info->tag_err > 0)
+               gdk_input_remove(child_info->tag_err);
 
        if (child_info->chld_in >= 0)
                close(child_info->chld_in);
-       close(child_info->chld_out);
-       close(child_info->chld_err);
+       if (child_info->chld_out >= 0)
+               close(child_info->chld_out);
+       if (child_info->chld_err >= 0)
+               close(child_info->chld_err);
+
        close(child_info->chld_status);
 }
 
 static void free_children(Children *children)
 {
-       GSList *cur;
        ChildInfo *child_info;
+       void (*callback)(void *data) = NULL;
+       void *data = NULL;
 
        debug_print("Freeing children data %p\n", children);
 
        g_free(children->action);
-       for (cur = children->list; cur;) {
-               child_info = (ChildInfo *)cur->data;
+       while (children->list != NULL) {
+               child_info = (ChildInfo *)children->list->data;
                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);
-               cur = children->list;
        }
+
+       if (callback)
+               callback(data);
+       
        g_free(children);
 }
 
@@ -1001,28 +1122,44 @@ static void update_io_dialog(Children *children)
 
        debug_print("Updating actions input/output dialog.\n");
 
+       if (children->progress_bar) {
+               gchar *text;
+               
+               gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar),
+                                             (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);
                gtk_widget_set_sensitive(children->close_btn, TRUE);
                if (children->input_hbox)
                        gtk_widget_set_sensitive(children->input_hbox, FALSE);
                gtk_widget_grab_focus(children->close_btn);
-               gtk_signal_connect(GTK_OBJECT(children->dialog),
-                                  "key_press_event",
-                                  GTK_SIGNAL_FUNC(io_dialog_key_pressed_cb),
-                                  children);
+               g_signal_connect(G_OBJECT(children->dialog),
+                                "key_press_event",
+                                G_CALLBACK(io_dialog_key_pressed_cb),
+                                children);
        }
 
        if (children->output) {
                GtkWidget *text = children->text;
+               GtkTextBuffer *textbuf;
+               GtkTextIter iter, start_iter, end_iter;
                gchar *caption;
                ChildInfo *child_info;
 
                gtk_widget_show(children->scrolledwin);
-               gtk_text_freeze(GTK_TEXT(text));
-               gtk_text_set_point(GTK_TEXT(text), 0);
-               gtk_text_forward_delete(GTK_TEXT(text), 
-                                       gtk_text_get_length(GTK_TEXT(text)));
+               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)
@@ -1034,14 +1171,12 @@ static void update_io_dialog(Children *children)
                                        (_("--- Ended: %s\n"),
                                         child_info->cmd);
 
-                       gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
-                                       caption, -1);
-                       gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
-                                       child_info->output->str, -1);
+                       gtk_text_buffer_insert(textbuf, &iter, caption, -1);
+                       gtk_text_buffer_insert(textbuf, &iter,
+                                              child_info->output->str, -1);
                        g_free(caption);
                        child_info->new_out = FALSE;
                }
-               gtk_text_thaw(GTK_TEXT(text));
        }
 }
 
@@ -1056,6 +1191,7 @@ static void create_io_dialog(Children *children)
        GtkWidget *text;
        GtkWidget *scrolledwin;
        GtkWidget *hbox;
+       GtkWidget *progress_bar = NULL;
        GtkWidget *abort_button;
        GtkWidget *close_button;
 
@@ -1068,11 +1204,11 @@ static void create_io_dialog(Children *children)
        gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
        gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
        manage_window_set_transient(GTK_WINDOW(dialog));
-       gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
-                       GTK_SIGNAL_FUNC(delete_io_dialog_cb), children);
-       gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
-                       GTK_SIGNAL_FUNC(hide_io_dialog_cb),
-                       children);
+       g_signal_connect(G_OBJECT(dialog), "delete_event",
+                        G_CALLBACK(delete_io_dialog_cb), children);
+       g_signal_connect(G_OBJECT(dialog), "destroy",
+                        G_CALLBACK(hide_io_dialog_cb),
+                        children);
 
        vbox = gtk_vbox_new(FALSE, 8);
        gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
@@ -1085,16 +1221,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_usize(scrolledwin, 480, 200);
+       gtk_widget_set_size_request(scrolledwin, 560, 200);
        gtk_widget_hide(scrolledwin);
 
-       text = gtk_text_new(gtk_scrolled_window_get_hadjustment
-                           (GTK_SCROLLED_WINDOW(scrolledwin)),
-                           gtk_scrolled_window_get_vadjustment
-                           (GTK_SCROLLED_WINDOW(scrolledwin)));
-       gtk_text_set_editable(GTK_TEXT(text), FALSE);
+       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);
 
@@ -1103,17 +1253,17 @@ static void create_io_dialog(Children *children)
                gtk_widget_show(input_hbox);
 
                entry = gtk_entry_new();
-               gtk_widget_set_usize(entry, 320, -1);
-               gtk_signal_connect(GTK_OBJECT(entry), "activate",
-                                  GTK_SIGNAL_FUNC(send_input), children);
+               gtk_widget_set_size_request(entry, 320, -1);
+               g_signal_connect(G_OBJECT(entry), "activate",
+                                G_CALLBACK(send_input), children);
                gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
-               if (children->open_in & ACTION_USER_HIDDEN_IN)
+               if (children->action_type & ACTION_USER_HIDDEN_IN)
                        gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
                gtk_widget_show(entry);
 
-               send_button = gtk_button_new_with_label(_(" Send "));
-               gtk_signal_connect(GTK_OBJECT(send_button), "clicked",
-                                  GTK_SIGNAL_FUNC(send_input), children);
+               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,
                                   FALSE, 0);
                gtk_widget_show(send_button);
@@ -1122,12 +1272,27 @@ static void create_io_dialog(Children *children)
                gtk_widget_grab_focus(entry);
        }
 
-       gtkut_button_set_create(&hbox, &abort_button, _("Abort"),
-                               &close_button, _("Close"), NULL, NULL);
-       gtk_signal_connect(GTK_OBJECT(abort_button), "clicked",
-                       GTK_SIGNAL_FUNC(kill_children_cb), children);
-       gtk_signal_connect(GTK_OBJECT(close_button), "clicked",
-                       GTK_SIGNAL_FUNC(hide_io_dialog_cb), children);
+       if (children->initial_nb > 1) {
+               gchar * text;
+               
+               progress_bar = gtk_progress_bar_new();
+               gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
+                               GTK_PROGRESS_LEFT_TO_RIGHT);
+               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_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",
+                        G_CALLBACK(hide_io_dialog_cb), children);
        gtk_widget_show(hbox);
 
        if (children->nb)
@@ -1135,13 +1300,14 @@ static void create_io_dialog(Children *children)
 
        gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
 
-       children->dialog      = dialog;
-       children->scrolledwin = scrolledwin;
-       children->text        = text;
-       children->input_hbox  = children->open_in ? input_hbox : NULL;
-       children->input_entry = children->open_in ? entry : NULL;
-       children->abort_btn   = abort_button;
-       children->close_btn   = close_button;
+       children->dialog       = dialog;
+       children->scrolledwin  = scrolledwin;
+       children->text         = text;
+       children->input_hbox   = children->open_in ? input_hbox : NULL;
+       children->input_entry  = children->open_in ? entry : NULL;
+       children->progress_bar = progress_bar;
+       children->abort_btn    = abort_button;
+       children->close_btn    = close_button;
 
        gtk_widget_show(dialog);
 }
@@ -1157,7 +1323,9 @@ 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;
 
@@ -1168,8 +1336,9 @@ 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))
@@ -1180,6 +1349,13 @@ static void catch_input(gpointer data, gint source, GdkInputCondition cond)
 
        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;
 
@@ -1204,42 +1380,83 @@ 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->type & (ACTION_PIPE_OUT | ACTION_INSERT)
+       if (child_info->children->action_type &
+           (ACTION_PIPE_OUT | ACTION_INSERT)
            && source == child_info->chld_out) {
-               gboolean is_selection = FALSE;
-               GtkWidget *text = child_info->children->msg_text;
+               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);
 
-               if (GTK_EDITABLE(text)->has_selection)
-                       is_selection = TRUE;
-               gtk_stext_freeze(GTK_STEXT(text));
                while (TRUE) {
+                       gsize bytes_read = 0, bytes_written = 0;
+                       gchar *ret_str;
+
                        c = read(source, buf, sizeof(buf) - 1);
                        if (c == 0)
                                break;
-                       gtk_stext_insert(GTK_STEXT(text),
-                                        child_info->children->msgfont,
-                                        NULL, NULL, 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,
+                                        bytes_written);
+                               g_free(ret_str);
+                       } else
+                               gtk_text_buffer_insert(textbuf, &iter, buf, c);
                }
-               if (is_selection) {
-                       /* Using the select_region draws things. Should not.
-                        * so we just change selection position and 
-                        * defere drawing when thawing. Hack?
-                        */
-                       GTK_EDITABLE(text)->selection_end_pos =
-                                       gtk_stext_get_point(GTK_STEXT(text));
+
+               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);
                }
-               gtk_stext_thaw(GTK_STEXT(text));
        } 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) {
+                       gdk_input_remove(child_info->tag_out);
+                       child_info->tag_out = -1;
+                       close(child_info->chld_out);
+                       child_info->chld_out = -1;
+               } else {
+                       gdk_input_remove(child_info->tag_err);
+                       child_info->tag_err = -1;
+                       close(child_info->chld_err);
+                       child_info->chld_err = -1;
+               }
+       }
+       
        wait_for_children(child_info->children);
 }
 
@@ -1252,7 +1469,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
@@ -1261,7 +1478,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