Forgot to adjust default window size
[claws.git] / src / action.c
blobf5ab41b3b58de137b277fcc17df33f47408076c6
1 /*
2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2016 Hiroyuki Yamamoto & The Claws Mail Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
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;
374 gchar *p, *q;
375 gchar escape_ch[] = "\\ ";
377 cm_return_val_if_fail(msginfo, FALSE);
379 filename = procmsg_get_message_file(msginfo);
381 if (!filename) {
382 alertpanel_error(_("Could not get message file %d"),
383 msginfo->msgnum);
384 return FALSE;
387 p = filename;
388 g_string_append(cmd, "\"");
389 #ifdef G_OS_UNIX
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 !strcmp2(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 mainwin_actions_execute(MainWindow *mainwin, guint action_nb,
625 GtkWidget *widget)
627 GSList *msg_list;
629 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
630 message_actions_execute(mainwin->messageview, action_nb, msg_list);
631 g_slist_free(msg_list);
634 static void msgview_actions_execute_cb(GtkWidget *widget, gpointer data)
636 MessageView *msgview = (MessageView *)data;
637 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
638 msgview_actions_execute(msgview, action_nb, NULL);
641 static void msgview_actions_execute(MessageView *msgview, guint action_nb,
642 GtkWidget *widget)
644 GSList *msg_list = NULL;
646 if (msgview->msginfo)
647 msg_list = g_slist_append(msg_list, msgview->msginfo);
648 message_actions_execute(msgview, action_nb, msg_list);
649 g_slist_free(msg_list);
652 static void message_actions_execute(MessageView *msgview, guint action_nb,
653 GSList *msg_list)
655 TextView *textview;
656 MimeInfo *partinfo;
657 gchar *buf;
658 gchar *action;
659 GtkWidget *text = NULL;
660 guint body_pos = 0;
661 ActionType action_type;
663 cm_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
665 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
667 cm_return_if_fail(buf);
668 cm_return_if_fail((action = strstr(buf, ": ")));
670 /* Point to the beginning of the command-line */
671 action += 2;
673 textview = messageview_get_current_textview(msgview);
674 if (textview) {
675 text = textview->text;
676 body_pos = textview->body_pos;
678 partinfo = messageview_get_selected_mime_part(msgview);
680 /* this command will alter the message text */
681 action_type = action_get_type(action);
682 if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
683 msgview->filtered = TRUE;
685 if (action_type & ACTION_FILTERING_ACTION)
686 /* CLAWS: most of the above code is not necessary for applying
687 * filtering */
688 execute_filtering_actions(action, msg_list);
689 else
690 execute_actions(action, msg_list, text, body_pos, partinfo,
691 NULL, NULL);
694 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
696 GSList *action_list, *p;
697 const gchar *sbegin, *send;
698 gchar *action_string;
699 SummaryView *summaryview = NULL;
700 MainWindow *mainwin = NULL;
702 if (mainwindow_get_mainwindow()) {
703 summaryview = mainwindow_get_mainwindow()->summaryview;
704 mainwin = mainwindow_get_mainwindow();
707 if (NULL == (sbegin = strstr2(action, "%as{")))
708 return FALSE;
709 sbegin += sizeof "%as{" - 1;
710 if (NULL == (send = strrchr(sbegin, '}')))
711 return FALSE;
712 action_string = g_strndup(sbegin, send - sbegin);
714 action_list = matcher_parser_get_action_list(action_string);
715 if (action_list == NULL) {
716 gchar *tmp = g_strdup(action_string);
718 g_strstrip(tmp);
719 if (*tmp == '\0')
720 alertpanel_error(_("There is no filtering action set"));
721 else
722 alertpanel_error(_("Invalid filtering action(s):\n%s"), tmp);
723 g_free(action_string);
724 g_free(tmp);
725 return FALSE;
727 g_free(action_string);
729 /* apply actions on each message info */
730 for (p = msglist; p && p->data; p = g_slist_next(p)) {
731 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
734 if (summaryview) {
735 summary_lock(summaryview);
736 main_window_cursor_wait(mainwin);
737 summary_freeze(summaryview);
738 folder_item_update_freeze();
741 filtering_move_and_copy_msgs(msglist);
743 if (summaryview) {
744 folder_item_update_thaw();
745 summary_thaw(summaryview);
746 main_window_cursor_normal(mainwin);
747 summary_unlock(summaryview);
748 summary_show(summaryview, summaryview->folder_item);
750 for (p = action_list; p; p = g_slist_next(p))
751 if (p->data) filteringaction_free(p->data);
752 g_slist_free(action_list);
753 return TRUE;
756 static gboolean execute_actions(gchar *action, GSList *msg_list,
757 GtkWidget *text,
758 gint body_pos, MimeInfo *partinfo,
759 void (*callback)(void *data), void *data)
761 GSList *children_list = NULL, *cur = NULL;
762 gint is_ok = TRUE;
763 gint msg_list_len;
764 Children *children;
765 ChildInfo *child_info;
766 ActionType action_type;
767 MsgInfo *msginfo;
768 gchar *cmd;
769 gchar *sel_str = NULL;
770 gchar *msg_str = NULL;
771 gchar *user_str = NULL;
772 gchar *user_hidden_str = NULL;
773 GtkTextIter start_iter, end_iter;
774 gboolean is_selection = FALSE;
776 cm_return_val_if_fail(action && *action, FALSE);
778 action_type = action_get_type(action);
780 if (action_type == ACTION_ERROR)
781 return FALSE; /* ERR: syntax error */
783 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
784 return FALSE; /* ERR: file command without selection */
786 msg_list_len = g_slist_length(msg_list);
788 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
789 if (msg_list_len > 1)
790 return FALSE; /* ERR: pipe + multiple selection */
791 if (!text)
792 return FALSE; /* ERR: pipe and no displayed text */
795 if (action_type & ACTION_SELECTION_STR) {
796 if (!text)
797 return FALSE; /* ERR: selection string but no text */
800 if (text) {
801 GtkTextBuffer *textbuf;
803 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
804 is_selection = gtk_text_buffer_get_selection_bounds
805 (textbuf, &start_iter, &end_iter);
806 if (!is_selection) {
807 gtk_text_buffer_get_iter_at_offset
808 (textbuf, &start_iter, body_pos);
809 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
811 msg_str = gtk_text_buffer_get_text
812 (textbuf, &start_iter, &end_iter, FALSE);
813 if (is_selection)
814 sel_str = g_strdup(msg_str);
817 if (action_type & ACTION_USER_STR) {
818 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
819 g_free(msg_str);
820 g_free(sel_str);
821 return FALSE;
825 if (action_type & ACTION_USER_HIDDEN_STR) {
826 if (!(user_hidden_str =
827 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
828 g_free(msg_str);
829 g_free(sel_str);
830 g_free(user_str);
831 return FALSE;
835 if (text && (action_type & ACTION_PIPE_OUT)) {
836 GtkTextBuffer *textbuf;
837 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
838 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
841 children = g_new0(Children, 1);
843 children->action = g_strdup(action);
844 children->action_type = action_type;
845 children->msg_text = text;
846 children->is_selection = is_selection;
848 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
849 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
850 children->open_in = 1;
852 /* Pre-fetch bodies, makes it easier on IMAP (see bug #3011) */
853 for (cur = msg_list; cur; cur = cur->next) {
854 gchar *dummy;
855 msginfo = (MsgInfo *)cur->data;
857 dummy = procmsg_get_message_file((MsgInfo *)cur->data);
858 if (dummy)
859 g_free(dummy);
860 else
861 is_ok = FALSE;
864 if (is_ok && (action_type & ACTION_SINGLE)) {
865 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
866 msginfo = (MsgInfo *)cur->data;
867 if (!msginfo) {
868 is_ok = FALSE; /* ERR: msginfo missing */
869 break;
871 cmd = parse_action_cmd(action, msginfo, msg_list,
872 partinfo, user_str,
873 user_hidden_str, sel_str);
874 if (!cmd) {
875 debug_print("Action command error\n");
876 is_ok = FALSE; /* ERR: incorrect command */
877 break;
879 if ((child_info = fork_child(cmd, msg_str, children))) {
880 /* Pass msginfo to catch_status () */
881 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
882 child_info->msginfo_list =
883 g_slist_append (NULL, msginfo);
884 children_list = g_slist_append(children_list,
885 child_info);
887 g_free(cmd);
889 } else if (is_ok) {
890 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
891 user_str, user_hidden_str, sel_str);
892 if (cmd) {
893 if ((child_info = fork_child(cmd, msg_str, children))) {
894 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
895 child_info->msginfo_list =
896 g_slist_copy (msg_list);
897 children_list = g_slist_append(children_list,
898 child_info);
900 g_free(cmd);
901 } else
902 is_ok = FALSE; /* ERR: incorrect command */
905 g_free(msg_str);
906 g_free(sel_str);
907 g_free(user_str);
908 g_free(user_hidden_str);
910 if (!children_list) {
911 /* If not waiting for children, return */
912 free_children(children);
913 } else {
914 GSList *cur;
916 children->list = children_list;
917 children->nb = g_slist_length(children_list);
918 children->initial_nb = children->nb;
920 for (cur = children_list; cur; cur = cur->next) {
921 child_info = (ChildInfo *) cur->data;
922 child_info->callback = callback;
923 child_info->data = data;
924 child_info->tag_status =
925 g_child_watch_add(child_info->pid, catch_status, child_info);
928 create_io_dialog(children);
930 return is_ok;
933 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
934 Children *children)
936 gint chld_in, chld_out, chld_err;
937 gchar **argv, *ret_str, *trim_cmd;
938 GPid pid;
939 ChildInfo *child_info;
940 gint follow_child;
941 gssize by_written = 0, by_read = 0;
942 gboolean result = FALSE;
943 GError *error = NULL;
945 follow_child = !(children->action_type & ACTION_ASYNC);
947 chld_in = chld_out = chld_err = -1;
949 ret_str = g_locale_from_utf8(cmd, strlen(cmd),
950 &by_read, &by_written,
951 NULL);
952 if (!ret_str || !by_written)
953 ret_str = g_strdup(cmd);
955 trim_cmd = ret_str;
957 while (g_ascii_isspace(trim_cmd[0]))
958 trim_cmd++;
960 #ifdef G_OS_UNIX
961 argv = g_new0(gchar *, 4);
962 argv[0] = g_strdup("/bin/sh");
963 argv[1] = g_strdup("-c");
964 argv[2] = g_strdup(trim_cmd);
965 argv[3] = 0;
966 #else
967 argv = strsplit_with_quote(trim_cmd, " ", 0);
968 #endif
969 g_free(ret_str);
971 if (follow_child) {
972 result = g_spawn_async_with_pipes(NULL, argv, NULL,
973 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
974 NULL, NULL, &pid, &chld_in, &chld_out,
975 &chld_err, &error);
976 } else {
977 result = g_spawn_async(NULL, argv, NULL,
978 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH, NULL, NULL,
979 &pid, &error);
982 debug_print("spawning %s: %d\n", cmd, result);
984 g_strfreev(argv);
986 if (!result) {
987 alertpanel_error(_("Could not fork to execute the following "
988 "command:\n%s\n%s"),
989 cmd, error ? error->message : _("Unknown error"));
990 if (error)
991 g_error_free(error);
992 return NULL;
995 if (!(children->action_type &
996 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
997 (void)close(chld_in);
999 if (!follow_child) {
1000 g_spawn_close_pid(pid);
1001 return NULL;
1003 child_info = g_new0(ChildInfo, 1);
1005 child_info->children = children;
1007 child_info->pid = pid;
1008 #ifdef G_OS_UNIX
1009 child_info->next_sig = SIGTERM;
1010 #endif
1011 child_info->cmd = g_strdup(cmd);
1012 child_info->new_out = FALSE;
1013 child_info->output = g_string_new(NULL);
1014 child_info->chld_in =
1015 (children->action_type &
1016 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
1017 ? chld_in : -1;
1018 child_info->chld_out = chld_out;
1019 child_info->chld_err = chld_err;
1020 child_info->tag_status = -1;
1021 child_info->tag_in = -1;
1022 child_info->tag_out = claws_input_add(chld_out, G_IO_IN | G_IO_HUP | G_IO_ERR,
1023 catch_output, child_info, FALSE);
1024 child_info->tag_err = claws_input_add(chld_err, G_IO_IN | G_IO_HUP | G_IO_ERR,
1025 catch_output, child_info, FALSE);
1027 if (!(children->action_type &
1028 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
1029 return child_info;
1031 if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
1032 int r;
1033 ret_str = g_locale_from_utf8(msg_str, strlen(msg_str),
1034 &by_read, &by_written, NULL);
1035 if (ret_str && by_written) {
1036 r = write(chld_in, ret_str, strlen(ret_str));
1037 g_free(ret_str);
1038 } else
1039 r = write(chld_in, msg_str, strlen(msg_str));
1040 if (!(children->action_type &
1041 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
1042 r = close(chld_in);
1043 child_info->chld_in = -1; /* No more input */
1044 if (r != 0)
1045 debug_print("piping to child process: %s (%d)\n", g_strerror(errno), errno);
1048 return child_info;
1051 static void kill_children_cb(GtkWidget *widget, gpointer data)
1053 GSList *cur;
1054 Children *children = (Children *) data;
1055 ChildInfo *child_info;
1057 for (cur = children->list; cur; cur = cur->next) {
1058 child_info = (ChildInfo *)(cur->data);
1059 debug_print("Killing child group id %d\n", child_info->pid);
1060 #ifdef G_OS_UNIX
1061 if (child_info->pid && kill(child_info->pid, child_info->next_sig) < 0)
1062 perror("kill");
1063 child_info->next_sig = SIGKILL;
1064 #else
1065 TerminateProcess(child_info->pid, 0);
1066 #endif
1070 static gint wait_for_children(Children *children)
1072 gboolean new_output;
1073 ChildInfo *child_info;
1074 GSList *cur;
1076 cur = children->list;
1077 new_output = FALSE;
1078 while (cur) {
1079 child_info = (ChildInfo *)cur->data;
1080 new_output |= child_info->new_out;
1081 cur = cur->next;
1084 children->output |= new_output;
1086 if (new_output || (children->dialog && (children->initial_nb != children->nb)))
1087 update_io_dialog(children);
1089 if (children->nb)
1090 return FALSE;
1092 if (!children->dialog) {
1093 free_children(children);
1094 } else if (!children->output) {
1095 gtk_widget_destroy(children->dialog);
1098 return FALSE;
1101 static void send_input(GtkWidget *w, gpointer data)
1103 Children *children = (Children *) data;
1104 ChildInfo *child_info = (ChildInfo *) children->list->data;
1106 child_info->tag_in = claws_input_add(child_info->chld_in,
1107 G_IO_OUT | G_IO_ERR,
1108 catch_input, children, FALSE);
1111 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1113 hide_io_dialog_cb(w, data);
1114 return TRUE;
1117 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1120 Children *children = (Children *)data;
1122 if (!children->nb) {
1123 g_signal_handlers_disconnect_matched
1124 (G_OBJECT(children->dialog), G_SIGNAL_MATCH_DATA,
1125 0, 0, NULL, NULL, children);
1126 gtk_widget_destroy(children->dialog);
1127 free_children(children);
1131 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1132 gpointer data)
1134 if (event && (event->keyval == GDK_KEY_Escape ||
1135 event->keyval == GDK_KEY_Return ||
1136 event->keyval == GDK_KEY_KP_Enter))
1137 hide_io_dialog_cb(widget, data);
1138 return TRUE;
1141 static void childinfo_close_pipes(ChildInfo *child_info)
1143 /* stdout and stderr pipes are guaranteed to be removed by
1144 * their handler, but in case where we receive child exit notification
1145 * before grand-child's pipes closing signals, we check them and close
1146 * them if necessary
1148 if (child_info->tag_in > 0)
1149 g_source_remove(child_info->tag_in);
1150 if (child_info->tag_out > 0)
1151 g_source_remove(child_info->tag_out);
1152 if (child_info->tag_err > 0)
1153 g_source_remove(child_info->tag_err);
1155 if (child_info->chld_in >= 0)
1156 (void)close(child_info->chld_in);
1157 if (child_info->chld_out >= 0)
1158 (void)close(child_info->chld_out);
1159 if (child_info->chld_err >= 0)
1160 (void)close(child_info->chld_err);
1163 static void free_children(Children *children)
1165 ChildInfo *child_info;
1166 void (*callback)(void *data) = NULL;
1167 void *data = NULL;
1169 debug_print("Freeing children data %p\n", children);
1171 g_free(children->action);
1172 while (children->list != NULL) {
1173 child_info = (ChildInfo *)children->list->data;
1174 g_free(child_info->cmd);
1175 g_string_free(child_info->output, TRUE);
1176 children->list = g_slist_remove(children->list, child_info);
1177 callback = child_info->callback;
1178 data = child_info->data;
1179 g_free(child_info);
1182 if (callback)
1183 callback(data);
1185 g_free(children);
1188 static void update_io_dialog(Children *children)
1190 GSList *cur;
1192 debug_print("Updating actions input/output dialog.\n");
1194 if (children->progress_bar) {
1195 gchar *text;
1196 #ifdef GENERIC_UMPC
1197 /* use a more compact format */
1198 const gchar *format = "%s %d/%d";
1199 #else
1200 const gchar *format = "%s %d / %d";
1201 #endif
1203 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar),
1204 (children->initial_nb == 0) ? 0 :
1205 (gdouble) (children->initial_nb - children->nb) /
1206 (gdouble) children->initial_nb);
1207 text = g_strdup_printf(format, _("Completed"),
1208 children->initial_nb - children->nb,
1209 children->initial_nb);
1210 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(children->progress_bar), text);
1211 g_free(text);
1214 if (!children->nb) {
1215 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1216 gtk_widget_set_sensitive(children->close_btn, TRUE);
1217 if (children->input_hbox)
1218 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1219 gtk_widget_grab_focus(children->close_btn);
1220 g_signal_connect(G_OBJECT(children->dialog),
1221 "key_press_event",
1222 G_CALLBACK(io_dialog_key_pressed_cb),
1223 children);
1226 if (children->output) {
1227 GtkWidget *text = children->text;
1228 GtkTextBuffer *textbuf;
1229 GtkTextIter iter, start_iter, end_iter;
1230 gchar *caption;
1231 ChildInfo *child_info;
1233 gtk_widget_show(children->scrolledwin);
1234 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
1235 gtk_text_buffer_get_bounds(textbuf, &start_iter, &end_iter);
1236 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
1237 gtk_text_buffer_get_start_iter(textbuf, &iter);
1239 for (cur = children->list; cur; cur = cur->next) {
1240 child_info = (ChildInfo *)cur->data;
1241 if (child_info->pid)
1242 caption = g_strdup_printf
1243 (_("--- Running: %s\n"),
1244 child_info->cmd);
1245 else
1246 caption = g_strdup_printf
1247 (_("--- Ended: %s\n"),
1248 child_info->cmd);
1250 gtk_text_buffer_insert(textbuf, &iter, caption, -1);
1251 gtk_text_buffer_insert(textbuf, &iter,
1252 child_info->output->str, -1);
1253 g_free(caption);
1254 child_info->new_out = FALSE;
1259 static void create_io_dialog(Children *children)
1261 GtkWidget *dialog;
1262 GtkWidget *vbox;
1263 GtkWidget *entry = NULL;
1264 GtkWidget *input_hbox = NULL;
1265 GtkWidget *send_button;
1266 GtkWidget *label;
1267 GtkWidget *text;
1268 GtkWidget *scrolledwin;
1269 GtkWidget *hbox;
1270 GtkWidget *progress_bar = NULL;
1271 GtkWidget *abort_button;
1272 GtkWidget *close_button;
1274 debug_print("Creating action IO dialog\n");
1276 dialog = gtk_dialog_new();
1277 gtk_container_set_border_width
1278 (GTK_CONTAINER(gtk_dialog_get_action_area(GTK_DIALOG(dialog))), 5);
1279 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1280 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1281 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1282 manage_window_set_transient(GTK_WINDOW(dialog));
1283 g_signal_connect(G_OBJECT(dialog), "delete_event",
1284 G_CALLBACK(delete_io_dialog_cb), children);
1285 g_signal_connect(G_OBJECT(dialog), "destroy",
1286 G_CALLBACK(hide_io_dialog_cb),
1287 children);
1289 vbox = gtk_vbox_new(FALSE, 8);
1290 gtk_container_add(GTK_CONTAINER(
1291 gtk_dialog_get_content_area(GTK_DIALOG(dialog))), vbox);
1292 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1293 gtk_widget_show(vbox);
1295 label = gtk_label_new(children->action);
1296 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1297 gtk_widget_show(label);
1299 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1300 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1301 GTK_POLICY_AUTOMATIC,
1302 GTK_POLICY_AUTOMATIC);
1303 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
1304 GTK_SHADOW_IN);
1305 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1306 gtk_widget_set_size_request(scrolledwin, 560, 200);
1307 gtk_widget_hide(scrolledwin);
1309 text = gtk_text_view_new();
1311 if (prefs_common.textfont) {
1312 PangoFontDescription *font_desc;
1313 font_desc = pango_font_description_from_string
1314 (prefs_common.textfont);
1315 if (font_desc) {
1316 gtk_widget_modify_font(text, font_desc);
1317 pango_font_description_free(font_desc);
1321 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
1322 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
1323 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
1324 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
1325 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1326 gtk_widget_show(text);
1328 if (children->open_in) {
1329 input_hbox = gtk_hbox_new(FALSE, 8);
1330 gtk_widget_show(input_hbox);
1332 entry = gtk_entry_new();
1333 gtk_widget_set_size_request(entry, 320, -1);
1334 g_signal_connect(G_OBJECT(entry), "activate",
1335 G_CALLBACK(send_input), children);
1336 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1337 if (children->action_type & ACTION_USER_HIDDEN_IN) {
1338 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1340 gtk_widget_show(entry);
1342 send_button = gtk_button_new_from_stock(GTK_STOCK_EXECUTE);
1343 g_signal_connect(G_OBJECT(send_button), "clicked",
1344 G_CALLBACK(send_input), children);
1345 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1346 FALSE, 0);
1347 gtk_widget_show(send_button);
1349 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1350 gtk_widget_grab_focus(entry);
1353 if (children->initial_nb > 1) {
1354 gchar *text;
1355 #ifdef GENERIC_UMPC
1356 /* use a more compact format */
1357 const gchar *format = "%s 0/%d\n";
1358 #else
1359 const gchar *format = "%s 0 / %d\n";
1360 #endif
1362 progress_bar = gtk_progress_bar_new();
1363 #if !GTK_CHECK_VERSION(3, 0, 0)
1364 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1365 GTK_PROGRESS_LEFT_TO_RIGHT);
1366 #else
1367 gtk_orientable_set_orientation(GTK_ORIENTABLE(progress_bar),
1368 GTK_ORIENTATION_HORIZONTAL);
1369 gtk_progress_bar_set_inverted(GTK_PROGRESS_BAR(progress_bar),
1370 FALSE);
1371 #endif
1372 text = g_strdup_printf(format, _("Completed"),
1373 children->initial_nb);
1374 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar),
1375 text);
1376 g_free(text);
1377 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1378 gtk_widget_show(progress_bar);
1381 gtkut_stock_button_set_create(&hbox, &abort_button, GTK_STOCK_STOP,
1382 &close_button, GTK_STOCK_CLOSE, NULL, NULL);
1383 g_signal_connect(G_OBJECT(abort_button), "clicked",
1384 G_CALLBACK(kill_children_cb), children);
1385 g_signal_connect(G_OBJECT(close_button), "clicked",
1386 G_CALLBACK(hide_io_dialog_cb), children);
1387 gtk_widget_show(hbox);
1389 if (children->nb)
1390 gtk_widget_set_sensitive(close_button, FALSE);
1392 gtk_container_add(GTK_CONTAINER(
1393 gtk_dialog_get_action_area(GTK_DIALOG(dialog))), hbox);
1395 children->dialog = dialog;
1396 children->scrolledwin = scrolledwin;
1397 children->text = text;
1398 children->input_hbox = children->open_in ? input_hbox : NULL;
1399 children->input_entry = children->open_in ? entry : NULL;
1400 children->progress_bar = progress_bar;
1401 children->abort_btn = abort_button;
1402 children->close_btn = close_button;
1404 gtk_widget_show(dialog);
1407 static void catch_status(GPid pid, gint status, gpointer data)
1409 ChildInfo *child_info = (ChildInfo *)data;
1411 debug_print("Child returned %d\n", status);
1413 childinfo_close_pipes(child_info);
1414 g_spawn_close_pid(child_info->pid);
1415 child_info->pid = 0;
1417 if (child_info->children->action_type & (ACTION_SINGLE | ACTION_MULTIPLE)
1418 && child_info->msginfo_list) {
1419 /* Actions on message *files* might change size and
1420 * time stamp, and thus invalidate the cache */
1421 SummaryView *summaryview = NULL;
1422 GSList *cur;
1423 MsgInfo *msginfo, *nmi; /* newmsginfo */
1424 char *file;
1425 gboolean modified_something = FALSE;
1426 FolderItem *last_item = NULL;
1427 if (mainwindow_get_mainwindow ())
1428 summaryview = mainwindow_get_mainwindow ()->summaryview;
1429 for (cur = child_info->msginfo_list; cur; cur = cur->next) {
1430 msginfo = (MsgInfo *)cur->data;
1431 if (!(msginfo && /* Stuff used valid? */
1432 msginfo->folder && msginfo->folder->cache))
1433 continue;
1434 file = procmsg_get_message_file_path (msginfo);
1435 if (!file)
1436 continue;
1437 nmi = procheader_parse_file (file, msginfo->flags, TRUE, FALSE);
1438 if (!nmi)
1439 continue; /* Deleted? */
1440 if (msginfo->mtime != nmi->mtime || msginfo->size != nmi->size) {
1441 nmi->folder = msginfo->folder;
1442 nmi->msgnum = msginfo->msgnum;
1443 msgcache_update_msg (msginfo->folder->cache, nmi);
1444 modified_something = TRUE;
1445 last_item = nmi->folder;
1447 procmsg_msginfo_free (&nmi);
1448 if (summaryview && summaryview->displayed &&
1449 summaryview->folder_item == msginfo->folder &&
1450 summary_get_msgnum(summaryview, summaryview->displayed) == msginfo->msgnum)
1451 summary_redisplay_msg(summaryview);
1454 if (modified_something && last_item &&
1455 summaryview && summaryview->folder_item == last_item) {
1456 summary_show (summaryview, summaryview->folder_item);
1458 g_slist_free (child_info->msginfo_list);
1459 child_info->msginfo_list = NULL;
1462 if (!child_info->pid)
1463 child_info->children->nb--;
1465 wait_for_children(child_info->children);
1468 static void catch_input(gpointer data, gint source, GIOCondition cond)
1470 Children *children = (Children *)data;
1471 ChildInfo *child_info = (ChildInfo *)children->list->data;
1472 gchar *input, *ret_str;
1473 gint c, count, len, r;
1474 gssize by_read = 0, by_written = 0;
1476 debug_print("Sending input to grand child.\n");
1477 if (!(cond & (G_IO_OUT | G_IO_ERR)))
1478 return;
1480 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1481 gtk_widget_grab_focus(children->abort_btn);
1483 g_source_remove(child_info->tag_in);
1484 child_info->tag_in = -1;
1486 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1487 0, -1);
1488 ret_str = g_locale_from_utf8(input, strlen(input), &by_read,
1489 &by_written, NULL);
1490 if (ret_str && by_written) {
1491 g_free(input);
1492 input = ret_str;
1495 len = strlen(input);
1496 count = 0;
1498 do {
1499 c = write(child_info->chld_in, input + count, len - count);
1500 if (c >= 0)
1501 count += c;
1502 } while (c >= 0 && count < len);
1504 if (c >= 0)
1505 r = write(child_info->chld_in, "\n", 2);
1507 g_free(input);
1509 r = close(child_info->chld_in);
1510 child_info->chld_in = -1;
1511 if (r != 0)
1512 debug_print("closing child input fd: %s (%d)\n", g_strerror(errno), errno);
1513 child_info->chld_in = -1;
1514 debug_print("Input to grand child sent.\n");
1517 static void catch_output(gpointer data, gint source, GIOCondition cond)
1519 ChildInfo *child_info = (ChildInfo *)data;
1520 gint c;
1521 gchar buf[BUFFSIZE];
1523 debug_print("Catching grand child's output.\n");
1524 if (child_info->children->action_type &
1525 (ACTION_PIPE_OUT | ACTION_INSERT)
1526 && source == child_info->chld_out) {
1527 GtkTextView *text =
1528 GTK_TEXT_VIEW(child_info->children->msg_text);
1529 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
1530 GtkTextIter iter;
1531 GtkTextMark *mark;
1532 gint ins_pos;
1534 mark = gtk_text_buffer_get_insert(textbuf);
1535 gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1536 ins_pos = gtk_text_iter_get_offset(&iter);
1538 while (TRUE) {
1539 gsize bytes_read = 0, bytes_written = 0;
1540 gchar *ret_str;
1542 c = read(source, buf, sizeof(buf) - 1);
1543 if (c == 0)
1544 break;
1546 ret_str = g_locale_to_utf8
1547 (buf, c - 1, &bytes_read, &bytes_written, NULL);
1548 if (ret_str && bytes_written > 0) {
1549 gtk_text_buffer_insert
1550 (textbuf, &iter, ret_str,
1551 -1);
1552 g_free(ret_str);
1553 } else
1554 gtk_text_buffer_insert(textbuf, &iter, buf, c - 1);
1557 if (child_info->children->is_selection) {
1558 GtkTextIter ins;
1560 gtk_text_buffer_get_iter_at_offset
1561 (textbuf, &ins, ins_pos);
1562 gtk_text_buffer_select_range(textbuf, &ins, &iter);
1564 } else {
1565 c = read(source, buf, sizeof(buf) - 1);
1566 if (c > 0) {
1567 gsize bytes_read = 0, bytes_written = 0;
1568 gchar *ret_str;
1570 ret_str = g_locale_to_utf8
1571 (buf, c, &bytes_read, &bytes_written, NULL);
1572 if (ret_str && bytes_written > 0) {
1573 g_string_append_len
1574 (child_info->output, ret_str,
1575 bytes_written);
1576 g_free(ret_str);
1577 } else
1578 g_string_append_len(child_info->output, buf, c);
1580 child_info->new_out = TRUE;
1583 if (c == 0) {
1584 if (source == child_info->chld_out) {
1585 g_source_remove(child_info->tag_out);
1586 child_info->tag_out = -1;
1587 (void)close(child_info->chld_out);
1588 child_info->chld_out = -1;
1589 } else {
1590 g_source_remove(child_info->tag_err);
1591 child_info->tag_err = -1;
1592 (void)close(child_info->chld_err);
1593 child_info->chld_err = -1;
1597 wait_for_children(child_info->children);
1600 static gchar *get_user_string(const gchar *action, ActionType type)
1602 gchar *message;
1603 gchar *user_str = NULL;
1605 switch (type) {
1606 case ACTION_USER_HIDDEN_STR:
1607 message = g_strdup_printf
1608 (_("Enter the argument for the following action:\n"
1609 "('%%h' will be replaced with the argument)\n"
1610 " %s"),
1611 action);
1612 user_str = input_dialog_with_invisible
1613 (_("Action's hidden user argument"), message, NULL);
1614 break;
1615 case ACTION_USER_STR:
1616 message = g_strdup_printf
1617 (_("Enter the argument for the following action:\n"
1618 "('%%u' will be replaced with the argument)\n"
1619 " %s"),
1620 action);
1621 user_str = input_dialog
1622 (_("Action's user argument"), message, NULL);
1623 break;
1624 default:
1625 g_warning("Unsupported action type %d", type);
1628 return user_str;