Remove GTK+ checks lower than required version
[claws.git] / src / plugins / rssyl / rssyl_prefs.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto
4  * This file (C) 2005 Andrej Kacian <andrej@kacian.sk>
5  *
6  * - Plugin preferences
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 /* Global includes */
28 #include <glib.h>
29 #include <glib/gi18n.h>
30
31 /* Claws Mail includes */
32 #include <common/defs.h>
33 #include <prefs_gtk.h>
34 #include <mainwindow.h>
35
36 /* Local includes */
37 #include "rssyl.h"
38 #include "rssyl_prefs.h"
39 #include "rssyl_feed.h"
40
41 static RPrefsPage rssyl_gtk_prefs_page;
42 RPrefs rssyl_prefs;
43
44 static void destroy_rssyl_prefs_page(PrefsPage *page);
45 static void create_rssyl_prefs_page(PrefsPage *page,
46                 GtkWindow *window, gpointer data);
47 static void save_rssyl_prefs(PrefsPage *page);
48 static void rssyl_apply_prefs(void);
49
50         static PrefParam param[] = {
51                 { "refresh_interval", PREF_DEFAULT_REFRESH, &rssyl_prefs.refresh, P_INT,
52                         NULL, NULL, NULL },
53                 { "refresh_on_startup", "FALSE", &rssyl_prefs.refresh_on_startup, P_BOOL,
54                         NULL, NULL, NULL },
55                 { "refresh_enabled", "TRUE", &rssyl_prefs.refresh_enabled, P_BOOL,
56                         NULL, NULL, NULL },
57                 { "cookies_path", "", &rssyl_prefs.cookies_path, P_STRING,
58                         NULL, NULL, NULL },
59                 { "ssl_verify_peer", "TRUE", &rssyl_prefs.ssl_verify_peer, P_BOOL,
60                         NULL, NULL, NULL },
61                 { 0, 0, 0, 0, 0, 0, 0 }
62 };
63
64 void rssyl_prefs_init(void)
65 {
66         static gchar *path[3];
67         gchar *rcpath;
68
69         path[0] = _("Plugins");
70         path[1] = "RSSyl";              /* We don't need this translated */
71         path[2] = NULL;
72
73         prefs_set_default(param);
74         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
75         prefs_read_config(param, PREFS_BLOCK_NAME, rcpath, NULL);
76         g_free(rcpath);
77
78         rssyl_gtk_prefs_page.page.path = path;
79         rssyl_gtk_prefs_page.page.create_widget = create_rssyl_prefs_page;
80         rssyl_gtk_prefs_page.page.destroy_widget = destroy_rssyl_prefs_page;
81         rssyl_gtk_prefs_page.page.save_page = save_rssyl_prefs;
82         rssyl_gtk_prefs_page.page.weight = 30.0;
83
84         prefs_gtk_register_page((PrefsPage *) &rssyl_gtk_prefs_page);
85 }
86
87 void rssyl_prefs_done(void)
88 {
89         prefs_gtk_unregister_page((PrefsPage *) &rssyl_gtk_prefs_page);
90 }
91
92 /* Toggle the refresh timeout spinbutton sensitivity after the
93  * checkbutton was toggled. */
94 static gboolean
95 rssyl_refresh_enabled_toggled_cb(GtkToggleButton *tb, gpointer data)
96 {
97         gtk_widget_set_sensitive(GTK_WIDGET(data),
98                         gtk_toggle_button_get_active(tb));
99         return FALSE;
100 }
101
102 static void create_rssyl_prefs_page(PrefsPage *page,
103                 GtkWindow *window, gpointer data)
104 {
105         int row = 0;
106         RPrefsPage *prefs_page = (RPrefsPage *) page;
107         GtkWidget *table;
108         GtkWidget *refresh, *refresh_enabled;
109         GtkWidget *label;
110         GtkWidget *refresh_on_startup;
111         GtkObject *refresh_adj;
112         GtkWidget *cookies_path;
113         GtkWidget *ssl_verify_peer;
114
115         table = gtk_table_new(3, 2, FALSE);
116         gtk_container_set_border_width(GTK_CONTAINER(table), 5);
117         gtk_table_set_row_spacings(GTK_TABLE(table), VSPACING_NARROW);
118         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
119
120         /* Refresh interval */
121         refresh_enabled = gtk_check_button_new_with_label(
122                         _("Default refresh interval in minutes"));
123         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(refresh_enabled),
124                         rssyl_prefs.refresh_enabled);
125         gtk_table_attach(GTK_TABLE(table), refresh_enabled, 0, 1, row, row+1,
126                         GTK_FILL | GTK_EXPAND, 0, 0, 0);
127
128         refresh_adj = gtk_adjustment_new(rssyl_prefs.refresh,
129                         1, 100000, 1, 10, 0);
130         refresh = gtk_spin_button_new(GTK_ADJUSTMENT(refresh_adj), 1, 0);
131         gtk_widget_set_sensitive(GTK_WIDGET(refresh), rssyl_prefs.refresh_enabled);
132         gtk_table_attach(GTK_TABLE(table), refresh, 1, 2, row, row+1,
133                         GTK_FILL, 0, 0, 0);
134
135         g_signal_connect(G_OBJECT(refresh_enabled), "toggled",
136                         G_CALLBACK(rssyl_refresh_enabled_toggled_cb), refresh);
137
138         row++;
139         /* Whether to refresh all feeds on CM startup */
140         refresh_on_startup = gtk_check_button_new_with_label(
141                         _("Refresh all feeds on application start"));
142         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(refresh_on_startup),
143                         rssyl_prefs.refresh_on_startup);
144         gtk_table_attach(GTK_TABLE(table), refresh_on_startup, 0, 2, row, row+1,
145                         GTK_FILL | GTK_EXPAND, 0, 0, 0);
146
147         row++;
148         /* Path to cookies file for libcurl to use */
149         label = gtk_label_new(_("Path to cookies file"));
150         gtk_table_attach(GTK_TABLE(table), label, 0, 1, row, row+1,
151                         GTK_FILL | GTK_EXPAND, 0, 0, 0);
152         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
153
154         cookies_path = gtk_entry_new();
155         gtk_entry_set_text(GTK_ENTRY(cookies_path), rssyl_prefs.cookies_path);
156         gtk_table_attach(GTK_TABLE(table), cookies_path, 1, 2, row, row+1,
157                         GTK_FILL, 0, 0, 0);
158         gtk_widget_set_tooltip_text(cookies_path,
159                         _("Path to Netscape-style cookies.txt file containing your cookies"));
160
161         row++;
162
163         /* Whether to verify SSL peer certificate */
164         ssl_verify_peer = gtk_check_button_new_with_label(
165                         _("Verify SSL certificates validity for new feeds"));
166         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ssl_verify_peer),
167                         rssyl_prefs.ssl_verify_peer);
168         gtk_table_attach(GTK_TABLE(table), ssl_verify_peer, 0, 2, row, row+1,
169                         GTK_FILL | GTK_EXPAND, 0, 0, 0);
170
171         gtk_widget_show_all(table);
172
173         /* Store pointers to relevant widgets */
174         prefs_page->page.widget = table;
175         prefs_page->refresh_enabled = refresh_enabled;
176         prefs_page->refresh = refresh;
177         prefs_page->refresh_on_startup = refresh_on_startup;
178         prefs_page->cookies_path = cookies_path;
179         prefs_page->ssl_verify_peer = ssl_verify_peer;
180 }
181
182 static void destroy_rssyl_prefs_page(PrefsPage *page)
183 {
184         /* Do nothing! */
185 }
186
187 static void save_rssyl_prefs(PrefsPage *page)
188 {
189         RPrefsPage *prefs_page = (RPrefsPage *)page;
190         PrefFile *pref_file;
191         gchar *rc_file_path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
192                         COMMON_RC, NULL);
193
194         /* Grab values from GTK widgets */
195         rssyl_prefs.refresh_enabled = gtk_toggle_button_get_active(
196                         GTK_TOGGLE_BUTTON(prefs_page->refresh_enabled));
197         rssyl_prefs.refresh = gtk_spin_button_get_value_as_int(
198                         GTK_SPIN_BUTTON(prefs_page->refresh));
199         rssyl_prefs.refresh_on_startup = gtk_toggle_button_get_active(
200                         GTK_TOGGLE_BUTTON(prefs_page->refresh_on_startup));
201         g_free(rssyl_prefs.cookies_path);
202         rssyl_prefs.cookies_path = g_strdup(gtk_entry_get_text(
203                                 GTK_ENTRY(prefs_page->cookies_path)));
204         rssyl_prefs.ssl_verify_peer = gtk_toggle_button_get_active(
205                         GTK_TOGGLE_BUTTON(prefs_page->ssl_verify_peer));
206
207         /* Store prefs in rc file */
208         pref_file = prefs_write_open(rc_file_path);
209         g_free(rc_file_path);
210
211         if( !pref_file || prefs_set_block_label(pref_file, PREFS_BLOCK_NAME) < 0 )
212                                 return;
213
214         if( prefs_write_param(param, pref_file->fp) < 0 ) {
215                 g_warning("Failed to write RSSyl plugin configuration\n");
216                 prefs_file_close_revert(pref_file);
217                 return;
218         }
219
220         fprintf(pref_file->fp, "\n");
221         prefs_file_close(pref_file);
222
223         rssyl_apply_prefs();
224 }
225
226 RPrefs *rssyl_prefs_get(void)
227 {
228         return &rssyl_prefs;
229 }
230
231 static void rssyl_start_default_refresh_timeouts_func(FolderItem *item,
232                 gpointer data)
233 {
234         RFolderItem *ritem = (RFolderItem *)item;
235         guint prefs_interval = GPOINTER_TO_UINT(data);
236
237         if( !IS_RSSYL_FOLDER_ITEM(item) )
238                 return;
239
240         if( folder_item_parent(item) == NULL || ritem->url == NULL )
241                 return;
242
243         /* Feeds which use default refresh interval */
244         if( ritem->default_refresh_interval ) {
245                 /* Start a new timer if the default value has changed
246                  * (ritem->refresh_interval should contain previous default
247                  * value in this case). */
248                 if( ritem->refresh_interval != prefs_interval ) {
249                         ritem->refresh_interval = prefs_interval;
250                         rssyl_feed_start_refresh_timeout(ritem);
251                 }
252         }
253 }
254
255 static void rssyl_start_default_refresh_timeouts(void)
256 {
257         RPrefs *rsprefs = rssyl_prefs_get();
258
259         folder_func_to_all_folders(
260                         (FolderItemFunc)rssyl_start_default_refresh_timeouts_func,
261                         GUINT_TO_POINTER(rsprefs->refresh));
262 }
263
264 /* rssyl_apply_prefs():
265  * Do what's needed to start using newly set preferences */
266 static void rssyl_apply_prefs(void)
267 {
268         /* Update refresh timeouts for feeds which use default interval. */
269         rssyl_start_default_refresh_timeouts();
270
271         /* Nothing else here, so far... */
272 }