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 gboolean 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_TOPLEVEL);
151 gtk_container_set_border_width(GTK_CONTAINER (window), 8);
152 gtk_window_set_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 g_signal_connect(G_OBJECT(window), "delete_event",
169 G_CALLBACK(prefs_actions_deleted), NULL);
170 g_signal_connect(G_OBJECT(window), "key_press_event",
171 G_CALLBACK(prefs_actions_key_pressed), NULL);
172 MANAGE_WINDOW_SIGNALS_CONNECT(window);
173 g_signal_connect(G_OBJECT(ok_btn), "clicked",
174 G_CALLBACK(prefs_actions_ok), mainwin);
175 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
176 G_CALLBACK(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_size_request(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 g_signal_connect(G_OBJECT(reg_btn), "clicked",
225 G_CALLBACK(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 g_signal_connect(G_OBJECT(subst_btn), "clicked",
231 G_CALLBACK(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 g_signal_connect(G_OBJECT(del_btn), "clicked",
238 G_CALLBACK(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 g_signal_connect(G_OBJECT(help_button), "clicked",
244 G_CALLBACK(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_size_request(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 g_signal_connect(G_OBJECT(cond_clist), "select_row",
269 G_CALLBACK(prefs_actions_select), NULL);
270 g_signal_connect_after(G_OBJECT(cond_clist), "row_move",
271 G_CALLBACK(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 g_signal_connect(G_OBJECT(up_btn), "clicked",
282 G_CALLBACK(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 g_signal_connect(G_OBJECT(down_btn), "clicked",
288 G_CALLBACK(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) {
327 const gchar *src_codeset = conv_get_current_charset_str();
328 const gchar *dest_codeset = CS_UTF_8;
331 tmp = conv_codeset_strdup(buf, src_codeset, dest_codeset);
333 g_warning("Faild to convert character set of action configuration\n");
338 act = strstr(tmp, ": ");
340 action_get_type(&act[2]) != ACTION_ERROR)
341 prefs_common.actions_list =
342 g_slist_append(prefs_common.actions_list,
350 void prefs_actions_write_config(void)
356 debug_print("Writing actions configuration...\n");
358 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
359 if ((pfile= prefs_write_open(rcpath)) == NULL) {
360 g_warning("failed to write configuration to file\n");
365 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
366 gchar *tmp = (gchar *)cur->data;
367 const gchar *src_codeset = CS_UTF_8;
368 const gchar *dest_codeset = conv_get_current_charset_str();
371 act = conv_codeset_strdup(tmp, src_codeset, dest_codeset);
373 g_warning("Faild to convert character set of action configuration\n");
377 if (fputs(act, pfile->fp) == EOF ||
378 fputc('\n', pfile->fp) == EOF) {
379 FILE_OP_ERROR(rcpath, "fputs || fputc");
380 prefs_file_close_revert(pfile);
389 if (prefs_file_close(pfile) < 0) {
390 g_warning("failed to write configuration to file\n");
395 static void prefs_actions_set_dialog(void)
397 GtkCList *clist = GTK_CLIST(actions.actions_clist);
399 gchar *action_str[1];
402 gtk_clist_freeze(clist);
403 gtk_clist_clear(clist);
405 action_str[0] = _("(New)");
406 row = gtk_clist_append(clist, action_str);
407 gtk_clist_set_row_data(clist, row, NULL);
409 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
412 action[0] = (gchar *)cur->data;
413 row = gtk_clist_append(clist, action);
414 gtk_clist_set_row_data(clist, row, action[0]);
417 gtk_clist_thaw(clist);
420 static void prefs_actions_set_list(void)
425 g_slist_free(prefs_common.actions_list);
426 prefs_common.actions_list = NULL;
428 while ((action = (gchar *)gtk_clist_get_row_data
429 (GTK_CLIST(actions.actions_clist), row)) != NULL) {
430 prefs_common.actions_list =
431 g_slist_append(prefs_common.actions_list, action);
436 #define GET_ENTRY(entry) \
437 entry_text = gtk_entry_get_text(GTK_ENTRY(entry))
439 static gint prefs_actions_clist_set_row(gint row)
441 GtkCList *clist = GTK_CLIST(actions.actions_clist);
442 const gchar *entry_text;
444 gchar action[PREFSBUFSIZE];
447 g_return_val_if_fail(row != 0, -1);
449 GET_ENTRY(actions.name_entry);
450 if (entry_text[0] == '\0') {
451 alertpanel_error(_("Menu name is not set."));
455 if (strchr(entry_text, ':')) {
456 alertpanel_error(_("Colon ':' is not allowed in the menu name."));
460 strncpy(action, entry_text, PREFSBUFSIZE - 1);
463 /* Keep space for the ': ' delimiter */
464 len = strlen(action) + 2;
465 if (len >= PREFSBUFSIZE - 1) {
466 alertpanel_error(_("Menu name is too long."));
470 strcat(action, ": ");
472 GET_ENTRY(actions.cmd_entry);
474 if (entry_text[0] == '\0') {
475 alertpanel_error(_("Command line not set."));
479 if (len + strlen(entry_text) >= PREFSBUFSIZE - 1) {
480 alertpanel_error(_("Menu name and command are too long."));
484 if (action_get_type(entry_text) == ACTION_ERROR) {
485 alertpanel_error(_("The command\n%s\nhas a syntax error."),
490 strcat(action, entry_text);
494 row = gtk_clist_append(clist, buf);
497 gtk_clist_set_text(clist, row, 0, action);
498 old_action = (gchar *) gtk_clist_get_row_data(clist, row);
503 buf[0] = g_strdup(action);
505 gtk_clist_set_row_data(clist, row, buf[0]);
507 prefs_actions_set_list();
512 /* callback functions */
514 static void prefs_actions_register_cb(GtkWidget *w, gpointer data)
516 prefs_actions_clist_set_row(-1);
519 static void prefs_actions_substitute_cb(GtkWidget *w, gpointer data)
521 GtkCList *clist = GTK_CLIST(actions.actions_clist);
525 if (!clist->selection) return;
527 row = GPOINTER_TO_INT(clist->selection->data);
528 if (row == 0) return;
530 action = gtk_clist_get_row_data(clist, row);
533 prefs_actions_clist_set_row(row);
536 static void prefs_actions_delete_cb(GtkWidget *w, gpointer data)
538 GtkCList *clist = GTK_CLIST(actions.actions_clist);
542 if (!clist->selection) return;
543 row = GPOINTER_TO_INT(clist->selection->data);
544 if (row == 0) return;
546 if (alertpanel(_("Delete action"),
547 _("Do you really want to delete this action?"),
548 _("Yes"), _("No"), NULL) == G_ALERTALTERNATE)
551 action = gtk_clist_get_row_data(clist, row);
553 gtk_clist_remove(clist, row);
554 prefs_common.actions_list = g_slist_remove(prefs_common.actions_list,
558 static void prefs_actions_up(GtkWidget *w, gpointer data)
560 GtkCList *clist = GTK_CLIST(actions.actions_clist);
563 if (!clist->selection) return;
565 row = GPOINTER_TO_INT(clist->selection->data);
567 gtk_clist_row_move(clist, row, row - 1);
570 static void prefs_actions_down(GtkWidget *w, gpointer data)
572 GtkCList *clist = GTK_CLIST(actions.actions_clist);
575 if (!clist->selection) return;
577 row = GPOINTER_TO_INT(clist->selection->data);
578 if (row > 0 && row < clist->rows - 1)
579 gtk_clist_row_move(clist, row, row + 1);
582 #define ENTRY_SET_TEXT(entry, str) \
583 gtk_entry_set_text(GTK_ENTRY(entry), str ? str : "")
585 static void prefs_actions_select(GtkCList *clist, gint row, gint column,
590 gchar buf[PREFSBUFSIZE];
591 action = gtk_clist_get_row_data(clist, row);
594 ENTRY_SET_TEXT(actions.name_entry, "");
595 ENTRY_SET_TEXT(actions.cmd_entry, "");
599 strncpy(buf, action, PREFSBUFSIZE - 1);
600 buf[PREFSBUFSIZE - 1] = 0x00;
601 cmd = strstr(buf, ": ");
604 ENTRY_SET_TEXT(actions.cmd_entry, &cmd[2]);
609 ENTRY_SET_TEXT(actions.name_entry, buf);
612 static void prefs_actions_row_move(GtkCList *clist,
613 gint source_row, gint dest_row)
615 prefs_actions_set_list();
616 if (gtk_clist_row_is_visible(clist, dest_row) != GTK_VISIBILITY_FULL) {
617 gtk_clist_moveto(clist, dest_row, -1,
618 source_row < dest_row ? 1.0 : 0.0, 0.0);
622 static gint prefs_actions_deleted(GtkWidget *widget, GdkEventAny *event,
625 prefs_actions_cancel(widget, data);
629 static gboolean prefs_actions_key_pressed(GtkWidget *widget, GdkEventKey *event,
632 if (event && event->keyval == GDK_Escape)
633 prefs_actions_cancel(widget, data);
637 static void prefs_actions_cancel(GtkWidget *w, gpointer data)
639 prefs_actions_read_config();
640 gtk_widget_hide(actions.window);
644 static void prefs_actions_ok(GtkWidget *widget, gpointer data)
646 MainWindow *mainwin = (MainWindow *) data;
649 MessageView *msgview;
652 prefs_actions_write_config();
654 /* Update mainwindow actions menu */
655 main_window_update_actions_menu(mainwin);
657 /* Update separated message view actions menu */
658 list = messageview_get_msgview_list();
659 for (iter = list; iter; iter = iter->next) {
660 msgview = (MessageView *) iter->data;
661 messageview_update_actions_menu(msgview);
664 /* Update compose windows actions menu */
665 list = compose_get_compose_list();
666 for (iter = list; iter; iter = iter->next) {
667 compose = (Compose *) iter->data;
668 compose_update_actions_menu(compose);
671 gtk_widget_hide(actions.window);
676 * Strings describing action format strings
678 * When adding new lines, remember to put one string for each line
680 static gchar *actions_desc_strings[] = {
681 N_("MENU NAME:"), NULL,
682 " ", N_("Use / in menu name to make submenus."),
684 N_("COMMAND LINE:"), NULL,
685 N_("Begin with:"), NULL,
686 " |", N_("to send message body or selection to command's standard input"),
687 " >", N_("to send user provided text to command's standard input"),
688 " *", N_("to send user provided hidden text to command's standard input"),
689 N_("End with:"), NULL,
690 " |", N_("to replace message body or selection with command's standard output"),
691 " >", N_("to insert command's standard output without replacing old text"),
692 " &", N_("to run command asynchronously"),
694 " %f", N_("for the file of the selected message in RFC822/2822 format "),
695 " %F", N_("for the list of the files of the selected messages in RFC822/2822 format"),
696 " %p", N_("for the file of the selected decoded message MIME part"),
697 " %u", N_("for a user provided argument"),
698 " %h", N_("for a user provided hidden argument (e.g. password)"),
699 " %s", N_("for the text selection"),
700 " %as{}", N_("apply filtering actions between {} to selected messages"),
705 static DescriptionWindow actions_desc_win = {
708 N_("Description of symbols"),
713 static void prefs_actions_help_cb(GtkWidget *w, gpointer data)
715 description_window_create(&actions_desc_win);