2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 Hiroyuki Yamamoto & The Sylpheed Claws Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include <gdk/gdkkeysyms.h>
34 #include <sys/types.h>
42 #include "manage_window.h"
43 #include "mainwindow.h"
44 #include "prefs_common.h"
45 #include "alertpanel.h"
46 #include "inputdialog.h"
52 #include "matcher_parser.h" /* CLAWS */
53 #include "filtering.h"
55 typedef struct _Children Children;
56 typedef struct _ChildInfo ChildInfo;
57 typedef struct _UserStringDialog UserStringDialog;
63 GtkWidget *input_entry;
64 GtkWidget *input_hbox;
65 GtkWidget *progress_bar;
68 GtkWidget *scrolledwin;
71 ActionType action_type;
100 static void action_update_menu (GtkItemFactory *ifactory,
101 const gchar *branch_path,
104 static void compose_actions_execute_cb (Compose *compose,
107 static void mainwin_actions_execute_cb (MainWindow *mainwin,
110 static void msgview_actions_execute_cb (MessageView *msgview,
113 static void message_actions_execute (MessageView *msgview,
117 static gboolean execute_filtering_actions(gchar *action,
120 static gboolean execute_actions (gchar *action,
127 static gchar *parse_action_cmd (gchar *action,
131 const gchar *user_str,
132 const gchar *user_hidden_str,
133 const gchar *sel_str);
134 static gboolean parse_append_filename (GString *cmd,
137 static gboolean parse_append_msgpart (GString *cmd,
141 static ChildInfo *fork_child (gchar *cmd,
142 const gchar *msg_str,
145 static gint wait_for_children (Children *children);
147 static void free_children (Children *children);
149 static void childinfo_close_pipes (ChildInfo *child_info);
151 static void create_io_dialog (Children *children);
152 static void update_io_dialog (Children *children);
154 static void hide_io_dialog_cb (GtkWidget *widget,
156 static gint io_dialog_key_pressed_cb (GtkWidget *widget,
160 static void catch_output (gpointer data,
162 GdkInputCondition cond);
163 static void catch_input (gpointer data,
165 GdkInputCondition cond);
166 static void catch_status (gpointer data,
168 GdkInputCondition cond);
170 static gchar *get_user_string (const gchar *action,
174 ActionType action_get_type(const gchar *action_str)
177 ActionType action_type = ACTION_NONE;
179 g_return_val_if_fail(action_str, ACTION_ERROR);
180 g_return_val_if_fail(*action_str, ACTION_ERROR);
185 action_type |= ACTION_PIPE_IN;
187 } else if (p[0] == '>') {
188 action_type |= ACTION_USER_IN;
190 } else if (p[0] == '*') {
191 action_type |= ACTION_USER_HIDDEN_IN;
198 while (*p && action_type != ACTION_ERROR) {
199 if (p[0] == '%' && p[1]) {
202 /* CLAWS: filtering action is a mutually exclusive
203 * action. we can enable others if needed later. we
204 * add ACTION_SINGLE | ACTION_MULTIPLE so it will
205 * only be executed from the main window toolbar */
206 if (p[2] == 's') /* source messages */
207 action_type = ACTION_FILTERING_ACTION
212 action_type |= ACTION_SINGLE;
215 action_type |= ACTION_MULTIPLE;
218 action_type |= ACTION_SINGLE;
221 action_type |= ACTION_SELECTION_STR;
224 action_type |= ACTION_USER_STR;
227 action_type |= ACTION_USER_HIDDEN_STR;
230 action_type = ACTION_ERROR;
233 } else if (p[0] == '|') {
235 action_type |= ACTION_PIPE_OUT;
236 } else if (p[0] == '>') {
238 action_type |= ACTION_INSERT;
239 } else if (p[0] == '&') {
241 action_type |= ACTION_ASYNC;
249 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
250 GSList *msg_list, MimeInfo *partinfo,
251 const gchar *user_str,
252 const gchar *user_hidden_str,
253 const gchar *sel_str)
261 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
264 cmd = g_string_sized_new(strlen(action));
267 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
268 if (p[0] == '%' && p[1]) {
271 if (!parse_append_filename(cmd, msginfo)) {
272 g_string_free(cmd, TRUE);
278 for (cur = msg_list; cur != NULL;
280 MsgInfo *msg = (MsgInfo *)cur->data;
282 if (!parse_append_filename(cmd, msg)) {
283 g_string_free(cmd, TRUE);
287 g_string_append_c(cmd, ' ');
292 if (!parse_append_msgpart(cmd, msginfo,
294 g_string_free(cmd, TRUE);
301 g_string_append(cmd, sel_str);
306 g_string_append(cmd, user_str);
311 g_string_append(cmd, user_hidden_str);
315 g_string_append_c(cmd, p[0]);
316 g_string_append_c(cmd, p[1]);
320 g_string_append_c(cmd, p[0]);
325 g_string_free(cmd, TRUE);
330 g_string_free(cmd, FALSE);
334 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
338 g_return_val_if_fail(msginfo, FALSE);
340 filename = procmsg_get_message_file(msginfo);
343 g_string_append(cmd, filename);
346 alertpanel_error(_("Could not get message file %d"),
354 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
357 gboolean single_part = FALSE;
359 gchar *part_filename;
363 partinfo = procmime_scan_message(msginfo);
365 alertpanel_error(_("Could not get message part."));
372 filename = procmsg_get_message_file_path(msginfo);
373 part_filename = procmime_get_tmp_file_name(partinfo);
375 ret = procmime_get_part(part_filename, partinfo);
378 procmime_mimeinfo_free_all(partinfo);
382 alertpanel_error(_("Can't get part of multipart message"));
383 g_free(part_filename);
387 g_string_append(cmd, part_filename);
389 g_free(part_filename);
394 void actions_execute(gpointer data,
399 if (source == TOOLBAR_MAIN)
400 mainwin_actions_execute_cb((MainWindow*)data, action_nb, widget);
401 else if (source == TOOLBAR_COMPOSE)
402 compose_actions_execute_cb((Compose*)data, action_nb, widget);
403 else if (source == TOOLBAR_MSGVIEW)
404 msgview_actions_execute_cb((MessageView*)data, action_nb, widget);
407 void action_update_mainwin_menu(GtkItemFactory *ifactory,
408 const gchar *branch_path,
411 action_update_menu(ifactory, branch_path,
412 mainwin_actions_execute_cb, mainwin);
415 void action_update_msgview_menu(GtkItemFactory *ifactory,
416 const gchar *branch_path,
417 MessageView *msgview)
419 action_update_menu(ifactory, branch_path,
420 msgview_actions_execute_cb, msgview);
423 void action_update_compose_menu(GtkItemFactory *ifactory,
424 const gchar *branch_path,
427 action_update_menu(ifactory, branch_path,
428 compose_actions_execute_cb, compose);
431 static void action_update_menu(GtkItemFactory *ifactory,
432 const 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,
480 ActionType action_type;
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, %%as or %%p."));
500 execute_actions(action, NULL, compose->text, NULL, 0, NULL);
503 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
508 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
509 message_actions_execute(mainwin->messageview, action_nb, msg_list);
510 g_slist_free(msg_list);
513 static void msgview_actions_execute_cb(MessageView *msgview, guint action_nb,
516 GSList *msg_list = NULL;
518 if (msgview->msginfo)
519 msg_list = g_slist_append(msg_list, msgview->msginfo);
520 message_actions_execute(msgview, action_nb, msg_list);
521 g_slist_free(msg_list);
524 static void message_actions_execute(MessageView *msgview, guint action_nb,
531 GtkWidget *text = NULL;
532 GdkFont *msgfont = NULL;
534 ActionType action_type;
536 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
538 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
540 g_return_if_fail(buf);
541 g_return_if_fail(action = strstr(buf, ": "));
543 /* Point to the beginning of the command-line */
546 textview = messageview_get_current_textview(msgview);
548 text = textview->text;
549 msgfont = textview->msgfont;
550 body_pos = textview->body_pos;
552 partinfo = messageview_get_selected_mime_part(msgview);
554 /* this command will alter the message text */
555 action_type = action_get_type(action);
556 if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
557 msgview->filtered = TRUE;
559 if (action_type & ACTION_FILTERING_ACTION)
560 /* CLAWS: most of the above code is not necessary for applying
562 execute_filtering_actions(action, msg_list);
564 execute_actions(action, msg_list, text, msgfont, body_pos, partinfo);
567 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
569 GSList *action_list, *p;
570 const gchar *sbegin, *send;
571 gchar *action_string;
573 if (NULL == (sbegin = strstr2(action, "%as{")))
575 sbegin += sizeof "%as{" - 1;
576 if (NULL == (send = strrchr(sbegin, '}')))
578 action_string = g_strndup(sbegin, send - sbegin);
580 action_list = matcher_parser_get_action_list(action_string);
581 g_free(action_string);
582 if (action_list == NULL) return FALSE;
584 /* apply actions on each message info */
585 for (p = msglist; p && p->data; p = g_slist_next(p))
586 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
588 for (p = action_list; p; p = g_slist_next(p))
589 if (p->data) filteringaction_free(p->data);
590 g_slist_free(action_list);
594 static gboolean execute_actions(gchar *action, GSList *msg_list,
595 GtkWidget *text, GdkFont *msgfont,
596 gint body_pos, MimeInfo *partinfo)
598 GSList *children_list = NULL;
602 ChildInfo *child_info;
603 ActionType action_type;
606 guint start = 0, end = 0;
607 gchar *sel_str = NULL;
608 gchar *msg_str = NULL;
609 gchar *user_str = NULL;
610 gchar *user_hidden_str = NULL;
612 g_return_val_if_fail(action && *action, FALSE);
614 action_type = action_get_type(action);
616 if (action_type == ACTION_ERROR)
617 return FALSE; /* ERR: syntax error */
619 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
620 return FALSE; /* ERR: file command without selection */
622 msg_list_len = g_slist_length(msg_list);
624 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
625 if (msg_list_len > 1)
626 return FALSE; /* ERR: pipe + multiple selection */
628 return FALSE; /* ERR: pipe and no displayed text */
631 if (action_type & ACTION_SELECTION_STR) {
633 return FALSE; /* ERR: selection string but no text */
637 if (GTK_EDITABLE(text)->has_selection) {
638 start = GTK_EDITABLE(text)->selection_start_pos;
639 end = GTK_EDITABLE(text)->selection_end_pos;
649 end = gtk_stext_get_length(GTK_STEXT(text));
650 msg_str = gtk_editable_get_chars
651 (GTK_EDITABLE(text), start, end);
653 sel_str = gtk_editable_get_chars
654 (GTK_EDITABLE(text), start, end);
655 msg_str = g_strdup(sel_str);
659 end = gtk_stext_get_length(GTK_STEXT(text));
660 msg_str = gtk_editable_get_chars(GTK_EDITABLE(text),
665 if (action_type & ACTION_USER_STR) {
666 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
673 if (action_type & ACTION_USER_HIDDEN_STR) {
674 if (!(user_hidden_str =
675 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
683 if (action_type & ACTION_PIPE_OUT) {
684 gtk_stext_freeze(GTK_STEXT(text));
685 gtk_stext_set_point(GTK_STEXT(text), start);
686 gtk_stext_forward_delete(GTK_STEXT(text), end - start);
687 gtk_stext_thaw(GTK_STEXT(text));
690 children = g_new0(Children, 1);
692 children->action = g_strdup(action);
693 children->action_type = action_type;
694 children->msg_text = text;
695 children->msgfont = msgfont;
697 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
698 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
699 children->open_in = 1;
701 if (action_type & ACTION_SINGLE) {
704 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
705 msginfo = (MsgInfo *)cur->data;
707 is_ok = FALSE; /* ERR: msginfo missing */
710 cmd = parse_action_cmd(action, msginfo, msg_list,
712 user_hidden_str, sel_str);
714 debug_print("Action command error\n");
715 is_ok = FALSE; /* ERR: incorrect command */
718 if ((child_info = fork_child(cmd, msg_str, children))) {
719 children_list = g_slist_append(children_list,
725 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
726 user_str, user_hidden_str, sel_str);
728 if ((child_info = fork_child(cmd, msg_str, children))) {
729 children_list = g_slist_append(children_list,
734 is_ok = FALSE; /* ERR: incorrect command */
740 g_free(user_hidden_str);
742 if (!children_list) {
743 /* If not waiting for children, return */
744 free_children(children);
748 children->list = children_list;
749 children->nb = g_slist_length(children_list);
750 children->initial_nb = children->nb;
752 for (cur = children_list; cur; cur = cur->next) {
753 child_info = (ChildInfo *) cur->data;
754 child_info->tag_status =
755 gdk_input_add(child_info->chld_status,
757 catch_status, child_info);
760 create_io_dialog(children);
766 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
769 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
772 ChildInfo *child_info;
775 sync = !(children->action_type & ACTION_ASYNC);
777 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
778 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
781 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
783 alertpanel_error(_("Command could not be started. "
784 "Pipe creation failed.\n%s"),
786 /* Closing fd = -1 fails silently */
793 close(chld_status[0]);
794 close(chld_status[1]);
795 return NULL; /* Pipe error */
799 debug_print("Forking child and grandchild.\n");
802 if (pid == 0) { /* Child */
806 close(ConnectionNumber(gdk_display));
811 if (setpgid(0, getppid()))
815 if (children->action_type &
818 ACTION_USER_HIDDEN_IN)) {
819 close(fileno(stdin));
825 close(fileno(stdout));
830 close(fileno(stderr));
840 execvp("/bin/sh", cmdline);
844 } else if (gch_pid < (pid_t) 0) { /* Fork error */
846 write(chld_status[1], "1\n", 2);
857 close(chld_status[0]);
859 debug_print("Child: Waiting for grandchild\n");
860 waitpid(gch_pid, NULL, 0);
861 debug_print("Child: grandchild ended\n");
862 write(chld_status[1], "0\n", 2);
863 close(chld_status[1]);
867 } else if (pid < 0) { /* Fork error */
868 alertpanel_error(_("Could not fork to execute the following "
870 cmd, g_strerror(errno));
877 waitpid(pid, NULL, 0);
882 if (!(children->action_type &
883 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
887 close(chld_status[1]);
889 child_info = g_new0(ChildInfo, 1);
891 child_info->children = children;
893 child_info->pid = pid;
894 child_info->cmd = g_strdup(cmd);
895 child_info->new_out = FALSE;
896 child_info->output = g_string_new(NULL);
897 child_info->chld_in =
898 (children->action_type &
899 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
901 child_info->chld_out = chld_out[0];
902 child_info->chld_err = chld_err[0];
903 child_info->chld_status = chld_status[0];
904 child_info->tag_in = -1;
905 child_info->tag_out = gdk_input_add(chld_out[0], GDK_INPUT_READ,
906 catch_output, child_info);
907 child_info->tag_err = gdk_input_add(chld_err[0], GDK_INPUT_READ,
908 catch_output, child_info);
910 if (!(children->action_type &
911 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
914 if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
915 write(chld_in[1], msg_str, strlen(msg_str));
916 if (!(children->action_type &
917 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
919 child_info->chld_in = -1; /* No more input */
925 static void kill_children_cb(GtkWidget *widget, gpointer data)
928 Children *children = (Children *) data;
929 ChildInfo *child_info;
931 for (cur = children->list; cur; cur = cur->next) {
932 child_info = (ChildInfo *)(cur->data);
933 debug_print("Killing child group id %d\n", child_info->pid);
934 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
939 static gint wait_for_children(Children *children)
942 ChildInfo *child_info;
944 gint nb = children->nb;
948 cur = children->list;
951 child_info = (ChildInfo *)cur->data;
954 new_output |= child_info->new_out;
958 children->output |= new_output;
960 if (new_output || (children->dialog && (nb != children->nb)))
961 update_io_dialog(children);
966 if (!children->dialog) {
967 free_children(children);
968 } else if (!children->output) {
969 gtk_widget_destroy(children->dialog);
975 static void send_input(GtkWidget *w, gpointer data)
977 Children *children = (Children *) data;
978 ChildInfo *child_info = (ChildInfo *) children->list->data;
980 child_info->tag_in = gdk_input_add(child_info->chld_in,
982 catch_input, children);
983 gtk_widget_set_sensitive(children->input_hbox, FALSE);
986 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
988 hide_io_dialog_cb(w, data);
992 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
995 Children *children = (Children *)data;
998 gtk_signal_disconnect_by_data(GTK_OBJECT(children->dialog),
1000 gtk_widget_destroy(children->dialog);
1001 free_children(children);
1005 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1008 if (event && event->keyval == GDK_Escape)
1009 hide_io_dialog_cb(widget, data);
1013 static void childinfo_close_pipes(ChildInfo *child_info)
1015 /* stdout and stderr pipes are guaranteed to be removed by
1016 * their handler, but in case where we receive child exit notification
1017 * before grand-child's pipes closing signals, we check them and close
1020 if (child_info->tag_in > 0)
1021 gdk_input_remove(child_info->tag_in);
1022 if (child_info->tag_out > 0)
1023 gdk_input_remove(child_info->tag_out);
1024 if (child_info->tag_err > 0)
1025 gdk_input_remove(child_info->tag_err);
1027 if (child_info->chld_in >= 0)
1028 close(child_info->chld_in);
1029 if (child_info->chld_out >= 0)
1030 close(child_info->chld_out);
1031 if (child_info->chld_err >= 0)
1032 close(child_info->chld_err);
1034 close(child_info->chld_status);
1037 static void free_children(Children *children)
1039 ChildInfo *child_info;
1041 debug_print("Freeing children data %p\n", children);
1043 g_free(children->action);
1044 while (children->list != NULL) {
1045 child_info = (ChildInfo *)children->list->data;
1046 g_free(child_info->cmd);
1047 g_string_free(child_info->output, TRUE);
1048 children->list = g_slist_remove(children->list, child_info);
1054 static void update_io_dialog(Children *children)
1058 debug_print("Updating actions input/output dialog.\n");
1060 if (children->progress_bar) {
1061 gtk_progress_configure(GTK_PROGRESS(children->progress_bar),
1062 children->initial_nb -children->nb,
1063 0.0, children->initial_nb);
1066 if (!children->nb) {
1067 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1068 gtk_widget_set_sensitive(children->close_btn, TRUE);
1069 if (children->input_hbox)
1070 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1071 gtk_widget_grab_focus(children->close_btn);
1072 gtk_signal_connect(GTK_OBJECT(children->dialog),
1074 GTK_SIGNAL_FUNC(io_dialog_key_pressed_cb),
1078 if (children->output) {
1079 GtkWidget *text = children->text;
1081 ChildInfo *child_info;
1084 font = gtk_object_get_data(GTK_OBJECT(children->dialog),
1086 gtk_widget_show(children->scrolledwin);
1087 gtk_text_freeze(GTK_TEXT(text));
1088 gtk_text_set_point(GTK_TEXT(text), 0);
1089 gtk_text_forward_delete(GTK_TEXT(text),
1090 gtk_text_get_length(GTK_TEXT(text)));
1091 for (cur = children->list; cur; cur = cur->next) {
1092 child_info = (ChildInfo *)cur->data;
1093 if (child_info->pid)
1094 caption = g_strdup_printf
1095 (_("--- Running: %s\n"),
1098 caption = g_strdup_printf
1099 (_("--- Ended: %s\n"),
1102 gtk_text_insert(GTK_TEXT(text), font, NULL, NULL,
1104 gtk_text_insert(GTK_TEXT(text), font, NULL, NULL,
1105 child_info->output->str, -1);
1107 child_info->new_out = FALSE;
1109 gtk_text_thaw(GTK_TEXT(text));
1113 static void create_io_dialog(Children *children)
1117 GtkWidget *entry = NULL;
1118 GtkWidget *input_hbox = NULL;
1119 GtkWidget *send_button;
1122 GtkWidget *scrolledwin;
1124 GtkWidget *progress_bar = NULL;
1125 GtkWidget *abort_button;
1126 GtkWidget *close_button;
1127 GdkFont *output_font;
1129 debug_print("Creating action IO dialog\n");
1131 dialog = gtk_dialog_new();
1132 gtk_container_set_border_width
1133 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1134 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1135 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1136 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1137 manage_window_set_transient(GTK_WINDOW(dialog));
1138 gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
1139 GTK_SIGNAL_FUNC(delete_io_dialog_cb), children);
1140 gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
1141 GTK_SIGNAL_FUNC(hide_io_dialog_cb),
1144 vbox = gtk_vbox_new(FALSE, 8);
1145 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1146 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1147 gtk_widget_show(vbox);
1149 label = gtk_label_new(children->action);
1150 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1151 gtk_widget_show(label);
1153 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1154 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1155 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
1156 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1157 gtk_widget_set_usize(scrolledwin, 480, 200);
1158 gtk_widget_hide(scrolledwin);
1160 text = gtk_text_new(gtk_scrolled_window_get_hadjustment
1161 (GTK_SCROLLED_WINDOW(scrolledwin)),
1162 gtk_scrolled_window_get_vadjustment
1163 (GTK_SCROLLED_WINDOW(scrolledwin)));
1164 gtk_text_set_editable(GTK_TEXT(text), FALSE);
1165 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1166 gtk_widget_show(text);
1168 if (children->open_in) {
1169 input_hbox = gtk_hbox_new(FALSE, 8);
1170 gtk_widget_show(input_hbox);
1172 entry = gtk_entry_new();
1173 gtk_widget_set_usize(entry, 320, -1);
1174 gtk_signal_connect(GTK_OBJECT(entry), "activate",
1175 GTK_SIGNAL_FUNC(send_input), children);
1176 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1177 if (children->action_type & ACTION_USER_HIDDEN_IN)
1178 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1179 gtk_widget_show(entry);
1181 send_button = gtk_button_new_with_label(_(" Send "));
1182 gtk_signal_connect(GTK_OBJECT(send_button), "clicked",
1183 GTK_SIGNAL_FUNC(send_input), children);
1184 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1186 gtk_widget_show(send_button);
1188 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1189 gtk_widget_grab_focus(entry);
1192 if (children->initial_nb > 1) {
1193 progress_bar = gtk_progress_bar_new();
1194 gtk_progress_bar_set_bar_style(GTK_PROGRESS_BAR(progress_bar),
1195 GTK_PROGRESS_CONTINUOUS);
1196 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1197 GTK_PROGRESS_LEFT_TO_RIGHT);
1198 gtk_progress_set_activity_mode(GTK_PROGRESS(progress_bar),
1200 gtk_progress_set_format_string(GTK_PROGRESS(progress_bar),
1201 _("Completed %v/%u"));
1202 gtk_progress_set_show_text(GTK_PROGRESS(progress_bar), TRUE);
1203 gtk_progress_configure(GTK_PROGRESS(progress_bar),
1204 children->initial_nb -children->nb,
1205 0.0, children->initial_nb);
1207 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1208 gtk_widget_show(progress_bar);
1211 gtkut_button_set_create(&hbox, &abort_button, _("Abort"),
1212 &close_button, _("Close"), NULL, NULL);
1213 gtk_signal_connect(GTK_OBJECT(abort_button), "clicked",
1214 GTK_SIGNAL_FUNC(kill_children_cb), children);
1215 gtk_signal_connect(GTK_OBJECT(close_button), "clicked",
1216 GTK_SIGNAL_FUNC(hide_io_dialog_cb), children);
1217 gtk_widget_show(hbox);
1220 gtk_widget_set_sensitive(close_button, FALSE);
1222 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1224 children->dialog = dialog;
1225 children->scrolledwin = scrolledwin;
1226 children->text = text;
1227 children->input_hbox = children->open_in ? input_hbox : NULL;
1228 children->input_entry = children->open_in ? entry : NULL;
1229 children->progress_bar = progress_bar;
1230 children->abort_btn = abort_button;
1231 children->close_btn = close_button;
1233 output_font = gtkut_font_load_from_fontset(prefs_common.textfont);
1234 gtk_object_set_data_full(GTK_OBJECT(dialog), "s_txtfont",
1236 (GtkDestroyNotify)gdk_font_unref);
1237 gtk_widget_show(dialog);
1240 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1242 ChildInfo *child_info = (ChildInfo *)data;
1246 gdk_input_remove(child_info->tag_status);
1248 c = read(source, &buf, 1);
1249 debug_print("Child returned %c\n", buf);
1251 waitpid(-child_info->pid, NULL, 0);
1252 childinfo_close_pipes(child_info);
1253 child_info->pid = 0;
1255 wait_for_children(child_info->children);
1258 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1260 Children *children = (Children *)data;
1261 ChildInfo *child_info = (ChildInfo *)children->list->data;
1265 debug_print("Sending input to grand child.\n");
1266 if (!(cond && GDK_INPUT_WRITE))
1269 gdk_input_remove(child_info->tag_in);
1270 child_info->tag_in = -1;
1272 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1274 len = strlen(input);
1278 c = write(child_info->chld_in, input + count, len - count);
1281 } while (c >= 0 && count < len);
1284 write(child_info->chld_in, "\n", 2);
1288 gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1289 gtk_widget_set_sensitive(children->input_hbox, TRUE);
1290 close(child_info->chld_in);
1291 child_info->chld_in = -1;
1292 debug_print("Input to grand child sent.\n");
1295 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1297 ChildInfo *child_info = (ChildInfo *)data;
1299 gchar buf[BUFFSIZE];
1301 debug_print("Catching grand child's output.\n");
1302 if (child_info->children->action_type &
1303 (ACTION_PIPE_OUT | ACTION_INSERT)
1304 && source == child_info->chld_out) {
1305 gboolean is_selection = FALSE;
1306 GtkWidget *text = child_info->children->msg_text;
1308 if (GTK_EDITABLE(text)->has_selection)
1309 is_selection = TRUE;
1310 gtk_stext_freeze(GTK_STEXT(text));
1312 c = read(source, buf, sizeof(buf) - 1);
1315 gtk_stext_insert(GTK_STEXT(text),
1316 child_info->children->msgfont,
1317 NULL, NULL, buf, c);
1320 /* Using the select_region draws things. Should not.
1321 * so we just change selection position and
1322 * defere drawing when thawing. Hack?
1324 GTK_EDITABLE(text)->selection_end_pos =
1325 gtk_stext_get_point(GTK_STEXT(text));
1327 gtk_stext_thaw(GTK_STEXT(text));
1329 c = read(source, buf, sizeof(buf) - 1);
1330 for (i = 0; i < c; i++)
1331 g_string_append_c(child_info->output, buf[i]);
1333 child_info->new_out = TRUE;
1336 if (source == child_info->chld_out) {
1337 gdk_input_remove(child_info->tag_out);
1338 child_info->tag_out = -1;
1339 close(child_info->chld_out);
1340 child_info->chld_out = -1;
1342 gdk_input_remove(child_info->tag_err);
1343 child_info->tag_err = -1;
1344 close(child_info->chld_err);
1345 child_info->chld_err = -1;
1349 wait_for_children(child_info->children);
1352 static gchar *get_user_string(const gchar *action, ActionType type)
1355 gchar *user_str = NULL;
1358 case ACTION_USER_HIDDEN_STR:
1359 message = g_strdup_printf
1360 (_("Enter the argument for the following action:\n"
1361 "(`%%h' will be replaced with the argument)\n"
1364 user_str = input_dialog_with_invisible
1365 (_("Action's hidden user argument"), message, NULL);
1367 case ACTION_USER_STR:
1368 message = g_strdup_printf
1369 (_("Enter the argument for the following action:\n"
1370 "(`%%u' will be replaced with the argument)\n"
1373 user_str = input_dialog
1374 (_("Action's user argument"), message, NULL);
1377 g_warning("Unsupported action type %d", type);