Sync Spanish manual
[claws.git] / src / action.c
blob0c459aab6527ba5a818b7d927f77f1839589a2e9
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2021 the Claws Mail Team and Hiroyuki Yamamoto
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/>.
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #include "claws-features.h"
22 #endif
24 #include "defs.h"
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gtk/gtk.h>
29 #include <gdk/gdkkeysyms.h>
30 #ifdef GDK_WINDOWING_X11
31 # include <gdk/gdkx.h>
32 #endif /* GDK_WINDOWING_X11 */
33 #ifdef GDK_WINDOWING_QUARTZ
34 # include <gdk/gdkquartz.h>
35 #endif /* GDK_WINDOWING_QUARTZ */
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <sys/types.h>
41 #if HAVE_SYS_WAIT_H
42 # include <sys/wait.h>
43 #endif
44 #include <signal.h>
45 #include <unistd.h>
47 #include "utils.h"
48 #include "gtkutils.h"
49 #include "manage_window.h"
50 #include "mainwindow.h"
51 #include "prefs_common.h"
52 #include "alertpanel.h"
53 #include "inputdialog.h"
54 #include "action.h"
55 #include "compose.h"
56 #include "procmsg.h"
57 #include "msgcache.h"
58 #include "textview.h"
59 #include "matcher_parser.h" /* CLAWS */
60 #include "filtering.h"
61 #include "procheader.h"
63 #ifdef G_OS_WIN32
64 #include <windows.h>
65 #endif
67 typedef struct _Children Children;
68 typedef struct _ChildInfo ChildInfo;
69 typedef struct _UserStringDialog UserStringDialog;
71 struct _Children
73 GtkWidget *dialog;
74 GtkWidget *text;
75 GtkWidget *input_entry;
76 GtkWidget *input_hbox;
77 GtkWidget *progress_bar;
78 GtkWidget *abort_btn;
79 GtkWidget *close_btn;
80 GtkWidget *scrolledwin;
82 gchar *action;
83 ActionType action_type;
84 GSList *list;
85 gint nb;
86 gint initial_nb;
87 gint open_in;
88 gboolean output;
90 GtkWidget *msg_text;
92 gboolean is_selection;
95 struct _ChildInfo
97 Children *children;
98 gchar *cmd;
99 GPid pid;
100 gint next_sig;
101 gint chld_in;
102 gint chld_out;
103 gint chld_err;
104 gint tag_in;
105 gint tag_out;
106 gint tag_err;
107 gint tag_status;
108 gint new_out;
110 GString *output;
111 void (*callback)(void *data);
112 void *data;
114 GSList *msginfo_list;
117 static void action_update_menu (GtkUIManager *ui_manager,
118 const gchar *accel_group,
119 gchar *branch_path,
120 gpointer callback,
121 gpointer data);
122 static void compose_actions_execute_cb (GtkWidget *widget,
123 gpointer data);
124 static void compose_actions_execute (Compose *compose,
125 guint action_nb,
126 GtkWidget *widget);
128 static void mainwin_actions_execute_cb (GtkWidget *widget,
129 gpointer data);
130 static void mainwin_actions_execute (MainWindow *mainwin,
131 guint action_nb,
132 GtkWidget *widget);
134 static void msgview_actions_execute_cb (GtkWidget *widget,
135 gpointer data);
136 static void msgview_actions_execute (MessageView *msgview,
137 guint action_nb,
138 GtkWidget *widget);
140 static void message_actions_execute (MessageView *msgview,
141 guint action_nb,
142 GSList *msg_list);
144 static gboolean execute_filtering_actions(gchar *action,
145 GSList *msglist);
147 static gboolean execute_actions (gchar *action,
148 GSList *msg_list,
149 GtkWidget *text,
150 gint body_pos,
151 MimeInfo *partinfo,
152 void (*callback)(void *data),
153 void *data);
155 static gchar *parse_action_cmd (gchar *action,
156 MsgInfo *msginfo,
157 GSList *msg_list,
158 MimeInfo *partinfo,
159 const gchar *user_str,
160 const gchar *user_hidden_str,
161 const gchar *sel_str);
162 static gboolean parse_append_filename (GString *cmd,
163 MsgInfo *msginfo);
165 static gboolean parse_append_msgpart (GString *cmd,
166 MsgInfo *msginfo,
167 MimeInfo *partinfo);
169 static ChildInfo *fork_child (gchar *cmd,
170 const gchar *msg_str,
171 Children *children);
173 static gint wait_for_children (Children *children);
175 static void free_children (Children *children);
177 static void childinfo_close_pipes (ChildInfo *child_info);
179 static void create_io_dialog (Children *children);
180 static void update_io_dialog (Children *children);
182 static void hide_io_dialog_cb (GtkWidget *widget,
183 gpointer data);
184 static gint io_dialog_key_pressed_cb (GtkWidget *widget,
185 GdkEventKey *event,
186 gpointer data);
188 static void catch_output (gpointer data,
189 gint source,
190 GIOCondition cond);
191 static void catch_input (gpointer data,
192 gint source,
193 GIOCondition cond);
194 static void catch_status (GPid pid, gint status, gpointer data);
196 static gchar *get_user_string (const gchar *action,
197 ActionType type);
200 ActionType action_get_type(const gchar *action_str)
202 const gchar *p;
203 gboolean in_filtering_action = FALSE;
204 ActionType action_type = ACTION_NONE;
206 cm_return_val_if_fail(action_str, ACTION_ERROR);
207 cm_return_val_if_fail(*action_str, ACTION_ERROR);
209 p = action_str;
211 if (p[0] == '|') {
212 action_type |= ACTION_PIPE_IN;
213 p++;
214 } else if (p[0] == '>') {
215 action_type |= ACTION_USER_IN;
216 p++;
217 } else if (p[0] == '*') {
218 action_type |= ACTION_USER_HIDDEN_IN;
219 p++;
222 if (p[0] == '\0')
223 return ACTION_ERROR;
225 while (*p && action_type != ACTION_ERROR) {
226 if (!in_filtering_action) {
227 if (p[0] == '%' && p[1]) {
228 switch (p[1]) {
229 case 'a':
230 /* CLAWS: filtering action is a mutually exclusive
231 * action. we can enable others if needed later. we
232 * add ACTION_SINGLE | ACTION_MULTIPLE so it will
233 * only be executed from the main window toolbar */
234 if (p[2] == 's') /* source messages */
235 action_type = ACTION_FILTERING_ACTION
236 | ACTION_SINGLE
237 | ACTION_MULTIPLE;
238 in_filtering_action = TRUE;
239 break;
240 case 'f':
241 action_type |= ACTION_SINGLE;
242 break;
243 case 'F':
244 action_type |= ACTION_MULTIPLE;
245 break;
246 case 'p':
247 action_type |= ACTION_SINGLE;
248 break;
249 case 's':
250 action_type |= ACTION_SELECTION_STR;
251 break;
252 case 'u':
253 action_type |= ACTION_USER_STR;
254 break;
255 case 'h':
256 action_type |= ACTION_USER_HIDDEN_STR;
257 break;
258 case '%':
259 /* literal '%' */
260 break;
261 default:
262 action_type = ACTION_ERROR;
263 break;
265 p++;
266 } else if (p[0] == '|') {
267 if (p[1] == '\0')
268 action_type |= ACTION_PIPE_OUT;
269 } else if (p[0] == '>') {
270 if (p[1] == '\0')
271 action_type |= ACTION_INSERT;
272 } else if (p[0] == '&') {
273 if (p[1] == '\0')
274 action_type |= ACTION_ASYNC;
275 } else if (p[0] == '}') {
276 in_filtering_action = FALSE;
279 p++;
282 return action_type;
285 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
286 GSList *msg_list, MimeInfo *partinfo,
287 const gchar *user_str,
288 const gchar *user_hidden_str,
289 const gchar *sel_str)
291 GString *cmd;
292 gchar *p;
293 GSList *cur;
295 p = action;
297 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
298 p++;
300 cmd = g_string_sized_new(strlen(action));
302 while (p[0] &&
303 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
304 if (p[0] == '%' && p[1]) {
305 switch (p[1]) {
306 case 'f':
307 if (!parse_append_filename(cmd, msginfo)) {
308 g_string_free(cmd, TRUE);
309 return NULL;
311 p++;
312 break;
313 case 'F':
314 for (cur = msg_list; cur != NULL;
315 cur = cur->next) {
316 MsgInfo *msg = (MsgInfo *)cur->data;
318 if (!parse_append_filename(cmd, msg)) {
319 g_string_free(cmd, TRUE);
320 return NULL;
322 if (cur->next)
323 g_string_append_c(cmd, ' ');
325 p++;
326 break;
327 case 'p':
328 if (!parse_append_msgpart(cmd, msginfo,
329 partinfo)) {
330 g_string_free(cmd, TRUE);
331 return NULL;
333 p++;
334 break;
335 case 's':
336 if (sel_str)
337 g_string_append(cmd, sel_str);
338 p++;
339 break;
340 case 'u':
341 if (user_str)
342 g_string_append(cmd, user_str);
343 p++;
344 break;
345 case 'h':
346 if (user_hidden_str)
347 g_string_append(cmd, user_hidden_str);
348 p++;
349 break;
350 case '%':
351 g_string_append_c(cmd, p[1]);
352 p++;
353 break;
354 default:
355 g_string_append_c(cmd, p[0]);
356 g_string_append_c(cmd, p[1]);
357 p++;
359 } else {
360 g_string_append_c(cmd, p[0]);
362 p++;
364 if (cmd->len == 0) {
365 g_string_free(cmd, TRUE);
366 return NULL;
369 return g_string_free(cmd, FALSE);
372 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
374 gchar *filename;
376 cm_return_val_if_fail(msginfo, FALSE);
378 filename = procmsg_get_message_file(msginfo);
380 if (!filename) {
381 alertpanel_error(_("Could not get message file %d"),
382 msginfo->msgnum);
383 return FALSE;
386 g_string_append(cmd, "\"");
387 #ifdef G_OS_UNIX
388 gchar *p = filename, *q;
389 gchar escape_ch[] = "\\ ";
390 while ((q = strpbrk(p, "$\"`\\~")) != NULL) {
391 escape_ch[1] = *q;
392 *q = '\0';
393 g_string_append(cmd, p);
394 g_string_append(cmd, escape_ch);
395 p = q + 1;
398 g_string_append(cmd, p);
399 #else
400 g_string_append(cmd, filename);
401 #endif
402 g_string_append(cmd, "\"");
403 g_free(filename);
405 return TRUE;
408 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
409 MimeInfo *partinfo)
411 gboolean single_part = FALSE;
412 gchar *filename;
413 gchar *part_filename;
414 gint ret;
416 if (!partinfo) {
417 partinfo = procmime_scan_message(msginfo);
418 if (!partinfo) {
419 alertpanel_error(_("Could not get message part."));
420 return FALSE;
423 single_part = TRUE;
426 filename = procmsg_get_message_file_path(msginfo);
427 part_filename = procmime_get_tmp_file_name(partinfo);
429 ret = procmime_get_part(part_filename, partinfo);
431 if (single_part)
432 procmime_mimeinfo_free_all(&partinfo);
433 g_free(filename);
435 if (ret < 0) {
436 alertpanel_error(_("Can't get part of multipart message: %s"), g_strerror(-ret));
437 g_free(part_filename);
438 return FALSE;
441 g_string_append(cmd, part_filename);
443 g_free(part_filename);
445 return TRUE;
448 void actions_execute(gpointer data,
449 guint action_nb,
450 GtkWidget *widget,
451 gint source)
453 if (source == TOOLBAR_MAIN)
454 mainwin_actions_execute((MainWindow*)data, action_nb, widget);
455 else if (source == TOOLBAR_COMPOSE)
456 compose_actions_execute((Compose*)data, action_nb, widget);
457 else if (source == TOOLBAR_MSGVIEW)
458 msgview_actions_execute((MessageView*)data, action_nb, widget);
461 void action_update_mainwin_menu(GtkUIManager *ui_manager,
462 gchar *branch_path,
463 MainWindow *mainwin)
465 action_update_menu(ui_manager, "<MainwinActions>", branch_path,
466 mainwin_actions_execute_cb, mainwin);
469 void action_update_msgview_menu(GtkUIManager *ui_manager,
470 gchar *branch_path,
471 MessageView *msgview)
473 action_update_menu(ui_manager, "<MsgviewActions>", branch_path,
474 msgview_actions_execute_cb, msgview);
477 void action_update_compose_menu(GtkUIManager *ui_manager,
478 gchar *branch_path,
479 Compose *compose)
481 action_update_menu(ui_manager, "<ComposeActions>", branch_path,
482 compose_actions_execute_cb, compose);
485 static GtkWidget *find_item_in_menu(GtkWidget *menu, gchar *name)
487 GList *children = gtk_container_get_children(GTK_CONTAINER(GTK_MENU_SHELL(menu)));
488 GList *amenu = children;
489 const gchar *existing_name;
490 while (amenu) {
491 GtkWidget *item = GTK_WIDGET(amenu->data);
492 if ((existing_name = g_object_get_data(G_OBJECT(item), "s_name")) != NULL &&
493 !g_strcmp0(name, existing_name))
495 g_list_free(children);
496 return item;
498 amenu = amenu->next;
501 g_list_free(children);
503 return NULL;
506 static GtkWidget *create_submenus(GtkWidget *menu, const gchar *action)
508 gchar *submenu = g_strdup(action);
509 GtkWidget *new_menu = NULL;
511 if (strchr(submenu, '/')) {
512 const gchar *end = (strchr(submenu, '/')+1);
513 GtkWidget *menu_item = NULL;
514 if (end && *end) {
515 *strchr(submenu, '/') = '\0';
516 if ((menu_item = find_item_in_menu(menu, submenu)) == NULL) {
517 menu_item = gtk_menu_item_new_with_mnemonic(submenu);
518 g_object_set_data_full(G_OBJECT(menu_item), "s_name", g_strdup(submenu), g_free);
519 gtk_widget_show(menu_item);
520 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);
521 new_menu = gtk_menu_new();
522 gtk_widget_show(new_menu);
523 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), new_menu);
524 } else {
525 new_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(menu_item));
527 new_menu = create_submenus(new_menu, end);
530 g_free(submenu);
531 return new_menu ? new_menu : menu;
534 static void action_update_menu(GtkUIManager *ui_manager,
535 const gchar *accel_group,
536 gchar *branch_path,
537 gpointer callback, gpointer data)
539 GSList *cur;
540 gchar *action, *action_p;
541 int callback_action = 0;
542 GtkWidget *menu = gtk_menu_new();
543 GtkWidget *item;
545 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
546 GtkWidget *cur_menu = menu;
547 const gchar *action_name = NULL;
548 action = g_strdup((gchar *)cur->data);
549 action_p = strstr(action, ": ");
550 if (action_p && action_p[2] &&
551 (action_get_type(&action_p[2]) != ACTION_ERROR) &&
552 (action[0] != '/')) {
553 gchar *accel_path = NULL;
555 action_p[0] = '\0';
556 if (strchr(action, '/')) {
557 cur_menu = create_submenus(cur_menu, action);
558 action_name = strrchr(action, '/')+1;
559 } else {
560 action_name = action;
562 gtk_menu_set_accel_group (GTK_MENU (cur_menu),
563 gtk_ui_manager_get_accel_group(ui_manager));
564 item = gtk_menu_item_new_with_label(action_name);
565 gtk_menu_shell_append(GTK_MENU_SHELL(cur_menu), item);
566 g_signal_connect(G_OBJECT(item), "activate",
567 G_CALLBACK(callback), data);
568 g_object_set_data(G_OBJECT(item), "action_num", GINT_TO_POINTER(callback_action));
569 gtk_widget_show(item);
570 accel_path = g_strconcat(accel_group,branch_path, "/", action, NULL);
571 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(item), accel_path);
572 g_free(accel_path);
575 g_free(action);
576 callback_action++;
579 gtk_widget_show(menu);
580 gtk_menu_item_set_submenu(GTK_MENU_ITEM(gtk_ui_manager_get_widget(ui_manager, branch_path)), menu);
583 static void compose_actions_execute_cb(GtkWidget *widget, gpointer data)
585 Compose *compose = (Compose *)data;
586 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
587 compose_actions_execute(compose, action_nb, NULL);
590 static void compose_actions_execute(Compose *compose, guint action_nb, GtkWidget *widget)
592 gchar *buf, *action;
593 ActionType action_type;
595 cm_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
597 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
598 cm_return_if_fail(buf != NULL);
599 action = strstr(buf, ": ");
600 cm_return_if_fail(action != NULL);
602 /* Point to the beginning of the command-line */
603 action += 2;
605 action_type = action_get_type(action);
606 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
607 alertpanel_warning
608 (_("The selected action cannot be used in the compose window\n"
609 "because it contains %%f, %%F, %%as or %%p."));
610 return;
613 execute_actions(action, NULL, compose->text, 0, NULL,
614 compose_action_cb, compose);
617 static void mainwin_actions_execute_cb(GtkWidget *widget, gpointer data)
619 MainWindow *mainwin = (MainWindow *)data;
620 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
621 mainwin_actions_execute(mainwin, action_nb, NULL);
624 static void _free_msginfos(gpointer data, gpointer user_data)
626 MsgInfo *msginfo = (MsgInfo *)data;
628 procmsg_msginfo_free(&msginfo);
631 static void mainwin_actions_execute(MainWindow *mainwin, guint action_nb,
632 GtkWidget *widget)
634 GSList *msg_list;
636 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
637 message_actions_execute(mainwin->messageview, action_nb, msg_list);
638 summary_select_by_msg_list(mainwin->summaryview, msg_list);
639 g_slist_foreach(msg_list, _free_msginfos, NULL);
640 g_slist_free(msg_list);
643 static void msgview_actions_execute_cb(GtkWidget *widget, gpointer data)
645 MessageView *msgview = (MessageView *)data;
646 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
647 msgview_actions_execute(msgview, action_nb, NULL);
650 static void msgview_actions_execute(MessageView *msgview, guint action_nb,
651 GtkWidget *widget)
653 GSList *msg_list = NULL;
655 if (msgview->msginfo)
656 msg_list = g_slist_append(msg_list, msgview->msginfo);
657 message_actions_execute(msgview, action_nb, msg_list);
658 g_slist_free(msg_list);
661 static void message_actions_execute(MessageView *msgview, guint action_nb,
662 GSList *msg_list)
664 TextView *textview;
665 MimeInfo *partinfo;
666 gchar *buf;
667 gchar *action;
668 GtkWidget *text = NULL;
669 guint body_pos = 0;
670 ActionType action_type;
672 cm_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
674 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
676 cm_return_if_fail(buf);
677 cm_return_if_fail((action = strstr(buf, ": ")));
679 /* Point to the beginning of the command-line */
680 action += 2;
682 textview = messageview_get_current_textview(msgview);
683 if (textview) {
684 text = textview->text;
685 body_pos = textview->body_pos;
687 partinfo = messageview_get_selected_mime_part(msgview);
689 /* this command will alter the message text */
690 action_type = action_get_type(action);
691 if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
692 msgview->filtered = TRUE;
694 if (action_type & ACTION_FILTERING_ACTION)
695 /* CLAWS: most of the above code is not necessary for applying
696 * filtering */
697 execute_filtering_actions(action, msg_list);
698 else
699 execute_actions(action, msg_list, text, body_pos, partinfo,
700 NULL, NULL);
703 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
705 GSList *action_list, *p;
706 const gchar *sbegin, *send;
707 gchar *action_string;
708 SummaryView *summaryview = NULL;
709 MainWindow *mainwin = NULL;
711 if (mainwindow_get_mainwindow()) {
712 summaryview = mainwindow_get_mainwindow()->summaryview;
713 mainwin = mainwindow_get_mainwindow();
716 if (NULL == (sbegin = g_strstr_len(action, -1, "%as{")))
717 return FALSE;
718 sbegin += sizeof "%as{" - 1;
719 if (NULL == (send = strrchr(sbegin, '}')))
720 return FALSE;
721 action_string = g_strndup(sbegin, send - sbegin);
723 action_list = matcher_parser_get_action_list(action_string);
724 if (action_list == NULL) {
725 gchar *tmp = g_strdup(action_string);
727 g_strstrip(tmp);
728 if (*tmp == '\0')
729 alertpanel_error(_("There is no filtering action set"));
730 else
731 alertpanel_error(_("Invalid filtering action(s):\n%s"), tmp);
732 g_free(action_string);
733 g_free(tmp);
734 return FALSE;
736 g_free(action_string);
738 /* apply actions on each message info */
739 for (p = msglist; p && p->data; p = g_slist_next(p)) {
740 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
743 if (summaryview) {
744 summary_lock(summaryview);
745 main_window_cursor_wait(mainwin);
746 summary_freeze(summaryview);
747 folder_item_update_freeze();
750 filtering_move_and_copy_msgs(msglist);
752 if (summaryview) {
753 folder_item_update_thaw();
754 summary_thaw(summaryview);
755 main_window_cursor_normal(mainwin);
756 summary_unlock(summaryview);
757 summary_show(summaryview, summaryview->folder_item, FALSE);
759 for (p = action_list; p; p = g_slist_next(p))
760 if (p->data) filteringaction_free(p->data);
761 g_slist_free(action_list);
762 return TRUE;
765 static gboolean execute_actions(gchar *action, GSList *msg_list,
766 GtkWidget *text,
767 gint body_pos, MimeInfo *partinfo,
768 void (*callback)(void *data), void *data)
770 GSList *children_list = NULL, *cur = NULL;
771 gint is_ok = TRUE;
772 gint msg_list_len;
773 Children *children;
774 ChildInfo *child_info;
775 ActionType action_type;
776 MsgInfo *msginfo;
777 gchar *cmd;
778 gchar *sel_str = NULL;
779 gchar *msg_str = NULL;
780 gchar *user_str = NULL;
781 gchar *user_hidden_str = NULL;
782 GtkTextIter start_iter, end_iter;
783 gboolean is_selection = FALSE;
785 cm_return_val_if_fail(action && *action, FALSE);
787 action_type = action_get_type(action);
789 if (action_type == ACTION_ERROR)
790 return FALSE; /* ERR: syntax error */
792 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
793 return FALSE; /* ERR: file command without selection */
795 msg_list_len = g_slist_length(msg_list);
797 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
798 if (msg_list_len > 1)
799 return FALSE; /* ERR: pipe + multiple selection */
800 if (!text)
801 return FALSE; /* ERR: pipe and no displayed text */
804 if (action_type & ACTION_SELECTION_STR) {
805 if (!text)
806 return FALSE; /* ERR: selection string but no text */
809 if (text) {
810 GtkTextBuffer *textbuf;
812 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
813 is_selection = gtk_text_buffer_get_selection_bounds
814 (textbuf, &start_iter, &end_iter);
815 if (!is_selection) {
816 gtk_text_buffer_get_iter_at_offset
817 (textbuf, &start_iter, body_pos);
818 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
820 msg_str = gtk_text_buffer_get_text
821 (textbuf, &start_iter, &end_iter, FALSE);
822 if (is_selection)
823 sel_str = g_strdup(msg_str);
826 if (action_type & ACTION_USER_STR) {
827 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
828 g_free(msg_str);
829 g_free(sel_str);
830 return FALSE;
834 if (action_type & ACTION_USER_HIDDEN_STR) {
835 if (!(user_hidden_str =
836 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
837 g_free(msg_str);
838 g_free(sel_str);
839 g_free(user_str);
840 return FALSE;
844 if (text && (action_type & ACTION_PIPE_OUT)) {
845 GtkTextBuffer *textbuf;
846 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
847 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
850 children = g_new0(Children, 1);
852 children->nb = 0;
853 children->action = g_strdup(action);
854 children->action_type = action_type;
855 children->msg_text = text;
856 children->is_selection = is_selection;
858 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
859 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
860 children->open_in = 1;
862 /* Pre-fetch bodies, makes it easier on IMAP (see bug #3011) */
863 for (cur = msg_list; cur; cur = cur->next) {
864 gchar *dummy;
865 msginfo = (MsgInfo *)cur->data;
867 dummy = procmsg_get_message_file((MsgInfo *)cur->data);
868 if (dummy)
869 g_free(dummy);
870 else
871 is_ok = FALSE;
874 if (is_ok && (action_type & ACTION_SINGLE)) {
875 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
876 msginfo = (MsgInfo *)cur->data;
877 if (!msginfo) {
878 is_ok = FALSE; /* ERR: msginfo missing */
879 break;
881 cmd = parse_action_cmd(action, msginfo, msg_list,
882 partinfo, user_str,
883 user_hidden_str, sel_str);
884 if (!cmd) {
885 debug_print("Action command error\n");
886 is_ok = FALSE; /* ERR: incorrect command */
887 break;
889 if ((child_info = fork_child(cmd, msg_str, children))) {
890 /* Pass msginfo to catch_status () */
891 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
892 child_info->msginfo_list =
893 g_slist_append (NULL, msginfo);
894 children_list = g_slist_append(children_list,
895 child_info);
896 children->nb++;
898 g_free(cmd);
900 } else if (is_ok) {
901 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
902 user_str, user_hidden_str, sel_str);
903 if (cmd) {
904 if ((child_info = fork_child(cmd, msg_str, children))) {
905 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
906 child_info->msginfo_list =
907 g_slist_copy (msg_list);
908 children_list = g_slist_append(children_list,
909 child_info);
910 children->nb++;
912 g_free(cmd);
913 } else
914 is_ok = FALSE; /* ERR: incorrect command */
917 g_free(msg_str);
918 g_free(sel_str);
919 g_free(user_str);
920 g_free(user_hidden_str);
922 if (!children_list) {
923 /* If not waiting for children, return */
924 free_children(children);
925 } else {
926 GSList *cur;
928 children->list = children_list;
929 children->initial_nb = children->nb;
931 for (cur = children_list; cur; cur = cur->next) {
932 child_info = (ChildInfo *) cur->data;
933 child_info->callback = callback;
934 child_info->data = data;
935 child_info->tag_status =
936 g_child_watch_add(child_info->pid, catch_status, child_info);
939 create_io_dialog(children);
941 return is_ok;
944 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
945 Children *children)
947 gint chld_in, chld_out, chld_err;
948 gchar **argv, *ret_str, *trim_cmd;
949 GPid pid;
950 ChildInfo *child_info;
951 gint follow_child;
952 gssize by_written = 0, by_read = 0;
953 gboolean result = FALSE;
954 GError *error = NULL;
956 follow_child = !(children->action_type & ACTION_ASYNC);
958 chld_in = chld_out = chld_err = -1;
960 ret_str = g_locale_from_utf8(cmd, strlen(cmd),
961 &by_read, &by_written,
962 NULL);
963 if (!ret_str || !by_written) {
964 if (ret_str)
965 g_free(ret_str);
966 ret_str = g_strdup(cmd);
969 trim_cmd = ret_str;
971 while (g_ascii_isspace(trim_cmd[0]))
972 trim_cmd++;
974 #ifdef G_OS_UNIX
975 argv = g_new0(gchar *, 4);
976 argv[0] = g_strdup("/bin/sh");
977 argv[1] = g_strdup("-c");
978 argv[2] = g_strdup(trim_cmd);
979 argv[3] = 0;
980 #else
981 argv = strsplit_with_quote(trim_cmd, " ", 0);
982 #endif
983 g_free(ret_str);
985 if (follow_child) {
986 result = g_spawn_async_with_pipes(NULL, argv, NULL,
987 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
988 NULL, NULL, &pid, &chld_in, &chld_out,
989 &chld_err, &error);
990 } else {
991 result = g_spawn_async(NULL, argv, NULL,
992 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH, NULL, NULL,
993 &pid, &error);
996 debug_print("spawning %s: %d\n", cmd, result);
998 g_strfreev(argv);
1000 if (!result) {
1001 alertpanel_error(_("Could not fork to execute the following "
1002 "command:\n%s\n%s"),
1003 cmd, error ? error->message : _("Unknown error"));
1004 if (error)
1005 g_error_free(error);
1006 return NULL;
1009 if (!(children->action_type &
1010 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
1011 (void)close(chld_in);
1013 if (!follow_child) {
1014 g_spawn_close_pid(pid);
1015 return NULL;
1017 child_info = g_new0(ChildInfo, 1);
1019 child_info->children = children;
1021 child_info->pid = pid;
1022 #ifdef G_OS_UNIX
1023 child_info->next_sig = SIGTERM;
1024 #endif
1025 child_info->cmd = g_strdup(cmd);
1026 child_info->new_out = FALSE;
1027 child_info->output = g_string_new(NULL);
1028 child_info->chld_in =
1029 (children->action_type &
1030 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
1031 ? chld_in : -1;
1032 child_info->chld_out = chld_out;
1033 child_info->chld_err = chld_err;
1034 child_info->tag_status = -1;
1035 child_info->tag_in = -1;
1036 child_info->tag_out = claws_input_add(chld_out, G_IO_IN | G_IO_HUP | G_IO_ERR,
1037 catch_output, child_info, FALSE);
1038 child_info->tag_err = claws_input_add(chld_err, G_IO_IN | G_IO_HUP | G_IO_ERR,
1039 catch_output, child_info, FALSE);
1041 if (!(children->action_type &
1042 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
1043 return child_info;
1045 if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
1046 int r;
1047 ret_str = g_locale_from_utf8(msg_str, strlen(msg_str),
1048 &by_read, &by_written, NULL);
1049 if (ret_str && by_written)
1050 r = write(chld_in, ret_str, strlen(ret_str));
1051 else
1052 r = write(chld_in, msg_str, strlen(msg_str));
1053 if (ret_str)
1054 g_free(ret_str);
1056 if (!(children->action_type &
1057 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
1058 r = close(chld_in);
1059 child_info->chld_in = -1; /* No more input */
1060 if (r != 0)
1061 debug_print("piping to child process: %s (%d)\n", g_strerror(errno), errno);
1064 return child_info;
1067 static void kill_children_cb(GtkWidget *widget, gpointer data)
1069 GSList *cur;
1070 Children *children = (Children *) data;
1071 ChildInfo *child_info;
1073 for (cur = children->list; cur; cur = cur->next) {
1074 child_info = (ChildInfo *)(cur->data);
1075 #ifdef G_OS_WIN32
1076 debug_print("Killing child group HANDLE %p\n", child_info->pid);
1077 TerminateProcess(child_info->pid, 0);
1078 #else
1079 debug_print("Killing child group id %d\n", child_info->pid);
1080 if (child_info->pid && kill(child_info->pid, child_info->next_sig) < 0)
1081 perror("kill");
1082 child_info->next_sig = SIGKILL;
1083 #endif
1087 static gint wait_for_children(Children *children)
1089 gboolean new_output;
1090 ChildInfo *child_info;
1091 GSList *cur;
1093 cur = children->list;
1094 new_output = FALSE;
1095 while (cur) {
1096 child_info = (ChildInfo *)cur->data;
1097 new_output |= child_info->new_out;
1098 cur = cur->next;
1101 children->output |= new_output;
1103 if (new_output || (children->dialog && (children->initial_nb != children->nb)))
1104 update_io_dialog(children);
1106 if (children->nb)
1107 return FALSE;
1109 if (!children->dialog) {
1110 free_children(children);
1111 } else if (!children->output) {
1112 gtk_widget_destroy(children->dialog);
1115 return FALSE;
1118 static void send_input(GtkWidget *w, gpointer data)
1120 Children *children = (Children *) data;
1121 ChildInfo *child_info = (ChildInfo *) children->list->data;
1123 child_info->tag_in = claws_input_add(child_info->chld_in,
1124 G_IO_OUT | G_IO_ERR,
1125 catch_input, children, FALSE);
1128 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1130 hide_io_dialog_cb(w, data);
1131 return TRUE;
1134 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1137 Children *children = (Children *)data;
1139 if (!children->nb) {
1140 g_signal_handlers_disconnect_matched
1141 (G_OBJECT(children->dialog), G_SIGNAL_MATCH_DATA,
1142 0, 0, NULL, NULL, children);
1143 gtk_widget_destroy(children->dialog);
1144 free_children(children);
1148 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1149 gpointer data)
1151 if (event && (event->keyval == GDK_KEY_Escape ||
1152 event->keyval == GDK_KEY_Return ||
1153 event->keyval == GDK_KEY_KP_Enter))
1154 hide_io_dialog_cb(widget, data);
1155 return TRUE;
1158 static void childinfo_close_pipes(ChildInfo *child_info)
1160 /* stdout and stderr pipes are guaranteed to be removed by
1161 * their handler, but in case where we receive child exit notification
1162 * before grand-child's pipes closing signals, we check them and close
1163 * them if necessary
1165 if (child_info->tag_in > 0)
1166 g_source_remove(child_info->tag_in);
1167 if (child_info->tag_out > 0)
1168 g_source_remove(child_info->tag_out);
1169 if (child_info->tag_err > 0)
1170 g_source_remove(child_info->tag_err);
1172 if (child_info->chld_in >= 0)
1173 (void)close(child_info->chld_in);
1174 if (child_info->chld_out >= 0)
1175 (void)close(child_info->chld_out);
1176 if (child_info->chld_err >= 0)
1177 (void)close(child_info->chld_err);
1180 static void free_children(Children *children)
1182 ChildInfo *child_info;
1183 void (*callback)(void *data) = NULL;
1184 void *data = NULL;
1186 debug_print("Freeing children data %p\n", children);
1188 g_free(children->action);
1189 while (children->list != NULL) {
1190 child_info = (ChildInfo *)children->list->data;
1191 g_free(child_info->cmd);
1192 g_string_free(child_info->output, TRUE);
1193 children->list = g_slist_remove(children->list, child_info);
1194 callback = child_info->callback;
1195 data = child_info->data;
1196 g_free(child_info);
1199 if (callback)
1200 callback(data);
1202 g_free(children);
1205 static void update_io_dialog(Children *children)
1207 GSList *cur;
1209 debug_print("Updating actions input/output dialog.\n");
1211 if (children->progress_bar) {
1212 gchar *text;
1213 #ifdef GENERIC_UMPC
1214 /* use a more compact format */
1215 const gchar *format = "%s %d/%d";
1216 #else
1217 const gchar *format = "%s %d / %d";
1218 #endif
1220 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar),
1221 (children->initial_nb == 0) ? 0 :
1222 (gdouble) (children->initial_nb - children->nb) /
1223 (gdouble) children->initial_nb);
1224 text = g_strdup_printf(format, _("Completed"),
1225 children->initial_nb - children->nb,
1226 children->initial_nb);
1227 gtk_progress_bar_set_show_text(GTK_PROGRESS_BAR(children->progress_bar), TRUE);
1228 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(children->progress_bar), text);
1229 g_free(text);
1232 if (!children->nb) {
1233 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1234 gtk_widget_set_sensitive(children->close_btn, TRUE);
1235 if (children->input_hbox)
1236 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1237 gtk_widget_grab_focus(children->close_btn);
1238 g_signal_connect(G_OBJECT(children->dialog),
1239 "key_press_event",
1240 G_CALLBACK(io_dialog_key_pressed_cb),
1241 children);
1244 if (children->output) {
1245 GtkWidget *text = children->text;
1246 GtkTextBuffer *textbuf;
1247 GtkTextIter iter, start_iter, end_iter;
1248 gchar *caption;
1249 ChildInfo *child_info;
1251 gtk_widget_show(children->scrolledwin);
1252 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
1253 gtk_text_buffer_get_bounds(textbuf, &start_iter, &end_iter);
1254 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
1255 gtk_text_buffer_get_start_iter(textbuf, &iter);
1257 for (cur = children->list; cur; cur = cur->next) {
1258 child_info = (ChildInfo *)cur->data;
1259 if (child_info->pid)
1260 caption = g_strdup_printf
1261 (_("--- Running: %s\n"),
1262 child_info->cmd);
1263 else
1264 caption = g_strdup_printf
1265 (_("--- Ended: %s\n"),
1266 child_info->cmd);
1268 gtk_text_buffer_insert(textbuf, &iter, caption, -1);
1269 gtk_text_buffer_insert(textbuf, &iter,
1270 child_info->output->str, -1);
1271 g_free(caption);
1272 child_info->new_out = FALSE;
1278 *\brief Save Gtk object size to prefs dataset
1280 static void actions_io_size_allocate_cb(GtkWidget *widget,
1281 GtkAllocation *allocation)
1283 cm_return_if_fail(allocation != NULL);
1285 gtk_window_get_size(GTK_WINDOW(widget),
1286 &prefs_common.actionsiodialog_width, &prefs_common.actionsiodialog_height);
1289 static void create_io_dialog(Children *children)
1291 GtkWidget *dialog;
1292 GtkWidget *vbox;
1293 GtkWidget *entry = NULL;
1294 GtkWidget *input_hbox = NULL;
1295 GtkWidget *send_button;
1296 GtkWidget *label;
1297 GtkWidget *text;
1298 GtkWidget *scrolledwin;
1299 GtkWidget *progress_bar = NULL;
1300 GtkWidget *abort_button;
1301 GtkWidget *close_button;
1302 static GdkGeometry geometry;
1304 debug_print("Creating action IO dialog\n");
1306 dialog = gtk_dialog_new();
1307 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1308 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1309 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1310 manage_window_set_transient(GTK_WINDOW(dialog));
1311 g_signal_connect(G_OBJECT(dialog), "delete_event",
1312 G_CALLBACK(delete_io_dialog_cb), children);
1313 g_signal_connect(G_OBJECT(dialog), "destroy",
1314 G_CALLBACK(hide_io_dialog_cb),
1315 children);
1316 g_signal_connect(G_OBJECT(dialog), "size_allocate",
1317 G_CALLBACK(actions_io_size_allocate_cb), NULL);
1319 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
1320 gtk_container_add(GTK_CONTAINER(
1321 gtk_dialog_get_content_area(GTK_DIALOG(dialog))), vbox);
1322 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1323 gtk_widget_show(vbox);
1325 label = gtk_label_new(children->action);
1326 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1327 gtk_widget_show(label);
1329 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1330 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1331 GTK_POLICY_AUTOMATIC,
1332 GTK_POLICY_AUTOMATIC);
1333 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
1334 GTK_SHADOW_IN);
1335 gtk_scrolled_window_set_propagate_natural_height(GTK_SCROLLED_WINDOW(scrolledwin), TRUE);
1336 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1337 gtk_widget_hide(scrolledwin);
1339 text = gtk_text_view_new();
1341 if (prefs_common.textfont) {
1342 PangoFontDescription *font_desc;
1343 font_desc = pango_font_description_from_string
1344 (prefs_common.textfont);
1345 if (font_desc) {
1346 gtk_widget_override_font(text, font_desc);
1347 pango_font_description_free(font_desc);
1351 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
1352 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
1353 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
1354 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
1355 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1356 gtk_widget_show(text);
1358 if (children->open_in) {
1359 input_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
1360 gtk_widget_show(input_hbox);
1362 entry = gtk_entry_new();
1363 gtk_widget_set_size_request(entry, 320, -1);
1364 g_signal_connect(G_OBJECT(entry), "activate",
1365 G_CALLBACK(send_input), children);
1366 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1367 if (children->action_type & ACTION_USER_HIDDEN_IN) {
1368 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1370 gtk_widget_show(entry);
1372 send_button = gtkut_stock_button("system-run", _("_Execute"));
1373 g_signal_connect(G_OBJECT(send_button), "clicked",
1374 G_CALLBACK(send_input), children);
1375 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1376 FALSE, 0);
1377 gtk_widget_show(send_button);
1379 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1380 gtk_widget_grab_focus(entry);
1383 if (children->initial_nb > 1) {
1384 gchar *text;
1385 #ifdef GENERIC_UMPC
1386 /* use a more compact format */
1387 const gchar *format = "%s 0/%d\n";
1388 #else
1389 const gchar *format = "%s 0 / %d\n";
1390 #endif
1392 progress_bar = gtk_progress_bar_new();
1393 gtk_orientable_set_orientation(GTK_ORIENTABLE(progress_bar),
1394 GTK_ORIENTATION_HORIZONTAL);
1395 gtk_progress_bar_set_inverted(GTK_PROGRESS_BAR(progress_bar),
1396 FALSE);
1398 text = g_strdup_printf(format, _("Completed"),
1399 children->initial_nb);
1400 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar),
1401 text);
1402 g_free(text);
1403 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1404 gtk_widget_show(progress_bar);
1407 abort_button = gtk_dialog_add_button(GTK_DIALOG(dialog), _("_Stop"),
1408 GTK_RESPONSE_NONE);
1409 close_button = gtk_dialog_add_button(GTK_DIALOG(dialog),_("_Close"),
1410 GTK_RESPONSE_NONE);
1411 gtk_widget_grab_default(close_button);
1412 g_signal_connect(G_OBJECT(abort_button), "clicked",
1413 G_CALLBACK(kill_children_cb), children);
1414 g_signal_connect(G_OBJECT(close_button), "clicked",
1415 G_CALLBACK(hide_io_dialog_cb), children);
1417 if (children->nb)
1418 gtk_widget_set_sensitive(close_button, FALSE);
1420 if (!geometry.min_height) {
1421 geometry.min_width = 582;
1422 geometry.min_height = 310;
1425 gtk_window_set_geometry_hints(GTK_WINDOW(dialog), NULL, &geometry,
1426 GDK_HINT_MIN_SIZE);
1427 gtk_window_set_default_size(GTK_WINDOW(dialog), prefs_common.actionsiodialog_width,
1428 prefs_common.actionsiodialog_height);
1430 gtk_widget_show(dialog);
1432 children->dialog = dialog;
1433 children->scrolledwin = scrolledwin;
1434 children->text = text;
1435 children->input_hbox = children->open_in ? input_hbox : NULL;
1436 children->input_entry = children->open_in ? entry : NULL;
1437 children->progress_bar = progress_bar;
1438 children->abort_btn = abort_button;
1439 children->close_btn = close_button;
1442 static void catch_status(GPid pid, gint status, gpointer data)
1444 ChildInfo *child_info = (ChildInfo *)data;
1446 debug_print("Child returned %d\n", status);
1448 childinfo_close_pipes(child_info);
1449 g_spawn_close_pid(child_info->pid);
1450 child_info->pid = 0;
1452 if (child_info->children->action_type & (ACTION_SINGLE | ACTION_MULTIPLE)
1453 && child_info->msginfo_list) {
1454 /* Actions on message *files* might change size and
1455 * time stamp, and thus invalidate the cache */
1456 SummaryView *summaryview = NULL;
1457 GSList *cur;
1458 MsgInfo *msginfo, *nmi; /* newmsginfo */
1459 char *file;
1460 gboolean modified_something = FALSE;
1461 FolderItem *last_item = NULL;
1462 if (mainwindow_get_mainwindow ())
1463 summaryview = mainwindow_get_mainwindow ()->summaryview;
1464 for (cur = child_info->msginfo_list; cur; cur = cur->next) {
1465 msginfo = (MsgInfo *)cur->data;
1466 if (!(msginfo && /* Stuff used valid? */
1467 msginfo->folder && msginfo->folder->cache))
1468 continue;
1469 file = procmsg_get_message_file_path (msginfo);
1470 if (!file)
1471 continue;
1472 nmi = procheader_parse_file (file, msginfo->flags, TRUE, FALSE);
1473 if (!nmi)
1474 continue; /* Deleted? */
1475 if (msginfo->mtime != nmi->mtime || msginfo->size != nmi->size) {
1476 nmi->folder = msginfo->folder;
1477 nmi->msgnum = msginfo->msgnum;
1478 msgcache_update_msg (msginfo->folder->cache, nmi);
1479 modified_something = TRUE;
1480 last_item = nmi->folder;
1482 procmsg_msginfo_free (&nmi);
1483 if (summaryview && summaryview->displayed &&
1484 summaryview->folder_item == msginfo->folder &&
1485 summary_get_msgnum(summaryview, summaryview->displayed) == msginfo->msgnum)
1486 summary_redisplay_msg(summaryview);
1489 if (modified_something && last_item &&
1490 summaryview && summaryview->folder_item == last_item) {
1491 summary_show (summaryview, summaryview->folder_item, FALSE);
1493 g_slist_free (child_info->msginfo_list);
1494 child_info->msginfo_list = NULL;
1497 if (!child_info->pid)
1498 child_info->children->nb--;
1500 wait_for_children(child_info->children);
1503 static void catch_input(gpointer data, gint source, GIOCondition cond)
1505 Children *children = (Children *)data;
1506 ChildInfo *child_info = (ChildInfo *)children->list->data;
1507 gchar *input, *ret_str;
1508 gint c, count, len, r;
1509 gssize by_read = 0, by_written = 0;
1511 debug_print("Sending input to grand child.\n");
1512 if (!(cond & (G_IO_OUT | G_IO_ERR)))
1513 return;
1515 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1516 gtk_widget_grab_focus(children->abort_btn);
1518 g_source_remove(child_info->tag_in);
1519 child_info->tag_in = -1;
1521 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1522 0, -1);
1523 ret_str = g_locale_from_utf8(input, strlen(input), &by_read,
1524 &by_written, NULL);
1525 if (ret_str) {
1526 if (by_written) {
1527 g_free(input);
1528 input = ret_str;
1529 } else
1530 g_free(ret_str);
1533 len = strlen(input);
1534 count = 0;
1536 do {
1537 c = write(child_info->chld_in, input + count, len - count);
1538 if (c >= 0)
1539 count += c;
1540 } while (c >= 0 && count < len);
1542 if (c >= 0)
1543 r = write(child_info->chld_in, "\n", 2);
1545 g_free(input);
1547 r = close(child_info->chld_in);
1548 child_info->chld_in = -1;
1549 if (r != 0)
1550 debug_print("closing child input fd: %s (%d)\n", g_strerror(errno), errno);
1551 child_info->chld_in = -1;
1552 debug_print("Input to grand child sent.\n");
1555 static void catch_output(gpointer data, gint source, GIOCondition cond)
1557 ChildInfo *child_info = (ChildInfo *)data;
1558 gint c;
1559 gchar buf[BUFFSIZE];
1561 debug_print("Catching grand child's output.\n");
1562 if (child_info->children->action_type &
1563 (ACTION_PIPE_OUT | ACTION_INSERT)
1564 && source == child_info->chld_out) {
1565 GtkTextView *text =
1566 GTK_TEXT_VIEW(child_info->children->msg_text);
1567 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
1568 GtkTextIter iter;
1569 GtkTextMark *mark;
1570 gint ins_pos;
1572 mark = gtk_text_buffer_get_insert(textbuf);
1573 gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1574 ins_pos = gtk_text_iter_get_offset(&iter);
1576 while (TRUE) {
1577 gsize bytes_read = 0, bytes_written = 0;
1578 gchar *ret_str;
1580 c = read(source, buf, sizeof(buf) - 1);
1581 if (c <= 0)
1582 break;
1584 buf[c] = 0;
1585 ret_str = g_locale_to_utf8
1586 (buf, c, &bytes_read, &bytes_written, NULL);
1587 if (ret_str && bytes_written > 0)
1588 gtk_text_buffer_insert(textbuf, &iter, ret_str, -1);
1589 else
1590 gtk_text_buffer_insert(textbuf, &iter, buf, c);
1591 if (ret_str)
1592 g_free(ret_str);
1595 if (child_info->children->is_selection) {
1596 GtkTextIter ins;
1598 gtk_text_buffer_get_iter_at_offset
1599 (textbuf, &ins, ins_pos);
1600 gtk_text_buffer_select_range(textbuf, &ins, &iter);
1602 } else {
1603 c = read(source, buf, sizeof(buf) - 1);
1604 if (c > 0) {
1605 gsize bytes_read = 0, bytes_written = 0;
1606 gchar *ret_str;
1608 buf[c] = 0;
1609 ret_str = g_locale_to_utf8
1610 (buf, c, &bytes_read, &bytes_written, NULL);
1611 if (ret_str) {
1612 if (bytes_written) {
1613 g_string_append_len
1614 (child_info->output, ret_str,
1615 bytes_written);
1617 g_free(ret_str);
1618 } else
1619 g_string_append_len(child_info->output, buf, c);
1621 child_info->new_out = TRUE;
1624 if (c == 0) {
1625 if (source == child_info->chld_out) {
1626 g_source_remove(child_info->tag_out);
1627 child_info->tag_out = -1;
1628 (void)close(child_info->chld_out);
1629 child_info->chld_out = -1;
1630 } else {
1631 g_source_remove(child_info->tag_err);
1632 child_info->tag_err = -1;
1633 (void)close(child_info->chld_err);
1634 child_info->chld_err = -1;
1638 wait_for_children(child_info->children);
1641 static gchar *get_user_string(const gchar *action, ActionType type)
1643 gchar *message;
1644 gchar *user_str = NULL;
1646 switch (type) {
1647 case ACTION_USER_HIDDEN_STR:
1648 message = g_strdup_printf
1649 (_("Enter the argument for the following action:\n"
1650 "('%%h' will be replaced with the argument)\n"
1651 " %s"),
1652 action);
1653 user_str = input_dialog_with_invisible
1654 (_("Action's hidden user argument"), message, NULL);
1655 break;
1656 case ACTION_USER_STR:
1657 message = g_strdup_printf
1658 (_("Enter the argument for the following action:\n"
1659 "('%%u' will be replaced with the argument)\n"
1660 " %s"),
1661 action);
1662 user_str = input_dialog
1663 (_("Action's user argument"), message, NULL);
1664 break;
1665 default:
1666 g_warning("unsupported action type %d", type);
1669 return user_str;