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>
44 #include "manage_window.h"
45 #include "mainwindow.h"
46 #include "prefs_common.h"
47 #include "alertpanel.h"
48 #include "prefs_actions.h"
57 ACTION_PIPE_IN = 1 << 1,
58 ACTION_PIPE_OUT = 1 << 2,
59 ACTION_SINGLE = 1 << 3,
60 ACTION_MULTIPLE = 1 << 4,
61 ACTION_ASYNC = 1 << 5,
62 ACTION_OPEN_IN = 1 << 6,
63 ACTION_HIDE_IN = 1 << 7,
64 ACTION_ERROR = 1 << 8,
73 GtkWidget *name_entry;
76 GtkWidget *actions_clist;
79 typedef struct _Children Children;
80 typedef struct _ChildInfo ChildInfo;
87 GtkWidget *input_entry;
88 GtkWidget *input_hbox;
91 GtkWidget *scrolledwin;
119 /* widget creating functions */
120 static void prefs_actions_create (MainWindow *mainwin);
121 static void prefs_actions_set_dialog (void);
122 static gint prefs_actions_clist_set_row (gint row);
124 /* callback functions */
125 static void prefs_actions_help_cb (GtkWidget *w,
127 static void prefs_actions_register_cb (GtkWidget *w,
129 static void prefs_actions_substitute_cb (GtkWidget *w,
131 static void prefs_actions_delete_cb (GtkWidget *w,
133 static void prefs_actions_up (GtkWidget *w,
135 static void prefs_actions_down (GtkWidget *w,
137 static void prefs_actions_select (GtkCList *clist,
141 static void prefs_actions_row_move (GtkCList *clist,
144 static gint prefs_actions_deleted (GtkWidget *widget,
147 static void prefs_actions_key_pressed (GtkWidget *widget,
150 static void prefs_actions_cancel (GtkWidget *w,
152 static void prefs_actions_ok (GtkWidget *w,
154 static void update_actions_menu (GtkItemFactory *ifactory,
158 static void mainwin_actions_execute_cb (MainWindow *mainwin,
161 static void compose_actions_execute_cb (Compose *compose,
164 static guint get_action_type (gchar *action);
166 static gboolean execute_actions (gchar *action,
172 static gchar *parse_action_cmd (gchar *action,
176 static GString *parse_append_filename (GString *cmd,
179 static GString *parse_append_msgpart (GString *cmd,
183 ChildInfo *fork_child (gchar *cmd,
188 static gint wait_for_children (gpointer data);
190 static void free_children (Children *children);
192 static void childinfo_close_pipes (ChildInfo *child_info);
194 static void create_io_dialog (Children *children);
195 static void update_io_dialog (Children *children);
197 static void hide_io_dialog_cb (GtkWidget *widget,
200 static void catch_output (gpointer data,
202 GdkInputCondition cond);
203 static void catch_input (gpointer data,
205 GdkInputCondition cond);
206 static void catch_status (gpointer data,
208 GdkInputCondition cond);
210 void prefs_actions_open(MainWindow *mainwin)
213 if (prefs_rc_is_readonly(ACTIONS_RC))
219 prefs_actions_create(mainwin);
221 manage_window_set_transient(GTK_WINDOW(actions.window));
222 gtk_widget_grab_focus(actions.ok_btn);
224 prefs_actions_set_dialog();
226 gtk_widget_show(actions.window);
229 static void prefs_actions_create(MainWindow *mainwin)
234 GtkWidget *cancel_btn;
235 GtkWidget *confirm_area;
239 GtkWidget *entry_vbox;
241 GtkWidget *name_label;
242 GtkWidget *name_entry;
243 GtkWidget *cmd_label;
244 GtkWidget *cmd_entry;
250 GtkWidget *subst_btn;
253 GtkWidget *cond_hbox;
254 GtkWidget *cond_scrolledwin;
255 GtkWidget *cond_clist;
257 GtkWidget *help_vbox;
258 GtkWidget *help_label;
259 GtkWidget *help_toggle;
267 debug_print("Creating actions setting window...\n");
269 window = gtk_window_new (GTK_WINDOW_DIALOG);
271 gtk_container_set_border_width(GTK_CONTAINER (window), 8);
272 gtk_window_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
273 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
274 gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, TRUE);
275 gtk_window_set_default_size(GTK_WINDOW(window), 400, -1);
277 vbox = gtk_vbox_new(FALSE, 6);
278 gtk_widget_show(vbox);
279 gtk_container_add(GTK_CONTAINER(window), vbox);
281 gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
282 &cancel_btn, _("Cancel"), NULL, NULL);
283 gtk_widget_show(confirm_area);
284 gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
285 gtk_widget_grab_default(ok_btn);
287 gtk_window_set_title(GTK_WINDOW(window), _("Actions setting"));
288 gtk_signal_connect(GTK_OBJECT(window), "delete_event",
289 GTK_SIGNAL_FUNC(prefs_actions_deleted), NULL);
290 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
291 GTK_SIGNAL_FUNC(prefs_actions_key_pressed), NULL);
292 MANAGE_WINDOW_SIGNALS_CONNECT(window);
293 gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
294 GTK_SIGNAL_FUNC(prefs_actions_ok), mainwin);
295 gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
296 GTK_SIGNAL_FUNC(prefs_actions_cancel), NULL);
298 vbox1 = gtk_vbox_new(FALSE, 8);
299 gtk_widget_show(vbox1);
300 gtk_box_pack_start(GTK_BOX(vbox), vbox1, TRUE, TRUE, 0);
301 gtk_container_set_border_width(GTK_CONTAINER(vbox1), 2);
303 entry_vbox = gtk_vbox_new(FALSE, 4);
304 gtk_box_pack_start(GTK_BOX(vbox1), entry_vbox, FALSE, FALSE, 0);
306 hbox = gtk_hbox_new(FALSE, 8);
307 gtk_box_pack_start(GTK_BOX(entry_vbox), hbox, FALSE, FALSE, 0);
309 name_label = gtk_label_new(_("Menu name:"));
310 gtk_box_pack_start(GTK_BOX(hbox), name_label, FALSE, FALSE, 0);
312 name_entry = gtk_entry_new();
313 gtk_box_pack_start(GTK_BOX(hbox), name_entry, TRUE, TRUE, 0);
315 hbox = gtk_hbox_new(FALSE, 8);
316 gtk_box_pack_start(GTK_BOX(entry_vbox), hbox, TRUE, TRUE, 0);
318 cmd_label = gtk_label_new(_("Command line:"));
319 gtk_box_pack_start(GTK_BOX(hbox), cmd_label, FALSE, FALSE, 0);
321 cmd_entry = gtk_entry_new();
322 gtk_box_pack_start(GTK_BOX(hbox), cmd_entry, TRUE, TRUE, 0);
324 gtk_widget_show_all(entry_vbox);
326 help_vbox = gtk_vbox_new(FALSE, 8);
327 gtk_box_pack_start(GTK_BOX(vbox1), help_vbox, FALSE, FALSE, 0);
329 help_label = gtk_label_new
331 " Use / in menu name to make submenus.\n"
334 " | to send message body or selection to command\n"
335 " > to send user provided text to command\n"
336 " * to send user provided hidden text to command\n"
338 " | to replace message body or selection with command output\n"
339 " & to run command asynchronously\n"
340 " Use %f for message file name\n"
341 " %F for the list of the file names of selected messages\n"
342 " %p for the selected message part."));
343 gtk_misc_set_alignment(GTK_MISC(help_label), 0, 0.5);
344 gtk_label_set_justify(GTK_LABEL(help_label), GTK_JUSTIFY_LEFT);
345 gtk_widget_show(help_label);
346 gtk_box_pack_start(GTK_BOX(help_vbox), help_label, FALSE, FALSE, 0);
347 gtk_widget_hide(help_vbox);
349 /* register / substitute / delete */
351 reg_hbox = gtk_hbox_new(FALSE, 4);
352 gtk_widget_show(reg_hbox);
353 gtk_box_pack_start(GTK_BOX(vbox1), reg_hbox, FALSE, FALSE, 0);
355 arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
356 gtk_widget_show(arrow);
357 gtk_box_pack_start(GTK_BOX(reg_hbox), arrow, FALSE, FALSE, 0);
358 gtk_widget_set_usize(arrow, -1, 16);
360 btn_hbox = gtk_hbox_new(TRUE, 4);
361 gtk_widget_show(btn_hbox);
362 gtk_box_pack_start(GTK_BOX(reg_hbox), btn_hbox, FALSE, FALSE, 0);
364 reg_btn = gtk_button_new_with_label(_("Register"));
365 gtk_widget_show(reg_btn);
366 gtk_box_pack_start(GTK_BOX(btn_hbox), reg_btn, FALSE, TRUE, 0);
367 gtk_signal_connect(GTK_OBJECT(reg_btn), "clicked",
368 GTK_SIGNAL_FUNC(prefs_actions_register_cb), NULL);
370 subst_btn = gtk_button_new_with_label(_(" Substitute "));
371 gtk_widget_show(subst_btn);
372 gtk_box_pack_start(GTK_BOX(btn_hbox), subst_btn, FALSE, TRUE, 0);
373 gtk_signal_connect(GTK_OBJECT(subst_btn), "clicked",
374 GTK_SIGNAL_FUNC(prefs_actions_substitute_cb),
377 del_btn = gtk_button_new_with_label(_("Delete"));
378 gtk_widget_show(del_btn);
379 gtk_box_pack_start(GTK_BOX(btn_hbox), del_btn, FALSE, TRUE, 0);
380 gtk_signal_connect(GTK_OBJECT(del_btn), "clicked",
381 GTK_SIGNAL_FUNC(prefs_actions_delete_cb), NULL);
383 help_toggle = gtk_toggle_button_new_with_label(_(" Syntax help "));
384 gtk_widget_show(help_toggle);
385 gtk_box_pack_end(GTK_BOX(reg_hbox), help_toggle, FALSE, FALSE, 0);
386 gtk_signal_connect(GTK_OBJECT(help_toggle), "toggled",
387 GTK_SIGNAL_FUNC(prefs_actions_help_cb), help_vbox);
389 cond_hbox = gtk_hbox_new(FALSE, 8);
390 gtk_widget_show(cond_hbox);
391 gtk_box_pack_start(GTK_BOX(vbox1), cond_hbox, TRUE, TRUE, 0);
393 cond_scrolledwin = gtk_scrolled_window_new(NULL, NULL);
394 gtk_widget_show(cond_scrolledwin);
395 gtk_widget_set_usize(cond_scrolledwin, -1, 150);
396 gtk_box_pack_start(GTK_BOX(cond_hbox), cond_scrolledwin,
398 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (cond_scrolledwin),
399 GTK_POLICY_AUTOMATIC,
400 GTK_POLICY_AUTOMATIC);
402 title[0] = _("Registered actions");
403 cond_clist = gtk_clist_new_with_titles(1, title);
404 gtk_widget_show(cond_clist);
405 gtk_container_add(GTK_CONTAINER (cond_scrolledwin), cond_clist);
406 gtk_clist_set_column_width(GTK_CLIST (cond_clist), 0, 80);
407 gtk_clist_set_selection_mode(GTK_CLIST (cond_clist),
408 GTK_SELECTION_BROWSE);
409 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(cond_clist)->column[0].button,
411 gtk_signal_connect(GTK_OBJECT(cond_clist), "select_row",
412 GTK_SIGNAL_FUNC(prefs_actions_select), NULL);
413 gtk_signal_connect_after(GTK_OBJECT(cond_clist), "row_move",
414 GTK_SIGNAL_FUNC(prefs_actions_row_move),
417 btn_vbox = gtk_vbox_new(FALSE, 8);
418 gtk_widget_show(btn_vbox);
419 gtk_box_pack_start(GTK_BOX(cond_hbox), btn_vbox, FALSE, FALSE, 0);
421 up_btn = gtk_button_new_with_label(_("Up"));
422 gtk_widget_show(up_btn);
423 gtk_box_pack_start(GTK_BOX(btn_vbox), up_btn, FALSE, FALSE, 0);
424 gtk_signal_connect(GTK_OBJECT(up_btn), "clicked",
425 GTK_SIGNAL_FUNC(prefs_actions_up), NULL);
427 down_btn = gtk_button_new_with_label(_("Down"));
428 gtk_widget_show(down_btn);
429 gtk_box_pack_start(GTK_BOX(btn_vbox), down_btn, FALSE, FALSE, 0);
430 gtk_signal_connect(GTK_OBJECT(down_btn), "clicked",
431 GTK_SIGNAL_FUNC(prefs_actions_down), NULL);
433 gtk_widget_show(window);
435 actions.window = window;
436 actions.ok_btn = ok_btn;
438 actions.name_entry = name_entry;
439 actions.cmd_entry = cmd_entry;
441 actions.actions_clist = cond_clist;
444 static void prefs_actions_help_cb(GtkWidget *w, gpointer data)
446 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)))
447 gtk_widget_show(GTK_WIDGET(data));
449 gtk_widget_hide(GTK_WIDGET(data));
452 void prefs_actions_read_config(void)
456 gchar buf[PREFSBUFSIZE];
459 debug_print("Reading actions configurations...\n");
461 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
462 if ((fp = fopen(rcpath, "rb")) == NULL) {
463 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
469 while (prefs_common.actions_list != NULL) {
470 act = (gchar *)prefs_common.actions_list->data;
471 prefs_common.actions_list =
472 g_slist_remove(prefs_common.actions_list, act);
476 while (fgets(buf, sizeof(buf), fp) != NULL) {
478 act = strstr(buf, ": ");
480 get_action_type(&act[2]) != ACTION_ERROR)
481 prefs_common.actions_list =
482 g_slist_append(prefs_common.actions_list,
488 void prefs_actions_write_config(void)
494 debug_print("Writing actions configuration...\n");
496 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
497 if ((pfile= prefs_write_open(rcpath)) == NULL) {
498 g_warning(_("failed to write configuration to file\n"));
503 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
504 gchar *act = (gchar *)cur->data;
505 if (fputs(act, pfile->fp) == EOF ||
506 fputc('\n', pfile->fp) == EOF) {
507 FILE_OP_ERROR(rcpath, "fputs || fputc");
508 prefs_write_close_revert(pfile);
516 if (prefs_write_close(pfile) < 0) {
517 g_warning(_("failed to write configuration to file\n"));
522 static guint get_action_type(gchar *action)
525 guint action_type = ACTION_NONE;
527 g_return_val_if_fail(action, ACTION_ERROR);
528 g_return_val_if_fail(*action, ACTION_ERROR);
533 action_type |= ACTION_PIPE_IN;
535 } else if (p[0] == '>') {
536 action_type |= ACTION_OPEN_IN;
538 } else if (p[0] == '*') {
539 action_type |= ACTION_HIDE_IN;
546 while (*p && action_type != ACTION_ERROR) {
550 action_type |= ACTION_SINGLE;
553 action_type |= ACTION_MULTIPLE;
556 action_type |= ACTION_SINGLE;
559 action_type = ACTION_ERROR;
562 } else if (p[0] == '|') {
564 action_type |= ACTION_PIPE_OUT;
565 } else if (p[0] == '&') {
567 action_type |= ACTION_ASYNC;
569 action_type = ACTION_ERROR;
577 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
578 GtkCTree *ctree, MimeView *mimeview)
580 GString *cmd, *tmpcmd;
587 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
590 cmd = g_string_sized_new(strlen(action));
592 while (p[0] && !(p[0] == '|' && !p[1]) && p[0] != '&') {
593 if (p[0] == '%' && p[1]) {
596 cmd = parse_append_filename(cmd, msginfo);
600 for (cur = GTK_CLIST(ctree)->selection;
601 cur != NULL; cur = cur->next) {
602 msg = gtk_ctree_node_get_row_data(ctree,
603 GTK_CTREE_NODE(cur->data));
604 cmd = parse_append_filename(cmd, msg);
606 cmd = g_string_append_c(cmd, ' ');
611 tmpcmd = parse_append_msgpart
612 (cmd, msginfo, mimeview);
616 g_string_free(cmd, TRUE);
622 cmd = g_string_append_c(cmd, p[0]);
623 cmd = g_string_append_c(cmd, p[1]);
627 cmd = g_string_append_c(cmd, p[0]);
632 g_string_free(cmd, TRUE);
637 g_string_free(cmd, FALSE);
641 static GString *parse_append_filename(GString *cmd, MsgInfo *msginfo)
645 g_return_val_if_fail(msginfo, cmd);
647 filename = procmsg_get_message_file(msginfo);
650 cmd = g_string_append(cmd, filename);
657 static GString *parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
668 if ((fp = procmsg_open_message_decrypted(msginfo, &partinfo))
670 alertpanel_error(_("Could not get message file."));
674 if ((fp = procmsg_open_message(msginfo)) == NULL) {
675 alertpanel_error(_("Could not get message file."));
678 partinfo = procmime_scan_mime_header(fp);
682 procmime_mimeinfo_free(partinfo);
683 alertpanel_error(_("Could not get message part."));
686 filename = procmsg_get_message_file(msginfo);
688 if (!mimeview->opened) {
689 alertpanel_error(_("No message part selected."));
692 if (!mimeview->file) {
693 alertpanel_error(_("No message file selected."));
696 partinfo = gtk_ctree_node_get_row_data
697 (GTK_CTREE(mimeview->ctree),
699 g_return_val_if_fail(partinfo != NULL, cmd);
700 filename = mimeview->file;
702 partname = procmime_get_tmp_file_name(partinfo);
704 ret = procmime_get_part(partname, filename, partinfo);
707 procmime_mimeinfo_free(partinfo);
712 alertpanel_error(_("Can't get part of multipart message"));
717 cmd = g_string_append(cmd,partname);
724 static void prefs_actions_set_dialog(void)
726 GtkCList *clist = GTK_CLIST(actions.actions_clist);
728 gchar *action_str[1];
731 gtk_clist_freeze(clist);
732 gtk_clist_clear(clist);
734 action_str[0] = _("(New)");
735 row = gtk_clist_append(clist, action_str);
736 gtk_clist_set_row_data(clist, row, NULL);
738 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
741 action[0] = (gchar *)cur->data;
742 row = gtk_clist_append(clist, action);
743 gtk_clist_set_row_data(clist, row, action[0]);
746 gtk_clist_thaw(clist);
749 static void prefs_actions_set_list(void)
754 g_slist_free(prefs_common.actions_list);
755 prefs_common.actions_list = NULL;
757 while ((action = (gchar *)gtk_clist_get_row_data
758 (GTK_CLIST(actions.actions_clist), row)) != NULL) {
759 prefs_common.actions_list =
760 g_slist_append(prefs_common.actions_list, action);
765 #define GET_ENTRY(entry) \
766 entry_text = gtk_entry_get_text(GTK_ENTRY(entry))
768 static gint prefs_actions_clist_set_row(gint row)
770 GtkCList *clist = GTK_CLIST(actions.actions_clist);
773 gchar action[PREFSBUFSIZE];
776 g_return_val_if_fail(row != 0, -1);
778 GET_ENTRY(actions.name_entry);
779 if (entry_text[0] == '\0') {
780 alertpanel_error(_("Menu name is not set."));
784 if (strchr(entry_text, ':')) {
785 alertpanel_error(_("Colon ':' is not allowed in the menu name."));
789 strncpy(action, entry_text, PREFSBUFSIZE - 1);
792 /* Keep space for the ': ' delimiter */
793 len = strlen(action) + 2;
794 if (len >= PREFSBUFSIZE - 1) {
795 alertpanel_error(_("Menu name is too long."));
799 strcat(action, ": ");
801 GET_ENTRY(actions.cmd_entry);
803 if (entry_text[0] == '\0') {
804 alertpanel_error(_("Command line not set."));
808 if (len + strlen(entry_text) >= PREFSBUFSIZE - 1) {
809 alertpanel_error(_("Menu name and command are too long."));
813 if (get_action_type(entry_text) == ACTION_ERROR) {
814 alertpanel_error(_("The command\n%s\nhas a syntax error."),
819 strcat(action, entry_text);
823 row = gtk_clist_append(clist, buf);
826 gtk_clist_set_text(clist, row, 0, action);
827 old_action = (gchar *) gtk_clist_get_row_data(clist, row);
832 buf[0] = g_strdup(action);
834 gtk_clist_set_row_data(clist, row, buf[0]);
836 prefs_actions_set_list();
841 /* callback functions */
843 static void prefs_actions_register_cb(GtkWidget *w, gpointer data)
845 prefs_actions_clist_set_row(-1);
848 static void prefs_actions_substitute_cb(GtkWidget *w, gpointer data)
850 GtkCList *clist = GTK_CLIST(actions.actions_clist);
854 if (!clist->selection) return;
856 row = GPOINTER_TO_INT(clist->selection->data);
857 if (row == 0) return;
859 action = gtk_clist_get_row_data(clist, row);
862 prefs_actions_clist_set_row(row);
865 static void prefs_actions_delete_cb(GtkWidget *w, gpointer data)
867 GtkCList *clist = GTK_CLIST(actions.actions_clist);
871 if (!clist->selection) return;
872 row = GPOINTER_TO_INT(clist->selection->data);
873 if (row == 0) return;
875 if (alertpanel(_("Delete action"),
876 _("Do you really want to delete this action?"),
877 _("Yes"), _("No"), NULL) == G_ALERTALTERNATE)
880 action = gtk_clist_get_row_data(clist, row);
882 gtk_clist_remove(clist, row);
883 prefs_common.actions_list = g_slist_remove(prefs_common.actions_list,
887 static void prefs_actions_up(GtkWidget *w, gpointer data)
889 GtkCList *clist = GTK_CLIST(actions.actions_clist);
892 if (!clist->selection) return;
894 row = GPOINTER_TO_INT(clist->selection->data);
896 gtk_clist_row_move(clist, row, row - 1);
899 static void prefs_actions_down(GtkWidget *w, gpointer data)
901 GtkCList *clist = GTK_CLIST(actions.actions_clist);
904 if (!clist->selection) return;
906 row = GPOINTER_TO_INT(clist->selection->data);
907 if (row > 0 && row < clist->rows - 1)
908 gtk_clist_row_move(clist, row, row + 1);
911 #define ENTRY_SET_TEXT(entry, str) \
912 gtk_entry_set_text(GTK_ENTRY(entry), str ? str : "")
914 static void prefs_actions_select(GtkCList *clist, gint row, gint column,
919 gchar buf[PREFSBUFSIZE];
920 action = gtk_clist_get_row_data(clist, row);
923 ENTRY_SET_TEXT(actions.name_entry, "");
924 ENTRY_SET_TEXT(actions.cmd_entry, "");
928 strncpy(buf, action, PREFSBUFSIZE - 1);
929 buf[PREFSBUFSIZE - 1] = 0x00;
930 cmd = strstr(buf, ": ");
933 ENTRY_SET_TEXT(actions.cmd_entry, &cmd[2]);
938 ENTRY_SET_TEXT(actions.name_entry, buf);
941 static void prefs_actions_row_move(GtkCList *clist,
942 gint source_row, gint dest_row)
944 prefs_actions_set_list();
945 if (gtk_clist_row_is_visible(clist, dest_row) != GTK_VISIBILITY_FULL) {
946 gtk_clist_moveto(clist, dest_row, -1,
947 source_row < dest_row ? 1.0 : 0.0, 0.0);
951 static gint prefs_actions_deleted(GtkWidget *widget, GdkEventAny *event,
954 prefs_actions_cancel(widget, data);
958 static void prefs_actions_key_pressed(GtkWidget *widget, GdkEventKey *event,
961 if (event && event->keyval == GDK_Escape)
962 prefs_actions_cancel(widget, data);
965 static void prefs_actions_cancel(GtkWidget *w, gpointer data)
967 prefs_actions_read_config();
968 gtk_widget_hide(actions.window);
972 static void prefs_actions_ok(GtkWidget *widget, gpointer data)
974 GtkItemFactory *ifactory;
975 MainWindow *mainwin = (MainWindow *)data;
977 prefs_actions_write_config();
978 ifactory = gtk_item_factory_from_widget(mainwin->menubar);
979 update_mainwin_actions_menu(ifactory, mainwin);
980 gtk_widget_hide(actions.window);
984 void update_mainwin_actions_menu(GtkItemFactory *ifactory,
987 update_actions_menu(ifactory, "/Tools/Actions",
988 mainwin_actions_execute_cb,
992 void update_compose_actions_menu(GtkItemFactory *ifactory,
996 update_actions_menu(ifactory, branch_path,
997 compose_actions_execute_cb,
1001 void actions_execute(MainWindow *mainwin,
1005 mainwin_actions_execute_cb(mainwin, action_nb, widget);
1009 static void update_actions_menu(GtkItemFactory *ifactory,
1014 GtkWidget *menuitem;
1017 gchar *action, *action_p;
1019 GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
1021 ifentry.path = branch_path;
1022 menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
1023 g_return_if_fail(menuitem != NULL);
1025 amenu = GTK_MENU_SHELL(menuitem)->children;
1026 while (amenu != NULL) {
1027 GList *alist = amenu->next;
1028 gtk_widget_destroy(GTK_WIDGET(amenu->data));
1032 ifentry.accelerator = NULL;
1033 ifentry.callback_action = 0;
1034 ifentry.callback = callback;
1035 ifentry.item_type = NULL;
1037 for (cur = prefs_common.actions_list; cur; cur = cur->next) {
1038 action = g_strdup((gchar *)cur->data);
1039 action_p = strstr(action, ": ");
1040 if (action_p && action_p[2] &&
1041 get_action_type(&action_p[2]) != ACTION_ERROR) {
1043 menu_path = g_strdup_printf("%s/%s", branch_path,
1045 ifentry.path = menu_path;
1046 gtk_item_factory_create_item(ifactory, &ifentry, data,
1051 ifentry.callback_action++;
1055 static void compose_actions_execute_cb(Compose *compose, guint action_nb,
1058 gchar *buf, *action;
1061 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
1063 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
1064 g_return_if_fail(buf != NULL);
1065 action = strstr(buf, ": ");
1066 g_return_if_fail(action != NULL);
1068 /* Point to the beginning of the command-line */
1071 action_type = get_action_type(action);
1072 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
1074 (_("The selected action cannot be used in the compose window\n"
1075 "because it contains %%f, %%F or %%p."));
1079 execute_actions(action, compose->window, NULL, compose->text, NULL);
1082 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
1085 MessageView *messageview = mainwin->messageview;
1086 GtkWidget *text = NULL;
1089 MimeView *mimeview = NULL;
1091 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
1093 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
1095 g_return_if_fail(buf);
1096 g_return_if_fail(action = strstr(buf, ": "));
1098 /* Point to the beginning of the command-line */
1101 switch (messageview->type) {
1103 if (messageview->textview && messageview->textview->text)
1104 text = messageview->textview->text;
1107 if (messageview->mimeview &&
1108 messageview->mimeview->type == MIMEVIEW_TEXT &&
1109 messageview->mimeview->textview &&
1110 messageview->mimeview->textview->text) {
1111 text = messageview->mimeview->textview->text;
1112 mimeview = messageview->mimeview;
1117 execute_actions(action, mainwin->window,
1118 GTK_CTREE(mainwin->summaryview->ctree), text, mimeview);
1121 static gboolean execute_actions(gchar *action, GtkWidget *window,
1122 GtkCTree *ctree, GtkWidget *text,
1125 GList *cur, *selection = NULL;
1126 GSList *children_list = NULL;
1128 gint selection_len = 0;
1130 ChildInfo *child_info;
1135 g_return_val_if_fail(action && *action, FALSE);
1137 action_type = get_action_type(action);
1139 if (action_type == ACTION_ERROR)
1140 return FALSE; /* ERR: syntax error */
1142 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) &&
1143 !(ctree && GTK_CLIST(ctree)->selection))
1144 return FALSE; /* ERR: file command without selection */
1147 selection = GTK_CLIST(ctree)->selection;
1148 selection_len = g_list_length(selection);
1151 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN)) {
1152 if (ctree && selection_len > 1)
1153 return FALSE; /* ERR: pipe + multiple selection */
1155 return FALSE; /* ERR: pipe and no displayed text */
1158 children = g_new0(Children, 1);
1160 if (action_type & ACTION_SINGLE) {
1161 for (cur = selection; cur && is_ok == TRUE; cur = cur->next) {
1162 msginfo = gtk_ctree_node_get_row_data(ctree,
1163 GTK_CTREE_NODE(cur->data));
1165 is_ok = FALSE; /* ERR: msginfo missing */
1168 cmd = parse_action_cmd(action, msginfo, ctree,
1171 debug_print("Action command error\n");
1172 is_ok = FALSE; /* ERR: incorrect command */
1175 if ((child_info = fork_child(cmd, action_type, text,
1177 children_list = g_slist_append(children_list,
1179 children->open_in = (selection_len == 1) ?
1182 ACTION_HIDE_IN)) : 0;
1187 cmd = parse_action_cmd(action, NULL, ctree, mimeview);
1189 if ((child_info = fork_child(cmd, action_type, text,
1191 children_list = g_slist_append(children_list,
1193 children->open_in = action_type &
1199 is_ok = FALSE; /* ERR: incorrect command */
1202 if (!children_list) {
1203 /* If not waiting for children, return */
1208 children->action = g_strdup(action);
1209 children->window = window;
1210 children->dialog = NULL;
1211 children->list = children_list;
1212 children->nb = g_slist_length(children_list);
1214 for (cur = children_list; cur; cur = cur->next) {
1215 child_info = (ChildInfo *) cur->data;
1216 child_info->tag_status =
1217 gdk_input_add(child_info->chld_status,
1219 catch_status, child_info);
1222 create_io_dialog(children);
1228 ChildInfo *fork_child(gchar *cmd, gint action_type, GtkWidget *text,
1231 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
1233 gint start, end, is_selection;
1236 ChildInfo *child_info;
1239 sync = !(action_type & ACTION_ASYNC);
1241 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
1242 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
1245 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
1247 alertpanel_error(_("Command could not be started. "
1248 "Pipe creation failed.\n%s"),
1250 /* Closing fd = -1 fails silently */
1257 close(chld_status[0]);
1258 close(chld_status[1]);
1259 return NULL; /* Pipe error */
1263 debug_print("Forking child and grandchild.\n");
1266 if (pid == 0) { /* Child */
1270 close(ConnectionNumber(gdk_display));
1275 if (setpgid(0, getppid()))
1282 close(fileno(stdin));
1288 close(fileno(stdout));
1293 close(fileno(stderr));
1303 execvp("/bin/sh", cmdline);
1307 } else if (gch_pid < (pid_t) 0) { /* Fork error */
1309 write(chld_status[1], "1\n", 2);
1320 close(chld_status[0]);
1323 debug_print("Child: Waiting for grandchild\n");
1324 waitpid(gch_pid, NULL, 0);
1325 debug_print("Child: grandchild ended\n");
1327 write(chld_status[1], "0\n", 2);
1328 close(chld_status[1]);
1332 } else if (pid < 0) { /* Fork error */
1333 alertpanel_error(_("Could not fork to execute the following "
1334 "command:\n%s\n%s"),
1335 cmd, g_strerror(errno));
1345 if (!(action_type & (ACTION_PIPE_IN | ACTION_OPEN_IN | ACTION_HIDE_IN)))
1349 close(chld_status[1]);
1351 child_info = g_new0(ChildInfo, 1);
1353 child_info->children = children;
1355 child_info->pid = pid;
1356 child_info->cmd = g_strdup(cmd);
1357 child_info->type = action_type;
1358 child_info->new_out = FALSE;
1359 child_info->output = g_string_sized_new(0);
1360 child_info->chld_in =
1362 (ACTION_PIPE_IN | ACTION_OPEN_IN | ACTION_HIDE_IN))
1364 child_info->chld_out = chld_out[0];
1365 child_info->chld_err = chld_err[0];
1366 child_info->chld_status = chld_status[0];
1367 child_info->tag_in = -1;
1368 child_info->tag_out = gdk_input_add(chld_out[0], GDK_INPUT_READ,
1369 catch_output, child_info);
1370 child_info->tag_err = gdk_input_add(chld_err[0], GDK_INPUT_READ,
1371 catch_output, child_info);
1373 if (!(action_type & (ACTION_PIPE_IN | ACTION_PIPE_OUT)))
1376 child_info->text = text;
1379 end = gtk_stext_get_length(GTK_STEXT(text));
1381 if (GTK_EDITABLE(text)->has_selection) {
1382 start = GTK_EDITABLE(text)->selection_start_pos;
1383 end = GTK_EDITABLE(text)->selection_end_pos;
1390 is_selection = TRUE;
1393 end = gtk_stext_get_length(GTK_STEXT(text));
1394 is_selection = FALSE;
1398 selection = gtk_editable_get_chars(GTK_EDITABLE(text), start, end);
1400 if (action_type & ACTION_PIPE_IN) {
1401 write(chld_in[1], selection, strlen(selection));
1402 if (!(action_type & (ACTION_OPEN_IN | ACTION_HIDE_IN)))
1404 child_info->chld_in = -1; /* No more input */
1408 gtk_stext_freeze(GTK_STEXT(text));
1409 if (action_type & ACTION_PIPE_OUT) {
1410 gtk_stext_set_point(GTK_STEXT(text), start);
1411 gtk_stext_forward_delete(GTK_STEXT(text), end - start);
1414 gtk_stext_thaw(GTK_STEXT(text));
1419 static void kill_children_cb(GtkWidget *widget, gpointer data)
1422 Children *children = (Children *) data;
1423 ChildInfo *child_info;
1425 for (cur = children->list; cur; cur = cur->next) {
1426 child_info = (ChildInfo *)(cur->data);
1427 debug_print("Killing child group id %d\n", child_info->pid);
1428 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
1433 static gint wait_for_children(gpointer data)
1435 gboolean new_output;
1436 Children *children = (Children *)data;
1437 ChildInfo *child_info;
1439 gint nb = children->nb;
1443 cur = children->list;
1446 child_info = (ChildInfo *)cur->data;
1447 if (child_info->pid)
1449 new_output |= child_info->new_out;
1453 children->output |= new_output;
1455 if (new_output || (children->dialog && (nb != children->nb)))
1456 update_io_dialog(children);
1461 if (!children->dialog) {
1462 free_children(children);
1463 } else if (!children->output) {
1464 gtk_widget_destroy(children->dialog);
1470 static void send_input(GtkWidget *w, gpointer data)
1472 Children *children = (Children *) data;
1473 ChildInfo *child_info = (ChildInfo *) children->list->data;
1475 child_info->tag_in = gdk_input_add(child_info->chld_in,
1477 catch_input, children);
1478 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1481 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1483 hide_io_dialog_cb(w, data);
1487 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1490 Children *children = (Children *)data;
1492 if (!children->nb) {
1493 gtk_signal_disconnect_by_data(GTK_OBJECT(children->dialog),
1495 gtk_widget_destroy(children->dialog);
1496 free_children(children);
1500 static void childinfo_close_pipes(ChildInfo *child_info)
1502 if (child_info->tag_in > 0)
1503 gdk_input_remove(child_info->tag_in);
1504 gdk_input_remove(child_info->tag_out);
1505 gdk_input_remove(child_info->tag_err);
1507 if (child_info->chld_in >= 0)
1508 close(child_info->chld_in);
1509 close(child_info->chld_out);
1510 close(child_info->chld_err);
1511 close(child_info->chld_status);
1514 static void free_children(Children *children)
1517 ChildInfo *child_info;
1519 debug_print("Freeing children data %p\n", children);
1521 g_free(children->action);
1522 for (cur = children->list; cur;) {
1523 child_info = (ChildInfo *)cur->data;
1524 g_free(child_info->cmd);
1525 g_string_free(child_info->output, TRUE);
1526 children->list = g_slist_remove(children->list, child_info);
1528 cur = children->list;
1533 static void update_io_dialog(Children *children)
1537 debug_print("Updating actions input/output dialog.\n");
1539 if (!children->nb) {
1540 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1541 gtk_widget_set_sensitive(children->close_btn, TRUE);
1542 if (children->input_hbox)
1543 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1546 if (children->output) {
1547 GtkWidget *text = children->text;
1549 ChildInfo *child_info;
1551 gtk_widget_show(children->scrolledwin);
1552 gtk_text_freeze(GTK_TEXT(text));
1553 gtk_text_set_point(GTK_TEXT(text), 0);
1554 gtk_text_forward_delete(GTK_TEXT(text),
1555 gtk_text_get_length(GTK_TEXT(text)));
1556 for (cur = children->list; cur; cur = cur->next) {
1557 child_info = (ChildInfo *)cur->data;
1558 if (child_info->pid)
1559 caption = g_strdup_printf
1560 (_("--- Running: %s\n"),
1563 caption = g_strdup_printf
1564 (_("--- Ended: %s\n"),
1567 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1569 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1570 child_info->output->str, -1);
1572 child_info->new_out = FALSE;
1574 gtk_text_thaw(GTK_TEXT(text));
1578 static void create_io_dialog(Children *children)
1582 GtkWidget *entry = NULL;
1583 GtkWidget *input_hbox = NULL;
1584 GtkWidget *send_button;
1587 GtkWidget *scrolledwin;
1589 GtkWidget *abort_button;
1590 GtkWidget *close_button;
1592 debug_print("Creating action IO dialog\n");
1594 dialog = gtk_dialog_new();
1595 gtk_container_set_border_width
1596 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1597 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1598 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1599 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1600 manage_window_set_transient(GTK_WINDOW(dialog));
1601 gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
1602 GTK_SIGNAL_FUNC(delete_io_dialog_cb), children);
1603 gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
1604 GTK_SIGNAL_FUNC(hide_io_dialog_cb),
1607 vbox = gtk_vbox_new(FALSE, 8);
1608 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1609 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1610 gtk_widget_show(vbox);
1612 label = gtk_label_new(children->action);
1613 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1614 gtk_widget_show(label);
1616 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1617 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1618 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
1619 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1620 gtk_widget_set_usize(scrolledwin, 480, 200);
1621 gtk_widget_hide(scrolledwin);
1623 text = gtk_text_new(gtk_scrolled_window_get_hadjustment
1624 (GTK_SCROLLED_WINDOW(scrolledwin)),
1625 gtk_scrolled_window_get_vadjustment
1626 (GTK_SCROLLED_WINDOW(scrolledwin)));
1627 gtk_text_set_editable(GTK_TEXT(text), FALSE);
1628 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1629 gtk_widget_show(text);
1631 if (children->open_in) {
1632 input_hbox = gtk_hbox_new(FALSE, 8);
1633 gtk_widget_show(input_hbox);
1635 entry = gtk_entry_new();
1636 gtk_widget_set_usize(entry, 320, -1);
1637 gtk_signal_connect(GTK_OBJECT(entry), "activate",
1638 GTK_SIGNAL_FUNC(send_input), children);
1639 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1640 if (children->open_in & ACTION_HIDE_IN)
1641 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1642 gtk_widget_show(entry);
1644 send_button = gtk_button_new_with_label(_(" Send "));
1645 gtk_signal_connect(GTK_OBJECT(send_button), "clicked",
1646 GTK_SIGNAL_FUNC(send_input), children);
1647 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1649 gtk_widget_show(send_button);
1651 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1652 gtk_widget_grab_focus(entry);
1655 gtkut_button_set_create(&hbox, &abort_button, _("Abort"),
1656 &close_button, _("Close"), NULL, NULL);
1657 gtk_signal_connect(GTK_OBJECT(abort_button), "clicked",
1658 GTK_SIGNAL_FUNC(kill_children_cb), children);
1659 gtk_signal_connect(GTK_OBJECT(close_button), "clicked",
1660 GTK_SIGNAL_FUNC(hide_io_dialog_cb), children);
1661 gtk_widget_show(hbox);
1664 gtk_widget_set_sensitive(close_button, FALSE);
1666 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1668 children->dialog = dialog;
1669 children->scrolledwin = scrolledwin;
1670 children->text = text;
1671 children->input_hbox = children->open_in ? input_hbox : NULL;
1672 children->input_entry = children->open_in ? entry : NULL;
1673 children->abort_btn = abort_button;
1674 children->close_btn = close_button;
1676 gtk_widget_show(dialog);
1679 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1681 ChildInfo *child_info = (ChildInfo *)data;
1685 gdk_input_remove(child_info->tag_status);
1687 c = read(source, &buf, 1);
1688 debug_print("Child returned %c\n", buf);
1690 waitpid(-child_info->pid, NULL, 0);
1691 childinfo_close_pipes(child_info);
1692 child_info->pid = 0;
1694 wait_for_children(child_info->children);
1697 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1699 Children *children = (Children *)data;
1700 ChildInfo *child_info = (ChildInfo *)children->list->data;
1704 debug_print("Sending input to grand child.\n");
1705 if (!(cond && GDK_INPUT_WRITE))
1708 gdk_input_remove(child_info->tag_in);
1709 child_info->tag_in = -1;
1711 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1713 c = write(child_info->chld_in, input, strlen(input));
1717 write(child_info->chld_in, "\n", 2);
1719 gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1720 gtk_widget_set_sensitive(children->input_hbox, TRUE);
1721 debug_print("Input to grand child sent.\n");
1724 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1726 ChildInfo *child_info = (ChildInfo *)data;
1728 gchar buf[PREFSBUFSIZE];
1730 debug_print("Catching grand child's output.\n");
1731 if (child_info->type & ACTION_PIPE_OUT
1732 && source == child_info->chld_out) {
1733 gboolean is_selection = FALSE;
1734 GtkWidget *text = child_info->text;
1735 if (GTK_EDITABLE(text)->has_selection)
1736 is_selection = TRUE;
1737 gtk_stext_freeze(GTK_STEXT(text));
1739 c = read(source, buf, PREFSBUFSIZE - 1);
1742 gtk_stext_insert(GTK_STEXT(text), NULL, NULL, NULL,
1746 /* Using the select_region draws things. Should not.
1747 * so we just change selection position and
1748 * defere drawing when thawing. Hack?
1750 GTK_EDITABLE(text)->selection_end_pos =
1751 gtk_stext_get_point(GTK_STEXT(text));
1753 gtk_stext_thaw(GTK_STEXT(child_info->text));
1755 c = read(source, buf, PREFSBUFSIZE - 1);
1756 for (i = 0; i < c; i++)
1757 child_info->output = g_string_append_c
1758 (child_info->output, buf[i]);
1760 child_info->new_out = TRUE;