image viewer: don't make the 'load image' button larger than necessary
[claws.git] / src / action.c
blob293a699ec9ca25ba0d830bf57422ed578f0d05ee
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 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <sys/types.h>
38 #if HAVE_SYS_WAIT_H
39 # include <sys/wait.h>
40 #endif
41 #include <signal.h>
42 #include <unistd.h>
44 #include "utils.h"
45 #include "gtkutils.h"
46 #include "manage_window.h"
47 #include "mainwindow.h"
48 #include "prefs_common.h"
49 #include "alertpanel.h"
50 #include "inputdialog.h"
51 #include "action.h"
52 #include "compose.h"
53 #include "procmsg.h"
54 #include "msgcache.h"
55 #include "textview.h"
56 #include "matcher_parser.h" /* CLAWS */
57 #include "filtering.h"
58 #include "procheader.h"
60 #ifdef G_OS_WIN32
61 #include <windows.h>
62 #endif
64 typedef struct _Children Children;
65 typedef struct _ChildInfo ChildInfo;
66 typedef struct _UserStringDialog UserStringDialog;
68 struct _Children
70 GtkWidget *dialog;
71 GtkWidget *text;
72 GtkWidget *input_entry;
73 GtkWidget *input_hbox;
74 GtkWidget *progress_bar;
75 GtkWidget *abort_btn;
76 GtkWidget *close_btn;
77 GtkWidget *scrolledwin;
79 gchar *action;
80 ActionType action_type;
81 GSList *list;
82 gint nb;
83 gint initial_nb;
84 gint open_in;
85 gboolean output;
87 GtkWidget *msg_text;
89 gboolean is_selection;
92 struct _ChildInfo
94 Children *children;
95 gchar *cmd;
96 GPid pid;
97 gint next_sig;
98 gint chld_in;
99 gint chld_out;
100 gint chld_err;
101 gint tag_in;
102 gint tag_out;
103 gint tag_err;
104 gint tag_status;
105 gint new_out;
107 GString *output;
108 void (*callback)(void *data);
109 void *data;
111 GSList *msginfo_list;
114 static void action_update_menu (GtkUIManager *ui_manager,
115 const gchar *accel_group,
116 gchar *branch_path,
117 gpointer callback,
118 gpointer data);
119 static void compose_actions_execute_cb (GtkWidget *widget,
120 gpointer data);
121 static void compose_actions_execute (Compose *compose,
122 guint action_nb,
123 GtkWidget *widget);
125 static void mainwin_actions_execute_cb (GtkWidget *widget,
126 gpointer data);
127 static void mainwin_actions_execute (MainWindow *mainwin,
128 guint action_nb,
129 GtkWidget *widget);
131 static void msgview_actions_execute_cb (GtkWidget *widget,
132 gpointer data);
133 static void msgview_actions_execute (MessageView *msgview,
134 guint action_nb,
135 GtkWidget *widget);
137 static void message_actions_execute (MessageView *msgview,
138 guint action_nb,
139 GSList *msg_list);
141 static gboolean execute_filtering_actions(gchar *action,
142 GSList *msglist);
144 static gboolean execute_actions (gchar *action,
145 GSList *msg_list,
146 GtkWidget *text,
147 gint body_pos,
148 MimeInfo *partinfo,
149 void (*callback)(void *data),
150 void *data);
152 static gchar *parse_action_cmd (gchar *action,
153 MsgInfo *msginfo,
154 GSList *msg_list,
155 MimeInfo *partinfo,
156 const gchar *user_str,
157 const gchar *user_hidden_str,
158 const gchar *sel_str);
159 static gboolean parse_append_filename (GString *cmd,
160 MsgInfo *msginfo);
162 static gboolean parse_append_msgpart (GString *cmd,
163 MsgInfo *msginfo,
164 MimeInfo *partinfo);
166 static ChildInfo *fork_child (gchar *cmd,
167 const gchar *msg_str,
168 Children *children);
170 static gint wait_for_children (Children *children);
172 static void free_children (Children *children);
174 static void childinfo_close_pipes (ChildInfo *child_info);
176 static void create_io_dialog (Children *children);
177 static void update_io_dialog (Children *children);
179 static void hide_io_dialog_cb (GtkWidget *widget,
180 gpointer data);
181 static gint io_dialog_key_pressed_cb (GtkWidget *widget,
182 GdkEventKey *event,
183 gpointer data);
185 static void catch_output (gpointer data,
186 gint source,
187 GIOCondition cond);
188 static void catch_input (gpointer data,
189 gint source,
190 GIOCondition cond);
191 static void catch_status (GPid pid, gint status, gpointer data);
193 static gchar *get_user_string (const gchar *action,
194 ActionType type);
197 ActionType action_get_type(const gchar *action_str)
199 const gchar *p;
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);
206 p = action_str;
208 if (p[0] == '|') {
209 action_type |= ACTION_PIPE_IN;
210 p++;
211 } else if (p[0] == '>') {
212 action_type |= ACTION_USER_IN;
213 p++;
214 } else if (p[0] == '*') {
215 action_type |= ACTION_USER_HIDDEN_IN;
216 p++;
219 if (p[0] == '\0')
220 return ACTION_ERROR;
222 while (*p && action_type != ACTION_ERROR) {
223 if (!in_filtering_action) {
224 if (p[0] == '%' && p[1]) {
225 switch (p[1]) {
226 case 'a':
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
233 | ACTION_SINGLE
234 | ACTION_MULTIPLE;
235 in_filtering_action = TRUE;
236 break;
237 case 'f':
238 action_type |= ACTION_SINGLE;
239 break;
240 case 'F':
241 action_type |= ACTION_MULTIPLE;
242 break;
243 case 'p':
244 action_type |= ACTION_SINGLE;
245 break;
246 case 's':
247 action_type |= ACTION_SELECTION_STR;
248 break;
249 case 'u':
250 action_type |= ACTION_USER_STR;
251 break;
252 case 'h':
253 action_type |= ACTION_USER_HIDDEN_STR;
254 break;
255 case '%':
256 /* literal '%' */
257 break;
258 default:
259 action_type = ACTION_ERROR;
260 break;
262 p++;
263 } else if (p[0] == '|') {
264 if (p[1] == '\0')
265 action_type |= ACTION_PIPE_OUT;
266 } else if (p[0] == '>') {
267 if (p[1] == '\0')
268 action_type |= ACTION_INSERT;
269 } else if (p[0] == '&') {
270 if (p[1] == '\0')
271 action_type |= ACTION_ASYNC;
272 } else if (p[0] == '}') {
273 in_filtering_action = FALSE;
276 p++;
279 return action_type;
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)
288 GString *cmd;
289 gchar *p;
290 GSList *cur;
292 p = action;
294 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
295 p++;
297 cmd = g_string_sized_new(strlen(action));
299 while (p[0] &&
300 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
301 if (p[0] == '%' && p[1]) {
302 switch (p[1]) {
303 case 'f':
304 if (!parse_append_filename(cmd, msginfo)) {
305 g_string_free(cmd, TRUE);
306 return NULL;
308 p++;
309 break;
310 case 'F':
311 for (cur = msg_list; cur != NULL;
312 cur = cur->next) {
313 MsgInfo *msg = (MsgInfo *)cur->data;
315 if (!parse_append_filename(cmd, msg)) {
316 g_string_free(cmd, TRUE);
317 return NULL;
319 if (cur->next)
320 g_string_append_c(cmd, ' ');
322 p++;
323 break;
324 case 'p':
325 if (!parse_append_msgpart(cmd, msginfo,
326 partinfo)) {
327 g_string_free(cmd, TRUE);
328 return NULL;
330 p++;
331 break;
332 case 's':
333 if (sel_str)
334 g_string_append(cmd, sel_str);
335 p++;
336 break;
337 case 'u':
338 if (user_str)
339 g_string_append(cmd, user_str);
340 p++;
341 break;
342 case 'h':
343 if (user_hidden_str)
344 g_string_append(cmd, user_hidden_str);
345 p++;
346 break;
347 case '%':
348 g_string_append_c(cmd, p[1]);
349 p++;
350 break;
351 default:
352 g_string_append_c(cmd, p[0]);
353 g_string_append_c(cmd, p[1]);
354 p++;
356 } else {
357 g_string_append_c(cmd, p[0]);
359 p++;
361 if (cmd->len == 0) {
362 g_string_free(cmd, TRUE);
363 return NULL;
366 p = cmd->str;
367 g_string_free(cmd, FALSE);
368 return p;
371 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
373 gchar *filename;
375 cm_return_val_if_fail(msginfo, FALSE);
377 filename = procmsg_get_message_file(msginfo);
379 if (!filename) {
380 alertpanel_error(_("Could not get message file %d"),
381 msginfo->msgnum);
382 return FALSE;
385 g_string_append(cmd, "\"");
386 #ifdef G_OS_UNIX
387 gchar *p = filename, *q;
388 gchar escape_ch[] = "\\ ";
389 while ((q = strpbrk(p, "$\"`\\~")) != NULL) {
390 escape_ch[1] = *q;
391 *q = '\0';
392 g_string_append(cmd, p);
393 g_string_append(cmd, escape_ch);
394 p = q + 1;
397 g_string_append(cmd, p);
398 #else
399 g_string_append(cmd, filename);
400 #endif
401 g_string_append(cmd, "\"");
402 g_free(filename);
404 return TRUE;
407 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
408 MimeInfo *partinfo)
410 gboolean single_part = FALSE;
411 gchar *filename;
412 gchar *part_filename;
413 gint ret;
415 if (!partinfo) {
416 partinfo = procmime_scan_message(msginfo);
417 if (!partinfo) {
418 alertpanel_error(_("Could not get message part."));
419 return FALSE;
422 single_part = TRUE;
425 filename = procmsg_get_message_file_path(msginfo);
426 part_filename = procmime_get_tmp_file_name(partinfo);
428 ret = procmime_get_part(part_filename, partinfo);
430 if (single_part)
431 procmime_mimeinfo_free_all(&partinfo);
432 g_free(filename);
434 if (ret < 0) {
435 alertpanel_error(_("Can't get part of multipart message: %s"), g_strerror(-ret));
436 g_free(part_filename);
437 return FALSE;
440 g_string_append(cmd, part_filename);
442 g_free(part_filename);
444 return TRUE;
447 void actions_execute(gpointer data,
448 guint action_nb,
449 GtkWidget *widget,
450 gint source)
452 if (source == TOOLBAR_MAIN)
453 mainwin_actions_execute((MainWindow*)data, action_nb, widget);
454 else if (source == TOOLBAR_COMPOSE)
455 compose_actions_execute((Compose*)data, action_nb, widget);
456 else if (source == TOOLBAR_MSGVIEW)
457 msgview_actions_execute((MessageView*)data, action_nb, widget);
460 void action_update_mainwin_menu(GtkUIManager *ui_manager,
461 gchar *branch_path,
462 MainWindow *mainwin)
464 action_update_menu(ui_manager, "<MainwinActions>", branch_path,
465 mainwin_actions_execute_cb, mainwin);
468 void action_update_msgview_menu(GtkUIManager *ui_manager,
469 gchar *branch_path,
470 MessageView *msgview)
472 action_update_menu(ui_manager, "<MsgviewActions>", branch_path,
473 msgview_actions_execute_cb, msgview);
476 void action_update_compose_menu(GtkUIManager *ui_manager,
477 gchar *branch_path,
478 Compose *compose)
480 action_update_menu(ui_manager, "<ComposeActions>", branch_path,
481 compose_actions_execute_cb, compose);
484 static GtkWidget *find_item_in_menu(GtkWidget *menu, gchar *name)
486 GList *children = gtk_container_get_children(GTK_CONTAINER(GTK_MENU_SHELL(menu)));
487 GList *amenu = children;
488 const gchar *existing_name;
489 while (amenu) {
490 GtkWidget *item = GTK_WIDGET(amenu->data);
491 if ((existing_name = g_object_get_data(G_OBJECT(item), "s_name")) != NULL &&
492 !g_strcmp0(name, existing_name))
494 g_list_free(children);
495 return item;
497 amenu = amenu->next;
500 g_list_free(children);
502 return NULL;
505 static GtkWidget *create_submenus(GtkWidget *menu, const gchar *action)
507 gchar *submenu = g_strdup(action);
508 GtkWidget *new_menu = NULL;
510 if (strchr(submenu, '/')) {
511 const gchar *end = (strchr(submenu, '/')+1);
512 GtkWidget *menu_item = NULL;
513 if (end && *end) {
514 *strchr(submenu, '/') = '\0';
515 if ((menu_item = find_item_in_menu(menu, submenu)) == NULL) {
516 menu_item = gtk_menu_item_new_with_mnemonic(submenu);
517 g_object_set_data_full(G_OBJECT(menu_item), "s_name", g_strdup(submenu), g_free);
518 gtk_widget_show(menu_item);
519 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);
520 new_menu = gtk_menu_new();
521 gtk_widget_show(new_menu);
522 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), new_menu);
523 } else {
524 new_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(menu_item));
526 new_menu = create_submenus(new_menu, end);
529 g_free(submenu);
530 return new_menu ? new_menu : menu;
533 static void action_update_menu(GtkUIManager *ui_manager,
534 const gchar *accel_group,
535 gchar *branch_path,
536 gpointer callback, gpointer data)
538 GSList *cur;
539 gchar *action, *action_p;
540 int callback_action = 0;
541 GtkWidget *menu = gtk_menu_new();
542 GtkWidget *item;
544 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
545 GtkWidget *cur_menu = menu;
546 const gchar *action_name = NULL;
547 action = g_strdup((gchar *)cur->data);
548 action_p = strstr(action, ": ");
549 if (action_p && action_p[2] &&
550 (action_get_type(&action_p[2]) != ACTION_ERROR) &&
551 (action[0] != '/')) {
552 gchar *accel_path = NULL;
554 action_p[0] = '\0';
555 if (strchr(action, '/')) {
556 cur_menu = create_submenus(cur_menu, action);
557 action_name = strrchr(action, '/')+1;
558 } else {
559 action_name = action;
561 gtk_menu_set_accel_group (GTK_MENU (cur_menu),
562 gtk_ui_manager_get_accel_group(ui_manager));
563 item = gtk_menu_item_new_with_label(action_name);
564 gtk_menu_shell_append(GTK_MENU_SHELL(cur_menu), item);
565 g_signal_connect(G_OBJECT(item), "activate",
566 G_CALLBACK(callback), data);
567 g_object_set_data(G_OBJECT(item), "action_num", GINT_TO_POINTER(callback_action));
568 gtk_widget_show(item);
569 accel_path = g_strconcat(accel_group,branch_path, "/", action, NULL);
570 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(item), accel_path);
571 g_free(accel_path);
574 g_free(action);
575 callback_action++;
578 gtk_widget_show(menu);
579 gtk_menu_item_set_submenu(GTK_MENU_ITEM(gtk_ui_manager_get_widget(ui_manager, branch_path)), menu);
582 static void compose_actions_execute_cb(GtkWidget *widget, gpointer data)
584 Compose *compose = (Compose *)data;
585 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
586 compose_actions_execute(compose, action_nb, NULL);
589 static void compose_actions_execute(Compose *compose, guint action_nb, GtkWidget *widget)
591 gchar *buf, *action;
592 ActionType action_type;
594 cm_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
596 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
597 cm_return_if_fail(buf != NULL);
598 action = strstr(buf, ": ");
599 cm_return_if_fail(action != NULL);
601 /* Point to the beginning of the command-line */
602 action += 2;
604 action_type = action_get_type(action);
605 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
606 alertpanel_warning
607 (_("The selected action cannot be used in the compose window\n"
608 "because it contains %%f, %%F, %%as or %%p."));
609 return;
612 execute_actions(action, NULL, compose->text, 0, NULL,
613 compose_action_cb, compose);
616 static void mainwin_actions_execute_cb(GtkWidget *widget, gpointer data)
618 MainWindow *mainwin = (MainWindow *)data;
619 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
620 mainwin_actions_execute(mainwin, action_nb, NULL);
623 static void _free_msginfos(gpointer data, gpointer user_data)
625 MsgInfo *msginfo = (MsgInfo *)data;
627 procmsg_msginfo_free(&msginfo);
630 static void mainwin_actions_execute(MainWindow *mainwin, guint action_nb,
631 GtkWidget *widget)
633 GSList *msg_list;
635 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
636 message_actions_execute(mainwin->messageview, action_nb, msg_list);
637 summary_select_by_msg_list(mainwin->summaryview, msg_list);
638 g_slist_foreach(msg_list, _free_msginfos, NULL);
639 g_slist_free(msg_list);
642 static void msgview_actions_execute_cb(GtkWidget *widget, gpointer data)
644 MessageView *msgview = (MessageView *)data;
645 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
646 msgview_actions_execute(msgview, action_nb, NULL);
649 static void msgview_actions_execute(MessageView *msgview, guint action_nb,
650 GtkWidget *widget)
652 GSList *msg_list = NULL;
654 if (msgview->msginfo)
655 msg_list = g_slist_append(msg_list, msgview->msginfo);
656 message_actions_execute(msgview, action_nb, msg_list);
657 g_slist_free(msg_list);
660 static void message_actions_execute(MessageView *msgview, guint action_nb,
661 GSList *msg_list)
663 TextView *textview;
664 MimeInfo *partinfo;
665 gchar *buf;
666 gchar *action;
667 GtkWidget *text = NULL;
668 guint body_pos = 0;
669 ActionType action_type;
671 cm_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
673 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
675 cm_return_if_fail(buf);
676 cm_return_if_fail((action = strstr(buf, ": ")));
678 /* Point to the beginning of the command-line */
679 action += 2;
681 textview = messageview_get_current_textview(msgview);
682 if (textview) {
683 text = textview->text;
684 body_pos = textview->body_pos;
686 partinfo = messageview_get_selected_mime_part(msgview);
688 /* this command will alter the message text */
689 action_type = action_get_type(action);
690 if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
691 msgview->filtered = TRUE;
693 if (action_type & ACTION_FILTERING_ACTION)
694 /* CLAWS: most of the above code is not necessary for applying
695 * filtering */
696 execute_filtering_actions(action, msg_list);
697 else
698 execute_actions(action, msg_list, text, body_pos, partinfo,
699 NULL, NULL);
702 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
704 GSList *action_list, *p;
705 const gchar *sbegin, *send;
706 gchar *action_string;
707 SummaryView *summaryview = NULL;
708 MainWindow *mainwin = NULL;
710 if (mainwindow_get_mainwindow()) {
711 summaryview = mainwindow_get_mainwindow()->summaryview;
712 mainwin = mainwindow_get_mainwindow();
715 if (NULL == (sbegin = g_strstr_len(action, -1, "%as{")))
716 return FALSE;
717 sbegin += sizeof "%as{" - 1;
718 if (NULL == (send = strrchr(sbegin, '}')))
719 return FALSE;
720 action_string = g_strndup(sbegin, send - sbegin);
722 action_list = matcher_parser_get_action_list(action_string);
723 if (action_list == NULL) {
724 gchar *tmp = g_strdup(action_string);
726 g_strstrip(tmp);
727 if (*tmp == '\0')
728 alertpanel_error(_("There is no filtering action set"));
729 else
730 alertpanel_error(_("Invalid filtering action(s):\n%s"), tmp);
731 g_free(action_string);
732 g_free(tmp);
733 return FALSE;
735 g_free(action_string);
737 /* apply actions on each message info */
738 for (p = msglist; p && p->data; p = g_slist_next(p)) {
739 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
742 if (summaryview) {
743 summary_lock(summaryview);
744 main_window_cursor_wait(mainwin);
745 summary_freeze(summaryview);
746 folder_item_update_freeze();
749 filtering_move_and_copy_msgs(msglist);
751 if (summaryview) {
752 folder_item_update_thaw();
753 summary_thaw(summaryview);
754 main_window_cursor_normal(mainwin);
755 summary_unlock(summaryview);
756 summary_show(summaryview, summaryview->folder_item, FALSE);
758 for (p = action_list; p; p = g_slist_next(p))
759 if (p->data) filteringaction_free(p->data);
760 g_slist_free(action_list);
761 return TRUE;
764 static gboolean execute_actions(gchar *action, GSList *msg_list,
765 GtkWidget *text,
766 gint body_pos, MimeInfo *partinfo,
767 void (*callback)(void *data), void *data)
769 GSList *children_list = NULL, *cur = NULL;
770 gint is_ok = TRUE;
771 gint msg_list_len;
772 Children *children;
773 ChildInfo *child_info;
774 ActionType action_type;
775 MsgInfo *msginfo;
776 gchar *cmd;
777 gchar *sel_str = NULL;
778 gchar *msg_str = NULL;
779 gchar *user_str = NULL;
780 gchar *user_hidden_str = NULL;
781 GtkTextIter start_iter, end_iter;
782 gboolean is_selection = FALSE;
784 cm_return_val_if_fail(action && *action, FALSE);
786 action_type = action_get_type(action);
788 if (action_type == ACTION_ERROR)
789 return FALSE; /* ERR: syntax error */
791 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
792 return FALSE; /* ERR: file command without selection */
794 msg_list_len = g_slist_length(msg_list);
796 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
797 if (msg_list_len > 1)
798 return FALSE; /* ERR: pipe + multiple selection */
799 if (!text)
800 return FALSE; /* ERR: pipe and no displayed text */
803 if (action_type & ACTION_SELECTION_STR) {
804 if (!text)
805 return FALSE; /* ERR: selection string but no text */
808 if (text) {
809 GtkTextBuffer *textbuf;
811 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
812 is_selection = gtk_text_buffer_get_selection_bounds
813 (textbuf, &start_iter, &end_iter);
814 if (!is_selection) {
815 gtk_text_buffer_get_iter_at_offset
816 (textbuf, &start_iter, body_pos);
817 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
819 msg_str = gtk_text_buffer_get_text
820 (textbuf, &start_iter, &end_iter, FALSE);
821 if (is_selection)
822 sel_str = g_strdup(msg_str);
825 if (action_type & ACTION_USER_STR) {
826 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
827 g_free(msg_str);
828 g_free(sel_str);
829 return FALSE;
833 if (action_type & ACTION_USER_HIDDEN_STR) {
834 if (!(user_hidden_str =
835 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
836 g_free(msg_str);
837 g_free(sel_str);
838 g_free(user_str);
839 return FALSE;
843 if (text && (action_type & ACTION_PIPE_OUT)) {
844 GtkTextBuffer *textbuf;
845 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
846 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
849 children = g_new0(Children, 1);
851 children->nb = 0;
852 children->action = g_strdup(action);
853 children->action_type = action_type;
854 children->msg_text = text;
855 children->is_selection = is_selection;
857 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
858 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
859 children->open_in = 1;
861 /* Pre-fetch bodies, makes it easier on IMAP (see bug #3011) */
862 for (cur = msg_list; cur; cur = cur->next) {
863 gchar *dummy;
864 msginfo = (MsgInfo *)cur->data;
866 dummy = procmsg_get_message_file((MsgInfo *)cur->data);
867 if (dummy)
868 g_free(dummy);
869 else
870 is_ok = FALSE;
873 if (is_ok && (action_type & ACTION_SINGLE)) {
874 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
875 msginfo = (MsgInfo *)cur->data;
876 if (!msginfo) {
877 is_ok = FALSE; /* ERR: msginfo missing */
878 break;
880 cmd = parse_action_cmd(action, msginfo, msg_list,
881 partinfo, user_str,
882 user_hidden_str, sel_str);
883 if (!cmd) {
884 debug_print("Action command error\n");
885 is_ok = FALSE; /* ERR: incorrect command */
886 break;
888 if ((child_info = fork_child(cmd, msg_str, children))) {
889 /* Pass msginfo to catch_status () */
890 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
891 child_info->msginfo_list =
892 g_slist_append (NULL, msginfo);
893 children_list = g_slist_append(children_list,
894 child_info);
895 children->nb++;
897 g_free(cmd);
899 } else if (is_ok) {
900 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
901 user_str, user_hidden_str, sel_str);
902 if (cmd) {
903 if ((child_info = fork_child(cmd, msg_str, children))) {
904 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
905 child_info->msginfo_list =
906 g_slist_copy (msg_list);
907 children_list = g_slist_append(children_list,
908 child_info);
909 children->nb++;
911 g_free(cmd);
912 } else
913 is_ok = FALSE; /* ERR: incorrect command */
916 g_free(msg_str);
917 g_free(sel_str);
918 g_free(user_str);
919 g_free(user_hidden_str);
921 if (!children_list) {
922 /* If not waiting for children, return */
923 free_children(children);
924 } else {
925 GSList *cur;
927 children->list = children_list;
928 children->initial_nb = children->nb;
930 for (cur = children_list; cur; cur = cur->next) {
931 child_info = (ChildInfo *) cur->data;
932 child_info->callback = callback;
933 child_info->data = data;
934 child_info->tag_status =
935 g_child_watch_add(child_info->pid, catch_status, child_info);
938 create_io_dialog(children);
940 return is_ok;
943 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
944 Children *children)
946 gint chld_in, chld_out, chld_err;
947 gchar **argv, *ret_str, *trim_cmd;
948 GPid pid;
949 ChildInfo *child_info;
950 gint follow_child;
951 gssize by_written = 0, by_read = 0;
952 gboolean result = FALSE;
953 GError *error = NULL;
955 follow_child = !(children->action_type & ACTION_ASYNC);
957 chld_in = chld_out = chld_err = -1;
959 ret_str = g_locale_from_utf8(cmd, strlen(cmd),
960 &by_read, &by_written,
961 NULL);
962 if (!ret_str || !by_written) {
963 if (ret_str)
964 g_free(ret_str);
965 ret_str = g_strdup(cmd);
968 trim_cmd = ret_str;
970 while (g_ascii_isspace(trim_cmd[0]))
971 trim_cmd++;
973 #ifdef G_OS_UNIX
974 argv = g_new0(gchar *, 4);
975 argv[0] = g_strdup("/bin/sh");
976 argv[1] = g_strdup("-c");
977 argv[2] = g_strdup(trim_cmd);
978 argv[3] = 0;
979 #else
980 argv = strsplit_with_quote(trim_cmd, " ", 0);
981 #endif
982 g_free(ret_str);
984 if (follow_child) {
985 result = g_spawn_async_with_pipes(NULL, argv, NULL,
986 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
987 NULL, NULL, &pid, &chld_in, &chld_out,
988 &chld_err, &error);
989 } else {
990 result = g_spawn_async(NULL, argv, NULL,
991 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH, NULL, NULL,
992 &pid, &error);
995 debug_print("spawning %s: %d\n", cmd, result);
997 g_strfreev(argv);
999 if (!result) {
1000 alertpanel_error(_("Could not fork to execute the following "
1001 "command:\n%s\n%s"),
1002 cmd, error ? error->message : _("Unknown error"));
1003 if (error)
1004 g_error_free(error);
1005 return NULL;
1008 if (!(children->action_type &
1009 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
1010 (void)close(chld_in);
1012 if (!follow_child) {
1013 g_spawn_close_pid(pid);
1014 return NULL;
1016 child_info = g_new0(ChildInfo, 1);
1018 child_info->children = children;
1020 child_info->pid = pid;
1021 #ifdef G_OS_UNIX
1022 child_info->next_sig = SIGTERM;
1023 #endif
1024 child_info->cmd = g_strdup(cmd);
1025 child_info->new_out = FALSE;
1026 child_info->output = g_string_new(NULL);
1027 child_info->chld_in =
1028 (children->action_type &
1029 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
1030 ? chld_in : -1;
1031 child_info->chld_out = chld_out;
1032 child_info->chld_err = chld_err;
1033 child_info->tag_status = -1;
1034 child_info->tag_in = -1;
1035 child_info->tag_out = claws_input_add(chld_out, G_IO_IN | G_IO_HUP | G_IO_ERR,
1036 catch_output, child_info, FALSE);
1037 child_info->tag_err = claws_input_add(chld_err, G_IO_IN | G_IO_HUP | G_IO_ERR,
1038 catch_output, child_info, FALSE);
1040 if (!(children->action_type &
1041 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
1042 return child_info;
1044 if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
1045 int r;
1046 ret_str = g_locale_from_utf8(msg_str, strlen(msg_str),
1047 &by_read, &by_written, NULL);
1048 if (ret_str && by_written)
1049 r = write(chld_in, ret_str, strlen(ret_str));
1050 else
1051 r = write(chld_in, msg_str, strlen(msg_str));
1052 if (ret_str)
1053 g_free(ret_str);
1055 if (!(children->action_type &
1056 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
1057 r = close(chld_in);
1058 child_info->chld_in = -1; /* No more input */
1059 if (r != 0)
1060 debug_print("piping to child process: %s (%d)\n", g_strerror(errno), errno);
1063 return child_info;
1066 static void kill_children_cb(GtkWidget *widget, gpointer data)
1068 GSList *cur;
1069 Children *children = (Children *) data;
1070 ChildInfo *child_info;
1072 for (cur = children->list; cur; cur = cur->next) {
1073 child_info = (ChildInfo *)(cur->data);
1074 #ifdef G_OS_WIN32
1075 debug_print("Killing child group HANDLE %p\n", child_info->pid);
1076 TerminateProcess(child_info->pid, 0);
1077 #else
1078 debug_print("Killing child group id %d\n", child_info->pid);
1079 if (child_info->pid && kill(child_info->pid, child_info->next_sig) < 0)
1080 perror("kill");
1081 child_info->next_sig = SIGKILL;
1082 #endif
1086 static gint wait_for_children(Children *children)
1088 gboolean new_output;
1089 ChildInfo *child_info;
1090 GSList *cur;
1092 cur = children->list;
1093 new_output = FALSE;
1094 while (cur) {
1095 child_info = (ChildInfo *)cur->data;
1096 new_output |= child_info->new_out;
1097 cur = cur->next;
1100 children->output |= new_output;
1102 if (new_output || (children->dialog && (children->initial_nb != children->nb)))
1103 update_io_dialog(children);
1105 if (children->nb)
1106 return FALSE;
1108 if (!children->dialog) {
1109 free_children(children);
1110 } else if (!children->output) {
1111 gtk_widget_destroy(children->dialog);
1114 return FALSE;
1117 static void send_input(GtkWidget *w, gpointer data)
1119 Children *children = (Children *) data;
1120 ChildInfo *child_info = (ChildInfo *) children->list->data;
1122 child_info->tag_in = claws_input_add(child_info->chld_in,
1123 G_IO_OUT | G_IO_ERR,
1124 catch_input, children, FALSE);
1127 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1129 hide_io_dialog_cb(w, data);
1130 return TRUE;
1133 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1136 Children *children = (Children *)data;
1138 if (!children->nb) {
1139 g_signal_handlers_disconnect_matched
1140 (G_OBJECT(children->dialog), G_SIGNAL_MATCH_DATA,
1141 0, 0, NULL, NULL, children);
1142 gtk_widget_destroy(children->dialog);
1143 free_children(children);
1147 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1148 gpointer data)
1150 if (event && (event->keyval == GDK_KEY_Escape ||
1151 event->keyval == GDK_KEY_Return ||
1152 event->keyval == GDK_KEY_KP_Enter))
1153 hide_io_dialog_cb(widget, data);
1154 return TRUE;
1157 static void childinfo_close_pipes(ChildInfo *child_info)
1159 /* stdout and stderr pipes are guaranteed to be removed by
1160 * their handler, but in case where we receive child exit notification
1161 * before grand-child's pipes closing signals, we check them and close
1162 * them if necessary
1164 if (child_info->tag_in > 0)
1165 g_source_remove(child_info->tag_in);
1166 if (child_info->tag_out > 0)
1167 g_source_remove(child_info->tag_out);
1168 if (child_info->tag_err > 0)
1169 g_source_remove(child_info->tag_err);
1171 if (child_info->chld_in >= 0)
1172 (void)close(child_info->chld_in);
1173 if (child_info->chld_out >= 0)
1174 (void)close(child_info->chld_out);
1175 if (child_info->chld_err >= 0)
1176 (void)close(child_info->chld_err);
1179 static void free_children(Children *children)
1181 ChildInfo *child_info;
1182 void (*callback)(void *data) = NULL;
1183 void *data = NULL;
1185 debug_print("Freeing children data %p\n", children);
1187 g_free(children->action);
1188 while (children->list != NULL) {
1189 child_info = (ChildInfo *)children->list->data;
1190 g_free(child_info->cmd);
1191 g_string_free(child_info->output, TRUE);
1192 children->list = g_slist_remove(children->list, child_info);
1193 callback = child_info->callback;
1194 data = child_info->data;
1195 g_free(child_info);
1198 if (callback)
1199 callback(data);
1201 g_free(children);
1204 static void update_io_dialog(Children *children)
1206 GSList *cur;
1208 debug_print("Updating actions input/output dialog.\n");
1210 if (children->progress_bar) {
1211 gchar *text;
1212 #ifdef GENERIC_UMPC
1213 /* use a more compact format */
1214 const gchar *format = "%s %d/%d";
1215 #else
1216 const gchar *format = "%s %d / %d";
1217 #endif
1219 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar),
1220 (children->initial_nb == 0) ? 0 :
1221 (gdouble) (children->initial_nb - children->nb) /
1222 (gdouble) children->initial_nb);
1223 text = g_strdup_printf(format, _("Completed"),
1224 children->initial_nb - children->nb,
1225 children->initial_nb);
1226 gtk_progress_bar_set_show_text(GTK_PROGRESS_BAR(children->progress_bar), TRUE);
1227 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(children->progress_bar), text);
1228 g_free(text);
1231 if (!children->nb) {
1232 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1233 gtk_widget_set_sensitive(children->close_btn, TRUE);
1234 if (children->input_hbox)
1235 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1236 gtk_widget_grab_focus(children->close_btn);
1237 g_signal_connect(G_OBJECT(children->dialog),
1238 "key_press_event",
1239 G_CALLBACK(io_dialog_key_pressed_cb),
1240 children);
1243 if (children->output) {
1244 GtkWidget *text = children->text;
1245 GtkTextBuffer *textbuf;
1246 GtkTextIter iter, start_iter, end_iter;
1247 gchar *caption;
1248 ChildInfo *child_info;
1250 gtk_widget_show(children->scrolledwin);
1251 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
1252 gtk_text_buffer_get_bounds(textbuf, &start_iter, &end_iter);
1253 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
1254 gtk_text_buffer_get_start_iter(textbuf, &iter);
1256 for (cur = children->list; cur; cur = cur->next) {
1257 child_info = (ChildInfo *)cur->data;
1258 if (child_info->pid)
1259 caption = g_strdup_printf
1260 (_("--- Running: %s\n"),
1261 child_info->cmd);
1262 else
1263 caption = g_strdup_printf
1264 (_("--- Ended: %s\n"),
1265 child_info->cmd);
1267 gtk_text_buffer_insert(textbuf, &iter, caption, -1);
1268 gtk_text_buffer_insert(textbuf, &iter,
1269 child_info->output->str, -1);
1270 g_free(caption);
1271 child_info->new_out = FALSE;
1277 *\brief Save Gtk object size to prefs dataset
1279 static void actions_io_size_allocate_cb(GtkWidget *widget,
1280 GtkAllocation *allocation)
1282 cm_return_if_fail(allocation != NULL);
1284 gtk_window_get_size(GTK_WINDOW(widget),
1285 &prefs_common.actionsiodialog_width, &prefs_common.actionsiodialog_height);
1288 static void create_io_dialog(Children *children)
1290 GtkWidget *dialog;
1291 GtkWidget *vbox;
1292 GtkWidget *entry = NULL;
1293 GtkWidget *input_hbox = NULL;
1294 GtkWidget *send_button;
1295 GtkWidget *label;
1296 GtkWidget *text;
1297 GtkWidget *scrolledwin;
1298 GtkWidget *progress_bar = NULL;
1299 GtkWidget *abort_button;
1300 GtkWidget *close_button;
1301 static GdkGeometry geometry;
1303 debug_print("Creating action IO dialog\n");
1305 dialog = gtk_dialog_new();
1306 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1307 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1308 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1309 manage_window_set_transient(GTK_WINDOW(dialog));
1310 g_signal_connect(G_OBJECT(dialog), "delete_event",
1311 G_CALLBACK(delete_io_dialog_cb), children);
1312 g_signal_connect(G_OBJECT(dialog), "destroy",
1313 G_CALLBACK(hide_io_dialog_cb),
1314 children);
1315 g_signal_connect(G_OBJECT(dialog), "size_allocate",
1316 G_CALLBACK(actions_io_size_allocate_cb), NULL);
1318 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
1319 gtk_container_add(GTK_CONTAINER(
1320 gtk_dialog_get_content_area(GTK_DIALOG(dialog))), vbox);
1321 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1322 gtk_widget_show(vbox);
1324 label = gtk_label_new(children->action);
1325 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1326 gtk_widget_show(label);
1328 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1329 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1330 GTK_POLICY_AUTOMATIC,
1331 GTK_POLICY_AUTOMATIC);
1332 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
1333 GTK_SHADOW_IN);
1334 gtk_scrolled_window_set_propagate_natural_height(GTK_SCROLLED_WINDOW(scrolledwin), TRUE);
1335 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1336 gtk_widget_hide(scrolledwin);
1338 text = gtk_text_view_new();
1340 if (prefs_common.textfont) {
1341 PangoFontDescription *font_desc;
1342 font_desc = pango_font_description_from_string
1343 (prefs_common.textfont);
1344 if (font_desc) {
1345 gtk_widget_override_font(text, font_desc);
1346 pango_font_description_free(font_desc);
1350 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
1351 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
1352 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
1353 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
1354 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1355 gtk_widget_show(text);
1357 if (children->open_in) {
1358 input_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
1359 gtk_widget_show(input_hbox);
1361 entry = gtk_entry_new();
1362 gtk_widget_set_size_request(entry, 320, -1);
1363 g_signal_connect(G_OBJECT(entry), "activate",
1364 G_CALLBACK(send_input), children);
1365 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1366 if (children->action_type & ACTION_USER_HIDDEN_IN) {
1367 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1369 gtk_widget_show(entry);
1371 send_button = gtkut_stock_button("system-run", _("_Execute"));
1372 g_signal_connect(G_OBJECT(send_button), "clicked",
1373 G_CALLBACK(send_input), children);
1374 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1375 FALSE, 0);
1376 gtk_widget_show(send_button);
1378 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1379 gtk_widget_grab_focus(entry);
1382 if (children->initial_nb > 1) {
1383 gchar *text;
1384 #ifdef GENERIC_UMPC
1385 /* use a more compact format */
1386 const gchar *format = "%s 0/%d\n";
1387 #else
1388 const gchar *format = "%s 0 / %d\n";
1389 #endif
1391 progress_bar = gtk_progress_bar_new();
1392 gtk_orientable_set_orientation(GTK_ORIENTABLE(progress_bar),
1393 GTK_ORIENTATION_HORIZONTAL);
1394 gtk_progress_bar_set_inverted(GTK_PROGRESS_BAR(progress_bar),
1395 FALSE);
1397 text = g_strdup_printf(format, _("Completed"),
1398 children->initial_nb);
1399 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar),
1400 text);
1401 g_free(text);
1402 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1403 gtk_widget_show(progress_bar);
1406 abort_button = gtk_dialog_add_button(GTK_DIALOG(dialog), _("_Stop"),
1407 GTK_RESPONSE_NONE);
1408 close_button = gtk_dialog_add_button(GTK_DIALOG(dialog),_("_Close"),
1409 GTK_RESPONSE_NONE);
1410 gtk_widget_grab_default(close_button);
1411 g_signal_connect(G_OBJECT(abort_button), "clicked",
1412 G_CALLBACK(kill_children_cb), children);
1413 g_signal_connect(G_OBJECT(close_button), "clicked",
1414 G_CALLBACK(hide_io_dialog_cb), children);
1416 if (children->nb)
1417 gtk_widget_set_sensitive(close_button, FALSE);
1419 if (!geometry.min_height) {
1420 geometry.min_width = 582;
1421 geometry.min_height = 310;
1424 gtk_window_set_geometry_hints(GTK_WINDOW(dialog), NULL, &geometry,
1425 GDK_HINT_MIN_SIZE);
1426 gtk_window_set_default_size(GTK_WINDOW(dialog), prefs_common.actionsiodialog_width,
1427 prefs_common.actionsiodialog_height);
1429 gtk_widget_show(dialog);
1431 children->dialog = dialog;
1432 children->scrolledwin = scrolledwin;
1433 children->text = text;
1434 children->input_hbox = children->open_in ? input_hbox : NULL;
1435 children->input_entry = children->open_in ? entry : NULL;
1436 children->progress_bar = progress_bar;
1437 children->abort_btn = abort_button;
1438 children->close_btn = close_button;
1441 static void catch_status(GPid pid, gint status, gpointer data)
1443 ChildInfo *child_info = (ChildInfo *)data;
1445 debug_print("Child returned %d\n", status);
1447 childinfo_close_pipes(child_info);
1448 g_spawn_close_pid(child_info->pid);
1449 child_info->pid = 0;
1451 if (child_info->children->action_type & (ACTION_SINGLE | ACTION_MULTIPLE)
1452 && child_info->msginfo_list) {
1453 /* Actions on message *files* might change size and
1454 * time stamp, and thus invalidate the cache */
1455 SummaryView *summaryview = NULL;
1456 GSList *cur;
1457 MsgInfo *msginfo, *nmi; /* newmsginfo */
1458 char *file;
1459 gboolean modified_something = FALSE;
1460 FolderItem *last_item = NULL;
1461 if (mainwindow_get_mainwindow ())
1462 summaryview = mainwindow_get_mainwindow ()->summaryview;
1463 for (cur = child_info->msginfo_list; cur; cur = cur->next) {
1464 msginfo = (MsgInfo *)cur->data;
1465 if (!(msginfo && /* Stuff used valid? */
1466 msginfo->folder && msginfo->folder->cache))
1467 continue;
1468 file = procmsg_get_message_file_path (msginfo);
1469 if (!file)
1470 continue;
1471 nmi = procheader_parse_file (file, msginfo->flags, TRUE, FALSE);
1472 if (!nmi)
1473 continue; /* Deleted? */
1474 if (msginfo->mtime != nmi->mtime || msginfo->size != nmi->size) {
1475 nmi->folder = msginfo->folder;
1476 nmi->msgnum = msginfo->msgnum;
1477 msgcache_update_msg (msginfo->folder->cache, nmi);
1478 modified_something = TRUE;
1479 last_item = nmi->folder;
1481 procmsg_msginfo_free (&nmi);
1482 if (summaryview && summaryview->displayed &&
1483 summaryview->folder_item == msginfo->folder &&
1484 summary_get_msgnum(summaryview, summaryview->displayed) == msginfo->msgnum)
1485 summary_redisplay_msg(summaryview);
1488 if (modified_something && last_item &&
1489 summaryview && summaryview->folder_item == last_item) {
1490 summary_show (summaryview, summaryview->folder_item, FALSE);
1492 g_slist_free (child_info->msginfo_list);
1493 child_info->msginfo_list = NULL;
1496 if (!child_info->pid)
1497 child_info->children->nb--;
1499 wait_for_children(child_info->children);
1502 static void catch_input(gpointer data, gint source, GIOCondition cond)
1504 Children *children = (Children *)data;
1505 ChildInfo *child_info = (ChildInfo *)children->list->data;
1506 gchar *input, *ret_str;
1507 gint c, count, len, r;
1508 gssize by_read = 0, by_written = 0;
1510 debug_print("Sending input to grand child.\n");
1511 if (!(cond & (G_IO_OUT | G_IO_ERR)))
1512 return;
1514 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1515 gtk_widget_grab_focus(children->abort_btn);
1517 g_source_remove(child_info->tag_in);
1518 child_info->tag_in = -1;
1520 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1521 0, -1);
1522 ret_str = g_locale_from_utf8(input, strlen(input), &by_read,
1523 &by_written, NULL);
1524 if (ret_str) {
1525 if (by_written) {
1526 g_free(input);
1527 input = ret_str;
1528 } else
1529 g_free(ret_str);
1532 len = strlen(input);
1533 count = 0;
1535 do {
1536 c = write(child_info->chld_in, input + count, len - count);
1537 if (c >= 0)
1538 count += c;
1539 } while (c >= 0 && count < len);
1541 if (c >= 0)
1542 r = write(child_info->chld_in, "\n", 2);
1544 g_free(input);
1546 r = close(child_info->chld_in);
1547 child_info->chld_in = -1;
1548 if (r != 0)
1549 debug_print("closing child input fd: %s (%d)\n", g_strerror(errno), errno);
1550 child_info->chld_in = -1;
1551 debug_print("Input to grand child sent.\n");
1554 static void catch_output(gpointer data, gint source, GIOCondition cond)
1556 ChildInfo *child_info = (ChildInfo *)data;
1557 gint c;
1558 gchar buf[BUFFSIZE];
1560 debug_print("Catching grand child's output.\n");
1561 if (child_info->children->action_type &
1562 (ACTION_PIPE_OUT | ACTION_INSERT)
1563 && source == child_info->chld_out) {
1564 GtkTextView *text =
1565 GTK_TEXT_VIEW(child_info->children->msg_text);
1566 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
1567 GtkTextIter iter;
1568 GtkTextMark *mark;
1569 gint ins_pos;
1571 mark = gtk_text_buffer_get_insert(textbuf);
1572 gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1573 ins_pos = gtk_text_iter_get_offset(&iter);
1575 while (TRUE) {
1576 gsize bytes_read = 0, bytes_written = 0;
1577 gchar *ret_str;
1579 c = read(source, buf, sizeof(buf) - 1);
1580 if (c <= 0)
1581 break;
1583 buf[c] = 0;
1584 ret_str = g_locale_to_utf8
1585 (buf, c, &bytes_read, &bytes_written, NULL);
1586 if (ret_str && bytes_written > 0)
1587 gtk_text_buffer_insert(textbuf, &iter, ret_str, -1);
1588 else
1589 gtk_text_buffer_insert(textbuf, &iter, buf, c);
1590 if (ret_str)
1591 g_free(ret_str);
1594 if (child_info->children->is_selection) {
1595 GtkTextIter ins;
1597 gtk_text_buffer_get_iter_at_offset
1598 (textbuf, &ins, ins_pos);
1599 gtk_text_buffer_select_range(textbuf, &ins, &iter);
1601 } else {
1602 c = read(source, buf, sizeof(buf) - 1);
1603 if (c > 0) {
1604 gsize bytes_read = 0, bytes_written = 0;
1605 gchar *ret_str;
1607 buf[c] = 0;
1608 ret_str = g_locale_to_utf8
1609 (buf, c, &bytes_read, &bytes_written, NULL);
1610 if (ret_str) {
1611 if (bytes_written) {
1612 g_string_append_len
1613 (child_info->output, ret_str,
1614 bytes_written);
1616 g_free(ret_str);
1617 } else
1618 g_string_append_len(child_info->output, buf, c);
1620 child_info->new_out = TRUE;
1623 if (c == 0) {
1624 if (source == child_info->chld_out) {
1625 g_source_remove(child_info->tag_out);
1626 child_info->tag_out = -1;
1627 (void)close(child_info->chld_out);
1628 child_info->chld_out = -1;
1629 } else {
1630 g_source_remove(child_info->tag_err);
1631 child_info->tag_err = -1;
1632 (void)close(child_info->chld_err);
1633 child_info->chld_err = -1;
1637 wait_for_children(child_info->children);
1640 static gchar *get_user_string(const gchar *action, ActionType type)
1642 gchar *message;
1643 gchar *user_str = NULL;
1645 switch (type) {
1646 case ACTION_USER_HIDDEN_STR:
1647 message = g_strdup_printf
1648 (_("Enter the argument for the following action:\n"
1649 "('%%h' will be replaced with the argument)\n"
1650 " %s"),
1651 action);
1652 user_str = input_dialog_with_invisible
1653 (_("Action's hidden user argument"), message, NULL);
1654 break;
1655 case ACTION_USER_STR:
1656 message = g_strdup_printf
1657 (_("Enter the argument for the following action:\n"
1658 "('%%u' will be replaced with the argument)\n"
1659 " %s"),
1660 action);
1661 user_str = input_dialog
1662 (_("Action's user argument"), message, NULL);
1663 break;
1664 default:
1665 g_warning("unsupported action type %d", type);
1668 return user_str;