2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2004 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>
34 #include <sys/types.h>
42 #include "manage_window.h"
43 #include "mainwindow.h"
44 #include "prefs_common.h"
45 #include "alertpanel.h"
46 #include "inputdialog.h"
52 #include "matcher_parser.h" /* CLAWS */
53 #include "filtering.h"
55 typedef struct _Children Children;
56 typedef struct _ChildInfo ChildInfo;
57 typedef struct _UserStringDialog UserStringDialog;
63 GtkWidget *input_entry;
64 GtkWidget *input_hbox;
65 GtkWidget *progress_bar;
68 GtkWidget *scrolledwin;
71 ActionType action_type;
100 static void action_update_menu (GtkItemFactory *ifactory,
104 static void compose_actions_execute_cb (Compose *compose,
107 static void mainwin_actions_execute_cb (MainWindow *mainwin,
110 static void msgview_actions_execute_cb (MessageView *msgview,
113 static void message_actions_execute (MessageView *msgview,
117 static gboolean execute_filtering_actions(gchar *action,
120 static gboolean execute_actions (gchar *action,
127 static gchar *parse_action_cmd (gchar *action,
131 const gchar *user_str,
132 const gchar *user_hidden_str,
133 const gchar *sel_str);
134 static gboolean parse_append_filename (GString *cmd,
137 static gboolean parse_append_msgpart (GString *cmd,
141 static ChildInfo *fork_child (gchar *cmd,
142 const gchar *msg_str,
145 static gint wait_for_children (Children *children);
147 static void free_children (Children *children);
149 static void childinfo_close_pipes (ChildInfo *child_info);
151 static void create_io_dialog (Children *children);
152 static void update_io_dialog (Children *children);
154 static void hide_io_dialog_cb (GtkWidget *widget,
156 static gint io_dialog_key_pressed_cb (GtkWidget *widget,
160 static void catch_output (gpointer data,
162 GdkInputCondition cond);
163 static void catch_input (gpointer data,
165 GdkInputCondition cond);
166 static void catch_status (gpointer data,
168 GdkInputCondition cond);
170 static gchar *get_user_string (const gchar *action,
174 ActionType action_get_type(const gchar *action_str)
177 ActionType action_type = ACTION_NONE;
179 g_return_val_if_fail(action_str, ACTION_ERROR);
180 g_return_val_if_fail(*action_str, ACTION_ERROR);
185 action_type |= ACTION_PIPE_IN;
187 } else if (p[0] == '>') {
188 action_type |= ACTION_USER_IN;
190 } else if (p[0] == '*') {
191 action_type |= ACTION_USER_HIDDEN_IN;
198 while (*p && action_type != ACTION_ERROR) {
199 if (p[0] == '%' && p[1]) {
202 /* CLAWS: filtering action is a mutually exclusive
203 * action. we can enable others if needed later. we
204 * add ACTION_SINGLE | ACTION_MULTIPLE so it will
205 * only be executed from the main window toolbar */
206 if (p[2] == 's') /* source messages */
207 action_type = ACTION_FILTERING_ACTION
212 action_type |= ACTION_SINGLE;
215 action_type |= ACTION_MULTIPLE;
218 action_type |= ACTION_SINGLE;
221 action_type |= ACTION_SELECTION_STR;
224 action_type |= ACTION_USER_STR;
227 action_type |= ACTION_USER_HIDDEN_STR;
230 action_type = ACTION_ERROR;
233 } else if (p[0] == '|') {
235 action_type |= ACTION_PIPE_OUT;
236 } else if (p[0] == '>') {
238 action_type |= ACTION_INSERT;
239 } else if (p[0] == '&') {
241 action_type |= ACTION_ASYNC;
249 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
250 GSList *msg_list, MimeInfo *partinfo,
251 const gchar *user_str,
252 const gchar *user_hidden_str,
253 const gchar *sel_str)
261 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
264 cmd = g_string_sized_new(strlen(action));
267 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
268 if (p[0] == '%' && p[1]) {
271 if (!parse_append_filename(cmd, msginfo)) {
272 g_string_free(cmd, TRUE);
278 for (cur = msg_list; cur != NULL;
280 MsgInfo *msg = (MsgInfo *)cur->data;
282 if (!parse_append_filename(cmd, msg)) {
283 g_string_free(cmd, TRUE);
287 g_string_append_c(cmd, ' ');
292 if (!parse_append_msgpart(cmd, msginfo,
294 g_string_free(cmd, TRUE);
301 g_string_append(cmd, sel_str);
306 g_string_append(cmd, user_str);
311 g_string_append(cmd, user_hidden_str);
315 g_string_append_c(cmd, p[0]);
316 g_string_append_c(cmd, p[1]);
320 g_string_append_c(cmd, p[0]);
325 g_string_free(cmd, TRUE);
330 g_string_free(cmd, FALSE);
334 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
338 gchar escape_ch[] = "\\ ";
340 g_return_val_if_fail(msginfo, FALSE);
342 filename = procmsg_get_message_file(msginfo);
345 alertpanel_error(_("Could not get message file %d"),
351 while ((q = strpbrk(p, "$\"`'\\ \t*?[]&|;<>()!#~")) != NULL) {
354 g_string_append(cmd, p);
355 g_string_append(cmd, escape_ch);
358 g_string_append(cmd, p);
365 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
368 gboolean single_part = FALSE;
370 gchar *part_filename;
374 partinfo = procmime_scan_message(msginfo);
376 alertpanel_error(_("Could not get message part."));
383 filename = procmsg_get_message_file_path(msginfo);
384 part_filename = procmime_get_tmp_file_name(partinfo);
386 ret = procmime_get_part(part_filename, partinfo);
389 procmime_mimeinfo_free_all(partinfo);
393 alertpanel_error(_("Can't get part of multipart message"));
394 g_free(part_filename);
398 g_string_append(cmd, part_filename);
400 g_free(part_filename);
405 void actions_execute(gpointer data,
410 if (source == TOOLBAR_MAIN)
411 mainwin_actions_execute_cb((MainWindow*)data, action_nb, widget);
412 else if (source == TOOLBAR_COMPOSE)
413 compose_actions_execute_cb((Compose*)data, action_nb, widget);
414 else if (source == TOOLBAR_MSGVIEW)
415 msgview_actions_execute_cb((MessageView*)data, action_nb, widget);
418 void action_update_mainwin_menu(GtkItemFactory *ifactory,
422 action_update_menu(ifactory, branch_path,
423 mainwin_actions_execute_cb, mainwin);
426 void action_update_msgview_menu(GtkItemFactory *ifactory,
428 MessageView *msgview)
430 action_update_menu(ifactory, branch_path,
431 msgview_actions_execute_cb, msgview);
434 void action_update_compose_menu(GtkItemFactory *ifactory,
438 action_update_menu(ifactory, branch_path,
439 compose_actions_execute_cb, compose);
442 static void action_update_menu(GtkItemFactory *ifactory,
444 gpointer callback, gpointer data)
449 gchar *action, *action_p;
451 GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
453 ifentry.path = branch_path;
454 menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
455 g_return_if_fail(menuitem != NULL);
457 amenu = GTK_MENU_SHELL(menuitem)->children;
458 while (amenu != NULL) {
459 GList *alist = amenu->next;
460 gtk_widget_destroy(GTK_WIDGET(amenu->data));
464 ifentry.accelerator = NULL;
465 ifentry.callback_action = 0;
466 ifentry.callback = callback;
467 ifentry.item_type = NULL;
469 for (cur = prefs_common.actions_list; cur; cur = cur->next) {
470 action = g_strdup((gchar *)cur->data);
471 action_p = strstr(action, ": ");
472 if (action_p && action_p[2] &&
473 action_get_type(&action_p[2]) != ACTION_ERROR) {
475 menu_path = g_strdup_printf("%s/%s", branch_path,
477 ifentry.path = menu_path;
478 gtk_item_factory_create_item(ifactory, &ifentry, data,
483 ifentry.callback_action++;
487 static void compose_actions_execute_cb(Compose *compose, guint action_nb,
491 ActionType action_type;
493 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
495 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
496 g_return_if_fail(buf != NULL);
497 action = strstr(buf, ": ");
498 g_return_if_fail(action != NULL);
500 /* Point to the beginning of the command-line */
503 action_type = action_get_type(action);
504 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
506 (_("The selected action cannot be used in the compose window\n"
507 "because it contains %%f, %%F, %%as or %%p."));
511 execute_actions(action, NULL, compose->text, NULL, 0, NULL);
514 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
519 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
520 message_actions_execute(mainwin->messageview, action_nb, msg_list);
521 g_slist_free(msg_list);
524 static void msgview_actions_execute_cb(MessageView *msgview, guint action_nb,
527 GSList *msg_list = NULL;
529 if (msgview->msginfo)
530 msg_list = g_slist_append(msg_list, msgview->msginfo);
531 message_actions_execute(msgview, action_nb, msg_list);
532 g_slist_free(msg_list);
535 static void message_actions_execute(MessageView *msgview, guint action_nb,
542 GtkWidget *text = NULL;
543 GdkFont *msgfont = NULL;
545 ActionType action_type;
547 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
549 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
551 g_return_if_fail(buf);
552 g_return_if_fail(action = strstr(buf, ": "));
554 /* Point to the beginning of the command-line */
557 textview = messageview_get_current_textview(msgview);
559 text = textview->text;
560 msgfont = textview->msgfont;
561 body_pos = textview->body_pos;
563 partinfo = messageview_get_selected_mime_part(msgview);
565 /* this command will alter the message text */
566 action_type = action_get_type(action);
567 if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
568 msgview->filtered = TRUE;
570 if (action_type & ACTION_FILTERING_ACTION)
571 /* CLAWS: most of the above code is not necessary for applying
573 execute_filtering_actions(action, msg_list);
575 execute_actions(action, msg_list, text, msgfont, body_pos, partinfo);
578 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
580 GSList *action_list, *p;
581 const gchar *sbegin, *send;
582 gchar *action_string;
584 if (NULL == (sbegin = strstr2(action, "%as{")))
586 sbegin += sizeof "%as{" - 1;
587 if (NULL == (send = strrchr(sbegin, '}')))
589 action_string = g_strndup(sbegin, send - sbegin);
591 action_list = matcher_parser_get_action_list(action_string);
592 g_free(action_string);
593 if (action_list == NULL) return FALSE;
595 /* apply actions on each message info */
596 for (p = msglist; p && p->data; p = g_slist_next(p))
597 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
599 for (p = action_list; p; p = g_slist_next(p))
600 if (p->data) filteringaction_free(p->data);
601 g_slist_free(action_list);
605 static gboolean execute_actions(gchar *action, GSList *msg_list,
606 GtkWidget *text, GdkFont *msgfont,
607 gint body_pos, MimeInfo *partinfo)
609 GSList *children_list = NULL;
613 ChildInfo *child_info;
614 ActionType action_type;
617 guint start = 0, end = 0;
618 gchar *sel_str = NULL;
619 gchar *msg_str = NULL;
620 gchar *user_str = NULL;
621 gchar *user_hidden_str = NULL;
623 g_return_val_if_fail(action && *action, FALSE);
625 action_type = action_get_type(action);
627 if (action_type == ACTION_ERROR)
628 return FALSE; /* ERR: syntax error */
630 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
631 return FALSE; /* ERR: file command without selection */
633 msg_list_len = g_slist_length(msg_list);
635 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
636 if (msg_list_len > 1)
637 return FALSE; /* ERR: pipe + multiple selection */
639 return FALSE; /* ERR: pipe and no displayed text */
642 if (action_type & ACTION_SELECTION_STR) {
644 return FALSE; /* ERR: selection string but no text */
648 if (GTK_EDITABLE(text)->has_selection) {
649 start = GTK_EDITABLE(text)->selection_start_pos;
650 end = GTK_EDITABLE(text)->selection_end_pos;
660 end = gtk_stext_get_length(GTK_STEXT(text));
661 msg_str = gtk_editable_get_chars
662 (GTK_EDITABLE(text), start, end);
664 sel_str = gtk_editable_get_chars
665 (GTK_EDITABLE(text), start, end);
666 msg_str = g_strdup(sel_str);
670 end = gtk_stext_get_length(GTK_STEXT(text));
671 msg_str = gtk_editable_get_chars(GTK_EDITABLE(text),
676 if (action_type & ACTION_USER_STR) {
677 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
684 if (action_type & ACTION_USER_HIDDEN_STR) {
685 if (!(user_hidden_str =
686 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
694 if (action_type & ACTION_PIPE_OUT) {
695 gtk_stext_freeze(GTK_STEXT(text));
696 gtk_stext_set_point(GTK_STEXT(text), start);
697 gtk_stext_forward_delete(GTK_STEXT(text), end - start);
698 gtk_stext_thaw(GTK_STEXT(text));
701 children = g_new0(Children, 1);
703 children->action = g_strdup(action);
704 children->action_type = action_type;
705 children->msg_text = text;
706 children->msgfont = msgfont;
708 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
709 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
710 children->open_in = 1;
712 if (action_type & ACTION_SINGLE) {
715 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
716 msginfo = (MsgInfo *)cur->data;
718 is_ok = FALSE; /* ERR: msginfo missing */
721 cmd = parse_action_cmd(action, msginfo, msg_list,
723 user_hidden_str, sel_str);
725 debug_print("Action command error\n");
726 is_ok = FALSE; /* ERR: incorrect command */
729 if ((child_info = fork_child(cmd, msg_str, children))) {
730 children_list = g_slist_append(children_list,
736 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
737 user_str, user_hidden_str, sel_str);
739 if ((child_info = fork_child(cmd, msg_str, children))) {
740 children_list = g_slist_append(children_list,
745 is_ok = FALSE; /* ERR: incorrect command */
751 g_free(user_hidden_str);
753 if (!children_list) {
754 /* If not waiting for children, return */
755 free_children(children);
759 children->list = children_list;
760 children->nb = g_slist_length(children_list);
761 children->initial_nb = children->nb;
763 for (cur = children_list; cur; cur = cur->next) {
764 child_info = (ChildInfo *) cur->data;
765 child_info->tag_status =
766 gdk_input_add(child_info->chld_status,
768 catch_status, child_info);
771 create_io_dialog(children);
777 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
780 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
783 ChildInfo *child_info;
786 sync = !(children->action_type & ACTION_ASYNC);
788 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
789 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
792 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
794 alertpanel_error(_("Command could not be started. "
795 "Pipe creation failed.\n%s"),
797 /* Closing fd = -1 fails silently */
804 close(chld_status[0]);
805 close(chld_status[1]);
806 return NULL; /* Pipe error */
810 debug_print("Forking child and grandchild.\n");
811 debug_print("Executing: /bin/sh -c %s\n", cmd);
814 if (pid == 0) { /* Child */
818 close(ConnectionNumber(gdk_display));
823 if (setpgid(0, getppid()))
827 if (children->action_type &
830 ACTION_USER_HIDDEN_IN)) {
831 close(fileno(stdin));
837 close(fileno(stdout));
842 close(fileno(stderr));
852 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 write(chld_in[1], msg_str, strlen(msg_str));
928 if (!(children->action_type &
929 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
931 child_info->chld_in = -1; /* No more input */
937 static void kill_children_cb(GtkWidget *widget, gpointer data)
940 Children *children = (Children *) data;
941 ChildInfo *child_info;
943 for (cur = children->list; cur; cur = cur->next) {
944 child_info = (ChildInfo *)(cur->data);
945 debug_print("Killing child group id %d\n", child_info->pid);
946 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
951 static gint wait_for_children(Children *children)
954 ChildInfo *child_info;
956 gint nb = children->nb;
960 cur = children->list;
963 child_info = (ChildInfo *)cur->data;
966 new_output |= child_info->new_out;
970 children->output |= new_output;
972 if (new_output || (children->dialog && (nb != children->nb)))
973 update_io_dialog(children);
978 if (!children->dialog) {
979 free_children(children);
980 } else if (!children->output) {
981 gtk_widget_destroy(children->dialog);
987 static void send_input(GtkWidget *w, gpointer data)
989 Children *children = (Children *) data;
990 ChildInfo *child_info = (ChildInfo *) children->list->data;
992 child_info->tag_in = gdk_input_add(child_info->chld_in,
994 catch_input, children);
995 gtk_widget_set_sensitive(children->input_hbox, FALSE);
998 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1000 hide_io_dialog_cb(w, data);
1004 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1007 Children *children = (Children *)data;
1009 if (!children->nb) {
1010 gtk_signal_disconnect_by_data(GTK_OBJECT(children->dialog),
1012 gtk_widget_destroy(children->dialog);
1013 free_children(children);
1017 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1020 if (event && event->keyval == GDK_Escape)
1021 hide_io_dialog_cb(widget, data);
1025 static void childinfo_close_pipes(ChildInfo *child_info)
1027 /* stdout and stderr pipes are guaranteed to be removed by
1028 * their handler, but in case where we receive child exit notification
1029 * before grand-child's pipes closing signals, we check them and close
1032 if (child_info->tag_in > 0)
1033 gdk_input_remove(child_info->tag_in);
1034 if (child_info->tag_out > 0)
1035 gdk_input_remove(child_info->tag_out);
1036 if (child_info->tag_err > 0)
1037 gdk_input_remove(child_info->tag_err);
1039 if (child_info->chld_in >= 0)
1040 close(child_info->chld_in);
1041 if (child_info->chld_out >= 0)
1042 close(child_info->chld_out);
1043 if (child_info->chld_err >= 0)
1044 close(child_info->chld_err);
1046 close(child_info->chld_status);
1049 static void free_children(Children *children)
1051 ChildInfo *child_info;
1053 debug_print("Freeing children data %p\n", children);
1055 g_free(children->action);
1056 while (children->list != NULL) {
1057 child_info = (ChildInfo *)children->list->data;
1058 g_free(child_info->cmd);
1059 g_string_free(child_info->output, TRUE);
1060 children->list = g_slist_remove(children->list, child_info);
1066 static void update_io_dialog(Children *children)
1070 debug_print("Updating actions input/output dialog.\n");
1072 if (children->progress_bar) {
1073 gtk_progress_configure(GTK_PROGRESS(children->progress_bar),
1074 children->initial_nb -children->nb,
1075 0.0, children->initial_nb);
1078 if (!children->nb) {
1079 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1080 gtk_widget_set_sensitive(children->close_btn, TRUE);
1081 if (children->input_hbox)
1082 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1083 gtk_widget_grab_focus(children->close_btn);
1084 gtk_signal_connect(GTK_OBJECT(children->dialog),
1086 GTK_SIGNAL_FUNC(io_dialog_key_pressed_cb),
1090 if (children->output) {
1091 GtkWidget *text = children->text;
1093 ChildInfo *child_info;
1096 font = gtk_object_get_data(GTK_OBJECT(children->dialog),
1098 gtk_widget_show(children->scrolledwin);
1099 gtk_text_freeze(GTK_TEXT(text));
1100 gtk_text_set_point(GTK_TEXT(text), 0);
1101 gtk_text_forward_delete(GTK_TEXT(text),
1102 gtk_text_get_length(GTK_TEXT(text)));
1103 for (cur = children->list; cur; cur = cur->next) {
1104 child_info = (ChildInfo *)cur->data;
1105 if (child_info->pid)
1106 caption = g_strdup_printf
1107 (_("--- Running: %s\n"),
1110 caption = g_strdup_printf
1111 (_("--- Ended: %s\n"),
1114 gtk_text_insert(GTK_TEXT(text), font, NULL, NULL,
1116 gtk_text_insert(GTK_TEXT(text), font, NULL, NULL,
1117 child_info->output->str, -1);
1119 child_info->new_out = FALSE;
1121 gtk_text_thaw(GTK_TEXT(text));
1125 static void create_io_dialog(Children *children)
1129 GtkWidget *entry = NULL;
1130 GtkWidget *input_hbox = NULL;
1131 GtkWidget *send_button;
1134 GtkWidget *scrolledwin;
1136 GtkWidget *progress_bar = NULL;
1137 GtkWidget *abort_button;
1138 GtkWidget *close_button;
1139 GdkFont *output_font;
1141 debug_print("Creating action IO dialog\n");
1143 dialog = gtk_dialog_new();
1144 gtk_container_set_border_width
1145 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1146 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1147 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1148 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1149 manage_window_set_transient(GTK_WINDOW(dialog));
1150 gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
1151 GTK_SIGNAL_FUNC(delete_io_dialog_cb), children);
1152 gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
1153 GTK_SIGNAL_FUNC(hide_io_dialog_cb),
1156 vbox = gtk_vbox_new(FALSE, 8);
1157 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1158 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1159 gtk_widget_show(vbox);
1161 label = gtk_label_new(children->action);
1162 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1163 gtk_widget_show(label);
1165 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1166 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1167 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
1168 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1169 gtk_widget_set_usize(scrolledwin, 480, 200);
1170 gtk_widget_hide(scrolledwin);
1172 text = gtk_text_new(gtk_scrolled_window_get_hadjustment
1173 (GTK_SCROLLED_WINDOW(scrolledwin)),
1174 gtk_scrolled_window_get_vadjustment
1175 (GTK_SCROLLED_WINDOW(scrolledwin)));
1176 gtk_text_set_editable(GTK_TEXT(text), FALSE);
1177 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1178 gtk_widget_show(text);
1180 if (children->open_in) {
1181 input_hbox = gtk_hbox_new(FALSE, 8);
1182 gtk_widget_show(input_hbox);
1184 entry = gtk_entry_new();
1185 gtk_widget_set_usize(entry, 320, -1);
1186 gtk_signal_connect(GTK_OBJECT(entry), "activate",
1187 GTK_SIGNAL_FUNC(send_input), children);
1188 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1189 if (children->action_type & ACTION_USER_HIDDEN_IN)
1190 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1191 gtk_widget_show(entry);
1193 send_button = gtk_button_new_with_label(_(" Send "));
1194 gtk_signal_connect(GTK_OBJECT(send_button), "clicked",
1195 GTK_SIGNAL_FUNC(send_input), children);
1196 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1198 gtk_widget_show(send_button);
1200 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1201 gtk_widget_grab_focus(entry);
1204 if (children->initial_nb > 1) {
1205 progress_bar = gtk_progress_bar_new();
1206 gtk_progress_bar_set_bar_style(GTK_PROGRESS_BAR(progress_bar),
1207 GTK_PROGRESS_CONTINUOUS);
1208 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1209 GTK_PROGRESS_LEFT_TO_RIGHT);
1210 gtk_progress_set_activity_mode(GTK_PROGRESS(progress_bar),
1212 gtk_progress_set_format_string(GTK_PROGRESS(progress_bar),
1213 _("Completed %v/%u"));
1214 gtk_progress_set_show_text(GTK_PROGRESS(progress_bar), TRUE);
1215 gtk_progress_configure(GTK_PROGRESS(progress_bar),
1216 children->initial_nb -children->nb,
1217 0.0, children->initial_nb);
1219 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1220 gtk_widget_show(progress_bar);
1223 gtkut_button_set_create(&hbox, &abort_button, _("Abort"),
1224 &close_button, _("Close"), NULL, NULL);
1225 gtk_signal_connect(GTK_OBJECT(abort_button), "clicked",
1226 GTK_SIGNAL_FUNC(kill_children_cb), children);
1227 gtk_signal_connect(GTK_OBJECT(close_button), "clicked",
1228 GTK_SIGNAL_FUNC(hide_io_dialog_cb), children);
1229 gtk_widget_show(hbox);
1232 gtk_widget_set_sensitive(close_button, FALSE);
1234 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1236 children->dialog = dialog;
1237 children->scrolledwin = scrolledwin;
1238 children->text = text;
1239 children->input_hbox = children->open_in ? input_hbox : NULL;
1240 children->input_entry = children->open_in ? entry : NULL;
1241 children->progress_bar = progress_bar;
1242 children->abort_btn = abort_button;
1243 children->close_btn = close_button;
1245 output_font = gtkut_font_load_from_fontset(prefs_common.textfont);
1246 gtk_object_set_data_full(GTK_OBJECT(dialog), "s_txtfont",
1248 (GtkDestroyNotify)gdk_font_unref);
1249 gtk_widget_show(dialog);
1252 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1254 ChildInfo *child_info = (ChildInfo *)data;
1258 gdk_input_remove(child_info->tag_status);
1260 c = read(source, &buf, 1);
1261 debug_print("Child returned %c\n", buf);
1263 waitpid(-child_info->pid, NULL, 0);
1264 childinfo_close_pipes(child_info);
1265 child_info->pid = 0;
1267 wait_for_children(child_info->children);
1270 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1272 Children *children = (Children *)data;
1273 ChildInfo *child_info = (ChildInfo *)children->list->data;
1277 debug_print("Sending input to grand child.\n");
1278 if (!(cond && GDK_INPUT_WRITE))
1281 gdk_input_remove(child_info->tag_in);
1282 child_info->tag_in = -1;
1284 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1286 len = strlen(input);
1290 c = write(child_info->chld_in, input + count, len - count);
1293 } while (c >= 0 && count < len);
1296 write(child_info->chld_in, "\n", 2);
1300 gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1301 gtk_widget_set_sensitive(children->input_hbox, TRUE);
1302 close(child_info->chld_in);
1303 child_info->chld_in = -1;
1304 debug_print("Input to grand child sent.\n");
1307 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1309 ChildInfo *child_info = (ChildInfo *)data;
1311 gchar buf[BUFFSIZE];
1313 debug_print("Catching grand child's output.\n");
1314 if (child_info->children->action_type &
1315 (ACTION_PIPE_OUT | ACTION_INSERT)
1316 && source == child_info->chld_out) {
1317 gboolean is_selection = FALSE;
1318 GtkWidget *text = child_info->children->msg_text;
1320 if (GTK_EDITABLE(text)->has_selection)
1321 is_selection = TRUE;
1322 gtk_stext_freeze(GTK_STEXT(text));
1324 c = read(source, buf, sizeof(buf) - 1);
1327 gtk_stext_insert(GTK_STEXT(text),
1328 child_info->children->msgfont,
1329 NULL, NULL, buf, c);
1332 /* Using the select_region draws things. Should not.
1333 * so we just change selection position and
1334 * defere drawing when thawing. Hack?
1336 GTK_EDITABLE(text)->selection_end_pos =
1337 gtk_stext_get_point(GTK_STEXT(text));
1339 gtk_stext_thaw(GTK_STEXT(text));
1341 c = read(source, buf, sizeof(buf) - 1);
1342 for (i = 0; i < c; i++)
1343 g_string_append_c(child_info->output, buf[i]);
1345 child_info->new_out = TRUE;
1348 if (source == child_info->chld_out) {
1349 gdk_input_remove(child_info->tag_out);
1350 child_info->tag_out = -1;
1351 close(child_info->chld_out);
1352 child_info->chld_out = -1;
1354 gdk_input_remove(child_info->tag_err);
1355 child_info->tag_err = -1;
1356 close(child_info->chld_err);
1357 child_info->chld_err = -1;
1361 wait_for_children(child_info->children);
1364 static gchar *get_user_string(const gchar *action, ActionType type)
1367 gchar *user_str = NULL;
1370 case ACTION_USER_HIDDEN_STR:
1371 message = g_strdup_printf
1372 (_("Enter the argument for the following action:\n"
1373 "(`%%h' will be replaced with the argument)\n"
1376 user_str = input_dialog_with_invisible
1377 (_("Action's hidden user argument"), message, NULL);
1379 case ACTION_USER_STR:
1380 message = g_strdup_printf
1381 (_("Enter the argument for the following action:\n"
1382 "(`%%u' will be replaced with the argument)\n"
1385 user_str = input_dialog
1386 (_("Action's user argument"), message, NULL);
1389 g_warning("Unsupported action type %d", type);