summaryrefslogtreecommitdiffstats
path: root/panels/network/net-proxy.c
blob: fc6c7448c4aef1bbed0520fb4330ed5f09824494 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2011-2012 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <glib-object.h>
#include <glib/gi18n.h>
#include <gio/gio.h>

#include "net-proxy.h"

typedef enum
{
        MODE_DISABLED,
        MODE_MANUAL,
        MODE_AUTOMATIC
} ProxyMode;

struct _NetProxy
{
        AdwBin            parent;

        GtkCheckButton   *automatic_radio;
        GtkDialog        *dialog;
        GtkCheckButton   *manual_radio;
        GtkCheckButton   *none_radio;
        GtkEntry         *proxy_ftp_entry;
        GtkEntry         *proxy_http_entry;
        GtkEntry         *proxy_https_entry;
        GtkEntry         *proxy_ignore_entry;
        GtkAdjustment    *proxy_port_ftp_adjustment;
        GtkAdjustment    *proxy_port_http_adjustment;
        GtkAdjustment    *proxy_port_https_adjustment;
        GtkAdjustment    *proxy_port_socks_adjustment;
        GtkEntry         *proxy_socks_entry;
        GtkEntry         *proxy_url_entry;
        GtkLabel         *proxy_warning_label;
        GtkStack         *stack;
        GtkLabel         *status_label;

        GSettings        *settings;
};

G_DEFINE_TYPE (NetProxy, net_proxy, ADW_TYPE_BIN)

static const gchar *
panel_get_string_for_value (ProxyMode mode)
{
        switch (mode) {
        case MODE_DISABLED:
                return _("Off");
        case MODE_MANUAL:
                return _("Manual");
        case MODE_AUTOMATIC:
                return _("Automatic");
        default:
                g_assert_not_reached ();
        }
}

static inline void
panel_update_status_label (NetProxy  *self,
                           ProxyMode  mode)
{
        gtk_label_set_label (self->status_label, panel_get_string_for_value (mode));
}

static void
check_wpad_warning (NetProxy *self)
{
        g_autofree gchar *autoconfig_url = NULL;
        GString *string = NULL;
        gboolean ret = FALSE;
        guint mode;

        string = g_string_new ("");

        /* check we're using 'Automatic' */
        mode = g_settings_get_enum (self->settings, "mode");
        if (mode != MODE_AUTOMATIC)
                goto out;

        /* see if the PAC is blank */
        autoconfig_url = g_settings_get_string (self->settings,
                                                "autoconfig-url");
        ret = autoconfig_url == NULL ||
              autoconfig_url[0] == '\0';
        if (!ret)
                goto out;

        g_string_append (string, "<small>");

        /* TRANSLATORS: this is when the use leaves the PAC textbox blank */
        g_string_append (string, _("Web Proxy Autodiscovery is used when a Configuration URL is not provided."));

        g_string_append (string, "\n");

        /* TRANSLATORS: WPAD is bad: if you enable it on an untrusted
         * network, then anyone else on that network can tell your
         * machine that it should proxy all of your web traffic
         * through them. */
        g_string_append (string, _("This is not recommended for untrusted public networks."));
        g_string_append (string, "</small>");
out:
        gtk_label_set_markup (self->proxy_warning_label, string->str);
        gtk_widget_set_visible (GTK_WIDGET (self->proxy_warning_label), (string->len > 0));

        g_string_free (string, TRUE);
}

static void
settings_changed_cb (NetProxy *self)
{
        check_wpad_warning (self);
}

static void
panel_proxy_mode_setup_widgets (NetProxy *self, ProxyMode value)
{
        /* hide or show the PAC text box */
        switch (value) {
        case MODE_DISABLED:
                gtk_stack_set_visible_child_name (self->stack, "disabled");
                break;
        case MODE_MANUAL:
                gtk_stack_set_visible_child_name (self->stack, "manual");
                break;
        case MODE_AUTOMATIC:
                gtk_stack_set_visible_child_name (self->stack, "automatic");
                break;
        default:
                g_assert_not_reached ();
        }

        /* perhaps show the wpad warning */
        check_wpad_warning (self);
}

