Fix bug #3380. Initialize widget before callback handler for 'clicked'
[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) 1999-2015 Hiroyuki Yamamoto and the Claws Mail Team
4  * Copyright (C) 2009-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 "address_keeper.h"
30
31 #include <gtk/gtk.h>
32
33 #include "defs.h"
34 #include "address_keeper_prefs.h"
35 #include "prefs_common.h"
36 #include "prefs_gtk.h"
37
38 #define PREFS_BLOCK_NAME "AddressKeeper"
39
40 AddressKeeperPrefs addkeeperprefs;
41
42 struct AddressKeeperPrefsPage
43 {
44         PrefsPage page;
45         
46         GtkWidget *addressbook_folder;
47         GtkWidget *keep_to_addrs_check;
48         GtkWidget *keep_cc_addrs_check;
49         GtkWidget *keep_bcc_addrs_check;
50         GtkWidget *block_matching_addrs;
51 };
52
53 struct AddressKeeperPrefsPage addkeeperprefs_page;
54
55 static PrefParam param[] = {
56         {"addressbook_folder", "", &addkeeperprefs.addressbook_folder,
57          P_STRING, NULL, NULL, NULL},
58         {"keep_to_addrs", "TRUE", &addkeeperprefs.keep_to_addrs,
59          P_BOOL, NULL, NULL, NULL},
60         {"keep_cc_addrs", "TRUE", &addkeeperprefs.keep_cc_addrs,
61          P_BOOL, NULL, NULL, NULL},
62         {"keep_bcc_addrs", "FALSE", &addkeeperprefs.keep_bcc_addrs,
63          P_BOOL, NULL, NULL, NULL},
64         {"block_matching_addrs", "", &addkeeperprefs.block_matching_addrs,
65          P_STRING, NULL, NULL, NULL},
66         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
67 };
68
69 #ifndef USE_NEW_ADDRBOOK
70 static void select_addressbook_clicked_cb(GtkWidget *widget, gpointer data) {
71         const gchar *folderpath = NULL;
72         gchar *new_path = NULL;
73
74         folderpath = gtk_entry_get_text(GTK_ENTRY(data));
75         new_path = addressbook_folder_selection(folderpath);
76         if (new_path) {
77                 gtk_entry_set_text(GTK_ENTRY(data), new_path);
78                 g_free(new_path);
79         }
80 }
81 #endif
82
83 static void addkeeper_prefs_create_widget_func(PrefsPage * _page,
84                                                GtkWindow * window,
85                                                gpointer data)
86 {
87         struct AddressKeeperPrefsPage *page = (struct AddressKeeperPrefsPage *) _page;
88         GtkWidget *path_frame;
89         GtkWidget *path_hbox;
90         GtkWidget *path_vbox;
91         GtkWidget *path_entry;
92         GtkWidget *path_label;
93         GtkWidget *path_button;
94         GtkWidget *keep_frame;
95         GtkWidget *keep_hbox;
96         GtkWidget *keep_to_checkbox;
97         GtkWidget *keep_cc_checkbox;
98         GtkWidget *keep_bcc_checkbox;
99         GtkWidget *blocked_frame;
100         GtkWidget *blocked_vbox;
101         GtkWidget *blocked_scrolledwin;
102         GtkWidget *vbox;
103         GtkTextBuffer *buffer;
104
105         vbox = gtk_vbox_new(FALSE, 6);
106
107         path_vbox = gtkut_get_options_frame(vbox, &path_frame,
108                 _("Address book location"));
109         gtk_container_set_border_width(GTK_CONTAINER(path_frame), 6);
110         path_hbox = gtk_hbox_new(FALSE, 6);
111         gtk_box_pack_start(GTK_BOX(path_vbox), path_hbox, FALSE, FALSE, 0);
112
113         path_label = gtk_label_new(_("Keep to folder"));
114         gtk_box_pack_start(GTK_BOX(path_hbox), path_label, FALSE, FALSE, 0);
115         gtk_widget_show(path_label);
116
117         path_entry = gtk_entry_new();
118         gtk_entry_set_text(GTK_ENTRY(path_entry), addkeeperprefs.addressbook_folder);
119         gtk_box_pack_start(GTK_BOX(path_hbox), path_entry, TRUE, TRUE, 0);
120         gtk_widget_show(path_entry);
121         CLAWS_SET_TIP(path_entry, _("Address book path where addresses are kept"));
122
123         path_button = gtk_button_new_with_label(_("Select..."));
124         gtk_box_pack_start(GTK_BOX(path_hbox), path_button, FALSE, FALSE, 0);
125 #ifndef USE_NEW_ADDRBOOK
126         g_signal_connect(G_OBJECT (path_button), "clicked",
127                          G_CALLBACK (select_addressbook_clicked_cb),
128                          path_entry);
129 #else
130         gtk_widget_set_sensitive(path_button, FALSE);
131 #endif
132         gtk_widget_show(path_button);
133         gtk_widget_show(path_hbox);
134         gtk_widget_show(path_vbox);
135
136         page->addressbook_folder = path_entry;
137
138         keep_hbox = gtkut_get_options_frame(vbox, &keep_frame,
139                 _("Fields to keep addresses from"));
140         gtk_container_set_border_width(GTK_CONTAINER(keep_frame), 6);
141
142         keep_to_checkbox = gtk_check_button_new_with_label(_("To"));
143         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_to_checkbox), addkeeperprefs.keep_to_addrs);
144         gtk_box_pack_start(GTK_BOX(keep_hbox), keep_to_checkbox, FALSE, FALSE, 0);
145         gtk_widget_show(keep_to_checkbox);
146         CLAWS_SET_TIP(keep_to_checkbox, _("Keep addresses which appear in 'To' headers"));
147         gtk_widget_show(keep_to_checkbox);
148
149         page->keep_to_addrs_check = keep_to_checkbox;
150
151         keep_cc_checkbox = gtk_check_button_new_with_label(_("Cc"));
152         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_cc_checkbox), addkeeperprefs.keep_cc_addrs);
153         gtk_box_pack_start(GTK_BOX(keep_hbox), keep_cc_checkbox, FALSE, FALSE, 0);
154         gtk_widget_show(keep_cc_checkbox);
155         CLAWS_SET_TIP(keep_cc_checkbox, _("Keep addresses which appear in 'Cc' headers"));
156         gtk_widget_show(keep_cc_checkbox);
157
158         page->keep_cc_addrs_check = keep_cc_checkbox;
159
160         keep_bcc_checkbox = gtk_check_button_new_with_label(_("Bcc"));
161         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_bcc_checkbox), addkeeperprefs.keep_bcc_addrs);
162         gtk_box_pack_start(GTK_BOX(keep_hbox), keep_bcc_checkbox, FALSE, FALSE, 0);
163         gtk_widget_show(keep_bcc_checkbox);
164         CLAWS_SET_TIP(keep_bcc_checkbox, _("Keep addresses which appear in 'Bcc' headers"));
165         gtk_widget_show(keep_bcc_checkbox);
166
167         page->keep_bcc_addrs_check = keep_bcc_checkbox;
168
169         blocked_vbox = gtkut_get_options_frame(vbox, &blocked_frame,
170                 _("Exclude addresses matching the following regular expressions (one per line)"));
171         gtk_container_set_border_width(GTK_CONTAINER(blocked_frame), 6);
172
173         page->block_matching_addrs = gtk_text_view_new();
174         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(page->block_matching_addrs));
175         gtk_text_buffer_set_text(buffer, addkeeperprefs.block_matching_addrs, -1);
176         
177         blocked_scrolledwin = gtk_scrolled_window_new(NULL, NULL);
178         gtk_scrolled_window_set_policy
179                 (GTK_SCROLLED_WINDOW (blocked_scrolledwin),
180                  GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
181         gtk_scrolled_window_set_shadow_type
182                 (GTK_SCROLLED_WINDOW (blocked_scrolledwin), GTK_SHADOW_IN);
183
184         gtk_container_add(GTK_CONTAINER(blocked_scrolledwin), page->block_matching_addrs);
185         gtk_widget_set_size_request(page->block_matching_addrs, -1, 72);
186         gtk_box_pack_start(GTK_BOX(blocked_vbox), blocked_scrolledwin, FALSE, FALSE, 0);
187         
188         gtk_widget_show_all(vbox);
189
190         page->page.widget = vbox;
191 }
192
193 static void addkeeper_prefs_destroy_widget_func(PrefsPage *_page)
194 {
195 }
196
197 static void addkeeper_save_config(void)
198 {
199         PrefFile *pfile;
200         gchar *rcpath;
201
202         debug_print("Saving AddressKeeper Page\n");
203
204         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
205         pfile = prefs_write_open(rcpath);
206         g_free(rcpath);
207         if (!pfile || (prefs_set_block_label(pfile, PREFS_BLOCK_NAME) < 0))
208                 return;
209
210         if (prefs_write_param(param, pfile->fp) < 0) {
211                 g_warning("Failed to write AddressKeeper configuration to file\n");
212                 prefs_file_close_revert(pfile);
213                 return;
214         }
215         if (fprintf(pfile->fp, "\n") < 0) {
216                 FILE_OP_ERROR(rcpath, "fprintf");
217                 prefs_file_close_revert(pfile);
218         } else
219                 prefs_file_close(pfile);
220 }
221
222
223 static void addkeeper_prefs_save_func(PrefsPage * _page)
224 {
225         struct AddressKeeperPrefsPage *page = (struct AddressKeeperPrefsPage *) _page;
226         const gchar *text;
227         GtkTextBuffer *buffer;
228         GtkTextIter start, end;
229
230         text = gtk_entry_get_text(GTK_ENTRY(page->addressbook_folder));
231         addkeeperprefs.addressbook_folder = g_strdup(text);
232         addkeeperprefs.keep_to_addrs = gtk_toggle_button_get_active(
233                 GTK_TOGGLE_BUTTON(page->keep_to_addrs_check));
234         addkeeperprefs.keep_cc_addrs = gtk_toggle_button_get_active(
235                 GTK_TOGGLE_BUTTON(page->keep_cc_addrs_check));
236         addkeeperprefs.keep_bcc_addrs = gtk_toggle_button_get_active(
237                 GTK_TOGGLE_BUTTON(page->keep_bcc_addrs_check));
238
239         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(page->block_matching_addrs));
240         gtk_text_buffer_get_start_iter(buffer, &start);
241         gtk_text_buffer_get_end_iter(buffer, &end);
242         text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
243         g_free(addkeeperprefs.block_matching_addrs);
244         addkeeperprefs.block_matching_addrs = g_malloc(2 * strlen(text) + 1);
245         pref_get_escaped_pref(addkeeperprefs.block_matching_addrs, text);
246
247         addkeeper_save_config();
248         g_free(addkeeperprefs.block_matching_addrs);
249         addkeeperprefs.block_matching_addrs = (gchar *)text;
250 }
251
252 void address_keeper_prefs_init(void)
253 {
254         static gchar *path[3];
255         gchar *rcpath;
256         gchar *tmp;
257         
258         path[0] = _("Plugins");
259         path[1] = _("Address Keeper");
260         path[2] = NULL;
261
262         prefs_set_default(param);
263         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
264         prefs_read_config(param, PREFS_BLOCK_NAME, rcpath, NULL);
265         g_free(rcpath);
266
267         tmp = g_malloc(strlen(addkeeperprefs.block_matching_addrs) + 1);
268         pref_get_unescaped_pref(tmp, addkeeperprefs.block_matching_addrs);
269         g_free(addkeeperprefs.block_matching_addrs);
270         addkeeperprefs.block_matching_addrs = tmp;
271
272         addkeeperprefs_page.page.path = path;
273         addkeeperprefs_page.page.create_widget = addkeeper_prefs_create_widget_func;
274         addkeeperprefs_page.page.destroy_widget = addkeeper_prefs_destroy_widget_func;
275         addkeeperprefs_page.page.save_page = addkeeper_prefs_save_func;
276
277         prefs_gtk_register_page((PrefsPage *) &addkeeperprefs_page);
278 }
279
280 void address_keeper_prefs_done(void)
281 {
282         prefs_gtk_unregister_page((PrefsPage *) &addkeeperprefs_page);
283 }