Debian Codegraph @

src/technology.c

raw
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 #include <string.h>
23
24 #include <gio/gio.h>
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <gtk/gtk.h>
28
29 #include "config.h"
30 #include "connection.h"
31 #include "dialog.h"
32 #include "main.h"
33 #include "status.h"
34 #include "style.h"
35 #include "technology.h"
36 #include "vpn.h"
37 #include "wireless.h"
38
39 struct technology_list_item *technology_create_item(struct technology *tech)
40 {
41 GtkWidget *grid;
42 struct technology_list_item *item;
43 const gchar *name;
44
45 item = g_malloc(sizeof(*item));
46 item->technology = tech;
47 name = mnemonic_tech_name(tech->type);
48
49 grid = gtk_grid_new();
50 item->item = gtk_list_box_row_new();
51 item->icon = gtk_image_new();
52 item->label = gtk_label_new_with_mnemonic(name);
53
54 g_object_ref(item->item);
55 g_object_ref(item->icon);
56 g_object_ref(item->label);
57
58 g_object_set_data(G_OBJECT(item->item), "content",
59 tech->settings->grid);
60 g_object_set_data(G_OBJECT(item->item), "technology-type",
61 &tech->type);
62
63 style_set_margin(grid, MARGIN_SMALL);
64 style_set_margin_start(item->label, MARGIN_SMALL);
65
66 gtk_container_add(GTK_CONTAINER(grid), item->icon);
67 gtk_container_add(GTK_CONTAINER(grid), item->label);
68 gtk_container_add(GTK_CONTAINER(item->item), grid);
69
70 gtk_widget_show_all(item->item);
71 return item;
72 }
73
74 static void free_list_item(struct technology_list_item *item)
75 {
76 if(!item)
77 return;
78 g_object_unref(item->icon);
79 g_object_unref(item->label);
80 g_object_unref(item->item);
81 gtk_widget_destroy(item->item);
82
83 g_free(item);
84 }
85
86 static gpointer toggle_power_sync(gpointer data)
87 {
88 struct technology *tech = data;
89 GtkWidget *pw = tech->settings->power_switch;
90 gboolean state = gtk_switch_get_active(GTK_SWITCH(pw));
91 technology_set_property(tech, "Powered", g_variant_new("b", state));
92 return NULL;
93 }
94
95 static gboolean toggle_power(GtkSwitch *widget, GParamSpec *pspec,
96 gpointer user_data)
97 {
98 g_thread_new("toggle_power", toggle_power_sync, user_data);
99 return TRUE;
100 }
101
102 static void update_status(struct technology *tech)
103 {
104 struct technology_settings *item;
105 gboolean connected, powered, tethering;
106 const gchar *name;
107
108 if(tech->type == CONNECTION_TYPE_VPN) {
109 vpn_update_status(tech);
110 return;
111 }
112
113 item = tech->settings;
114 connected = technology_get_property_bool(tech, "Connected");
115 powered = technology_get_property_bool(tech, "Powered");
116 tethering = technology_get_property_bool(tech, "Tethering");
117 name = technology_get_property_string(tech, "Name");
118 gtk_label_set_text(GTK_LABEL(item->title), name);
119
120 if(connected) {
121 gtk_label_set_text(GTK_LABEL(item->status), _("Connected"));
122 if(tech->type == CONNECTION_TYPE_ETHERNET)
123 gtk_image_set_from_icon_name(GTK_IMAGE(item->icon),
124 "network-wired",
125 GTK_ICON_SIZE_DIALOG);
126 return;
127 }
128
129 if(tech->type == CONNECTION_TYPE_ETHERNET)
130 gtk_image_set_from_icon_name(GTK_IMAGE(item->icon),
131 "network-wired-disconnected",
132 GTK_ICON_SIZE_DIALOG);
133 if(powered) {
134 if(tethering)
135 gtk_label_set_text(GTK_LABEL(item->status),
136 _("Tethering"));
137 else
138 gtk_label_set_text(GTK_LABEL(item->status),
139 _("Not connected"));
140 gtk_widget_show(item->buttons);
141 gtk_widget_show(item->contents);
142 } else {
143 gtk_label_set_text(GTK_LABEL(item->status),
144 _("Disabled"));
145 gtk_widget_hide(item->buttons);
146 gtk_widget_hide(item->contents);
147 }
148 }
149
150 static void update_power(struct technology *tech)
151 {
152 struct technology_settings *item = tech->settings;
153
154 gboolean powered = technology_get_property_bool(tech, "Powered");
155 g_signal_handler_block(G_OBJECT(item->power_switch), item->powersig);
156 gtk_switch_set_active(GTK_SWITCH(item->power_switch), powered);
157 g_signal_handler_unblock(G_OBJECT(item->power_switch), item->powersig);
158 update_status(tech);
159 }
160
161 static void handle_proxy_signal(GDBusProxy *proxy, gchar *sender,
162 gchar *signal, GVariant *parameters,
163 gpointer user_data)
164 {
165 struct technology *tech = user_data;
166 if(!strcmp(signal, "PropertyChanged")) {
167 GVariant *name_v, *value_v, *value;
168 gchar *name;
169 name_v = g_variant_get_child_value(parameters, 0);
170 value_v = g_variant_get_child_value(parameters, 1);
171 value = g_variant_get_child_value(value_v, 0);
172 name = g_variant_dup_string(name_v, NULL);
173 g_hash_table_replace(tech->settings->properties, name, value);
174 technology_property_changed(tech, name);
175
176 g_variant_unref(name_v);
177 g_variant_unref(value_v);
178 }
179 }
180
181 static void update_service_separator(GtkListBoxRow *row, GtkListBoxRow *before,
182 gpointer user_data)
183 {
184 GtkWidget *cur;
185 if(!before) {
186 gtk_list_box_row_set_header(row, NULL);
187 return;
188 }
189 cur = gtk_list_box_row_get_header(row);
190 if(!cur) {
191 cur = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
192 gtk_widget_show(cur);
193 gtk_list_box_row_set_header(row, cur);
194 }
195 }
196
197 static void connect_button_cb(GtkButton *widget, gpointer user_data)
198 {
199 struct technology *tech = user_data;
200 if(tech->settings->selected)
201 service_toggle_connection(tech->settings->selected);
202 }
203
204 static void tether_button_cb(GtkButton *widget, gpointer user_data)
205 {
206 struct technology *tech = user_data;
207 gboolean state = technology_get_property_bool(tech, "Tethering");
208 if(tech->type != CONNECTION_TYPE_WIRELESS || state) {
209 technology_set_property(tech, "Tethering",
210 g_variant_new("b", !state));
211 } else
212 technology_wireless_tether(tech);
213 }
214
215 static void update_tethering(struct technology *tech)
216 {
217
218 gboolean state = technology_get_property_bool(tech, "Tethering");
219 GtkButton *button = GTK_BUTTON(tech->settings->tethering);
220 if(state)
221 gtk_button_set_label(button, _("Disable _tethering"));
222 else
223 gtk_button_set_label(button, _("Enable _tethering"));
224 }
225
226 static void update_connect_button(struct technology *tech)
227 {
228 struct technology_settings *item;
229 const gchar *button_state;
230 gchar *state;
231
232 item = tech->settings;
233
234 if(!item->selected) {
235 if(!shutting_down)
236 gtk_widget_set_sensitive(item->connect_button, FALSE);
237 gtk_widget_set_can_focus(item->connect_button, FALSE);
238 return;
239 }
240
241 state = service_get_property_string_raw(item->selected, "State", NULL);
242
243 gtk_widget_set_sensitive(item->connect_button, TRUE);
244 gtk_widget_set_can_focus(item->connect_button, TRUE);
245 if(!strcmp(state, "idle") || !strcmp(state, "disconnect"))
246 button_state = _("_Connect");
247 else if(!strcmp(state, "failure"))
248 button_state = _("Re_connect");
249 else
250 button_state = _("Dis_connect");
251 gtk_button_set_label(GTK_BUTTON(item->connect_button), button_state);
252
253 g_free(state);
254 }
255
256 static void service_selected(GtkListBox *box, GtkListBoxRow *row,
257 gpointer user_data)
258 {
259 struct technology *tech = user_data;
260 struct service *serv = NULL;
261 if(row)
262 serv = g_object_get_data(G_OBJECT(row), "service");
263 tech->settings->selected = serv;
264 update_connect_button(tech);
265 }
266
267 void service_evented(GtkWidget *widget, GdkEvent *event, gpointer user_data)
268 {
269 if(event->type == GDK_2BUTTON_PRESS)
270 connect_button_cb(NULL, user_data);
271 }
272
273 struct technology_settings *technology_create_settings(struct technology *tech,
274 GVariant *properties, GDBusProxy *proxy)
275 {
276 struct technology_settings *item = g_malloc(sizeof(*item));
277
278 GVariantIter *iter;
279 gchar *key;
280 GVariant *value;
281 GtkWidget *powerbox, *frame, *scrolled_window, *eventbox;
282
283 item->technology = tech;
284 item->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
285 g_free, (GDestroyNotify)g_variant_unref);
286 item->selected = NULL;
287
288 iter = g_variant_iter_new(properties);
289 while(g_variant_iter_loop(iter, "{sv}", &key, &value)) {
290 gchar *hkey = g_strdup(key);
291 g_variant_ref(value);
292 g_hash_table_insert(item->properties, hkey, value);
293 }
294 g_variant_iter_free(iter);
295
296 item->proxy = proxy;
297 g_signal_connect(proxy, "g-signal", G_CALLBACK(handle_proxy_signal),
298 tech);
299
300 item->grid = gtk_grid_new();
301 item->icon = gtk_image_new();
302 item->title = gtk_label_new(NULL);
303 item->status = gtk_label_new(NULL);
304 item->power_switch = gtk_switch_new();
305 item->contents = gtk_grid_new();
306 item->services = gtk_list_box_new();
307 item->buttons = gtk_grid_new();
308 item->connect_button = gtk_button_new_with_mnemonic(_("_Connect"));
309 item->filler = gtk_label_new(NULL);
310 powerbox = gtk_grid_new();
311 frame = gtk_frame_new(NULL);
312 scrolled_window = gtk_scrolled_window_new(NULL, NULL);
313 item->tethering = gtk_button_new_with_mnemonic(_("Enable _tethering"));
314 eventbox = gtk_event_box_new();
315
316 g_object_ref(item->grid);
317 g_object_ref(item->icon);
318 g_object_ref(item->title);
319 g_object_ref(item->status);
320 g_object_ref(item->power_switch);
321 g_object_ref(item->contents);
322 g_object_ref(item->services);
323 g_object_ref(item->buttons);
324 g_object_ref(item->connect_button);
325 g_object_ref(item->filler);
326 g_object_ref(item->tethering);
327
328 item->powersig = g_signal_connect(item->power_switch, "notify::active",
329 G_CALLBACK(toggle_power), tech);
330 gtk_list_box_set_selection_mode(GTK_LIST_BOX(item->services),
331 GTK_SELECTION_SINGLE);
332 gtk_list_box_set_header_func(GTK_LIST_BOX(item->services),
333 update_service_separator, NULL, NULL);
334 g_signal_connect(item->services, "row-selected",
335 G_CALLBACK(service_selected), tech);
336 g_signal_connect(eventbox, "button-press-event",
337 G_CALLBACK(service_evented), tech);
338 g_signal_connect(item->connect_button, "clicked",
339 G_CALLBACK(connect_button_cb), tech);
340 g_signal_connect(item->tethering, "clicked",
341 G_CALLBACK(tether_button_cb), tech);
342
343 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
344 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
345
346 style_set_margin_start(item->grid, MARGIN_LARGE);
347 style_set_margin_end(item->grid, MARGIN_LARGE);
348 style_set_margin_start(item->title, MARGIN_MEDIUM);
349 style_set_margin_end(item->title, MARGIN_MEDIUM);
350 style_add_context(item->title);
351 gtk_style_context_add_class(gtk_widget_get_style_context(item->title),
352 "cm-header-title");
353 style_set_margin_start(item->status, MARGIN_MEDIUM);
354 style_set_margin_end(item->status, MARGIN_MEDIUM);
355 gtk_widget_set_margin_top(item->contents, MARGIN_LARGE);
356 gtk_widget_set_margin_top(item->buttons, MARGIN_SMALL);
357
358 gtk_widget_set_hexpand(item->title, TRUE);
359 gtk_widget_set_hexpand(item->status, TRUE);
360 gtk_widget_set_vexpand(powerbox, FALSE);
361 gtk_widget_set_hexpand(item->contents, TRUE);
362 gtk_widget_set_vexpand(item->contents, TRUE);
363 gtk_widget_set_hexpand(item->services, TRUE);
364 gtk_widget_set_vexpand(item->services, TRUE);
365 gtk_widget_set_vexpand(scrolled_window, TRUE);
366 gtk_widget_set_hexpand(item->filler, TRUE);
367 gtk_widget_set_hexpand(item->buttons, TRUE);
368
369 gtk_widget_set_halign(item->icon, GTK_ALIGN_START);
370 gtk_widget_set_halign(item->title, GTK_ALIGN_START);
371 gtk_widget_set_halign(item->status, GTK_ALIGN_START);
372 gtk_widget_set_halign(item->power_switch, GTK_ALIGN_END);
373 gtk_widget_set_valign(item->power_switch, GTK_ALIGN_START);
374 gtk_widget_set_valign(powerbox, GTK_ALIGN_CENTER);
375 gtk_widget_set_halign(powerbox, GTK_ALIGN_END);
376 gtk_widget_set_valign(item->buttons, GTK_ALIGN_END);
377 gtk_widget_set_halign(item->tethering, GTK_ALIGN_START);
378 gtk_widget_set_halign(item->connect_button, GTK_ALIGN_END);
379
380 gtk_grid_attach(GTK_GRID(powerbox), item->power_switch, 0, 0, 1, 1);
381 gtk_container_add(GTK_CONTAINER(eventbox), item->services);
382 gtk_container_add(GTK_CONTAINER(scrolled_window), eventbox);
383 gtk_container_add(GTK_CONTAINER(frame), scrolled_window);
384 gtk_grid_attach(GTK_GRID(item->contents), frame, 0, 0, 1, 1);
385 gtk_grid_attach(GTK_GRID(item->buttons), item->tethering, 0, 0, 1, 1);
386 gtk_grid_attach(GTK_GRID(item->buttons), item->filler, 1, 0, 1, 1);
387 gtk_grid_attach(GTK_GRID(item->buttons), item->connect_button,
388 2, 0, 1, 1);
389 gtk_grid_attach(GTK_GRID(item->grid), item->icon, 0, 0, 1, 2);
390 gtk_grid_attach(GTK_GRID(item->grid), item->title,1, 0, 1, 1);
391 gtk_grid_attach(GTK_GRID(item->grid), item->status, 1, 1, 1, 1);
392 gtk_grid_attach(GTK_GRID(item->grid), powerbox, 2, 0, 1, 2);
393 gtk_grid_attach(GTK_GRID(item->grid), item->contents,0, 2, 3, 1);
394 gtk_grid_attach(GTK_GRID(item->grid), item->buttons, 0, 3, 3, 1);
395
396 gtk_widget_show_all(item->grid);
397
398 if(tech->type == CONNECTION_TYPE_P2P)
399 gtk_widget_hide(item->tethering);
400
401 return item;
402 }
403
404 void free_technology_settings(struct technology_settings *item)
405 {
406 if(!item)
407 return;
408
409 g_object_unref(item->icon);
410 g_object_unref(item->title);
411 g_object_unref(item->status);
412 g_object_unref(item->filler);
413 g_object_unref(item->tethering);
414 g_object_unref(item->power_switch);
415 g_object_unref(item->contents);
416 g_object_unref(item->services);
417 g_object_unref(item->buttons);
418 g_object_unref(item->connect_button);
419 g_object_unref(item->grid);
420 gtk_widget_destroy(item->grid);
421
422 g_object_unref(item->proxy);
423 g_hash_table_unref(item->properties);
424
425 g_free(item);
426 }
427
428 void technology_property_changed(struct technology *tech, const gchar *key)
429 {
430 update_power(tech);
431 update_status(tech);
432 update_tethering(tech);
433 }
434
435 void technology_add_service(struct technology *tech, struct service *serv)
436 {
437 gtk_container_add(GTK_CONTAINER(tech->settings->services), serv->item);
438 g_hash_table_insert(tech->services, g_strdup(serv->path), serv);
439
440 if(tech->type == CONNECTION_TYPE_VPN)
441 vpn_update_status(tech);
442 }
443
444 void technology_service_updated(struct technology *tech, struct service *serv)
445 {
446 if(tech->settings->selected == serv)
447 update_connect_button(tech);
448
449 if(tech->type == CONNECTION_TYPE_VPN)
450 vpn_update_status(tech);
451 }
452
453 void technology_remove_service(struct technology *tech, const gchar *path)
454 {
455 if(tech->settings->selected == g_hash_table_lookup(tech->services,
456 path)) {
457 tech->settings->selected = NULL;
458 update_connect_button(tech);
459 }
460 g_hash_table_remove(tech->services, path);
461
462 if(tech->type == CONNECTION_TYPE_VPN)
463 vpn_update_status(tech);
464 }
465
466 void technology_free(struct technology *item)
467 {
468 if(!item)
469 return;
470 free_list_item(item->list_item);
471 free_technology_settings(item->settings);
472 g_hash_table_unref(item->services);
473 g_free(item->path);
474 if(item->type == CONNECTION_TYPE_WIRELESS)
475 technology_wireless_free(item);
476 g_free(item);
477 }
478
479 void technology_init(struct technology *tech, GVariant *properties_v,
480 GDBusProxy *proxy)
481 {
482 GVariant *type_v;
483 const gchar *type;
484 GVariantDict *properties;
485
486 properties = g_variant_dict_new(properties_v);
487 type_v = g_variant_dict_lookup_value(properties, "Type", NULL);
488 type = g_variant_get_string(type_v, NULL);
489 g_variant_dict_unref(properties);
490
491 tech->type = connection_type_from_string(type);
492 tech->services = g_hash_table_new_full(g_str_hash, g_str_equal,
493 g_free, NULL);
494 tech->settings = technology_create_settings(tech, properties_v,
495 proxy);
496 tech->list_item = technology_create_item(tech);
497
498 update_connect_button(tech);
499 update_status(tech);
500 update_power(tech);
501 update_tethering(tech);
502
503 g_variant_unref(type_v);
504 }
505
506 static void set_icons(struct technology *tech)
507 {
508 const gchar *list_icon = "";
509 const gchar *settings_icon = "";
510
511 switch(tech->type) {
512 case CONNECTION_TYPE_ETHERNET:
513 list_icon = "network-wired-symbolic";
514 settings_icon = "network-wired";
515 break;
516 case CONNECTION_TYPE_WIRELESS:
517 list_icon = "network-wireless-symbolic";
518 settings_icon = "network-wireless";
519 break;
520 case CONNECTION_TYPE_BLUETOOTH:
521 list_icon = "bluetooth-symbolic";
522 settings_icon = "bluetooth-symbolic";
523 break;
524 case CONNECTION_TYPE_CELLULAR:
525 list_icon = "network-cellular-connected-symbolic";
526 settings_icon = "network-cellular-connected";
527 break;
528 case CONNECTION_TYPE_P2P:
529 list_icon = "network-transmit-symbolic";
530 settings_icon = "preferences-system-network";
531 break;
532 case CONNECTION_TYPE_VPN:
533 list_icon = "network-vpn-symbolic";
534 settings_icon = "network-vpn";
535 break;
536 case CONNECTION_TYPE_UNKNOWN:
537 case CONNECTION_TYPE_COUNT:
538 break;
539 }
540
541 gtk_image_set_from_icon_name(GTK_IMAGE(tech->list_item->icon),
542 list_icon, GTK_ICON_SIZE_LARGE_TOOLBAR);
543 gtk_image_set_from_icon_name(GTK_IMAGE(tech->settings->icon),
544 settings_icon, GTK_ICON_SIZE_DIALOG);
545
546 }
547
548 struct technology *technology_create(GDBusProxy *proxy, const gchar *path,
549 GVariant *properties)
550 {
551 struct technology *item;
552 GVariantDict *properties_d;
553 GVariant *type_v;
554 enum connection_type type;
555
556 properties_d = g_variant_dict_new(properties);
557 type_v = g_variant_dict_lookup_value(properties_d, "Type", NULL);
558 type = connection_type_from_string(g_variant_get_string(type_v, NULL));
559 g_variant_unref(type_v);
560 g_variant_dict_unref(properties_d);
561
562 item = g_malloc(sizeof(*item));
563 item->type = type;
564 item->path = g_strdup(path);
565
566 technology_init(item, properties, proxy);
567 set_icons(item);
568 if(type == CONNECTION_TYPE_WIRELESS)
569 technology_wireless_init(item, properties, proxy);
570
571 /* XXX: hack to fix window width with variable text length */
572 gtk_button_set_label(GTK_BUTTON(item->settings->connect_button),
573 _("Re_connect"));
574 gtk_button_set_label(GTK_BUTTON(item->settings->connect_button),
575 _("Dis_connect"));
576 gtk_button_set_label(GTK_BUTTON(item->settings->connect_button),
577 _("_Connect"));
578 update_connect_button(item);
579
580 return item;
581 }
582
583 GVariant *technology_get_property(struct technology *tech, const gchar *key)
584 {
585 return g_hash_table_lookup(tech->settings->properties, key);
586 }
587
588 const gchar *technology_get_property_string(struct technology *tech,
589 const gchar *key)
590 {
591 GVariant *value = technology_get_property(tech, key);
592 if(value)
593 return g_variant_get_string(value, NULL);
594 return "";
595 }
596
597 gboolean technology_get_property_bool(struct technology *tech, const gchar *key)
598 {
599 GVariant *value = technology_get_property(tech, key);
600 if(value)
601 return g_variant_get_boolean(value);
602 return FALSE;
603 }
604
605 void technology_set_property(struct technology *tech, const gchar *key,
606 GVariant *value)
607 {
608 GVariant *ret;
609 GError *error = NULL;
610
611 ret = g_dbus_proxy_call_sync(tech->settings->proxy, "SetProperty",
612 g_variant_new("(sv)", key, value),
613 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
614 if(error) {
615 g_warning("failed to set technology property %s: %s",
616 key, error->message);
617 if(!strcmp(key, "Tethering") &&
618 tech->type == CONNECTION_TYPE_ETHERNET &&
619 strstr(error->message, "NotSupported"))
620 show_error(_("Failed to enable ethernet tethering."),
621 _("Ethernet tethering has to be manually allowed in /etc/connman/main.conf."));
622 g_error_free(error);
623 return;
624 }
625 g_variant_unref(ret);
626 }