2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto & The Sylpheed Claws Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <glib/gi18n.h>
29 #include <gdk/gdkkeysyms.h>
30 #ifdef GDK_WINDOWING_X11
31 # include <gdk/gdkx.h>
32 #endif /* GDK_WINDOWING_X11 */
37 #include <sys/types.h>
39 # include <sys/wait.h>
46 #include "manage_window.h"
47 #include "mainwindow.h"
48 #include "prefs_common.h"
49 #include "alertpanel.h"
50 #include "inputdialog.h"
55 #include "matcher_parser.h" /* CLAWS */
56 #include "filtering.h"
57 #include "procheader.h"
59 typedef struct _Children Children;
60 typedef struct _ChildInfo ChildInfo;
61 typedef struct _UserStringDialog UserStringDialog;
67 GtkWidget *input_entry;
68 GtkWidget *input_hbox;
69 GtkWidget *progress_bar;
72 GtkWidget *scrolledwin;
75 ActionType action_type;
84 gboolean is_selection;
103 void (*callback)(void *data);
106 GSList *msginfo_list;
109 static void action_update_menu (GtkItemFactory *ifactory,
113 static void compose_actions_execute_cb (Compose *compose,
116 static void mainwin_actions_execute_cb (MainWindow *mainwin,
119 static void msgview_actions_execute_cb (MessageView *msgview,
122 static void message_actions_execute (MessageView *msgview,
126 static gboolean execute_filtering_actions(gchar *action,
129 static gboolean execute_actions (gchar *action,
134 void (*callback)(void *data),
137 static gchar *parse_action_cmd (gchar *action,
141 const gchar *user_str,
142 const gchar *user_hidden_str,
143 const gchar *sel_str);
144 static gboolean parse_append_filename (GString *cmd,
147 static gboolean parse_append_msgpart (GString *cmd,
151 static ChildInfo *fork_child (gchar *cmd,
152 const gchar *msg_str,
155 static gint wait_for_children (Children *children);
157 static void free_children (Children *children);
159 static void childinfo_close_pipes (ChildInfo *child_info);
161 static void create_io_dialog (Children *children);
162 static void update_io_dialog (Children *children);
164 static void hide_io_dialog_cb (GtkWidget *widget,
166 static gint io_dialog_key_pressed_cb (GtkWidget *widget,
170 static void catch_output (gpointer data,
172 GdkInputCondition cond);
173 static void catch_input (gpointer data,
175 GdkInputCondition cond);
176 static void catch_status (gpointer data,
178 GdkInputCondition cond);
180 static gchar *get_user_string (const gchar *action,
184 ActionType action_get_type(const gchar *action_str)
187 ActionType action_type = ACTION_NONE;
189 g_return_val_if_fail(action_str, ACTION_ERROR);
190 g_return_val_if_fail(*action_str, ACTION_ERROR);
195 action_type |= ACTION_PIPE_IN;
197 } else if (p[0] == '>') {
198 action_type |= ACTION_USER_IN;
200 } else if (p[0] == '*') {
201 action_type |= ACTION_USER_HIDDEN_IN;
208 while (*p && action_type != ACTION_ERROR) {
209 if (p[0] == '%' && p[1]) {
212 /* CLAWS: filtering action is a mutually exclusive
213 * action. we can enable others if needed later. we
214 * add ACTION_SINGLE | ACTION_MULTIPLE so it will
215 * only be executed from the main window toolbar */
216 if (p[2] == 's') /* source messages */
217 action_type = ACTION_FILTERING_ACTION
222 action_type |= ACTION_SINGLE;
225 action_type |= ACTION_MULTIPLE;
228 action_type |= ACTION_SINGLE;
231 action_type |= ACTION_SELECTION_STR;
234 action_type |= ACTION_USER_STR;
237 action_type |= ACTION_USER_HIDDEN_STR;
240 action_type = ACTION_ERROR;
243 } else if (p[0] == '|') {
245 action_type |= ACTION_PIPE_OUT;
246 } else if (p[0] == '>') {
248 action_type |= ACTION_INSERT;
249 } else if (p[0] == '&') {
251 action_type |= ACTION_ASYNC;
259 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
260 GSList *msg_list, MimeInfo *partinfo,
261 const gchar *user_str,
262 const gchar *user_hidden_str,
263 const gchar *sel_str)
271 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
274 cmd = g_string_sized_new(strlen(action));
277 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
278 if (p[0] == '%' && p[1]) {
281 if (!parse_append_filename(cmd, msginfo)) {
282 g_string_free(cmd, TRUE);
288 for (cur = msg_list; cur != NULL;
290 MsgInfo *msg = (MsgInfo *)cur->data;
292 if (!parse_append_filename(cmd, msg)) {
293 g_string_free(cmd, TRUE);
297 g_string_append_c(cmd, ' ');
302 if (!parse_append_msgpart(cmd, msginfo,
304 g_string_free(cmd, TRUE);
311 g_string_append(cmd, sel_str);
316 g_string_append(cmd, user_str);
321 g_string_append(cmd, user_hidden_str);
325 g_string_append_c(cmd, p[0]);
326 g_string_append_c(cmd, p[1]);
330 g_string_append_c(cmd, p[0]);
335 g_string_free(cmd, TRUE);
340 g_string_free(cmd, FALSE);
344 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
348 gchar escape_ch[] = "\\ ";
350 g_return_val_if_fail(msginfo, FALSE);
352 filename = procmsg_get_message_file(msginfo);
355 alertpanel_error(_("Could not get message file %d"),
361 while ((q = strpbrk(p, "$\"`'\\ \t*?[]&|;<>()!#~")) != NULL) {
364 g_string_append(cmd, p);
365 g_string_append(cmd, escape_ch);
368 g_string_append(cmd, p);
375 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
378 gboolean single_part = FALSE;
380 gchar *part_filename;
384 partinfo = procmime_scan_message(msginfo);
386 alertpanel_error(_("Could not get message part."));
393 filename = procmsg_get_message_file_path(msginfo);
394 part_filename = procmime_get_tmp_file_name(partinfo);
396 ret = procmime_get_part(part_filename, partinfo);
399 procmime_mimeinfo_free_all(partinfo);
403 alertpanel_error(_("Can't get part of multipart message"));
404 g_free(part_filename);
408 g_string_append(cmd, part_filename);
410 g_free(part_filename);
415 void actions_execute(gpointer data,
420 if (source == TOOLBAR_MAIN)
421 mainwin_actions_execute_cb((MainWindow*)data, action_nb, widget);
422 else if (source == TOOLBAR_COMPOSE)
423 compose_actions_execute_cb((Compose*)data, action_nb, widget);
424 else if (source == TOOLBAR_MSGVIEW)
425 msgview_actions_execute_cb((MessageView*)data, action_nb, widget);
428 void action_update_mainwin_menu(GtkItemFactory *ifactory,
432 action_update_menu(ifactory, branch_path,
433 mainwin_actions_execute_cb, mainwin);
436 void action_update_msgview_menu(GtkItemFactory *ifactory,
438 MessageView *msgview)
440 action_update_menu(ifactory, branch_path,
441 msgview_actions_execute_cb, msgview);
444 void action_update_compose_menu(GtkItemFactory *ifactory,
448 action_update_menu(ifactory, branch_path,
449 compose_actions_execute_cb, compose);
452 static void action_update_menu(GtkItemFactory *ifactory,
454 gpointer callback, gpointer data)
459 gchar *action, *action_p;
461 GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
463 ifentry.path = branch_path;
464 menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
465 g_return_if_fail(menuitem != NULL);
467 amenu = GTK_MENU_SHELL(menuitem)->children;
468 while (amenu != NULL) {
469 GList *alist = amenu->next;
470 gtk_widget_destroy(GTK_WIDGET(amenu->data));
474 ifentry.accelerator = NULL;
475 ifentry.callback_action = 0;
476 ifentry.callback = callback;
477 ifentry.item_type = NULL;
479 for (cur = prefs_common.actions_list; cur; cur = cur->next) {
480 action = g_strdup((gchar *)cur->data);
481 action_p = strstr(action, ": ");
482 if (action_p && action_p[2] &&
483 (action_get_type(&action_p[2]) != ACTION_ERROR) &&
484 (action[0] != '/')) {
486 menu_path = g_strdup_printf("%s/%s", branch_path,
488 ifentry.path = menu_path;
489 gtk_item_factory_create_item(ifactory, &ifentry, data,
494 ifentry.callback_action++;
498 static void compose_actions_execute_cb(Compose *compose, guint action_nb,
502 ActionType action_type;
504 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
506 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
507 g_return_if_fail(buf != NULL);
508 action = strstr(buf, ": ");
509 g_return_if_fail(action != NULL);
511 /* Point to the beginning of the command-line */
514 action_type = action_get_type(action);
515 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
517 (_("The selected action cannot be used in the compose window\n"
518 "because it contains %%f, %%F, %%as or %%p."));
522 execute_actions(action, NULL, compose->text, 0, NULL,
523 compose_action_cb, compose);
526 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
531 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
532 message_actions_execute(mainwin->messageview, action_nb, msg_list);
533 g_slist_free(msg_list);
536 static void msgview_actions_execute_cb(MessageView *msgview, guint action_nb,
539 GSList *msg_list = NULL;
541 if (msgview->msginfo)
542 msg_list = g_slist_append(msg_list, msgview->msginfo);
543 message_actions_execute(msgview, action_nb, msg_list);
544 g_slist_free(msg_list);
547 static void message_actions_execute(MessageView *msgview, guint action_nb,
554 GtkWidget *text = NULL;
556 ActionType action_type;
558 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
560 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
562 g_return_if_fail(buf);
563 g_return_if_fail((action = strstr(buf, ": ")));
565 /* Point to the beginning of the command-line */
568 textview = messageview_get_current_textview(msgview);
570 text = textview->text;
571 body_pos = textview->body_pos;
573 partinfo = messageview_get_selected_mime_part(msgview);
575 /* this command will alter the message text */
576 action_type = action_get_type(action);
577 if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
578 msgview->filtered = TRUE;
580 if (action_type & ACTION_FILTERING_ACTION)
581 /* CLAWS: most of the above code is not necessary for applying
583 execute_filtering_actions(action, msg_list);
585 execute_actions(action, msg_list, text, body_pos, partinfo,
589 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
591 GSList *action_list, *p;
592 const gchar *sbegin, *send;
593 gchar *action_string;
594 SummaryView *summaryview = NULL;
595 MainWindow *mainwin = NULL;
597 if (mainwindow_get_mainwindow()) {
598 summaryview = mainwindow_get_mainwindow()->summaryview;
599 mainwin = mainwindow_get_mainwindow();
602 if (NULL == (sbegin = strstr2(action, "%as{")))
604 sbegin += sizeof "%as{" - 1;
605 if (NULL == (send = strrchr(sbegin, '}')))
607 action_string = g_strndup(sbegin, send - sbegin);
609 action_list = matcher_parser_get_action_list(action_string);
610 g_free(action_string);
611 if (action_list == NULL) return FALSE;
613 /* apply actions on each message info */
614 for (p = msglist; p && p->data; p = g_slist_next(p)) {
615 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
620 summary_lock(summaryview);
621 main_window_cursor_wait(mainwin);
622 gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
623 folder_item_update_freeze();
626 filtering_move_and_copy_msgs(msglist);
629 folder_item_update_thaw();
630 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
631 main_window_cursor_normal(mainwin);
632 summary_unlock(summaryview);
633 summary_show(summaryview, summaryview->folder_item);
635 for (p = action_list; p; p = g_slist_next(p))
636 if (p->data) filteringaction_free(p->data);
637 g_slist_free(action_list);
641 static gboolean execute_actions(gchar *action, GSList *msg_list,
643 gint body_pos, MimeInfo *partinfo,
644 void (*callback)(void *data), void *data)
646 GSList *children_list = NULL;
650 ChildInfo *child_info;
651 ActionType action_type;
654 gchar *sel_str = NULL;
655 gchar *msg_str = NULL;
656 gchar *user_str = NULL;
657 gchar *user_hidden_str = NULL;
658 GtkTextIter start_iter, end_iter;
659 gboolean is_selection = FALSE;
661 g_return_val_if_fail(action && *action, FALSE);
663 action_type = action_get_type(action);
665 if (action_type == ACTION_ERROR)
666 return FALSE; /* ERR: syntax error */
668 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
669 return FALSE; /* ERR: file command without selection */
671 msg_list_len = g_slist_length(msg_list);
673 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
674 if (msg_list_len > 1)
675 return FALSE; /* ERR: pipe + multiple selection */
677 return FALSE; /* ERR: pipe and no displayed text */
680 if (action_type & ACTION_SELECTION_STR) {
682 return FALSE; /* ERR: selection string but no text */
686 GtkTextBuffer *textbuf;
688 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
689 is_selection = gtk_text_buffer_get_selection_bounds
690 (textbuf, &start_iter, &end_iter);
692 gtk_text_buffer_get_iter_at_offset
693 (textbuf, &start_iter, body_pos);
694 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
696 msg_str = gtk_text_buffer_get_text
697 (textbuf, &start_iter, &end_iter, FALSE);
699 sel_str = g_strdup(msg_str);
702 if (action_type & ACTION_USER_STR) {
703 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
710 if (action_type & ACTION_USER_HIDDEN_STR) {
711 if (!(user_hidden_str =
712 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
720 if (text && (action_type & ACTION_PIPE_OUT)) {
721 GtkTextBuffer *textbuf;
722 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
723 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
726 children = g_new0(Children, 1);
728 children->action = g_strdup(action);
729 children->action_type = action_type;
730 children->msg_text = text;
731 children->is_selection = is_selection;
733 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
734 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
735 children->open_in = 1;
737 if (action_type & ACTION_SINGLE) {
740 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
741 msginfo = (MsgInfo *)cur->data;
743 is_ok = FALSE; /* ERR: msginfo missing */
746 cmd = parse_action_cmd(action, msginfo, msg_list,
748 user_hidden_str, sel_str);
750 debug_print("Action command error\n");
751 is_ok = FALSE; /* ERR: incorrect command */
754 if ((child_info = fork_child(cmd, msg_str, children))) {
755 /* Pass msginfo to catch_status () */
756 child_info->msginfo_list = g_slist_append (NULL, msginfo);
757 children_list = g_slist_append(children_list,
763 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
764 user_str, user_hidden_str, sel_str);
766 if ((child_info = fork_child(cmd, msg_str, children))) {
767 child_info->msginfo_list = g_slist_copy (msg_list);
768 children_list = g_slist_append(children_list,
773 is_ok = FALSE; /* ERR: incorrect command */
779 g_free(user_hidden_str);
781 if (!children_list) {
782 /* If not waiting for children, return */
783 free_children(children);
787 children->list = children_list;
788 children->nb = g_slist_length(children_list);
789 children->initial_nb = children->nb;
791 for (cur = children_list; cur; cur = cur->next) {
792 child_info = (ChildInfo *) cur->data;
793 child_info->callback = callback;
794 child_info->data = data;
795 child_info->tag_status =
796 gdk_input_add(child_info->chld_status,
798 catch_status, child_info);
801 create_io_dialog(children);
806 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
810 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
811 gchar *cmdline[4], *ret_str;
813 ChildInfo *child_info;
815 gssize by_written = 0, by_read = 0;
817 sync = !(children->action_type & ACTION_ASYNC);
819 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
820 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
823 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
825 alertpanel_error(_("Command could not be started. "
826 "Pipe creation failed.\n%s"),
828 /* Closing fd = -1 fails silently */
835 close(chld_status[0]);
836 close(chld_status[1]);
837 return NULL; /* Pipe error */
841 debug_print("Forking child and grandchild.\n");
842 debug_print("Executing: /bin/sh -c %s\n", cmd);
845 if (pid == 0) { /* Child */
849 #ifdef GDK_WINDOWING_X11
850 close(ConnectionNumber(gdk_display));
851 #endif /* GDK_WINDOWING_X11 */
856 if (setpgid(0, getppid()))
860 if (children->action_type &
863 ACTION_USER_HIDDEN_IN)) {
864 close(fileno(stdin));
870 close(fileno(stdout));
875 close(fileno(stderr));
883 ret_str = g_locale_from_utf8(cmd, strlen(cmd),
884 &by_read, &by_written,
886 if (ret_str && by_written)
887 cmdline[2] = ret_str;
891 execvp("/bin/sh", cmdline);
896 } else if (gch_pid < (pid_t) 0) { /* Fork error */
898 write(chld_status[1], "1\n", 2);
909 close(chld_status[0]);
911 debug_print("Child: Waiting for grandchild\n");
912 waitpid(gch_pid, NULL, 0);
913 debug_print("Child: grandchild ended\n");
914 write(chld_status[1], "0\n", 2);
915 close(chld_status[1]);
919 } else if (pid < 0) { /* Fork error */
920 alertpanel_error(_("Could not fork to execute the following "
922 cmd, g_strerror(errno));
929 waitpid(pid, NULL, 0);
934 if (!(children->action_type &
935 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
939 close(chld_status[1]);
941 child_info = g_new0(ChildInfo, 1);
943 child_info->children = children;
945 child_info->pid = pid;
946 child_info->cmd = g_strdup(cmd);
947 child_info->new_out = FALSE;
948 child_info->output = g_string_new(NULL);
949 child_info->chld_in =
950 (children->action_type &
951 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
953 child_info->chld_out = chld_out[0];
954 child_info->chld_err = chld_err[0];
955 child_info->chld_status = chld_status[0];
956 child_info->tag_in = -1;
957 child_info->tag_out = gdk_input_add(chld_out[0], GDK_INPUT_READ,
958 catch_output, child_info);
959 child_info->tag_err = gdk_input_add(chld_err[0], GDK_INPUT_READ,
960 catch_output, child_info);
962 if (!(children->action_type &
963 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
966 if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
967 ret_str = g_locale_from_utf8(msg_str, strlen(msg_str),
968 &by_read, &by_written, NULL);
969 if (ret_str && by_written) {
970 write(chld_in[1], ret_str, strlen(ret_str));
973 write(chld_in[1], msg_str, strlen(msg_str));
974 if (!(children->action_type &
975 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
977 child_info->chld_in = -1; /* No more input */
983 #endif /* G_OS_UNIX */
986 static void kill_children_cb(GtkWidget *widget, gpointer data)
990 Children *children = (Children *) data;
991 ChildInfo *child_info;
993 for (cur = children->list; cur; cur = cur->next) {
994 child_info = (ChildInfo *)(cur->data);
995 debug_print("Killing child group id %d\n", child_info->pid);
996 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
999 #endif /* G_OS_UNIX */
1002 static gint wait_for_children(Children *children)
1004 gboolean new_output;
1005 ChildInfo *child_info;
1007 gint nb = children->nb;
1011 cur = children->list;
1014 child_info = (ChildInfo *)cur->data;
1015 if (child_info->pid)
1017 new_output |= child_info->new_out;
1021 children->output |= new_output;
1023 if (new_output || (children->dialog && (nb != children->nb)))
1024 update_io_dialog(children);
1029 if (!children->dialog) {
1030 free_children(children);
1031 } else if (!children->output) {
1032 gtk_widget_destroy(children->dialog);
1038 static void send_input(GtkWidget *w, gpointer data)
1040 Children *children = (Children *) data;
1041 ChildInfo *child_info = (ChildInfo *) children->list->data;
1043 child_info->tag_in = gdk_input_add(child_info->chld_in,
1045 catch_input, children);
1046 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1049 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1051 hide_io_dialog_cb(w, data);
1055 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1058 Children *children = (Children *)data;
1060 if (!children->nb) {
1061 g_signal_handlers_disconnect_matched
1062 (G_OBJECT(children->dialog), G_SIGNAL_MATCH_DATA,
1063 0, 0, NULL, NULL, children);
1064 gtk_widget_destroy(children->dialog);
1065 free_children(children);
1069 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1072 if (event && event->keyval == GDK_Escape)
1073 hide_io_dialog_cb(widget, data);
1077 static void childinfo_close_pipes(ChildInfo *child_info)
1079 /* stdout and stderr pipes are guaranteed to be removed by
1080 * their handler, but in case where we receive child exit notification
1081 * before grand-child's pipes closing signals, we check them and close
1084 if (child_info->tag_in > 0)
1085 gdk_input_remove(child_info->tag_in);
1086 if (child_info->tag_out > 0)
1087 gdk_input_remove(child_info->tag_out);
1088 if (child_info->tag_err > 0)
1089 gdk_input_remove(child_info->tag_err);
1091 if (child_info->chld_in >= 0)
1092 close(child_info->chld_in);
1093 if (child_info->chld_out >= 0)
1094 close(child_info->chld_out);
1095 if (child_info->chld_err >= 0)
1096 close(child_info->chld_err);
1098 close(child_info->chld_status);
1101 static void free_children(Children *children)
1103 ChildInfo *child_info;
1104 void (*callback)(void *data) = NULL;
1107 debug_print("Freeing children data %p\n", children);
1109 g_free(children->action);
1110 while (children->list != NULL) {
1111 child_info = (ChildInfo *)children->list->data;
1112 g_free(child_info->cmd);
1113 g_string_free(child_info->output, TRUE);
1114 children->list = g_slist_remove(children->list, child_info);
1115 callback = child_info->callback;
1116 data = child_info->data;
1126 static void update_io_dialog(Children *children)
1130 debug_print("Updating actions input/output dialog.\n");
1132 if (children->progress_bar) {
1135 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar),
1136 (gdouble) (children->initial_nb - children->nb) /
1137 (gdouble) children->initial_nb);
1138 text = g_strdup_printf("%s %d/%d", _("Completed"),
1139 children->initial_nb - children->nb,
1140 children->initial_nb);
1141 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(children->progress_bar), text);
1145 if (!children->nb) {
1146 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1147 gtk_widget_set_sensitive(children->close_btn, TRUE);
1148 if (children->input_hbox)
1149 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1150 gtk_widget_grab_focus(children->close_btn);
1151 g_signal_connect(G_OBJECT(children->dialog),
1153 G_CALLBACK(io_dialog_key_pressed_cb),
1157 if (children->output) {
1158 GtkWidget *text = children->text;
1159 GtkTextBuffer *textbuf;
1160 GtkTextIter iter, start_iter, end_iter;
1162 ChildInfo *child_info;
1164 gtk_widget_show(children->scrolledwin);
1165 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
1166 gtk_text_buffer_get_bounds(textbuf, &start_iter, &end_iter);
1167 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
1168 gtk_text_buffer_get_start_iter(textbuf, &iter);
1170 for (cur = children->list; cur; cur = cur->next) {
1171 child_info = (ChildInfo *)cur->data;
1172 if (child_info->pid)
1173 caption = g_strdup_printf
1174 (_("--- Running: %s\n"),
1177 caption = g_strdup_printf
1178 (_("--- Ended: %s\n"),
1181 gtk_text_buffer_insert(textbuf, &iter, caption, -1);
1182 gtk_text_buffer_insert(textbuf, &iter,
1183 child_info->output->str, -1);
1185 child_info->new_out = FALSE;
1190 static void create_io_dialog(Children *children)
1194 GtkWidget *entry = NULL;
1195 GtkWidget *input_hbox = NULL;
1196 GtkWidget *send_button;
1199 GtkWidget *scrolledwin;
1201 GtkWidget *progress_bar = NULL;
1202 GtkWidget *abort_button;
1203 GtkWidget *close_button;
1205 debug_print("Creating action IO dialog\n");
1207 dialog = gtk_dialog_new();
1208 gtk_container_set_border_width
1209 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1210 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1211 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1212 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1213 manage_window_set_transient(GTK_WINDOW(dialog));
1214 g_signal_connect(G_OBJECT(dialog), "delete_event",
1215 G_CALLBACK(delete_io_dialog_cb), children);
1216 g_signal_connect(G_OBJECT(dialog), "destroy",
1217 G_CALLBACK(hide_io_dialog_cb),
1220 vbox = gtk_vbox_new(FALSE, 8);
1221 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1222 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1223 gtk_widget_show(vbox);
1225 label = gtk_label_new(children->action);
1226 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1227 gtk_widget_show(label);
1229 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1230 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1231 GTK_POLICY_AUTOMATIC,
1232 GTK_POLICY_AUTOMATIC);
1233 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
1235 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1236 gtk_widget_set_size_request(scrolledwin, 560, 200);
1237 gtk_widget_hide(scrolledwin);
1239 text = gtk_text_view_new();
1241 if (prefs_common.textfont) {
1242 PangoFontDescription *font_desc;
1243 font_desc = pango_font_description_from_string
1244 (prefs_common.textfont);
1246 gtk_widget_modify_font(text, font_desc);
1247 pango_font_description_free(font_desc);
1251 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
1252 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
1253 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
1254 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
1255 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1256 gtk_widget_show(text);
1258 if (children->open_in) {
1259 input_hbox = gtk_hbox_new(FALSE, 8);
1260 gtk_widget_show(input_hbox);
1262 entry = gtk_entry_new();
1263 gtk_widget_set_size_request(entry, 320, -1);
1264 g_signal_connect(G_OBJECT(entry), "activate",
1265 G_CALLBACK(send_input), children);
1266 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1267 if (children->action_type & ACTION_USER_HIDDEN_IN)
1268 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1269 gtk_widget_show(entry);
1271 send_button = gtk_button_new_from_stock(GTK_STOCK_EXECUTE);
1272 g_signal_connect(G_OBJECT(send_button), "clicked",
1273 G_CALLBACK(send_input), children);
1274 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1276 gtk_widget_show(send_button);
1278 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1279 gtk_widget_grab_focus(entry);
1282 if (children->initial_nb > 1) {
1285 progress_bar = gtk_progress_bar_new();
1286 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1287 GTK_PROGRESS_LEFT_TO_RIGHT);
1288 text = g_strdup_printf("%s 0/%d\n", _("Completed"),
1289 children->initial_nb);
1290 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar),
1293 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1294 gtk_widget_show(progress_bar);
1297 gtkut_stock_button_set_create(&hbox, &abort_button, GTK_STOCK_STOP,
1298 &close_button, GTK_STOCK_CLOSE, NULL, NULL);
1299 g_signal_connect(G_OBJECT(abort_button), "clicked",
1300 G_CALLBACK(kill_children_cb), children);
1301 g_signal_connect(G_OBJECT(close_button), "clicked",
1302 G_CALLBACK(hide_io_dialog_cb), children);
1303 gtk_widget_show(hbox);
1306 gtk_widget_set_sensitive(close_button, FALSE);
1308 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1310 children->dialog = dialog;
1311 children->scrolledwin = scrolledwin;
1312 children->text = text;
1313 children->input_hbox = children->open_in ? input_hbox : NULL;
1314 children->input_entry = children->open_in ? entry : NULL;
1315 children->progress_bar = progress_bar;
1316 children->abort_btn = abort_button;
1317 children->close_btn = close_button;
1319 gtk_widget_show(dialog);
1322 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1324 ChildInfo *child_info = (ChildInfo *)data;
1328 gdk_input_remove(child_info->tag_status);
1330 c = read(source, &buf, 1);
1331 debug_print("Child returned %c\n", buf);
1334 waitpid(-child_info->pid, NULL, 0);
1336 childinfo_close_pipes(child_info);
1337 child_info->pid = 0;
1339 if (child_info->children->action_type & (ACTION_SINGLE | ACTION_MULTIPLE)
1340 && child_info->msginfo_list) {
1341 /* Actions on message *files* might change size and
1342 * time stamp, and thus invalidate the cache */
1343 SummaryView *summaryview = NULL;
1345 MsgInfo *msginfo, *nmi; /* newmsginfo */
1348 if (mainwindow_get_mainwindow ())
1349 summaryview = mainwindow_get_mainwindow ()->summaryview;
1350 for (cur = child_info->msginfo_list; cur; cur = cur->next) {
1351 msginfo = (MsgInfo *)cur->data;
1352 if (!(msginfo && /* Stuff used valid? */
1353 msginfo->folder && msginfo->folder->cache))
1355 file = procmsg_get_message_file_path (msginfo);
1358 nmi = procheader_parse_file (file, msginfo->flags, FALSE, FALSE);
1360 continue; /* Deleted? */
1361 if (msginfo->mtime != nmi->mtime || msginfo->size != nmi->size) {
1362 msginfo->mtime = nmi->mtime;
1363 msginfo->size = nmi->size;
1364 msgcache_update_msg (msginfo->folder->cache, msginfo);
1366 procmsg_msginfo_free (nmi);
1368 /* refresh the message display, but only if this message is still
1371 && summaryview->displayed
1372 && summaryview->folder_item == msginfo->folder
1373 && summary_get_msgnum (summaryview, summaryview->displayed) == msginfo->msgnum) {
1374 summary_show (summaryview, summaryview->folder_item);
1375 summary_select_node (summaryview, summaryview->displayed, TRUE, TRUE);
1378 g_slist_free (child_info->msginfo_list);
1379 child_info->msginfo_list = NULL;
1382 wait_for_children(child_info->children);
1385 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1387 Children *children = (Children *)data;
1388 ChildInfo *child_info = (ChildInfo *)children->list->data;
1389 gchar *input, *ret_str;
1391 gssize by_read = 0, by_written = 0;
1393 debug_print("Sending input to grand child.\n");
1394 if (!(cond && GDK_INPUT_WRITE))
1397 gdk_input_remove(child_info->tag_in);
1398 child_info->tag_in = -1;
1400 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1402 ret_str = g_locale_from_utf8(input, strlen(input), &by_read,
1404 if (ret_str && by_written) {
1409 len = strlen(input);
1413 c = write(child_info->chld_in, input + count, len - count);
1416 } while (c >= 0 && count < len);
1419 write(child_info->chld_in, "\n", 2);
1423 gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1424 gtk_widget_set_sensitive(children->input_hbox, TRUE);
1425 close(child_info->chld_in);
1426 child_info->chld_in = -1;
1427 debug_print("Input to grand child sent.\n");
1430 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1432 ChildInfo *child_info = (ChildInfo *)data;
1434 gchar buf[BUFFSIZE];
1436 debug_print("Catching grand child's output.\n");
1437 if (child_info->children->action_type &
1438 (ACTION_PIPE_OUT | ACTION_INSERT)
1439 && source == child_info->chld_out) {
1441 GTK_TEXT_VIEW(child_info->children->msg_text);
1442 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
1447 mark = gtk_text_buffer_get_insert(textbuf);
1448 gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1449 ins_pos = gtk_text_iter_get_offset(&iter);
1452 gsize bytes_read = 0, bytes_written = 0;
1455 c = read(source, buf, sizeof(buf) - 1);
1459 ret_str = g_locale_to_utf8
1460 (buf, c, &bytes_read, &bytes_written, NULL);
1461 if (ret_str && bytes_written > 0) {
1462 gtk_text_buffer_insert
1463 (textbuf, &iter, ret_str,
1467 gtk_text_buffer_insert(textbuf, &iter, buf, c);
1470 if (child_info->children->is_selection) {
1473 gtk_text_buffer_get_iter_at_offset
1474 (textbuf, &ins, ins_pos);
1475 gtk_text_buffer_select_range(textbuf, &ins, &iter);
1478 c = read(source, buf, sizeof(buf) - 1);
1480 gsize bytes_read = 0, bytes_written = 0;
1483 ret_str = g_locale_to_utf8
1484 (buf, c, &bytes_read, &bytes_written, NULL);
1485 if (ret_str && bytes_written > 0) {
1487 (child_info->output, ret_str,
1491 g_string_append_len(child_info->output, buf, c);
1493 child_info->new_out = TRUE;
1497 if (source == child_info->chld_out) {
1498 gdk_input_remove(child_info->tag_out);
1499 child_info->tag_out = -1;
1500 close(child_info->chld_out);
1501 child_info->chld_out = -1;
1503 gdk_input_remove(child_info->tag_err);
1504 child_info->tag_err = -1;
1505 close(child_info->chld_err);
1506 child_info->chld_err = -1;
1510 wait_for_children(child_info->children);
1513 static gchar *get_user_string(const gchar *action, ActionType type)
1516 gchar *user_str = NULL;
1519 case ACTION_USER_HIDDEN_STR:
1520 message = g_strdup_printf
1521 (_("Enter the argument for the following action:\n"
1522 "('%%h' will be replaced with the argument)\n"
1525 user_str = input_dialog_with_invisible
1526 (_("Action's hidden user argument"), message, NULL);
1528 case ACTION_USER_STR:
1529 message = g_strdup_printf
1530 (_("Enter the argument for the following action:\n"
1531 "('%%u' will be replaced with the argument)\n"
1534 user_str = input_dialog
1535 (_("Action's user argument"), message, NULL);
1538 g_warning("Unsupported action type %d", type);