fec2ff56fa02af7289691b66bbd7f06a8664bba4
[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 };
47 typedef struct _LHPrefsPage LHPrefsPage;
48
49 static PrefParam param[] = {
50         { "enable_remote_content", "FALSE", &lh_prefs.enable_remote_content, P_BOOL,
51                 NULL, NULL, NULL },
52         { NULL, NULL, NULL, 0, NULL, NULL, NULL }
53 };
54
55 static LHPrefsPage lh_prefs_page;
56
57 LHPrefs *lh_prefs_get(void)
58 {
59         return &lh_prefs;
60 }
61
62 void lh_prefs_init(void)
63 {
64         static gchar *path[3];
65         gchar *rcpath;
66
67         path[0] = _("Plugins");
68         path[1] = "LiteHTML";
69         path[2] = NULL;
70
71         prefs_set_default(param);
72         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
73         prefs_read_config(param, PREFS_BLOCK_NAME, rcpath, NULL);
74         g_free(rcpath);
75
76         lh_prefs_page.page.path = path;
77         lh_prefs_page.page.create_widget = create_lh_prefs_page;
78         lh_prefs_page.page.destroy_widget = destroy_lh_prefs_page;
79         lh_prefs_page.page.save_page = save_lh_prefs_page;
80         lh_prefs_page.page.weight = 30.0;
81         prefs_gtk_register_page((PrefsPage *) &lh_prefs_page);
82 }
83
84 void lh_prefs_done(void)
85 {
86 }
87
88 static void create_lh_prefs_page(PrefsPage *page, GtkWindow *window,
89                 gpointer data)
90 {
91         LHPrefsPage *prefs_page = (LHPrefsPage *)page;
92         GtkWidget *vbox;
93         GtkWidget *vbox_remote;
94         GtkWidget *frame;
95         GtkWidget *label;
96         GtkWidget *enable_remote_content;
97
98         vbox = gtk_vbox_new(FALSE, 3);
99         gtk_container_set_border_width(GTK_CONTAINER(vbox), VBOX_BORDER);
100         gtk_widget_show(vbox);
101
102         /* Enable remote content */
103         vbox_remote = gtkut_get_options_frame(vbox, &frame, _("Remote resources"));
104
105         label = gtk_label_new(_("Loading remote resources can lead to some privacy issues.\n"
106                                 "When remote content loading is disabled, nothing will be requested\n"
107                                 "from the network."));
108         gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
109
110         enable_remote_content = gtk_check_button_new_with_label(_("Enable loading of remote content"));
111         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(enable_remote_content),
112                         lh_prefs.enable_remote_content);
113
114         gtk_box_pack_start(GTK_BOX(vbox_remote), label, FALSE, FALSE, 0);
115         gtk_box_pack_start(GTK_BOX(vbox_remote), enable_remote_content, FALSE, FALSE, 0);
116         gtk_widget_show_all(vbox_remote);
117
118         prefs_page->enable_remote_content = enable_remote_content;
119         prefs_page->page.widget = vbox;
120 }
121
122 static void destroy_lh_prefs_page(PrefsPage *page)
123 {
124 }
125
126 static void save_lh_prefs_page(PrefsPage *page)
127 {
128         LHPrefsPage *prefs_page = (LHPrefsPage *)page;
129
130         lh_prefs.enable_remote_content = gtk_toggle_button_get_active(
131                         GTK_TOGGLE_BUTTON(prefs_page->enable_remote_content));
132
133         save_prefs();
134 }
135
136 static void save_prefs(void)
137 {
138         PrefFile *pref_file;
139         gchar *rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
140                         COMMON_RC, NULL);
141
142         pref_file = prefs_write_open(rcpath);
143
144         if (!pref_file) {
145                 g_warning("failed to open configuration file '%s' for writing", rcpath);
146                 g_free(rcpath);
147                 return;
148         }
149         if (prefs_set_block_label(pref_file, PREFS_BLOCK_NAME) < 0) {
150                 g_warning("failed to set block label "PREFS_BLOCK_NAME);
151                 g_free(rcpath);
152                 return;
153         }
154
155         if (prefs_write_param(param, pref_file->fp) < 0) {
156                 g_warning("failed to write LiteHTML Viewer plugin configuration");
157                 prefs_file_close_revert(pref_file);
158                 g_free(rcpath);
159                 return;
160         }
161
162         if (fprintf(pref_file->fp, "\n") < 0) {
163                 FILE_OP_ERROR(rcpath, "fprintf");
164                 prefs_file_close_revert(pref_file);
165         } else {
166                 debug_print("successfully saved LiteHTML Viewer plugin configuration\n");
167                 prefs_file_close(pref_file);
168         }
169
170         g_free(rcpath);
171 }