Debian Codegraph @

src/settings.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * ConnMan GTK GUI
3 *
4 * Copyright (C) 2015 Intel Corporation. All rights reserved.
5 * Author: Jaakko Hannikainen <[email protected]>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <glib.h>
22 #include <glib/gi18n.h>
23 #include <gtk/gtk.h>
24
25 #include "config.h"
26 #include "connection.h"
27 #include "service.h"
28 #include "settings.h"
29 #include "settings_content.h"
30 #include "style.h"
31 #include "util.h"
32
33 static void free_page(GtkWidget *widget, gpointer user_data)
34 {
35 struct settings_page *page = user_data;
36 g_free(page);
37 }
38
39 static struct settings_page *create_page(GtkWidget *notebook, gboolean scrolled)
40 {
41 struct settings_page *page = g_malloc(sizeof(*page));
42
43 page->index = 0;
44 page->grid = gtk_grid_new();
45 page->item = page->grid;
46
47 if(scrolled) {
48 GtkWidget *scroll = gtk_scrolled_window_new(NULL, NULL);
49 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
50 GTK_POLICY_NEVER,
51 GTK_POLICY_AUTOMATIC);
52 gtk_container_add(GTK_CONTAINER(scroll), page->item);
53 page->item = scroll;
54 }
55
56 g_signal_connect(page->item, "destroy",
57 G_CALLBACK(free_page), page);
58 style_set_margin_start(page->item, MARGIN_LARGE);
59 gtk_widget_show_all(page->item);
60 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), page->item, NULL);
61 return page;
62 }
63
64 void page_mnemonic(GtkWidget *widget, gboolean arg1, gpointer user_data)
65 {
66 list_item_selected(NULL, GTK_LIST_BOX_ROW(widget), user_data);
67 }
68
69 static struct settings_page *add_page_to_settings(struct settings *sett,
70 const gchar *name, gboolean scrolled)
71 {
72 struct settings_page *page;
73 GtkWidget *item, *label;
74
75 page = create_page(sett->notebook, scrolled);
76 page->sett = sett;
77
78 item = gtk_list_box_row_new();
79 label = gtk_label_new_with_mnemonic(name);
80
81 g_signal_connect(item, "mnemonic-activate", G_CALLBACK(page_mnemonic),
82 sett->notebook);
83 g_object_set_data(G_OBJECT(item), "content", page->item);
84
85 style_set_margin(label, MARGIN_SMALL);
86 style_set_margin_start(label, MARGIN_LARGE);
87 style_set_margin_end(label, MARGIN_LARGE);
88
89 gtk_widget_set_halign(label, GTK_ALIGN_START);
90
91 gtk_container_add(GTK_CONTAINER(item), label);
92 gtk_container_add(GTK_CONTAINER(sett->list), item);
93
94 gtk_widget_show_all(item);
95
96 return page;
97 }
98
99 static struct settings_page *add_page_to_combo_box(struct settings *sett,
100 GtkWidget *box,
101 const gchar *id,
102 const gchar *name,
103 gboolean active)
104 {
105 GtkWidget *notebook = g_object_get_data(G_OBJECT(box), "notebook");
106 GHashTable *items = g_object_get_data(G_OBJECT(box), "items");
107 struct settings_page *page = create_page(notebook, FALSE);
108 page->sett = sett;
109 g_hash_table_insert(items, g_strdup(name), page->item);
110 gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(box), id, name);
111 if(active)
112 g_object_set_data(G_OBJECT(page->item), "selected", page->item);
113 else
114 g_object_set_data(G_OBJECT(page->item), "selected", NULL);
115 if(active)
116 gtk_combo_box_set_active_id(GTK_COMBO_BOX(box), id);
117 return page;
118 }
119
120 static void free_settings(struct settings *sett)
121 {
122 sett->closed(sett->serv);
123 dual_hash_table_unref(sett->callbacks);
124 dual_hash_table_unref(sett->contents);
125 g_free(sett);
126 }
127
128 static void add_security(struct settings *sett, struct settings_page *page)
129 {
130 GVariant *security;
131 GVariant *child;
132 if(sett->serv->type != CONNECTION_TYPE_WIRELESS)
133 return;
134 security = service_get_property(sett->serv, "Security", NULL);
135 child = g_variant_get_child_value(security, 0);
136 if(!strcmp(g_variant_get_string(child, NULL), "none")) {
137 settings_add_static_text(page, _("Security"), _("None"));
138 goto out;
139 }
140 else
141 settings_add_text(page, _("Security"), "Security", NULL);
142
143 out:
144 g_variant_unref(child);
145 g_variant_unref(security);
146 }
147
148 static void add_info_page(struct settings *sett)
149 {
150 struct settings_page *page = add_page_to_settings(sett, _("_Info"),
151 FALSE);
152
153 settings_add_switch(sett, page, always_write, _("Autoconnect"),
154 "AutoConnect", NULL);
155
156 settings_add_text(page, _("Name"), "Name", NULL);
157 settings_add_text(page, _("State"), "State", NULL);
158 add_security(sett, page);
159 settings_add_text(page, _("MAC address"), "Ethernet", "Address");
160 settings_add_text(page, _("Interface"), "Ethernet", "Interface");
161 settings_add_text(page, _("IPv4 address"), "IPv4", "Address");
162 settings_add_text(page, _("IPv6 address"), "IPv6", "Address");
163 settings_add_text(page, _("Nameservers"), "Nameservers", NULL);
164 }
165
166 static gboolean valid_ipv4_entry(GtkWidget *entry)
167 {
168 const gchar *value = gtk_entry_get_text(GTK_ENTRY(entry));
169 return valid_ipv4(value);
170 }
171
172 static gboolean valid_ipv6_entry(GtkWidget *entry)
173 {
174 const gchar *value = gtk_entry_get_text(GTK_ENTRY(entry));
175 return valid_ipv6(value);
176 }
177
178 static gboolean valid_address_entry(GtkWidget *entry)
179 {
180 const gchar *value = gtk_entry_get_text(GTK_ENTRY(entry));
181 return valid_ipv4(value) || valid_ipv6(value);
182 }
183
184 static void add_immutable_ipv_page(struct settings *sett)
185 {
186 struct settings_page *page;
187
188 page = add_page_to_settings(sett, _("IP _details"), FALSE);
189 settings_add_text(page, _("IPv4 method"), "IPv4", "Method");
190 settings_add_text(page, _("Address"), "IPv4", "Address");
191 settings_add_text(page, _("Gateway"), "IPv4", "Gateway");
192
193 settings_add_text(page, "", NULL, NULL);
194
195 settings_add_text(page, _("IPv6 method"), "IPv6", "Method");
196 settings_add_text(page, _("Address"), "IPv6", "Address");
197 settings_add_text(page, _("Gateway"), "IPv6", "Gateway");
198
199 return;
200 }
201
202 static void add_ipv_page(struct settings *sett, int ipv)
203 {
204 struct settings_page *page;
205 settings_entry_validator validator;
206 const char *local;
207 const char *conf;
208 const char *ipvs;
209 GtkWidget *box;
210 struct settings_page *none;
211 struct settings_page *dhcp;
212 struct settings_page *manual;
213 GtkWidget *ipv6_privacy;
214
215 if(ipv == 4) {
216 local = _("IPv_4");
217 validator = valid_ipv4_entry;
218 conf = "IPv4.Configuration";
219 ipvs = "IPv4";
220 } else {
221 local = _("IPv_6");
222 validator = valid_ipv6_entry;
223 conf = "IPv6.Configuration";
224 ipvs = "IPv6";
225 }
226 char *cur = service_get_property_string_raw(sett->serv, ipvs, "Method");
227 if(!strlen(cur)) {
228 g_free(cur);
229 cur = service_get_property_string_raw(sett->serv, conf, "Method");
230 if(!strlen(cur)) {
231 g_free(cur);
232 return;
233 }
234 }
235
236 page = add_page_to_settings(sett, local, FALSE);
237 box = settings_add_combo_box(sett, page, always_write, _("Method"),
238 conf, "Method", ipvs);
239
240 none = add_page_to_combo_box(sett, box, "off", _("None"), TRUE);
241
242 if(ipv == 4)
243 dhcp = add_page_to_combo_box(sett, box, "dhcp", _("Automatic"),
244 !strcmp("dhcp", cur) ||
245 !strcmp("fixed", cur));
246 else
247 dhcp = add_page_to_combo_box(sett, box, "auto", _("Automatic"),
248 !strcmp("auto", cur) ||
249 !strcmp("6to4", cur) ||
250 !strcmp("fixed", cur));
251
252 if(!strcmp(cur, "fixed"))
253 return;
254
255 manual = add_page_to_combo_box(sett, box, "manual", _("Manual"),
256 !strcmp("manual", cur));
257 g_free(cur);
258
259 settings_add_text(none, NULL, NULL, NULL);
260
261 settings_add_text(dhcp, _("Address"), ipvs, "Address");
262 settings_add_text(dhcp, _("Gateway"), ipvs, "Gateway");
263 if(ipv == 4)
264 settings_add_text(dhcp, _("Netmask"), ipvs, "Netmask");
265 else
266 settings_add_text(dhcp, _("Prefix length"), ipvs,
267 "PrefixLength");
268
269 settings_add_text(manual, _("Address"), NULL, NULL);
270 settings_add_entry(sett, manual, write_if_selected, NULL, conf,
271 "Address", ipvs, validator);
272 settings_add_text(manual, _("Gateway"), NULL, NULL);
273 settings_add_entry(sett, manual, write_if_selected, NULL, conf,
274 "Gateway", ipvs, validator);
275 if(ipv == 4) {
276 settings_add_text(manual, _("Netmask"), NULL, NULL);
277 settings_add_entry(sett, manual, write_if_selected, NULL, conf,
278 "Netmask", ipvs, validator);
279 } else {
280 settings_add_prefix_entry(sett, manual, write_if_selected);
281 gtk_widget_set_halign(manual->item, GTK_ALIGN_START);
282
283 ipv6_privacy = settings_add_combo_box(sett, dhcp,
284 write_if_selected,
285 _("Privacy"), conf,
286 "Privacy", ipvs);
287
288 cur = service_get_property_string_raw(sett->serv, ipvs, "Privacy");
289 add_page_to_combo_box(sett, ipv6_privacy, "disabled",
290 _("Disabled"), TRUE);
291 add_page_to_combo_box(sett, ipv6_privacy, "enabled",
292 _("Enabled"), !strcmp("enabled", cur));
293 add_page_to_combo_box(sett, ipv6_privacy, "prefered",
294 _("Prefered"), !strcmp("prefered", cur));
295 g_free(cur);
296 }
297 }
298
299 static void add_immutable_server_page(struct settings *sett)
300 {
301 struct settings_page *page;
302 gchar *cur = service_get_property_string_raw(sett->serv,
303 "Proxy", "Method");
304 page = add_page_to_settings(sett, _("_Servers"), TRUE);
305
306 settings_add_text(page, _("Nameservers"), "Nameservers", NULL);
307 settings_add_text(page, _("Timeservers"), "Timeservers", NULL);
308 settings_add_text(page, _("Domains"), "Domains", NULL);
309 settings_add_text(page, _("Proxy method"), "Proxy", "Method");
310
311 if(!strcmp(cur, "direct"));
312 else if(!strcmp(cur, "auto"))
313 settings_add_text(page, _("URL"), "Proxy", "URL");
314 else {
315 settings_add_text(page, _("Servers"), "Proxy",
316 "Servers");
317 settings_add_text(page, _("Excludes"), "Proxy",
318 "Excludes");
319 }
320
321 g_free(cur);
322 return;
323 }
324
325 static void add_server_page(struct settings *sett)
326 {
327 struct settings_page *page;
328
329 page = add_page_to_settings(sett, _("_Nameservers"), TRUE);
330 settings_add_entry_list(sett, page, always_write, _("Nameservers"),
331 "Nameservers.Configuration", NULL,
332 "Nameservers", valid_address_entry);
333
334 page = add_page_to_settings(sett, _("_Timeservers"), TRUE);
335 settings_add_text(page, _("Timeservers"), "Timeservers", NULL);
336 settings_add_static_text(page, "", "");;
337 settings_add_entry_list(sett, page, always_write,
338 _("Custom Timeservers"),
339 "Timeservers.Configuration", NULL,
340 NULL, always_valid);
341
342 page = add_page_to_settings(sett, _("_Domains"), TRUE);
343 settings_add_entry_list(sett, page, always_write, _("Domains"),
344 "Domains.Configuration", NULL,
345 "Domains", always_valid);
346 }
347
348 static void add_proxy_page(struct settings *sett)
349 {
350 struct settings_page *page, *direct, *automatic, *manual;
351 GtkWidget *box;
352 const gchar *conf = "Proxy.Configuration";
353 gchar *cur;
354
355 page = add_page_to_settings(sett, _("_Proxy"), TRUE);
356 cur = service_get_property_string_raw(sett->serv, "Proxy", "Method");
357 box = settings_add_combo_box(sett, page, always_write, _("Method"),
358 conf, "Method", "Proxy");
359
360 direct = add_page_to_combo_box(sett, box, "direct", _("Direct"),
361 !strlen(cur) || !strcmp("direct", cur));
362 automatic = add_page_to_combo_box(sett, box, "auto", _("Automatic"),
363 !strcmp("auto", cur));
364 manual = add_page_to_combo_box(sett, box, "manual", _("Manual"),
365 !strcmp("manual", cur));
366 g_free(cur);
367
368 settings_add_text(direct, NULL, NULL, NULL);
369
370 settings_add_entry(sett, automatic, write_if_selected, _("URL"),
371 conf, "URL", "Proxy", always_valid);
372
373 settings_add_entry_list(sett, manual, write_if_selected, _("Servers"),
374 conf, "Servers", "Proxy", always_valid);
375 settings_add_entry_list(sett, manual, write_if_selected, _("Excludes"),
376 conf, "Excludes", "Proxy", always_valid);
377 }
378
379 static void add_vpn_info_page(struct settings *sett)
380 {
381 struct settings_page *page = add_page_to_settings(sett, _("_Info"),
382 FALSE);
383
384 settings_add_text(page, _("Name"), "Name", NULL);
385 settings_add_text(page, _("Type"), "Type", NULL);
386 settings_add_text(page, _("State"), "State", NULL);
387 settings_add_text(page, _("Host"), "Host", NULL);
388 settings_add_text(page, _("IPv4 address"), "IPv4", "Address");
389 settings_add_text(page, _("IPv6 address"), "IPv6", "Address");
390 settings_add_text(page, _("Nameservers"), "Nameservers", NULL);
391 }
392
393 /*
394 * TODO: Reorder text fields
395 * TODO: Show empty fields properly
396 */
397 static void add_immutable_openconnect_page(struct settings *sett)
398 {
399 struct settings_page *page;
400
401 page = add_page_to_settings(sett, "Openconnect", TRUE);
402 settings_add_text(page, _("Server fingerprint"),
403 "OpenConnect.ServerCert", NULL);
404 settings_add_text(page, _("Certificate Authority file"),
405 "OpenConnect.CACert", NULL);
406 settings_add_text(page, _("Client certificate file"),
407 "OpenConnect.ClientCert", NULL);
408 settings_add_text(page, _("VPN host"),
409 "OpenConnect.VPNHost", NULL);
410 }
411
412 static void add_immutable_openvpn_page(struct settings *sett)
413 {
414 struct settings_page *page;
415
416 page = add_page_to_settings(sett, "OpenVPN", TRUE);
417 settings_add_text(page, _("Certificate Authority file"),
418 "OpenVPN.CACert", NULL);
419 settings_add_text(page, _("Client certificate file"),
420 "OpenVPN.Cert", NULL);
421 settings_add_text(page, _("Client private key file"),
422 "OpenVPN.Key", NULL);
423 settings_add_text(page, _("Maximum transmission unit (MTU)"),
424 "OpenVPN.MTU", NULL);
425 settings_add_text(page, _("Peer certificate type"),
426 "OpenVPN.NSCertType", NULL);
427 settings_add_text(page, _("Protocol"), "OpenVPN.Proto", NULL);
428 settings_add_text(page, _("Port"), "OpenVPN.Port", NULL);
429 settings_add_text(page, _("Authenticate using username/password"),
430 "OpenVPN.AuthUserPass", NULL);
431 settings_add_text(page, _("Private key password file"),
432 "OpenVPN.AskPass", NULL);
433 settings_add_text(page, _("Do not cache password"),
434 "OpenVPN.AuthNoCache", NULL);
435 settings_add_text(page, _("Cipher"), "OpenVPN.Cipher", NULL);
436 settings_add_text(page,
437 _("Message digest algorithm for packet authentication"),
438 "OpenVPN.Auth", NULL);
439 settings_add_text(page, _("Use fast LZO compression"),
440 "OpenVPN.CompLZO", NULL);
441 settings_add_text(page, _("External configuration file"),
442 "OpenVPN.ConfigFile", NULL);
443 }
444
445 static void add_immutable_vpnc_page(struct settings *sett)
446 {
447 struct settings_page *page;
448
449 page = add_page_to_settings(sett, "VPNC", TRUE);
450 settings_add_text(page, _("Group username"), "VPNC.IPSec.ID", NULL);
451 settings_add_text(page, _("Username"), "VPNC.XAuth.ID", NULL);
452 settings_add_text(page, _("IKE Authentication mode"),
453 "VPNC.IKE.Authmode", NULL);
454 settings_add_text(page, _("Diffie-Hellman group"), "VPNC.PFS", NULL);
455 settings_add_text(page, _("Domain"), "VPNC.Domain", NULL);
456 settings_add_text(page, _("Vendor"), "VPNC.Vendor", NULL);
457 settings_add_text(page, _("Local port"), "VPNC.LocalPort", NULL);
458 settings_add_text(page, _("Cisco port"), "VPNC.CiscoPort", NULL);
459 settings_add_text(page, _("Application version"), "VPNC.AppVersion",
460 NULL);
461 settings_add_text(page, _("NAT traversal method"), "VPNC.NATTMode",
462 NULL);
463 settings_add_text(page, _("DPD idle timeout"), "VPNC.DPDTimeout", NULL);
464 settings_add_text(page, _("Single DES encryption"), "VPNC.SingleDES",
465 NULL);
466 settings_add_text(page, _("No encryption"), "VPNC.NoEncryption", NULL);
467 }
468
469 static void add_immutable_l2tp_page(struct settings *sett)
470 {
471 struct settings_page *page;
472
473 page = add_page_to_settings(sett, "L2TP", TRUE);
474 settings_add_text(page, _("Username"), "L2TP.User", NULL);
475 settings_add_text(page, _("Maximum bandwidth (bps)"), "L2TP.BPS", NULL);
476 settings_add_text(page, _("Maximum transmit bandwidth (tx)"),
477 "L2TP.TXBPS", NULL);
478 settings_add_text(page, _("Maximum receive bandwidth (rx)"),
479 "L2TP.RXBPS", NULL);
480 settings_add_text(page, _("Use length bit"), "L2TP.LengthBit", NULL);
481 settings_add_text(page, _("Use challenge authentication"),
482 "L2TP.Challenge", NULL);
483 settings_add_text(page, _("Default route"), "L2TP.DefaultRoute", NULL);
484 settings_add_text(page, _("Use seq numbers"), "L2TP.FlowBit", NULL);
485 settings_add_text(page, _("Window size"), "L2TP.TunnelRWS", NULL);
486 settings_add_text(page, _("Use only one control channel"),
487 "L2TP.Exclusive", NULL);
488 settings_add_text(page, _("Redial if disconnected"),
489 "L2TP.Redial", NULL);
490 settings_add_text(page, _("Redial timeout"),
491 "L2TP.RedialTimeout", NULL);
492 settings_add_text(page, _("Maximum redial tries"),
493 "L2TP.MaxRedials", NULL);
494 settings_add_text(page, _("Require PAP"), "L2TP.RequirePAP", NULL);
495 settings_add_text(page, _("Require CHAP"), "L2TP.RequireCHAP", NULL);
496 settings_add_text(page, _("Require authentication"),
497 "L2TP.ReqAuth", NULL);
498 settings_add_text(page, _("Accept only these peers"),
499 "L2TP.AccessControl", NULL);
500 settings_add_text(page, _("Authentication file location"),
501 "L2TP.AuthFile", NULL);
502 settings_add_text(page, _("Listen address"), "L2TP.ListenAddr", NULL);
503 settings_add_text(page, _("Use IPSec SA"), "L2TP.IPsecSaref", NULL);
504 settings_add_text(page, _("UDP Port"), "L2TP.Port", NULL);
505 }
506
507 static void add_immutable_pptp_page(struct settings *sett)
508 {
509 struct settings_page *page;
510
511 page = add_page_to_settings(sett, "PPTP", TRUE);
512 settings_add_text(page, _("Username"), "PPTP.User", NULL);
513 }
514
515 static void add_immutable_pppd_page(struct settings *sett)
516 {
517 struct settings_page *page;
518
519 page = add_page_to_settings(sett, "PPPD", TRUE);
520 settings_add_text(page, _("Dead peer check count"),
521 "PPPD.EchoFailure", NULL);
522 settings_add_text(page, _("Dead peer check interval"),
523 "PPPD.EchoInterval", NULL);
524 settings_add_text(page, _("Debug level"), "PPPD.Debug", NULL);
525 settings_add_text(page, _("Refuse EAP authentication"),
526 "PPPD.RefuseEAP", NULL);
527 settings_add_text(page, _("Refuse PAP authentication"),
528 "PPPD.RefusePAP", NULL);
529 settings_add_text(page, _("Refuse CHAP authentication"),
530 "PPPD.RefusePAP", NULL);
531 settings_add_text(page, _("Refuse MSCHAP authentication"),
532 "PPPD.RefuseMSCHAP", NULL);
533 settings_add_text(page, _("Refuse MSCHAPv2 authentication"),
534 "PPPD.RefuseMSCHAP2", NULL);
535 settings_add_text(page, _("Disable BSD compression"),
536 "PPPD.NoBSDComp", NULL);
537 settings_add_text(page, _("Disable DEFLATE compression"),
538 "PPPD.NoDeflate", NULL);
539 settings_add_text(page, _("Require the use of MPPE"),
540 "PPPD.RequirMPPE", NULL);
541 settings_add_text(page, _("Require the use of MPPE 40 bit"),
542 "PPPD.RequirMPPE40", NULL);
543 settings_add_text(page, _("Require the use of MPPE 128 bit"),
544 "PPPD.RequirMPPE128", NULL);
545 settings_add_text(page, _("Allow MPPE to use stateful mode"),
546 "PPPD.RequirMPPEStateful", NULL);
547 settings_add_text(page, _("No Van Jacobson compression"),
548 "PPPD.NoVJ", NULL);
549 }
550
551 static void clear_cb(GtkWidget *button, gpointer user_data)
552 {
553 service_clear_properties(user_data);
554 }
555
556 static void remove_cb(GtkWidget *button, gpointer user_data)
557 {
558 service_remove(user_data);
559 }
560
561 static void add_clear_page(struct settings *sett)
562 {
563 struct settings_page *page;
564 GtkWidget *clear, *clear_l, *remove, *remove_l;
565
566 page = add_page_to_settings(sett, _("_Clear settings"), TRUE);
567
568 clear = gtk_button_new_with_mnemonic(_("_Reset"));
569 remove = gtk_button_new_with_mnemonic(_("_Forget"));
570 clear_l = gtk_label_new(_("Set all properties to defaults."));
571 remove_l = gtk_label_new(_("Remove this service."));
572
573 g_signal_connect(clear, "clicked", G_CALLBACK(clear_cb), sett->serv);
574 g_signal_connect(remove, "clicked", G_CALLBACK(remove_cb), sett->serv);
575
576 gtk_widget_set_halign(clear_l, GTK_ALIGN_START);
577 gtk_widget_set_halign(remove_l, GTK_ALIGN_START);
578 style_set_margin(clear, MARGIN_LARGE);
579 style_set_margin(remove, MARGIN_LARGE);
580 gtk_widget_set_margin_top(clear, 0);
581 gtk_widget_set_margin_bottom(clear, 0);
582
583 gtk_grid_attach(GTK_GRID(page->grid), clear, 0, 0, 1, 1);
584 gtk_grid_attach(GTK_GRID(page->grid), clear_l, 1, 0, 1, 1);
585 gtk_grid_attach(GTK_GRID(page->grid), remove, 0, 1, 1, 1);
586 gtk_grid_attach(GTK_GRID(page->grid), remove_l, 1, 1, 1, 1);
587
588 gtk_widget_show_all(page->grid);
589
590 if(sett->serv->type == CONNECTION_TYPE_ETHERNET) {
591 gtk_widget_hide(remove);
592 gtk_widget_hide(remove_l);
593 gtk_widget_set_margin_top(remove, 0);
594 gtk_widget_set_margin_bottom(remove, 0);
595 }
596 }
597
598 static void add_pages(struct settings *sett)
599 {
600 gboolean immutable = service_get_property_boolean(sett->serv,
601 "Immutable", NULL);
602 if(immutable || sett->serv->type == CONNECTION_TYPE_VPN) {
603 gchar *type;
604
605 if(sett->serv->type != CONNECTION_TYPE_VPN)
606 add_info_page(sett);
607 else
608 add_vpn_info_page(sett);
609
610 add_immutable_ipv_page(sett);
611 add_immutable_server_page(sett);
612 if(sett->serv->type != CONNECTION_TYPE_VPN)
613 return;
614
615 type = service_get_property_string_raw(sett->serv,
616 "Type", NULL);
617 if(!strcmp(type, "openconnect"))
618 add_immutable_openconnect_page(sett);
619 else if(!strcmp(type, "openvpn"))
620 add_immutable_openvpn_page(sett);
621 else if(!strcmp(type, "vpnc"))
622 add_immutable_vpnc_page(sett);
623 else if(!strcmp(type, "l2tp") || !strcmp(type, "pptp")) {
624 if(!strcmp(type, "l2tp"))
625 add_immutable_l2tp_page(sett);
626 else
627 add_immutable_pptp_page(sett);
628 add_immutable_pppd_page(sett);
629 }
630
631 g_free(type);
632
633 return;
634 }
635
636 add_info_page(sett);
637 add_ipv_page(sett, 4);
638 add_ipv_page(sett, 6);
639 add_server_page(sett);
640 add_proxy_page(sett);
641 add_clear_page(sett);
642 }
643
644 static void append_dict_inner(const gchar *key, const gchar *subkey,
645 gpointer value, gpointer user_data)
646 {
647 DualHashTable *dict = user_data;
648 struct settings_content *content = value;
649 if(!content->writable(content))
650 return;
651 GVariant *variant = content->value(content);
652 if(!variant)
653 return;
654 hash_table_set_dual_key(dict, content->key, content->subkey,
655 g_variant_ref(variant));
656
657 }
658
659 static void apply_cb(GtkWidget *window, gpointer user_data)
660 {
661 struct settings *sett = user_data;
662 DualHashTable *table =
663 dual_hash_table_new((GDestroyNotify)g_variant_unref);
664 GVariant *out;
665
666 dual_hash_table_foreach(sett->contents, append_dict_inner, table);
667 out = dual_hash_table_to_variant(table);
668 service_set_properties(sett->serv, out);
669 g_variant_unref(out);
670 dual_hash_table_unref(table);
671 }
672
673 static gboolean delete_event(GtkWidget *window, GdkEvent *event,
674 gpointer user_data)
675 {
676 free_settings(user_data);
677 return FALSE;
678 }
679
680 static void init_settings(struct settings *sett)
681 {
682 gchar *name;
683 gchar *title;
684 GtkWidget *frame;
685 GtkGrid *grid = GTK_GRID(gtk_grid_new());
686
687 sett->invalid_count = 0;
688 sett->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
689 name = service_get_property_string(sett->serv, "Name", NULL);
690 title = g_strdup_printf("%s - %s", _("Network Settings"), name);
691 gtk_window_set_title(GTK_WINDOW(sett->window), title);
692 gtk_window_set_default_size(GTK_WINDOW(sett->window), SETTINGS_WIDTH,
693 SETTINGS_HEIGHT);
694
695 g_free(title);
696 g_free(name);
697
698 sett->list = gtk_list_box_new();
699 sett->notebook = gtk_notebook_new();
700 sett->apply = gtk_button_new_with_mnemonic(_("_Apply"));
701 frame = gtk_frame_new(NULL);
702
703 g_object_ref(sett->window);
704 g_object_ref(sett->list);
705 g_object_ref(sett->notebook);
706
707 g_signal_connect(sett->window, "delete-event", G_CALLBACK(delete_event),
708 sett);
709 g_signal_connect(sett->list, "row-selected",
710 G_CALLBACK(list_item_selected), sett->notebook);
711 g_signal_connect(sett->apply, "clicked",
712 G_CALLBACK(apply_cb), sett);
713 g_signal_connect(sett->window, "key_press_event", G_CALLBACK(handle_keyboard_shortcut), sett);
714
715 style_set_margin(GTK_WIDGET(grid), MARGIN_LARGE);
716 gtk_widget_set_margin_top(sett->apply, MARGIN_LARGE);
717
718 gtk_widget_set_size_request(sett->list, SETTINGS_LIST_WIDTH, -1);
719 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(sett->notebook), FALSE);
720 gtk_notebook_set_show_border(GTK_NOTEBOOK(sett->notebook), FALSE);
721 gtk_widget_set_vexpand(sett->list, TRUE);
722 gtk_widget_set_hexpand(sett->notebook, TRUE);
723 gtk_widget_set_vexpand(sett->notebook, TRUE);
724 gtk_widget_set_valign(sett->apply, GTK_ALIGN_END);
725 gtk_widget_set_halign(sett->apply, GTK_ALIGN_END);
726
727 gtk_container_add(GTK_CONTAINER(frame), sett->list);
728 gtk_grid_attach(grid, frame, 0, 0, 1, 2);
729 gtk_grid_attach(grid, sett->notebook, 1, 0, 1, 1);
730 gtk_grid_attach(grid, sett->apply, 1, 1, 1, 1);
731 gtk_container_add(GTK_CONTAINER(sett->window), GTK_WIDGET(grid));
732
733 gtk_widget_show_all(sett->window);
734
735 if(sett->serv->type == CONNECTION_TYPE_VPN)
736 gtk_widget_hide(sett->apply);
737
738 add_pages(sett);
739 }
740
741 struct settings *settings_create(struct service *serv,
742 void (*closed)(struct service *serv))
743 {
744 struct settings *sett;
745
746 sett = g_malloc(sizeof(*sett));
747
748 sett->serv = serv;
749 sett->closed = closed;
750 sett->callbacks = dual_hash_table_new(content_callback_free);
751 sett->contents = dual_hash_table_new(NULL);
752
753 init_settings(sett);
754
755 return sett;
756 }
757
758 void settings_update(struct settings *sett, const gchar *key,
759 const gchar *subkey, GVariant *value)
760 {
761 struct content_callback *cb;
762
763 cb = hash_table_get_dual_key(sett->callbacks, key, subkey);
764 if(cb)
765 handle_content_callback(value, key, subkey, cb);
766 }
767
768 void settings_set_callback(struct settings *sett, const gchar *key,
769 const gchar *subkey, struct content_callback *cb)
770 {
771 hash_table_set_dual_key(sett->callbacks, key, subkey, cb);
772 }
773