2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2011 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;
104 void (*callback)(void *data);
107 GSList *msginfo_list;
110 static void action_update_menu (GtkUIManager *ui_manager,
111 const gchar *accel_group,
115 static void compose_actions_execute_cb (GtkWidget *widget,
117 static void compose_actions_execute (Compose *compose,
121 static void mainwin_actions_execute_cb (GtkWidget *widget,
123 static void mainwin_actions_execute (MainWindow *mainwin,
127 static void msgview_actions_execute_cb (GtkWidget *widget,
129 static void msgview_actions_execute (MessageView *msgview,
133 static void message_actions_execute (MessageView *msgview,
137 static gboolean execute_filtering_actions(gchar *action,
140 static gboolean execute_actions (gchar *action,
145 void (*callback)(void *data),
148 static gchar *parse_action_cmd (gchar *action,
152 const gchar *user_str,
153 const gchar *user_hidden_str,
154 const gchar *sel_str);
155 static gboolean parse_append_filename (GString *cmd,
158 static gboolean parse_append_msgpart (GString *cmd,
162 static ChildInfo *fork_child (gchar *cmd,
163 const gchar *msg_str,
166 static gint wait_for_children (Children *children);
168 static void free_children (Children *children);
170 static void childinfo_close_pipes (ChildInfo *child_info);
172 static void create_io_dialog (Children *children);
173 static void update_io_dialog (Children *children);
175 static void hide_io_dialog_cb (GtkWidget *widget,
177 static gint io_dialog_key_pressed_cb (GtkWidget *widget,
181 static void catch_output (gpointer data,
184 static void catch_input (gpointer data,
187 static void catch_status (gpointer data,
191 static gchar *get_user_string (const gchar *action,
195 ActionType action_get_type(const gchar *action_str)
198 gboolean in_filtering_action = FALSE;
199 ActionType action_type = ACTION_NONE;
201 cm_return_val_if_fail(action_str, ACTION_ERROR);
202 cm_return_val_if_fail(*action_str, ACTION_ERROR);
207 action_type |= ACTION_PIPE_IN;
209 } else if (p[0] == '>') {
210 action_type |= ACTION_USER_IN;
212 } else if (p[0] == '*') {
213 action_type |= ACTION_USER_HIDDEN_IN;
220 while (*p && action_type != ACTION_ERROR) {
221 if (!in_filtering_action) {
222 if (p[0] == '%' && p[1]) {
225 /* CLAWS: filtering action is a mutually exclusive
226 * action. we can enable others if needed later. we
227 * add ACTION_SINGLE | ACTION_MULTIPLE so it will
228 * only be executed from the main window toolbar */
229 if (p[2] == 's') /* source messages */
230 action_type = ACTION_FILTERING_ACTION
233 in_filtering_action = TRUE;
236 action_type |= ACTION_SINGLE;
239 action_type |= ACTION_MULTIPLE;
242 action_type |= ACTION_SINGLE;
245 action_type |= ACTION_SELECTION_STR;
248 action_type |= ACTION_USER_STR;
251 action_type |= ACTION_USER_HIDDEN_STR;
257 action_type = ACTION_ERROR;
261 } else if (p[0] == '|') {
263 action_type |= ACTION_PIPE_OUT;
264 } else if (p[0] == '>') {
266 action_type |= ACTION_INSERT;
267 } else if (p[0] == '&') {
269 action_type |= ACTION_ASYNC;
270 } else if (p[0] == '}') {
271 in_filtering_action = FALSE;
280 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
281 GSList *msg_list, MimeInfo *partinfo,
282 const gchar *user_str,
283 const gchar *user_hidden_str,
284 const gchar *sel_str)
292 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
295 cmd = g_string_sized_new(strlen(action));
298 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
299 if (p[0] == '%' && p[1]) {
302 if (!parse_append_filename(cmd, msginfo)) {
303 g_string_free(cmd, TRUE);
309 for (cur = msg_list; cur != NULL;
311 MsgInfo *msg = (MsgInfo *)cur->data;
313 if (!parse_append_filename(cmd, msg)) {
314 g_string_free(cmd, TRUE);
318 g_string_append_c(cmd, ' ');
323 if (!parse_append_msgpart(cmd, msginfo,
325 g_string_free(cmd, TRUE);
332 g_string_append(cmd, sel_str);
337 g_string_append(cmd, user_str);
342 g_string_append(cmd, user_hidden_str);
346 g_string_append_c(cmd, p[1]);
350 g_string_append_c(cmd, p[0]);
351 g_string_append_c(cmd, p[1]);
355 g_string_append_c(cmd, p[0]);
360 g_string_free(cmd, TRUE);
365 g_string_free(cmd, FALSE);
369 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
373 gchar escape_ch[] = "\\ ";
375 cm_return_val_if_fail(msginfo, FALSE);
377 filename = procmsg_get_message_file(msginfo);
380 alertpanel_error(_("Could not get message file %d"),
386 while ((q = strpbrk(p, "$\"`'\\ \t*?[]&|;<>()!#~")) != NULL) {
389 g_string_append(cmd, p);
390 g_string_append(cmd, escape_ch);
393 g_string_append(cmd, p);
400 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
403 gboolean single_part = FALSE;
405 gchar *part_filename;
409 partinfo = procmime_scan_message(msginfo);
411 alertpanel_error(_("Could not get message part."));
418 filename = procmsg_get_message_file_path(msginfo);
419 part_filename = procmime_get_tmp_file_name(partinfo);
421 ret = procmime_get_part(part_filename, partinfo);
424 procmime_mimeinfo_free_all(partinfo);
428 alertpanel_error(_("Can't get part of multipart message: %s"), strerror(-ret));
429 g_free(part_filename);
433 g_string_append(cmd, part_filename);
435 g_free(part_filename);
440 void actions_execute(gpointer data,
445 if (source == TOOLBAR_MAIN)
446 mainwin_actions_execute((MainWindow*)data, action_nb, widget);
447 else if (source == TOOLBAR_COMPOSE)
448 compose_actions_execute((Compose*)data, action_nb, widget);
449 else if (source == TOOLBAR_MSGVIEW)
450 msgview_actions_execute((MessageView*)data, action_nb, widget);
453 void action_update_mainwin_menu(GtkUIManager *ui_manager,
457 action_update_menu(ui_manager, "<MainwinActions>", branch_path,
458 mainwin_actions_execute_cb, mainwin);
461 void action_update_msgview_menu(GtkUIManager *ui_manager,
463 MessageView *msgview)
465 action_update_menu(ui_manager, "<MsgviewActions>", branch_path,
466 msgview_actions_execute_cb, msgview);
469 void action_update_compose_menu(GtkUIManager *ui_manager,
473 action_update_menu(ui_manager, "<ComposeActions>", branch_path,
474 compose_actions_execute_cb, compose);
477 static GtkWidget *find_item_in_menu(GtkWidget *menu, gchar *name)
479 GList *children = gtk_container_get_children(GTK_CONTAINER(GTK_MENU_SHELL(menu)));
480 GList *amenu = children;
481 const gchar *existing_name;
483 GtkWidget *item = GTK_WIDGET(amenu->data);
484 if ((existing_name = g_object_get_data(G_OBJECT(item), "s_name")) != NULL &&
485 !strcmp2(name, existing_name))
487 g_list_free(children);
493 g_list_free(children);
498 static GtkWidget *create_submenus(GtkWidget *menu, const gchar *action)
500 gchar *submenu = g_strdup(action);
501 GtkWidget *new_menu = NULL;
503 if (strchr(submenu, '/')) {
504 const gchar *end = (strchr(submenu, '/')+1);
505 GtkWidget *menu_item = NULL;
507 *strchr(submenu, '/') = '\0';
508 if ((menu_item = find_item_in_menu(menu, submenu)) == NULL) {
509 menu_item = gtk_menu_item_new_with_mnemonic(submenu);
510 g_object_set_data_full(G_OBJECT(menu_item), "s_name", g_strdup(submenu), g_free);
511 gtk_widget_show(menu_item);
512 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);
513 new_menu = gtk_menu_new();
514 gtk_widget_show(new_menu);
515 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), new_menu);
517 new_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(menu_item));
519 new_menu = create_submenus(new_menu, end);
523 return new_menu ? new_menu : menu;
526 static void action_update_menu(GtkUIManager *ui_manager,
527 const gchar *accel_group,
529 gpointer callback, gpointer data)
532 gchar *action, *action_p;
533 int callback_action = 0;
534 GtkWidget *menu = gtk_menu_new();
537 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
538 GtkWidget *cur_menu = menu;
539 const gchar *action_name = NULL;
540 action = g_strdup((gchar *)cur->data);
541 action_p = strstr(action, ": ");
542 if (action_p && action_p[2] &&
543 (action_get_type(&action_p[2]) != ACTION_ERROR) &&
544 (action[0] != '/')) {
545 gchar *accel_path = NULL;
548 if (strchr(action, '/')) {
549 cur_menu = create_submenus(cur_menu, action);
550 action_name = strrchr(action, '/')+1;
552 action_name = action;
554 gtk_menu_set_accel_group (GTK_MENU (cur_menu),
555 gtk_ui_manager_get_accel_group(ui_manager));
556 item = gtk_menu_item_new_with_label(action_name);
557 gtk_menu_shell_append(GTK_MENU_SHELL(cur_menu), item);
558 g_signal_connect(G_OBJECT(item), "activate",
559 G_CALLBACK(callback), data);
560 g_object_set_data(G_OBJECT(item), "action_num", GINT_TO_POINTER(callback_action));
561 gtk_widget_show(item);
562 accel_path = g_strconcat(accel_group,branch_path, "/", action, NULL);
563 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(item), accel_path);
571 gtk_widget_show(menu);
572 gtk_menu_item_set_submenu(GTK_MENU_ITEM(gtk_ui_manager_get_widget(ui_manager, branch_path)), menu);
575 static void compose_actions_execute_cb(GtkWidget *widget, gpointer data)
577 Compose *compose = (Compose *)data;
578 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
579 compose_actions_execute(compose, action_nb, NULL);
582 static void compose_actions_execute(Compose *compose, guint action_nb, GtkWidget *widget)
585 ActionType action_type;
587 cm_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
589 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
590 cm_return_if_fail(buf != NULL);
591 action = strstr(buf, ": ");
592 cm_return_if_fail(action != NULL);
594 /* Point to the beginning of the command-line */
597 action_type = action_get_type(action);
598 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
600 (_("The selected action cannot be used in the compose window\n"
601 "because it contains %%f, %%F, %%as or %%p."));
605 execute_actions(action, NULL, compose->text, 0, NULL,
606 compose_action_cb, compose);
609 static void mainwin_actions_execute_cb(GtkWidget *widget, gpointer data)
611 MainWindow *mainwin = (MainWindow *)data;
612 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
613 mainwin_actions_execute(mainwin, action_nb, NULL);
616 static void mainwin_actions_execute(MainWindow *mainwin, guint action_nb,
621 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
622 message_actions_execute(mainwin->messageview, action_nb, msg_list);
623 g_slist_free(msg_list);
626 static void msgview_actions_execute_cb(GtkWidget *widget, gpointer data)
628 MessageView *msgview = (MessageView *)data;
629 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
630 msgview_actions_execute(msgview, action_nb, NULL);
633 static void msgview_actions_execute(MessageView *msgview, guint action_nb,
636 GSList *msg_list = NULL;
638 if (msgview->msginfo)
639 msg_list = g_slist_append(msg_list, msgview->msginfo);
640 message_actions_execute(msgview, action_nb, msg_list);
641 g_slist_free(msg_list);
644 static void message_actions_execute(MessageView *msgview, guint action_nb,
651 GtkWidget *text = NULL;
653 ActionType action_type;
655 cm_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
657 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
659 cm_return_if_fail(buf);
660 cm_return_if_fail((action = strstr(buf, ": ")));
662 /* Point to the beginning of the command-line */
665 textview = messageview_get_current_textview(msgview);
667 text = textview->text;
668 body_pos = textview->body_pos;
670 partinfo = messageview_get_selected_mime_part(msgview);
672 /* this command will alter the message text */
673 action_type = action_get_type(action);
674 if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
675 msgview->filtered = TRUE;
677 if (action_type & ACTION_FILTERING_ACTION)
678 /* CLAWS: most of the above code is not necessary for applying
680 execute_filtering_actions(action, msg_list);
682 execute_actions(action, msg_list, text, body_pos, partinfo,
686 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
688 GSList *action_list, *p;
689 const gchar *sbegin, *send;
690 gchar *action_string;
691 SummaryView *summaryview = NULL;
692 MainWindow *mainwin = NULL;
694 if (mainwindow_get_mainwindow()) {
695 summaryview = mainwindow_get_mainwindow()->summaryview;
696 mainwin = mainwindow_get_mainwindow();
699 if (NULL == (sbegin = strstr2(action, "%as{")))
701 sbegin += sizeof "%as{" - 1;
702 if (NULL == (send = strrchr(sbegin, '}')))
704 action_string = g_strndup(sbegin, send - sbegin);
706 action_list = matcher_parser_get_action_list(action_string);
707 if (action_list == NULL) {
708 gchar *tmp = g_strdup(action_string);
712 alertpanel_error(_("There is no filtering action set"));
714 alertpanel_error(_("Invalid filtering action(s):\n%s"), tmp);
715 g_free(action_string);
719 g_free(action_string);
721 /* apply actions on each message info */
722 for (p = msglist; p && p->data; p = g_slist_next(p)) {
723 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
727 summary_lock(summaryview);
728 main_window_cursor_wait(mainwin);
729 summary_freeze(summaryview);
730 folder_item_update_freeze();
733 filtering_move_and_copy_msgs(msglist);
736 folder_item_update_thaw();
737 summary_thaw(summaryview);
738 main_window_cursor_normal(mainwin);
739 summary_unlock(summaryview);
740 summary_show(summaryview, summaryview->folder_item);
742 for (p = action_list; p; p = g_slist_next(p))
743 if (p->data) filteringaction_free(p->data);
744 g_slist_free(action_list);
748 static gboolean execute_actions(gchar *action, GSList *msg_list,
750 gint body_pos, MimeInfo *partinfo,
751 void (*callback)(void *data), void *data)
753 GSList *children_list = NULL;
757 ChildInfo *child_info;
758 ActionType action_type;
761 gchar *sel_str = NULL;
762 gchar *msg_str = NULL;
763 gchar *user_str = NULL;
764 gchar *user_hidden_str = NULL;
765 GtkTextIter start_iter, end_iter;
766 gboolean is_selection = FALSE;
768 cm_return_val_if_fail(action && *action, FALSE);
770 action_type = action_get_type(action);
772 if (action_type == ACTION_ERROR)
773 return FALSE; /* ERR: syntax error */
775 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
776 return FALSE; /* ERR: file command without selection */
778 msg_list_len = g_slist_length(msg_list);
780 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
781 if (msg_list_len > 1)
782 return FALSE; /* ERR: pipe + multiple selection */
784 return FALSE; /* ERR: pipe and no displayed text */
787 if (action_type & ACTION_SELECTION_STR) {
789 return FALSE; /* ERR: selection string but no text */
793 GtkTextBuffer *textbuf;
795 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
796 is_selection = gtk_text_buffer_get_selection_bounds
797 (textbuf, &start_iter, &end_iter);
799 gtk_text_buffer_get_iter_at_offset
800 (textbuf, &start_iter, body_pos);
801 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
803 msg_str = gtk_text_buffer_get_text
804 (textbuf, &start_iter, &end_iter, FALSE);
806 sel_str = g_strdup(msg_str);
809 if (action_type & ACTION_USER_STR) {
810 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
817 if (action_type & ACTION_USER_HIDDEN_STR) {
818 if (!(user_hidden_str =
819 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
827 if (text && (action_type & ACTION_PIPE_OUT)) {
828 GtkTextBuffer *textbuf;
829 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
830 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
833 children = g_new0(Children, 1);
835 children->action = g_strdup(action);
836 children->action_type = action_type;
837 children->msg_text = text;
838 children->is_selection = is_selection;
840 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
841 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
842 children->open_in = 1;
844 if (action_type & ACTION_SINGLE) {
847 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
848 msginfo = (MsgInfo *)cur->data;
850 is_ok = FALSE; /* ERR: msginfo missing */
853 cmd = parse_action_cmd(action, msginfo, msg_list,
855 user_hidden_str, sel_str);
857 debug_print("Action command error\n");
858 is_ok = FALSE; /* ERR: incorrect command */
861 if ((child_info = fork_child(cmd, msg_str, children))) {
862 /* Pass msginfo to catch_status () */
863 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
864 child_info->msginfo_list =
865 g_slist_append (NULL, msginfo);
866 children_list = g_slist_append(children_list,
872 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
873 user_str, user_hidden_str, sel_str);
875 if ((child_info = fork_child(cmd, msg_str, children))) {
876 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
877 child_info->msginfo_list =
878 g_slist_copy (msg_list);
879 children_list = g_slist_append(children_list,
884 is_ok = FALSE; /* ERR: incorrect command */
890 g_free(user_hidden_str);
892 if (!children_list) {
893 /* If not waiting for children, return */
894 free_children(children);
898 children->list = children_list;
899 children->nb = g_slist_length(children_list);
900 children->initial_nb = children->nb;
902 for (cur = children_list; cur; cur = cur->next) {
903 child_info = (ChildInfo *) cur->data;
904 child_info->callback = callback;
905 child_info->data = data;
906 child_info->tag_status =
907 claws_input_add(child_info->chld_status,
908 G_IO_IN | G_IO_HUP | G_IO_ERR,
909 catch_status, child_info,
913 create_io_dialog(children);
918 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
922 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
923 gchar *cmdline[4], *ret_str;
925 ChildInfo *child_info;
927 gssize by_written = 0, by_read = 0;
929 sync = !(children->action_type & ACTION_ASYNC);
931 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
932 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
935 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
937 alertpanel_error(_("Command could not be started. "
938 "Pipe creation failed.\n%s"),
940 /* Closing fd = -1 fails silently */
941 (void)close(chld_in[0]);
942 (void)close(chld_in[1]);
943 (void)close(chld_out[0]);
944 (void)close(chld_out[1]);
945 (void)close(chld_err[0]);
946 (void)close(chld_err[1]);
947 (void)close(chld_status[0]);
948 (void)close(chld_status[1]);
949 return NULL; /* Pipe error */
953 debug_print("Forking child and grandchild.\n");
954 debug_print("Executing: /bin/sh -c %s\n", cmd);
957 if (pid == 0) { /* Child */
965 if (setpgid(0, getppid()))
969 if (children->action_type &
972 ACTION_USER_HIDDEN_IN)) {
973 r |= close(fileno(stdin));
974 (void) dup (chld_in[0]);
976 r |= close(chld_in[0]);
977 r |= close(chld_in[1]);
979 r |= close(fileno(stdout));
980 (void) dup (chld_out[1]);
981 r |= close(chld_out[0]);
982 r |= close(chld_out[1]);
984 r |= close(fileno(stderr));
985 (void) dup (chld_err[1]);
986 r |= close(chld_err[0]);
987 r |= close(chld_err[1]);
990 debug_print("%s(%d)", strerror(errno), errno);
995 ret_str = g_locale_from_utf8(cmd, strlen(cmd),
996 &by_read, &by_written,
998 if (ret_str && by_written)
999 cmdline[2] = ret_str;
1003 execvp("/bin/sh", cmdline);
1008 } else if (gch_pid < (pid_t) 0) { /* Fork error */
1010 r = write(chld_status[1], "1\n", 2);
1012 debug_print("%s(%d)", strerror(errno), errno);
1015 } else { /* Child */
1017 r |= close(chld_in[0]);
1018 r |= close(chld_in[1]);
1019 r |= close(chld_out[0]);
1020 r |= close(chld_out[1]);
1021 r |= close(chld_err[0]);
1022 r |= close(chld_err[1]);
1023 r |= close(chld_status[0]);
1025 debug_print("Child: waiting for grandchild\n");
1026 r |= waitpid(gch_pid, NULL, 0);
1027 debug_print("Child: grandchild ended\n");
1028 r |= write(chld_status[1], "0\n", 2);
1029 r |= close(chld_status[1]);
1032 debug_print("%s(%d)", strerror(errno), errno);
1036 } else if (pid < 0) { /* Fork error */
1037 alertpanel_error(_("Could not fork to execute the following "
1038 "command:\n%s\n%s"),
1039 cmd, g_strerror(errno));
1046 waitpid(pid, NULL, 0);
1050 (void)close(chld_in[0]);
1051 if (!(children->action_type &
1052 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
1053 (void)close(chld_in[1]);
1054 (void)close(chld_out[1]);
1055 (void)close(chld_err[1]);
1056 (void)close(chld_status[1]);
1058 child_info = g_new0(ChildInfo, 1);
1060 child_info->children = children;
1062 child_info->pid = pid;
1063 child_info->next_sig = SIGTERM;
1064 child_info->cmd = g_strdup(cmd);
1065 child_info->new_out = FALSE;
1066 child_info->output = g_string_new(NULL);
1067 child_info->chld_in =
1068 (children->action_type &
1069 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
1071 child_info->chld_out = chld_out[0];
1072 child_info->chld_err = chld_err[0];
1073 child_info->chld_status = chld_status[0];
1074 child_info->tag_in = -1;
1075 child_info->tag_out = claws_input_add(chld_out[0], G_IO_IN | G_IO_HUP | G_IO_ERR,
1076 catch_output, child_info, FALSE);
1077 child_info->tag_err = claws_input_add(chld_err[0], G_IO_IN | G_IO_HUP | G_IO_ERR,
1078 catch_output, child_info, FALSE);
1080 if (!(children->action_type &
1081 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
1084 if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
1086 ret_str = g_locale_from_utf8(msg_str, strlen(msg_str),
1087 &by_read, &by_written, NULL);
1088 if (ret_str && by_written) {
1089 r = write(chld_in[1], ret_str, strlen(ret_str));
1092 r = write(chld_in[1], msg_str, strlen(msg_str));
1093 if (!(children->action_type &
1094 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
1095 r = close(chld_in[1]);
1096 child_info->chld_in = -1; /* No more input */
1098 debug_print("%s(%d)", strerror(errno), errno);
1104 #endif /* G_OS_UNIX */
1107 static void kill_children_cb(GtkWidget *widget, gpointer data)
1111 Children *children = (Children *) data;
1112 ChildInfo *child_info;
1114 for (cur = children->list; cur; cur = cur->next) {
1115 child_info = (ChildInfo *)(cur->data);
1116 debug_print("Killing child group id %d\n", child_info->pid);
1117 if (child_info->pid && kill(-child_info->pid, child_info->next_sig) < 0)
1119 child_info->next_sig = SIGKILL;
1121 #endif /* G_OS_UNIX */
1124 static gint wait_for_children(Children *children)
1126 gboolean new_output;
1127 ChildInfo *child_info;
1130 cur = children->list;
1133 child_info = (ChildInfo *)cur->data;
1134 new_output |= child_info->new_out;
1138 children->output |= new_output;
1140 if (new_output || (children->dialog && (children->initial_nb != children->nb)))
1141 update_io_dialog(children);
1146 if (!children->dialog) {
1147 free_children(children);
1148 } else if (!children->output) {
1149 gtk_widget_destroy(children->dialog);
1155 static void send_input(GtkWidget *w, gpointer data)
1157 Children *children = (Children *) data;
1158 ChildInfo *child_info = (ChildInfo *) children->list->data;
1160 child_info->tag_in = claws_input_add(child_info->chld_in,
1161 G_IO_OUT | G_IO_ERR,
1162 catch_input, children, FALSE);
1165 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1167 hide_io_dialog_cb(w, data);
1171 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1174 Children *children = (Children *)data;
1176 if (!children->nb) {
1177 g_signal_handlers_disconnect_matched
1178 (G_OBJECT(children->dialog), G_SIGNAL_MATCH_DATA,
1179 0, 0, NULL, NULL, children);
1180 gtk_widget_destroy(children->dialog);
1181 free_children(children);
1185 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1188 if (event && (event->keyval == GDK_KEY_Escape ||
1189 event->keyval == GDK_KEY_Return ||
1190 event->keyval == GDK_KEY_KP_Enter))
1191 hide_io_dialog_cb(widget, data);
1195 static void childinfo_close_pipes(ChildInfo *child_info)
1197 /* stdout and stderr pipes are guaranteed to be removed by
1198 * their handler, but in case where we receive child exit notification
1199 * before grand-child's pipes closing signals, we check them and close
1202 if (child_info->tag_in > 0)
1203 g_source_remove(child_info->tag_in);
1204 if (child_info->tag_out > 0)
1205 g_source_remove(child_info->tag_out);
1206 if (child_info->tag_err > 0)
1207 g_source_remove(child_info->tag_err);
1209 if (child_info->chld_in >= 0)
1210 (void)close(child_info->chld_in);
1211 if (child_info->chld_out >= 0)
1212 (void)close(child_info->chld_out);
1213 if (child_info->chld_err >= 0)
1214 (void)close(child_info->chld_err);
1216 (void)close(child_info->chld_status);
1219 static void free_children(Children *children)
1221 ChildInfo *child_info;
1222 void (*callback)(void *data) = NULL;
1225 debug_print("Freeing children data %p\n", children);
1227 g_free(children->action);
1228 while (children->list != NULL) {
1229 child_info = (ChildInfo *)children->list->data;
1230 g_free(child_info->cmd);
1231 g_string_free(child_info->output, TRUE);
1232 children->list = g_slist_remove(children->list, child_info);
1233 callback = child_info->callback;
1234 data = child_info->data;
1244 static void update_io_dialog(Children *children)
1248 debug_print("Updating actions input/output dialog.\n");
1250 if (children->progress_bar) {
1253 /* use a more compact format */
1254 const gchar *format = "%s %d/%d";
1256 const gchar *format = "%s %d / %d";
1259 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar),
1260 (children->initial_nb == 0) ? 0 :
1261 (gdouble) (children->initial_nb - children->nb) /
1262 (gdouble) children->initial_nb);
1263 text = g_strdup_printf(format, _("Completed"),
1264 children->initial_nb - children->nb,
1265 children->initial_nb);
1266 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(children->progress_bar), text);
1270 if (!children->nb) {
1271 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1272 gtk_widget_set_sensitive(children->close_btn, TRUE);
1273 if (children->input_hbox)
1274 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1275 gtk_widget_grab_focus(children->close_btn);
1276 g_signal_connect(G_OBJECT(children->dialog),
1278 G_CALLBACK(io_dialog_key_pressed_cb),
1282 if (children->output) {
1283 GtkWidget *text = children->text;
1284 GtkTextBuffer *textbuf;
1285 GtkTextIter iter, start_iter, end_iter;
1287 ChildInfo *child_info;
1289 gtk_widget_show(children->scrolledwin);
1290 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
1291 gtk_text_buffer_get_bounds(textbuf, &start_iter, &end_iter);
1292 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
1293 gtk_text_buffer_get_start_iter(textbuf, &iter);
1295 for (cur = children->list; cur; cur = cur->next) {
1296 child_info = (ChildInfo *)cur->data;
1297 if (child_info->pid)
1298 caption = g_strdup_printf
1299 (_("--- Running: %s\n"),
1302 caption = g_strdup_printf
1303 (_("--- Ended: %s\n"),
1306 gtk_text_buffer_insert(textbuf, &iter, caption, -1);
1307 gtk_text_buffer_insert(textbuf, &iter,
1308 child_info->output->str, -1);
1310 child_info->new_out = FALSE;
1315 static void create_io_dialog(Children *children)
1319 GtkWidget *entry = NULL;
1320 GtkWidget *input_hbox = NULL;
1321 GtkWidget *send_button;
1324 GtkWidget *scrolledwin;
1326 GtkWidget *progress_bar = NULL;
1327 GtkWidget *abort_button;
1328 GtkWidget *close_button;
1330 debug_print("Creating action IO dialog\n");
1332 dialog = gtk_dialog_new();
1333 gtk_container_set_border_width
1334 (GTK_CONTAINER(gtk_dialog_get_action_area(GTK_DIALOG(dialog))), 5);
1335 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1336 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1337 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1338 manage_window_set_transient(GTK_WINDOW(dialog));
1339 g_signal_connect(G_OBJECT(dialog), "delete_event",
1340 G_CALLBACK(delete_io_dialog_cb), children);
1341 g_signal_connect(G_OBJECT(dialog), "destroy",
1342 G_CALLBACK(hide_io_dialog_cb),
1345 vbox = gtk_vbox_new(FALSE, 8);
1346 gtk_container_add(GTK_CONTAINER(
1347 gtk_dialog_get_content_area(GTK_DIALOG(dialog))), vbox);
1348 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1349 gtk_widget_show(vbox);
1351 label = gtk_label_new(children->action);
1352 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1353 gtk_widget_show(label);
1355 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1356 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1357 GTK_POLICY_AUTOMATIC,
1358 GTK_POLICY_AUTOMATIC);
1359 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
1361 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1362 gtk_widget_set_size_request(scrolledwin, 560, 200);
1363 gtk_widget_hide(scrolledwin);
1365 text = gtk_text_view_new();
1367 if (prefs_common.textfont) {
1368 PangoFontDescription *font_desc;
1369 font_desc = pango_font_description_from_string
1370 (prefs_common.textfont);
1372 gtk_widget_modify_font(text, font_desc);
1373 pango_font_description_free(font_desc);
1377 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
1378 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
1379 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
1380 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
1381 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1382 gtk_widget_show(text);
1384 if (children->open_in) {
1385 input_hbox = gtk_hbox_new(FALSE, 8);
1386 gtk_widget_show(input_hbox);
1388 entry = gtk_entry_new();
1389 gtk_widget_set_size_request(entry, 320, -1);
1390 g_signal_connect(G_OBJECT(entry), "activate",
1391 G_CALLBACK(send_input), children);
1392 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1393 if (children->action_type & ACTION_USER_HIDDEN_IN) {
1394 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1396 hildon_gtk_entry_set_input_mode(GTK_ENTRY(entry),
1397 HILDON_GTK_INPUT_MODE_FULL |
1398 HILDON_GTK_INPUT_MODE_INVISIBLE);
1401 gtk_widget_show(entry);
1403 send_button = gtk_button_new_from_stock(GTK_STOCK_EXECUTE);
1404 g_signal_connect(G_OBJECT(send_button), "clicked",
1405 G_CALLBACK(send_input), children);
1406 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1408 gtk_widget_show(send_button);
1410 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1411 gtk_widget_grab_focus(entry);
1414 if (children->initial_nb > 1) {
1417 /* use a more compact format */
1418 const gchar *format = "%s 0/%d\n";
1420 const gchar *format = "%s 0 / %d\n";
1423 progress_bar = gtk_progress_bar_new();
1424 #if !GTK_CHECK_VERSION(3, 0, 0)
1425 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1426 GTK_PROGRESS_LEFT_TO_RIGHT);
1428 gtk_orientable_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1429 GTK_ORIENTATION_HORIZONTAL);
1430 gtk_progress_bar_set_inverted(GTK_PROGRESS_BAR(progress_bar),
1433 text = g_strdup_printf(format, _("Completed"),
1434 children->initial_nb);
1435 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar),
1438 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1439 gtk_widget_show(progress_bar);
1442 gtkut_stock_button_set_create(&hbox, &abort_button, GTK_STOCK_STOP,
1443 &close_button, GTK_STOCK_CLOSE, NULL, NULL);
1444 g_signal_connect(G_OBJECT(abort_button), "clicked",
1445 G_CALLBACK(kill_children_cb), children);
1446 g_signal_connect(G_OBJECT(close_button), "clicked",
1447 G_CALLBACK(hide_io_dialog_cb), children);
1448 gtk_widget_show(hbox);
1451 gtk_widget_set_sensitive(close_button, FALSE);
1453 gtk_container_add(GTK_CONTAINER(
1454 gtk_dialog_get_action_area(GTK_DIALOG(dialog))), hbox);
1456 children->dialog = dialog;
1457 children->scrolledwin = scrolledwin;
1458 children->text = text;
1459 children->input_hbox = children->open_in ? input_hbox : NULL;
1460 children->input_entry = children->open_in ? entry : NULL;
1461 children->progress_bar = progress_bar;
1462 children->abort_btn = abort_button;
1463 children->close_btn = close_button;
1465 gtk_widget_show(dialog);
1468 static void catch_status(gpointer data, gint source, GIOCondition cond)
1470 ChildInfo *child_info = (ChildInfo *)data;
1474 g_source_remove(child_info->tag_status);
1476 c = read(source, &buf, 1);
1477 debug_print("Child returned %c\n", buf);
1480 waitpid(-child_info->pid, NULL, 0);
1482 childinfo_close_pipes(child_info);
1483 child_info->pid = 0;
1485 if (child_info->children->action_type & (ACTION_SINGLE | ACTION_MULTIPLE)
1486 && child_info->msginfo_list) {
1487 /* Actions on message *files* might change size and
1488 * time stamp, and thus invalidate the cache */
1489 SummaryView *summaryview = NULL;
1491 MsgInfo *msginfo, *nmi; /* newmsginfo */
1493 gboolean modified_something = FALSE;
1494 FolderItem *last_item = NULL;
1495 if (mainwindow_get_mainwindow ())
1496 summaryview = mainwindow_get_mainwindow ()->summaryview;
1497 for (cur = child_info->msginfo_list; cur; cur = cur->next) {
1498 msginfo = (MsgInfo *)cur->data;
1499 if (!(msginfo && /* Stuff used valid? */
1500 msginfo->folder && msginfo->folder->cache))
1502 file = procmsg_get_message_file_path (msginfo);
1505 nmi = procheader_parse_file (file, msginfo->flags, TRUE, FALSE);
1507 continue; /* Deleted? */
1508 if (msginfo->mtime != nmi->mtime || msginfo->size != nmi->size) {
1509 nmi->folder = msginfo->folder;
1510 nmi->msgnum = msginfo->msgnum;
1511 msgcache_update_msg (msginfo->folder->cache, nmi);
1512 modified_something = TRUE;
1513 last_item = nmi->folder;
1515 procmsg_msginfo_free (nmi);
1516 if (summaryview && summaryview->displayed &&
1517 summaryview->folder_item == msginfo->folder &&
1518 summary_get_msgnum(summaryview, summaryview->displayed) == msginfo->msgnum)
1519 summary_redisplay_msg(summaryview);
1522 if (modified_something && last_item &&
1523 summaryview && summaryview->folder_item == last_item) {
1524 summary_show (summaryview, summaryview->folder_item);
1526 g_slist_free (child_info->msginfo_list);
1527 child_info->msginfo_list = NULL;
1530 if (!child_info->pid)
1531 child_info->children->nb--;
1533 wait_for_children(child_info->children);
1536 static void catch_input(gpointer data, gint source, GIOCondition cond)
1538 Children *children = (Children *)data;
1539 ChildInfo *child_info = (ChildInfo *)children->list->data;
1540 gchar *input, *ret_str;
1541 gint c, count, len, r;
1542 gssize by_read = 0, by_written = 0;
1544 debug_print("Sending input to grand child.\n");
1545 if (!(cond & (G_IO_OUT | G_IO_ERR)))
1548 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1549 gtk_widget_grab_focus(children->abort_btn);
1551 g_source_remove(child_info->tag_in);
1552 child_info->tag_in = -1;
1554 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1556 ret_str = g_locale_from_utf8(input, strlen(input), &by_read,
1558 if (ret_str && by_written) {
1563 len = strlen(input);
1567 c = write(child_info->chld_in, input + count, len - count);
1570 } while (c >= 0 && count < len);
1573 r = write(child_info->chld_in, "\n", 2);
1577 r = close(child_info->chld_in);
1579 debug_print("%s(%d)", strerror(errno), errno);
1580 child_info->chld_in = -1;
1581 debug_print("Input to grand child sent.\n");
1584 static void catch_output(gpointer data, gint source, GIOCondition cond)
1586 ChildInfo *child_info = (ChildInfo *)data;
1588 gchar buf[BUFFSIZE];
1590 debug_print("Catching grand child's output.\n");
1591 if (child_info->children->action_type &
1592 (ACTION_PIPE_OUT | ACTION_INSERT)
1593 && source == child_info->chld_out) {
1595 GTK_TEXT_VIEW(child_info->children->msg_text);
1596 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
1601 mark = gtk_text_buffer_get_insert(textbuf);
1602 gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1603 ins_pos = gtk_text_iter_get_offset(&iter);
1606 gsize bytes_read = 0, bytes_written = 0;
1609 c = read(source, buf, sizeof(buf) - 1);
1613 ret_str = g_locale_to_utf8
1614 (buf, c, &bytes_read, &bytes_written, NULL);
1615 if (ret_str && bytes_written > 0) {
1616 gtk_text_buffer_insert
1617 (textbuf, &iter, ret_str,
1621 gtk_text_buffer_insert(textbuf, &iter, buf, c);
1624 if (child_info->children->is_selection) {
1627 gtk_text_buffer_get_iter_at_offset
1628 (textbuf, &ins, ins_pos);
1629 gtk_text_buffer_select_range(textbuf, &ins, &iter);
1632 c = read(source, buf, sizeof(buf) - 1);
1634 gsize bytes_read = 0, bytes_written = 0;
1637 ret_str = g_locale_to_utf8
1638 (buf, c, &bytes_read, &bytes_written, NULL);
1639 if (ret_str && bytes_written > 0) {
1641 (child_info->output, ret_str,
1645 g_string_append_len(child_info->output, buf, c);
1647 child_info->new_out = TRUE;
1651 if (source == child_info->chld_out) {
1652 g_source_remove(child_info->tag_out);
1653 child_info->tag_out = -1;
1654 (void)close(child_info->chld_out);
1655 child_info->chld_out = -1;
1657 g_source_remove(child_info->tag_err);
1658 child_info->tag_err = -1;
1659 (void)close(child_info->chld_err);
1660 child_info->chld_err = -1;
1664 wait_for_children(child_info->children);
1667 static gchar *get_user_string(const gchar *action, ActionType type)
1670 gchar *user_str = NULL;
1673 case ACTION_USER_HIDDEN_STR:
1674 message = g_strdup_printf
1675 (_("Enter the argument for the following action:\n"
1676 "('%%h' will be replaced with the argument)\n"
1679 user_str = input_dialog_with_invisible
1680 (_("Action's hidden user argument"), message, NULL);
1682 case ACTION_USER_STR:
1683 message = g_strdup_printf
1684 (_("Enter the argument for the following action:\n"
1685 "('%%u' will be replaced with the argument)\n"
1688 user_str = input_dialog
1689 (_("Action's user argument"), message, NULL);
1692 g_warning("Unsupported action type %d", type);