add .cvsignore files
[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 };
51
52 struct AddressKeeperPrefsPage addkeeperprefs_page;
53
54 static PrefParam param[] = {
55         {"addressbook_folder", "", &addkeeperprefs.addressbook_folder,
56          P_STRING, NULL, NULL, NULL},
57         {"keep_to_addrs", "TRUE", &addkeeperprefs.keep_to_addrs,
58          P_BOOL, NULL, NULL, NULL},
59         {"keep_cc_addrs", "TRUE", &addkeeperprefs.keep_cc_addrs,
60          P_BOOL, NULL, NULL, NULL},
61         {"keep_bcc_addrs", "FALSE", &addkeeperprefs.keep_bcc_addrs,
62          P_BOOL, NULL, NULL, NULL},
63         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
64 };
65
66 #ifndef USE_NEW_ADDRBOOK
67 static void select_addressbook_clicked_cb(GtkWidget *widget, gpointer data) {
68         const gchar *folderpath = NULL;
69         gchar *new_path = NULL;
70
71         folderpath = gtk_entry_get_text(GTK_ENTRY(data));
72         new_path = addressbook_folder_selection(folderpath);
73         if (new_path) {
74                 gtk_entry_set_text(GTK_ENTRY(data), new_path);
75                 g_free(new_path);
76         }
77 }
78 #endif
79
80 static void addkeeper_prefs_create_widget_func(PrefsPage * _page,
81                                                GtkWindow * window,
82                                                gpointer data)
83 {
84         struct AddressKeeperPrefsPage *page = (struct AddressKeeperPrefsPage *) _page;
85         GtkWidget *entry;
86         GtkWidget *label;
87         GtkWidget *button;
88         GtkWidget *keep_to_checkbox;
89         GtkWidget *keep_cc_checkbox;
90         GtkWidget *keep_bcc_checkbox;
91         GtkWidget *hbox;
92         GtkWidget *vbox;
93
94         vbox = gtk_vbox_new(FALSE, 6);
95         hbox = gtk_hbox_new(FALSE, 6);
96
97         label = gtk_label_new(_("Keep to folder"));
98         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
99         gtk_widget_show(label);
100
101         entry = gtk_entry_new();
102         gtk_entry_set_text(GTK_ENTRY(entry), addkeeperprefs.addressbook_folder);
103         gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
104         gtk_widget_show(entry);
105         CLAWS_SET_TIP(entry, _("Address book path where addresses are kept"));
106
107         button = gtk_button_new_with_label(_("Select..."));
108         gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
109 #ifndef USE_NEW_ADDRBOOK
110         g_signal_connect(G_OBJECT (button), "clicked",
111                          G_CALLBACK (select_addressbook_clicked_cb),
112                          entry);
113 #else
114         gtk_widget_set_sensitive(button, FALSE);
115 #endif
116         gtk_container_set_border_width(GTK_CONTAINER(hbox), 6);
117         gtk_widget_show(button);
118         gtk_widget_show(hbox);
119         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
120
121         page->addressbook_folder = entry;
122
123         keep_to_checkbox = gtk_check_button_new_with_label(_("Keep 'To' addresses"));
124         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_to_checkbox), addkeeperprefs.keep_to_addrs);
125         gtk_container_set_border_width(GTK_CONTAINER(keep_to_checkbox), 6);
126         gtk_box_pack_start(GTK_BOX(vbox), keep_to_checkbox, FALSE, FALSE, 0);
127         gtk_widget_show(keep_to_checkbox);
128         CLAWS_SET_TIP(keep_to_checkbox, _("Keep addresses which appear in 'To' headers"));
129         gtk_widget_show(keep_to_checkbox);
130
131         page->keep_to_addrs_check = keep_to_checkbox;
132
133         keep_cc_checkbox = gtk_check_button_new_with_label(_("Keep 'Cc' addresses"));
134         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_cc_checkbox), addkeeperprefs.keep_cc_addrs);
135         gtk_container_set_border_width(GTK_CONTAINER(keep_cc_checkbox), 6);
136         gtk_box_pack_start(GTK_BOX(vbox), keep_cc_checkbox, FALSE, FALSE, 0);
137         gtk_widget_show(keep_cc_checkbox);
138         CLAWS_SET_TIP(keep_cc_checkbox, _("Keep addresses which appear in 'Cc' headers"));
139         gtk_widget_show(keep_cc_checkbox);
140
141         page->keep_cc_addrs_check = keep_cc_checkbox;
142
143         keep_bcc_checkbox = gtk_check_button_new_with_label(_("Keep 'Bcc' addresses"));
144         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(keep_bcc_checkbox), addkeeperprefs.keep_bcc_addrs);
145         gtk_container_set_border_width(GTK_CONTAINER(keep_bcc_checkbox), 6);
146         gtk_box_pack_start(GTK_BOX(vbox), keep_bcc_checkbox, FALSE, FALSE, 0);
147         gtk_widget_show(keep_bcc_checkbox);
148         CLAWS_SET_TIP(keep_bcc_checkbox, _("Keep addresses which appear in 'Bcc' headers"));
149         gtk_widget_show(keep_bcc_checkbox);
150
151         page->keep_bcc_addrs_check = keep_bcc_checkbox;
152         
153         gtk_widget_show_all(vbox);
154
155         page->page.widget = vbox;
156 }
157
158 static void addkeeper_prefs_destroy_widget_func(PrefsPage *_page)
159 {
160 }
161
162 static void addkeeper_save_config(void)
163 {
164         PrefFile *pfile;
165         gchar *rcpath;
166
167         debug_print("Saving AddressKeeper Page\n");
168
169         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
170         pfile = prefs_write_open(rcpath);
171         g_free(rcpath);
172         if (!pfile || (prefs_set_block_label(pfile, PREFS_BLOCK_NAME) < 0))
173                 return;
174
175         if (prefs_write_param(param, pfile->fp) < 0) {
176                 g_warning("Failed to write AddressKeeper configuration to file\n");
177                 prefs_file_close_revert(pfile);
178                 return;
179         }
180         if (fprintf(pfile->fp, "\n") < 0) {
181                 FILE_OP_ERROR(rcpath, "fprintf");
182                 prefs_file_close_revert(pfile);
183         } else
184                 prefs_file_close(pfile);
185 }
186
187
188 static void addkeeper_prefs_save_func(PrefsPage * _page)
189 {
190         struct AddressKeeperPrefsPage *page = (struct AddressKeeperPrefsPage *) _page;
191         const gchar *text;
192         text = gtk_entry_get_text(GTK_ENTRY(page->addressbook_folder));
193         addkeeperprefs.addressbook_folder = g_strdup(text);
194         addkeeperprefs.keep_to_addrs = gtk_toggle_button_get_active(
195                 GTK_TOGGLE_BUTTON(page->keep_to_addrs_check));
196         addkeeperprefs.keep_cc_addrs = gtk_toggle_button_get_active(
197                 GTK_TOGGLE_BUTTON(page->keep_cc_addrs_check));
198         addkeeperprefs.keep_bcc_addrs = gtk_toggle_button_get_active(
199                 GTK_TOGGLE_BUTTON(page->keep_bcc_addrs_check));
200         addkeeper_save_config();
201 }
202
203 void address_keeper_prefs_init(void)
204 {
205         static gchar *path[3];
206         gchar *rcpath;
207         
208         path[0] = _("Plugins");
209         path[1] = _("Address Keeper");
210         path[2] = NULL;
211
212         prefs_set_default(param);
213         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
214         prefs_read_config(param, PREFS_BLOCK_NAME, rcpath, NULL);
215         g_free(rcpath);
216
217         addkeeperprefs_page.page.path = path;
218         addkeeperprefs_page.page.create_widget = addkeeper_prefs_create_widget_func;
219         addkeeperprefs_page.page.destroy_widget = addkeeper_prefs_destroy_widget_func;
220         addkeeperprefs_page.page.save_page = addkeeper_prefs_save_func;
221
222         prefs_gtk_register_page((PrefsPage *) &addkeeperprefs_page);
223 }
224
225 void address_keeper_prefs_done(void)
226 {
227         prefs_gtk_unregister_page((PrefsPage *) &addkeeperprefs_page);
228 }