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"
61 typedef struct _Children Children;
62 typedef struct _ChildInfo ChildInfo;
63 typedef struct _UserStringDialog UserStringDialog;
69 GtkWidget *input_entry;
70 GtkWidget *input_hbox;
71 GtkWidget *progress_bar;
74 GtkWidget *scrolledwin;
77 ActionType action_type;
86 gboolean is_selection;
106 void (*callback)(void *data);
109 GSList *msginfo_list;
112 static void action_update_menu (GtkUIManager *ui_manager,
113 const gchar *accel_group,
117 static void compose_actions_execute_cb (GtkWidget *widget,
119 static void compose_actions_execute (Compose *compose,
123 static void mainwin_actions_execute_cb (GtkWidget *widget,
125 static void mainwin_actions_execute (MainWindow *mainwin,
129 static void msgview_actions_execute_cb (GtkWidget *widget,
131 static void msgview_actions_execute (MessageView *msgview,
135 static void message_actions_execute (MessageView *msgview,
139 static gboolean execute_filtering_actions(gchar *action,
142 static gboolean execute_actions (gchar *action,
147 void (*callback)(void *data),
150 static gchar *parse_action_cmd (gchar *action,
154 const gchar *user_str,
155 const gchar *user_hidden_str,
156 const gchar *sel_str);
157 static gboolean parse_append_filename (GString *cmd,
160 static gboolean parse_append_msgpart (GString *cmd,
164 static ChildInfo *fork_child (gchar *cmd,
165 const gchar *msg_str,
168 static gint wait_for_children (Children *children);
170 static void free_children (Children *children);
172 static void childinfo_close_pipes (ChildInfo *child_info);
174 static void create_io_dialog (Children *children);
175 static void update_io_dialog (Children *children);
177 static void hide_io_dialog_cb (GtkWidget *widget,
179 static gint io_dialog_key_pressed_cb (GtkWidget *widget,
183 static void catch_output (gpointer data,
186 static void catch_input (gpointer data,
189 static void catch_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 gchar escape_ch[] = "\\ ";
377 cm_return_val_if_fail(msginfo, FALSE);
379 filename = procmsg_get_message_file(msginfo);
382 alertpanel_error(_("Could not get message file %d"),
388 while ((q = strpbrk(p, "$\"`'\\ \t*?[]&|;<>()!#~")) != NULL) {
391 g_string_append(cmd, p);
392 g_string_append(cmd, escape_ch);
395 g_string_append(cmd, p);
402 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
405 gboolean single_part = FALSE;
407 gchar *part_filename;
411 partinfo = procmime_scan_message(msginfo);
413 alertpanel_error(_("Could not get message part."));
420 filename = procmsg_get_message_file_path(msginfo);
421 part_filename = procmime_get_tmp_file_name(partinfo);
423 ret = procmime_get_part(part_filename, partinfo);
426 procmime_mimeinfo_free_all(partinfo);
430 alertpanel_error(_("Can't get part of multipart message: %s"), strerror(-ret));
431 g_free(part_filename);
435 g_string_append(cmd, part_filename);
437 g_free(part_filename);
442 void actions_execute(gpointer data,
447 if (source == TOOLBAR_MAIN)
448 mainwin_actions_execute((MainWindow*)data, action_nb, widget);
449 else if (source == TOOLBAR_COMPOSE)
450 compose_actions_execute((Compose*)data, action_nb, widget);
451 else if (source == TOOLBAR_MSGVIEW)
452 msgview_actions_execute((MessageView*)data, action_nb, widget);
455 void action_update_mainwin_menu(GtkUIManager *ui_manager,
459 action_update_menu(ui_manager, "<MainwinActions>", branch_path,
460 mainwin_actions_execute_cb, mainwin);
463 void action_update_msgview_menu(GtkUIManager *ui_manager,
465 MessageView *msgview)
467 action_update_menu(ui_manager, "<MsgviewActions>", branch_path,
468 msgview_actions_execute_cb, msgview);
471 void action_update_compose_menu(GtkUIManager *ui_manager,
475 action_update_menu(ui_manager, "<ComposeActions>", branch_path,
476 compose_actions_execute_cb, compose);
479 static GtkWidget *find_item_in_menu(GtkWidget *menu, gchar *name)
481 GList *children = gtk_container_get_children(GTK_CONTAINER(GTK_MENU_SHELL(menu)));
482 GList *amenu = children;
483 const gchar *existing_name;
485 GtkWidget *item = GTK_WIDGET(amenu->data);
486 if ((existing_name = g_object_get_data(G_OBJECT(item), "s_name")) != NULL &&
487 !strcmp2(name, existing_name))
489 g_list_free(children);
495 g_list_free(children);
500 static GtkWidget *create_submenus(GtkWidget *menu, const gchar *action)
502 gchar *submenu = g_strdup(action);
503 GtkWidget *new_menu = NULL;
505 if (strchr(submenu, '/')) {
506 const gchar *end = (strchr(submenu, '/')+1);
507 GtkWidget *menu_item = NULL;
509 *strchr(submenu, '/') = '\0';
510 if ((menu_item = find_item_in_menu(menu, submenu)) == NULL) {
511 menu_item = gtk_menu_item_new_with_mnemonic(submenu);
512 g_object_set_data_full(G_OBJECT(menu_item), "s_name", g_strdup(submenu), g_free);
513 gtk_widget_show(menu_item);
514 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);
515 new_menu = gtk_menu_new();
516 gtk_widget_show(new_menu);
517 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), new_menu);
519 new_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(menu_item));
521 new_menu = create_submenus(new_menu, end);
525 return new_menu ? new_menu : menu;
528 static void action_update_menu(GtkUIManager *ui_manager,
529 const gchar *accel_group,
531 gpointer callback, gpointer data)
534 gchar *action, *action_p;
535 int callback_action = 0;
536 GtkWidget *menu = gtk_menu_new();
539 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
540 GtkWidget *cur_menu = menu;
541 const gchar *action_name = NULL;
542 action = g_strdup((gchar *)cur->data);
543 action_p = strstr(action, ": ");
544 if (action_p && action_p[2] &&
545 (action_get_type(&action_p[2]) != ACTION_ERROR) &&
546 (action[0] != '/')) {
547 gchar *accel_path = NULL;
550 if (strchr(action, '/')) {
551 cur_menu = create_submenus(cur_menu, action);
552 action_name = strrchr(action, '/')+1;
554 action_name = action;
556 gtk_menu_set_accel_group (GTK_MENU (cur_menu),
557 gtk_ui_manager_get_accel_group(ui_manager));
558 item = gtk_menu_item_new_with_label(action_name);
559 gtk_menu_shell_append(GTK_MENU_SHELL(cur_menu), item);
560 g_signal_connect(G_OBJECT(item), "activate",
561 G_CALLBACK(callback), data);
562 g_object_set_data(G_OBJECT(item), "action_num", GINT_TO_POINTER(callback_action));
563 gtk_widget_show(item);
564 accel_path = g_strconcat(accel_group,branch_path, "/", action, NULL);
565 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(item), accel_path);
573 gtk_widget_show(menu);
574 gtk_menu_item_set_submenu(GTK_MENU_ITEM(gtk_ui_manager_get_widget(ui_manager, branch_path)), menu);
577 static void compose_actions_execute_cb(GtkWidget *widget, gpointer data)
579 Compose *compose = (Compose *)data;
580 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
581 compose_actions_execute(compose, action_nb, NULL);
584 static void compose_actions_execute(Compose *compose, guint action_nb, GtkWidget *widget)
587 ActionType action_type;
589 cm_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
591 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
592 cm_return_if_fail(buf != NULL);
593 action = strstr(buf, ": ");
594 cm_return_if_fail(action != NULL);
596 /* Point to the beginning of the command-line */
599 action_type = action_get_type(action);
600 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
602 (_("The selected action cannot be used in the compose window\n"
603 "because it contains %%f, %%F, %%as or %%p."));
607 execute_actions(action, NULL, compose->text, 0, NULL,
608 compose_action_cb, compose);
611 static void mainwin_actions_execute_cb(GtkWidget *widget, gpointer data)
613 MainWindow *mainwin = (MainWindow *)data;
614 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
615 mainwin_actions_execute(mainwin, action_nb, NULL);
618 static void mainwin_actions_execute(MainWindow *mainwin, guint action_nb,
623 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
624 message_actions_execute(mainwin->messageview, action_nb, msg_list);
625 g_slist_free(msg_list);
628 static void msgview_actions_execute_cb(GtkWidget *widget, gpointer data)
630 MessageView *msgview = (MessageView *)data;
631 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
632 msgview_actions_execute(msgview, action_nb, NULL);
635 static void msgview_actions_execute(MessageView *msgview, guint action_nb,
638 GSList *msg_list = NULL;
640 if (msgview->msginfo)
641 msg_list = g_slist_append(msg_list, msgview->msginfo);
642 message_actions_execute(msgview, action_nb, msg_list);
643 g_slist_free(msg_list);
646 static void message_actions_execute(MessageView *msgview, guint action_nb,
653 GtkWidget *text = NULL;
655 ActionType action_type;
657 cm_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
659 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
661 cm_return_if_fail(buf);
662 cm_return_if_fail((action = strstr(buf, ": ")));
664 /* Point to the beginning of the command-line */
667 textview = messageview_get_current_textview(msgview);
669 text = textview->text;
670 body_pos = textview->body_pos;
672 partinfo = messageview_get_selected_mime_part(msgview);
674 /* this command will alter the message text */
675 action_type = action_get_type(action);
676 if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
677 msgview->filtered = TRUE;
679 if (action_type & ACTION_FILTERING_ACTION)
680 /* CLAWS: most of the above code is not necessary for applying
682 execute_filtering_actions(action, msg_list);
684 execute_actions(action, msg_list, text, body_pos, partinfo,
688 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
690 GSList *action_list, *p;
691 const gchar *sbegin, *send;
692 gchar *action_string;
693 SummaryView *summaryview = NULL;
694 MainWindow *mainwin = NULL;
696 if (mainwindow_get_mainwindow()) {
697 summaryview = mainwindow_get_mainwindow()->summaryview;
698 mainwin = mainwindow_get_mainwindow();
701 if (NULL == (sbegin = strstr2(action, "%as{")))
703 sbegin += sizeof "%as{" - 1;
704 if (NULL == (send = strrchr(sbegin, '}')))
706 action_string = g_strndup(sbegin, send - sbegin);
708 action_list = matcher_parser_get_action_list(action_string);
709 if (action_list == NULL) {
710 gchar *tmp = g_strdup(action_string);
714 alertpanel_error(_("There is no filtering action set"));
716 alertpanel_error(_("Invalid filtering action(s):\n%s"), tmp);
717 g_free(action_string);
721 g_free(action_string);
723 /* apply actions on each message info */
724 for (p = msglist; p && p->data; p = g_slist_next(p)) {
725 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
729 summary_lock(summaryview);
730 main_window_cursor_wait(mainwin);
731 summary_freeze(summaryview);
732 folder_item_update_freeze();
735 filtering_move_and_copy_msgs(msglist);
738 folder_item_update_thaw();
739 summary_thaw(summaryview);
740 main_window_cursor_normal(mainwin);
741 summary_unlock(summaryview);
742 summary_show(summaryview, summaryview->folder_item);
744 for (p = action_list; p; p = g_slist_next(p))
745 if (p->data) filteringaction_free(p->data);
746 g_slist_free(action_list);
750 static gboolean execute_actions(gchar *action, GSList *msg_list,
752 gint body_pos, MimeInfo *partinfo,
753 void (*callback)(void *data), void *data)
755 GSList *children_list = NULL;
759 ChildInfo *child_info;
760 ActionType action_type;
763 gchar *sel_str = NULL;
764 gchar *msg_str = NULL;
765 gchar *user_str = NULL;
766 gchar *user_hidden_str = NULL;
767 GtkTextIter start_iter, end_iter;
768 gboolean is_selection = FALSE;
770 cm_return_val_if_fail(action && *action, FALSE);
772 action_type = action_get_type(action);
774 if (action_type == ACTION_ERROR)
775 return FALSE; /* ERR: syntax error */
777 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
778 return FALSE; /* ERR: file command without selection */
780 msg_list_len = g_slist_length(msg_list);
782 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
783 if (msg_list_len > 1)
784 return FALSE; /* ERR: pipe + multiple selection */
786 return FALSE; /* ERR: pipe and no displayed text */
789 if (action_type & ACTION_SELECTION_STR) {
791 return FALSE; /* ERR: selection string but no text */
795 GtkTextBuffer *textbuf;
797 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
798 is_selection = gtk_text_buffer_get_selection_bounds
799 (textbuf, &start_iter, &end_iter);
801 gtk_text_buffer_get_iter_at_offset
802 (textbuf, &start_iter, body_pos);
803 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
805 msg_str = gtk_text_buffer_get_text
806 (textbuf, &start_iter, &end_iter, FALSE);
808 sel_str = g_strdup(msg_str);
811 if (action_type & ACTION_USER_STR) {
812 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
819 if (action_type & ACTION_USER_HIDDEN_STR) {
820 if (!(user_hidden_str =
821 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
829 if (text && (action_type & ACTION_PIPE_OUT)) {
830 GtkTextBuffer *textbuf;
831 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
832 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
835 children = g_new0(Children, 1);
837 children->action = g_strdup(action);
838 children->action_type = action_type;
839 children->msg_text = text;
840 children->is_selection = is_selection;
842 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
843 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
844 children->open_in = 1;
846 if (action_type & ACTION_SINGLE) {
849 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
850 msginfo = (MsgInfo *)cur->data;
852 is_ok = FALSE; /* ERR: msginfo missing */
855 cmd = parse_action_cmd(action, msginfo, msg_list,
857 user_hidden_str, sel_str);
859 debug_print("Action command error\n");
860 is_ok = FALSE; /* ERR: incorrect command */
863 if ((child_info = fork_child(cmd, msg_str, children))) {
864 /* Pass msginfo to catch_status () */
865 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
866 child_info->msginfo_list =
867 g_slist_append (NULL, msginfo);
868 children_list = g_slist_append(children_list,
874 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
875 user_str, user_hidden_str, sel_str);
877 if ((child_info = fork_child(cmd, msg_str, children))) {
878 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
879 child_info->msginfo_list =
880 g_slist_copy (msg_list);
881 children_list = g_slist_append(children_list,
886 is_ok = FALSE; /* ERR: incorrect command */
892 g_free(user_hidden_str);
894 if (!children_list) {
895 /* If not waiting for children, return */
896 free_children(children);
900 children->list = children_list;
901 children->nb = g_slist_length(children_list);
902 children->initial_nb = children->nb;
904 for (cur = children_list; cur; cur = cur->next) {
905 child_info = (ChildInfo *) cur->data;
906 child_info->callback = callback;
907 child_info->data = data;
908 child_info->tag_status =
909 claws_input_add(child_info->chld_status,
910 G_IO_IN | G_IO_HUP | G_IO_ERR,
911 catch_status, child_info,
915 create_io_dialog(children);
920 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
924 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
925 gchar *cmdline[4], *ret_str;
927 ChildInfo *child_info;
929 gssize by_written = 0, by_read = 0;
931 sync = !(children->action_type & ACTION_ASYNC);
933 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
934 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
937 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
939 alertpanel_error(_("Command could not be started. "
940 "Pipe creation failed.\n%s"),
942 /* Closing fd = -1 fails silently */
943 (void)close(chld_in[0]);
944 (void)close(chld_in[1]);
945 (void)close(chld_out[0]);
946 (void)close(chld_out[1]);
947 (void)close(chld_err[0]);
948 (void)close(chld_err[1]);
949 (void)close(chld_status[0]);
950 (void)close(chld_status[1]);
951 return NULL; /* Pipe error */
955 debug_print("Forking child and grandchild.\n");
956 debug_print("Executing: /bin/sh -c %s\n", cmd);
959 if (pid == 0) { /* Child */
967 if (setpgid(0, getppid()))
971 if (children->action_type &
974 ACTION_USER_HIDDEN_IN)) {
975 r |= close(fileno(stdin));
976 (void) dup (chld_in[0]);
978 r |= close(chld_in[0]);
979 r |= close(chld_in[1]);
981 r |= close(fileno(stdout));
982 (void) dup (chld_out[1]);
983 r |= close(chld_out[0]);
984 r |= close(chld_out[1]);
986 r |= close(fileno(stderr));
987 (void) dup (chld_err[1]);
988 r |= close(chld_err[0]);
989 r |= close(chld_err[1]);
992 debug_print("%s(%d)", strerror(errno), errno);
997 ret_str = g_locale_from_utf8(cmd, strlen(cmd),
998 &by_read, &by_written,
1000 if (ret_str && by_written)
1001 cmdline[2] = ret_str;
1005 execvp("/bin/sh", cmdline);
1010 } else if (gch_pid < (pid_t) 0) { /* Fork error */
1012 r = write(chld_status[1], "1\n", 2);
1014 debug_print("%s(%d)", strerror(errno), errno);
1017 } else { /* Child */
1019 r |= close(chld_in[0]);
1020 r |= close(chld_in[1]);
1021 r |= close(chld_out[0]);
1022 r |= close(chld_out[1]);
1023 r |= close(chld_err[0]);
1024 r |= close(chld_err[1]);
1025 r |= close(chld_status[0]);
1027 debug_print("Child: waiting for grandchild\n");
1028 r |= waitpid(gch_pid, NULL, 0);
1029 debug_print("Child: grandchild ended\n");
1030 r |= write(chld_status[1], "0\n", 2);
1031 r |= close(chld_status[1]);
1034 debug_print("%s(%d)", strerror(errno), errno);
1038 } else if (pid < 0) { /* Fork error */
1039 alertpanel_error(_("Could not fork to execute the following "
1040 "command:\n%s\n%s"),
1041 cmd, g_strerror(errno));
1048 waitpid(pid, NULL, 0);
1052 (void)close(chld_in[0]);
1053 if (!(children->action_type &
1054 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
1055 (void)close(chld_in[1]);
1056 (void)close(chld_out[1]);
1057 (void)close(chld_err[1]);
1058 (void)close(chld_status[1]);
1060 child_info = g_new0(ChildInfo, 1);
1062 child_info->children = children;
1064 child_info->pid = pid;
1065 child_info->next_sig = SIGTERM;
1066 child_info->cmd = g_strdup(cmd);
1067 child_info->new_out = FALSE;
1068 child_info->output = g_string_new(NULL);
1069 child_info->chld_in =
1070 (children->action_type &
1071 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
1073 child_info->chld_out = chld_out[0];
1074 child_info->chld_err = chld_err[0];
1075 child_info->chld_status = chld_status[0];
1076 child_info->tag_in = -1;
1077 child_info->tag_out = claws_input_add(chld_out[0], G_IO_IN | G_IO_HUP | G_IO_ERR,
1078 catch_output, child_info, FALSE);
1079 child_info->tag_err = claws_input_add(chld_err[0], G_IO_IN | G_IO_HUP | G_IO_ERR,
1080 catch_output, child_info, FALSE);
1082 if (!(children->action_type &
1083 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
1086 if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
1088 ret_str = g_locale_from_utf8(msg_str, strlen(msg_str),
1089 &by_read, &by_written, NULL);
1090 if (ret_str && by_written) {
1091 r = write(chld_in[1], ret_str, strlen(ret_str));
1094 r = write(chld_in[1], msg_str, strlen(msg_str));
1095 if (!(children->action_type &
1096 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
1097 r = close(chld_in[1]);
1098 child_info->chld_in = -1; /* No more input */
1100 debug_print("%s(%d)", strerror(errno), errno);
1106 #endif /* G_OS_UNIX */
1109 static void kill_children_cb(GtkWidget *widget, gpointer data)
1113 Children *children = (Children *) data;
1114 ChildInfo *child_info;
1116 for (cur = children->list; cur; cur = cur->next) {
1117 child_info = (ChildInfo *)(cur->data);
1118 debug_print("Killing child group id %d\n", child_info->pid);
1119 if (child_info->pid && kill(-child_info->pid, child_info->next_sig) < 0)
1121 child_info->next_sig = SIGKILL;
1123 #endif /* G_OS_UNIX */
1126 static gint wait_for_children(Children *children)
1128 gboolean new_output;
1129 ChildInfo *child_info;
1132 cur = children->list;
1135 child_info = (ChildInfo *)cur->data;
1136 new_output |= child_info->new_out;
1140 children->output |= new_output;
1142 if (new_output || (children->dialog && (children->initial_nb != children->nb)))
1143 update_io_dialog(children);
1148 if (!children->dialog) {
1149 free_children(children);
1150 } else if (!children->output) {
1151 gtk_widget_destroy(children->dialog);
1157 static void send_input(GtkWidget *w, gpointer data)
1159 Children *children = (Children *) data;
1160 ChildInfo *child_info = (ChildInfo *) children->list->data;
1162 child_info->tag_in = claws_input_add(child_info->chld_in,
1163 G_IO_OUT | G_IO_ERR,
1164 catch_input, children, FALSE);
1167 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1169 hide_io_dialog_cb(w, data);
1173 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1176 Children *children = (Children *)data;
1178 if (!children->nb) {
1179 g_signal_handlers_disconnect_matched
1180 (G_OBJECT(children->dialog), G_SIGNAL_MATCH_DATA,
1181 0, 0, NULL, NULL, children);
1182 gtk_widget_destroy(children->dialog);
1183 free_children(children);
1187 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1190 if (event && (event->keyval == GDK_KEY_Escape ||
1191 event->keyval == GDK_KEY_Return ||
1192 event->keyval == GDK_KEY_KP_Enter))
1193 hide_io_dialog_cb(widget, data);
1197 static void childinfo_close_pipes(ChildInfo *child_info)
1199 /* stdout and stderr pipes are guaranteed to be removed by
1200 * their handler, but in case where we receive child exit notification
1201 * before grand-child's pipes closing signals, we check them and close
1204 if (child_info->tag_in > 0)
1205 g_source_remove(child_info->tag_in);
1206 if (child_info->tag_out > 0)
1207 g_source_remove(child_info->tag_out);
1208 if (child_info->tag_err > 0)
1209 g_source_remove(child_info->tag_err);
1211 if (child_info->chld_in >= 0)
1212 (void)close(child_info->chld_in);
1213 if (child_info->chld_out >= 0)
1214 (void)close(child_info->chld_out);
1215 if (child_info->chld_err >= 0)
1216 (void)close(child_info->chld_err);
1218 (void)close(child_info->chld_status);
1221 static void free_children(Children *children)
1223 ChildInfo *child_info;
1224 void (*callback)(void *data) = NULL;
1227 debug_print("Freeing children data %p\n", children);
1229 g_free(children->action);
1230 while (children->list != NULL) {
1231 child_info = (ChildInfo *)children->list->data;
1232 g_free(child_info->cmd);
1233 g_string_free(child_info->output, TRUE);
1234 children->list = g_slist_remove(children->list, child_info);
1235 callback = child_info->callback;
1236 data = child_info->data;
1246 static void update_io_dialog(Children *children)
1250 debug_print("Updating actions input/output dialog.\n");
1252 if (children->progress_bar) {
1255 /* use a more compact format */
1256 const gchar *format = "%s %d/%d";
1258 const gchar *format = "%s %d / %d";
1261 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar),
1262 (children->initial_nb == 0) ? 0 :
1263 (gdouble) (children->initial_nb - children->nb) /
1264 (gdouble) children->initial_nb);
1265 text = g_strdup_printf(format, _("Completed"),
1266 children->initial_nb - children->nb,
1267 children->initial_nb);
1268 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(children->progress_bar), text);
1272 if (!children->nb) {
1273 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1274 gtk_widget_set_sensitive(children->close_btn, TRUE);
1275 if (children->input_hbox)
1276 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1277 gtk_widget_grab_focus(children->close_btn);
1278 g_signal_connect(G_OBJECT(children->dialog),
1280 G_CALLBACK(io_dialog_key_pressed_cb),
1284 if (children->output) {
1285 GtkWidget *text = children->text;
1286 GtkTextBuffer *textbuf;
1287 GtkTextIter iter, start_iter, end_iter;
1289 ChildInfo *child_info;
1291 gtk_widget_show(children->scrolledwin);
1292 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
1293 gtk_text_buffer_get_bounds(textbuf, &start_iter, &end_iter);
1294 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
1295 gtk_text_buffer_get_start_iter(textbuf, &iter);
1297 for (cur = children->list; cur; cur = cur->next) {
1298 child_info = (ChildInfo *)cur->data;
1299 if (child_info->pid)
1300 caption = g_strdup_printf
1301 (_("--- Running: %s\n"),
1304 caption = g_strdup_printf
1305 (_("--- Ended: %s\n"),
1308 gtk_text_buffer_insert(textbuf, &iter, caption, -1);
1309 gtk_text_buffer_insert(textbuf, &iter,
1310 child_info->output->str, -1);
1312 child_info->new_out = FALSE;
1317 static void create_io_dialog(Children *children)
1321 GtkWidget *entry = NULL;
1322 GtkWidget *input_hbox = NULL;
1323 GtkWidget *send_button;
1326 GtkWidget *scrolledwin;
1328 GtkWidget *progress_bar = NULL;
1329 GtkWidget *abort_button;
1330 GtkWidget *close_button;
1332 debug_print("Creating action IO dialog\n");
1334 dialog = gtk_dialog_new();
1335 gtk_container_set_border_width
1336 (GTK_CONTAINER(gtk_dialog_get_action_area(GTK_DIALOG(dialog))), 5);
1337 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1338 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1339 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1340 manage_window_set_transient(GTK_WINDOW(dialog));
1341 g_signal_connect(G_OBJECT(dialog), "delete_event",
1342 G_CALLBACK(delete_io_dialog_cb), children);
1343 g_signal_connect(G_OBJECT(dialog), "destroy",
1344 G_CALLBACK(hide_io_dialog_cb),
1347 vbox = gtk_vbox_new(FALSE, 8);
1348 gtk_container_add(GTK_CONTAINER(
1349 gtk_dialog_get_content_area(GTK_DIALOG(dialog))), vbox);
1350 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1351 gtk_widget_show(vbox);
1353 label = gtk_label_new(children->action);
1354 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1355 gtk_widget_show(label);
1357 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1358 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1359 GTK_POLICY_AUTOMATIC,
1360 GTK_POLICY_AUTOMATIC);
1361 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
1363 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1364 gtk_widget_set_size_request(scrolledwin, 560, 200);
1365 gtk_widget_hide(scrolledwin);
1367 text = gtk_text_view_new();
1369 if (prefs_common.textfont) {
1370 PangoFontDescription *font_desc;
1371 font_desc = pango_font_description_from_string
1372 (prefs_common.textfont);
1374 gtk_widget_modify_font(text, font_desc);
1375 pango_font_description_free(font_desc);
1379 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
1380 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
1381 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
1382 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
1383 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1384 gtk_widget_show(text);
1386 if (children->open_in) {
1387 input_hbox = gtk_hbox_new(FALSE, 8);
1388 gtk_widget_show(input_hbox);
1390 entry = gtk_entry_new();
1391 gtk_widget_set_size_request(entry, 320, -1);
1392 g_signal_connect(G_OBJECT(entry), "activate",
1393 G_CALLBACK(send_input), children);
1394 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1395 if (children->action_type & ACTION_USER_HIDDEN_IN) {
1396 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1398 hildon_gtk_entry_set_input_mode(GTK_ENTRY(entry),
1399 HILDON_GTK_INPUT_MODE_FULL |
1400 HILDON_GTK_INPUT_MODE_INVISIBLE);
1403 gtk_widget_show(entry);
1405 send_button = gtk_button_new_from_stock(GTK_STOCK_EXECUTE);
1406 g_signal_connect(G_OBJECT(send_button), "clicked",
1407 G_CALLBACK(send_input), children);
1408 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1410 gtk_widget_show(send_button);
1412 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1413 gtk_widget_grab_focus(entry);
1416 if (children->initial_nb > 1) {
1419 /* use a more compact format */
1420 const gchar *format = "%s 0/%d\n";
1422 const gchar *format = "%s 0 / %d\n";
1425 progress_bar = gtk_progress_bar_new();
1426 #if !GTK_CHECK_VERSION(3, 0, 0)
1427 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1428 GTK_PROGRESS_LEFT_TO_RIGHT);
1430 gtk_orientable_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1431 GTK_ORIENTATION_HORIZONTAL);
1432 gtk_progress_bar_set_inverted(GTK_PROGRESS_BAR(progress_bar),
1435 text = g_strdup_printf(format, _("Completed"),
1436 children->initial_nb);
1437 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar),
1440 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1441 gtk_widget_show(progress_bar);
1444 gtkut_stock_button_set_create(&hbox, &abort_button, GTK_STOCK_STOP,
1445 &close_button, GTK_STOCK_CLOSE, NULL, NULL);
1446 g_signal_connect(G_OBJECT(abort_button), "clicked",
1447 G_CALLBACK(kill_children_cb), children);
1448 g_signal_connect(G_OBJECT(close_button), "clicked",
1449 G_CALLBACK(hide_io_dialog_cb), children);
1450 gtk_widget_show(hbox);
1453 gtk_widget_set_sensitive(close_button, FALSE);
1455 gtk_container_add(GTK_CONTAINER(
1456 gtk_dialog_get_action_area(GTK_DIALOG(dialog))), hbox);
1458 children->dialog = dialog;
1459 children->scrolledwin = scrolledwin;
1460 children->text = text;
1461 children->input_hbox = children->open_in ? input_hbox : NULL;
1462 children->input_entry = children->open_in ? entry : NULL;
1463 children->progress_bar = progress_bar;
1464 children->abort_btn = abort_button;
1465 children->close_btn = close_button;
1467 gtk_widget_show(dialog);
1470 static void catch_status(gpointer data, gint source, GIOCondition cond)
1472 ChildInfo *child_info = (ChildInfo *)data;
1476 g_source_remove(child_info->tag_status);
1478 c = read(source, &buf, 1);
1480 g_message("error reading child return status\n");
1482 debug_print("Child returned %c\n", buf);
1485 waitpid(-child_info->pid, NULL, 0);
1487 childinfo_close_pipes(child_info);
1488 child_info->pid = 0;
1490 if (child_info->children->action_type & (ACTION_SINGLE | ACTION_MULTIPLE)
1491 && child_info->msginfo_list) {
1492 /* Actions on message *files* might change size and
1493 * time stamp, and thus invalidate the cache */
1494 SummaryView *summaryview = NULL;
1496 MsgInfo *msginfo, *nmi; /* newmsginfo */
1498 gboolean modified_something = FALSE;
1499 FolderItem *last_item = NULL;
1500 if (mainwindow_get_mainwindow ())
1501 summaryview = mainwindow_get_mainwindow ()->summaryview;
1502 for (cur = child_info->msginfo_list; cur; cur = cur->next) {
1503 msginfo = (MsgInfo *)cur->data;
1504 if (!(msginfo && /* Stuff used valid? */
1505 msginfo->folder && msginfo->folder->cache))
1507 file = procmsg_get_message_file_path (msginfo);
1510 nmi = procheader_parse_file (file, msginfo->flags, TRUE, FALSE);
1512 continue; /* Deleted? */
1513 if (msginfo->mtime != nmi->mtime || msginfo->size != nmi->size) {
1514 nmi->folder = msginfo->folder;
1515 nmi->msgnum = msginfo->msgnum;
1516 msgcache_update_msg (msginfo->folder->cache, nmi);
1517 modified_something = TRUE;
1518 last_item = nmi->folder;
1520 procmsg_msginfo_free (nmi);
1521 if (summaryview && summaryview->displayed &&
1522 summaryview->folder_item == msginfo->folder &&
1523 summary_get_msgnum(summaryview, summaryview->displayed) == msginfo->msgnum)
1524 summary_redisplay_msg(summaryview);
1527 if (modified_something && last_item &&
1528 summaryview && summaryview->folder_item == last_item) {
1529 summary_show (summaryview, summaryview->folder_item);
1531 g_slist_free (child_info->msginfo_list);
1532 child_info->msginfo_list = NULL;
1535 if (!child_info->pid)
1536 child_info->children->nb--;
1538 wait_for_children(child_info->children);
1541 static void catch_input(gpointer data, gint source, GIOCondition cond)
1543 Children *children = (Children *)data;
1544 ChildInfo *child_info = (ChildInfo *)children->list->data;
1545 gchar *input, *ret_str;
1546 gint c, count, len, r;
1547 gssize by_read = 0, by_written = 0;
1549 debug_print("Sending input to grand child.\n");
1550 if (!(cond & (G_IO_OUT | G_IO_ERR)))
1553 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1554 gtk_widget_grab_focus(children->abort_btn);
1556 g_source_remove(child_info->tag_in);
1557 child_info->tag_in = -1;
1559 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1561 ret_str = g_locale_from_utf8(input, strlen(input), &by_read,
1563 if (ret_str && by_written) {
1568 len = strlen(input);
1572 c = write(child_info->chld_in, input + count, len - count);
1575 } while (c >= 0 && count < len);
1578 r = write(child_info->chld_in, "\n", 2);
1582 r = close(child_info->chld_in);
1584 debug_print("%s(%d)", strerror(errno), errno);
1585 child_info->chld_in = -1;
1586 debug_print("Input to grand child sent.\n");
1589 static void catch_output(gpointer data, gint source, GIOCondition cond)
1591 ChildInfo *child_info = (ChildInfo *)data;
1593 gchar buf[BUFFSIZE];
1595 debug_print("Catching grand child's output.\n");
1596 if (child_info->children->action_type &
1597 (ACTION_PIPE_OUT | ACTION_INSERT)
1598 && source == child_info->chld_out) {
1600 GTK_TEXT_VIEW(child_info->children->msg_text);
1601 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
1606 mark = gtk_text_buffer_get_insert(textbuf);
1607 gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1608 ins_pos = gtk_text_iter_get_offset(&iter);
1611 gsize bytes_read = 0, bytes_written = 0;
1614 c = read(source, buf, sizeof(buf) - 1);
1618 ret_str = g_locale_to_utf8
1619 (buf, c, &bytes_read, &bytes_written, NULL);
1620 if (ret_str && bytes_written > 0) {
1621 gtk_text_buffer_insert
1622 (textbuf, &iter, ret_str,
1626 gtk_text_buffer_insert(textbuf, &iter, buf, c);
1629 if (child_info->children->is_selection) {
1632 gtk_text_buffer_get_iter_at_offset
1633 (textbuf, &ins, ins_pos);
1634 gtk_text_buffer_select_range(textbuf, &ins, &iter);
1637 c = read(source, buf, sizeof(buf) - 1);
1639 gsize bytes_read = 0, bytes_written = 0;
1642 ret_str = g_locale_to_utf8
1643 (buf, c, &bytes_read, &bytes_written, NULL);
1644 if (ret_str && bytes_written > 0) {
1646 (child_info->output, ret_str,
1650 g_string_append_len(child_info->output, buf, c);
1652 child_info->new_out = TRUE;
1656 if (source == child_info->chld_out) {
1657 g_source_remove(child_info->tag_out);
1658 child_info->tag_out = -1;
1659 (void)close(child_info->chld_out);
1660 child_info->chld_out = -1;
1662 g_source_remove(child_info->tag_err);
1663 child_info->tag_err = -1;
1664 (void)close(child_info->chld_err);
1665 child_info->chld_err = -1;
1669 wait_for_children(child_info->children);
1672 static gchar *get_user_string(const gchar *action, ActionType type)
1675 gchar *user_str = NULL;
1678 case ACTION_USER_HIDDEN_STR:
1679 message = g_strdup_printf
1680 (_("Enter the argument for the following action:\n"
1681 "('%%h' will be replaced with the argument)\n"
1684 user_str = input_dialog_with_invisible
1685 (_("Action's hidden user argument"), message, NULL);
1687 case ACTION_USER_STR:
1688 message = g_strdup_printf
1689 (_("Enter the argument for the following action:\n"
1690 "('%%u' will be replaced with the argument)\n"
1693 user_str = input_dialog
1694 (_("Action's user argument"), message, NULL);
1697 g_warning("Unsupported action type %d", type);