Fix error creation and warning
[claws.git] / src / prefs_proxy.c
blob6956986e71f300d380f119e8215e80fb77c16a17
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 2018-2019 the Claws Mail team
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.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "config.h"
22 #include <glib.h>
23 #include <glib/gi18n.h>
24 #include <gtk/gtk.h>
26 #include "common/defs.h"
27 #include "common/proxy.h"
29 #include "gtk/menu.h"
31 #include "prefs_common.h"
32 #include "prefs_gtk.h"
33 #include "passwordstore.h"
35 typedef struct _ProxyPage
37 PrefsPage page;
39 GtkWidget *proxy_checkbtn;
40 GtkWidget *socks4_radiobtn;
41 GtkWidget *socks5_radiobtn;
42 GtkWidget *proxy_host_entry;
43 GtkWidget *proxy_port_spinbtn;
44 GtkWidget *proxy_auth_checkbtn;
45 GtkWidget *proxy_name_entry;
46 GtkWidget *proxy_pass_entry;
47 } ProxyPage;
49 static void showpwd_checkbtn_toggled(GtkToggleButton *button,
50 gpointer user_data);
52 static void prefs_proxy_create_widget(PrefsPage *_page, GtkWindow *window,
53 gpointer data)
55 ProxyPage *page = (ProxyPage *)_page;
57 GtkWidget *vbox0, *vbox1, *vbox2;
58 GtkWidget *hbox;
59 GtkWidget *label;
60 GtkWidget *proxy_checkbtn;
61 GtkWidget *socks4_radiobtn, *socks5_radiobtn;
62 GtkWidget *proxy_auth_checkbtn;
63 GtkWidget *proxy_frame;
64 GtkWidget *proxy_host_entry;
65 GtkWidget *proxy_port_spinbtn;
66 GtkWidget *proxy_name_entry;
67 GtkWidget *proxy_pass_entry;
68 GtkWidget *button;
69 GtkWidget *table;
70 gchar *buf;
72 vbox0 = gtk_box_new(GTK_ORIENTATION_VERTICAL, VSPACING);
73 gtk_container_set_border_width(GTK_CONTAINER(vbox0), VBOX_BORDER);
75 proxy_checkbtn = gtk_check_button_new_with_label(_("Use proxy server"));
76 PACK_FRAME(vbox0, proxy_frame, NULL);
77 gtk_frame_set_label_widget(GTK_FRAME(proxy_frame), proxy_checkbtn);
79 vbox1 = gtk_box_new(GTK_ORIENTATION_VERTICAL, VSPACING_NARROW);
80 gtk_container_set_border_width(GTK_CONTAINER(vbox1), 8);
81 gtk_container_add(GTK_CONTAINER(proxy_frame), vbox1);
83 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
84 gtk_box_pack_start(GTK_BOX(vbox1), hbox, FALSE, FALSE, 0);
86 socks4_radiobtn = gtk_radio_button_new_with_label(NULL, "SOCKS4");
87 gtk_box_pack_start(GTK_BOX(hbox), socks4_radiobtn, FALSE, FALSE, 0);
88 g_object_set_data(G_OBJECT(socks4_radiobtn), MENU_VAL_ID,
89 GINT_TO_POINTER(PROXY_SOCKS4));
91 socks5_radiobtn = gtk_radio_button_new_with_label_from_widget(
92 GTK_RADIO_BUTTON(socks4_radiobtn), "SOCKS5");
93 gtk_box_pack_start(GTK_BOX(hbox), socks5_radiobtn, FALSE, FALSE, 0);
94 g_object_set_data(G_OBJECT(socks5_radiobtn), MENU_VAL_ID,
95 GINT_TO_POINTER(PROXY_SOCKS5));
97 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
98 gtk_box_pack_start(GTK_BOX(vbox1), hbox, FALSE, FALSE, 0);
100 label = gtk_label_new(_("Hostname"));
101 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
103 proxy_host_entry = gtk_entry_new();
104 gtk_widget_set_size_request(proxy_host_entry, DEFAULT_ENTRY_WIDTH, -1);
105 gtk_box_pack_start(GTK_BOX(hbox), proxy_host_entry, TRUE, TRUE, 0);
107 label = gtk_label_new(_("Port"));
108 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
110 proxy_port_spinbtn = gtk_spin_button_new_with_range(0, 65535, 1080);
111 gtk_widget_set_size_request(proxy_port_spinbtn, 64, -1);
112 gtk_box_pack_start(GTK_BOX(hbox), proxy_port_spinbtn, FALSE, FALSE, 0);
114 vbox2 = gtk_box_new(GTK_ORIENTATION_VERTICAL, VSPACING_NARROW);
115 gtk_box_pack_start(GTK_BOX(vbox1), vbox2, FALSE, FALSE, 0);
117 PACK_CHECK_BUTTON(vbox2, proxy_auth_checkbtn, _("Use authentication"));
119 table = gtk_grid_new();
121 gtk_grid_set_row_spacing(GTK_GRID(table), VSPACING_NARROW);
122 gtk_grid_set_column_spacing(GTK_GRID(table), 9);
123 gtk_box_pack_start(GTK_BOX(vbox2), table, FALSE, FALSE, 0);
125 label = gtk_label_new(_("Username"));
126 gtk_grid_attach(GTK_GRID(table), label, 0, 0, 1, 1);
128 proxy_name_entry = gtk_entry_new();
129 gtk_widget_set_size_request(proxy_name_entry, DEFAULT_ENTRY_WIDTH, -1);
130 gtk_grid_attach(GTK_GRID(table), proxy_name_entry, 1, 0, 1, 1);
131 gtk_widget_set_hexpand(proxy_name_entry, TRUE);
132 gtk_widget_set_halign(proxy_name_entry, GTK_ALIGN_FILL);
134 label = gtk_label_new(_("Password"));
135 gtk_grid_attach(GTK_GRID(table), label, 2, 0, 1, 1);
137 proxy_pass_entry = gtk_entry_new();
138 gtk_widget_set_size_request(proxy_pass_entry, DEFAULT_ENTRY_WIDTH, -1);
139 gtk_entry_set_visibility(GTK_ENTRY(proxy_pass_entry), FALSE);
140 gtk_grid_attach(GTK_GRID(table), proxy_pass_entry, 3, 0, 1, 1);
141 gtk_widget_set_hexpand(proxy_pass_entry, TRUE);
142 gtk_widget_set_halign(proxy_pass_entry, GTK_ALIGN_FILL);
144 button = gtk_check_button_new_with_label(_("Show password"));
145 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), FALSE);
146 gtk_grid_attach(GTK_GRID(table), button, 3, 1, 1, 1);
147 g_signal_connect(G_OBJECT(button), "toggled",
148 G_CALLBACK(showpwd_checkbtn_toggled), proxy_pass_entry);
150 gtk_widget_show_all(vbox0);
152 SET_TOGGLE_SENSITIVITY(proxy_checkbtn, vbox1);
153 SET_TOGGLE_SENSITIVITY(proxy_auth_checkbtn, table);
154 SET_TOGGLE_SENSITIVITY(socks5_radiobtn, vbox2);
156 /* Set widgets to their correct states, based on prefs. */
157 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(proxy_checkbtn),
158 prefs_common.use_proxy);
159 if (prefs_common.proxy_info.proxy_type == PROXY_SOCKS4)
160 button = socks4_radiobtn;
161 else
162 button = socks5_radiobtn;
163 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), TRUE);
164 gtk_entry_set_text(GTK_ENTRY(proxy_host_entry),
165 prefs_common.proxy_info.proxy_host);
166 gtk_spin_button_set_value(GTK_SPIN_BUTTON(proxy_port_spinbtn),
167 (gdouble)prefs_common.proxy_info.proxy_port);
168 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(proxy_auth_checkbtn),
169 prefs_common.proxy_info.use_proxy_auth);
170 gtk_entry_set_text(GTK_ENTRY(proxy_name_entry),
171 prefs_common.proxy_info.proxy_name);
173 buf =
174 passwd_store_get(PWS_CORE, PWS_CORE_PROXY, PWS_CORE_PROXY_PASS);
175 gtk_entry_set_text(GTK_ENTRY(proxy_pass_entry), buf != NULL ? buf : "");
176 if (buf != NULL) {
177 memset(buf, 0, strlen(buf));
178 g_free(buf);
181 page->proxy_checkbtn = proxy_checkbtn;
182 page->socks4_radiobtn = socks4_radiobtn;
183 page->socks5_radiobtn = socks5_radiobtn;
184 page->proxy_host_entry = proxy_host_entry;
185 page->proxy_port_spinbtn = proxy_port_spinbtn;
186 page->proxy_auth_checkbtn = proxy_auth_checkbtn;
187 page->proxy_name_entry = proxy_name_entry;
188 page->proxy_pass_entry = proxy_pass_entry;
189 page->page.widget = vbox0;
193 static void prefs_proxy_destroy_widget(PrefsPage *_page)
198 static void prefs_proxy_save(PrefsPage *_page)
200 ProxyPage *page = (ProxyPage *)_page;
202 prefs_common.use_proxy = gtk_toggle_button_get_active(
203 GTK_TOGGLE_BUTTON(page->proxy_checkbtn));
205 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->socks4_radiobtn)))
206 prefs_common.proxy_info.proxy_type = PROXY_SOCKS4;
207 else
208 prefs_common.proxy_info.proxy_type = PROXY_SOCKS5;
210 g_free(prefs_common.proxy_info.proxy_host);
211 prefs_common.proxy_info.proxy_host = g_strdup(gtk_entry_get_text(
212 GTK_ENTRY(page->proxy_host_entry)));
214 prefs_common.proxy_info.proxy_port = gtk_spin_button_get_value_as_int(
215 GTK_SPIN_BUTTON(page->proxy_port_spinbtn));
217 prefs_common.proxy_info.use_proxy_auth = gtk_toggle_button_get_active(
218 GTK_TOGGLE_BUTTON(page->proxy_auth_checkbtn));
220 g_free(prefs_common.proxy_info.proxy_name);
221 prefs_common.proxy_info.proxy_name = g_strdup(gtk_entry_get_text(
222 GTK_ENTRY(page->proxy_name_entry)));
224 passwd_store_set(PWS_CORE, PWS_CORE_PROXY, PWS_CORE_PROXY_PASS,
225 gtk_entry_get_text(GTK_ENTRY(page->proxy_pass_entry)), FALSE);
229 ProxyPage *prefs_proxy;
231 void prefs_proxy_init(void)
233 ProxyPage *page;
234 static gchar *path[3];
236 path[0] = _("Mail Handling");
237 path[1] = _("Proxy");
238 path[2] = NULL;
240 page = g_new0(ProxyPage, 1);
241 page->page.path = path;
242 page->page.create_widget = prefs_proxy_create_widget;
243 page->page.destroy_widget = prefs_proxy_destroy_widget;
244 page->page.save_page = prefs_proxy_save;
245 page->page.weight = 190.0;
247 prefs_gtk_register_page((PrefsPage *)page);
248 prefs_proxy = page;
251 void prefs_proxy_done(void)
253 prefs_gtk_unregister_page((PrefsPage *)prefs_proxy);
254 g_free(prefs_proxy);
257 static void showpwd_checkbtn_toggled(GtkToggleButton *button,
258 gpointer user_data)
260 gboolean active = gtk_toggle_button_get_active(button);
261 GtkWidget *entry = GTK_WIDGET(user_data);
263 gtk_entry_set_visibility(GTK_ENTRY(entry), active);