fix building RSSyl
[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 #if !(GTK_CHECK_VERSION(2, 12, 0))
115         GtkTooltips *tooltips;
116
117         tooltips = gtk_tooltips_new();
118         gtk_tooltips_enable(tooltips);
119 #endif
120
121         table = gtk_table_new(3, 2, FALSE);
122         gtk_container_set_border_width(GTK_CONTAINER(table), 5);
123         gtk_table_set_row_spacings(GTK_TABLE(table), VSPACING_NARROW);
124         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
125
126         /* Refresh interval */
127         refresh_enabled = gtk_check_button_new_with_label(
128                         _("Default refresh interval in minutes"));
129         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(refresh_enabled),
130                         rssyl_prefs.refresh_enabled);
131         gtk_table_attach(GTK_TABLE(table), refresh_enabled, 0, 1, row, row+1,
132                         GTK_FILL | GTK_EXPAND, 0, 0, 0);
133
134         refresh_adj = gtk_adjustment_new(rssyl_prefs.refresh,
135                         1, 100000, 1, 10, 0);
136         refresh = gtk_spin_button_new(GTK_ADJUSTMENT(refresh_adj), 1, 0);
137         gtk_widget_set_sensitive(GTK_WIDGET(refresh), rssyl_prefs.refresh_enabled);
138         gtk_table_attach(GTK_TABLE(table), refresh, 1, 2, row, row+1,
139                         GTK_FILL, 0, 0, 0);
140
141         g_signal_connect(G_OBJECT(refresh_enabled), "toggled",
142                         G_CALLBACK(rssyl_refresh_enabled_toggled_cb), refresh);
143
144         row++;
145         /* Whether to refresh all feeds on CM startup */
146         refresh_on_startup = gtk_check_button_new_with_label(
147                         _("Refresh all feeds on application start"));
148         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(refresh_on_startup),
149                         rssyl_prefs.refresh_on_startup);
150         gtk_table_attach(GTK_TABLE(table), refresh_on_startup, 0, 2, row, row+1,
151                         GTK_FILL | GTK_EXPAND, 0, 0, 0);
152
153         row++;
154         /* Path to cookies file for libcurl to use */
155         label = gtk_label_new(_("Path to cookies file"));
156         gtk_table_attach(GTK_TABLE(table), label, 0, 1, row, row+1,
157                         GTK_FILL | GTK_EXPAND, 0, 0, 0);
158         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
159
160         cookies_path = gtk_entry_new();
161         gtk_entry_set_text(GTK_ENTRY(cookies_path), rssyl_prefs.cookies_path);
162         gtk_table_attach(GTK_TABLE(table), cookies_path, 1, 2, row, row+1,
163                         GTK_FILL, 0, 0, 0);
164 #if !(GTK_CHECK_VERSION(2, 12, 0))
165         gtk_tooltips_set_tip(tooltips, cookies_path,
166                         _("Path to Netscape-style cookies.txt file containing your cookies"), NULL);
167 #else
168         gtk_widget_set_tooltip_text(cookies_path,
169                         _("Path to Netscape-style cookies.txt file containing your cookies"));
170 #endif
171
172         row++;
173
174         /* Whether to verify SSL peer certificate */
175         ssl_verify_peer = gtk_check_button_new_with_label(
176                         _("Verify SSL certificates validity for new feeds"));
177         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ssl_verify_peer),
178                         rssyl_prefs.ssl_verify_peer);
179         gtk_table_attach(GTK_TABLE(table), ssl_verify_peer, 0, 2, row, row+1,
180                         GTK_FILL | GTK_EXPAND, 0, 0, 0);
181
182         gtk_widget_show_all(table);
183
184         /* Store pointers to relevant widgets */
185         prefs_page->page.widget = table;
186         prefs_page->refresh_enabled = refresh_enabled;
187         prefs_page->refresh = refresh;
188         prefs_page->refresh_on_startup = refresh_on_startup;
189         prefs_page->cookies_path = cookies_path;
190         prefs_page->ssl_verify_peer = ssl_verify_peer;
191 }
192
193 static void destroy_rssyl_prefs_page(PrefsPage *page)
194 {
195         /* Do nothing! */
196 }
197
198 static void save_rssyl_prefs(PrefsPage *page)
199 {
200         RPrefsPage *prefs_page = (RPrefsPage *)page;
201         PrefFile *pref_file;
202         gchar *rc_file_path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
203                         COMMON_RC, NULL);
204
205         /* Grab values from GTK widgets */
206         rssyl_prefs.refresh_enabled = gtk_toggle_button_get_active(
207                         GTK_TOGGLE_BUTTON(prefs_page->refresh_enabled));
208         rssyl_prefs.refresh = gtk_spin_button_get_value_as_int(
209                         GTK_SPIN_BUTTON(prefs_page->refresh));
210         rssyl_prefs.refresh_on_startup = gtk_toggle_button_get_active(
211                         GTK_TOGGLE_BUTTON(prefs_page->refresh_on_startup));
212         g_free(rssyl_prefs.cookies_path);
213         rssyl_prefs.cookies_path = g_strdup(gtk_entry_get_text(
214                                 GTK_ENTRY(prefs_page->cookies_path)));
215         rssyl_prefs.ssl_verify_peer = gtk_toggle_button_get_active(
216                         GTK_TOGGLE_BUTTON(prefs_page->ssl_verify_peer));
217
218         /* Store prefs in rc file */
219         pref_file = prefs_write_open(rc_file_path);
220         g_free(rc_file_path);
221
222         if( !pref_file || prefs_set_block_label(pref_file, PREFS_BLOCK_NAME) < 0 )
223                                 return;
224
225         if( prefs_write_param(param, pref_file->fp) < 0 ) {
226                 g_warning("Failed to write RSSyl plugin configuration\n");
227                 prefs_file_close_revert(pref_file);
228                 return;
229         }
230
231         fprintf(pref_file->fp, "\n");
232         prefs_file_close(pref_file);
233
234         rssyl_apply_prefs();
235 }
236
237 RPrefs *rssyl_prefs_get(void)
238 {
239         return &rssyl_prefs;
240 }
241
242 static void rssyl_start_default_refresh_timeouts_func(FolderItem *item,
243                 gpointer data)
244 {
245         RFolderItem *ritem = (RFolderItem *)item;
246         guint prefs_interval = GPOINTER_TO_UINT(data);
247
248         if( !IS_RSSYL_FOLDER_ITEM(item) )
249                 return;
250
251         if( folder_item_parent(item) == NULL || ritem->url == NULL )
252                 return;
253
254         /* Feeds which use default refresh interval */
255         if( ritem->default_refresh_interval ) {
256                 /* Start a new timer if the default value has changed
257                  * (ritem->refresh_interval should contain previous default
258                  * value in this case). */
259                 if( ritem->refresh_interval != prefs_interval ) {
260                         ritem->refresh_interval = prefs_interval;
261                         rssyl_feed_start_refresh_timeout(ritem);
262                 }
263         }
264 }
265
266 static void rssyl_start_default_refresh_timeouts(void)
267 {
268         RPrefs *rsprefs = rssyl_prefs_get();
269
270         folder_func_to_all_folders(
271                         (FolderItemFunc)rssyl_start_default_refresh_timeouts_func,
272                         GUINT_TO_POINTER(rsprefs->refresh));
273 }
274
275 /* rssyl_apply_prefs():
276  * Do what's needed to start using newly set preferences */
277 static void rssyl_apply_prefs(void)
278 {
279         /* Update refresh timeouts for feeds which use default interval. */
280         rssyl_start_default_refresh_timeouts();
281
282         /* Nothing else here, so far... */
283 }