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"
54 #define WAIT_LAP 10000
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_OPEN_IN = 1 << 6,
65 ACTION_HIDE_IN = 1 << 7,
66 ACTION_ERROR = 1 << 8,
75 GtkWidget *name_entry;
78 GtkWidget *actions_clist;
81 typedef struct _Children Children;
82 typedef struct _ChildInfo ChildInfo;
89 GtkWidget *input_entry;
90 GtkWidget *input_hbox;
93 GtkWidget *scrolledwin;
122 /* widget creating functions */
123 static void prefs_actions_create (MainWindow *mainwin);
124 static void prefs_actions_set_dialog (void);
125 static gint prefs_actions_clist_set_row (gint row);
127 /* callback functions */
128 static void prefs_actions_help_cb (GtkWidget *w,
130 static void prefs_actions_register_cb (GtkWidget *w,
132 static void prefs_actions_substitute_cb (GtkWidget *w,
134 static void prefs_actions_delete_cb (GtkWidget *w,
136 static void prefs_actions_up (GtkWidget *w,
138 static void prefs_actions_down (GtkWidget *w,
140 static void prefs_actions_select (GtkCList *clist,
144 static void prefs_actions_row_move (GtkCList *clist,
147 static gint prefs_actions_deleted (GtkWidget *widget,
150 static void prefs_actions_key_pressed (GtkWidget *widget,
153 static void prefs_actions_cancel (GtkWidget *w,
155 static void prefs_actions_ok (GtkWidget *w,
157 static void update_actions_menu (GtkItemFactory *ifactory,
161 static void mainwin_actions_execute_cb (MainWindow *mainwin,
164 static void compose_actions_execute_cb (Compose *compose,
167 static guint get_action_type (gchar *action);
169 static gboolean execute_actions (gchar *action,
175 static gchar *parse_action_cmd (gchar *action,
179 static GString *parse_append_filename (GString *cmd,
182 static GString *parse_append_msgpart (GString *cmd,
186 ChildInfo *fork_child (gchar *cmd,
191 static gint wait_for_children (gpointer data);
193 static void free_children (Children *children);
195 static void childinfo_close_pipes (ChildInfo *child_info);
197 static void create_io_dialog (Children *children);
199 static void update_io_dialog (Children *children);
201 static void hide_io_dialog_cb (GtkWidget *widget,
204 static void catch_output (gpointer data,
206 GdkInputCondition cond);
207 static void catch_input (gpointer data,
209 GdkInputCondition cond);
210 static void catch_status (gpointer data,
212 GdkInputCondition cond);
214 void prefs_actions_open(MainWindow *mainwin)
217 if (prefs_rc_is_readonly(ACTIONS_RC))
223 prefs_actions_create(mainwin);
225 manage_window_set_transient(GTK_WINDOW(actions.window));
226 gtk_widget_grab_focus(actions.ok_btn);
228 prefs_actions_set_dialog();
230 gtk_widget_show(actions.window);
233 static void prefs_actions_create(MainWindow *mainwin)
238 GtkWidget *cancel_btn;
239 GtkWidget *confirm_area;
243 GtkWidget *entry_vbox;
245 GtkWidget *name_label;
246 GtkWidget *name_entry;
247 GtkWidget *cmd_label;
248 GtkWidget *cmd_entry;
254 GtkWidget *subst_btn;
257 GtkWidget *cond_hbox;
258 GtkWidget *cond_scrolledwin;
259 GtkWidget *cond_clist;
261 GtkWidget *help_vbox;
262 GtkWidget *help_label;
263 GtkWidget *help_toggle;
271 debug_print(_("Creating actions setting window...\n"));
273 window = gtk_window_new (GTK_WINDOW_DIALOG);
275 gtk_container_set_border_width(GTK_CONTAINER (window), 8);
276 gtk_window_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
277 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
278 gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, TRUE);
279 gtk_window_set_default_size(GTK_WINDOW(window), 400, -1);
281 vbox = gtk_vbox_new(FALSE, 6);
282 gtk_widget_show(vbox);
283 gtk_container_add(GTK_CONTAINER(window), vbox);
285 gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
286 &cancel_btn, _("Cancel"), NULL, NULL);
287 gtk_widget_show(confirm_area);
288 gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
289 gtk_widget_grab_default(ok_btn);
291 gtk_window_set_title(GTK_WINDOW(window), _("Actions setting"));
292 gtk_signal_connect(GTK_OBJECT(window), "delete_event",
293 GTK_SIGNAL_FUNC(prefs_actions_deleted), NULL);
294 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
295 GTK_SIGNAL_FUNC(prefs_actions_key_pressed), NULL);
296 MANAGE_WINDOW_SIGNALS_CONNECT(window);
297 gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
298 GTK_SIGNAL_FUNC(prefs_actions_ok), mainwin);
299 gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
300 GTK_SIGNAL_FUNC(prefs_actions_cancel), NULL);
302 vbox1 = gtk_vbox_new(FALSE, 8);
303 gtk_widget_show(vbox1);
304 gtk_box_pack_start(GTK_BOX(vbox), vbox1, TRUE, TRUE, 0);
305 gtk_container_set_border_width(GTK_CONTAINER(vbox1), 2);
307 entry_vbox = gtk_vbox_new(FALSE, 4);
308 gtk_box_pack_start(GTK_BOX(vbox1), entry_vbox, FALSE, FALSE, 0);
310 hbox = gtk_hbox_new(FALSE, 8);
311 gtk_box_pack_start(GTK_BOX(entry_vbox), hbox, FALSE, FALSE, 0);
313 name_label = gtk_label_new(_("Menu name:"));
314 gtk_box_pack_start(GTK_BOX(hbox), name_label, FALSE, FALSE, 0);
316 name_entry = gtk_entry_new();
317 gtk_box_pack_start(GTK_BOX(hbox), name_entry, TRUE, TRUE, 0);
319 hbox = gtk_hbox_new(FALSE, 8);
320 gtk_box_pack_start(GTK_BOX(entry_vbox), hbox, TRUE, TRUE, 0);
322 cmd_label = gtk_label_new(_("Command line:"));
323 gtk_box_pack_start(GTK_BOX(hbox), cmd_label, FALSE, FALSE, 0);
325 cmd_entry = gtk_entry_new();
326 gtk_box_pack_start(GTK_BOX(hbox), cmd_entry, TRUE, TRUE, 0);
328 gtk_widget_show_all(entry_vbox);
330 help_vbox = gtk_vbox_new(FALSE, 8);
331 gtk_box_pack_start(GTK_BOX(vbox1), help_vbox, FALSE, FALSE, 0);
333 help_label = gtk_label_new
335 " Use / in menu name to make submenus.\n"
338 " | to send message body or selection to command\n"
339 " > to send user provided text to command\n"
340 " * to send user provided hidden text to command\n"
342 " | to replace message body or selection with command output\n"
343 " & to run command asynchronously\n"
344 " Use %f for message file name\n"
345 " %F for the list of the file names of selected messages\n"
346 " %p for the selected message part."));
347 gtk_misc_set_alignment(GTK_MISC(help_label), 0, 0.5);
348 gtk_label_set_justify(GTK_LABEL(help_label), GTK_JUSTIFY_LEFT);
349 gtk_widget_show(help_label);
350 gtk_box_pack_start(GTK_BOX(help_vbox), help_label, FALSE, FALSE, 0);
351 gtk_widget_hide(help_vbox);
353 /* register / substitute / delete */
355 reg_hbox = gtk_hbox_new(FALSE, 4);
356 gtk_widget_show(reg_hbox);
357 gtk_box_pack_start(GTK_BOX(vbox1), reg_hbox, FALSE, FALSE, 0);
359 arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
360 gtk_widget_show(arrow);
361 gtk_box_pack_start(GTK_BOX(reg_hbox), arrow, FALSE, FALSE, 0);
362 gtk_widget_set_usize(arrow, -1, 16);
364 btn_hbox = gtk_hbox_new(TRUE, 4);
365 gtk_widget_show(btn_hbox);
366 gtk_box_pack_start(GTK_BOX(reg_hbox), btn_hbox, FALSE, FALSE, 0);
368 reg_btn = gtk_button_new_with_label(_("Register"));
369 gtk_widget_show(reg_btn);
370 gtk_box_pack_start(GTK_BOX(btn_hbox), reg_btn, FALSE, TRUE, 0);
371 gtk_signal_connect(GTK_OBJECT(reg_btn), "clicked",
372 GTK_SIGNAL_FUNC(prefs_actions_register_cb), NULL);
374 subst_btn = gtk_button_new_with_label(_(" Substitute "));
375 gtk_widget_show(subst_btn);
376 gtk_box_pack_start(GTK_BOX(btn_hbox), subst_btn, FALSE, TRUE, 0);
377 gtk_signal_connect(GTK_OBJECT(subst_btn), "clicked",
378 GTK_SIGNAL_FUNC(prefs_actions_substitute_cb),
381 del_btn = gtk_button_new_with_label(_("Delete"));
382 gtk_widget_show(del_btn);
383 gtk_box_pack_start(GTK_BOX(btn_hbox), del_btn, FALSE, TRUE, 0);
384 gtk_signal_connect(GTK_OBJECT(del_btn), "clicked",
385 GTK_SIGNAL_FUNC(prefs_actions_delete_cb), NULL);
387 help_toggle = gtk_toggle_button_new_with_label(_(" Syntax help "));
388 gtk_widget_show(help_toggle);
389 gtk_box_pack_end(GTK_BOX(reg_hbox), help_toggle, FALSE, FALSE, 0);
390 gtk_signal_connect(GTK_OBJECT(help_toggle), "toggled",
391 GTK_SIGNAL_FUNC(prefs_actions_help_cb), help_vbox);
393 cond_hbox = gtk_hbox_new(FALSE, 8);
394 gtk_widget_show(cond_hbox);
395 gtk_box_pack_start(GTK_BOX(vbox1), cond_hbox, TRUE, TRUE, 0);
397 cond_scrolledwin = gtk_scrolled_window_new(NULL, NULL);
398 gtk_widget_show(cond_scrolledwin);
399 gtk_widget_set_usize(cond_scrolledwin, -1, 150);
400 gtk_box_pack_start(GTK_BOX(cond_hbox), cond_scrolledwin,
402 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (cond_scrolledwin),
403 GTK_POLICY_AUTOMATIC,
404 GTK_POLICY_AUTOMATIC);
406 title[0] = _("Registered actions");
407 cond_clist = gtk_clist_new_with_titles(1, title);
408 gtk_widget_show(cond_clist);
409 gtk_container_add(GTK_CONTAINER (cond_scrolledwin), cond_clist);
410 gtk_clist_set_column_width(GTK_CLIST (cond_clist), 0, 80);
411 gtk_clist_set_selection_mode(GTK_CLIST (cond_clist),
412 GTK_SELECTION_BROWSE);
413 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(cond_clist)->column[0].button,
415 gtk_signal_connect(GTK_OBJECT(cond_clist), "select_row",
416 GTK_SIGNAL_FUNC(prefs_actions_select), NULL);
417 gtk_signal_connect_after(GTK_OBJECT(cond_clist), "row_move",
418 GTK_SIGNAL_FUNC(prefs_actions_row_move),
421 btn_vbox = gtk_vbox_new(FALSE, 8);
422 gtk_widget_show(btn_vbox);
423 gtk_box_pack_start(GTK_BOX(cond_hbox), btn_vbox, FALSE, FALSE, 0);
425 up_btn = gtk_button_new_with_label(_("Up"));
426 gtk_widget_show(up_btn);
427 gtk_box_pack_start(GTK_BOX(btn_vbox), up_btn, FALSE, FALSE, 0);
428 gtk_signal_connect(GTK_OBJECT(up_btn), "clicked",
429 GTK_SIGNAL_FUNC(prefs_actions_up), NULL);
431 down_btn = gtk_button_new_with_label(_("Down"));
432 gtk_widget_show(down_btn);
433 gtk_box_pack_start(GTK_BOX(btn_vbox), down_btn, FALSE, FALSE, 0);
434 gtk_signal_connect(GTK_OBJECT(down_btn), "clicked",
435 GTK_SIGNAL_FUNC(prefs_actions_down), NULL);
437 gtk_widget_show(window);
439 actions.window = window;
440 actions.ok_btn = ok_btn;
442 actions.name_entry = name_entry;
443 actions.cmd_entry = cmd_entry;
445 actions.actions_clist = cond_clist;
448 static void prefs_actions_help_cb(GtkWidget *w, gpointer data)
450 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)))
451 gtk_widget_show(GTK_WIDGET(data));
453 gtk_widget_hide(GTK_WIDGET(data));
456 void prefs_actions_read_config(void)
460 gchar buf[PREFSBUFSIZE];
463 debug_print(_("Reading actions configurations...\n"));
465 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
466 if ((fp = fopen(rcpath, "rb")) == NULL) {
467 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
473 while (prefs_common.actions_list != NULL) {
474 act = (gchar *)prefs_common.actions_list->data;
475 prefs_common.actions_list =
476 g_slist_remove(prefs_common.actions_list, act);
480 while (fgets(buf, sizeof(buf), fp) != NULL) {
482 act = strstr(buf, ": ");
484 get_action_type(&act[2]) != ACTION_ERROR)
485 prefs_common.actions_list =
486 g_slist_append(prefs_common.actions_list,
492 void prefs_actions_write_config(void)
498 debug_print(_("Writing actions configuration...\n"));
500 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
501 if ((pfile= prefs_write_open(rcpath)) == NULL) {
502 g_warning(_("failed to write configuration to file\n"));
507 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
508 gchar *act = (gchar *)cur->data;
509 if (fputs(act, pfile->fp) == EOF ||
510 fputc('\n', pfile->fp) == EOF) {
511 FILE_OP_ERROR(rcpath, "fputs || fputc");
512 prefs_write_close_revert(pfile);
520 if (prefs_write_close(pfile) < 0) {
521 g_warning(_("failed to write configuration to file\n"));
526 static guint get_action_type(gchar *action)
529 guint action_type = ACTION_NONE;
531 g_return_val_if_fail(action, ACTION_ERROR);
532 g_return_val_if_fail(*action, ACTION_ERROR);
537 action_type |= ACTION_PIPE_IN;
539 } else if (p[0] == '>') {
540 action_type |= ACTION_OPEN_IN;
542 } else if (p[0] == '*') {
543 action_type |= ACTION_HIDE_IN;
550 while (*p && action_type != ACTION_ERROR) {
554 action_type |= ACTION_SINGLE;
557 action_type |= ACTION_MULTIPLE;
560 action_type |= ACTION_SINGLE;
563 action_type = ACTION_ERROR;
566 } else if (p[0] == '|') {
568 action_type |= ACTION_PIPE_OUT;
569 } else if (p[0] == '&') {
571 action_type |= ACTION_ASYNC;
573 action_type = ACTION_ERROR;
581 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
582 GtkCTree *ctree, MimeView *mimeview)
584 GString *cmd, *tmpcmd;
591 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
594 cmd = g_string_sized_new(strlen(action));
596 while (p[0] && !(p[0] == '|' && !p[1]) && p[0] != '&') {
597 if (p[0] == '%' && p[1]) {
600 cmd = parse_append_filename(cmd, msginfo);
604 for (cur = GTK_CLIST(ctree)->selection;
605 cur != NULL; cur = cur->next) {
606 msg = gtk_ctree_node_get_row_data(ctree,
607 GTK_CTREE_NODE(cur->data));
608 cmd = parse_append_filename(cmd, msg);
610 cmd = g_string_append_c(cmd, ' ');
615 tmpcmd = parse_append_msgpart
616 (cmd, msginfo, mimeview);
620 g_string_free(cmd, TRUE);
626 cmd = g_string_append_c(cmd, p[0]);
627 cmd = g_string_append_c(cmd, p[1]);
631 cmd = g_string_append_c(cmd, p[0]);
636 g_string_free(cmd, TRUE);
641 g_string_free(cmd, FALSE);
645 static GString *parse_append_filename(GString *cmd, MsgInfo *msginfo)
649 g_return_val_if_fail(msginfo, cmd);
651 filename = procmsg_get_message_file(msginfo);
654 cmd = g_string_append(cmd, filename);
661 static GString *parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
672 if ((fp = procmsg_open_message_decrypted(msginfo, &partinfo))
674 alertpanel_error(_("Could not get message file."));
678 if ((fp = procmsg_open_message(msginfo)) == NULL) {
679 alertpanel_error(_("Could not get message file."));
682 partinfo = procmime_scan_mime_header(fp);
686 procmime_mimeinfo_free(partinfo);
687 alertpanel_error(_("Could not get message part."));
690 filename = procmsg_get_message_file(msginfo);
692 if (!mimeview->opened) {
693 alertpanel_error(_("No message part selected."));
696 if (!mimeview->file) {
697 alertpanel_error(_("No message file selected."));
700 partinfo = gtk_ctree_node_get_row_data
701 (GTK_CTREE(mimeview->ctree),
703 g_return_val_if_fail(partinfo != NULL, cmd);
704 filename = mimeview->file;
706 partname = procmime_get_tmp_file_name(partinfo);
708 ret = procmime_get_part(partname, filename, partinfo);
711 procmime_mimeinfo_free(partinfo);
716 alertpanel_error(_("Can't get part of multipart message"));
721 cmd = g_string_append(cmd,partname);
728 static void prefs_actions_set_dialog(void)
730 GtkCList *clist = GTK_CLIST(actions.actions_clist);
732 gchar *action_str[1];
735 gtk_clist_freeze(clist);
736 gtk_clist_clear(clist);
738 action_str[0] = _("(New)");
739 row = gtk_clist_append(clist, action_str);
740 gtk_clist_set_row_data(clist, row, NULL);
742 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
745 action[0] = (gchar *)cur->data;
746 row = gtk_clist_append(clist, action);
747 gtk_clist_set_row_data(clist, row, action[0]);
750 gtk_clist_thaw(clist);
753 static void prefs_actions_set_list(void)
758 g_slist_free(prefs_common.actions_list);
759 prefs_common.actions_list = NULL;
761 while ((action = (gchar *)gtk_clist_get_row_data
762 (GTK_CLIST(actions.actions_clist), row)) != NULL) {
763 prefs_common.actions_list =
764 g_slist_append(prefs_common.actions_list, action);
769 #define GET_ENTRY(entry) \
770 entry_text = gtk_entry_get_text(GTK_ENTRY(entry))
772 static gint prefs_actions_clist_set_row(gint row)
774 GtkCList *clist = GTK_CLIST(actions.actions_clist);
777 gchar action[PREFSBUFSIZE];
780 g_return_val_if_fail(row != 0, -1);
782 GET_ENTRY(actions.name_entry);
783 if (entry_text[0] == '\0') {
784 alertpanel_error(_("Menu name is not set."));
788 if (strchr(entry_text, ':')) {
789 alertpanel_error(_("Colon ':' is not allowed in the menu name."));
793 strncpy(action, entry_text, PREFSBUFSIZE - 1);
796 /* Keep space for the ': ' delimiter */
797 len = strlen(action) + 2;
798 if (len >= PREFSBUFSIZE - 1) {
799 alertpanel_error(_("Menu name is too long."));
803 strcat(action, ": ");
805 GET_ENTRY(actions.cmd_entry);
807 if (entry_text[0] == '\0') {
808 alertpanel_error(_("Command line not set."));
812 if (len + strlen(entry_text) >= PREFSBUFSIZE - 1) {
813 alertpanel_error(_("Menu name and command are too long."));
817 if (get_action_type(entry_text) == ACTION_ERROR) {
818 alertpanel_error(_("The command\n%s\nhas a syntax error."),
823 strcat(action, entry_text);
827 row = gtk_clist_append(clist, buf);
830 gtk_clist_set_text(clist, row, 0, action);
831 old_action = (gchar *) gtk_clist_get_row_data(clist, row);
836 buf[0] = g_strdup(action);
838 gtk_clist_set_row_data(clist, row, buf[0]);
840 prefs_actions_set_list();
845 /* callback functions */
847 static void prefs_actions_register_cb(GtkWidget *w, gpointer data)
849 prefs_actions_clist_set_row(-1);
852 static void prefs_actions_substitute_cb(GtkWidget *w, gpointer data)
854 GtkCList *clist = GTK_CLIST(actions.actions_clist);
858 if (!clist->selection) return;
860 row = GPOINTER_TO_INT(clist->selection->data);
861 if (row == 0) return;
863 action = gtk_clist_get_row_data(clist, row);
866 prefs_actions_clist_set_row(row);
869 static void prefs_actions_delete_cb(GtkWidget *w, gpointer data)
871 GtkCList *clist = GTK_CLIST(actions.actions_clist);
875 if (!clist->selection) return;
876 row = GPOINTER_TO_INT(clist->selection->data);
877 if (row == 0) return;
879 if (alertpanel(_("Delete action"),
880 _("Do you really want to delete this action?"),
881 _("Yes"), _("No"), NULL) == G_ALERTALTERNATE)
884 action = gtk_clist_get_row_data(clist, row);
886 gtk_clist_remove(clist, row);
887 prefs_common.actions_list = g_slist_remove(prefs_common.actions_list,
891 static void prefs_actions_up(GtkWidget *w, gpointer data)
893 GtkCList *clist = GTK_CLIST(actions.actions_clist);
896 if (!clist->selection) return;
898 row = GPOINTER_TO_INT(clist->selection->data);
900 gtk_clist_row_move(clist, row, row - 1);
903 static void prefs_actions_down(GtkWidget *w, gpointer data)
905 GtkCList *clist = GTK_CLIST(actions.actions_clist);
908 if (!clist->selection) return;
910 row = GPOINTER_TO_INT(clist->selection->data);
911 if (row > 0 && row < clist->rows - 1)
912 gtk_clist_row_move(clist, row, row + 1);
915 #define ENTRY_SET_TEXT(entry, str) \
916 gtk_entry_set_text(GTK_ENTRY(entry), str ? str : "")
918 static void prefs_actions_select(GtkCList *clist, gint row, gint column,
923 gchar buf[PREFSBUFSIZE];
924 action = gtk_clist_get_row_data(clist, row);
927 ENTRY_SET_TEXT(actions.name_entry, "");
928 ENTRY_SET_TEXT(actions.cmd_entry, "");
932 strncpy(buf, action, PREFSBUFSIZE - 1);
933 buf[PREFSBUFSIZE - 1] = 0x00;
934 cmd = strstr(buf, ": ");
937 ENTRY_SET_TEXT(actions.cmd_entry, &cmd[2]);
942 ENTRY_SET_TEXT(actions.name_entry, buf);
945 static void prefs_actions_row_move(GtkCList *clist,
946 gint source_row, gint dest_row)
948 prefs_actions_set_list();
949 if (gtk_clist_row_is_visible(clist, dest_row) != GTK_VISIBILITY_FULL) {
950 gtk_clist_moveto(clist, dest_row, -1,
951 source_row < dest_row ? 1.0 : 0.0, 0.0);
955 static gint prefs_actions_deleted(GtkWidget *widget, GdkEventAny *event,
958 prefs_actions_cancel(widget, data);
962 static void prefs_actions_key_pressed(GtkWidget *widget, GdkEventKey *event,
965 if (event && event->keyval == GDK_Escape)
966 prefs_actions_cancel(widget, data);
969 static void prefs_actions_cancel(GtkWidget *w, gpointer data)
971 prefs_actions_read_config();
972 gtk_widget_hide(actions.window);
976 static void prefs_actions_ok(GtkWidget *widget, gpointer data)
978 GtkItemFactory *ifactory;
979 MainWindow *mainwin = (MainWindow *)data;
981 prefs_actions_write_config();
982 ifactory = gtk_item_factory_from_widget(mainwin->menubar);
983 update_mainwin_actions_menu(ifactory, mainwin);
984 gtk_widget_hide(actions.window);
988 void update_mainwin_actions_menu(GtkItemFactory *ifactory,
991 update_actions_menu(ifactory, "/Tools/Actions",
992 mainwin_actions_execute_cb,
996 void update_compose_actions_menu(GtkItemFactory *ifactory,
1000 update_actions_menu(ifactory, branch_path,
1001 compose_actions_execute_cb,
1005 static void update_actions_menu(GtkItemFactory *ifactory,
1010 GtkWidget *menuitem;
1013 gchar *action, *action_p;
1015 GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
1017 ifentry.path = branch_path;
1018 menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
1019 g_return_if_fail(menuitem != NULL);
1021 amenu = GTK_MENU_SHELL(menuitem)->children;
1022 while (amenu != NULL) {
1023 GList *alist = amenu->next;
1024 gtk_widget_destroy(GTK_WIDGET(amenu->data));
1028 ifentry.accelerator = NULL;
1029 ifentry.callback_action = 0;
1030 ifentry.callback = callback;
1031 ifentry.item_type = NULL;
1033 for (cur = prefs_common.actions_list; cur; cur = cur->next) {
1034 action = g_strdup((gchar *)cur->data);
1035 action_p = strstr(action, ": ");
1036 if (action_p && action_p[2] &&
1037 get_action_type(&action_p[2]) != ACTION_ERROR) {
1039 menu_path = g_strdup_printf("%s/%s", branch_path,
1041 ifentry.path = menu_path;
1042 gtk_item_factory_create_item(ifactory, &ifentry, data,
1047 ifentry.callback_action++;
1051 static void compose_actions_execute_cb(Compose *compose, guint action_nb,
1054 gchar *buf, *action;
1057 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
1059 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
1060 g_return_if_fail(buf != NULL);
1061 action = strstr(buf, ": ");
1062 g_return_if_fail(action != NULL);
1064 /* Point to the beginning of the command-line */
1067 action_type = get_action_type(action);
1068 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
1070 (_("The selected action cannot be used in the compose window\n"
1071 "because it contains %%f, %%F or %%p."));
1075 execute_actions(action, compose->window, NULL, compose->text, NULL);
1078 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
1081 MessageView *messageview = mainwin->messageview;
1082 GtkWidget *text = NULL;
1085 MimeView *mimeview = NULL;
1087 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
1089 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
1091 g_return_if_fail(buf);
1092 g_return_if_fail(action = strstr(buf, ": "));
1094 /* Point to the beginning of the command-line */
1097 switch (messageview->type) {
1099 if (messageview->textview && messageview->textview->text)
1100 text = messageview->textview->text;
1103 if (messageview->mimeview &&
1104 messageview->mimeview->type == MIMEVIEW_TEXT &&
1105 messageview->mimeview->textview &&
1106 messageview->mimeview->textview->text) {
1107 text = messageview->mimeview->textview->text;
1108 mimeview = messageview->mimeview;
1113 execute_actions(action, mainwin->window,
1114 GTK_CTREE(mainwin->summaryview->ctree), text, mimeview);
1117 static gboolean execute_actions(gchar *action, GtkWidget *window,
1118 GtkCTree *ctree, GtkWidget *text,
1121 GList *cur, *selection = NULL;
1122 GSList *children_list = NULL;
1124 gint selection_len = 0;
1126 ChildInfo *child_info;
1131 g_return_val_if_fail(action && *action, FALSE);
1133 action_type = get_action_type(action);
1135 if (action_type == ACTION_ERROR)
1136 return FALSE; /* ERR: syntax error */
1138 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) &&
1139 !(ctree && GTK_CLIST(ctree)->selection))
1140 return FALSE; /* ERR: file command without selection */
1143 selection = GTK_CLIST(ctree)->selection;
1144 selection_len = g_list_length(selection);
1147 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN)) {
1148 if (ctree && selection_len > 1)
1149 return FALSE; /* ERR: pipe + multiple selection */
1151 return FALSE; /* ERR: pipe and no displayed text */
1154 if (!(action_type & ACTION_ASYNC) && window) {
1155 gtk_widget_set_sensitive(window, FALSE);
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 */
1205 if (!(action_type & ACTION_ASYNC) && window) {
1206 gtk_widget_set_sensitive(window, TRUE);
1211 children->action = g_strdup(action);
1212 children->window = window;
1213 children->dialog = NULL;
1214 children->list = children_list;
1215 children->nb = g_slist_length(children_list);
1217 for (cur = children_list; cur; cur = cur->next) {
1218 child_info = (ChildInfo *) cur->data;
1219 child_info->tag_status =
1220 gdk_input_add(child_info->chld_status,
1222 catch_status, child_info);
1224 children->timer = children->open_in ? 0 :
1225 gtk_timeout_add(WAIT_LAP, wait_for_children,
1228 if (children->open_in)
1229 create_io_dialog(children);
1234 ChildInfo *fork_child(gchar *cmd, gint action_type, GtkWidget *text,
1237 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
1239 gint start, end, is_selection;
1242 ChildInfo *child_info;
1245 sync = !(action_type & ACTION_ASYNC);
1247 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
1248 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
1251 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
1253 alertpanel_error(_("Command could not be started. "
1254 "Pipe creation failed.\n%s"),
1256 /* Closing fd = -1 fails silently */
1263 close(chld_status[0]);
1264 close(chld_status[1]);
1265 return NULL; /* Pipe error */
1269 debug_print(_("Forking child and grandchild.\n"));
1272 if (pid == 0) { /* Child */
1276 close(ConnectionNumber(gdk_display));
1281 if (setpgid(0, getppid()))
1288 close(fileno(stdin));
1294 close(fileno(stdout));
1299 close(fileno(stderr));
1309 execvp("/bin/sh", cmdline);
1313 } else if (gch_pid < (pid_t) 0) { /* Fork error */
1315 write(chld_status[1], "1\n", 2);
1326 close(chld_status[0]);
1329 debug_print(_("Child: Waiting for grandchild\n"));
1330 waitpid(gch_pid, NULL, 0);
1331 debug_print(_("Child: grandchild ended\n"));
1333 write(chld_status[1], "0\n", 2);
1334 close(chld_status[1]);
1338 } else if (pid < 0) { /* Fork error */
1339 alertpanel_error(_("Could not fork to execute the following "
1340 "command:\n%s\n%s"),
1341 cmd, g_strerror(errno));
1351 if (!(action_type & (ACTION_PIPE_IN | ACTION_OPEN_IN | ACTION_HIDE_IN)))
1355 close(chld_status[1]);
1357 child_info = g_new0(ChildInfo, 1);
1359 child_info->children = children;
1361 child_info->pid = pid;
1362 child_info->cmd = g_strdup(cmd);
1363 child_info->type = action_type;
1364 child_info->new_out = FALSE;
1365 child_info->output = g_string_sized_new(0);
1366 child_info->chld_in =
1368 (ACTION_PIPE_IN | ACTION_OPEN_IN | ACTION_HIDE_IN))
1370 child_info->chld_out = chld_out[0];
1371 child_info->chld_err = chld_err[0];
1372 child_info->chld_status = chld_status[0];
1373 child_info->tag_in = -1;
1374 child_info->tag_out = gdk_input_add(chld_out[0], GDK_INPUT_READ,
1375 catch_output, child_info);
1376 child_info->tag_err = gdk_input_add(chld_err[0], GDK_INPUT_READ,
1377 catch_output, child_info);
1379 if (!(action_type & (ACTION_PIPE_IN | ACTION_PIPE_OUT)))
1382 child_info->text = text;
1385 end = gtk_stext_get_length(GTK_STEXT(text));
1387 if (GTK_EDITABLE(text)->has_selection) {
1388 start = GTK_EDITABLE(text)->selection_start_pos;
1389 end = GTK_EDITABLE(text)->selection_end_pos;
1396 is_selection = TRUE;
1399 end = gtk_stext_get_length(GTK_STEXT(text));
1400 is_selection = FALSE;
1404 selection = gtk_editable_get_chars(GTK_EDITABLE(text), start, end);
1406 if (action_type & ACTION_PIPE_IN) {
1407 write(chld_in[1], selection, strlen(selection));
1408 if (!(action_type & (ACTION_OPEN_IN | ACTION_HIDE_IN)))
1410 child_info->chld_in = -1; /* No more input */
1414 gtk_stext_freeze(GTK_STEXT(text));
1415 if (action_type & ACTION_PIPE_OUT) {
1416 gtk_stext_set_point(GTK_STEXT(text), start);
1417 gtk_stext_forward_delete(GTK_STEXT(text), end - start);
1420 gtk_stext_thaw(GTK_STEXT(text));
1425 static void kill_children_cb(GtkWidget *widget, gpointer data)
1428 Children *children = (Children *) data;
1429 ChildInfo *child_info;
1431 for (cur = children->list; cur; cur = cur->next) {
1432 child_info = (ChildInfo *)(cur->data);
1433 debug_print(_("Killing child group id %d\n"), child_info->pid);
1434 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
1439 static gint wait_for_children(gpointer data)
1441 gboolean new_output;
1442 Children *children = (Children *)data;
1443 ChildInfo *child_info;
1445 gint nb = children->nb;
1449 cur = children->list;
1452 child_info = (ChildInfo *)cur->data;
1453 if (child_info->pid)
1455 new_output |= child_info->new_out;
1459 if (!children->dialog &&
1460 (new_output || children->timer))
1461 create_io_dialog(children);
1463 if (children->timer) {
1464 gtk_timeout_remove(children->timer);
1465 children->timer = 0;
1467 children->output |= new_output;
1469 if (new_output || (children->dialog && (nb != children->nb)))
1470 update_io_dialog(children);
1475 if (!children->dialog) {
1476 gtk_widget_set_sensitive(children->window, TRUE);
1477 free_children(children);
1478 } else if (!children->output) {
1479 gtk_widget_destroy(children->dialog);
1484 static void send_input(GtkWidget *w, gpointer data)
1486 Children *children = (Children *) data;
1487 ChildInfo *child_info = (ChildInfo *) children->list->data;
1489 child_info->tag_in = gdk_input_add(child_info->chld_in,
1491 catch_input, children);
1492 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1495 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1497 hide_io_dialog_cb(w, data);
1501 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1504 Children *children = (Children *)data;
1506 if (!children->nb) {
1507 gtk_widget_set_sensitive(children->window, TRUE);
1508 gtk_signal_disconnect_by_data(GTK_OBJECT(children->dialog),
1510 gtk_widget_destroy(children->dialog);
1511 free_children(children);
1515 static void childinfo_close_pipes(ChildInfo *child_info)
1517 if (child_info->tag_in > 0)
1518 gdk_input_remove(child_info->tag_in);
1519 gdk_input_remove(child_info->tag_out);
1520 gdk_input_remove(child_info->tag_err);
1522 if (child_info->chld_in >= 0)
1523 close(child_info->chld_in);
1524 close(child_info->chld_out);
1525 close(child_info->chld_err);
1526 close(child_info->chld_status);
1529 static void free_children(Children *children)
1532 ChildInfo *child_info;
1534 debug_print(_("Freeing children data %p\n"), children);
1536 g_free(children->action);
1537 for (cur = children->list; cur;) {
1538 child_info = (ChildInfo *)cur->data;
1539 g_free(child_info->cmd);
1540 g_string_free(child_info->output, TRUE);
1541 children->list = g_slist_remove(children->list, child_info);
1543 cur = children->list;
1548 static void update_io_dialog(Children *children)
1552 debug_print(_("Updating actions input/output dialog.\n"));
1554 if (!children->nb) {
1555 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1556 gtk_widget_set_sensitive(children->hide_btn, TRUE);
1557 gtk_widget_set_sensitive(children->window, TRUE);
1558 if (children->input_hbox)
1559 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1562 if (children->output) {
1563 GtkWidget *text = children->text;
1565 ChildInfo *child_info;
1567 gtk_widget_show(children->scrolledwin);
1568 gtk_text_freeze(GTK_TEXT(text));
1569 gtk_text_set_point(GTK_TEXT(text), 0);
1570 gtk_text_forward_delete(GTK_TEXT(text),
1571 gtk_text_get_length(GTK_TEXT(text)));
1572 for (cur = children->list; cur; cur = cur->next) {
1573 child_info = (ChildInfo *)cur->data;
1574 if (child_info->pid)
1575 caption = g_strdup_printf
1576 (_("--- Running: %s\n"),
1579 caption = g_strdup_printf
1580 (_("--- Ended: %s\n"),
1583 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1585 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1586 child_info->output->str, -1);
1588 child_info->new_out = FALSE;
1590 gtk_text_thaw(GTK_TEXT(text));
1594 static void create_io_dialog(Children *children)
1597 GtkWidget *entry = NULL;
1598 GtkWidget *input_hbox = NULL;
1599 GtkWidget *send_button;
1602 GtkWidget *scrolledwin;
1604 GtkWidget *abort_button;
1605 GtkWidget *hide_button;
1607 debug_print(_("Creating actions dialog\n"));
1609 dialog = gtk_dialog_new();
1610 label = gtk_label_new(children->action);
1611 gtk_misc_set_padding(GTK_MISC(label), 8, 8);
1612 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, FALSE,
1615 gtk_window_set_title(GTK_WINDOW(dialog), _("Actions' input/output"));
1616 gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
1617 GTK_SIGNAL_FUNC(delete_io_dialog_cb), children);
1618 gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
1619 GTK_SIGNAL_FUNC(hide_io_dialog_cb),
1622 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1623 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1624 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
1625 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), scrolledwin, TRUE,
1627 gtk_widget_set_usize(scrolledwin, 600, 200);
1628 gtk_widget_hide(scrolledwin);
1630 text = gtk_text_new(gtk_scrolled_window_get_hadjustment
1631 (GTK_SCROLLED_WINDOW(scrolledwin)),
1632 gtk_scrolled_window_get_vadjustment
1633 (GTK_SCROLLED_WINDOW(scrolledwin)));
1634 gtk_text_set_editable(GTK_TEXT(text), FALSE);
1635 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1636 gtk_widget_show(text);
1638 if (children->open_in) {
1639 input_hbox = gtk_hbox_new(FALSE, 8);
1640 gtk_widget_show(input_hbox);
1642 entry = gtk_entry_new();
1643 gtk_signal_connect(GTK_OBJECT(entry), "activate",
1644 GTK_SIGNAL_FUNC(send_input), children);
1645 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 8);
1646 if (children->open_in & ACTION_HIDE_IN)
1647 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1648 gtk_widget_show(entry);
1650 send_button = gtk_button_new_with_label(_("Send"));
1651 gtk_signal_connect(GTK_OBJECT(send_button), "clicked",
1652 GTK_SIGNAL_FUNC(send_input), children);
1653 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1655 gtk_widget_show(send_button);
1657 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1658 input_hbox, FALSE, FALSE, 8);
1659 gtk_widget_grab_focus(entry);
1662 hbox = gtk_hbox_new(TRUE, 0);
1663 gtk_widget_show(hbox);
1665 abort_button = gtk_button_new_with_label(_("Abort actions"));
1666 gtk_signal_connect(GTK_OBJECT(abort_button), "clicked",
1667 GTK_SIGNAL_FUNC(kill_children_cb), children);
1668 gtk_box_pack_start(GTK_BOX(hbox), abort_button, TRUE, TRUE, 8);
1669 gtk_widget_show(abort_button);
1671 hide_button = gtk_button_new_with_label(_("Close window"));
1672 gtk_signal_connect(GTK_OBJECT(hide_button), "clicked",
1673 GTK_SIGNAL_FUNC(hide_io_dialog_cb), children);
1674 gtk_box_pack_start(GTK_BOX(hbox), hide_button, TRUE, TRUE, 8);
1675 gtk_widget_show(hide_button);
1678 gtk_widget_set_sensitive(hide_button, FALSE);
1680 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1682 children->dialog = dialog;
1683 children->scrolledwin = scrolledwin;
1684 children->text = text;
1685 children->input_hbox = children->open_in ? input_hbox : NULL;
1686 children->input_entry = children->open_in ? entry : NULL;
1687 children->abort_btn = abort_button;
1688 children->hide_btn = hide_button;
1690 gtk_widget_show(dialog);
1693 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1695 ChildInfo *child_info = (ChildInfo *)data;
1699 gdk_input_remove(child_info->tag_status);
1701 c = read(source, &buf, 1);
1702 debug_print(_("Child returned %c\n"), buf);
1704 waitpid(-child_info->pid, NULL, 0);
1705 childinfo_close_pipes(child_info);
1706 child_info->pid = 0;
1708 wait_for_children(child_info->children);
1711 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1713 Children *children = (Children *)data;
1714 ChildInfo *child_info = (ChildInfo *)children->list->data;
1718 debug_print(_("Sending input to grand child.\n"));
1719 if (!(cond && GDK_INPUT_WRITE))
1722 gdk_input_remove(child_info->tag_in);
1723 child_info->tag_in = -1;
1725 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1727 c = write(child_info->chld_in, input, strlen(input));
1731 write(child_info->chld_in, "\n", 2);
1733 gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1734 gtk_widget_set_sensitive(children->input_hbox, TRUE);
1735 debug_print(_("Input to grand child sent.\n"));
1738 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1740 ChildInfo *child_info = (ChildInfo *)data;
1742 gchar buf[PREFSBUFSIZE];
1744 debug_print(_("Catching grand child's output.\n"));
1745 if (child_info->type & ACTION_PIPE_OUT
1746 && source == child_info->chld_out) {
1747 gboolean is_selection = FALSE;
1748 GtkWidget *text = child_info->text;
1749 if (GTK_EDITABLE(text)->has_selection)
1750 is_selection = TRUE;
1751 gtk_stext_freeze(GTK_STEXT(text));
1753 c = read(source, buf, PREFSBUFSIZE - 1);
1756 gtk_stext_insert(GTK_STEXT(text), NULL, NULL, NULL,
1760 /* Using the select_region draws things. Should not.
1761 * so we just change selection position and
1762 * defere drawing when thawing. Hack?
1764 GTK_EDITABLE(text)->selection_end_pos =
1765 gtk_stext_get_point(GTK_STEXT(text));
1767 gtk_stext_thaw(GTK_STEXT(child_info->text));
1769 c = read(source, buf, PREFSBUFSIZE - 1);
1770 for (i = 0; i < c; i++)
1771 child_info->output = g_string_append_c
1772 (child_info->output, buf[i]);
1774 child_info->new_out = TRUE;