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;
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,
136 const gchar *msg_str,
139 static gint wait_for_children (Children *children);
141 static void free_children (Children *children);
143 static void childinfo_close_pipes (ChildInfo *child_info);
145 static void create_io_dialog (Children *children);
146 static void update_io_dialog (Children *children);
148 static void hide_io_dialog_cb (GtkWidget *widget,
150 static gint io_dialog_key_pressed_cb (GtkWidget *widget,
154 static void catch_output (gpointer data,
156 GdkInputCondition cond);
157 static void catch_input (gpointer data,
159 GdkInputCondition cond);
160 static void catch_status (gpointer data,
162 GdkInputCondition cond);
164 static gchar *get_user_string (const gchar *action,
168 ActionType action_get_type(const gchar *action_str)
171 ActionType action_type = ACTION_NONE;
173 g_return_val_if_fail(action_str, ACTION_ERROR);
174 g_return_val_if_fail(*action_str, ACTION_ERROR);
179 action_type |= ACTION_PIPE_IN;
181 } else if (p[0] == '>') {
182 action_type |= ACTION_USER_IN;
184 } else if (p[0] == '*') {
185 action_type |= ACTION_USER_HIDDEN_IN;
192 while (*p && action_type != ACTION_ERROR) {
196 action_type |= ACTION_SINGLE;
199 action_type |= ACTION_MULTIPLE;
202 action_type |= ACTION_SINGLE;
205 action_type |= ACTION_SELECTION_STR;
208 action_type |= ACTION_USER_STR;
211 action_type |= ACTION_USER_HIDDEN_STR;
214 action_type = ACTION_ERROR;
217 } else if (p[0] == '|') {
219 action_type |= ACTION_PIPE_OUT;
220 } else if (p[0] == '>') {
222 action_type |= ACTION_INSERT;
223 } else if (p[0] == '&') {
225 action_type |= ACTION_ASYNC;
233 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
234 GSList *msg_list, MimeInfo *partinfo,
235 const gchar *user_str,
236 const gchar *user_hidden_str,
237 const gchar *sel_str)
245 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
248 cmd = g_string_sized_new(strlen(action));
251 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
252 if (p[0] == '%' && p[1]) {
255 if (!parse_append_filename(cmd, msginfo)) {
256 g_string_free(cmd, TRUE);
262 for (cur = msg_list; cur != NULL;
264 MsgInfo *msg = (MsgInfo *)cur->data;
266 if (!parse_append_filename(cmd, msg)) {
267 g_string_free(cmd, TRUE);
271 g_string_append_c(cmd, ' ');
276 if (!parse_append_msgpart(cmd, msginfo,
278 g_string_free(cmd, TRUE);
285 g_string_append(cmd, sel_str);
290 g_string_append(cmd, user_str);
295 g_string_append(cmd, user_hidden_str);
299 g_string_append_c(cmd, p[0]);
300 g_string_append_c(cmd, p[1]);
304 g_string_append_c(cmd, p[0]);
309 g_string_free(cmd, TRUE);
314 g_string_free(cmd, FALSE);
318 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
322 g_return_val_if_fail(msginfo, FALSE);
324 filename = procmsg_get_message_file(msginfo);
327 g_string_append(cmd, filename);
330 alertpanel_error(_("Could not get message file %d"),
338 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
341 gboolean single_part = FALSE;
343 gchar *part_filename;
349 if ((fp = procmsg_open_message_decrypted(msginfo, &partinfo))
351 alertpanel_error(_("Could not get message file."));
355 if ((fp = procmsg_open_message(msginfo)) == NULL) {
356 alertpanel_error(_("Could not get message file."));
359 partinfo = procmime_scan_mime_header(fp);
363 procmime_mimeinfo_free_all(partinfo);
364 alertpanel_error(_("Could not get message part."));
371 filename = procmsg_get_message_file_path(msginfo);
372 part_filename = procmime_get_tmp_file_name(partinfo);
374 ret = procmime_get_part(part_filename, filename, partinfo);
377 procmime_mimeinfo_free_all(partinfo);
381 alertpanel_error(_("Can't get part of multipart message"));
382 g_free(part_filename);
386 g_string_append(cmd, part_filename);
388 g_free(part_filename);
393 void actions_execute(gpointer data,
398 if (source == TOOLBAR_MAIN)
399 mainwin_actions_execute_cb((MainWindow*)data, action_nb, widget);
400 else if (source == TOOLBAR_COMPOSE)
401 compose_actions_execute_cb((Compose*)data, action_nb, widget);
402 else if (source == TOOLBAR_MSGVIEW)
403 msgview_actions_execute_cb((MessageView*)data, action_nb, widget);
406 void action_update_mainwin_menu(GtkItemFactory *ifactory, MainWindow *mainwin)
408 action_update_menu(ifactory, "/Tools/Actions",
409 mainwin_actions_execute_cb, mainwin);
412 void action_update_msgview_menu(GtkItemFactory *ifactory, MessageView *msgview)
414 action_update_menu(ifactory, "/Tools/Actions",
415 msgview_actions_execute_cb, msgview);
418 void action_update_compose_menu(GtkItemFactory *ifactory, Compose *compose)
420 action_update_menu(ifactory, "/Tools/Actions",
421 compose_actions_execute_cb, compose);
424 static void action_update_menu(GtkItemFactory *ifactory, gchar *branch_path,
425 gpointer callback, gpointer data)
430 gchar *action, *action_p;
432 GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
434 ifentry.path = branch_path;
435 menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
436 g_return_if_fail(menuitem != NULL);
438 amenu = GTK_MENU_SHELL(menuitem)->children;
439 while (amenu != NULL) {
440 GList *alist = amenu->next;
441 gtk_widget_destroy(GTK_WIDGET(amenu->data));
445 ifentry.accelerator = NULL;
446 ifentry.callback_action = 0;
447 ifentry.callback = callback;
448 ifentry.item_type = NULL;
450 for (cur = prefs_common.actions_list; cur; cur = cur->next) {
451 action = g_strdup((gchar *)cur->data);
452 action_p = strstr(action, ": ");
453 if (action_p && action_p[2] &&
454 action_get_type(&action_p[2]) != ACTION_ERROR) {
456 menu_path = g_strdup_printf("%s/%s", branch_path,
458 ifentry.path = menu_path;
459 gtk_item_factory_create_item(ifactory, &ifentry, data,
464 ifentry.callback_action++;
468 static void compose_actions_execute_cb(Compose *compose, guint action_nb,
474 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
476 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
477 g_return_if_fail(buf != NULL);
478 action = strstr(buf, ": ");
479 g_return_if_fail(action != NULL);
481 /* Point to the beginning of the command-line */
484 action_type = action_get_type(action);
485 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
487 (_("The selected action cannot be used in the compose window\n"
488 "because it contains %%f, %%F or %%p."));
492 execute_actions(action, NULL, compose->text, NULL, 0, NULL);
495 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
500 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
501 message_actions_execute(mainwin->messageview, action_nb, msg_list);
502 g_slist_free(msg_list);
505 static void msgview_actions_execute_cb(MessageView *msgview, guint action_nb,
508 GSList *msg_list = NULL;
510 if (msgview->msginfo)
511 msg_list = g_slist_append(msg_list, msgview->msginfo);
512 message_actions_execute(msgview, action_nb, msg_list);
513 g_slist_free(msg_list);
516 static void message_actions_execute(MessageView *msgview, guint action_nb,
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 textview = messageview_get_current_textview(msgview);
539 text = textview->text;
540 msgfont = textview->msgfont;
541 body_pos = textview->body_pos;
543 partinfo = messageview_get_selected_mime_part(msgview);
545 execute_actions(action, msg_list, text, msgfont, body_pos, partinfo);
548 static gboolean execute_actions(gchar *action, GSList *msg_list,
549 GtkWidget *text, GdkFont *msgfont,
550 gint body_pos, MimeInfo *partinfo)
552 GSList *children_list = NULL;
556 ChildInfo *child_info;
560 guint start = 0, end = 0;
561 gchar *sel_str = NULL;
562 gchar *msg_str = NULL;
563 gchar *user_str = NULL;
564 gchar *user_hidden_str = NULL;
566 g_return_val_if_fail(action && *action, FALSE);
568 action_type = action_get_type(action);
570 if (action_type == ACTION_ERROR)
571 return FALSE; /* ERR: syntax error */
573 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
574 return FALSE; /* ERR: file command without selection */
576 msg_list_len = g_slist_length(msg_list);
578 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
579 if (msg_list_len > 1)
580 return FALSE; /* ERR: pipe + multiple selection */
582 return FALSE; /* ERR: pipe and no displayed text */
585 if (action_type & ACTION_SELECTION_STR) {
587 return FALSE; /* ERR: selection string but no text */
591 if (GTK_EDITABLE(text)->has_selection) {
592 start = GTK_EDITABLE(text)->selection_start_pos;
593 end = GTK_EDITABLE(text)->selection_end_pos;
603 end = gtk_stext_get_length(GTK_STEXT(text));
604 msg_str = gtk_editable_get_chars(GTK_EDITABLE(text),
607 sel_str = gtk_editable_get_chars(GTK_EDITABLE(text),
609 msg_str = g_strdup(sel_str);
613 end = gtk_stext_get_length(GTK_STEXT(text));
614 msg_str = gtk_editable_get_chars(GTK_EDITABLE(text),
619 if (action_type & ACTION_USER_STR) {
620 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
627 if (action_type & ACTION_USER_HIDDEN_STR) {
628 if (!(user_hidden_str =
629 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
637 if (action_type & ACTION_PIPE_OUT) {
638 gtk_stext_freeze(GTK_STEXT(text));
639 gtk_stext_set_point(GTK_STEXT(text), start);
640 gtk_stext_forward_delete(GTK_STEXT(text), end - start);
641 gtk_stext_thaw(GTK_STEXT(text));
644 children = g_new0(Children, 1);
646 children->msg_text = text;
647 children->msgfont = msgfont;
649 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
650 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
651 children->open_in = 1;
653 if (action_type & ACTION_SINGLE) {
656 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
657 msginfo = (MsgInfo *)cur->data;
659 is_ok = FALSE; /* ERR: msginfo missing */
662 cmd = parse_action_cmd(action, msginfo, msg_list,
664 user_hidden_str, sel_str);
666 debug_print("Action command error\n");
667 is_ok = FALSE; /* ERR: incorrect command */
670 if ((child_info = fork_child(cmd, action_type, msg_str,
672 children_list = g_slist_append(children_list,
678 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
679 user_str, user_hidden_str, sel_str);
681 if ((child_info = fork_child(cmd, action_type, msg_str,
683 children_list = g_slist_append(children_list,
688 is_ok = FALSE; /* ERR: incorrect command */
694 g_free(user_hidden_str);
696 if (!children_list) {
697 /* If not waiting for children, return */
702 children->action = g_strdup(action);
703 children->dialog = NULL;
704 children->list = children_list;
705 children->nb = g_slist_length(children_list);
707 for (cur = children_list; cur; cur = cur->next) {
708 child_info = (ChildInfo *) cur->data;
709 child_info->tag_status =
710 gdk_input_add(child_info->chld_status,
712 catch_status, child_info);
715 create_io_dialog(children);
721 static ChildInfo *fork_child(gchar *cmd, gint action_type,
722 const gchar *msg_str, Children *children)
724 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
727 ChildInfo *child_info;
730 sync = !(action_type & ACTION_ASYNC);
732 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
733 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
736 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
738 alertpanel_error(_("Command could not be started. "
739 "Pipe creation failed.\n%s"),
741 /* Closing fd = -1 fails silently */
748 close(chld_status[0]);
749 close(chld_status[1]);
750 return NULL; /* Pipe error */
754 debug_print("Forking child and grandchild.\n");
757 if (pid == 0) { /* Child */
761 close(ConnectionNumber(gdk_display));
766 if (setpgid(0, getppid()))
773 ACTION_USER_HIDDEN_IN)) {
774 close(fileno(stdin));
780 close(fileno(stdout));
785 close(fileno(stderr));
795 execvp("/bin/sh", cmdline);
799 } else if (gch_pid < (pid_t) 0) { /* Fork error */
801 write(chld_status[1], "1\n", 2);
812 close(chld_status[0]);
814 debug_print("Child: Waiting for grandchild\n");
815 waitpid(gch_pid, NULL, 0);
816 debug_print("Child: grandchild ended\n");
817 write(chld_status[1], "0\n", 2);
818 close(chld_status[1]);
822 } else if (pid < 0) { /* Fork error */
823 alertpanel_error(_("Could not fork to execute the following "
825 cmd, g_strerror(errno));
832 waitpid(pid, NULL, 0);
838 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
842 close(chld_status[1]);
844 child_info = g_new0(ChildInfo, 1);
846 child_info->children = children;
848 child_info->pid = pid;
849 child_info->cmd = g_strdup(cmd);
850 child_info->type = action_type;
851 child_info->new_out = FALSE;
852 child_info->output = g_string_new(NULL);
853 child_info->chld_in =
855 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
857 child_info->chld_out = chld_out[0];
858 child_info->chld_err = chld_err[0];
859 child_info->chld_status = chld_status[0];
860 child_info->tag_in = -1;
861 child_info->tag_out = gdk_input_add(chld_out[0], GDK_INPUT_READ,
862 catch_output, child_info);
863 child_info->tag_err = gdk_input_add(chld_err[0], GDK_INPUT_READ,
864 catch_output, child_info);
866 if (!(action_type & (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
869 if ((action_type & ACTION_PIPE_IN) && msg_str) {
870 write(chld_in[1], msg_str, strlen(msg_str));
871 if (!(action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
873 child_info->chld_in = -1; /* No more input */
879 static void kill_children_cb(GtkWidget *widget, gpointer data)
882 Children *children = (Children *) data;
883 ChildInfo *child_info;
885 for (cur = children->list; cur; cur = cur->next) {
886 child_info = (ChildInfo *)(cur->data);
887 debug_print("Killing child group id %d\n", child_info->pid);
888 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
893 static gint wait_for_children(Children *children)
896 ChildInfo *child_info;
898 gint nb = children->nb;
902 cur = children->list;
905 child_info = (ChildInfo *)cur->data;
908 new_output |= child_info->new_out;
912 children->output |= new_output;
914 if (new_output || (children->dialog && (nb != children->nb)))
915 update_io_dialog(children);
920 if (!children->dialog) {
921 free_children(children);
922 } else if (!children->output) {
923 gtk_widget_destroy(children->dialog);
929 static void send_input(GtkWidget *w, gpointer data)
931 Children *children = (Children *) data;
932 ChildInfo *child_info = (ChildInfo *) children->list->data;
934 child_info->tag_in = gdk_input_add(child_info->chld_in,
936 catch_input, children);
937 gtk_widget_set_sensitive(children->input_hbox, FALSE);
940 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
942 hide_io_dialog_cb(w, data);
946 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
949 Children *children = (Children *)data;
952 gtk_signal_disconnect_by_data(GTK_OBJECT(children->dialog),
954 gtk_widget_destroy(children->dialog);
955 free_children(children);
959 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
962 if (event && event->keyval == GDK_Escape)
963 hide_io_dialog_cb(widget, data);
967 static void childinfo_close_pipes(ChildInfo *child_info)
969 if (child_info->tag_in > 0)
970 gdk_input_remove(child_info->tag_in);
971 gdk_input_remove(child_info->tag_out);
972 gdk_input_remove(child_info->tag_err);
974 if (child_info->chld_in >= 0)
975 close(child_info->chld_in);
976 close(child_info->chld_out);
977 close(child_info->chld_err);
978 close(child_info->chld_status);
981 static void free_children(Children *children)
984 ChildInfo *child_info;
986 debug_print("Freeing children data %p\n", children);
988 g_free(children->action);
989 for (cur = children->list; cur;) {
990 child_info = (ChildInfo *)cur->data;
991 g_free(child_info->cmd);
992 g_string_free(child_info->output, TRUE);
993 children->list = g_slist_remove(children->list, child_info);
995 cur = children->list;
1000 static void update_io_dialog(Children *children)
1004 debug_print("Updating actions input/output dialog.\n");
1006 if (!children->nb) {
1007 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1008 gtk_widget_set_sensitive(children->close_btn, TRUE);
1009 if (children->input_hbox)
1010 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1011 gtk_widget_grab_focus(children->close_btn);
1012 gtk_signal_connect(GTK_OBJECT(children->dialog),
1014 GTK_SIGNAL_FUNC(io_dialog_key_pressed_cb),
1018 if (children->output) {
1019 GtkWidget *text = children->text;
1021 ChildInfo *child_info;
1023 gtk_widget_show(children->scrolledwin);
1024 gtk_text_freeze(GTK_TEXT(text));
1025 gtk_text_set_point(GTK_TEXT(text), 0);
1026 gtk_text_forward_delete(GTK_TEXT(text),
1027 gtk_text_get_length(GTK_TEXT(text)));
1028 for (cur = children->list; cur; cur = cur->next) {
1029 child_info = (ChildInfo *)cur->data;
1030 if (child_info->pid)
1031 caption = g_strdup_printf
1032 (_("--- Running: %s\n"),
1035 caption = g_strdup_printf
1036 (_("--- Ended: %s\n"),
1039 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1041 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1042 child_info->output->str, -1);
1044 child_info->new_out = FALSE;
1046 gtk_text_thaw(GTK_TEXT(text));
1050 static void create_io_dialog(Children *children)
1054 GtkWidget *entry = NULL;
1055 GtkWidget *input_hbox = NULL;
1056 GtkWidget *send_button;
1059 GtkWidget *scrolledwin;
1061 GtkWidget *abort_button;
1062 GtkWidget *close_button;
1064 debug_print("Creating action IO dialog\n");
1066 dialog = gtk_dialog_new();
1067 gtk_container_set_border_width
1068 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1069 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1070 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1071 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1072 manage_window_set_transient(GTK_WINDOW(dialog));
1073 gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
1074 GTK_SIGNAL_FUNC(delete_io_dialog_cb), children);
1075 gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
1076 GTK_SIGNAL_FUNC(hide_io_dialog_cb),
1079 vbox = gtk_vbox_new(FALSE, 8);
1080 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1081 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1082 gtk_widget_show(vbox);
1084 label = gtk_label_new(children->action);
1085 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1086 gtk_widget_show(label);
1088 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1089 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1090 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
1091 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1092 gtk_widget_set_usize(scrolledwin, 480, 200);
1093 gtk_widget_hide(scrolledwin);
1095 text = gtk_text_new(gtk_scrolled_window_get_hadjustment
1096 (GTK_SCROLLED_WINDOW(scrolledwin)),
1097 gtk_scrolled_window_get_vadjustment
1098 (GTK_SCROLLED_WINDOW(scrolledwin)));
1099 gtk_text_set_editable(GTK_TEXT(text), FALSE);
1100 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1101 gtk_widget_show(text);
1103 if (children->open_in) {
1104 input_hbox = gtk_hbox_new(FALSE, 8);
1105 gtk_widget_show(input_hbox);
1107 entry = gtk_entry_new();
1108 gtk_widget_set_usize(entry, 320, -1);
1109 gtk_signal_connect(GTK_OBJECT(entry), "activate",
1110 GTK_SIGNAL_FUNC(send_input), children);
1111 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1112 if (children->open_in & ACTION_USER_HIDDEN_IN)
1113 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1114 gtk_widget_show(entry);
1116 send_button = gtk_button_new_with_label(_(" Send "));
1117 gtk_signal_connect(GTK_OBJECT(send_button), "clicked",
1118 GTK_SIGNAL_FUNC(send_input), children);
1119 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1121 gtk_widget_show(send_button);
1123 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1124 gtk_widget_grab_focus(entry);
1127 gtkut_button_set_create(&hbox, &abort_button, _("Abort"),
1128 &close_button, _("Close"), NULL, NULL);
1129 gtk_signal_connect(GTK_OBJECT(abort_button), "clicked",
1130 GTK_SIGNAL_FUNC(kill_children_cb), children);
1131 gtk_signal_connect(GTK_OBJECT(close_button), "clicked",
1132 GTK_SIGNAL_FUNC(hide_io_dialog_cb), children);
1133 gtk_widget_show(hbox);
1136 gtk_widget_set_sensitive(close_button, FALSE);
1138 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1140 children->dialog = dialog;
1141 children->scrolledwin = scrolledwin;
1142 children->text = text;
1143 children->input_hbox = children->open_in ? input_hbox : NULL;
1144 children->input_entry = children->open_in ? entry : NULL;
1145 children->abort_btn = abort_button;
1146 children->close_btn = close_button;
1148 gtk_widget_show(dialog);
1151 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1153 ChildInfo *child_info = (ChildInfo *)data;
1157 gdk_input_remove(child_info->tag_status);
1159 c = read(source, &buf, 1);
1160 debug_print("Child returned %c\n", buf);
1162 waitpid(-child_info->pid, NULL, 0);
1163 childinfo_close_pipes(child_info);
1164 child_info->pid = 0;
1166 wait_for_children(child_info->children);
1169 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1171 Children *children = (Children *)data;
1172 ChildInfo *child_info = (ChildInfo *)children->list->data;
1176 debug_print("Sending input to grand child.\n");
1177 if (!(cond && GDK_INPUT_WRITE))
1180 gdk_input_remove(child_info->tag_in);
1181 child_info->tag_in = -1;
1183 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1185 len = strlen(input);
1189 c = write(child_info->chld_in, input + count, len - count);
1192 } while (c >= 0 && count < len);
1195 write(child_info->chld_in, "\n", 2);
1199 gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1200 gtk_widget_set_sensitive(children->input_hbox, TRUE);
1201 close(child_info->chld_in);
1202 child_info->chld_in = -1;
1203 debug_print("Input to grand child sent.\n");
1206 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1208 ChildInfo *child_info = (ChildInfo *)data;
1210 gchar buf[BUFFSIZE];
1212 debug_print("Catching grand child's output.\n");
1213 if (child_info->type & (ACTION_PIPE_OUT | ACTION_INSERT)
1214 && source == child_info->chld_out) {
1215 gboolean is_selection = FALSE;
1216 GtkWidget *text = child_info->children->msg_text;
1218 if (GTK_EDITABLE(text)->has_selection)
1219 is_selection = TRUE;
1220 gtk_stext_freeze(GTK_STEXT(text));
1222 c = read(source, buf, sizeof(buf) - 1);
1225 gtk_stext_insert(GTK_STEXT(text),
1226 child_info->children->msgfont,
1227 NULL, NULL, buf, c);
1230 /* Using the select_region draws things. Should not.
1231 * so we just change selection position and
1232 * defere drawing when thawing. Hack?
1234 GTK_EDITABLE(text)->selection_end_pos =
1235 gtk_stext_get_point(GTK_STEXT(text));
1237 gtk_stext_thaw(GTK_STEXT(text));
1239 c = read(source, buf, sizeof(buf) - 1);
1240 for (i = 0; i < c; i++)
1241 g_string_append_c(child_info->output, buf[i]);
1243 child_info->new_out = TRUE;
1245 wait_for_children(child_info->children);
1248 static gchar *get_user_string(const gchar *action, ActionType type)
1251 gchar *user_str = NULL;
1254 case ACTION_USER_HIDDEN_STR:
1255 message = g_strdup_printf
1256 (_("Enter the argument for the following action:\n"
1257 "(`%%h' will be replaced with the argument)\n"
1260 user_str = input_dialog_with_invisible
1261 (_("Action's hidden user argument"), message, NULL);
1263 case ACTION_USER_STR:
1264 message = g_strdup_printf
1265 (_("Enter the argument for the following action:\n"
1266 "(`%%u' will be replaced with the argument)\n"
1269 user_str = input_dialog
1270 (_("Action's user argument"), message, NULL);
1273 g_warning("Unsupported action type %d", type);