2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include <gdk/gdkkeysyms.h>
29 #ifdef GDK_WINDOWING_X11
30 # include <gdk/gdkx.h>
31 #endif /* GDK_WINDOWING_X11 */
36 #include <sys/types.h>
44 #include "manage_window.h"
45 #include "mainwindow.h"
46 #include "prefs_common.h"
47 #include "alertpanel.h"
48 #include "inputdialog.h"
53 #include "matcher_parser.h" /* CLAWS */
54 #include "filtering.h"
56 typedef struct _Children Children;
57 typedef struct _ChildInfo ChildInfo;
58 typedef struct _UserStringDialog UserStringDialog;
64 GtkWidget *input_entry;
65 GtkWidget *input_hbox;
66 GtkWidget *progress_bar;
69 GtkWidget *scrolledwin;
72 ActionType action_type;
81 gboolean is_selection;
102 static void action_update_menu (GtkItemFactory *ifactory,
106 static void compose_actions_execute_cb (Compose *compose,
109 static void mainwin_actions_execute_cb (MainWindow *mainwin,
112 static void msgview_actions_execute_cb (MessageView *msgview,
115 static void message_actions_execute (MessageView *msgview,
119 static gboolean execute_filtering_actions(gchar *action,
122 static gboolean execute_actions (gchar *action,
128 static gchar *parse_action_cmd (gchar *action,
132 const gchar *user_str,
133 const gchar *user_hidden_str,
134 const gchar *sel_str);
135 static gboolean parse_append_filename (GString *cmd,
138 static gboolean parse_append_msgpart (GString *cmd,
142 static ChildInfo *fork_child (gchar *cmd,
143 const gchar *msg_str,
146 static gint wait_for_children (Children *children);
148 static void free_children (Children *children);
150 static void childinfo_close_pipes (ChildInfo *child_info);
152 static void create_io_dialog (Children *children);
153 static void update_io_dialog (Children *children);
155 static void hide_io_dialog_cb (GtkWidget *widget,
157 static gint io_dialog_key_pressed_cb (GtkWidget *widget,
161 static void catch_output (gpointer data,
163 GdkInputCondition cond);
164 static void catch_input (gpointer data,
166 GdkInputCondition cond);
167 static void catch_status (gpointer data,
169 GdkInputCondition cond);
171 static gchar *get_user_string (const gchar *action,
175 ActionType action_get_type(const gchar *action_str)
178 ActionType action_type = ACTION_NONE;
180 g_return_val_if_fail(action_str, ACTION_ERROR);
181 g_return_val_if_fail(*action_str, ACTION_ERROR);
186 action_type |= ACTION_PIPE_IN;
188 } else if (p[0] == '>') {
189 action_type |= ACTION_USER_IN;
191 } else if (p[0] == '*') {
192 action_type |= ACTION_USER_HIDDEN_IN;
199 while (*p && action_type != ACTION_ERROR) {
200 if (p[0] == '%' && p[1]) {
203 /* CLAWS: filtering action is a mutually exclusive
204 * action. we can enable others if needed later. we
205 * add ACTION_SINGLE | ACTION_MULTIPLE so it will
206 * only be executed from the main window toolbar */
207 if (p[2] == 's') /* source messages */
208 action_type = ACTION_FILTERING_ACTION
213 action_type |= ACTION_SINGLE;
216 action_type |= ACTION_MULTIPLE;
219 action_type |= ACTION_SINGLE;
222 action_type |= ACTION_SELECTION_STR;
225 action_type |= ACTION_USER_STR;
228 action_type |= ACTION_USER_HIDDEN_STR;
231 action_type = ACTION_ERROR;
234 } else if (p[0] == '|') {
236 action_type |= ACTION_PIPE_OUT;
237 } else if (p[0] == '>') {
239 action_type |= ACTION_INSERT;
240 } else if (p[0] == '&') {
242 action_type |= ACTION_ASYNC;
250 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
251 GSList *msg_list, MimeInfo *partinfo,
252 const gchar *user_str,
253 const gchar *user_hidden_str,
254 const gchar *sel_str)
262 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
265 cmd = g_string_sized_new(strlen(action));
268 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
269 if (p[0] == '%' && p[1]) {
272 if (!parse_append_filename(cmd, msginfo)) {
273 g_string_free(cmd, TRUE);
279 for (cur = msg_list; cur != NULL;
281 MsgInfo *msg = (MsgInfo *)cur->data;
283 if (!parse_append_filename(cmd, msg)) {
284 g_string_free(cmd, TRUE);
288 g_string_append_c(cmd, ' ');
293 if (!parse_append_msgpart(cmd, msginfo,
295 g_string_free(cmd, TRUE);
302 g_string_append(cmd, sel_str);
307 g_string_append(cmd, user_str);
312 g_string_append(cmd, user_hidden_str);
316 g_string_append_c(cmd, p[0]);
317 g_string_append_c(cmd, p[1]);
321 g_string_append_c(cmd, p[0]);
326 g_string_free(cmd, TRUE);
331 g_string_free(cmd, FALSE);
335 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
340 g_return_val_if_fail(msginfo, FALSE);
342 filename = procmsg_get_message_file(msginfo);
345 g_string_append_c(cmd, '"');
346 for (p = filename; *p != '\0'; p++) {
352 g_string_append_c(cmd, '\\');
357 g_string_append_c(cmd, *p);
359 g_string_append_c(cmd, '"');
362 alertpanel_error(_("Could not get message file %d"),
370 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
373 gboolean single_part = FALSE;
375 gchar *part_filename;
379 partinfo = procmime_scan_message(msginfo);
381 alertpanel_error(_("Could not get message part."));
388 filename = procmsg_get_message_file_path(msginfo);
389 part_filename = procmime_get_tmp_file_name(partinfo);
391 ret = procmime_get_part(part_filename, partinfo);
394 procmime_mimeinfo_free_all(partinfo);
398 alertpanel_error(_("Can't get part of multipart message"));
399 g_free(part_filename);
403 g_string_append(cmd, part_filename);
405 g_free(part_filename);
410 void actions_execute(gpointer data,
415 if (source == TOOLBAR_MAIN)
416 mainwin_actions_execute_cb((MainWindow*)data, action_nb, widget);
417 else if (source == TOOLBAR_COMPOSE)
418 compose_actions_execute_cb((Compose*)data, action_nb, widget);
419 else if (source == TOOLBAR_MSGVIEW)
420 msgview_actions_execute_cb((MessageView*)data, action_nb, widget);
423 void action_update_mainwin_menu(GtkItemFactory *ifactory,
427 action_update_menu(ifactory, branch_path,
428 mainwin_actions_execute_cb, mainwin);
431 void action_update_msgview_menu(GtkItemFactory *ifactory,
433 MessageView *msgview)
435 action_update_menu(ifactory, branch_path,
436 msgview_actions_execute_cb, msgview);
439 void action_update_compose_menu(GtkItemFactory *ifactory,
443 action_update_menu(ifactory, branch_path,
444 compose_actions_execute_cb, compose);
447 static void action_update_menu(GtkItemFactory *ifactory,
449 gpointer callback, gpointer data)
454 gchar *action, *action_p;
456 GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
458 ifentry.path = branch_path;
459 menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
460 g_return_if_fail(menuitem != NULL);
462 amenu = GTK_MENU_SHELL(menuitem)->children;
463 while (amenu != NULL) {
464 GList *alist = amenu->next;
465 gtk_widget_destroy(GTK_WIDGET(amenu->data));
469 ifentry.accelerator = NULL;
470 ifentry.callback_action = 0;
471 ifentry.callback = callback;
472 ifentry.item_type = NULL;
474 for (cur = prefs_common.actions_list; cur; cur = cur->next) {
475 action = g_strdup((gchar *)cur->data);
476 action_p = strstr(action, ": ");
477 if (action_p && action_p[2] &&
478 action_get_type(&action_p[2]) != ACTION_ERROR) {
480 menu_path = g_strdup_printf("%s/%s", branch_path,
482 ifentry.path = menu_path;
483 gtk_item_factory_create_item(ifactory, &ifentry, data,
488 ifentry.callback_action++;
492 static void compose_actions_execute_cb(Compose *compose, guint action_nb,
496 ActionType action_type;
498 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
500 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
501 g_return_if_fail(buf != NULL);
502 action = strstr(buf, ": ");
503 g_return_if_fail(action != NULL);
505 /* Point to the beginning of the command-line */
508 action_type = action_get_type(action);
509 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
511 (_("The selected action cannot be used in the compose window\n"
512 "because it contains %%f, %%F, %%as or %%p."));
516 execute_actions(action, NULL, compose->text, 0, NULL);
519 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
524 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
525 message_actions_execute(mainwin->messageview, action_nb, msg_list);
526 g_slist_free(msg_list);
529 static void msgview_actions_execute_cb(MessageView *msgview, guint action_nb,
532 GSList *msg_list = NULL;
534 if (msgview->msginfo)
535 msg_list = g_slist_append(msg_list, msgview->msginfo);
536 message_actions_execute(msgview, action_nb, msg_list);
537 g_slist_free(msg_list);
540 static void message_actions_execute(MessageView *msgview, guint action_nb,
547 GtkWidget *text = NULL;
549 ActionType action_type;
551 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
553 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
555 g_return_if_fail(buf);
556 g_return_if_fail((action = strstr(buf, ": ")));
558 /* Point to the beginning of the command-line */
561 textview = messageview_get_current_textview(msgview);
563 text = textview->text;
564 body_pos = textview->body_pos;
566 partinfo = messageview_get_selected_mime_part(msgview);
568 /* this command will alter the message text */
569 action_type = action_get_type(action);
570 if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
571 msgview->filtered = TRUE;
573 if (action_type & ACTION_FILTERING_ACTION)
574 /* CLAWS: most of the above code is not necessary for applying
576 execute_filtering_actions(action, msg_list);
578 execute_actions(action, msg_list, text, body_pos, partinfo);
581 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
583 GSList *action_list, *p;
584 const gchar *sbegin, *send;
585 gchar *action_string;
587 if (NULL == (sbegin = strstr2(action, "%as{")))
589 sbegin += sizeof "%as{" - 1;
590 if (NULL == (send = strrchr(sbegin, '}')))
592 action_string = g_strndup(sbegin, send - sbegin);
594 action_list = matcher_parser_get_action_list(action_string);
595 g_free(action_string);
596 if (action_list == NULL) return FALSE;
598 /* apply actions on each message info */
599 for (p = msglist; p && p->data; p = g_slist_next(p))
600 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
602 for (p = action_list; p; p = g_slist_next(p))
603 if (p->data) filteringaction_free(p->data);
604 g_slist_free(action_list);
608 static gboolean execute_actions(gchar *action, GSList *msg_list,
610 gint body_pos, MimeInfo *partinfo)
612 GSList *children_list = NULL;
616 ChildInfo *child_info;
617 ActionType action_type;
620 gchar *sel_str = NULL;
621 gchar *msg_str = NULL;
622 gchar *user_str = NULL;
623 gchar *user_hidden_str = NULL;
624 GtkTextIter start_iter, end_iter;
625 gboolean is_selection = FALSE;
627 g_return_val_if_fail(action && *action, FALSE);
629 action_type = action_get_type(action);
631 if (action_type == ACTION_ERROR)
632 return FALSE; /* ERR: syntax error */
634 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
635 return FALSE; /* ERR: file command without selection */
637 msg_list_len = g_slist_length(msg_list);
639 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
640 if (msg_list_len > 1)
641 return FALSE; /* ERR: pipe + multiple selection */
643 return FALSE; /* ERR: pipe and no displayed text */
646 if (action_type & ACTION_SELECTION_STR) {
648 return FALSE; /* ERR: selection string but no text */
652 GtkTextBuffer *textbuf;
654 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
655 is_selection = gtk_text_buffer_get_selection_bounds(textbuf,
659 gtk_text_buffer_get_start_iter(textbuf, &start_iter);
660 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
662 msg_str = gtk_text_buffer_get_text(textbuf,
663 &start_iter, &end_iter,
666 sel_str = g_strdup (msg_str);
669 if (action_type & ACTION_USER_STR) {
670 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
677 if (action_type & ACTION_USER_HIDDEN_STR) {
678 if (!(user_hidden_str =
679 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
687 if (text && (action_type & ACTION_PIPE_OUT)) {
688 GtkTextBuffer *textbuf;
689 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
690 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
693 children = g_new0(Children, 1);
695 children->action = g_strdup(action);
696 children->action_type = action_type;
697 children->msg_text = text;
698 children->is_selection = is_selection;
700 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
701 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
702 children->open_in = 1;
704 if (action_type & ACTION_SINGLE) {
707 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
708 msginfo = (MsgInfo *)cur->data;
710 is_ok = FALSE; /* ERR: msginfo missing */
713 cmd = parse_action_cmd(action, msginfo, msg_list,
715 user_hidden_str, sel_str);
717 debug_print("Action command error\n");
718 is_ok = FALSE; /* ERR: incorrect command */
721 if ((child_info = fork_child(cmd, msg_str, children))) {
722 children_list = g_slist_append(children_list,
728 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
729 user_str, user_hidden_str, sel_str);
731 if ((child_info = fork_child(cmd, msg_str, children))) {
732 children_list = g_slist_append(children_list,
737 is_ok = FALSE; /* ERR: incorrect command */
743 g_free(user_hidden_str);
745 if (!children_list) {
746 /* If not waiting for children, return */
747 free_children(children);
751 children->list = children_list;
752 children->nb = g_slist_length(children_list);
753 children->initial_nb = children->nb;
755 for (cur = children_list; cur; cur = cur->next) {
756 child_info = (ChildInfo *) cur->data;
757 child_info->tag_status =
758 gdk_input_add(child_info->chld_status,
760 catch_status, child_info);
763 create_io_dialog(children);
769 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
772 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
775 ChildInfo *child_info;
778 sync = !(children->action_type & ACTION_ASYNC);
780 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
781 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
784 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
786 alertpanel_error(_("Command could not be started. "
787 "Pipe creation failed.\n%s"),
789 /* Closing fd = -1 fails silently */
796 close(chld_status[0]);
797 close(chld_status[1]);
798 return NULL; /* Pipe error */
802 debug_print("Forking child and grandchild.\n");
803 debug_print("Executing: /bin/sh -c %s\n", cmd);
806 if (pid == 0) { /* Child */
810 #ifdef GDK_WINDOWING_X11
811 close(ConnectionNumber(gdk_display));
812 #endif /* GDK_WINDOWING_X11 */
817 if (setpgid(0, getppid()))
821 if (children->action_type &
824 ACTION_USER_HIDDEN_IN)) {
825 close(fileno(stdin));
831 close(fileno(stdout));
836 close(fileno(stderr));
846 execvp("/bin/sh", cmdline);
850 } else if (gch_pid < (pid_t) 0) { /* Fork error */
852 write(chld_status[1], "1\n", 2);
863 close(chld_status[0]);
865 debug_print("Child: Waiting for grandchild\n");
866 waitpid(gch_pid, NULL, 0);
867 debug_print("Child: grandchild ended\n");
868 write(chld_status[1], "0\n", 2);
869 close(chld_status[1]);
873 } else if (pid < 0) { /* Fork error */
874 alertpanel_error(_("Could not fork to execute the following "
876 cmd, g_strerror(errno));
883 waitpid(pid, NULL, 0);
888 if (!(children->action_type &
889 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
893 close(chld_status[1]);
895 child_info = g_new0(ChildInfo, 1);
897 child_info->children = children;
899 child_info->pid = pid;
900 child_info->cmd = g_strdup(cmd);
901 child_info->new_out = FALSE;
902 child_info->output = g_string_new(NULL);
903 child_info->chld_in =
904 (children->action_type &
905 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
907 child_info->chld_out = chld_out[0];
908 child_info->chld_err = chld_err[0];
909 child_info->chld_status = chld_status[0];
910 child_info->tag_in = -1;
911 child_info->tag_out = gdk_input_add(chld_out[0], GDK_INPUT_READ,
912 catch_output, child_info);
913 child_info->tag_err = gdk_input_add(chld_err[0], GDK_INPUT_READ,
914 catch_output, child_info);
916 if (!(children->action_type &
917 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
920 if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
921 write(chld_in[1], msg_str, strlen(msg_str));
922 if (!(children->action_type &
923 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
925 child_info->chld_in = -1; /* No more input */
931 static void kill_children_cb(GtkWidget *widget, gpointer data)
934 Children *children = (Children *) data;
935 ChildInfo *child_info;
937 for (cur = children->list; cur; cur = cur->next) {
938 child_info = (ChildInfo *)(cur->data);
939 debug_print("Killing child group id %d\n", child_info->pid);
940 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
945 static gint wait_for_children(Children *children)
948 ChildInfo *child_info;
950 gint nb = children->nb;
954 cur = children->list;
957 child_info = (ChildInfo *)cur->data;
960 new_output |= child_info->new_out;
964 children->output |= new_output;
966 if (new_output || (children->dialog && (nb != children->nb)))
967 update_io_dialog(children);
972 if (!children->dialog) {
973 free_children(children);
974 } else if (!children->output) {
975 gtk_widget_destroy(children->dialog);
981 static void send_input(GtkWidget *w, gpointer data)
983 Children *children = (Children *) data;
984 ChildInfo *child_info = (ChildInfo *) children->list->data;
986 child_info->tag_in = gdk_input_add(child_info->chld_in,
988 catch_input, children);
989 gtk_widget_set_sensitive(children->input_hbox, FALSE);
992 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
994 hide_io_dialog_cb(w, data);
998 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1001 Children *children = (Children *)data;
1003 if (!children->nb) {
1004 g_signal_handlers_disconnect_matched
1005 (G_OBJECT(children->dialog), G_SIGNAL_MATCH_DATA,
1006 0, 0, NULL, NULL, children);
1007 gtk_widget_destroy(children->dialog);
1008 free_children(children);
1012 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1015 if (event && event->keyval == GDK_Escape)
1016 hide_io_dialog_cb(widget, data);
1020 static void childinfo_close_pipes(ChildInfo *child_info)
1022 /* stdout and stderr pipes are guaranteed to be removed by
1023 * their handler, but in case where we receive child exit notification
1024 * before grand-child's pipes closing signals, we check them and close
1027 if (child_info->tag_in > 0)
1028 gdk_input_remove(child_info->tag_in);
1029 if (child_info->tag_out > 0)
1030 gdk_input_remove(child_info->tag_out);
1031 if (child_info->tag_err > 0)
1032 gdk_input_remove(child_info->tag_err);
1034 if (child_info->chld_in >= 0)
1035 close(child_info->chld_in);
1036 if (child_info->chld_out >= 0)
1037 close(child_info->chld_out);
1038 if (child_info->chld_err >= 0)
1039 close(child_info->chld_err);
1041 close(child_info->chld_status);
1044 static void free_children(Children *children)
1046 ChildInfo *child_info;
1048 debug_print("Freeing children data %p\n", children);
1050 g_free(children->action);
1051 while (children->list != NULL) {
1052 child_info = (ChildInfo *)children->list->data;
1053 g_free(child_info->cmd);
1054 g_string_free(child_info->output, TRUE);
1055 children->list = g_slist_remove(children->list, child_info);
1061 static void update_io_dialog(Children *children)
1065 debug_print("Updating actions input/output dialog.\n");
1067 if (children->progress_bar) {
1070 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar),
1071 (gdouble) (children->initial_nb - children->nb) /
1072 (gdouble) children->initial_nb);
1073 text = g_strdup_printf("%s %d/%d", _("Completed"),
1074 children->initial_nb - children->nb,
1075 children->initial_nb);
1076 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(children->progress_bar), text);
1080 if (!children->nb) {
1081 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1082 gtk_widget_set_sensitive(children->close_btn, TRUE);
1083 if (children->input_hbox)
1084 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1085 gtk_widget_grab_focus(children->close_btn);
1086 g_signal_connect(G_OBJECT(children->dialog),
1088 G_CALLBACK(io_dialog_key_pressed_cb),
1092 if (children->output) {
1093 GtkWidget *text = children->text;
1095 ChildInfo *child_info;
1096 GtkTextBuffer *textbuf;
1097 GtkTextIter iter, start_iter, end_iter;
1099 gtk_widget_show(children->scrolledwin);
1100 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
1101 gtk_text_buffer_get_start_iter(textbuf, &start_iter);
1102 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
1103 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
1104 gtk_text_buffer_get_start_iter(textbuf, &start_iter);
1106 for (cur = children->list; cur; cur = cur->next) {
1107 child_info = (ChildInfo *)cur->data;
1108 if (child_info->pid)
1109 caption = g_strdup_printf
1110 (_("--- Running: %s\n"),
1113 caption = g_strdup_printf
1114 (_("--- Ended: %s\n"),
1117 gtk_text_buffer_insert(textbuf, &iter,
1119 gtk_text_buffer_insert(textbuf, &iter,
1120 child_info->output->str, -1);
1122 child_info->new_out = FALSE;
1127 static void create_io_dialog(Children *children)
1131 GtkWidget *entry = NULL;
1132 GtkWidget *input_hbox = NULL;
1133 GtkWidget *send_button;
1136 GtkWidget *scrolledwin;
1138 GtkWidget *progress_bar = NULL;
1139 GtkWidget *abort_button;
1140 GtkWidget *close_button;
1141 PangoFontDescription *text_font;
1143 debug_print("Creating action IO dialog\n");
1145 dialog = gtk_dialog_new();
1146 gtk_container_set_border_width
1147 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1148 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1149 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1150 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1151 manage_window_set_transient(GTK_WINDOW(dialog));
1152 g_signal_connect(G_OBJECT(dialog), "delete_event",
1153 G_CALLBACK(delete_io_dialog_cb), children);
1154 g_signal_connect(G_OBJECT(dialog), "destroy",
1155 G_CALLBACK(hide_io_dialog_cb),
1158 vbox = gtk_vbox_new(FALSE, 8);
1159 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1160 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1161 gtk_widget_show(vbox);
1163 label = gtk_label_new(children->action);
1164 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1165 gtk_widget_show(label);
1167 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1168 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1169 GTK_POLICY_AUTOMATIC,
1170 GTK_POLICY_AUTOMATIC);
1171 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1172 gtk_widget_set_size_request(scrolledwin, 480, 200);
1173 gtk_widget_hide(scrolledwin);
1175 text = gtk_text_view_new();
1176 if (prefs_common.textfont) {
1177 if (NULL != (text_font = pango_font_description_from_string
1178 (prefs_common.textfont))) {
1179 gtk_widget_modify_font(text, text_font);
1180 pango_font_description_free(text_font);
1183 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
1184 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1185 gtk_widget_show(text);
1187 if (children->open_in) {
1188 input_hbox = gtk_hbox_new(FALSE, 8);
1189 gtk_widget_show(input_hbox);
1191 entry = gtk_entry_new();
1192 gtk_widget_set_size_request(entry, 320, -1);
1193 g_signal_connect(G_OBJECT(entry), "activate",
1194 G_CALLBACK(send_input), children);
1195 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1196 if (children->action_type & ACTION_USER_HIDDEN_IN)
1197 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1198 gtk_widget_show(entry);
1200 send_button = gtk_button_new_with_label(_(" Send "));
1201 g_signal_connect(G_OBJECT(send_button), "clicked",
1202 G_CALLBACK(send_input), children);
1203 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1205 gtk_widget_show(send_button);
1207 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1208 gtk_widget_grab_focus(entry);
1211 if (children->initial_nb > 1) {
1214 progress_bar = gtk_progress_bar_new();
1215 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1216 GTK_PROGRESS_LEFT_TO_RIGHT);
1217 text = g_strdup_printf("%s 0/%d\n", _("Completed"),
1218 children->initial_nb);
1219 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar),
1222 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1223 gtk_widget_show(progress_bar);
1226 gtkut_button_set_create(&hbox, &abort_button, _("Abort"),
1227 &close_button, _("Close"), NULL, NULL);
1228 g_signal_connect(G_OBJECT(abort_button), "clicked",
1229 G_CALLBACK(kill_children_cb), children);
1230 g_signal_connect(G_OBJECT(close_button), "clicked",
1231 G_CALLBACK(hide_io_dialog_cb), children);
1232 gtk_widget_show(hbox);
1235 gtk_widget_set_sensitive(close_button, FALSE);
1237 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1239 children->dialog = dialog;
1240 children->scrolledwin = scrolledwin;
1241 children->text = text;
1242 children->input_hbox = children->open_in ? input_hbox : NULL;
1243 children->input_entry = children->open_in ? entry : NULL;
1244 children->progress_bar = progress_bar;
1245 children->abort_btn = abort_button;
1246 children->close_btn = close_button;
1248 gtk_widget_show(dialog);
1251 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1253 ChildInfo *child_info = (ChildInfo *)data;
1257 gdk_input_remove(child_info->tag_status);
1259 c = read(source, &buf, 1);
1260 debug_print("Child returned %c\n", buf);
1262 waitpid(-child_info->pid, NULL, 0);
1263 childinfo_close_pipes(child_info);
1264 child_info->pid = 0;
1266 wait_for_children(child_info->children);
1269 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1271 Children *children = (Children *)data;
1272 ChildInfo *child_info = (ChildInfo *)children->list->data;
1276 debug_print("Sending input to grand child.\n");
1277 if (!(cond && GDK_INPUT_WRITE))
1280 gdk_input_remove(child_info->tag_in);
1281 child_info->tag_in = -1;
1283 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1285 len = strlen(input);
1289 c = write(child_info->chld_in, input + count, len - count);
1292 } while (c >= 0 && count < len);
1295 write(child_info->chld_in, "\n", 2);
1299 gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1300 gtk_widget_set_sensitive(children->input_hbox, TRUE);
1301 close(child_info->chld_in);
1302 child_info->chld_in = -1;
1303 debug_print("Input to grand child sent.\n");
1306 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1308 ChildInfo *child_info = (ChildInfo *)data;
1310 gchar buf[BUFFSIZE];
1312 debug_print("Catching grand child's output.\n");
1313 if (child_info->children->action_type &
1314 (ACTION_PIPE_OUT | ACTION_INSERT)
1315 && source == child_info->chld_out) {
1316 GtkTextView *text = GTK_TEXT_VIEW(child_info->children->msg_text);
1317 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
1318 GtkTextIter iter1, iter2;
1321 mark = gtk_text_buffer_get_insert(textbuf);
1322 gtk_text_buffer_get_iter_at_mark(textbuf, &iter1, mark);
1323 gtk_text_buffer_get_iter_at_mark(textbuf, &iter2, mark);
1326 c = read(source, buf, sizeof(buf) - 1);
1330 gssize by_read = 0, by_written = 0;
1331 gchar *ret_str = g_locale_to_utf8(buf, c, &by_read,
1334 if (ret_str && by_written) {
1335 gtk_text_buffer_insert(textbuf, &iter2,
1336 ret_str, by_written);
1339 gtk_text_buffer_insert(textbuf, &iter2, buf, c);
1342 if (child_info->children->is_selection) {
1343 gtk_text_buffer_place_cursor(textbuf, &iter1);
1344 gtk_text_buffer_move_mark_by_name
1345 (textbuf, "selection_bound", &iter2);
1348 c = read(source, buf, sizeof(buf) - 1);
1351 gssize by_read = 0, by_written = 0;
1352 gchar *ret_str = g_locale_to_utf8(buf, c, &by_read,
1355 if (ret_str && by_written) {
1356 g_string_append_len(child_info->output,
1357 ret_str, by_written);
1360 g_string_append_len(child_info->output, buf, c);
1364 child_info->new_out = TRUE;
1367 if (source == child_info->chld_out) {
1368 gdk_input_remove(child_info->tag_out);
1369 child_info->tag_out = -1;
1370 close(child_info->chld_out);
1371 child_info->chld_out = -1;
1373 gdk_input_remove(child_info->tag_err);
1374 child_info->tag_err = -1;
1375 close(child_info->chld_err);
1376 child_info->chld_err = -1;
1380 wait_for_children(child_info->children);
1383 static gchar *get_user_string(const gchar *action, ActionType type)
1386 gchar *user_str = NULL;
1389 case ACTION_USER_HIDDEN_STR:
1390 message = g_strdup_printf
1391 (_("Enter the argument for the following action:\n"
1392 "(`%%h' will be replaced with the argument)\n"
1395 user_str = input_dialog_with_invisible
1396 (_("Action's hidden user argument"), message, NULL);
1398 case ACTION_USER_STR:
1399 message = g_strdup_printf
1400 (_("Enter the argument for the following action:\n"
1401 "(`%%u' will be replaced with the argument)\n"
1404 user_str = input_dialog
1405 (_("Action's user argument"), message, NULL);
1408 g_warning("Unsupported action type %d", type);