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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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>
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
652 (textbuf, &start_iter, &end_iter);
654 gtk_text_buffer_get_iter_at_offset
655 (textbuf, &start_iter, body_pos);
656 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
658 msg_str = gtk_text_buffer_get_text
659 (textbuf, &start_iter, &end_iter, FALSE);
661 sel_str = g_strdup(msg_str);
664 if (action_type & ACTION_USER_STR) {
665 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
672 if (action_type & ACTION_USER_HIDDEN_STR) {
673 if (!(user_hidden_str =
674 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
682 if (text && (action_type & ACTION_PIPE_OUT)) {
683 GtkTextBuffer *textbuf;
684 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
685 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
688 children = g_new0(Children, 1);
690 children->action = g_strdup(action);
691 children->action_type = action_type;
692 children->msg_text = text;
693 children->is_selection = is_selection;
695 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
696 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
697 children->open_in = 1;
699 if (action_type & ACTION_SINGLE) {
702 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
703 msginfo = (MsgInfo *)cur->data;
705 is_ok = FALSE; /* ERR: msginfo missing */
708 cmd = parse_action_cmd(action, msginfo, msg_list,
710 user_hidden_str, sel_str);
712 debug_print("Action command error\n");
713 is_ok = FALSE; /* ERR: incorrect command */
716 if ((child_info = fork_child(cmd, msg_str, children))) {
717 children_list = g_slist_append(children_list,
723 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
724 user_str, user_hidden_str, sel_str);
726 if ((child_info = fork_child(cmd, msg_str, children))) {
727 children_list = g_slist_append(children_list,
732 is_ok = FALSE; /* ERR: incorrect command */
738 g_free(user_hidden_str);
740 if (!children_list) {
741 /* If not waiting for children, return */
742 free_children(children);
746 children->list = children_list;
747 children->nb = g_slist_length(children_list);
748 children->initial_nb = children->nb;
750 for (cur = children_list; cur; cur = cur->next) {
751 child_info = (ChildInfo *) cur->data;
752 child_info->tag_status =
753 gdk_input_add(child_info->chld_status,
755 catch_status, child_info);
758 create_io_dialog(children);
764 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
767 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
768 gchar *cmdline[4], *ret_str;
770 ChildInfo *child_info;
772 gssize by_written = 0, by_read = 0;
774 sync = !(children->action_type & ACTION_ASYNC);
776 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
777 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
780 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
782 alertpanel_error(_("Command could not be started. "
783 "Pipe creation failed.\n%s"),
785 /* Closing fd = -1 fails silently */
792 close(chld_status[0]);
793 close(chld_status[1]);
794 return NULL; /* Pipe error */
798 debug_print("Forking child and grandchild.\n");
799 debug_print("Executing: /bin/sh -c %s\n", cmd);
802 if (pid == 0) { /* Child */
806 #ifdef GDK_WINDOWING_X11
807 close(ConnectionNumber(gdk_display));
808 #endif /* GDK_WINDOWING_X11 */
813 if (setpgid(0, getppid()))
817 if (children->action_type &
820 ACTION_USER_HIDDEN_IN)) {
821 close(fileno(stdin));
827 close(fileno(stdout));
832 close(fileno(stderr));
840 ret_str = g_locale_from_utf8(cmd, strlen(cmd),
841 &by_read, &by_written,
843 if (ret_str && by_written)
844 cmdline[2] = ret_str;
848 execvp("/bin/sh", cmdline);
853 } else if (gch_pid < (pid_t) 0) { /* Fork error */
855 write(chld_status[1], "1\n", 2);
866 close(chld_status[0]);
868 debug_print("Child: Waiting for grandchild\n");
869 waitpid(gch_pid, NULL, 0);
870 debug_print("Child: grandchild ended\n");
871 write(chld_status[1], "0\n", 2);
872 close(chld_status[1]);
876 } else if (pid < 0) { /* Fork error */
877 alertpanel_error(_("Could not fork to execute the following "
879 cmd, g_strerror(errno));
886 waitpid(pid, NULL, 0);
891 if (!(children->action_type &
892 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
896 close(chld_status[1]);
898 child_info = g_new0(ChildInfo, 1);
900 child_info->children = children;
902 child_info->pid = pid;
903 child_info->cmd = g_strdup(cmd);
904 child_info->new_out = FALSE;
905 child_info->output = g_string_new(NULL);
906 child_info->chld_in =
907 (children->action_type &
908 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
910 child_info->chld_out = chld_out[0];
911 child_info->chld_err = chld_err[0];
912 child_info->chld_status = chld_status[0];
913 child_info->tag_in = -1;
914 child_info->tag_out = gdk_input_add(chld_out[0], GDK_INPUT_READ,
915 catch_output, child_info);
916 child_info->tag_err = gdk_input_add(chld_err[0], GDK_INPUT_READ,
917 catch_output, child_info);
919 if (!(children->action_type &
920 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
923 if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
924 ret_str = g_locale_from_utf8(msg_str, strlen(msg_str),
925 &by_read, &by_written, NULL);
926 if (ret_str && by_written) {
927 write(chld_in[1], ret_str, strlen(ret_str));
930 write(chld_in[1], msg_str, strlen(msg_str));
931 if (!(children->action_type &
932 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
934 child_info->chld_in = -1; /* No more input */
940 static void kill_children_cb(GtkWidget *widget, gpointer data)
943 Children *children = (Children *) data;
944 ChildInfo *child_info;
946 for (cur = children->list; cur; cur = cur->next) {
947 child_info = (ChildInfo *)(cur->data);
948 debug_print("Killing child group id %d\n", child_info->pid);
949 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
954 static gint wait_for_children(Children *children)
957 ChildInfo *child_info;
959 gint nb = children->nb;
963 cur = children->list;
966 child_info = (ChildInfo *)cur->data;
969 new_output |= child_info->new_out;
973 children->output |= new_output;
975 if (new_output || (children->dialog && (nb != children->nb)))
976 update_io_dialog(children);
981 if (!children->dialog) {
982 free_children(children);
983 } else if (!children->output) {
984 gtk_widget_destroy(children->dialog);
990 static void send_input(GtkWidget *w, gpointer data)
992 Children *children = (Children *) data;
993 ChildInfo *child_info = (ChildInfo *) children->list->data;
995 child_info->tag_in = gdk_input_add(child_info->chld_in,
997 catch_input, children);
998 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1001 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1003 hide_io_dialog_cb(w, data);
1007 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1010 Children *children = (Children *)data;
1012 if (!children->nb) {
1013 g_signal_handlers_disconnect_matched
1014 (G_OBJECT(children->dialog), G_SIGNAL_MATCH_DATA,
1015 0, 0, NULL, NULL, children);
1016 gtk_widget_destroy(children->dialog);
1017 free_children(children);
1021 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1024 if (event && event->keyval == GDK_Escape)
1025 hide_io_dialog_cb(widget, data);
1029 static void childinfo_close_pipes(ChildInfo *child_info)
1031 /* stdout and stderr pipes are guaranteed to be removed by
1032 * their handler, but in case where we receive child exit notification
1033 * before grand-child's pipes closing signals, we check them and close
1036 if (child_info->tag_in > 0)
1037 gdk_input_remove(child_info->tag_in);
1038 if (child_info->tag_out > 0)
1039 gdk_input_remove(child_info->tag_out);
1040 if (child_info->tag_err > 0)
1041 gdk_input_remove(child_info->tag_err);
1043 if (child_info->chld_in >= 0)
1044 close(child_info->chld_in);
1045 if (child_info->chld_out >= 0)
1046 close(child_info->chld_out);
1047 if (child_info->chld_err >= 0)
1048 close(child_info->chld_err);
1050 close(child_info->chld_status);
1053 static void free_children(Children *children)
1055 ChildInfo *child_info;
1057 debug_print("Freeing children data %p\n", children);
1059 g_free(children->action);
1060 while (children->list != NULL) {
1061 child_info = (ChildInfo *)children->list->data;
1062 g_free(child_info->cmd);
1063 g_string_free(child_info->output, TRUE);
1064 children->list = g_slist_remove(children->list, child_info);
1070 static void update_io_dialog(Children *children)
1074 debug_print("Updating actions input/output dialog.\n");
1076 if (children->progress_bar) {
1079 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar),
1080 (gdouble) (children->initial_nb - children->nb) /
1081 (gdouble) children->initial_nb);
1082 text = g_strdup_printf("%s %d/%d", _("Completed"),
1083 children->initial_nb - children->nb,
1084 children->initial_nb);
1085 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(children->progress_bar), text);
1089 if (!children->nb) {
1090 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1091 gtk_widget_set_sensitive(children->close_btn, TRUE);
1092 if (children->input_hbox)
1093 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1094 gtk_widget_grab_focus(children->close_btn);
1095 g_signal_connect(G_OBJECT(children->dialog),
1097 G_CALLBACK(io_dialog_key_pressed_cb),
1101 if (children->output) {
1102 GtkWidget *text = children->text;
1103 GtkTextBuffer *textbuf;
1104 GtkTextIter iter, start_iter, end_iter;
1106 ChildInfo *child_info;
1108 gtk_widget_show(children->scrolledwin);
1109 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
1110 gtk_text_buffer_get_bounds(textbuf, &start_iter, &end_iter);
1111 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
1112 gtk_text_buffer_get_start_iter(textbuf, &iter);
1114 for (cur = children->list; cur; cur = cur->next) {
1115 child_info = (ChildInfo *)cur->data;
1116 if (child_info->pid)
1117 caption = g_strdup_printf
1118 (_("--- Running: %s\n"),
1121 caption = g_strdup_printf
1122 (_("--- Ended: %s\n"),
1125 gtk_text_buffer_insert(textbuf, &iter, caption, -1);
1126 gtk_text_buffer_insert(textbuf, &iter,
1127 child_info->output->str, -1);
1129 child_info->new_out = FALSE;
1134 static void create_io_dialog(Children *children)
1138 GtkWidget *entry = NULL;
1139 GtkWidget *input_hbox = NULL;
1140 GtkWidget *send_button;
1143 GtkWidget *scrolledwin;
1145 GtkWidget *progress_bar = NULL;
1146 GtkWidget *abort_button;
1147 GtkWidget *close_button;
1148 PangoFontDescription *text_font;
1150 debug_print("Creating action IO dialog\n");
1152 dialog = gtk_dialog_new();
1153 gtk_container_set_border_width
1154 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1155 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1156 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1157 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1158 manage_window_set_transient(GTK_WINDOW(dialog));
1159 g_signal_connect(G_OBJECT(dialog), "delete_event",
1160 G_CALLBACK(delete_io_dialog_cb), children);
1161 g_signal_connect(G_OBJECT(dialog), "destroy",
1162 G_CALLBACK(hide_io_dialog_cb),
1165 vbox = gtk_vbox_new(FALSE, 8);
1166 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1167 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1168 gtk_widget_show(vbox);
1170 label = gtk_label_new(children->action);
1171 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1172 gtk_widget_show(label);
1174 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1175 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1176 GTK_POLICY_AUTOMATIC,
1177 GTK_POLICY_AUTOMATIC);
1178 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1179 gtk_widget_set_size_request(scrolledwin, 480, 200);
1180 gtk_widget_hide(scrolledwin);
1182 text = gtk_text_view_new();
1183 if (prefs_common.textfont) {
1184 if (NULL != (text_font = pango_font_description_from_string
1185 (prefs_common.textfont))) {
1186 gtk_widget_modify_font(text, text_font);
1187 pango_font_description_free(text_font);
1190 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
1191 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1192 gtk_widget_show(text);
1194 if (children->open_in) {
1195 input_hbox = gtk_hbox_new(FALSE, 8);
1196 gtk_widget_show(input_hbox);
1198 entry = gtk_entry_new();
1199 gtk_widget_set_size_request(entry, 320, -1);
1200 g_signal_connect(G_OBJECT(entry), "activate",
1201 G_CALLBACK(send_input), children);
1202 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1203 if (children->action_type & ACTION_USER_HIDDEN_IN)
1204 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1205 gtk_widget_show(entry);
1207 send_button = gtk_button_new_from_stock(GTK_STOCK_EXECUTE);
1208 g_signal_connect(G_OBJECT(send_button), "clicked",
1209 G_CALLBACK(send_input), children);
1210 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1212 gtk_widget_show(send_button);
1214 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1215 gtk_widget_grab_focus(entry);
1218 if (children->initial_nb > 1) {
1221 progress_bar = gtk_progress_bar_new();
1222 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1223 GTK_PROGRESS_LEFT_TO_RIGHT);
1224 text = g_strdup_printf("%s 0/%d\n", _("Completed"),
1225 children->initial_nb);
1226 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar),
1229 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1230 gtk_widget_show(progress_bar);
1233 gtkut_stock_button_set_create(&hbox, &abort_button, GTK_STOCK_STOP,
1234 &close_button, GTK_STOCK_CLOSE, NULL, NULL);
1235 g_signal_connect(G_OBJECT(abort_button), "clicked",
1236 G_CALLBACK(kill_children_cb), children);
1237 g_signal_connect(G_OBJECT(close_button), "clicked",
1238 G_CALLBACK(hide_io_dialog_cb), children);
1239 gtk_widget_show(hbox);
1242 gtk_widget_set_sensitive(close_button, FALSE);
1244 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1246 children->dialog = dialog;
1247 children->scrolledwin = scrolledwin;
1248 children->text = text;
1249 children->input_hbox = children->open_in ? input_hbox : NULL;
1250 children->input_entry = children->open_in ? entry : NULL;
1251 children->progress_bar = progress_bar;
1252 children->abort_btn = abort_button;
1253 children->close_btn = close_button;
1255 gtk_widget_show(dialog);
1258 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1260 ChildInfo *child_info = (ChildInfo *)data;
1264 gdk_input_remove(child_info->tag_status);
1266 c = read(source, &buf, 1);
1267 debug_print("Child returned %c\n", buf);
1269 waitpid(-child_info->pid, NULL, 0);
1270 childinfo_close_pipes(child_info);
1271 child_info->pid = 0;
1273 wait_for_children(child_info->children);
1276 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1278 Children *children = (Children *)data;
1279 ChildInfo *child_info = (ChildInfo *)children->list->data;
1280 gchar *input, *ret_str;
1282 gssize by_read = 0, by_written = 0;
1284 debug_print("Sending input to grand child.\n");
1285 if (!(cond && GDK_INPUT_WRITE))
1288 gdk_input_remove(child_info->tag_in);
1289 child_info->tag_in = -1;
1291 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1293 ret_str = g_locale_from_utf8(input, strlen(input), &by_read,
1295 if (ret_str && by_written) {
1300 len = strlen(input);
1304 c = write(child_info->chld_in, input + count, len - count);
1307 } while (c >= 0 && count < len);
1310 write(child_info->chld_in, "\n", 2);
1314 gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1315 gtk_widget_set_sensitive(children->input_hbox, TRUE);
1316 close(child_info->chld_in);
1317 child_info->chld_in = -1;
1318 debug_print("Input to grand child sent.\n");
1321 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1323 ChildInfo *child_info = (ChildInfo *)data;
1325 gchar buf[BUFFSIZE];
1327 debug_print("Catching grand child's output.\n");
1328 if (child_info->children->action_type &
1329 (ACTION_PIPE_OUT | ACTION_INSERT)
1330 && source == child_info->chld_out) {
1332 GTK_TEXT_VIEW(child_info->children->msg_text);
1333 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
1334 GtkTextIter iter1, iter2;
1337 mark = gtk_text_buffer_get_insert(textbuf);
1338 gtk_text_buffer_get_iter_at_mark(textbuf, &iter1, mark);
1339 gtk_text_buffer_get_iter_at_mark(textbuf, &iter2, mark);
1342 gsize bytes_read = 0, bytes_written = 0;
1345 c = read(source, buf, sizeof(buf) - 1);
1349 ret_str = g_locale_to_utf8
1350 (buf, c, &bytes_read, &bytes_written, NULL);
1351 if (ret_str && bytes_written > 0) {
1352 gtk_text_buffer_insert
1353 (textbuf, &iter2, ret_str,
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 gsize bytes_read = 0, bytes_written = 0;
1372 ret_str = g_locale_to_utf8
1373 (buf, c, &bytes_read, &bytes_written, NULL);
1374 if (ret_str && bytes_written > 0) {
1376 (child_info->output, ret_str,
1380 g_string_append_len(child_info->output, buf, c);
1382 child_info->new_out = TRUE;
1386 if (source == child_info->chld_out) {
1387 gdk_input_remove(child_info->tag_out);
1388 child_info->tag_out = -1;
1389 close(child_info->chld_out);
1390 child_info->chld_out = -1;
1392 gdk_input_remove(child_info->tag_err);
1393 child_info->tag_err = -1;
1394 close(child_info->chld_err);
1395 child_info->chld_err = -1;
1399 wait_for_children(child_info->children);
1402 static gchar *get_user_string(const gchar *action, ActionType type)
1405 gchar *user_str = NULL;
1408 case ACTION_USER_HIDDEN_STR:
1409 message = g_strdup_printf
1410 (_("Enter the argument for the following action:\n"
1411 "(`%%h' will be replaced with the argument)\n"
1414 user_str = input_dialog_with_invisible
1415 (_("Action's hidden user argument"), message, NULL);
1417 case ACTION_USER_STR:
1418 message = g_strdup_printf
1419 (_("Enter the argument for the following action:\n"
1420 "(`%%u' will be replaced with the argument)\n"
1423 user_str = input_dialog
1424 (_("Action's user argument"), message, NULL);
1427 g_warning("Unsupported action type %d", type);