2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 Hiroyuki Yamamoto & The Sylpheed Claws Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include <gdk/gdkkeysyms.h>
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 g_return_val_if_fail(msginfo, FALSE);
341 filename = procmsg_get_message_file(msginfo);
344 g_string_append(cmd, filename);
347 alertpanel_error(_("Could not get message file %d"),
355 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
358 gboolean single_part = FALSE;
360 gchar *part_filename;
364 partinfo = procmime_scan_message(msginfo);
366 alertpanel_error(_("Could not get message part."));
373 filename = procmsg_get_message_file_path(msginfo);
374 part_filename = procmime_get_tmp_file_name(partinfo);
376 ret = procmime_get_part(part_filename, partinfo);
379 procmime_mimeinfo_free_all(partinfo);
383 alertpanel_error(_("Can't get part of multipart message"));
384 g_free(part_filename);
388 g_string_append(cmd, part_filename);
390 g_free(part_filename);
395 void actions_execute(gpointer data,
400 if (source == TOOLBAR_MAIN)
401 mainwin_actions_execute_cb((MainWindow*)data, action_nb, widget);
402 else if (source == TOOLBAR_COMPOSE)
403 compose_actions_execute_cb((Compose*)data, action_nb, widget);
404 else if (source == TOOLBAR_MSGVIEW)
405 msgview_actions_execute_cb((MessageView*)data, action_nb, widget);
408 void action_update_mainwin_menu(GtkItemFactory *ifactory,
412 action_update_menu(ifactory, branch_path,
413 mainwin_actions_execute_cb, mainwin);
416 void action_update_msgview_menu(GtkItemFactory *ifactory,
418 MessageView *msgview)
420 action_update_menu(ifactory, branch_path,
421 msgview_actions_execute_cb, msgview);
424 void action_update_compose_menu(GtkItemFactory *ifactory,
428 action_update_menu(ifactory, branch_path,
429 compose_actions_execute_cb, compose);
432 static void action_update_menu(GtkItemFactory *ifactory,
434 gpointer callback, gpointer data)
439 gchar *action, *action_p;
441 GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
443 ifentry.path = branch_path;
444 menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
445 g_return_if_fail(menuitem != NULL);
447 amenu = GTK_MENU_SHELL(menuitem)->children;
448 while (amenu != NULL) {
449 GList *alist = amenu->next;
450 gtk_widget_destroy(GTK_WIDGET(amenu->data));
454 ifentry.accelerator = NULL;
455 ifentry.callback_action = 0;
456 ifentry.callback = callback;
457 ifentry.item_type = NULL;
459 for (cur = prefs_common.actions_list; cur; cur = cur->next) {
460 action = g_strdup((gchar *)cur->data);
461 action_p = strstr(action, ": ");
462 if (action_p && action_p[2] &&
463 action_get_type(&action_p[2]) != ACTION_ERROR) {
465 menu_path = g_strdup_printf("%s/%s", branch_path,
467 ifentry.path = menu_path;
468 gtk_item_factory_create_item(ifactory, &ifentry, data,
473 ifentry.callback_action++;
477 static void compose_actions_execute_cb(Compose *compose, guint action_nb,
481 ActionType action_type;
483 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
485 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
486 g_return_if_fail(buf != NULL);
487 action = strstr(buf, ": ");
488 g_return_if_fail(action != NULL);
490 /* Point to the beginning of the command-line */
493 action_type = action_get_type(action);
494 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
496 (_("The selected action cannot be used in the compose window\n"
497 "because it contains %%f, %%F, %%as or %%p."));
501 execute_actions(action, NULL, compose->text, 0, NULL);
504 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
509 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
510 message_actions_execute(mainwin->messageview, action_nb, msg_list);
511 g_slist_free(msg_list);
514 static void msgview_actions_execute_cb(MessageView *msgview, guint action_nb,
517 GSList *msg_list = NULL;
519 if (msgview->msginfo)
520 msg_list = g_slist_append(msg_list, msgview->msginfo);
521 message_actions_execute(msgview, action_nb, msg_list);
522 g_slist_free(msg_list);
525 static void message_actions_execute(MessageView *msgview, guint action_nb,
532 GtkWidget *text = NULL;
534 ActionType action_type;
536 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
538 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
540 g_return_if_fail(buf);
541 g_return_if_fail((action = strstr(buf, ": ")));
543 /* Point to the beginning of the command-line */
546 textview = messageview_get_current_textview(msgview);
548 text = textview->text;
549 body_pos = textview->body_pos;
551 partinfo = messageview_get_selected_mime_part(msgview);
553 /* this command will alter the message text */
554 action_type = action_get_type(action);
555 if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
556 msgview->filtered = TRUE;
558 if (action_type & ACTION_FILTERING_ACTION)
559 /* CLAWS: most of the above code is not necessary for applying
561 execute_filtering_actions(action, msg_list);
563 execute_actions(action, msg_list, text, body_pos, partinfo);
566 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
568 GSList *action_list, *p;
569 const gchar *sbegin, *send;
570 gchar *action_string;
572 if (NULL == (sbegin = strstr2(action, "%as{")))
574 sbegin += sizeof "%as{" - 1;
575 if (NULL == (send = strrchr(sbegin, '}')))
577 action_string = g_strndup(sbegin, send - sbegin);
579 action_list = matcher_parser_get_action_list(action_string);
580 g_free(action_string);
581 if (action_list == NULL) return FALSE;
583 /* apply actions on each message info */
584 for (p = msglist; p && p->data; p = g_slist_next(p))
585 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
587 for (p = action_list; p; p = g_slist_next(p))
588 if (p->data) filteringaction_free(p->data);
589 g_slist_free(action_list);
593 static gboolean execute_actions(gchar *action, GSList *msg_list,
595 gint body_pos, MimeInfo *partinfo)
597 GSList *children_list = NULL;
601 ChildInfo *child_info;
602 ActionType action_type;
605 gchar *sel_str = NULL;
606 gchar *msg_str = NULL;
607 gchar *user_str = NULL;
608 gchar *user_hidden_str = NULL;
609 GtkTextIter start_iter, end_iter;
610 gboolean is_selection = FALSE;
612 g_return_val_if_fail(action && *action, FALSE);
614 action_type = action_get_type(action);
616 if (action_type == ACTION_ERROR)
617 return FALSE; /* ERR: syntax error */
619 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
620 return FALSE; /* ERR: file command without selection */
622 msg_list_len = g_slist_length(msg_list);
624 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
625 if (msg_list_len > 1)
626 return FALSE; /* ERR: pipe + multiple selection */
628 return FALSE; /* ERR: pipe and no displayed text */
631 if (action_type & ACTION_SELECTION_STR) {
633 return FALSE; /* ERR: selection string but no text */
637 GtkTextBuffer *textbuf;
639 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
640 is_selection = gtk_text_buffer_get_selection_bounds(textbuf,
644 gtk_text_buffer_get_start_iter(textbuf, &start_iter);
645 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
647 msg_str = gtk_text_buffer_get_text(textbuf,
648 &start_iter, &end_iter,
651 sel_str = g_strdup (msg_str);
654 if (action_type & ACTION_USER_STR) {
655 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
662 if (action_type & ACTION_USER_HIDDEN_STR) {
663 if (!(user_hidden_str =
664 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
672 if (text && (action_type & ACTION_PIPE_OUT)) {
673 GtkTextBuffer *textbuf;
674 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
675 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
678 children = g_new0(Children, 1);
680 children->action = g_strdup(action);
681 children->action_type = action_type;
682 children->msg_text = text;
683 children->is_selection = is_selection;
685 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
686 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
687 children->open_in = 1;
689 if (action_type & ACTION_SINGLE) {
692 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
693 msginfo = (MsgInfo *)cur->data;
695 is_ok = FALSE; /* ERR: msginfo missing */
698 cmd = parse_action_cmd(action, msginfo, msg_list,
700 user_hidden_str, sel_str);
702 debug_print("Action command error\n");
703 is_ok = FALSE; /* ERR: incorrect command */
706 if ((child_info = fork_child(cmd, msg_str, children))) {
707 children_list = g_slist_append(children_list,
713 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
714 user_str, user_hidden_str, sel_str);
716 if ((child_info = fork_child(cmd, msg_str, children))) {
717 children_list = g_slist_append(children_list,
722 is_ok = FALSE; /* ERR: incorrect command */
728 g_free(user_hidden_str);
730 if (!children_list) {
731 /* If not waiting for children, return */
732 free_children(children);
736 children->list = children_list;
737 children->nb = g_slist_length(children_list);
738 children->initial_nb = children->nb;
740 for (cur = children_list; cur; cur = cur->next) {
741 child_info = (ChildInfo *) cur->data;
742 child_info->tag_status =
743 gdk_input_add(child_info->chld_status,
745 catch_status, child_info);
748 create_io_dialog(children);
754 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
757 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
760 ChildInfo *child_info;
763 sync = !(children->action_type & ACTION_ASYNC);
765 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
766 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
769 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
771 alertpanel_error(_("Command could not be started. "
772 "Pipe creation failed.\n%s"),
774 /* Closing fd = -1 fails silently */
781 close(chld_status[0]);
782 close(chld_status[1]);
783 return NULL; /* Pipe error */
787 debug_print("Forking child and grandchild.\n");
790 if (pid == 0) { /* Child */
794 #ifdef GDK_WINDOWING_X11
795 close(ConnectionNumber(gdk_display));
796 #endif /* GDK_WINDOWING_X11 */
801 if (setpgid(0, getppid()))
805 if (children->action_type &
808 ACTION_USER_HIDDEN_IN)) {
809 close(fileno(stdin));
815 close(fileno(stdout));
820 close(fileno(stderr));
830 execvp("/bin/sh", cmdline);
834 } else if (gch_pid < (pid_t) 0) { /* Fork error */
836 write(chld_status[1], "1\n", 2);
847 close(chld_status[0]);
849 debug_print("Child: Waiting for grandchild\n");
850 waitpid(gch_pid, NULL, 0);
851 debug_print("Child: grandchild ended\n");
852 write(chld_status[1], "0\n", 2);
853 close(chld_status[1]);
857 } else if (pid < 0) { /* Fork error */
858 alertpanel_error(_("Could not fork to execute the following "
860 cmd, g_strerror(errno));
867 waitpid(pid, NULL, 0);
872 if (!(children->action_type &
873 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
877 close(chld_status[1]);
879 child_info = g_new0(ChildInfo, 1);
881 child_info->children = children;
883 child_info->pid = pid;
884 child_info->cmd = g_strdup(cmd);
885 child_info->new_out = FALSE;
886 child_info->output = g_string_new(NULL);
887 child_info->chld_in =
888 (children->action_type &
889 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
891 child_info->chld_out = chld_out[0];
892 child_info->chld_err = chld_err[0];
893 child_info->chld_status = chld_status[0];
894 child_info->tag_in = -1;
895 child_info->tag_out = gdk_input_add(chld_out[0], GDK_INPUT_READ,
896 catch_output, child_info);
897 child_info->tag_err = gdk_input_add(chld_err[0], GDK_INPUT_READ,
898 catch_output, child_info);
900 if (!(children->action_type &
901 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
904 if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
905 write(chld_in[1], msg_str, strlen(msg_str));
906 if (!(children->action_type &
907 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
909 child_info->chld_in = -1; /* No more input */
915 static void kill_children_cb(GtkWidget *widget, gpointer data)
918 Children *children = (Children *) data;
919 ChildInfo *child_info;
921 for (cur = children->list; cur; cur = cur->next) {
922 child_info = (ChildInfo *)(cur->data);
923 debug_print("Killing child group id %d\n", child_info->pid);
924 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
929 static gint wait_for_children(Children *children)
932 ChildInfo *child_info;
934 gint nb = children->nb;
938 cur = children->list;
941 child_info = (ChildInfo *)cur->data;
944 new_output |= child_info->new_out;
948 children->output |= new_output;
950 if (new_output || (children->dialog && (nb != children->nb)))
951 update_io_dialog(children);
956 if (!children->dialog) {
957 free_children(children);
958 } else if (!children->output) {
959 gtk_widget_destroy(children->dialog);
965 static void send_input(GtkWidget *w, gpointer data)
967 Children *children = (Children *) data;
968 ChildInfo *child_info = (ChildInfo *) children->list->data;
970 child_info->tag_in = gdk_input_add(child_info->chld_in,
972 catch_input, children);
973 gtk_widget_set_sensitive(children->input_hbox, FALSE);
976 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
978 hide_io_dialog_cb(w, data);
982 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
985 Children *children = (Children *)data;
988 g_signal_handlers_disconnect_matched
989 (G_OBJECT(children->dialog), G_SIGNAL_MATCH_DATA,
990 0, 0, NULL, NULL, children);
991 gtk_widget_destroy(children->dialog);
992 free_children(children);
996 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
999 if (event && event->keyval == GDK_Escape)
1000 hide_io_dialog_cb(widget, data);
1004 static void childinfo_close_pipes(ChildInfo *child_info)
1006 /* stdout and stderr pipes are guaranteed to be removed by
1007 * their handler, but in case where we receive child exit notification
1008 * before grand-child's pipes closing signals, we check them and close
1011 if (child_info->tag_in > 0)
1012 gdk_input_remove(child_info->tag_in);
1013 if (child_info->tag_out > 0)
1014 gdk_input_remove(child_info->tag_out);
1015 if (child_info->tag_err > 0)
1016 gdk_input_remove(child_info->tag_err);
1018 if (child_info->chld_in >= 0)
1019 close(child_info->chld_in);
1020 if (child_info->chld_out >= 0)
1021 close(child_info->chld_out);
1022 if (child_info->chld_err >= 0)
1023 close(child_info->chld_err);
1025 close(child_info->chld_status);
1028 static void free_children(Children *children)
1030 ChildInfo *child_info;
1032 debug_print("Freeing children data %p\n", children);
1034 g_free(children->action);
1035 while (children->list != NULL) {
1036 child_info = (ChildInfo *)children->list->data;
1037 g_free(child_info->cmd);
1038 g_string_free(child_info->output, TRUE);
1039 children->list = g_slist_remove(children->list, child_info);
1045 static void update_io_dialog(Children *children)
1049 debug_print("Updating actions input/output dialog.\n");
1051 if (children->progress_bar) {
1054 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar),
1055 (gdouble) (children->initial_nb - children->nb) /
1056 (gdouble) children->initial_nb);
1057 text = g_strdup_printf("%s %d/%d", _("Completed"),
1058 children->initial_nb - children->nb,
1059 children->initial_nb);
1060 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(children->progress_bar), text);
1064 if (!children->nb) {
1065 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1066 gtk_widget_set_sensitive(children->close_btn, TRUE);
1067 if (children->input_hbox)
1068 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1069 gtk_widget_grab_focus(children->close_btn);
1070 g_signal_connect(G_OBJECT(children->dialog),
1072 G_CALLBACK(io_dialog_key_pressed_cb),
1076 if (children->output) {
1077 GtkWidget *text = children->text;
1079 ChildInfo *child_info;
1080 GtkTextBuffer *textbuf;
1081 GtkTextIter iter, start_iter, end_iter;
1083 gtk_widget_show(children->scrolledwin);
1084 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
1085 gtk_text_buffer_get_start_iter(textbuf, &start_iter);
1086 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
1087 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
1088 gtk_text_buffer_get_start_iter(textbuf, &start_iter);
1090 for (cur = children->list; cur; cur = cur->next) {
1091 child_info = (ChildInfo *)cur->data;
1092 if (child_info->pid)
1093 caption = g_strdup_printf
1094 (_("--- Running: %s\n"),
1097 caption = g_strdup_printf
1098 (_("--- Ended: %s\n"),
1101 gtk_text_buffer_insert(textbuf, &iter,
1103 gtk_text_buffer_insert(textbuf, &iter,
1104 child_info->output->str, -1);
1106 child_info->new_out = FALSE;
1111 static void create_io_dialog(Children *children)
1115 GtkWidget *entry = NULL;
1116 GtkWidget *input_hbox = NULL;
1117 GtkWidget *send_button;
1120 GtkWidget *scrolledwin;
1122 GtkWidget *progress_bar = NULL;
1123 GtkWidget *abort_button;
1124 GtkWidget *close_button;
1125 PangoFontDescription *text_font;
1127 debug_print("Creating action IO dialog\n");
1129 dialog = gtk_dialog_new();
1130 gtk_container_set_border_width
1131 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1132 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1133 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1134 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1135 manage_window_set_transient(GTK_WINDOW(dialog));
1136 g_signal_connect(G_OBJECT(dialog), "delete_event",
1137 G_CALLBACK(delete_io_dialog_cb), children);
1138 g_signal_connect(G_OBJECT(dialog), "destroy",
1139 G_CALLBACK(hide_io_dialog_cb),
1142 vbox = gtk_vbox_new(FALSE, 8);
1143 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1144 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1145 gtk_widget_show(vbox);
1147 label = gtk_label_new(children->action);
1148 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1149 gtk_widget_show(label);
1151 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1152 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1153 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1154 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1155 gtk_widget_set_size_request(scrolledwin, 480, 200);
1156 gtk_widget_hide(scrolledwin);
1158 text = gtk_text_view_new();
1159 if (prefs_common.textfont) {
1160 if (NULL != (text_font = pango_font_description_from_string
1161 (prefs_common.textfont))) {
1162 gtk_widget_modify_font(text, text_font);
1163 pango_font_description_free(text_font);
1166 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
1167 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1168 gtk_widget_show(text);
1170 if (children->open_in) {
1171 input_hbox = gtk_hbox_new(FALSE, 8);
1172 gtk_widget_show(input_hbox);
1174 entry = gtk_entry_new();
1175 gtk_widget_set_size_request(entry, 320, -1);
1176 g_signal_connect(G_OBJECT(entry), "activate",
1177 G_CALLBACK(send_input), children);
1178 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1179 if (children->action_type & ACTION_USER_HIDDEN_IN)
1180 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1181 gtk_widget_show(entry);
1183 send_button = gtk_button_new_with_label(_(" Send "));
1184 g_signal_connect(G_OBJECT(send_button), "clicked",
1185 G_CALLBACK(send_input), children);
1186 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1188 gtk_widget_show(send_button);
1190 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1191 gtk_widget_grab_focus(entry);
1194 if (children->initial_nb > 1) {
1197 progress_bar = gtk_progress_bar_new();
1198 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1199 GTK_PROGRESS_LEFT_TO_RIGHT);
1200 text = g_strdup_printf("%s 0/%d\n", _("Completed"),
1201 children->initial_nb);
1202 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar),
1205 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1206 gtk_widget_show(progress_bar);
1209 gtkut_button_set_create(&hbox, &abort_button, _("Abort"),
1210 &close_button, _("Close"), NULL, NULL);
1211 g_signal_connect(G_OBJECT(abort_button), "clicked",
1212 G_CALLBACK(kill_children_cb), children);
1213 g_signal_connect(G_OBJECT(close_button), "clicked",
1214 G_CALLBACK(hide_io_dialog_cb), children);
1215 gtk_widget_show(hbox);
1218 gtk_widget_set_sensitive(close_button, FALSE);
1220 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1222 children->dialog = dialog;
1223 children->scrolledwin = scrolledwin;
1224 children->text = text;
1225 children->input_hbox = children->open_in ? input_hbox : NULL;
1226 children->input_entry = children->open_in ? entry : NULL;
1227 children->progress_bar = progress_bar;
1228 children->abort_btn = abort_button;
1229 children->close_btn = close_button;
1231 gtk_widget_show(dialog);
1234 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1236 ChildInfo *child_info = (ChildInfo *)data;
1240 gdk_input_remove(child_info->tag_status);
1242 c = read(source, &buf, 1);
1243 debug_print("Child returned %c\n", buf);
1245 waitpid(-child_info->pid, NULL, 0);
1246 childinfo_close_pipes(child_info);
1247 child_info->pid = 0;
1249 wait_for_children(child_info->children);
1252 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1254 Children *children = (Children *)data;
1255 ChildInfo *child_info = (ChildInfo *)children->list->data;
1259 debug_print("Sending input to grand child.\n");
1260 if (!(cond && GDK_INPUT_WRITE))
1263 gdk_input_remove(child_info->tag_in);
1264 child_info->tag_in = -1;
1266 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1268 len = strlen(input);
1272 c = write(child_info->chld_in, input + count, len - count);
1275 } while (c >= 0 && count < len);
1278 write(child_info->chld_in, "\n", 2);
1282 gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1283 gtk_widget_set_sensitive(children->input_hbox, TRUE);
1284 close(child_info->chld_in);
1285 child_info->chld_in = -1;
1286 debug_print("Input to grand child sent.\n");
1289 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1291 ChildInfo *child_info = (ChildInfo *)data;
1293 gchar buf[BUFFSIZE];
1295 debug_print("Catching grand child's output.\n");
1296 if (child_info->children->action_type &
1297 (ACTION_PIPE_OUT | ACTION_INSERT)
1298 && source == child_info->chld_out) {
1299 GtkTextView *text = GTK_TEXT_VIEW(child_info->children->msg_text);
1300 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
1301 GtkTextIter iter1, iter2;
1304 mark = gtk_text_buffer_get_insert(textbuf);
1305 gtk_text_buffer_get_iter_at_mark(textbuf, &iter1, mark);
1306 gtk_text_buffer_get_iter_at_mark(textbuf, &iter2, mark);
1309 c = read(source, buf, sizeof(buf) - 1);
1312 gtk_text_buffer_insert(textbuf, &iter2, buf, c);
1314 if (child_info->children->is_selection) {
1315 gtk_text_buffer_place_cursor(textbuf, &iter1);
1316 gtk_text_buffer_move_mark_by_name
1317 (textbuf, "selection_bound", &iter2);
1320 c = read(source, buf, sizeof(buf) - 1);
1321 for (i = 0; i < c; i++)
1322 g_string_append_c(child_info->output, buf[i]);
1324 child_info->new_out = TRUE;
1327 if (source == child_info->chld_out) {
1328 gdk_input_remove(child_info->tag_out);
1329 child_info->tag_out = -1;
1330 close(child_info->chld_out);
1331 child_info->chld_out = -1;
1333 gdk_input_remove(child_info->tag_err);
1334 child_info->tag_err = -1;
1335 close(child_info->chld_err);
1336 child_info->chld_err = -1;
1340 wait_for_children(child_info->children);
1343 static gchar *get_user_string(const gchar *action, ActionType type)
1346 gchar *user_str = NULL;
1349 case ACTION_USER_HIDDEN_STR:
1350 message = g_strdup_printf
1351 (_("Enter the argument for the following action:\n"
1352 "(`%%h' will be replaced with the argument)\n"
1355 user_str = input_dialog_with_invisible
1356 (_("Action's hidden user argument"), message, NULL);
1358 case ACTION_USER_STR:
1359 message = g_strdup_printf
1360 (_("Enter the argument for the following action:\n"
1361 "(`%%u' will be replaced with the argument)\n"
1364 user_str = input_dialog
1365 (_("Action's user argument"), message, NULL);
1368 g_warning("Unsupported action type %d", type);