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"
53 #define WAIT_LAP 10000
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,
67 static struct Actions {
72 GtkWidget *name_entry;
75 GtkWidget *actions_clist;
78 typedef struct _Children Children;
83 GtkWidget *input_entry;
84 GtkWidget *input_hbox;
87 GtkWidget *scrolledwin;
97 typedef struct _ChildInfo ChildInfo;
116 /* widget creating functions */
117 static void prefs_actions_create (MainWindow *mainwin);
118 static void prefs_actions_set_dialog (void);
119 static gint prefs_actions_clist_set_row (gint row);
121 /* callback functions */
122 static void prefs_actions_help_cb (GtkWidget *w, gpointer data);
123 static void prefs_actions_register_cb (GtkWidget *w, gpointer data);
124 static void prefs_actions_substitute_cb (GtkWidget *w, gpointer data);
125 static void prefs_actions_delete_cb (GtkWidget *w, gpointer data);
126 static void prefs_actions_up (GtkWidget *w, gpointer data);
127 static void prefs_actions_down (GtkWidget *w, gpointer data);
128 static void prefs_actions_select (GtkCList *clist,
132 static void prefs_actions_row_move (GtkCList *clist,
135 static gint prefs_actions_deleted (GtkWidget *widget,
138 static void prefs_actions_key_pressed (GtkWidget *widget,
141 static void prefs_actions_cancel (GtkWidget *w, gpointer data);
142 static void prefs_actions_ok (GtkWidget *w, gpointer data);
143 static void update_actions_menu (GtkItemFactory *ifactory,
147 static void mainwin_actions_execute_cb (MainWindow *mainwin,
150 static void compose_actions_execute_cb (Compose *compose,
153 static guint get_action_type (gchar *action);
155 static gboolean execute_actions (gchar *action,
160 static gchar *parse_action_cmd (gchar *action,
163 static GString *parse_append_filename (GString *cmd,
166 ChildInfo *fork_child (gchar *cmd,
171 static gint wait_for_children (gpointer data);
173 static void free_children (Children *children);
175 static void childinfo_close_pipes (ChildInfo *child_info);
177 static void create_io_dialog (Children *children);
179 static void update_io_dialog (Children *children);
181 static void hide_io_dialog_cb (GtkWidget *widget,
184 static void catch_output (gpointer data,
186 GdkInputCondition cond);
187 static void catch_input (gpointer data,
189 GdkInputCondition cond);
190 static void catch_status (gpointer data,
192 GdkInputCondition cond);
194 void prefs_actions_open(MainWindow *mainwin)
196 if (prefs_rc_is_readonly(ACTIONS_RC))
201 prefs_actions_create(mainwin);
203 manage_window_set_transient(GTK_WINDOW(actions.window));
204 gtk_widget_grab_focus(actions.ok_btn);
206 prefs_actions_set_dialog();
208 gtk_widget_show(actions.window);
211 static void prefs_actions_create(MainWindow *mainwin)
216 GtkWidget *cancel_btn;
217 GtkWidget *confirm_area;
221 GtkWidget *name_label;
222 GtkWidget *name_entry;
223 GtkWidget *cmd_label;
224 GtkWidget *cmd_entry;
230 GtkWidget *subst_btn;
233 GtkWidget *cond_hbox;
234 GtkWidget *cond_scrolledwin;
235 GtkWidget *cond_clist;
237 GtkWidget *help_vbox;
238 GtkWidget *help_label;
239 GtkWidget *help_toggle;
247 debug_print(_("Creating actions setting window...\n"));
249 window = gtk_window_new (GTK_WINDOW_DIALOG);
251 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
252 gtk_window_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
253 gtk_window_set_modal (GTK_WINDOW (window), TRUE);
254 gtk_window_set_policy (GTK_WINDOW (window), FALSE, TRUE, TRUE);
256 vbox = gtk_vbox_new (FALSE, 6);
257 gtk_widget_show (vbox);
258 gtk_container_add (GTK_CONTAINER (window), vbox);
260 gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
261 &cancel_btn, _("Cancel"), NULL, NULL);
262 gtk_widget_show (confirm_area);
263 gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
264 gtk_widget_grab_default (ok_btn);
266 gtk_window_set_title (GTK_WINDOW(window),
267 _("Actions setting"));
268 gtk_signal_connect (GTK_OBJECT(window), "delete_event",
269 GTK_SIGNAL_FUNC(prefs_actions_deleted), NULL);
270 gtk_signal_connect (GTK_OBJECT(window), "key_press_event",
271 GTK_SIGNAL_FUNC(prefs_actions_key_pressed), NULL);
272 MANAGE_WINDOW_SIGNALS_CONNECT (window);
273 gtk_signal_connect (GTK_OBJECT(ok_btn), "clicked",
274 GTK_SIGNAL_FUNC(prefs_actions_ok), mainwin);
275 gtk_signal_connect (GTK_OBJECT(cancel_btn), "clicked",
276 GTK_SIGNAL_FUNC(prefs_actions_cancel), NULL);
278 vbox1 = gtk_vbox_new (FALSE, VSPACING);
279 gtk_widget_show (vbox1);
280 gtk_box_pack_start (GTK_BOX (vbox), vbox1, TRUE, TRUE, 0);
281 gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
283 hbox = gtk_hbox_new (FALSE, 0);
284 gtk_box_pack_start (GTK_BOX(vbox1), hbox, TRUE, TRUE, 0);
286 name_label = gtk_label_new(_("Menu name: "));
287 gtk_widget_show(name_label);
288 gtk_box_pack_start (GTK_BOX (hbox), name_label, FALSE, FALSE, 0);
290 name_entry = gtk_entry_new();
291 gtk_widget_show(name_entry);
292 gtk_box_pack_start (GTK_BOX (hbox), name_entry, TRUE, TRUE, 0);
294 gtk_widget_show_all(hbox);
296 hbox = gtk_hbox_new (FALSE, 0);
297 gtk_box_pack_start (GTK_BOX(vbox1), hbox, TRUE, TRUE, 0);
299 cmd_label = gtk_label_new(_("Command line: "));
300 gtk_box_pack_start (GTK_BOX (hbox), cmd_label, FALSE, FALSE, 0);
302 cmd_entry = gtk_entry_new();
303 gtk_box_pack_start (GTK_BOX (hbox), cmd_entry, TRUE, TRUE, 0);
305 gtk_widget_show_all(hbox);
307 help_vbox = gtk_vbox_new(FALSE, 8);
308 gtk_box_pack_start (GTK_BOX (vbox1), help_vbox, TRUE, TRUE, 0);
310 help_label = gtk_label_new (_("Menu name:\n"
311 " Use '/' in menu name to make submenus.\n"
313 " Begin with:\n '|' to send message "
315 "to command\n '>' to send user provided"
316 " text to command\n '*' to send user "
317 "provided hidden text to command\n"
318 " End with:\n '|' to replace "
319 "message body or selection with command "
321 "run command asynchronously\n Use '%f' "
322 "for message file name\n and '%F' for the"
323 " list of the file names of selected "
325 gtk_misc_set_alignment(GTK_MISC(help_label), 0, 0.5);
326 gtk_label_set_justify (GTK_LABEL(help_label), GTK_JUSTIFY_LEFT);
327 gtk_widget_show (help_label);
328 gtk_box_pack_start (GTK_BOX (help_vbox), help_label, TRUE, TRUE, 0);
329 gtk_widget_hide(help_vbox);
331 /* register / substitute / delete */
333 reg_hbox = gtk_hbox_new (FALSE, 4);
334 gtk_widget_show (reg_hbox);
335 gtk_box_pack_start (GTK_BOX (vbox1), reg_hbox, FALSE, FALSE, 0);
337 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
338 gtk_widget_show (arrow);
339 gtk_box_pack_start (GTK_BOX (reg_hbox), arrow, FALSE, FALSE, 0);
340 gtk_widget_set_usize (arrow, -1, 16);
342 btn_hbox = gtk_hbox_new (FALSE, 4);
343 gtk_widget_show (btn_hbox);
344 gtk_box_pack_start (GTK_BOX (reg_hbox), btn_hbox, FALSE, FALSE, 0);
346 reg_btn = gtk_button_new_with_label (_("Register"));
347 gtk_widget_show (reg_btn);
348 gtk_box_pack_start (GTK_BOX (btn_hbox), reg_btn, FALSE, TRUE, 0);
349 gtk_signal_connect (GTK_OBJECT (reg_btn), "clicked",
350 GTK_SIGNAL_FUNC (prefs_actions_register_cb), NULL);
352 subst_btn = gtk_button_new_with_label (_(" Substitute "));
353 gtk_widget_show (subst_btn);
354 gtk_box_pack_start (GTK_BOX (btn_hbox), subst_btn, FALSE, TRUE, 0);
355 gtk_signal_connect (GTK_OBJECT (subst_btn), "clicked",
356 GTK_SIGNAL_FUNC (prefs_actions_substitute_cb),
359 del_btn = gtk_button_new_with_label (_("Delete"));
360 gtk_widget_show (del_btn);
361 gtk_box_pack_start (GTK_BOX (btn_hbox), del_btn, FALSE, TRUE, 0);
362 gtk_signal_connect (GTK_OBJECT (del_btn), "clicked",
363 GTK_SIGNAL_FUNC (prefs_actions_delete_cb), NULL);
365 help_toggle = gtk_toggle_button_new_with_label (_("Help on syntax"));
366 gtk_widget_show (help_toggle);
367 gtk_box_pack_end (GTK_BOX (btn_hbox), help_toggle, FALSE, TRUE, 0);
368 gtk_signal_connect (GTK_OBJECT (help_toggle), "toggled",
369 GTK_SIGNAL_FUNC (prefs_actions_help_cb), help_vbox);
371 cond_hbox = gtk_hbox_new (FALSE, 8);
372 gtk_widget_show (cond_hbox);
373 gtk_box_pack_start (GTK_BOX (vbox1), cond_hbox, TRUE, TRUE, 0);
375 cond_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
376 gtk_widget_show (cond_scrolledwin);
377 gtk_widget_set_usize (cond_scrolledwin, -1, 150);
378 gtk_box_pack_start (GTK_BOX (cond_hbox), cond_scrolledwin,
380 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (cond_scrolledwin),
381 GTK_POLICY_AUTOMATIC,
382 GTK_POLICY_AUTOMATIC);
384 title[0] = _("Registered actions");
385 cond_clist = gtk_clist_new_with_titles(1, title);
386 gtk_widget_show (cond_clist);
387 gtk_container_add (GTK_CONTAINER (cond_scrolledwin), cond_clist);
388 gtk_clist_set_column_width (GTK_CLIST (cond_clist), 0, 80);
389 gtk_clist_set_selection_mode (GTK_CLIST (cond_clist),
390 GTK_SELECTION_BROWSE);
391 GTK_WIDGET_UNSET_FLAGS (GTK_CLIST (cond_clist)->column[0].button,
393 gtk_signal_connect (GTK_OBJECT (cond_clist), "select_row",
394 GTK_SIGNAL_FUNC (prefs_actions_select), NULL);
395 gtk_signal_connect_after (GTK_OBJECT (cond_clist), "row_move",
396 GTK_SIGNAL_FUNC (prefs_actions_row_move),
399 btn_vbox = gtk_vbox_new (FALSE, 8);
400 gtk_widget_show (btn_vbox);
401 gtk_box_pack_start (GTK_BOX (cond_hbox), btn_vbox, FALSE, FALSE, 0);
403 up_btn = gtk_button_new_with_label (_("Up"));
404 gtk_widget_show (up_btn);
405 gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
406 gtk_signal_connect (GTK_OBJECT (up_btn), "clicked",
407 GTK_SIGNAL_FUNC (prefs_actions_up), NULL);
409 down_btn = gtk_button_new_with_label (_("Down"));
410 gtk_widget_show (down_btn);
411 gtk_box_pack_start (GTK_BOX (btn_vbox), down_btn, FALSE, FALSE, 0);
412 gtk_signal_connect (GTK_OBJECT (down_btn), "clicked",
413 GTK_SIGNAL_FUNC (prefs_actions_down), NULL);
415 gtk_widget_show(window);
417 actions.window = window;
418 actions.ok_btn = ok_btn;
420 actions.name_entry = name_entry;
421 actions.cmd_entry = cmd_entry;
423 actions.actions_clist = cond_clist;
426 static void prefs_actions_help_cb(GtkWidget *w, gpointer data)
428 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)))
429 gtk_widget_show(GTK_WIDGET(data));
431 gtk_widget_hide(GTK_WIDGET(data));
434 void prefs_actions_read_config()
438 gchar buf[PREFSBUFSIZE];
441 debug_print(_("Reading actions configurations...\n"));
443 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
444 if ((fp = fopen(rcpath, "rb")) == NULL) {
445 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
451 while (prefs_common.actionslst != NULL) {
452 act = (gchar *) prefs_common.actionslst->data;
453 prefs_common.actionslst = g_slist_remove(
454 prefs_common.actionslst,
459 while (fgets(buf, sizeof(buf), fp) != NULL) {
461 act = strstr(buf, ": ");
463 get_action_type(&act[2]) != ACTION_ERROR)
464 prefs_common.actionslst = g_slist_append(
465 prefs_common.actionslst,
471 void prefs_actions_write_config()
477 debug_print(_("Writing actions configuration...\n"));
479 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
480 if ((pfile= prefs_write_open(rcpath)) == NULL) {
481 g_warning(_("failed to write configuration to file\n"));
486 for (cur = prefs_common.actionslst; cur != NULL; cur = cur->next) {
487 gchar *act = (gchar *) cur->data;
488 if (fputs(act, pfile->fp) == EOF ||
489 fputc('\n', pfile->fp) == EOF) {
490 FILE_OP_ERROR(rcpath, "fputs || fputc");
491 prefs_write_close_revert(pfile);
499 if (prefs_write_close(pfile) < 0) {
500 g_warning(_("failed to write configuration to file\n"));
505 static guint get_action_type(gchar *action)
508 guint action_type = ACTION_NONE;
510 g_return_val_if_fail(action, ACTION_ERROR);
511 g_return_val_if_fail(*action, ACTION_ERROR);
516 action_type |= ACTION_PIPE_IN;
518 } else if (p[0] == '>') {
519 action_type |= ACTION_OPEN_IN;
521 } else if (p[0] == '*') {
522 action_type |= ACTION_HIDE_IN;
529 while (*p && action_type != ACTION_ERROR) {
532 case 'f': action_type |= ACTION_SINGLE;
534 case 'F': action_type |= ACTION_MULTIPLE;
536 default: action_type = ACTION_ERROR;
539 } else if (p[0] == '|') {
541 action_type |= ACTION_PIPE_OUT;
543 action_type = ACTION_ERROR;
544 } else if (p[0] == '&') {
546 action_type |= ACTION_ASYNC;
548 action_type = ACTION_ERROR;
556 static gchar *parse_action_cmd (gchar *action,
567 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
570 cmd = g_string_sized_new(strlen(action));
572 while (p[0] && p[0] != '|' && p[0] != '&') {
573 if (p[0] == '%' && p[1]) {
575 case 'f': cmd = parse_append_filename(cmd, msginfo);
579 for (cur =GTK_CLIST(ctree)->selection;
580 cur; cur = cur->next) {
581 msg = gtk_ctree_node_get_row_data(ctree,
582 GTK_CTREE_NODE(cur->data));
583 cmd = parse_append_filename(cmd, msg);
584 cmd = g_string_append_c(cmd, ' ');
588 default: cmd = g_string_append_c(cmd, p[0]);
589 cmd = g_string_append_c(cmd, p[1]);
593 cmd = g_string_append_c(cmd, p[0]);
598 g_string_free(cmd, TRUE);
603 g_string_free(cmd, FALSE);
607 static GString *parse_append_filename(GString *cmd, MsgInfo *msginfo)
611 g_return_val_if_fail(msginfo, cmd);
613 filename = procmsg_get_message_file(msginfo);
616 cmd = g_string_append(cmd, filename);
623 static void prefs_actions_set_dialog (void)
625 GtkCList *clist = GTK_CLIST(actions.actions_clist);
627 gchar *action_str[1];
630 gtk_clist_freeze(clist);
631 gtk_clist_clear(clist);
633 action_str[0] = _("(New)");
634 row = gtk_clist_append(clist, action_str);
635 gtk_clist_set_row_data(clist, row, NULL);
637 for (cur = prefs_common.actionslst; cur != NULL; cur = cur->next) {
639 action[0] = (gchar *) cur->data;
641 row = gtk_clist_append(clist, action);
642 gtk_clist_set_row_data(clist, row, action[0]);
645 gtk_clist_thaw(clist);
647 static void prefs_actions_set_list(void)
652 g_slist_free(prefs_common.actionslst);
653 prefs_common.actionslst = NULL;
655 while ( (action = (gchar *) gtk_clist_get_row_data(GTK_CLIST(
656 actions.actions_clist), row))
658 prefs_common.actionslst = g_slist_append(prefs_common.actionslst,
665 #define GET_ENTRY(entry) \
666 entry_text = gtk_entry_get_text(GTK_ENTRY(entry))
667 static gint prefs_actions_clist_set_row (gint row)
669 GtkCList *clist = GTK_CLIST(actions.actions_clist);
672 gchar action[PREFSBUFSIZE];
676 g_return_val_if_fail(row != 0, -1);
680 GET_ENTRY(actions.name_entry);
681 if (entry_text[0] == '\0') {
682 alertpanel_error(_("Menu name is not set."));
686 if (strchr(entry_text, ':')) {
687 alertpanel_error(_("Colon ':' is not allowed in the menu name."));
691 strncpy(action, entry_text, PREFSBUFSIZE - 1);
694 /* Keep space for the ': ' delimiter */
695 len = strlen(action) + 2;
696 if (len >= PREFSBUFSIZE - 1) {
697 alertpanel_error(_("Menu name is too long."));
701 strcat(action, ": ");
703 GET_ENTRY(actions.cmd_entry);
705 if (entry_text[0] == '\0') {
706 alertpanel_error(_("Command line not set."));
710 if (len + strlen(entry_text) >= PREFSBUFSIZE - 1) {
711 alertpanel_error(_("Menu name and command are too long."));
715 if (get_action_type(entry_text) == ACTION_ERROR) {
716 alertpanel_error(_("The command\n%s\nhas a syntax error."),
721 strcat(action, entry_text);
725 row = gtk_clist_append(clist, buf);
728 gtk_clist_set_text(clist, row, 0, action);
729 old_action = (gchar *) gtk_clist_get_row_data(clist, row);
734 buf[0] = g_strdup(action);
736 gtk_clist_set_row_data(clist, row, buf[0]);
738 prefs_actions_set_list();
743 /* callback functions */
744 static void prefs_actions_register_cb (GtkWidget *w, gpointer data)
746 prefs_actions_clist_set_row(-1);
749 static void prefs_actions_substitute_cb (GtkWidget *w, gpointer data)
751 GtkCList *clist = GTK_CLIST(actions.actions_clist);
755 if (!clist->selection) return;
757 row = GPOINTER_TO_INT(clist->selection->data);
758 if (row == 0) return;
760 action = gtk_clist_get_row_data(clist, row);
763 prefs_actions_clist_set_row(row);
766 static void prefs_actions_delete_cb (GtkWidget *w, gpointer data)
768 GtkCList *clist = GTK_CLIST(actions.actions_clist);
772 if (!clist->selection) return;
773 row = GPOINTER_TO_INT(clist->selection->data);
774 if (row == 0) return;
776 if (alertpanel(_("Delete action"),
777 _("Do you really want to delete this action?"),
778 _("Yes"), _("No"), NULL) == G_ALERTALTERNATE)
781 action = gtk_clist_get_row_data(clist, row);
783 gtk_clist_remove(clist, row);
784 prefs_common.actionslst = g_slist_remove(prefs_common.actionslst, action);
787 static void prefs_actions_up (GtkWidget *w, gpointer data)
789 GtkCList *clist = GTK_CLIST(actions.actions_clist);
792 if (!clist->selection) return;
794 row = GPOINTER_TO_INT(clist->selection->data);
796 gtk_clist_row_move(clist, row, row - 1);
799 static void prefs_actions_down (GtkWidget *w, gpointer data)
801 GtkCList *clist = GTK_CLIST(actions.actions_clist);
804 if (!clist->selection) return;
806 row = GPOINTER_TO_INT(clist->selection->data);
807 if (row > 0 && row < clist->rows - 1)
808 gtk_clist_row_move(clist, row, row + 1);
811 #define ENTRY_SET_TEXT(entry, str) \
812 gtk_entry_set_text(GTK_ENTRY(entry), str ? str : "")
813 static void prefs_actions_select (GtkCList *clist,
820 gchar buf[PREFSBUFSIZE];
821 action = gtk_clist_get_row_data(clist, row);
824 ENTRY_SET_TEXT(actions.name_entry, "");
825 ENTRY_SET_TEXT(actions.cmd_entry, "");
829 strncpy(buf, action, PREFSBUFSIZE - 1);
830 buf[PREFSBUFSIZE - 1] = 0x00;
831 cmd = strstr2(buf, ": ");
834 ENTRY_SET_TEXT(actions.cmd_entry, &cmd[2]);
839 ENTRY_SET_TEXT(actions.name_entry, buf);
842 static void prefs_actions_row_move (GtkCList *clist,
846 prefs_actions_set_list();
847 if (gtk_clist_row_is_visible(clist, dest_row) != GTK_VISIBILITY_FULL) {
848 gtk_clist_moveto(clist, dest_row, -1,
849 source_row < dest_row ? 1.0 : 0.0, 0.0);
853 static gint prefs_actions_deleted (GtkWidget *widget,
857 prefs_actions_cancel(widget, data);
861 static void prefs_actions_key_pressed (GtkWidget *widget,
865 if (event && event->keyval == GDK_Escape)
866 prefs_actions_cancel(widget, data);
869 static void prefs_actions_cancel (GtkWidget *w, gpointer data)
871 prefs_actions_read_config();
872 gtk_widget_hide(actions.window);
876 static void prefs_actions_ok (GtkWidget *widget, gpointer data)
878 GtkItemFactory *ifactory;
879 MainWindow *mainwin = (MainWindow *) data;
881 prefs_actions_write_config();
882 ifactory = gtk_item_factory_from_widget(mainwin->menubar);
883 update_mainwin_actions_menu(ifactory, mainwin);
884 gtk_widget_hide(actions.window);
888 void update_mainwin_actions_menu(GtkItemFactory *ifactory,
891 update_actions_menu(ifactory, "/Edit/Actions",
892 mainwin_actions_execute_cb,
896 void update_compose_actions_menu(GtkItemFactory *ifactory,
900 update_actions_menu(ifactory, branch_path,
901 compose_actions_execute_cb,
905 static void update_actions_menu(GtkItemFactory *ifactory,
913 gchar *action, *action_p;
916 GtkItemFactoryEntry ifentry = {
917 branch_path, NULL, NULL, 0, "<Branch>"};
918 menuitem = gtk_item_factory_get_item(ifactory, branch_path);
919 /* FIXME: is there a better way to remove unknown submenu items? */
920 /* Deleting and recreating the parent looses the translation */
921 menu = GTK_WIDGET(GTK_MENU_ITEM(menuitem)->submenu);
922 if (GTK_MENU_SHELL(menu)->children) {
923 GList *amenu, *alist;
924 for (amenu = (GTK_MENU_SHELL(menu)->children); amenu; ) {
926 gtk_widget_destroy(GTK_WIDGET(amenu->data));
931 ifentry.accelerator = NULL;
932 ifentry.callback_action = 0;
933 ifentry.callback = callback;
934 ifentry.item_type = NULL;
936 for (cur = prefs_common.actionslst; cur; cur = cur->next) {
937 action = g_strdup((gchar *) cur->data);
938 action_p = strstr(action, ": ");
939 if (action_p && action_p[2] &&
940 get_action_type(&action_p[2]) != ACTION_ERROR) {
942 menu_path = g_strdup_printf("%s/%s", branch_path,
944 ifentry.path = menu_path;
945 gtk_item_factory_create_item(ifactory, &ifentry, data,
950 ifentry.callback_action++;
954 static void compose_actions_execute_cb (Compose *compose,
961 g_return_if_fail(action_nb < g_slist_length(prefs_common.actionslst));
963 buf = (gchar *) g_slist_nth_data(prefs_common.actionslst, action_nb);
965 g_return_if_fail(buf);
966 g_return_if_fail(action = strstr(buf, ": "));
968 /* Point to the beginning of the command-line */
972 action_type = get_action_type(action);
973 if (action_type & ACTION_MULTIPLE ||
974 action_type & ACTION_SINGLE ) {
975 alertpanel_warning(_("The selected action is not a pipe "
976 "action.\n You can only use pipe actions "
977 "when composing a message."));
981 execute_actions(action, compose->window, NULL, compose->text);
984 static void mainwin_actions_execute_cb (MainWindow *mainwin,
988 MessageView *messageview = mainwin->messageview;
989 GtkWidget *text = NULL;
993 g_return_if_fail (action_nb < g_slist_length(prefs_common.actionslst));
995 buf = (gchar *) g_slist_nth_data(prefs_common.actionslst, action_nb);
997 g_return_if_fail(buf);
998 g_return_if_fail(action = strstr(buf, ": "));
1000 /* Point to the beginning of the command-line */
1004 switch (messageview->type) {
1006 if (messageview->textview &&
1007 messageview->textview->text)
1008 text = messageview->textview->text;
1011 if (messageview->mimeview &&
1012 messageview->mimeview->type == MIMEVIEW_TEXT &&
1013 messageview->mimeview->textview &&
1014 messageview->mimeview->textview->text)
1015 text = messageview->mimeview->textview->text;
1019 execute_actions(action, mainwin->window,
1020 GTK_CTREE(mainwin->summaryview->ctree), text);
1023 static gboolean execute_actions(gchar *action,
1028 GList *cur, *selection;
1029 GSList *children_list = NULL;
1033 ChildInfo *child_info;
1038 g_return_val_if_fail(action && *action, FALSE);
1040 action_type = get_action_type(action);
1042 if (action_type == ACTION_ERROR)
1043 return FALSE; /* ERR: syntax error */
1045 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) &&
1046 !(ctree && GTK_CLIST(ctree)->selection))
1047 return FALSE; /* ERR: file command without selection */
1050 selection = GTK_CLIST(ctree)->selection;
1051 selection_len = g_list_length(selection);
1054 if ((action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN))) {
1055 if (ctree && selection_len > 1)
1056 return FALSE; /* ERR: pipe + multiple selection */
1058 return FALSE; /* ERR: pipe and no displayed text */
1061 if (!(action_type & ACTION_ASYNC) && window) {
1062 gtk_widget_set_sensitive(window, FALSE);
1065 children = g_new0(Children, 1);
1067 if (action_type & ACTION_SINGLE) {
1068 for (cur = selection; cur && is_ok == TRUE; cur = cur->next) {
1069 msginfo = gtk_ctree_node_get_row_data(ctree,
1070 GTK_CTREE_NODE(cur->data));
1072 is_ok = FALSE; /* ERR: msginfo missing */
1075 cmd = parse_action_cmd(action, msginfo, ctree);
1077 debug_print(_("Action command error\n"));
1078 is_ok = FALSE; /* ERR: incorrect command */
1081 if ((child_info = fork_child(cmd, action_type, text,
1083 children_list = g_slist_append(children_list,
1085 children->open_in = (selection_len == 1) ?
1088 ACTION_HIDE_IN)) : 0;
1093 cmd = parse_action_cmd(action, NULL, ctree);
1095 if ((child_info = fork_child(cmd, action_type, text,
1097 children_list = g_slist_append(children_list,
1099 children->open_in = action_type &
1105 is_ok = FALSE; /* ERR: incorrect command */
1108 if (!children_list) {
1109 /* If not waiting for children, return */
1111 if (!(action_type & ACTION_ASYNC) && window) {
1112 gtk_widget_set_sensitive(window, TRUE);
1117 children->action = g_strdup(action);
1118 children->window = window;
1119 children->dialog = NULL;
1120 children->list = children_list;
1121 children->nb = g_slist_length(children_list);
1123 for (cur = children_list; cur; cur = cur->next) {
1124 child_info = (ChildInfo *) cur->data;
1125 child_info->tag_status =
1126 gdk_input_add(child_info->chld_status,
1128 catch_status, child_info);
1130 children->timer = children->open_in ? 0 :
1131 gtk_timeout_add(WAIT_LAP, wait_for_children,
1134 if (children->open_in)
1135 create_io_dialog(children);
1140 ChildInfo *fork_child(gchar *cmd,
1145 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
1147 gint start, end, is_selection;
1149 pid_t pid_c, pid_gc;
1150 ChildInfo *child_info;
1152 if (action_type & ACTION_ASYNC) {
1153 execute_command_line(cmd, TRUE);
1154 return NULL; /* Asynchronous command */
1157 if (pipe(chld_in) || pipe(chld_out) || pipe(chld_err) ||
1158 pipe(chld_status)) {
1159 alertpanel_error(_("Command could not started. Pipe creation"
1160 " failed.\n%s"), g_strerror(errno));
1161 return NULL; /* Pipe error */
1164 debug_print(_("Forking child and granchild.\n"));
1167 if (pid_c == (pid_t) 0) {/* Child */
1171 close(ConnectionNumber(gdk_display));
1176 if (setpgid(0, getppid()))
1179 (ACTION_PIPE_IN | ACTION_OPEN_IN | ACTION_HIDE_IN)) {
1180 close(fileno(stdin));
1186 close(fileno(stdout));
1191 close(fileno(stderr));
1196 cmdline = strsplit_with_quote(cmd, " ", 1024);
1198 execvp(cmdline[0], cmdline);
1200 g_strfreev(cmdline);
1203 } else if (pid_gc < (pid_t) 0) {/* Fork erro */
1204 write(chld_status[1], "1\n", 2);
1215 close(chld_status[0]);
1216 debug_print(_("Child: Waiting for grandchild\n"));
1217 waitpid(pid_gc, NULL, 0);
1218 debug_print(_("Child: grandchild ended\n"));
1219 write(chld_status[1], "0\n", 2);
1220 close(chld_status[1]);
1223 } else if (pid_c < (pid_t) 0) {/* Fork error */
1224 alertpanel_error(_("Could not fork to execute the following "
1225 "command:\n%s\n%s"),
1226 cmd, g_strerror(errno));
1232 if (!(action_type & (ACTION_PIPE_IN | ACTION_OPEN_IN | ACTION_HIDE_IN)))
1236 close(chld_status[1]);
1238 child_info = g_new0(ChildInfo, 1);
1240 child_info->children = children;
1242 child_info->pid = pid_c;
1243 child_info->cmd = g_strdup(cmd);
1244 child_info->type = action_type;
1245 child_info->new_out = FALSE;
1246 child_info->output = g_string_sized_new(0);
1247 child_info->chld_in = (action_type & (ACTION_PIPE_IN | ACTION_OPEN_IN
1250 child_info->chld_out = chld_out[0];
1251 child_info->chld_err = chld_err[0];
1252 child_info->chld_status = chld_status[0];
1253 child_info->tag_in = -1;
1254 child_info->tag_out = gdk_input_add(chld_out[0], GDK_INPUT_READ,
1255 catch_output, child_info);
1256 child_info->tag_err = gdk_input_add(chld_err[0], GDK_INPUT_READ,
1257 catch_output, child_info);
1259 if (!(action_type & (ACTION_PIPE_IN | ACTION_PIPE_OUT)))
1262 child_info->text = text;
1265 end = gtk_stext_get_length(GTK_STEXT(text));
1267 if (GTK_EDITABLE(text)->has_selection) {
1268 start = GTK_EDITABLE(text)->selection_start_pos;
1269 end = GTK_EDITABLE(text)->selection_end_pos;
1276 is_selection = TRUE;
1279 end = gtk_stext_get_length(GTK_STEXT(text));
1280 is_selection = FALSE;
1284 selection = gtk_editable_get_chars(GTK_EDITABLE(text), start, end);
1287 if (action_type & ACTION_PIPE_IN) {
1288 write(chld_in[1], selection, strlen(selection));
1289 if (!(action_type & (ACTION_OPEN_IN | ACTION_HIDE_IN)))
1291 child_info->chld_in = -1; /* No more input */
1295 gtk_stext_freeze(GTK_STEXT(text));
1296 if (action_type & ACTION_PIPE_OUT) {
1297 gtk_stext_set_point(GTK_STEXT(text), start);
1298 gtk_stext_forward_delete(GTK_STEXT(text), end - start);
1301 gtk_stext_thaw(GTK_STEXT(text));
1306 static void kill_children_cb(GtkWidget *widget, gpointer data)
1309 Children *children = (Children *) data;
1310 ChildInfo *child_info;
1312 for (cur = children->list; cur; cur = cur->next) {
1313 child_info = (ChildInfo *)(cur->data);
1314 debug_print(_("Killing child group id %d\n"), child_info->pid);
1315 if (child_info->pid && (kill(-child_info->pid, SIGTERM) < 0))
1320 static gint wait_for_children(gpointer data)
1322 gboolean new_output;
1323 Children *children = (Children *) data;
1324 ChildInfo *child_info;
1326 gint nb = children->nb;
1330 cur = children->list;
1333 child_info = (ChildInfo *) cur->data;
1334 if (child_info->pid)
1336 new_output |= child_info->new_out;
1340 if (!children->dialog &&
1341 (new_output || children->timer))
1342 create_io_dialog(children);
1344 if (children->timer) {
1345 gtk_timeout_remove(children->timer);
1346 children->timer = 0;
1348 children->output |= new_output;
1350 if (new_output || (children->dialog && (nb != children->nb)))
1351 update_io_dialog(children);
1356 if (!children->dialog) {
1357 gtk_widget_set_sensitive(children->window, TRUE);
1358 free_children(children);
1359 } else if (!children->output) {
1360 gtk_widget_destroy(children->dialog);
1365 static void send_input(GtkWidget *w, gpointer data)
1367 Children *children = (Children *) data;
1368 ChildInfo *child_info = (ChildInfo *) children->list->data;
1370 child_info->tag_in = gdk_input_add(child_info->chld_in,
1372 catch_input, children);
1373 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1376 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1378 hide_io_dialog_cb(w, data);
1382 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1385 Children *children = (Children *) data;
1387 if (!children->nb) {
1388 gtk_widget_set_sensitive(children->window, TRUE);
1389 gtk_signal_disconnect_by_data(GTK_OBJECT(children->dialog),
1391 gtk_widget_destroy(children->dialog);
1392 free_children(children);
1396 static void childinfo_close_pipes(ChildInfo *child_info)
1398 if (child_info->tag_in > 0)
1399 gdk_input_remove(child_info->tag_in);
1400 gdk_input_remove(child_info->tag_out);
1401 gdk_input_remove(child_info->tag_err);
1403 if (child_info->chld_in >= 0)
1404 close(child_info->chld_in);
1405 close(child_info->chld_out);
1406 close(child_info->chld_err);
1407 close(child_info->chld_status);
1410 static void free_children(Children *children)
1413 ChildInfo *child_info;
1415 debug_print(_("Freeing children data %x\n"), (guint) children);
1417 g_free(children->action);
1418 for (cur = children->list; cur;) {
1419 child_info = (ChildInfo *) cur->data;
1420 g_free(child_info->cmd);
1421 g_string_free(child_info->output, TRUE);
1422 children->list = g_slist_remove(children->list, child_info);
1424 cur = children->list;
1429 static void update_io_dialog(Children *children)
1433 debug_print(_("Updating actions input/output dialog.\n"));
1435 if (!children->nb) {
1436 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1437 gtk_widget_set_sensitive(children->hide_btn, TRUE);
1438 if (children->input_hbox)
1439 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1442 if (children->output) {
1443 GtkWidget *text = children->text;
1445 ChildInfo *child_info;
1447 gtk_widget_show(children->scrolledwin);
1448 gtk_text_freeze(GTK_TEXT(text));
1449 gtk_text_set_point(GTK_TEXT(text), 0);
1450 gtk_text_forward_delete(GTK_TEXT(text),
1451 gtk_text_get_length(GTK_TEXT(text)));
1452 for (cur = children->list; cur; cur = cur->next)
1454 child_info = (ChildInfo *) cur->data;
1455 if (child_info->pid)
1456 caption = g_strdup_printf(_("--- Running: %s\n")
1459 caption = g_strdup_printf(_("--- Ended: %s\n")
1462 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1464 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1465 child_info->output->str, -1);
1467 child_info->new_out = FALSE;
1469 gtk_text_thaw(GTK_TEXT(text));
1474 static void create_io_dialog(Children *children)
1478 GtkWidget *input_hbox;
1479 GtkWidget *send_button;
1482 GtkWidget *scrolledwin;
1484 GtkWidget *abort_button;
1485 GtkWidget *hide_button;
1487 debug_print(_("Creating actions dialog\n"));
1489 dialog = gtk_dialog_new();
1490 label = gtk_label_new(children->action);
1491 gtk_misc_set_padding(GTK_MISC(label), 8, 8);
1492 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, FALSE,
1495 gtk_window_set_title(GTK_WINDOW(dialog), _("Actions' input/output"));
1496 gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
1497 GTK_SIGNAL_FUNC(delete_io_dialog_cb), children);
1498 gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
1499 GTK_SIGNAL_FUNC(hide_io_dialog_cb),
1502 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1503 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1504 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
1505 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), scrolledwin, TRUE,
1507 gtk_widget_set_usize(scrolledwin, 600, 200);
1508 gtk_widget_hide(scrolledwin);
1510 text = gtk_text_new(gtk_scrolled_window_get_hadjustment
1511 (GTK_SCROLLED_WINDOW(scrolledwin)),
1512 gtk_scrolled_window_get_vadjustment
1513 (GTK_SCROLLED_WINDOW(scrolledwin)));
1514 gtk_text_set_editable(GTK_TEXT(text), FALSE);
1515 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1516 gtk_widget_show(text);
1518 if (children->open_in) {
1519 input_hbox = gtk_hbox_new(FALSE, 8);
1520 gtk_widget_show(input_hbox);
1522 entry = gtk_entry_new();
1523 gtk_signal_connect(GTK_OBJECT(entry), "activate",
1524 GTK_SIGNAL_FUNC(send_input), children);
1525 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 8);
1526 if (children->open_in & ACTION_HIDE_IN)
1527 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1528 gtk_widget_show(entry);
1530 send_button = gtk_button_new_with_label(_("Send"));
1531 gtk_signal_connect(GTK_OBJECT(send_button), "clicked",
1532 GTK_SIGNAL_FUNC(send_input), children);
1533 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1535 gtk_widget_show(send_button);
1537 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1538 input_hbox, FALSE, FALSE, 8);
1539 gtk_widget_grab_focus(entry);
1542 hbox = gtk_hbox_new(TRUE, 0);
1543 gtk_widget_show(hbox);
1545 abort_button = gtk_button_new_with_label(_("Abort actions"));
1546 gtk_signal_connect(GTK_OBJECT(abort_button), "clicked",
1547 GTK_SIGNAL_FUNC(kill_children_cb), children);
1548 gtk_box_pack_start(GTK_BOX(hbox), abort_button, TRUE, TRUE, 8);
1549 gtk_widget_show(abort_button);
1551 hide_button = gtk_button_new_with_label(_("Close window"));
1552 gtk_signal_connect(GTK_OBJECT(hide_button), "clicked",
1553 GTK_SIGNAL_FUNC(hide_io_dialog_cb), children);
1554 gtk_box_pack_start(GTK_BOX(hbox), hide_button, TRUE, TRUE, 8);
1555 gtk_widget_show(hide_button);
1558 gtk_widget_set_sensitive(hide_button, FALSE);
1560 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
1563 children->dialog = dialog;
1564 children->scrolledwin = scrolledwin;
1565 children->text = text;
1566 children->input_hbox = children->open_in ? input_hbox : NULL;
1567 children->input_entry = children->open_in ? entry : NULL;
1568 children->abort_btn = abort_button;
1569 children->hide_btn = hide_button;
1571 gtk_widget_show(dialog);
1574 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1576 ChildInfo *child_info = (ChildInfo *) data;
1580 gdk_input_remove(child_info->tag_status);
1582 c = read(source, &buf, 1);
1583 debug_print(_("Child returned %c\n"), buf);
1585 waitpid(-child_info->pid, NULL, 0);
1586 childinfo_close_pipes(child_info);
1587 child_info->pid = 0;
1589 wait_for_children(child_info->children);
1592 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1594 Children *children = (Children *) data;
1595 ChildInfo *child_info = (ChildInfo *) children->list->data;
1599 debug_print(_("Sending input to grand child.\n"));
1600 if (!(cond && GDK_INPUT_WRITE))
1603 gdk_input_remove(child_info->tag_in);
1604 child_info->tag_in = -1;
1606 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry), 0,
1608 c = write(child_info->chld_in, input, strlen(input));
1612 write(child_info->chld_in, "\n", 2);
1614 gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1615 gtk_widget_set_sensitive(children->input_hbox, TRUE);
1616 debug_print(_("Input to grand child sent.\n"));
1619 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1621 ChildInfo *child_info = (ChildInfo *) data;
1623 gchar buf[PREFSBUFSIZE];
1625 debug_print(_("Catching grand child's output.\n"));
1626 if (child_info->type & ACTION_PIPE_OUT
1627 && source == child_info->chld_out) {
1628 gboolean is_selection = FALSE;
1629 GtkWidget *text = child_info->text;
1630 if (GTK_EDITABLE(text)->has_selection)
1631 is_selection = TRUE;
1632 gtk_stext_freeze(GTK_STEXT(text));
1634 c =read(source, buf, PREFSBUFSIZE - 1);
1637 gtk_stext_insert(GTK_STEXT(text), NULL, NULL, NULL,
1641 /* Using the select_region draws things. Should not.
1642 * so we just change selection position and
1643 * defere drawing when thawing. Hack?
1645 GTK_EDITABLE(text)->selection_end_pos =
1646 gtk_stext_get_point(GTK_STEXT(text));
1648 gtk_stext_thaw(GTK_STEXT(child_info->text));
1651 c = read(source, buf, PREFSBUFSIZE - 1);
1654 for (i = 0; i < c; i++)
1655 child_info->output = g_string_append_c(
1656 child_info->output, buf[i]);
1657 child_info->new_out = TRUE;