2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <glib/gi18n.h>
29 #include <gdk/gdkkeysyms.h>
30 #ifdef GDK_WINDOWING_X11
31 # include <gdk/gdkx.h>
32 #endif /* GDK_WINDOWING_X11 */
37 #include <sys/types.h>
39 # include <sys/wait.h>
46 #include "manage_window.h"
47 #include "mainwindow.h"
48 #include "prefs_common.h"
49 #include "alertpanel.h"
50 #include "inputdialog.h"
55 #include "matcher_parser.h" /* CLAWS */
56 #include "filtering.h"
58 typedef struct _Children Children;
59 typedef struct _ChildInfo ChildInfo;
60 typedef struct _UserStringDialog UserStringDialog;
66 GtkWidget *input_entry;
67 GtkWidget *input_hbox;
68 GtkWidget *progress_bar;
71 GtkWidget *scrolledwin;
74 ActionType action_type;
83 gboolean is_selection;
104 static void action_update_menu (GtkItemFactory *ifactory,
108 static void compose_actions_execute_cb (Compose *compose,
111 static void mainwin_actions_execute_cb (MainWindow *mainwin,
114 static void msgview_actions_execute_cb (MessageView *msgview,
117 static void message_actions_execute (MessageView *msgview,
121 static gboolean execute_filtering_actions(gchar *action,
124 static gboolean execute_actions (gchar *action,
130 static gchar *parse_action_cmd (gchar *action,
134 const gchar *user_str,
135 const gchar *user_hidden_str,
136 const gchar *sel_str);
137 static gboolean parse_append_filename (GString *cmd,
140 static gboolean parse_append_msgpart (GString *cmd,
144 static ChildInfo *fork_child (gchar *cmd,
145 const gchar *msg_str,
148 static gint wait_for_children (Children *children);
150 static void free_children (Children *children);
152 static void childinfo_close_pipes (ChildInfo *child_info);
154 static void create_io_dialog (Children *children);
155 static void update_io_dialog (Children *children);
157 static void hide_io_dialog_cb (GtkWidget *widget,
159 static gint io_dialog_key_pressed_cb (GtkWidget *widget,
163 static void catch_output (gpointer data,
165 GdkInputCondition cond);
166 static void catch_input (gpointer data,
168 GdkInputCondition cond);
169 static void catch_status (gpointer data,
171 GdkInputCondition cond);
173 static gchar *get_user_string (const gchar *action,
177 ActionType action_get_type(const gchar *action_str)
180 ActionType action_type = ACTION_NONE;
182 g_return_val_if_fail(action_str, ACTION_ERROR);
183 g_return_val_if_fail(*action_str, ACTION_ERROR);
188 action_type |= ACTION_PIPE_IN;
190 } else if (p[0] == '>') {
191 action_type |= ACTION_USER_IN;
193 } else if (p[0] == '*') {
194 action_type |= ACTION_USER_HIDDEN_IN;
201 while (*p && action_type != ACTION_ERROR) {
202 if (p[0] == '%' && p[1]) {
205 /* CLAWS: filtering action is a mutually exclusive
206 * action. we can enable others if needed later. we
207 * add ACTION_SINGLE | ACTION_MULTIPLE so it will
208 * only be executed from the main window toolbar */
209 if (p[2] == 's') /* source messages */
210 action_type = ACTION_FILTERING_ACTION
215 action_type |= ACTION_SINGLE;
218 action_type |= ACTION_MULTIPLE;
221 action_type |= ACTION_SINGLE;
224 action_type |= ACTION_SELECTION_STR;
227 action_type |= ACTION_USER_STR;
230 action_type |= ACTION_USER_HIDDEN_STR;
233 action_type = ACTION_ERROR;
236 } else if (p[0] == '|') {
238 action_type |= ACTION_PIPE_OUT;
239 } else if (p[0] == '>') {
241 action_type |= ACTION_INSERT;
242 } else if (p[0] == '&') {
244 action_type |= ACTION_ASYNC;
252 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
253 GSList *msg_list, MimeInfo *partinfo,
254 const gchar *user_str,
255 const gchar *user_hidden_str,
256 const gchar *sel_str)
264 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
267 cmd = g_string_sized_new(strlen(action));
270 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
271 if (p[0] == '%' && p[1]) {
274 if (!parse_append_filename(cmd, msginfo)) {
275 g_string_free(cmd, TRUE);
281 for (cur = msg_list; cur != NULL;
283 MsgInfo *msg = (MsgInfo *)cur->data;
285 if (!parse_append_filename(cmd, msg)) {
286 g_string_free(cmd, TRUE);
290 g_string_append_c(cmd, ' ');
295 if (!parse_append_msgpart(cmd, msginfo,
297 g_string_free(cmd, TRUE);
304 g_string_append(cmd, sel_str);
309 g_string_append(cmd, user_str);
314 g_string_append(cmd, user_hidden_str);
318 g_string_append_c(cmd, p[0]);
319 g_string_append_c(cmd, p[1]);
323 g_string_append_c(cmd, p[0]);
328 g_string_free(cmd, TRUE);
333 g_string_free(cmd, FALSE);
337 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
341 gchar escape_ch[] = "\\ ";
343 g_return_val_if_fail(msginfo, FALSE);
345 filename = procmsg_get_message_file(msginfo);
348 alertpanel_error(_("Could not get message file %d"),
354 while ((q = strpbrk(p, "$\"`'\\ \t*?[]&|;<>()!#~")) != NULL) {
357 g_string_append(cmd, p);
358 g_string_append(cmd, escape_ch);
361 g_string_append(cmd, p);
368 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
371 gboolean single_part = FALSE;
373 gchar *part_filename;
377 partinfo = procmime_scan_message(msginfo);
379 alertpanel_error(_("Could not get message part."));
386 filename = procmsg_get_message_file_path(msginfo);
387 part_filename = procmime_get_tmp_file_name(partinfo);
389 ret = procmime_get_part(part_filename, partinfo);
392 procmime_mimeinfo_free_all(partinfo);
396 alertpanel_error(_("Can't get part of multipart message"));
397 g_free(part_filename);
401 g_string_append(cmd, part_filename);
403 g_free(part_filename);
408 void actions_execute(gpointer data,
413 if (source == TOOLBAR_MAIN)
414 mainwin_actions_execute_cb((MainWindow*)data, action_nb, widget);
415 else if (source == TOOLBAR_COMPOSE)
416 compose_actions_execute_cb((Compose*)data, action_nb, widget);
417 else if (source == TOOLBAR_MSGVIEW)
418 msgview_actions_execute_cb((MessageView*)data, action_nb, widget);
421 void action_update_mainwin_menu(GtkItemFactory *ifactory,
425 action_update_menu(ifactory, branch_path,
426 mainwin_actions_execute_cb, mainwin);
429 void action_update_msgview_menu(GtkItemFactory *ifactory,
431 MessageView *msgview)
433 action_update_menu(ifactory, branch_path,
434 msgview_actions_execute_cb, msgview);
437 void action_update_compose_menu(GtkItemFactory *ifactory,
441 action_update_menu(ifactory, branch_path,
442 compose_actions_execute_cb, compose);
445 static void action_update_menu(GtkItemFactory *ifactory,
447 gpointer callback, gpointer data)
452 gchar *action, *action_p;
454 GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
456 ifentry.path = branch_path;
457 menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
458 g_return_if_fail(menuitem != NULL);
460 amenu = GTK_MENU_SHELL(menuitem)->children;
461 while (amenu != NULL) {
462 GList *alist = amenu->next;
463 gtk_widget_destroy(GTK_WIDGET(amenu->data));
467 ifentry.accelerator = NULL;
468 ifentry.callback_action = 0;
469 ifentry.callback = callback;
470 ifentry.item_type = NULL;
472 for (cur = prefs_common.actions_list; cur; cur = cur->next) {
473 action = g_strdup((gchar *)cur->data);
474 action_p = strstr(action, ": ");
475 if (action_p && action_p[2] &&
476 (action_get_type(&action_p[2]) != ACTION_ERROR) &&
477 (action[0] != '/')) {
479 menu_path = g_strdup_printf("%s/%s", branch_path,
481 ifentry.path = menu_path;
482 gtk_item_factory_create_item(ifactory, &ifentry, data,
487 ifentry.callback_action++;
491 static void compose_actions_execute_cb(Compose *compose, guint action_nb,
495 ActionType action_type;
497 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
499 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
500 g_return_if_fail(buf != NULL);
501 action = strstr(buf, ": ");
502 g_return_if_fail(action != NULL);
504 /* Point to the beginning of the command-line */
507 action_type = action_get_type(action);
508 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
510 (_("The selected action cannot be used in the compose window\n"
511 "because it contains %%f, %%F, %%as or %%p."));
515 execute_actions(action, NULL, compose->text, 0, NULL);
518 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
523 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
524 message_actions_execute(mainwin->messageview, action_nb, msg_list);
525 g_slist_free(msg_list);
528 static void msgview_actions_execute_cb(MessageView *msgview, guint action_nb,
531 GSList *msg_list = NULL;
533 if (msgview->msginfo)
534 msg_list = g_slist_append(msg_list, msgview->msginfo);
535 message_actions_execute(msgview, action_nb, msg_list);
536 g_slist_free(msg_list);
539 static void message_actions_execute(MessageView *msgview, guint action_nb,
546 GtkWidget *text = NULL;
548 ActionType action_type;
550 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
552 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
554 g_return_if_fail(buf);
555 g_return_if_fail((action = strstr(buf, ": ")));
557 /* Point to the beginning of the command-line */
560 textview = messageview_get_current_textview(msgview);
562 text = textview->text;
563 body_pos = textview->body_pos;
565 partinfo = messageview_get_selected_mime_part(msgview);
567 /* this command will alter the message text */
568 action_type = action_get_type(action);
569 if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
570 msgview->filtered = TRUE;
572 if (action_type & ACTION_FILTERING_ACTION)
573 /* CLAWS: most of the above code is not necessary for applying
575 execute_filtering_actions(action, msg_list);
577 execute_actions(action, msg_list, text, body_pos, partinfo);
580 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
582 GSList *action_list, *p;
583 const gchar *sbegin, *send;
584 gchar *action_string;
586 if (NULL == (sbegin = strstr2(action, "%as{")))
588 sbegin += sizeof "%as{" - 1;
589 if (NULL == (send = strrchr(sbegin, '}')))
591 action_string = g_strndup(sbegin, send - sbegin);
593 action_list = matcher_parser_get_action_list(action_string);
594 g_free(action_string);
595 if (action_list == NULL) return FALSE;
597 /* apply actions on each message info */
598 for (p = msglist; p && p->data; p = g_slist_next(p))
599 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
601 for (p = action_list; p; p = g_slist_next(p))
602 if (p->data) filteringaction_free(p->data);
603 g_slist_free(action_list);
607 static gboolean execute_actions(gchar *action, GSList *msg_list,
609 gint body_pos, MimeInfo *partinfo)
611 GSList *children_list = NULL;
615 ChildInfo *child_info;
616 ActionType action_type;
619 gchar *sel_str = NULL;
620 gchar *msg_str = NULL;
621 gchar *user_str = NULL;
622 gchar *user_hidden_str = NULL;
623 GtkTextIter start_iter, end_iter;
624 gboolean is_selection = FALSE;
626 g_return_val_if_fail(action && *action, FALSE);
628 action_type = action_get_type(action);
630 if (action_type == ACTION_ERROR)
631 return FALSE; /* ERR: syntax error */
633 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
634 return FALSE; /* ERR: file command without selection */
636 msg_list_len = g_slist_length(msg_list);
638 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
639 if (msg_list_len > 1)
640 return FALSE; /* ERR: pipe + multiple selection */
642 return FALSE; /* ERR: pipe and no displayed text */
645 if (action_type & ACTION_SELECTION_STR) {
647 return FALSE; /* ERR: selection string but no text */
651 GtkTextBuffer *textbuf;
653 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
654 is_selection = gtk_text_buffer_get_selection_bounds
655 (textbuf, &start_iter, &end_iter);
657 gtk_text_buffer_get_iter_at_offset
658 (textbuf, &start_iter, body_pos);
659 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
661 msg_str = gtk_text_buffer_get_text
662 (textbuf, &start_iter, &end_iter, FALSE);
664 sel_str = g_strdup(msg_str);
667 if (action_type & ACTION_USER_STR) {
668 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
675 if (action_type & ACTION_USER_HIDDEN_STR) {
676 if (!(user_hidden_str =
677 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
685 if (text && (action_type & ACTION_PIPE_OUT)) {
686 GtkTextBuffer *textbuf;
687 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
688 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
691 children = g_new0(Children, 1);
693 children->action = g_strdup(action);
694 children->action_type = action_type;
695 children->msg_text = text;
696 children->is_selection = is_selection;
698 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
699 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
700 children->open_in = 1;
702 if (action_type & ACTION_SINGLE) {
705 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
706 msginfo = (MsgInfo *)cur->data;
708 is_ok = FALSE; /* ERR: msginfo missing */
711 cmd = parse_action_cmd(action, msginfo, msg_list,
713 user_hidden_str, sel_str);
715 debug_print("Action command error\n");
716 is_ok = FALSE; /* ERR: incorrect command */
719 if ((child_info = fork_child(cmd, msg_str, children))) {
720 children_list = g_slist_append(children_list,
726 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
727 user_str, user_hidden_str, sel_str);
729 if ((child_info = fork_child(cmd, msg_str, children))) {
730 children_list = g_slist_append(children_list,
735 is_ok = FALSE; /* ERR: incorrect command */
741 g_free(user_hidden_str);
743 if (!children_list) {
744 /* If not waiting for children, return */
745 free_children(children);
749 children->list = children_list;
750 children->nb = g_slist_length(children_list);
751 children->initial_nb = children->nb;
753 for (cur = children_list; cur; cur = cur->next) {
754 child_info = (ChildInfo *) cur->data;
755 child_info->tag_status =
756 gdk_input_add(child_info->chld_status,
758 catch_status, child_info);
761 create_io_dialog(children);
767 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
771 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
772 gchar *cmdline[4], *ret_str;
774 ChildInfo *child_info;
776 gssize by_written = 0, by_read = 0;
778 sync = !(children->action_type & ACTION_ASYNC);
780 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
781 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
784 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
786 alertpanel_error(_("Command could not be started. "
787 "Pipe creation failed.\n%s"),
789 /* Closing fd = -1 fails silently */
796 close(chld_status[0]);
797 close(chld_status[1]);
798 return NULL; /* Pipe error */
802 debug_print("Forking child and grandchild.\n");
803 debug_print("Executing: /bin/sh -c %s\n", cmd);
806 if (pid == 0) { /* Child */
810 #ifdef GDK_WINDOWING_X11
811 close(ConnectionNumber(gdk_display));
812 #endif /* GDK_WINDOWING_X11 */
817 if (setpgid(0, getppid()))
821 if (children->action_type &
824 ACTION_USER_HIDDEN_IN)) {
825 close(fileno(stdin));
831 close(fileno(stdout));
836 close(fileno(stderr));
844 ret_str = g_locale_from_utf8(cmd, strlen(cmd),
845 &by_read, &by_written,
847 if (ret_str && by_written)
848 cmdline[2] = ret_str;
852 execvp("/bin/sh", cmdline);
857 } else if (gch_pid < (pid_t) 0) { /* Fork error */
859 write(chld_status[1], "1\n", 2);
870 close(chld_status[0]);
872 debug_print("Child: Waiting for grandchild\n");
873 waitpid(gch_pid, NULL, 0);
874 debug_print("Child: grandchild ended\n");
875 write(chld_status[1], "0\n", 2);
876 close(chld_status[1]);
880 } else if (pid < 0) { /* Fork error */
881 alertpanel_error(_("Could not fork to execute the following "
883 cmd, g_strerror(errno));
890 waitpid(pid, NULL, 0);
895 if (!(children->action_type &
896 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
900 close(chld_status[1]);
902 child_info = g_new0(ChildInfo, 1);
904 child_info->children = children;
906 child_info->pid = pid;
907 child_info->cmd = g_strdup(cmd);
908 child_info->new_out = FALSE;
909 child_info->output = g_string_new(NULL);
910 child_info->chld_in =
911 (children->action_type &
912 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
914 child_info->chld_out = chld_out[0];
915 child_info->chld_err = chld_err[0];
916 child_info->chld_status = chld_status[0];
917 child_info->tag_in = -1;
918 child_info->tag_out = gdk_input_add(chld_out[0], GDK_INPUT_READ,
919 catch_output, child_info);
920 child_info->tag_err = gdk_input_add(chld_err[0], GDK_INPUT_READ,
921 catch_output, child_info);
923 if (!(children->action_type &
924 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
927 if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
928 ret_str = g_locale_from_utf8(msg_str, strlen(msg_str),
929 &by_read, &by_written, NULL);
930 if (ret_str && by_written) {
931 write(chld_in[1], ret_str, strlen(ret_str));
934 write(chld_in[1], msg_str, strlen(msg_str));
935 if (!(children->action_type &
936 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
938 child_info->chld_in = -1; /* No more input */
944 #endif /* G_OS_UNIX */
947 static void kill_children_cb(GtkWidget *widget, gpointer data)
951 Children *children = (Children *) data;
952 ChildInfo *child_info;
954 for (cur = children->list; cur; cur = cur->next) {
955 child_info = (ChildInfo *)(cur->data);
956 debug_print("Killing child group id %d\n", child_info->pid);
957 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
960 #endif /* G_OS_UNIX */
963 static gint wait_for_children(Children *children)
966 ChildInfo *child_info;
968 gint nb = children->nb;
972 cur = children->list;
975 child_info = (ChildInfo *)cur->data;
978 new_output |= child_info->new_out;
982 children->output |= new_output;
984 if (new_output || (children->dialog && (nb != children->nb)))
985 update_io_dialog(children);
990 if (!children->dialog) {
991 free_children(children);
992 } else if (!children->output) {
993 gtk_widget_destroy(children->dialog);
999 static void send_input(GtkWidget *w, gpointer data)
1001 Children *children = (Children *) data;
1002 ChildInfo *child_info = (ChildInfo *) children->list->data;
1004 child_info->tag_in = gdk_input_add(child_info->chld_in,
1006 catch_input, children);
1007 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1010 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1012 hide_io_dialog_cb(w, data);
1016 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1019 Children *children = (Children *)data;
1021 if (!children->nb) {
1022 g_signal_handlers_disconnect_matched
1023 (G_OBJECT(children->dialog), G_SIGNAL_MATCH_DATA,
1024 0, 0, NULL, NULL, children);
1025 gtk_widget_destroy(children->dialog);
1026 free_children(children);
1030 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1033 if (event && event->keyval == GDK_Escape)
1034 hide_io_dialog_cb(widget, data);
1038 static void childinfo_close_pipes(ChildInfo *child_info)
1040 /* stdout and stderr pipes are guaranteed to be removed by
1041 * their handler, but in case where we receive child exit notification
1042 * before grand-child's pipes closing signals, we check them and close
1045 if (child_info->tag_in > 0)
1046 gdk_input_remove(child_info->tag_in);
1047 if (child_info->tag_out > 0)
1048 gdk_input_remove(child_info->tag_out);
1049 if (child_info->tag_err > 0)
1050 gdk_input_remove(child_info->tag_err);
1052 if (child_info->chld_in >= 0)
1053 close(child_info->chld_in);
1054 if (child_info->chld_out >= 0)
1055 close(child_info->chld_out);
1056 if (child_info->chld_err >= 0)
1057 close(child_info->chld_err);
1059 close(child_info->chld_status);
1062 static void free_children(Children *children)
1064 ChildInfo *child_info;
1066 debug_print("Freeing children data %p\n", children);
1068 g_free(children->action);
1069 while (children->list != NULL) {
1070 child_info = (ChildInfo *)children->list->data;
1071 g_free(child_info->cmd);
1072 g_string_free(child_info->output, TRUE);
1073 children->list = g_slist_remove(children->list, child_info);
1079 static void update_io_dialog(Children *children)
1083 debug_print("Updating actions input/output dialog.\n");
1085 if (children->progress_bar) {
1088 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar),
1089 (gdouble) (children->initial_nb - children->nb) /
1090 (gdouble) children->initial_nb);
1091 text = g_strdup_printf("%s %d/%d", _("Completed"),
1092 children->initial_nb - children->nb,
1093 children->initial_nb);
1094 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(children->progress_bar), text);
1098 if (!children->nb) {
1099 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1100 gtk_widget_set_sensitive(children->close_btn, TRUE);
1101 if (children->input_hbox)
1102 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1103 gtk_widget_grab_focus(children->close_btn);
1104 g_signal_connect(G_OBJECT(children->dialog),
1106 G_CALLBACK(io_dialog_key_pressed_cb),
1110 if (children->output) {
1111 GtkWidget *text = children->text;
1112 GtkTextBuffer *textbuf;
1113 GtkTextIter iter, start_iter, end_iter;
1115 ChildInfo *child_info;
1117 gtk_widget_show(children->scrolledwin);
1118 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
1119 gtk_text_buffer_get_bounds(textbuf, &start_iter, &end_iter);
1120 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
1121 gtk_text_buffer_get_start_iter(textbuf, &iter);
1123 for (cur = children->list; cur; cur = cur->next) {
1124 child_info = (ChildInfo *)cur->data;
1125 if (child_info->pid)
1126 caption = g_strdup_printf
1127 (_("--- Running: %s\n"),
1130 caption = g_strdup_printf
1131 (_("--- Ended: %s\n"),
1134 gtk_text_buffer_insert(textbuf, &iter, caption, -1);
1135 gtk_text_buffer_insert(textbuf, &iter,
1136 child_info->output->str, -1);
1138 child_info->new_out = FALSE;
1143 static void create_io_dialog(Children *children)
1147 GtkWidget *entry = NULL;
1148 GtkWidget *input_hbox = NULL;
1149 GtkWidget *send_button;
1152 GtkWidget *scrolledwin;
1154 GtkWidget *progress_bar = NULL;
1155 GtkWidget *abort_button;
1156 GtkWidget *close_button;
1158 debug_print("Creating action IO dialog\n");
1160 dialog = gtk_dialog_new();
1161 gtk_container_set_border_width
1162 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1163 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1164 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1165 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1166 manage_window_set_transient(GTK_WINDOW(dialog));
1167 g_signal_connect(G_OBJECT(dialog), "delete_event",
1168 G_CALLBACK(delete_io_dialog_cb), children);
1169 g_signal_connect(G_OBJECT(dialog), "destroy",
1170 G_CALLBACK(hide_io_dialog_cb),
1173 vbox = gtk_vbox_new(FALSE, 8);
1174 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1175 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1176 gtk_widget_show(vbox);
1178 label = gtk_label_new(children->action);
1179 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1180 gtk_widget_show(label);
1182 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1183 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1184 GTK_POLICY_AUTOMATIC,
1185 GTK_POLICY_AUTOMATIC);
1186 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
1188 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1189 gtk_widget_set_size_request(scrolledwin, 560, 200);
1190 gtk_widget_hide(scrolledwin);
1192 text = gtk_text_view_new();
1194 if (prefs_common.textfont) {
1195 PangoFontDescription *font_desc;
1196 font_desc = pango_font_description_from_string
1197 (prefs_common.textfont);
1199 gtk_widget_modify_font(text, font_desc);
1200 pango_font_description_free(font_desc);
1204 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
1205 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
1206 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
1207 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
1208 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1209 gtk_widget_show(text);
1211 if (children->open_in) {
1212 input_hbox = gtk_hbox_new(FALSE, 8);
1213 gtk_widget_show(input_hbox);
1215 entry = gtk_entry_new();
1216 gtk_widget_set_size_request(entry, 320, -1);
1217 g_signal_connect(G_OBJECT(entry), "activate",
1218 G_CALLBACK(send_input), children);
1219 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1220 if (children->action_type & ACTION_USER_HIDDEN_IN)
1221 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1222 gtk_widget_show(entry);
1224 send_button = gtk_button_new_from_stock(GTK_STOCK_EXECUTE);
1225 g_signal_connect(G_OBJECT(send_button), "clicked",
1226 G_CALLBACK(send_input), children);
1227 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1229 gtk_widget_show(send_button);
1231 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1232 gtk_widget_grab_focus(entry);
1235 if (children->initial_nb > 1) {
1238 progress_bar = gtk_progress_bar_new();
1239 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1240 GTK_PROGRESS_LEFT_TO_RIGHT);
1241 text = g_strdup_printf("%s 0/%d\n", _("Completed"),
1242 children->initial_nb);
1243 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar),
1246 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1247 gtk_widget_show(progress_bar);
1250 gtkut_stock_button_set_create(&hbox, &abort_button, GTK_STOCK_STOP,
1251 &close_button, GTK_STOCK_CLOSE, NULL, NULL);
1252 g_signal_connect(G_OBJECT(abort_button), "clicked",
1253 G_CALLBACK(kill_children_cb), children);
1254 g_signal_connect(G_OBJECT(close_button), "clicked",
1255 G_CALLBACK(hide_io_dialog_cb), children);
1256 gtk_widget_show(hbox);
1259 gtk_widget_set_sensitive(close_button, FALSE);
1261 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1263 children->dialog = dialog;
1264 children->scrolledwin = scrolledwin;
1265 children->text = text;
1266 children->input_hbox = children->open_in ? input_hbox : NULL;
1267 children->input_entry = children->open_in ? entry : NULL;
1268 children->progress_bar = progress_bar;
1269 children->abort_btn = abort_button;
1270 children->close_btn = close_button;
1272 gtk_widget_show(dialog);
1275 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1277 ChildInfo *child_info = (ChildInfo *)data;
1281 gdk_input_remove(child_info->tag_status);
1283 c = read(source, &buf, 1);
1284 debug_print("Child returned %c\n", buf);
1287 waitpid(-child_info->pid, NULL, 0);
1289 childinfo_close_pipes(child_info);
1290 child_info->pid = 0;
1292 wait_for_children(child_info->children);
1295 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1297 Children *children = (Children *)data;
1298 ChildInfo *child_info = (ChildInfo *)children->list->data;
1299 gchar *input, *ret_str;
1301 gssize by_read = 0, by_written = 0;
1303 debug_print("Sending input to grand child.\n");
1304 if (!(cond && GDK_INPUT_WRITE))
1307 gdk_input_remove(child_info->tag_in);
1308 child_info->tag_in = -1;
1310 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1312 ret_str = g_locale_from_utf8(input, strlen(input), &by_read,
1314 if (ret_str && by_written) {
1319 len = strlen(input);
1323 c = write(child_info->chld_in, input + count, len - count);
1326 } while (c >= 0 && count < len);
1329 write(child_info->chld_in, "\n", 2);
1333 gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1334 gtk_widget_set_sensitive(children->input_hbox, TRUE);
1335 close(child_info->chld_in);
1336 child_info->chld_in = -1;
1337 debug_print("Input to grand child sent.\n");
1340 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1342 ChildInfo *child_info = (ChildInfo *)data;
1344 gchar buf[BUFFSIZE];
1346 debug_print("Catching grand child's output.\n");
1347 if (child_info->children->action_type &
1348 (ACTION_PIPE_OUT | ACTION_INSERT)
1349 && source == child_info->chld_out) {
1351 GTK_TEXT_VIEW(child_info->children->msg_text);
1352 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
1357 mark = gtk_text_buffer_get_insert(textbuf);
1358 gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1359 ins_pos = gtk_text_iter_get_offset(&iter);
1362 gsize bytes_read = 0, bytes_written = 0;
1365 c = read(source, buf, sizeof(buf) - 1);
1369 ret_str = g_locale_to_utf8
1370 (buf, c, &bytes_read, &bytes_written, NULL);
1371 if (ret_str && bytes_written > 0) {
1372 gtk_text_buffer_insert
1373 (textbuf, &iter, ret_str,
1377 gtk_text_buffer_insert(textbuf, &iter, buf, c);
1380 if (child_info->children->is_selection) {
1383 gtk_text_buffer_get_iter_at_offset
1384 (textbuf, &ins, ins_pos);
1385 gtk_text_buffer_select_range(textbuf, &ins, &iter);
1388 c = read(source, buf, sizeof(buf) - 1);
1390 gsize bytes_read = 0, bytes_written = 0;
1393 ret_str = g_locale_to_utf8
1394 (buf, c, &bytes_read, &bytes_written, NULL);
1395 if (ret_str && bytes_written > 0) {
1397 (child_info->output, ret_str,
1401 g_string_append_len(child_info->output, buf, c);
1403 child_info->new_out = TRUE;
1407 if (source == child_info->chld_out) {
1408 gdk_input_remove(child_info->tag_out);
1409 child_info->tag_out = -1;
1410 close(child_info->chld_out);
1411 child_info->chld_out = -1;
1413 gdk_input_remove(child_info->tag_err);
1414 child_info->tag_err = -1;
1415 close(child_info->chld_err);
1416 child_info->chld_err = -1;
1420 wait_for_children(child_info->children);
1423 static gchar *get_user_string(const gchar *action, ActionType type)
1426 gchar *user_str = NULL;
1429 case ACTION_USER_HIDDEN_STR:
1430 message = g_strdup_printf
1431 (_("Enter the argument for the following action:\n"
1432 "('%%h' will be replaced with the argument)\n"
1435 user_str = input_dialog_with_invisible
1436 (_("Action's hidden user argument"), message, NULL);
1438 case ACTION_USER_STR:
1439 message = g_strdup_printf
1440 (_("Enter the argument for the following action:\n"
1441 "('%%u' will be replaced with the argument)\n"
1444 user_str = input_dialog
1445 (_("Action's user argument"), message, NULL);
1448 g_warning("Unsupported action type %d", type);