2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 Hiroyuki Yamamoto
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"
54 typedef struct _Children Children;
55 typedef struct _ChildInfo ChildInfo;
56 typedef struct _UserStringDialog UserStringDialog;
62 GtkWidget *input_entry;
63 GtkWidget *input_hbox;
66 GtkWidget *scrolledwin;
95 static void action_update_menu (GtkItemFactory *ifactory,
99 static void compose_actions_execute_cb (Compose *compose,
102 static void mainwin_actions_execute_cb (MainWindow *mainwin,
105 static void msgview_actions_execute_cb (MessageView *msgview,
108 static void message_actions_execute (MessageView *msgview,
112 static gboolean execute_actions (gchar *action,
119 static gchar *parse_action_cmd (gchar *action,
123 const gchar *user_str,
124 const gchar *user_hidden_str,
125 const gchar *sel_str);
126 static gboolean parse_append_filename (GString *cmd,
129 static gboolean parse_append_msgpart (GString *cmd,
133 static ChildInfo *fork_child (gchar *cmd,
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 GtkCTree *ctree, MimeView *mimeview,
236 const gchar *user_str,
237 const gchar *user_hidden_str,
238 const gchar *sel_str)
247 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
250 cmd = g_string_sized_new(strlen(action));
253 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
254 if (p[0] == '%' && p[1]) {
257 if (!parse_append_filename(cmd, msginfo)) {
258 g_string_free(cmd, TRUE);
264 for (cur = GTK_CLIST(ctree)->selection;
265 cur != NULL; cur = cur->next) {
266 msg = gtk_ctree_node_get_row_data(ctree,
267 GTK_CTREE_NODE(cur->data));
268 if (!parse_append_filename(cmd, msg)) {
269 g_string_free(cmd, TRUE);
273 g_string_append_c(cmd, ' ');
278 if (!parse_append_msgpart(cmd, msginfo,
280 g_string_free(cmd, TRUE);
287 g_string_append(cmd, sel_str);
292 g_string_append(cmd, user_str);
297 g_string_append(cmd, user_hidden_str);
301 g_string_append_c(cmd, p[0]);
302 g_string_append_c(cmd, p[1]);
306 g_string_append_c(cmd, p[0]);
311 g_string_free(cmd, TRUE);
316 g_string_free(cmd, FALSE);
320 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
324 g_return_val_if_fail(msginfo, FALSE);
326 filename = procmsg_get_message_file(msginfo);
329 g_string_append(cmd, filename);
332 alertpanel_error(_("Could not get message file %d"),
340 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
351 if ((fp = procmsg_open_message_decrypted(msginfo, &partinfo))
353 alertpanel_error(_("Could not get message file."));
357 if ((fp = procmsg_open_message(msginfo)) == NULL) {
358 alertpanel_error(_("Could not get message file."));
361 partinfo = procmime_scan_mime_header(fp);
365 procmime_mimeinfo_free_all(partinfo);
366 alertpanel_error(_("Could not get message part."));
369 filename = procmsg_get_message_file(msginfo);
371 if (!mimeview->opened) {
372 alertpanel_error(_("No message part selected."));
375 if (!mimeview->file) {
376 alertpanel_error(_("No message file selected."));
379 partinfo = gtk_ctree_node_get_row_data
380 (GTK_CTREE(mimeview->ctree),
382 g_return_val_if_fail(partinfo != NULL, FALSE);
383 filename = mimeview->file;
385 partname = procmime_get_tmp_file_name(partinfo);
387 ret = procmime_get_part(partname, filename, partinfo);
390 procmime_mimeinfo_free_all(partinfo);
395 alertpanel_error(_("Can't get part of multipart message"));
400 g_string_append(cmd, partname);
407 void actions_execute(gpointer data,
412 if (source == TOOLBAR_MAIN)
413 mainwin_actions_execute_cb((MainWindow*)data, action_nb, widget);
414 else if (source == TOOLBAR_COMPOSE)
415 compose_actions_execute_cb((Compose*)data, action_nb, widget);
416 else if (source == TOOLBAR_MSGVIEW)
417 msgview_actions_execute_cb((MessageView*)data, action_nb, widget);
420 void action_update_mainwin_menu(GtkItemFactory *ifactory, MainWindow *mainwin)
422 action_update_menu(ifactory, "/Tools/Actions",
423 mainwin_actions_execute_cb, mainwin);
426 void action_update_compose_menu(GtkItemFactory *ifactory, Compose *compose)
428 action_update_menu(ifactory, "/Tools/Actions",
429 compose_actions_execute_cb, compose);
432 static void action_update_menu(GtkItemFactory *ifactory, gchar *branch_path,
433 gpointer callback, gpointer data)
438 gchar *action, *action_p;
440 GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
442 ifentry.path = branch_path;
443 menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
444 g_return_if_fail(menuitem != NULL);
446 amenu = GTK_MENU_SHELL(menuitem)->children;
447 while (amenu != NULL) {
448 GList *alist = amenu->next;
449 gtk_widget_destroy(GTK_WIDGET(amenu->data));
453 ifentry.accelerator = NULL;
454 ifentry.callback_action = 0;
455 ifentry.callback = callback;
456 ifentry.item_type = NULL;
458 for (cur = prefs_common.actions_list; cur; cur = cur->next) {
459 action = g_strdup((gchar *)cur->data);
460 action_p = strstr(action, ": ");
461 if (action_p && action_p[2] &&
462 action_get_type(&action_p[2]) != ACTION_ERROR) {
464 menu_path = g_strdup_printf("%s/%s", branch_path,
466 ifentry.path = menu_path;
467 gtk_item_factory_create_item(ifactory, &ifentry, data,
472 ifentry.callback_action++;
476 static void compose_actions_execute_cb(Compose *compose, guint action_nb,
482 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
484 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
485 g_return_if_fail(buf != NULL);
486 action = strstr(buf, ": ");
487 g_return_if_fail(action != NULL);
489 /* Point to the beginning of the command-line */
492 action_type = action_get_type(action);
493 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
495 (_("The selected action cannot be used in the compose window\n"
496 "because it contains %%f, %%F or %%p."));
500 execute_actions(action, NULL, compose->text, NULL, 0, NULL);
503 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
506 message_actions_execute(mainwin->messageview, action_nb,
507 GTK_CTREE(mainwin->summaryview->ctree));
510 static void msgview_actions_execute_cb(MessageView *msgview, guint action_nb,
513 message_actions_execute(msgview, action_nb, NULL);
516 static void message_actions_execute(MessageView *msgview, guint action_nb,
519 TextView *textview = NULL;
520 MimeView *mimeview = NULL;
523 GtkWidget *text = NULL;
524 GdkFont *msgfont = NULL;
527 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
529 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
531 g_return_if_fail(buf);
532 g_return_if_fail(action = strstr(buf, ": "));
534 /* Point to the beginning of the command-line */
537 switch (msgview->type) {
539 if (msgview->textview && msgview->textview->text)
540 textview = msgview->textview;
543 if (msgview->mimeview) {
544 mimeview = msgview->mimeview;
545 if (msgview->mimeview->type == MIMEVIEW_TEXT &&
546 msgview->mimeview->textview &&
547 msgview->mimeview->textview->text)
548 textview = msgview->mimeview->textview;
554 text = textview->text;
555 msgfont = textview->msgfont;
556 body_pos = textview->body_pos;
559 execute_actions(action, ctree, text, msgfont, body_pos, mimeview);
562 static gboolean execute_actions(gchar *action, GtkCTree *ctree,
563 GtkWidget *text, GdkFont *msgfont,
564 gint body_pos, MimeView *mimeview)
566 GList *cur, *selection = NULL;
567 GSList *children_list = NULL;
569 gint selection_len = 0;
571 ChildInfo *child_info;
575 gchar *sel_str = NULL;
576 gchar *user_str = NULL;
577 gchar *user_hidden_str = NULL;
579 g_return_val_if_fail(action && *action, FALSE);
581 action_type = action_get_type(action);
583 if (action_type == ACTION_ERROR)
584 return FALSE; /* ERR: syntax error */
586 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) &&
587 !(ctree && GTK_CLIST(ctree)->selection))
588 return FALSE; /* ERR: file command without selection */
591 selection = GTK_CLIST(ctree)->selection;
592 selection_len = g_list_length(selection);
595 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
596 if (ctree && selection_len > 1)
597 return FALSE; /* ERR: pipe + multiple selection */
599 return FALSE; /* ERR: pipe and no displayed text */
602 if (action_type & ACTION_SELECTION_STR) {
604 return FALSE; /* ERR: selection string but no text */
606 guint start = 0, end = 0;
607 if (GTK_EDITABLE(text)->has_selection) {
608 start = GTK_EDITABLE(text)->selection_start_pos;
609 end = GTK_EDITABLE(text)->selection_end_pos;
617 sel_str = gtk_editable_get_chars(GTK_EDITABLE(text),
622 if (action_type & (ACTION_USER_STR))
623 if (!(user_str = get_user_string(action, ACTION_USER_STR)))
626 if (action_type & (ACTION_USER_HIDDEN_STR))
627 if (!(user_hidden_str =
628 get_user_string(action, ACTION_USER_HIDDEN_STR)))
631 children = g_new0(Children, 1);
633 if (action_type & ACTION_SINGLE) {
634 for (cur = selection; cur && is_ok == TRUE; cur = cur->next) {
635 msginfo = gtk_ctree_node_get_row_data(ctree,
636 GTK_CTREE_NODE(cur->data));
638 is_ok = FALSE; /* ERR: msginfo missing */
641 cmd = parse_action_cmd(action, msginfo, ctree,
643 user_hidden_str, sel_str);
645 debug_print("Action command error\n");
646 is_ok = FALSE; /* ERR: incorrect command */
649 if ((child_info = fork_child(cmd, action_type, text,
652 children_list = g_slist_append(children_list,
654 children->open_in = (selection_len == 1) ?
657 ACTION_USER_HIDDEN_IN)) : 0;
662 cmd = parse_action_cmd(action, NULL, ctree, mimeview, user_str,
663 user_hidden_str, sel_str);
665 if ((child_info = fork_child(cmd, action_type, text,
668 children_list = g_slist_append(children_list,
670 children->open_in = action_type &
672 ACTION_USER_HIDDEN_IN);
676 is_ok = FALSE; /* ERR: incorrect command */
680 g_free(user_hidden_str);
682 if (!children_list) {
683 /* If not waiting for children, return */
688 children->action = g_strdup(action);
689 children->dialog = NULL;
690 children->list = children_list;
691 children->nb = g_slist_length(children_list);
693 for (cur = children_list; cur; cur = cur->next) {
694 child_info = (ChildInfo *) cur->data;
695 child_info->tag_status =
696 gdk_input_add(child_info->chld_status,
698 catch_status, child_info);
701 create_io_dialog(children);
707 static ChildInfo *fork_child(gchar *cmd, gint action_type, GtkWidget *text,
708 GdkFont *msgfont, gint body_pos,
711 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
717 ChildInfo *child_info;
720 sync = !(action_type & ACTION_ASYNC);
722 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
723 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
726 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
728 alertpanel_error(_("Command could not be started. "
729 "Pipe creation failed.\n%s"),
731 /* Closing fd = -1 fails silently */
738 close(chld_status[0]);
739 close(chld_status[1]);
740 return NULL; /* Pipe error */
744 debug_print("Forking child and grandchild.\n");
747 if (pid == 0) { /* Child */
751 close(ConnectionNumber(gdk_display));
756 if (setpgid(0, getppid()))
762 ACTION_USER_HIDDEN_IN)) {
763 close(fileno(stdin));
769 close(fileno(stdout));
774 close(fileno(stderr));
784 execvp("/bin/sh", cmdline);
788 } else if (gch_pid < (pid_t) 0) { /* Fork error */
790 write(chld_status[1], "1\n", 2);
801 close(chld_status[0]);
804 debug_print("Child: Waiting for grandchild\n");
805 waitpid(gch_pid, NULL, 0);
806 debug_print("Child: grandchild ended\n");
807 write(chld_status[1], "0\n", 2);
808 close(chld_status[1]);
812 } else if (pid < 0) { /* Fork error */
813 alertpanel_error(_("Could not fork to execute the following "
815 cmd, g_strerror(errno));
822 waitpid(pid, NULL, 0);
827 if (!(action_type & (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
831 close(chld_status[1]);
833 child_info = g_new0(ChildInfo, 1);
835 child_info->children = children;
837 child_info->pid = pid;
838 child_info->cmd = g_strdup(cmd);
839 child_info->type = action_type;
840 child_info->new_out = FALSE;
841 child_info->output = g_string_new(NULL);
842 child_info->chld_in =
844 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
846 child_info->chld_out = chld_out[0];
847 child_info->chld_err = chld_err[0];
848 child_info->chld_status = chld_status[0];
849 child_info->tag_in = -1;
850 child_info->tag_out = gdk_input_add(chld_out[0], GDK_INPUT_READ,
851 catch_output, child_info);
852 child_info->tag_err = gdk_input_add(chld_err[0], GDK_INPUT_READ,
853 catch_output, child_info);
855 if (!(action_type & (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
858 child_info->text = text;
859 child_info->msgfont = msgfont;
862 end = gtk_stext_get_length(GTK_STEXT(text));
864 if (GTK_EDITABLE(text)->has_selection) {
865 start = GTK_EDITABLE(text)->selection_start_pos;
866 end = GTK_EDITABLE(text)->selection_end_pos;
876 end = gtk_stext_get_length(GTK_STEXT(text));
877 is_selection = FALSE;
881 selection = gtk_editable_get_chars(GTK_EDITABLE(text), start, end);
883 if (action_type & ACTION_PIPE_IN) {
884 write(chld_in[1], selection, strlen(selection));
885 if (!(action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
887 child_info->chld_in = -1; /* No more input */
891 gtk_stext_freeze(GTK_STEXT(text));
892 if (action_type & ACTION_PIPE_OUT) {
893 gtk_stext_set_point(GTK_STEXT(text), start);
894 gtk_stext_forward_delete(GTK_STEXT(text), end - start);
897 gtk_stext_thaw(GTK_STEXT(text));
902 static void kill_children_cb(GtkWidget *widget, gpointer data)
905 Children *children = (Children *) data;
906 ChildInfo *child_info;
908 for (cur = children->list; cur; cur = cur->next) {
909 child_info = (ChildInfo *)(cur->data);
910 debug_print("Killing child group id %d\n", child_info->pid);
911 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
916 static gint wait_for_children(Children *children)
919 ChildInfo *child_info;
921 gint nb = children->nb;
925 cur = children->list;
928 child_info = (ChildInfo *)cur->data;
931 new_output |= child_info->new_out;
935 children->output |= new_output;
937 if (new_output || (children->dialog && (nb != children->nb)))
938 update_io_dialog(children);
943 if (!children->dialog) {
944 free_children(children);
945 } else if (!children->output) {
946 gtk_widget_destroy(children->dialog);
952 static void send_input(GtkWidget *w, gpointer data)
954 Children *children = (Children *) data;
955 ChildInfo *child_info = (ChildInfo *) children->list->data;
957 child_info->tag_in = gdk_input_add(child_info->chld_in,
959 catch_input, children);
960 gtk_widget_set_sensitive(children->input_hbox, FALSE);
963 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
965 hide_io_dialog_cb(w, data);
969 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
972 Children *children = (Children *)data;
975 gtk_signal_disconnect_by_data(GTK_OBJECT(children->dialog),
977 gtk_widget_destroy(children->dialog);
978 free_children(children);
982 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
985 if (event && event->keyval == GDK_Escape)
986 hide_io_dialog_cb(widget, data);
990 static void childinfo_close_pipes(ChildInfo *child_info)
992 if (child_info->tag_in > 0)
993 gdk_input_remove(child_info->tag_in);
994 gdk_input_remove(child_info->tag_out);
995 gdk_input_remove(child_info->tag_err);
997 if (child_info->chld_in >= 0)
998 close(child_info->chld_in);
999 close(child_info->chld_out);
1000 close(child_info->chld_err);
1001 close(child_info->chld_status);
1004 static void free_children(Children *children)
1007 ChildInfo *child_info;
1009 debug_print("Freeing children data %p\n", children);
1011 g_free(children->action);
1012 for (cur = children->list; cur;) {
1013 child_info = (ChildInfo *)cur->data;
1014 g_free(child_info->cmd);
1015 g_string_free(child_info->output, TRUE);
1016 children->list = g_slist_remove(children->list, child_info);
1018 cur = children->list;
1023 static void update_io_dialog(Children *children)
1027 debug_print("Updating actions input/output dialog.\n");
1029 if (!children->nb) {
1030 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1031 gtk_widget_set_sensitive(children->close_btn, TRUE);
1032 if (children->input_hbox)
1033 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1034 gtk_widget_grab_focus(children->close_btn);
1035 gtk_signal_connect(GTK_OBJECT(children->dialog),
1037 GTK_SIGNAL_FUNC(io_dialog_key_pressed_cb),
1041 if (children->output) {
1042 GtkWidget *text = children->text;
1044 ChildInfo *child_info;
1046 gtk_widget_show(children->scrolledwin);
1047 gtk_text_freeze(GTK_TEXT(text));
1048 gtk_text_set_point(GTK_TEXT(text), 0);
1049 gtk_text_forward_delete(GTK_TEXT(text),
1050 gtk_text_get_length(GTK_TEXT(text)));
1051 for (cur = children->list; cur; cur = cur->next) {
1052 child_info = (ChildInfo *)cur->data;
1053 if (child_info->pid)
1054 caption = g_strdup_printf
1055 (_("--- Running: %s\n"),
1058 caption = g_strdup_printf
1059 (_("--- Ended: %s\n"),
1062 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1064 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1065 child_info->output->str, -1);
1067 child_info->new_out = FALSE;
1069 gtk_text_thaw(GTK_TEXT(text));
1073 static void create_io_dialog(Children *children)
1077 GtkWidget *entry = NULL;
1078 GtkWidget *input_hbox = NULL;
1079 GtkWidget *send_button;
1082 GtkWidget *scrolledwin;
1084 GtkWidget *abort_button;
1085 GtkWidget *close_button;
1087 debug_print("Creating action IO dialog\n");
1089 dialog = gtk_dialog_new();
1090 gtk_container_set_border_width
1091 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1092 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1093 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1094 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1095 manage_window_set_transient(GTK_WINDOW(dialog));
1096 gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
1097 GTK_SIGNAL_FUNC(delete_io_dialog_cb), children);
1098 gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
1099 GTK_SIGNAL_FUNC(hide_io_dialog_cb),
1102 vbox = gtk_vbox_new(FALSE, 8);
1103 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1104 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1105 gtk_widget_show(vbox);
1107 label = gtk_label_new(children->action);
1108 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1109 gtk_widget_show(label);
1111 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1112 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1113 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
1114 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1115 gtk_widget_set_usize(scrolledwin, 480, 200);
1116 gtk_widget_hide(scrolledwin);
1118 text = gtk_text_new(gtk_scrolled_window_get_hadjustment
1119 (GTK_SCROLLED_WINDOW(scrolledwin)),
1120 gtk_scrolled_window_get_vadjustment
1121 (GTK_SCROLLED_WINDOW(scrolledwin)));
1122 gtk_text_set_editable(GTK_TEXT(text), FALSE);
1123 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1124 gtk_widget_show(text);
1126 if (children->open_in) {
1127 input_hbox = gtk_hbox_new(FALSE, 8);
1128 gtk_widget_show(input_hbox);
1130 entry = gtk_entry_new();
1131 gtk_widget_set_usize(entry, 320, -1);
1132 gtk_signal_connect(GTK_OBJECT(entry), "activate",
1133 GTK_SIGNAL_FUNC(send_input), children);
1134 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1135 if (children->open_in & ACTION_USER_HIDDEN_IN)
1136 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1137 gtk_widget_show(entry);
1139 send_button = gtk_button_new_with_label(_(" Send "));
1140 gtk_signal_connect(GTK_OBJECT(send_button), "clicked",
1141 GTK_SIGNAL_FUNC(send_input), children);
1142 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1144 gtk_widget_show(send_button);
1146 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1147 gtk_widget_grab_focus(entry);
1150 gtkut_button_set_create(&hbox, &abort_button, _("Abort"),
1151 &close_button, _("Close"), NULL, NULL);
1152 gtk_signal_connect(GTK_OBJECT(abort_button), "clicked",
1153 GTK_SIGNAL_FUNC(kill_children_cb), children);
1154 gtk_signal_connect(GTK_OBJECT(close_button), "clicked",
1155 GTK_SIGNAL_FUNC(hide_io_dialog_cb), children);
1156 gtk_widget_show(hbox);
1159 gtk_widget_set_sensitive(close_button, FALSE);
1161 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1163 children->dialog = dialog;
1164 children->scrolledwin = scrolledwin;
1165 children->text = text;
1166 children->input_hbox = children->open_in ? input_hbox : NULL;
1167 children->input_entry = children->open_in ? entry : NULL;
1168 children->abort_btn = abort_button;
1169 children->close_btn = close_button;
1171 gtk_widget_show(dialog);
1174 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1176 ChildInfo *child_info = (ChildInfo *)data;
1180 gdk_input_remove(child_info->tag_status);
1182 c = read(source, &buf, 1);
1183 debug_print("Child returned %c\n", buf);
1185 waitpid(-child_info->pid, NULL, 0);
1186 childinfo_close_pipes(child_info);
1187 child_info->pid = 0;
1189 wait_for_children(child_info->children);
1192 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1194 Children *children = (Children *)data;
1195 ChildInfo *child_info = (ChildInfo *)children->list->data;
1199 debug_print("Sending input to grand child.\n");
1200 if (!(cond && GDK_INPUT_WRITE))
1203 gdk_input_remove(child_info->tag_in);
1204 child_info->tag_in = -1;
1206 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1208 len = strlen(input);
1212 c = write(child_info->chld_in, input + count, len - count);
1215 } while (c >= 0 && count < len);
1218 write(child_info->chld_in, "\n", 2);
1222 gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1223 gtk_widget_set_sensitive(children->input_hbox, TRUE);
1224 close(child_info->chld_in);
1225 child_info->chld_in = -1;
1226 debug_print("Input to grand child sent.\n");
1229 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1231 ChildInfo *child_info = (ChildInfo *)data;
1233 gchar buf[BUFFSIZE];
1235 debug_print("Catching grand child's output.\n");
1236 if (child_info->type & (ACTION_PIPE_OUT | ACTION_INSERT)
1237 && source == child_info->chld_out) {
1238 gboolean is_selection = FALSE;
1239 GtkWidget *text = child_info->text;
1240 if (GTK_EDITABLE(text)->has_selection)
1241 is_selection = TRUE;
1242 gtk_stext_freeze(GTK_STEXT(text));
1244 c = read(source, buf, sizeof(buf) - 1);
1247 gtk_stext_insert(GTK_STEXT(text), child_info->msgfont,
1248 NULL, NULL, buf, c);
1251 /* Using the select_region draws things. Should not.
1252 * so we just change selection position and
1253 * defere drawing when thawing. Hack?
1255 GTK_EDITABLE(text)->selection_end_pos =
1256 gtk_stext_get_point(GTK_STEXT(text));
1258 gtk_stext_thaw(GTK_STEXT(child_info->text));
1260 c = read(source, buf, sizeof(buf) - 1);
1261 for (i = 0; i < c; i++)
1262 g_string_append_c(child_info->output, buf[i]);
1264 child_info->new_out = TRUE;
1266 wait_for_children(child_info->children);
1269 static gboolean user_string_dialog_delete_cb(GtkWidget *widget,
1270 GdkEvent *event, gpointer data)
1275 static void user_string_dialog_destroy_cb(GtkWidget *widget, gpointer data)
1280 static gchar *get_user_string(const gchar *action, ActionType type)
1283 gchar *user_str = NULL;
1286 case ACTION_USER_HIDDEN_STR:
1287 message = g_strdup_printf
1288 (_("Enter the argument for the following action:\n"
1289 "(`%%h' will be replaced with the argument)\n"
1292 user_str = input_dialog_with_invisible
1293 (_("Action's hidden user argument"), message, NULL);
1295 case ACTION_USER_STR:
1296 message = g_strdup_printf
1297 (_("Enter the argument for the following action:\n"
1298 "(`%%u' will be replaced with the argument)\n"
1301 user_str = input_dialog
1302 (_("Action's user argument"), message, NULL);
1305 g_warning("Unsupported action type %d", type);