82fe167cfd9bda589a3145b1cf6c95666d6aa4cd
[claws.git] / src / plugins / attachwarner / attachwarner_prefs.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail Team
4  * Copyright (C) 2006-2012 Ricardo Mones
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #include "claws-features.h"
24 #endif
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28
29 #include "attachwarner.h"
30
31 #include "defs.h"
32 #include "attachwarner_prefs.h"
33 #include "prefs_common.h"
34 #include "prefs_gtk.h"
35
36 #define PREFS_BLOCK_NAME "AttachWarner"
37
38 AttachWarnerPrefs attwarnerprefs;
39
40 struct AttachWarnerPrefsPage
41 {
42         PrefsPage page;
43         
44         GtkWidget *regexp_text;
45         GtkWidget *skip_quotes_checkbox;
46         GtkWidget *skip_forwards_and_redirections;
47         GtkWidget *skip_signature;
48         GtkWidget *case_sensitive_checkbox;
49 };
50
51 struct AttachWarnerPrefsPage attwarnerprefs_page;
52
53 static PrefParam param[] = {
54         {"match_strings", N_("attach"), &attwarnerprefs.match_strings, P_STRING,
55          NULL, NULL, NULL},
56         {"skip_quotes", "TRUE", &attwarnerprefs.skip_quotes, P_BOOL,
57          NULL, NULL, NULL},
58         {"skip_forwards_and_redirections", "TRUE", &attwarnerprefs.skip_forwards_and_redirections, P_BOOL,
59          NULL, NULL, NULL},
60         {"skip_signature", "TRUE", &attwarnerprefs.skip_signature, P_BOOL,
61          NULL, NULL, NULL},
62         {"case_sensitive", "TRUE", &attwarnerprefs.case_sensitive, P_BOOL,
63          NULL, NULL, NULL},
64         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
65 };
66
67 static void attwarner_prefs_create_widget_func(PrefsPage * _page,
68                                            GtkWindow * window,
69                                            gpointer data)
70 {
71         struct AttachWarnerPrefsPage *page = (struct AttachWarnerPrefsPage *) _page;
72         GtkWidget *vbox, *hbox;;
73         GtkWidget *label;
74         GtkWidget *scrolledwin;
75         GtkTextBuffer *buffer;
76         GtkWidget *skip_quotes_checkbox;
77         GtkWidget *skip_fwd_redir_checkbox;
78         GtkWidget *skip_signature_checkbox;
79         GtkWidget *case_sensitive_checkbox;
80
81         vbox = gtk_vbox_new(FALSE, 6);
82         hbox = gtk_hbox_new(FALSE, 6);
83         
84         label = gtk_label_new(_("Warn when matching the following regular expressions:\n(one per line)"));
85         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
86         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 6);
87
88         case_sensitive_checkbox = gtk_check_button_new_with_label(_("Case sensitive"));
89         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(case_sensitive_checkbox),
90                  attwarnerprefs.case_sensitive);
91         gtk_box_pack_start(GTK_BOX(vbox), case_sensitive_checkbox, FALSE, FALSE, 0);
92         gtk_widget_show(case_sensitive_checkbox);
93
94         CLAWS_SET_TIP(case_sensitive_checkbox,
95                         _("Case sensitive when matching for the regular expressions in the list"));
96         page->case_sensitive_checkbox = case_sensitive_checkbox;
97
98         page->regexp_text = gtk_text_view_new();
99         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(page->regexp_text));
100         gtk_text_buffer_set_text(buffer, attwarnerprefs.match_strings, -1);
101         
102         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
103         gtk_scrolled_window_set_policy
104                 (GTK_SCROLLED_WINDOW (scrolledwin),
105                  GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
106         gtk_scrolled_window_set_shadow_type
107                 (GTK_SCROLLED_WINDOW (scrolledwin), GTK_SHADOW_IN);
108
109         gtk_container_add(GTK_CONTAINER(scrolledwin), page->regexp_text);
110         gtk_widget_set_size_request(page->regexp_text, -1, 100);
111         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, FALSE, FALSE, 0);
112         
113         skip_quotes_checkbox = gtk_check_button_new_with_label(_("Skip quoted lines"));
114         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(skip_quotes_checkbox),
115                  attwarnerprefs.skip_quotes);
116         gtk_box_pack_start(GTK_BOX(vbox), skip_quotes_checkbox, FALSE, FALSE, 0);
117         gtk_widget_show(skip_quotes_checkbox);
118
119         CLAWS_SET_TIP(skip_quotes_checkbox,
120                         _("Exclude quoted lines from checking for the regular expressions above"));
121         page->skip_quotes_checkbox = skip_quotes_checkbox;
122         
123         skip_fwd_redir_checkbox = gtk_check_button_new_with_label(_("Skip forwards and redirections"));
124         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(skip_fwd_redir_checkbox),
125                  attwarnerprefs.skip_forwards_and_redirections);
126         gtk_box_pack_start(GTK_BOX(vbox), skip_fwd_redir_checkbox, FALSE, FALSE, 0);
127         gtk_widget_show(skip_fwd_redir_checkbox);
128
129         CLAWS_SET_TIP(skip_fwd_redir_checkbox,
130                         _("Don't check for missing attachments when forwarding or redirecting messages"));
131         page->skip_forwards_and_redirections = skip_fwd_redir_checkbox;
132
133         skip_signature_checkbox = gtk_check_button_new_with_label(_("Skip signature"));
134         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(skip_signature_checkbox),
135                  attwarnerprefs.skip_signature);
136         gtk_box_pack_start(GTK_BOX(vbox), skip_signature_checkbox, FALSE, FALSE, 0);
137         gtk_widget_show(skip_signature_checkbox);
138
139         CLAWS_SET_TIP(skip_signature_checkbox,
140                         _("Exclude lines from the first signature-separator onwards from checking for the regular expressions above"));
141         page->skip_signature = skip_signature_checkbox;
142         
143         gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 6);
144         gtk_widget_show_all(hbox);
145         
146         page->page.widget = hbox;
147 }
148
149 static void attwarner_prefs_destroy_widget_func(PrefsPage *_page)
150 {
151 }
152
153 static void attwarner_save_config(void)
154 {
155         PrefFile *pfile;
156         gchar *rcpath;
157
158         debug_print("Saving AttachWarner Page\n");
159
160         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
161         pfile = prefs_write_open(rcpath);
162         g_free(rcpath);
163         if (!pfile || (prefs_set_block_label(pfile, PREFS_BLOCK_NAME) < 0))
164                 return;
165
166         if (prefs_write_param(param, pfile->fp) < 0) {
167                 g_warning("Failed to write AttachWarner configuration to file\n");
168                 prefs_file_close_revert(pfile);
169                 return;
170         }
171         if (fprintf(pfile->fp, "\n") < 0) {
172                 FILE_OP_ERROR(rcpath, "fprintf");
173                 prefs_file_close_revert(pfile);
174         } else
175                 prefs_file_close(pfile);
176 }
177
178
179 static void attwarner_prefs_save_func(PrefsPage * _page)
180 {
181         struct AttachWarnerPrefsPage *page = (struct AttachWarnerPrefsPage *) _page;
182         GtkTextBuffer *buffer;
183         GtkTextIter start, end;
184         gchar *tmp;
185         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(page->regexp_text));
186         
187         g_free(attwarnerprefs.match_strings);
188         
189         gtk_text_buffer_get_start_iter(buffer, &start);
190         gtk_text_buffer_get_end_iter(buffer, &end);
191         
192         tmp = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
193         
194         attwarnerprefs.match_strings = g_malloc(2*strlen(tmp)+1);
195         pref_get_escaped_pref(attwarnerprefs.match_strings, tmp);
196
197         attwarnerprefs.skip_quotes = gtk_toggle_button_get_active
198                         (GTK_TOGGLE_BUTTON(page->skip_quotes_checkbox));
199         attwarnerprefs.skip_forwards_and_redirections = gtk_toggle_button_get_active
200                         (GTK_TOGGLE_BUTTON(page->skip_forwards_and_redirections));
201         attwarnerprefs.skip_signature = gtk_toggle_button_get_active
202                         (GTK_TOGGLE_BUTTON(page->skip_signature));
203         attwarnerprefs.case_sensitive = gtk_toggle_button_get_active
204                         (GTK_TOGGLE_BUTTON(page->case_sensitive_checkbox));
205
206         attwarner_save_config();
207         g_free(attwarnerprefs.match_strings);
208         attwarnerprefs.match_strings = tmp;
209 }
210
211 void attachwarner_prefs_init(void)
212 {
213         static gchar *path[3];
214         gchar *rcpath;
215         gchar *tmp;
216         
217         path[0] = _("Plugins");
218         path[1] = _("Attach Warner");
219         path[2] = NULL;
220
221         prefs_set_default(param);
222         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
223         prefs_read_config(param, PREFS_BLOCK_NAME, rcpath, NULL);
224         g_free(rcpath);
225
226         tmp = g_malloc(strlen(attwarnerprefs.match_strings)+1);
227         pref_get_unescaped_pref(tmp, attwarnerprefs.match_strings);
228         
229         g_free(attwarnerprefs.match_strings);
230         attwarnerprefs.match_strings = tmp;
231         
232         attwarnerprefs_page.page.path = path;
233         attwarnerprefs_page.page.create_widget = attwarner_prefs_create_widget_func;
234         attwarnerprefs_page.page.destroy_widget = attwarner_prefs_destroy_widget_func;
235         attwarnerprefs_page.page.save_page = attwarner_prefs_save_func;
236
237         prefs_gtk_register_page((PrefsPage *) &attwarnerprefs_page);
238 }
239
240 void attachwarner_prefs_done(void)
241 {
242         prefs_gtk_unregister_page((PrefsPage *) &attwarnerprefs_page);
243 }