Use libetpan CFLAGS for header check
[claws.git] / src / plugins / address_keeper / address_keeper_prefs.c
1 /*
2  * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3  * Copyright (C) 2009-2018 Ricardo Mones and the Claws Mail Team
4  *
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 3 of the License, or
8  * (at your option) any later version.
9  *
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.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #  include "config.h"
21 #include "claws-features.h"
22 #endif
23
24 #include <glib.h>
25 #include <glib/gi18n.h>
26
27 #include "address_keeper.h"
28
29 #include <gtk/gtk.h>
30
31 #include "defs.h"
32 #include "address_keeper_prefs.h"
33 #include "prefs_common.h"
34 #include "prefs_gtk.h"
35
36 #define PREFS_BLOCK_NAME "AddressKeeper"
37
38 AddressKeeperPrefs addkeeperprefs;
39
40 struct AddressKeeperPrefsPage
41 {
42         PrefsPage page;
43         
44         GtkWidget *addressbook_folder;
45         GtkWidget *keep_to_addrs_check;
46         GtkWidget *keep_cc_addrs_check;
47         GtkWidget *keep_bcc_addrs_check;
48         GtkWidget *block_matching_addrs;
49 };
50
51 struct AddressKeeperPrefsPage addkeeperprefs_page;
52
53 static PrefParam param[] = {
54         {"addressbook_folder", "", &addkeeperprefs.addressbook_folder,
55          P_STRING, NULL, NULL, NULL},
56         {"keep_to_addrs", "TRUE", &addkeeperprefs.keep_to_addrs,
57          P_BOOL, NULL, NULL, NULL},
58         {"keep_cc_addrs", "TRUE", &addkeeperprefs.keep_cc_addrs,
59          P_BOOL, NULL, NULL, NULL},
60         {"keep_bcc_addrs", "FALSE", &addkeeperprefs.keep_bcc_addrs,
61          P_BOOL, NULL, NULL, NULL},
62         {"block_matching_addrs", "", &addkeeperprefs.block_matching_addrs,
63          P_STRING, NULL, NULL, NULL},
64         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
65 };
66
67 #ifndef USE_ALT_ADDRBOOK
68 static void select_addressbook_clicked_cb(GtkWidget *widget, gpointer data) {
69         const gchar *folderpath = NULL;
70         gchar *new_path = NULL;
71
72         folderpath = gtk_entry_get_text(GTK_ENTRY(data));
73         new_path = addressbook_folder_selection(folderpath);
74         if (new_path) {
75                 gtk_entry_set_text(GTK_ENTRY(data), new_path);
76                 g_free(new_path);
77         }
78 }
79 #endif
80
81 #define SPACING 10
82 #define SWIN_HEIGHT 100
83
84 static void addkeeper_prefs_create_widget_func(PrefsPage * _page,
85                                                GtkWindow * window,
86                                                gpointer data)
87 {
88         struct AddressKeeperPrefsPage *page = (struct AddressKeeperPrefsPage *) _page;
89         GtkWidget *path_frame;
90         GtkWidget *path_hbox;
91         GtkWidget *path_vbox;
92         GtkWidget *path_entry;
93         GtkWidget *path_label;
94         GtkWidget *path_button;
95         GtkWidget *keep_frame;
96         GtkWidget *keep_hbox;
97         GtkWidget *keep_to_checkbox;
98         GtkWidget *keep_cc_checkbox;
99         GtkWidget *keep_bcc_checkbox;
100         GtkWidget *blocked_frame;
101         GtkWidget *blocked_vbox;
102         GtkWidget *blocked_scrolledwin;
103         GtkWidget *vbox;
104         GtkTextBuffer *buffer;
105         gchar *text;
106         gchar *tr;
107
108         vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, SPACING);
109
110         path_vbox = gtkut_get_options_frame(vbox, &path_frame,
111                 _("Address book location"));
112         gtk_container_set_border_width(GTK_CONTAINER(path_frame), SPACING);
113         path_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, SPACING);
114         gtk_box_pack_start(GTK_BOX(path_vbox), path_hbox, FALSE, FALSE, 0);
115
116         path_label = gtk_label_new(_("Keep to folder"));
117         gtk_box_pack_start(GTK_BOX(path_hbox), path_label, FALSE, FALSE, 0);
118         gtk_widget_show(path_label);
119
120         path_entry = gtk_entry_new();
121         gtk_entry_set_text(GTK_ENTRY(path_entry), addkeeperprefs.addressbook_folder);
122         gtk_box_pack_start(GTK_BOX(path_hbox), path_entry, TRUE, TRUE, 0);
123         gtk_widget_show(path_entry);
124         CLAWS_SET_TIP(path_entry, _("Address book path where addresses are kept"));
125
126         path_button = gtk_button_new_with_label(_("Select..."));
127         gtk_box_pack_start(GTK_BOX(path_hbox), path_button, FALSE, FALSE, 0);
128 #ifndef USE_ALT_ADDRBOOK
129         g_signal_connect(G_OBJECT (path_button), "clicked",
130                          G_CALLBACK (select_addressbook_clicked_cb),
131                          path_entry);
132 #else
133         gtk_widget_set_sensitive(path_button, FALSE);
134 #endif
135         gtk_widget_show(path_button);
136         gtk_widget_show(path_hbox);
137         gtk_widget_show(path_vbox);
138
139         page->addressbook_folder = path_entry;
140
141         keep_hbox = gtkut_get_options_frame(vbox, &keep_frame,
142                 _("Fields to keep addresses from"));
143         gtk_container_set_border_width(GTK_CONTAINER(keep_frame), SPACING);
144
145         keep_to_checkbox = gtk_check_button_new_with_label(prefs_common_translated_header_name("To"));
146         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_to_checkbox), addkeeperprefs.keep_to_addrs);
147         gtk_box_pack_start(GTK_BOX(keep_hbox), keep_to_checkbox, FALSE, FALSE, 0);
148         gtk_widget_show(keep_to_checkbox);
149         tr = g_strdup(C_("address keeper: %s stands for a header name",
150                         "Keep addresses which appear in '%s' headers"));
151         text = g_strdup_printf(tr, prefs_common_translated_header_name("To"));
152         CLAWS_SET_TIP(keep_to_checkbox, text);
153         g_free(tr);
154         g_free(text);
155         gtk_widget_show(keep_to_checkbox);
156
157         page->keep_to_addrs_check = keep_to_checkbox;
158
159         keep_cc_checkbox = gtk_check_button_new_with_label(prefs_common_translated_header_name("Cc"));
160         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_cc_checkbox), addkeeperprefs.keep_cc_addrs);
161         gtk_box_pack_start(GTK_BOX(keep_hbox), keep_cc_checkbox, FALSE, FALSE, 0);
162         gtk_widget_show(keep_cc_checkbox);
163         tr = g_strdup(C_("address keeper: %s stands for a header name",
164                         "Keep addresses which appear in '%s' headers"));
165         text = g_strdup_printf(tr, prefs_common_translated_header_name("Cc"));
166         CLAWS_SET_TIP(keep_cc_checkbox, text);
167         g_free(text);
168         g_free(tr);
169         gtk_widget_show(keep_cc_checkbox);
170
171         page->keep_cc_addrs_check = keep_cc_checkbox;
172
173         keep_bcc_checkbox = gtk_check_button_new_with_label(prefs_common_translated_header_name("Bcc"));
174         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_bcc_checkbox), addkeeperprefs.keep_bcc_addrs);
175         gtk_box_pack_start(GTK_BOX(keep_hbox), keep_bcc_checkbox, FALSE, FALSE, 0);
176         gtk_widget_show(keep_bcc_checkbox);
177         tr = g_strdup(C_("address keeper: %s stands for a header name",
178                         "Keep addresses which appear in '%s' headers"));
179         text = g_strdup_printf(tr, prefs_common_translated_header_name("Bcc"));
180         CLAWS_SET_TIP(keep_bcc_checkbox, text);
181         g_free(text);
182         g_free(tr);
183         gtk_widget_show(keep_bcc_checkbox);
184
185         page->keep_bcc_addrs_check = keep_bcc_checkbox;
186
187         blocked_vbox = gtkut_get_options_frame(vbox, &blocked_frame,
188                 _("Exclude addresses matching the following regular expressions (one per line)"));
189         gtk_container_set_border_width(GTK_CONTAINER(blocked_frame), SPACING);
190
191         page->block_matching_addrs = gtk_text_view_new();
192         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(page->block_matching_addrs));
193         gtk_text_buffer_set_text(buffer, addkeeperprefs.block_matching_addrs, -1);
194         
195         blocked_scrolledwin = gtk_scrolled_window_new(NULL, NULL);
196         gtk_scrolled_window_set_policy(
197                 GTK_SCROLLED_WINDOW (blocked_scrolledwin),
198                 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
199         gtk_scrolled_window_set_shadow_type(
200                 GTK_SCROLLED_WINDOW (blocked_scrolledwin), GTK_SHADOW_IN);
201         gtk_scrolled_window_set_min_content_height(
202                 GTK_SCROLLED_WINDOW (blocked_scrolledwin), SWIN_HEIGHT);
203
204         gtk_container_add(GTK_CONTAINER(blocked_scrolledwin), page->block_matching_addrs);
205         gtk_box_pack_start(GTK_BOX(blocked_vbox), blocked_scrolledwin, FALSE, FALSE, 0);
206         
207         gtk_widget_show_all(vbox);
208
209         page->page.widget = vbox;
210 }
211
212 #undef SPACING
213 #undef SWIN_HEIGHT
214
215 static void addkeeper_prefs_destroy_widget_func(PrefsPage *_page)
216 {
217 }
218
219 static void addkeeper_save_config(void)
220 {
221         PrefFile *pfile;
222         gchar *rcpath;
223
224         debug_print("Saving AddressKeeper Page\n");
225
226         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
227         pfile = prefs_write_open(rcpath);
228         g_free(rcpath);
229         if (!pfile || (prefs_set_block_label(pfile, PREFS_BLOCK_NAME) < 0))
230                 return;
231
232         if (prefs_write_param(param, pfile->fp) < 0) {
233                 g_warning("failed to write Address Keeper configuration to file");
234                 prefs_file_close_revert(pfile);
235                 return;
236         }
237         if (fprintf(pfile->fp, "\n") < 0) {
238                 FILE_OP_ERROR(rcpath, "fprintf");
239                 prefs_file_close_revert(pfile);
240         } else
241                 prefs_file_close(pfile);
242 }
243
244
245 static void addkeeper_prefs_save_func(PrefsPage * _page)
246 {
247         struct AddressKeeperPrefsPage *page = (struct AddressKeeperPrefsPage *) _page;
248         const gchar *text;
249         GtkTextBuffer *buffer;
250         GtkTextIter start, end;
251
252         text = gtk_entry_get_text(GTK_ENTRY(page->addressbook_folder));
253         addkeeperprefs.addressbook_folder = g_strdup(text);
254         addkeeperprefs.keep_to_addrs = gtk_toggle_button_get_active(
255                 GTK_TOGGLE_BUTTON(page->keep_to_addrs_check));
256         addkeeperprefs.keep_cc_addrs = gtk_toggle_button_get_active(
257                 GTK_TOGGLE_BUTTON(page->keep_cc_addrs_check));
258         addkeeperprefs.keep_bcc_addrs = gtk_toggle_button_get_active(
259                 GTK_TOGGLE_BUTTON(page->keep_bcc_addrs_check));
260
261         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(page->block_matching_addrs));
262         gtk_text_buffer_get_start_iter(buffer, &start);
263         gtk_text_buffer_get_end_iter(buffer, &end);
264         text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
265         g_free(addkeeperprefs.block_matching_addrs);
266         addkeeperprefs.block_matching_addrs = g_malloc(2 * strlen(text) + 1);
267         pref_get_escaped_pref(addkeeperprefs.block_matching_addrs, text);
268
269         addkeeper_save_config();
270         g_free(addkeeperprefs.block_matching_addrs);
271         addkeeperprefs.block_matching_addrs = (gchar *)text;
272 }
273
274 void address_keeper_prefs_init(void)
275 {
276         static gchar *path[3];
277         gchar *rcpath;
278         gchar *tmp;
279         
280         path[0] = _("Plugins");
281         path[1] = _("Address Keeper");
282         path[2] = NULL;
283
284         prefs_set_default(param);
285         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
286         prefs_read_config(param, PREFS_BLOCK_NAME, rcpath, NULL);
287         g_free(rcpath);
288
289         tmp = g_malloc(strlen(addkeeperprefs.block_matching_addrs) + 1);
290         pref_get_unescaped_pref(tmp, addkeeperprefs.block_matching_addrs);
291         g_free(addkeeperprefs.block_matching_addrs);
292         addkeeperprefs.block_matching_addrs = tmp;
293
294         addkeeperprefs_page.page.path = path;
295         addkeeperprefs_page.page.create_widget = addkeeper_prefs_create_widget_func;
296         addkeeperprefs_page.page.destroy_widget = addkeeper_prefs_destroy_widget_func;
297         addkeeperprefs_page.page.save_page = addkeeper_prefs_save_func;
298         addkeeperprefs_page.page.weight = 40.0;
299
300         prefs_gtk_register_page((PrefsPage *) &addkeeperprefs_page);
301 }
302
303 void address_keeper_prefs_done(void)
304 {
305         prefs_gtk_unregister_page((PrefsPage *) &addkeeperprefs_page);
306 }