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;
65 GtkWidget *scrolledwin;
68 ActionType action_type;
96 static void action_update_menu (GtkItemFactory *ifactory,
100 static void compose_actions_execute_cb (Compose *compose,
103 static void mainwin_actions_execute_cb (MainWindow *mainwin,
106 static void msgview_actions_execute_cb (MessageView *msgview,
109 static void message_actions_execute (MessageView *msgview,
113 static gboolean execute_actions (gchar *action,
120 static gchar *parse_action_cmd (gchar *action,
124 const gchar *user_str,
125 const gchar *user_hidden_str,
126 const gchar *sel_str);
127 static gboolean parse_append_filename (GString *cmd,
130 static gboolean parse_append_msgpart (GString *cmd,
134 static ChildInfo *fork_child (gchar *cmd,
135 const gchar *msg_str,
138 static gint wait_for_children (Children *children);
140 static void free_children (Children *children);
142 static void childinfo_close_pipes (ChildInfo *child_info);
144 static void create_io_dialog (Children *children);
145 static void update_io_dialog (Children *children);
147 static void hide_io_dialog_cb (GtkWidget *widget,
149 static gint io_dialog_key_pressed_cb (GtkWidget *widget,
153 static void catch_output (gpointer data,
155 GdkInputCondition cond);
156 static void catch_input (gpointer data,
158 GdkInputCondition cond);
159 static void catch_status (gpointer data,
161 GdkInputCondition cond);
163 static gchar *get_user_string (const gchar *action,
167 ActionType action_get_type(const gchar *action_str)
170 ActionType action_type = ACTION_NONE;
172 g_return_val_if_fail(action_str, ACTION_ERROR);
173 g_return_val_if_fail(*action_str, ACTION_ERROR);
178 action_type |= ACTION_PIPE_IN;
180 } else if (p[0] == '>') {
181 action_type |= ACTION_USER_IN;
183 } else if (p[0] == '*') {
184 action_type |= ACTION_USER_HIDDEN_IN;
191 while (*p && action_type != ACTION_ERROR) {
195 action_type |= ACTION_SINGLE;
198 action_type |= ACTION_MULTIPLE;
201 action_type |= ACTION_SINGLE;
204 action_type |= ACTION_SELECTION_STR;
207 action_type |= ACTION_USER_STR;
210 action_type |= ACTION_USER_HIDDEN_STR;
213 action_type = ACTION_ERROR;
216 } else if (p[0] == '|') {
218 action_type |= ACTION_PIPE_OUT;
219 } else if (p[0] == '>') {
221 action_type |= ACTION_INSERT;
222 } else if (p[0] == '&') {
224 action_type |= ACTION_ASYNC;
232 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
233 GSList *msg_list, MimeInfo *partinfo,
234 const gchar *user_str,
235 const gchar *user_hidden_str,
236 const gchar *sel_str)
244 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
247 cmd = g_string_sized_new(strlen(action));
250 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
251 if (p[0] == '%' && p[1]) {
254 if (!parse_append_filename(cmd, msginfo)) {
255 g_string_free(cmd, TRUE);
261 for (cur = msg_list; cur != NULL;
263 MsgInfo *msg = (MsgInfo *)cur->data;
265 if (!parse_append_filename(cmd, msg)) {
266 g_string_free(cmd, TRUE);
270 g_string_append_c(cmd, ' ');
275 if (!parse_append_msgpart(cmd, msginfo,
277 g_string_free(cmd, TRUE);
284 g_string_append(cmd, sel_str);
289 g_string_append(cmd, user_str);
294 g_string_append(cmd, user_hidden_str);
298 g_string_append_c(cmd, p[0]);
299 g_string_append_c(cmd, p[1]);
303 g_string_append_c(cmd, p[0]);
308 g_string_free(cmd, TRUE);
313 g_string_free(cmd, FALSE);
317 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
321 g_return_val_if_fail(msginfo, FALSE);
323 filename = procmsg_get_message_file(msginfo);
326 g_string_append(cmd, filename);
329 alertpanel_error(_("Could not get message file %d"),
337 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
340 gboolean single_part = FALSE;
342 gchar *part_filename;
346 partinfo = procmime_scan_message(msginfo);
348 alertpanel_error(_("Could not get message part."));
355 filename = procmsg_get_message_file_path(msginfo);
356 part_filename = procmime_get_tmp_file_name(partinfo);
358 ret = procmime_get_part(part_filename, filename, partinfo);
361 procmime_mimeinfo_free_all(partinfo);
365 alertpanel_error(_("Can't get part of multipart message"));
366 g_free(part_filename);
370 g_string_append(cmd, part_filename);
372 g_free(part_filename);
377 void actions_execute(gpointer data,
382 if (source == TOOLBAR_MAIN)
383 mainwin_actions_execute_cb((MainWindow*)data, action_nb, widget);
384 else if (source == TOOLBAR_COMPOSE)
385 compose_actions_execute_cb((Compose*)data, action_nb, widget);
386 else if (source == TOOLBAR_MSGVIEW)
387 msgview_actions_execute_cb((MessageView*)data, action_nb, widget);
390 void action_update_mainwin_menu(GtkItemFactory *ifactory, MainWindow *mainwin)
392 action_update_menu(ifactory, "/Tools/Actions",
393 mainwin_actions_execute_cb, mainwin);
396 void action_update_msgview_menu(GtkItemFactory *ifactory, MessageView *msgview)
398 action_update_menu(ifactory, "/Tools/Actions",
399 msgview_actions_execute_cb, msgview);
402 void action_update_compose_menu(GtkItemFactory *ifactory, Compose *compose)
404 action_update_menu(ifactory, "/Tools/Actions",
405 compose_actions_execute_cb, compose);
408 static void action_update_menu(GtkItemFactory *ifactory, gchar *branch_path,
409 gpointer callback, gpointer data)
414 gchar *action, *action_p;
416 GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
418 ifentry.path = branch_path;
419 menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
420 g_return_if_fail(menuitem != NULL);
422 amenu = GTK_MENU_SHELL(menuitem)->children;
423 while (amenu != NULL) {
424 GList *alist = amenu->next;
425 gtk_widget_destroy(GTK_WIDGET(amenu->data));
429 ifentry.accelerator = NULL;
430 ifentry.callback_action = 0;
431 ifentry.callback = callback;
432 ifentry.item_type = NULL;
434 for (cur = prefs_common.actions_list; cur; cur = cur->next) {
435 action = g_strdup((gchar *)cur->data);
436 action_p = strstr(action, ": ");
437 if (action_p && action_p[2] &&
438 action_get_type(&action_p[2]) != ACTION_ERROR) {
440 menu_path = g_strdup_printf("%s/%s", branch_path,
442 ifentry.path = menu_path;
443 gtk_item_factory_create_item(ifactory, &ifentry, data,
448 ifentry.callback_action++;
452 static void compose_actions_execute_cb(Compose *compose, guint action_nb,
456 ActionType action_type;
458 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
460 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
461 g_return_if_fail(buf != NULL);
462 action = strstr(buf, ": ");
463 g_return_if_fail(action != NULL);
465 /* Point to the beginning of the command-line */
468 action_type = action_get_type(action);
469 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
471 (_("The selected action cannot be used in the compose window\n"
472 "because it contains %%f, %%F or %%p."));
476 execute_actions(action, NULL, compose->text, NULL, 0, NULL);
479 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
484 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
485 message_actions_execute(mainwin->messageview, action_nb, msg_list);
486 g_slist_free(msg_list);
489 static void msgview_actions_execute_cb(MessageView *msgview, guint action_nb,
492 GSList *msg_list = NULL;
494 if (msgview->msginfo)
495 msg_list = g_slist_append(msg_list, msgview->msginfo);
496 message_actions_execute(msgview, action_nb, msg_list);
497 g_slist_free(msg_list);
500 static void message_actions_execute(MessageView *msgview, guint action_nb,
507 GtkWidget *text = NULL;
508 GdkFont *msgfont = NULL;
510 ActionType action_type;
512 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
514 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
516 g_return_if_fail(buf);
517 g_return_if_fail(action = strstr(buf, ": "));
519 /* Point to the beginning of the command-line */
522 textview = messageview_get_current_textview(msgview);
524 text = textview->text;
525 msgfont = textview->msgfont;
526 body_pos = textview->body_pos;
528 partinfo = messageview_get_selected_mime_part(msgview);
530 /* this command will alter the message text */
531 action_type = action_get_type(action);
532 if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
533 msgview->filtered = TRUE;
535 execute_actions(action, msg_list, text, msgfont, body_pos, partinfo);
538 static gboolean execute_actions(gchar *action, GSList *msg_list,
539 GtkWidget *text, GdkFont *msgfont,
540 gint body_pos, MimeInfo *partinfo)
542 GSList *children_list = NULL;
546 ChildInfo *child_info;
547 ActionType action_type;
550 guint start = 0, end = 0;
551 gchar *sel_str = NULL;
552 gchar *msg_str = NULL;
553 gchar *user_str = NULL;
554 gchar *user_hidden_str = NULL;
556 g_return_val_if_fail(action && *action, FALSE);
558 action_type = action_get_type(action);
560 if (action_type == ACTION_ERROR)
561 return FALSE; /* ERR: syntax error */
563 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
564 return FALSE; /* ERR: file command without selection */
566 msg_list_len = g_slist_length(msg_list);
568 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
569 if (msg_list_len > 1)
570 return FALSE; /* ERR: pipe + multiple selection */
572 return FALSE; /* ERR: pipe and no displayed text */
575 if (action_type & ACTION_SELECTION_STR) {
577 return FALSE; /* ERR: selection string but no text */
581 if (GTK_EDITABLE(text)->has_selection) {
582 start = GTK_EDITABLE(text)->selection_start_pos;
583 end = GTK_EDITABLE(text)->selection_end_pos;
593 end = gtk_stext_get_length(GTK_STEXT(text));
594 msg_str = gtk_editable_get_chars
595 (GTK_EDITABLE(text), start, end);
597 sel_str = gtk_editable_get_chars
598 (GTK_EDITABLE(text), start, end);
599 msg_str = g_strdup(sel_str);
603 end = gtk_stext_get_length(GTK_STEXT(text));
604 msg_str = gtk_editable_get_chars(GTK_EDITABLE(text),
609 if (action_type & ACTION_USER_STR) {
610 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
617 if (action_type & ACTION_USER_HIDDEN_STR) {
618 if (!(user_hidden_str =
619 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
627 if (action_type & ACTION_PIPE_OUT) {
628 gtk_stext_freeze(GTK_STEXT(text));
629 gtk_stext_set_point(GTK_STEXT(text), start);
630 gtk_stext_forward_delete(GTK_STEXT(text), end - start);
631 gtk_stext_thaw(GTK_STEXT(text));
634 children = g_new0(Children, 1);
636 children->action = g_strdup(action);
637 children->action_type = action_type;
638 children->msg_text = text;
639 children->msgfont = msgfont;
641 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
642 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
643 children->open_in = 1;
645 if (action_type & ACTION_SINGLE) {
648 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
649 msginfo = (MsgInfo *)cur->data;
651 is_ok = FALSE; /* ERR: msginfo missing */
654 cmd = parse_action_cmd(action, msginfo, msg_list,
656 user_hidden_str, sel_str);
658 debug_print("Action command error\n");
659 is_ok = FALSE; /* ERR: incorrect command */
662 if ((child_info = fork_child(cmd, msg_str, children))) {
663 children_list = g_slist_append(children_list,
669 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
670 user_str, user_hidden_str, sel_str);
672 if ((child_info = fork_child(cmd, msg_str, children))) {
673 children_list = g_slist_append(children_list,
678 is_ok = FALSE; /* ERR: incorrect command */
684 g_free(user_hidden_str);
686 if (!children_list) {
687 /* If not waiting for children, return */
688 free_children(children);
692 children->list = children_list;
693 children->nb = g_slist_length(children_list);
695 for (cur = children_list; cur; cur = cur->next) {
696 child_info = (ChildInfo *) cur->data;
697 child_info->tag_status =
698 gdk_input_add(child_info->chld_status,
700 catch_status, child_info);
703 create_io_dialog(children);
709 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
712 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
715 ChildInfo *child_info;
718 sync = !(children->action_type & ACTION_ASYNC);
720 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
721 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
724 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
726 alertpanel_error(_("Command could not be started. "
727 "Pipe creation failed.\n%s"),
729 /* Closing fd = -1 fails silently */
736 close(chld_status[0]);
737 close(chld_status[1]);
738 return NULL; /* Pipe error */
742 debug_print("Forking child and grandchild.\n");
745 if (pid == 0) { /* Child */
749 close(ConnectionNumber(gdk_display));
754 if (setpgid(0, getppid()))
758 if (children->action_type &
761 ACTION_USER_HIDDEN_IN)) {
762 close(fileno(stdin));
768 close(fileno(stdout));
773 close(fileno(stderr));
783 execvp("/bin/sh", cmdline);
787 } else if (gch_pid < (pid_t) 0) { /* Fork error */
789 write(chld_status[1], "1\n", 2);
800 close(chld_status[0]);
802 debug_print("Child: Waiting for grandchild\n");
803 waitpid(gch_pid, NULL, 0);
804 debug_print("Child: grandchild ended\n");
805 write(chld_status[1], "0\n", 2);
806 close(chld_status[1]);
810 } else if (pid < 0) { /* Fork error */
811 alertpanel_error(_("Could not fork to execute the following "
813 cmd, g_strerror(errno));
820 waitpid(pid, NULL, 0);
825 if (!(children->action_type &
826 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
830 close(chld_status[1]);
832 child_info = g_new0(ChildInfo, 1);
834 child_info->children = children;
836 child_info->pid = pid;
837 child_info->cmd = g_strdup(cmd);
838 child_info->new_out = FALSE;
839 child_info->output = g_string_new(NULL);
840 child_info->chld_in =
841 (children->action_type &
842 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
844 child_info->chld_out = chld_out[0];
845 child_info->chld_err = chld_err[0];
846 child_info->chld_status = chld_status[0];
847 child_info->tag_in = -1;
848 child_info->tag_out = gdk_input_add(chld_out[0], GDK_INPUT_READ,
849 catch_output, child_info);
850 child_info->tag_err = gdk_input_add(chld_err[0], GDK_INPUT_READ,
851 catch_output, child_info);
853 if (!(children->action_type &
854 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
857 if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
858 write(chld_in[1], msg_str, strlen(msg_str));
859 if (!(children->action_type &
860 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
862 child_info->chld_in = -1; /* No more input */
868 static void kill_children_cb(GtkWidget *widget, gpointer data)
871 Children *children = (Children *) data;
872 ChildInfo *child_info;
874 for (cur = children->list; cur; cur = cur->next) {
875 child_info = (ChildInfo *)(cur->data);
876 debug_print("Killing child group id %d\n", child_info->pid);
877 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
882 static gint wait_for_children(Children *children)
885 ChildInfo *child_info;
887 gint nb = children->nb;
891 cur = children->list;
894 child_info = (ChildInfo *)cur->data;
897 new_output |= child_info->new_out;
901 children->output |= new_output;
903 if (new_output || (children->dialog && (nb != children->nb)))
904 update_io_dialog(children);
909 if (!children->dialog) {
910 free_children(children);
911 } else if (!children->output) {
912 gtk_widget_destroy(children->dialog);
918 static void send_input(GtkWidget *w, gpointer data)
920 Children *children = (Children *) data;
921 ChildInfo *child_info = (ChildInfo *) children->list->data;
923 child_info->tag_in = gdk_input_add(child_info->chld_in,
925 catch_input, children);
926 gtk_widget_set_sensitive(children->input_hbox, FALSE);
929 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
931 hide_io_dialog_cb(w, data);
935 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
938 Children *children = (Children *)data;
941 gtk_signal_disconnect_by_data(GTK_OBJECT(children->dialog),
943 gtk_widget_destroy(children->dialog);
944 free_children(children);
948 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
951 if (event && event->keyval == GDK_Escape)
952 hide_io_dialog_cb(widget, data);
956 static void childinfo_close_pipes(ChildInfo *child_info)
958 if (child_info->tag_in > 0)
959 gdk_input_remove(child_info->tag_in);
960 gdk_input_remove(child_info->tag_out);
961 gdk_input_remove(child_info->tag_err);
963 if (child_info->chld_in >= 0)
964 close(child_info->chld_in);
965 close(child_info->chld_out);
966 close(child_info->chld_err);
967 close(child_info->chld_status);
970 static void free_children(Children *children)
972 ChildInfo *child_info;
974 debug_print("Freeing children data %p\n", children);
976 g_free(children->action);
977 while (children->list != NULL) {
978 child_info = (ChildInfo *)children->list->data;
979 g_free(child_info->cmd);
980 g_string_free(child_info->output, TRUE);
981 children->list = g_slist_remove(children->list, child_info);
987 static void update_io_dialog(Children *children)
991 debug_print("Updating actions input/output dialog.\n");
994 gtk_widget_set_sensitive(children->abort_btn, FALSE);
995 gtk_widget_set_sensitive(children->close_btn, TRUE);
996 if (children->input_hbox)
997 gtk_widget_set_sensitive(children->input_hbox, FALSE);
998 gtk_widget_grab_focus(children->close_btn);
999 gtk_signal_connect(GTK_OBJECT(children->dialog),
1001 GTK_SIGNAL_FUNC(io_dialog_key_pressed_cb),
1005 if (children->output) {
1006 GtkWidget *text = children->text;
1008 ChildInfo *child_info;
1010 gtk_widget_show(children->scrolledwin);
1011 gtk_text_freeze(GTK_TEXT(text));
1012 gtk_text_set_point(GTK_TEXT(text), 0);
1013 gtk_text_forward_delete(GTK_TEXT(text),
1014 gtk_text_get_length(GTK_TEXT(text)));
1015 for (cur = children->list; cur; cur = cur->next) {
1016 child_info = (ChildInfo *)cur->data;
1017 if (child_info->pid)
1018 caption = g_strdup_printf
1019 (_("--- Running: %s\n"),
1022 caption = g_strdup_printf
1023 (_("--- Ended: %s\n"),
1026 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1028 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1029 child_info->output->str, -1);
1031 child_info->new_out = FALSE;
1033 gtk_text_thaw(GTK_TEXT(text));
1037 static void create_io_dialog(Children *children)
1041 GtkWidget *entry = NULL;
1042 GtkWidget *input_hbox = NULL;
1043 GtkWidget *send_button;
1046 GtkWidget *scrolledwin;
1048 GtkWidget *abort_button;
1049 GtkWidget *close_button;
1051 debug_print("Creating action IO dialog\n");
1053 dialog = gtk_dialog_new();
1054 gtk_container_set_border_width
1055 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1056 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1057 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1058 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1059 manage_window_set_transient(GTK_WINDOW(dialog));
1060 gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
1061 GTK_SIGNAL_FUNC(delete_io_dialog_cb), children);
1062 gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
1063 GTK_SIGNAL_FUNC(hide_io_dialog_cb),
1066 vbox = gtk_vbox_new(FALSE, 8);
1067 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1068 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1069 gtk_widget_show(vbox);
1071 label = gtk_label_new(children->action);
1072 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1073 gtk_widget_show(label);
1075 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1076 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1077 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
1078 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1079 gtk_widget_set_usize(scrolledwin, 480, 200);
1080 gtk_widget_hide(scrolledwin);
1082 text = gtk_text_new(gtk_scrolled_window_get_hadjustment
1083 (GTK_SCROLLED_WINDOW(scrolledwin)),
1084 gtk_scrolled_window_get_vadjustment
1085 (GTK_SCROLLED_WINDOW(scrolledwin)));
1086 gtk_text_set_editable(GTK_TEXT(text), FALSE);
1087 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1088 gtk_widget_show(text);
1090 if (children->open_in) {
1091 input_hbox = gtk_hbox_new(FALSE, 8);
1092 gtk_widget_show(input_hbox);
1094 entry = gtk_entry_new();
1095 gtk_widget_set_usize(entry, 320, -1);
1096 gtk_signal_connect(GTK_OBJECT(entry), "activate",
1097 GTK_SIGNAL_FUNC(send_input), children);
1098 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1099 if (children->action_type & ACTION_USER_HIDDEN_IN)
1100 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1101 gtk_widget_show(entry);
1103 send_button = gtk_button_new_with_label(_(" Send "));
1104 gtk_signal_connect(GTK_OBJECT(send_button), "clicked",
1105 GTK_SIGNAL_FUNC(send_input), children);
1106 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1108 gtk_widget_show(send_button);
1110 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1111 gtk_widget_grab_focus(entry);
1114 gtkut_button_set_create(&hbox, &abort_button, _("Abort"),
1115 &close_button, _("Close"), NULL, NULL);
1116 gtk_signal_connect(GTK_OBJECT(abort_button), "clicked",
1117 GTK_SIGNAL_FUNC(kill_children_cb), children);
1118 gtk_signal_connect(GTK_OBJECT(close_button), "clicked",
1119 GTK_SIGNAL_FUNC(hide_io_dialog_cb), children);
1120 gtk_widget_show(hbox);
1123 gtk_widget_set_sensitive(close_button, FALSE);
1125 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1127 children->dialog = dialog;
1128 children->scrolledwin = scrolledwin;
1129 children->text = text;
1130 children->input_hbox = children->open_in ? input_hbox : NULL;
1131 children->input_entry = children->open_in ? entry : NULL;
1132 children->abort_btn = abort_button;
1133 children->close_btn = close_button;
1135 gtk_widget_show(dialog);
1138 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1140 ChildInfo *child_info = (ChildInfo *)data;
1144 gdk_input_remove(child_info->tag_status);
1146 c = read(source, &buf, 1);
1147 debug_print("Child returned %c\n", buf);
1149 waitpid(-child_info->pid, NULL, 0);
1150 childinfo_close_pipes(child_info);
1151 child_info->pid = 0;
1153 wait_for_children(child_info->children);
1156 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1158 Children *children = (Children *)data;
1159 ChildInfo *child_info = (ChildInfo *)children->list->data;
1163 debug_print("Sending input to grand child.\n");
1164 if (!(cond && GDK_INPUT_WRITE))
1167 gdk_input_remove(child_info->tag_in);
1168 child_info->tag_in = -1;
1170 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1172 len = strlen(input);
1176 c = write(child_info->chld_in, input + count, len - count);
1179 } while (c >= 0 && count < len);
1182 write(child_info->chld_in, "\n", 2);
1186 gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1187 gtk_widget_set_sensitive(children->input_hbox, TRUE);
1188 close(child_info->chld_in);
1189 child_info->chld_in = -1;
1190 debug_print("Input to grand child sent.\n");
1193 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1195 ChildInfo *child_info = (ChildInfo *)data;
1197 gchar buf[BUFFSIZE];
1199 debug_print("Catching grand child's output.\n");
1200 if (child_info->children->action_type &
1201 (ACTION_PIPE_OUT | ACTION_INSERT)
1202 && source == child_info->chld_out) {
1203 gboolean is_selection = FALSE;
1204 GtkWidget *text = child_info->children->msg_text;
1206 if (GTK_EDITABLE(text)->has_selection)
1207 is_selection = TRUE;
1208 gtk_stext_freeze(GTK_STEXT(text));
1210 c = read(source, buf, sizeof(buf) - 1);
1213 gtk_stext_insert(GTK_STEXT(text),
1214 child_info->children->msgfont,
1215 NULL, NULL, buf, c);
1218 /* Using the select_region draws things. Should not.
1219 * so we just change selection position and
1220 * defere drawing when thawing. Hack?
1222 GTK_EDITABLE(text)->selection_end_pos =
1223 gtk_stext_get_point(GTK_STEXT(text));
1225 gtk_stext_thaw(GTK_STEXT(text));
1227 c = read(source, buf, sizeof(buf) - 1);
1228 for (i = 0; i < c; i++)
1229 g_string_append_c(child_info->output, buf[i]);
1231 child_info->new_out = TRUE;
1233 wait_for_children(child_info->children);
1236 static gchar *get_user_string(const gchar *action, ActionType type)
1239 gchar *user_str = NULL;
1242 case ACTION_USER_HIDDEN_STR:
1243 message = g_strdup_printf
1244 (_("Enter the argument for the following action:\n"
1245 "(`%%h' will be replaced with the argument)\n"
1248 user_str = input_dialog_with_invisible
1249 (_("Action's hidden user argument"), message, NULL);
1251 case ACTION_USER_STR:
1252 message = g_strdup_printf
1253 (_("Enter the argument for the following action:\n"
1254 "(`%%u' will be replaced with the argument)\n"
1257 user_str = input_dialog
1258 (_("Action's user argument"), message, NULL);
1261 g_warning("Unsupported action type %d", type);