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>
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 g_return_val_if_fail(msginfo, FALSE);
340 filename = procmsg_get_message_file(msginfo);
343 g_string_append(cmd, filename);
346 alertpanel_error(_("Could not get message file %d"),
354 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
357 gboolean single_part = FALSE;
359 gchar *part_filename;
363 partinfo = procmime_scan_message(msginfo);
365 alertpanel_error(_("Could not get message part."));
372 filename = procmsg_get_message_file_path(msginfo);
373 part_filename = procmime_get_tmp_file_name(partinfo);
375 ret = procmime_get_part(part_filename, partinfo);
378 procmime_mimeinfo_free_all(partinfo);
382 alertpanel_error(_("Can't get part of multipart message"));
383 g_free(part_filename);
387 g_string_append(cmd, part_filename);
389 g_free(part_filename);
394 void actions_execute(gpointer data,
399 if (source == TOOLBAR_MAIN)
400 mainwin_actions_execute_cb((MainWindow*)data, action_nb, widget);
401 else if (source == TOOLBAR_COMPOSE)
402 compose_actions_execute_cb((Compose*)data, action_nb, widget);
403 else if (source == TOOLBAR_MSGVIEW)
404 msgview_actions_execute_cb((MessageView*)data, action_nb, widget);
407 void action_update_mainwin_menu(GtkItemFactory *ifactory, MainWindow *mainwin)
409 action_update_menu(ifactory, "/Tools/Actions",
410 mainwin_actions_execute_cb, mainwin);
413 void action_update_msgview_menu(GtkItemFactory *ifactory, MessageView *msgview)
415 action_update_menu(ifactory, "/Tools/Actions",
416 msgview_actions_execute_cb, msgview);
419 void action_update_compose_menu(GtkItemFactory *ifactory, Compose *compose)
421 action_update_menu(ifactory, "/Tools/Actions",
422 compose_actions_execute_cb, compose);
425 static void action_update_menu(GtkItemFactory *ifactory, gchar *branch_path,
426 gpointer callback, gpointer data)
431 gchar *action, *action_p;
433 GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
435 ifentry.path = branch_path;
436 menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
437 g_return_if_fail(menuitem != NULL);
439 amenu = GTK_MENU_SHELL(menuitem)->children;
440 while (amenu != NULL) {
441 GList *alist = amenu->next;
442 gtk_widget_destroy(GTK_WIDGET(amenu->data));
446 ifentry.accelerator = NULL;
447 ifentry.callback_action = 0;
448 ifentry.callback = callback;
449 ifentry.item_type = NULL;
451 for (cur = prefs_common.actions_list; cur; cur = cur->next) {
452 action = g_strdup((gchar *)cur->data);
453 action_p = strstr(action, ": ");
454 if (action_p && action_p[2] &&
455 action_get_type(&action_p[2]) != ACTION_ERROR) {
457 menu_path = g_strdup_printf("%s/%s", branch_path,
459 ifentry.path = menu_path;
460 gtk_item_factory_create_item(ifactory, &ifentry, data,
465 ifentry.callback_action++;
469 static void compose_actions_execute_cb(Compose *compose, guint action_nb,
473 ActionType action_type;
475 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
477 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
478 g_return_if_fail(buf != NULL);
479 action = strstr(buf, ": ");
480 g_return_if_fail(action != NULL);
482 /* Point to the beginning of the command-line */
485 action_type = action_get_type(action);
486 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
488 (_("The selected action cannot be used in the compose window\n"
489 "because it contains %%f, %%F, %%as or %%p."));
493 execute_actions(action, NULL, compose->text, NULL, 0, NULL);
496 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
501 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
502 message_actions_execute(mainwin->messageview, action_nb, msg_list);
503 g_slist_free(msg_list);
506 static void msgview_actions_execute_cb(MessageView *msgview, guint action_nb,
509 GSList *msg_list = NULL;
511 if (msgview->msginfo)
512 msg_list = g_slist_append(msg_list, msgview->msginfo);
513 message_actions_execute(msgview, action_nb, msg_list);
514 g_slist_free(msg_list);
517 static void message_actions_execute(MessageView *msgview, guint action_nb,
524 GtkWidget *text = NULL;
525 GdkFont *msgfont = NULL;
527 ActionType action_type;
529 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
531 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
533 g_return_if_fail(buf);
534 g_return_if_fail(action = strstr(buf, ": "));
536 /* Point to the beginning of the command-line */
539 textview = messageview_get_current_textview(msgview);
541 text = textview->text;
542 msgfont = textview->msgfont;
543 body_pos = textview->body_pos;
545 partinfo = messageview_get_selected_mime_part(msgview);
547 /* this command will alter the message text */
548 action_type = action_get_type(action);
549 if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
550 msgview->filtered = TRUE;
552 if (action_type & ACTION_FILTERING_ACTION)
553 /* CLAWS: most of the above code is not necessary for applying
555 execute_filtering_actions(action, msg_list);
557 execute_actions(action, msg_list, text, msgfont, body_pos, partinfo);
560 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
562 GSList *action_list, *p;
563 const gchar *sbegin, *send;
564 gchar *action_string;
566 if (NULL == (sbegin = strstr2(action, "%as{")))
568 sbegin += sizeof "%as{" - 1;
569 if (NULL == (send = strrchr(sbegin, '}')))
571 action_string = g_strndup(sbegin, send - sbegin);
573 action_list = matcher_parser_get_action_list(action_string);
574 g_free(action_string);
575 if (action_list == NULL) return FALSE;
577 /* apply actions on each message info */
578 for (p = msglist; p && p->data; p = g_slist_next(p))
579 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
581 for (p = action_list; p; p = g_slist_next(p))
582 if (p->data) filteringaction_free(p->data);
583 g_slist_free(action_list);
587 static gboolean execute_actions(gchar *action, GSList *msg_list,
588 GtkWidget *text, GdkFont *msgfont,
589 gint body_pos, MimeInfo *partinfo)
591 GSList *children_list = NULL;
595 ChildInfo *child_info;
596 ActionType action_type;
599 guint start = 0, end = 0;
600 gchar *sel_str = NULL;
601 gchar *msg_str = NULL;
602 gchar *user_str = NULL;
603 gchar *user_hidden_str = NULL;
605 g_return_val_if_fail(action && *action, FALSE);
607 action_type = action_get_type(action);
609 if (action_type == ACTION_ERROR)
610 return FALSE; /* ERR: syntax error */
612 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
613 return FALSE; /* ERR: file command without selection */
615 msg_list_len = g_slist_length(msg_list);
617 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
618 if (msg_list_len > 1)
619 return FALSE; /* ERR: pipe + multiple selection */
621 return FALSE; /* ERR: pipe and no displayed text */
624 if (action_type & ACTION_SELECTION_STR) {
626 return FALSE; /* ERR: selection string but no text */
630 if (GTK_EDITABLE(text)->has_selection) {
631 start = GTK_EDITABLE(text)->selection_start_pos;
632 end = GTK_EDITABLE(text)->selection_end_pos;
642 end = gtk_stext_get_length(GTK_STEXT(text));
643 msg_str = gtk_editable_get_chars
644 (GTK_EDITABLE(text), start, end);
646 sel_str = gtk_editable_get_chars
647 (GTK_EDITABLE(text), start, end);
648 msg_str = g_strdup(sel_str);
652 end = gtk_stext_get_length(GTK_STEXT(text));
653 msg_str = gtk_editable_get_chars(GTK_EDITABLE(text),
658 if (action_type & ACTION_USER_STR) {
659 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
666 if (action_type & ACTION_USER_HIDDEN_STR) {
667 if (!(user_hidden_str =
668 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
676 if (action_type & ACTION_PIPE_OUT) {
677 gtk_stext_freeze(GTK_STEXT(text));
678 gtk_stext_set_point(GTK_STEXT(text), start);
679 gtk_stext_forward_delete(GTK_STEXT(text), end - start);
680 gtk_stext_thaw(GTK_STEXT(text));
683 children = g_new0(Children, 1);
685 children->action = g_strdup(action);
686 children->action_type = action_type;
687 children->msg_text = text;
688 children->msgfont = msgfont;
690 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
691 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
692 children->open_in = 1;
694 if (action_type & ACTION_SINGLE) {
697 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
698 msginfo = (MsgInfo *)cur->data;
700 is_ok = FALSE; /* ERR: msginfo missing */
703 cmd = parse_action_cmd(action, msginfo, msg_list,
705 user_hidden_str, sel_str);
707 debug_print("Action command error\n");
708 is_ok = FALSE; /* ERR: incorrect command */
711 if ((child_info = fork_child(cmd, msg_str, children))) {
712 children_list = g_slist_append(children_list,
718 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
719 user_str, user_hidden_str, sel_str);
721 if ((child_info = fork_child(cmd, msg_str, children))) {
722 children_list = g_slist_append(children_list,
727 is_ok = FALSE; /* ERR: incorrect command */
733 g_free(user_hidden_str);
735 if (!children_list) {
736 /* If not waiting for children, return */
737 free_children(children);
741 children->list = children_list;
742 children->nb = g_slist_length(children_list);
743 children->initial_nb = children->nb;
745 for (cur = children_list; cur; cur = cur->next) {
746 child_info = (ChildInfo *) cur->data;
747 child_info->tag_status =
748 gdk_input_add(child_info->chld_status,
750 catch_status, child_info);
753 create_io_dialog(children);
759 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
762 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
765 ChildInfo *child_info;
768 sync = !(children->action_type & ACTION_ASYNC);
770 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
771 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
774 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
776 alertpanel_error(_("Command could not be started. "
777 "Pipe creation failed.\n%s"),
779 /* Closing fd = -1 fails silently */
786 close(chld_status[0]);
787 close(chld_status[1]);
788 return NULL; /* Pipe error */
792 debug_print("Forking child and grandchild.\n");
795 if (pid == 0) { /* Child */
799 close(ConnectionNumber(gdk_display));
804 if (setpgid(0, getppid()))
808 if (children->action_type &
811 ACTION_USER_HIDDEN_IN)) {
812 close(fileno(stdin));
818 close(fileno(stdout));
823 close(fileno(stderr));
833 execvp("/bin/sh", cmdline);
837 } else if (gch_pid < (pid_t) 0) { /* Fork error */
839 write(chld_status[1], "1\n", 2);
850 close(chld_status[0]);
852 debug_print("Child: Waiting for grandchild\n");
853 waitpid(gch_pid, NULL, 0);
854 debug_print("Child: grandchild ended\n");
855 write(chld_status[1], "0\n", 2);
856 close(chld_status[1]);
860 } else if (pid < 0) { /* Fork error */
861 alertpanel_error(_("Could not fork to execute the following "
863 cmd, g_strerror(errno));
870 waitpid(pid, NULL, 0);
875 if (!(children->action_type &
876 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
880 close(chld_status[1]);
882 child_info = g_new0(ChildInfo, 1);
884 child_info->children = children;
886 child_info->pid = pid;
887 child_info->cmd = g_strdup(cmd);
888 child_info->new_out = FALSE;
889 child_info->output = g_string_new(NULL);
890 child_info->chld_in =
891 (children->action_type &
892 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
894 child_info->chld_out = chld_out[0];
895 child_info->chld_err = chld_err[0];
896 child_info->chld_status = chld_status[0];
897 child_info->tag_in = -1;
898 child_info->tag_out = gdk_input_add(chld_out[0], GDK_INPUT_READ,
899 catch_output, child_info);
900 child_info->tag_err = gdk_input_add(chld_err[0], GDK_INPUT_READ,
901 catch_output, child_info);
903 if (!(children->action_type &
904 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
907 if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
908 write(chld_in[1], msg_str, strlen(msg_str));
909 if (!(children->action_type &
910 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
912 child_info->chld_in = -1; /* No more input */
918 static void kill_children_cb(GtkWidget *widget, gpointer data)
921 Children *children = (Children *) data;
922 ChildInfo *child_info;
924 for (cur = children->list; cur; cur = cur->next) {
925 child_info = (ChildInfo *)(cur->data);
926 debug_print("Killing child group id %d\n", child_info->pid);
927 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
932 static gint wait_for_children(Children *children)
935 ChildInfo *child_info;
937 gint nb = children->nb;
941 cur = children->list;
944 child_info = (ChildInfo *)cur->data;
947 new_output |= child_info->new_out;
951 children->output |= new_output;
953 if (new_output || (children->dialog && (nb != children->nb)))
954 update_io_dialog(children);
959 if (!children->dialog) {
960 free_children(children);
961 } else if (!children->output) {
962 gtk_widget_destroy(children->dialog);
968 static void send_input(GtkWidget *w, gpointer data)
970 Children *children = (Children *) data;
971 ChildInfo *child_info = (ChildInfo *) children->list->data;
973 child_info->tag_in = gdk_input_add(child_info->chld_in,
975 catch_input, children);
976 gtk_widget_set_sensitive(children->input_hbox, FALSE);
979 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
981 hide_io_dialog_cb(w, data);
985 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
988 Children *children = (Children *)data;
991 gtk_signal_disconnect_by_data(GTK_OBJECT(children->dialog),
993 gtk_widget_destroy(children->dialog);
994 free_children(children);
998 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1001 if (event && event->keyval == GDK_Escape)
1002 hide_io_dialog_cb(widget, data);
1006 static void childinfo_close_pipes(ChildInfo *child_info)
1008 /* stdout and stderr pipes are guaranteed to be removed by
1009 * their handler, but in case where we receive child exit notification
1010 * before grand-child's pipes closing signals, we check them and close
1013 if (child_info->tag_in > 0)
1014 gdk_input_remove(child_info->tag_in);
1015 if (child_info->tag_out > 0)
1016 gdk_input_remove(child_info->tag_out);
1017 if (child_info->tag_err > 0)
1018 gdk_input_remove(child_info->tag_err);
1020 if (child_info->chld_in >= 0)
1021 close(child_info->chld_in);
1022 if (child_info->chld_out >= 0)
1023 close(child_info->chld_out);
1024 if (child_info->chld_err >= 0)
1025 close(child_info->chld_err);
1027 close(child_info->chld_status);
1030 static void free_children(Children *children)
1032 ChildInfo *child_info;
1034 debug_print("Freeing children data %p\n", children);
1036 g_free(children->action);
1037 while (children->list != NULL) {
1038 child_info = (ChildInfo *)children->list->data;
1039 g_free(child_info->cmd);
1040 g_string_free(child_info->output, TRUE);
1041 children->list = g_slist_remove(children->list, child_info);
1047 static void update_io_dialog(Children *children)
1051 debug_print("Updating actions input/output dialog.\n");
1053 if (children->progress_bar) {
1054 gtk_progress_configure(GTK_PROGRESS(children->progress_bar),
1055 children->initial_nb -children->nb,
1056 0.0, children->initial_nb);
1059 if (!children->nb) {
1060 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1061 gtk_widget_set_sensitive(children->close_btn, TRUE);
1062 if (children->input_hbox)
1063 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1064 gtk_widget_grab_focus(children->close_btn);
1065 gtk_signal_connect(GTK_OBJECT(children->dialog),
1067 GTK_SIGNAL_FUNC(io_dialog_key_pressed_cb),
1071 if (children->output) {
1072 GtkWidget *text = children->text;
1074 ChildInfo *child_info;
1077 font = gtk_object_get_data(GTK_OBJECT(children->dialog),
1079 gtk_widget_show(children->scrolledwin);
1080 gtk_text_freeze(GTK_TEXT(text));
1081 gtk_text_set_point(GTK_TEXT(text), 0);
1082 gtk_text_forward_delete(GTK_TEXT(text),
1083 gtk_text_get_length(GTK_TEXT(text)));
1084 for (cur = children->list; cur; cur = cur->next) {
1085 child_info = (ChildInfo *)cur->data;
1086 if (child_info->pid)
1087 caption = g_strdup_printf
1088 (_("--- Running: %s\n"),
1091 caption = g_strdup_printf
1092 (_("--- Ended: %s\n"),
1095 gtk_text_insert(GTK_TEXT(text), font, NULL, NULL,
1097 gtk_text_insert(GTK_TEXT(text), font, NULL, NULL,
1098 child_info->output->str, -1);
1100 child_info->new_out = FALSE;
1102 gtk_text_thaw(GTK_TEXT(text));
1106 static void create_io_dialog(Children *children)
1110 GtkWidget *entry = NULL;
1111 GtkWidget *input_hbox = NULL;
1112 GtkWidget *send_button;
1115 GtkWidget *scrolledwin;
1117 GtkWidget *progress_bar = NULL;
1118 GtkWidget *abort_button;
1119 GtkWidget *close_button;
1120 GdkFont *output_font;
1122 debug_print("Creating action IO dialog\n");
1124 dialog = gtk_dialog_new();
1125 gtk_container_set_border_width
1126 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1127 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1128 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1129 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1130 manage_window_set_transient(GTK_WINDOW(dialog));
1131 gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
1132 GTK_SIGNAL_FUNC(delete_io_dialog_cb), children);
1133 gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
1134 GTK_SIGNAL_FUNC(hide_io_dialog_cb),
1137 vbox = gtk_vbox_new(FALSE, 8);
1138 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1139 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1140 gtk_widget_show(vbox);
1142 label = gtk_label_new(children->action);
1143 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1144 gtk_widget_show(label);
1146 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1147 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1148 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
1149 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1150 gtk_widget_set_usize(scrolledwin, 480, 200);
1151 gtk_widget_hide(scrolledwin);
1153 text = gtk_text_new(gtk_scrolled_window_get_hadjustment
1154 (GTK_SCROLLED_WINDOW(scrolledwin)),
1155 gtk_scrolled_window_get_vadjustment
1156 (GTK_SCROLLED_WINDOW(scrolledwin)));
1157 gtk_text_set_editable(GTK_TEXT(text), FALSE);
1158 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1159 gtk_widget_show(text);
1161 if (children->open_in) {
1162 input_hbox = gtk_hbox_new(FALSE, 8);
1163 gtk_widget_show(input_hbox);
1165 entry = gtk_entry_new();
1166 gtk_widget_set_usize(entry, 320, -1);
1167 gtk_signal_connect(GTK_OBJECT(entry), "activate",
1168 GTK_SIGNAL_FUNC(send_input), children);
1169 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1170 if (children->action_type & ACTION_USER_HIDDEN_IN)
1171 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1172 gtk_widget_show(entry);
1174 send_button = gtk_button_new_with_label(_(" Send "));
1175 gtk_signal_connect(GTK_OBJECT(send_button), "clicked",
1176 GTK_SIGNAL_FUNC(send_input), children);
1177 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1179 gtk_widget_show(send_button);
1181 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1182 gtk_widget_grab_focus(entry);
1185 if (children->initial_nb > 1) {
1186 progress_bar = gtk_progress_bar_new();
1187 gtk_progress_bar_set_bar_style(GTK_PROGRESS_BAR(progress_bar),
1188 GTK_PROGRESS_CONTINUOUS);
1189 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1190 GTK_PROGRESS_LEFT_TO_RIGHT);
1191 gtk_progress_set_activity_mode(GTK_PROGRESS(progress_bar),
1193 gtk_progress_set_format_string(GTK_PROGRESS(progress_bar),
1194 _("Completed %v/%u"));
1195 gtk_progress_set_show_text(GTK_PROGRESS(progress_bar), TRUE);
1196 gtk_progress_configure(GTK_PROGRESS(progress_bar),
1197 children->initial_nb -children->nb,
1198 0.0, children->initial_nb);
1200 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1201 gtk_widget_show(progress_bar);
1204 gtkut_button_set_create(&hbox, &abort_button, _("Abort"),
1205 &close_button, _("Close"), NULL, NULL);
1206 gtk_signal_connect(GTK_OBJECT(abort_button), "clicked",
1207 GTK_SIGNAL_FUNC(kill_children_cb), children);
1208 gtk_signal_connect(GTK_OBJECT(close_button), "clicked",
1209 GTK_SIGNAL_FUNC(hide_io_dialog_cb), children);
1210 gtk_widget_show(hbox);
1213 gtk_widget_set_sensitive(close_button, FALSE);
1215 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1217 children->dialog = dialog;
1218 children->scrolledwin = scrolledwin;
1219 children->text = text;
1220 children->input_hbox = children->open_in ? input_hbox : NULL;
1221 children->input_entry = children->open_in ? entry : NULL;
1222 children->progress_bar = progress_bar;
1223 children->abort_btn = abort_button;
1224 children->close_btn = close_button;
1226 output_font = gtkut_font_load_from_fontset(prefs_common.textfont);
1227 gtk_object_set_data_full(GTK_OBJECT(dialog), "s_txtfont",
1229 (GtkDestroyNotify)gdk_font_unref);
1230 gtk_widget_show(dialog);
1233 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1235 ChildInfo *child_info = (ChildInfo *)data;
1239 gdk_input_remove(child_info->tag_status);
1241 c = read(source, &buf, 1);
1242 debug_print("Child returned %c\n", buf);
1244 waitpid(-child_info->pid, NULL, 0);
1245 childinfo_close_pipes(child_info);
1246 child_info->pid = 0;
1248 wait_for_children(child_info->children);
1251 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1253 Children *children = (Children *)data;
1254 ChildInfo *child_info = (ChildInfo *)children->list->data;
1258 debug_print("Sending input to grand child.\n");
1259 if (!(cond && GDK_INPUT_WRITE))
1262 gdk_input_remove(child_info->tag_in);
1263 child_info->tag_in = -1;
1265 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1267 len = strlen(input);
1271 c = write(child_info->chld_in, input + count, len - count);
1274 } while (c >= 0 && count < len);
1277 write(child_info->chld_in, "\n", 2);
1281 gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1282 gtk_widget_set_sensitive(children->input_hbox, TRUE);
1283 close(child_info->chld_in);
1284 child_info->chld_in = -1;
1285 debug_print("Input to grand child sent.\n");
1288 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1290 ChildInfo *child_info = (ChildInfo *)data;
1292 gchar buf[BUFFSIZE];
1294 debug_print("Catching grand child's output.\n");
1295 if (child_info->children->action_type &
1296 (ACTION_PIPE_OUT | ACTION_INSERT)
1297 && source == child_info->chld_out) {
1298 gboolean is_selection = FALSE;
1299 GtkWidget *text = child_info->children->msg_text;
1301 if (GTK_EDITABLE(text)->has_selection)
1302 is_selection = TRUE;
1303 gtk_stext_freeze(GTK_STEXT(text));
1305 c = read(source, buf, sizeof(buf) - 1);
1308 gtk_stext_insert(GTK_STEXT(text),
1309 child_info->children->msgfont,
1310 NULL, NULL, buf, c);
1313 /* Using the select_region draws things. Should not.
1314 * so we just change selection position and
1315 * defere drawing when thawing. Hack?
1317 GTK_EDITABLE(text)->selection_end_pos =
1318 gtk_stext_get_point(GTK_STEXT(text));
1320 gtk_stext_thaw(GTK_STEXT(text));
1322 c = read(source, buf, sizeof(buf) - 1);
1323 for (i = 0; i < c; i++)
1324 g_string_append_c(child_info->output, buf[i]);
1326 child_info->new_out = TRUE;
1329 if (source == child_info->chld_out) {
1330 gdk_input_remove(child_info->tag_out);
1331 child_info->tag_out = -1;
1332 close(child_info->chld_out);
1333 child_info->chld_out = -1;
1335 gdk_input_remove(child_info->tag_err);
1336 child_info->tag_err = -1;
1337 close(child_info->chld_err);
1338 child_info->chld_err = -1;
1342 wait_for_children(child_info->children);
1345 static gchar *get_user_string(const gchar *action, ActionType type)
1348 gchar *user_str = NULL;
1351 case ACTION_USER_HIDDEN_STR:
1352 message = g_strdup_printf
1353 (_("Enter the argument for the following action:\n"
1354 "(`%%h' will be replaced with the argument)\n"
1357 user_str = input_dialog_with_invisible
1358 (_("Action's hidden user argument"), message, NULL);
1360 case ACTION_USER_STR:
1361 message = g_strdup_printf
1362 (_("Enter the argument for the following action:\n"
1363 "(`%%u' will be replaced with the argument)\n"
1366 user_str = input_dialog
1367 (_("Action's user argument"), message, NULL);
1370 g_warning("Unsupported action type %d", type);