static void
panel_proxy_mode_radio_changed_cb (NetProxy *self, GtkCheckButton *radio)
{
        ProxyMode value;

        if (!gtk_check_button_get_active (GTK_CHECK_BUTTON (radio)))
                return;

        /* get selected radio */
        if (radio == self->none_radio)
                value = MODE_DISABLED;
        else if (radio == self->manual_radio)
                value = MODE_MANUAL;
        else if (radio == self->automatic_radio)
                value = MODE_AUTOMATIC;
        else
                g_assert_not_reached ();

        /* set */
        g_settings_set_enum (self->settings, "mode", value);

        /* hide or show the correct widgets */
        panel_proxy_mode_setup_widgets (self, value);

        /* status label */
        panel_update_status_label (self, value);
}

static void
show_dialog_cb (NetProxy *self)
{
        gtk_window_set_transient_for (GTK_WINDOW (self->dialog), GTK_WINDOW (gtk_widget_get_native (GTK_WIDGET (self))));
        gtk_window_present (GTK_WINDOW (self->dialog));
}

static void
net_proxy_finalize (GObject *object)
{
        NetProxy *self = NET_PROXY (object);

        g_clear_object (&self->settings);

        G_OBJECT_CLASS (net_proxy_parent_class)->finalize (object);
}

static void
net_proxy_class_init (NetProxyClass *klass)
{
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
        GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

        object_class->finalize = net_proxy_finalize;

        gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/network/network-proxy.ui");

        gtk_widget_class_bind_template_child (widget_class, NetProxy, automatic_radio);
        gtk_widget_class_bind_template_child (widget_class, NetProxy, dialog);
        gtk_widget_class_bind_template_child (widget_class, NetProxy, manual_radio);
        gtk_widget_class_bind_template_child (widget_class, NetProxy, none_radio);
        gtk_widget_class_bind_template_child (widget_class, NetProxy, proxy_ftp_entry);
        gtk_widget_class_bind_template_child (widget_class, NetProxy, proxy_http_entry);
        gtk_widget_class_bind_template_child (widget_class, NetProxy, proxy_https_entry);
        gtk_widget_class_bind_template_child (widget_class, NetProxy, proxy_ignore_entry);
        gtk_widget_class_bind_template_child (widget_class, NetProxy, proxy_port_ftp_adjustment);
        gtk_widget_class_bind_template_child (widget_class, NetProxy, proxy_port_http_adjustment);
        gtk_widget_class_bind_template_child (widget_class, NetProxy, proxy_port_https_adjustment);
        gtk_widget_class_bind_template_child (widget_class, NetProxy, proxy_port_socks_adjustment);
        gtk_widget_class_bind_template_child (widget_class, NetProxy, proxy_socks_entry);
        gtk_widget_class_bind_template_child (widget_class, NetProxy, proxy_url_entry);
        gtk_widget_class_bind_template_child (widget_class, NetProxy, proxy_warning_label);
        gtk_widget_class_bind_template_child (widget_class, NetProxy, stack);
        gtk_widget_class_bind_template_child (widget_class, NetProxy, status_label);

        gtk_widget_class_bind_template_callback (widget_class, panel_proxy_mode_radio_changed_cb);
        gtk_widget_class_bind_template_callback (widget_class, show_dialog_cb);
}

static gboolean
get_ignore_hosts (GValue   *value,
                  GVariant *variant,
                  gpointer  user_data)
{
        GVariantIter iter;
        const gchar *s;
        g_autofree gchar **av = NULL;
        gchar **p;
        gsize n;

        n = g_variant_iter_init (&iter, variant);
        p = av = g_new0 (gchar *, n + 1);

        while (g_variant_iter_next (&iter, "&s", &s))
                if (s[0] != '\0') {
                        *p = (gchar *) s;
                        ++p;
                }

        g_value_take_string (value, g_strjoinv (", ", av));

        return TRUE;
}

