2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 Hiroyuki Yamamoto & The Sylpheed Claws Team
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>
35 #include "prefs_gtk.h"
39 #include "manage_window.h"
40 #include "mainwindow.h"
41 #include "prefs_common.h"
42 #include "alertpanel.h"
43 #include "prefs_actions.h"
45 #include "description_window.h"
53 GtkWidget *name_entry;
56 GtkWidget *actions_clist;
59 /* widget creating functions */
60 static void prefs_actions_create (MainWindow *mainwin);
61 static void prefs_actions_set_dialog (void);
62 static gint prefs_actions_clist_set_row (gint row);
64 /* callback functions */
65 static void prefs_actions_help_cb (GtkWidget *w,
67 static void prefs_actions_register_cb (GtkWidget *w,
69 static void prefs_actions_substitute_cb (GtkWidget *w,
71 static void prefs_actions_delete_cb (GtkWidget *w,
73 static void prefs_actions_up (GtkWidget *w,
75 static void prefs_actions_down (GtkWidget *w,
77 static void prefs_actions_select (GtkCList *clist,
81 static void prefs_actions_row_move (GtkCList *clist,
84 static gint prefs_actions_deleted (GtkWidget *widget,
87 static void prefs_actions_key_pressed (GtkWidget *widget,
90 static void prefs_actions_cancel (GtkWidget *w,
92 static void prefs_actions_ok (GtkWidget *w,
96 void prefs_actions_open(MainWindow *mainwin)
101 prefs_actions_create(mainwin);
103 manage_window_set_transient(GTK_WINDOW(actions.window));
104 gtk_widget_grab_focus(actions.ok_btn);
106 prefs_actions_set_dialog();
108 gtk_widget_show(actions.window);
111 static void prefs_actions_create(MainWindow *mainwin)
116 GtkWidget *cancel_btn;
117 GtkWidget *confirm_area;
121 GtkWidget *entry_vbox;
123 GtkWidget *name_label;
124 GtkWidget *name_entry;
125 GtkWidget *cmd_label;
126 GtkWidget *cmd_entry;
132 GtkWidget *subst_btn;
135 GtkWidget *cond_hbox;
136 GtkWidget *cond_scrolledwin;
137 GtkWidget *cond_clist;
139 GtkWidget *help_button;
147 debug_print("Creating actions configuration window...\n");
149 window = gtk_window_new (GTK_WINDOW_DIALOG);
151 gtk_container_set_border_width(GTK_CONTAINER (window), 8);
152 gtk_window_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
153 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
154 gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, TRUE);
155 gtk_window_set_default_size(GTK_WINDOW(window), 400, -1);
157 vbox = gtk_vbox_new(FALSE, 6);
158 gtk_widget_show(vbox);
159 gtk_container_add(GTK_CONTAINER(window), vbox);
161 gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
162 &cancel_btn, _("Cancel"), NULL, NULL);
163 gtk_widget_show(confirm_area);
164 gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
165 gtk_widget_grab_default(ok_btn);
167 gtk_window_set_title(GTK_WINDOW(window), _("Actions configuration"));
168 gtk_signal_connect(GTK_OBJECT(window), "delete_event",
169 GTK_SIGNAL_FUNC(prefs_actions_deleted), NULL);
170 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
171 GTK_SIGNAL_FUNC(prefs_actions_key_pressed), NULL);
172 MANAGE_WINDOW_SIGNALS_CONNECT(window);
173 gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
174 GTK_SIGNAL_FUNC(prefs_actions_ok), mainwin);
175 gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
176 GTK_SIGNAL_FUNC(prefs_actions_cancel), NULL);
178 vbox1 = gtk_vbox_new(FALSE, 8);
179 gtk_widget_show(vbox1);
180 gtk_box_pack_start(GTK_BOX(vbox), vbox1, TRUE, TRUE, 0);
181 gtk_container_set_border_width(GTK_CONTAINER(vbox1), 2);
183 entry_vbox = gtk_vbox_new(FALSE, 4);
184 gtk_box_pack_start(GTK_BOX(vbox1), entry_vbox, FALSE, FALSE, 0);
186 hbox = gtk_hbox_new(FALSE, 8);
187 gtk_box_pack_start(GTK_BOX(entry_vbox), hbox, FALSE, FALSE, 0);
189 name_label = gtk_label_new(_("Menu name:"));
190 gtk_box_pack_start(GTK_BOX(hbox), name_label, FALSE, FALSE, 0);
192 name_entry = gtk_entry_new();
193 gtk_box_pack_start(GTK_BOX(hbox), name_entry, TRUE, TRUE, 0);
195 hbox = gtk_hbox_new(FALSE, 8);
196 gtk_box_pack_start(GTK_BOX(entry_vbox), hbox, TRUE, TRUE, 0);
198 cmd_label = gtk_label_new(_("Command line:"));
199 gtk_box_pack_start(GTK_BOX(hbox), cmd_label, FALSE, FALSE, 0);
201 cmd_entry = gtk_entry_new();
202 gtk_box_pack_start(GTK_BOX(hbox), cmd_entry, TRUE, TRUE, 0);
204 gtk_widget_show_all(entry_vbox);
206 /* register / substitute / delete */
208 reg_hbox = gtk_hbox_new(FALSE, 4);
209 gtk_widget_show(reg_hbox);
210 gtk_box_pack_start(GTK_BOX(vbox1), reg_hbox, FALSE, FALSE, 0);
212 arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
213 gtk_widget_show(arrow);
214 gtk_box_pack_start(GTK_BOX(reg_hbox), arrow, FALSE, FALSE, 0);
215 gtk_widget_set_usize(arrow, -1, 16);
217 btn_hbox = gtk_hbox_new(TRUE, 4);
218 gtk_widget_show(btn_hbox);
219 gtk_box_pack_start(GTK_BOX(reg_hbox), btn_hbox, FALSE, FALSE, 0);
221 reg_btn = gtk_button_new_with_label(_("Add"));
222 gtk_widget_show(reg_btn);
223 gtk_box_pack_start(GTK_BOX(btn_hbox), reg_btn, FALSE, TRUE, 0);
224 gtk_signal_connect(GTK_OBJECT(reg_btn), "clicked",
225 GTK_SIGNAL_FUNC(prefs_actions_register_cb), NULL);
227 subst_btn = gtk_button_new_with_label(_(" Replace "));
228 gtk_widget_show(subst_btn);
229 gtk_box_pack_start(GTK_BOX(btn_hbox), subst_btn, FALSE, TRUE, 0);
230 gtk_signal_connect(GTK_OBJECT(subst_btn), "clicked",
231 GTK_SIGNAL_FUNC(prefs_actions_substitute_cb),
234 del_btn = gtk_button_new_with_label(_("Delete"));
235 gtk_widget_show(del_btn);
236 gtk_box_pack_start(GTK_BOX(btn_hbox), del_btn, FALSE, TRUE, 0);
237 gtk_signal_connect(GTK_OBJECT(del_btn), "clicked",
238 GTK_SIGNAL_FUNC(prefs_actions_delete_cb), NULL);
240 help_button = gtk_button_new_with_label(_(" Syntax help "));
241 gtk_widget_show(help_button);
242 gtk_box_pack_end(GTK_BOX(reg_hbox), help_button, FALSE, FALSE, 0);
243 gtk_signal_connect(GTK_OBJECT(help_button), "clicked",
244 GTK_SIGNAL_FUNC(prefs_actions_help_cb), NULL);
246 cond_hbox = gtk_hbox_new(FALSE, 8);
247 gtk_widget_show(cond_hbox);
248 gtk_box_pack_start(GTK_BOX(vbox1), cond_hbox, TRUE, TRUE, 0);
250 cond_scrolledwin = gtk_scrolled_window_new(NULL, NULL);
251 gtk_widget_show(cond_scrolledwin);
252 gtk_widget_set_usize(cond_scrolledwin, -1, 150);
253 gtk_box_pack_start(GTK_BOX(cond_hbox), cond_scrolledwin,
255 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (cond_scrolledwin),
256 GTK_POLICY_AUTOMATIC,
257 GTK_POLICY_AUTOMATIC);
259 title[0] = _("Current actions");
260 cond_clist = gtk_clist_new_with_titles(1, title);
261 gtk_widget_show(cond_clist);
262 gtk_container_add(GTK_CONTAINER (cond_scrolledwin), cond_clist);
263 gtk_clist_set_column_width(GTK_CLIST (cond_clist), 0, 80);
264 gtk_clist_set_selection_mode(GTK_CLIST (cond_clist),
265 GTK_SELECTION_BROWSE);
266 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(cond_clist)->column[0].button,
268 gtk_signal_connect(GTK_OBJECT(cond_clist), "select_row",
269 GTK_SIGNAL_FUNC(prefs_actions_select), NULL);
270 gtk_signal_connect_after(GTK_OBJECT(cond_clist), "row_move",
271 GTK_SIGNAL_FUNC(prefs_actions_row_move),
274 btn_vbox = gtk_vbox_new(FALSE, 8);
275 gtk_widget_show(btn_vbox);
276 gtk_box_pack_start(GTK_BOX(cond_hbox), btn_vbox, FALSE, FALSE, 0);
278 up_btn = gtk_button_new_with_label(_("Up"));
279 gtk_widget_show(up_btn);
280 gtk_box_pack_start(GTK_BOX(btn_vbox), up_btn, FALSE, FALSE, 0);
281 gtk_signal_connect(GTK_OBJECT(up_btn), "clicked",
282 GTK_SIGNAL_FUNC(prefs_actions_up), NULL);
284 down_btn = gtk_button_new_with_label(_("Down"));
285 gtk_widget_show(down_btn);
286 gtk_box_pack_start(GTK_BOX(btn_vbox), down_btn, FALSE, FALSE, 0);
287 gtk_signal_connect(GTK_OBJECT(down_btn), "clicked",
288 GTK_SIGNAL_FUNC(prefs_actions_down), NULL);
290 gtk_widget_show(window);
292 actions.window = window;
293 actions.ok_btn = ok_btn;
295 actions.name_entry = name_entry;
296 actions.cmd_entry = cmd_entry;
298 actions.actions_clist = cond_clist;
302 void prefs_actions_read_config(void)
306 gchar buf[PREFSBUFSIZE];
309 debug_print("Reading actions configurations...\n");
311 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
312 if ((fp = fopen(rcpath, "rb")) == NULL) {
313 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
319 while (prefs_common.actions_list != NULL) {
320 act = (gchar *)prefs_common.actions_list->data;
321 prefs_common.actions_list =
322 g_slist_remove(prefs_common.actions_list, act);
326 while (fgets(buf, sizeof(buf), fp) != NULL) {
328 act = strstr(buf, ": ");
330 action_get_type(&act[2]) != ACTION_ERROR)
331 prefs_common.actions_list =
332 g_slist_append(prefs_common.actions_list,
338 void prefs_actions_write_config(void)
344 debug_print("Writing actions configuration...\n");
346 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
347 if ((pfile= prefs_write_open(rcpath)) == NULL) {
348 g_warning("failed to write configuration to file\n");
353 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
354 gchar *act = (gchar *)cur->data;
355 if (fputs(act, pfile->fp) == EOF ||
356 fputc('\n', pfile->fp) == EOF) {
357 FILE_OP_ERROR(rcpath, "fputs || fputc");
358 prefs_file_close_revert(pfile);
366 if (prefs_file_close(pfile) < 0) {
367 g_warning("failed to write configuration to file\n");
372 static void prefs_actions_set_dialog(void)
374 GtkCList *clist = GTK_CLIST(actions.actions_clist);
376 gchar *action_str[1];
379 gtk_clist_freeze(clist);
380 gtk_clist_clear(clist);
382 action_str[0] = _("(New)");
383 row = gtk_clist_append(clist, action_str);
384 gtk_clist_set_row_data(clist, row, NULL);
386 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
389 action[0] = (gchar *)cur->data;
390 row = gtk_clist_append(clist, action);
391 gtk_clist_set_row_data(clist, row, action[0]);
394 gtk_clist_thaw(clist);
397 static void prefs_actions_set_list(void)
402 g_slist_free(prefs_common.actions_list);
403 prefs_common.actions_list = NULL;
405 while ((action = (gchar *)gtk_clist_get_row_data
406 (GTK_CLIST(actions.actions_clist), row)) != NULL) {
407 prefs_common.actions_list =
408 g_slist_append(prefs_common.actions_list, action);
413 #define GET_ENTRY(entry) \
414 entry_text = gtk_entry_get_text(GTK_ENTRY(entry))
416 static gint prefs_actions_clist_set_row(gint row)
418 GtkCList *clist = GTK_CLIST(actions.actions_clist);
421 gchar action[PREFSBUFSIZE];
424 g_return_val_if_fail(row != 0, -1);
426 GET_ENTRY(actions.name_entry);
427 if (entry_text[0] == '\0') {
428 alertpanel_error(_("Menu name is not set."));
432 if (strchr(entry_text, ':')) {
433 alertpanel_error(_("Colon ':' is not allowed in the menu name."));
437 strncpy(action, entry_text, PREFSBUFSIZE - 1);
440 /* Keep space for the ': ' delimiter */
441 len = strlen(action) + 2;
442 if (len >= PREFSBUFSIZE - 1) {
443 alertpanel_error(_("Menu name is too long."));
447 strcat(action, ": ");
449 GET_ENTRY(actions.cmd_entry);
451 if (entry_text[0] == '\0') {
452 alertpanel_error(_("Command line not set."));
456 if (len + strlen(entry_text) >= PREFSBUFSIZE - 1) {
457 alertpanel_error(_("Menu name and command are too long."));
461 if (action_get_type(entry_text) == ACTION_ERROR) {
462 alertpanel_error(_("The command\n%s\nhas a syntax error."),
467 strcat(action, entry_text);
471 row = gtk_clist_append(clist, buf);
474 gtk_clist_set_text(clist, row, 0, action);
475 old_action = (gchar *) gtk_clist_get_row_data(clist, row);
480 buf[0] = g_strdup(action);
482 gtk_clist_set_row_data(clist, row, buf[0]);
484 prefs_actions_set_list();
489 /* callback functions */
491 static void prefs_actions_register_cb(GtkWidget *w, gpointer data)
493 prefs_actions_clist_set_row(-1);
496 static void prefs_actions_substitute_cb(GtkWidget *w, gpointer data)
498 GtkCList *clist = GTK_CLIST(actions.actions_clist);
502 if (!clist->selection) return;
504 row = GPOINTER_TO_INT(clist->selection->data);
505 if (row == 0) return;
507 action = gtk_clist_get_row_data(clist, row);
510 prefs_actions_clist_set_row(row);
513 static void prefs_actions_delete_cb(GtkWidget *w, gpointer data)
515 GtkCList *clist = GTK_CLIST(actions.actions_clist);
519 if (!clist->selection) return;
520 row = GPOINTER_TO_INT(clist->selection->data);
521 if (row == 0) return;
523 if (alertpanel(_("Delete action"),
524 _("Do you really want to delete this action?"),
525 _("Yes"), _("No"), NULL) == G_ALERTALTERNATE)
528 action = gtk_clist_get_row_data(clist, row);
530 gtk_clist_remove(clist, row);
531 prefs_common.actions_list = g_slist_remove(prefs_common.actions_list,
535 static void prefs_actions_up(GtkWidget *w, gpointer data)
537 GtkCList *clist = GTK_CLIST(actions.actions_clist);
540 if (!clist->selection) return;
542 row = GPOINTER_TO_INT(clist->selection->data);
544 gtk_clist_row_move(clist, row, row - 1);
547 static void prefs_actions_down(GtkWidget *w, gpointer data)
549 GtkCList *clist = GTK_CLIST(actions.actions_clist);
552 if (!clist->selection) return;
554 row = GPOINTER_TO_INT(clist->selection->data);
555 if (row > 0 && row < clist->rows - 1)
556 gtk_clist_row_move(clist, row, row + 1);
559 #define ENTRY_SET_TEXT(entry, str) \
560 gtk_entry_set_text(GTK_ENTRY(entry), str ? str : "")
562 static void prefs_actions_select(GtkCList *clist, gint row, gint column,
567 gchar buf[PREFSBUFSIZE];
568 action = gtk_clist_get_row_data(clist, row);
571 ENTRY_SET_TEXT(actions.name_entry, "");
572 ENTRY_SET_TEXT(actions.cmd_entry, "");
576 strncpy(buf, action, PREFSBUFSIZE - 1);
577 buf[PREFSBUFSIZE - 1] = 0x00;
578 cmd = strstr(buf, ": ");
581 ENTRY_SET_TEXT(actions.cmd_entry, &cmd[2]);
586 ENTRY_SET_TEXT(actions.name_entry, buf);
589 static void prefs_actions_row_move(GtkCList *clist,
590 gint source_row, gint dest_row)
592 prefs_actions_set_list();
593 if (gtk_clist_row_is_visible(clist, dest_row) != GTK_VISIBILITY_FULL) {
594 gtk_clist_moveto(clist, dest_row, -1,
595 source_row < dest_row ? 1.0 : 0.0, 0.0);
599 static gint prefs_actions_deleted(GtkWidget *widget, GdkEventAny *event,
602 prefs_actions_cancel(widget, data);
606 static void prefs_actions_key_pressed(GtkWidget *widget, GdkEventKey *event,
609 if (event && event->keyval == GDK_Escape)
610 prefs_actions_cancel(widget, data);
613 static void prefs_actions_cancel(GtkWidget *w, gpointer data)
615 prefs_actions_read_config();
616 gtk_widget_hide(actions.window);
620 static void prefs_actions_ok(GtkWidget *widget, gpointer data)
622 MainWindow *mainwin = (MainWindow *) data;
625 MessageView *msgview;
628 prefs_actions_write_config();
630 /* Update mainwindow actions menu */
631 main_window_update_actions_menu(mainwin);
633 /* Update separated message view actions menu */
634 list = messageview_get_msgview_list();
635 for (iter = list; iter; iter = iter->next) {
636 msgview = (MessageView *) iter->data;
637 messageview_update_actions_menu(msgview);
640 /* Update compose windows actions menu */
641 list = compose_get_compose_list();
642 for (iter = list; iter; iter = iter->next) {
643 compose = (Compose *) iter->data;
644 compose_update_actions_menu(compose);
647 gtk_widget_hide(actions.window);
652 * Strings describing action format strings
654 * When adding new lines, remember to put one string for each line
656 static gchar *actions_desc_strings[] = {
657 N_("MENU NAME:"), NULL,
658 " ", N_("Use / in menu name to make submenus."),
660 N_("COMMAND LINE:"), NULL,
661 N_("Begin with:"), NULL,
662 " |", N_("to send message body or selection to command's standard input"),
663 " >", N_("to send user provided text to command's standard input"),
664 " *", N_("to send user provided hidden text to command's standard input"),
665 N_("End with:"), NULL,
666 " |", N_("to replace message body or selection with command's standard output"),
667 " >", N_("to insert command's standard output without replacing old text"),
668 " &", N_("to run command asynchronously"),
670 " %f", N_("for the file of the selected message in RFC822/2822 format "),
671 " %F", N_("for the list of the files of the selected messages in RFC822/2822 format"),
672 " %p", N_("for the file of the selected decoded message MIME part"),
673 " %u", N_("for a user provided argument"),
674 " %h", N_("for a user provided hidden argument (e.g. password)"),
675 " %s", N_("for the text selection"),
676 " %as{}", N_("apply filtering actions between {} to selected messages"),
681 static DescriptionWindow actions_desc_win = {
684 N_("Description of symbols"),
689 static void prefs_actions_help_cb(GtkWidget *w, gpointer data)
691 description_window_create(&actions_desc_win);