2013-02-20 [colin] 3.9.0cvs83
[claws.git] / src / plugins / spam_report / spam_report_prefs.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Colin Leroy <colin@colino.net>
4  * and the Claws Mail Team
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 3 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 "defs.h"
27
28 #include <glib.h>
29 #include <glib/gi18n.h>
30 #include <gtk/gtk.h>
31
32 #include "gtkutils.h"
33 #include "prefs.h"
34 #include "prefs_gtk.h"
35 #include "prefswindow.h"
36 #include "alertpanel.h"
37 #include "utils.h"
38
39 #include "spam_report_prefs.h"
40
41 #define PREFS_BLOCK_NAME "SpamReport"
42
43 SpamReportPrefs spamreport_prefs;
44 void spamreport_clear_cache(void);
45
46 typedef struct _SpamReportPage SpamReportPage;
47
48 struct _SpamReportPage {
49         PrefsPage page;
50         GtkWidget *frame[INTF_LAST];
51         GtkWidget *enabled_chkbtn[INTF_LAST];
52         GtkWidget *user_entry[INTF_LAST];
53         GtkWidget *pass_entry[INTF_LAST];
54 };
55
56 static PrefParam param[] = {
57         {"signalspam_enabled", "FALSE", &spamreport_prefs.enabled[INTF_SIGNALSPAM], P_BOOL, NULL, NULL, NULL},
58         {"signalspam_user", "", &spamreport_prefs.user[INTF_SIGNALSPAM], P_STRING, NULL, NULL, NULL},
59         {"signalspam_pass", "", &spamreport_prefs.pass[INTF_SIGNALSPAM], P_PASSWORD, NULL, NULL, NULL},
60         {"spamcop_enabled",    "FALSE", &spamreport_prefs.enabled[INTF_SPAMCOP], P_BOOL, NULL, NULL, NULL},
61         {"spamcop_user",    "", &spamreport_prefs.user[INTF_SPAMCOP], P_STRING, NULL, NULL, NULL},
62         {"debianspam_enabled", "FALSE", &spamreport_prefs.enabled[INTF_DEBIANSPAM], P_BOOL, NULL, NULL, NULL},
63        {0,0,0,0,0,0,0}
64 };
65
66 static SpamReportPage spamreport_prefs_page;
67
68 static void create_spamreport_prefs_page        (PrefsPage *page,
69                                          GtkWindow *window,
70                                          gpointer   data);
71 static void destroy_spamreport_prefs_page       (PrefsPage *page);
72 static void save_spamreport_prefs               (PrefsPage *page);
73
74 void spamreport_prefs_init(void)
75 {
76         static gchar *path[3];
77         gchar *rcpath;
78
79         path[0] = _("Plugins");
80         path[1] = _("SpamReport");
81         path[2] = NULL;
82
83         prefs_set_default(param);
84         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
85         prefs_read_config(param, PREFS_BLOCK_NAME, rcpath, NULL);
86         g_free(rcpath);
87         
88         spamreport_prefs_page.page.path = path;
89         spamreport_prefs_page.page.create_widget = create_spamreport_prefs_page;
90         spamreport_prefs_page.page.destroy_widget = destroy_spamreport_prefs_page;
91         spamreport_prefs_page.page.save_page = save_spamreport_prefs;
92         spamreport_prefs_page.page.weight = 30.0;
93         
94         prefs_gtk_register_page((PrefsPage *) &spamreport_prefs_page);
95 }
96
97 void spamreport_prefs_done(void)
98 {
99         prefs_gtk_unregister_page((PrefsPage *) &spamreport_prefs_page);
100 }
101
102 static void create_spamreport_prefs_page(PrefsPage *page,
103                                     GtkWindow *window,
104                                     gpointer data)
105 {
106         SpamReportPage *prefs_page = (SpamReportPage *) page;
107
108         GtkWidget *vbox, *table;
109         GtkWidget *tmp;
110         int i = 0;
111         
112         vbox = gtk_vbox_new(FALSE, VSPACING_NARROW);
113         gtk_container_set_border_width(GTK_CONTAINER(vbox), VBOX_BORDER);
114         gtk_widget_show(vbox);
115        
116         for (i = 0; i < INTF_LAST; i++) {
117                 prefs_page->frame[i] = gtk_frame_new(spam_interfaces[i].name);
118                 gtk_box_pack_start(GTK_BOX(vbox), prefs_page->frame[i], FALSE, FALSE, 6);
119
120                 prefs_page->user_entry[i] = gtk_entry_new();
121                 prefs_page->pass_entry[i] = gtk_entry_new();
122                 prefs_page->enabled_chkbtn[i] = gtk_check_button_new_with_label(_("Enabled"));
123
124                 gtk_entry_set_visibility(GTK_ENTRY(prefs_page->pass_entry[i]), FALSE);
125
126                 gtk_entry_set_text(GTK_ENTRY(prefs_page->user_entry[i]),
127                         spamreport_prefs.user[i] ? spamreport_prefs.user[i]:"");
128                 gtk_entry_set_text(GTK_ENTRY(prefs_page->pass_entry[i]),
129                         spamreport_prefs.pass[i] ? spamreport_prefs.pass[i]:"");
130                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_page->enabled_chkbtn[i]),
131                         spamreport_prefs.enabled[i]);
132
133                 table = gtk_table_new(3, 2, FALSE);
134                 gtk_container_set_border_width(GTK_CONTAINER(table), 8);
135                 gtk_table_set_row_spacings(GTK_TABLE(table), 4);
136                 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
137         
138                 gtk_container_add(GTK_CONTAINER(prefs_page->frame[i]), table);
139                 gtk_widget_show(prefs_page->frame[i]);
140                 gtk_widget_show(table);
141
142                 gtk_table_attach(GTK_TABLE(table), prefs_page->enabled_chkbtn[i], 0, 2, 0, 1,
143                                 GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, 
144                                 0, 0);
145                 gtk_widget_show(prefs_page->enabled_chkbtn[i]);
146
147                 switch(spam_interfaces[i].type) {
148                 case INTF_MAIL:
149                         tmp = gtk_label_new(_("Forward to:"));
150                         break;
151                 default:
152                         tmp = gtk_label_new(_("Username:"));
153                 }
154                 gtk_table_attach(GTK_TABLE(table), tmp, 0, 1, 1, 2,
155                                 0, 0, 
156                                 0, 0);
157                 gtk_table_attach(GTK_TABLE(table), prefs_page->user_entry[i], 1, 2, 1, 2,
158                                 GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, 
159                                 0, 0);
160                 if (spam_interfaces[i].type != INTF_HTTP_GET) {
161                         gtk_widget_show(tmp);
162                         gtk_widget_show(prefs_page->user_entry[i]);
163                 }
164
165                 tmp = gtk_label_new(_("Password:"));
166                 gtk_table_attach(GTK_TABLE(table), tmp, 0, 1, 2, 3,
167                                 0, 0, 
168                                 0, 0);
169                 gtk_table_attach(GTK_TABLE(table), prefs_page->pass_entry[i], 1, 2, 2, 3,
170                                 GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, 
171                                 0, 0);
172                 if (spam_interfaces[i].type != INTF_MAIL && spam_interfaces[i].type != INTF_HTTP_GET) {
173                         gtk_widget_show(tmp);
174                         gtk_widget_show(prefs_page->pass_entry[i]);
175                 }
176         }
177         prefs_page->page.widget = vbox;
178 }
179
180 static void destroy_spamreport_prefs_page(PrefsPage *page)
181 {
182         /* Do nothing! */
183 }
184
185 static void save_spamreport_prefs(PrefsPage *page)
186 {
187         SpamReportPage *prefs_page = (SpamReportPage *) page;
188         PrefFile *pref_file;
189         gchar *rc_file_path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
190                                           COMMON_RC, NULL);
191         int i = 0;
192         
193         for (i = 0; i < INTF_LAST; i++) {
194
195                 g_free(spamreport_prefs.user[i]);
196                 g_free(spamreport_prefs.pass[i]);
197
198                 spamreport_prefs.enabled[i] = gtk_toggle_button_get_active(
199                         GTK_TOGGLE_BUTTON(prefs_page->enabled_chkbtn[i]));
200                 spamreport_prefs.user[i] = gtk_editable_get_chars(
201                         GTK_EDITABLE(prefs_page->user_entry[i]), 0, -1);
202                 spamreport_prefs.pass[i] = gtk_editable_get_chars(
203                         GTK_EDITABLE(prefs_page->pass_entry[i]), 0, -1);
204         }
205
206         pref_file = prefs_write_open(rc_file_path);
207         g_free(rc_file_path);
208         
209         if (!(pref_file) ||
210             (prefs_set_block_label(pref_file, PREFS_BLOCK_NAME) < 0))
211           return;
212         
213         if (prefs_write_param(param, pref_file->fp) < 0) {
214           g_warning("failed to write SpamReport Plugin configuration\n");
215           prefs_file_close_revert(pref_file);
216           return;
217         }
218         if (fprintf(pref_file->fp, "\n") < 0) {
219                 FILE_OP_ERROR(rc_file_path, "fprintf");
220                 prefs_file_close_revert(pref_file);
221         } else
222                 prefs_file_close(pref_file);
223 }