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"
53 typedef struct _Children Children;
54 typedef struct _ChildInfo ChildInfo;
55 typedef struct _UserStringDialog UserStringDialog;
61 GtkWidget *input_entry;
62 GtkWidget *input_hbox;
63 GtkWidget *progress_bar;
66 GtkWidget *scrolledwin;
69 ActionType action_type;
98 static void action_update_menu (GtkItemFactory *ifactory,
102 static void compose_actions_execute_cb (Compose *compose,
105 static void mainwin_actions_execute_cb (MainWindow *mainwin,
108 static void msgview_actions_execute_cb (MessageView *msgview,
111 static void message_actions_execute (MessageView *msgview,
115 static gboolean execute_actions (gchar *action,
122 static gchar *parse_action_cmd (gchar *action,
126 const gchar *user_str,
127 const gchar *user_hidden_str,
128 const gchar *sel_str);
129 static gboolean parse_append_filename (GString *cmd,
132 static gboolean parse_append_msgpart (GString *cmd,
136 static ChildInfo *fork_child (gchar *cmd,
137 const gchar *msg_str,
140 static gint wait_for_children (Children *children);
142 static void free_children (Children *children);
144 static void childinfo_close_pipes (ChildInfo *child_info);
146 static void create_io_dialog (Children *children);
147 static void update_io_dialog (Children *children);
149 static void hide_io_dialog_cb (GtkWidget *widget,
151 static gint io_dialog_key_pressed_cb (GtkWidget *widget,
155 static void catch_output (gpointer data,
157 GdkInputCondition cond);
158 static void catch_input (gpointer data,
160 GdkInputCondition cond);
161 static void catch_status (gpointer data,
163 GdkInputCondition cond);
165 static gchar *get_user_string (const gchar *action,
169 ActionType action_get_type(const gchar *action_str)
172 ActionType action_type = ACTION_NONE;
174 g_return_val_if_fail(action_str, ACTION_ERROR);
175 g_return_val_if_fail(*action_str, ACTION_ERROR);
180 action_type |= ACTION_PIPE_IN;
182 } else if (p[0] == '>') {
183 action_type |= ACTION_USER_IN;
185 } else if (p[0] == '*') {
186 action_type |= ACTION_USER_HIDDEN_IN;
193 while (*p && action_type != ACTION_ERROR) {
197 action_type |= ACTION_SINGLE;
200 action_type |= ACTION_MULTIPLE;
203 action_type |= ACTION_SINGLE;
206 action_type |= ACTION_SELECTION_STR;
209 action_type |= ACTION_USER_STR;
212 action_type |= ACTION_USER_HIDDEN_STR;
215 action_type = ACTION_ERROR;
218 } else if (p[0] == '|') {
220 action_type |= ACTION_PIPE_OUT;
221 } else if (p[0] == '>') {
223 action_type |= ACTION_INSERT;
224 } else if (p[0] == '&') {
226 action_type |= ACTION_ASYNC;
234 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
235 GSList *msg_list, MimeInfo *partinfo,
236 const gchar *user_str,
237 const gchar *user_hidden_str,
238 const gchar *sel_str)
246 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
249 cmd = g_string_sized_new(strlen(action));
252 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
253 if (p[0] == '%' && p[1]) {
256 if (!parse_append_filename(cmd, msginfo)) {
257 g_string_free(cmd, TRUE);
263 for (cur = msg_list; cur != NULL;
265 MsgInfo *msg = (MsgInfo *)cur->data;
267 if (!parse_append_filename(cmd, msg)) {
268 g_string_free(cmd, TRUE);
272 g_string_append_c(cmd, ' ');
277 if (!parse_append_msgpart(cmd, msginfo,
279 g_string_free(cmd, TRUE);
286 g_string_append(cmd, sel_str);
291 g_string_append(cmd, user_str);
296 g_string_append(cmd, user_hidden_str);
300 g_string_append_c(cmd, p[0]);
301 g_string_append_c(cmd, p[1]);
305 g_string_append_c(cmd, p[0]);
310 g_string_free(cmd, TRUE);
315 g_string_free(cmd, FALSE);
319 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
323 g_return_val_if_fail(msginfo, FALSE);
325 filename = procmsg_get_message_file(msginfo);
328 g_string_append(cmd, filename);
331 alertpanel_error(_("Could not get message file %d"),
339 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
342 gboolean single_part = FALSE;
344 gchar *part_filename;
348 partinfo = procmime_scan_message(msginfo);
350 alertpanel_error(_("Could not get message part."));
357 filename = procmsg_get_message_file_path(msginfo);
358 part_filename = procmime_get_tmp_file_name(partinfo);
360 ret = procmime_get_part(part_filename, filename, partinfo);
363 procmime_mimeinfo_free_all(partinfo);
367 alertpanel_error(_("Can't get part of multipart message"));
368 g_free(part_filename);
372 g_string_append(cmd, part_filename);
374 g_free(part_filename);
379 void actions_execute(gpointer data,
384 if (source == TOOLBAR_MAIN)
385 mainwin_actions_execute_cb((MainWindow*)data, action_nb, widget);
386 else if (source == TOOLBAR_COMPOSE)
387 compose_actions_execute_cb((Compose*)data, action_nb, widget);
388 else if (source == TOOLBAR_MSGVIEW)
389 msgview_actions_execute_cb((MessageView*)data, action_nb, widget);
392 void action_update_mainwin_menu(GtkItemFactory *ifactory, MainWindow *mainwin)
394 action_update_menu(ifactory, "/Tools/Actions",
395 mainwin_actions_execute_cb, mainwin);
398 void action_update_msgview_menu(GtkItemFactory *ifactory, MessageView *msgview)
400 action_update_menu(ifactory, "/Tools/Actions",
401 msgview_actions_execute_cb, msgview);
404 void action_update_compose_menu(GtkItemFactory *ifactory, Compose *compose)
406 action_update_menu(ifactory, "/Tools/Actions",
407 compose_actions_execute_cb, compose);
410 static void action_update_menu(GtkItemFactory *ifactory, gchar *branch_path,
411 gpointer callback, gpointer data)
416 gchar *action, *action_p;
418 GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
420 ifentry.path = branch_path;
421 menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
422 g_return_if_fail(menuitem != NULL);
424 amenu = GTK_MENU_SHELL(menuitem)->children;
425 while (amenu != NULL) {
426 GList *alist = amenu->next;
427 gtk_widget_destroy(GTK_WIDGET(amenu->data));
431 ifentry.accelerator = NULL;
432 ifentry.callback_action = 0;
433 ifentry.callback = callback;
434 ifentry.item_type = NULL;
436 for (cur = prefs_common.actions_list; cur; cur = cur->next) {
437 action = g_strdup((gchar *)cur->data);
438 action_p = strstr(action, ": ");
439 if (action_p && action_p[2] &&
440 action_get_type(&action_p[2]) != ACTION_ERROR) {
442 menu_path = g_strdup_printf("%s/%s", branch_path,
444 ifentry.path = menu_path;
445 gtk_item_factory_create_item(ifactory, &ifentry, data,
450 ifentry.callback_action++;
454 static void compose_actions_execute_cb(Compose *compose, guint action_nb,
458 ActionType action_type;
460 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
462 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
463 g_return_if_fail(buf != NULL);
464 action = strstr(buf, ": ");
465 g_return_if_fail(action != NULL);
467 /* Point to the beginning of the command-line */
470 action_type = action_get_type(action);
471 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
473 (_("The selected action cannot be used in the compose window\n"
474 "because it contains %%f, %%F or %%p."));
478 execute_actions(action, NULL, compose->text, NULL, 0, NULL);
481 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
486 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
487 message_actions_execute(mainwin->messageview, action_nb, msg_list);
488 g_slist_free(msg_list);
491 static void msgview_actions_execute_cb(MessageView *msgview, guint action_nb,
494 GSList *msg_list = NULL;
496 if (msgview->msginfo)
497 msg_list = g_slist_append(msg_list, msgview->msginfo);
498 message_actions_execute(msgview, action_nb, msg_list);
499 g_slist_free(msg_list);
502 static void message_actions_execute(MessageView *msgview, guint action_nb,
509 GtkWidget *text = NULL;
510 GdkFont *msgfont = NULL;
512 ActionType action_type;
514 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
516 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
518 g_return_if_fail(buf);
519 g_return_if_fail(action = strstr(buf, ": "));
521 /* Point to the beginning of the command-line */
524 textview = messageview_get_current_textview(msgview);
526 text = textview->text;
527 msgfont = textview->msgfont;
528 body_pos = textview->body_pos;
530 partinfo = messageview_get_selected_mime_part(msgview);
532 /* this command will alter the message text */
533 action_type = action_get_type(action);
534 if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
535 msgview->filtered = TRUE;
537 execute_actions(action, msg_list, text, msgfont, body_pos, partinfo);
540 static gboolean execute_actions(gchar *action, GSList *msg_list,
541 GtkWidget *text, GdkFont *msgfont,
542 gint body_pos, MimeInfo *partinfo)
544 GSList *children_list = NULL;
548 ChildInfo *child_info;
549 ActionType action_type;
552 guint start = 0, end = 0;
553 gchar *sel_str = NULL;
554 gchar *msg_str = NULL;
555 gchar *user_str = NULL;
556 gchar *user_hidden_str = NULL;
558 g_return_val_if_fail(action && *action, FALSE);
560 action_type = action_get_type(action);
562 if (action_type == ACTION_ERROR)
563 return FALSE; /* ERR: syntax error */
565 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
566 return FALSE; /* ERR: file command without selection */
568 msg_list_len = g_slist_length(msg_list);
570 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
571 if (msg_list_len > 1)
572 return FALSE; /* ERR: pipe + multiple selection */
574 return FALSE; /* ERR: pipe and no displayed text */
577 if (action_type & ACTION_SELECTION_STR) {
579 return FALSE; /* ERR: selection string but no text */
583 if (GTK_EDITABLE(text)->has_selection) {
584 start = GTK_EDITABLE(text)->selection_start_pos;
585 end = GTK_EDITABLE(text)->selection_end_pos;
595 end = gtk_stext_get_length(GTK_STEXT(text));
596 msg_str = gtk_editable_get_chars
597 (GTK_EDITABLE(text), start, end);
599 sel_str = gtk_editable_get_chars
600 (GTK_EDITABLE(text), start, end);
601 msg_str = g_strdup(sel_str);
605 end = gtk_stext_get_length(GTK_STEXT(text));
606 msg_str = gtk_editable_get_chars(GTK_EDITABLE(text),
611 if (action_type & ACTION_USER_STR) {
612 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
619 if (action_type & ACTION_USER_HIDDEN_STR) {
620 if (!(user_hidden_str =
621 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
629 if (action_type & ACTION_PIPE_OUT) {
630 gtk_stext_freeze(GTK_STEXT(text));
631 gtk_stext_set_point(GTK_STEXT(text), start);
632 gtk_stext_forward_delete(GTK_STEXT(text), end - start);
633 gtk_stext_thaw(GTK_STEXT(text));
636 children = g_new0(Children, 1);
638 children->action = g_strdup(action);
639 children->action_type = action_type;
640 children->msg_text = text;
641 children->msgfont = msgfont;
643 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
644 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
645 children->open_in = 1;
647 if (action_type & ACTION_SINGLE) {
650 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
651 msginfo = (MsgInfo *)cur->data;
653 is_ok = FALSE; /* ERR: msginfo missing */
656 cmd = parse_action_cmd(action, msginfo, msg_list,
658 user_hidden_str, sel_str);
660 debug_print("Action command error\n");
661 is_ok = FALSE; /* ERR: incorrect command */
664 if ((child_info = fork_child(cmd, msg_str, children))) {
665 children_list = g_slist_append(children_list,
671 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
672 user_str, user_hidden_str, sel_str);
674 if ((child_info = fork_child(cmd, msg_str, children))) {
675 children_list = g_slist_append(children_list,
680 is_ok = FALSE; /* ERR: incorrect command */
686 g_free(user_hidden_str);
688 if (!children_list) {
689 /* If not waiting for children, return */
690 free_children(children);
694 children->list = children_list;
695 children->nb = g_slist_length(children_list);
696 children->initial_nb = children->nb;
698 for (cur = children_list; cur; cur = cur->next) {
699 child_info = (ChildInfo *) cur->data;
700 child_info->tag_status =
701 gdk_input_add(child_info->chld_status,
703 catch_status, child_info);
706 create_io_dialog(children);
712 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
715 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
718 ChildInfo *child_info;
721 sync = !(children->action_type & ACTION_ASYNC);
723 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
724 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
727 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
729 alertpanel_error(_("Command could not be started. "
730 "Pipe creation failed.\n%s"),
732 /* Closing fd = -1 fails silently */
739 close(chld_status[0]);
740 close(chld_status[1]);
741 return NULL; /* Pipe error */
745 debug_print("Forking child and grandchild.\n");
748 if (pid == 0) { /* Child */
752 close(ConnectionNumber(gdk_display));
757 if (setpgid(0, getppid()))
761 if (children->action_type &
764 ACTION_USER_HIDDEN_IN)) {
765 close(fileno(stdin));
771 close(fileno(stdout));
776 close(fileno(stderr));
786 execvp("/bin/sh", cmdline);
790 } else if (gch_pid < (pid_t) 0) { /* Fork error */
792 write(chld_status[1], "1\n", 2);
803 close(chld_status[0]);
805 debug_print("Child: Waiting for grandchild\n");
806 waitpid(gch_pid, NULL, 0);
807 debug_print("Child: grandchild ended\n");
808 write(chld_status[1], "0\n", 2);
809 close(chld_status[1]);
813 } else if (pid < 0) { /* Fork error */
814 alertpanel_error(_("Could not fork to execute the following "
816 cmd, g_strerror(errno));
823 waitpid(pid, NULL, 0);
828 if (!(children->action_type &
829 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
833 close(chld_status[1]);
835 child_info = g_new0(ChildInfo, 1);
837 child_info->children = children;
839 child_info->pid = pid;
840 child_info->cmd = g_strdup(cmd);
841 child_info->new_out = FALSE;
842 child_info->output = g_string_new(NULL);
843 child_info->chld_in =
844 (children->action_type &
845 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
847 child_info->chld_out = chld_out[0];
848 child_info->chld_err = chld_err[0];
849 child_info->chld_status = chld_status[0];
850 child_info->tag_in = -1;
851 child_info->tag_out = gdk_input_add(chld_out[0], GDK_INPUT_READ,
852 catch_output, child_info);
853 child_info->tag_err = gdk_input_add(chld_err[0], GDK_INPUT_READ,
854 catch_output, child_info);
856 if (!(children->action_type &
857 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
860 if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
861 write(chld_in[1], msg_str, strlen(msg_str));
862 if (!(children->action_type &
863 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
865 child_info->chld_in = -1; /* No more input */
871 static void kill_children_cb(GtkWidget *widget, gpointer data)
874 Children *children = (Children *) data;
875 ChildInfo *child_info;
877 for (cur = children->list; cur; cur = cur->next) {
878 child_info = (ChildInfo *)(cur->data);
879 debug_print("Killing child group id %d\n", child_info->pid);
880 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
885 static gint wait_for_children(Children *children)
888 ChildInfo *child_info;
890 gint nb = children->nb;
894 cur = children->list;
897 child_info = (ChildInfo *)cur->data;
900 new_output |= child_info->new_out;
904 children->output |= new_output;
906 if (new_output || (children->dialog && (nb != children->nb)))
907 update_io_dialog(children);
912 if (!children->dialog) {
913 free_children(children);
914 } else if (!children->output) {
915 gtk_widget_destroy(children->dialog);
921 static void send_input(GtkWidget *w, gpointer data)
923 Children *children = (Children *) data;
924 ChildInfo *child_info = (ChildInfo *) children->list->data;
926 child_info->tag_in = gdk_input_add(child_info->chld_in,
928 catch_input, children);
929 gtk_widget_set_sensitive(children->input_hbox, FALSE);
932 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
934 hide_io_dialog_cb(w, data);
938 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
941 Children *children = (Children *)data;
944 gtk_signal_disconnect_by_data(GTK_OBJECT(children->dialog),
946 gtk_widget_destroy(children->dialog);
947 free_children(children);
951 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
954 if (event && event->keyval == GDK_Escape)
955 hide_io_dialog_cb(widget, data);
959 static void childinfo_close_pipes(ChildInfo *child_info)
961 /* stdout and stderr pipes are guaranteed to be removed by
962 * their handler, but in case where we receive child exit notification
963 * before grand-child's pipes closing signals, we check them and close
966 if (child_info->tag_in > 0)
967 gdk_input_remove(child_info->tag_in);
968 if (child_info->tag_out > 0)
969 gdk_input_remove(child_info->tag_out);
970 if (child_info->tag_err > 0)
971 gdk_input_remove(child_info->tag_err);
973 if (child_info->chld_in >= 0)
974 close(child_info->chld_in);
975 if (child_info->chld_out >= 0)
976 close(child_info->chld_out);
977 if (child_info->chld_err >= 0)
978 close(child_info->chld_err);
980 close(child_info->chld_status);
983 static void free_children(Children *children)
985 ChildInfo *child_info;
987 debug_print("Freeing children data %p\n", children);
989 g_free(children->action);
990 while (children->list != NULL) {
991 child_info = (ChildInfo *)children->list->data;
992 g_free(child_info->cmd);
993 g_string_free(child_info->output, TRUE);
994 children->list = g_slist_remove(children->list, child_info);
1000 static void update_io_dialog(Children *children)
1004 debug_print("Updating actions input/output dialog.\n");
1006 if (children->progress_bar) {
1007 gtk_progress_configure(GTK_PROGRESS(children->progress_bar),
1008 children->initial_nb -children->nb,
1009 0.0, children->initial_nb);
1012 if (!children->nb) {
1013 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1014 gtk_widget_set_sensitive(children->close_btn, TRUE);
1015 if (children->input_hbox)
1016 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1017 gtk_widget_grab_focus(children->close_btn);
1018 gtk_signal_connect(GTK_OBJECT(children->dialog),
1020 GTK_SIGNAL_FUNC(io_dialog_key_pressed_cb),
1024 if (children->output) {
1025 GtkWidget *text = children->text;
1027 ChildInfo *child_info;
1029 gtk_widget_show(children->scrolledwin);
1030 gtk_text_freeze(GTK_TEXT(text));
1031 gtk_text_set_point(GTK_TEXT(text), 0);
1032 gtk_text_forward_delete(GTK_TEXT(text),
1033 gtk_text_get_length(GTK_TEXT(text)));
1034 for (cur = children->list; cur; cur = cur->next) {
1035 child_info = (ChildInfo *)cur->data;
1036 if (child_info->pid)
1037 caption = g_strdup_printf
1038 (_("--- Running: %s\n"),
1041 caption = g_strdup_printf
1042 (_("--- Ended: %s\n"),
1045 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1047 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1048 child_info->output->str, -1);
1050 child_info->new_out = FALSE;
1052 gtk_text_thaw(GTK_TEXT(text));
1056 static void create_io_dialog(Children *children)
1060 GtkWidget *entry = NULL;
1061 GtkWidget *input_hbox = NULL;
1062 GtkWidget *send_button;
1065 GtkWidget *scrolledwin;
1067 GtkWidget *progress_bar = NULL;
1068 GtkWidget *abort_button;
1069 GtkWidget *close_button;
1071 debug_print("Creating action IO dialog\n");
1073 dialog = gtk_dialog_new();
1074 gtk_container_set_border_width
1075 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1076 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1077 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1078 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1079 manage_window_set_transient(GTK_WINDOW(dialog));
1080 gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
1081 GTK_SIGNAL_FUNC(delete_io_dialog_cb), children);
1082 gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
1083 GTK_SIGNAL_FUNC(hide_io_dialog_cb),
1086 vbox = gtk_vbox_new(FALSE, 8);
1087 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1088 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1089 gtk_widget_show(vbox);
1091 label = gtk_label_new(children->action);
1092 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1093 gtk_widget_show(label);
1095 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1096 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1097 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
1098 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1099 gtk_widget_set_usize(scrolledwin, 480, 200);
1100 gtk_widget_hide(scrolledwin);
1102 text = gtk_text_new(gtk_scrolled_window_get_hadjustment
1103 (GTK_SCROLLED_WINDOW(scrolledwin)),
1104 gtk_scrolled_window_get_vadjustment
1105 (GTK_SCROLLED_WINDOW(scrolledwin)));
1106 gtk_text_set_editable(GTK_TEXT(text), FALSE);
1107 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1108 gtk_widget_show(text);
1110 if (children->open_in) {
1111 input_hbox = gtk_hbox_new(FALSE, 8);
1112 gtk_widget_show(input_hbox);
1114 entry = gtk_entry_new();
1115 gtk_widget_set_usize(entry, 320, -1);
1116 gtk_signal_connect(GTK_OBJECT(entry), "activate",
1117 GTK_SIGNAL_FUNC(send_input), children);
1118 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1119 if (children->action_type & ACTION_USER_HIDDEN_IN)
1120 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1121 gtk_widget_show(entry);
1123 send_button = gtk_button_new_with_label(_(" Send "));
1124 gtk_signal_connect(GTK_OBJECT(send_button), "clicked",
1125 GTK_SIGNAL_FUNC(send_input), children);
1126 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1128 gtk_widget_show(send_button);
1130 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1131 gtk_widget_grab_focus(entry);
1134 if (children->initial_nb > 1) {
1135 progress_bar = gtk_progress_bar_new();
1136 gtk_progress_bar_set_bar_style(GTK_PROGRESS_BAR(progress_bar),
1137 GTK_PROGRESS_CONTINUOUS);
1138 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1139 GTK_PROGRESS_LEFT_TO_RIGHT);
1140 gtk_progress_set_activity_mode(GTK_PROGRESS(progress_bar),
1142 gtk_progress_set_format_string(GTK_PROGRESS(progress_bar),
1143 _("Completed %v/%u"));
1144 gtk_progress_set_show_text(GTK_PROGRESS(progress_bar), TRUE);
1145 gtk_progress_configure(GTK_PROGRESS(progress_bar),
1146 children->initial_nb -children->nb,
1147 0.0, children->initial_nb);
1149 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, TRUE, TRUE, 0);
1150 gtk_widget_show(progress_bar);
1153 gtkut_button_set_create(&hbox, &abort_button, _("Abort"),
1154 &close_button, _("Close"), NULL, NULL);
1155 gtk_signal_connect(GTK_OBJECT(abort_button), "clicked",
1156 GTK_SIGNAL_FUNC(kill_children_cb), children);
1157 gtk_signal_connect(GTK_OBJECT(close_button), "clicked",
1158 GTK_SIGNAL_FUNC(hide_io_dialog_cb), children);
1159 gtk_widget_show(hbox);
1162 gtk_widget_set_sensitive(close_button, FALSE);
1164 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1166 children->dialog = dialog;
1167 children->scrolledwin = scrolledwin;
1168 children->text = text;
1169 children->input_hbox = children->open_in ? input_hbox : NULL;
1170 children->input_entry = children->open_in ? entry : NULL;
1171 children->progress_bar = progress_bar;
1172 children->abort_btn = abort_button;
1173 children->close_btn = close_button;
1175 gtk_widget_show(dialog);
1178 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1180 ChildInfo *child_info = (ChildInfo *)data;
1184 gdk_input_remove(child_info->tag_status);
1186 c = read(source, &buf, 1);
1187 debug_print("Child returned %c\n", buf);
1189 waitpid(-child_info->pid, NULL, 0);
1190 childinfo_close_pipes(child_info);
1191 child_info->pid = 0;
1193 wait_for_children(child_info->children);
1196 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1198 Children *children = (Children *)data;
1199 ChildInfo *child_info = (ChildInfo *)children->list->data;
1203 debug_print("Sending input to grand child.\n");
1204 if (!(cond && GDK_INPUT_WRITE))
1207 gdk_input_remove(child_info->tag_in);
1208 child_info->tag_in = -1;
1210 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1212 len = strlen(input);
1216 c = write(child_info->chld_in, input + count, len - count);
1219 } while (c >= 0 && count < len);
1222 write(child_info->chld_in, "\n", 2);
1226 gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1227 gtk_widget_set_sensitive(children->input_hbox, TRUE);
1228 close(child_info->chld_in);
1229 child_info->chld_in = -1;
1230 debug_print("Input to grand child sent.\n");
1233 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1235 ChildInfo *child_info = (ChildInfo *)data;
1237 gchar buf[BUFFSIZE];
1239 debug_print("Catching grand child's output.\n");
1240 if (child_info->children->action_type &
1241 (ACTION_PIPE_OUT | ACTION_INSERT)
1242 && source == child_info->chld_out) {
1243 gboolean is_selection = FALSE;
1244 GtkWidget *text = child_info->children->msg_text;
1246 if (GTK_EDITABLE(text)->has_selection)
1247 is_selection = TRUE;
1248 gtk_stext_freeze(GTK_STEXT(text));
1250 c = read(source, buf, sizeof(buf) - 1);
1253 gtk_stext_insert(GTK_STEXT(text),
1254 child_info->children->msgfont,
1255 NULL, NULL, buf, c);
1258 /* Using the select_region draws things. Should not.
1259 * so we just change selection position and
1260 * defere drawing when thawing. Hack?
1262 GTK_EDITABLE(text)->selection_end_pos =
1263 gtk_stext_get_point(GTK_STEXT(text));
1265 gtk_stext_thaw(GTK_STEXT(text));
1267 c = read(source, buf, sizeof(buf) - 1);
1268 for (i = 0; i < c; i++)
1269 g_string_append_c(child_info->output, buf[i]);
1271 child_info->new_out = TRUE;
1274 if (source == child_info->chld_out) {
1275 gdk_input_remove(child_info->tag_out);
1276 child_info->tag_out = -1;
1277 close(child_info->chld_out);
1278 child_info->chld_out = -1;
1280 gdk_input_remove(child_info->tag_err);
1281 child_info->tag_err = -1;
1282 close(child_info->chld_err);
1283 child_info->chld_err = -1;
1287 wait_for_children(child_info->children);
1290 static gchar *get_user_string(const gchar *action, ActionType type)
1293 gchar *user_str = NULL;
1296 case ACTION_USER_HIDDEN_STR:
1297 message = g_strdup_printf
1298 (_("Enter the argument for the following action:\n"
1299 "(`%%h' will be replaced with the argument)\n"
1302 user_str = input_dialog_with_invisible
1303 (_("Action's hidden user argument"), message, NULL);
1305 case ACTION_USER_STR:
1306 message = g_strdup_printf
1307 (_("Enter the argument for the following action:\n"
1308 "(`%%u' will be replaced with the argument)\n"
1311 user_str = input_dialog
1312 (_("Action's user argument"), message, NULL);
1315 g_warning("Unsupported action type %d", type);