RSSyl: Allow use of .netrc by libcurl. Bug/enhancement #3309, by Vincent Pelletier
[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, *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_box_pack_start(GTK_BOX(vbox1), label, FALSE, FALSE, 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_box_pack_start(GTK_BOX(vbox1), case_sensitive_checkbox, FALSE, FALSE, 0);
93         gtk_widget_show(case_sensitive_checkbox);
94
95         CLAWS_SET_TIP(case_sensitive_checkbox,
96                         _("Case sensitive when matching for the regular expressions in the list"));
97         page->case_sensitive_checkbox = case_sensitive_checkbox;
98
99         page->regexp_text = gtk_text_view_new();
100         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(page->regexp_text));
101         gtk_text_buffer_set_text(buffer, attwarnerprefs.match_strings, -1);
102         
103         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
104         gtk_scrolled_window_set_policy
105                 (GTK_SCROLLED_WINDOW (scrolledwin),
106                  GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
107         gtk_scrolled_window_set_shadow_type
108                 (GTK_SCROLLED_WINDOW (scrolledwin), GTK_SHADOW_IN);
109
110         gtk_container_add(GTK_CONTAINER(scrolledwin), page->regexp_text);
111         gtk_widget_set_size_request(page->regexp_text, -1, 100);
112         gtk_box_pack_start(GTK_BOX(vbox1), scrolledwin, FALSE, FALSE, 0);
113         
114         skip_quotes_checkbox = gtk_check_button_new_with_label(_("Lines starting with quotation marks"));
115         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(skip_quotes_checkbox),
116                  attwarnerprefs.skip_quotes);
117         gtk_box_pack_start(GTK_BOX(vbox2), skip_quotes_checkbox, FALSE, FALSE, 0);
118         gtk_widget_show(skip_quotes_checkbox);
119
120         CLAWS_SET_TIP(skip_quotes_checkbox,
121                         _("Exclude quoted lines from checking for the regular expressions above. Note that handmade quotes cannot be distinguished from quotes generated by replying."));
122         page->skip_quotes_checkbox = skip_quotes_checkbox;
123         
124         skip_fwd_redir_checkbox = gtk_check_button_new_with_label(_("Forwarded or redirected messages"));
125         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(skip_fwd_redir_checkbox),
126                  attwarnerprefs.skip_forwards_and_redirections);
127         gtk_box_pack_start(GTK_BOX(vbox2), skip_fwd_redir_checkbox, FALSE, FALSE, 0);
128         gtk_widget_show(skip_fwd_redir_checkbox);
129
130         CLAWS_SET_TIP(skip_fwd_redir_checkbox,
131                         _("Don't check for missing attachments when forwarding or redirecting messages"));
132         page->skip_forwards_and_redirections = skip_fwd_redir_checkbox;
133
134         skip_signature_checkbox = gtk_check_button_new_with_label(_("Signatures"));
135         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(skip_signature_checkbox),
136                  attwarnerprefs.skip_signature);
137         gtk_box_pack_start(GTK_BOX(vbox2), skip_signature_checkbox, FALSE, FALSE, 0);
138         gtk_widget_show(skip_signature_checkbox);
139
140         CLAWS_SET_TIP(skip_signature_checkbox,
141                         _("Exclude lines from the first signature-separator onwards from checking for the regular expressions above"));
142         page->skip_signature = skip_signature_checkbox;
143         
144         vbox = gtk_vbox_new(FALSE, VSPACING);
145         gtk_container_set_border_width (GTK_CONTAINER (vbox), VBOX_BORDER);
146
147         PACK_FRAME (vbox, frame, _("Warn when"));
148         gtk_container_set_border_width(GTK_CONTAINER(vbox1), 6);
149         gtk_container_add(GTK_CONTAINER(frame), vbox1);
150
151         PACK_FRAME (vbox, frame, _("Excluding"));
152         gtk_container_set_border_width(GTK_CONTAINER(vbox2), 4);
153         gtk_container_add(GTK_CONTAINER(frame), vbox2);
154
155         gtk_widget_show_all(vbox);
156         
157         page->page.widget = vbox;
158 }
159
160 static void attwarner_prefs_destroy_widget_func(PrefsPage *_page)
161 {
162 }
163
164 static void attwarner_save_config(void)
165 {
166         PrefFile *pfile;
167         gchar *rcpath;
168
169         debug_print("Saving AttachWarner Page\n");
170
171         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
172         pfile = prefs_write_open(rcpath);
173         g_free(rcpath);
174         if (!pfile || (prefs_set_block_label(pfile, PREFS_BLOCK_NAME) < 0))
175                 return;
176
177         if (prefs_write_param(param, pfile->fp) < 0) {
178                 g_warning("Failed to write AttachWarner configuration to file\n");
179                 prefs_file_close_revert(pfile);
180                 return;
181         }
182         if (fprintf(pfile->fp, "\n") < 0) {
183                 FILE_OP_ERROR(rcpath, "fprintf");
184                 prefs_file_close_revert(pfile);
185         } else
186                 prefs_file_close(pfile);
187 }
188
189
190 static void attwarner_prefs_save_func(PrefsPage * _page)
191 {
192         struct AttachWarnerPrefsPage *page = (struct AttachWarnerPrefsPage *) _page;
193         GtkTextBuffer *buffer;
194         GtkTextIter start, end;
195         gchar *tmp;
196         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(page->regexp_text));
197         
198         g_free(attwarnerprefs.match_strings);
199         
200         gtk_text_buffer_get_start_iter(buffer, &start);
201         gtk_text_buffer_get_end_iter(buffer, &end);
202         
203         tmp = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
204         
205         attwarnerprefs.match_strings = g_malloc(2*strlen(tmp)+1);
206         pref_get_escaped_pref(attwarnerprefs.match_strings, tmp);
207
208         attwarnerprefs.skip_quotes = gtk_toggle_button_get_active
209                         (GTK_TOGGLE_BUTTON(page->skip_quotes_checkbox));
210         attwarnerprefs.skip_forwards_and_redirections = gtk_toggle_button_get_active
211                         (GTK_TOGGLE_BUTTON(page->skip_forwards_and_redirections));
212         attwarnerprefs.skip_signature = gtk_toggle_button_get_active
213                         (GTK_TOGGLE_BUTTON(page->skip_signature));
214         attwarnerprefs.case_sensitive = gtk_toggle_button_get_active
215                         (GTK_TOGGLE_BUTTON(page->case_sensitive_checkbox));
216
217         attwarner_save_config();
218         g_free(attwarnerprefs.match_strings);
219         attwarnerprefs.match_strings = tmp;
220 }
221
222 void attachwarner_prefs_init(void)
223 {
224         static gchar *path[3];
225         gchar *rcpath;
226         gchar *tmp;
227         
228         path[0] = _("Plugins");
229         path[1] = _("Attach Warner");
230         path[2] = NULL;
231
232         prefs_set_default(param);
233         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
234         prefs_read_config(param, PREFS_BLOCK_NAME, rcpath, NULL);
235         g_free(rcpath);
236
237         tmp = g_malloc(strlen(attwarnerprefs.match_strings)+1);
238         pref_get_unescaped_pref(tmp, attwarnerprefs.match_strings);
239         
240         g_free(attwarnerprefs.match_strings);
241         attwarnerprefs.match_strings = tmp;
242         
243         attwarnerprefs_page.page.path = path;
244         attwarnerprefs_page.page.create_widget = attwarner_prefs_create_widget_func;
245         attwarnerprefs_page.page.destroy_widget = attwarner_prefs_destroy_widget_func;
246         attwarnerprefs_page.page.save_page = attwarner_prefs_save_func;
247
248         prefs_gtk_register_page((PrefsPage *) &attwarnerprefs_page);
249 }
250
251 void attachwarner_prefs_done(void)
252 {
253         prefs_gtk_unregister_page((PrefsPage *) &attwarnerprefs_page);
254 }