2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 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;
102 void (*callback)(void *data);
106 static void action_update_menu (GtkItemFactory *ifactory,
110 static void compose_actions_execute_cb (Compose *compose,
113 static void mainwin_actions_execute_cb (MainWindow *mainwin,
116 static void msgview_actions_execute_cb (MessageView *msgview,
119 static void message_actions_execute (MessageView *msgview,
123 static gboolean execute_filtering_actions(gchar *action,
126 static gboolean execute_actions (gchar *action,
131 void (*callback)(void *data),
134 static gchar *parse_action_cmd (gchar *action,
138 const gchar *user_str,
139 const gchar *user_hidden_str,
140 const gchar *sel_str);
141 static gboolean parse_append_filename (GString *cmd,
144 static gboolean parse_append_msgpart (GString *cmd,
148 static ChildInfo *fork_child (gchar *cmd,
149 const gchar *msg_str,
152 static gint wait_for_children (Children *children);
154 static void free_children (Children *children);
156 static void childinfo_close_pipes (ChildInfo *child_info);
158 static void create_io_dialog (Children *children);
159 static void update_io_dialog (Children *children);
161 static void hide_io_dialog_cb (GtkWidget *widget,
163 static gint io_dialog_key_pressed_cb (GtkWidget *widget,
167 static void catch_output (gpointer data,
169 GdkInputCondition cond);
170 static void catch_input (gpointer data,
172 GdkInputCondition cond);
173 static void catch_status (gpointer data,
175 GdkInputCondition cond);
177 static gchar *get_user_string (const gchar *action,
181 ActionType action_get_type(const gchar *action_str)
184 ActionType action_type = ACTION_NONE;
186 g_return_val_if_fail(action_str, ACTION_ERROR);
187 g_return_val_if_fail(*action_str, ACTION_ERROR);
192 action_type |= ACTION_PIPE_IN;
194 } else if (p[0] == '>') {
195 action_type |= ACTION_USER_IN;
197 } else if (p[0] == '*') {
198 action_type |= ACTION_USER_HIDDEN_IN;
205 while (*p && action_type != ACTION_ERROR) {
206 if (p[0] == '%' && p[1]) {
209 /* CLAWS: filtering action is a mutually exclusive
210 * action. we can enable others if needed later. we
211 * add ACTION_SINGLE | ACTION_MULTIPLE so it will
212 * only be executed from the main window toolbar */
213 if (p[2] == 's') /* source messages */
214 action_type = ACTION_FILTERING_ACTION
219 action_type |= ACTION_SINGLE;
222 action_type |= ACTION_MULTIPLE;
225 action_type |= ACTION_SINGLE;
228 action_type |= ACTION_SELECTION_STR;
231 action_type |= ACTION_USER_STR;
234 action_type |= ACTION_USER_HIDDEN_STR;
237 action_type = ACTION_ERROR;
240 } else if (p[0] == '|') {
242 action_type |= ACTION_PIPE_OUT;
243 } else if (p[0] == '>') {
245 action_type |= ACTION_INSERT;
246 } else if (p[0] == '&') {
248 action_type |= ACTION_ASYNC;
256 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
257 GSList *msg_list, MimeInfo *partinfo,
258 const gchar *user_str,
259 const gchar *user_hidden_str,
260 const gchar *sel_str)
268 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
271 cmd = g_string_sized_new(strlen(action));
274 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
275 if (p[0] == '%' && p[1]) {
278 if (!parse_append_filename(cmd, msginfo)) {
279 g_string_free(cmd, TRUE);
285 for (cur = msg_list; cur != NULL;
287 MsgInfo *msg = (MsgInfo *)cur->data;
289 if (!parse_append_filename(cmd, msg)) {
290 g_string_free(cmd, TRUE);
294 g_string_append_c(cmd, ' ');
299 if (!parse_append_msgpart(cmd, msginfo,
301 g_string_free(cmd, TRUE);
308 g_string_append(cmd, sel_str);
313 g_string_append(cmd, user_str);
318 g_string_append(cmd, user_hidden_str);
322 g_string_append_c(cmd, p[0]);
323 g_string_append_c(cmd, p[1]);
327 g_string_append_c(cmd, p[0]);
332 g_string_free(cmd, TRUE);
337 g_string_free(cmd, FALSE);
341 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
345 gchar escape_ch[] = "\\ ";
347 g_return_val_if_fail(msginfo, FALSE);
349 filename = procmsg_get_message_file(msginfo);
352 alertpanel_error(_("Could not get message file %d"),
358 while ((q = strpbrk(p, "$\"`'\\ \t*?[]&|;<>()!#~")) != NULL) {
361 g_string_append(cmd, p);
362 g_string_append(cmd, escape_ch);
365 g_string_append(cmd, p);
372 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
375 gboolean single_part = FALSE;
377 gchar *part_filename;
381 partinfo = procmime_scan_message(msginfo);
383 alertpanel_error(_("Could not get message part."));
390 filename = procmsg_get_message_file_path(msginfo);
391 part_filename = procmime_get_tmp_file_name(partinfo);
393 ret = procmime_get_part(part_filename, partinfo);
396 procmime_mimeinfo_free_all(partinfo);
400 alertpanel_error(_("Can't get part of multipart message"));
401 g_free(part_filename);
405 g_string_append(cmd, part_filename);
407 g_free(part_filename);
412 void actions_execute(gpointer data,
417 if (source == TOOLBAR_MAIN)
418 mainwin_actions_execute_cb((MainWindow*)data, action_nb, widget);
419 else if (source == TOOLBAR_COMPOSE)
420 compose_actions_execute_cb((Compose*)data, action_nb, widget);
421 else if (source == TOOLBAR_MSGVIEW)
422 msgview_actions_execute_cb((MessageView*)data, action_nb, widget);
425 void action_update_mainwin_menu(GtkItemFactory *ifactory,
429 action_update_menu(ifactory, branch_path,
430 mainwin_actions_execute_cb, mainwin);
433 void action_update_msgview_menu(GtkItemFactory *ifactory,
435 MessageView *msgview)
437 action_update_menu(ifactory, branch_path,
438 msgview_actions_execute_cb, msgview);
441 void action_update_compose_menu(GtkItemFactory *ifactory,
445 action_update_menu(ifactory, branch_path,
446 compose_actions_execute_cb, compose);
449 static void action_update_menu(GtkItemFactory *ifactory,
451 gpointer callback, gpointer data)
456 gchar *action, *action_p;
458 GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
460 ifentry.path = branch_path;
461 menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
462 g_return_if_fail(menuitem != NULL);
464 amenu = GTK_MENU_SHELL(menuitem)->children;
465 while (amenu != NULL) {
466 GList *alist = amenu->next;
467 gtk_widget_destroy(GTK_WIDGET(amenu->data));
471 ifentry.accelerator = NULL;
472 ifentry.callback_action = 0;
473 ifentry.callback = callback;
474 ifentry.item_type = NULL;
476 for (cur = prefs_common.actions_list; cur; cur = cur->next) {
477 action = g_strdup((gchar *)cur->data);
478 action_p = strstr(action, ": ");
479 if (action_p && action_p[2] &&
480 (action_get_type(&action_p[2]) != ACTION_ERROR) &&
481 (action[0] != '/')) {
483 menu_path = g_strdup_printf("%s/%s", branch_path,
485 ifentry.path = menu_path;
486 gtk_item_factory_create_item(ifactory, &ifentry, data,
491 ifentry.callback_action++;
495 static void compose_actions_execute_cb(Compose *compose, guint action_nb,
499 ActionType action_type;
501 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
503 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
504 g_return_if_fail(buf != NULL);
505 action = strstr(buf, ": ");
506 g_return_if_fail(action != NULL);
508 /* Point to the beginning of the command-line */
511 action_type = action_get_type(action);
512 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
514 (_("The selected action cannot be used in the compose window\n"
515 "because it contains %%f, %%F, %%as or %%p."));
519 execute_actions(action, NULL, compose->text, 0, NULL,
520 compose_action_cb, compose);
523 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
528 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
529 message_actions_execute(mainwin->messageview, action_nb, msg_list);
530 g_slist_free(msg_list);
533 static void msgview_actions_execute_cb(MessageView *msgview, guint action_nb,
536 GSList *msg_list = NULL;
538 if (msgview->msginfo)
539 msg_list = g_slist_append(msg_list, msgview->msginfo);
540 message_actions_execute(msgview, action_nb, msg_list);
541 g_slist_free(msg_list);
544 static void message_actions_execute(MessageView *msgview, guint action_nb,
551 GtkWidget *text = NULL;
553 ActionType action_type;
555 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
557 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
559 g_return_if_fail(buf);
560 g_return_if_fail((action = strstr(buf, ": ")));
562 /* Point to the beginning of the command-line */
565 textview = messageview_get_current_textview(msgview);
567 text = textview->text;
568 body_pos = textview->body_pos;
570 partinfo = messageview_get_selected_mime_part(msgview);
572 /* this command will alter the message text */
573 action_type = action_get_type(action);
574 if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
575 msgview->filtered = TRUE;
577 if (action_type & ACTION_FILTERING_ACTION)
578 /* CLAWS: most of the above code is not necessary for applying
580 execute_filtering_actions(action, msg_list);
582 execute_actions(action, msg_list, text, body_pos, partinfo,
586 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
588 GSList *action_list, *p;
589 const gchar *sbegin, *send;
590 gchar *action_string;
592 if (NULL == (sbegin = strstr2(action, "%as{")))
594 sbegin += sizeof "%as{" - 1;
595 if (NULL == (send = strrchr(sbegin, '}')))
597 action_string = g_strndup(sbegin, send - sbegin);
599 action_list = matcher_parser_get_action_list(action_string);
600 g_free(action_string);
601 if (action_list == NULL) return FALSE;
603 /* apply actions on each message info */
604 for (p = msglist; p && p->data; p = g_slist_next(p))
605 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
607 for (p = action_list; p; p = g_slist_next(p))
608 if (p->data) filteringaction_free(p->data);
609 g_slist_free(action_list);
613 static gboolean execute_actions(gchar *action, GSList *msg_list,
615 gint body_pos, MimeInfo *partinfo,
616 void (*callback)(void *data), void *data)
618 GSList *children_list = NULL;
622 ChildInfo *child_info;
623 ActionType action_type;
626 gchar *sel_str = NULL;
627 gchar *msg_str = NULL;
628 gchar *user_str = NULL;
629 gchar *user_hidden_str = NULL;
630 GtkTextIter start_iter, end_iter;
631 gboolean is_selection = FALSE;
633 g_return_val_if_fail(action && *action, FALSE);
635 action_type = action_get_type(action);
637 if (action_type == ACTION_ERROR)
638 return FALSE; /* ERR: syntax error */
640 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
641 return FALSE; /* ERR: file command without selection */
643 msg_list_len = g_slist_length(msg_list);
645 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
646 if (msg_list_len > 1)
647 return FALSE; /* ERR: pipe + multiple selection */
649 return FALSE; /* ERR: pipe and no displayed text */
652 if (action_type & ACTION_SELECTION_STR) {
654 return FALSE; /* ERR: selection string but no text */
658 GtkTextBuffer *textbuf;
660 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
661 is_selection = gtk_text_buffer_get_selection_bounds
662 (textbuf, &start_iter, &end_iter);
664 gtk_text_buffer_get_iter_at_offset
665 (textbuf, &start_iter, body_pos);
666 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
668 msg_str = gtk_text_buffer_get_text
669 (textbuf, &start_iter, &end_iter, FALSE);
671 sel_str = g_strdup(msg_str);
674 if (action_type & ACTION_USER_STR) {
675 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
682 if (action_type & ACTION_USER_HIDDEN_STR) {
683 if (!(user_hidden_str =
684 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
692 if (text && (action_type & ACTION_PIPE_OUT)) {
693 GtkTextBuffer *textbuf;
694 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
695 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
698 children = g_new0(Children, 1);
700 children->action = g_strdup(action);
701 children->action_type = action_type;
702 children->msg_text = text;
703 children->is_selection = is_selection;
705 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
706 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
707 children->open_in = 1;
709 if (action_type & ACTION_SINGLE) {
712 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
713 msginfo = (MsgInfo *)cur->data;
715 is_ok = FALSE; /* ERR: msginfo missing */
718 cmd = parse_action_cmd(action, msginfo, msg_list,
720 user_hidden_str, sel_str);
722 debug_print("Action command error\n");
723 is_ok = FALSE; /* ERR: incorrect command */
726 if ((child_info = fork_child(cmd, msg_str, children))) {
727 children_list = g_slist_append(children_list,
733 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
734 user_str, user_hidden_str, sel_str);
736 if ((child_info = fork_child(cmd, msg_str, children))) {
737 children_list = g_slist_append(children_list,
742 is_ok = FALSE; /* ERR: incorrect command */
748 g_free(user_hidden_str);
750 if (!children_list) {
751 /* If not waiting for children, return */
752 free_children(children);
756 children->list = children_list;
757 children->nb = g_slist_length(children_list);
758 children->initial_nb = children->nb;
760 for (cur = children_list; cur; cur = cur->next) {
761 child_info = (ChildInfo *) cur->data;
762 child_info->callback = callback;
763 child_info->data = data;
764 child_info->tag_status =
765 gdk_input_add(child_info->chld_status,
767 catch_status, child_info);
770 create_io_dialog(children);
775 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
779 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
780 gchar *cmdline[4], *ret_str;
782 ChildInfo *child_info;
784 gssize by_written = 0, by_read = 0;
786 sync = !(children->action_type & ACTION_ASYNC);
788 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
789 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
792 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
794 alertpanel_error(_("Command could not be started. "
795 "Pipe creation failed.\n%s"),
797 /* Closing fd = -1 fails silently */
804 close(chld_status[0]);
805 close(chld_status[1]);
806 return NULL; /* Pipe error */
810 debug_print("Forking child and grandchild.\n");
811 debug_print("Executing: /bin/sh -c %s\n", cmd);
814 if (pid == 0) { /* Child */
818 #ifdef GDK_WINDOWING_X11
819 close(ConnectionNumber(gdk_display));
820 #endif /* GDK_WINDOWING_X11 */
825 if (setpgid(0, getppid()))
829 if (children->action_type &
832 ACTION_USER_HIDDEN_IN)) {
833 close(fileno(stdin));
839 close(fileno(stdout));
844 close(fileno(stderr));
852 ret_str = g_locale_from_utf8(cmd, strlen(cmd),
853 &by_read, &by_written,
855 if (ret_str && by_written)
856 cmdline[2] = ret_str;
860 execvp("/bin/sh", cmdline);
865 } else if (gch_pid < (pid_t) 0) { /* Fork error */
867 write(chld_status[1], "1\n", 2);
878 close(chld_status[0]);
880 debug_print("Child: Waiting for grandchild\n");
881 waitpid(gch_pid, NULL, 0);
882 debug_print("Child: grandchild ended\n");
883 write(chld_status[1], "0\n", 2);
884 close(chld_status[1]);
888 } else if (pid < 0) { /* Fork error */
889 alertpanel_error(_("Could not fork to execute the following "
891 cmd, g_strerror(errno));
898 waitpid(pid, NULL, 0);
903 if (!(children->action_type &
904 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
908 close(chld_status[1]);
910 child_info = g_new0(ChildInfo, 1);
912 child_info->children = children;
914 child_info->pid = pid;
915 child_info->cmd = g_strdup(cmd);
916 child_info->new_out = FALSE;
917 child_info->output = g_string_new(NULL);
918 child_info->chld_in =
919 (children->action_type &
920 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
922 child_info->chld_out = chld_out[0];
923 child_info->chld_err = chld_err[0];
924 child_info->chld_status = chld_status[0];
925 child_info->tag_in = -1;
926 child_info->tag_out = gdk_input_add(chld_out[0], GDK_INPUT_READ,
927 catch_output, child_info);
928 child_info->tag_err = gdk_input_add(chld_err[0], GDK_INPUT_READ,
929 catch_output, child_info);
931 if (!(children->action_type &
932 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
935 if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
936 ret_str = g_locale_from_utf8(msg_str, strlen(msg_str),
937 &by_read, &by_written, NULL);
938 if (ret_str && by_written) {
939 write(chld_in[1], ret_str, strlen(ret_str));
942 write(chld_in[1], msg_str, strlen(msg_str));
943 if (!(children->action_type &
944 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
946 child_info->chld_in = -1; /* No more input */
952 #endif /* G_OS_UNIX */
955 static void kill_children_cb(GtkWidget *widget, gpointer data)
959 Children *children = (Children *) data;
960 ChildInfo *child_info;
962 for (cur = children->list; cur; cur = cur->next) {
963 child_info = (ChildInfo *)(cur->data);
964 debug_print("Killing child group id %d\n", child_info->pid);
965 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
968 #endif /* G_OS_UNIX */
971 static gint wait_for_children(Children *children)
974 ChildInfo *child_info;
976 gint nb = children->nb;
980 cur = children->list;
983 child_info = (ChildInfo *)cur->data;
986 new_output |= child_info->new_out;
990 children->output |= new_output;
992 if (new_output || (children->dialog && (nb != children->nb)))
993 update_io_dialog(children);
998 if (!children->dialog) {
999 free_children(children);
1000 } else if (!children->output) {
1001 gtk_widget_destroy(children->dialog);
1007 static void send_input(GtkWidget *w, gpointer data)
1009 Children *children = (Children *) data;
1010 ChildInfo *child_info = (ChildInfo *) children->list->data;
1012 child_info->tag_in = gdk_input_add(child_info->chld_in,
1014 catch_input, children);
1015 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1018 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1020 hide_io_dialog_cb(w, data);
1024 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1027 Children *children = (Children *)data;
1029 if (!children->nb) {
1030 g_signal_handlers_disconnect_matched
1031 (G_OBJECT(children->dialog), G_SIGNAL_MATCH_DATA,
1032 0, 0, NULL, NULL, children);
1033 gtk_widget_destroy(children->dialog);
1034 free_children(children);
1038 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1041 if (event && event->keyval == GDK_Escape)
1042 hide_io_dialog_cb(widget, data);
1046 static void childinfo_close_pipes(ChildInfo *child_info)
1048 /* stdout and stderr pipes are guaranteed to be removed by
1049 * their handler, but in case where we receive child exit notification
1050 * before grand-child's pipes closing signals, we check them and close
1053 if (child_info->tag_in > 0)
1054 gdk_input_remove(child_info->tag_in);
1055 if (child_info->tag_out > 0)
1056 gdk_input_remove(child_info->tag_out);
1057 if (child_info->tag_err > 0)
1058 gdk_input_remove(child_info->tag_err);
1060 if (child_info->chld_in >= 0)
1061 close(child_info->chld_in);
1062 if (child_info->chld_out >= 0)
1063 close(child_info->chld_out);
1064 if (child_info->chld_err >= 0)
1065 close(child_info->chld_err);
1067 close(child_info->chld_status);
1070 static void free_children(Children *children)
1072 ChildInfo *child_info;
1073 void (*callback)(void *data) = NULL;
1076 debug_print("Freeing children data %p\n", children);
1078 g_free(children->action);
1079 while (children->list != NULL) {
1080 child_info = (ChildInfo *)children->list->data;
1081 g_free(child_info->cmd);
1082 g_string_free(child_info->output, TRUE);
1083 children->list = g_slist_remove(children->list, child_info);
1084 callback = child_info->callback;
1085 data = child_info->data;
1095 static void update_io_dialog(Children *children)
1099 debug_print("Updating actions input/output dialog.\n");
1101 if (children->progress_bar) {
1104 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar),
1105 (gdouble) (children->initial_nb - children->nb) /
1106 (gdouble) children->initial_nb);
1107 text = g_strdup_printf("%s %d/%d", _("Completed"),
1108 children->initial_nb - children->nb,
1109 children->initial_nb);
1110 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(children->progress_bar), text);
1114 if (!children->nb) {
1115 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1116 gtk_widget_set_sensitive(children->close_btn, TRUE);
1117 if (children->input_hbox)
1118 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1119 gtk_widget_grab_focus(children->close_btn);
1120 g_signal_connect(G_OBJECT(children->dialog),
1122 G_CALLBACK(io_dialog_key_pressed_cb),
1126 if (children->output) {
1127 GtkWidget *text = children->text;
1128 GtkTextBuffer *textbuf;
1129 GtkTextIter iter, start_iter, end_iter;
1131 ChildInfo *child_info;
1133 gtk_widget_show(children->scrolledwin);
1134 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
1135 gtk_text_buffer_get_bounds(textbuf, &start_iter, &end_iter);
1136 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
1137 gtk_text_buffer_get_start_iter(textbuf, &iter);
1139 for (cur = children->list; cur; cur = cur->next) {
1140 child_info = (ChildInfo *)cur->data;
1141 if (child_info->pid)
1142 caption = g_strdup_printf
1143 (_("--- Running: %s\n"),
1146 caption = g_strdup_printf
1147 (_("--- Ended: %s\n"),
1150 gtk_text_buffer_insert(textbuf, &iter, caption, -1);
1151 gtk_text_buffer_insert(textbuf, &iter,
1152 child_info->output->str, -1);
1154 child_info->new_out = FALSE;
1159 static void create_io_dialog(Children *children)
1163 GtkWidget *entry = NULL;
1164 GtkWidget *input_hbox = NULL;
1165 GtkWidget *send_button;
1168 GtkWidget *scrolledwin;
1170 GtkWidget *progress_bar = NULL;
1171 GtkWidget *abort_button;
1172 GtkWidget *close_button;
1174 debug_print("Creating action IO dialog\n");
1176 dialog = gtk_dialog_new();
1177 gtk_container_set_border_width
1178 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1179 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1180 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1181 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1182 manage_window_set_transient(GTK_WINDOW(dialog));
1183 g_signal_connect(G_OBJECT(dialog), "delete_event",
1184 G_CALLBACK(delete_io_dialog_cb), children);
1185 g_signal_connect(G_OBJECT(dialog), "destroy",
1186 G_CALLBACK(hide_io_dialog_cb),
1189 vbox = gtk_vbox_new(FALSE, 8);
1190 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1191 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1192 gtk_widget_show(vbox);
1194 label = gtk_label_new(children->action);
1195 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1196 gtk_widget_show(label);
1198 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1199 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1200 GTK_POLICY_AUTOMATIC,
1201 GTK_POLICY_AUTOMATIC);
1202 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
1204 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1205 gtk_widget_set_size_request(scrolledwin, 560, 200);
1206 gtk_widget_hide(scrolledwin);
1208 text = gtk_text_view_new();
1210 if (prefs_common.textfont) {
1211 PangoFontDescription *font_desc;
1212 font_desc = pango_font_description_from_string
1213 (prefs_common.textfont);
1215 gtk_widget_modify_font(text, font_desc);
1216 pango_font_description_free(font_desc);
1220 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
1221 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
1222 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
1223 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
1224 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1225 gtk_widget_show(text);
1227 if (children->open_in) {
1228 input_hbox = gtk_hbox_new(FALSE, 8);
1229 gtk_widget_show(input_hbox);
1231 entry = gtk_entry_new();
1232 gtk_widget_set_size_request(entry, 320, -1);
1233 g_signal_connect(G_OBJECT(entry), "activate",
1234 G_CALLBACK(send_input), children);
1235 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1236 if (children->action_type & ACTION_USER_HIDDEN_IN)
1237 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1238 gtk_widget_show(entry);
1240 send_button = gtk_button_new_from_stock(GTK_STOCK_EXECUTE);
1241 g_signal_connect(G_OBJECT(send_button), "clicked",
1242 G_CALLBACK(send_input), children);
1243 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1245 gtk_widget_show(send_button);
1247 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1248 gtk_widget_grab_focus(entry);
1251 if (children->initial_nb > 1) {
1254 progress_bar = gtk_progress_bar_new();
1255 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1256 GTK_PROGRESS_LEFT_TO_RIGHT);
1257 text = g_strdup_printf("%s 0/%d\n", _("Completed"),
1258 children->initial_nb);
1259 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar),
1262 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1263 gtk_widget_show(progress_bar);
1266 gtkut_stock_button_set_create(&hbox, &abort_button, GTK_STOCK_STOP,
1267 &close_button, GTK_STOCK_CLOSE, NULL, NULL);
1268 g_signal_connect(G_OBJECT(abort_button), "clicked",
1269 G_CALLBACK(kill_children_cb), children);
1270 g_signal_connect(G_OBJECT(close_button), "clicked",
1271 G_CALLBACK(hide_io_dialog_cb), children);
1272 gtk_widget_show(hbox);
1275 gtk_widget_set_sensitive(close_button, FALSE);
1277 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1279 children->dialog = dialog;
1280 children->scrolledwin = scrolledwin;
1281 children->text = text;
1282 children->input_hbox = children->open_in ? input_hbox : NULL;
1283 children->input_entry = children->open_in ? entry : NULL;
1284 children->progress_bar = progress_bar;
1285 children->abort_btn = abort_button;
1286 children->close_btn = close_button;
1288 gtk_widget_show(dialog);
1291 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1293 ChildInfo *child_info = (ChildInfo *)data;
1297 gdk_input_remove(child_info->tag_status);
1299 c = read(source, &buf, 1);
1300 debug_print("Child returned %c\n", buf);
1303 waitpid(-child_info->pid, NULL, 0);
1305 childinfo_close_pipes(child_info);
1306 child_info->pid = 0;
1308 wait_for_children(child_info->children);
1311 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1313 Children *children = (Children *)data;
1314 ChildInfo *child_info = (ChildInfo *)children->list->data;
1315 gchar *input, *ret_str;
1317 gssize by_read = 0, by_written = 0;
1319 debug_print("Sending input to grand child.\n");
1320 if (!(cond && GDK_INPUT_WRITE))
1323 gdk_input_remove(child_info->tag_in);
1324 child_info->tag_in = -1;
1326 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1328 ret_str = g_locale_from_utf8(input, strlen(input), &by_read,
1330 if (ret_str && by_written) {
1335 len = strlen(input);
1339 c = write(child_info->chld_in, input + count, len - count);
1342 } while (c >= 0 && count < len);
1345 write(child_info->chld_in, "\n", 2);
1349 gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1350 gtk_widget_set_sensitive(children->input_hbox, TRUE);
1351 close(child_info->chld_in);
1352 child_info->chld_in = -1;
1353 debug_print("Input to grand child sent.\n");
1356 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1358 ChildInfo *child_info = (ChildInfo *)data;
1360 gchar buf[BUFFSIZE];
1362 debug_print("Catching grand child's output.\n");
1363 if (child_info->children->action_type &
1364 (ACTION_PIPE_OUT | ACTION_INSERT)
1365 && source == child_info->chld_out) {
1367 GTK_TEXT_VIEW(child_info->children->msg_text);
1368 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
1373 mark = gtk_text_buffer_get_insert(textbuf);
1374 gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1375 ins_pos = gtk_text_iter_get_offset(&iter);
1378 gsize bytes_read = 0, bytes_written = 0;
1381 c = read(source, buf, sizeof(buf) - 1);
1385 ret_str = g_locale_to_utf8
1386 (buf, c, &bytes_read, &bytes_written, NULL);
1387 if (ret_str && bytes_written > 0) {
1388 gtk_text_buffer_insert
1389 (textbuf, &iter, ret_str,
1393 gtk_text_buffer_insert(textbuf, &iter, buf, c);
1396 if (child_info->children->is_selection) {
1399 gtk_text_buffer_get_iter_at_offset
1400 (textbuf, &ins, ins_pos);
1401 gtk_text_buffer_select_range(textbuf, &ins, &iter);
1404 c = read(source, buf, sizeof(buf) - 1);
1406 gsize bytes_read = 0, bytes_written = 0;
1409 ret_str = g_locale_to_utf8
1410 (buf, c, &bytes_read, &bytes_written, NULL);
1411 if (ret_str && bytes_written > 0) {
1413 (child_info->output, ret_str,
1417 g_string_append_len(child_info->output, buf, c);
1419 child_info->new_out = TRUE;
1423 if (source == child_info->chld_out) {
1424 gdk_input_remove(child_info->tag_out);
1425 child_info->tag_out = -1;
1426 close(child_info->chld_out);
1427 child_info->chld_out = -1;
1429 gdk_input_remove(child_info->tag_err);
1430 child_info->tag_err = -1;
1431 close(child_info->chld_err);
1432 child_info->chld_err = -1;
1436 wait_for_children(child_info->children);
1439 static gchar *get_user_string(const gchar *action, ActionType type)
1442 gchar *user_str = NULL;
1445 case ACTION_USER_HIDDEN_STR:
1446 message = g_strdup_printf
1447 (_("Enter the argument for the following action:\n"
1448 "('%%h' will be replaced with the argument)\n"
1451 user_str = input_dialog_with_invisible
1452 (_("Action's hidden user argument"), message, NULL);
1454 case ACTION_USER_STR:
1455 message = g_strdup_printf
1456 (_("Enter the argument for the following action:\n"
1457 "('%%u' will be replaced with the argument)\n"
1460 user_str = input_dialog
1461 (_("Action's user argument"), message, NULL);
1464 g_warning("Unsupported action type %d", type);