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
58 ACTION_PIPE_IN = 1 << 1,
59 ACTION_PIPE_OUT = 1 << 2,
60 ACTION_SINGLE = 1 << 3,
61 ACTION_MULTIPLE = 1 << 4,
62 ACTION_ASYNC = 1 << 5,
63 ACTION_OPEN_IN = 1 << 6,
64 ACTION_HIDE_IN = 1 << 7,
65 ACTION_ERROR = 1 << 8,
68 static struct Actions {
73 GtkWidget *name_entry;
76 GtkWidget *actions_clist;
79 typedef struct _Children Children;
84 GtkWidget *input_entry;
85 GtkWidget *input_hbox;
88 GtkWidget *scrolledwin;
98 typedef struct _ChildInfo ChildInfo;
117 /* widget creating functions */
118 static void prefs_actions_create (MainWindow *mainwin);
119 static void prefs_actions_set_dialog (void);
120 static gint prefs_actions_clist_set_row (gint row);
122 /* callback functions */
123 static void prefs_actions_help_cb (GtkWidget *w, gpointer data);
124 static void prefs_actions_register_cb (GtkWidget *w, gpointer data);
125 static void prefs_actions_substitute_cb (GtkWidget *w, gpointer data);
126 static void prefs_actions_delete_cb (GtkWidget *w, gpointer data);
127 static void prefs_actions_up (GtkWidget *w, gpointer data);
128 static void prefs_actions_down (GtkWidget *w, gpointer data);
129 static void prefs_actions_select (GtkCList *clist,
133 static void prefs_actions_row_move (GtkCList *clist,
136 static gint prefs_actions_deleted (GtkWidget *widget,
139 static void prefs_actions_key_pressed (GtkWidget *widget,
142 static void prefs_actions_cancel (GtkWidget *w, gpointer data);
143 static void prefs_actions_ok (GtkWidget *w, gpointer data);
144 static void update_actions_menu (GtkItemFactory *ifactory,
148 static void mainwin_actions_execute_cb (MainWindow *mainwin,
151 static void compose_actions_execute_cb (Compose *compose,
154 static guint get_action_type (gchar *action);
156 static gboolean execute_actions (gchar *action,
162 static gchar *parse_action_cmd (gchar *action,
166 static GString *parse_append_filename (GString *cmd,
169 static GString *parse_append_msgpart (GString *cmd,
173 ChildInfo *fork_child (gchar *cmd,
178 static gint wait_for_children (gpointer data);
180 static void free_children (Children *children);
182 static void childinfo_close_pipes (ChildInfo *child_info);
184 static void create_io_dialog (Children *children);
186 static void update_io_dialog (Children *children);
188 static void hide_io_dialog_cb (GtkWidget *widget,
191 static void catch_output (gpointer data,
193 GdkInputCondition cond);
194 static void catch_input (gpointer data,
196 GdkInputCondition cond);
197 static void catch_status (gpointer data,
199 GdkInputCondition cond);
201 void prefs_actions_open(MainWindow *mainwin)
204 if (prefs_rc_is_readonly(ACTIONS_RC))
210 prefs_actions_create(mainwin);
212 manage_window_set_transient(GTK_WINDOW(actions.window));
213 gtk_widget_grab_focus(actions.ok_btn);
215 prefs_actions_set_dialog();
217 gtk_widget_show(actions.window);
220 static void prefs_actions_create(MainWindow *mainwin)
225 GtkWidget *cancel_btn;
226 GtkWidget *confirm_area;
230 GtkWidget *name_label;
231 GtkWidget *name_entry;
232 GtkWidget *cmd_label;
233 GtkWidget *cmd_entry;
239 GtkWidget *subst_btn;
242 GtkWidget *cond_hbox;
243 GtkWidget *cond_scrolledwin;
244 GtkWidget *cond_clist;
246 GtkWidget *help_vbox;
247 GtkWidget *help_label;
248 GtkWidget *help_toggle;
256 debug_print(_("Creating actions setting window...\n"));
258 window = gtk_window_new (GTK_WINDOW_DIALOG);
260 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
261 gtk_window_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
262 gtk_window_set_modal (GTK_WINDOW (window), TRUE);
263 gtk_window_set_policy (GTK_WINDOW (window), FALSE, TRUE, TRUE);
265 vbox = gtk_vbox_new (FALSE, 6);
266 gtk_widget_show (vbox);
267 gtk_container_add (GTK_CONTAINER (window), vbox);
269 gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
270 &cancel_btn, _("Cancel"), NULL, NULL);
271 gtk_widget_show (confirm_area);
272 gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
273 gtk_widget_grab_default (ok_btn);
275 gtk_window_set_title (GTK_WINDOW(window),
276 _("Actions setting"));
277 gtk_signal_connect (GTK_OBJECT(window), "delete_event",
278 GTK_SIGNAL_FUNC(prefs_actions_deleted), NULL);
279 gtk_signal_connect (GTK_OBJECT(window), "key_press_event",
280 GTK_SIGNAL_FUNC(prefs_actions_key_pressed), NULL);
281 MANAGE_WINDOW_SIGNALS_CONNECT (window);
282 gtk_signal_connect (GTK_OBJECT(ok_btn), "clicked",
283 GTK_SIGNAL_FUNC(prefs_actions_ok), mainwin);
284 gtk_signal_connect (GTK_OBJECT(cancel_btn), "clicked",
285 GTK_SIGNAL_FUNC(prefs_actions_cancel), NULL);
287 vbox1 = gtk_vbox_new (FALSE, VSPACING);
288 gtk_widget_show (vbox1);
289 gtk_box_pack_start (GTK_BOX (vbox), vbox1, TRUE, TRUE, 0);
290 gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
292 hbox = gtk_hbox_new (FALSE, 0);
293 gtk_box_pack_start (GTK_BOX(vbox1), hbox, TRUE, TRUE, 0);
295 name_label = gtk_label_new(_("Menu name: "));
296 gtk_widget_show(name_label);
297 gtk_box_pack_start (GTK_BOX (hbox), name_label, FALSE, FALSE, 0);
299 name_entry = gtk_entry_new();
300 gtk_widget_show(name_entry);
301 gtk_box_pack_start (GTK_BOX (hbox), name_entry, TRUE, TRUE, 0);
303 gtk_widget_show_all(hbox);
305 hbox = gtk_hbox_new (FALSE, 0);
306 gtk_box_pack_start (GTK_BOX(vbox1), hbox, TRUE, TRUE, 0);
308 cmd_label = gtk_label_new(_("Command line: "));
309 gtk_box_pack_start (GTK_BOX (hbox), cmd_label, FALSE, FALSE, 0);
311 cmd_entry = gtk_entry_new();
312 gtk_box_pack_start (GTK_BOX (hbox), cmd_entry, TRUE, TRUE, 0);
314 gtk_widget_show_all(hbox);
316 help_vbox = gtk_vbox_new(FALSE, 8);
317 gtk_box_pack_start (GTK_BOX (vbox1), help_vbox, TRUE, TRUE, 0);
319 help_label = gtk_label_new (_("Menu name:\n"
320 " Use / in menu name to make submenus.\n"
322 " Begin with:\n | to send message "
324 "to command\n > to send user provided"
325 " text to command\n * to send user "
326 "provided hidden text to command\n"
327 " End with:\n | to replace "
328 "message body or selection with command "
330 "run command asynchronously\n Use %f "
331 "for message file name\n %F for the"
332 " list of the file names of selected "
333 "messages\n %p for the selected message "
335 gtk_misc_set_alignment(GTK_MISC(help_label), 0, 0.5);
336 gtk_label_set_justify (GTK_LABEL(help_label), GTK_JUSTIFY_LEFT);
337 gtk_widget_show (help_label);
338 gtk_box_pack_start (GTK_BOX (help_vbox), help_label, TRUE, TRUE, 0);
339 gtk_widget_hide(help_vbox);
341 /* register / substitute / delete */
343 reg_hbox = gtk_hbox_new (FALSE, 4);
344 gtk_widget_show (reg_hbox);
345 gtk_box_pack_start (GTK_BOX (vbox1), reg_hbox, FALSE, FALSE, 0);
347 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
348 gtk_widget_show (arrow);
349 gtk_box_pack_start (GTK_BOX (reg_hbox), arrow, FALSE, FALSE, 0);
350 gtk_widget_set_usize (arrow, -1, 16);
352 btn_hbox = gtk_hbox_new (FALSE, 4);
353 gtk_widget_show (btn_hbox);
354 gtk_box_pack_start (GTK_BOX (reg_hbox), btn_hbox, FALSE, FALSE, 0);
356 reg_btn = gtk_button_new_with_label (_("Register"));
357 gtk_widget_show (reg_btn);
358 gtk_box_pack_start (GTK_BOX (btn_hbox), reg_btn, FALSE, TRUE, 0);
359 gtk_signal_connect (GTK_OBJECT (reg_btn), "clicked",
360 GTK_SIGNAL_FUNC (prefs_actions_register_cb), NULL);
362 subst_btn = gtk_button_new_with_label (_(" Substitute "));
363 gtk_widget_show (subst_btn);
364 gtk_box_pack_start (GTK_BOX (btn_hbox), subst_btn, FALSE, TRUE, 0);
365 gtk_signal_connect (GTK_OBJECT (subst_btn), "clicked",
366 GTK_SIGNAL_FUNC (prefs_actions_substitute_cb),
369 del_btn = gtk_button_new_with_label (_("Delete"));
370 gtk_widget_show (del_btn);
371 gtk_box_pack_start (GTK_BOX (btn_hbox), del_btn, FALSE, TRUE, 0);
372 gtk_signal_connect (GTK_OBJECT (del_btn), "clicked",
373 GTK_SIGNAL_FUNC (prefs_actions_delete_cb), NULL);
375 help_toggle = gtk_toggle_button_new_with_label (_("Help on syntax"));
376 gtk_widget_show (help_toggle);
377 gtk_box_pack_end (GTK_BOX (btn_hbox), help_toggle, FALSE, TRUE, 0);
378 gtk_signal_connect (GTK_OBJECT (help_toggle), "toggled",
379 GTK_SIGNAL_FUNC (prefs_actions_help_cb), help_vbox);
381 cond_hbox = gtk_hbox_new (FALSE, 8);
382 gtk_widget_show (cond_hbox);
383 gtk_box_pack_start (GTK_BOX (vbox1), cond_hbox, TRUE, TRUE, 0);
385 cond_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
386 gtk_widget_show (cond_scrolledwin);
387 gtk_widget_set_usize (cond_scrolledwin, -1, 150);
388 gtk_box_pack_start (GTK_BOX (cond_hbox), cond_scrolledwin,
390 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (cond_scrolledwin),
391 GTK_POLICY_AUTOMATIC,
392 GTK_POLICY_AUTOMATIC);
394 title[0] = _("Registered actions");
395 cond_clist = gtk_clist_new_with_titles(1, title);
396 gtk_widget_show (cond_clist);
397 gtk_container_add (GTK_CONTAINER (cond_scrolledwin), cond_clist);
398 gtk_clist_set_column_width (GTK_CLIST (cond_clist), 0, 80);
399 gtk_clist_set_selection_mode (GTK_CLIST (cond_clist),
400 GTK_SELECTION_BROWSE);
401 GTK_WIDGET_UNSET_FLAGS (GTK_CLIST (cond_clist)->column[0].button,
403 gtk_signal_connect (GTK_OBJECT (cond_clist), "select_row",
404 GTK_SIGNAL_FUNC (prefs_actions_select), NULL);
405 gtk_signal_connect_after (GTK_OBJECT (cond_clist), "row_move",
406 GTK_SIGNAL_FUNC (prefs_actions_row_move),
409 btn_vbox = gtk_vbox_new (FALSE, 8);
410 gtk_widget_show (btn_vbox);
411 gtk_box_pack_start (GTK_BOX (cond_hbox), btn_vbox, FALSE, FALSE, 0);
413 up_btn = gtk_button_new_with_label (_("Up"));
414 gtk_widget_show (up_btn);
415 gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
416 gtk_signal_connect (GTK_OBJECT (up_btn), "clicked",
417 GTK_SIGNAL_FUNC (prefs_actions_up), NULL);
419 down_btn = gtk_button_new_with_label (_("Down"));
420 gtk_widget_show (down_btn);
421 gtk_box_pack_start (GTK_BOX (btn_vbox), down_btn, FALSE, FALSE, 0);
422 gtk_signal_connect (GTK_OBJECT (down_btn), "clicked",
423 GTK_SIGNAL_FUNC (prefs_actions_down), NULL);
425 gtk_widget_show(window);
427 actions.window = window;
428 actions.ok_btn = ok_btn;
430 actions.name_entry = name_entry;
431 actions.cmd_entry = cmd_entry;
433 actions.actions_clist = cond_clist;
436 static void prefs_actions_help_cb(GtkWidget *w, gpointer data)
438 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)))
439 gtk_widget_show(GTK_WIDGET(data));
441 gtk_widget_hide(GTK_WIDGET(data));
444 void prefs_actions_read_config()
448 gchar buf[PREFSBUFSIZE];
451 debug_print(_("Reading actions configurations...\n"));
453 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
454 if ((fp = fopen(rcpath, "rb")) == NULL) {
455 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
461 while (prefs_common.actionslst != NULL) {
462 act = (gchar *) prefs_common.actionslst->data;
463 prefs_common.actionslst = g_slist_remove(
464 prefs_common.actionslst,
469 while (fgets(buf, sizeof(buf), fp) != NULL) {
471 act = strstr(buf, ": ");
473 get_action_type(&act[2]) != ACTION_ERROR)
474 prefs_common.actionslst = g_slist_append(
475 prefs_common.actionslst,
481 void prefs_actions_write_config()
487 debug_print(_("Writing actions configuration...\n"));
489 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
490 if ((pfile= prefs_write_open(rcpath)) == NULL) {
491 g_warning(_("failed to write configuration to file\n"));
496 for (cur = prefs_common.actionslst; cur != NULL; cur = cur->next) {
497 gchar *act = (gchar *) cur->data;
498 if (fputs(act, pfile->fp) == EOF ||
499 fputc('\n', pfile->fp) == EOF) {
500 FILE_OP_ERROR(rcpath, "fputs || fputc");
501 prefs_write_close_revert(pfile);
509 if (prefs_write_close(pfile) < 0) {
510 g_warning(_("failed to write configuration to file\n"));
515 static guint get_action_type(gchar *action)
518 guint action_type = ACTION_NONE;
520 g_return_val_if_fail(action, ACTION_ERROR);
521 g_return_val_if_fail(*action, ACTION_ERROR);
526 action_type |= ACTION_PIPE_IN;
528 } else if (p[0] == '>') {
529 action_type |= ACTION_OPEN_IN;
531 } else if (p[0] == '*') {
532 action_type |= ACTION_HIDE_IN;
539 while (*p && action_type != ACTION_ERROR) {
542 case 'f': action_type |= ACTION_SINGLE;
544 case 'F': action_type |= ACTION_MULTIPLE;
546 case 'p': action_type |= ACTION_SINGLE;
548 default: action_type = ACTION_ERROR;
551 } else if (p[0] == '|') {
553 action_type |= ACTION_PIPE_OUT;
554 } else if (p[0] == '&') {
556 action_type |= ACTION_ASYNC;
558 action_type = ACTION_ERROR;
566 static gchar *parse_action_cmd (gchar *action,
571 GString *cmd, *tmpcmd;
578 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
581 cmd = g_string_sized_new(strlen(action));
583 while (p[0] && !(p[0] == '|' && !p[1]) && p[0] != '&') {
584 if (p[0] == '%' && p[1]) {
586 case 'f': cmd = parse_append_filename(cmd, msginfo);
590 for (cur =GTK_CLIST(ctree)->selection;
591 cur; cur = cur->next) {
592 msg = gtk_ctree_node_get_row_data(ctree,
593 GTK_CTREE_NODE(cur->data));
594 cmd = parse_append_filename(cmd, msg);
596 cmd = g_string_append_c(cmd, ' ');
601 tmpcmd = parse_append_msgpart(cmd,
607 g_string_free(cmd, TRUE);
613 default: cmd = g_string_append_c(cmd, p[0]);
614 cmd = g_string_append_c(cmd, p[1]);
618 cmd = g_string_append_c(cmd, p[0]);
623 g_string_free(cmd, TRUE);
628 g_string_free(cmd, FALSE);
632 static GString *parse_append_filename(GString *cmd, MsgInfo *msginfo)
636 g_return_val_if_fail(msginfo, cmd);
638 filename = procmsg_get_message_file(msginfo);
641 cmd = g_string_append(cmd, filename);
648 static GString *parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
659 if ((fp = procmsg_open_message_decrypted(msginfo, &partinfo))
661 alertpanel_error(_("Could not get message file."));
665 if ((fp = procmsg_open_message(msginfo)) == NULL)
667 alertpanel_error(_("Could not get message file."));
670 partinfo = procmime_scan_mime_header(fp);
674 procmime_mimeinfo_free(partinfo);
675 alertpanel_error(_("Could not get message part."));
678 filename = procmsg_get_message_file(msginfo);
680 if (!mimeview->opened) {
681 alertpanel_error(_("No message part selected."));
684 if (!mimeview->file) {
685 alertpanel_error(_("No message file selected."));
688 partinfo = gtk_ctree_node_get_row_data
689 (GTK_CTREE(mimeview->ctree),
691 g_return_val_if_fail(partinfo != NULL, cmd);
692 filename = mimeview->file;
694 partname = procmime_get_tmp_file_name(partinfo);
696 ret = procmime_get_part(partname, filename, partinfo);
699 procmime_mimeinfo_free(partinfo);
704 alertpanel_error(_("Can't get part of multipart message"));
709 cmd = g_string_append(cmd,partname);
717 static void prefs_actions_set_dialog (void)
719 GtkCList *clist = GTK_CLIST(actions.actions_clist);
721 gchar *action_str[1];
724 gtk_clist_freeze(clist);
725 gtk_clist_clear(clist);
727 action_str[0] = _("(New)");
728 row = gtk_clist_append(clist, action_str);
729 gtk_clist_set_row_data(clist, row, NULL);
731 for (cur = prefs_common.actionslst; cur != NULL; cur = cur->next) {
733 action[0] = (gchar *) cur->data;
735 row = gtk_clist_append(clist, action);
736 gtk_clist_set_row_data(clist, row, action[0]);
739 gtk_clist_thaw(clist);
741 static void prefs_actions_set_list(void)
746 g_slist_free(prefs_common.actionslst);
747 prefs_common.actionslst = NULL;
749 while ( (action = (gchar *) gtk_clist_get_row_data(GTK_CLIST(
750 actions.actions_clist), row))
752 prefs_common.actionslst = g_slist_append(prefs_common.actionslst,
759 #define GET_ENTRY(entry) \
760 entry_text = gtk_entry_get_text(GTK_ENTRY(entry))
761 static gint prefs_actions_clist_set_row (gint row)
763 GtkCList *clist = GTK_CLIST(actions.actions_clist);
766 gchar action[PREFSBUFSIZE];
770 g_return_val_if_fail(row != 0, -1);
774 GET_ENTRY(actions.name_entry);
775 if (entry_text[0] == '\0') {
776 alertpanel_error(_("Menu name is not set."));
780 if (strchr(entry_text, ':')) {
781 alertpanel_error(_("Colon ':' is not allowed in the menu name."));
785 strncpy(action, entry_text, PREFSBUFSIZE - 1);
788 /* Keep space for the ': ' delimiter */
789 len = strlen(action) + 2;
790 if (len >= PREFSBUFSIZE - 1) {
791 alertpanel_error(_("Menu name is too long."));
795 strcat(action, ": ");
797 GET_ENTRY(actions.cmd_entry);
799 if (entry_text[0] == '\0') {
800 alertpanel_error(_("Command line not set."));
804 if (len + strlen(entry_text) >= PREFSBUFSIZE - 1) {
805 alertpanel_error(_("Menu name and command are too long."));
809 if (get_action_type(entry_text) == ACTION_ERROR) {
810 alertpanel_error(_("The command\n%s\nhas a syntax error."),
815 strcat(action, entry_text);
819 row = gtk_clist_append(clist, buf);
822 gtk_clist_set_text(clist, row, 0, action);
823 old_action = (gchar *) gtk_clist_get_row_data(clist, row);
828 buf[0] = g_strdup(action);
830 gtk_clist_set_row_data(clist, row, buf[0]);
832 prefs_actions_set_list();
837 /* callback functions */
838 static void prefs_actions_register_cb (GtkWidget *w, gpointer data)
840 prefs_actions_clist_set_row(-1);
843 static void prefs_actions_substitute_cb (GtkWidget *w, gpointer data)
845 GtkCList *clist = GTK_CLIST(actions.actions_clist);
849 if (!clist->selection) return;
851 row = GPOINTER_TO_INT(clist->selection->data);
852 if (row == 0) return;
854 action = gtk_clist_get_row_data(clist, row);
857 prefs_actions_clist_set_row(row);
860 static void prefs_actions_delete_cb (GtkWidget *w, gpointer data)
862 GtkCList *clist = GTK_CLIST(actions.actions_clist);
866 if (!clist->selection) return;
867 row = GPOINTER_TO_INT(clist->selection->data);
868 if (row == 0) return;
870 if (alertpanel(_("Delete action"),
871 _("Do you really want to delete this action?"),
872 _("Yes"), _("No"), NULL) == G_ALERTALTERNATE)
875 action = gtk_clist_get_row_data(clist, row);
877 gtk_clist_remove(clist, row);
878 prefs_common.actionslst = g_slist_remove(prefs_common.actionslst, action);
881 static void prefs_actions_up (GtkWidget *w, gpointer data)
883 GtkCList *clist = GTK_CLIST(actions.actions_clist);
886 if (!clist->selection) return;
888 row = GPOINTER_TO_INT(clist->selection->data);
890 gtk_clist_row_move(clist, row, row - 1);
893 static void prefs_actions_down (GtkWidget *w, gpointer data)
895 GtkCList *clist = GTK_CLIST(actions.actions_clist);
898 if (!clist->selection) return;
900 row = GPOINTER_TO_INT(clist->selection->data);
901 if (row > 0 && row < clist->rows - 1)
902 gtk_clist_row_move(clist, row, row + 1);
905 #define ENTRY_SET_TEXT(entry, str) \
906 gtk_entry_set_text(GTK_ENTRY(entry), str ? str : "")
907 static void prefs_actions_select (GtkCList *clist,
914 gchar buf[PREFSBUFSIZE];
915 action = gtk_clist_get_row_data(clist, row);
918 ENTRY_SET_TEXT(actions.name_entry, "");
919 ENTRY_SET_TEXT(actions.cmd_entry, "");
923 strncpy(buf, action, PREFSBUFSIZE - 1);
924 buf[PREFSBUFSIZE - 1] = 0x00;
925 cmd = strstr(buf, ": ");
928 ENTRY_SET_TEXT(actions.cmd_entry, &cmd[2]);
933 ENTRY_SET_TEXT(actions.name_entry, buf);
936 static void prefs_actions_row_move (GtkCList *clist,
940 prefs_actions_set_list();
941 if (gtk_clist_row_is_visible(clist, dest_row) != GTK_VISIBILITY_FULL) {
942 gtk_clist_moveto(clist, dest_row, -1,
943 source_row < dest_row ? 1.0 : 0.0, 0.0);
947 static gint prefs_actions_deleted (GtkWidget *widget,
951 prefs_actions_cancel(widget, data);
955 static void prefs_actions_key_pressed (GtkWidget *widget,
959 if (event && event->keyval == GDK_Escape)
960 prefs_actions_cancel(widget, data);
963 static void prefs_actions_cancel (GtkWidget *w, gpointer data)
965 prefs_actions_read_config();
966 gtk_widget_hide(actions.window);
970 static void prefs_actions_ok (GtkWidget *widget, gpointer data)
972 GtkItemFactory *ifactory;
973 MainWindow *mainwin = (MainWindow *) data;
975 prefs_actions_write_config();
976 ifactory = gtk_item_factory_from_widget(mainwin->menubar);
977 update_mainwin_actions_menu(ifactory, mainwin);
978 gtk_widget_hide(actions.window);
982 void update_mainwin_actions_menu(GtkItemFactory *ifactory,
985 update_actions_menu(ifactory, "/Tools/Actions",
986 mainwin_actions_execute_cb,
990 void update_compose_actions_menu(GtkItemFactory *ifactory,
994 update_actions_menu(ifactory, branch_path,
995 compose_actions_execute_cb,
999 static void update_actions_menu(GtkItemFactory *ifactory,
1004 GtkWidget *menuitem;
1007 gchar *action, *action_p;
1009 GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
1011 ifentry.path = branch_path;
1012 menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
1013 g_return_if_fail(menuitem != NULL);
1015 amenu = GTK_MENU_SHELL(menuitem)->children;
1016 while (amenu != NULL) {
1017 GList *alist = amenu->next;
1018 gtk_widget_destroy(GTK_WIDGET(amenu->data));
1022 ifentry.accelerator = NULL;
1023 ifentry.callback_action = 0;
1024 ifentry.callback = callback;
1025 ifentry.item_type = NULL;
1027 for (cur = prefs_common.actionslst; cur; cur = cur->next) {
1028 action = g_strdup((gchar *) cur->data);
1029 action_p = strstr(action, ": ");
1030 if (action_p && action_p[2] &&
1031 get_action_type(&action_p[2]) != ACTION_ERROR) {
1033 menu_path = g_strdup_printf("%s/%s", branch_path,
1035 ifentry.path = menu_path;
1036 gtk_item_factory_create_item(ifactory, &ifentry, data,
1041 ifentry.callback_action++;
1045 static void compose_actions_execute_cb (Compose *compose,
1049 gchar *buf, *action;
1052 g_return_if_fail(action_nb < g_slist_length(prefs_common.actionslst));
1054 buf = (gchar *) g_slist_nth_data(prefs_common.actionslst, action_nb);
1056 g_return_if_fail(buf);
1057 g_return_if_fail(action = strstr(buf, ": "));
1059 /* Point to the beginning of the command-line */
1063 action_type = get_action_type(action);
1064 if (action_type & ACTION_MULTIPLE ||
1065 action_type & ACTION_SINGLE ) {
1066 alertpanel_warning(_("The selected action cannot be used "
1067 "in the compose window because it "
1068 "contains %%f, %%F or %%p."));
1072 execute_actions(action, compose->window, NULL, compose->text, NULL);
1075 static void mainwin_actions_execute_cb (MainWindow *mainwin,
1079 MessageView *messageview = mainwin->messageview;
1080 GtkWidget *text = NULL;
1083 MimeView *mimeview = NULL;
1085 g_return_if_fail (action_nb < g_slist_length(prefs_common.actionslst));
1087 buf = (gchar *) g_slist_nth_data(prefs_common.actionslst, action_nb);
1089 g_return_if_fail(buf);
1090 g_return_if_fail(action = strstr(buf, ": "));
1092 /* Point to the beginning of the command-line */
1096 switch (messageview->type) {
1098 if (messageview->textview &&
1099 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,
1123 GList *cur, *selection = NULL;
1124 GSList *children_list = NULL;
1126 gint selection_len = 0;
1128 ChildInfo *child_info;
1133 g_return_val_if_fail(action && *action, FALSE);
1135 action_type = get_action_type(action);
1137 if (action_type == ACTION_ERROR)
1138 return FALSE; /* ERR: syntax error */
1140 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) &&
1141 !(ctree && GTK_CLIST(ctree)->selection))
1142 return FALSE; /* ERR: file command without selection */
1145 selection = GTK_CLIST(ctree)->selection;
1146 selection_len = g_list_length(selection);
1149 if ((action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN))) {
1150 if (ctree && selection_len > 1)
1151 return FALSE; /* ERR: pipe + multiple selection */
1153 return FALSE; /* ERR: pipe and no displayed text */
1156 if (!(action_type & ACTION_ASYNC) && window) {
1157 gtk_widget_set_sensitive(window, FALSE);
1160 children = g_new0(Children, 1);
1162 if (action_type & ACTION_SINGLE) {
1163 for (cur = selection; cur && is_ok == TRUE; cur = cur->next) {
1164 msginfo = gtk_ctree_node_get_row_data(ctree,
1165 GTK_CTREE_NODE(cur->data));
1167 is_ok = FALSE; /* ERR: msginfo missing */
1170 cmd = parse_action_cmd(action, msginfo, ctree,
1173 debug_print(_("Action command error\n"));
1174 is_ok = FALSE; /* ERR: incorrect command */
1177 if ((child_info = fork_child(cmd, action_type, text,
1179 children_list = g_slist_append(children_list,
1181 children->open_in = (selection_len == 1) ?
1184 ACTION_HIDE_IN)) : 0;
1189 cmd = parse_action_cmd(action, NULL, ctree, mimeview);
1191 if ((child_info = fork_child(cmd, action_type, text,
1193 children_list = g_slist_append(children_list,
1195 children->open_in = action_type &
1201 is_ok = FALSE; /* ERR: incorrect command */
1204 if (!children_list) {
1205 /* If not waiting for children, return */
1207 if (!(action_type & ACTION_ASYNC) && window) {
1208 gtk_widget_set_sensitive(window, TRUE);
1213 children->action = g_strdup(action);
1214 children->window = window;
1215 children->dialog = NULL;
1216 children->list = children_list;
1217 children->nb = g_slist_length(children_list);
1219 for (cur = children_list; cur; cur = cur->next) {
1220 child_info = (ChildInfo *) cur->data;
1221 child_info->tag_status =
1222 gdk_input_add(child_info->chld_status,
1224 catch_status, child_info);
1226 children->timer = children->open_in ? 0 :
1227 gtk_timeout_add(WAIT_LAP, wait_for_children,
1230 if (children->open_in)
1231 create_io_dialog(children);
1236 ChildInfo *fork_child(gchar *cmd,
1241 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
1243 gint start, end, is_selection;
1246 ChildInfo *child_info;
1249 sync = !(action_type & ACTION_ASYNC);
1251 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
1252 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
1255 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out)
1256 || pipe(chld_err)) {
1257 alertpanel_error(_("Command could not started. "
1258 "Pipe creation failed.\n%s"),
1260 /* Closing fd = -1 fails silently */
1267 close(chld_status[0]);
1268 close(chld_status[1]);
1269 return NULL; /* Pipe error */
1272 debug_print(_("Forking child and grandchild.\n"));
1275 if (pid == (pid_t) 0) {/* Child */
1279 close(ConnectionNumber(gdk_display));
1284 if (setpgid(0, getppid()))
1291 close(fileno(stdin));
1297 close(fileno(stdout));
1302 close(fileno(stderr));
1312 execvp("/bin/sh", cmdline);
1317 } else if (gch_pid < (pid_t) 0) {/* Fork error */
1319 write(chld_status[1], "1\n", 2);
1330 close(chld_status[0]);
1333 debug_print(_("Child: Waiting for grandchild\n"));
1334 waitpid(gch_pid, NULL, 0);
1335 debug_print(_("Child: grandchild ended\n"));
1337 write(chld_status[1], "0\n", 2);
1338 close(chld_status[1]);
1342 } else if (pid < (pid_t) 0) {/* Fork error */
1343 alertpanel_error(_("Could not fork to execute the following "
1344 "command:\n%s\n%s"),
1345 cmd, g_strerror(errno));
1354 if (!(action_type & (ACTION_PIPE_IN | ACTION_OPEN_IN | ACTION_HIDE_IN)))
1358 close(chld_status[1]);
1360 child_info = g_new0(ChildInfo, 1);
1362 child_info->children = children;
1364 child_info->pid = pid;
1365 child_info->cmd = g_strdup(cmd);
1366 child_info->type = action_type;
1367 child_info->new_out = FALSE;
1368 child_info->output = g_string_sized_new(0);
1369 child_info->chld_in = (action_type & (ACTION_PIPE_IN | ACTION_OPEN_IN
1372 child_info->chld_out = chld_out[0];
1373 child_info->chld_err = chld_err[0];
1374 child_info->chld_status = chld_status[0];
1375 child_info->tag_in = -1;
1376 child_info->tag_out = gdk_input_add(chld_out[0], GDK_INPUT_READ,
1377 catch_output, child_info);
1378 child_info->tag_err = gdk_input_add(chld_err[0], GDK_INPUT_READ,
1379 catch_output, child_info);
1381 if (!(action_type & (ACTION_PIPE_IN | ACTION_PIPE_OUT)))
1384 child_info->text = text;
1387 end = gtk_stext_get_length(GTK_STEXT(text));
1389 if (GTK_EDITABLE(text)->has_selection) {
1390 start = GTK_EDITABLE(text)->selection_start_pos;
1391 end = GTK_EDITABLE(text)->selection_end_pos;
1398 is_selection = TRUE;
1401 end = gtk_stext_get_length(GTK_STEXT(text));
1402 is_selection = FALSE;
1406 selection = gtk_editable_get_chars(GTK_EDITABLE(text), start, end);
1409 if (action_type & ACTION_PIPE_IN) {
1410 write(chld_in[1], selection, strlen(selection));
1411 if (!(action_type & (ACTION_OPEN_IN | ACTION_HIDE_IN)))
1413 child_info->chld_in = -1; /* No more input */
1417 gtk_stext_freeze(GTK_STEXT(text));
1418 if (action_type & ACTION_PIPE_OUT) {
1419 gtk_stext_set_point(GTK_STEXT(text), start);
1420 gtk_stext_forward_delete(GTK_STEXT(text), end - start);
1423 gtk_stext_thaw(GTK_STEXT(text));
1428 static void kill_children_cb(GtkWidget *widget, gpointer data)
1431 Children *children = (Children *) data;
1432 ChildInfo *child_info;
1434 for (cur = children->list; cur; cur = cur->next) {
1435 child_info = (ChildInfo *)(cur->data);
1436 debug_print(_("Killing child group id %d\n"), child_info->pid);
1437 if (child_info->pid && (kill(-child_info->pid, SIGTERM) < 0))
1442 static gint wait_for_children(gpointer data)
1444 gboolean new_output;
1445 Children *children = (Children *) data;
1446 ChildInfo *child_info;
1448 gint nb = children->nb;
1452 cur = children->list;
1455 child_info = (ChildInfo *) cur->data;
1456 if (child_info->pid)
1458 new_output |= child_info->new_out;
1462 if (!children->dialog &&
1463 (new_output || children->timer))
1464 create_io_dialog(children);
1466 if (children->timer) {
1467 gtk_timeout_remove(children->timer);
1468 children->timer = 0;
1470 children->output |= new_output;
1472 if (new_output || (children->dialog && (nb != children->nb)))
1473 update_io_dialog(children);
1478 if (!children->dialog) {
1479 gtk_widget_set_sensitive(children->window, TRUE);
1480 free_children(children);
1481 } else if (!children->output) {
1482 gtk_widget_destroy(children->dialog);
1487 static void send_input(GtkWidget *w, gpointer data)
1489 Children *children = (Children *) data;
1490 ChildInfo *child_info = (ChildInfo *) children->list->data;
1492 child_info->tag_in = gdk_input_add(child_info->chld_in,
1494 catch_input, children);
1495 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1498 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1500 hide_io_dialog_cb(w, data);
1504 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1507 Children *children = (Children *) data;
1509 if (!children->nb) {
1510 gtk_widget_set_sensitive(children->window, TRUE);
1511 gtk_signal_disconnect_by_data(GTK_OBJECT(children->dialog),
1513 gtk_widget_destroy(children->dialog);
1514 free_children(children);
1518 static void childinfo_close_pipes(ChildInfo *child_info)
1520 if (child_info->tag_in > 0)
1521 gdk_input_remove(child_info->tag_in);
1522 gdk_input_remove(child_info->tag_out);
1523 gdk_input_remove(child_info->tag_err);
1525 if (child_info->chld_in >= 0)
1526 close(child_info->chld_in);
1527 close(child_info->chld_out);
1528 close(child_info->chld_err);
1529 close(child_info->chld_status);
1532 static void free_children(Children *children)
1535 ChildInfo *child_info;
1537 debug_print(_("Freeing children data %x\n"), (guint) children);
1539 g_free(children->action);
1540 for (cur = children->list; cur;) {
1541 child_info = (ChildInfo *) cur->data;
1542 g_free(child_info->cmd);
1543 g_string_free(child_info->output, TRUE);
1544 children->list = g_slist_remove(children->list, child_info);
1546 cur = children->list;
1551 static void update_io_dialog(Children *children)
1555 debug_print(_("Updating actions input/output dialog.\n"));
1557 if (!children->nb) {
1558 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1559 gtk_widget_set_sensitive(children->hide_btn, TRUE);
1560 gtk_widget_set_sensitive(children->window, TRUE);
1561 if (children->input_hbox)
1562 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1565 if (children->output) {
1566 GtkWidget *text = children->text;
1568 ChildInfo *child_info;
1570 gtk_widget_show(children->scrolledwin);
1571 gtk_text_freeze(GTK_TEXT(text));
1572 gtk_text_set_point(GTK_TEXT(text), 0);
1573 gtk_text_forward_delete(GTK_TEXT(text),
1574 gtk_text_get_length(GTK_TEXT(text)));
1575 for (cur = children->list; cur; cur = cur->next)
1577 child_info = (ChildInfo *) cur->data;
1578 if (child_info->pid)
1579 caption = g_strdup_printf(_("--- Running: %s\n")
1582 caption = g_strdup_printf(_("--- Ended: %s\n")
1585 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1587 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1588 child_info->output->str, -1);
1590 child_info->new_out = FALSE;
1592 gtk_text_thaw(GTK_TEXT(text));
1597 static void create_io_dialog(Children *children)
1600 GtkWidget *entry = NULL;
1601 GtkWidget *input_hbox = NULL;
1602 GtkWidget *send_button;
1605 GtkWidget *scrolledwin;
1607 GtkWidget *abort_button;
1608 GtkWidget *hide_button;
1610 debug_print(_("Creating actions dialog\n"));
1612 dialog = gtk_dialog_new();
1613 label = gtk_label_new(children->action);
1614 gtk_misc_set_padding(GTK_MISC(label), 8, 8);
1615 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, FALSE,
1618 gtk_window_set_title(GTK_WINDOW(dialog), _("Actions' input/output"));
1619 gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
1620 GTK_SIGNAL_FUNC(delete_io_dialog_cb), children);
1621 gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
1622 GTK_SIGNAL_FUNC(hide_io_dialog_cb),
1625 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1626 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1627 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
1628 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), scrolledwin, TRUE,
1630 gtk_widget_set_usize(scrolledwin, 600, 200);
1631 gtk_widget_hide(scrolledwin);
1633 text = gtk_text_new(gtk_scrolled_window_get_hadjustment
1634 (GTK_SCROLLED_WINDOW(scrolledwin)),
1635 gtk_scrolled_window_get_vadjustment
1636 (GTK_SCROLLED_WINDOW(scrolledwin)));
1637 gtk_text_set_editable(GTK_TEXT(text), FALSE);
1638 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1639 gtk_widget_show(text);
1641 if (children->open_in) {
1642 input_hbox = gtk_hbox_new(FALSE, 8);
1643 gtk_widget_show(input_hbox);
1645 entry = gtk_entry_new();
1646 gtk_signal_connect(GTK_OBJECT(entry), "activate",
1647 GTK_SIGNAL_FUNC(send_input), children);
1648 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 8);
1649 if (children->open_in & ACTION_HIDE_IN)
1650 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1651 gtk_widget_show(entry);
1653 send_button = gtk_button_new_with_label(_("Send"));
1654 gtk_signal_connect(GTK_OBJECT(send_button), "clicked",
1655 GTK_SIGNAL_FUNC(send_input), children);
1656 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1658 gtk_widget_show(send_button);
1660 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1661 input_hbox, FALSE, FALSE, 8);
1662 gtk_widget_grab_focus(entry);
1665 hbox = gtk_hbox_new(TRUE, 0);
1666 gtk_widget_show(hbox);
1668 abort_button = gtk_button_new_with_label(_("Abort actions"));
1669 gtk_signal_connect(GTK_OBJECT(abort_button), "clicked",
1670 GTK_SIGNAL_FUNC(kill_children_cb), children);
1671 gtk_box_pack_start(GTK_BOX(hbox), abort_button, TRUE, TRUE, 8);
1672 gtk_widget_show(abort_button);
1674 hide_button = gtk_button_new_with_label(_("Close window"));
1675 gtk_signal_connect(GTK_OBJECT(hide_button), "clicked",
1676 GTK_SIGNAL_FUNC(hide_io_dialog_cb), children);
1677 gtk_box_pack_start(GTK_BOX(hbox), hide_button, TRUE, TRUE, 8);
1678 gtk_widget_show(hide_button);
1681 gtk_widget_set_sensitive(hide_button, FALSE);
1683 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
1686 children->dialog = dialog;
1687 children->scrolledwin = scrolledwin;
1688 children->text = text;
1689 children->input_hbox = children->open_in ? input_hbox : NULL;
1690 children->input_entry = children->open_in ? entry : NULL;
1691 children->abort_btn = abort_button;
1692 children->hide_btn = hide_button;
1694 gtk_widget_show(dialog);
1697 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1699 ChildInfo *child_info = (ChildInfo *) data;
1703 gdk_input_remove(child_info->tag_status);
1705 c = read(source, &buf, 1);
1706 debug_print(_("Child returned %c\n"), buf);
1708 waitpid(-child_info->pid, NULL, 0);
1709 childinfo_close_pipes(child_info);
1710 child_info->pid = 0;
1712 wait_for_children(child_info->children);
1715 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1717 Children *children = (Children *) data;
1718 ChildInfo *child_info = (ChildInfo *) children->list->data;
1722 debug_print(_("Sending input to grand child.\n"));
1723 if (!(cond && GDK_INPUT_WRITE))
1726 gdk_input_remove(child_info->tag_in);
1727 child_info->tag_in = -1;
1729 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry), 0,
1731 c = write(child_info->chld_in, input, strlen(input));
1735 write(child_info->chld_in, "\n", 2);
1737 gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1738 gtk_widget_set_sensitive(children->input_hbox, TRUE);
1739 debug_print(_("Input to grand child sent.\n"));
1742 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1744 ChildInfo *child_info = (ChildInfo *) data;
1746 gchar buf[PREFSBUFSIZE];
1748 debug_print(_("Catching grand child's output.\n"));
1749 if (child_info->type & ACTION_PIPE_OUT
1750 && source == child_info->chld_out) {
1751 gboolean is_selection = FALSE;
1752 GtkWidget *text = child_info->text;
1753 if (GTK_EDITABLE(text)->has_selection)
1754 is_selection = TRUE;
1755 gtk_stext_freeze(GTK_STEXT(text));
1757 c =read(source, buf, PREFSBUFSIZE - 1);
1760 gtk_stext_insert(GTK_STEXT(text), NULL, NULL, NULL,
1764 /* Using the select_region draws things. Should not.
1765 * so we just change selection position and
1766 * defere drawing when thawing. Hack?
1768 GTK_EDITABLE(text)->selection_end_pos =
1769 gtk_stext_get_point(GTK_STEXT(text));
1771 gtk_stext_thaw(GTK_STEXT(child_info->text));
1773 c = read(source, buf, PREFSBUFSIZE - 1);
1774 for (i = 0; i < c; i++)
1775 child_info->output = g_string_append_c(
1776 child_info->output, buf[i]);
1778 child_info->new_out = TRUE;