Debian Codegraph @

src/main.c

raw ◀ ▶
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 <locale.h>
22
23 #include <gio/gio.h>
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27
28 #include "config.h"
29 #include "agent.h"
30 #include "connection.h"
31 #include "configurator.h"
32 #include "dialog.h"
33 #include "technology.h"
34 #include "interfaces.h"
35 #include "status.h"
36 #include "style.h"
37 #include "vpn.h"
38 #include "util.h"
39
40 GtkWidget *list, *notebook, *main_window;
41 GHashTable *technology_types, *services;
42 GDBusProxy *manager_proxy, *vpn_manager_proxy;
43 struct technology *technologies[CONNECTION_TYPE_COUNT];
44 gboolean shutting_down = FALSE;
45
46 const gchar *default_page;
47
48 gboolean no_icon;
49
50 /* sort smallest enum value first */
51 gint technology_list_sort_cb(GtkListBoxRow *row1, GtkListBoxRow *row2,
52 gpointer user_data)
53 {
54 enum connection_type type1 = *(enum connection_type *)g_object_get_data(
55 G_OBJECT(row1), "technology-type");
56 enum connection_type type2 = *(enum connection_type *)g_object_get_data(
57 G_OBJECT(row2), "technology-type");
58 return type1 - type2;
59 }
60
61 static void create_content(void)
62 {
63 GtkWidget *frame, *grid;
64 #ifdef HAVE_CONFIG_SETTINGS
65 GtkWidget *settings;
66 #endif
67
68 grid = gtk_grid_new();
69 style_set_margin(grid, MARGIN_LARGE);
70 gtk_widget_set_hexpand(grid, TRUE);
71 gtk_widget_set_vexpand(grid, TRUE);
72
73 frame = gtk_frame_new(NULL);
74 list = gtk_list_box_new();
75 notebook = gtk_notebook_new();
76 gtk_list_box_set_selection_mode(GTK_LIST_BOX(list),
77 GTK_SELECTION_BROWSE);
78 gtk_list_box_set_sort_func(GTK_LIST_BOX(list), technology_list_sort_cb,
79 NULL, NULL);
80 g_signal_connect(list, "row-selected", G_CALLBACK(list_item_selected),
81 notebook);
82 gtk_widget_set_size_request(list, LIST_WIDTH, -1);
83
84 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(notebook), FALSE);
85 gtk_notebook_set_show_border(GTK_NOTEBOOK(notebook), FALSE);
86 gtk_widget_set_vexpand(frame, TRUE);
87 gtk_widget_set_hexpand(notebook, TRUE);
88 gtk_widget_set_vexpand(notebook, TRUE);
89
90 gtk_container_add(GTK_CONTAINER(frame), list);
91 gtk_grid_attach(GTK_GRID(grid), frame, 0, 0, 1, 1);
92 gtk_grid_attach(GTK_GRID(grid), notebook, 1, 0, 1, 2);
93 gtk_container_add(GTK_CONTAINER(main_window), grid);
94
95 #ifdef HAVE_CONFIG_SETTINGS
96 settings = gtk_button_new_with_mnemonic(_("_Settings"));
97 g_signal_connect(settings, "clicked", G_CALLBACK(config_window_open),
98 NULL);
99 gtk_widget_set_margin_top(settings, MARGIN_SMALL);
100 gtk_widget_set_vexpand(settings, FALSE);
101 gtk_widget_set_valign(settings, GTK_ALIGN_END);
102 gtk_grid_attach(GTK_GRID(grid), settings, 0, 1, 1, 1);
103 #endif
104 }
105
106 static void tech_item_mnemonic_callback(GtkWidget *widget, gboolean arg1,
107 gpointer user_data)
108 {
109 list_item_selected(NULL, GTK_LIST_BOX_ROW(widget), user_data);
110 }
111
112 static void add_technology(GDBusConnection *connection, GVariant *technology)
113 {
114 GVariant *path;
115 const gchar *object_path;
116 GVariant *properties;
117 GDBusProxy *proxy;
118 GDBusNodeInfo *info;
119 GError *error = NULL;
120 struct technology *item;
121 info = g_dbus_node_info_new_for_xml(TECHNOLOGY_INTERFACE, &error);
122 if(error) {
123 g_warning("Failed to load technology interface: %s",
124 error->message);
125 g_error_free(error);
126 return;
127 }
128
129 path = g_variant_get_child_value(technology, 0);
130 properties = g_variant_get_child_value(technology, 1);
131
132 object_path = g_variant_get_string(path, NULL);
133
134 if(g_hash_table_contains(technology_types, object_path))
135 goto out;
136
137 proxy = g_dbus_proxy_new_sync(connection, G_DBUS_PROXY_FLAGS_NONE,
138 g_dbus_node_info_lookup_interface(info,
139 "net.connman.Technology"),
140 "net.connman", object_path,
141 "net.connman.Technology", NULL, &error);
142 if(error) {
143 g_warning("failed to connect ConnMan technology proxy: %s",
144 error->message);
145 g_error_free(error);
146 goto out;
147 }
148
149 item = technology_create(proxy, object_path, properties);
150 g_signal_connect(item->list_item->item, "mnemonic-activate",
151 G_CALLBACK(tech_item_mnemonic_callback), notebook);
152
153 g_hash_table_insert(technology_types, g_strdup(object_path),
154 &item->type);
155 technologies[item->type] = item;
156
157 gtk_container_add(GTK_CONTAINER(list), item->list_item->item);
158 gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
159 item->settings->grid, NULL);
160
161 out:
162 g_dbus_node_info_unref(info);
163 g_variant_unref(path);
164 g_variant_unref(properties);
165 }
166
167 static void remove_technology_by_path(const gchar *path)
168 {
169 enum connection_type *type_p;
170 enum connection_type type;
171
172 type_p = g_hash_table_lookup(technology_types, path);
173
174 if(!type_p)
175 return;
176 type = *type_p;
177 g_hash_table_remove(technology_types, path);
178 technology_free(technologies[type]);
179 technologies[type] = NULL;
180 }
181
182 static void remove_technology(GVariant *parameters)
183 {
184 GVariant *path_v;
185 const gchar *path;
186
187 path_v = g_variant_get_child_value(parameters, 0);
188 path = g_variant_get_string(path_v, NULL);
189 remove_technology_by_path(path);
190 g_variant_unref(path_v);
191 }
192
193 static void add_service(GDBusConnection *connection, const gchar *path,
194 GVariant *properties)
195 {
196 struct service *serv;
197 GDBusProxy *proxy;
198 GDBusNodeInfo *info;
199 GDBusInterfaceInfo *interface;
200 GError *error = NULL;
201 enum connection_type type;
202 const char *interface_xml;
203 const char *connman_name;
204 const char *interface_name;
205
206 type = connection_type_from_properties(properties);
207 if(type == CONNECTION_TYPE_VPN) {
208 interface_xml = VPN_CONNECTION_INTERFACE;
209 connman_name = CONNMAN_VPN_PATH;
210 interface_name = VPN_CONNECTION_NAME;
211 }
212 else {
213 interface_xml = SERVICE_INTERFACE;
214 connman_name = CONNMAN_PATH;
215 interface_name = SERVICE_NAME;
216 }
217 info = g_dbus_node_info_new_for_xml(interface_xml, &error);
218 if(error) {
219 g_warning("Failed to load service interface: %s",
220 error->message);
221 g_error_free(error);
222 return;
223 }
224
225 interface = g_dbus_node_info_lookup_interface(info, interface_name);
226 proxy = g_dbus_proxy_new_sync(connection, G_DBUS_PROXY_FLAGS_NONE,
227 interface, connman_name, path,
228 interface_name, NULL, &error);
229 if(error) {
230 g_warning("failed to connect ConnMan service proxy: %s",
231 error->message);
232 g_error_free(error);
233 goto out;
234 }
235
236 serv = service_create(technologies[type], proxy, path, properties);
237 g_hash_table_insert(services, g_strdup(path), serv);
238 if(technologies[type])
239 technology_add_service(technologies[type], serv);
240
241 out:
242 g_dbus_node_info_unref(info);
243 }
244
245 void modify_service(GDBusConnection *connection, const gchar *path,
246 GVariant *properties)
247 {
248 enum connection_type type;
249 struct service *serv;
250
251 serv = g_hash_table_lookup(services, path);
252
253 if(serv)
254 type = serv->type;
255 else
256 type = connection_type_from_properties(properties);
257
258 if(type != CONNECTION_TYPE_UNKNOWN) {
259 if(!serv) {
260 add_service(connection, path, properties);
261 return;
262 }
263 service_update(serv, properties);
264 }
265 }
266
267 static void remove_service_struct(struct service *serv)
268 {
269 enum connection_type type;
270
271 type = serv->type;
272 if(type != CONNECTION_TYPE_UNKNOWN && technologies[type])
273 technology_remove_service(technologies[type], serv->path);
274
275 service_free(serv);
276 }
277
278 void remove_service(const gchar *path)
279 {
280 g_hash_table_remove(services, path);
281 }
282
283 static void services_changed(GDBusConnection *connection, GVariant *parameters)
284 {
285 GVariant *modified, *deleted;
286 GVariantIter *iter;
287 gchar *path;
288 GVariant *value;
289
290 modified = g_variant_get_child_value(parameters, 0);
291 deleted = g_variant_get_child_value(parameters, 1);
292
293 iter = g_variant_iter_new(modified);
294 while(g_variant_iter_loop(iter, "(o@*)", &path, &value))
295 if(!strstr(path, "service/vpn"))
296 modify_service(connection, path, value);
297 g_variant_iter_free(iter);
298
299 iter = g_variant_iter_new(deleted);
300 while(g_variant_iter_loop(iter, "o", &path))
301 if(!strstr(path, "service/vpn"))
302 remove_service(path);
303 g_variant_iter_free(iter);
304
305 g_variant_unref(modified);
306 g_variant_unref(deleted);
307 }
308
309 static void manager_signal(GDBusProxy *proxy, gchar *sender, gchar *signal,
310 GVariant *parameters, gpointer user_data)
311 {
312 GDBusConnection *connection = g_dbus_proxy_get_connection(proxy);
313 if(!strcmp(signal, "TechnologyAdded")) {
314 add_technology(connection, parameters);
315 } else if(!strcmp(signal, "TechnologyRemoved")) {
316 remove_technology(parameters);
317 } else if(!strcmp(signal, "ServicesChanged")) {
318 services_changed(connection, parameters);
319 }
320
321 status_update();
322 }
323
324 static void add_all_technologies(GDBusConnection *connection,
325 GVariant *technologies_v)
326 {
327 int i;
328 int size = g_variant_n_children(technologies_v);
329 enum connection_type default_type = CONNECTION_TYPE_UNKNOWN;
330 for(i = 0; i < size; i++) {
331 GVariant *child = g_variant_get_child_value(technologies_v, i);
332 add_technology(connection, child);
333 g_variant_unref(child);
334 }
335
336 if(default_page)
337 default_type = connection_type_from_string(default_page);
338 /* If the default page is "vpn", we have to ignore it because VPN
339 * connections may not have been added yet */
340 if(default_type && default_type != CONNECTION_TYPE_VPN) {
341 for(i = CONNECTION_TYPE_ETHERNET;
342 i < CONNECTION_TYPE_COUNT; i++) {
343 GtkWidget *row;
344
345 if(!technologies[i])
346 continue;
347 if(default_type != technologies[i]->type)
348 continue;
349
350 row = technologies[i]->list_item->item;
351 gtk_list_box_select_row(GTK_LIST_BOX(list),
352 GTK_LIST_BOX_ROW(row));
353
354 default_page = NULL;
355 return;
356 }
357 }
358
359 /* Default page wasn't set or wasn't found, select any possible page */
360 for(i = CONNECTION_TYPE_ETHERNET; i < CONNECTION_TYPE_COUNT; i++) {
361 if(technologies[i]) {
362 GtkWidget *row = technologies[i]->list_item->item;
363 gtk_list_box_select_row(GTK_LIST_BOX(list),
364 GTK_LIST_BOX_ROW(row));
365 break;
366 }
367 }
368 }
369
370 static void add_all_services(GDBusConnection *connection, GVariant *services_v)
371 {
372 int i;
373 int size = g_variant_n_children(services_v);
374 for(i = 0; i < size; i++) {
375 GVariant *path_v, *properties, *child;;
376 const gchar *path;
377
378 child = g_variant_get_child_value(services_v, i);
379 path_v = g_variant_get_child_value(child, 0);
380 properties = g_variant_get_child_value(child, 1);
381 path = g_variant_get_string(path_v, NULL);
382 if(!strstr(path, "service/vpn"))
383 add_service(connection, path, properties);
384
385 g_variant_unref(child);
386 g_variant_unref(path_v);
387 g_variant_unref(properties);
388 }
389 }
390
391 static GDBusProxy *manager_create(GDBusConnection *connection,
392 const gchar *interface_data,
393 const gchar *path, const gchar *connman_path)
394 {
395 GDBusNodeInfo *info;
396 GDBusInterfaceInfo *interface;
397 GError *error = NULL;
398 GDBusProxy *proxy;
399
400 info = g_dbus_node_info_new_for_xml(interface_data, &error);
401 if(error)
402 goto out;
403
404 interface = g_dbus_node_info_lookup_interface(info, path);
405 proxy = g_dbus_proxy_new_sync(connection, G_DBUS_PROXY_FLAGS_NONE,
406 interface, connman_path, "/", path, NULL,
407 &error);
408 g_dbus_node_info_unref(info);
409 if(error)
410 goto out;
411
412 return proxy;
413
414 out:
415 g_critical("Failed to connect to connman: %s", error->message);
416 show_error(_("Failed to connect to ConnMan."), error->message);
417 g_error_free(error);
418 return NULL;
419 }
420
421 static GDBusProxy *manager_register(GDBusConnection *connection)
422 {
423 GError *error = NULL;
424 GDBusProxy *proxy;
425 GVariant *data, *child;
426
427 proxy = manager_create(connection, MANAGER_INTERFACE, MANAGER_NAME,
428 CONNMAN_PATH);
429 if(!proxy)
430 return NULL;
431
432 data = g_dbus_proxy_call_sync(proxy, "GetTechnologies", NULL,
433 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
434 if(error) {
435 g_object_unref(proxy);
436 goto out;
437 }
438
439 g_signal_connect(proxy, "g-signal", G_CALLBACK(manager_signal), NULL);
440
441 child = g_variant_get_child_value(data, 0);
442 add_all_technologies(connection, child);
443
444 g_variant_unref(data);
445 g_variant_unref(child);
446
447 data = g_dbus_proxy_call_sync(proxy, "GetServices", NULL,
448 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
449 if(error) {
450 g_object_unref(proxy);
451 goto out;
452 }
453
454 child = g_variant_get_child_value(data, 0);
455 add_all_services(connection, child);
456
457 g_variant_unref(data);
458 g_variant_unref(child);
459
460 return proxy;
461
462 out:
463 g_critical("Failed to connect to connman: %s", error->message);
464 g_error_free(error);
465 return NULL;
466 }
467
468 static GDBusProxy *vpn_manager_register(GDBusConnection *connection)
469 {
470 GDBusProxy *proxy;
471
472 proxy = manager_create(connection, VPN_MANAGER_INTERFACE,
473 VPN_MANAGER_NAME, CONNMAN_VPN_PATH);
474 if(!proxy)
475 return NULL;
476
477 vpn_get_connections(proxy);
478 return proxy;
479 }
480
481 static void connman_appeared(GDBusConnection *connection, const gchar *name,
482 const gchar *name_owner, gpointer user_data)
483 {
484 manager_proxy = manager_register(connection);
485 register_agent(connection, manager_proxy);
486
487 status_update();
488 }
489
490 static gboolean is_service(gpointer key, gpointer value, gpointer user_data)
491 {
492 return key && !strstr(key, "/vpn");
493 }
494
495 static void connman_disappeared(GDBusConnection *connection, const gchar *name,
496 gpointer user_data)
497 {
498 int i;
499 g_hash_table_foreach_remove(services, is_service, NULL);
500 for(i = CONNECTION_TYPE_ETHERNET; i < CONNECTION_TYPE_COUNT; i++) {
501 if(i == CONNECTION_TYPE_VPN)
502 continue;
503
504 if(technologies[i] && technologies[i]->path)
505 remove_technology_by_path(technologies[i]->path);
506
507 technologies[i] = NULL;
508 }
509
510 agent_release();
511 if(manager_proxy)
512 g_object_unref(manager_proxy);
513 manager_proxy = NULL;
514 default_page = NULL;
515 }
516
517 static void connman_vpn_appeared(GDBusConnection *connection, const gchar *name,
518 const gchar *name_owner, gpointer user_data)
519 {
520 enum connection_type type = CONNECTION_TYPE_VPN;
521 if(technologies[type])
522 return;
523 struct technology *vpn = vpn_register(connection, list, notebook);
524 technologies[type] = vpn;
525 gtk_container_add(GTK_CONTAINER(list), vpn->list_item->item);
526 gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
527 vpn->settings->grid, NULL);
528 vpn_manager_proxy = vpn_manager_register(connection);
529 register_vpn_agent(connection, vpn_manager_proxy);
530 if(default_page && !strcmp(default_page, "vpn")) {
531 GtkWidget *row = technologies[type]->list_item->item;
532 gtk_list_box_select_row(GTK_LIST_BOX(list),
533 GTK_LIST_BOX_ROW(row));
534 default_page = NULL;
535 }
536 }
537
538 static gboolean is_connection(gpointer key, gpointer value, gpointer user_data)
539 {
540 return key && strstr(key, "/vpn");
541 }
542
543 static void connman_vpn_disappeared(GDBusConnection *connection,
544 const gchar *name, gpointer user_data)
545 {
546 enum connection_type type = CONNECTION_TYPE_VPN;
547
548 g_hash_table_foreach_remove(services, is_connection, NULL);
549
550 if(technologies[type])
551 technology_free(technologies[type]);
552
553 technologies[type] = NULL;
554 vpn_agent_release();
555 if(vpn_manager_proxy)
556 g_object_unref(vpn_manager_proxy);
557 vpn_manager_proxy = NULL;
558 if(default_page && !strcmp(default_page, "vpn"))
559 default_page = NULL;
560 }
561
562 static void dbus_connected(GObject *source, GAsyncResult *res,
563 gpointer user_data)
564 {
565 (void)source;
566 (void)user_data;
567 GDBusConnection *connection;
568 GError *error = NULL;
569
570 connection = g_bus_get_finish(res, &error);
571 if(error) {
572 g_error("failed to connect to system dbus: %s",
573 error->message);
574 show_error(_("Failed to connect to system DBus."),
575 error->message);
576 g_error_free(error);
577 return;
578 }
579
580 g_bus_watch_name_on_connection(connection, "net.connman",
581 G_BUS_NAME_WATCHER_FLAGS_NONE,
582 connman_appeared, connman_disappeared,
583 NULL, NULL);
584 g_bus_watch_name_on_connection(connection, "net.connman.vpn",
585 G_BUS_NAME_WATCHER_FLAGS_NONE,
586 connman_vpn_appeared,
587 connman_vpn_disappeared,
588 NULL, NULL);
589 }
590
591 static gboolean delete_event(GtkApplication *app, GdkEvent *event,
592 gpointer user_data)
593 {
594 shutting_down = TRUE;
595 agent_release();
596 return FALSE;
597 }
598
599 static void startup(GtkApplication *app, gpointer user_data)
600 {
601 g_bus_get(G_BUS_TYPE_SYSTEM, NULL, dbus_connected, NULL);
602
603 if(no_icon)
604 status_icon_enabled = FALSE;
•605 else if(launch_to_tray)
606 status_icon_enabled = TRUE;
607 no_icon = FALSE;
608
609 main_window = gtk_application_window_new(app);
610 g_signal_connect(app, "window-removed",
611 G_CALLBACK(delete_event), NULL);
612 gtk_window_set_title(GTK_WINDOW(main_window), _("Network Settings"));
613 gtk_window_set_default_size(GTK_WINDOW(main_window), DEFAULT_WIDTH,
614 DEFAULT_HEIGHT);
615
616 create_content();
617
618 g_signal_connect(G_OBJECT(main_window), "key_press_event", G_CALLBACK(handle_keyboard_shortcut), NULL);
619
620 #ifdef USE_STATUS_ICON
621 if(status_icon_enabled && !no_icon) {
622 g_signal_connect(main_window, "delete-event",
623 G_CALLBACK(gtk_widget_hide_on_delete),
624 main_window);
625 status_init(app);
626 }
627 #endif
628 }
629
630 static void activate(GtkApplication *app, gpointer user_data)
631 {
632 #ifdef USE_STATUS_ICON
•633 static gboolean window_has_shown = FALSE;
634 if(!launch_to_tray || !status_icon_enabled || no_icon) {
635 if(window_has_shown)
636 gtk_widget_show(main_window);
637 else
638 gtk_widget_show_all(main_window);
639
640 window_has_shown = TRUE;
641 }
642 launch_to_tray = FALSE;
643 #endif
644 }
645
646 /* Hide the options if support not compiled in and show a warning instead of
647 * not including the option so that 'invalid' options won't just shut it down */
648
649 #ifdef USE_STATUS_ICON
650 #define STATUS_ICON_HIDDEN 0
651 #else
652 #define STATUS_ICON_HIDDEN G_OPTION_FLAG_HIDDEN
653 #endif
654
655 #ifdef USE_OPENCONNECT
656 #define FSID_HIDDEN 0
657 #else
658 #define FSID_HIDDEN G_OPTION_FLAG_HIDDEN
659 #endif
660
661 static const GOptionEntry options[] = {
662 { "page", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &default_page,
663 NULL, NULL },
664 { "no-icon", 0, STATUS_ICON_HIDDEN,
665 G_OPTION_ARG_NONE,
666 &no_icon, "Disable status icon", NULL },
667 { "tray", 0, STATUS_ICON_HIDDEN,
668 G_OPTION_ARG_NONE,
669 &launch_to_tray, "Launch connman-gtk straight to tray", NULL },
670 { "use-fsid", 0, FSID_HIDDEN,
671 G_OPTION_ARG_NONE,
672 &use_fsid,
673 "Use FSID with openconnect", NULL },
674 { NULL }
675 };
676
677 int main(int argc, char *argv[])
678 {
679 GtkApplication *app;
680 int status;
681
682 setlocale(LC_ALL, "");
683 bindtextdomain(GETTEXT_PACKAGE, CONNMAN_GTK_LOCALEDIR);
684 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
685 textdomain(GETTEXT_PACKAGE);
686
687 style_init();
688
689 technology_types = g_hash_table_new_full(g_str_hash, g_str_equal,
690 g_free, NULL);
691 services = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
692 (GDestroyNotify)remove_service_struct);
693
694 app = gtk_application_new(NULL, G_APPLICATION_FLAGS_NONE);
•695 config_load(app);
696 g_application_add_main_option_entries(G_APPLICATION(app), options);
697 g_signal_connect(app, "startup", G_CALLBACK(startup), NULL);
698 g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
699 status = g_application_run(G_APPLICATION(app), argc, argv);
700 g_object_unref(app);
701
702 g_object_unref(css_provider);
703
704 return status;
705 }