2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 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/>.
22 #include "claws-features.h"
28 #include <glib/gi18n.h>
30 #include <gdk/gdkkeysyms.h>
31 #ifdef GDK_WINDOWING_X11
32 # include <gdk/gdkx.h>
33 #endif /* GDK_WINDOWING_X11 */
38 #include <sys/types.h>
40 # include <sys/wait.h>
47 #include "manage_window.h"
48 #include "mainwindow.h"
49 #include "prefs_common.h"
50 #include "alertpanel.h"
51 #include "inputdialog.h"
57 #include "matcher_parser.h" /* CLAWS */
58 #include "filtering.h"
59 #include "procheader.h"
65 typedef struct _Children Children;
66 typedef struct _ChildInfo ChildInfo;
67 typedef struct _UserStringDialog UserStringDialog;
73 GtkWidget *input_entry;
74 GtkWidget *input_hbox;
75 GtkWidget *progress_bar;
78 GtkWidget *scrolledwin;
81 ActionType action_type;
90 gboolean is_selection;
109 void (*callback)(void *data);
112 GSList *msginfo_list;
115 static void action_update_menu (GtkUIManager *ui_manager,
116 const gchar *accel_group,
120 static void compose_actions_execute_cb (GtkWidget *widget,
122 static void compose_actions_execute (Compose *compose,
126 static void mainwin_actions_execute_cb (GtkWidget *widget,
128 static void mainwin_actions_execute (MainWindow *mainwin,
132 static void msgview_actions_execute_cb (GtkWidget *widget,
134 static void msgview_actions_execute (MessageView *msgview,
138 static void message_actions_execute (MessageView *msgview,
142 static gboolean execute_filtering_actions(gchar *action,
145 static gboolean execute_actions (gchar *action,
150 void (*callback)(void *data),
153 static gchar *parse_action_cmd (gchar *action,
157 const gchar *user_str,
158 const gchar *user_hidden_str,
159 const gchar *sel_str);
160 static gboolean parse_append_filename (GString *cmd,
163 static gboolean parse_append_msgpart (GString *cmd,
167 static ChildInfo *fork_child (gchar *cmd,
168 const gchar *msg_str,
171 static gint wait_for_children (Children *children);
173 static void free_children (Children *children);
175 static void childinfo_close_pipes (ChildInfo *child_info);
177 static void create_io_dialog (Children *children);
178 static void update_io_dialog (Children *children);
180 static void hide_io_dialog_cb (GtkWidget *widget,
182 static gint io_dialog_key_pressed_cb (GtkWidget *widget,
186 static void catch_output (gpointer data,
189 static void catch_input (gpointer data,
192 static void catch_status (GPid pid, gint status, gpointer data);
194 static gchar *get_user_string (const gchar *action,
198 ActionType action_get_type(const gchar *action_str)
201 gboolean in_filtering_action = FALSE;
202 ActionType action_type = ACTION_NONE;
204 cm_return_val_if_fail(action_str, ACTION_ERROR);
205 cm_return_val_if_fail(*action_str, ACTION_ERROR);
210 action_type |= ACTION_PIPE_IN;
212 } else if (p[0] == '>') {
213 action_type |= ACTION_USER_IN;
215 } else if (p[0] == '*') {
216 action_type |= ACTION_USER_HIDDEN_IN;
223 while (*p && action_type != ACTION_ERROR) {
224 if (!in_filtering_action) {
225 if (p[0] == '%' && p[1]) {
228 /* CLAWS: filtering action is a mutually exclusive
229 * action. we can enable others if needed later. we
230 * add ACTION_SINGLE | ACTION_MULTIPLE so it will
231 * only be executed from the main window toolbar */
232 if (p[2] == 's') /* source messages */
233 action_type = ACTION_FILTERING_ACTION
236 in_filtering_action = TRUE;
239 action_type |= ACTION_SINGLE;
242 action_type |= ACTION_MULTIPLE;
245 action_type |= ACTION_SINGLE;
248 action_type |= ACTION_SELECTION_STR;
251 action_type |= ACTION_USER_STR;
254 action_type |= ACTION_USER_HIDDEN_STR;
260 action_type = ACTION_ERROR;
264 } else if (p[0] == '|') {
266 action_type |= ACTION_PIPE_OUT;
267 } else if (p[0] == '>') {
269 action_type |= ACTION_INSERT;
270 } else if (p[0] == '&') {
272 action_type |= ACTION_ASYNC;
273 } else if (p[0] == '}') {
274 in_filtering_action = FALSE;
283 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
284 GSList *msg_list, MimeInfo *partinfo,
285 const gchar *user_str,
286 const gchar *user_hidden_str,
287 const gchar *sel_str)
295 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
298 cmd = g_string_sized_new(strlen(action));
301 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
302 if (p[0] == '%' && p[1]) {
305 if (!parse_append_filename(cmd, msginfo)) {
306 g_string_free(cmd, TRUE);
312 for (cur = msg_list; cur != NULL;
314 MsgInfo *msg = (MsgInfo *)cur->data;
316 if (!parse_append_filename(cmd, msg)) {
317 g_string_free(cmd, TRUE);
321 g_string_append_c(cmd, ' ');
326 if (!parse_append_msgpart(cmd, msginfo,
328 g_string_free(cmd, TRUE);
335 g_string_append(cmd, sel_str);
340 g_string_append(cmd, user_str);
345 g_string_append(cmd, user_hidden_str);
349 g_string_append_c(cmd, p[1]);
353 g_string_append_c(cmd, p[0]);
354 g_string_append_c(cmd, p[1]);
358 g_string_append_c(cmd, p[0]);
363 g_string_free(cmd, TRUE);
368 g_string_free(cmd, FALSE);
372 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
376 gchar escape_ch[] = "\\ ";
378 cm_return_val_if_fail(msginfo, FALSE);
380 filename = procmsg_get_message_file(msginfo);
383 alertpanel_error(_("Could not get message file %d"),
389 g_string_append(cmd, "\"");
391 while ((q = strpbrk(p, "$\"`\\~")) != NULL) {
394 g_string_append(cmd, p);
395 g_string_append(cmd, escape_ch);
399 g_string_append(cmd, p);
401 g_string_append(cmd, filename);
403 g_string_append(cmd, "\"");
409 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
412 gboolean single_part = FALSE;
414 gchar *part_filename;
418 partinfo = procmime_scan_message(msginfo);
420 alertpanel_error(_("Could not get message part."));
427 filename = procmsg_get_message_file_path(msginfo);
428 part_filename = procmime_get_tmp_file_name(partinfo);
430 ret = procmime_get_part(part_filename, partinfo);
433 procmime_mimeinfo_free_all(partinfo);
437 alertpanel_error(_("Can't get part of multipart message: %s"), strerror(-ret));
438 g_free(part_filename);
442 g_string_append(cmd, part_filename);
444 g_free(part_filename);
449 void actions_execute(gpointer data,
454 if (source == TOOLBAR_MAIN)
455 mainwin_actions_execute((MainWindow*)data, action_nb, widget);
456 else if (source == TOOLBAR_COMPOSE)
457 compose_actions_execute((Compose*)data, action_nb, widget);
458 else if (source == TOOLBAR_MSGVIEW)
459 msgview_actions_execute((MessageView*)data, action_nb, widget);
462 void action_update_mainwin_menu(GtkUIManager *ui_manager,
466 action_update_menu(ui_manager, "<MainwinActions>", branch_path,
467 mainwin_actions_execute_cb, mainwin);
470 void action_update_msgview_menu(GtkUIManager *ui_manager,
472 MessageView *msgview)
474 action_update_menu(ui_manager, "<MsgviewActions>", branch_path,
475 msgview_actions_execute_cb, msgview);
478 void action_update_compose_menu(GtkUIManager *ui_manager,
482 action_update_menu(ui_manager, "<ComposeActions>", branch_path,
483 compose_actions_execute_cb, compose);
486 static GtkWidget *find_item_in_menu(GtkWidget *menu, gchar *name)
488 GList *children = gtk_container_get_children(GTK_CONTAINER(GTK_MENU_SHELL(menu)));
489 GList *amenu = children;
490 const gchar *existing_name;
492 GtkWidget *item = GTK_WIDGET(amenu->data);
493 if ((existing_name = g_object_get_data(G_OBJECT(item), "s_name")) != NULL &&
494 !strcmp2(name, existing_name))
496 g_list_free(children);
502 g_list_free(children);
507 static GtkWidget *create_submenus(GtkWidget *menu, const gchar *action)
509 gchar *submenu = g_strdup(action);
510 GtkWidget *new_menu = NULL;
512 if (strchr(submenu, '/')) {
513 const gchar *end = (strchr(submenu, '/')+1);
514 GtkWidget *menu_item = NULL;
516 *strchr(submenu, '/') = '\0';
517 if ((menu_item = find_item_in_menu(menu, submenu)) == NULL) {
518 menu_item = gtk_menu_item_new_with_mnemonic(submenu);
519 g_object_set_data_full(G_OBJECT(menu_item), "s_name", g_strdup(submenu), g_free);
520 gtk_widget_show(menu_item);
521 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);
522 new_menu = gtk_menu_new();
523 gtk_widget_show(new_menu);
524 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), new_menu);
526 new_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(menu_item));
528 new_menu = create_submenus(new_menu, end);
532 return new_menu ? new_menu : menu;
535 static void action_update_menu(GtkUIManager *ui_manager,
536 const gchar *accel_group,
538 gpointer callback, gpointer data)
541 gchar *action, *action_p;
542 int callback_action = 0;
543 GtkWidget *menu = gtk_menu_new();
546 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
547 GtkWidget *cur_menu = menu;
548 const gchar *action_name = NULL;
549 action = g_strdup((gchar *)cur->data);
550 action_p = strstr(action, ": ");
551 if (action_p && action_p[2] &&
552 (action_get_type(&action_p[2]) != ACTION_ERROR) &&
553 (action[0] != '/')) {
554 gchar *accel_path = NULL;
557 if (strchr(action, '/')) {
558 cur_menu = create_submenus(cur_menu, action);
559 action_name = strrchr(action, '/')+1;
561 action_name = action;
563 gtk_menu_set_accel_group (GTK_MENU (cur_menu),
564 gtk_ui_manager_get_accel_group(ui_manager));
565 item = gtk_menu_item_new_with_label(action_name);
566 gtk_menu_shell_append(GTK_MENU_SHELL(cur_menu), item);
567 g_signal_connect(G_OBJECT(item), "activate",
568 G_CALLBACK(callback), data);
569 g_object_set_data(G_OBJECT(item), "action_num", GINT_TO_POINTER(callback_action));
570 gtk_widget_show(item);
571 accel_path = g_strconcat(accel_group,branch_path, "/", action, NULL);
572 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(item), accel_path);
580 gtk_widget_show(menu);
581 gtk_menu_item_set_submenu(GTK_MENU_ITEM(gtk_ui_manager_get_widget(ui_manager, branch_path)), menu);
584 static void compose_actions_execute_cb(GtkWidget *widget, gpointer data)
586 Compose *compose = (Compose *)data;
587 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
588 compose_actions_execute(compose, action_nb, NULL);
591 static void compose_actions_execute(Compose *compose, guint action_nb, GtkWidget *widget)
594 ActionType action_type;
596 cm_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
598 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
599 cm_return_if_fail(buf != NULL);
600 action = strstr(buf, ": ");
601 cm_return_if_fail(action != NULL);
603 /* Point to the beginning of the command-line */
606 action_type = action_get_type(action);
607 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
609 (_("The selected action cannot be used in the compose window\n"
610 "because it contains %%f, %%F, %%as or %%p."));
614 execute_actions(action, NULL, compose->text, 0, NULL,
615 compose_action_cb, compose);
618 static void mainwin_actions_execute_cb(GtkWidget *widget, gpointer data)
620 MainWindow *mainwin = (MainWindow *)data;
621 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
622 mainwin_actions_execute(mainwin, action_nb, NULL);
625 static void mainwin_actions_execute(MainWindow *mainwin, guint action_nb,
630 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
631 message_actions_execute(mainwin->messageview, action_nb, msg_list);
632 g_slist_free(msg_list);
635 static void msgview_actions_execute_cb(GtkWidget *widget, gpointer data)
637 MessageView *msgview = (MessageView *)data;
638 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
639 msgview_actions_execute(msgview, action_nb, NULL);
642 static void msgview_actions_execute(MessageView *msgview, guint action_nb,
645 GSList *msg_list = NULL;
647 if (msgview->msginfo)
648 msg_list = g_slist_append(msg_list, msgview->msginfo);
649 message_actions_execute(msgview, action_nb, msg_list);
650 g_slist_free(msg_list);
653 static void message_actions_execute(MessageView *msgview, guint action_nb,
660 GtkWidget *text = NULL;
662 ActionType action_type;
664 cm_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
666 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
668 cm_return_if_fail(buf);
669 cm_return_if_fail((action = strstr(buf, ": ")));
671 /* Point to the beginning of the command-line */
674 textview = messageview_get_current_textview(msgview);
676 text = textview->text;
677 body_pos = textview->body_pos;
679 partinfo = messageview_get_selected_mime_part(msgview);
681 /* this command will alter the message text */
682 action_type = action_get_type(action);
683 if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
684 msgview->filtered = TRUE;
686 if (action_type & ACTION_FILTERING_ACTION)
687 /* CLAWS: most of the above code is not necessary for applying
689 execute_filtering_actions(action, msg_list);
691 execute_actions(action, msg_list, text, body_pos, partinfo,
695 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
697 GSList *action_list, *p;
698 const gchar *sbegin, *send;
699 gchar *action_string;
700 SummaryView *summaryview = NULL;
701 MainWindow *mainwin = NULL;
703 if (mainwindow_get_mainwindow()) {
704 summaryview = mainwindow_get_mainwindow()->summaryview;
705 mainwin = mainwindow_get_mainwindow();
708 if (NULL == (sbegin = strstr2(action, "%as{")))
710 sbegin += sizeof "%as{" - 1;
711 if (NULL == (send = strrchr(sbegin, '}')))
713 action_string = g_strndup(sbegin, send - sbegin);
715 action_list = matcher_parser_get_action_list(action_string);
716 if (action_list == NULL) {
717 gchar *tmp = g_strdup(action_string);
721 alertpanel_error(_("There is no filtering action set"));
723 alertpanel_error(_("Invalid filtering action(s):\n%s"), tmp);
724 g_free(action_string);
728 g_free(action_string);
730 /* apply actions on each message info */
731 for (p = msglist; p && p->data; p = g_slist_next(p)) {
732 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
736 summary_lock(summaryview);
737 main_window_cursor_wait(mainwin);
738 summary_freeze(summaryview);
739 folder_item_update_freeze();
742 filtering_move_and_copy_msgs(msglist);
745 folder_item_update_thaw();
746 summary_thaw(summaryview);
747 main_window_cursor_normal(mainwin);
748 summary_unlock(summaryview);
749 summary_show(summaryview, summaryview->folder_item);
751 for (p = action_list; p; p = g_slist_next(p))
752 if (p->data) filteringaction_free(p->data);
753 g_slist_free(action_list);
757 static gboolean execute_actions(gchar *action, GSList *msg_list,
759 gint body_pos, MimeInfo *partinfo,
760 void (*callback)(void *data), void *data)
762 GSList *children_list = NULL;
766 ChildInfo *child_info;
767 ActionType action_type;
770 gchar *sel_str = NULL;
771 gchar *msg_str = NULL;
772 gchar *user_str = NULL;
773 gchar *user_hidden_str = NULL;
774 GtkTextIter start_iter, end_iter;
775 gboolean is_selection = FALSE;
777 cm_return_val_if_fail(action && *action, FALSE);
779 action_type = action_get_type(action);
781 if (action_type == ACTION_ERROR)
782 return FALSE; /* ERR: syntax error */
784 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
785 return FALSE; /* ERR: file command without selection */
787 msg_list_len = g_slist_length(msg_list);
789 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
790 if (msg_list_len > 1)
791 return FALSE; /* ERR: pipe + multiple selection */
793 return FALSE; /* ERR: pipe and no displayed text */
796 if (action_type & ACTION_SELECTION_STR) {
798 return FALSE; /* ERR: selection string but no text */
802 GtkTextBuffer *textbuf;
804 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
805 is_selection = gtk_text_buffer_get_selection_bounds
806 (textbuf, &start_iter, &end_iter);
808 gtk_text_buffer_get_iter_at_offset
809 (textbuf, &start_iter, body_pos);
810 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
812 msg_str = gtk_text_buffer_get_text
813 (textbuf, &start_iter, &end_iter, FALSE);
815 sel_str = g_strdup(msg_str);
818 if (action_type & ACTION_USER_STR) {
819 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
826 if (action_type & ACTION_USER_HIDDEN_STR) {
827 if (!(user_hidden_str =
828 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
836 if (text && (action_type & ACTION_PIPE_OUT)) {
837 GtkTextBuffer *textbuf;
838 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
839 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
842 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 if (action_type & ACTION_SINGLE) {
856 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
857 msginfo = (MsgInfo *)cur->data;
859 is_ok = FALSE; /* ERR: msginfo missing */
862 cmd = parse_action_cmd(action, msginfo, msg_list,
864 user_hidden_str, sel_str);
866 debug_print("Action command error\n");
867 is_ok = FALSE; /* ERR: incorrect command */
870 if ((child_info = fork_child(cmd, msg_str, children))) {
871 /* Pass msginfo to catch_status () */
872 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
873 child_info->msginfo_list =
874 g_slist_append (NULL, msginfo);
875 children_list = g_slist_append(children_list,
881 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
882 user_str, user_hidden_str, sel_str);
884 if ((child_info = fork_child(cmd, msg_str, children))) {
885 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
886 child_info->msginfo_list =
887 g_slist_copy (msg_list);
888 children_list = g_slist_append(children_list,
893 is_ok = FALSE; /* ERR: incorrect command */
899 g_free(user_hidden_str);
901 if (!children_list) {
902 /* If not waiting for children, return */
903 free_children(children);
907 children->list = children_list;
908 children->nb = g_slist_length(children_list);
909 children->initial_nb = children->nb;
911 for (cur = children_list; cur; cur = cur->next) {
912 child_info = (ChildInfo *) cur->data;
913 child_info->callback = callback;
914 child_info->data = data;
915 child_info->tag_status =
916 g_child_watch_add(child_info->pid, catch_status, child_info);
919 create_io_dialog(children);
924 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
927 gint chld_in, chld_out, chld_err;
928 gchar **argv, *ret_str, *trim_cmd;
930 ChildInfo *child_info;
932 gssize by_written = 0, by_read = 0;
933 gboolean result = FALSE;
934 GError *error = NULL;
936 follow_child = !(children->action_type & ACTION_ASYNC);
938 chld_in = chld_out = chld_err = -1;
940 ret_str = g_locale_from_utf8(cmd, strlen(cmd),
941 &by_read, &by_written,
943 if (!ret_str || !by_written)
944 ret_str = g_strdup(cmd);
948 while (g_ascii_isspace(trim_cmd[0]))
952 argv = g_new0(gchar *, 4);
953 argv[0] = g_strdup("/bin/sh");
954 argv[1] = g_strdup("-c");
955 argv[2] = g_strdup(trim_cmd);
958 argv = strsplit_with_quote(trim_cmd, " ", 0);
963 result = g_spawn_async_with_pipes(NULL, argv, NULL,
964 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
965 NULL, NULL, &pid, &chld_in, &chld_out,
968 result = g_spawn_async(NULL, argv, NULL,
969 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH, NULL, NULL,
973 debug_print("spawning %s: %d\n", cmd, result);
978 alertpanel_error(_("Could not fork to execute the following "
980 cmd, error ? error->message : _("Unknown error"));
986 if (!(children->action_type &
987 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
988 (void)close(chld_in);
991 g_spawn_close_pid(pid);
994 child_info = g_new0(ChildInfo, 1);
996 child_info->children = children;
998 child_info->pid = pid;
1000 child_info->next_sig = SIGTERM;
1002 child_info->cmd = g_strdup(cmd);
1003 child_info->new_out = FALSE;
1004 child_info->output = g_string_new(NULL);
1005 child_info->chld_in =
1006 (children->action_type &
1007 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
1009 child_info->chld_out = chld_out;
1010 child_info->chld_err = chld_err;
1011 child_info->tag_status = -1;
1012 child_info->tag_in = -1;
1013 child_info->tag_out = claws_input_add(chld_out, G_IO_IN | G_IO_HUP | G_IO_ERR,
1014 catch_output, child_info, FALSE);
1015 child_info->tag_err = claws_input_add(chld_err, G_IO_IN | G_IO_HUP | G_IO_ERR,
1016 catch_output, child_info, FALSE);
1018 if (!(children->action_type &
1019 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
1022 if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
1024 ret_str = g_locale_from_utf8(msg_str, strlen(msg_str),
1025 &by_read, &by_written, NULL);
1026 if (ret_str && by_written) {
1027 r = write(chld_in, ret_str, strlen(ret_str));
1030 r = write(chld_in, msg_str, strlen(msg_str));
1031 if (!(children->action_type &
1032 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
1034 child_info->chld_in = -1; /* No more input */
1036 debug_print("%s(%d)", strerror(errno), errno);
1042 static void kill_children_cb(GtkWidget *widget, gpointer data)
1045 Children *children = (Children *) data;
1046 ChildInfo *child_info;
1048 for (cur = children->list; cur; cur = cur->next) {
1049 child_info = (ChildInfo *)(cur->data);
1050 debug_print("Killing child group id %d\n", child_info->pid);
1052 if (child_info->pid && kill(child_info->pid, child_info->next_sig) < 0)
1054 child_info->next_sig = SIGKILL;
1056 TerminateProcess(child_info->pid, 0);
1061 static gint wait_for_children(Children *children)
1063 gboolean new_output;
1064 ChildInfo *child_info;
1067 cur = children->list;
1070 child_info = (ChildInfo *)cur->data;
1071 new_output |= child_info->new_out;
1075 children->output |= new_output;
1077 if (new_output || (children->dialog && (children->initial_nb != children->nb)))
1078 update_io_dialog(children);
1083 if (!children->dialog) {
1084 free_children(children);
1085 } else if (!children->output) {
1086 gtk_widget_destroy(children->dialog);
1092 static void send_input(GtkWidget *w, gpointer data)
1094 Children *children = (Children *) data;
1095 ChildInfo *child_info = (ChildInfo *) children->list->data;
1097 child_info->tag_in = claws_input_add(child_info->chld_in,
1098 G_IO_OUT | G_IO_ERR,
1099 catch_input, children, FALSE);
1102 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1104 hide_io_dialog_cb(w, data);
1108 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1111 Children *children = (Children *)data;
1113 if (!children->nb) {
1114 g_signal_handlers_disconnect_matched
1115 (G_OBJECT(children->dialog), G_SIGNAL_MATCH_DATA,
1116 0, 0, NULL, NULL, children);
1117 gtk_widget_destroy(children->dialog);
1118 free_children(children);
1122 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1125 if (event && (event->keyval == GDK_KEY_Escape ||
1126 event->keyval == GDK_KEY_Return ||
1127 event->keyval == GDK_KEY_KP_Enter))
1128 hide_io_dialog_cb(widget, data);
1132 static void childinfo_close_pipes(ChildInfo *child_info)
1134 /* stdout and stderr pipes are guaranteed to be removed by
1135 * their handler, but in case where we receive child exit notification
1136 * before grand-child's pipes closing signals, we check them and close
1139 if (child_info->tag_in > 0)
1140 g_source_remove(child_info->tag_in);
1141 if (child_info->tag_out > 0)
1142 g_source_remove(child_info->tag_out);
1143 if (child_info->tag_err > 0)
1144 g_source_remove(child_info->tag_err);
1146 if (child_info->chld_in >= 0)
1147 (void)close(child_info->chld_in);
1148 if (child_info->chld_out >= 0)
1149 (void)close(child_info->chld_out);
1150 if (child_info->chld_err >= 0)
1151 (void)close(child_info->chld_err);
1154 static void free_children(Children *children)
1156 ChildInfo *child_info;
1157 void (*callback)(void *data) = NULL;
1160 debug_print("Freeing children data %p\n", children);
1162 g_free(children->action);
1163 while (children->list != NULL) {
1164 child_info = (ChildInfo *)children->list->data;
1165 g_free(child_info->cmd);
1166 g_string_free(child_info->output, TRUE);
1167 children->list = g_slist_remove(children->list, child_info);
1168 callback = child_info->callback;
1169 data = child_info->data;
1179 static void update_io_dialog(Children *children)
1183 debug_print("Updating actions input/output dialog.\n");
1185 if (children->progress_bar) {
1188 /* use a more compact format */
1189 const gchar *format = "%s %d/%d";
1191 const gchar *format = "%s %d / %d";
1194 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar),
1195 (children->initial_nb == 0) ? 0 :
1196 (gdouble) (children->initial_nb - children->nb) /
1197 (gdouble) children->initial_nb);
1198 text = g_strdup_printf(format, _("Completed"),
1199 children->initial_nb - children->nb,
1200 children->initial_nb);
1201 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(children->progress_bar), text);
1205 if (!children->nb) {
1206 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1207 gtk_widget_set_sensitive(children->close_btn, TRUE);
1208 if (children->input_hbox)
1209 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1210 gtk_widget_grab_focus(children->close_btn);
1211 g_signal_connect(G_OBJECT(children->dialog),
1213 G_CALLBACK(io_dialog_key_pressed_cb),
1217 if (children->output) {
1218 GtkWidget *text = children->text;
1219 GtkTextBuffer *textbuf;
1220 GtkTextIter iter, start_iter, end_iter;
1222 ChildInfo *child_info;
1224 gtk_widget_show(children->scrolledwin);
1225 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
1226 gtk_text_buffer_get_bounds(textbuf, &start_iter, &end_iter);
1227 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
1228 gtk_text_buffer_get_start_iter(textbuf, &iter);
1230 for (cur = children->list; cur; cur = cur->next) {
1231 child_info = (ChildInfo *)cur->data;
1232 if (child_info->pid)
1233 caption = g_strdup_printf
1234 (_("--- Running: %s\n"),
1237 caption = g_strdup_printf
1238 (_("--- Ended: %s\n"),
1241 gtk_text_buffer_insert(textbuf, &iter, caption, -1);
1242 gtk_text_buffer_insert(textbuf, &iter,
1243 child_info->output->str, -1);
1245 child_info->new_out = FALSE;
1250 static void create_io_dialog(Children *children)
1254 GtkWidget *entry = NULL;
1255 GtkWidget *input_hbox = NULL;
1256 GtkWidget *send_button;
1259 GtkWidget *scrolledwin;
1261 GtkWidget *progress_bar = NULL;
1262 GtkWidget *abort_button;
1263 GtkWidget *close_button;
1265 debug_print("Creating action IO dialog\n");
1267 dialog = gtk_dialog_new();
1268 gtk_container_set_border_width
1269 (GTK_CONTAINER(gtk_dialog_get_action_area(GTK_DIALOG(dialog))), 5);
1270 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1271 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1272 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1273 manage_window_set_transient(GTK_WINDOW(dialog));
1274 g_signal_connect(G_OBJECT(dialog), "delete_event",
1275 G_CALLBACK(delete_io_dialog_cb), children);
1276 g_signal_connect(G_OBJECT(dialog), "destroy",
1277 G_CALLBACK(hide_io_dialog_cb),
1280 vbox = gtk_vbox_new(FALSE, 8);
1281 gtk_container_add(GTK_CONTAINER(
1282 gtk_dialog_get_content_area(GTK_DIALOG(dialog))), vbox);
1283 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1284 gtk_widget_show(vbox);
1286 label = gtk_label_new(children->action);
1287 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1288 gtk_widget_show(label);
1290 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1291 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1292 GTK_POLICY_AUTOMATIC,
1293 GTK_POLICY_AUTOMATIC);
1294 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
1296 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1297 gtk_widget_set_size_request(scrolledwin, 560, 200);
1298 gtk_widget_hide(scrolledwin);
1300 text = gtk_text_view_new();
1302 if (prefs_common.textfont) {
1303 PangoFontDescription *font_desc;
1304 font_desc = pango_font_description_from_string
1305 (prefs_common.textfont);
1307 gtk_widget_modify_font(text, font_desc);
1308 pango_font_description_free(font_desc);
1312 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
1313 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
1314 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
1315 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
1316 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1317 gtk_widget_show(text);
1319 if (children->open_in) {
1320 input_hbox = gtk_hbox_new(FALSE, 8);
1321 gtk_widget_show(input_hbox);
1323 entry = gtk_entry_new();
1324 gtk_widget_set_size_request(entry, 320, -1);
1325 g_signal_connect(G_OBJECT(entry), "activate",
1326 G_CALLBACK(send_input), children);
1327 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1328 if (children->action_type & ACTION_USER_HIDDEN_IN) {
1329 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1331 hildon_gtk_entry_set_input_mode(GTK_ENTRY(entry),
1332 HILDON_GTK_INPUT_MODE_FULL |
1333 HILDON_GTK_INPUT_MODE_INVISIBLE);
1336 gtk_widget_show(entry);
1338 send_button = gtk_button_new_from_stock(GTK_STOCK_EXECUTE);
1339 g_signal_connect(G_OBJECT(send_button), "clicked",
1340 G_CALLBACK(send_input), children);
1341 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1343 gtk_widget_show(send_button);
1345 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1346 gtk_widget_grab_focus(entry);
1349 if (children->initial_nb > 1) {
1352 /* use a more compact format */
1353 const gchar *format = "%s 0/%d\n";
1355 const gchar *format = "%s 0 / %d\n";
1358 progress_bar = gtk_progress_bar_new();
1359 #if !GTK_CHECK_VERSION(3, 0, 0)
1360 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1361 GTK_PROGRESS_LEFT_TO_RIGHT);
1363 gtk_orientable_set_orientation(GTK_ORIENTABLE(progress_bar),
1364 GTK_ORIENTATION_HORIZONTAL);
1365 gtk_progress_bar_set_inverted(GTK_PROGRESS_BAR(progress_bar),
1368 text = g_strdup_printf(format, _("Completed"),
1369 children->initial_nb);
1370 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar),
1373 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1374 gtk_widget_show(progress_bar);
1377 gtkut_stock_button_set_create(&hbox, &abort_button, GTK_STOCK_STOP,
1378 &close_button, GTK_STOCK_CLOSE, NULL, NULL);
1379 g_signal_connect(G_OBJECT(abort_button), "clicked",
1380 G_CALLBACK(kill_children_cb), children);
1381 g_signal_connect(G_OBJECT(close_button), "clicked",
1382 G_CALLBACK(hide_io_dialog_cb), children);
1383 gtk_widget_show(hbox);
1386 gtk_widget_set_sensitive(close_button, FALSE);
1388 gtk_container_add(GTK_CONTAINER(
1389 gtk_dialog_get_action_area(GTK_DIALOG(dialog))), hbox);
1391 children->dialog = dialog;
1392 children->scrolledwin = scrolledwin;
1393 children->text = text;
1394 children->input_hbox = children->open_in ? input_hbox : NULL;
1395 children->input_entry = children->open_in ? entry : NULL;
1396 children->progress_bar = progress_bar;
1397 children->abort_btn = abort_button;
1398 children->close_btn = close_button;
1400 gtk_widget_show(dialog);
1403 static void catch_status(GPid pid, gint status, gpointer data)
1405 ChildInfo *child_info = (ChildInfo *)data;
1407 debug_print("Child returned %d\n", status);
1409 childinfo_close_pipes(child_info);
1410 g_spawn_close_pid(child_info->pid);
1411 child_info->pid = 0;
1413 if (child_info->children->action_type & (ACTION_SINGLE | ACTION_MULTIPLE)
1414 && child_info->msginfo_list) {
1415 /* Actions on message *files* might change size and
1416 * time stamp, and thus invalidate the cache */
1417 SummaryView *summaryview = NULL;
1419 MsgInfo *msginfo, *nmi; /* newmsginfo */
1421 gboolean modified_something = FALSE;
1422 FolderItem *last_item = NULL;
1423 if (mainwindow_get_mainwindow ())
1424 summaryview = mainwindow_get_mainwindow ()->summaryview;
1425 for (cur = child_info->msginfo_list; cur; cur = cur->next) {
1426 msginfo = (MsgInfo *)cur->data;
1427 if (!(msginfo && /* Stuff used valid? */
1428 msginfo->folder && msginfo->folder->cache))
1430 file = procmsg_get_message_file_path (msginfo);
1433 nmi = procheader_parse_file (file, msginfo->flags, TRUE, FALSE);
1435 continue; /* Deleted? */
1436 if (msginfo->mtime != nmi->mtime || msginfo->size != nmi->size) {
1437 nmi->folder = msginfo->folder;
1438 nmi->msgnum = msginfo->msgnum;
1439 msgcache_update_msg (msginfo->folder->cache, nmi);
1440 modified_something = TRUE;
1441 last_item = nmi->folder;
1443 procmsg_msginfo_free (nmi);
1444 if (summaryview && summaryview->displayed &&
1445 summaryview->folder_item == msginfo->folder &&
1446 summary_get_msgnum(summaryview, summaryview->displayed) == msginfo->msgnum)
1447 summary_redisplay_msg(summaryview);
1450 if (modified_something && last_item &&
1451 summaryview && summaryview->folder_item == last_item) {
1452 summary_show (summaryview, summaryview->folder_item);
1454 g_slist_free (child_info->msginfo_list);
1455 child_info->msginfo_list = NULL;
1458 if (!child_info->pid)
1459 child_info->children->nb--;
1461 wait_for_children(child_info->children);
1464 static void catch_input(gpointer data, gint source, GIOCondition cond)
1466 Children *children = (Children *)data;
1467 ChildInfo *child_info = (ChildInfo *)children->list->data;
1468 gchar *input, *ret_str;
1469 gint c, count, len, r;
1470 gssize by_read = 0, by_written = 0;
1472 debug_print("Sending input to grand child.\n");
1473 if (!(cond & (G_IO_OUT | G_IO_ERR)))
1476 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1477 gtk_widget_grab_focus(children->abort_btn);
1479 g_source_remove(child_info->tag_in);
1480 child_info->tag_in = -1;
1482 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1484 ret_str = g_locale_from_utf8(input, strlen(input), &by_read,
1486 if (ret_str && by_written) {
1491 len = strlen(input);
1495 c = write(child_info->chld_in, input + count, len - count);
1498 } while (c >= 0 && count < len);
1501 r = write(child_info->chld_in, "\n", 2);
1505 r = close(child_info->chld_in);
1506 child_info->chld_in = -1;
1508 debug_print("%s(%d)", strerror(errno), errno);
1509 child_info->chld_in = -1;
1510 debug_print("Input to grand child sent.\n");
1513 static void catch_output(gpointer data, gint source, GIOCondition cond)
1515 ChildInfo *child_info = (ChildInfo *)data;
1517 gchar buf[BUFFSIZE];
1519 debug_print("Catching grand child's output.\n");
1520 if (child_info->children->action_type &
1521 (ACTION_PIPE_OUT | ACTION_INSERT)
1522 && source == child_info->chld_out) {
1524 GTK_TEXT_VIEW(child_info->children->msg_text);
1525 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
1530 mark = gtk_text_buffer_get_insert(textbuf);
1531 gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1532 ins_pos = gtk_text_iter_get_offset(&iter);
1535 gsize bytes_read = 0, bytes_written = 0;
1538 c = read(source, buf, sizeof(buf) - 1);
1542 ret_str = g_locale_to_utf8
1543 (buf, c - 1, &bytes_read, &bytes_written, NULL);
1544 if (ret_str && bytes_written > 0) {
1545 gtk_text_buffer_insert
1546 (textbuf, &iter, ret_str,
1550 gtk_text_buffer_insert(textbuf, &iter, buf, c - 1);
1553 if (child_info->children->is_selection) {
1556 gtk_text_buffer_get_iter_at_offset
1557 (textbuf, &ins, ins_pos);
1558 gtk_text_buffer_select_range(textbuf, &ins, &iter);
1561 c = read(source, buf, sizeof(buf) - 1);
1563 gsize bytes_read = 0, bytes_written = 0;
1566 ret_str = g_locale_to_utf8
1567 (buf, c, &bytes_read, &bytes_written, NULL);
1568 if (ret_str && bytes_written > 0) {
1570 (child_info->output, ret_str,
1574 g_string_append_len(child_info->output, buf, c);
1576 child_info->new_out = TRUE;
1580 if (source == child_info->chld_out) {
1581 g_source_remove(child_info->tag_out);
1582 child_info->tag_out = -1;
1583 (void)close(child_info->chld_out);
1584 child_info->chld_out = -1;
1586 g_source_remove(child_info->tag_err);
1587 child_info->tag_err = -1;
1588 (void)close(child_info->chld_err);
1589 child_info->chld_err = -1;
1593 wait_for_children(child_info->children);
1596 static gchar *get_user_string(const gchar *action, ActionType type)
1599 gchar *user_str = NULL;
1602 case ACTION_USER_HIDDEN_STR:
1603 message = g_strdup_printf
1604 (_("Enter the argument for the following action:\n"
1605 "('%%h' will be replaced with the argument)\n"
1608 user_str = input_dialog_with_invisible
1609 (_("Action's hidden user argument"), message, NULL);
1611 case ACTION_USER_STR:
1612 message = g_strdup_printf
1613 (_("Enter the argument for the following action:\n"
1614 "('%%u' will be replaced with the argument)\n"
1617 user_str = input_dialog
1618 (_("Action's user argument"), message, NULL);
1621 g_warning("Unsupported action type %d", type);