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>
40 #include "prefs_gtk.h"
44 #include "manage_window.h"
45 #include "mainwindow.h"
46 #include "prefs_common.h"
47 #include "alertpanel.h"
48 #include "prefs_actions.h"
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,
75 GtkWidget
*name_entry
;
78 GtkWidget
*actions_clist
;
81 typedef struct _Children Children
;
82 typedef struct _ChildInfo ChildInfo
;
89 GtkWidget
*input_entry
;
90 GtkWidget
*input_hbox
;
93 GtkWidget
*scrolledwin
;
122 /* widget creating functions */
123 static void prefs_actions_create (MainWindow
*mainwin
);
124 static void prefs_actions_set_dialog (void);
125 static gint
prefs_actions_clist_set_row (gint row
);
127 /* callback functions */
128 static void prefs_actions_help_cb (GtkWidget
*w
,
130 static void prefs_actions_register_cb (GtkWidget
*w
,
132 static void prefs_actions_substitute_cb (GtkWidget
*w
,
134 static void prefs_actions_delete_cb (GtkWidget
*w
,
136 static void prefs_actions_up (GtkWidget
*w
,
138 static void prefs_actions_down (GtkWidget
*w
,
140 static void prefs_actions_select (GtkCList
*clist
,
144 static void prefs_actions_row_move (GtkCList
*clist
,
147 static gint
prefs_actions_deleted (GtkWidget
*widget
,
150 static void prefs_actions_key_pressed (GtkWidget
*widget
,
153 static void prefs_actions_cancel (GtkWidget
*w
,
155 static void prefs_actions_ok (GtkWidget
*w
,
157 static void update_actions_menu (GtkItemFactory
*ifactory
,
161 static void mainwin_actions_execute_cb (MainWindow
*mainwin
,
164 static void compose_actions_execute_cb (Compose
*compose
,
167 static void msgview_actions_execute_cb (MessageView
*msgview
,
170 static guint
get_action_type (gchar
*action
);
172 static gboolean
execute_actions (gchar
*action
,
180 static gchar
*parse_action_cmd (gchar
*action
,
184 static gboolean
parse_append_filename (GString
**cmd
,
187 static gboolean
parse_append_msgpart (GString
**cmd
,
191 ChildInfo
*fork_child (gchar
*cmd
,
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
,
210 static void catch_output (gpointer data
,
212 GdkInputCondition cond
);
213 static void catch_input (gpointer data
,
215 GdkInputCondition cond
);
216 static void catch_status (gpointer data
,
218 GdkInputCondition cond
);
220 void prefs_actions_open(MainWindow
*mainwin
)
223 if (prefs_rc_is_readonly(ACTIONS_RC
))
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
)
244 GtkWidget
*cancel_btn
;
245 GtkWidget
*confirm_area
;
249 GtkWidget
*entry_vbox
;
251 GtkWidget
*name_label
;
252 GtkWidget
*name_entry
;
253 GtkWidget
*cmd_label
;
254 GtkWidget
*cmd_entry
;
260 GtkWidget
*subst_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
;
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
341 " Use / in menu name to make submenus.\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"
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
),
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
,
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
,
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
),
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
));
460 gtk_widget_hide(GTK_WIDGET(data
));
463 void prefs_actions_read_config(void)
467 gchar buf
[PREFSBUFSIZE
];
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");
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
);
487 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
489 act
= strstr(buf
, ": ");
491 get_action_type(&act
[2]) != ACTION_ERROR
)
492 prefs_common
.actions_list
=
493 g_slist_append(prefs_common
.actions_list
,
499 void prefs_actions_write_config(void)
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");
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
);
527 if (prefs_write_close(pfile
) < 0) {
528 g_warning("failed to write configuration to file\n");
533 static guint
get_action_type(gchar
*action
)
536 guint action_type
= ACTION_NONE
;
538 g_return_val_if_fail(action
, ACTION_ERROR
);
539 g_return_val_if_fail(*action
, ACTION_ERROR
);
544 action_type
|= ACTION_PIPE_IN
;
546 } else if (p
[0] == '>') {
547 action_type
|= ACTION_OPEN_IN
;
549 } else if (p
[0] == '*') {
550 action_type
|= ACTION_HIDE_IN
;
557 while (*p
&& action_type
!= ACTION_ERROR
) {
561 action_type
|= ACTION_SINGLE
;
564 action_type
|= ACTION_MULTIPLE
;
567 action_type
|= ACTION_SINGLE
;
570 action_type
= ACTION_ERROR
;
573 } else if (p
[0] == '|') {
575 action_type
|= ACTION_PIPE_OUT
;
576 } else if (p
[0] == '>') {
578 action_type
|= ACTION_INSERT
;
579 } else if (p
[0] == '&') {
581 action_type
|= ACTION_ASYNC
;
583 action_type
= ACTION_ERROR
;
591 static gchar
*parse_action_cmd(gchar
*action
, MsgInfo
*msginfo
,
592 GtkCTree
*ctree
, MimeView
*mimeview
)
601 if (p
[0] == '|' || p
[0] == '>' || p
[0] == '*')
604 cmd
= g_string_sized_new(strlen(action
));
607 !((p
[0] == '|' || p
[0] == '>' || p
[0] == '&') && !p
[1])) {
608 if (p
[0] == '%' && p
[1]) {
611 if (!parse_append_filename(&cmd
, msginfo
)) {
612 g_string_free(cmd
, TRUE
);
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
);
627 cmd
= g_string_append_c(cmd
, ' ');
632 if (!parse_append_msgpart(&cmd
, msginfo
,
634 g_string_free(cmd
, TRUE
);
640 cmd
= g_string_append_c(cmd
, p
[0]);
641 cmd
= g_string_append_c(cmd
, p
[1]);
645 cmd
= g_string_append_c(cmd
, p
[0]);
650 g_string_free(cmd
, TRUE
);
655 g_string_free(cmd
, FALSE
);
659 static gboolean
parse_append_filename(GString
**cmd
, MsgInfo
*msginfo
)
663 g_return_val_if_fail(msginfo
, FALSE
);
665 filename
= procmsg_get_message_file(msginfo
);
668 *cmd
= g_string_append(*cmd
, filename
);
671 alertpanel_error(_("Could not get message file %d"),
679 static gboolean
parse_append_msgpart(GString
**cmd
, MsgInfo
*msginfo
,
690 if ((fp
= procmsg_open_message_decrypted(msginfo
, &partinfo
))
692 alertpanel_error(_("Could not get message file."));
696 if ((fp
= procmsg_open_message(msginfo
)) == NULL
) {
697 alertpanel_error(_("Could not get message file."));
700 partinfo
= procmime_scan_mime_header(fp
);
704 procmime_mimeinfo_free_all(partinfo
);
705 alertpanel_error(_("Could not get message part."));
708 filename
= procmsg_get_message_file(msginfo
);
710 if (!mimeview
->opened
) {
711 alertpanel_error(_("No message part selected."));
714 if (!mimeview
->file
) {
715 alertpanel_error(_("No message file selected."));
718 partinfo
= gtk_ctree_node_get_row_data
719 (GTK_CTREE(mimeview
->ctree
),
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
);
729 procmime_mimeinfo_free_all(partinfo
);
734 alertpanel_error(_("Can't get part of multipart message"));
739 *cmd
= g_string_append(*cmd
, partname
);
746 static void prefs_actions_set_dialog(void)
748 GtkCList
*clist
= GTK_CLIST(actions
.actions_clist
);
750 gchar
*action_str
[1];
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
) {
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)
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
);
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
);
795 gchar action
[PREFSBUFSIZE
];
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."));
806 if (strchr(entry_text
, ':')) {
807 alertpanel_error(_("Colon ':' is not allowed in the menu name."));
811 strncpy(action
, entry_text
, PREFSBUFSIZE
- 1);
814 /* Keep space for the ': ' delimiter */
815 len
= strlen(action
) + 2;
816 if (len
>= PREFSBUFSIZE
- 1) {
817 alertpanel_error(_("Menu name is too long."));
821 strcat(action
, ": ");
823 GET_ENTRY(actions
.cmd_entry
);
825 if (entry_text
[0] == '\0') {
826 alertpanel_error(_("Command line not set."));
830 if (len
+ strlen(entry_text
) >= PREFSBUFSIZE
- 1) {
831 alertpanel_error(_("Menu name and command are too long."));
835 if (get_action_type(entry_text
) == ACTION_ERROR
) {
836 alertpanel_error(_("The command\n%s\nhas a syntax error."),
841 strcat(action
, entry_text
);
845 row
= gtk_clist_append(clist
, buf
);
848 gtk_clist_set_text(clist
, row
, 0, action
);
849 old_action
= (gchar
*) gtk_clist_get_row_data(clist
, row
);
854 buf
[0] = g_strdup(action
);
856 gtk_clist_set_row_data(clist
, row
, buf
[0]);
858 prefs_actions_set_list();
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
);
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
);
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
);
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
)
902 action
= gtk_clist_get_row_data(clist
, row
);
904 gtk_clist_remove(clist
, row
);
905 prefs_common
.actions_list
= g_slist_remove(prefs_common
.actions_list
,
909 static void prefs_actions_up(GtkWidget
*w
, gpointer data
)
911 GtkCList
*clist
= GTK_CLIST(actions
.actions_clist
);
914 if (!clist
->selection
) return;
916 row
= GPOINTER_TO_INT(clist
->selection
->data
);
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
);
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
,
941 gchar buf
[PREFSBUFSIZE
];
942 action
= gtk_clist_get_row_data(clist
, row
);
945 ENTRY_SET_TEXT(actions
.name_entry
, "");
946 ENTRY_SET_TEXT(actions
.cmd_entry
, "");
950 strncpy(buf
, action
, PREFSBUFSIZE
- 1);
951 buf
[PREFSBUFSIZE
- 1] = 0x00;
952 cmd
= strstr(buf
, ": ");
955 ENTRY_SET_TEXT(actions
.cmd_entry
, &cmd
[2]);
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
,
976 prefs_actions_cancel(widget
, data
);
980 static void prefs_actions_key_pressed(GtkWidget
*widget
, GdkEventKey
*event
,
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
);
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
);
1006 void update_mainwin_actions_menu(GtkItemFactory
*ifactory
,
1007 MainWindow
*mainwin
)
1009 update_actions_menu(ifactory
, "/Tools/Actions",
1010 mainwin_actions_execute_cb
,
1014 void update_compose_actions_menu(GtkItemFactory
*ifactory
,
1018 update_actions_menu(ifactory
, branch_path
,
1019 compose_actions_execute_cb
,
1024 void actions_execute(gpointer data
,
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
,
1043 GtkWidget
*menuitem
;
1046 gchar
*action
, *action_p
;
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
));
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
) {
1072 menu_path
= g_strdup_printf("%s/%s", branch_path
,
1074 ifentry
.path
= menu_path
;
1075 gtk_item_factory_create_item(ifactory
, &ifentry
, data
,
1080 ifentry
.callback_action
++;
1084 static void compose_actions_execute_cb(Compose
*compose
, guint action_nb
,
1087 gchar
*buf
, *action
;
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 */
1100 action_type
= get_action_type(action
);
1101 if (action_type
& (ACTION_SINGLE
| ACTION_MULTIPLE
)) {
1103 (_("The selected action cannot be used in the compose window\n"
1104 "because it contains %%f, %%F or %%p."));
1108 execute_actions(action
, compose
->window
, NULL
, compose
->text
, NULL
, 0,
1112 static void mainwin_actions_execute_cb(MainWindow
*mainwin
, guint action_nb
,
1115 MessageView
*messageview
= mainwin
->messageview
;
1116 TextView
*textview
= NULL
;
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 */
1131 switch (messageview
->type
) {
1133 if (messageview
->textview
&& messageview
->textview
->text
)
1134 textview
= messageview
->textview
;
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
;
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
,
1157 TextView
*textview
= NULL
;
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 */
1172 switch (msgview
->type
) {
1174 if (msgview
->textview
&& msgview
->textview
->text
)
1175 textview
= msgview
->textview
;
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
;
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
,
1197 GList
*cur
, *selection
= NULL
;
1198 GSList
*children_list
= NULL
;
1200 gint selection_len
= 0;
1202 ChildInfo
*child_info
;
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 */
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 */
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
));
1237 is_ok
= FALSE
; /* ERR: msginfo missing */
1240 cmd
= parse_action_cmd(action
, msginfo
, ctree
,
1243 debug_print("Action command error\n");
1244 is_ok
= FALSE
; /* ERR: incorrect command */
1247 if ((child_info
= fork_child(cmd
, action_type
, text
,
1250 children_list
= g_slist_append(children_list
,
1252 children
->open_in
= (selection_len
== 1) ?
1255 ACTION_HIDE_IN
)) : 0;
1260 cmd
= parse_action_cmd(action
, NULL
, ctree
, mimeview
);
1262 if ((child_info
= fork_child(cmd
, action_type
, text
,
1265 children_list
= g_slist_append(children_list
,
1267 children
->open_in
= action_type
&
1273 is_ok
= FALSE
; /* ERR: incorrect command */
1276 if (!children_list
) {
1277 /* If not waiting for children, return */
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
,
1293 catch_status
, child_info
);
1296 create_io_dialog(children
);
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];
1307 gint start
, end
, is_selection
;
1310 ChildInfo
*child_info
;
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;
1319 if (pipe(chld_status
) || pipe(chld_in
) || pipe(chld_out
) ||
1321 alertpanel_error(_("Command could not be started. "
1322 "Pipe creation failed.\n%s"),
1324 /* Closing fd = -1 fails silently */
1331 close(chld_status
[0]);
1332 close(chld_status
[1]);
1333 return NULL
; /* Pipe error */
1337 debug_print("Forking child and grandchild.\n");
1340 if (pid
== 0) { /* Child */
1344 close(ConnectionNumber(gdk_display
));
1349 if (setpgid(0, getppid()))
1356 close(fileno(stdin
));
1362 close(fileno(stdout
));
1367 close(fileno(stderr
));
1377 execvp("/bin/sh", cmdline
);
1381 } else if (gch_pid
< (pid_t
) 0) { /* Fork error */
1383 write(chld_status
[1], "1\n", 2);
1394 close(chld_status
[0]);
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]);
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
));
1415 waitpid(pid
, NULL
, 0);
1420 if (!(action_type
& (ACTION_PIPE_IN
| ACTION_OPEN_IN
| ACTION_HIDE_IN
)))
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
=
1437 (ACTION_PIPE_IN
| ACTION_OPEN_IN
| ACTION_HIDE_IN
))
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
)))
1451 child_info
->text
= text
;
1452 child_info
->msgfont
= msgfont
;
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
;
1466 is_selection
= TRUE
;
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
)))
1480 child_info
->chld_in
= -1; /* No more input */
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
));
1495 static void kill_children_cb(GtkWidget
*widget
, gpointer data
)
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)
1509 static gint
wait_for_children(gpointer data
)
1511 gboolean new_output
;
1512 Children
*children
= (Children
*)data
;
1513 ChildInfo
*child_info
;
1515 gint nb
= children
->nb
;
1519 cur
= children
->list
;
1522 child_info
= (ChildInfo
*)cur
->data
;
1523 if (child_info
->pid
)
1525 new_output
|= child_info
->new_out
;
1529 children
->output
|= new_output
;
1531 if (new_output
|| (children
->dialog
&& (nb
!= children
->nb
)))
1532 update_io_dialog(children
);
1537 if (!children
->dialog
) {
1538 free_children(children
);
1539 } else if (!children
->output
) {
1540 gtk_widget_destroy(children
->dialog
);
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
,
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
);
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
),
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
)
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
);
1604 cur
= children
->list
;
1609 static void update_io_dialog(Children
*children
)
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
;
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"),
1639 caption
= g_strdup_printf
1640 (_("--- Ended: %s\n"),
1643 gtk_text_insert(GTK_TEXT(text
), NULL
, NULL
, NULL
,
1645 gtk_text_insert(GTK_TEXT(text
), NULL
, NULL
, NULL
,
1646 child_info
->output
->str
, -1);
1648 child_info
->new_out
= FALSE
;
1650 gtk_text_thaw(GTK_TEXT(text
));
1654 static void create_io_dialog(Children
*children
)
1658 GtkWidget
*entry
= NULL
;
1659 GtkWidget
*input_hbox
= NULL
;
1660 GtkWidget
*send_button
;
1663 GtkWidget
*scrolledwin
;
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
),
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
,
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
);
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
;
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
;
1780 debug_print("Sending input to grand child.\n");
1781 if (!(cond
&& GDK_INPUT_WRITE
))
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
),
1789 c
= write(child_info
->chld_in
, input
, strlen(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
;
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
));
1815 c
= read(source
, buf
, PREFSBUFSIZE
- 1);
1818 gtk_stext_insert(GTK_STEXT(text
), child_info
->msgfont
,
1819 NULL
, NULL
, buf
, c
);
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
));
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
]);
1836 child_info
->new_out
= TRUE
;
1838 wait_for_children(child_info
->children
);