Clean address_keeper const-ness warnings
[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-2009 Hiroyuki Yamamoto and the Claws Mail Team
4  * Copyright (C) 2009-2010 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 *entry;
89         GtkWidget *label;
90         GtkWidget *button;
91         GtkWidget *keep_to_checkbox;
92         GtkWidget *keep_cc_checkbox;
93         GtkWidget *keep_bcc_checkbox;
94         GtkWidget *blocked_frame;
95         GtkWidget *blocked_vbox;
96         GtkWidget *blocked_scrolledwin;
97         GtkWidget *hbox;
98         GtkWidget *vbox;
99         GtkTextBuffer *buffer;
100
101         vbox = gtk_vbox_new(FALSE, 6);
102         hbox = gtk_hbox_new(FALSE, 6);
103
104         label = gtk_label_new(_("Keep to folder"));
105         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
106         gtk_widget_show(label);
107
108         entry = gtk_entry_new();
109         gtk_entry_set_text(GTK_ENTRY(entry), addkeeperprefs.addressbook_folder);
110         gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
111         gtk_widget_show(entry);
112         CLAWS_SET_TIP(entry, _("Address book path where addresses are kept"));
113
114         button = gtk_button_new_with_label(_("Select..."));
115         gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
116 #ifndef USE_NEW_ADDRBOOK
117         g_signal_connect(G_OBJECT (button), "clicked",
118                          G_CALLBACK (select_addressbook_clicked_cb),
119                          entry);
120 #else
121         gtk_widget_set_sensitive(button, FALSE);
122 #endif
123         gtk_container_set_border_width(GTK_CONTAINER(hbox), 6);
124         gtk_widget_show(button);
125         gtk_widget_show(hbox);
126         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
127
128         page->addressbook_folder = entry;
129
130         keep_to_checkbox = gtk_check_button_new_with_label(_("Keep 'To' addresses"));
131         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_to_checkbox), addkeeperprefs.keep_to_addrs);
132         gtk_container_set_border_width(GTK_CONTAINER(keep_to_checkbox), 6);
133         gtk_box_pack_start(GTK_BOX(vbox), keep_to_checkbox, FALSE, FALSE, 0);
134         gtk_widget_show(keep_to_checkbox);
135         CLAWS_SET_TIP(keep_to_checkbox, _("Keep addresses which appear in 'To' headers"));
136         gtk_widget_show(keep_to_checkbox);
137
138         page->keep_to_addrs_check = keep_to_checkbox;
139
140         keep_cc_checkbox = gtk_check_button_new_with_label(_("Keep 'Cc' addresses"));
141         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_cc_checkbox), addkeeperprefs.keep_cc_addrs);
142         gtk_container_set_border_width(GTK_CONTAINER(keep_cc_checkbox), 6);
143         gtk_box_pack_start(GTK_BOX(vbox), keep_cc_checkbox, FALSE, FALSE, 0);
144         gtk_widget_show(keep_cc_checkbox);
145         CLAWS_SET_TIP(keep_cc_checkbox, _("Keep addresses which appear in 'Cc' headers"));
146         gtk_widget_show(keep_cc_checkbox);
147
148         page->keep_cc_addrs_check = keep_cc_checkbox;
149
150         keep_bcc_checkbox = gtk_check_button_new_with_label(_("Keep 'Bcc' addresses"));
151         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_bcc_checkbox), addkeeperprefs.keep_bcc_addrs);
152         gtk_container_set_border_width(GTK_CONTAINER(keep_bcc_checkbox), 6);
153         gtk_box_pack_start(GTK_BOX(vbox), keep_bcc_checkbox, FALSE, FALSE, 0);
154         gtk_widget_show(keep_bcc_checkbox);
155         CLAWS_SET_TIP(keep_bcc_checkbox, _("Keep addresses which appear in 'Bcc' headers"));
156         gtk_widget_show(keep_bcc_checkbox);
157
158         page->keep_bcc_addrs_check = keep_bcc_checkbox;
159
160         blocked_vbox = gtkut_get_options_frame(vbox, &blocked_frame, _("Exclude addresses matching the following regular expressions (one per line):"));
161         gtk_container_set_border_width(GTK_CONTAINER(blocked_frame), 6);
162
163         page->block_matching_addrs = gtk_text_view_new();
164         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(page->block_matching_addrs));
165         gtk_text_buffer_set_text(buffer, addkeeperprefs.block_matching_addrs, -1);
166         
167         blocked_scrolledwin = gtk_scrolled_window_new(NULL, NULL);
168         gtk_scrolled_window_set_policy
169                 (GTK_SCROLLED_WINDOW (blocked_scrolledwin),
170                  GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
171         gtk_scrolled_window_set_shadow_type
172                 (GTK_SCROLLED_WINDOW (blocked_scrolledwin), GTK_SHADOW_IN);
173
174         gtk_container_add(GTK_CONTAINER(blocked_scrolledwin), page->block_matching_addrs);
175         gtk_widget_set_size_request(page->block_matching_addrs, -1, 72);
176         gtk_box_pack_start(GTK_BOX(blocked_vbox), blocked_scrolledwin, FALSE, FALSE, 0);
177         
178         gtk_widget_show_all(vbox);
179
180         page->page.widget = vbox;
181 }
182
183 static void addkeeper_prefs_destroy_widget_func(PrefsPage *_page)
184 {
185 }
186
187 static void addkeeper_save_config(void)
188 {
189         PrefFile *pfile;
190         gchar *rcpath;
191
192         debug_print("Saving AddressKeeper Page\n");
193
194         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
195         pfile = prefs_write_open(rcpath);
196         g_free(rcpath);
197         if (!pfile || (prefs_set_block_label(pfile, PREFS_BLOCK_NAME) < 0))
198                 return;
199
200         if (prefs_write_param(param, pfile->fp) < 0) {
201                 g_warning("Failed to write AddressKeeper configuration to file\n");
202                 prefs_file_close_revert(pfile);
203                 return;
204         }
205         if (fprintf(pfile->fp, "\n") < 0) {
206                 FILE_OP_ERROR(rcpath, "fprintf");
207                 prefs_file_close_revert(pfile);
208         } else
209                 prefs_file_close(pfile);
210 }
211
212
213 static void addkeeper_prefs_save_func(PrefsPage * _page)
214 {
215         struct AddressKeeperPrefsPage *page = (struct AddressKeeperPrefsPage *) _page;
216         const gchar *text;
217         GtkTextBuffer *buffer;
218         GtkTextIter start, end;
219
220         text = gtk_entry_get_text(GTK_ENTRY(page->addressbook_folder));
221         addkeeperprefs.addressbook_folder = g_strdup(text);
222         addkeeperprefs.keep_to_addrs = gtk_toggle_button_get_active(
223                 GTK_TOGGLE_BUTTON(page->keep_to_addrs_check));
224         addkeeperprefs.keep_cc_addrs = gtk_toggle_button_get_active(
225                 GTK_TOGGLE_BUTTON(page->keep_cc_addrs_check));
226         addkeeperprefs.keep_bcc_addrs = gtk_toggle_button_get_active(
227                 GTK_TOGGLE_BUTTON(page->keep_bcc_addrs_check));
228
229         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(page->block_matching_addrs));
230         gtk_text_buffer_get_start_iter(buffer, &start);
231         gtk_text_buffer_get_end_iter(buffer, &end);
232         text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
233         g_free(addkeeperprefs.block_matching_addrs);
234         addkeeperprefs.block_matching_addrs = g_malloc(2 * strlen(text) + 1);
235         pref_get_escaped_pref(addkeeperprefs.block_matching_addrs, text);
236
237         addkeeper_save_config();
238         g_free(addkeeperprefs.block_matching_addrs);
239         addkeeperprefs.block_matching_addrs = (gchar *)text;
240 }
241
242 void address_keeper_prefs_init(void)
243 {
244         static gchar *path[3];
245         gchar *rcpath;
246         gchar *tmp;
247         
248         path[0] = _("Plugins");
249         path[1] = _("Address Keeper");
250         path[2] = NULL;
251
252         prefs_set_default(param);
253         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
254         prefs_read_config(param, PREFS_BLOCK_NAME, rcpath, NULL);
255         g_free(rcpath);
256
257         tmp = g_malloc(strlen(addkeeperprefs.block_matching_addrs) + 1);
258         pref_get_unescaped_pref(tmp, addkeeperprefs.block_matching_addrs);
259         g_free(addkeeperprefs.block_matching_addrs);
260         addkeeperprefs.block_matching_addrs = tmp;
261
262         addkeeperprefs_page.page.path = path;
263         addkeeperprefs_page.page.create_widget = addkeeper_prefs_create_widget_func;
264         addkeeperprefs_page.page.destroy_widget = addkeeper_prefs_destroy_widget_func;
265         addkeeperprefs_page.page.save_page = addkeeper_prefs_save_func;
266
267         prefs_gtk_register_page((PrefsPage *) &addkeeperprefs_page);
268 }
269
270 void address_keeper_prefs_done(void)
271 {
272         prefs_gtk_unregister_page((PrefsPage *) &addkeeperprefs_page);
273 }