2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 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 2 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include <gdk/gdkkeysyms.h>
34 #include <sys/types.h>
40 #include "prefs_gtk.h"
44 #include "manage_window.h"
45 #include "mainwindow.h"
46 #include "prefs_common.h"
47 #include "alertpanel.h"
48 #include "prefs_actions.h"
53 #include "description_window.h"
59 ACTION_PIPE_IN = 1 << 1,
60 ACTION_PIPE_OUT = 1 << 2,
61 ACTION_SINGLE = 1 << 3,
62 ACTION_MULTIPLE = 1 << 4,
63 ACTION_ASYNC = 1 << 5,
64 ACTION_USER_IN = 1 << 6,
65 ACTION_USER_HIDDEN_IN = 1 << 7,
66 ACTION_INSERT = 1 << 8,
67 ACTION_USER_STR = 1 << 9,
68 ACTION_USER_HIDDEN_STR = 1 << 10,
69 ACTION_SELECTION_STR = 1 << 11,
70 ACTION_ERROR = 1 << 30,
79 GtkWidget *name_entry;
82 GtkWidget *actions_clist;
85 typedef struct _Children Children;
86 typedef struct _ChildInfo ChildInfo;
87 typedef struct _UserStringDialog UserStringDialog;
93 GtkWidget *input_entry;
94 GtkWidget *input_hbox;
97 GtkWidget *scrolledwin;
126 struct _UserStringDialog {
132 /* widget creating functions */
133 static void prefs_actions_create (MainWindow *mainwin);
134 static void prefs_actions_set_dialog (void);
135 static gint prefs_actions_clist_set_row (gint row);
137 /* callback functions */
138 static void prefs_actions_help_cb (GtkWidget *w,
140 static void prefs_actions_register_cb (GtkWidget *w,
142 static void prefs_actions_substitute_cb (GtkWidget *w,
144 static void prefs_actions_delete_cb (GtkWidget *w,
146 static void prefs_actions_up (GtkWidget *w,
148 static void prefs_actions_down (GtkWidget *w,
150 static void prefs_actions_select (GtkCList *clist,
154 static void prefs_actions_row_move (GtkCList *clist,
157 static gint prefs_actions_deleted (GtkWidget *widget,
160 static void prefs_actions_key_pressed (GtkWidget *widget,
163 static void prefs_actions_cancel (GtkWidget *w,
165 static void prefs_actions_ok (GtkWidget *w,
167 static void update_actions_menu (GtkItemFactory *ifactory,
171 static void compose_actions_execute_cb (Compose *compose,
174 static void mainwin_actions_execute_cb (MainWindow *mainwin,
177 static void msgview_actions_execute_cb (MessageView *msgview,
180 static void message_actions_execute (MessageView *msgview,
183 static guint get_action_type (gchar *action);
185 static gboolean execute_actions (gchar *action,
192 static gchar *parse_action_cmd (gchar *action,
196 const gchar *user_str,
197 const gchar *user_hidden_str,
198 const gchar *sel_str);
199 static gboolean parse_append_filename (GString **cmd,
202 static gboolean parse_append_msgpart (GString **cmd,
206 ChildInfo *fork_child (gchar *cmd,
213 static gint wait_for_children (gpointer data);
215 static void free_children (Children *children);
217 static void childinfo_close_pipes (ChildInfo *child_info);
219 static void create_io_dialog (Children *children);
220 static void update_io_dialog (Children *children);
222 static void hide_io_dialog_cb (GtkWidget *widget,
224 static gint io_dialog_key_pressed_cb (GtkWidget *widget,
227 static void catch_output (gpointer data,
229 GdkInputCondition cond);
230 static void catch_input (gpointer data,
232 GdkInputCondition cond);
233 static void catch_status (gpointer data,
235 GdkInputCondition cond);
237 static gboolean user_string_dialog_delete_cb
241 static void user_string_dialog_destroy_cb
246 static void user_string_dialog_activate_cb
250 static gchar *get_user_string (const gchar *action,
253 void prefs_actions_open(MainWindow *mainwin)
256 if (prefs_rc_is_readonly(ACTIONS_RC))
262 prefs_actions_create(mainwin);
264 manage_window_set_transient(GTK_WINDOW(actions.window));
265 gtk_widget_grab_focus(actions.ok_btn);
267 prefs_actions_set_dialog();
269 gtk_widget_show(actions.window);
272 static void prefs_actions_create(MainWindow *mainwin)
277 GtkWidget *cancel_btn;
278 GtkWidget *confirm_area;
282 GtkWidget *entry_vbox;
284 GtkWidget *name_label;
285 GtkWidget *name_entry;
286 GtkWidget *cmd_label;
287 GtkWidget *cmd_entry;
293 GtkWidget *subst_btn;
296 GtkWidget *cond_hbox;
297 GtkWidget *cond_scrolledwin;
298 GtkWidget *cond_clist;
300 GtkWidget *help_button;
308 debug_print("Creating actions configuration window...\n");
310 window = gtk_window_new (GTK_WINDOW_DIALOG);
312 gtk_container_set_border_width(GTK_CONTAINER (window), 8);
313 gtk_window_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
314 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
315 gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, TRUE);
316 gtk_window_set_default_size(GTK_WINDOW(window), 400, -1);
318 vbox = gtk_vbox_new(FALSE, 6);
319 gtk_widget_show(vbox);
320 gtk_container_add(GTK_CONTAINER(window), vbox);
322 gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
323 &cancel_btn, _("Cancel"), NULL, NULL);
324 gtk_widget_show(confirm_area);
325 gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
326 gtk_widget_grab_default(ok_btn);
328 gtk_window_set_title(GTK_WINDOW(window), _("Actions configuration"));
329 gtk_signal_connect(GTK_OBJECT(window), "delete_event",
330 GTK_SIGNAL_FUNC(prefs_actions_deleted), NULL);
331 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
332 GTK_SIGNAL_FUNC(prefs_actions_key_pressed), NULL);
333 MANAGE_WINDOW_SIGNALS_CONNECT(window);
334 gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
335 GTK_SIGNAL_FUNC(prefs_actions_ok), mainwin);
336 gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
337 GTK_SIGNAL_FUNC(prefs_actions_cancel), NULL);
339 vbox1 = gtk_vbox_new(FALSE, 8);
340 gtk_widget_show(vbox1);
341 gtk_box_pack_start(GTK_BOX(vbox), vbox1, TRUE, TRUE, 0);
342 gtk_container_set_border_width(GTK_CONTAINER(vbox1), 2);
344 entry_vbox = gtk_vbox_new(FALSE, 4);
345 gtk_box_pack_start(GTK_BOX(vbox1), entry_vbox, FALSE, FALSE, 0);
347 hbox = gtk_hbox_new(FALSE, 8);
348 gtk_box_pack_start(GTK_BOX(entry_vbox), hbox, FALSE, FALSE, 0);
350 name_label = gtk_label_new(_("Menu name:"));
351 gtk_box_pack_start(GTK_BOX(hbox), name_label, FALSE, FALSE, 0);
353 name_entry = gtk_entry_new();
354 gtk_box_pack_start(GTK_BOX(hbox), name_entry, TRUE, TRUE, 0);
356 hbox = gtk_hbox_new(FALSE, 8);
357 gtk_box_pack_start(GTK_BOX(entry_vbox), hbox, TRUE, TRUE, 0);
359 cmd_label = gtk_label_new(_("Command line:"));
360 gtk_box_pack_start(GTK_BOX(hbox), cmd_label, FALSE, FALSE, 0);
362 cmd_entry = gtk_entry_new();
363 gtk_box_pack_start(GTK_BOX(hbox), cmd_entry, TRUE, TRUE, 0);
365 gtk_widget_show_all(entry_vbox);
367 /* register / substitute / delete */
369 reg_hbox = gtk_hbox_new(FALSE, 4);
370 gtk_widget_show(reg_hbox);
371 gtk_box_pack_start(GTK_BOX(vbox1), reg_hbox, FALSE, FALSE, 0);
373 arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
374 gtk_widget_show(arrow);
375 gtk_box_pack_start(GTK_BOX(reg_hbox), arrow, FALSE, FALSE, 0);
376 gtk_widget_set_usize(arrow, -1, 16);
378 btn_hbox = gtk_hbox_new(TRUE, 4);
379 gtk_widget_show(btn_hbox);
380 gtk_box_pack_start(GTK_BOX(reg_hbox), btn_hbox, FALSE, FALSE, 0);
382 reg_btn = gtk_button_new_with_label(_("Add"));
383 gtk_widget_show(reg_btn);
384 gtk_box_pack_start(GTK_BOX(btn_hbox), reg_btn, FALSE, TRUE, 0);
385 gtk_signal_connect(GTK_OBJECT(reg_btn), "clicked",
386 GTK_SIGNAL_FUNC(prefs_actions_register_cb), NULL);
388 subst_btn = gtk_button_new_with_label(_(" Replace "));
389 gtk_widget_show(subst_btn);
390 gtk_box_pack_start(GTK_BOX(btn_hbox), subst_btn, FALSE, TRUE, 0);
391 gtk_signal_connect(GTK_OBJECT(subst_btn), "clicked",
392 GTK_SIGNAL_FUNC(prefs_actions_substitute_cb),
395 del_btn = gtk_button_new_with_label(_("Delete"));
396 gtk_widget_show(del_btn);
397 gtk_box_pack_start(GTK_BOX(btn_hbox), del_btn, FALSE, TRUE, 0);
398 gtk_signal_connect(GTK_OBJECT(del_btn), "clicked",
399 GTK_SIGNAL_FUNC(prefs_actions_delete_cb), NULL);
401 help_button = gtk_button_new_with_label(_(" Syntax help "));
402 gtk_widget_show(help_button);
403 gtk_box_pack_end(GTK_BOX(reg_hbox), help_button, FALSE, FALSE, 0);
404 gtk_signal_connect(GTK_OBJECT(help_button), "clicked",
405 GTK_SIGNAL_FUNC(prefs_actions_help_cb), NULL);
407 cond_hbox = gtk_hbox_new(FALSE, 8);
408 gtk_widget_show(cond_hbox);
409 gtk_box_pack_start(GTK_BOX(vbox1), cond_hbox, TRUE, TRUE, 0);
411 cond_scrolledwin = gtk_scrolled_window_new(NULL, NULL);
412 gtk_widget_show(cond_scrolledwin);
413 gtk_widget_set_usize(cond_scrolledwin, -1, 150);
414 gtk_box_pack_start(GTK_BOX(cond_hbox), cond_scrolledwin,
416 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (cond_scrolledwin),
417 GTK_POLICY_AUTOMATIC,
418 GTK_POLICY_AUTOMATIC);
420 title[0] = _("Current actions");
421 cond_clist = gtk_clist_new_with_titles(1, title);
422 gtk_widget_show(cond_clist);
423 gtk_container_add(GTK_CONTAINER (cond_scrolledwin), cond_clist);
424 gtk_clist_set_column_width(GTK_CLIST (cond_clist), 0, 80);
425 gtk_clist_set_selection_mode(GTK_CLIST (cond_clist),
426 GTK_SELECTION_BROWSE);
427 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(cond_clist)->column[0].button,
429 gtk_signal_connect(GTK_OBJECT(cond_clist), "select_row",
430 GTK_SIGNAL_FUNC(prefs_actions_select), NULL);
431 gtk_signal_connect_after(GTK_OBJECT(cond_clist), "row_move",
432 GTK_SIGNAL_FUNC(prefs_actions_row_move),
435 btn_vbox = gtk_vbox_new(FALSE, 8);
436 gtk_widget_show(btn_vbox);
437 gtk_box_pack_start(GTK_BOX(cond_hbox), btn_vbox, FALSE, FALSE, 0);
439 up_btn = gtk_button_new_with_label(_("Up"));
440 gtk_widget_show(up_btn);
441 gtk_box_pack_start(GTK_BOX(btn_vbox), up_btn, FALSE, FALSE, 0);
442 gtk_signal_connect(GTK_OBJECT(up_btn), "clicked",
443 GTK_SIGNAL_FUNC(prefs_actions_up), NULL);
445 down_btn = gtk_button_new_with_label(_("Down"));
446 gtk_widget_show(down_btn);
447 gtk_box_pack_start(GTK_BOX(btn_vbox), down_btn, FALSE, FALSE, 0);
448 gtk_signal_connect(GTK_OBJECT(down_btn), "clicked",
449 GTK_SIGNAL_FUNC(prefs_actions_down), NULL);
451 gtk_widget_show(window);
453 actions.window = window;
454 actions.ok_btn = ok_btn;
456 actions.name_entry = name_entry;
457 actions.cmd_entry = cmd_entry;
459 actions.actions_clist = cond_clist;
463 void prefs_actions_read_config(void)
467 gchar buf[PREFSBUFSIZE];
470 debug_print("Reading actions configurations...\n");
472 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
473 if ((fp = fopen(rcpath, "rb")) == NULL) {
474 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
480 while (prefs_common.actions_list != NULL) {
481 act = (gchar *)prefs_common.actions_list->data;
482 prefs_common.actions_list =
483 g_slist_remove(prefs_common.actions_list, act);
487 while (fgets(buf, sizeof(buf), fp) != NULL) {
489 act = strstr(buf, ": ");
491 get_action_type(&act[2]) != ACTION_ERROR)
492 prefs_common.actions_list =
493 g_slist_append(prefs_common.actions_list,
499 void prefs_actions_write_config(void)
505 debug_print("Writing actions configuration...\n");
507 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
508 if ((pfile= prefs_write_open(rcpath)) == NULL) {
509 g_warning("failed to write configuration to file\n");
514 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
515 gchar *act = (gchar *)cur->data;
516 if (fputs(act, pfile->fp) == EOF ||
517 fputc('\n', pfile->fp) == EOF) {
518 FILE_OP_ERROR(rcpath, "fputs || fputc");
519 prefs_file_close_revert(pfile);
527 if (prefs_file_close(pfile) < 0) {
528 g_warning("failed to write configuration to file\n");
533 static guint get_action_type(gchar *action)
536 guint action_type = ACTION_NONE;
538 g_return_val_if_fail(action, ACTION_ERROR);
539 g_return_val_if_fail(*action, ACTION_ERROR);
544 action_type |= ACTION_PIPE_IN;
546 } else if (p[0] == '>') {
547 action_type |= ACTION_USER_IN;
549 } else if (p[0] == '*') {
550 action_type |= ACTION_USER_HIDDEN_IN;
557 while (*p && action_type != ACTION_ERROR) {
561 action_type |= ACTION_SINGLE;
564 action_type |= ACTION_MULTIPLE;
567 action_type |= ACTION_SINGLE;
570 action_type |= ACTION_SELECTION_STR;
573 action_type |= ACTION_USER_STR;
576 action_type |= ACTION_USER_HIDDEN_STR;
579 action_type = ACTION_ERROR;
582 } else if (p[0] == '|') {
584 action_type |= ACTION_PIPE_OUT;
585 } else if (p[0] == '>') {
587 action_type |= ACTION_INSERT;
588 } else if (p[0] == '&') {
590 action_type |= ACTION_ASYNC;
598 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
599 GtkCTree *ctree, MimeView *mimeview,
600 const gchar *user_str,
601 const gchar *user_hidden_str,
602 const gchar *sel_str)
611 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
614 cmd = g_string_sized_new(strlen(action));
617 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
618 if (p[0] == '%' && p[1]) {
621 if (!parse_append_filename(&cmd, msginfo)) {
622 g_string_free(cmd, TRUE);
628 for (cur = GTK_CLIST(ctree)->selection;
629 cur != NULL; cur = cur->next) {
630 msg = gtk_ctree_node_get_row_data(ctree,
631 GTK_CTREE_NODE(cur->data));
632 if (!parse_append_filename(&cmd, msg)) {
633 g_string_free(cmd, TRUE);
637 cmd = g_string_append_c(cmd, ' ');
643 if (!parse_append_msgpart(&cmd, msginfo,
645 g_string_free(cmd, TRUE);
652 cmd = g_string_append(cmd, sel_str);
658 cmd = g_string_append(cmd, user_str);
663 cmd = g_string_append(cmd,
669 cmd = g_string_append_c(cmd, p[0]);
670 cmd = g_string_append_c(cmd, p[1]);
674 cmd = g_string_append_c(cmd, p[0]);
679 g_string_free(cmd, TRUE);
684 g_string_free(cmd, FALSE);
688 static gboolean parse_append_filename(GString **cmd, MsgInfo *msginfo)
692 g_return_val_if_fail(msginfo, FALSE);
694 filename = procmsg_get_message_file(msginfo);
697 *cmd = g_string_append(*cmd, filename);
700 alertpanel_error(_("Could not get message file %d"),
708 static gboolean parse_append_msgpart(GString **cmd, MsgInfo *msginfo,
719 if ((fp = procmsg_open_message_decrypted(msginfo, &partinfo))
721 alertpanel_error(_("Could not get message file."));
725 if ((fp = procmsg_open_message(msginfo)) == NULL) {
726 alertpanel_error(_("Could not get message file."));
729 partinfo = procmime_scan_mime_header(fp);
733 procmime_mimeinfo_free_all(partinfo);
734 alertpanel_error(_("Could not get message part."));
737 filename = procmsg_get_message_file(msginfo);
739 if (!mimeview->opened) {
740 alertpanel_error(_("No message part selected."));
743 if (!mimeview->file) {
744 alertpanel_error(_("No message file selected."));
747 partinfo = gtk_ctree_node_get_row_data
748 (GTK_CTREE(mimeview->ctree),
750 g_return_val_if_fail(partinfo != NULL, FALSE);
751 filename = mimeview->file;
753 partname = procmime_get_tmp_file_name(partinfo);
755 ret = procmime_get_part(partname, filename, partinfo);
758 procmime_mimeinfo_free_all(partinfo);
763 alertpanel_error(_("Can't get part of multipart message"));
768 *cmd = g_string_append(*cmd, partname);
775 static void prefs_actions_set_dialog(void)
777 GtkCList *clist = GTK_CLIST(actions.actions_clist);
779 gchar *action_str[1];
782 gtk_clist_freeze(clist);
783 gtk_clist_clear(clist);
785 action_str[0] = _("(New)");
786 row = gtk_clist_append(clist, action_str);
787 gtk_clist_set_row_data(clist, row, NULL);
789 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
792 action[0] = (gchar *)cur->data;
793 row = gtk_clist_append(clist, action);
794 gtk_clist_set_row_data(clist, row, action[0]);
797 gtk_clist_thaw(clist);
800 static void prefs_actions_set_list(void)
805 g_slist_free(prefs_common.actions_list);
806 prefs_common.actions_list = NULL;
808 while ((action = (gchar *)gtk_clist_get_row_data
809 (GTK_CLIST(actions.actions_clist), row)) != NULL) {
810 prefs_common.actions_list =
811 g_slist_append(prefs_common.actions_list, action);
816 #define GET_ENTRY(entry) \
817 entry_text = gtk_entry_get_text(GTK_ENTRY(entry))
819 static gint prefs_actions_clist_set_row(gint row)
821 GtkCList *clist = GTK_CLIST(actions.actions_clist);
824 gchar action[PREFSBUFSIZE];
827 g_return_val_if_fail(row != 0, -1);
829 GET_ENTRY(actions.name_entry);
830 if (entry_text[0] == '\0') {
831 alertpanel_error(_("Menu name is not set."));
835 if (strchr(entry_text, ':')) {
836 alertpanel_error(_("Colon ':' is not allowed in the menu name."));
840 strncpy(action, entry_text, PREFSBUFSIZE - 1);
843 /* Keep space for the ': ' delimiter */
844 len = strlen(action) + 2;
845 if (len >= PREFSBUFSIZE - 1) {
846 alertpanel_error(_("Menu name is too long."));
850 strcat(action, ": ");
852 GET_ENTRY(actions.cmd_entry);
854 if (entry_text[0] == '\0') {
855 alertpanel_error(_("Command line not set."));
859 if (len + strlen(entry_text) >= PREFSBUFSIZE - 1) {
860 alertpanel_error(_("Menu name and command are too long."));
864 if (get_action_type(entry_text) == ACTION_ERROR) {
865 alertpanel_error(_("The command\n%s\nhas a syntax error."),
870 strcat(action, entry_text);
874 row = gtk_clist_append(clist, buf);
877 gtk_clist_set_text(clist, row, 0, action);
878 old_action = (gchar *) gtk_clist_get_row_data(clist, row);
883 buf[0] = g_strdup(action);
885 gtk_clist_set_row_data(clist, row, buf[0]);
887 prefs_actions_set_list();
892 /* callback functions */
894 static void prefs_actions_register_cb(GtkWidget *w, gpointer data)
896 prefs_actions_clist_set_row(-1);
899 static void prefs_actions_substitute_cb(GtkWidget *w, gpointer data)
901 GtkCList *clist = GTK_CLIST(actions.actions_clist);
905 if (!clist->selection) return;
907 row = GPOINTER_TO_INT(clist->selection->data);
908 if (row == 0) return;
910 action = gtk_clist_get_row_data(clist, row);
913 prefs_actions_clist_set_row(row);
916 static void prefs_actions_delete_cb(GtkWidget *w, gpointer data)
918 GtkCList *clist = GTK_CLIST(actions.actions_clist);
922 if (!clist->selection) return;
923 row = GPOINTER_TO_INT(clist->selection->data);
924 if (row == 0) return;
926 if (alertpanel(_("Delete action"),
927 _("Do you really want to delete this action?"),
928 _("Yes"), _("No"), NULL) == G_ALERTALTERNATE)
931 action = gtk_clist_get_row_data(clist, row);
933 gtk_clist_remove(clist, row);
934 prefs_common.actions_list = g_slist_remove(prefs_common.actions_list,
938 static void prefs_actions_up(GtkWidget *w, gpointer data)
940 GtkCList *clist = GTK_CLIST(actions.actions_clist);
943 if (!clist->selection) return;
945 row = GPOINTER_TO_INT(clist->selection->data);
947 gtk_clist_row_move(clist, row, row - 1);
950 static void prefs_actions_down(GtkWidget *w, gpointer data)
952 GtkCList *clist = GTK_CLIST(actions.actions_clist);
955 if (!clist->selection) return;
957 row = GPOINTER_TO_INT(clist->selection->data);
958 if (row > 0 && row < clist->rows - 1)
959 gtk_clist_row_move(clist, row, row + 1);
962 #define ENTRY_SET_TEXT(entry, str) \
963 gtk_entry_set_text(GTK_ENTRY(entry), str ? str : "")
965 static void prefs_actions_select(GtkCList *clist, gint row, gint column,
970 gchar buf[PREFSBUFSIZE];
971 action = gtk_clist_get_row_data(clist, row);
974 ENTRY_SET_TEXT(actions.name_entry, "");
975 ENTRY_SET_TEXT(actions.cmd_entry, "");
979 strncpy(buf, action, PREFSBUFSIZE - 1);
980 buf[PREFSBUFSIZE - 1] = 0x00;
981 cmd = strstr(buf, ": ");
984 ENTRY_SET_TEXT(actions.cmd_entry, &cmd[2]);
989 ENTRY_SET_TEXT(actions.name_entry, buf);
992 static void prefs_actions_row_move(GtkCList *clist,
993 gint source_row, gint dest_row)
995 prefs_actions_set_list();
996 if (gtk_clist_row_is_visible(clist, dest_row) != GTK_VISIBILITY_FULL) {
997 gtk_clist_moveto(clist, dest_row, -1,
998 source_row < dest_row ? 1.0 : 0.0, 0.0);
1002 static gint prefs_actions_deleted(GtkWidget *widget, GdkEventAny *event,
1005 prefs_actions_cancel(widget, data);
1009 static void prefs_actions_key_pressed(GtkWidget *widget, GdkEventKey *event,
1012 if (event && event->keyval == GDK_Escape)
1013 prefs_actions_cancel(widget, data);
1016 static void prefs_actions_cancel(GtkWidget *w, gpointer data)
1018 prefs_actions_read_config();
1019 gtk_widget_hide(actions.window);
1023 static void prefs_actions_ok(GtkWidget *widget, gpointer data)
1025 GtkItemFactory *ifactory;
1026 MainWindow *mainwin = (MainWindow *)data;
1028 prefs_actions_write_config();
1029 ifactory = gtk_item_factory_from_widget(mainwin->menubar);
1030 update_mainwin_actions_menu(ifactory, mainwin);
1031 gtk_widget_hide(actions.window);
1035 void update_mainwin_actions_menu(GtkItemFactory *ifactory,
1036 MainWindow *mainwin)
1038 update_actions_menu(ifactory, "/Tools/Actions",
1039 mainwin_actions_execute_cb,
1043 void update_compose_actions_menu(GtkItemFactory *ifactory,
1047 update_actions_menu(ifactory, branch_path,
1048 compose_actions_execute_cb,
1053 void actions_execute(gpointer data,
1058 if (source == TOOLBAR_MAIN)
1059 mainwin_actions_execute_cb((MainWindow*)data, action_nb, widget);
1060 else if (source == TOOLBAR_COMPOSE)
1061 compose_actions_execute_cb((Compose*)data, action_nb, widget);
1062 else if (source == TOOLBAR_MSGVIEW)
1063 msgview_actions_execute_cb((MessageView*)data, action_nb, widget);
1067 static void update_actions_menu(GtkItemFactory *ifactory,
1072 GtkWidget *menuitem;
1075 gchar *action, *action_p;
1077 GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
1079 ifentry.path = branch_path;
1080 menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
1081 g_return_if_fail(menuitem != NULL);
1083 amenu = GTK_MENU_SHELL(menuitem)->children;
1084 while (amenu != NULL) {
1085 GList *alist = amenu->next;
1086 gtk_widget_destroy(GTK_WIDGET(amenu->data));
1090 ifentry.accelerator = NULL;
1091 ifentry.callback_action = 0;
1092 ifentry.callback = callback;
1093 ifentry.item_type = NULL;
1095 for (cur = prefs_common.actions_list; cur; cur = cur->next) {
1096 action = g_strdup((gchar *)cur->data);
1097 action_p = strstr(action, ": ");
1098 if (action_p && action_p[2] &&
1099 get_action_type(&action_p[2]) != ACTION_ERROR) {
1101 menu_path = g_strdup_printf("%s/%s", branch_path,
1103 ifentry.path = menu_path;
1104 gtk_item_factory_create_item(ifactory, &ifentry, data,
1109 ifentry.callback_action++;
1113 static void compose_actions_execute_cb(Compose *compose, guint action_nb,
1116 gchar *buf, *action;
1119 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
1121 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
1122 g_return_if_fail(buf != NULL);
1123 action = strstr(buf, ": ");
1124 g_return_if_fail(action != NULL);
1126 /* Point to the beginning of the command-line */
1129 action_type = get_action_type(action);
1130 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
1132 (_("The selected action cannot be used in the compose window\n"
1133 "because it contains %%f, %%F or %%p."));
1137 execute_actions(action, NULL, compose->text, NULL, 0, NULL);
1140 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
1143 message_actions_execute(mainwin->messageview, action_nb,
1144 GTK_CTREE(mainwin->summaryview->ctree));
1147 static void msgview_actions_execute_cb(MessageView *msgview, guint action_nb,
1150 message_actions_execute(msgview, action_nb, NULL);
1154 static void message_actions_execute(MessageView *msgview, guint action_nb,
1157 TextView *textview = NULL;
1160 MimeView *mimeview = NULL;
1161 GdkFont *msgfont = NULL;
1163 GtkWidget *text = NULL;
1165 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
1167 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
1169 g_return_if_fail(buf);
1170 g_return_if_fail(action = strstr(buf, ": "));
1172 /* Point to the beginning of the command-line */
1175 switch (msgview->type) {
1177 if (msgview->textview && msgview->textview->text)
1178 textview = msgview->textview;
1181 if (msgview->mimeview) {
1182 mimeview = msgview->mimeview;
1183 if (msgview->mimeview->type == MIMEVIEW_TEXT &&
1184 msgview->mimeview->textview &&
1185 msgview->mimeview->textview->text)
1186 textview = msgview->mimeview->textview;
1192 text = textview->text;
1193 msgfont = textview->msgfont;
1194 body_pos = textview->body_pos;
1197 execute_actions(action, ctree, text, msgfont, body_pos, mimeview);
1200 static gboolean execute_actions(gchar *action,
1207 GList *cur, *selection = NULL;
1208 GSList *children_list = NULL;
1210 gint selection_len = 0;
1212 ChildInfo *child_info;
1216 gchar *sel_str = NULL;
1217 gchar *user_str = NULL;
1218 gchar *user_hidden_str = NULL;
1220 g_return_val_if_fail(action && *action, FALSE);
1222 action_type = get_action_type(action);
1224 if (action_type == ACTION_ERROR)
1225 return FALSE; /* ERR: syntax error */
1227 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) &&
1228 !(ctree && GTK_CLIST(ctree)->selection))
1229 return FALSE; /* ERR: file command without selection */
1232 selection = GTK_CLIST(ctree)->selection;
1233 selection_len = g_list_length(selection);
1236 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
1237 if (ctree && selection_len > 1)
1238 return FALSE; /* ERR: pipe + multiple selection */
1240 return FALSE; /* ERR: pipe and no displayed text */
1243 if (action_type & ACTION_SELECTION_STR) {
1245 return FALSE; /* ERR: selection string but no text */
1247 guint start = 0, end = 0;
1248 if (GTK_EDITABLE(text)->has_selection) {
1249 start = GTK_EDITABLE(text)->selection_start_pos;
1250 end = GTK_EDITABLE(text)->selection_end_pos;
1258 sel_str = gtk_editable_get_chars(GTK_EDITABLE(text),
1265 if (action_type & (ACTION_USER_STR))
1266 if (!(user_str = get_user_string(action, ACTION_USER_STR)))
1269 if (action_type & (ACTION_USER_HIDDEN_STR))
1270 if (!(user_hidden_str = get_user_string(action,
1271 ACTION_USER_HIDDEN_STR)))
1274 children = g_new0(Children, 1);
1276 if (action_type & ACTION_SINGLE) {
1277 for (cur = selection; cur && is_ok == TRUE; cur = cur->next) {
1278 msginfo = gtk_ctree_node_get_row_data(ctree,
1279 GTK_CTREE_NODE(cur->data));
1281 is_ok = FALSE; /* ERR: msginfo missing */
1284 cmd = parse_action_cmd(action, msginfo, ctree,
1286 user_hidden_str, sel_str);
1288 debug_print("Action command error\n");
1289 is_ok = FALSE; /* ERR: incorrect command */
1292 if ((child_info = fork_child(cmd, action_type, text,
1295 children_list = g_slist_append(children_list,
1297 children->open_in = (selection_len == 1) ?
1300 ACTION_USER_HIDDEN_IN)) : 0;
1305 cmd = parse_action_cmd(action, NULL, ctree, mimeview, user_str,
1306 user_hidden_str, sel_str);
1308 if ((child_info = fork_child(cmd, action_type, text,
1311 children_list = g_slist_append(children_list,
1313 children->open_in = action_type &
1315 ACTION_USER_HIDDEN_IN);
1319 is_ok = FALSE; /* ERR: incorrect command */
1324 if (user_hidden_str)
1325 g_free(user_hidden_str);
1327 if (!children_list) {
1328 /* If not waiting for children, return */
1333 children->action = g_strdup(action);
1334 children->dialog = NULL;
1335 children->list = children_list;
1336 children->nb = g_slist_length(children_list);
1338 for (cur = children_list; cur; cur = cur->next) {
1339 child_info = (ChildInfo *) cur->data;
1340 child_info->tag_status =
1341 gdk_input_add(child_info->chld_status,
1343 catch_status, child_info);
1346 create_io_dialog(children);
1352 ChildInfo *fork_child(gchar *cmd, gint action_type, GtkWidget *text,
1353 GdkFont *msgfont, gint body_pos, Children *children)
1355 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
1361 ChildInfo *child_info;
1364 sync = !(action_type & ACTION_ASYNC);
1366 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
1367 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
1370 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
1372 alertpanel_error(_("Command could not be started. "
1373 "Pipe creation failed.\n%s"),
1375 /* Closing fd = -1 fails silently */
1382 close(chld_status[0]);
1383 close(chld_status[1]);
1384 return NULL; /* Pipe error */
1388 debug_print("Forking child and grandchild.\n");
1391 if (pid == 0) { /* Child */
1395 close(ConnectionNumber(gdk_display));
1400 if (setpgid(0, getppid()))
1406 ACTION_USER_HIDDEN_IN)) {
1407 close(fileno(stdin));
1413 close(fileno(stdout));
1418 close(fileno(stderr));
1428 execvp("/bin/sh", cmdline);
1432 } else if (gch_pid < (pid_t) 0) { /* Fork error */
1434 write(chld_status[1], "1\n", 2);
1445 close(chld_status[0]);
1448 debug_print("Child: Waiting for grandchild\n");
1449 waitpid(gch_pid, NULL, 0);
1450 debug_print("Child: grandchild ended\n");
1451 write(chld_status[1], "0\n", 2);
1452 close(chld_status[1]);
1456 } else if (pid < 0) { /* Fork error */
1457 alertpanel_error(_("Could not fork to execute the following "
1458 "command:\n%s\n%s"),
1459 cmd, g_strerror(errno));
1466 waitpid(pid, NULL, 0);
1471 if (!(action_type & (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
1475 close(chld_status[1]);
1477 child_info = g_new0(ChildInfo, 1);
1479 child_info->children = children;
1481 child_info->pid = pid;
1482 child_info->cmd = g_strdup(cmd);
1483 child_info->type = action_type;
1484 child_info->new_out = FALSE;
1485 child_info->output = g_string_sized_new(0);
1486 child_info->chld_in =
1488 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
1490 child_info->chld_out = chld_out[0];
1491 child_info->chld_err = chld_err[0];
1492 child_info->chld_status = chld_status[0];
1493 child_info->tag_in = -1;
1494 child_info->tag_out = gdk_input_add(chld_out[0], GDK_INPUT_READ,
1495 catch_output, child_info);
1496 child_info->tag_err = gdk_input_add(chld_err[0], GDK_INPUT_READ,
1497 catch_output, child_info);
1499 if (!(action_type & (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
1502 child_info->text = text;
1503 child_info->msgfont = msgfont;
1506 end = gtk_stext_get_length(GTK_STEXT(text));
1508 if (GTK_EDITABLE(text)->has_selection) {
1509 start = GTK_EDITABLE(text)->selection_start_pos;
1510 end = GTK_EDITABLE(text)->selection_end_pos;
1517 is_selection = TRUE;
1520 end = gtk_stext_get_length(GTK_STEXT(text));
1521 is_selection = FALSE;
1525 selection = gtk_editable_get_chars(GTK_EDITABLE(text), start, end);
1527 if (action_type & ACTION_PIPE_IN) {
1528 write(chld_in[1], selection, strlen(selection));
1529 if (!(action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
1531 child_info->chld_in = -1; /* No more input */
1535 gtk_stext_freeze(GTK_STEXT(text));
1536 if (action_type & ACTION_PIPE_OUT) {
1537 gtk_stext_set_point(GTK_STEXT(text), start);
1538 gtk_stext_forward_delete(GTK_STEXT(text), end - start);
1541 gtk_stext_thaw(GTK_STEXT(text));
1546 static void kill_children_cb(GtkWidget *widget, gpointer data)
1549 Children *children = (Children *) data;
1550 ChildInfo *child_info;
1552 for (cur = children->list; cur; cur = cur->next) {
1553 child_info = (ChildInfo *)(cur->data);
1554 debug_print("Killing child group id %d\n", child_info->pid);
1555 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
1560 static gint wait_for_children(gpointer data)
1562 gboolean new_output;
1563 Children *children = (Children *)data;
1564 ChildInfo *child_info;
1566 gint nb = children->nb;
1570 cur = children->list;
1573 child_info = (ChildInfo *)cur->data;
1574 if (child_info->pid)
1576 new_output |= child_info->new_out;
1580 children->output |= new_output;
1582 if (new_output || (children->dialog && (nb != children->nb)))
1583 update_io_dialog(children);
1588 if (!children->dialog) {
1589 free_children(children);
1590 } else if (!children->output) {
1591 gtk_widget_destroy(children->dialog);
1597 static void send_input(GtkWidget *w, gpointer data)
1599 Children *children = (Children *) data;
1600 ChildInfo *child_info = (ChildInfo *) children->list->data;
1602 child_info->tag_in = gdk_input_add(child_info->chld_in,
1604 catch_input, children);
1605 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1608 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1610 hide_io_dialog_cb(w, data);
1614 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1617 Children *children = (Children *)data;
1619 if (!children->nb) {
1620 gtk_signal_disconnect_by_data(GTK_OBJECT(children->dialog),
1622 gtk_widget_destroy(children->dialog);
1623 free_children(children);
1627 static gint io_dialog_key_pressed_cb(GtkWidget *widget,
1631 if (event && event->keyval == GDK_Escape)
1632 hide_io_dialog_cb(widget, data);
1636 static void childinfo_close_pipes(ChildInfo *child_info)
1638 if (child_info->tag_in > 0)
1639 gdk_input_remove(child_info->tag_in);
1640 gdk_input_remove(child_info->tag_out);
1641 gdk_input_remove(child_info->tag_err);
1643 if (child_info->chld_in >= 0)
1644 close(child_info->chld_in);
1645 close(child_info->chld_out);
1646 close(child_info->chld_err);
1647 close(child_info->chld_status);
1650 static void free_children(Children *children)
1653 ChildInfo *child_info;
1655 debug_print("Freeing children data %p\n", children);
1657 g_free(children->action);
1658 for (cur = children->list; cur;) {
1659 child_info = (ChildInfo *)cur->data;
1660 g_free(child_info->cmd);
1661 g_string_free(child_info->output, TRUE);
1662 children->list = g_slist_remove(children->list, child_info);
1664 cur = children->list;
1669 static void update_io_dialog(Children *children)
1673 debug_print("Updating actions input/output dialog.\n");
1675 if (!children->nb) {
1676 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1677 gtk_widget_set_sensitive(children->close_btn, TRUE);
1678 if (children->input_hbox)
1679 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1680 gtk_widget_grab_focus(children->close_btn);
1681 gtk_signal_connect(GTK_OBJECT(children->dialog),
1683 GTK_SIGNAL_FUNC(io_dialog_key_pressed_cb),
1687 if (children->output) {
1688 GtkWidget *text = children->text;
1690 ChildInfo *child_info;
1692 gtk_widget_show(children->scrolledwin);
1693 gtk_text_freeze(GTK_TEXT(text));
1694 gtk_text_set_point(GTK_TEXT(text), 0);
1695 gtk_text_forward_delete(GTK_TEXT(text),
1696 gtk_text_get_length(GTK_TEXT(text)));
1697 for (cur = children->list; cur; cur = cur->next) {
1698 child_info = (ChildInfo *)cur->data;
1699 if (child_info->pid)
1700 caption = g_strdup_printf
1701 (_("--- Running: %s\n"),
1704 caption = g_strdup_printf
1705 (_("--- Ended: %s\n"),
1708 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1710 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1711 child_info->output->str, -1);
1713 child_info->new_out = FALSE;
1715 gtk_text_thaw(GTK_TEXT(text));
1719 static void create_io_dialog(Children *children)
1723 GtkWidget *entry = NULL;
1724 GtkWidget *input_hbox = NULL;
1725 GtkWidget *send_button;
1728 GtkWidget *scrolledwin;
1730 GtkWidget *abort_button;
1731 GtkWidget *close_button;
1733 debug_print("Creating action IO dialog\n");
1735 dialog = gtk_dialog_new();
1736 gtk_container_set_border_width
1737 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1738 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1739 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1740 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1741 manage_window_set_transient(GTK_WINDOW(dialog));
1742 gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
1743 GTK_SIGNAL_FUNC(delete_io_dialog_cb), children);
1744 gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
1745 GTK_SIGNAL_FUNC(hide_io_dialog_cb),
1748 vbox = gtk_vbox_new(FALSE, 8);
1749 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1750 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1751 gtk_widget_show(vbox);
1753 label = gtk_label_new(children->action);
1754 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1755 gtk_widget_show(label);
1757 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1758 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1759 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
1760 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1761 gtk_widget_set_usize(scrolledwin, 480, 200);
1762 gtk_widget_hide(scrolledwin);
1764 text = gtk_text_new(gtk_scrolled_window_get_hadjustment
1765 (GTK_SCROLLED_WINDOW(scrolledwin)),
1766 gtk_scrolled_window_get_vadjustment
1767 (GTK_SCROLLED_WINDOW(scrolledwin)));
1768 gtk_text_set_editable(GTK_TEXT(text), FALSE);
1769 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1770 gtk_widget_show(text);
1772 if (children->open_in) {
1773 input_hbox = gtk_hbox_new(FALSE, 8);
1774 gtk_widget_show(input_hbox);
1776 entry = gtk_entry_new();
1777 gtk_widget_set_usize(entry, 320, -1);
1778 gtk_signal_connect(GTK_OBJECT(entry), "activate",
1779 GTK_SIGNAL_FUNC(send_input), children);
1780 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1781 if (children->open_in & ACTION_USER_HIDDEN_IN)
1782 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1783 gtk_widget_show(entry);
1785 send_button = gtk_button_new_with_label(_(" Send "));
1786 gtk_signal_connect(GTK_OBJECT(send_button), "clicked",
1787 GTK_SIGNAL_FUNC(send_input), children);
1788 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1790 gtk_widget_show(send_button);
1792 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1793 gtk_widget_grab_focus(entry);
1796 gtkut_button_set_create(&hbox, &abort_button, _("Abort"),
1797 &close_button, _("Close"), NULL, NULL);
1798 gtk_signal_connect(GTK_OBJECT(abort_button), "clicked",
1799 GTK_SIGNAL_FUNC(kill_children_cb), children);
1800 gtk_signal_connect(GTK_OBJECT(close_button), "clicked",
1801 GTK_SIGNAL_FUNC(hide_io_dialog_cb), children);
1802 gtk_widget_show(hbox);
1805 gtk_widget_set_sensitive(close_button, FALSE);
1807 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1809 children->dialog = dialog;
1810 children->scrolledwin = scrolledwin;
1811 children->text = text;
1812 children->input_hbox = children->open_in ? input_hbox : NULL;
1813 children->input_entry = children->open_in ? entry : NULL;
1814 children->abort_btn = abort_button;
1815 children->close_btn = close_button;
1817 gtk_widget_show(dialog);
1820 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1822 ChildInfo *child_info = (ChildInfo *)data;
1826 gdk_input_remove(child_info->tag_status);
1828 c = read(source, &buf, 1);
1829 debug_print("Child returned %c\n", buf);
1831 waitpid(-child_info->pid, NULL, 0);
1832 childinfo_close_pipes(child_info);
1833 child_info->pid = 0;
1835 wait_for_children(child_info->children);
1838 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1840 Children *children = (Children *)data;
1841 ChildInfo *child_info = (ChildInfo *)children->list->data;
1845 debug_print("Sending input to grand child.\n");
1846 if (!(cond && GDK_INPUT_WRITE))
1849 gdk_input_remove(child_info->tag_in);
1850 child_info->tag_in = -1;
1852 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1854 len = strlen(input);
1858 c = write(child_info->chld_in, input + count, len - count);
1862 } while (c >= 0 && count < len);
1865 write(child_info->chld_in, "\n", 2);
1869 gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1870 gtk_widget_set_sensitive(children->input_hbox, TRUE);
1871 close(child_info->chld_in);
1872 child_info->chld_in = -1;
1873 debug_print("Input to grand child sent.\n");
1876 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1878 ChildInfo *child_info = (ChildInfo *)data;
1880 gchar buf[PREFSBUFSIZE];
1882 debug_print("Catching grand child's output.\n");
1883 if (child_info->type & (ACTION_PIPE_OUT | ACTION_INSERT)
1884 && source == child_info->chld_out) {
1885 gboolean is_selection = FALSE;
1886 GtkWidget *text = child_info->text;
1887 if (GTK_EDITABLE(text)->has_selection)
1888 is_selection = TRUE;
1889 gtk_stext_freeze(GTK_STEXT(text));
1891 c = read(source, buf, PREFSBUFSIZE - 1);
1894 gtk_stext_insert(GTK_STEXT(text), child_info->msgfont,
1895 NULL, NULL, buf, c);
1898 /* Using the select_region draws things. Should not.
1899 * so we just change selection position and
1900 * defere drawing when thawing. Hack?
1902 GTK_EDITABLE(text)->selection_end_pos =
1903 gtk_stext_get_point(GTK_STEXT(text));
1905 gtk_stext_thaw(GTK_STEXT(child_info->text));
1907 c = read(source, buf, PREFSBUFSIZE - 1);
1908 for (i = 0; i < c; i++)
1909 child_info->output = g_string_append_c
1910 (child_info->output, buf[i]);
1912 child_info->new_out = TRUE;
1914 wait_for_children(child_info->children);
1917 static gboolean user_string_dialog_delete_cb(GtkWidget *widget,
1924 static void user_string_dialog_destroy_cb(GtkWidget *widget, gpointer data)
1929 static void user_string_dialog_activate_cb(GtkWidget *widget, gpointer data)
1931 UserStringDialog *user_dialog = (UserStringDialog *) data;
1932 if (user_dialog->user_str)
1933 g_free(user_dialog->user_str);
1934 user_dialog->user_str =
1935 gtk_editable_get_chars(GTK_EDITABLE(user_dialog->entry), 0, -1);
1936 gtk_widget_destroy(user_dialog->dialog);
1939 static gchar *get_user_string(const gchar *action, ActionType type )
1944 GtkWidget *ok_button;
1945 GtkWidget *cancel_button;
1947 UserStringDialog user_dialog;
1949 dialog = gtk_dialog_new();
1950 gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
1951 gtk_container_set_border_width(
1952 GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), 8);
1953 gtk_container_set_border_width(
1954 GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1955 gtk_window_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1958 case ACTION_USER_HIDDEN_STR:
1959 gtk_window_set_title(GTK_WINDOW(dialog),
1960 _("Action's user hidden argument"));
1961 label_text = g_strdup_printf(_("Enter the '%%h' "
1962 "argument for the following "
1963 "action:\n%s"), action);
1965 case ACTION_USER_STR:
1966 gtk_window_set_title(GTK_WINDOW(dialog),
1967 _("Action's user argument"));
1968 label_text = g_strdup_printf(_("Enter the '%%u' "
1969 "argument for the following "
1970 "action:\n%s"), action);
1974 debug_print("Unsupported action type %d", type);
1977 label = gtk_label_new(label_text);
1979 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label,
1982 entry = gtk_entry_new();
1983 gtk_entry_set_visibility(GTK_ENTRY(entry),
1984 type != ACTION_USER_HIDDEN_STR);
1985 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), entry,
1988 ok_button = gtk_button_new_with_label(_("OK"));
1989 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1990 ok_button, TRUE, TRUE, 0);
1992 cancel_button = gtk_button_new_with_label(_("Cancel"));
1993 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1994 cancel_button, TRUE, TRUE, 0);
1996 user_dialog.dialog = dialog;
1997 user_dialog.user_str = NULL;
1998 user_dialog.entry = entry;
2000 gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
2001 GTK_SIGNAL_FUNC(user_string_dialog_delete_cb),
2003 gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
2004 GTK_SIGNAL_FUNC(user_string_dialog_destroy_cb),
2006 gtk_signal_connect(GTK_OBJECT(entry), "activate",
2007 GTK_SIGNAL_FUNC(user_string_dialog_activate_cb),
2009 gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
2010 GTK_SIGNAL_FUNC(user_string_dialog_activate_cb),
2012 gtk_signal_connect_object(GTK_OBJECT(cancel_button), "clicked",
2013 GTK_SIGNAL_FUNC(gtk_widget_destroy),
2014 GTK_OBJECT(dialog));
2016 gtk_widget_grab_focus(entry);
2018 gtk_widget_show_all(dialog);
2022 return user_dialog.user_str;
2026 * Strings describing action format strings
2028 * When adding new lines, remember to put one string for each line
2030 static gchar *actions_desc_strings[] = {
2031 N_("MENU NAME:"), NULL,
2032 " ", N_("Use / in menu name to make submenus."),
2034 N_("COMMAND LINE:"), NULL,
2035 N_("Begin with:"), NULL,
2036 " |", N_("to send message body or selection to command"),
2037 " >", N_("to send user provided text to command"),
2038 " *", N_("to send user provided hidden text to command"),
2039 N_("End with:"), NULL,
2040 " |", N_("to replace message body or selection with command output"),
2041 " >", N_("to insert command's output without replacing old text"),
2042 " &", N_("to run command asynchronously"),
2044 " %f", N_("for message file name"),
2045 " %F", N_("for the list of the file names of selected messages"),
2046 " %p", N_("for the selected message MIME part"),
2047 " %u", N_("for a user provided argument"),
2048 " %h", N_("for a user provided hidden argument"),
2049 " %s", N_("for the text selection"),
2054 static DescriptionWindow actions_desc_win = {
2057 N_("Description of symbols"),
2058 actions_desc_strings
2062 static void prefs_actions_help_cb(GtkWidget *w, gpointer data)
2064 description_window_create(&actions_desc_win);