2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto & The Claws Mail 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 3 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, see <http://www.gnu.org/licenses/>.
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"
57 #include "procheader.h"
59 typedef struct _Children Children;
60 typedef struct _ChildInfo ChildInfo;
61 typedef struct _UserStringDialog UserStringDialog;
67 GtkWidget *input_entry;
68 GtkWidget *input_hbox;
69 GtkWidget *progress_bar;
72 GtkWidget *scrolledwin;
75 ActionType action_type;
84 gboolean is_selection;
103 void (*callback)(void *data);
106 GSList *msginfo_list;
109 static void action_update_menu (GtkItemFactory *ifactory,
113 static void compose_actions_execute_cb (Compose *compose,
116 static void mainwin_actions_execute_cb (MainWindow *mainwin,
119 static void msgview_actions_execute_cb (MessageView *msgview,
122 static void message_actions_execute (MessageView *msgview,
126 static gboolean execute_filtering_actions(gchar *action,
129 static gboolean execute_actions (gchar *action,
134 void (*callback)(void *data),
137 static gchar *parse_action_cmd (gchar *action,
141 const gchar *user_str,
142 const gchar *user_hidden_str,
143 const gchar *sel_str);
144 static gboolean parse_append_filename (GString *cmd,
147 static gboolean parse_append_msgpart (GString *cmd,
151 static ChildInfo *fork_child (gchar *cmd,
152 const gchar *msg_str,
155 static gint wait_for_children (Children *children);
157 static void free_children (Children *children);
159 static void childinfo_close_pipes (ChildInfo *child_info);
161 static void create_io_dialog (Children *children);
162 static void update_io_dialog (Children *children);
164 static void hide_io_dialog_cb (GtkWidget *widget,
166 static gint io_dialog_key_pressed_cb (GtkWidget *widget,
170 static void catch_output (gpointer data,
172 GdkInputCondition cond);
173 static void catch_input (gpointer data,
175 GdkInputCondition cond);
176 static void catch_status (gpointer data,
178 GdkInputCondition cond);
180 static gchar *get_user_string (const gchar *action,
184 ActionType action_get_type(const gchar *action_str)
187 ActionType action_type = ACTION_NONE;
189 g_return_val_if_fail(action_str, ACTION_ERROR);
190 g_return_val_if_fail(*action_str, ACTION_ERROR);
195 action_type |= ACTION_PIPE_IN;
197 } else if (p[0] == '>') {
198 action_type |= ACTION_USER_IN;
200 } else if (p[0] == '*') {
201 action_type |= ACTION_USER_HIDDEN_IN;
208 while (*p && action_type != ACTION_ERROR) {
209 if (p[0] == '%' && p[1]) {
212 /* CLAWS: filtering action is a mutually exclusive
213 * action. we can enable others if needed later. we
214 * add ACTION_SINGLE | ACTION_MULTIPLE so it will
215 * only be executed from the main window toolbar */
216 if (p[2] == 's') /* source messages */
217 action_type = ACTION_FILTERING_ACTION
222 action_type |= ACTION_SINGLE;
225 action_type |= ACTION_MULTIPLE;
228 action_type |= ACTION_SINGLE;
231 action_type |= ACTION_SELECTION_STR;
234 action_type |= ACTION_USER_STR;
237 action_type |= ACTION_USER_HIDDEN_STR;
243 action_type = ACTION_ERROR;
247 } else if (p[0] == '|') {
249 action_type |= ACTION_PIPE_OUT;
250 } else if (p[0] == '>') {
252 action_type |= ACTION_INSERT;
253 } else if (p[0] == '&') {
255 action_type |= ACTION_ASYNC;
263 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
264 GSList *msg_list, MimeInfo *partinfo,
265 const gchar *user_str,
266 const gchar *user_hidden_str,
267 const gchar *sel_str)
275 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
278 cmd = g_string_sized_new(strlen(action));
281 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
282 if (p[0] == '%' && p[1]) {
285 if (!parse_append_filename(cmd, msginfo)) {
286 g_string_free(cmd, TRUE);
292 for (cur = msg_list; cur != NULL;
294 MsgInfo *msg = (MsgInfo *)cur->data;
296 if (!parse_append_filename(cmd, msg)) {
297 g_string_free(cmd, TRUE);
301 g_string_append_c(cmd, ' ');
306 if (!parse_append_msgpart(cmd, msginfo,
308 g_string_free(cmd, TRUE);
315 g_string_append(cmd, sel_str);
320 g_string_append(cmd, user_str);
325 g_string_append(cmd, user_hidden_str);
329 g_string_append_c(cmd, p[1]);
333 g_string_append_c(cmd, p[0]);
334 g_string_append_c(cmd, p[1]);
338 g_string_append_c(cmd, p[0]);
343 g_string_free(cmd, TRUE);
348 g_string_free(cmd, FALSE);
352 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
356 gchar escape_ch[] = "\\ ";
358 g_return_val_if_fail(msginfo, FALSE);
360 filename = procmsg_get_message_file(msginfo);
363 alertpanel_error(_("Could not get message file %d"),
369 while ((q = strpbrk(p, "$\"`'\\ \t*?[]&|;<>()!#~")) != NULL) {
372 g_string_append(cmd, p);
373 g_string_append(cmd, escape_ch);
376 g_string_append(cmd, p);
383 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
386 gboolean single_part = FALSE;
388 gchar *part_filename;
392 partinfo = procmime_scan_message(msginfo);
394 alertpanel_error(_("Could not get message part."));
401 filename = procmsg_get_message_file_path(msginfo);
402 part_filename = procmime_get_tmp_file_name(partinfo);
404 ret = procmime_get_part(part_filename, partinfo);
407 procmime_mimeinfo_free_all(partinfo);
411 alertpanel_error(_("Can't get part of multipart message: %s"), strerror(-ret));
412 g_free(part_filename);
416 g_string_append(cmd, part_filename);
418 g_free(part_filename);
423 void actions_execute(gpointer data,
428 if (source == TOOLBAR_MAIN)
429 mainwin_actions_execute_cb((MainWindow*)data, action_nb, widget);
430 else if (source == TOOLBAR_COMPOSE)
431 compose_actions_execute_cb((Compose*)data, action_nb, widget);
432 else if (source == TOOLBAR_MSGVIEW)
433 msgview_actions_execute_cb((MessageView*)data, action_nb, widget);
436 void action_update_mainwin_menu(GtkItemFactory *ifactory,
440 action_update_menu(ifactory, branch_path,
441 mainwin_actions_execute_cb, mainwin);
444 void action_update_msgview_menu(GtkItemFactory *ifactory,
446 MessageView *msgview)
448 action_update_menu(ifactory, branch_path,
449 msgview_actions_execute_cb, msgview);
452 void action_update_compose_menu(GtkItemFactory *ifactory,
456 action_update_menu(ifactory, branch_path,
457 compose_actions_execute_cb, compose);
460 static void action_update_menu(GtkItemFactory *ifactory,
462 gpointer callback, gpointer data)
467 gchar *action, *action_p;
469 GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
471 ifentry.path = branch_path;
472 menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
473 g_return_if_fail(menuitem != NULL);
475 amenu = GTK_MENU_SHELL(menuitem)->children;
476 while (amenu != NULL) {
477 GList *alist = amenu->next;
478 gtk_widget_destroy(GTK_WIDGET(amenu->data));
482 ifentry.accelerator = NULL;
483 ifentry.callback_action = 0;
484 ifentry.callback = callback;
485 ifentry.item_type = NULL;
487 for (cur = prefs_common.actions_list; cur; cur = cur->next) {
488 action = g_strdup((gchar *)cur->data);
489 action_p = strstr(action, ": ");
490 if (action_p && action_p[2] &&
491 (action_get_type(&action_p[2]) != ACTION_ERROR) &&
492 (action[0] != '/')) {
494 menu_path = g_strdup_printf("%s/%s", branch_path,
496 ifentry.path = menu_path;
497 gtk_item_factory_create_item(ifactory, &ifentry, data,
502 ifentry.callback_action++;
506 static void compose_actions_execute_cb(Compose *compose, guint action_nb,
510 ActionType action_type;
512 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
514 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
515 g_return_if_fail(buf != NULL);
516 action = strstr(buf, ": ");
517 g_return_if_fail(action != NULL);
519 /* Point to the beginning of the command-line */
522 action_type = action_get_type(action);
523 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
525 (_("The selected action cannot be used in the compose window\n"
526 "because it contains %%f, %%F, %%as or %%p."));
530 execute_actions(action, NULL, compose->text, 0, NULL,
531 compose_action_cb, compose);
534 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
539 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
540 message_actions_execute(mainwin->messageview, action_nb, msg_list);
541 g_slist_free(msg_list);
544 static void msgview_actions_execute_cb(MessageView *msgview, guint action_nb,
547 GSList *msg_list = NULL;
549 if (msgview->msginfo)
550 msg_list = g_slist_append(msg_list, msgview->msginfo);
551 message_actions_execute(msgview, action_nb, msg_list);
552 g_slist_free(msg_list);
555 static void message_actions_execute(MessageView *msgview, guint action_nb,
562 GtkWidget *text = NULL;
564 ActionType action_type;
566 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
568 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
570 g_return_if_fail(buf);
571 g_return_if_fail((action = strstr(buf, ": ")));
573 /* Point to the beginning of the command-line */
576 textview = messageview_get_current_textview(msgview);
578 text = textview->text;
579 body_pos = textview->body_pos;
581 partinfo = messageview_get_selected_mime_part(msgview);
583 /* this command will alter the message text */
584 action_type = action_get_type(action);
585 if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
586 msgview->filtered = TRUE;
588 if (action_type & ACTION_FILTERING_ACTION)
589 /* CLAWS: most of the above code is not necessary for applying
591 execute_filtering_actions(action, msg_list);
593 execute_actions(action, msg_list, text, body_pos, partinfo,
597 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
599 GSList *action_list, *p;
600 const gchar *sbegin, *send;
601 gchar *action_string;
602 SummaryView *summaryview = NULL;
603 MainWindow *mainwin = NULL;
605 if (mainwindow_get_mainwindow()) {
606 summaryview = mainwindow_get_mainwindow()->summaryview;
607 mainwin = mainwindow_get_mainwindow();
610 if (NULL == (sbegin = strstr2(action, "%as{")))
612 sbegin += sizeof "%as{" - 1;
613 if (NULL == (send = strrchr(sbegin, '}')))
615 action_string = g_strndup(sbegin, send - sbegin);
617 action_list = matcher_parser_get_action_list(action_string);
618 if (action_list == NULL) {
619 gchar *tmp = g_strdup(action_string);
623 alertpanel_error(_("There is no filtering action set"));
625 alertpanel_error(_("Invalid filtering action(s):\n%s"), tmp);
626 g_free(action_string);
630 g_free(action_string);
632 /* apply actions on each message info */
633 for (p = msglist; p && p->data; p = g_slist_next(p)) {
634 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
638 summary_lock(summaryview);
639 main_window_cursor_wait(mainwin);
640 summary_freeze(summaryview);
641 folder_item_update_freeze();
644 filtering_move_and_copy_msgs(msglist);
647 folder_item_update_thaw();
648 summary_thaw(summaryview);
649 main_window_cursor_normal(mainwin);
650 summary_unlock(summaryview);
651 summary_show(summaryview, summaryview->folder_item);
653 for (p = action_list; p; p = g_slist_next(p))
654 if (p->data) filteringaction_free(p->data);
655 g_slist_free(action_list);
659 static gboolean execute_actions(gchar *action, GSList *msg_list,
661 gint body_pos, MimeInfo *partinfo,
662 void (*callback)(void *data), void *data)
664 GSList *children_list = NULL;
668 ChildInfo *child_info;
669 ActionType action_type;
672 gchar *sel_str = NULL;
673 gchar *msg_str = NULL;
674 gchar *user_str = NULL;
675 gchar *user_hidden_str = NULL;
676 GtkTextIter start_iter, end_iter;
677 gboolean is_selection = FALSE;
679 g_return_val_if_fail(action && *action, FALSE);
681 action_type = action_get_type(action);
683 if (action_type == ACTION_ERROR)
684 return FALSE; /* ERR: syntax error */
686 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
687 return FALSE; /* ERR: file command without selection */
689 msg_list_len = g_slist_length(msg_list);
691 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
692 if (msg_list_len > 1)
693 return FALSE; /* ERR: pipe + multiple selection */
695 return FALSE; /* ERR: pipe and no displayed text */
698 if (action_type & ACTION_SELECTION_STR) {
700 return FALSE; /* ERR: selection string but no text */
704 GtkTextBuffer *textbuf;
706 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
707 is_selection = gtk_text_buffer_get_selection_bounds
708 (textbuf, &start_iter, &end_iter);
710 gtk_text_buffer_get_iter_at_offset
711 (textbuf, &start_iter, body_pos);
712 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
714 msg_str = gtk_text_buffer_get_text
715 (textbuf, &start_iter, &end_iter, FALSE);
717 sel_str = g_strdup(msg_str);
720 if (action_type & ACTION_USER_STR) {
721 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
728 if (action_type & ACTION_USER_HIDDEN_STR) {
729 if (!(user_hidden_str =
730 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
738 if (text && (action_type & ACTION_PIPE_OUT)) {
739 GtkTextBuffer *textbuf;
740 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
741 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
744 children = g_new0(Children, 1);
746 children->action = g_strdup(action);
747 children->action_type = action_type;
748 children->msg_text = text;
749 children->is_selection = is_selection;
751 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
752 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
753 children->open_in = 1;
755 if (action_type & ACTION_SINGLE) {
758 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
759 msginfo = (MsgInfo *)cur->data;
761 is_ok = FALSE; /* ERR: msginfo missing */
764 cmd = parse_action_cmd(action, msginfo, msg_list,
766 user_hidden_str, sel_str);
768 debug_print("Action command error\n");
769 is_ok = FALSE; /* ERR: incorrect command */
772 if ((child_info = fork_child(cmd, msg_str, children))) {
773 /* Pass msginfo to catch_status () */
774 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
775 child_info->msginfo_list =
776 g_slist_append (NULL, msginfo);
777 children_list = g_slist_append(children_list,
783 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
784 user_str, user_hidden_str, sel_str);
786 if ((child_info = fork_child(cmd, msg_str, children))) {
787 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
788 child_info->msginfo_list =
789 g_slist_copy (msg_list);
790 children_list = g_slist_append(children_list,
795 is_ok = FALSE; /* ERR: incorrect command */
801 g_free(user_hidden_str);
803 if (!children_list) {
804 /* If not waiting for children, return */
805 free_children(children);
809 children->list = children_list;
810 children->nb = g_slist_length(children_list);
811 children->initial_nb = children->nb;
813 for (cur = children_list; cur; cur = cur->next) {
814 child_info = (ChildInfo *) cur->data;
815 child_info->callback = callback;
816 child_info->data = data;
817 child_info->tag_status =
818 gdk_input_add(child_info->chld_status,
820 catch_status, child_info);
823 create_io_dialog(children);
828 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
832 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
833 gchar *cmdline[4], *ret_str;
835 ChildInfo *child_info;
837 gssize by_written = 0, by_read = 0;
839 sync = !(children->action_type & ACTION_ASYNC);
841 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
842 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
845 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
847 alertpanel_error(_("Command could not be started. "
848 "Pipe creation failed.\n%s"),
850 /* Closing fd = -1 fails silently */
851 (void)close(chld_in[0]);
852 (void)close(chld_in[1]);
853 (void)close(chld_out[0]);
854 (void)close(chld_out[1]);
855 (void)close(chld_err[0]);
856 (void)close(chld_err[1]);
857 (void)close(chld_status[0]);
858 (void)close(chld_status[1]);
859 return NULL; /* Pipe error */
863 debug_print("Forking child and grandchild.\n");
864 debug_print("Executing: /bin/sh -c %s\n", cmd);
867 if (pid == 0) { /* Child */
871 #ifdef GDK_WINDOWING_X11
872 (void)close(ConnectionNumber(gdk_display));
873 #endif /* GDK_WINDOWING_X11 */
878 if (setpgid(0, getppid()))
882 if (children->action_type &
885 ACTION_USER_HIDDEN_IN)) {
886 (void)close(fileno(stdin));
887 (void)dup (chld_in[0]);
889 (void)close(chld_in[0]);
890 (void)close(chld_in[1]);
892 (void)close(fileno(stdout));
893 (void)dup (chld_out[1]);
894 (void)close(chld_out[0]);
895 (void)close(chld_out[1]);
897 (void)close(fileno(stderr));
898 (void)dup (chld_err[1]);
899 (void)close(chld_err[0]);
900 (void)close(chld_err[1]);
905 ret_str = g_locale_from_utf8(cmd, strlen(cmd),
906 &by_read, &by_written,
908 if (ret_str && by_written)
909 cmdline[2] = ret_str;
913 execvp("/bin/sh", cmdline);
918 } else if (gch_pid < (pid_t) 0) { /* Fork error */
920 (void)write(chld_status[1], "1\n", 2);
925 (void)close(chld_in[0]);
926 (void)close(chld_in[1]);
927 (void)close(chld_out[0]);
928 (void)close(chld_out[1]);
929 (void)close(chld_err[0]);
930 (void)close(chld_err[1]);
931 (void)close(chld_status[0]);
933 debug_print("Child: waiting for grandchild\n");
934 waitpid(gch_pid, NULL, 0);
935 debug_print("Child: grandchild ended\n");
936 (void)write(chld_status[1], "0\n", 2);
937 (void)close(chld_status[1]);
941 } else if (pid < 0) { /* Fork error */
942 alertpanel_error(_("Could not fork to execute the following "
944 cmd, g_strerror(errno));
951 waitpid(pid, NULL, 0);
955 (void)close(chld_in[0]);
956 if (!(children->action_type &
957 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
958 (void)close(chld_in[1]);
959 (void)close(chld_out[1]);
960 (void)close(chld_err[1]);
961 (void)close(chld_status[1]);
963 child_info = g_new0(ChildInfo, 1);
965 child_info->children = children;
967 child_info->pid = pid;
968 child_info->cmd = g_strdup(cmd);
969 child_info->new_out = FALSE;
970 child_info->output = g_string_new(NULL);
971 child_info->chld_in =
972 (children->action_type &
973 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
975 child_info->chld_out = chld_out[0];
976 child_info->chld_err = chld_err[0];
977 child_info->chld_status = chld_status[0];
978 child_info->tag_in = -1;
979 child_info->tag_out = gdk_input_add(chld_out[0], GDK_INPUT_READ,
980 catch_output, child_info);
981 child_info->tag_err = gdk_input_add(chld_err[0], GDK_INPUT_READ,
982 catch_output, child_info);
984 if (!(children->action_type &
985 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
988 if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
989 ret_str = g_locale_from_utf8(msg_str, strlen(msg_str),
990 &by_read, &by_written, NULL);
991 if (ret_str && by_written) {
992 (void)write(chld_in[1], ret_str, strlen(ret_str));
995 (void)write(chld_in[1], msg_str, strlen(msg_str));
996 if (!(children->action_type &
997 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
998 (void)close(chld_in[1]);
999 child_info->chld_in = -1; /* No more input */
1005 #endif /* G_OS_UNIX */
1008 static void kill_children_cb(GtkWidget *widget, gpointer data)
1012 Children *children = (Children *) data;
1013 ChildInfo *child_info;
1015 for (cur = children->list; cur; cur = cur->next) {
1016 child_info = (ChildInfo *)(cur->data);
1017 debug_print("Killing child group id %d\n", child_info->pid);
1018 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
1021 #endif /* G_OS_UNIX */
1024 static gint wait_for_children(Children *children)
1026 gboolean new_output;
1027 ChildInfo *child_info;
1029 gint nb = children->nb;
1033 cur = children->list;
1036 child_info = (ChildInfo *)cur->data;
1037 if (child_info->pid)
1039 new_output |= child_info->new_out;
1043 children->output |= new_output;
1045 if (new_output || (children->dialog && (nb != children->nb)))
1046 update_io_dialog(children);
1051 if (!children->dialog) {
1052 free_children(children);
1053 } else if (!children->output) {
1054 gtk_widget_destroy(children->dialog);
1060 static void send_input(GtkWidget *w, gpointer data)
1062 Children *children = (Children *) data;
1063 ChildInfo *child_info = (ChildInfo *) children->list->data;
1065 child_info->tag_in = gdk_input_add(child_info->chld_in,
1067 catch_input, children);
1070 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1072 hide_io_dialog_cb(w, data);
1076 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1079 Children *children = (Children *)data;
1081 if (!children->nb) {
1082 g_signal_handlers_disconnect_matched
1083 (G_OBJECT(children->dialog), G_SIGNAL_MATCH_DATA,
1084 0, 0, NULL, NULL, children);
1085 gtk_widget_destroy(children->dialog);
1086 free_children(children);
1090 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1093 if (event && (event->keyval == GDK_Escape ||
1094 event->keyval == GDK_Return ||
1095 event->keyval == GDK_KP_Enter))
1096 hide_io_dialog_cb(widget, data);
1100 static void childinfo_close_pipes(ChildInfo *child_info)
1102 /* stdout and stderr pipes are guaranteed to be removed by
1103 * their handler, but in case where we receive child exit notification
1104 * before grand-child's pipes closing signals, we check them and close
1107 if (child_info->tag_in > 0)
1108 gdk_input_remove(child_info->tag_in);
1109 if (child_info->tag_out > 0)
1110 gdk_input_remove(child_info->tag_out);
1111 if (child_info->tag_err > 0)
1112 gdk_input_remove(child_info->tag_err);
1114 if (child_info->chld_in >= 0)
1115 (void)close(child_info->chld_in);
1116 if (child_info->chld_out >= 0)
1117 (void)close(child_info->chld_out);
1118 if (child_info->chld_err >= 0)
1119 (void)close(child_info->chld_err);
1121 (void)close(child_info->chld_status);
1124 static void free_children(Children *children)
1126 ChildInfo *child_info;
1127 void (*callback)(void *data) = NULL;
1130 debug_print("Freeing children data %p\n", children);
1132 g_free(children->action);
1133 while (children->list != NULL) {
1134 child_info = (ChildInfo *)children->list->data;
1135 g_free(child_info->cmd);
1136 g_string_free(child_info->output, TRUE);
1137 children->list = g_slist_remove(children->list, child_info);
1138 callback = child_info->callback;
1139 data = child_info->data;
1149 static void update_io_dialog(Children *children)
1153 debug_print("Updating actions input/output dialog.\n");
1155 if (children->progress_bar) {
1158 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar),
1159 (children->initial_nb == 0) ? 0 :
1160 (gdouble) (children->initial_nb - children->nb) /
1161 (gdouble) children->initial_nb);
1162 text = g_strdup_printf("%s %d/%d", _("Completed"),
1163 children->initial_nb - children->nb,
1164 children->initial_nb);
1165 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(children->progress_bar), text);
1169 if (!children->nb) {
1170 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1171 gtk_widget_set_sensitive(children->close_btn, TRUE);
1172 if (children->input_hbox)
1173 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1174 gtk_widget_grab_focus(children->close_btn);
1175 g_signal_connect(G_OBJECT(children->dialog),
1177 G_CALLBACK(io_dialog_key_pressed_cb),
1181 if (children->output) {
1182 GtkWidget *text = children->text;
1183 GtkTextBuffer *textbuf;
1184 GtkTextIter iter, start_iter, end_iter;
1186 ChildInfo *child_info;
1188 gtk_widget_show(children->scrolledwin);
1189 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
1190 gtk_text_buffer_get_bounds(textbuf, &start_iter, &end_iter);
1191 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
1192 gtk_text_buffer_get_start_iter(textbuf, &iter);
1194 for (cur = children->list; cur; cur = cur->next) {
1195 child_info = (ChildInfo *)cur->data;
1196 if (child_info->pid)
1197 caption = g_strdup_printf
1198 (_("--- Running: %s\n"),
1201 caption = g_strdup_printf
1202 (_("--- Ended: %s\n"),
1205 gtk_text_buffer_insert(textbuf, &iter, caption, -1);
1206 gtk_text_buffer_insert(textbuf, &iter,
1207 child_info->output->str, -1);
1209 child_info->new_out = FALSE;
1214 static void create_io_dialog(Children *children)
1218 GtkWidget *entry = NULL;
1219 GtkWidget *input_hbox = NULL;
1220 GtkWidget *send_button;
1223 GtkWidget *scrolledwin;
1225 GtkWidget *progress_bar = NULL;
1226 GtkWidget *abort_button;
1227 GtkWidget *close_button;
1229 debug_print("Creating action IO dialog\n");
1231 dialog = gtk_dialog_new();
1232 gtk_container_set_border_width
1233 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1234 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1235 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1236 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1237 manage_window_set_transient(GTK_WINDOW(dialog));
1238 g_signal_connect(G_OBJECT(dialog), "delete_event",
1239 G_CALLBACK(delete_io_dialog_cb), children);
1240 g_signal_connect(G_OBJECT(dialog), "destroy",
1241 G_CALLBACK(hide_io_dialog_cb),
1244 vbox = gtk_vbox_new(FALSE, 8);
1245 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1246 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1247 gtk_widget_show(vbox);
1249 label = gtk_label_new(children->action);
1250 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1251 gtk_widget_show(label);
1253 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1254 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1255 GTK_POLICY_AUTOMATIC,
1256 GTK_POLICY_AUTOMATIC);
1257 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
1259 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1260 gtk_widget_set_size_request(scrolledwin, 560, 200);
1261 gtk_widget_hide(scrolledwin);
1263 text = gtk_text_view_new();
1265 if (prefs_common.textfont) {
1266 PangoFontDescription *font_desc;
1267 font_desc = pango_font_description_from_string
1268 (prefs_common.textfont);
1270 gtk_widget_modify_font(text, font_desc);
1271 pango_font_description_free(font_desc);
1275 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
1276 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
1277 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
1278 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
1279 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1280 gtk_widget_show(text);
1282 if (children->open_in) {
1283 input_hbox = gtk_hbox_new(FALSE, 8);
1284 gtk_widget_show(input_hbox);
1286 entry = gtk_entry_new();
1287 gtk_widget_set_size_request(entry, 320, -1);
1288 g_signal_connect(G_OBJECT(entry), "activate",
1289 G_CALLBACK(send_input), children);
1290 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1291 if (children->action_type & ACTION_USER_HIDDEN_IN) {
1292 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1294 hildon_gtk_entry_set_input_mode(GTK_ENTRY(entry),
1295 HILDON_GTK_INPUT_MODE_FULL |
1296 HILDON_GTK_INPUT_MODE_INVISIBLE);
1299 gtk_widget_show(entry);
1301 send_button = gtk_button_new_from_stock(GTK_STOCK_EXECUTE);
1302 g_signal_connect(G_OBJECT(send_button), "clicked",
1303 G_CALLBACK(send_input), children);
1304 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1306 gtk_widget_show(send_button);
1308 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1309 gtk_widget_grab_focus(entry);
1312 if (children->initial_nb > 1) {
1315 progress_bar = gtk_progress_bar_new();
1316 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1317 GTK_PROGRESS_LEFT_TO_RIGHT);
1318 text = g_strdup_printf("%s 0/%d\n", _("Completed"),
1319 children->initial_nb);
1320 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar),
1323 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1324 gtk_widget_show(progress_bar);
1327 gtkut_stock_button_set_create(&hbox, &abort_button, GTK_STOCK_STOP,
1328 &close_button, GTK_STOCK_CLOSE, NULL, NULL);
1329 g_signal_connect(G_OBJECT(abort_button), "clicked",
1330 G_CALLBACK(kill_children_cb), children);
1331 g_signal_connect(G_OBJECT(close_button), "clicked",
1332 G_CALLBACK(hide_io_dialog_cb), children);
1333 gtk_widget_show(hbox);
1336 gtk_widget_set_sensitive(close_button, FALSE);
1338 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1340 children->dialog = dialog;
1341 children->scrolledwin = scrolledwin;
1342 children->text = text;
1343 children->input_hbox = children->open_in ? input_hbox : NULL;
1344 children->input_entry = children->open_in ? entry : NULL;
1345 children->progress_bar = progress_bar;
1346 children->abort_btn = abort_button;
1347 children->close_btn = close_button;
1349 gtk_widget_show(dialog);
1352 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1354 ChildInfo *child_info = (ChildInfo *)data;
1358 gdk_input_remove(child_info->tag_status);
1360 c = read(source, &buf, 1);
1361 debug_print("Child returned %c\n", buf);
1364 waitpid(-child_info->pid, NULL, 0);
1366 childinfo_close_pipes(child_info);
1367 child_info->pid = 0;
1369 if (child_info->children->action_type & (ACTION_SINGLE | ACTION_MULTIPLE)
1370 && child_info->msginfo_list) {
1371 /* Actions on message *files* might change size and
1372 * time stamp, and thus invalidate the cache */
1373 SummaryView *summaryview = NULL;
1375 MsgInfo *msginfo, *nmi; /* newmsginfo */
1377 gboolean modified_something = FALSE;
1378 FolderItem *last_item = NULL;
1379 if (mainwindow_get_mainwindow ())
1380 summaryview = mainwindow_get_mainwindow ()->summaryview;
1381 for (cur = child_info->msginfo_list; cur; cur = cur->next) {
1382 msginfo = (MsgInfo *)cur->data;
1383 if (!(msginfo && /* Stuff used valid? */
1384 msginfo->folder && msginfo->folder->cache))
1386 file = procmsg_get_message_file_path (msginfo);
1389 nmi = procheader_parse_file (file, msginfo->flags, TRUE, FALSE);
1391 continue; /* Deleted? */
1392 if (msginfo->mtime != nmi->mtime || msginfo->size != nmi->size) {
1393 nmi->folder = msginfo->folder;
1394 nmi->msgnum = msginfo->msgnum;
1395 msgcache_update_msg (msginfo->folder->cache, nmi);
1396 modified_something = TRUE;
1397 last_item = nmi->folder;
1399 procmsg_msginfo_free (nmi);
1400 if (summaryview && summaryview->displayed &&
1401 summaryview->folder_item == msginfo->folder &&
1402 summary_get_msgnum(summaryview, summaryview->displayed) == msginfo->msgnum)
1403 summary_redisplay_msg(summaryview);
1406 if (modified_something && last_item &&
1407 summaryview && summaryview->folder_item == last_item) {
1408 summary_show (summaryview, summaryview->folder_item);
1410 g_slist_free (child_info->msginfo_list);
1411 child_info->msginfo_list = NULL;
1414 wait_for_children(child_info->children);
1417 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1419 Children *children = (Children *)data;
1420 ChildInfo *child_info = (ChildInfo *)children->list->data;
1421 gchar *input, *ret_str;
1423 gssize by_read = 0, by_written = 0;
1425 debug_print("Sending input to grand child.\n");
1426 if (!(cond && GDK_INPUT_WRITE))
1429 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1430 gtk_widget_grab_focus(children->abort_btn);
1432 gdk_input_remove(child_info->tag_in);
1433 child_info->tag_in = -1;
1435 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1437 ret_str = g_locale_from_utf8(input, strlen(input), &by_read,
1439 if (ret_str && by_written) {
1444 len = strlen(input);
1448 c = write(child_info->chld_in, input + count, len - count);
1451 } while (c >= 0 && count < len);
1454 (void)write(child_info->chld_in, "\n", 2);
1458 (void)close(child_info->chld_in);
1459 child_info->chld_in = -1;
1460 debug_print("Input to grand child sent.\n");
1463 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1465 ChildInfo *child_info = (ChildInfo *)data;
1467 gchar buf[BUFFSIZE];
1469 debug_print("Catching grand child's output.\n");
1470 if (child_info->children->action_type &
1471 (ACTION_PIPE_OUT | ACTION_INSERT)
1472 && source == child_info->chld_out) {
1474 GTK_TEXT_VIEW(child_info->children->msg_text);
1475 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
1480 mark = gtk_text_buffer_get_insert(textbuf);
1481 gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1482 ins_pos = gtk_text_iter_get_offset(&iter);
1485 gsize bytes_read = 0, bytes_written = 0;
1488 c = read(source, buf, sizeof(buf) - 1);
1492 ret_str = g_locale_to_utf8
1493 (buf, c, &bytes_read, &bytes_written, NULL);
1494 if (ret_str && bytes_written > 0) {
1495 gtk_text_buffer_insert
1496 (textbuf, &iter, ret_str,
1500 gtk_text_buffer_insert(textbuf, &iter, buf, c);
1503 if (child_info->children->is_selection) {
1506 gtk_text_buffer_get_iter_at_offset
1507 (textbuf, &ins, ins_pos);
1508 gtk_text_buffer_select_range(textbuf, &ins, &iter);
1511 c = read(source, buf, sizeof(buf) - 1);
1513 gsize bytes_read = 0, bytes_written = 0;
1516 ret_str = g_locale_to_utf8
1517 (buf, c, &bytes_read, &bytes_written, NULL);
1518 if (ret_str && bytes_written > 0) {
1520 (child_info->output, ret_str,
1524 g_string_append_len(child_info->output, buf, c);
1526 child_info->new_out = TRUE;
1530 if (source == child_info->chld_out) {
1531 gdk_input_remove(child_info->tag_out);
1532 child_info->tag_out = -1;
1533 (void)close(child_info->chld_out);
1534 child_info->chld_out = -1;
1536 gdk_input_remove(child_info->tag_err);
1537 child_info->tag_err = -1;
1538 (void)close(child_info->chld_err);
1539 child_info->chld_err = -1;
1543 wait_for_children(child_info->children);
1546 static gchar *get_user_string(const gchar *action, ActionType type)
1549 gchar *user_str = NULL;
1552 case ACTION_USER_HIDDEN_STR:
1553 message = g_strdup_printf
1554 (_("Enter the argument for the following action:\n"
1555 "('%%h' will be replaced with the argument)\n"
1558 user_str = input_dialog_with_invisible
1559 (_("Action's hidden user argument"), message, NULL);
1561 case ACTION_USER_STR:
1562 message = g_strdup_printf
1563 (_("Enter the argument for the following action:\n"
1564 "('%%u' will be replaced with the argument)\n"
1567 user_str = input_dialog
1568 (_("Action's user argument"), message, NULL);
1571 g_warning("Unsupported action type %d", type);