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"
56 #include "matcher_parser.h" /* CLAWS */
57 #include "filtering.h"
58 #include "procheader.h"
60 typedef struct _Children Children;
61 typedef struct _ChildInfo ChildInfo;
62 typedef struct _UserStringDialog UserStringDialog;
68 GtkWidget *input_entry;
69 GtkWidget *input_hbox;
70 GtkWidget *progress_bar;
73 GtkWidget *scrolledwin;
76 ActionType action_type;
85 gboolean is_selection;
105 void (*callback)(void *data);
108 GSList *msginfo_list;
111 static void action_update_menu (GtkUIManager *ui_manager,
112 const gchar *accel_group,
116 static void compose_actions_execute_cb (GtkWidget *widget,
118 static void compose_actions_execute (Compose *compose,
122 static void mainwin_actions_execute_cb (GtkWidget *widget,
124 static void mainwin_actions_execute (MainWindow *mainwin,
128 static void msgview_actions_execute_cb (GtkWidget *widget,
130 static void msgview_actions_execute (MessageView *msgview,
134 static void message_actions_execute (MessageView *msgview,
138 static gboolean execute_filtering_actions(gchar *action,
141 static gboolean execute_actions (gchar *action,
146 void (*callback)(void *data),
149 static gchar *parse_action_cmd (gchar *action,
153 const gchar *user_str,
154 const gchar *user_hidden_str,
155 const gchar *sel_str);
156 static gboolean parse_append_filename (GString *cmd,
159 static gboolean parse_append_msgpart (GString *cmd,
163 static ChildInfo *fork_child (gchar *cmd,
164 const gchar *msg_str,
167 static gint wait_for_children (Children *children);
169 static void free_children (Children *children);
171 static void childinfo_close_pipes (ChildInfo *child_info);
173 static void create_io_dialog (Children *children);
174 static void update_io_dialog (Children *children);
176 static void hide_io_dialog_cb (GtkWidget *widget,
178 static gint io_dialog_key_pressed_cb (GtkWidget *widget,
182 static void catch_output (gpointer data,
185 static void catch_input (gpointer data,
188 static void catch_status (gpointer data,
192 static gchar *get_user_string (const gchar *action,
196 ActionType action_get_type(const gchar *action_str)
199 gboolean in_filtering_action = FALSE;
200 ActionType action_type = ACTION_NONE;
202 cm_return_val_if_fail(action_str, ACTION_ERROR);
203 cm_return_val_if_fail(*action_str, ACTION_ERROR);
208 action_type |= ACTION_PIPE_IN;
210 } else if (p[0] == '>') {
211 action_type |= ACTION_USER_IN;
213 } else if (p[0] == '*') {
214 action_type |= ACTION_USER_HIDDEN_IN;
221 while (*p && action_type != ACTION_ERROR) {
222 if (!in_filtering_action) {
223 if (p[0] == '%' && p[1]) {
226 /* CLAWS: filtering action is a mutually exclusive
227 * action. we can enable others if needed later. we
228 * add ACTION_SINGLE | ACTION_MULTIPLE so it will
229 * only be executed from the main window toolbar */
230 if (p[2] == 's') /* source messages */
231 action_type = ACTION_FILTERING_ACTION
234 in_filtering_action = TRUE;
237 action_type |= ACTION_SINGLE;
240 action_type |= ACTION_MULTIPLE;
243 action_type |= ACTION_SINGLE;
246 action_type |= ACTION_SELECTION_STR;
249 action_type |= ACTION_USER_STR;
252 action_type |= ACTION_USER_HIDDEN_STR;
258 action_type = ACTION_ERROR;
262 } else if (p[0] == '|') {
264 action_type |= ACTION_PIPE_OUT;
265 } else if (p[0] == '>') {
267 action_type |= ACTION_INSERT;
268 } else if (p[0] == '&') {
270 action_type |= ACTION_ASYNC;
271 } else if (p[0] == '}') {
272 in_filtering_action = FALSE;
281 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
282 GSList *msg_list, MimeInfo *partinfo,
283 const gchar *user_str,
284 const gchar *user_hidden_str,
285 const gchar *sel_str)
293 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
296 cmd = g_string_sized_new(strlen(action));
299 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
300 if (p[0] == '%' && p[1]) {
303 if (!parse_append_filename(cmd, msginfo)) {
304 g_string_free(cmd, TRUE);
310 for (cur = msg_list; cur != NULL;
312 MsgInfo *msg = (MsgInfo *)cur->data;
314 if (!parse_append_filename(cmd, msg)) {
315 g_string_free(cmd, TRUE);
319 g_string_append_c(cmd, ' ');
324 if (!parse_append_msgpart(cmd, msginfo,
326 g_string_free(cmd, TRUE);
333 g_string_append(cmd, sel_str);
338 g_string_append(cmd, user_str);
343 g_string_append(cmd, user_hidden_str);
347 g_string_append_c(cmd, p[1]);
351 g_string_append_c(cmd, p[0]);
352 g_string_append_c(cmd, p[1]);
356 g_string_append_c(cmd, p[0]);
361 g_string_free(cmd, TRUE);
366 g_string_free(cmd, FALSE);
370 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
374 gchar escape_ch[] = "\\ ";
376 cm_return_val_if_fail(msginfo, FALSE);
378 filename = procmsg_get_message_file(msginfo);
381 alertpanel_error(_("Could not get message file %d"),
387 while ((q = strpbrk(p, "$\"`'\\ \t*?[]&|;<>()!#~")) != NULL) {
390 g_string_append(cmd, p);
391 g_string_append(cmd, escape_ch);
394 g_string_append(cmd, p);
401 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
404 gboolean single_part = FALSE;
406 gchar *part_filename;
410 partinfo = procmime_scan_message(msginfo);
412 alertpanel_error(_("Could not get message part."));
419 filename = procmsg_get_message_file_path(msginfo);
420 part_filename = procmime_get_tmp_file_name(partinfo);
422 ret = procmime_get_part(part_filename, partinfo);
425 procmime_mimeinfo_free_all(partinfo);
429 alertpanel_error(_("Can't get part of multipart message: %s"), strerror(-ret));
430 g_free(part_filename);
434 g_string_append(cmd, part_filename);
436 g_free(part_filename);
441 void actions_execute(gpointer data,
446 if (source == TOOLBAR_MAIN)
447 mainwin_actions_execute((MainWindow*)data, action_nb, widget);
448 else if (source == TOOLBAR_COMPOSE)
449 compose_actions_execute((Compose*)data, action_nb, widget);
450 else if (source == TOOLBAR_MSGVIEW)
451 msgview_actions_execute((MessageView*)data, action_nb, widget);
454 void action_update_mainwin_menu(GtkUIManager *ui_manager,
458 action_update_menu(ui_manager, "<MainwinActions>", branch_path,
459 mainwin_actions_execute_cb, mainwin);
462 void action_update_msgview_menu(GtkUIManager *ui_manager,
464 MessageView *msgview)
466 action_update_menu(ui_manager, "<MsgviewActions>", branch_path,
467 msgview_actions_execute_cb, msgview);
470 void action_update_compose_menu(GtkUIManager *ui_manager,
474 action_update_menu(ui_manager, "<ComposeActions>", branch_path,
475 compose_actions_execute_cb, compose);
478 static GtkWidget *find_item_in_menu(GtkWidget *menu, gchar *name)
480 GList *children = gtk_container_get_children(GTK_CONTAINER(GTK_MENU_SHELL(menu)));
481 GList *amenu = children;
482 const gchar *existing_name;
484 GtkWidget *item = GTK_WIDGET(amenu->data);
485 if ((existing_name = g_object_get_data(G_OBJECT(item), "s_name")) != NULL &&
486 !strcmp2(name, existing_name))
488 g_list_free(children);
494 g_list_free(children);
499 static GtkWidget *create_submenus(GtkWidget *menu, const gchar *action)
501 gchar *submenu = g_strdup(action);
502 GtkWidget *new_menu = NULL;
504 if (strchr(submenu, '/')) {
505 const gchar *end = (strchr(submenu, '/')+1);
506 GtkWidget *menu_item = NULL;
508 *strchr(submenu, '/') = '\0';
509 if ((menu_item = find_item_in_menu(menu, submenu)) == NULL) {
510 menu_item = gtk_menu_item_new_with_mnemonic(submenu);
511 g_object_set_data_full(G_OBJECT(menu_item), "s_name", g_strdup(submenu), g_free);
512 gtk_widget_show(menu_item);
513 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);
514 new_menu = gtk_menu_new();
515 gtk_widget_show(new_menu);
516 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), new_menu);
518 new_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(menu_item));
520 new_menu = create_submenus(new_menu, end);
524 return new_menu ? new_menu : menu;
527 static void action_update_menu(GtkUIManager *ui_manager,
528 const gchar *accel_group,
530 gpointer callback, gpointer data)
533 gchar *action, *action_p;
534 int callback_action = 0;
535 GtkWidget *menu = gtk_menu_new();
538 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
539 GtkWidget *cur_menu = menu;
540 const gchar *action_name = NULL;
541 action = g_strdup((gchar *)cur->data);
542 action_p = strstr(action, ": ");
543 if (action_p && action_p[2] &&
544 (action_get_type(&action_p[2]) != ACTION_ERROR) &&
545 (action[0] != '/')) {
546 gchar *accel_path = NULL;
549 if (strchr(action, '/')) {
550 cur_menu = create_submenus(cur_menu, action);
551 action_name = strrchr(action, '/')+1;
553 action_name = action;
555 gtk_menu_set_accel_group (GTK_MENU (cur_menu),
556 gtk_ui_manager_get_accel_group(ui_manager));
557 item = gtk_menu_item_new_with_label(action_name);
558 gtk_menu_shell_append(GTK_MENU_SHELL(cur_menu), item);
559 g_signal_connect(G_OBJECT(item), "activate",
560 G_CALLBACK(callback), data);
561 g_object_set_data(G_OBJECT(item), "action_num", GINT_TO_POINTER(callback_action));
562 gtk_widget_show(item);
563 accel_path = g_strconcat(accel_group,branch_path, "/", action, NULL);
564 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(item), accel_path);
572 gtk_widget_show(menu);
573 gtk_menu_item_set_submenu(GTK_MENU_ITEM(gtk_ui_manager_get_widget(ui_manager, branch_path)), menu);
576 static void compose_actions_execute_cb(GtkWidget *widget, gpointer data)
578 Compose *compose = (Compose *)data;
579 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
580 compose_actions_execute(compose, action_nb, NULL);
583 static void compose_actions_execute(Compose *compose, guint action_nb, GtkWidget *widget)
586 ActionType action_type;
588 cm_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
590 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
591 cm_return_if_fail(buf != NULL);
592 action = strstr(buf, ": ");
593 cm_return_if_fail(action != NULL);
595 /* Point to the beginning of the command-line */
598 action_type = action_get_type(action);
599 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
601 (_("The selected action cannot be used in the compose window\n"
602 "because it contains %%f, %%F, %%as or %%p."));
606 execute_actions(action, NULL, compose->text, 0, NULL,
607 compose_action_cb, compose);
610 static void mainwin_actions_execute_cb(GtkWidget *widget, gpointer data)
612 MainWindow *mainwin = (MainWindow *)data;
613 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
614 mainwin_actions_execute(mainwin, action_nb, NULL);
617 static void mainwin_actions_execute(MainWindow *mainwin, guint action_nb,
622 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
623 message_actions_execute(mainwin->messageview, action_nb, msg_list);
624 g_slist_free(msg_list);
627 static void msgview_actions_execute_cb(GtkWidget *widget, gpointer data)
629 MessageView *msgview = (MessageView *)data;
630 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
631 msgview_actions_execute(msgview, action_nb, NULL);
634 static void msgview_actions_execute(MessageView *msgview, guint action_nb,
637 GSList *msg_list = NULL;
639 if (msgview->msginfo)
640 msg_list = g_slist_append(msg_list, msgview->msginfo);
641 message_actions_execute(msgview, action_nb, msg_list);
642 g_slist_free(msg_list);
645 static void message_actions_execute(MessageView *msgview, guint action_nb,
652 GtkWidget *text = NULL;
654 ActionType action_type;
656 cm_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
658 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
660 cm_return_if_fail(buf);
661 cm_return_if_fail((action = strstr(buf, ": ")));
663 /* Point to the beginning of the command-line */
666 textview = messageview_get_current_textview(msgview);
668 text = textview->text;
669 body_pos = textview->body_pos;
671 partinfo = messageview_get_selected_mime_part(msgview);
673 /* this command will alter the message text */
674 action_type = action_get_type(action);
675 if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
676 msgview->filtered = TRUE;
678 if (action_type & ACTION_FILTERING_ACTION)
679 /* CLAWS: most of the above code is not necessary for applying
681 execute_filtering_actions(action, msg_list);
683 execute_actions(action, msg_list, text, body_pos, partinfo,
687 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
689 GSList *action_list, *p;
690 const gchar *sbegin, *send;
691 gchar *action_string;
692 SummaryView *summaryview = NULL;
693 MainWindow *mainwin = NULL;
695 if (mainwindow_get_mainwindow()) {
696 summaryview = mainwindow_get_mainwindow()->summaryview;
697 mainwin = mainwindow_get_mainwindow();
700 if (NULL == (sbegin = strstr2(action, "%as{")))
702 sbegin += sizeof "%as{" - 1;
703 if (NULL == (send = strrchr(sbegin, '}')))
705 action_string = g_strndup(sbegin, send - sbegin);
707 action_list = matcher_parser_get_action_list(action_string);
708 if (action_list == NULL) {
709 gchar *tmp = g_strdup(action_string);
713 alertpanel_error(_("There is no filtering action set"));
715 alertpanel_error(_("Invalid filtering action(s):\n%s"), tmp);
716 g_free(action_string);
720 g_free(action_string);
722 /* apply actions on each message info */
723 for (p = msglist; p && p->data; p = g_slist_next(p)) {
724 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
728 summary_lock(summaryview);
729 main_window_cursor_wait(mainwin);
730 summary_freeze(summaryview);
731 folder_item_update_freeze();
734 filtering_move_and_copy_msgs(msglist);
737 folder_item_update_thaw();
738 summary_thaw(summaryview);
739 main_window_cursor_normal(mainwin);
740 summary_unlock(summaryview);
741 summary_show(summaryview, summaryview->folder_item);
743 for (p = action_list; p; p = g_slist_next(p))
744 if (p->data) filteringaction_free(p->data);
745 g_slist_free(action_list);
749 static gboolean execute_actions(gchar *action, GSList *msg_list,
751 gint body_pos, MimeInfo *partinfo,
752 void (*callback)(void *data), void *data)
754 GSList *children_list = NULL;
758 ChildInfo *child_info;
759 ActionType action_type;
762 gchar *sel_str = NULL;
763 gchar *msg_str = NULL;
764 gchar *user_str = NULL;
765 gchar *user_hidden_str = NULL;
766 GtkTextIter start_iter, end_iter;
767 gboolean is_selection = FALSE;
769 cm_return_val_if_fail(action && *action, FALSE);
771 action_type = action_get_type(action);
773 if (action_type == ACTION_ERROR)
774 return FALSE; /* ERR: syntax error */
776 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
777 return FALSE; /* ERR: file command without selection */
779 msg_list_len = g_slist_length(msg_list);
781 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
782 if (msg_list_len > 1)
783 return FALSE; /* ERR: pipe + multiple selection */
785 return FALSE; /* ERR: pipe and no displayed text */
788 if (action_type & ACTION_SELECTION_STR) {
790 return FALSE; /* ERR: selection string but no text */
794 GtkTextBuffer *textbuf;
796 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
797 is_selection = gtk_text_buffer_get_selection_bounds
798 (textbuf, &start_iter, &end_iter);
800 gtk_text_buffer_get_iter_at_offset
801 (textbuf, &start_iter, body_pos);
802 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
804 msg_str = gtk_text_buffer_get_text
805 (textbuf, &start_iter, &end_iter, FALSE);
807 sel_str = g_strdup(msg_str);
810 if (action_type & ACTION_USER_STR) {
811 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
818 if (action_type & ACTION_USER_HIDDEN_STR) {
819 if (!(user_hidden_str =
820 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
828 if (text && (action_type & ACTION_PIPE_OUT)) {
829 GtkTextBuffer *textbuf;
830 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
831 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
834 children = g_new0(Children, 1);
836 children->action = g_strdup(action);
837 children->action_type = action_type;
838 children->msg_text = text;
839 children->is_selection = is_selection;
841 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
842 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
843 children->open_in = 1;
845 if (action_type & ACTION_SINGLE) {
848 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
849 msginfo = (MsgInfo *)cur->data;
851 is_ok = FALSE; /* ERR: msginfo missing */
854 cmd = parse_action_cmd(action, msginfo, msg_list,
856 user_hidden_str, sel_str);
858 debug_print("Action command error\n");
859 is_ok = FALSE; /* ERR: incorrect command */
862 if ((child_info = fork_child(cmd, msg_str, children))) {
863 /* Pass msginfo to catch_status () */
864 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
865 child_info->msginfo_list =
866 g_slist_append (NULL, msginfo);
867 children_list = g_slist_append(children_list,
873 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
874 user_str, user_hidden_str, sel_str);
876 if ((child_info = fork_child(cmd, msg_str, children))) {
877 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
878 child_info->msginfo_list =
879 g_slist_copy (msg_list);
880 children_list = g_slist_append(children_list,
885 is_ok = FALSE; /* ERR: incorrect command */
891 g_free(user_hidden_str);
893 if (!children_list) {
894 /* If not waiting for children, return */
895 free_children(children);
899 children->list = children_list;
900 children->nb = g_slist_length(children_list);
901 children->initial_nb = children->nb;
903 for (cur = children_list; cur; cur = cur->next) {
904 child_info = (ChildInfo *) cur->data;
905 child_info->callback = callback;
906 child_info->data = data;
907 child_info->tag_status =
908 claws_input_add(child_info->chld_status,
909 G_IO_IN | G_IO_HUP | G_IO_ERR,
910 catch_status, child_info,
914 create_io_dialog(children);
919 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
923 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
924 gchar *cmdline[4], *ret_str;
926 ChildInfo *child_info;
928 gssize by_written = 0, by_read = 0;
930 sync = !(children->action_type & ACTION_ASYNC);
932 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
933 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
936 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
938 alertpanel_error(_("Command could not be started. "
939 "Pipe creation failed.\n%s"),
941 /* Closing fd = -1 fails silently */
942 (void)close(chld_in[0]);
943 (void)close(chld_in[1]);
944 (void)close(chld_out[0]);
945 (void)close(chld_out[1]);
946 (void)close(chld_err[0]);
947 (void)close(chld_err[1]);
948 (void)close(chld_status[0]);
949 (void)close(chld_status[1]);
950 return NULL; /* Pipe error */
954 debug_print("Forking child and grandchild.\n");
955 debug_print("Executing: /bin/sh -c %s\n", cmd);
958 if (pid == 0) { /* Child */
966 if (setpgid(0, getppid()))
970 if (children->action_type &
973 ACTION_USER_HIDDEN_IN)) {
974 r |= close(fileno(stdin));
975 (void) dup (chld_in[0]);
977 r |= close(chld_in[0]);
978 r |= close(chld_in[1]);
980 r |= close(fileno(stdout));
981 (void) dup (chld_out[1]);
982 r |= close(chld_out[0]);
983 r |= close(chld_out[1]);
985 r |= close(fileno(stderr));
986 (void) dup (chld_err[1]);
987 r |= close(chld_err[0]);
988 r |= close(chld_err[1]);
991 debug_print("%s(%d)", strerror(errno), errno);
996 ret_str = g_locale_from_utf8(cmd, strlen(cmd),
997 &by_read, &by_written,
999 if (ret_str && by_written)
1000 cmdline[2] = ret_str;
1004 execvp("/bin/sh", cmdline);
1009 } else if (gch_pid < (pid_t) 0) { /* Fork error */
1011 r = write(chld_status[1], "1\n", 2);
1013 debug_print("%s(%d)", strerror(errno), errno);
1016 } else { /* Child */
1018 r |= close(chld_in[0]);
1019 r |= close(chld_in[1]);
1020 r |= close(chld_out[0]);
1021 r |= close(chld_out[1]);
1022 r |= close(chld_err[0]);
1023 r |= close(chld_err[1]);
1024 r |= close(chld_status[0]);
1026 debug_print("Child: waiting for grandchild\n");
1027 r |= waitpid(gch_pid, NULL, 0);
1028 debug_print("Child: grandchild ended\n");
1029 r |= write(chld_status[1], "0\n", 2);
1030 r |= close(chld_status[1]);
1033 debug_print("%s(%d)", strerror(errno), errno);
1037 } else if (pid < 0) { /* Fork error */
1038 alertpanel_error(_("Could not fork to execute the following "
1039 "command:\n%s\n%s"),
1040 cmd, g_strerror(errno));
1047 waitpid(pid, NULL, 0);
1051 (void)close(chld_in[0]);
1052 if (!(children->action_type &
1053 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
1054 (void)close(chld_in[1]);
1055 (void)close(chld_out[1]);
1056 (void)close(chld_err[1]);
1057 (void)close(chld_status[1]);
1059 child_info = g_new0(ChildInfo, 1);
1061 child_info->children = children;
1063 child_info->pid = pid;
1064 child_info->next_sig = SIGTERM;
1065 child_info->cmd = g_strdup(cmd);
1066 child_info->new_out = FALSE;
1067 child_info->output = g_string_new(NULL);
1068 child_info->chld_in =
1069 (children->action_type &
1070 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
1072 child_info->chld_out = chld_out[0];
1073 child_info->chld_err = chld_err[0];
1074 child_info->chld_status = chld_status[0];
1075 child_info->tag_in = -1;
1076 child_info->tag_out = claws_input_add(chld_out[0], G_IO_IN | G_IO_HUP | G_IO_ERR,
1077 catch_output, child_info, FALSE);
1078 child_info->tag_err = claws_input_add(chld_err[0], G_IO_IN | G_IO_HUP | G_IO_ERR,
1079 catch_output, child_info, FALSE);
1081 if (!(children->action_type &
1082 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
1085 if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
1087 ret_str = g_locale_from_utf8(msg_str, strlen(msg_str),
1088 &by_read, &by_written, NULL);
1089 if (ret_str && by_written) {
1090 r = write(chld_in[1], ret_str, strlen(ret_str));
1093 r = write(chld_in[1], msg_str, strlen(msg_str));
1094 if (!(children->action_type &
1095 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
1096 r = close(chld_in[1]);
1097 child_info->chld_in = -1; /* No more input */
1099 debug_print("%s(%d)", strerror(errno), errno);
1105 #endif /* G_OS_UNIX */
1108 static void kill_children_cb(GtkWidget *widget, gpointer data)
1112 Children *children = (Children *) data;
1113 ChildInfo *child_info;
1115 for (cur = children->list; cur; cur = cur->next) {
1116 child_info = (ChildInfo *)(cur->data);
1117 debug_print("Killing child group id %d\n", child_info->pid);
1118 if (child_info->pid && kill(-child_info->pid, child_info->next_sig) < 0)
1120 child_info->next_sig = SIGKILL;
1122 #endif /* G_OS_UNIX */
1125 static gint wait_for_children(Children *children)
1127 gboolean new_output;
1128 ChildInfo *child_info;
1131 cur = children->list;
1134 child_info = (ChildInfo *)cur->data;
1135 new_output |= child_info->new_out;
1139 children->output |= new_output;
1141 if (new_output || (children->dialog && (children->initial_nb != children->nb)))
1142 update_io_dialog(children);
1147 if (!children->dialog) {
1148 free_children(children);
1149 } else if (!children->output) {
1150 gtk_widget_destroy(children->dialog);
1156 static void send_input(GtkWidget *w, gpointer data)
1158 Children *children = (Children *) data;
1159 ChildInfo *child_info = (ChildInfo *) children->list->data;
1161 child_info->tag_in = claws_input_add(child_info->chld_in,
1162 G_IO_OUT | G_IO_ERR,
1163 catch_input, children, FALSE);
1166 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1168 hide_io_dialog_cb(w, data);
1172 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1175 Children *children = (Children *)data;
1177 if (!children->nb) {
1178 g_signal_handlers_disconnect_matched
1179 (G_OBJECT(children->dialog), G_SIGNAL_MATCH_DATA,
1180 0, 0, NULL, NULL, children);
1181 gtk_widget_destroy(children->dialog);
1182 free_children(children);
1186 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1189 if (event && (event->keyval == GDK_KEY_Escape ||
1190 event->keyval == GDK_KEY_Return ||
1191 event->keyval == GDK_KEY_KP_Enter))
1192 hide_io_dialog_cb(widget, data);
1196 static void childinfo_close_pipes(ChildInfo *child_info)
1198 /* stdout and stderr pipes are guaranteed to be removed by
1199 * their handler, but in case where we receive child exit notification
1200 * before grand-child's pipes closing signals, we check them and close
1203 if (child_info->tag_in > 0)
1204 g_source_remove(child_info->tag_in);
1205 if (child_info->tag_out > 0)
1206 g_source_remove(child_info->tag_out);
1207 if (child_info->tag_err > 0)
1208 g_source_remove(child_info->tag_err);
1210 if (child_info->chld_in >= 0)
1211 (void)close(child_info->chld_in);
1212 if (child_info->chld_out >= 0)
1213 (void)close(child_info->chld_out);
1214 if (child_info->chld_err >= 0)
1215 (void)close(child_info->chld_err);
1217 (void)close(child_info->chld_status);
1220 static void free_children(Children *children)
1222 ChildInfo *child_info;
1223 void (*callback)(void *data) = NULL;
1226 debug_print("Freeing children data %p\n", children);
1228 g_free(children->action);
1229 while (children->list != NULL) {
1230 child_info = (ChildInfo *)children->list->data;
1231 g_free(child_info->cmd);
1232 g_string_free(child_info->output, TRUE);
1233 children->list = g_slist_remove(children->list, child_info);
1234 callback = child_info->callback;
1235 data = child_info->data;
1245 static void update_io_dialog(Children *children)
1249 debug_print("Updating actions input/output dialog.\n");
1251 if (children->progress_bar) {
1254 /* use a more compact format */
1255 const gchar *format = "%s %d/%d";
1257 const gchar *format = "%s %d / %d";
1260 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar),
1261 (children->initial_nb == 0) ? 0 :
1262 (gdouble) (children->initial_nb - children->nb) /
1263 (gdouble) children->initial_nb);
1264 text = g_strdup_printf(format, _("Completed"),
1265 children->initial_nb - children->nb,
1266 children->initial_nb);
1267 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(children->progress_bar), text);
1271 if (!children->nb) {
1272 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1273 gtk_widget_set_sensitive(children->close_btn, TRUE);
1274 if (children->input_hbox)
1275 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1276 gtk_widget_grab_focus(children->close_btn);
1277 g_signal_connect(G_OBJECT(children->dialog),
1279 G_CALLBACK(io_dialog_key_pressed_cb),
1283 if (children->output) {
1284 GtkWidget *text = children->text;
1285 GtkTextBuffer *textbuf;
1286 GtkTextIter iter, start_iter, end_iter;
1288 ChildInfo *child_info;
1290 gtk_widget_show(children->scrolledwin);
1291 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
1292 gtk_text_buffer_get_bounds(textbuf, &start_iter, &end_iter);
1293 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
1294 gtk_text_buffer_get_start_iter(textbuf, &iter);
1296 for (cur = children->list; cur; cur = cur->next) {
1297 child_info = (ChildInfo *)cur->data;
1298 if (child_info->pid)
1299 caption = g_strdup_printf
1300 (_("--- Running: %s\n"),
1303 caption = g_strdup_printf
1304 (_("--- Ended: %s\n"),
1307 gtk_text_buffer_insert(textbuf, &iter, caption, -1);
1308 gtk_text_buffer_insert(textbuf, &iter,
1309 child_info->output->str, -1);
1311 child_info->new_out = FALSE;
1316 static void create_io_dialog(Children *children)
1320 GtkWidget *entry = NULL;
1321 GtkWidget *input_hbox = NULL;
1322 GtkWidget *send_button;
1325 GtkWidget *scrolledwin;
1327 GtkWidget *progress_bar = NULL;
1328 GtkWidget *abort_button;
1329 GtkWidget *close_button;
1331 debug_print("Creating action IO dialog\n");
1333 dialog = gtk_dialog_new();
1334 gtk_container_set_border_width
1335 (GTK_CONTAINER(gtk_dialog_get_action_area(GTK_DIALOG(dialog))), 5);
1336 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1337 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1338 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1339 manage_window_set_transient(GTK_WINDOW(dialog));
1340 g_signal_connect(G_OBJECT(dialog), "delete_event",
1341 G_CALLBACK(delete_io_dialog_cb), children);
1342 g_signal_connect(G_OBJECT(dialog), "destroy",
1343 G_CALLBACK(hide_io_dialog_cb),
1346 vbox = gtk_vbox_new(FALSE, 8);
1347 gtk_container_add(GTK_CONTAINER(
1348 gtk_dialog_get_content_area(GTK_DIALOG(dialog))), vbox);
1349 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1350 gtk_widget_show(vbox);
1352 label = gtk_label_new(children->action);
1353 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1354 gtk_widget_show(label);
1356 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1357 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1358 GTK_POLICY_AUTOMATIC,
1359 GTK_POLICY_AUTOMATIC);
1360 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
1362 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1363 gtk_widget_set_size_request(scrolledwin, 560, 200);
1364 gtk_widget_hide(scrolledwin);
1366 text = gtk_text_view_new();
1368 if (prefs_common.textfont) {
1369 PangoFontDescription *font_desc;
1370 font_desc = pango_font_description_from_string
1371 (prefs_common.textfont);
1373 gtk_widget_modify_font(text, font_desc);
1374 pango_font_description_free(font_desc);
1378 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
1379 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
1380 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
1381 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
1382 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1383 gtk_widget_show(text);
1385 if (children->open_in) {
1386 input_hbox = gtk_hbox_new(FALSE, 8);
1387 gtk_widget_show(input_hbox);
1389 entry = gtk_entry_new();
1390 gtk_widget_set_size_request(entry, 320, -1);
1391 g_signal_connect(G_OBJECT(entry), "activate",
1392 G_CALLBACK(send_input), children);
1393 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1394 if (children->action_type & ACTION_USER_HIDDEN_IN) {
1395 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1397 hildon_gtk_entry_set_input_mode(GTK_ENTRY(entry),
1398 HILDON_GTK_INPUT_MODE_FULL |
1399 HILDON_GTK_INPUT_MODE_INVISIBLE);
1402 gtk_widget_show(entry);
1404 send_button = gtk_button_new_from_stock(GTK_STOCK_EXECUTE);
1405 g_signal_connect(G_OBJECT(send_button), "clicked",
1406 G_CALLBACK(send_input), children);
1407 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1409 gtk_widget_show(send_button);
1411 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1412 gtk_widget_grab_focus(entry);
1415 if (children->initial_nb > 1) {
1418 /* use a more compact format */
1419 const gchar *format = "%s 0/%d\n";
1421 const gchar *format = "%s 0 / %d\n";
1424 progress_bar = gtk_progress_bar_new();
1425 #if !GTK_CHECK_VERSION(3, 0, 0)
1426 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1427 GTK_PROGRESS_LEFT_TO_RIGHT);
1429 gtk_orientable_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1430 GTK_ORIENTATION_HORIZONTAL);
1431 gtk_progress_bar_set_inverted(GTK_PROGRESS_BAR(progress_bar),
1434 text = g_strdup_printf(format, _("Completed"),
1435 children->initial_nb);
1436 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar),
1439 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1440 gtk_widget_show(progress_bar);
1443 gtkut_stock_button_set_create(&hbox, &abort_button, GTK_STOCK_STOP,
1444 &close_button, GTK_STOCK_CLOSE, NULL, NULL);
1445 g_signal_connect(G_OBJECT(abort_button), "clicked",
1446 G_CALLBACK(kill_children_cb), children);
1447 g_signal_connect(G_OBJECT(close_button), "clicked",
1448 G_CALLBACK(hide_io_dialog_cb), children);
1449 gtk_widget_show(hbox);
1452 gtk_widget_set_sensitive(close_button, FALSE);
1454 gtk_container_add(GTK_CONTAINER(
1455 gtk_dialog_get_action_area(GTK_DIALOG(dialog))), hbox);
1457 children->dialog = dialog;
1458 children->scrolledwin = scrolledwin;
1459 children->text = text;
1460 children->input_hbox = children->open_in ? input_hbox : NULL;
1461 children->input_entry = children->open_in ? entry : NULL;
1462 children->progress_bar = progress_bar;
1463 children->abort_btn = abort_button;
1464 children->close_btn = close_button;
1466 gtk_widget_show(dialog);
1469 static void catch_status(gpointer data, gint source, GIOCondition cond)
1471 ChildInfo *child_info = (ChildInfo *)data;
1475 g_source_remove(child_info->tag_status);
1477 c = read(source, &buf, 1);
1478 debug_print("Child returned %c\n", buf);
1481 waitpid(-child_info->pid, NULL, 0);
1483 childinfo_close_pipes(child_info);
1484 child_info->pid = 0;
1486 if (child_info->children->action_type & (ACTION_SINGLE | ACTION_MULTIPLE)
1487 && child_info->msginfo_list) {
1488 /* Actions on message *files* might change size and
1489 * time stamp, and thus invalidate the cache */
1490 SummaryView *summaryview = NULL;
1492 MsgInfo *msginfo, *nmi; /* newmsginfo */
1494 gboolean modified_something = FALSE;
1495 FolderItem *last_item = NULL;
1496 if (mainwindow_get_mainwindow ())
1497 summaryview = mainwindow_get_mainwindow ()->summaryview;
1498 for (cur = child_info->msginfo_list; cur; cur = cur->next) {
1499 msginfo = (MsgInfo *)cur->data;
1500 if (!(msginfo && /* Stuff used valid? */
1501 msginfo->folder && msginfo->folder->cache))
1503 file = procmsg_get_message_file_path (msginfo);
1506 nmi = procheader_parse_file (file, msginfo->flags, TRUE, FALSE);
1508 continue; /* Deleted? */
1509 if (msginfo->mtime != nmi->mtime || msginfo->size != nmi->size) {
1510 nmi->folder = msginfo->folder;
1511 nmi->msgnum = msginfo->msgnum;
1512 msgcache_update_msg (msginfo->folder->cache, nmi);
1513 modified_something = TRUE;
1514 last_item = nmi->folder;
1516 procmsg_msginfo_free (nmi);
1517 if (summaryview && summaryview->displayed &&
1518 summaryview->folder_item == msginfo->folder &&
1519 summary_get_msgnum(summaryview, summaryview->displayed) == msginfo->msgnum)
1520 summary_redisplay_msg(summaryview);
1523 if (modified_something && last_item &&
1524 summaryview && summaryview->folder_item == last_item) {
1525 summary_show (summaryview, summaryview->folder_item);
1527 g_slist_free (child_info->msginfo_list);
1528 child_info->msginfo_list = NULL;
1531 if (!child_info->pid)
1532 child_info->children->nb--;
1534 wait_for_children(child_info->children);
1537 static void catch_input(gpointer data, gint source, GIOCondition cond)
1539 Children *children = (Children *)data;
1540 ChildInfo *child_info = (ChildInfo *)children->list->data;
1541 gchar *input, *ret_str;
1542 gint c, count, len, r;
1543 gssize by_read = 0, by_written = 0;
1545 debug_print("Sending input to grand child.\n");
1546 if (!(cond & (G_IO_OUT | G_IO_ERR)))
1549 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1550 gtk_widget_grab_focus(children->abort_btn);
1552 g_source_remove(child_info->tag_in);
1553 child_info->tag_in = -1;
1555 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1557 ret_str = g_locale_from_utf8(input, strlen(input), &by_read,
1559 if (ret_str && by_written) {
1564 len = strlen(input);
1568 c = write(child_info->chld_in, input + count, len - count);
1571 } while (c >= 0 && count < len);
1574 r = write(child_info->chld_in, "\n", 2);
1578 r = close(child_info->chld_in);
1580 debug_print("%s(%d)", strerror(errno), errno);
1581 child_info->chld_in = -1;
1582 debug_print("Input to grand child sent.\n");
1585 static void catch_output(gpointer data, gint source, GIOCondition cond)
1587 ChildInfo *child_info = (ChildInfo *)data;
1589 gchar buf[BUFFSIZE];
1591 debug_print("Catching grand child's output.\n");
1592 if (child_info->children->action_type &
1593 (ACTION_PIPE_OUT | ACTION_INSERT)
1594 && source == child_info->chld_out) {
1596 GTK_TEXT_VIEW(child_info->children->msg_text);
1597 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
1602 mark = gtk_text_buffer_get_insert(textbuf);
1603 gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1604 ins_pos = gtk_text_iter_get_offset(&iter);
1607 gsize bytes_read = 0, bytes_written = 0;
1610 c = read(source, buf, sizeof(buf) - 1);
1614 ret_str = g_locale_to_utf8
1615 (buf, c, &bytes_read, &bytes_written, NULL);
1616 if (ret_str && bytes_written > 0) {
1617 gtk_text_buffer_insert
1618 (textbuf, &iter, ret_str,
1622 gtk_text_buffer_insert(textbuf, &iter, buf, c);
1625 if (child_info->children->is_selection) {
1628 gtk_text_buffer_get_iter_at_offset
1629 (textbuf, &ins, ins_pos);
1630 gtk_text_buffer_select_range(textbuf, &ins, &iter);
1633 c = read(source, buf, sizeof(buf) - 1);
1635 gsize bytes_read = 0, bytes_written = 0;
1638 ret_str = g_locale_to_utf8
1639 (buf, c, &bytes_read, &bytes_written, NULL);
1640 if (ret_str && bytes_written > 0) {
1642 (child_info->output, ret_str,
1646 g_string_append_len(child_info->output, buf, c);
1648 child_info->new_out = TRUE;
1652 if (source == child_info->chld_out) {
1653 g_source_remove(child_info->tag_out);
1654 child_info->tag_out = -1;
1655 (void)close(child_info->chld_out);
1656 child_info->chld_out = -1;
1658 g_source_remove(child_info->tag_err);
1659 child_info->tag_err = -1;
1660 (void)close(child_info->chld_err);
1661 child_info->chld_err = -1;
1665 wait_for_children(child_info->children);
1668 static gchar *get_user_string(const gchar *action, ActionType type)
1671 gchar *user_str = NULL;
1674 case ACTION_USER_HIDDEN_STR:
1675 message = g_strdup_printf
1676 (_("Enter the argument for the following action:\n"
1677 "('%%h' will be replaced with the argument)\n"
1680 user_str = input_dialog_with_invisible
1681 (_("Action's hidden user argument"), message, NULL);
1683 case ACTION_USER_STR:
1684 message = g_strdup_printf
1685 (_("Enter the argument for the following action:\n"
1686 "('%%u' will be replaced with the argument)\n"
1689 user_str = input_dialog
1690 (_("Action's user argument"), message, NULL);
1693 g_warning("Unsupported action type %d", type);