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)
203 if (prefs_rc_is_readonly(ACTIONS_RC))
208 prefs_actions_create(mainwin);
210 manage_window_set_transient(GTK_WINDOW(actions.window));
211 gtk_widget_grab_focus(actions.ok_btn);
213 prefs_actions_set_dialog();
215 gtk_widget_show(actions.window);
218 static void prefs_actions_create(MainWindow *mainwin)
223 GtkWidget *cancel_btn;
224 GtkWidget *confirm_area;
228 GtkWidget *name_label;
229 GtkWidget *name_entry;
230 GtkWidget *cmd_label;
231 GtkWidget *cmd_entry;
237 GtkWidget *subst_btn;
240 GtkWidget *cond_hbox;
241 GtkWidget *cond_scrolledwin;
242 GtkWidget *cond_clist;
244 GtkWidget *help_vbox;
245 GtkWidget *help_label;
246 GtkWidget *help_toggle;
254 debug_print(_("Creating actions setting window...\n"));
256 window = gtk_window_new (GTK_WINDOW_DIALOG);
258 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
259 gtk_window_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
260 gtk_window_set_modal (GTK_WINDOW (window), TRUE);
261 gtk_window_set_policy (GTK_WINDOW (window), FALSE, TRUE, TRUE);
263 vbox = gtk_vbox_new (FALSE, 6);
264 gtk_widget_show (vbox);
265 gtk_container_add (GTK_CONTAINER (window), vbox);
267 gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
268 &cancel_btn, _("Cancel"), NULL, NULL);
269 gtk_widget_show (confirm_area);
270 gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
271 gtk_widget_grab_default (ok_btn);
273 gtk_window_set_title (GTK_WINDOW(window),
274 _("Actions setting"));
275 gtk_signal_connect (GTK_OBJECT(window), "delete_event",
276 GTK_SIGNAL_FUNC(prefs_actions_deleted), NULL);
277 gtk_signal_connect (GTK_OBJECT(window), "key_press_event",
278 GTK_SIGNAL_FUNC(prefs_actions_key_pressed), NULL);
279 MANAGE_WINDOW_SIGNALS_CONNECT (window);
280 gtk_signal_connect (GTK_OBJECT(ok_btn), "clicked",
281 GTK_SIGNAL_FUNC(prefs_actions_ok), mainwin);
282 gtk_signal_connect (GTK_OBJECT(cancel_btn), "clicked",
283 GTK_SIGNAL_FUNC(prefs_actions_cancel), NULL);
285 vbox1 = gtk_vbox_new (FALSE, VSPACING);
286 gtk_widget_show (vbox1);
287 gtk_box_pack_start (GTK_BOX (vbox), vbox1, TRUE, TRUE, 0);
288 gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
290 hbox = gtk_hbox_new (FALSE, 0);
291 gtk_box_pack_start (GTK_BOX(vbox1), hbox, TRUE, TRUE, 0);
293 name_label = gtk_label_new(_("Menu name: "));
294 gtk_widget_show(name_label);
295 gtk_box_pack_start (GTK_BOX (hbox), name_label, FALSE, FALSE, 0);
297 name_entry = gtk_entry_new();
298 gtk_widget_show(name_entry);
299 gtk_box_pack_start (GTK_BOX (hbox), name_entry, TRUE, TRUE, 0);
301 gtk_widget_show_all(hbox);
303 hbox = gtk_hbox_new (FALSE, 0);
304 gtk_box_pack_start (GTK_BOX(vbox1), hbox, TRUE, TRUE, 0);
306 cmd_label = gtk_label_new(_("Command line: "));
307 gtk_box_pack_start (GTK_BOX (hbox), cmd_label, FALSE, FALSE, 0);
309 cmd_entry = gtk_entry_new();
310 gtk_box_pack_start (GTK_BOX (hbox), cmd_entry, TRUE, TRUE, 0);
312 gtk_widget_show_all(hbox);
314 help_vbox = gtk_vbox_new(FALSE, 8);
315 gtk_box_pack_start (GTK_BOX (vbox1), help_vbox, TRUE, TRUE, 0);
317 help_label = gtk_label_new (_("Menu name:\n"
318 " Use / in menu name to make submenus.\n"
320 " Begin with:\n | to send message "
322 "to command\n > to send user provided"
323 " text to command\n * to send user "
324 "provided hidden text to command\n"
325 " End with:\n | to replace "
326 "message body or selection with command "
328 "run command asynchronously\n Use %f "
329 "for message file name\n %F for the"
330 " list of the file names of selected "
331 "messages\n %p for the selected message "
333 gtk_misc_set_alignment(GTK_MISC(help_label), 0, 0.5);
334 gtk_label_set_justify (GTK_LABEL(help_label), GTK_JUSTIFY_LEFT);
335 gtk_widget_show (help_label);
336 gtk_box_pack_start (GTK_BOX (help_vbox), help_label, TRUE, TRUE, 0);
337 gtk_widget_hide(help_vbox);
339 /* register / substitute / delete */
341 reg_hbox = gtk_hbox_new (FALSE, 4);
342 gtk_widget_show (reg_hbox);
343 gtk_box_pack_start (GTK_BOX (vbox1), reg_hbox, FALSE, FALSE, 0);
345 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
346 gtk_widget_show (arrow);
347 gtk_box_pack_start (GTK_BOX (reg_hbox), arrow, FALSE, FALSE, 0);
348 gtk_widget_set_usize (arrow, -1, 16);
350 btn_hbox = gtk_hbox_new (FALSE, 4);
351 gtk_widget_show (btn_hbox);
352 gtk_box_pack_start (GTK_BOX (reg_hbox), btn_hbox, FALSE, FALSE, 0);
354 reg_btn = gtk_button_new_with_label (_("Register"));
355 gtk_widget_show (reg_btn);
356 gtk_box_pack_start (GTK_BOX (btn_hbox), reg_btn, FALSE, TRUE, 0);
357 gtk_signal_connect (GTK_OBJECT (reg_btn), "clicked",
358 GTK_SIGNAL_FUNC (prefs_actions_register_cb), NULL);
360 subst_btn = gtk_button_new_with_label (_(" Substitute "));
361 gtk_widget_show (subst_btn);
362 gtk_box_pack_start (GTK_BOX (btn_hbox), subst_btn, FALSE, TRUE, 0);
363 gtk_signal_connect (GTK_OBJECT (subst_btn), "clicked",
364 GTK_SIGNAL_FUNC (prefs_actions_substitute_cb),
367 del_btn = gtk_button_new_with_label (_("Delete"));
368 gtk_widget_show (del_btn);
369 gtk_box_pack_start (GTK_BOX (btn_hbox), del_btn, FALSE, TRUE, 0);
370 gtk_signal_connect (GTK_OBJECT (del_btn), "clicked",
371 GTK_SIGNAL_FUNC (prefs_actions_delete_cb), NULL);
373 help_toggle = gtk_toggle_button_new_with_label (_("Help on syntax"));
374 gtk_widget_show (help_toggle);
375 gtk_box_pack_end (GTK_BOX (btn_hbox), help_toggle, FALSE, TRUE, 0);
376 gtk_signal_connect (GTK_OBJECT (help_toggle), "toggled",
377 GTK_SIGNAL_FUNC (prefs_actions_help_cb), help_vbox);
379 cond_hbox = gtk_hbox_new (FALSE, 8);
380 gtk_widget_show (cond_hbox);
381 gtk_box_pack_start (GTK_BOX (vbox1), cond_hbox, TRUE, TRUE, 0);
383 cond_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
384 gtk_widget_show (cond_scrolledwin);
385 gtk_widget_set_usize (cond_scrolledwin, -1, 150);
386 gtk_box_pack_start (GTK_BOX (cond_hbox), cond_scrolledwin,
388 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (cond_scrolledwin),
389 GTK_POLICY_AUTOMATIC,
390 GTK_POLICY_AUTOMATIC);
392 title[0] = _("Registered actions");
393 cond_clist = gtk_clist_new_with_titles(1, title);
394 gtk_widget_show (cond_clist);
395 gtk_container_add (GTK_CONTAINER (cond_scrolledwin), cond_clist);
396 gtk_clist_set_column_width (GTK_CLIST (cond_clist), 0, 80);
397 gtk_clist_set_selection_mode (GTK_CLIST (cond_clist),
398 GTK_SELECTION_BROWSE);
399 GTK_WIDGET_UNSET_FLAGS (GTK_CLIST (cond_clist)->column[0].button,
401 gtk_signal_connect (GTK_OBJECT (cond_clist), "select_row",
402 GTK_SIGNAL_FUNC (prefs_actions_select), NULL);
403 gtk_signal_connect_after (GTK_OBJECT (cond_clist), "row_move",
404 GTK_SIGNAL_FUNC (prefs_actions_row_move),
407 btn_vbox = gtk_vbox_new (FALSE, 8);
408 gtk_widget_show (btn_vbox);
409 gtk_box_pack_start (GTK_BOX (cond_hbox), btn_vbox, FALSE, FALSE, 0);
411 up_btn = gtk_button_new_with_label (_("Up"));
412 gtk_widget_show (up_btn);
413 gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
414 gtk_signal_connect (GTK_OBJECT (up_btn), "clicked",
415 GTK_SIGNAL_FUNC (prefs_actions_up), NULL);
417 down_btn = gtk_button_new_with_label (_("Down"));
418 gtk_widget_show (down_btn);
419 gtk_box_pack_start (GTK_BOX (btn_vbox), down_btn, FALSE, FALSE, 0);
420 gtk_signal_connect (GTK_OBJECT (down_btn), "clicked",
421 GTK_SIGNAL_FUNC (prefs_actions_down), NULL);
423 gtk_widget_show(window);
425 actions.window = window;
426 actions.ok_btn = ok_btn;
428 actions.name_entry = name_entry;
429 actions.cmd_entry = cmd_entry;
431 actions.actions_clist = cond_clist;
434 static void prefs_actions_help_cb(GtkWidget *w, gpointer data)
436 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)))
437 gtk_widget_show(GTK_WIDGET(data));
439 gtk_widget_hide(GTK_WIDGET(data));
442 void prefs_actions_read_config()
446 gchar buf[PREFSBUFSIZE];
449 debug_print(_("Reading actions configurations...\n"));
451 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
452 if ((fp = fopen(rcpath, "rb")) == NULL) {
453 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
459 while (prefs_common.actionslst != NULL) {
460 act = (gchar *) prefs_common.actionslst->data;
461 prefs_common.actionslst = g_slist_remove(
462 prefs_common.actionslst,
467 while (fgets(buf, sizeof(buf), fp) != NULL) {
469 act = strstr(buf, ": ");
471 get_action_type(&act[2]) != ACTION_ERROR)
472 prefs_common.actionslst = g_slist_append(
473 prefs_common.actionslst,
479 void prefs_actions_write_config()
485 debug_print(_("Writing actions configuration...\n"));
487 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
488 if ((pfile= prefs_write_open(rcpath)) == NULL) {
489 g_warning(_("failed to write configuration to file\n"));
494 for (cur = prefs_common.actionslst; cur != NULL; cur = cur->next) {
495 gchar *act = (gchar *) cur->data;
496 if (fputs(act, pfile->fp) == EOF ||
497 fputc('\n', pfile->fp) == EOF) {
498 FILE_OP_ERROR(rcpath, "fputs || fputc");
499 prefs_write_close_revert(pfile);
507 if (prefs_write_close(pfile) < 0) {
508 g_warning(_("failed to write configuration to file\n"));
513 static guint get_action_type(gchar *action)
516 guint action_type = ACTION_NONE;
518 g_return_val_if_fail(action, ACTION_ERROR);
519 g_return_val_if_fail(*action, ACTION_ERROR);
524 action_type |= ACTION_PIPE_IN;
526 } else if (p[0] == '>') {
527 action_type |= ACTION_OPEN_IN;
529 } else if (p[0] == '*') {
530 action_type |= ACTION_HIDE_IN;
537 while (*p && action_type != ACTION_ERROR) {
540 case 'f': action_type |= ACTION_SINGLE;
542 case 'F': action_type |= ACTION_MULTIPLE;
544 case 'p': action_type |= ACTION_SINGLE;
546 default: action_type = ACTION_ERROR;
549 } else if (p[0] == '|') {
551 action_type |= ACTION_PIPE_OUT;
553 action_type = ACTION_ERROR;
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[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);
595 cmd = g_string_append_c(cmd, ' ');
600 tmpcmd = parse_append_msgpart(cmd,
606 g_string_free(cmd, TRUE);
612 default: cmd = g_string_append_c(cmd, p[0]);
613 cmd = g_string_append_c(cmd, p[1]);
617 cmd = g_string_append_c(cmd, p[0]);
622 g_string_free(cmd, TRUE);
627 g_string_free(cmd, FALSE);
631 static GString *parse_append_filename(GString *cmd, MsgInfo *msginfo)
635 g_return_val_if_fail(msginfo, cmd);
637 filename = procmsg_get_message_file(msginfo);
640 cmd = g_string_append(cmd, filename);
647 static GString *parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
658 if ((fp = procmsg_open_message_decrypted(msginfo, &partinfo))
660 alertpanel_error(_("Could not get message file."));
664 if ((fp = procmsg_open_message(msginfo)) == NULL)
666 alertpanel_error(_("Could not get message file."));
669 partinfo = procmime_scan_mime_header(fp);
673 procmime_mimeinfo_free(partinfo);
674 alertpanel_error(_("Could not get message part."));
677 filename = procmsg_get_message_file(msginfo);
679 if (!mimeview->opened) {
680 alertpanel_error(_("No message part selected."));
683 if (!mimeview->file) {
684 alertpanel_error(_("No message file selected."));
687 partinfo = gtk_ctree_node_get_row_data
688 (GTK_CTREE(mimeview->ctree),
690 g_return_val_if_fail(partinfo != NULL, cmd);
691 filename = mimeview->file;
693 partname = procmime_get_tmp_file_name(partinfo);
695 ret = procmime_get_part(partname, filename, partinfo);
698 procmime_mimeinfo_free(partinfo);
703 alertpanel_error(_("Can't get part of multipart message"));
707 cmd = g_string_append_c(cmd, '"');
708 cmd = g_string_append(cmd,partname);
709 cmd = g_string_append_c(cmd, '"');
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 = strstr2(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;
1010 GtkItemFactoryEntry ifentry = {
1011 NULL, NULL, NULL, 0, "<Branch>"};
1012 ifentry.path = branch_path;
1013 menuitem = gtk_item_factory_get_item(ifactory, branch_path);
1014 g_return_if_fail(menuitem);
1015 /* FIXME: is there a better way to remove unknown submenu items? */
1016 /* Deleting and recreating the parent looses the translation */
1017 menu = GTK_WIDGET(GTK_MENU_ITEM(menuitem)->submenu);
1018 if (GTK_MENU_SHELL(menu)->children) {
1019 GList *amenu, *alist;
1020 for (amenu = (GTK_MENU_SHELL(menu)->children); amenu; ) {
1021 alist = amenu->next;
1022 gtk_widget_destroy(GTK_WIDGET(amenu->data));
1027 ifentry.accelerator = NULL;
1028 ifentry.callback_action = 0;
1029 ifentry.callback = callback;
1030 ifentry.item_type = NULL;
1032 for (cur = prefs_common.actionslst; cur; cur = cur->next) {
1033 action = g_strdup((gchar *) cur->data);
1034 action_p = strstr(action, ": ");
1035 if (action_p && action_p[2] &&
1036 get_action_type(&action_p[2]) != ACTION_ERROR) {
1038 menu_path = g_strdup_printf("%s/%s", branch_path,
1040 ifentry.path = menu_path;
1041 gtk_item_factory_create_item(ifactory, &ifentry, data,
1046 ifentry.callback_action++;
1050 static void compose_actions_execute_cb (Compose *compose,
1054 gchar *buf, *action;
1057 g_return_if_fail(action_nb < g_slist_length(prefs_common.actionslst));
1059 buf = (gchar *) g_slist_nth_data(prefs_common.actionslst, action_nb);
1061 g_return_if_fail(buf);
1062 g_return_if_fail(action = strstr(buf, ": "));
1064 /* Point to the beginning of the command-line */
1068 action_type = get_action_type(action);
1069 if (action_type & ACTION_MULTIPLE ||
1070 action_type & ACTION_SINGLE ) {
1071 alertpanel_warning(_("The selected action is not a pipe "
1072 "action.\n You can only use pipe actions "
1073 "when composing a message."));
1077 execute_actions(action, compose->window, NULL, compose->text, NULL);
1080 static void mainwin_actions_execute_cb (MainWindow *mainwin,
1084 MessageView *messageview = mainwin->messageview;
1085 GtkWidget *text = NULL;
1088 MimeView *mimeview = NULL;
1090 g_return_if_fail (action_nb < g_slist_length(prefs_common.actionslst));
1092 buf = (gchar *) g_slist_nth_data(prefs_common.actionslst, action_nb);
1094 g_return_if_fail(buf);
1095 g_return_if_fail(action = strstr(buf, ": "));
1097 /* Point to the beginning of the command-line */
1101 switch (messageview->type) {
1103 if (messageview->textview &&
1104 messageview->textview->text)
1105 text = messageview->textview->text;
1108 if (messageview->mimeview &&
1109 messageview->mimeview->type == MIMEVIEW_TEXT &&
1110 messageview->mimeview->textview &&
1111 messageview->mimeview->textview->text) {
1112 text = messageview->mimeview->textview->text;
1113 mimeview = messageview->mimeview;
1118 execute_actions(action, mainwin->window,
1119 GTK_CTREE(mainwin->summaryview->ctree), text, mimeview);
1122 static gboolean execute_actions(gchar *action,
1128 GList *cur, *selection;
1129 GSList *children_list = NULL;
1133 ChildInfo *child_info;
1138 g_return_val_if_fail(action && *action, FALSE);
1140 action_type = get_action_type(action);
1142 if (action_type == ACTION_ERROR)
1143 return FALSE; /* ERR: syntax error */
1145 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) &&
1146 !(ctree && GTK_CLIST(ctree)->selection))
1147 return FALSE; /* ERR: file command without selection */
1150 selection = GTK_CLIST(ctree)->selection;
1151 selection_len = g_list_length(selection);
1154 if ((action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN))) {
1155 if (ctree && selection_len > 1)
1156 return FALSE; /* ERR: pipe + multiple selection */
1158 return FALSE; /* ERR: pipe and no displayed text */
1161 if (!(action_type & ACTION_ASYNC) && window) {
1162 gtk_widget_set_sensitive(window, FALSE);
1165 children = g_new0(Children, 1);
1167 if (action_type & ACTION_SINGLE) {
1168 for (cur = selection; cur && is_ok == TRUE; cur = cur->next) {
1169 msginfo = gtk_ctree_node_get_row_data(ctree,
1170 GTK_CTREE_NODE(cur->data));
1172 is_ok = FALSE; /* ERR: msginfo missing */
1175 cmd = parse_action_cmd(action, msginfo, ctree,
1178 debug_print(_("Action command error\n"));
1179 is_ok = FALSE; /* ERR: incorrect command */
1182 if ((child_info = fork_child(cmd, action_type, text,
1184 children_list = g_slist_append(children_list,
1186 children->open_in = (selection_len == 1) ?
1189 ACTION_HIDE_IN)) : 0;
1194 cmd = parse_action_cmd(action, NULL, ctree, mimeview);
1196 if ((child_info = fork_child(cmd, action_type, text,
1198 children_list = g_slist_append(children_list,
1200 children->open_in = action_type &
1206 is_ok = FALSE; /* ERR: incorrect command */
1209 if (!children_list) {
1210 /* If not waiting for children, return */
1212 if (!(action_type & ACTION_ASYNC) && window) {
1213 gtk_widget_set_sensitive(window, TRUE);
1218 children->action = g_strdup(action);
1219 children->window = window;
1220 children->dialog = NULL;
1221 children->list = children_list;
1222 children->nb = g_slist_length(children_list);
1224 for (cur = children_list; cur; cur = cur->next) {
1225 child_info = (ChildInfo *) cur->data;
1226 child_info->tag_status =
1227 gdk_input_add(child_info->chld_status,
1229 catch_status, child_info);
1231 children->timer = children->open_in ? 0 :
1232 gtk_timeout_add(WAIT_LAP, wait_for_children,
1235 if (children->open_in)
1236 create_io_dialog(children);
1241 ChildInfo *fork_child(gchar *cmd,
1246 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
1248 gint start, end, is_selection;
1250 pid_t pid_c, pid_gc;
1251 ChildInfo *child_info;
1253 if (action_type & ACTION_ASYNC) {
1254 execute_command_line(cmd, TRUE);
1255 return NULL; /* Asynchronous command */
1258 if (pipe(chld_in) || pipe(chld_out) || pipe(chld_err) ||
1259 pipe(chld_status)) {
1260 alertpanel_error(_("Command could not started. Pipe creation"
1261 " failed.\n%s"), g_strerror(errno));
1262 return NULL; /* Pipe error */
1265 debug_print(_("Forking child and grandchild.\n"));
1268 if (pid_c == (pid_t) 0) {/* Child */
1272 close(ConnectionNumber(gdk_display));
1277 if (setpgid(0, getppid()))
1280 (ACTION_PIPE_IN | ACTION_OPEN_IN | ACTION_HIDE_IN)) {
1281 close(fileno(stdin));
1287 close(fileno(stdout));
1292 close(fileno(stderr));
1297 cmdline = strsplit_with_quote(cmd, " ", 1024);
1299 execvp(cmdline[0], cmdline);
1301 g_strfreev(cmdline);
1304 } else if (pid_gc < (pid_t) 0) {/* Fork erro */
1305 write(chld_status[1], "1\n", 2);
1316 close(chld_status[0]);
1317 debug_print(_("Child: Waiting for grandchild\n"));
1318 waitpid(pid_gc, NULL, 0);
1319 debug_print(_("Child: grandchild ended\n"));
1320 write(chld_status[1], "0\n", 2);
1321 close(chld_status[1]);
1324 } else if (pid_c < (pid_t) 0) {/* Fork error */
1325 alertpanel_error(_("Could not fork to execute the following "
1326 "command:\n%s\n%s"),
1327 cmd, g_strerror(errno));
1333 if (!(action_type & (ACTION_PIPE_IN | ACTION_OPEN_IN | ACTION_HIDE_IN)))
1337 close(chld_status[1]);
1339 child_info = g_new0(ChildInfo, 1);
1341 child_info->children = children;
1343 child_info->pid = pid_c;
1344 child_info->cmd = g_strdup(cmd);
1345 child_info->type = action_type;
1346 child_info->new_out = FALSE;
1347 child_info->output = g_string_sized_new(0);
1348 child_info->chld_in = (action_type & (ACTION_PIPE_IN | ACTION_OPEN_IN
1351 child_info->chld_out = chld_out[0];
1352 child_info->chld_err = chld_err[0];
1353 child_info->chld_status = chld_status[0];
1354 child_info->tag_in = -1;
1355 child_info->tag_out = gdk_input_add(chld_out[0], GDK_INPUT_READ,
1356 catch_output, child_info);
1357 child_info->tag_err = gdk_input_add(chld_err[0], GDK_INPUT_READ,
1358 catch_output, child_info);
1360 if (!(action_type & (ACTION_PIPE_IN | ACTION_PIPE_OUT)))
1363 child_info->text = text;
1366 end = gtk_stext_get_length(GTK_STEXT(text));
1368 if (GTK_EDITABLE(text)->has_selection) {
1369 start = GTK_EDITABLE(text)->selection_start_pos;
1370 end = GTK_EDITABLE(text)->selection_end_pos;
1377 is_selection = TRUE;
1380 end = gtk_stext_get_length(GTK_STEXT(text));
1381 is_selection = FALSE;
1385 selection = gtk_editable_get_chars(GTK_EDITABLE(text), start, end);
1388 if (action_type & ACTION_PIPE_IN) {
1389 write(chld_in[1], selection, strlen(selection));
1390 if (!(action_type & (ACTION_OPEN_IN | ACTION_HIDE_IN)))
1392 child_info->chld_in = -1; /* No more input */
1396 gtk_stext_freeze(GTK_STEXT(text));
1397 if (action_type & ACTION_PIPE_OUT) {
1398 gtk_stext_set_point(GTK_STEXT(text), start);
1399 gtk_stext_forward_delete(GTK_STEXT(text), end - start);
1402 gtk_stext_thaw(GTK_STEXT(text));
1407 static void kill_children_cb(GtkWidget *widget, gpointer data)
1410 Children *children = (Children *) data;
1411 ChildInfo *child_info;
1413 for (cur = children->list; cur; cur = cur->next) {
1414 child_info = (ChildInfo *)(cur->data);
1415 debug_print(_("Killing child group id %d\n"), child_info->pid);
1416 if (child_info->pid && (kill(-child_info->pid, SIGTERM) < 0))
1421 static gint wait_for_children(gpointer data)
1423 gboolean new_output;
1424 Children *children = (Children *) data;
1425 ChildInfo *child_info;
1427 gint nb = children->nb;
1431 cur = children->list;
1434 child_info = (ChildInfo *) cur->data;
1435 if (child_info->pid)
1437 new_output |= child_info->new_out;
1441 if (!children->dialog &&
1442 (new_output || children->timer))
1443 create_io_dialog(children);
1445 if (children->timer) {
1446 gtk_timeout_remove(children->timer);
1447 children->timer = 0;
1449 children->output |= new_output;
1451 if (new_output || (children->dialog && (nb != children->nb)))
1452 update_io_dialog(children);
1457 if (!children->dialog) {
1458 gtk_widget_set_sensitive(children->window, TRUE);
1459 free_children(children);
1460 } else if (!children->output) {
1461 gtk_widget_destroy(children->dialog);
1466 static void send_input(GtkWidget *w, gpointer data)
1468 Children *children = (Children *) data;
1469 ChildInfo *child_info = (ChildInfo *) children->list->data;
1471 child_info->tag_in = gdk_input_add(child_info->chld_in,
1473 catch_input, children);
1474 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1477 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1479 hide_io_dialog_cb(w, data);
1483 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1486 Children *children = (Children *) data;
1488 if (!children->nb) {
1489 gtk_widget_set_sensitive(children->window, TRUE);
1490 gtk_signal_disconnect_by_data(GTK_OBJECT(children->dialog),
1492 gtk_widget_destroy(children->dialog);
1493 free_children(children);
1497 static void childinfo_close_pipes(ChildInfo *child_info)
1499 if (child_info->tag_in > 0)
1500 gdk_input_remove(child_info->tag_in);
1501 gdk_input_remove(child_info->tag_out);
1502 gdk_input_remove(child_info->tag_err);
1504 if (child_info->chld_in >= 0)
1505 close(child_info->chld_in);
1506 close(child_info->chld_out);
1507 close(child_info->chld_err);
1508 close(child_info->chld_status);
1511 static void free_children(Children *children)
1514 ChildInfo *child_info;
1516 debug_print(_("Freeing children data %x\n"), (guint) children);
1518 g_free(children->action);
1519 for (cur = children->list; cur;) {
1520 child_info = (ChildInfo *) cur->data;
1521 g_free(child_info->cmd);
1522 g_string_free(child_info->output, TRUE);
1523 children->list = g_slist_remove(children->list, child_info);
1525 cur = children->list;
1530 static void update_io_dialog(Children *children)
1534 debug_print(_("Updating actions input/output dialog.\n"));
1536 if (!children->nb) {
1537 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1538 gtk_widget_set_sensitive(children->hide_btn, TRUE);
1539 gtk_widget_set_sensitive(children->window, TRUE);
1540 if (children->input_hbox)
1541 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1544 if (children->output) {
1545 GtkWidget *text = children->text;
1547 ChildInfo *child_info;
1549 gtk_widget_show(children->scrolledwin);
1550 gtk_text_freeze(GTK_TEXT(text));
1551 gtk_text_set_point(GTK_TEXT(text), 0);
1552 gtk_text_forward_delete(GTK_TEXT(text),
1553 gtk_text_get_length(GTK_TEXT(text)));
1554 for (cur = children->list; cur; cur = cur->next)
1556 child_info = (ChildInfo *) cur->data;
1557 if (child_info->pid)
1558 caption = g_strdup_printf(_("--- Running: %s\n")
1561 caption = g_strdup_printf(_("--- Ended: %s\n")
1564 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1566 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1567 child_info->output->str, -1);
1569 child_info->new_out = FALSE;
1571 gtk_text_thaw(GTK_TEXT(text));
1576 static void create_io_dialog(Children *children)
1580 GtkWidget *input_hbox;
1581 GtkWidget *send_button;
1584 GtkWidget *scrolledwin;
1586 GtkWidget *abort_button;
1587 GtkWidget *hide_button;
1589 debug_print(_("Creating actions dialog\n"));
1591 dialog = gtk_dialog_new();
1592 label = gtk_label_new(children->action);
1593 gtk_misc_set_padding(GTK_MISC(label), 8, 8);
1594 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, FALSE,
1597 gtk_window_set_title(GTK_WINDOW(dialog), _("Actions' input/output"));
1598 gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
1599 GTK_SIGNAL_FUNC(delete_io_dialog_cb), children);
1600 gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
1601 GTK_SIGNAL_FUNC(hide_io_dialog_cb),
1604 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1605 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1606 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
1607 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), scrolledwin, TRUE,
1609 gtk_widget_set_usize(scrolledwin, 600, 200);
1610 gtk_widget_hide(scrolledwin);
1612 text = gtk_text_new(gtk_scrolled_window_get_hadjustment
1613 (GTK_SCROLLED_WINDOW(scrolledwin)),
1614 gtk_scrolled_window_get_vadjustment
1615 (GTK_SCROLLED_WINDOW(scrolledwin)));
1616 gtk_text_set_editable(GTK_TEXT(text), FALSE);
1617 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1618 gtk_widget_show(text);
1620 if (children->open_in) {
1621 input_hbox = gtk_hbox_new(FALSE, 8);
1622 gtk_widget_show(input_hbox);
1624 entry = gtk_entry_new();
1625 gtk_signal_connect(GTK_OBJECT(entry), "activate",
1626 GTK_SIGNAL_FUNC(send_input), children);
1627 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 8);
1628 if (children->open_in & ACTION_HIDE_IN)
1629 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1630 gtk_widget_show(entry);
1632 send_button = gtk_button_new_with_label(_("Send"));
1633 gtk_signal_connect(GTK_OBJECT(send_button), "clicked",
1634 GTK_SIGNAL_FUNC(send_input), children);
1635 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1637 gtk_widget_show(send_button);
1639 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1640 input_hbox, FALSE, FALSE, 8);
1641 gtk_widget_grab_focus(entry);
1644 hbox = gtk_hbox_new(TRUE, 0);
1645 gtk_widget_show(hbox);
1647 abort_button = gtk_button_new_with_label(_("Abort actions"));
1648 gtk_signal_connect(GTK_OBJECT(abort_button), "clicked",
1649 GTK_SIGNAL_FUNC(kill_children_cb), children);
1650 gtk_box_pack_start(GTK_BOX(hbox), abort_button, TRUE, TRUE, 8);
1651 gtk_widget_show(abort_button);
1653 hide_button = gtk_button_new_with_label(_("Close window"));
1654 gtk_signal_connect(GTK_OBJECT(hide_button), "clicked",
1655 GTK_SIGNAL_FUNC(hide_io_dialog_cb), children);
1656 gtk_box_pack_start(GTK_BOX(hbox), hide_button, TRUE, TRUE, 8);
1657 gtk_widget_show(hide_button);
1660 gtk_widget_set_sensitive(hide_button, FALSE);
1662 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
1665 children->dialog = dialog;
1666 children->scrolledwin = scrolledwin;
1667 children->text = text;
1668 children->input_hbox = children->open_in ? input_hbox : NULL;
1669 children->input_entry = children->open_in ? entry : NULL;
1670 children->abort_btn = abort_button;
1671 children->hide_btn = hide_button;
1673 gtk_widget_show(dialog);
1676 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1678 ChildInfo *child_info = (ChildInfo *) data;
1682 gdk_input_remove(child_info->tag_status);
1684 c = read(source, &buf, 1);
1685 debug_print(_("Child returned %c\n"), buf);
1687 waitpid(-child_info->pid, NULL, 0);
1688 childinfo_close_pipes(child_info);
1689 child_info->pid = 0;
1691 wait_for_children(child_info->children);
1694 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1696 Children *children = (Children *) data;
1697 ChildInfo *child_info = (ChildInfo *) children->list->data;
1701 debug_print(_("Sending input to grand child.\n"));
1702 if (!(cond && GDK_INPUT_WRITE))
1705 gdk_input_remove(child_info->tag_in);
1706 child_info->tag_in = -1;
1708 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry), 0,
1710 c = write(child_info->chld_in, input, strlen(input));
1714 write(child_info->chld_in, "\n", 2);
1716 gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1717 gtk_widget_set_sensitive(children->input_hbox, TRUE);
1718 debug_print(_("Input to grand child sent.\n"));
1721 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1723 ChildInfo *child_info = (ChildInfo *) data;
1725 gchar buf[PREFSBUFSIZE];
1727 debug_print(_("Catching grand child's output.\n"));
1728 if (child_info->type & ACTION_PIPE_OUT
1729 && source == child_info->chld_out) {
1730 gboolean is_selection = FALSE;
1731 GtkWidget *text = child_info->text;
1732 if (GTK_EDITABLE(text)->has_selection)
1733 is_selection = TRUE;
1734 gtk_stext_freeze(GTK_STEXT(text));
1736 c =read(source, buf, PREFSBUFSIZE - 1);
1739 gtk_stext_insert(GTK_STEXT(text), NULL, NULL, NULL,
1743 /* Using the select_region draws things. Should not.
1744 * so we just change selection position and
1745 * defere drawing when thawing. Hack?
1747 GTK_EDITABLE(text)->selection_end_pos =
1748 gtk_stext_get_point(GTK_STEXT(text));
1750 gtk_stext_thaw(GTK_STEXT(child_info->text));
1752 c = read(source, buf, PREFSBUFSIZE - 1);
1753 for (i = 0; i < c; i++)
1754 child_info->output = g_string_append_c(
1755 child_info->output, buf[i]);
1757 child_info->new_out = TRUE;