fix CID 1596595: Resource leaks, and CID 1596594: (CHECKED_RETURN)
[claws.git] / src / plugins / litehtml_viewer / lh_prefs.c
1 /*
2  * Claws Mail -- A GTK based, lightweight, and fast e-mail client
3  * Copyright(C) 2019 the Claws Mail Team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write tothe Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16  */
17
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #include "claws-features.h"
21 #endif
22
23 #include <glib.h>
24 #include <glib/gi18n.h>
25
26 #include "prefs_gtk.h"
27 #include "common/defs.h"
28 #include "common/utils.h"
29 #include "gtk/gtkutils.h"
30
31 #include "lh_prefs.h"
32
33 #define PREFS_BLOCK_NAME "LiteHTML"
34
35 static void create_lh_prefs_page(PrefsPage *page, GtkWindow *window,
36                 gpointer data);
37 static void destroy_lh_prefs_page(PrefsPage *page);
38 static void save_lh_prefs_page(PrefsPage *page);
39 static void save_prefs(void);
40
41 LHPrefs lh_prefs;
42
43 struct _LHPrefsPage {
44         PrefsPage page;
45         GtkWidget *enable_remote_content;
46         GtkWidget *image_cache_size;
47         GtkWidget *default_font;
48 };
49 typedef struct _LHPrefsPage LHPrefsPage;
50
51 static PrefParam param[] = {
52         { "enable_remote_content", "FALSE", &lh_prefs.enable_remote_content, P_BOOL,
53                 NULL, NULL, NULL },
54         { "image_cache_size", "20", &lh_prefs.image_cache_size, P_INT,
55                 NULL, NULL, NULL },
56         { "default_font", "Sans 16", &lh_prefs.default_font, P_STRING,
57                 NULL, NULL, NULL },
58         { NULL, NULL, NULL, 0, NULL, NULL, NULL }
59 };
60
61 static LHPrefsPage lh_prefs_page;
62
63 LHPrefs *lh_prefs_get(void)
64 {
65         return &lh_prefs;
66 }
67
68 void lh_prefs_init(void)
69 {
70         static gchar *path[3];
71         gchar *rcpath;
72
73         path[0] = _("Plugins");
74         path[1] = "LiteHTML";
75         path[2] = NULL;
76
77         prefs_set_default(param);
78         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
79         prefs_read_config(param, PREFS_BLOCK_NAME, rcpath, NULL);
80         g_free(rcpath);
81
82         lh_prefs_page.page.path = path;
83         lh_prefs_page.page.create_widget = create_lh_prefs_page;
84         lh_prefs_page.page.destroy_widget = destroy_lh_prefs_page;
85         lh_prefs_page.page.save_page = save_lh_prefs_page;
86         lh_prefs_page.page.weight = 30.0;
87         prefs_gtk_register_page((PrefsPage *) &lh_prefs_page);
88 }
89
90 void lh_prefs_done(void)
91 {
92         prefs_gtk_unregister_page((PrefsPage *) &lh_prefs_page);
93 }
94
95 static void create_lh_prefs_page(PrefsPage *page, GtkWindow *window,
96                 gpointer data)
97 {
98         LHPrefsPage *prefs_page = (LHPrefsPage *)page;
99         GtkWidget *vbox;
100         GtkWidget *vbox_remote;
101         GtkWidget *hbox;
102         GtkWidget *frame;
103         GtkWidget *label;
104         GtkWidget *enable_remote_content;
105         GtkWidget *image_cache_size;
106         GtkWidget *default_font;
107         GtkAdjustment *adj;
108
109         vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 3);
110         gtk_container_set_border_width(GTK_CONTAINER(vbox), VBOX_BORDER);
111         gtk_widget_show(vbox);
112
113         /* Enable remote content */
114         vbox_remote = gtkut_get_options_frame(vbox, &frame, _("Remote resources"));
115
116         label = gtk_label_new(_("Loading remote resources can lead to some privacy issues.\n"
117                                 "When remote content loading is disabled, nothing will be requested\n"
118                                 "from the network."));
119         gtk_label_set_xalign(GTK_LABEL(label),0);
120         gtk_label_set_yalign(GTK_LABEL(label),0);
121
122         enable_remote_content = gtk_check_button_new_with_label(_("Enable loading of remote content"));
123         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(enable_remote_content),
124                         lh_prefs.enable_remote_content);
125
126         gtk_box_pack_start(GTK_BOX(vbox_remote), label, FALSE, FALSE, 0);
127         gtk_box_pack_start(GTK_BOX(vbox_remote), enable_remote_content, FALSE, FALSE, 0);
128         gtk_widget_show_all(vbox_remote);
129
130         /* Image cache size */
131         hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
132         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
133
134         label = gtk_label_new(_("Size of image cache in megabytes"));
135         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
136
137         adj = gtk_adjustment_new(0, 0, 99999, 1, 10, 0);
138         image_cache_size = gtk_spin_button_new(GTK_ADJUSTMENT(adj), 1, 0);
139         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(image_cache_size), TRUE);
140         gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(image_cache_size), FALSE);
141         gtk_spin_button_set_value(GTK_SPIN_BUTTON(image_cache_size),
142                         lh_prefs.image_cache_size);
143         gtk_box_pack_start(GTK_BOX(hbox), image_cache_size, FALSE, FALSE, 0);
144         gtk_widget_show_all(hbox);
145
146         /* Font */
147         hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
148         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
149
150         label = gtk_label_new(_("Default font"));
151         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
152
153         default_font = gtk_font_button_new_with_font(lh_prefs.default_font);
154         g_object_set(G_OBJECT(default_font), "use-font", TRUE, NULL);
155         gtk_box_pack_start(GTK_BOX(hbox), default_font, FALSE, FALSE, 0);
156
157         gtk_widget_show_all(hbox);
158
159         prefs_page->enable_remote_content = enable_remote_content;
160         prefs_page->image_cache_size = image_cache_size;
161         prefs_page->default_font = default_font;
162         prefs_page->page.widget = vbox;
163 }
164
165 static void destroy_lh_prefs_page(PrefsPage *page)
166 {
167 }
168
169 static void save_lh_prefs_page(PrefsPage *page)
170 {
171         LHPrefsPage *prefs_page = (LHPrefsPage *)page;
172
173         lh_prefs.enable_remote_content = gtk_toggle_button_get_active(
174                         GTK_TOGGLE_BUTTON(prefs_page->enable_remote_content));
175
176         lh_prefs.image_cache_size = gtk_spin_button_get_value_as_int(
177                         GTK_SPIN_BUTTON(prefs_page->image_cache_size));
178
179         g_free(lh_prefs.default_font);
180         lh_prefs.default_font = g_strdup(gtk_font_chooser_get_font(
181                         GTK_FONT_CHOOSER(prefs_page->default_font)));
182
183         save_prefs();
184 }
185
186 static void save_prefs(void)
187 {
188         PrefFile *pref_file;
189         gchar *rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
190                         COMMON_RC, NULL);
191
192         pref_file = prefs_write_open(rcpath);
193
194         if (!pref_file) {
195                 g_warning("failed to open configuration file '%s' for writing", rcpath);
196                 g_free(rcpath);
197                 return;
198         }
199         if (prefs_set_block_label(pref_file, PREFS_BLOCK_NAME) < 0) {
200                 g_warning("failed to set block label "PREFS_BLOCK_NAME);
201                 g_free(rcpath);
202                 return;
203         }
204
205         if (prefs_write_param(param, pref_file->fp) < 0) {
206                 g_warning("failed to write LiteHTML Viewer plugin configuration");
207                 prefs_file_close_revert(pref_file);
208                 g_free(rcpath);
209                 return;
210         }
211
212         if (fprintf(pref_file->fp, "\n") < 0) {
213                 FILE_OP_ERROR(rcpath, "fprintf");
214                 prefs_file_close_revert(pref_file);
215         } else {
216                 debug_print("successfully saved LiteHTML Viewer plugin configuration\n");
217                 prefs_file_close(pref_file);
218         }
219
220         g_free(rcpath);
221 }