static GVariant *
set_ignore_hosts (const GValue       *value,
                  const GVariantType *expected_type,
                  gpointer            user_data)
{
        GVariantBuilder builder;
        const gchar *sv;
        gchar **av, **p;

        sv = g_value_get_string (value);
        av = g_strsplit_set (sv, ", ", 0);

        g_variant_builder_init (&builder, G_VARIANT_TYPE_STRING_ARRAY);
        for (p = av; *p; ++p) {
                if (*p[0] != '\0')
                        g_variant_builder_add (&builder, "s", *p);
        }

        g_strfreev (av);

        return g_variant_builder_end (&builder);
}

static void
net_proxy_init (NetProxy *self)
{
        g_autoptr(GSettings) http_settings = NULL;
        g_autoptr(GSettings) https_settings = NULL;
        g_autoptr(GSettings) ftp_settings = NULL;
        g_autoptr(GSettings) socks_settings = NULL;
        ProxyMode value;

        gtk_widget_init_template (GTK_WIDGET (self));

        self->settings = g_settings_new ("org.gnome.system.proxy");
        g_signal_connect_object (self->settings,
                                 "changed",
                                 G_CALLBACK (settings_changed_cb),
                                 self,
                                 G_CONNECT_SWAPPED);

        /* actions */
        value = g_settings_get_enum (self->settings, "mode");

        /* bind the proxy values */
        g_settings_bind (self->settings, "autoconfig-url",
                         self->proxy_url_entry, "text",
                         G_SETTINGS_BIND_DEFAULT);

        /* bind the HTTP proxy values */
        http_settings = g_settings_get_child (self->settings, "http");
        g_settings_bind (http_settings, "host",
                         self->proxy_http_entry, "text",
                         G_SETTINGS_BIND_DEFAULT);
        g_settings_bind (http_settings, "port",
                         self->proxy_port_http_adjustment, "value",
                         G_SETTINGS_BIND_DEFAULT);

        /* bind the HTTPS proxy values */
        https_settings = g_settings_get_child (self->settings, "https");
        g_settings_bind (https_settings, "host",
                         self->proxy_https_entry, "text",
                         G_SETTINGS_BIND_DEFAULT);
        g_settings_bind (https_settings, "port",
                         self->proxy_port_https_adjustment, "value",
                         G_SETTINGS_BIND_DEFAULT);

        /* bind the FTP proxy values */
        ftp_settings = g_settings_get_child (self->settings, "ftp");
        g_settings_bind (ftp_settings, "host",
                         self->proxy_ftp_entry, "text",
                         G_SETTINGS_BIND_DEFAULT);
        g_settings_bind (ftp_settings, "port",
                         self->proxy_port_ftp_adjustment, "value",
                         G_SETTINGS_BIND_DEFAULT);

        /* bind the SOCKS proxy values */
        socks_settings = g_settings_get_child (self->settings, "socks");
        g_settings_bind (socks_settings, "host",
                         self->proxy_socks_entry, "text",
                         G_SETTINGS_BIND_DEFAULT);
        g_settings_bind (socks_settings, "port",
                         self->proxy_port_socks_adjustment, "value",
                         G_SETTINGS_BIND_DEFAULT);

        /* bind the proxy ignore hosts */
        g_settings_bind_with_mapping (self->settings, "ignore-hosts",
                                      self->proxy_ignore_entry, "text",
                                      G_SETTINGS_BIND_DEFAULT, get_ignore_hosts, set_ignore_hosts,
                                      NULL, NULL);

        /* setup the radio before connecting to the :toggled signal */
        switch (value) {
        case MODE_DISABLED:
                gtk_check_button_set_active (GTK_CHECK_BUTTON (self->none_radio), TRUE);
                break;
        case MODE_MANUAL:
                gtk_check_button_set_active (GTK_CHECK_BUTTON (self->manual_radio), TRUE);
                break;
        case MODE_AUTOMATIC:
                gtk_check_button_set_active (GTK_CHECK_BUTTON (self->automatic_radio), TRUE);
                break;
        default:
                g_assert_not_reached ();
        }
        panel_proxy_mode_setup_widgets (self, value);
        panel_update_status_label (self, value);
}

NetProxy *
net_proxy_new (void)
{
        return g_object_new (net_proxy_get_type (), NULL);
}