2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2016 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/>.
21 #include "claws-features.h"
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"
56 #include "matcher_parser.h" /* CLAWS */
57 #include "filtering.h"
58 #include "procheader.h"
64 typedef struct _Children Children;
65 typedef struct _ChildInfo ChildInfo;
66 typedef struct _UserStringDialog UserStringDialog;
72 GtkWidget *input_entry;
73 GtkWidget *input_hbox;
74 GtkWidget *progress_bar;
77 GtkWidget *scrolledwin;
80 ActionType action_type;
89 gboolean is_selection;
108 void (*callback)(void *data);
111 GSList *msginfo_list;
114 static void action_update_menu (GtkUIManager *ui_manager,
115 const gchar *accel_group,
119 static void compose_actions_execute_cb (GtkWidget *widget,
121 static void compose_actions_execute (Compose *compose,
125 static void mainwin_actions_execute_cb (GtkWidget *widget,
127 static void mainwin_actions_execute (MainWindow *mainwin,
131 static void msgview_actions_execute_cb (GtkWidget *widget,
133 static void msgview_actions_execute (MessageView *msgview,
137 static void message_actions_execute (MessageView *msgview,
141 static gboolean execute_filtering_actions(gchar *action,
144 static gboolean execute_actions (gchar *action,
149 void (*callback)(void *data),
152 static gchar *parse_action_cmd (gchar *action,
156 const gchar *user_str,
157 const gchar *user_hidden_str,
158 const gchar *sel_str);
159 static gboolean parse_append_filename (GString *cmd,
162 static gboolean parse_append_msgpart (GString *cmd,
166 static ChildInfo *fork_child (gchar *cmd,
167 const gchar *msg_str,
170 static gint wait_for_children (Children *children);
172 static void free_children (Children *children);
174 static void childinfo_close_pipes (ChildInfo *child_info);
176 static void create_io_dialog (Children *children);
177 static void update_io_dialog (Children *children);
179 static void hide_io_dialog_cb (GtkWidget *widget,
181 static gint io_dialog_key_pressed_cb (GtkWidget *widget,
185 static void catch_output (gpointer data,
188 static void catch_input (gpointer data,
191 static void catch_status (GPid pid, gint status, gpointer data);
193 static gchar *get_user_string (const gchar *action,
197 ActionType action_get_type(const gchar *action_str)
200 gboolean in_filtering_action = FALSE;
201 ActionType action_type = ACTION_NONE;
203 cm_return_val_if_fail(action_str, ACTION_ERROR);
204 cm_return_val_if_fail(*action_str, ACTION_ERROR);
209 action_type |= ACTION_PIPE_IN;
211 } else if (p[0] == '>') {
212 action_type |= ACTION_USER_IN;
214 } else if (p[0] == '*') {
215 action_type |= ACTION_USER_HIDDEN_IN;
222 while (*p && action_type != ACTION_ERROR) {
223 if (!in_filtering_action) {
224 if (p[0] == '%' && p[1]) {
227 /* CLAWS: filtering action is a mutually exclusive
228 * action. we can enable others if needed later. we
229 * add ACTION_SINGLE | ACTION_MULTIPLE so it will
230 * only be executed from the main window toolbar */
231 if (p[2] == 's') /* source messages */
232 action_type = ACTION_FILTERING_ACTION
235 in_filtering_action = TRUE;
238 action_type |= ACTION_SINGLE;
241 action_type |= ACTION_MULTIPLE;
244 action_type |= ACTION_SINGLE;
247 action_type |= ACTION_SELECTION_STR;
250 action_type |= ACTION_USER_STR;
253 action_type |= ACTION_USER_HIDDEN_STR;
259 action_type = ACTION_ERROR;
263 } else if (p[0] == '|') {
265 action_type |= ACTION_PIPE_OUT;
266 } else if (p[0] == '>') {
268 action_type |= ACTION_INSERT;
269 } else if (p[0] == '&') {
271 action_type |= ACTION_ASYNC;
272 } else if (p[0] == '}') {
273 in_filtering_action = FALSE;
282 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
283 GSList *msg_list, MimeInfo *partinfo,
284 const gchar *user_str,
285 const gchar *user_hidden_str,
286 const gchar *sel_str)
294 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
297 cmd = g_string_sized_new(strlen(action));
300 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
301 if (p[0] == '%' && p[1]) {
304 if (!parse_append_filename(cmd, msginfo)) {
305 g_string_free(cmd, TRUE);
311 for (cur = msg_list; cur != NULL;
313 MsgInfo *msg = (MsgInfo *)cur->data;
315 if (!parse_append_filename(cmd, msg)) {
316 g_string_free(cmd, TRUE);
320 g_string_append_c(cmd, ' ');
325 if (!parse_append_msgpart(cmd, msginfo,
327 g_string_free(cmd, TRUE);
334 g_string_append(cmd, sel_str);
339 g_string_append(cmd, user_str);
344 g_string_append(cmd, user_hidden_str);
348 g_string_append_c(cmd, p[1]);
352 g_string_append_c(cmd, p[0]);
353 g_string_append_c(cmd, p[1]);
357 g_string_append_c(cmd, p[0]);
362 g_string_free(cmd, TRUE);
367 g_string_free(cmd, FALSE);
371 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
375 cm_return_val_if_fail(msginfo, FALSE);
377 filename = procmsg_get_message_file(msginfo);
380 alertpanel_error(_("Could not get message file %d"),
385 g_string_append(cmd, "\"");
387 gchar *p = filename, *q;
388 gchar escape_ch[] = "\\ ";
389 while ((q = strpbrk(p, "$\"`\\~")) != NULL) {
392 g_string_append(cmd, p);
393 g_string_append(cmd, escape_ch);
397 g_string_append(cmd, p);
399 g_string_append(cmd, filename);
401 g_string_append(cmd, "\"");
407 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
410 gboolean single_part = FALSE;
412 gchar *part_filename;
416 partinfo = procmime_scan_message(msginfo);
418 alertpanel_error(_("Could not get message part."));
425 filename = procmsg_get_message_file_path(msginfo);
426 part_filename = procmime_get_tmp_file_name(partinfo);
428 ret = procmime_get_part(part_filename, partinfo);
431 procmime_mimeinfo_free_all(&partinfo);
435 alertpanel_error(_("Can't get part of multipart message: %s"), g_strerror(-ret));
436 g_free(part_filename);
440 g_string_append(cmd, part_filename);
442 g_free(part_filename);
447 void actions_execute(gpointer data,
452 if (source == TOOLBAR_MAIN)
453 mainwin_actions_execute((MainWindow*)data, action_nb, widget);
454 else if (source == TOOLBAR_COMPOSE)
455 compose_actions_execute((Compose*)data, action_nb, widget);
456 else if (source == TOOLBAR_MSGVIEW)
457 msgview_actions_execute((MessageView*)data, action_nb, widget);
460 void action_update_mainwin_menu(GtkUIManager *ui_manager,
464 action_update_menu(ui_manager, "<MainwinActions>", branch_path,
465 mainwin_actions_execute_cb, mainwin);
468 void action_update_msgview_menu(GtkUIManager *ui_manager,
470 MessageView *msgview)
472 action_update_menu(ui_manager, "<MsgviewActions>", branch_path,
473 msgview_actions_execute_cb, msgview);
476 void action_update_compose_menu(GtkUIManager *ui_manager,
480 action_update_menu(ui_manager, "<ComposeActions>", branch_path,
481 compose_actions_execute_cb, compose);
484 static GtkWidget *find_item_in_menu(GtkWidget *menu, gchar *name)
486 GList *children = gtk_container_get_children(GTK_CONTAINER(GTK_MENU_SHELL(menu)));
487 GList *amenu = children;
488 const gchar *existing_name;
490 GtkWidget *item = GTK_WIDGET(amenu->data);
491 if ((existing_name = g_object_get_data(G_OBJECT(item), "s_name")) != NULL &&
492 !g_strcmp0(name, existing_name))
494 g_list_free(children);
500 g_list_free(children);
505 static GtkWidget *create_submenus(GtkWidget *menu, const gchar *action)
507 gchar *submenu = g_strdup(action);
508 GtkWidget *new_menu = NULL;
510 if (strchr(submenu, '/')) {
511 const gchar *end = (strchr(submenu, '/')+1);
512 GtkWidget *menu_item = NULL;
514 *strchr(submenu, '/') = '\0';
515 if ((menu_item = find_item_in_menu(menu, submenu)) == NULL) {
516 menu_item = gtk_menu_item_new_with_mnemonic(submenu);
517 g_object_set_data_full(G_OBJECT(menu_item), "s_name", g_strdup(submenu), g_free);
518 gtk_widget_show(menu_item);
519 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);
520 new_menu = gtk_menu_new();
521 gtk_widget_show(new_menu);
522 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), new_menu);
524 new_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(menu_item));
526 new_menu = create_submenus(new_menu, end);
530 return new_menu ? new_menu : menu;
533 static void action_update_menu(GtkUIManager *ui_manager,
534 const gchar *accel_group,
536 gpointer callback, gpointer data)
539 gchar *action, *action_p;
540 int callback_action = 0;
541 GtkWidget *menu = gtk_menu_new();
544 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
545 GtkWidget *cur_menu = menu;
546 const gchar *action_name = NULL;
547 action = g_strdup((gchar *)cur->data);
548 action_p = strstr(action, ": ");
549 if (action_p && action_p[2] &&
550 (action_get_type(&action_p[2]) != ACTION_ERROR) &&
551 (action[0] != '/')) {
552 gchar *accel_path = NULL;
555 if (strchr(action, '/')) {
556 cur_menu = create_submenus(cur_menu, action);
557 action_name = strrchr(action, '/')+1;
559 action_name = action;
561 gtk_menu_set_accel_group (GTK_MENU (cur_menu),
562 gtk_ui_manager_get_accel_group(ui_manager));
563 item = gtk_menu_item_new_with_label(action_name);
564 gtk_menu_shell_append(GTK_MENU_SHELL(cur_menu), item);
565 g_signal_connect(G_OBJECT(item), "activate",
566 G_CALLBACK(callback), data);
567 g_object_set_data(G_OBJECT(item), "action_num", GINT_TO_POINTER(callback_action));
568 gtk_widget_show(item);
569 accel_path = g_strconcat(accel_group,branch_path, "/", action, NULL);
570 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(item), accel_path);
578 gtk_widget_show(menu);
579 gtk_menu_item_set_submenu(GTK_MENU_ITEM(gtk_ui_manager_get_widget(ui_manager, branch_path)), menu);
582 static void compose_actions_execute_cb(GtkWidget *widget, gpointer data)
584 Compose *compose = (Compose *)data;
585 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
586 compose_actions_execute(compose, action_nb, NULL);
589 static void compose_actions_execute(Compose *compose, guint action_nb, GtkWidget *widget)
592 ActionType action_type;
594 cm_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
596 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
597 cm_return_if_fail(buf != NULL);
598 action = strstr(buf, ": ");
599 cm_return_if_fail(action != NULL);
601 /* Point to the beginning of the command-line */
604 action_type = action_get_type(action);
605 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
607 (_("The selected action cannot be used in the compose window\n"
608 "because it contains %%f, %%F, %%as or %%p."));
612 execute_actions(action, NULL, compose->text, 0, NULL,
613 compose_action_cb, compose);
616 static void mainwin_actions_execute_cb(GtkWidget *widget, gpointer data)
618 MainWindow *mainwin = (MainWindow *)data;
619 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
620 mainwin_actions_execute(mainwin, action_nb, NULL);
623 static void mainwin_actions_execute(MainWindow *mainwin, guint action_nb,
628 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
629 message_actions_execute(mainwin->messageview, action_nb, msg_list);
630 summary_select_by_msg_list(mainwin->summaryview, msg_list);
631 g_slist_free(msg_list);
634 static void msgview_actions_execute_cb(GtkWidget *widget, gpointer data)
636 MessageView *msgview = (MessageView *)data;
637 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
638 msgview_actions_execute(msgview, action_nb, NULL);
641 static void msgview_actions_execute(MessageView *msgview, guint action_nb,
644 GSList *msg_list = NULL;
646 if (msgview->msginfo)
647 msg_list = g_slist_append(msg_list, msgview->msginfo);
648 message_actions_execute(msgview, action_nb, msg_list);
649 g_slist_free(msg_list);
652 static void message_actions_execute(MessageView *msgview, guint action_nb,
659 GtkWidget *text = NULL;
661 ActionType action_type;
663 cm_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
665 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
667 cm_return_if_fail(buf);
668 cm_return_if_fail((action = strstr(buf, ": ")));
670 /* Point to the beginning of the command-line */
673 textview = messageview_get_current_textview(msgview);
675 text = textview->text;
676 body_pos = textview->body_pos;
678 partinfo = messageview_get_selected_mime_part(msgview);
680 /* this command will alter the message text */
681 action_type = action_get_type(action);
682 if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
683 msgview->filtered = TRUE;
685 if (action_type & ACTION_FILTERING_ACTION)
686 /* CLAWS: most of the above code is not necessary for applying
688 execute_filtering_actions(action, msg_list);
690 execute_actions(action, msg_list, text, body_pos, partinfo,
694 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
696 GSList *action_list, *p;
697 const gchar *sbegin, *send;
698 gchar *action_string;
699 SummaryView *summaryview = NULL;
700 MainWindow *mainwin = NULL;
702 if (mainwindow_get_mainwindow()) {
703 summaryview = mainwindow_get_mainwindow()->summaryview;
704 mainwin = mainwindow_get_mainwindow();
707 if (NULL == (sbegin = g_strstr_len(action, -1, "%as{")))
709 sbegin += sizeof "%as{" - 1;
710 if (NULL == (send = strrchr(sbegin, '}')))
712 action_string = g_strndup(sbegin, send - sbegin);
714 action_list = matcher_parser_get_action_list(action_string);
715 if (action_list == NULL) {
716 gchar *tmp = g_strdup(action_string);
720 alertpanel_error(_("There is no filtering action set"));
722 alertpanel_error(_("Invalid filtering action(s):\n%s"), tmp);
723 g_free(action_string);
727 g_free(action_string);
729 /* apply actions on each message info */
730 for (p = msglist; p && p->data; p = g_slist_next(p)) {
731 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
735 summary_lock(summaryview);
736 main_window_cursor_wait(mainwin);
737 summary_freeze(summaryview);
738 folder_item_update_freeze();
741 filtering_move_and_copy_msgs(msglist);
744 folder_item_update_thaw();
745 summary_thaw(summaryview);
746 main_window_cursor_normal(mainwin);
747 summary_unlock(summaryview);
748 summary_show(summaryview, summaryview->folder_item);
750 for (p = action_list; p; p = g_slist_next(p))
751 if (p->data) filteringaction_free(p->data);
752 g_slist_free(action_list);
756 static gboolean execute_actions(gchar *action, GSList *msg_list,
758 gint body_pos, MimeInfo *partinfo,
759 void (*callback)(void *data), void *data)
761 GSList *children_list = NULL, *cur = NULL;
765 ChildInfo *child_info;
766 ActionType action_type;
769 gchar *sel_str = NULL;
770 gchar *msg_str = NULL;
771 gchar *user_str = NULL;
772 gchar *user_hidden_str = NULL;
773 GtkTextIter start_iter, end_iter;
774 gboolean is_selection = FALSE;
776 cm_return_val_if_fail(action && *action, FALSE);
778 action_type = action_get_type(action);
780 if (action_type == ACTION_ERROR)
781 return FALSE; /* ERR: syntax error */
783 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
784 return FALSE; /* ERR: file command without selection */
786 msg_list_len = g_slist_length(msg_list);
788 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
789 if (msg_list_len > 1)
790 return FALSE; /* ERR: pipe + multiple selection */
792 return FALSE; /* ERR: pipe and no displayed text */
795 if (action_type & ACTION_SELECTION_STR) {
797 return FALSE; /* ERR: selection string but no text */
801 GtkTextBuffer *textbuf;
803 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
804 is_selection = gtk_text_buffer_get_selection_bounds
805 (textbuf, &start_iter, &end_iter);
807 gtk_text_buffer_get_iter_at_offset
808 (textbuf, &start_iter, body_pos);
809 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
811 msg_str = gtk_text_buffer_get_text
812 (textbuf, &start_iter, &end_iter, FALSE);
814 sel_str = g_strdup(msg_str);
817 if (action_type & ACTION_USER_STR) {
818 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
825 if (action_type & ACTION_USER_HIDDEN_STR) {
826 if (!(user_hidden_str =
827 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
835 if (text && (action_type & ACTION_PIPE_OUT)) {
836 GtkTextBuffer *textbuf;
837 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
838 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
841 children = g_new0(Children, 1);
844 children->action = g_strdup(action);
845 children->action_type = action_type;
846 children->msg_text = text;
847 children->is_selection = is_selection;
849 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
850 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
851 children->open_in = 1;
853 /* Pre-fetch bodies, makes it easier on IMAP (see bug #3011) */
854 for (cur = msg_list; cur; cur = cur->next) {
856 msginfo = (MsgInfo *)cur->data;
858 dummy = procmsg_get_message_file((MsgInfo *)cur->data);
865 if (is_ok && (action_type & ACTION_SINGLE)) {
866 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
867 msginfo = (MsgInfo *)cur->data;
869 is_ok = FALSE; /* ERR: msginfo missing */
872 cmd = parse_action_cmd(action, msginfo, msg_list,
874 user_hidden_str, sel_str);
876 debug_print("Action command error\n");
877 is_ok = FALSE; /* ERR: incorrect command */
880 if ((child_info = fork_child(cmd, msg_str, children))) {
881 /* Pass msginfo to catch_status () */
882 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
883 child_info->msginfo_list =
884 g_slist_append (NULL, msginfo);
885 children_list = g_slist_append(children_list,
892 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
893 user_str, user_hidden_str, sel_str);
895 if ((child_info = fork_child(cmd, msg_str, children))) {
896 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
897 child_info->msginfo_list =
898 g_slist_copy (msg_list);
899 children_list = g_slist_append(children_list,
905 is_ok = FALSE; /* ERR: incorrect command */
911 g_free(user_hidden_str);
913 if (!children_list) {
914 /* If not waiting for children, return */
915 free_children(children);
919 children->list = children_list;
920 children->initial_nb = children->nb;
922 for (cur = children_list; cur; cur = cur->next) {
923 child_info = (ChildInfo *) cur->data;
924 child_info->callback = callback;
925 child_info->data = data;
926 child_info->tag_status =
927 g_child_watch_add(child_info->pid, catch_status, child_info);
930 create_io_dialog(children);
935 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
938 gint chld_in, chld_out, chld_err;
939 gchar **argv, *ret_str, *trim_cmd;
941 ChildInfo *child_info;
943 gssize by_written = 0, by_read = 0;
944 gboolean result = FALSE;
945 GError *error = NULL;
947 follow_child = !(children->action_type & ACTION_ASYNC);
949 chld_in = chld_out = chld_err = -1;
951 ret_str = g_locale_from_utf8(cmd, strlen(cmd),
952 &by_read, &by_written,
954 if (!ret_str || !by_written)
955 ret_str = g_strdup(cmd);
959 while (g_ascii_isspace(trim_cmd[0]))
963 argv = g_new0(gchar *, 4);
964 argv[0] = g_strdup("/bin/sh");
965 argv[1] = g_strdup("-c");
966 argv[2] = g_strdup(trim_cmd);
969 argv = strsplit_with_quote(trim_cmd, " ", 0);
974 result = g_spawn_async_with_pipes(NULL, argv, NULL,
975 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
976 NULL, NULL, &pid, &chld_in, &chld_out,
979 result = g_spawn_async(NULL, argv, NULL,
980 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH, NULL, NULL,
984 debug_print("spawning %s: %d\n", cmd, result);
989 alertpanel_error(_("Could not fork to execute the following "
991 cmd, error ? error->message : _("Unknown error"));
997 if (!(children->action_type &
998 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
999 (void)close(chld_in);
1001 if (!follow_child) {
1002 g_spawn_close_pid(pid);
1005 child_info = g_new0(ChildInfo, 1);
1007 child_info->children = children;
1009 child_info->pid = pid;
1011 child_info->next_sig = SIGTERM;
1013 child_info->cmd = g_strdup(cmd);
1014 child_info->new_out = FALSE;
1015 child_info->output = g_string_new(NULL);
1016 child_info->chld_in =
1017 (children->action_type &
1018 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
1020 child_info->chld_out = chld_out;
1021 child_info->chld_err = chld_err;
1022 child_info->tag_status = -1;
1023 child_info->tag_in = -1;
1024 child_info->tag_out = claws_input_add(chld_out, G_IO_IN | G_IO_HUP | G_IO_ERR,
1025 catch_output, child_info, FALSE);
1026 child_info->tag_err = claws_input_add(chld_err, G_IO_IN | G_IO_HUP | G_IO_ERR,
1027 catch_output, child_info, FALSE);
1029 if (!(children->action_type &
1030 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
1033 if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
1035 ret_str = g_locale_from_utf8(msg_str, strlen(msg_str),
1036 &by_read, &by_written, NULL);
1037 if (ret_str && by_written) {
1038 r = write(chld_in, ret_str, strlen(ret_str));
1041 r = write(chld_in, msg_str, strlen(msg_str));
1042 if (!(children->action_type &
1043 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
1045 child_info->chld_in = -1; /* No more input */
1047 debug_print("piping to child process: %s (%d)\n", g_strerror(errno), errno);
1053 static void kill_children_cb(GtkWidget *widget, gpointer data)
1056 Children *children = (Children *) data;
1057 ChildInfo *child_info;
1059 for (cur = children->list; cur; cur = cur->next) {
1060 child_info = (ChildInfo *)(cur->data);
1062 debug_print("Killing child group HANDLE %p\n", child_info->pid);
1063 TerminateProcess(child_info->pid, 0);
1065 debug_print("Killing child group id %d\n", child_info->pid);
1066 if (child_info->pid && kill(child_info->pid, child_info->next_sig) < 0)
1068 child_info->next_sig = SIGKILL;
1073 static gint wait_for_children(Children *children)
1075 gboolean new_output;
1076 ChildInfo *child_info;
1079 cur = children->list;
1082 child_info = (ChildInfo *)cur->data;
1083 new_output |= child_info->new_out;
1087 children->output |= new_output;
1089 if (new_output || (children->dialog && (children->initial_nb != children->nb)))
1090 update_io_dialog(children);
1095 if (!children->dialog) {
1096 free_children(children);
1097 } else if (!children->output) {
1098 gtk_widget_destroy(children->dialog);
1099 children->dialog = NULL;
1105 static void send_input(GtkWidget *w, gpointer data)
1107 Children *children = (Children *) data;
1108 ChildInfo *child_info = (ChildInfo *) children->list->data;
1110 child_info->tag_in = claws_input_add(child_info->chld_in,
1111 G_IO_OUT | G_IO_ERR,
1112 catch_input, children, FALSE);
1115 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1117 hide_io_dialog_cb(w, data);
1121 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1124 Children *children = (Children *)data;
1126 if (!children->nb) {
1127 g_signal_handlers_disconnect_matched
1128 (G_OBJECT(children->dialog), G_SIGNAL_MATCH_DATA,
1129 0, 0, NULL, NULL, children);
1130 gtk_widget_destroy(children->dialog);
1131 free_children(children);
1135 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1138 if (event && (event->keyval == GDK_KEY_Escape ||
1139 event->keyval == GDK_KEY_Return ||
1140 event->keyval == GDK_KEY_KP_Enter))
1141 hide_io_dialog_cb(widget, data);
1145 static void childinfo_close_pipes(ChildInfo *child_info)
1147 /* stdout and stderr pipes are guaranteed to be removed by
1148 * their handler, but in case where we receive child exit notification
1149 * before grand-child's pipes closing signals, we check them and close
1152 if (child_info->tag_in > 0)
1153 g_source_remove(child_info->tag_in);
1154 if (child_info->tag_out > 0)
1155 g_source_remove(child_info->tag_out);
1156 if (child_info->tag_err > 0)
1157 g_source_remove(child_info->tag_err);
1159 if (child_info->chld_in >= 0)
1160 (void)close(child_info->chld_in);
1161 if (child_info->chld_out >= 0)
1162 (void)close(child_info->chld_out);
1163 if (child_info->chld_err >= 0)
1164 (void)close(child_info->chld_err);
1167 static void free_children(Children *children)
1169 ChildInfo *child_info;
1170 void (*callback)(void *data) = NULL;
1173 debug_print("Freeing children data %p\n", children);
1175 g_free(children->action);
1176 while (children->list != NULL) {
1177 child_info = (ChildInfo *)children->list->data;
1178 g_free(child_info->cmd);
1179 g_string_free(child_info->output, TRUE);
1180 children->list = g_slist_remove(children->list, child_info);
1181 callback = child_info->callback;
1182 data = child_info->data;
1192 static void update_io_dialog(Children *children)
1196 debug_print("Updating actions input/output dialog.\n");
1198 if (children->progress_bar) {
1201 /* use a more compact format */
1202 const gchar *format = "%s %d/%d";
1204 const gchar *format = "%s %d / %d";
1207 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar),
1208 (children->initial_nb == 0) ? 0 :
1209 (gdouble) (children->initial_nb - children->nb) /
1210 (gdouble) children->initial_nb);
1211 text = g_strdup_printf(format, _("Completed"),
1212 children->initial_nb - children->nb,
1213 children->initial_nb);
1214 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(children->progress_bar), text);
1218 if (!children->nb) {
1219 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1220 gtk_widget_set_sensitive(children->close_btn, TRUE);
1221 if (children->input_hbox)
1222 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1223 gtk_widget_grab_focus(children->close_btn);
1224 g_signal_connect(G_OBJECT(children->dialog),
1226 G_CALLBACK(io_dialog_key_pressed_cb),
1230 if (children->output) {
1231 GtkWidget *text = children->text;
1232 GtkTextBuffer *textbuf;
1233 GtkTextIter iter, start_iter, end_iter;
1235 ChildInfo *child_info;
1237 gtk_widget_show(children->scrolledwin);
1238 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
1239 gtk_text_buffer_get_bounds(textbuf, &start_iter, &end_iter);
1240 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
1241 gtk_text_buffer_get_start_iter(textbuf, &iter);
1243 for (cur = children->list; cur; cur = cur->next) {
1244 child_info = (ChildInfo *)cur->data;
1245 if (child_info->pid)
1246 caption = g_strdup_printf
1247 (_("--- Running: %s\n"),
1250 caption = g_strdup_printf
1251 (_("--- Ended: %s\n"),
1254 gtk_text_buffer_insert(textbuf, &iter, caption, -1);
1255 gtk_text_buffer_insert(textbuf, &iter,
1256 child_info->output->str, -1);
1258 child_info->new_out = FALSE;
1264 *\brief Save Gtk object size to prefs dataset
1266 static void actions_io_size_allocate_cb(GtkWidget *widget,
1267 GtkAllocation *allocation)
1269 cm_return_if_fail(allocation != NULL);
1271 prefs_common.actionsiodialog_width = allocation->width;
1272 prefs_common.actionsiodialog_height = allocation->height;
1275 static void create_io_dialog(Children *children)
1279 GtkWidget *entry = NULL;
1280 GtkWidget *input_hbox = NULL;
1281 GtkWidget *send_button;
1284 GtkWidget *scrolledwin;
1286 GtkWidget *progress_bar = NULL;
1287 GtkWidget *abort_button;
1288 GtkWidget *close_button;
1289 static GdkGeometry geometry;
1291 debug_print("Creating action IO dialog\n");
1293 dialog = gtk_dialog_new();
1294 gtk_container_set_border_width
1295 (GTK_CONTAINER(gtk_dialog_get_action_area(GTK_DIALOG(dialog))), 5);
1296 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1297 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1298 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1299 manage_window_set_transient(GTK_WINDOW(dialog));
1300 g_signal_connect(G_OBJECT(dialog), "delete_event",
1301 G_CALLBACK(delete_io_dialog_cb), children);
1302 g_signal_connect(G_OBJECT(dialog), "destroy",
1303 G_CALLBACK(hide_io_dialog_cb),
1305 g_signal_connect(G_OBJECT(dialog), "size_allocate",
1306 G_CALLBACK(actions_io_size_allocate_cb), NULL);
1308 vbox = gtk_vbox_new(FALSE, 8);
1309 gtk_container_add(GTK_CONTAINER(
1310 gtk_dialog_get_content_area(GTK_DIALOG(dialog))), vbox);
1311 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1312 gtk_widget_show(vbox);
1314 label = gtk_label_new(children->action);
1315 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1316 gtk_widget_show(label);
1318 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1319 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1320 GTK_POLICY_AUTOMATIC,
1321 GTK_POLICY_AUTOMATIC);
1322 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
1324 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1325 gtk_widget_hide(scrolledwin);
1327 text = gtk_text_view_new();
1329 if (prefs_common.textfont) {
1330 PangoFontDescription *font_desc;
1331 font_desc = pango_font_description_from_string
1332 (prefs_common.textfont);
1334 gtk_widget_modify_font(text, font_desc);
1335 pango_font_description_free(font_desc);
1339 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
1340 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
1341 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
1342 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
1343 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1344 gtk_widget_show(text);
1346 if (children->open_in) {
1347 input_hbox = gtk_hbox_new(FALSE, 8);
1348 gtk_widget_show(input_hbox);
1350 entry = gtk_entry_new();
1351 gtk_widget_set_size_request(entry, 320, -1);
1352 g_signal_connect(G_OBJECT(entry), "activate",
1353 G_CALLBACK(send_input), children);
1354 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1355 if (children->action_type & ACTION_USER_HIDDEN_IN) {
1356 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1358 gtk_widget_show(entry);
1360 send_button = gtk_button_new_from_stock(GTK_STOCK_EXECUTE);
1361 g_signal_connect(G_OBJECT(send_button), "clicked",
1362 G_CALLBACK(send_input), children);
1363 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1365 gtk_widget_show(send_button);
1367 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1368 gtk_widget_grab_focus(entry);
1371 if (children->initial_nb > 1) {
1374 /* use a more compact format */
1375 const gchar *format = "%s 0/%d\n";
1377 const gchar *format = "%s 0 / %d\n";
1380 progress_bar = gtk_progress_bar_new();
1381 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1382 GTK_PROGRESS_LEFT_TO_RIGHT);
1383 text = g_strdup_printf(format, _("Completed"),
1384 children->initial_nb);
1385 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar),
1388 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1389 gtk_widget_show(progress_bar);
1392 gtkut_stock_button_set_create(&hbox, &abort_button, GTK_STOCK_STOP,
1393 &close_button, GTK_STOCK_CLOSE, NULL, NULL);
1394 g_signal_connect(G_OBJECT(abort_button), "clicked",
1395 G_CALLBACK(kill_children_cb), children);
1396 g_signal_connect(G_OBJECT(close_button), "clicked",
1397 G_CALLBACK(hide_io_dialog_cb), children);
1398 gtk_widget_show(hbox);
1401 gtk_widget_set_sensitive(close_button, FALSE);
1403 gtk_container_add(GTK_CONTAINER(
1404 gtk_dialog_get_action_area(GTK_DIALOG(dialog))), hbox);
1406 if (!geometry.min_height) {
1407 geometry.min_width = 582;
1408 geometry.min_height = 310;
1411 gtk_window_set_geometry_hints(GTK_WINDOW(dialog), NULL, &geometry,
1413 gtk_widget_set_size_request(dialog, prefs_common.actionsiodialog_width,
1414 prefs_common.actionsiodialog_height);
1416 gtk_widget_show(dialog);
1418 children->dialog = dialog;
1419 children->scrolledwin = scrolledwin;
1420 children->text = text;
1421 children->input_hbox = children->open_in ? input_hbox : NULL;
1422 children->input_entry = children->open_in ? entry : NULL;
1423 children->progress_bar = progress_bar;
1424 children->abort_btn = abort_button;
1425 children->close_btn = close_button;
1428 static void catch_status(GPid pid, gint status, gpointer data)
1430 ChildInfo *child_info = (ChildInfo *)data;
1432 debug_print("Child returned %d\n", status);
1434 childinfo_close_pipes(child_info);
1435 g_spawn_close_pid(child_info->pid);
1436 child_info->pid = 0;
1438 if (child_info->children->action_type & (ACTION_SINGLE | ACTION_MULTIPLE)
1439 && child_info->msginfo_list) {
1440 /* Actions on message *files* might change size and
1441 * time stamp, and thus invalidate the cache */
1442 SummaryView *summaryview = NULL;
1444 MsgInfo *msginfo, *nmi; /* newmsginfo */
1446 gboolean modified_something = FALSE;
1447 FolderItem *last_item = NULL;
1448 if (mainwindow_get_mainwindow ())
1449 summaryview = mainwindow_get_mainwindow ()->summaryview;
1450 for (cur = child_info->msginfo_list; cur; cur = cur->next) {
1451 msginfo = (MsgInfo *)cur->data;
1452 if (!(msginfo && /* Stuff used valid? */
1453 msginfo->folder && msginfo->folder->cache))
1455 file = procmsg_get_message_file_path (msginfo);
1458 nmi = procheader_parse_file (file, msginfo->flags, TRUE, FALSE);
1460 continue; /* Deleted? */
1461 if (msginfo->mtime != nmi->mtime || msginfo->size != nmi->size) {
1462 nmi->folder = msginfo->folder;
1463 nmi->msgnum = msginfo->msgnum;
1464 msgcache_update_msg (msginfo->folder->cache, nmi);
1465 modified_something = TRUE;
1466 last_item = nmi->folder;
1468 procmsg_msginfo_free (&nmi);
1469 if (summaryview && summaryview->displayed &&
1470 summaryview->folder_item == msginfo->folder &&
1471 summary_get_msgnum(summaryview, summaryview->displayed) == msginfo->msgnum)
1472 summary_redisplay_msg(summaryview);
1475 if (modified_something && last_item &&
1476 summaryview && summaryview->folder_item == last_item) {
1477 summary_show (summaryview, summaryview->folder_item);
1479 g_slist_free (child_info->msginfo_list);
1480 child_info->msginfo_list = NULL;
1483 if (!child_info->pid)
1484 child_info->children->nb--;
1486 wait_for_children(child_info->children);
1489 static void catch_input(gpointer data, gint source, GIOCondition cond)
1491 Children *children = (Children *)data;
1492 ChildInfo *child_info = (ChildInfo *)children->list->data;
1493 gchar *input, *ret_str;
1494 gint c, count, len, r;
1495 gssize by_read = 0, by_written = 0;
1497 debug_print("Sending input to grand child.\n");
1498 if (!(cond & (G_IO_OUT | G_IO_ERR)))
1501 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1502 gtk_widget_grab_focus(children->abort_btn);
1504 g_source_remove(child_info->tag_in);
1505 child_info->tag_in = -1;
1507 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1509 ret_str = g_locale_from_utf8(input, strlen(input), &by_read,
1511 if (ret_str && by_written) {
1516 len = strlen(input);
1520 c = write(child_info->chld_in, input + count, len - count);
1523 } while (c >= 0 && count < len);
1526 r = write(child_info->chld_in, "\n", 2);
1530 r = close(child_info->chld_in);
1531 child_info->chld_in = -1;
1533 debug_print("closing child input fd: %s (%d)\n", g_strerror(errno), errno);
1534 child_info->chld_in = -1;
1535 debug_print("Input to grand child sent.\n");
1538 static void catch_output(gpointer data, gint source, GIOCondition cond)
1540 ChildInfo *child_info = (ChildInfo *)data;
1542 gchar buf[BUFFSIZE];
1544 debug_print("Catching grand child's output.\n");
1545 if (child_info->children->action_type &
1546 (ACTION_PIPE_OUT | ACTION_INSERT)
1547 && source == child_info->chld_out) {
1549 GTK_TEXT_VIEW(child_info->children->msg_text);
1550 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
1555 mark = gtk_text_buffer_get_insert(textbuf);
1556 gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1557 ins_pos = gtk_text_iter_get_offset(&iter);
1560 gsize bytes_read = 0, bytes_written = 0;
1563 c = read(source, buf, sizeof(buf) - 1);
1567 ret_str = g_locale_to_utf8
1568 (buf, c - 1, &bytes_read, &bytes_written, NULL);
1569 if (ret_str && bytes_written > 0) {
1570 gtk_text_buffer_insert
1571 (textbuf, &iter, ret_str,
1575 gtk_text_buffer_insert(textbuf, &iter, buf, c - 1);
1578 if (child_info->children->is_selection) {
1581 gtk_text_buffer_get_iter_at_offset
1582 (textbuf, &ins, ins_pos);
1583 gtk_text_buffer_select_range(textbuf, &ins, &iter);
1586 c = read(source, buf, sizeof(buf) - 1);
1588 gsize bytes_read = 0, bytes_written = 0;
1591 ret_str = g_locale_to_utf8
1592 (buf, c, &bytes_read, &bytes_written, NULL);
1593 if (ret_str && bytes_written > 0) {
1595 (child_info->output, ret_str,
1599 g_string_append_len(child_info->output, buf, c);
1601 child_info->new_out = TRUE;
1605 if (source == child_info->chld_out) {
1606 g_source_remove(child_info->tag_out);
1607 child_info->tag_out = -1;
1608 (void)close(child_info->chld_out);
1609 child_info->chld_out = -1;
1611 g_source_remove(child_info->tag_err);
1612 child_info->tag_err = -1;
1613 (void)close(child_info->chld_err);
1614 child_info->chld_err = -1;
1618 wait_for_children(child_info->children);
1621 static gchar *get_user_string(const gchar *action, ActionType type)
1624 gchar *user_str = NULL;
1627 case ACTION_USER_HIDDEN_STR:
1628 message = g_strdup_printf
1629 (_("Enter the argument for the following action:\n"
1630 "('%%h' will be replaced with the argument)\n"
1633 user_str = input_dialog_with_invisible
1634 (_("Action's hidden user argument"), message, NULL);
1636 case ACTION_USER_STR:
1637 message = g_strdup_printf
1638 (_("Enter the argument for the following action:\n"
1639 "('%%u' will be replaced with the argument)\n"
1642 user_str = input_dialog
1643 (_("Action's user argument"), message, NULL);
1646 g_warning("Unsupported action type %d", type);