2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2004 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>
29 #ifdef GDK_WINDOWING_X11
30 # include <gdk/gdkx.h>
31 #endif /* GDK_WINDOWING_X11 */
36 #include <sys/types.h>
44 #include "manage_window.h"
45 #include "mainwindow.h"
46 #include "prefs_common.h"
47 #include "alertpanel.h"
48 #include "inputdialog.h"
53 #include "matcher_parser.h" /* CLAWS */
54 #include "filtering.h"
56 typedef struct _Children Children;
57 typedef struct _ChildInfo ChildInfo;
58 typedef struct _UserStringDialog UserStringDialog;
64 GtkWidget *input_entry;
65 GtkWidget *input_hbox;
66 GtkWidget *progress_bar;
69 GtkWidget *scrolledwin;
72 ActionType action_type;
81 gboolean is_selection;
102 static void action_update_menu (GtkItemFactory *ifactory,
106 static void compose_actions_execute_cb (Compose *compose,
109 static void mainwin_actions_execute_cb (MainWindow *mainwin,
112 static void msgview_actions_execute_cb (MessageView *msgview,
115 static void message_actions_execute (MessageView *msgview,
119 static gboolean execute_filtering_actions(gchar *action,
122 static gboolean execute_actions (gchar *action,
128 static gchar *parse_action_cmd (gchar *action,
132 const gchar *user_str,
133 const gchar *user_hidden_str,
134 const gchar *sel_str);
135 static gboolean parse_append_filename (GString *cmd,
138 static gboolean parse_append_msgpart (GString *cmd,
142 static ChildInfo *fork_child (gchar *cmd,
143 const gchar *msg_str,
146 static gint wait_for_children (Children *children);
148 static void free_children (Children *children);
150 static void childinfo_close_pipes (ChildInfo *child_info);
152 static void create_io_dialog (Children *children);
153 static void update_io_dialog (Children *children);
155 static void hide_io_dialog_cb (GtkWidget *widget,
157 static gint io_dialog_key_pressed_cb (GtkWidget *widget,
161 static void catch_output (gpointer data,
163 GdkInputCondition cond);
164 static void catch_input (gpointer data,
166 GdkInputCondition cond);
167 static void catch_status (gpointer data,
169 GdkInputCondition cond);
171 static gchar *get_user_string (const gchar *action,
175 ActionType action_get_type(const gchar *action_str)
178 ActionType action_type = ACTION_NONE;
180 g_return_val_if_fail(action_str, ACTION_ERROR);
181 g_return_val_if_fail(*action_str, ACTION_ERROR);
186 action_type |= ACTION_PIPE_IN;
188 } else if (p[0] == '>') {
189 action_type |= ACTION_USER_IN;
191 } else if (p[0] == '*') {
192 action_type |= ACTION_USER_HIDDEN_IN;
199 while (*p && action_type != ACTION_ERROR) {
200 if (p[0] == '%' && p[1]) {
203 /* CLAWS: filtering action is a mutually exclusive
204 * action. we can enable others if needed later. we
205 * add ACTION_SINGLE | ACTION_MULTIPLE so it will
206 * only be executed from the main window toolbar */
207 if (p[2] == 's') /* source messages */
208 action_type = ACTION_FILTERING_ACTION
213 action_type |= ACTION_SINGLE;
216 action_type |= ACTION_MULTIPLE;
219 action_type |= ACTION_SINGLE;
222 action_type |= ACTION_SELECTION_STR;
225 action_type |= ACTION_USER_STR;
228 action_type |= ACTION_USER_HIDDEN_STR;
231 action_type = ACTION_ERROR;
234 } else if (p[0] == '|') {
236 action_type |= ACTION_PIPE_OUT;
237 } else if (p[0] == '>') {
239 action_type |= ACTION_INSERT;
240 } else if (p[0] == '&') {
242 action_type |= ACTION_ASYNC;
250 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
251 GSList *msg_list, MimeInfo *partinfo,
252 const gchar *user_str,
253 const gchar *user_hidden_str,
254 const gchar *sel_str)
262 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
265 cmd = g_string_sized_new(strlen(action));
268 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
269 if (p[0] == '%' && p[1]) {
272 if (!parse_append_filename(cmd, msginfo)) {
273 g_string_free(cmd, TRUE);
279 for (cur = msg_list; cur != NULL;
281 MsgInfo *msg = (MsgInfo *)cur->data;
283 if (!parse_append_filename(cmd, msg)) {
284 g_string_free(cmd, TRUE);
288 g_string_append_c(cmd, ' ');
293 if (!parse_append_msgpart(cmd, msginfo,
295 g_string_free(cmd, TRUE);
302 g_string_append(cmd, sel_str);
307 g_string_append(cmd, user_str);
312 g_string_append(cmd, user_hidden_str);
316 g_string_append_c(cmd, p[0]);
317 g_string_append_c(cmd, p[1]);
321 g_string_append_c(cmd, p[0]);
326 g_string_free(cmd, TRUE);
331 g_string_free(cmd, FALSE);
335 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
339 gchar escape_ch[] = "\\ ";
341 g_return_val_if_fail(msginfo, FALSE);
343 filename = procmsg_get_message_file(msginfo);
346 alertpanel_error(_("Could not get message file %d"),
352 while ((q = strpbrk(p, "$\"`'\\ \t*?[]&|;<>()!#~")) != NULL) {
355 g_string_append(cmd, p);
356 g_string_append(cmd, escape_ch);
359 g_string_append(cmd, p);
366 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
369 gboolean single_part = FALSE;
371 gchar *part_filename;
375 partinfo = procmime_scan_message(msginfo);
377 alertpanel_error(_("Could not get message part."));
384 filename = procmsg_get_message_file_path(msginfo);
385 part_filename = procmime_get_tmp_file_name(partinfo);
387 ret = procmime_get_part(part_filename, partinfo);
390 procmime_mimeinfo_free_all(partinfo);
394 alertpanel_error(_("Can't get part of multipart message"));
395 g_free(part_filename);
399 g_string_append(cmd, part_filename);
401 g_free(part_filename);
406 void actions_execute(gpointer data,
411 if (source == TOOLBAR_MAIN)
412 mainwin_actions_execute_cb((MainWindow*)data, action_nb, widget);
413 else if (source == TOOLBAR_COMPOSE)
414 compose_actions_execute_cb((Compose*)data, action_nb, widget);
415 else if (source == TOOLBAR_MSGVIEW)
416 msgview_actions_execute_cb((MessageView*)data, action_nb, widget);
419 void action_update_mainwin_menu(GtkItemFactory *ifactory,
423 action_update_menu(ifactory, branch_path,
424 mainwin_actions_execute_cb, mainwin);
427 void action_update_msgview_menu(GtkItemFactory *ifactory,
429 MessageView *msgview)
431 action_update_menu(ifactory, branch_path,
432 msgview_actions_execute_cb, msgview);
435 void action_update_compose_menu(GtkItemFactory *ifactory,
439 action_update_menu(ifactory, branch_path,
440 compose_actions_execute_cb, compose);
443 static void action_update_menu(GtkItemFactory *ifactory,
445 gpointer callback, gpointer data)
450 gchar *action, *action_p;
452 GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
454 ifentry.path = branch_path;
455 menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
456 g_return_if_fail(menuitem != NULL);
458 amenu = GTK_MENU_SHELL(menuitem)->children;
459 while (amenu != NULL) {
460 GList *alist = amenu->next;
461 gtk_widget_destroy(GTK_WIDGET(amenu->data));
465 ifentry.accelerator = NULL;
466 ifentry.callback_action = 0;
467 ifentry.callback = callback;
468 ifentry.item_type = NULL;
470 for (cur = prefs_common.actions_list; cur; cur = cur->next) {
471 action = g_strdup((gchar *)cur->data);
472 action_p = strstr(action, ": ");
473 if (action_p && action_p[2] &&
474 action_get_type(&action_p[2]) != ACTION_ERROR) {
476 menu_path = g_strdup_printf("%s/%s", branch_path,
478 ifentry.path = menu_path;
479 gtk_item_factory_create_item(ifactory, &ifentry, data,
484 ifentry.callback_action++;
488 static void compose_actions_execute_cb(Compose *compose, guint action_nb,
492 ActionType action_type;
494 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
496 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
497 g_return_if_fail(buf != NULL);
498 action = strstr(buf, ": ");
499 g_return_if_fail(action != NULL);
501 /* Point to the beginning of the command-line */
504 action_type = action_get_type(action);
505 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
507 (_("The selected action cannot be used in the compose window\n"
508 "because it contains %%f, %%F, %%as or %%p."));
512 execute_actions(action, NULL, compose->text, 0, NULL);
515 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
520 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
521 message_actions_execute(mainwin->messageview, action_nb, msg_list);
522 g_slist_free(msg_list);
525 static void msgview_actions_execute_cb(MessageView *msgview, guint action_nb,
528 GSList *msg_list = NULL;
530 if (msgview->msginfo)
531 msg_list = g_slist_append(msg_list, msgview->msginfo);
532 message_actions_execute(msgview, action_nb, msg_list);
533 g_slist_free(msg_list);
536 static void message_actions_execute(MessageView *msgview, guint action_nb,
543 GtkWidget *text = NULL;
545 ActionType action_type;
547 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
549 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
551 g_return_if_fail(buf);
552 g_return_if_fail((action = strstr(buf, ": ")));
554 /* Point to the beginning of the command-line */
557 textview = messageview_get_current_textview(msgview);
559 text = textview->text;
560 body_pos = textview->body_pos;
562 partinfo = messageview_get_selected_mime_part(msgview);
564 /* this command will alter the message text */
565 action_type = action_get_type(action);
566 if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
567 msgview->filtered = TRUE;
569 if (action_type & ACTION_FILTERING_ACTION)
570 /* CLAWS: most of the above code is not necessary for applying
572 execute_filtering_actions(action, msg_list);
574 execute_actions(action, msg_list, text, body_pos, partinfo);
577 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
579 GSList *action_list, *p;
580 const gchar *sbegin, *send;
581 gchar *action_string;
583 if (NULL == (sbegin = strstr2(action, "%as{")))
585 sbegin += sizeof "%as{" - 1;
586 if (NULL == (send = strrchr(sbegin, '}')))
588 action_string = g_strndup(sbegin, send - sbegin);
590 action_list = matcher_parser_get_action_list(action_string);
591 g_free(action_string);
592 if (action_list == NULL) return FALSE;
594 /* apply actions on each message info */
595 for (p = msglist; p && p->data; p = g_slist_next(p))
596 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
598 for (p = action_list; p; p = g_slist_next(p))
599 if (p->data) filteringaction_free(p->data);
600 g_slist_free(action_list);
604 static gboolean execute_actions(gchar *action, GSList *msg_list,
606 gint body_pos, MimeInfo *partinfo)
608 GSList *children_list = NULL;
612 ChildInfo *child_info;
613 ActionType action_type;
616 gchar *sel_str = NULL;
617 gchar *msg_str = NULL;
618 gchar *user_str = NULL;
619 gchar *user_hidden_str = NULL;
620 GtkTextIter start_iter, end_iter;
621 gboolean is_selection = FALSE;
623 g_return_val_if_fail(action && *action, FALSE);
625 action_type = action_get_type(action);
627 if (action_type == ACTION_ERROR)
628 return FALSE; /* ERR: syntax error */
630 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
631 return FALSE; /* ERR: file command without selection */
633 msg_list_len = g_slist_length(msg_list);
635 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
636 if (msg_list_len > 1)
637 return FALSE; /* ERR: pipe + multiple selection */
639 return FALSE; /* ERR: pipe and no displayed text */
642 if (action_type & ACTION_SELECTION_STR) {
644 return FALSE; /* ERR: selection string but no text */
648 GtkTextBuffer *textbuf;
650 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
651 is_selection = gtk_text_buffer_get_selection_bounds(textbuf,
655 gtk_text_buffer_get_start_iter(textbuf, &start_iter);
656 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
658 msg_str = gtk_text_buffer_get_text(textbuf,
659 &start_iter, &end_iter,
662 sel_str = g_strdup (msg_str);
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 (text && (action_type & ACTION_PIPE_OUT)) {
684 GtkTextBuffer *textbuf;
685 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
686 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
689 children = g_new0(Children, 1);
691 children->action = g_strdup(action);
692 children->action_type = action_type;
693 children->msg_text = text;
694 children->is_selection = is_selection;
696 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
697 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
698 children->open_in = 1;
700 if (action_type & ACTION_SINGLE) {
703 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
704 msginfo = (MsgInfo *)cur->data;
706 is_ok = FALSE; /* ERR: msginfo missing */
709 cmd = parse_action_cmd(action, msginfo, msg_list,
711 user_hidden_str, sel_str);
713 debug_print("Action command error\n");
714 is_ok = FALSE; /* ERR: incorrect command */
717 if ((child_info = fork_child(cmd, msg_str, children))) {
718 children_list = g_slist_append(children_list,
724 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
725 user_str, user_hidden_str, sel_str);
727 if ((child_info = fork_child(cmd, msg_str, children))) {
728 children_list = g_slist_append(children_list,
733 is_ok = FALSE; /* ERR: incorrect command */
739 g_free(user_hidden_str);
741 if (!children_list) {
742 /* If not waiting for children, return */
743 free_children(children);
747 children->list = children_list;
748 children->nb = g_slist_length(children_list);
749 children->initial_nb = children->nb;
751 for (cur = children_list; cur; cur = cur->next) {
752 child_info = (ChildInfo *) cur->data;
753 child_info->tag_status =
754 gdk_input_add(child_info->chld_status,
756 catch_status, child_info);
759 create_io_dialog(children);
765 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
768 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
769 gchar *cmdline[4], *ret_str;
771 ChildInfo *child_info;
773 gssize by_written = 0, by_read = 0;
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");
800 debug_print("Executing: /bin/sh -c %s\n", cmd);
803 if (pid == 0) { /* Child */
807 #ifdef GDK_WINDOWING_X11
808 close(ConnectionNumber(gdk_display));
809 #endif /* GDK_WINDOWING_X11 */
814 if (setpgid(0, getppid()))
818 if (children->action_type &
821 ACTION_USER_HIDDEN_IN)) {
822 close(fileno(stdin));
828 close(fileno(stdout));
833 close(fileno(stderr));
841 ret_str = g_locale_from_utf8(cmd, strlen(cmd),
842 &by_read, &by_written,
844 if (ret_str && by_written)
845 cmdline[2] = ret_str;
849 execvp("/bin/sh", cmdline);
854 } else if (gch_pid < (pid_t) 0) { /* Fork error */
856 write(chld_status[1], "1\n", 2);
867 close(chld_status[0]);
869 debug_print("Child: Waiting for grandchild\n");
870 waitpid(gch_pid, NULL, 0);
871 debug_print("Child: grandchild ended\n");
872 write(chld_status[1], "0\n", 2);
873 close(chld_status[1]);
877 } else if (pid < 0) { /* Fork error */
878 alertpanel_error(_("Could not fork to execute the following "
880 cmd, g_strerror(errno));
887 waitpid(pid, NULL, 0);
892 if (!(children->action_type &
893 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
897 close(chld_status[1]);
899 child_info = g_new0(ChildInfo, 1);
901 child_info->children = children;
903 child_info->pid = pid;
904 child_info->cmd = g_strdup(cmd);
905 child_info->new_out = FALSE;
906 child_info->output = g_string_new(NULL);
907 child_info->chld_in =
908 (children->action_type &
909 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
911 child_info->chld_out = chld_out[0];
912 child_info->chld_err = chld_err[0];
913 child_info->chld_status = chld_status[0];
914 child_info->tag_in = -1;
915 child_info->tag_out = gdk_input_add(chld_out[0], GDK_INPUT_READ,
916 catch_output, child_info);
917 child_info->tag_err = gdk_input_add(chld_err[0], GDK_INPUT_READ,
918 catch_output, child_info);
920 if (!(children->action_type &
921 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
924 if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
925 ret_str = g_locale_from_utf8(msg_str, strlen(msg_str),
926 &by_read, &by_written, NULL);
927 if (ret_str && by_written) {
928 write(chld_in[1], ret_str, strlen(ret_str));
931 write(chld_in[1], msg_str, strlen(msg_str));
932 if (!(children->action_type &
933 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
935 child_info->chld_in = -1; /* No more input */
941 static void kill_children_cb(GtkWidget *widget, gpointer data)
944 Children *children = (Children *) data;
945 ChildInfo *child_info;
947 for (cur = children->list; cur; cur = cur->next) {
948 child_info = (ChildInfo *)(cur->data);
949 debug_print("Killing child group id %d\n", child_info->pid);
950 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
955 static gint wait_for_children(Children *children)
958 ChildInfo *child_info;
960 gint nb = children->nb;
964 cur = children->list;
967 child_info = (ChildInfo *)cur->data;
970 new_output |= child_info->new_out;
974 children->output |= new_output;
976 if (new_output || (children->dialog && (nb != children->nb)))
977 update_io_dialog(children);
982 if (!children->dialog) {
983 free_children(children);
984 } else if (!children->output) {
985 gtk_widget_destroy(children->dialog);
991 static void send_input(GtkWidget *w, gpointer data)
993 Children *children = (Children *) data;
994 ChildInfo *child_info = (ChildInfo *) children->list->data;
996 child_info->tag_in = gdk_input_add(child_info->chld_in,
998 catch_input, children);
999 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1002 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1004 hide_io_dialog_cb(w, data);
1008 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1011 Children *children = (Children *)data;
1013 if (!children->nb) {
1014 g_signal_handlers_disconnect_matched
1015 (G_OBJECT(children->dialog), G_SIGNAL_MATCH_DATA,
1016 0, 0, NULL, NULL, children);
1017 gtk_widget_destroy(children->dialog);
1018 free_children(children);
1022 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1025 if (event && event->keyval == GDK_Escape)
1026 hide_io_dialog_cb(widget, data);
1030 static void childinfo_close_pipes(ChildInfo *child_info)
1032 /* stdout and stderr pipes are guaranteed to be removed by
1033 * their handler, but in case where we receive child exit notification
1034 * before grand-child's pipes closing signals, we check them and close
1037 if (child_info->tag_in > 0)
1038 gdk_input_remove(child_info->tag_in);
1039 if (child_info->tag_out > 0)
1040 gdk_input_remove(child_info->tag_out);
1041 if (child_info->tag_err > 0)
1042 gdk_input_remove(child_info->tag_err);
1044 if (child_info->chld_in >= 0)
1045 close(child_info->chld_in);
1046 if (child_info->chld_out >= 0)
1047 close(child_info->chld_out);
1048 if (child_info->chld_err >= 0)
1049 close(child_info->chld_err);
1051 close(child_info->chld_status);
1054 static void free_children(Children *children)
1056 ChildInfo *child_info;
1058 debug_print("Freeing children data %p\n", children);
1060 g_free(children->action);
1061 while (children->list != NULL) {
1062 child_info = (ChildInfo *)children->list->data;
1063 g_free(child_info->cmd);
1064 g_string_free(child_info->output, TRUE);
1065 children->list = g_slist_remove(children->list, child_info);
1071 static void update_io_dialog(Children *children)
1075 debug_print("Updating actions input/output dialog.\n");
1077 if (children->progress_bar) {
1080 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar),
1081 (gdouble) (children->initial_nb - children->nb) /
1082 (gdouble) children->initial_nb);
1083 text = g_strdup_printf("%s %d/%d", _("Completed"),
1084 children->initial_nb - children->nb,
1085 children->initial_nb);
1086 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(children->progress_bar), text);
1090 if (!children->nb) {
1091 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1092 gtk_widget_set_sensitive(children->close_btn, TRUE);
1093 if (children->input_hbox)
1094 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1095 gtk_widget_grab_focus(children->close_btn);
1096 g_signal_connect(G_OBJECT(children->dialog),
1098 G_CALLBACK(io_dialog_key_pressed_cb),
1102 if (children->output) {
1103 GtkWidget *text = children->text;
1105 ChildInfo *child_info;
1106 GtkTextBuffer *textbuf;
1107 GtkTextIter iter, start_iter, end_iter;
1109 gtk_widget_show(children->scrolledwin);
1110 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
1111 gtk_text_buffer_get_start_iter(textbuf, &start_iter);
1112 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
1113 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
1114 gtk_text_buffer_get_start_iter(textbuf, &start_iter);
1116 for (cur = children->list; cur; cur = cur->next) {
1117 child_info = (ChildInfo *)cur->data;
1118 if (child_info->pid)
1119 caption = g_strdup_printf
1120 (_("--- Running: %s\n"),
1123 caption = g_strdup_printf
1124 (_("--- Ended: %s\n"),
1127 gtk_text_buffer_insert(textbuf, &iter,
1129 gtk_text_buffer_insert(textbuf, &iter,
1130 child_info->output->str, -1);
1132 child_info->new_out = FALSE;
1137 static void create_io_dialog(Children *children)
1141 GtkWidget *entry = NULL;
1142 GtkWidget *input_hbox = NULL;
1143 GtkWidget *send_button;
1146 GtkWidget *scrolledwin;
1148 GtkWidget *progress_bar = NULL;
1149 GtkWidget *abort_button;
1150 GtkWidget *close_button;
1151 PangoFontDescription *text_font;
1153 debug_print("Creating action IO dialog\n");
1155 dialog = gtk_dialog_new();
1156 gtk_container_set_border_width
1157 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1158 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1159 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1160 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1161 manage_window_set_transient(GTK_WINDOW(dialog));
1162 g_signal_connect(G_OBJECT(dialog), "delete_event",
1163 G_CALLBACK(delete_io_dialog_cb), children);
1164 g_signal_connect(G_OBJECT(dialog), "destroy",
1165 G_CALLBACK(hide_io_dialog_cb),
1168 vbox = gtk_vbox_new(FALSE, 8);
1169 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1170 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1171 gtk_widget_show(vbox);
1173 label = gtk_label_new(children->action);
1174 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1175 gtk_widget_show(label);
1177 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1178 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1179 GTK_POLICY_AUTOMATIC,
1180 GTK_POLICY_AUTOMATIC);
1181 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1182 gtk_widget_set_size_request(scrolledwin, 480, 200);
1183 gtk_widget_hide(scrolledwin);
1185 text = gtk_text_view_new();
1186 if (prefs_common.textfont) {
1187 if (NULL != (text_font = pango_font_description_from_string
1188 (prefs_common.textfont))) {
1189 gtk_widget_modify_font(text, text_font);
1190 pango_font_description_free(text_font);
1193 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
1194 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1195 gtk_widget_show(text);
1197 if (children->open_in) {
1198 input_hbox = gtk_hbox_new(FALSE, 8);
1199 gtk_widget_show(input_hbox);
1201 entry = gtk_entry_new();
1202 gtk_widget_set_size_request(entry, 320, -1);
1203 g_signal_connect(G_OBJECT(entry), "activate",
1204 G_CALLBACK(send_input), children);
1205 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1206 if (children->action_type & ACTION_USER_HIDDEN_IN)
1207 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1208 gtk_widget_show(entry);
1210 send_button = gtk_button_new_from_stock(GTK_STOCK_EXECUTE);
1211 g_signal_connect(G_OBJECT(send_button), "clicked",
1212 G_CALLBACK(send_input), children);
1213 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1215 gtk_widget_show(send_button);
1217 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1218 gtk_widget_grab_focus(entry);
1221 if (children->initial_nb > 1) {
1224 progress_bar = gtk_progress_bar_new();
1225 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1226 GTK_PROGRESS_LEFT_TO_RIGHT);
1227 text = g_strdup_printf("%s 0/%d\n", _("Completed"),
1228 children->initial_nb);
1229 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar),
1232 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1233 gtk_widget_show(progress_bar);
1236 gtkut_button_set_create_stock(&hbox, &abort_button, GTK_STOCK_STOP,
1237 &close_button, GTK_STOCK_CLOSE, NULL, NULL);
1238 g_signal_connect(G_OBJECT(abort_button), "clicked",
1239 G_CALLBACK(kill_children_cb), children);
1240 g_signal_connect(G_OBJECT(close_button), "clicked",
1241 G_CALLBACK(hide_io_dialog_cb), children);
1242 gtk_widget_show(hbox);
1245 gtk_widget_set_sensitive(close_button, FALSE);
1247 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1249 children->dialog = dialog;
1250 children->scrolledwin = scrolledwin;
1251 children->text = text;
1252 children->input_hbox = children->open_in ? input_hbox : NULL;
1253 children->input_entry = children->open_in ? entry : NULL;
1254 children->progress_bar = progress_bar;
1255 children->abort_btn = abort_button;
1256 children->close_btn = close_button;
1258 gtk_widget_show(dialog);
1261 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1263 ChildInfo *child_info = (ChildInfo *)data;
1267 gdk_input_remove(child_info->tag_status);
1269 c = read(source, &buf, 1);
1270 debug_print("Child returned %c\n", buf);
1272 waitpid(-child_info->pid, NULL, 0);
1273 childinfo_close_pipes(child_info);
1274 child_info->pid = 0;
1276 wait_for_children(child_info->children);
1279 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1281 Children *children = (Children *)data;
1282 ChildInfo *child_info = (ChildInfo *)children->list->data;
1283 gchar *input, *ret_str;
1285 gssize by_read = 0, by_written = 0;
1287 debug_print("Sending input to grand child.\n");
1288 if (!(cond && GDK_INPUT_WRITE))
1291 gdk_input_remove(child_info->tag_in);
1292 child_info->tag_in = -1;
1294 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1296 ret_str = g_locale_from_utf8(input, strlen(input), &by_read,
1298 if (ret_str && by_written) {
1303 len = strlen(input);
1307 c = write(child_info->chld_in, input + count, len - count);
1310 } while (c >= 0 && count < len);
1313 write(child_info->chld_in, "\n", 2);
1317 gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1318 gtk_widget_set_sensitive(children->input_hbox, TRUE);
1319 close(child_info->chld_in);
1320 child_info->chld_in = -1;
1321 debug_print("Input to grand child sent.\n");
1324 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1326 ChildInfo *child_info = (ChildInfo *)data;
1328 gchar buf[BUFFSIZE];
1330 debug_print("Catching grand child's output.\n");
1331 if (child_info->children->action_type &
1332 (ACTION_PIPE_OUT | ACTION_INSERT)
1333 && source == child_info->chld_out) {
1334 GtkTextView *text = GTK_TEXT_VIEW(child_info->children->msg_text);
1335 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
1336 GtkTextIter iter1, iter2;
1339 mark = gtk_text_buffer_get_insert(textbuf);
1340 gtk_text_buffer_get_iter_at_mark(textbuf, &iter1, mark);
1341 gtk_text_buffer_get_iter_at_mark(textbuf, &iter2, mark);
1344 c = read(source, buf, sizeof(buf) - 1);
1348 gssize by_read = 0, by_written = 0;
1349 gchar *ret_str = g_locale_to_utf8(buf, c, &by_read,
1352 if (ret_str && by_written) {
1353 gtk_text_buffer_insert(textbuf, &iter2,
1354 ret_str, by_written);
1357 gtk_text_buffer_insert(textbuf, &iter2, buf, c);
1360 if (child_info->children->is_selection) {
1361 gtk_text_buffer_place_cursor(textbuf, &iter1);
1362 gtk_text_buffer_move_mark_by_name
1363 (textbuf, "selection_bound", &iter2);
1366 c = read(source, buf, sizeof(buf) - 1);
1369 gssize by_read = 0, by_written = 0;
1370 gchar *ret_str = g_locale_to_utf8(buf, c, &by_read,
1373 if (ret_str && by_written) {
1374 g_string_append_len(child_info->output,
1375 ret_str, by_written);
1378 g_string_append_len(child_info->output, buf, c);
1382 child_info->new_out = TRUE;
1385 if (source == child_info->chld_out) {
1386 gdk_input_remove(child_info->tag_out);
1387 child_info->tag_out = -1;
1388 close(child_info->chld_out);
1389 child_info->chld_out = -1;
1391 gdk_input_remove(child_info->tag_err);
1392 child_info->tag_err = -1;
1393 close(child_info->chld_err);
1394 child_info->chld_err = -1;
1398 wait_for_children(child_info->children);
1401 static gchar *get_user_string(const gchar *action, ActionType type)
1404 gchar *user_str = NULL;
1407 case ACTION_USER_HIDDEN_STR:
1408 message = g_strdup_printf
1409 (_("Enter the argument for the following action:\n"
1410 "(`%%h' will be replaced with the argument)\n"
1413 user_str = input_dialog_with_invisible
1414 (_("Action's hidden user argument"), message, NULL);
1416 case ACTION_USER_STR:
1417 message = g_strdup_printf
1418 (_("Enter the argument for the following action:\n"
1419 "(`%%u' will be replaced with the argument)\n"
1422 user_str = input_dialog
1423 (_("Action's user argument"), message, NULL);
1426 g_warning("Unsupported action type %d", type);