AttachWarner: rearrange checkbox, standardise labels
[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-2015 Hiroyuki Yamamoto and the Claws Mail Team
4  * Copyright (C) 2006-2015 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, *vbox1, *vbox2;
73         GtkWidget *frame;
74         GtkWidget *label;
75         GtkWidget *scrolledwin;
76         GtkTextBuffer *buffer;
77         GtkWidget *skip_quotes_checkbox;
78         GtkWidget *skip_fwd_redir_checkbox;
79         GtkWidget *skip_signature_checkbox;
80         GtkWidget *case_sensitive_checkbox;
81
82         vbox1 = gtk_vbox_new(FALSE, 6);
83         vbox2 = gtk_vbox_new(FALSE, 6);
84         
85         label = gtk_label_new(_("One of the following regular expressions is matched (one per line)"));
86         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
87         gtk_misc_set_padding(GTK_MISC(label), 2, 0);
88
89         case_sensitive_checkbox = gtk_check_button_new_with_label(_("Expressions are case sensitive"));
90         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(case_sensitive_checkbox),
91                  attwarnerprefs.case_sensitive);
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         gtk_container_set_border_width(GTK_CONTAINER(scrolledwin), 3);
109
110         gtk_container_add(GTK_CONTAINER(scrolledwin), page->regexp_text);
111         gtk_widget_set_size_request(page->regexp_text, -1, 100);
112
113         gtk_box_pack_start(GTK_BOX(vbox1), label, FALSE, FALSE, 0);
114         gtk_box_pack_start(GTK_BOX(vbox1), scrolledwin, FALSE, FALSE, 0);
115         gtk_box_pack_start(GTK_BOX(vbox1), case_sensitive_checkbox, FALSE, FALSE, 0);
116         
117         skip_quotes_checkbox = gtk_check_button_new_with_label(_("Lines starting with quotation marks"));
118         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(skip_quotes_checkbox),
119                  attwarnerprefs.skip_quotes);
120         gtk_box_pack_start(GTK_BOX(vbox2), skip_quotes_checkbox, FALSE, FALSE, 0);
121         gtk_widget_show(skip_quotes_checkbox);
122
123         CLAWS_SET_TIP(skip_quotes_checkbox,
124                         _("Exclude quoted lines from checking for the regular expressions above. Note that handmade quotes cannot be distinguished from quotes generated by replying."));
125         page->skip_quotes_checkbox = skip_quotes_checkbox;
126         
127         skip_fwd_redir_checkbox = gtk_check_button_new_with_label(_("Forwarded or redirected messages"));
128         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(skip_fwd_redir_checkbox),
129                  attwarnerprefs.skip_forwards_and_redirections);
130         gtk_box_pack_start(GTK_BOX(vbox2), skip_fwd_redir_checkbox, FALSE, FALSE, 0);
131         gtk_widget_show(skip_fwd_redir_checkbox);
132
133         CLAWS_SET_TIP(skip_fwd_redir_checkbox,
134                         _("Don't check for missing attachments when forwarding or redirecting messages"));
135         page->skip_forwards_and_redirections = skip_fwd_redir_checkbox;
136
137         skip_signature_checkbox = gtk_check_button_new_with_label(_("Signatures"));
138         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(skip_signature_checkbox),
139                  attwarnerprefs.skip_signature);
140         gtk_box_pack_start(GTK_BOX(vbox2), skip_signature_checkbox, FALSE, FALSE, 0);
141         gtk_widget_show(skip_signature_checkbox);
142
143         CLAWS_SET_TIP(skip_signature_checkbox,
144                         _("Exclude lines from the first signature-separator onwards from checking for the regular expressions above"));
145         page->skip_signature = skip_signature_checkbox;
146         
147         vbox = gtk_vbox_new(FALSE, VSPACING);
148         gtk_container_set_border_width (GTK_CONTAINER (vbox), VBOX_BORDER);
149
150         PACK_FRAME (vbox, frame, _("Warn when"));
151         gtk_container_set_border_width(GTK_CONTAINER(vbox1), 6);
152         gtk_container_add(GTK_CONTAINER(frame), vbox1);
153
154         PACK_FRAME (vbox, frame, _("Excluding"));
155         gtk_container_set_border_width(GTK_CONTAINER(vbox2), 6);
156         gtk_container_add(GTK_CONTAINER(frame), vbox2);
157
158         gtk_widget_show_all(vbox);
159         
160         page->page.widget = vbox;
161 }
162
163 static void attwarner_prefs_destroy_widget_func(PrefsPage *_page)
164 {
165 }
166
167 static void attwarner_save_config(void)
168 {
169         PrefFile *pfile;
170         gchar *rcpath;
171
172         debug_print("Saving AttachWarner Page\n");
173
174         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
175         pfile = prefs_write_open(rcpath);
176         g_free(rcpath);
177         if (!pfile || (prefs_set_block_label(pfile, PREFS_BLOCK_NAME) < 0))
178                 return;
179
180         if (prefs_write_param(param, pfile->fp) < 0) {
181                 g_warning("Failed to write AttachWarner configuration to file\n");
182                 prefs_file_close_revert(pfile);
183                 return;
184         }
185         if (fprintf(pfile->fp, "\n") < 0) {
186                 FILE_OP_ERROR(rcpath, "fprintf");
187                 prefs_file_close_revert(pfile);
188         } else
189                 prefs_file_close(pfile);
190 }
191
192
193 static void attwarner_prefs_save_func(PrefsPage * _page)
194 {
195         struct AttachWarnerPrefsPage *page = (struct AttachWarnerPrefsPage *) _page;
196         GtkTextBuffer *buffer;
197         GtkTextIter start, end;
198         gchar *tmp;
199         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(page->regexp_text));
200         
201         g_free(attwarnerprefs.match_strings);
202         
203         gtk_text_buffer_get_start_iter(buffer, &start);
204         gtk_text_buffer_get_end_iter(buffer, &end);
205         
206         tmp = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
207         
208         attwarnerprefs.match_strings = g_malloc(2*strlen(tmp)+1);
209         pref_get_escaped_pref(attwarnerprefs.match_strings, tmp);
210
211         attwarnerprefs.skip_quotes = gtk_toggle_button_get_active
212                         (GTK_TOGGLE_BUTTON(page->skip_quotes_checkbox));
213         attwarnerprefs.skip_forwards_and_redirections = gtk_toggle_button_get_active
214                         (GTK_TOGGLE_BUTTON(page->skip_forwards_and_redirections));
215         attwarnerprefs.skip_signature = gtk_toggle_button_get_active
216                         (GTK_TOGGLE_BUTTON(page->skip_signature));
217         attwarnerprefs.case_sensitive = gtk_toggle_button_get_active
218                         (GTK_TOGGLE_BUTTON(page->case_sensitive_checkbox));
219
220         attwarner_save_config();
221         g_free(attwarnerprefs.match_strings);
222         attwarnerprefs.match_strings = tmp;
223 }
224
225 void attachwarner_prefs_init(void)
226 {
227         static gchar *path[3];
228         gchar *rcpath;
229         gchar *tmp;
230         
231         path[0] = _("Plugins");
232         path[1] = _("Attach Warner");
233         path[2] = NULL;
234
235         prefs_set_default(param);
236         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
237         prefs_read_config(param, PREFS_BLOCK_NAME, rcpath, NULL);
238         g_free(rcpath);
239
240         tmp = g_malloc(strlen(attwarnerprefs.match_strings)+1);
241         pref_get_unescaped_pref(tmp, attwarnerprefs.match_strings);
242         
243         g_free(attwarnerprefs.match_strings);
244         attwarnerprefs.match_strings = tmp;
245         
246         attwarnerprefs_page.page.path = path;
247         attwarnerprefs_page.page.create_widget = attwarner_prefs_create_widget_func;
248         attwarnerprefs_page.page.destroy_widget = attwarner_prefs_destroy_widget_func;
249         attwarnerprefs_page.page.save_page = attwarner_prefs_save_func;
250
251         prefs_gtk_register_page((PrefsPage *) &attwarnerprefs_page);
252 }
253
254 void attachwarner_prefs_done(void)
255 {
256         prefs_gtk_unregister_page((PrefsPage *) &attwarnerprefs_page);
257 }