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 if (dup(chld_in[0]) < 0)
979 r |= close(chld_in[0]);
980 r |= close(chld_in[1]);
982 r |= close(fileno(stdout));
983 if (dup(chld_out[1]) < 0)
986 r |= close(chld_out[0]);
987 r |= close(chld_out[1]);
989 r |= close(fileno(stderr));
990 if (dup(chld_err[1]) < 0)
993 r |= close(chld_err[0]);
994 r |= close(chld_err[1]);
997 debug_print("%s(%d)", strerror(errno), errno);
1002 ret_str = g_locale_from_utf8(cmd, strlen(cmd),
1003 &by_read, &by_written,
1005 if (ret_str && by_written)
1006 cmdline[2] = ret_str;
1010 execvp("/bin/sh", cmdline);
1015 } else if (gch_pid < (pid_t) 0) { /* Fork error */
1017 r = write(chld_status[1], "1\n", 2);
1019 debug_print("%s(%d)", strerror(errno), errno);
1022 } else { /* Child */
1024 r |= close(chld_in[0]);
1025 r |= close(chld_in[1]);
1026 r |= close(chld_out[0]);
1027 r |= close(chld_out[1]);
1028 r |= close(chld_err[0]);
1029 r |= close(chld_err[1]);
1030 r |= close(chld_status[0]);
1032 debug_print("Child: waiting for grandchild\n");
1033 r |= waitpid(gch_pid, NULL, 0);
1034 debug_print("Child: grandchild ended\n");
1035 r |= write(chld_status[1], "0\n", 2);
1036 r |= close(chld_status[1]);
1039 debug_print("%s(%d)", strerror(errno), errno);
1043 } else if (pid < 0) { /* Fork error */
1044 alertpanel_error(_("Could not fork to execute the following "
1045 "command:\n%s\n%s"),
1046 cmd, g_strerror(errno));
1053 waitpid(pid, NULL, 0);
1057 (void)close(chld_in[0]);
1058 if (!(children->action_type &
1059 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
1060 (void)close(chld_in[1]);
1061 (void)close(chld_out[1]);
1062 (void)close(chld_err[1]);
1063 (void)close(chld_status[1]);
1065 child_info = g_new0(ChildInfo, 1);
1067 child_info->children = children;
1069 child_info->pid = pid;
1070 child_info->next_sig = SIGTERM;
1071 child_info->cmd = g_strdup(cmd);
1072 child_info->new_out = FALSE;
1073 child_info->output = g_string_new(NULL);
1074 child_info->chld_in =
1075 (children->action_type &
1076 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
1078 child_info->chld_out = chld_out[0];
1079 child_info->chld_err = chld_err[0];
1080 child_info->chld_status = chld_status[0];
1081 child_info->tag_in = -1;
1082 child_info->tag_out = claws_input_add(chld_out[0], G_IO_IN | G_IO_HUP | G_IO_ERR,
1083 catch_output, child_info, FALSE);
1084 child_info->tag_err = claws_input_add(chld_err[0], G_IO_IN | G_IO_HUP | G_IO_ERR,
1085 catch_output, child_info, FALSE);
1087 if (!(children->action_type &
1088 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
1091 if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
1093 ret_str = g_locale_from_utf8(msg_str, strlen(msg_str),
1094 &by_read, &by_written, NULL);
1095 if (ret_str && by_written) {
1096 r = write(chld_in[1], ret_str, strlen(ret_str));
1099 r = write(chld_in[1], msg_str, strlen(msg_str));
1100 if (!(children->action_type &
1101 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
1102 r = close(chld_in[1]);
1103 child_info->chld_in = -1; /* No more input */
1105 debug_print("%s(%d)", strerror(errno), errno);
1111 #endif /* G_OS_UNIX */
1114 static void kill_children_cb(GtkWidget *widget, gpointer data)
1118 Children *children = (Children *) data;
1119 ChildInfo *child_info;
1121 for (cur = children->list; cur; cur = cur->next) {
1122 child_info = (ChildInfo *)(cur->data);
1123 debug_print("Killing child group id %d\n", child_info->pid);
1124 if (child_info->pid && kill(-child_info->pid, child_info->next_sig) < 0)
1126 child_info->next_sig = SIGKILL;
1128 #endif /* G_OS_UNIX */
1131 static gint wait_for_children(Children *children)
1133 gboolean new_output;
1134 ChildInfo *child_info;
1137 cur = children->list;
1140 child_info = (ChildInfo *)cur->data;
1141 new_output |= child_info->new_out;
1145 children->output |= new_output;
1147 if (new_output || (children->dialog && (children->initial_nb != children->nb)))
1148 update_io_dialog(children);
1153 if (!children->dialog) {
1154 free_children(children);
1155 } else if (!children->output) {
1156 gtk_widget_destroy(children->dialog);
1162 static void send_input(GtkWidget *w, gpointer data)
1164 Children *children = (Children *) data;
1165 ChildInfo *child_info = (ChildInfo *) children->list->data;
1167 child_info->tag_in = claws_input_add(child_info->chld_in,
1168 G_IO_OUT | G_IO_ERR,
1169 catch_input, children, FALSE);
1172 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1174 hide_io_dialog_cb(w, data);
1178 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1181 Children *children = (Children *)data;
1183 if (!children->nb) {
1184 g_signal_handlers_disconnect_matched
1185 (G_OBJECT(children->dialog), G_SIGNAL_MATCH_DATA,
1186 0, 0, NULL, NULL, children);
1187 gtk_widget_destroy(children->dialog);
1188 free_children(children);
1192 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1195 if (event && (event->keyval == GDK_KEY_Escape ||
1196 event->keyval == GDK_KEY_Return ||
1197 event->keyval == GDK_KEY_KP_Enter))
1198 hide_io_dialog_cb(widget, data);
1202 static void childinfo_close_pipes(ChildInfo *child_info)
1204 /* stdout and stderr pipes are guaranteed to be removed by
1205 * their handler, but in case where we receive child exit notification
1206 * before grand-child's pipes closing signals, we check them and close
1209 if (child_info->tag_in > 0)
1210 g_source_remove(child_info->tag_in);
1211 if (child_info->tag_out > 0)
1212 g_source_remove(child_info->tag_out);
1213 if (child_info->tag_err > 0)
1214 g_source_remove(child_info->tag_err);
1216 if (child_info->chld_in >= 0)
1217 (void)close(child_info->chld_in);
1218 if (child_info->chld_out >= 0)
1219 (void)close(child_info->chld_out);
1220 if (child_info->chld_err >= 0)
1221 (void)close(child_info->chld_err);
1223 (void)close(child_info->chld_status);
1226 static void free_children(Children *children)
1228 ChildInfo *child_info;
1229 void (*callback)(void *data) = NULL;
1232 debug_print("Freeing children data %p\n", children);
1234 g_free(children->action);
1235 while (children->list != NULL) {
1236 child_info = (ChildInfo *)children->list->data;
1237 g_free(child_info->cmd);
1238 g_string_free(child_info->output, TRUE);
1239 children->list = g_slist_remove(children->list, child_info);
1240 callback = child_info->callback;
1241 data = child_info->data;
1251 static void update_io_dialog(Children *children)
1255 debug_print("Updating actions input/output dialog.\n");
1257 if (children->progress_bar) {
1260 /* use a more compact format */
1261 const gchar *format = "%s %d/%d";
1263 const gchar *format = "%s %d / %d";
1266 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar),
1267 (children->initial_nb == 0) ? 0 :
1268 (gdouble) (children->initial_nb - children->nb) /
1269 (gdouble) children->initial_nb);
1270 text = g_strdup_printf(format, _("Completed"),
1271 children->initial_nb - children->nb,
1272 children->initial_nb);
1273 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(children->progress_bar), text);
1277 if (!children->nb) {
1278 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1279 gtk_widget_set_sensitive(children->close_btn, TRUE);
1280 if (children->input_hbox)
1281 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1282 gtk_widget_grab_focus(children->close_btn);
1283 g_signal_connect(G_OBJECT(children->dialog),
1285 G_CALLBACK(io_dialog_key_pressed_cb),
1289 if (children->output) {
1290 GtkWidget *text = children->text;
1291 GtkTextBuffer *textbuf;
1292 GtkTextIter iter, start_iter, end_iter;
1294 ChildInfo *child_info;
1296 gtk_widget_show(children->scrolledwin);
1297 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
1298 gtk_text_buffer_get_bounds(textbuf, &start_iter, &end_iter);
1299 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
1300 gtk_text_buffer_get_start_iter(textbuf, &iter);
1302 for (cur = children->list; cur; cur = cur->next) {
1303 child_info = (ChildInfo *)cur->data;
1304 if (child_info->pid)
1305 caption = g_strdup_printf
1306 (_("--- Running: %s\n"),
1309 caption = g_strdup_printf
1310 (_("--- Ended: %s\n"),
1313 gtk_text_buffer_insert(textbuf, &iter, caption, -1);
1314 gtk_text_buffer_insert(textbuf, &iter,
1315 child_info->output->str, -1);
1317 child_info->new_out = FALSE;
1322 static void create_io_dialog(Children *children)
1326 GtkWidget *entry = NULL;
1327 GtkWidget *input_hbox = NULL;
1328 GtkWidget *send_button;
1331 GtkWidget *scrolledwin;
1333 GtkWidget *progress_bar = NULL;
1334 GtkWidget *abort_button;
1335 GtkWidget *close_button;
1337 debug_print("Creating action IO dialog\n");
1339 dialog = gtk_dialog_new();
1340 gtk_container_set_border_width
1341 (GTK_CONTAINER(gtk_dialog_get_action_area(GTK_DIALOG(dialog))), 5);
1342 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1343 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1344 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1345 manage_window_set_transient(GTK_WINDOW(dialog));
1346 g_signal_connect(G_OBJECT(dialog), "delete_event",
1347 G_CALLBACK(delete_io_dialog_cb), children);
1348 g_signal_connect(G_OBJECT(dialog), "destroy",
1349 G_CALLBACK(hide_io_dialog_cb),
1352 vbox = gtk_vbox_new(FALSE, 8);
1353 gtk_container_add(GTK_CONTAINER(
1354 gtk_dialog_get_content_area(GTK_DIALOG(dialog))), vbox);
1355 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1356 gtk_widget_show(vbox);
1358 label = gtk_label_new(children->action);
1359 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1360 gtk_widget_show(label);
1362 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1363 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1364 GTK_POLICY_AUTOMATIC,
1365 GTK_POLICY_AUTOMATIC);
1366 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
1368 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1369 gtk_widget_set_size_request(scrolledwin, 560, 200);
1370 gtk_widget_hide(scrolledwin);
1372 text = gtk_text_view_new();
1374 if (prefs_common.textfont) {
1375 PangoFontDescription *font_desc;
1376 font_desc = pango_font_description_from_string
1377 (prefs_common.textfont);
1379 gtk_widget_modify_font(text, font_desc);
1380 pango_font_description_free(font_desc);
1384 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
1385 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
1386 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
1387 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
1388 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1389 gtk_widget_show(text);
1391 if (children->open_in) {
1392 input_hbox = gtk_hbox_new(FALSE, 8);
1393 gtk_widget_show(input_hbox);
1395 entry = gtk_entry_new();
1396 gtk_widget_set_size_request(entry, 320, -1);
1397 g_signal_connect(G_OBJECT(entry), "activate",
1398 G_CALLBACK(send_input), children);
1399 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1400 if (children->action_type & ACTION_USER_HIDDEN_IN) {
1401 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1403 hildon_gtk_entry_set_input_mode(GTK_ENTRY(entry),
1404 HILDON_GTK_INPUT_MODE_FULL |
1405 HILDON_GTK_INPUT_MODE_INVISIBLE);
1408 gtk_widget_show(entry);
1410 send_button = gtk_button_new_from_stock(GTK_STOCK_EXECUTE);
1411 g_signal_connect(G_OBJECT(send_button), "clicked",
1412 G_CALLBACK(send_input), children);
1413 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1415 gtk_widget_show(send_button);
1417 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1418 gtk_widget_grab_focus(entry);
1421 if (children->initial_nb > 1) {
1424 /* use a more compact format */
1425 const gchar *format = "%s 0/%d\n";
1427 const gchar *format = "%s 0 / %d\n";
1430 progress_bar = gtk_progress_bar_new();
1431 #if !GTK_CHECK_VERSION(3, 0, 0)
1432 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1433 GTK_PROGRESS_LEFT_TO_RIGHT);
1435 gtk_orientable_set_orientation(GTK_ORIENTABLE(progress_bar),
1436 GTK_ORIENTATION_HORIZONTAL);
1437 gtk_progress_bar_set_inverted(GTK_PROGRESS_BAR(progress_bar),
1440 text = g_strdup_printf(format, _("Completed"),
1441 children->initial_nb);
1442 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar),
1445 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1446 gtk_widget_show(progress_bar);
1449 gtkut_stock_button_set_create(&hbox, &abort_button, GTK_STOCK_STOP,
1450 &close_button, GTK_STOCK_CLOSE, NULL, NULL);
1451 g_signal_connect(G_OBJECT(abort_button), "clicked",
1452 G_CALLBACK(kill_children_cb), children);
1453 g_signal_connect(G_OBJECT(close_button), "clicked",
1454 G_CALLBACK(hide_io_dialog_cb), children);
1455 gtk_widget_show(hbox);
1458 gtk_widget_set_sensitive(close_button, FALSE);
1460 gtk_container_add(GTK_CONTAINER(
1461 gtk_dialog_get_action_area(GTK_DIALOG(dialog))), hbox);
1463 children->dialog = dialog;
1464 children->scrolledwin = scrolledwin;
1465 children->text = text;
1466 children->input_hbox = children->open_in ? input_hbox : NULL;
1467 children->input_entry = children->open_in ? entry : NULL;
1468 children->progress_bar = progress_bar;
1469 children->abort_btn = abort_button;
1470 children->close_btn = close_button;
1472 gtk_widget_show(dialog);
1475 static void catch_status(gpointer data, gint source, GIOCondition cond)
1477 ChildInfo *child_info = (ChildInfo *)data;
1481 g_source_remove(child_info->tag_status);
1483 c = read(source, &buf, 1);
1485 g_message("error reading child return status\n");
1487 debug_print("Child returned %c\n", buf);
1490 waitpid(-child_info->pid, NULL, 0);
1492 childinfo_close_pipes(child_info);
1493 child_info->pid = 0;
1495 if (child_info->children->action_type & (ACTION_SINGLE | ACTION_MULTIPLE)
1496 && child_info->msginfo_list) {
1497 /* Actions on message *files* might change size and
1498 * time stamp, and thus invalidate the cache */
1499 SummaryView *summaryview = NULL;
1501 MsgInfo *msginfo, *nmi; /* newmsginfo */
1503 gboolean modified_something = FALSE;
1504 FolderItem *last_item = NULL;
1505 if (mainwindow_get_mainwindow ())
1506 summaryview = mainwindow_get_mainwindow ()->summaryview;
1507 for (cur = child_info->msginfo_list; cur; cur = cur->next) {
1508 msginfo = (MsgInfo *)cur->data;
1509 if (!(msginfo && /* Stuff used valid? */
1510 msginfo->folder && msginfo->folder->cache))
1512 file = procmsg_get_message_file_path (msginfo);
1515 nmi = procheader_parse_file (file, msginfo->flags, TRUE, FALSE);
1517 continue; /* Deleted? */
1518 if (msginfo->mtime != nmi->mtime || msginfo->size != nmi->size) {
1519 nmi->folder = msginfo->folder;
1520 nmi->msgnum = msginfo->msgnum;
1521 msgcache_update_msg (msginfo->folder->cache, nmi);
1522 modified_something = TRUE;
1523 last_item = nmi->folder;
1525 procmsg_msginfo_free (nmi);
1526 if (summaryview && summaryview->displayed &&
1527 summaryview->folder_item == msginfo->folder &&
1528 summary_get_msgnum(summaryview, summaryview->displayed) == msginfo->msgnum)
1529 summary_redisplay_msg(summaryview);
1532 if (modified_something && last_item &&
1533 summaryview && summaryview->folder_item == last_item) {
1534 summary_show (summaryview, summaryview->folder_item);
1536 g_slist_free (child_info->msginfo_list);
1537 child_info->msginfo_list = NULL;
1540 if (!child_info->pid)
1541 child_info->children->nb--;
1543 wait_for_children(child_info->children);
1546 static void catch_input(gpointer data, gint source, GIOCondition cond)
1548 Children *children = (Children *)data;
1549 ChildInfo *child_info = (ChildInfo *)children->list->data;
1550 gchar *input, *ret_str;
1551 gint c, count, len, r;
1552 gssize by_read = 0, by_written = 0;
1554 debug_print("Sending input to grand child.\n");
1555 if (!(cond & (G_IO_OUT | G_IO_ERR)))
1558 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1559 gtk_widget_grab_focus(children->abort_btn);
1561 g_source_remove(child_info->tag_in);
1562 child_info->tag_in = -1;
1564 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1566 ret_str = g_locale_from_utf8(input, strlen(input), &by_read,
1568 if (ret_str && by_written) {
1573 len = strlen(input);
1577 c = write(child_info->chld_in, input + count, len - count);
1580 } while (c >= 0 && count < len);
1583 r = write(child_info->chld_in, "\n", 2);
1587 r = close(child_info->chld_in);
1589 debug_print("%s(%d)", strerror(errno), errno);
1590 child_info->chld_in = -1;
1591 debug_print("Input to grand child sent.\n");
1594 static void catch_output(gpointer data, gint source, GIOCondition cond)
1596 ChildInfo *child_info = (ChildInfo *)data;
1598 gchar buf[BUFFSIZE];
1600 debug_print("Catching grand child's output.\n");
1601 if (child_info->children->action_type &
1602 (ACTION_PIPE_OUT | ACTION_INSERT)
1603 && source == child_info->chld_out) {
1605 GTK_TEXT_VIEW(child_info->children->msg_text);
1606 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
1611 mark = gtk_text_buffer_get_insert(textbuf);
1612 gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1613 ins_pos = gtk_text_iter_get_offset(&iter);
1616 gsize bytes_read = 0, bytes_written = 0;
1619 c = read(source, buf, sizeof(buf) - 1);
1623 ret_str = g_locale_to_utf8
1624 (buf, c, &bytes_read, &bytes_written, NULL);
1625 if (ret_str && bytes_written > 0) {
1626 gtk_text_buffer_insert
1627 (textbuf, &iter, ret_str,
1631 gtk_text_buffer_insert(textbuf, &iter, buf, c);
1634 if (child_info->children->is_selection) {
1637 gtk_text_buffer_get_iter_at_offset
1638 (textbuf, &ins, ins_pos);
1639 gtk_text_buffer_select_range(textbuf, &ins, &iter);
1642 c = read(source, buf, sizeof(buf) - 1);
1644 gsize bytes_read = 0, bytes_written = 0;
1647 ret_str = g_locale_to_utf8
1648 (buf, c, &bytes_read, &bytes_written, NULL);
1649 if (ret_str && bytes_written > 0) {
1651 (child_info->output, ret_str,
1655 g_string_append_len(child_info->output, buf, c);
1657 child_info->new_out = TRUE;
1661 if (source == child_info->chld_out) {
1662 g_source_remove(child_info->tag_out);
1663 child_info->tag_out = -1;
1664 (void)close(child_info->chld_out);
1665 child_info->chld_out = -1;
1667 g_source_remove(child_info->tag_err);
1668 child_info->tag_err = -1;
1669 (void)close(child_info->chld_err);
1670 child_info->chld_err = -1;
1674 wait_for_children(child_info->children);
1677 static gchar *get_user_string(const gchar *action, ActionType type)
1680 gchar *user_str = NULL;
1683 case ACTION_USER_HIDDEN_STR:
1684 message = g_strdup_printf
1685 (_("Enter the argument for the following action:\n"
1686 "('%%h' will be replaced with the argument)\n"
1689 user_str = input_dialog_with_invisible
1690 (_("Action's hidden user argument"), message, NULL);
1692 case ACTION_USER_STR:
1693 message = g_strdup_printf
1694 (_("Enter the argument for the following action:\n"
1695 "('%%u' will be replaced with the argument)\n"
1698 user_str = input_dialog
1699 (_("Action's user argument"), message, NULL);
1702 g_warning("Unsupported action type %d", type);