This commit was manufactured by cvs2svn to create tag 'LAST_STABLE'.
[claws.git] / src / prefs_actions.c
blobad2e13284a26044c2b8a11492a0a35c029a120f4
1 /*
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.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #include "defs.h"
26 #include <glib.h>
27 #include <gtk/gtk.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <gdk/gdkx.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <signal.h>
37 #include <unistd.h>
39 #include "intl.h"
40 #include "prefs_gtk.h"
41 #include "inc.h"
42 #include "utils.h"
43 #include "gtkutils.h"
44 #include "manage_window.h"
45 #include "mainwindow.h"
46 #include "prefs_common.h"
47 #include "alertpanel.h"
48 #include "prefs_actions.h"
49 #include "compose.h"
50 #include "procmsg.h"
51 #include "gtkstext.h"
52 #include "mimeview.h"
53 #include "textview.h"
55 typedef enum
57 ACTION_NONE = 1 << 0,
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_INSERT = 1 << 8,
66 ACTION_ERROR = 1 << 9,
67 } ActionType;
69 static struct Actions
71 GtkWidget *window;
73 GtkWidget *ok_btn;
75 GtkWidget *name_entry;
76 GtkWidget *cmd_entry;
78 GtkWidget *actions_clist;
79 } actions;
81 typedef struct _Children Children;
82 typedef struct _ChildInfo ChildInfo;
84 struct _Children
86 GtkWidget *window;
87 GtkWidget *dialog;
88 GtkWidget *text;
89 GtkWidget *input_entry;
90 GtkWidget *input_hbox;
91 GtkWidget *abort_btn;
92 GtkWidget *close_btn;
93 GtkWidget *scrolledwin;
95 gchar *action;
96 GSList *list;
97 gint nb;
98 gint open_in;
99 gboolean output;
102 struct _ChildInfo
104 Children *children;
105 gchar *cmd;
106 guint type;
107 pid_t pid;
108 gint chld_in;
109 gint chld_out;
110 gint chld_err;
111 gint chld_status;
112 gint tag_in;
113 gint tag_out;
114 gint tag_err;
115 gint tag_status;
116 gint new_out;
117 GString *output;
118 GtkWidget *text;
119 GdkFont *msgfont;
122 /* widget creating functions */
123 static void prefs_actions_create (MainWindow *mainwin);
124 static void prefs_actions_set_dialog (void);
125 static gint prefs_actions_clist_set_row (gint row);
127 /* callback functions */
128 static void prefs_actions_help_cb (GtkWidget *w,
129 gpointer data);
130 static void prefs_actions_register_cb (GtkWidget *w,
131 gpointer data);
132 static void prefs_actions_substitute_cb (GtkWidget *w,
133 gpointer data);
134 static void prefs_actions_delete_cb (GtkWidget *w,
135 gpointer data);
136 static void prefs_actions_up (GtkWidget *w,
137 gpointer data);
138 static void prefs_actions_down (GtkWidget *w,
139 gpointer data);
140 static void prefs_actions_select (GtkCList *clist,
141 gint row,
142 gint column,
143 GdkEvent *event);
144 static void prefs_actions_row_move (GtkCList *clist,
145 gint source_row,
146 gint dest_row);
147 static gint prefs_actions_deleted (GtkWidget *widget,
148 GdkEventAny *event,
149 gpointer *data);
150 static void prefs_actions_key_pressed (GtkWidget *widget,
151 GdkEventKey *event,
152 gpointer data);
153 static void prefs_actions_cancel (GtkWidget *w,
154 gpointer data);
155 static void prefs_actions_ok (GtkWidget *w,
156 gpointer data);
157 static void update_actions_menu (GtkItemFactory *ifactory,
158 gchar *branch_path,
159 gpointer callback,
160 gpointer data);
161 static void mainwin_actions_execute_cb (MainWindow *mainwin,
162 guint action_nb,
163 GtkWidget *widget);
164 static void compose_actions_execute_cb (Compose *compose,
165 guint action_nb,
166 GtkWidget *widget);
167 static void msgview_actions_execute_cb (MessageView *msgview,
168 guint action_nb,
169 GtkWidget *widget);
170 static guint get_action_type (gchar *action);
172 static gboolean execute_actions (gchar *action,
173 GtkWidget *window,
174 GtkCTree *ctree,
175 GtkWidget *text,
176 GdkFont *msgfont,
177 gint body_pos,
178 MimeView *mimeview);
180 static gchar *parse_action_cmd (gchar *action,
181 MsgInfo *msginfo,
182 GtkCTree *ctree,
183 MimeView *mimeview);
184 static gboolean parse_append_filename (GString **cmd,
185 MsgInfo *msginfo);
187 static gboolean parse_append_msgpart (GString **cmd,
188 MsgInfo *msginfo,
189 MimeView *mimeview);
191 ChildInfo *fork_child (gchar *cmd,
192 gint action_type,
193 GtkWidget *text,
194 GdkFont *msgfont,
195 gint body_pos,
196 Children *children);
198 static gint wait_for_children (gpointer data);
200 static void free_children (Children *children);
202 static void childinfo_close_pipes (ChildInfo *child_info);
204 static void create_io_dialog (Children *children);
205 static void update_io_dialog (Children *children);
207 static void hide_io_dialog_cb (GtkWidget *widget,
208 gpointer data);
210 static void catch_output (gpointer data,
211 gint source,
212 GdkInputCondition cond);
213 static void catch_input (gpointer data,
214 gint source,
215 GdkInputCondition cond);
216 static void catch_status (gpointer data,
217 gint source,
218 GdkInputCondition cond);
220 void prefs_actions_open(MainWindow *mainwin)
222 #if 0
223 if (prefs_rc_is_readonly(ACTIONS_RC))
224 return;
225 #endif
226 inc_lock();
228 if (!actions.window)
229 prefs_actions_create(mainwin);
231 manage_window_set_transient(GTK_WINDOW(actions.window));
232 gtk_widget_grab_focus(actions.ok_btn);
234 prefs_actions_set_dialog();
236 gtk_widget_show(actions.window);
239 static void prefs_actions_create(MainWindow *mainwin)
241 GtkWidget *window;
242 GtkWidget *vbox;
243 GtkWidget *ok_btn;
244 GtkWidget *cancel_btn;
245 GtkWidget *confirm_area;
247 GtkWidget *vbox1;
249 GtkWidget *entry_vbox;
250 GtkWidget *hbox;
251 GtkWidget *name_label;
252 GtkWidget *name_entry;
253 GtkWidget *cmd_label;
254 GtkWidget *cmd_entry;
256 GtkWidget *reg_hbox;
257 GtkWidget *btn_hbox;
258 GtkWidget *arrow;
259 GtkWidget *reg_btn;
260 GtkWidget *subst_btn;
261 GtkWidget *del_btn;
263 GtkWidget *cond_hbox;
264 GtkWidget *cond_scrolledwin;
265 GtkWidget *cond_clist;
267 GtkWidget *help_vbox;
268 GtkWidget *help_label;
269 GtkWidget *help_toggle;
271 GtkWidget *btn_vbox;
272 GtkWidget *up_btn;
273 GtkWidget *down_btn;
275 gchar *title[1];
277 debug_print("Creating actions configuration window...\n");
279 window = gtk_window_new (GTK_WINDOW_DIALOG);
281 gtk_container_set_border_width(GTK_CONTAINER (window), 8);
282 gtk_window_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
283 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
284 gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, TRUE);
285 gtk_window_set_default_size(GTK_WINDOW(window), 400, -1);
287 vbox = gtk_vbox_new(FALSE, 6);
288 gtk_widget_show(vbox);
289 gtk_container_add(GTK_CONTAINER(window), vbox);
291 gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
292 &cancel_btn, _("Cancel"), NULL, NULL);
293 gtk_widget_show(confirm_area);
294 gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
295 gtk_widget_grab_default(ok_btn);
297 gtk_window_set_title(GTK_WINDOW(window), _("Actions configuration"));
298 gtk_signal_connect(GTK_OBJECT(window), "delete_event",
299 GTK_SIGNAL_FUNC(prefs_actions_deleted), NULL);
300 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
301 GTK_SIGNAL_FUNC(prefs_actions_key_pressed), NULL);
302 MANAGE_WINDOW_SIGNALS_CONNECT(window);
303 gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
304 GTK_SIGNAL_FUNC(prefs_actions_ok), mainwin);
305 gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
306 GTK_SIGNAL_FUNC(prefs_actions_cancel), NULL);
308 vbox1 = gtk_vbox_new(FALSE, 8);
309 gtk_widget_show(vbox1);
310 gtk_box_pack_start(GTK_BOX(vbox), vbox1, TRUE, TRUE, 0);
311 gtk_container_set_border_width(GTK_CONTAINER(vbox1), 2);
313 entry_vbox = gtk_vbox_new(FALSE, 4);
314 gtk_box_pack_start(GTK_BOX(vbox1), entry_vbox, FALSE, FALSE, 0);
316 hbox = gtk_hbox_new(FALSE, 8);
317 gtk_box_pack_start(GTK_BOX(entry_vbox), hbox, FALSE, FALSE, 0);
319 name_label = gtk_label_new(_("Menu name:"));
320 gtk_box_pack_start(GTK_BOX(hbox), name_label, FALSE, FALSE, 0);
322 name_entry = gtk_entry_new();
323 gtk_box_pack_start(GTK_BOX(hbox), name_entry, TRUE, TRUE, 0);
325 hbox = gtk_hbox_new(FALSE, 8);
326 gtk_box_pack_start(GTK_BOX(entry_vbox), hbox, TRUE, TRUE, 0);
328 cmd_label = gtk_label_new(_("Command line:"));
329 gtk_box_pack_start(GTK_BOX(hbox), cmd_label, FALSE, FALSE, 0);
331 cmd_entry = gtk_entry_new();
332 gtk_box_pack_start(GTK_BOX(hbox), cmd_entry, TRUE, TRUE, 0);
334 gtk_widget_show_all(entry_vbox);
336 help_vbox = gtk_vbox_new(FALSE, 8);
337 gtk_box_pack_start(GTK_BOX(vbox1), help_vbox, FALSE, FALSE, 0);
339 help_label = gtk_label_new
340 (_("Menu name:\n"
341 " Use / in menu name to make submenus.\n"
342 "Command line:\n"
343 " Begin with:\n"
344 " | to send message body or selection to command\n"
345 " > to send user provided text to command\n"
346 " * to send user provided hidden text to command\n"
347 " End with:\n"
348 " | to replace message body or selection with command output\n"
349 " > to insert command's output without replacing old text\n"
350 " & to run command asynchronously\n"
351 " Use %f for message file name\n"
352 " %F for the list of the file names of selected messages\n"
353 " %p for the selected message MIME part."));
354 gtk_misc_set_alignment(GTK_MISC(help_label), 0, 0.5);
355 gtk_label_set_justify(GTK_LABEL(help_label), GTK_JUSTIFY_LEFT);
356 gtk_widget_show(help_label);
357 gtk_box_pack_start(GTK_BOX(help_vbox), help_label, FALSE, FALSE, 0);
358 gtk_widget_hide(help_vbox);
360 /* register / substitute / delete */
362 reg_hbox = gtk_hbox_new(FALSE, 4);
363 gtk_widget_show(reg_hbox);
364 gtk_box_pack_start(GTK_BOX(vbox1), reg_hbox, FALSE, FALSE, 0);
366 arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
367 gtk_widget_show(arrow);
368 gtk_box_pack_start(GTK_BOX(reg_hbox), arrow, FALSE, FALSE, 0);
369 gtk_widget_set_usize(arrow, -1, 16);
371 btn_hbox = gtk_hbox_new(TRUE, 4);
372 gtk_widget_show(btn_hbox);
373 gtk_box_pack_start(GTK_BOX(reg_hbox), btn_hbox, FALSE, FALSE, 0);
375 reg_btn = gtk_button_new_with_label(_("Add"));
376 gtk_widget_show(reg_btn);
377 gtk_box_pack_start(GTK_BOX(btn_hbox), reg_btn, FALSE, TRUE, 0);
378 gtk_signal_connect(GTK_OBJECT(reg_btn), "clicked",
379 GTK_SIGNAL_FUNC(prefs_actions_register_cb), NULL);
381 subst_btn = gtk_button_new_with_label(_(" Replace "));
382 gtk_widget_show(subst_btn);
383 gtk_box_pack_start(GTK_BOX(btn_hbox), subst_btn, FALSE, TRUE, 0);
384 gtk_signal_connect(GTK_OBJECT(subst_btn), "clicked",
385 GTK_SIGNAL_FUNC(prefs_actions_substitute_cb),
386 NULL);
388 del_btn = gtk_button_new_with_label(_("Delete"));
389 gtk_widget_show(del_btn);
390 gtk_box_pack_start(GTK_BOX(btn_hbox), del_btn, FALSE, TRUE, 0);
391 gtk_signal_connect(GTK_OBJECT(del_btn), "clicked",
392 GTK_SIGNAL_FUNC(prefs_actions_delete_cb), NULL);
394 help_toggle = gtk_toggle_button_new_with_label(_(" Syntax help "));
395 gtk_widget_show(help_toggle);
396 gtk_box_pack_end(GTK_BOX(reg_hbox), help_toggle, FALSE, FALSE, 0);
397 gtk_signal_connect(GTK_OBJECT(help_toggle), "toggled",
398 GTK_SIGNAL_FUNC(prefs_actions_help_cb), help_vbox);
400 cond_hbox = gtk_hbox_new(FALSE, 8);
401 gtk_widget_show(cond_hbox);
402 gtk_box_pack_start(GTK_BOX(vbox1), cond_hbox, TRUE, TRUE, 0);
404 cond_scrolledwin = gtk_scrolled_window_new(NULL, NULL);
405 gtk_widget_show(cond_scrolledwin);
406 gtk_widget_set_usize(cond_scrolledwin, -1, 150);
407 gtk_box_pack_start(GTK_BOX(cond_hbox), cond_scrolledwin,
408 TRUE, TRUE, 0);
409 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (cond_scrolledwin),
410 GTK_POLICY_AUTOMATIC,
411 GTK_POLICY_AUTOMATIC);
413 title[0] = _("Current actions");
414 cond_clist = gtk_clist_new_with_titles(1, title);
415 gtk_widget_show(cond_clist);
416 gtk_container_add(GTK_CONTAINER (cond_scrolledwin), cond_clist);
417 gtk_clist_set_column_width(GTK_CLIST (cond_clist), 0, 80);
418 gtk_clist_set_selection_mode(GTK_CLIST (cond_clist),
419 GTK_SELECTION_BROWSE);
420 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(cond_clist)->column[0].button,
421 GTK_CAN_FOCUS);
422 gtk_signal_connect(GTK_OBJECT(cond_clist), "select_row",
423 GTK_SIGNAL_FUNC(prefs_actions_select), NULL);
424 gtk_signal_connect_after(GTK_OBJECT(cond_clist), "row_move",
425 GTK_SIGNAL_FUNC(prefs_actions_row_move),
426 NULL);
428 btn_vbox = gtk_vbox_new(FALSE, 8);
429 gtk_widget_show(btn_vbox);
430 gtk_box_pack_start(GTK_BOX(cond_hbox), btn_vbox, FALSE, FALSE, 0);
432 up_btn = gtk_button_new_with_label(_("Up"));
433 gtk_widget_show(up_btn);
434 gtk_box_pack_start(GTK_BOX(btn_vbox), up_btn, FALSE, FALSE, 0);
435 gtk_signal_connect(GTK_OBJECT(up_btn), "clicked",
436 GTK_SIGNAL_FUNC(prefs_actions_up), NULL);
438 down_btn = gtk_button_new_with_label(_("Down"));
439 gtk_widget_show(down_btn);
440 gtk_box_pack_start(GTK_BOX(btn_vbox), down_btn, FALSE, FALSE, 0);
441 gtk_signal_connect(GTK_OBJECT(down_btn), "clicked",
442 GTK_SIGNAL_FUNC(prefs_actions_down), NULL);
444 gtk_widget_show(window);
446 actions.window = window;
447 actions.ok_btn = ok_btn;
449 actions.name_entry = name_entry;
450 actions.cmd_entry = cmd_entry;
452 actions.actions_clist = cond_clist;
455 static void prefs_actions_help_cb(GtkWidget *w, gpointer data)
457 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)))
458 gtk_widget_show(GTK_WIDGET(data));
459 else
460 gtk_widget_hide(GTK_WIDGET(data));
463 void prefs_actions_read_config(void)
465 gchar *rcpath;
466 FILE *fp;
467 gchar buf[PREFSBUFSIZE];
468 gchar *act;
470 debug_print("Reading actions configurations...\n");
472 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
473 if ((fp = fopen(rcpath, "rb")) == NULL) {
474 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
475 g_free(rcpath);
476 return;
478 g_free(rcpath);
480 while (prefs_common.actions_list != NULL) {
481 act = (gchar *)prefs_common.actions_list->data;
482 prefs_common.actions_list =
483 g_slist_remove(prefs_common.actions_list, act);
484 g_free(act);
487 while (fgets(buf, sizeof(buf), fp) != NULL) {
488 g_strchomp(buf);
489 act = strstr(buf, ": ");
490 if (act && act[2] &&
491 get_action_type(&act[2]) != ACTION_ERROR)
492 prefs_common.actions_list =
493 g_slist_append(prefs_common.actions_list,
494 g_strdup(buf));
496 fclose(fp);
499 void prefs_actions_write_config(void)
501 gchar *rcpath;
502 PrefFile *pfile;
503 GSList *cur;
505 debug_print("Writing actions configuration...\n");
507 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
508 if ((pfile= prefs_write_open(rcpath)) == NULL) {
509 g_warning("failed to write configuration to file\n");
510 g_free(rcpath);
511 return;
514 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
515 gchar *act = (gchar *)cur->data;
516 if (fputs(act, pfile->fp) == EOF ||
517 fputc('\n', pfile->fp) == EOF) {
518 FILE_OP_ERROR(rcpath, "fputs || fputc");
519 prefs_write_close_revert(pfile);
520 g_free(rcpath);
521 return;
525 g_free(rcpath);
527 if (prefs_write_close(pfile) < 0) {
528 g_warning("failed to write configuration to file\n");
529 return;
533 static guint get_action_type(gchar *action)
535 gchar *p;
536 guint action_type = ACTION_NONE;
538 g_return_val_if_fail(action, ACTION_ERROR);
539 g_return_val_if_fail(*action, ACTION_ERROR);
541 p = action;
543 if (p[0] == '|') {
544 action_type |= ACTION_PIPE_IN;
545 p++;
546 } else if (p[0] == '>') {
547 action_type |= ACTION_OPEN_IN;
548 p++;
549 } else if (p[0] == '*') {
550 action_type |= ACTION_HIDE_IN;
551 p++;
554 if (p[0] == 0x00)
555 return ACTION_ERROR;
557 while (*p && action_type != ACTION_ERROR) {
558 if (p[0] == '%') {
559 switch (p[1]) {
560 case 'f':
561 action_type |= ACTION_SINGLE;
562 break;
563 case 'F':
564 action_type |= ACTION_MULTIPLE;
565 break;
566 case 'p':
567 action_type |= ACTION_SINGLE;
568 break;
569 default:
570 action_type = ACTION_ERROR;
571 break;
573 } else if (p[0] == '|') {
574 if (p[1] == 0x00)
575 action_type |= ACTION_PIPE_OUT;
576 } else if (p[0] == '>') {
577 if (p[1] == 0x00)
578 action_type |= ACTION_INSERT;
579 } else if (p[0] == '&') {
580 if (p[1] == 0x00)
581 action_type |= ACTION_ASYNC;
582 else
583 action_type = ACTION_ERROR;
585 p++;
588 return action_type;
591 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
592 GtkCTree *ctree, MimeView *mimeview)
594 GString *cmd;
595 gchar *p;
596 GList *cur;
597 MsgInfo *msg;
599 p = action;
601 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
602 p++;
604 cmd = g_string_sized_new(strlen(action));
606 while (p[0] &&
607 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
608 if (p[0] == '%' && p[1]) {
609 switch (p[1]) {
610 case 'f':
611 if (!parse_append_filename(&cmd, msginfo)) {
612 g_string_free(cmd, TRUE);
613 return NULL;
615 p++;
616 break;
617 case 'F':
618 for (cur = GTK_CLIST(ctree)->selection;
619 cur != NULL; cur = cur->next) {
620 msg = gtk_ctree_node_get_row_data(ctree,
621 GTK_CTREE_NODE(cur->data));
622 if (!parse_append_filename(&cmd, msg)) {
623 g_string_free(cmd, TRUE);
624 return NULL;
626 if (cur->next)
627 cmd = g_string_append_c(cmd, ' ');
629 p++;
630 break;
631 case 'p':
632 if (!parse_append_msgpart(&cmd, msginfo,
633 mimeview)) {
634 g_string_free(cmd, TRUE);
635 return NULL;
637 p++;
638 break;
639 default:
640 cmd = g_string_append_c(cmd, p[0]);
641 cmd = g_string_append_c(cmd, p[1]);
642 p++;
644 } else {
645 cmd = g_string_append_c(cmd, p[0]);
647 p++;
649 if (cmd->len == 0) {
650 g_string_free(cmd, TRUE);
651 return NULL;
654 p = cmd->str;
655 g_string_free(cmd, FALSE);
656 return p;
659 static gboolean parse_append_filename(GString **cmd, MsgInfo *msginfo)
661 gchar *filename;
663 g_return_val_if_fail(msginfo, FALSE);
665 filename = procmsg_get_message_file(msginfo);
667 if (filename) {
668 *cmd = g_string_append(*cmd, filename);
669 g_free(filename);
670 } else {
671 alertpanel_error(_("Could not get message file %d"),
672 msginfo->msgnum);
673 return FALSE;
676 return TRUE;
679 static gboolean parse_append_msgpart(GString **cmd, MsgInfo *msginfo,
680 MimeView *mimeview)
682 gchar *filename;
683 gchar *partname;
684 MimeInfo *partinfo;
685 gint ret;
686 FILE *fp;
688 if (!mimeview) {
689 #if USE_GPGME
690 if ((fp = procmsg_open_message_decrypted(msginfo, &partinfo))
691 == NULL) {
692 alertpanel_error(_("Could not get message file."));
693 return FALSE;
695 #else
696 if ((fp = procmsg_open_message(msginfo)) == NULL) {
697 alertpanel_error(_("Could not get message file."));
698 return FALSE;
700 partinfo = procmime_scan_mime_header(fp);
701 #endif
702 fclose(fp);
703 if (!partinfo) {
704 procmime_mimeinfo_free_all(partinfo);
705 alertpanel_error(_("Could not get message part."));
706 return FALSE;
708 filename = procmsg_get_message_file(msginfo);
709 } else {
710 if (!mimeview->opened) {
711 alertpanel_error(_("No message part selected."));
712 return FALSE;
714 if (!mimeview->file) {
715 alertpanel_error(_("No message file selected."));
716 return FALSE;
718 partinfo = gtk_ctree_node_get_row_data
719 (GTK_CTREE(mimeview->ctree),
720 mimeview->opened);
721 g_return_val_if_fail(partinfo != NULL, FALSE);
722 filename = mimeview->file;
724 partname = procmime_get_tmp_file_name(partinfo);
726 ret = procmime_get_part(partname, filename, partinfo);
728 if (!mimeview) {
729 procmime_mimeinfo_free_all(partinfo);
730 g_free(filename);
733 if (ret < 0) {
734 alertpanel_error(_("Can't get part of multipart message"));
735 g_free(partname);
736 return FALSE;
739 *cmd = g_string_append(*cmd, partname);
741 g_free(partname);
743 return TRUE;
746 static void prefs_actions_set_dialog(void)
748 GtkCList *clist = GTK_CLIST(actions.actions_clist);
749 GSList *cur;
750 gchar *action_str[1];
751 gint row;
753 gtk_clist_freeze(clist);
754 gtk_clist_clear(clist);
756 action_str[0] = _("(New)");
757 row = gtk_clist_append(clist, action_str);
758 gtk_clist_set_row_data(clist, row, NULL);
760 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
761 gchar *action[1];
763 action[0] = (gchar *)cur->data;
764 row = gtk_clist_append(clist, action);
765 gtk_clist_set_row_data(clist, row, action[0]);
768 gtk_clist_thaw(clist);
771 static void prefs_actions_set_list(void)
773 gint row = 1;
774 gchar *action;
776 g_slist_free(prefs_common.actions_list);
777 prefs_common.actions_list = NULL;
779 while ((action = (gchar *)gtk_clist_get_row_data
780 (GTK_CLIST(actions.actions_clist), row)) != NULL) {
781 prefs_common.actions_list =
782 g_slist_append(prefs_common.actions_list, action);
783 row++;
787 #define GET_ENTRY(entry) \
788 entry_text = gtk_entry_get_text(GTK_ENTRY(entry))
790 static gint prefs_actions_clist_set_row(gint row)
792 GtkCList *clist = GTK_CLIST(actions.actions_clist);
793 gchar *entry_text;
794 gint len;
795 gchar action[PREFSBUFSIZE];
796 gchar *buf[1];
798 g_return_val_if_fail(row != 0, -1);
800 GET_ENTRY(actions.name_entry);
801 if (entry_text[0] == '\0') {
802 alertpanel_error(_("Menu name is not set."));
803 return -1;
806 if (strchr(entry_text, ':')) {
807 alertpanel_error(_("Colon ':' is not allowed in the menu name."));
808 return -1;
811 strncpy(action, entry_text, PREFSBUFSIZE - 1);
812 g_strstrip(action);
814 /* Keep space for the ': ' delimiter */
815 len = strlen(action) + 2;
816 if (len >= PREFSBUFSIZE - 1) {
817 alertpanel_error(_("Menu name is too long."));
818 return -1;
821 strcat(action, ": ");
823 GET_ENTRY(actions.cmd_entry);
825 if (entry_text[0] == '\0') {
826 alertpanel_error(_("Command line not set."));
827 return -1;
830 if (len + strlen(entry_text) >= PREFSBUFSIZE - 1) {
831 alertpanel_error(_("Menu name and command are too long."));
832 return -1;
835 if (get_action_type(entry_text) == ACTION_ERROR) {
836 alertpanel_error(_("The command\n%s\nhas a syntax error."),
837 entry_text);
838 return -1;
841 strcat(action, entry_text);
843 buf[0] = action;
844 if (row < 0)
845 row = gtk_clist_append(clist, buf);
846 else {
847 gchar *old_action;
848 gtk_clist_set_text(clist, row, 0, action);
849 old_action = (gchar *) gtk_clist_get_row_data(clist, row);
850 if (old_action)
851 g_free(old_action);
854 buf[0] = g_strdup(action);
856 gtk_clist_set_row_data(clist, row, buf[0]);
858 prefs_actions_set_list();
860 return 0;
863 /* callback functions */
865 static void prefs_actions_register_cb(GtkWidget *w, gpointer data)
867 prefs_actions_clist_set_row(-1);
870 static void prefs_actions_substitute_cb(GtkWidget *w, gpointer data)
872 GtkCList *clist = GTK_CLIST(actions.actions_clist);
873 gchar *action;
874 gint row;
876 if (!clist->selection) return;
878 row = GPOINTER_TO_INT(clist->selection->data);
879 if (row == 0) return;
881 action = gtk_clist_get_row_data(clist, row);
882 if (!action) return;
884 prefs_actions_clist_set_row(row);
887 static void prefs_actions_delete_cb(GtkWidget *w, gpointer data)
889 GtkCList *clist = GTK_CLIST(actions.actions_clist);
890 gchar *action;
891 gint row;
893 if (!clist->selection) return;
894 row = GPOINTER_TO_INT(clist->selection->data);
895 if (row == 0) return;
897 if (alertpanel(_("Delete action"),
898 _("Do you really want to delete this action?"),
899 _("Yes"), _("No"), NULL) == G_ALERTALTERNATE)
900 return;
902 action = gtk_clist_get_row_data(clist, row);
903 g_free(action);
904 gtk_clist_remove(clist, row);
905 prefs_common.actions_list = g_slist_remove(prefs_common.actions_list,
906 action);
909 static void prefs_actions_up(GtkWidget *w, gpointer data)
911 GtkCList *clist = GTK_CLIST(actions.actions_clist);
912 gint row;
914 if (!clist->selection) return;
916 row = GPOINTER_TO_INT(clist->selection->data);
917 if (row > 1)
918 gtk_clist_row_move(clist, row, row - 1);
921 static void prefs_actions_down(GtkWidget *w, gpointer data)
923 GtkCList *clist = GTK_CLIST(actions.actions_clist);
924 gint row;
926 if (!clist->selection) return;
928 row = GPOINTER_TO_INT(clist->selection->data);
929 if (row > 0 && row < clist->rows - 1)
930 gtk_clist_row_move(clist, row, row + 1);
933 #define ENTRY_SET_TEXT(entry, str) \
934 gtk_entry_set_text(GTK_ENTRY(entry), str ? str : "")
936 static void prefs_actions_select(GtkCList *clist, gint row, gint column,
937 GdkEvent *event)
939 gchar *action;
940 gchar *cmd;
941 gchar buf[PREFSBUFSIZE];
942 action = gtk_clist_get_row_data(clist, row);
944 if (!action) {
945 ENTRY_SET_TEXT(actions.name_entry, "");
946 ENTRY_SET_TEXT(actions.cmd_entry, "");
947 return;
950 strncpy(buf, action, PREFSBUFSIZE - 1);
951 buf[PREFSBUFSIZE - 1] = 0x00;
952 cmd = strstr(buf, ": ");
954 if (cmd && cmd[2])
955 ENTRY_SET_TEXT(actions.cmd_entry, &cmd[2]);
956 else
957 return;
959 *cmd = 0x00;
960 ENTRY_SET_TEXT(actions.name_entry, buf);
963 static void prefs_actions_row_move(GtkCList *clist,
964 gint source_row, gint dest_row)
966 prefs_actions_set_list();
967 if (gtk_clist_row_is_visible(clist, dest_row) != GTK_VISIBILITY_FULL) {
968 gtk_clist_moveto(clist, dest_row, -1,
969 source_row < dest_row ? 1.0 : 0.0, 0.0);
973 static gint prefs_actions_deleted(GtkWidget *widget, GdkEventAny *event,
974 gpointer *data)
976 prefs_actions_cancel(widget, data);
977 return TRUE;
980 static void prefs_actions_key_pressed(GtkWidget *widget, GdkEventKey *event,
981 gpointer data)
983 if (event && event->keyval == GDK_Escape)
984 prefs_actions_cancel(widget, data);
987 static void prefs_actions_cancel(GtkWidget *w, gpointer data)
989 prefs_actions_read_config();
990 gtk_widget_hide(actions.window);
991 inc_unlock();
994 static void prefs_actions_ok(GtkWidget *widget, gpointer data)
996 GtkItemFactory *ifactory;
997 MainWindow *mainwin = (MainWindow *)data;
999 prefs_actions_write_config();
1000 ifactory = gtk_item_factory_from_widget(mainwin->menubar);
1001 update_mainwin_actions_menu(ifactory, mainwin);
1002 gtk_widget_hide(actions.window);
1003 inc_unlock();
1006 void update_mainwin_actions_menu(GtkItemFactory *ifactory,
1007 MainWindow *mainwin)
1009 update_actions_menu(ifactory, "/Tools/Actions",
1010 mainwin_actions_execute_cb,
1011 mainwin);
1014 void update_compose_actions_menu(GtkItemFactory *ifactory,
1015 gchar *branch_path,
1016 Compose *compose)
1018 update_actions_menu(ifactory, branch_path,
1019 compose_actions_execute_cb,
1020 compose);
1024 void actions_execute(gpointer data,
1025 guint action_nb,
1026 GtkWidget *widget,
1027 gint source)
1029 if (source == TOOLBAR_MAIN)
1030 mainwin_actions_execute_cb((MainWindow*)data, action_nb, widget);
1031 else if (source == TOOLBAR_COMPOSE)
1032 compose_actions_execute_cb((Compose*)data, action_nb, widget);
1033 else if (source == TOOLBAR_MSGVIEW)
1034 msgview_actions_execute_cb((MessageView*)data, action_nb, widget);
1038 static void update_actions_menu(GtkItemFactory *ifactory,
1039 gchar *branch_path,
1040 gpointer callback,
1041 gpointer data)
1043 GtkWidget *menuitem;
1044 gchar *menu_path;
1045 GSList *cur;
1046 gchar *action, *action_p;
1047 GList *amenu;
1048 GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
1050 ifentry.path = branch_path;
1051 menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
1052 g_return_if_fail(menuitem != NULL);
1054 amenu = GTK_MENU_SHELL(menuitem)->children;
1055 while (amenu != NULL) {
1056 GList *alist = amenu->next;
1057 gtk_widget_destroy(GTK_WIDGET(amenu->data));
1058 amenu = alist;
1061 ifentry.accelerator = NULL;
1062 ifentry.callback_action = 0;
1063 ifentry.callback = callback;
1064 ifentry.item_type = NULL;
1066 for (cur = prefs_common.actions_list; cur; cur = cur->next) {
1067 action = g_strdup((gchar *)cur->data);
1068 action_p = strstr(action, ": ");
1069 if (action_p && action_p[2] &&
1070 get_action_type(&action_p[2]) != ACTION_ERROR) {
1071 action_p[0] = 0x00;
1072 menu_path = g_strdup_printf("%s/%s", branch_path,
1073 action);
1074 ifentry.path = menu_path;
1075 gtk_item_factory_create_item(ifactory, &ifentry, data,
1077 g_free(menu_path);
1079 g_free(action);
1080 ifentry.callback_action++;
1084 static void compose_actions_execute_cb(Compose *compose, guint action_nb,
1085 GtkWidget *widget)
1087 gchar *buf, *action;
1088 guint action_type;
1090 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
1092 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
1093 g_return_if_fail(buf != NULL);
1094 action = strstr(buf, ": ");
1095 g_return_if_fail(action != NULL);
1097 /* Point to the beginning of the command-line */
1098 action += 2;
1100 action_type = get_action_type(action);
1101 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
1102 alertpanel_warning
1103 (_("The selected action cannot be used in the compose window\n"
1104 "because it contains %%f, %%F or %%p."));
1105 return;
1108 execute_actions(action, compose->window, NULL, compose->text, NULL, 0,
1109 NULL);
1112 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
1113 GtkWidget *widget)
1115 MessageView *messageview = mainwin->messageview;
1116 TextView *textview = NULL;
1117 gchar *buf,
1118 *action;
1119 MimeView *mimeview = NULL;
1121 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
1123 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
1125 g_return_if_fail(buf);
1126 g_return_if_fail(action = strstr(buf, ": "));
1128 /* Point to the beginning of the command-line */
1129 action += 2;
1131 switch (messageview->type) {
1132 case MVIEW_TEXT:
1133 if (messageview->textview && messageview->textview->text)
1134 textview = messageview->textview;
1135 break;
1136 case MVIEW_MIME:
1137 if (messageview->mimeview) {
1138 mimeview = messageview->mimeview;
1139 if (messageview->mimeview->type == MIMEVIEW_TEXT &&
1140 messageview->mimeview->textview &&
1141 messageview->mimeview->textview->text)
1142 textview = messageview->mimeview->textview;
1144 break;
1147 execute_actions(action, mainwin->window,
1148 GTK_CTREE(mainwin->summaryview->ctree), textview->text,
1149 textview->msgfont, textview->body_pos, mimeview);
1152 /* FIXME: Code duplication mainwindow_actions_execute_cb
1154 static void msgview_actions_execute_cb(MessageView *msgview, guint action_nb,
1155 GtkWidget *widget)
1157 TextView *textview = NULL;
1158 gchar *buf,
1159 *action;
1160 MimeView *mimeview = NULL;
1162 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
1164 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
1166 g_return_if_fail(buf);
1167 g_return_if_fail(action = strstr(buf, ": "));
1169 /* Point to the beginning of the command-line */
1170 action += 2;
1172 switch (msgview->type) {
1173 case MVIEW_TEXT:
1174 if (msgview->textview && msgview->textview->text)
1175 textview = msgview->textview;
1176 break;
1177 case MVIEW_MIME:
1178 if (msgview->mimeview) {
1179 mimeview = msgview->mimeview;
1180 if (msgview->mimeview->type == MIMEVIEW_TEXT &&
1181 msgview->mimeview->textview &&
1182 msgview->mimeview->textview->text)
1183 textview = msgview->mimeview->textview;
1185 break;
1188 execute_actions(action, msgview->window, NULL, textview->text,
1189 textview->msgfont, textview->body_pos, mimeview);
1192 static gboolean execute_actions(gchar *action, GtkWidget *window,
1193 GtkCTree *ctree, GtkWidget *text,
1194 GdkFont *msgfont, gint body_pos,
1195 MimeView *mimeview)
1197 GList *cur, *selection = NULL;
1198 GSList *children_list = NULL;
1199 gint is_ok = TRUE;
1200 gint selection_len = 0;
1201 Children *children;
1202 ChildInfo *child_info;
1203 gint action_type;
1204 MsgInfo *msginfo;
1205 gchar *cmd;
1207 g_return_val_if_fail(action && *action, FALSE);
1209 action_type = get_action_type(action);
1211 if (action_type == ACTION_ERROR)
1212 return FALSE; /* ERR: syntax error */
1214 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) &&
1215 !(ctree && GTK_CLIST(ctree)->selection))
1216 return FALSE; /* ERR: file command without selection */
1218 if (ctree) {
1219 selection = GTK_CLIST(ctree)->selection;
1220 selection_len = g_list_length(selection);
1223 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
1224 if (ctree && selection_len > 1)
1225 return FALSE; /* ERR: pipe + multiple selection */
1226 if (!text)
1227 return FALSE; /* ERR: pipe and no displayed text */
1230 children = g_new0(Children, 1);
1232 if (action_type & ACTION_SINGLE) {
1233 for (cur = selection; cur && is_ok == TRUE; cur = cur->next) {
1234 msginfo = gtk_ctree_node_get_row_data(ctree,
1235 GTK_CTREE_NODE(cur->data));
1236 if (!msginfo) {
1237 is_ok = FALSE; /* ERR: msginfo missing */
1238 break;
1240 cmd = parse_action_cmd(action, msginfo, ctree,
1241 mimeview);
1242 if (!cmd) {
1243 debug_print("Action command error\n");
1244 is_ok = FALSE; /* ERR: incorrect command */
1245 break;
1247 if ((child_info = fork_child(cmd, action_type, text,
1248 msgfont, body_pos,
1249 children))) {
1250 children_list = g_slist_append(children_list,
1251 child_info);
1252 children->open_in = (selection_len == 1) ?
1253 (action_type &
1254 (ACTION_OPEN_IN |
1255 ACTION_HIDE_IN)) : 0;
1257 g_free(cmd);
1259 } else {
1260 cmd = parse_action_cmd(action, NULL, ctree, mimeview);
1261 if (cmd) {
1262 if ((child_info = fork_child(cmd, action_type, text,
1263 msgfont, body_pos,
1264 children))) {
1265 children_list = g_slist_append(children_list,
1266 child_info);
1267 children->open_in = action_type &
1268 (ACTION_OPEN_IN |
1269 ACTION_HIDE_IN);
1271 g_free(cmd);
1272 } else
1273 is_ok = FALSE; /* ERR: incorrect command */
1276 if (!children_list) {
1277 /* If not waiting for children, return */
1278 g_free(children);
1279 } else {
1280 GSList *cur;
1282 children->action = g_strdup(action);
1283 children->window = window;
1284 children->dialog = NULL;
1285 children->list = children_list;
1286 children->nb = g_slist_length(children_list);
1288 for (cur = children_list; cur; cur = cur->next) {
1289 child_info = (ChildInfo *) cur->data;
1290 child_info->tag_status =
1291 gdk_input_add(child_info->chld_status,
1292 GDK_INPUT_READ,
1293 catch_status, child_info);
1296 create_io_dialog(children);
1299 return is_ok;
1302 ChildInfo *fork_child(gchar *cmd, gint action_type, GtkWidget *text,
1303 GdkFont *msgfont, gint body_pos, Children *children)
1305 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
1306 gchar *cmdline[4];
1307 gint start, end, is_selection;
1308 gchar *selection;
1309 pid_t pid, gch_pid;
1310 ChildInfo *child_info;
1311 gint sync;
1313 sync = !(action_type & ACTION_ASYNC);
1315 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
1316 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
1318 if (sync) {
1319 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
1320 pipe(chld_err)) {
1321 alertpanel_error(_("Command could not be started. "
1322 "Pipe creation failed.\n%s"),
1323 g_strerror(errno));
1324 /* Closing fd = -1 fails silently */
1325 close(chld_in[0]);
1326 close(chld_in[1]);
1327 close(chld_out[0]);
1328 close(chld_out[1]);
1329 close(chld_err[0]);
1330 close(chld_err[1]);
1331 close(chld_status[0]);
1332 close(chld_status[1]);
1333 return NULL; /* Pipe error */
1337 debug_print("Forking child and grandchild.\n");
1339 pid = fork();
1340 if (pid == 0) { /* Child */
1341 if (setpgid(0, 0))
1342 perror("setpgid");
1344 close(ConnectionNumber(gdk_display));
1346 gch_pid = fork();
1348 if (gch_pid == 0) {
1349 if (setpgid(0, getppid()))
1350 perror("setpgid");
1351 if (sync) {
1352 if (action_type &
1353 (ACTION_PIPE_IN |
1354 ACTION_OPEN_IN |
1355 ACTION_HIDE_IN)) {
1356 close(fileno(stdin));
1357 dup (chld_in[0]);
1359 close(chld_in[0]);
1360 close(chld_in[1]);
1362 close(fileno(stdout));
1363 dup (chld_out[1]);
1364 close(chld_out[0]);
1365 close(chld_out[1]);
1367 close(fileno(stderr));
1368 dup (chld_err[1]);
1369 close(chld_err[0]);
1370 close(chld_err[1]);
1373 cmdline[0] = "sh";
1374 cmdline[1] = "-c";
1375 cmdline[2] = cmd;
1376 cmdline[3] = 0;
1377 execvp("/bin/sh", cmdline);
1379 perror("execvp");
1380 _exit(1);
1381 } else if (gch_pid < (pid_t) 0) { /* Fork error */
1382 if (sync)
1383 write(chld_status[1], "1\n", 2);
1384 perror("fork");
1385 _exit(1);
1386 } else {/* Child */
1387 if (sync) {
1388 close(chld_in[0]);
1389 close(chld_in[1]);
1390 close(chld_out[0]);
1391 close(chld_out[1]);
1392 close(chld_err[0]);
1393 close(chld_err[1]);
1394 close(chld_status[0]);
1396 if (sync) {
1397 debug_print("Child: Waiting for grandchild\n");
1398 waitpid(gch_pid, NULL, 0);
1399 debug_print("Child: grandchild ended\n");
1400 write(chld_status[1], "0\n", 2);
1401 close(chld_status[1]);
1403 _exit(0);
1405 } else if (pid < 0) { /* Fork error */
1406 alertpanel_error(_("Could not fork to execute the following "
1407 "command:\n%s\n%s"),
1408 cmd, g_strerror(errno));
1409 return NULL;
1412 /* Parent */
1414 if (!sync) {
1415 waitpid(pid, NULL, 0);
1416 return NULL;
1419 close(chld_in[0]);
1420 if (!(action_type & (ACTION_PIPE_IN | ACTION_OPEN_IN | ACTION_HIDE_IN)))
1421 close(chld_in[1]);
1422 close(chld_out[1]);
1423 close(chld_err[1]);
1424 close(chld_status[1]);
1426 child_info = g_new0(ChildInfo, 1);
1428 child_info->children = children;
1430 child_info->pid = pid;
1431 child_info->cmd = g_strdup(cmd);
1432 child_info->type = action_type;
1433 child_info->new_out = FALSE;
1434 child_info->output = g_string_sized_new(0);
1435 child_info->chld_in =
1436 (action_type &
1437 (ACTION_PIPE_IN | ACTION_OPEN_IN | ACTION_HIDE_IN))
1438 ? chld_in [1] : -1;
1439 child_info->chld_out = chld_out[0];
1440 child_info->chld_err = chld_err[0];
1441 child_info->chld_status = chld_status[0];
1442 child_info->tag_in = -1;
1443 child_info->tag_out = gdk_input_add(chld_out[0], GDK_INPUT_READ,
1444 catch_output, child_info);
1445 child_info->tag_err = gdk_input_add(chld_err[0], GDK_INPUT_READ,
1446 catch_output, child_info);
1448 if (!(action_type & (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
1449 return child_info;
1451 child_info->text = text;
1452 child_info->msgfont = msgfont;
1454 start = body_pos;
1455 end = gtk_stext_get_length(GTK_STEXT(text));
1457 if (GTK_EDITABLE(text)->has_selection) {
1458 start = GTK_EDITABLE(text)->selection_start_pos;
1459 end = GTK_EDITABLE(text)->selection_end_pos;
1460 if (start > end) {
1461 gint tmp;
1462 tmp = start;
1463 start = end;
1464 end = tmp;
1466 is_selection = TRUE;
1467 if (start == end) {
1468 start = 0;
1469 end = gtk_stext_get_length(GTK_STEXT(text));
1470 is_selection = FALSE;
1474 selection = gtk_editable_get_chars(GTK_EDITABLE(text), start, end);
1476 if (action_type & ACTION_PIPE_IN) {
1477 write(chld_in[1], selection, strlen(selection));
1478 if (!(action_type & (ACTION_OPEN_IN | ACTION_HIDE_IN)))
1479 close(chld_in[1]);
1480 child_info->chld_in = -1; /* No more input */
1482 g_free(selection);
1484 gtk_stext_freeze(GTK_STEXT(text));
1485 if (action_type & ACTION_PIPE_OUT) {
1486 gtk_stext_set_point(GTK_STEXT(text), start);
1487 gtk_stext_forward_delete(GTK_STEXT(text), end - start);
1490 gtk_stext_thaw(GTK_STEXT(text));
1492 return child_info;
1495 static void kill_children_cb(GtkWidget *widget, gpointer data)
1497 GSList *cur;
1498 Children *children = (Children *) data;
1499 ChildInfo *child_info;
1501 for (cur = children->list; cur; cur = cur->next) {
1502 child_info = (ChildInfo *)(cur->data);
1503 debug_print("Killing child group id %d\n", child_info->pid);
1504 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
1505 perror("kill");
1509 static gint wait_for_children(gpointer data)
1511 gboolean new_output;
1512 Children *children = (Children *)data;
1513 ChildInfo *child_info;
1514 GSList *cur;
1515 gint nb = children->nb;
1517 children->nb = 0;
1519 cur = children->list;
1520 new_output = FALSE;
1521 while (cur) {
1522 child_info = (ChildInfo *)cur->data;
1523 if (child_info->pid)
1524 children->nb++;
1525 new_output |= child_info->new_out;
1526 cur = cur->next;
1529 children->output |= new_output;
1531 if (new_output || (children->dialog && (nb != children->nb)))
1532 update_io_dialog(children);
1534 if (children->nb)
1535 return FALSE;
1537 if (!children->dialog) {
1538 free_children(children);
1539 } else if (!children->output) {
1540 gtk_widget_destroy(children->dialog);
1543 return FALSE;
1546 static void send_input(GtkWidget *w, gpointer data)
1548 Children *children = (Children *) data;
1549 ChildInfo *child_info = (ChildInfo *) children->list->data;
1551 child_info->tag_in = gdk_input_add(child_info->chld_in,
1552 GDK_INPUT_WRITE,
1553 catch_input, children);
1554 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1557 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1559 hide_io_dialog_cb(w, data);
1560 return TRUE;
1563 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1566 Children *children = (Children *)data;
1568 if (!children->nb) {
1569 gtk_signal_disconnect_by_data(GTK_OBJECT(children->dialog),
1570 children);
1571 gtk_widget_destroy(children->dialog);
1572 free_children(children);
1576 static void childinfo_close_pipes(ChildInfo *child_info)
1578 if (child_info->tag_in > 0)
1579 gdk_input_remove(child_info->tag_in);
1580 gdk_input_remove(child_info->tag_out);
1581 gdk_input_remove(child_info->tag_err);
1583 if (child_info->chld_in >= 0)
1584 close(child_info->chld_in);
1585 close(child_info->chld_out);
1586 close(child_info->chld_err);
1587 close(child_info->chld_status);
1590 static void free_children(Children *children)
1592 GSList *cur;
1593 ChildInfo *child_info;
1595 debug_print("Freeing children data %p\n", children);
1597 g_free(children->action);
1598 for (cur = children->list; cur;) {
1599 child_info = (ChildInfo *)cur->data;
1600 g_free(child_info->cmd);
1601 g_string_free(child_info->output, TRUE);
1602 children->list = g_slist_remove(children->list, child_info);
1603 g_free(child_info);
1604 cur = children->list;
1606 g_free(children);
1609 static void update_io_dialog(Children *children)
1611 GSList *cur;
1613 debug_print("Updating actions input/output dialog.\n");
1615 if (!children->nb) {
1616 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1617 gtk_widget_set_sensitive(children->close_btn, TRUE);
1618 if (children->input_hbox)
1619 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1622 if (children->output) {
1623 GtkWidget *text = children->text;
1624 gchar *caption;
1625 ChildInfo *child_info;
1627 gtk_widget_show(children->scrolledwin);
1628 gtk_text_freeze(GTK_TEXT(text));
1629 gtk_text_set_point(GTK_TEXT(text), 0);
1630 gtk_text_forward_delete(GTK_TEXT(text),
1631 gtk_text_get_length(GTK_TEXT(text)));
1632 for (cur = children->list; cur; cur = cur->next) {
1633 child_info = (ChildInfo *)cur->data;
1634 if (child_info->pid)
1635 caption = g_strdup_printf
1636 (_("--- Running: %s\n"),
1637 child_info->cmd);
1638 else
1639 caption = g_strdup_printf
1640 (_("--- Ended: %s\n"),
1641 child_info->cmd);
1643 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1644 caption, -1);
1645 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1646 child_info->output->str, -1);
1647 g_free(caption);
1648 child_info->new_out = FALSE;
1650 gtk_text_thaw(GTK_TEXT(text));
1654 static void create_io_dialog(Children *children)
1656 GtkWidget *dialog;
1657 GtkWidget *vbox;
1658 GtkWidget *entry = NULL;
1659 GtkWidget *input_hbox = NULL;
1660 GtkWidget *send_button;
1661 GtkWidget *label;
1662 GtkWidget *text;
1663 GtkWidget *scrolledwin;
1664 GtkWidget *hbox;
1665 GtkWidget *abort_button;
1666 GtkWidget *close_button;
1668 debug_print("Creating action IO dialog\n");
1670 dialog = gtk_dialog_new();
1671 gtk_container_set_border_width
1672 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1673 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1674 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1675 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1676 manage_window_set_transient(GTK_WINDOW(dialog));
1677 gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
1678 GTK_SIGNAL_FUNC(delete_io_dialog_cb), children);
1679 gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
1680 GTK_SIGNAL_FUNC(hide_io_dialog_cb),
1681 children);
1683 vbox = gtk_vbox_new(FALSE, 8);
1684 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1685 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1686 gtk_widget_show(vbox);
1688 label = gtk_label_new(children->action);
1689 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1690 gtk_widget_show(label);
1692 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1693 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1694 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
1695 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1696 gtk_widget_set_usize(scrolledwin, 480, 200);
1697 gtk_widget_hide(scrolledwin);
1699 text = gtk_text_new(gtk_scrolled_window_get_hadjustment
1700 (GTK_SCROLLED_WINDOW(scrolledwin)),
1701 gtk_scrolled_window_get_vadjustment
1702 (GTK_SCROLLED_WINDOW(scrolledwin)));
1703 gtk_text_set_editable(GTK_TEXT(text), FALSE);
1704 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1705 gtk_widget_show(text);
1707 if (children->open_in) {
1708 input_hbox = gtk_hbox_new(FALSE, 8);
1709 gtk_widget_show(input_hbox);
1711 entry = gtk_entry_new();
1712 gtk_widget_set_usize(entry, 320, -1);
1713 gtk_signal_connect(GTK_OBJECT(entry), "activate",
1714 GTK_SIGNAL_FUNC(send_input), children);
1715 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1716 if (children->open_in & ACTION_HIDE_IN)
1717 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1718 gtk_widget_show(entry);
1720 send_button = gtk_button_new_with_label(_(" Send "));
1721 gtk_signal_connect(GTK_OBJECT(send_button), "clicked",
1722 GTK_SIGNAL_FUNC(send_input), children);
1723 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1724 FALSE, 0);
1725 gtk_widget_show(send_button);
1727 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1728 gtk_widget_grab_focus(entry);
1731 gtkut_button_set_create(&hbox, &abort_button, _("Abort"),
1732 &close_button, _("Close"), NULL, NULL);
1733 gtk_signal_connect(GTK_OBJECT(abort_button), "clicked",
1734 GTK_SIGNAL_FUNC(kill_children_cb), children);
1735 gtk_signal_connect(GTK_OBJECT(close_button), "clicked",
1736 GTK_SIGNAL_FUNC(hide_io_dialog_cb), children);
1737 gtk_widget_show(hbox);
1739 if (children->nb)
1740 gtk_widget_set_sensitive(close_button, FALSE);
1742 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1744 children->dialog = dialog;
1745 children->scrolledwin = scrolledwin;
1746 children->text = text;
1747 children->input_hbox = children->open_in ? input_hbox : NULL;
1748 children->input_entry = children->open_in ? entry : NULL;
1749 children->abort_btn = abort_button;
1750 children->close_btn = close_button;
1752 gtk_widget_show(dialog);
1755 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1757 ChildInfo *child_info = (ChildInfo *)data;
1758 gchar buf;
1759 gint c;
1761 gdk_input_remove(child_info->tag_status);
1763 c = read(source, &buf, 1);
1764 debug_print("Child returned %c\n", buf);
1766 waitpid(-child_info->pid, NULL, 0);
1767 childinfo_close_pipes(child_info);
1768 child_info->pid = 0;
1770 wait_for_children(child_info->children);
1773 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1775 Children *children = (Children *)data;
1776 ChildInfo *child_info = (ChildInfo *)children->list->data;
1777 gchar *input;
1778 gint c;
1780 debug_print("Sending input to grand child.\n");
1781 if (!(cond && GDK_INPUT_WRITE))
1782 return;
1784 gdk_input_remove(child_info->tag_in);
1785 child_info->tag_in = -1;
1787 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1788 0, -1);
1789 c = write(child_info->chld_in, input, strlen(input));
1791 g_free(input);
1793 write(child_info->chld_in, "\n", 2);
1795 gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1796 gtk_widget_set_sensitive(children->input_hbox, TRUE);
1797 debug_print("Input to grand child sent.\n");
1800 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1802 ChildInfo *child_info = (ChildInfo *)data;
1803 gint c, i;
1804 gchar buf[PREFSBUFSIZE];
1806 debug_print("Catching grand child's output.\n");
1807 if (child_info->type & (ACTION_PIPE_OUT | ACTION_INSERT)
1808 && source == child_info->chld_out) {
1809 gboolean is_selection = FALSE;
1810 GtkWidget *text = child_info->text;
1811 if (GTK_EDITABLE(text)->has_selection)
1812 is_selection = TRUE;
1813 gtk_stext_freeze(GTK_STEXT(text));
1814 while (TRUE) {
1815 c = read(source, buf, PREFSBUFSIZE - 1);
1816 if (c == 0)
1817 break;
1818 gtk_stext_insert(GTK_STEXT(text), child_info->msgfont,
1819 NULL, NULL, buf, c);
1821 if (is_selection) {
1822 /* Using the select_region draws things. Should not.
1823 * so we just change selection position and
1824 * defere drawing when thawing. Hack?
1826 GTK_EDITABLE(text)->selection_end_pos =
1827 gtk_stext_get_point(GTK_STEXT(text));
1829 gtk_stext_thaw(GTK_STEXT(child_info->text));
1830 } else {
1831 c = read(source, buf, PREFSBUFSIZE - 1);
1832 for (i = 0; i < c; i++)
1833 child_info->output = g_string_append_c
1834 (child_info->output, buf[i]);
1835 if (c > 0)
1836 child_info->new_out = TRUE;
1838 wait_for_children(child_info->children);