2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2005 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"
58 typedef struct _Children Children;
59 typedef struct _ChildInfo ChildInfo;
60 typedef struct _UserStringDialog UserStringDialog;
66 GtkWidget *input_entry;
67 GtkWidget *input_hbox;
68 GtkWidget *progress_bar;
71 GtkWidget *scrolledwin;
74 ActionType action_type;
83 gboolean is_selection;
104 static void action_update_menu (GtkItemFactory *ifactory,
108 static void compose_actions_execute_cb (Compose *compose,
111 static void mainwin_actions_execute_cb (MainWindow *mainwin,
114 static void msgview_actions_execute_cb (MessageView *msgview,
117 static void message_actions_execute (MessageView *msgview,
121 static gboolean execute_filtering_actions(gchar *action,
124 static gboolean execute_actions (gchar *action,
130 static gchar *parse_action_cmd (gchar *action,
134 const gchar *user_str,
135 const gchar *user_hidden_str,
136 const gchar *sel_str);
137 static gboolean parse_append_filename (GString *cmd,
140 static gboolean parse_append_msgpart (GString *cmd,
144 static ChildInfo *fork_child (gchar *cmd,
145 const gchar *msg_str,
148 static gint wait_for_children (Children *children);
150 static void free_children (Children *children);
152 static void childinfo_close_pipes (ChildInfo *child_info);
154 static void create_io_dialog (Children *children);
155 static void update_io_dialog (Children *children);
157 static void hide_io_dialog_cb (GtkWidget *widget,
159 static gint io_dialog_key_pressed_cb (GtkWidget *widget,
163 static void catch_output (gpointer data,
165 GdkInputCondition cond);
166 static void catch_input (gpointer data,
168 GdkInputCondition cond);
169 static void catch_status (gpointer data,
171 GdkInputCondition cond);
173 static gchar *get_user_string (const gchar *action,
177 ActionType action_get_type(const gchar *action_str)
180 ActionType action_type = ACTION_NONE;
182 g_return_val_if_fail(action_str, ACTION_ERROR);
183 g_return_val_if_fail(*action_str, ACTION_ERROR);
188 action_type |= ACTION_PIPE_IN;
190 } else if (p[0] == '>') {
191 action_type |= ACTION_USER_IN;
193 } else if (p[0] == '*') {
194 action_type |= ACTION_USER_HIDDEN_IN;
201 while (*p && action_type != ACTION_ERROR) {
202 if (p[0] == '%' && p[1]) {
205 /* CLAWS: filtering action is a mutually exclusive
206 * action. we can enable others if needed later. we
207 * add ACTION_SINGLE | ACTION_MULTIPLE so it will
208 * only be executed from the main window toolbar */
209 if (p[2] == 's') /* source messages */
210 action_type = ACTION_FILTERING_ACTION
215 action_type |= ACTION_SINGLE;
218 action_type |= ACTION_MULTIPLE;
221 action_type |= ACTION_SINGLE;
224 action_type |= ACTION_SELECTION_STR;
227 action_type |= ACTION_USER_STR;
230 action_type |= ACTION_USER_HIDDEN_STR;
233 action_type = ACTION_ERROR;
236 } else if (p[0] == '|') {
238 action_type |= ACTION_PIPE_OUT;
239 } else if (p[0] == '>') {
241 action_type |= ACTION_INSERT;
242 } else if (p[0] == '&') {
244 action_type |= ACTION_ASYNC;
252 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
253 GSList *msg_list, MimeInfo *partinfo,
254 const gchar *user_str,
255 const gchar *user_hidden_str,
256 const gchar *sel_str)
264 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
267 cmd = g_string_sized_new(strlen(action));
270 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
271 if (p[0] == '%' && p[1]) {
274 if (!parse_append_filename(cmd, msginfo)) {
275 g_string_free(cmd, TRUE);
281 for (cur = msg_list; cur != NULL;
283 MsgInfo *msg = (MsgInfo *)cur->data;
285 if (!parse_append_filename(cmd, msg)) {
286 g_string_free(cmd, TRUE);
290 g_string_append_c(cmd, ' ');
295 if (!parse_append_msgpart(cmd, msginfo,
297 g_string_free(cmd, TRUE);
304 g_string_append(cmd, sel_str);
309 g_string_append(cmd, user_str);
314 g_string_append(cmd, user_hidden_str);
318 g_string_append_c(cmd, p[0]);
319 g_string_append_c(cmd, p[1]);
323 g_string_append_c(cmd, p[0]);
328 g_string_free(cmd, TRUE);
333 g_string_free(cmd, FALSE);
337 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
341 gchar escape_ch[] = "\\ ";
343 g_return_val_if_fail(msginfo, FALSE);
345 filename = procmsg_get_message_file(msginfo);
348 alertpanel_error(_("Could not get message file %d"),
354 while ((q = strpbrk(p, "$\"`'\\ \t*?[]&|;<>()!#~")) != NULL) {
357 g_string_append(cmd, p);
358 g_string_append(cmd, escape_ch);
361 g_string_append(cmd, p);
368 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
371 gboolean single_part = FALSE;
373 gchar *part_filename;
377 partinfo = procmime_scan_message(msginfo);
379 alertpanel_error(_("Could not get message part."));
386 filename = procmsg_get_message_file_path(msginfo);
387 part_filename = procmime_get_tmp_file_name(partinfo);
389 ret = procmime_get_part(part_filename, partinfo);
392 procmime_mimeinfo_free_all(partinfo);
396 alertpanel_error(_("Can't get part of multipart message"));
397 g_free(part_filename);
401 g_string_append(cmd, part_filename);
403 g_free(part_filename);
408 void actions_execute(gpointer data,
413 if (source == TOOLBAR_MAIN)
414 mainwin_actions_execute_cb((MainWindow*)data, action_nb, widget);
415 else if (source == TOOLBAR_COMPOSE)
416 compose_actions_execute_cb((Compose*)data, action_nb, widget);
417 else if (source == TOOLBAR_MSGVIEW)
418 msgview_actions_execute_cb((MessageView*)data, action_nb, widget);
421 void action_update_mainwin_menu(GtkItemFactory *ifactory,
425 action_update_menu(ifactory, branch_path,
426 mainwin_actions_execute_cb, mainwin);
429 void action_update_msgview_menu(GtkItemFactory *ifactory,
431 MessageView *msgview)
433 action_update_menu(ifactory, branch_path,
434 msgview_actions_execute_cb, msgview);
437 void action_update_compose_menu(GtkItemFactory *ifactory,
441 action_update_menu(ifactory, branch_path,
442 compose_actions_execute_cb, compose);
445 static void action_update_menu(GtkItemFactory *ifactory,
447 gpointer callback, gpointer data)
452 gchar *action, *action_p;
454 GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
456 ifentry.path = branch_path;
457 menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
458 g_return_if_fail(menuitem != NULL);
460 amenu = GTK_MENU_SHELL(menuitem)->children;
461 while (amenu != NULL) {
462 GList *alist = amenu->next;
463 gtk_widget_destroy(GTK_WIDGET(amenu->data));
467 ifentry.accelerator = NULL;
468 ifentry.callback_action = 0;
469 ifentry.callback = callback;
470 ifentry.item_type = NULL;
472 for (cur = prefs_common.actions_list; cur; cur = cur->next) {
473 action = g_strdup((gchar *)cur->data);
474 action_p = strstr(action, ": ");
475 if (action_p && action_p[2] &&
476 action_get_type(&action_p[2]) != ACTION_ERROR) {
478 menu_path = g_strdup_printf("%s/%s", branch_path,
480 ifentry.path = menu_path;
481 gtk_item_factory_create_item(ifactory, &ifentry, data,
486 ifentry.callback_action++;
490 static void compose_actions_execute_cb(Compose *compose, guint action_nb,
494 ActionType action_type;
496 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
498 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
499 g_return_if_fail(buf != NULL);
500 action = strstr(buf, ": ");
501 g_return_if_fail(action != NULL);
503 /* Point to the beginning of the command-line */
506 action_type = action_get_type(action);
507 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
509 (_("The selected action cannot be used in the compose window\n"
510 "because it contains %%f, %%F, %%as or %%p."));
514 execute_actions(action, NULL, compose->text, 0, NULL);
517 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
522 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
523 message_actions_execute(mainwin->messageview, action_nb, msg_list);
524 g_slist_free(msg_list);
527 static void msgview_actions_execute_cb(MessageView *msgview, guint action_nb,
530 GSList *msg_list = NULL;
532 if (msgview->msginfo)
533 msg_list = g_slist_append(msg_list, msgview->msginfo);
534 message_actions_execute(msgview, action_nb, msg_list);
535 g_slist_free(msg_list);
538 static void message_actions_execute(MessageView *msgview, guint action_nb,
545 GtkWidget *text = NULL;
547 ActionType action_type;
549 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
551 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
553 g_return_if_fail(buf);
554 g_return_if_fail((action = strstr(buf, ": ")));
556 /* Point to the beginning of the command-line */
559 textview = messageview_get_current_textview(msgview);
561 text = textview->text;
562 body_pos = textview->body_pos;
564 partinfo = messageview_get_selected_mime_part(msgview);
566 /* this command will alter the message text */
567 action_type = action_get_type(action);
568 if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
569 msgview->filtered = TRUE;
571 if (action_type & ACTION_FILTERING_ACTION)
572 /* CLAWS: most of the above code is not necessary for applying
574 execute_filtering_actions(action, msg_list);
576 execute_actions(action, msg_list, text, body_pos, partinfo);
579 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
581 GSList *action_list, *p;
582 const gchar *sbegin, *send;
583 gchar *action_string;
585 if (NULL == (sbegin = strstr2(action, "%as{")))
587 sbegin += sizeof "%as{" - 1;
588 if (NULL == (send = strrchr(sbegin, '}')))
590 action_string = g_strndup(sbegin, send - sbegin);
592 action_list = matcher_parser_get_action_list(action_string);
593 g_free(action_string);
594 if (action_list == NULL) return FALSE;
596 /* apply actions on each message info */
597 for (p = msglist; p && p->data; p = g_slist_next(p))
598 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
600 for (p = action_list; p; p = g_slist_next(p))
601 if (p->data) filteringaction_free(p->data);
602 g_slist_free(action_list);
606 static gboolean execute_actions(gchar *action, GSList *msg_list,
608 gint body_pos, MimeInfo *partinfo)
610 GSList *children_list = NULL;
614 ChildInfo *child_info;
615 ActionType action_type;
618 gchar *sel_str = NULL;
619 gchar *msg_str = NULL;
620 gchar *user_str = NULL;
621 gchar *user_hidden_str = NULL;
622 GtkTextIter start_iter, end_iter;
623 gboolean is_selection = FALSE;
625 g_return_val_if_fail(action && *action, FALSE);
627 action_type = action_get_type(action);
629 if (action_type == ACTION_ERROR)
630 return FALSE; /* ERR: syntax error */
632 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
633 return FALSE; /* ERR: file command without selection */
635 msg_list_len = g_slist_length(msg_list);
637 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
638 if (msg_list_len > 1)
639 return FALSE; /* ERR: pipe + multiple selection */
641 return FALSE; /* ERR: pipe and no displayed text */
644 if (action_type & ACTION_SELECTION_STR) {
646 return FALSE; /* ERR: selection string but no text */
650 GtkTextBuffer *textbuf;
652 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
653 is_selection = gtk_text_buffer_get_selection_bounds
654 (textbuf, &start_iter, &end_iter);
656 gtk_text_buffer_get_iter_at_offset
657 (textbuf, &start_iter, body_pos);
658 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
660 msg_str = gtk_text_buffer_get_text
661 (textbuf, &start_iter, &end_iter, FALSE);
663 sel_str = g_strdup(msg_str);
666 if (action_type & ACTION_USER_STR) {
667 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
674 if (action_type & ACTION_USER_HIDDEN_STR) {
675 if (!(user_hidden_str =
676 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
684 if (text && (action_type & ACTION_PIPE_OUT)) {
685 GtkTextBuffer *textbuf;
686 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
687 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
690 children = g_new0(Children, 1);
692 children->action = g_strdup(action);
693 children->action_type = action_type;
694 children->msg_text = text;
695 children->is_selection = is_selection;
697 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
698 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
699 children->open_in = 1;
701 if (action_type & ACTION_SINGLE) {
704 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
705 msginfo = (MsgInfo *)cur->data;
707 is_ok = FALSE; /* ERR: msginfo missing */
710 cmd = parse_action_cmd(action, msginfo, msg_list,
712 user_hidden_str, sel_str);
714 debug_print("Action command error\n");
715 is_ok = FALSE; /* ERR: incorrect command */
718 if ((child_info = fork_child(cmd, msg_str, children))) {
719 children_list = g_slist_append(children_list,
725 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
726 user_str, user_hidden_str, sel_str);
728 if ((child_info = fork_child(cmd, msg_str, children))) {
729 children_list = g_slist_append(children_list,
734 is_ok = FALSE; /* ERR: incorrect command */
740 g_free(user_hidden_str);
742 if (!children_list) {
743 /* If not waiting for children, return */
744 free_children(children);
748 children->list = children_list;
749 children->nb = g_slist_length(children_list);
750 children->initial_nb = children->nb;
752 for (cur = children_list; cur; cur = cur->next) {
753 child_info = (ChildInfo *) cur->data;
754 child_info->tag_status =
755 gdk_input_add(child_info->chld_status,
757 catch_status, child_info);
760 create_io_dialog(children);
766 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
770 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
771 gchar *cmdline[4], *ret_str;
773 ChildInfo *child_info;
775 gssize by_written = 0, by_read = 0;
777 sync = !(children->action_type & ACTION_ASYNC);
779 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
780 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
783 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
785 alertpanel_error(_("Command could not be started. "
786 "Pipe creation failed.\n%s"),
788 /* Closing fd = -1 fails silently */
795 close(chld_status[0]);
796 close(chld_status[1]);
797 return NULL; /* Pipe error */
801 debug_print("Forking child and grandchild.\n");
802 debug_print("Executing: /bin/sh -c %s\n", cmd);
805 if (pid == 0) { /* Child */
809 #ifdef GDK_WINDOWING_X11
810 close(ConnectionNumber(gdk_display));
811 #endif /* GDK_WINDOWING_X11 */
816 if (setpgid(0, getppid()))
820 if (children->action_type &
823 ACTION_USER_HIDDEN_IN)) {
824 close(fileno(stdin));
830 close(fileno(stdout));
835 close(fileno(stderr));
843 ret_str = g_locale_from_utf8(cmd, strlen(cmd),
844 &by_read, &by_written,
846 if (ret_str && by_written)
847 cmdline[2] = ret_str;
851 execvp("/bin/sh", cmdline);
856 } else if (gch_pid < (pid_t) 0) { /* Fork error */
858 write(chld_status[1], "1\n", 2);
869 close(chld_status[0]);
871 debug_print("Child: Waiting for grandchild\n");
872 waitpid(gch_pid, NULL, 0);
873 debug_print("Child: grandchild ended\n");
874 write(chld_status[1], "0\n", 2);
875 close(chld_status[1]);
879 } else if (pid < 0) { /* Fork error */
880 alertpanel_error(_("Could not fork to execute the following "
882 cmd, g_strerror(errno));
889 waitpid(pid, NULL, 0);
894 if (!(children->action_type &
895 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
899 close(chld_status[1]);
901 child_info = g_new0(ChildInfo, 1);
903 child_info->children = children;
905 child_info->pid = pid;
906 child_info->cmd = g_strdup(cmd);
907 child_info->new_out = FALSE;
908 child_info->output = g_string_new(NULL);
909 child_info->chld_in =
910 (children->action_type &
911 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
913 child_info->chld_out = chld_out[0];
914 child_info->chld_err = chld_err[0];
915 child_info->chld_status = chld_status[0];
916 child_info->tag_in = -1;
917 child_info->tag_out = gdk_input_add(chld_out[0], GDK_INPUT_READ,
918 catch_output, child_info);
919 child_info->tag_err = gdk_input_add(chld_err[0], GDK_INPUT_READ,
920 catch_output, child_info);
922 if (!(children->action_type &
923 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
926 if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
927 ret_str = g_locale_from_utf8(msg_str, strlen(msg_str),
928 &by_read, &by_written, NULL);
929 if (ret_str && by_written) {
930 write(chld_in[1], ret_str, strlen(ret_str));
933 write(chld_in[1], msg_str, strlen(msg_str));
934 if (!(children->action_type &
935 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
937 child_info->chld_in = -1; /* No more input */
943 #endif /* G_OS_UNIX */
946 static void kill_children_cb(GtkWidget *widget, gpointer data)
950 Children *children = (Children *) data;
951 ChildInfo *child_info;
953 for (cur = children->list; cur; cur = cur->next) {
954 child_info = (ChildInfo *)(cur->data);
955 debug_print("Killing child group id %d\n", child_info->pid);
956 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
959 #endif /* G_OS_UNIX */
962 static gint wait_for_children(Children *children)
965 ChildInfo *child_info;
967 gint nb = children->nb;
971 cur = children->list;
974 child_info = (ChildInfo *)cur->data;
977 new_output |= child_info->new_out;
981 children->output |= new_output;
983 if (new_output || (children->dialog && (nb != children->nb)))
984 update_io_dialog(children);
989 if (!children->dialog) {
990 free_children(children);
991 } else if (!children->output) {
992 gtk_widget_destroy(children->dialog);
998 static void send_input(GtkWidget *w, gpointer data)
1000 Children *children = (Children *) data;
1001 ChildInfo *child_info = (ChildInfo *) children->list->data;
1003 child_info->tag_in = gdk_input_add(child_info->chld_in,
1005 catch_input, children);
1006 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1009 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1011 hide_io_dialog_cb(w, data);
1015 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1018 Children *children = (Children *)data;
1020 if (!children->nb) {
1021 g_signal_handlers_disconnect_matched
1022 (G_OBJECT(children->dialog), G_SIGNAL_MATCH_DATA,
1023 0, 0, NULL, NULL, children);
1024 gtk_widget_destroy(children->dialog);
1025 free_children(children);
1029 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1032 if (event && event->keyval == GDK_Escape)
1033 hide_io_dialog_cb(widget, data);
1037 static void childinfo_close_pipes(ChildInfo *child_info)
1039 /* stdout and stderr pipes are guaranteed to be removed by
1040 * their handler, but in case where we receive child exit notification
1041 * before grand-child's pipes closing signals, we check them and close
1044 if (child_info->tag_in > 0)
1045 gdk_input_remove(child_info->tag_in);
1046 if (child_info->tag_out > 0)
1047 gdk_input_remove(child_info->tag_out);
1048 if (child_info->tag_err > 0)
1049 gdk_input_remove(child_info->tag_err);
1051 if (child_info->chld_in >= 0)
1052 close(child_info->chld_in);
1053 if (child_info->chld_out >= 0)
1054 close(child_info->chld_out);
1055 if (child_info->chld_err >= 0)
1056 close(child_info->chld_err);
1058 close(child_info->chld_status);
1061 static void free_children(Children *children)
1063 ChildInfo *child_info;
1065 debug_print("Freeing children data %p\n", children);
1067 g_free(children->action);
1068 while (children->list != NULL) {
1069 child_info = (ChildInfo *)children->list->data;
1070 g_free(child_info->cmd);
1071 g_string_free(child_info->output, TRUE);
1072 children->list = g_slist_remove(children->list, child_info);
1078 static void update_io_dialog(Children *children)
1082 debug_print("Updating actions input/output dialog.\n");
1084 if (children->progress_bar) {
1087 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar),
1088 (gdouble) (children->initial_nb - children->nb) /
1089 (gdouble) children->initial_nb);
1090 text = g_strdup_printf("%s %d/%d", _("Completed"),
1091 children->initial_nb - children->nb,
1092 children->initial_nb);
1093 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(children->progress_bar), text);
1097 if (!children->nb) {
1098 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1099 gtk_widget_set_sensitive(children->close_btn, TRUE);
1100 if (children->input_hbox)
1101 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1102 gtk_widget_grab_focus(children->close_btn);
1103 g_signal_connect(G_OBJECT(children->dialog),
1105 G_CALLBACK(io_dialog_key_pressed_cb),
1109 if (children->output) {
1110 GtkWidget *text = children->text;
1111 GtkTextBuffer *textbuf;
1112 GtkTextIter iter, start_iter, end_iter;
1114 ChildInfo *child_info;
1116 gtk_widget_show(children->scrolledwin);
1117 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
1118 gtk_text_buffer_get_bounds(textbuf, &start_iter, &end_iter);
1119 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
1120 gtk_text_buffer_get_start_iter(textbuf, &iter);
1122 for (cur = children->list; cur; cur = cur->next) {
1123 child_info = (ChildInfo *)cur->data;
1124 if (child_info->pid)
1125 caption = g_strdup_printf
1126 (_("--- Running: %s\n"),
1129 caption = g_strdup_printf
1130 (_("--- Ended: %s\n"),
1133 gtk_text_buffer_insert(textbuf, &iter, caption, -1);
1134 gtk_text_buffer_insert(textbuf, &iter,
1135 child_info->output->str, -1);
1137 child_info->new_out = FALSE;
1142 static void create_io_dialog(Children *children)
1146 GtkWidget *entry = NULL;
1147 GtkWidget *input_hbox = NULL;
1148 GtkWidget *send_button;
1151 GtkWidget *scrolledwin;
1153 GtkWidget *progress_bar = NULL;
1154 GtkWidget *abort_button;
1155 GtkWidget *close_button;
1157 debug_print("Creating action IO dialog\n");
1159 dialog = gtk_dialog_new();
1160 gtk_container_set_border_width
1161 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1162 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1163 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1164 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1165 manage_window_set_transient(GTK_WINDOW(dialog));
1166 g_signal_connect(G_OBJECT(dialog), "delete_event",
1167 G_CALLBACK(delete_io_dialog_cb), children);
1168 g_signal_connect(G_OBJECT(dialog), "destroy",
1169 G_CALLBACK(hide_io_dialog_cb),
1172 vbox = gtk_vbox_new(FALSE, 8);
1173 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1174 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1175 gtk_widget_show(vbox);
1177 label = gtk_label_new(children->action);
1178 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1179 gtk_widget_show(label);
1181 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1182 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1183 GTK_POLICY_AUTOMATIC,
1184 GTK_POLICY_AUTOMATIC);
1185 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
1187 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1188 gtk_widget_set_size_request(scrolledwin, 560, 200);
1189 gtk_widget_hide(scrolledwin);
1191 text = gtk_text_view_new();
1193 if (prefs_common.textfont) {
1194 PangoFontDescription *font_desc;
1195 font_desc = pango_font_description_from_string
1196 (prefs_common.textfont);
1198 gtk_widget_modify_font(text, font_desc);
1199 pango_font_description_free(font_desc);
1203 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
1204 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
1205 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
1206 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
1207 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1208 gtk_widget_show(text);
1210 if (children->open_in) {
1211 input_hbox = gtk_hbox_new(FALSE, 8);
1212 gtk_widget_show(input_hbox);
1214 entry = gtk_entry_new();
1215 gtk_widget_set_size_request(entry, 320, -1);
1216 g_signal_connect(G_OBJECT(entry), "activate",
1217 G_CALLBACK(send_input), children);
1218 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1219 if (children->action_type & ACTION_USER_HIDDEN_IN)
1220 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1221 gtk_widget_show(entry);
1223 send_button = gtk_button_new_from_stock(GTK_STOCK_EXECUTE);
1224 g_signal_connect(G_OBJECT(send_button), "clicked",
1225 G_CALLBACK(send_input), children);
1226 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1228 gtk_widget_show(send_button);
1230 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1231 gtk_widget_grab_focus(entry);
1234 if (children->initial_nb > 1) {
1237 progress_bar = gtk_progress_bar_new();
1238 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1239 GTK_PROGRESS_LEFT_TO_RIGHT);
1240 text = g_strdup_printf("%s 0/%d\n", _("Completed"),
1241 children->initial_nb);
1242 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar),
1245 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1246 gtk_widget_show(progress_bar);
1249 gtkut_stock_button_set_create(&hbox, &abort_button, GTK_STOCK_STOP,
1250 &close_button, GTK_STOCK_CLOSE, NULL, NULL);
1251 g_signal_connect(G_OBJECT(abort_button), "clicked",
1252 G_CALLBACK(kill_children_cb), children);
1253 g_signal_connect(G_OBJECT(close_button), "clicked",
1254 G_CALLBACK(hide_io_dialog_cb), children);
1255 gtk_widget_show(hbox);
1258 gtk_widget_set_sensitive(close_button, FALSE);
1260 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1262 children->dialog = dialog;
1263 children->scrolledwin = scrolledwin;
1264 children->text = text;
1265 children->input_hbox = children->open_in ? input_hbox : NULL;
1266 children->input_entry = children->open_in ? entry : NULL;
1267 children->progress_bar = progress_bar;
1268 children->abort_btn = abort_button;
1269 children->close_btn = close_button;
1271 gtk_widget_show(dialog);
1274 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1276 ChildInfo *child_info = (ChildInfo *)data;
1280 gdk_input_remove(child_info->tag_status);
1282 c = read(source, &buf, 1);
1283 debug_print("Child returned %c\n", buf);
1286 waitpid(-child_info->pid, NULL, 0);
1288 childinfo_close_pipes(child_info);
1289 child_info->pid = 0;
1291 wait_for_children(child_info->children);
1294 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1296 Children *children = (Children *)data;
1297 ChildInfo *child_info = (ChildInfo *)children->list->data;
1298 gchar *input, *ret_str;
1300 gssize by_read = 0, by_written = 0;
1302 debug_print("Sending input to grand child.\n");
1303 if (!(cond && GDK_INPUT_WRITE))
1306 gdk_input_remove(child_info->tag_in);
1307 child_info->tag_in = -1;
1309 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1311 ret_str = g_locale_from_utf8(input, strlen(input), &by_read,
1313 if (ret_str && by_written) {
1318 len = strlen(input);
1322 c = write(child_info->chld_in, input + count, len - count);
1325 } while (c >= 0 && count < len);
1328 write(child_info->chld_in, "\n", 2);
1332 gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1333 gtk_widget_set_sensitive(children->input_hbox, TRUE);
1334 close(child_info->chld_in);
1335 child_info->chld_in = -1;
1336 debug_print("Input to grand child sent.\n");
1339 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1341 ChildInfo *child_info = (ChildInfo *)data;
1343 gchar buf[BUFFSIZE];
1345 debug_print("Catching grand child's output.\n");
1346 if (child_info->children->action_type &
1347 (ACTION_PIPE_OUT | ACTION_INSERT)
1348 && source == child_info->chld_out) {
1350 GTK_TEXT_VIEW(child_info->children->msg_text);
1351 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
1356 mark = gtk_text_buffer_get_insert(textbuf);
1357 gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1358 ins_pos = gtk_text_iter_get_offset(&iter);
1361 gsize bytes_read = 0, bytes_written = 0;
1364 c = read(source, buf, sizeof(buf) - 1);
1368 ret_str = g_locale_to_utf8
1369 (buf, c, &bytes_read, &bytes_written, NULL);
1370 if (ret_str && bytes_written > 0) {
1371 gtk_text_buffer_insert
1372 (textbuf, &iter, ret_str,
1376 gtk_text_buffer_insert(textbuf, &iter, buf, c);
1379 if (child_info->children->is_selection) {
1382 gtk_text_buffer_get_iter_at_offset
1383 (textbuf, &ins, ins_pos);
1384 gtk_text_buffer_select_range(textbuf, &ins, &iter);
1387 c = read(source, buf, sizeof(buf) - 1);
1389 gsize bytes_read = 0, bytes_written = 0;
1392 ret_str = g_locale_to_utf8
1393 (buf, c, &bytes_read, &bytes_written, NULL);
1394 if (ret_str && bytes_written > 0) {
1396 (child_info->output, ret_str,
1400 g_string_append_len(child_info->output, buf, c);
1402 child_info->new_out = TRUE;
1406 if (source == child_info->chld_out) {
1407 gdk_input_remove(child_info->tag_out);
1408 child_info->tag_out = -1;
1409 close(child_info->chld_out);
1410 child_info->chld_out = -1;
1412 gdk_input_remove(child_info->tag_err);
1413 child_info->tag_err = -1;
1414 close(child_info->chld_err);
1415 child_info->chld_err = -1;
1419 wait_for_children(child_info->children);
1422 static gchar *get_user_string(const gchar *action, ActionType type)
1425 gchar *user_str = NULL;
1428 case ACTION_USER_HIDDEN_STR:
1429 message = g_strdup_printf
1430 (_("Enter the argument for the following action:\n"
1431 "('%%h' will be replaced with the argument)\n"
1434 user_str = input_dialog_with_invisible
1435 (_("Action's hidden user argument"), message, NULL);
1437 case ACTION_USER_STR:
1438 message = g_strdup_printf
1439 (_("Enter the argument for the following action:\n"
1440 "('%%u' will be replaced with the argument)\n"
1443 user_str = input_dialog
1444 (_("Action's user argument"), message, NULL);
1447 g_warning("Unsupported action type %d", type);