20ec4d0cb7da3988707b86046f1f61960ea39893
[claws.git] / src / prefs_actions.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 Hiroyuki Yamamoto
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
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>
38
39 #include "intl.h"
40 #include "prefs.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
54 #define WAIT_LAP 10000
55                                                 
56 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_ERROR            = 1 << 8,
66 } ActionsTypes;
67
68 static struct Actions {
69         GtkWidget *window;
70
71         GtkWidget *ok_btn;
72
73         GtkWidget *name_entry;
74         GtkWidget *cmd_entry;
75
76         GtkWidget *actions_clist;
77 } actions;
78
79 typedef struct _Children Children;
80 struct _Children {
81         GtkWidget       *window;
82         GtkWidget       *dialog;
83         GtkWidget       *text;
84         GtkWidget       *input_entry;
85         GtkWidget       *input_hbox;
86         GtkWidget       *abort_btn;
87         GtkWidget       *hide_btn;
88         GtkWidget       *scrolledwin;
89
90         gchar           *action;
91         guint            timer;
92         GSList          *list;
93         gint             nb;
94         gint             open_in;
95         gboolean         output;
96 };
97
98 typedef struct _ChildInfo ChildInfo;
99 struct _ChildInfo {
100         Children        *children;
101         gchar           *cmd;
102         guint            type;
103         pid_t            pid;
104         gint             chld_in;
105         gint             chld_out;
106         gint             chld_err;
107         gint             chld_status;
108         gint             tag_in;
109         gint             tag_out;
110         gint             tag_err;
111         gint             tag_status;
112         gint             new_out;
113         GString         *output;
114         GtkWidget       *text;
115 };
116
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);
121
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,
130                                          gint            row,
131                                          gint            column,
132                                          GdkEvent       *event);
133 static void prefs_actions_row_move      (GtkCList       *clist,
134                                          gint            source_row,
135                                          gint            dest_row);
136 static gint prefs_actions_deleted       (GtkWidget      *widget,
137                                          GdkEventAny    *event,
138                                          gpointer       *data);
139 static void prefs_actions_key_pressed   (GtkWidget      *widget,
140                                          GdkEventKey    *event,
141                                          gpointer        data);
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,
145                                          gchar *branch_path,
146                                          gpointer callback,
147                                          gpointer data);
148 static void mainwin_actions_execute_cb  (MainWindow     *mainwin,
149                                          guint           action_nb,
150                                          GtkWidget      *widget);
151 static void compose_actions_execute_cb  (Compose        *compose,
152                                          guint           action_nb,
153                                          GtkWidget      *widget);
154 static guint get_action_type            (gchar *action);
155
156 static gboolean execute_actions         (gchar     *action, 
157                                          GtkWidget *window,
158                                          GtkCTree  *ctree, 
159                                          GtkWidget *text,
160                                          MimeView  *mimeview);
161
162 static gchar *parse_action_cmd          (gchar    *action,
163                                          MsgInfo  *msginfo,
164                                          GtkCTree *ctree,
165                                          MimeView *mimeview);
166 static GString *parse_append_filename   (GString *cmd,
167                                          MsgInfo *msginfo);
168
169 static GString *parse_append_msgpart    (GString *cmd, 
170                                          MsgInfo *msginfo,
171                                          MimeView *mimeview);
172
173 ChildInfo *fork_child                   (gchar     *cmd,
174                                          gint       action_type,
175                                          GtkWidget *text,
176                                          Children  *children);
177
178 static gint wait_for_children           (gpointer data);
179
180 static void free_children               (Children *children);
181
182 static void childinfo_close_pipes       (ChildInfo *child_info);
183
184 static void create_io_dialog            (Children *children);
185
186 static void update_io_dialog            (Children *children);
187
188 static void hide_io_dialog_cb           (GtkWidget *widget,
189                                          gpointer data);
190
191 static void catch_output                (gpointer data,
192                                          gint source,
193                                          GdkInputCondition cond);
194 static void catch_input                 (gpointer data, 
195                                          gint source,
196                                          GdkInputCondition cond);
197 static void catch_status                (gpointer data,
198                                          gint source,
199                                          GdkInputCondition cond);
200
201 void prefs_actions_open(MainWindow *mainwin)
202 {
203         if (prefs_rc_is_readonly(ACTIONS_RC))
204                 return;
205         inc_lock();
206
207         if (!actions.window)
208                 prefs_actions_create(mainwin);
209         
210         manage_window_set_transient(GTK_WINDOW(actions.window));
211         gtk_widget_grab_focus(actions.ok_btn);
212
213         prefs_actions_set_dialog();
214
215         gtk_widget_show(actions.window);
216 }
217
218 static void prefs_actions_create(MainWindow *mainwin)
219 {
220         GtkWidget *window;
221         GtkWidget *vbox;
222         GtkWidget *ok_btn;
223         GtkWidget *cancel_btn;
224         GtkWidget *confirm_area;
225
226         GtkWidget *vbox1;
227         GtkWidget *hbox;
228         GtkWidget *name_label;
229         GtkWidget *name_entry;
230         GtkWidget *cmd_label;
231         GtkWidget *cmd_entry;
232
233         GtkWidget *reg_hbox;
234         GtkWidget *btn_hbox;
235         GtkWidget *arrow;
236         GtkWidget *reg_btn;
237         GtkWidget *subst_btn;
238         GtkWidget *del_btn;
239
240         GtkWidget *cond_hbox;
241         GtkWidget *cond_scrolledwin;
242         GtkWidget *cond_clist;
243
244         GtkWidget *help_vbox;
245         GtkWidget *help_label;
246         GtkWidget *help_toggle;
247
248         GtkWidget *btn_vbox;
249         GtkWidget *up_btn;
250         GtkWidget *down_btn;
251         
252         gchar *title[1];
253
254         debug_print(_("Creating actions setting window...\n"));
255
256         window = gtk_window_new (GTK_WINDOW_DIALOG);
257                 
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);
262         
263         vbox = gtk_vbox_new (FALSE, 6);
264         gtk_widget_show (vbox);
265         gtk_container_add (GTK_CONTAINER (window), vbox);
266
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);
272                                          
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);
284
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);
289
290         hbox = gtk_hbox_new (FALSE, 0);
291         gtk_box_pack_start (GTK_BOX(vbox1), hbox, TRUE, TRUE, 0);
292         
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);
296
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);
300
301         gtk_widget_show_all(hbox);
302
303         hbox = gtk_hbox_new (FALSE, 0);
304         gtk_box_pack_start (GTK_BOX(vbox1), hbox, TRUE, TRUE, 0);
305                  
306         cmd_label = gtk_label_new(_("Command line: "));
307         gtk_box_pack_start (GTK_BOX (hbox), cmd_label, FALSE, FALSE, 0);
308
309         cmd_entry = gtk_entry_new();
310         gtk_box_pack_start (GTK_BOX (hbox), cmd_entry, TRUE, TRUE, 0);
311
312         gtk_widget_show_all(hbox);
313
314         help_vbox = gtk_vbox_new(FALSE, 8);
315         gtk_box_pack_start (GTK_BOX (vbox1), help_vbox, TRUE, TRUE, 0);
316
317         help_label = gtk_label_new (_("Menu name:\n"
318                                       " Use / in menu name to make submenus.\n"
319                                       "Command line:\n"
320                                       " Begin with:\n   | to send message "
321                                       "body or selection "
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 "
327                                       "output\n   & to "
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 "
332                                       "part."));
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);
338
339                 /* register / substitute / delete */
340
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);
344
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);
349
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);
353
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);
359
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),
365                             NULL);
366
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);
372
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);
378
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);
382
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,
387                             TRUE, TRUE, 0);
388         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (cond_scrolledwin),
389                                         GTK_POLICY_AUTOMATIC,
390                                         GTK_POLICY_AUTOMATIC);
391
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,
400                                 GTK_CAN_FOCUS);
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),
405                                   NULL);
406
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);
410
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);
416
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);
422
423         gtk_widget_show(window);
424
425         actions.window = window;
426         actions.ok_btn = ok_btn;
427
428         actions.name_entry = name_entry;
429         actions.cmd_entry  = cmd_entry;
430         
431         actions.actions_clist = cond_clist;
432 }
433
434 static void prefs_actions_help_cb(GtkWidget *w, gpointer data)
435 {
436         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)))
437                 gtk_widget_show(GTK_WIDGET(data));
438         else
439                 gtk_widget_hide(GTK_WIDGET(data));
440 }
441
442 void prefs_actions_read_config()
443 {
444         gchar *rcpath;
445         FILE *fp;
446         gchar buf[PREFSBUFSIZE];
447         gchar *act;
448
449         debug_print(_("Reading actions configurations...\n"));
450
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");
454                 g_free(rcpath);
455                 return;
456         }
457         g_free(rcpath);
458
459         while (prefs_common.actionslst != NULL) {
460                 act = (gchar *) prefs_common.actionslst->data;
461                 prefs_common.actionslst = g_slist_remove(
462                                                 prefs_common.actionslst,
463                                                 act);
464                 g_free(act);
465         }
466         
467         while (fgets(buf, sizeof(buf), fp) != NULL) {
468                 g_strchomp(buf);
469                 act = strstr(buf, ": ");
470                 if (act && act[2] && 
471                     get_action_type(&act[2]) != ACTION_ERROR)
472                         prefs_common.actionslst = g_slist_append(
473                                                         prefs_common.actionslst,
474                                                         g_strdup(buf));
475         }
476         fclose(fp);
477 }
478
479 void prefs_actions_write_config()
480 {
481         gchar *rcpath;
482         PrefFile *pfile;
483         GSList *cur;
484
485         debug_print(_("Writing actions configuration...\n"));
486
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"));
490                 g_free(rcpath);
491                 return;
492         }
493
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);
500                         g_free(rcpath);
501                         return;
502                 }
503         }
504         
505         g_free(rcpath);
506
507         if (prefs_write_close(pfile) < 0) {
508                 g_warning(_("failed to write configuration to file\n"));
509                 return;
510         }
511 }
512
513 static guint get_action_type(gchar *action)
514 {
515         gchar *p;
516         guint action_type = ACTION_NONE;
517         
518         g_return_val_if_fail(action,  ACTION_ERROR);
519         g_return_val_if_fail(*action, ACTION_ERROR);
520
521         p = action;
522
523         if (p[0] == '|') {
524                 action_type |= ACTION_PIPE_IN;
525                 p++;
526         } else if (p[0] == '>') {
527                 action_type |= ACTION_OPEN_IN;
528                 p++;
529         } else if (p[0] == '*') {
530                 action_type |= ACTION_HIDE_IN;
531                 p++;
532         }
533
534         if (p[0] == 0x00)
535                 return ACTION_ERROR;
536
537         while (*p && action_type != ACTION_ERROR) {
538                 if (p[0] == '%') {
539                         switch (p[1]) {
540                                 case 'f': action_type |= ACTION_SINGLE;
541                                           break;
542                                 case 'F': action_type |= ACTION_MULTIPLE;
543                                           break;
544                                 case 'p': action_type |= ACTION_SINGLE;
545                                           break;
546                                 default:  action_type  = ACTION_ERROR;
547                                           break;
548                         }
549                 } else if (p[0] == '|') {
550                         if (p[1] == 0x00)
551                                 action_type |= ACTION_PIPE_OUT;
552                         else
553                                 action_type  = ACTION_ERROR;
554                 } else if (p[0] == '&') {
555                         if (p[1] == 0x00)
556                                 action_type |= ACTION_ASYNC;
557                         else
558                                 action_type  = ACTION_ERROR;
559                 }
560                 p++;
561         }
562
563         return action_type;
564 }
565
566 static gchar *parse_action_cmd          (gchar *action,
567                                          MsgInfo *msginfo,
568                                          GtkCTree *ctree,
569                                          MimeView *mimeview)
570 {
571         GString *cmd, *tmpcmd;
572         gchar *p;
573         GList *cur;
574         MsgInfo *msg;
575         
576         p = action;
577         
578         if (p[0] == '|' || p[0] == '>' || p[0] == '*')
579                 p++;
580
581         cmd = g_string_sized_new(strlen(action));
582
583         while (p[0] && p[0] != '|' && p[0] != '&') {
584                 if (p[0] == '%' && p[1]) {
585                         switch (p[1]) {
586                            case 'f': cmd = parse_append_filename(cmd, msginfo);
587                                      p++;       
588                                      break;
589                            case 'F':
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, ' ');
596                                      }
597                                      p++;
598                                      break;
599                            case 'p':
600                                      tmpcmd = parse_append_msgpart(cmd,
601                                                                    msginfo, 
602                                                                    mimeview);
603                                      if (tmpcmd)
604                                              cmd = tmpcmd;
605                                      else {
606                                              g_string_free(cmd, TRUE);
607                                              return NULL;
608                                      }
609                                              
610                                      p++;
611                                      break;
612                            default: cmd = g_string_append_c(cmd, p[0]);
613                                     cmd = g_string_append_c(cmd, p[1]);
614                                     p++;
615                         }
616                 } else {
617                         cmd = g_string_append_c(cmd, p[0]);
618                 }
619                 p++;
620         }
621         if (!cmd->len) {
622                 g_string_free(cmd, TRUE);
623                 return NULL;
624         }
625
626         p = cmd->str;
627         g_string_free(cmd, FALSE);
628         return p;
629 }
630
631 static GString *parse_append_filename(GString *cmd, MsgInfo *msginfo)
632 {
633         gchar *filename;
634
635         g_return_val_if_fail(msginfo, cmd);
636
637         filename = procmsg_get_message_file(msginfo);
638
639         if (filename) {
640                 cmd = g_string_append(cmd, filename);
641                 g_free(filename);
642         } 
643
644         return cmd;
645 }
646
647 static GString *parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
648                                      MimeView *mimeview)
649 {
650         gchar    *filename;
651         gchar    *partname;
652         MimeInfo *partinfo;
653         gint      ret;
654         FILE     *fp;
655         
656         if (!mimeview) {
657 #if USE_GPGME
658                 if ((fp = procmsg_open_message_decrypted(msginfo, &partinfo))
659                     == NULL) {
660                         alertpanel_error(_("Could not get message file."));
661                         return NULL;
662                 }
663 #else
664                 if ((fp = procmsg_open_message(msginfo)) == NULL)
665                 {
666                         alertpanel_error(_("Could not get message file."));
667                         return NULL;
668                 }
669                 partinfo = procmime_scan_mime_header(fp);
670 #endif
671                 fclose(fp);
672                 if (!partinfo) {
673                         procmime_mimeinfo_free(partinfo);
674                         alertpanel_error(_("Could not get message part."));
675                         return NULL;
676                 }
677                 filename = procmsg_get_message_file(msginfo);
678         } else {
679                 if (!mimeview->opened) {
680                         alertpanel_error(_("No message part selected."));
681                         return NULL;
682                 }
683                 if (!mimeview->file) {
684                         alertpanel_error(_("No message file selected."));
685                         return NULL;
686                 }
687                 partinfo = gtk_ctree_node_get_row_data
688                                 (GTK_CTREE(mimeview->ctree),
689                                  mimeview->opened);
690                 g_return_val_if_fail(partinfo != NULL, cmd);
691                 filename = mimeview->file;
692         }
693         partname = procmime_get_tmp_file_name(partinfo);
694
695         ret = procmime_get_part(partname, filename, partinfo); 
696
697         if (!mimeview) {
698                 procmime_mimeinfo_free(partinfo);
699                 g_free(filename);
700         }
701
702         if (ret < 0) {
703                 alertpanel_error(_("Can't get part of multipart message"));
704                 g_free(partname);
705                 return NULL;
706         }
707         cmd = g_string_append_c(cmd, '"');
708         cmd = g_string_append(cmd,partname);
709         cmd = g_string_append_c(cmd, '"');
710         
711         g_free(partname);
712         
713         return cmd;
714         
715 }
716
717 static void prefs_actions_set_dialog    (void)
718 {
719         GtkCList *clist = GTK_CLIST(actions.actions_clist);
720         GSList *cur;
721         gchar *action_str[1];
722         gint row;
723
724         gtk_clist_freeze(clist);
725         gtk_clist_clear(clist);
726
727         action_str[0] = _("(New)");
728         row = gtk_clist_append(clist, action_str);
729         gtk_clist_set_row_data(clist, row, NULL);
730
731         for (cur = prefs_common.actionslst; cur != NULL; cur = cur->next) {
732                 gchar *action[1];
733                 action[0] = (gchar *) cur->data;
734
735                 row = gtk_clist_append(clist, action);
736                 gtk_clist_set_row_data(clist, row, action[0]);
737         }
738         
739         gtk_clist_thaw(clist);
740 }
741 static void prefs_actions_set_list(void)
742 {
743         gint row = 1;
744         gchar *action;
745         
746         g_slist_free(prefs_common.actionslst);
747         prefs_common.actionslst = NULL;
748
749         while ( (action = (gchar *) gtk_clist_get_row_data(GTK_CLIST(
750                                                  actions.actions_clist), row))
751                 != NULL) {
752                 prefs_common.actionslst = g_slist_append(prefs_common.actionslst,
753                                                          action);
754                 row++;
755         }
756 }
757                 
758
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)
762 {
763         GtkCList *clist = GTK_CLIST(actions.actions_clist);
764         gchar *entry_text;
765         gint len;
766         gchar action[PREFSBUFSIZE];
767         gchar *buf[1];
768         
769         
770         g_return_val_if_fail(row != 0, -1);
771
772         
773
774         GET_ENTRY(actions.name_entry);
775         if (entry_text[0] == '\0') {
776                 alertpanel_error(_("Menu name is not set."));
777                 return -1;
778         }
779
780         if (strchr(entry_text, ':')) {
781                 alertpanel_error(_("Colon ':' is not allowed in the menu name."));
782                 return -1;
783         }
784         
785         strncpy(action, entry_text, PREFSBUFSIZE - 1);
786         g_strstrip(action);
787
788         /* Keep space for the ': ' delimiter */
789         len = strlen(action) + 2;
790         if (len >= PREFSBUFSIZE - 1) {
791                 alertpanel_error(_("Menu name is too long."));
792                 return -1;
793         }
794
795         strcat(action, ": ");
796
797         GET_ENTRY(actions.cmd_entry);
798
799         if (entry_text[0] == '\0') {
800                 alertpanel_error(_("Command line not set."));
801                 return -1;
802         }
803
804         if (len + strlen(entry_text) >= PREFSBUFSIZE - 1) {
805                 alertpanel_error(_("Menu name and command are too long."));
806                 return -1;
807         }
808
809         if (get_action_type(entry_text) == ACTION_ERROR) {
810                 alertpanel_error(_("The command\n%s\nhas a syntax error."), 
811                                  entry_text);
812                 return -1;
813         }
814         
815         strcat(action, entry_text);
816
817         buf[0] = action;
818         if (row < 0)
819                 row = gtk_clist_append(clist, buf);
820         else {
821                 gchar *old_action;
822                 gtk_clist_set_text(clist, row, 0, action);
823                 old_action = (gchar *) gtk_clist_get_row_data(clist, row);
824                 if (old_action)
825                         g_free(old_action);
826         }
827
828         buf[0] = g_strdup(action);
829         
830         gtk_clist_set_row_data(clist, row, buf[0]);
831
832         prefs_actions_set_list();
833
834         return 0;
835 }
836         
837 /* callback functions */
838 static void prefs_actions_register_cb   (GtkWidget *w, gpointer data)
839 {
840         prefs_actions_clist_set_row(-1);
841 }
842
843 static void prefs_actions_substitute_cb (GtkWidget *w, gpointer data)
844 {
845         GtkCList *clist = GTK_CLIST(actions.actions_clist);
846         gchar *action;
847         gint row;
848
849         if (!clist->selection) return;
850
851         row = GPOINTER_TO_INT(clist->selection->data);
852         if (row == 0) return;
853
854         action = gtk_clist_get_row_data(clist, row);
855         if (!action) return;
856         
857         prefs_actions_clist_set_row(row);
858 }
859
860 static void prefs_actions_delete_cb     (GtkWidget *w, gpointer data)
861 {
862         GtkCList *clist = GTK_CLIST(actions.actions_clist);
863         gchar *action;
864         gint row;
865
866         if (!clist->selection) return;
867         row = GPOINTER_TO_INT(clist->selection->data);
868         if (row == 0) return;
869
870         if (alertpanel(_("Delete action"),
871                        _("Do you really want to delete this action?"),
872                        _("Yes"), _("No"), NULL) == G_ALERTALTERNATE)
873                 return;
874
875         action = gtk_clist_get_row_data(clist, row);
876         g_free(action);
877         gtk_clist_remove(clist, row);
878         prefs_common.actionslst = g_slist_remove(prefs_common.actionslst, action);
879 }
880
881 static void prefs_actions_up            (GtkWidget *w, gpointer data)
882 {
883         GtkCList *clist = GTK_CLIST(actions.actions_clist);
884         gint row;
885
886         if (!clist->selection) return;
887
888         row = GPOINTER_TO_INT(clist->selection->data);
889         if (row > 1)
890                 gtk_clist_row_move(clist, row, row - 1);
891 }
892
893 static void prefs_actions_down          (GtkWidget *w, gpointer data)
894 {
895         GtkCList *clist = GTK_CLIST(actions.actions_clist);
896         gint row;
897
898         if (!clist->selection) return;
899
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);
903 }
904
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,
908                                          gint            row,
909                                          gint            column,
910                                          GdkEvent       *event)
911 {
912         gchar *action;
913         gchar *cmd;
914         gchar buf[PREFSBUFSIZE];
915         action = gtk_clist_get_row_data(clist, row);
916
917         if (!action) {
918                 ENTRY_SET_TEXT(actions.name_entry, "");
919                 ENTRY_SET_TEXT(actions.cmd_entry, "");
920                 return;
921         }
922         
923         strncpy(buf, action, PREFSBUFSIZE - 1);
924         buf[PREFSBUFSIZE - 1] = 0x00;
925         cmd = strstr2(buf, ": ");
926
927         if (cmd && cmd[2])
928                 ENTRY_SET_TEXT(actions.cmd_entry, &cmd[2]);
929         else
930                 return;
931
932         *cmd = 0x00;
933         ENTRY_SET_TEXT(actions.name_entry, buf);
934 }
935         
936 static void prefs_actions_row_move      (GtkCList       *clist,
937                                          gint            source_row,
938                                          gint            dest_row)
939 {
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);
944         }
945 }
946
947 static gint prefs_actions_deleted       (GtkWidget      *widget,
948                                          GdkEventAny    *event,
949                                          gpointer       *data)
950 {
951         prefs_actions_cancel(widget, data);
952         return TRUE;
953 }
954
955 static void prefs_actions_key_pressed   (GtkWidget      *widget,
956                                          GdkEventKey    *event,
957                                          gpointer        data)
958 {
959         if (event && event->keyval == GDK_Escape)
960                 prefs_actions_cancel(widget, data);
961 }
962
963 static void prefs_actions_cancel        (GtkWidget *w, gpointer data)
964 {
965         prefs_actions_read_config();
966         gtk_widget_hide(actions.window);
967         inc_unlock();
968 }
969
970 static void prefs_actions_ok            (GtkWidget *widget, gpointer data)
971 {
972         GtkItemFactory *ifactory;
973         MainWindow *mainwin = (MainWindow *) data;
974
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);
979         inc_unlock();
980 }
981
982 void update_mainwin_actions_menu(GtkItemFactory *ifactory, 
983                                  MainWindow *mainwin)
984 {
985         update_actions_menu(ifactory, "/Tools/Actions", 
986                             mainwin_actions_execute_cb, 
987                             mainwin);
988 }
989
990 void update_compose_actions_menu(GtkItemFactory *ifactory, 
991                                  gchar *branch_path,
992                                  Compose *compose)
993 {
994         update_actions_menu(ifactory, branch_path, 
995                             compose_actions_execute_cb, 
996                             compose);
997 }
998                                  
999 static void update_actions_menu(GtkItemFactory *ifactory,
1000                                 gchar *branch_path,
1001                                 gpointer callback,
1002                                 gpointer data)
1003 {
1004         GtkWidget *menuitem;
1005         gchar *menu_path;
1006         GSList *cur;
1007         gchar *action, *action_p;
1008         GtkWidget *menu;
1009         
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));
1023                         amenu = alist;
1024                 }
1025         }
1026
1027         ifentry.accelerator     = NULL;
1028         ifentry.callback_action = 0;
1029         ifentry.callback        = callback;
1030         ifentry.item_type       = NULL;
1031
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) {
1037                         action_p[0] = 0x00;
1038                         menu_path = g_strdup_printf("%s/%s", branch_path, 
1039                                                     action);
1040                         ifentry.path = menu_path;
1041                         gtk_item_factory_create_item(ifactory, &ifentry, data,
1042                                                      1);
1043                         g_free(menu_path);
1044                 }
1045                 g_free(action);
1046                 ifentry.callback_action++;      
1047         }
1048 }
1049
1050 static void compose_actions_execute_cb  (Compose        *compose,
1051                                          guint           action_nb,
1052                                          GtkWidget      *widget)
1053 {
1054         gchar *buf, *action;
1055         guint action_type;      
1056
1057         g_return_if_fail(action_nb < g_slist_length(prefs_common.actionslst));
1058         
1059         buf = (gchar *) g_slist_nth_data(prefs_common.actionslst, action_nb);
1060         
1061         g_return_if_fail(buf);
1062         g_return_if_fail(action = strstr(buf, ": "));
1063
1064         /* Point to the beginning of the command-line */
1065         action++;
1066         action++;
1067         
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."));
1074                 return;
1075         }
1076
1077         execute_actions(action, compose->window, NULL, compose->text, NULL);
1078 }
1079
1080 static void mainwin_actions_execute_cb  (MainWindow     *mainwin,
1081                                          guint           action_nb,
1082                                          GtkWidget      *widget)
1083 {
1084         MessageView *messageview = mainwin->messageview;
1085         GtkWidget   *text = NULL;
1086         gchar       *buf,
1087                     *action;
1088         MimeView    *mimeview = NULL;
1089
1090         g_return_if_fail (action_nb < g_slist_length(prefs_common.actionslst));
1091
1092         buf = (gchar *) g_slist_nth_data(prefs_common.actionslst, action_nb);
1093         
1094         g_return_if_fail(buf);
1095         g_return_if_fail(action = strstr(buf, ": "));
1096
1097         /* Point to the beginning of the command-line */
1098         action++;
1099         action++;
1100
1101         switch (messageview->type) {
1102                 case MVIEW_TEXT:
1103                         if (messageview->textview && 
1104                             messageview->textview->text)
1105                                 text = messageview->textview->text;
1106                         break;
1107                 case MVIEW_MIME:
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;
1114                         }
1115                         break;
1116         }
1117
1118         execute_actions(action, mainwin->window,
1119                         GTK_CTREE(mainwin->summaryview->ctree), text, mimeview);
1120 }
1121
1122 static gboolean execute_actions(gchar     *action, 
1123                                 GtkWidget *window,
1124                                 GtkCTree  *ctree, 
1125                                 GtkWidget *text,
1126                                 MimeView  *mimeview)
1127 {
1128         GList *cur, *selection;
1129         GSList *children_list = NULL;
1130         gint is_ok  = TRUE;
1131         gint selection_len;
1132         Children *children;
1133         ChildInfo *child_info;
1134         gint action_type;
1135         MsgInfo *msginfo;
1136         gchar *cmd;
1137
1138         g_return_val_if_fail(action && *action, FALSE);
1139
1140         action_type = get_action_type(action);
1141
1142         if (action_type == ACTION_ERROR)
1143                 return FALSE;         /* ERR: syntax error */
1144
1145         if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && 
1146             !(ctree && GTK_CLIST(ctree)->selection))
1147                 return FALSE;         /* ERR: file command without selection */
1148
1149         if (ctree) {
1150                 selection = GTK_CLIST(ctree)->selection;
1151                 selection_len = g_list_length(selection);
1152         }
1153         
1154         if ((action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN))) {
1155                 if (ctree && selection_len > 1)
1156                         return FALSE; /* ERR: pipe + multiple selection */
1157                 if (!text)
1158                         return FALSE; /* ERR: pipe and no displayed text */
1159         }
1160
1161         if (!(action_type & ACTION_ASYNC) && window) {
1162                 gtk_widget_set_sensitive(window, FALSE);
1163         }
1164
1165         children = g_new0(Children, 1);
1166
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));
1171                         if (!msginfo) {
1172                                 is_ok  = FALSE; /* ERR: msginfo missing */
1173                                 break;
1174                         }
1175                         cmd = parse_action_cmd(action, msginfo, ctree,
1176                                                mimeview);
1177                         if (!cmd) {
1178                                 debug_print(_("Action command error\n"));
1179                                 is_ok  = FALSE; /* ERR: incorrect command */
1180                                 break;
1181                         }
1182                         if ((child_info = fork_child(cmd, action_type, text,
1183                                                      children))) {
1184                                 children_list = g_slist_append(children_list,
1185                                                                child_info);
1186                                 children->open_in = (selection_len == 1) ? 
1187                                                     (action_type & 
1188                                                      (ACTION_OPEN_IN |
1189                                                       ACTION_HIDE_IN)) : 0;
1190                         }
1191                         g_free(cmd);
1192                 }
1193         } else {
1194                 cmd = parse_action_cmd(action, NULL, ctree, mimeview);
1195                 if (cmd) {
1196                         if ((child_info = fork_child(cmd, action_type, text,
1197                                                      children))) {
1198                                 children_list = g_slist_append(children_list,
1199                                                                child_info);
1200                                 children->open_in = action_type &
1201                                                     (ACTION_OPEN_IN |
1202                                                      ACTION_HIDE_IN);
1203                         }
1204                         g_free(cmd);
1205                 } else
1206                         is_ok  = FALSE;         /* ERR: incorrect command */
1207         }
1208
1209         if (!children_list) {
1210                  /* If not waiting for children, return */
1211                 g_free(children);
1212                 if (!(action_type & ACTION_ASYNC) && window) {
1213                         gtk_widget_set_sensitive(window, TRUE);
1214                 }
1215         } else {
1216                 GSList *cur;
1217                 
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);
1223
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,
1228                                               GDK_INPUT_READ,
1229                                               catch_status, child_info);
1230                 }
1231                 children->timer = children->open_in ? 0 :
1232                                   gtk_timeout_add(WAIT_LAP, wait_for_children,
1233                                                   children);
1234         }
1235         if (children->open_in)
1236                 create_io_dialog(children);
1237
1238         return is_ok ;
1239 }
1240
1241 ChildInfo *fork_child(gchar *cmd,
1242                       gint action_type,
1243                       GtkWidget *text,
1244                       Children *children)
1245 {
1246         gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
1247         gchar **cmdline;
1248         gint start, end, is_selection;
1249         gchar *selection;
1250         pid_t pid_c, pid_gc;
1251         ChildInfo *child_info;
1252
1253         if (action_type & ACTION_ASYNC) {
1254                 execute_command_line(cmd, TRUE);
1255                 return NULL; /* Asynchronous command */
1256         }
1257
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 */
1263         }
1264         
1265         debug_print(_("Forking child and grandchild.\n"));
1266
1267         pid_c = fork();
1268         if (pid_c == (pid_t) 0) {/* Child */
1269                 if (setpgid(0, 0))
1270                         perror("setpgid");
1271
1272                 close(ConnectionNumber(gdk_display));
1273
1274                 pid_gc = fork();
1275
1276                 if (pid_gc == 0) {
1277                         if (setpgid(0, getppid()))
1278                                 perror("setpgid");
1279                         if (action_type & 
1280                            (ACTION_PIPE_IN | ACTION_OPEN_IN | ACTION_HIDE_IN)) {
1281                         close(fileno(stdin));
1282                         dup  (chld_in[0]);
1283                         }
1284                         close(chld_in[0]);
1285                         close(chld_in[1]);
1286
1287                         close(fileno(stdout));
1288                         dup  (chld_out[1]);
1289                         close(chld_out[0]);
1290                         close(chld_out[1]);
1291
1292                         close(fileno(stderr));
1293                         dup  (chld_err[1]);
1294                         close(chld_err[0]);
1295                         close(chld_err[1]);
1296
1297                         cmdline = strsplit_with_quote(cmd, " ", 1024);
1298
1299                         execvp(cmdline[0], cmdline);
1300                         perror("execvp");
1301                         g_strfreev(cmdline);
1302
1303                         _exit(1);
1304                 } else if (pid_gc < (pid_t) 0) {/* Fork erro */
1305                         write(chld_status[1], "1\n", 2);
1306                         perror("fork");
1307                         _exit(1);
1308                 } else {/* Child */
1309                         close(chld_in[0]);
1310                         close(chld_in[1]);
1311                         close(chld_out[0]);
1312                         close(chld_out[1]);
1313                         close(chld_err[0]);
1314                         close(chld_err[1]);
1315
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]);
1322                         _exit(0);
1323                 }
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));
1328                 return NULL; 
1329         }
1330         /* Parent */
1331
1332         close(chld_in[0]);
1333         if (!(action_type & (ACTION_PIPE_IN | ACTION_OPEN_IN | ACTION_HIDE_IN)))
1334                 close(chld_in[1]);
1335         close(chld_out[1]);
1336         close(chld_err[1]);
1337         close(chld_status[1]);
1338
1339         child_info = g_new0(ChildInfo, 1);
1340
1341         child_info->children = children;
1342         
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
1349                                 | ACTION_HIDE_IN))
1350                                ? chld_in [1] : -1;
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);
1359
1360         if (!(action_type & (ACTION_PIPE_IN | ACTION_PIPE_OUT)))
1361                 return child_info;
1362
1363         child_info->text     = text;
1364
1365         start = 0;
1366         end   = gtk_stext_get_length(GTK_STEXT(text));
1367
1368         if (GTK_EDITABLE(text)->has_selection) {
1369                 start = GTK_EDITABLE(text)->selection_start_pos;
1370                 end   = GTK_EDITABLE(text)->selection_end_pos;
1371                 if (start > end) {
1372                         gint tmp;
1373                         tmp = start;
1374                         start = end;
1375                         end = tmp;
1376                 }
1377                 is_selection = TRUE;
1378                 if (start == end) {
1379                         start = 0;
1380                         end   = gtk_stext_get_length(GTK_STEXT(text));
1381                         is_selection = FALSE;
1382                 }
1383         }
1384
1385         selection = gtk_editable_get_chars(GTK_EDITABLE(text), start, end);
1386
1387
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)))
1391                         close(chld_in[1]);
1392                 child_info->chld_in = -1; /* No more input */
1393         }
1394         g_free(selection);
1395
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);
1400         }
1401
1402         gtk_stext_thaw(GTK_STEXT(text));
1403         
1404         return child_info;
1405 }
1406         
1407 static void kill_children_cb(GtkWidget *widget, gpointer data)
1408 {
1409         GSList *cur;
1410         Children *children = (Children *) data;
1411         ChildInfo *child_info;
1412
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))
1417                                 perror("kill");
1418         }
1419 }
1420
1421 static gint wait_for_children(gpointer data)
1422 {
1423         gboolean new_output;
1424         Children *children = (Children *) data;
1425         ChildInfo *child_info;
1426         GSList *cur;
1427         gint nb = children->nb;
1428
1429         children->nb = 0;
1430                 
1431         cur = children->list;
1432         new_output = FALSE;
1433         while (cur) {
1434                 child_info = (ChildInfo *) cur->data;
1435                 if (child_info->pid)
1436                         children->nb++;
1437                 new_output |= child_info->new_out;
1438                 cur = cur->next;
1439         }
1440
1441         if (!children->dialog && 
1442             (new_output || children->timer))
1443                 create_io_dialog(children);
1444         
1445         if (children->timer) {
1446                 gtk_timeout_remove(children->timer);
1447                 children->timer = 0;
1448         }
1449         children->output |= new_output;
1450
1451         if (new_output || (children->dialog && (nb != children->nb)))
1452                 update_io_dialog(children);
1453
1454         if (children->nb)
1455                 return FALSE;
1456
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);
1462         }
1463         return FALSE;
1464 }
1465
1466 static void send_input(GtkWidget *w, gpointer data)
1467 {
1468         Children *children = (Children *) data;
1469         ChildInfo *child_info = (ChildInfo *) children->list->data;
1470         
1471         child_info->tag_in = gdk_input_add(child_info->chld_in,
1472                                            GDK_INPUT_WRITE,
1473                                            catch_input, children);
1474         gtk_widget_set_sensitive(children->input_hbox, FALSE);
1475 }
1476
1477 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1478 {
1479         hide_io_dialog_cb(w, data);
1480         return TRUE;
1481 }
1482
1483 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1484 {
1485
1486         Children *children = (Children *) data;
1487
1488         if (!children->nb) {
1489                 gtk_widget_set_sensitive(children->window, TRUE);
1490                 gtk_signal_disconnect_by_data(GTK_OBJECT(children->dialog),
1491                                               children);
1492                 gtk_widget_destroy(children->dialog);
1493                 free_children(children);
1494         }
1495 }
1496
1497 static void childinfo_close_pipes(ChildInfo *child_info)
1498 {
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);
1503
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);
1509 }
1510
1511 static void free_children(Children *children)
1512 {
1513         GSList *cur;
1514         ChildInfo *child_info;
1515
1516         debug_print(_("Freeing children data %x\n"), (guint) children);
1517                                 
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);
1524                 g_free(child_info);
1525                 cur = children->list;
1526         }
1527         g_free(children);
1528 }       
1529
1530 static void update_io_dialog(Children *children)
1531 {
1532         GSList *cur;
1533
1534         debug_print(_("Updating actions input/output dialog.\n"));
1535
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);
1542         }
1543
1544         if (children->output) {
1545                 GtkWidget *text = children->text;
1546                 gchar *caption;
1547                 ChildInfo *child_info;
1548
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)
1555                 {
1556                         child_info = (ChildInfo *) cur->data;
1557                         if (child_info->pid)
1558                                 caption = g_strdup_printf(_("--- Running: %s\n")
1559                                                     , child_info->cmd);
1560                         else
1561                                 caption = g_strdup_printf(_("--- Ended: %s\n")
1562                                                     , child_info->cmd);
1563                         
1564                         gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1565                                          caption, -1);
1566                         gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1567                                          child_info->output->str, -1);
1568                         g_free(caption);
1569                         child_info->new_out = FALSE;
1570                 }
1571                 gtk_text_thaw(GTK_TEXT(text));
1572         }
1573 }
1574         
1575         
1576 static void create_io_dialog(Children *children)
1577 {
1578         GtkWidget *dialog;
1579         GtkWidget *entry;
1580         GtkWidget *input_hbox;
1581         GtkWidget *send_button;
1582         GtkWidget *label;
1583         GtkWidget *text;
1584         GtkWidget *scrolledwin;
1585         GtkWidget *hbox;
1586         GtkWidget *abort_button;
1587         GtkWidget *hide_button;
1588         
1589         debug_print(_("Creating actions dialog\n"));
1590
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, 
1595                            FALSE, 0);
1596
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),
1602                         children);
1603
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,
1608                            TRUE, 0);
1609         gtk_widget_set_usize(scrolledwin, 600, 200);
1610         gtk_widget_hide(scrolledwin);
1611
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);
1619
1620         if (children->open_in) {
1621                 input_hbox = gtk_hbox_new(FALSE, 8);
1622                 gtk_widget_show(input_hbox);
1623
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);
1631
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, 
1636                                    FALSE, 8);
1637                 gtk_widget_show(send_button);
1638
1639                 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), 
1640                                 input_hbox, FALSE, FALSE, 8);
1641                 gtk_widget_grab_focus(entry);
1642         }
1643
1644         hbox = gtk_hbox_new(TRUE, 0);
1645         gtk_widget_show(hbox);
1646
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);
1652
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);
1658
1659         if (children->nb)
1660                 gtk_widget_set_sensitive(hide_button, FALSE);
1661
1662         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 
1663                           hbox);
1664
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;
1672
1673         gtk_widget_show(dialog);
1674 }
1675
1676 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1677 {
1678         ChildInfo *child_info = (ChildInfo *) data;
1679         gchar buf;
1680         gint c;
1681
1682         gdk_input_remove(child_info->tag_status);
1683
1684         c = read(source, &buf, 1);
1685         debug_print(_("Child returned %c\n"), buf);
1686
1687         waitpid(-child_info->pid, NULL, 0);
1688         childinfo_close_pipes(child_info);
1689         child_info->pid = 0;
1690         
1691         wait_for_children(child_info->children);
1692 }
1693         
1694 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1695 {
1696         Children *children = (Children *) data;
1697         ChildInfo *child_info = (ChildInfo *) children->list->data;
1698         gchar *input;
1699         gint c;
1700
1701         debug_print(_("Sending input to grand child.\n"));
1702         if (!(cond && GDK_INPUT_WRITE))
1703                 return;
1704         
1705         gdk_input_remove(child_info->tag_in);
1706         child_info->tag_in = -1;
1707
1708         input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry), 0,
1709                                        -1);
1710         c = write(child_info->chld_in, input, strlen(input));
1711
1712         g_free(input);
1713
1714         write(child_info->chld_in, "\n", 2);
1715
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"));
1719 }
1720
1721 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1722 {
1723         ChildInfo *child_info = (ChildInfo *) data;
1724         gint c, i;
1725         gchar buf[PREFSBUFSIZE];
1726
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));
1735                 while (TRUE) {
1736                         c =read(source, buf, PREFSBUFSIZE - 1);
1737                         if (c == 0)
1738                                 break;
1739                         gtk_stext_insert(GTK_STEXT(text), NULL, NULL, NULL,
1740                                          buf, c);
1741                 }
1742                 if (is_selection) {
1743                         /* Using the select_region draws things. Should not.
1744                          * so we just change selection position and 
1745                          * defere drawing when thawing. Hack?
1746                          */
1747                         GTK_EDITABLE(text)->selection_end_pos = 
1748                                         gtk_stext_get_point(GTK_STEXT(text));
1749                 }
1750                 gtk_stext_thaw(GTK_STEXT(child_info->text));
1751         } else {
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]);
1756                 child_info->new_out = TRUE;
1757         }
1758 }