| 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 <string.h> |
| 22 |
|
| 23 |
#include <gio/gio.h> |
| 24 |
#include <glib.h> |
| 25 |
#include <glib/gi18n.h> |
| 26 |
|
| 27 |
#include "config.h" |
| 28 |
#include "dialog.h" |
| 29 |
#include "status.h" |
| 30 |
#include "style.h" |
| 31 |
#include "service.h" |
| 32 |
#include "settings.h" |
| 33 |
#include "util.h" |
| 34 |
#include "vpn.h" |
| 35 |
#include "wireless.h" |
| 36 |
|
| 37 |
static void update_name(struct service *serv) |
| 38 |
{ |
| 39 |
enum connection_type type = serv->type; |
| 40 |
gchar *name, *state, *state_r, *title, *error; |
| 41 |
name = service_get_property_string(serv, "Name", NULL); |
| 42 |
state = service_get_property_string(serv, "State", NULL); |
| 43 |
state_r = service_get_property_string_raw(serv, "State", NULL); |
| 44 |
if(!strcmp(state_r, "failure")) { |
| 45 |
const gchar *failure; |
| 46 |
error = service_get_property_string(serv, "Error", NULL); |
| 47 |
failure = failure_localized(error); |
| 48 |
if(strlen(failure)) |
| 49 |
title = g_strdup_printf("%s - %s: %s", name, |
| 50 |
state, failure); |
| 51 |
else |
| 52 |
title = g_strdup_printf("%s - %s", name, state); |
| 53 |
g_free(error); |
| 54 |
} |
| 55 |
else if(type == CONNECTION_TYPE_WIRELESS && !strcmp(state_r, "idle")) |
| 56 |
title = g_strdup_printf("%s", name); |
| 57 |
else |
| 58 |
title = g_strdup_printf("%s - %s", name, state); |
| 59 |
gtk_label_set_text(GTK_LABEL(serv->title), title); |
| 60 |
g_free(name); |
| 61 |
g_free(state); |
| 62 |
g_free(state_r); |
| 63 |
g_free(title); |
| 64 |
} |
| 65 |
|
| 66 |
static void set_label(struct service *serv, GtkWidget *label, |
| 67 |
const gchar *key, const gchar *subkey) |
| 68 |
{ |
| 69 |
gchar *value = service_get_property_string_raw(serv, key, subkey); |
| 70 |
gtk_label_set_text(GTK_LABEL(label), value); |
| 71 |
g_free(value); |
| 72 |
} |
| 73 |
|
| 74 |
void service_update_property_value(struct service *serv, const gchar *key, |
| 75 |
const gchar *subkey, GVariant *value) |
| 76 |
{ |
| 77 |
hash_table_set_dual_key(serv->properties, key, subkey, |
| 78 |
g_variant_ref(value)); |
| 79 |
if(serv->sett) |
| 80 |
settings_update(serv->sett, key, subkey, value); |
| 81 |
} |
| 82 |
|
| 83 |
void service_update_property(struct service *serv, const gchar *key, |
| 84 |
GVariant *value) |
| 85 |
{ |
| 86 |
if(!strcmp(g_variant_get_type_string(value), "a{sv}")) { |
| 87 |
gchar *subkey; |
| 88 |
GVariantIter *iter = g_variant_iter_new(value); |
| 89 |
while(g_variant_iter_loop(iter, "{sv}", &subkey, &value)) |
| 90 |
service_update_property_value(serv, key, subkey, value); |
| 91 |
g_variant_iter_free(iter); |
| 92 |
} else |
| 93 |
service_update_property_value(serv, key, NULL, value); |
| 94 |
|
| 95 |
update_name(serv); |
| 96 |
|
| 97 |
if(serv->type == CONNECTION_TYPE_WIRELESS) |
| 98 |
return; |
| 99 |
|
| 100 |
set_label(serv, serv->ipv4, "IPv4", "Address"); |
| 101 |
set_label(serv, serv->ipv4gateway, "IPv4", "Gateway"); |
| 102 |
set_label(serv, serv->ipv6, "IPv6", "Address"); |
| 103 |
set_label(serv, serv->ipv6gateway, "IPv6", "Gateway"); |
| 104 |
set_label(serv, serv->mac, "Ethernet", "Address"); |
| 105 |
} |
| 106 |
|
| 107 |
static void show_field(GtkWidget *entry) |
| 108 |
{ |
| 109 |
GtkWidget *label = g_object_get_data(G_OBJECT(entry), "label"); |
| 110 |
if(!label) |
| 111 |
return; |
| 112 |
|
| 113 |
gtk_widget_show(entry); |
| 114 |
gtk_widget_show(label); |
| 115 |
} |
| 116 |
|
| 117 |
static void hide_field(GtkWidget *entry) |
| 118 |
{ |
| 119 |
GtkWidget *label = g_object_get_data(G_OBJECT(entry), "label"); |
| 120 |
if(!label) |
| 121 |
return; |
| 122 |
|
| 123 |
gtk_widget_hide(entry); |
| 124 |
gtk_widget_hide(label); |
| 125 |
} |
| 126 |
|
| 127 |
static void update_fields(struct service *serv) |
| 128 |
{ |
| 129 |
gchar *state = service_get_property_string_raw(serv, "State", NULL); |
| 130 |
if(!strcmp(state, "idle") || !strcmp(state, "failure") || |
| 131 |
!strcmp(state, "disconnect")) { |
| 132 |
hide_field(serv->ipv4); |
| 133 |
hide_field(serv->ipv4gateway); |
| 134 |
hide_field(serv->ipv6); |
| 135 |
hide_field(serv->ipv6gateway); |
| 136 |
hide_field(serv->mac); |
| 137 |
} |
| 138 |
else { |
| 139 |
show_field(serv->ipv4); |
| 140 |
show_field(serv->ipv4gateway); |
| 141 |
show_field(serv->ipv6); |
| 142 |
show_field(serv->ipv6gateway); |
| 143 |
show_field(serv->mac); |
| 144 |
} |
| 145 |
g_free(state); |
| 146 |
} |
| 147 |
|
| 148 |
void service_update(struct service *serv, GVariant *properties) |
| 149 |
{ |
| 150 |
GVariantIter *iter; |
| 151 |
gchar *key; |
| 152 |
GVariant *value; |
| 153 |
|
| 154 |
iter = g_variant_iter_new(properties); |
| 155 |
while(g_variant_iter_loop(iter, "{sv}", &key, &value)) |
| 156 |
service_update_property(serv, key, value); |
| 157 |
g_variant_iter_free(iter); |
| 158 |
update_name(serv); |
| 159 |
|
| 160 |
technology_service_updated(serv->tech, serv); |
| 161 |
|
| 162 |
if(serv->type != CONNECTION_TYPE_WIRELESS) { |
| 163 |
update_fields(serv); |
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
service_wireless_update(serv); |
| 168 |
} |
| 169 |
|
| 170 |
static void ensure_field(GVariantDict *dict, const gchar *field, |
| 171 |
GVariant *value) |
| 172 |
{ |
| 173 |
if(g_variant_dict_contains(dict, field)) { |
| 174 |
g_variant_unref(value); |
| 175 |
return; |
| 176 |
} |
| 177 |
g_variant_dict_insert_value(dict, field, value); |
| 178 |
} |
| 179 |
|
| 180 |
static GVariant *add_missing_fields(const gchar *name, GVariant *value) |
| 181 |
{ |
| 182 |
GVariantDict *dict; |
| 183 |
|
| 184 |
if(strcmp(name, "Ethernet") && strncmp(name, "IPv", 3) && |
| 185 |
strncmp(name, "Proxy", 5)) |
| 186 |
return value; |
| 187 |
|
| 188 |
dict = g_variant_dict_new(value); |
| 189 |
if(!strcmp(name, "Ethernet")) { |
| 190 |
ensure_field(dict, "Method", g_variant_new("s", "auto")); |
| 191 |
ensure_field(dict, "Interface", g_variant_new("s", "")); |
| 192 |
ensure_field(dict, "Address", g_variant_new("s", "")); |
| 193 |
ensure_field(dict, "MTU", g_variant_new("q", 1500)); |
| 194 |
} else if(!strcmp(name, "IPv4")) { |
| 195 |
ensure_field(dict, "Method", g_variant_new("s", "off")); |
| 196 |
ensure_field(dict, "Address", g_variant_new("s", "")); |
| 197 |
ensure_field(dict, "Netmask", g_variant_new("s", "")); |
| 198 |
ensure_field(dict, "Gateway", g_variant_new("s", "")); |
| 199 |
} else if(!strcmp(name, "IPv6")) { |
| 200 |
ensure_field(dict, "Method", g_variant_new("s", "off")); |
| 201 |
ensure_field(dict, "Address", g_variant_new("s", "")); |
| 202 |
ensure_field(dict, "PrefixLength", g_variant_new("s", "")); |
| 203 |
ensure_field(dict, "Gateway", g_variant_new("s", "")); |
| 204 |
} |
| 205 |
|
| 206 |
g_variant_unref(value); |
| 207 |
value = g_variant_dict_end(dict); |
| 208 |
g_variant_dict_unref(dict); |
| 209 |
return value; |
| 210 |
} |
| 211 |
|
| 212 |
static void service_proxy_signal(GDBusProxy *proxy, gchar *sender, |
| 213 |
gchar *signal, GVariant *parameters, |
| 214 |
gpointer user_data) |
| 215 |
{ |
| 216 |
struct service *serv = user_data; |
| 217 |
if(!strcmp(signal, "PropertyChanged")) { |
| 218 |
GVariant *name_v, *value_v, *value; |
| 219 |
const gchar *name; |
| 220 |
|
| 221 |
name_v = g_variant_get_child_value(parameters, 0); |
| 222 |
value_v = g_variant_get_child_value(parameters, 1); |
| 223 |
|
| 224 |
name = g_variant_get_string(name_v, NULL); |
| 225 |
value = g_variant_get_child_value(value_v, 0); |
| 226 |
value = add_missing_fields(name, value); |
| 227 |
|
| 228 |
service_update_property(serv, name, value); |
| 229 |
g_variant_unref(value); |
| 230 |
g_variant_unref(name_v); |
| 231 |
g_variant_unref(value_v); |
| 232 |
|
| 233 |
technology_service_updated(serv->tech, serv); |
| 234 |
|
| 235 |
if(!strcmp(name, "State")) |
| 236 |
status_update(); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
static void settings_closed(struct service *serv) |
| 241 |
{ |
| 242 |
if(serv) { |
| 243 |
serv->sett = NULL; |
| 244 |
gtk_widget_set_sensitive(serv->settings_button, TRUE); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
static void settings_button_cb(GtkButton *button, gpointer user_data) |
| 249 |
{ |
| 250 |
struct service *serv = user_data; |
| 251 |
gtk_widget_set_sensitive(GTK_WIDGET(button), FALSE); |
| 252 |
serv->sett = settings_create(serv, settings_closed); |
| 253 |
} |
| 254 |
|
| 255 |
static GtkWidget *add_label(GtkWidget *grid, gint y, const gchar *text) |
| 256 |
{ |
| 257 |
GtkWidget *label, *value; |
| 258 |
|
| 259 |
label = gtk_label_new(text); |
| 260 |
value = gtk_label_new(NULL); |
| 261 |
|
| 262 |
style_set_margin(label, MARGIN_SMALL); |
| 263 |
g_object_set_data(G_OBJECT(value), "label", label); |
| 264 |
style_set_margin_start(label, MARGIN_LARGE); |
| 265 |
gtk_style_context_add_class(gtk_widget_get_style_context(label), |
| 266 |
"dim-label"); |
| 267 |
gtk_label_set_selectable(GTK_LABEL(value), TRUE); |
| 268 |
|
| 269 |
gtk_widget_set_hexpand(label, TRUE); |
| 270 |
gtk_widget_set_halign(label, GTK_ALIGN_START); |
| 271 |
gtk_widget_set_halign(value, GTK_ALIGN_START); |
| 272 |
|
| 273 |
gtk_grid_attach(GTK_GRID(grid), label, 0, y, 1, 1); |
| 274 |
gtk_grid_attach(GTK_GRID(grid), value, 1, y, 1, 1); |
| 275 |
|
| 276 |
return value; |
| 277 |
} |
| 278 |
|
| 279 |
void service_init(struct service *serv, GDBusProxy *proxy, const gchar *path, |
| 280 |
GVariant *properties) |
| 281 |
{ |
| 282 |
GtkGrid *item_grid; |
| 283 |
|
| 284 |
serv->proxy = proxy; |
| 285 |
serv->path = g_strdup(path); |
| 286 |
serv->properties = dual_hash_table_new((GDestroyNotify)g_variant_unref); |
| 287 |
serv->sett = NULL; |
| 288 |
|
| 289 |
serv->item = gtk_list_box_row_new(); |
| 290 |
serv->header = gtk_grid_new(); |
| 291 |
serv->title = gtk_label_new(NULL); |
| 292 |
serv->contents = gtk_grid_new(); |
| 293 |
serv->settings_button = gtk_button_new_from_icon_name( |
| 294 |
"emblem-system-symbolic", |
| 295 |
GTK_ICON_SIZE_MENU); |
| 296 |
item_grid = GTK_GRID(gtk_grid_new()); |
| 297 |
|
| 298 |
g_object_ref(serv->item); |
| 299 |
g_object_ref(serv->header); |
| 300 |
g_object_ref(serv->title); |
| 301 |
g_object_ref(serv->contents); |
| 302 |
g_object_set_data(G_OBJECT(serv->item), "service", serv); |
| 303 |
|
| 304 |
g_signal_connect(proxy, "g-signal", G_CALLBACK(service_proxy_signal), |
| 305 |
serv); |
| 306 |
g_signal_connect(serv->settings_button, "clicked", |
| 307 |
G_CALLBACK(settings_button_cb), serv); |
| 308 |
|
| 309 |
gtk_grid_set_column_homogeneous(GTK_GRID(serv->contents), TRUE); |
| 310 |
|
| 311 |
style_set_margin(serv->item, MARGIN_SMALL); |
| 312 |
style_set_margin(serv->title, MARGIN_SMALL); |
| 313 |
style_set_margin_start(serv->title, MARGIN_LARGE); |
| 314 |
style_set_margin_start(serv->contents, MARGIN_LARGE); |
| 315 |
gtk_widget_set_margin_top(serv->contents, 0); |
| 316 |
gtk_widget_set_margin_bottom(serv->contents, 0); |
| 317 |
|
| 318 |
gtk_widget_set_hexpand(serv->item, TRUE); |
| 319 |
gtk_widget_set_hexpand(serv->header, TRUE); |
| 320 |
gtk_widget_set_hexpand(serv->title, TRUE); |
| 321 |
gtk_widget_set_hexpand(serv->contents, TRUE); |
| 322 |
gtk_widget_set_halign(serv->title, GTK_ALIGN_START); |
| 323 |
|
| 324 |
gtk_grid_attach(GTK_GRID(serv->header), serv->title, 0, 0, 1, 1); |
| 325 |
gtk_grid_attach(GTK_GRID(serv->header), serv->settings_button, |
| 326 |
1, 0, 1, 1); |
| 327 |
gtk_grid_attach(item_grid, serv->header, 0, 0, 1, 1); |
| 328 |
gtk_grid_attach(item_grid, serv->contents, 0, 1, 1, 1); |
| 329 |
gtk_container_add(GTK_CONTAINER(serv->item), GTK_WIDGET(item_grid)); |
| 330 |
if(serv->type == CONNECTION_TYPE_WIRELESS) { |
| 331 |
gtk_widget_show_all(serv->item); |
| 332 |
return; |
| 333 |
} |
| 334 |
|
| 335 |
serv->ipv4 = add_label(serv->contents, 0, _("IPv4 address")); |
| 336 |
serv->ipv4gateway = add_label(serv->contents, 1, _("IPv4 gateway")); |
| 337 |
serv->ipv6 = add_label(serv->contents, 2, _("IPv6 address")); |
| 338 |
serv->ipv6gateway = add_label(serv->contents, 3, _("IPv6 gateway")); |
| 339 |
if(serv->type != CONNECTION_TYPE_VPN) |
| 340 |
serv->mac = add_label(serv->contents, 4, _("MAC address")); |
| 341 |
else |
| 342 |
serv->mac = gtk_label_new(NULL); |
| 343 |
|
| 344 |
gtk_widget_show_all(serv->item); |
| 345 |
} |
| 346 |
|
| 347 |
struct service *service_create(struct technology *tech, GDBusProxy *proxy, |
| 348 |
const gchar *path, GVariant *properties) |
| 349 |
{ |
| 350 |
struct service *serv; |
| 351 |
enum connection_type type; |
| 352 |
GVariantDict *properties_d; |
| 353 |
GVariant *type_v; |
| 354 |
|
| 355 |
properties_d = g_variant_dict_new(properties); |
| 356 |
type_v = g_variant_dict_lookup_value(properties_d, "Type", NULL); |
| 357 |
type = connection_type_from_string(g_variant_get_string(type_v, NULL)); |
| 358 |
g_variant_unref(type_v); |
| 359 |
g_variant_dict_unref(properties_d); |
| 360 |
|
| 361 |
serv = g_malloc(sizeof(*serv)); |
| 362 |
serv->type = type; |
| 363 |
serv->tech = tech; |
| 364 |
serv->sett = NULL; |
| 365 |
|
| 366 |
service_init(serv, proxy, path, properties); |
| 367 |
if(serv->type == CONNECTION_TYPE_WIRELESS) |
| 368 |
service_wireless_init(serv, proxy, path, properties); |
| 369 |
|
| 370 |
service_update(serv, properties); |
| 371 |
|
| 372 |
return serv; |
| 373 |
} |
| 374 |
|
| 375 |
void service_free(struct service *serv) |
| 376 |
{ |
| 377 |
if(!serv) |
| 378 |
return; |
| 379 |
if(serv->sett) { |
| 380 |
serv->sett->serv = NULL; |
| 381 |
gtk_window_close(GTK_WINDOW(serv->sett->window)); |
| 382 |
} |
| 383 |
g_object_unref(serv->proxy); |
| 384 |
g_free(serv->path); |
| 385 |
dual_hash_table_unref(serv->properties); |
| 386 |
gtk_widget_destroy(serv->item); |
| 387 |
g_object_unref(serv->item); |
| 388 |
g_object_unref(serv->contents); |
| 389 |
g_object_unref(serv->header); |
| 390 |
if(serv->type == CONNECTION_TYPE_WIRELESS) |
| 391 |
service_wireless_free(serv); |
| 392 |
g_free(serv); |
| 393 |
} |
| 394 |
|
| 395 |
static gboolean strv_contains(const gchar *const *strv, const gchar *str) { |
| 396 |
const gchar *const *iter = strv; |
| 397 |
while(*iter && strcmp(*iter, str)) |
| 398 |
iter++; |
| 399 |
return *iter != NULL; |
| 400 |
} |
| 401 |
|
| 402 |
static void show_wireless_error(struct service *serv, const gchar *message) |
| 403 |
{ |
| 404 |
const gchar *e; |
| 405 |
gchar **security; |
| 406 |
|
| 407 |
e = _("IEEE8021x secured services have to be manually configured."); |
| 408 |
security = service_get_property_strv(serv, "Security", NULL); |
| 409 |
|
| 410 |
if(strv_contains((const gchar **)security, "ieee8021x")) |
| 411 |
show_error(_("Failed to toggle connection state."), e); |
| 412 |
|
| 413 |
g_strfreev(security); |
| 414 |
} |
| 415 |
|
| 416 |
static void service_toggle_connection_cb(GObject *source, GAsyncResult *res, |
| 417 |
gpointer user_data) |
| 418 |
{ |
| 419 |
struct service *serv; |
| 420 |
const gchar *ia = "GDBus.Error:net.connman.Error.InvalidArguments"; |
| 421 |
const gchar *c = "GDBus.Error:net.connman.Error.Canceled"; |
| 422 |
const gchar *ip = "GDBus.Error:net.connman.Error.InProgress"; |
| 423 |
const gchar *oa = "GDBus.Error:net.connman.Error.OperationAborted"; |
| 424 |
const gchar *f = "GDBus.Error:net.connman.Error.Failed"; |
| 425 |
GError *error = NULL; |
| 426 |
GVariant *out; |
| 427 |
|
| 428 |
serv = user_data; |
| 429 |
out = g_dbus_proxy_call_finish(serv->proxy, res, &error); |
| 430 |
if(error) { |
| 431 |
/* |
| 432 |
* InvalidArguments is thrown when user cancels the dialog, |
| 433 |
* so ignore it, Failed is returned in 1.29 and older when |
| 434 |
* cancelling connects to hidden wireless networks |
| 435 |
*/ |
| 436 |
g_warning("failed to toggle connection state: %s", |
| 437 |
error->message); |
| 438 |
if(strncmp(ia, error->message, strlen(ia)) && |
| 439 |
strncmp(c, error->message, strlen(c)) && |
| 440 |
strncmp(ip, error->message, strlen(ip)) && |
| 441 |
strncmp(oa, error->message, strlen(oa)) && |
| 442 |
strncmp(f, error->message, strlen(f))) |
| 443 |
show_error(_("Failed to toggle connection state."), |
| 444 |
error->message); |
| 445 |
else if(serv->type == CONNECTION_TYPE_WIRELESS && |
| 446 |
!strncmp(ia, error->message, strlen(ia))) |
| 447 |
show_wireless_error(serv, error->message); |
| 448 |
|
| 449 |
g_error_free(error); |
| 450 |
return; |
| 451 |
} |
| 452 |
g_variant_unref(out); |
| 453 |
} |
| 454 |
|
| 455 |
void service_toggle_connection(struct service *serv) |
| 456 |
{ |
| 457 |
const gchar *function; |
| 458 |
gchar *state; |
| 459 |
|
| 460 |
state = service_get_property_string_raw(serv, "State", NULL); |
| 461 |
|
| 462 |
if(!strcmp(state, "idle") || !strcmp(state, "failure")) |
| 463 |
function = "Connect"; |
| 464 |
else |
| 465 |
function = "Disconnect"; |
| 466 |
|
| 467 |
g_free(state); |
| 468 |
|
| 469 |
g_dbus_proxy_call(serv->proxy, function, NULL, |
| 470 |
G_DBUS_CALL_FLAGS_NONE, CONNECTION_TIMEOUT, NULL, |
| 471 |
service_toggle_connection_cb, serv); |
| 472 |
} |
| 473 |
|
| 474 |
GVariant *service_get_property(struct service *serv, const char *key, |
| 475 |
const char *subkey) |
| 476 |
{ |
| 477 |
GVariant *variant; |
| 478 |
|
| 479 |
variant = hash_table_get_dual_key(serv->properties, key, subkey); |
| 480 |
if(!variant) |
| 481 |
return NULL; |
| 482 |
g_variant_ref(variant); |
| 483 |
return variant; |
| 484 |
} |
| 485 |
|
| 486 |
gchar *service_get_property_string_raw(struct service *serv, const char *key, |
| 487 |
const char *subkey) |
| 488 |
{ |
| 489 |
if(!serv || !key) |
| 490 |
return g_strdup(""); |
| 491 |
GVariant *prop = service_get_property(serv, key, subkey); |
| 492 |
gchar *str = variant_to_str(prop); |
| 493 |
if(prop) |
| 494 |
g_variant_unref(prop); |
| 495 |
return str; |
| 496 |
} |
| 497 |
|
| 498 |
static gchar *wireless_name(struct service *serv) |
| 499 |
{ |
| 500 |
gchar *name, **security, **iter; |
| 501 |
const gchar *out; |
| 502 |
int security_level = 0; |
| 503 |
|
| 504 |
name = service_get_property_string_raw(serv, "Name", NULL); |
| 505 |
if(strlen(name)) |
| 506 |
return name; |
| 507 |
g_free(name); |
| 508 |
|
| 509 |
security = service_get_property_strv(serv, "Security", NULL); |
| 510 |
for(iter = security; *iter; iter++) { |
| 511 |
if(!strcmp("ieee8021x", *iter)) |
| 512 |
security_level = 3; |
| 513 |
else if(security_level < 2 && !strcmp("psk", *iter)) |
| 514 |
security_level = 2; |
| 515 |
else if(security_level < 1 && !strcmp("wps", *iter)) |
| 516 |
security_level = 1; |
| 517 |
} |
| 518 |
out = (security_level == 3 ? _("Hidden ieee8021x secured network") : |
| 519 |
security_level == 2 ? _("Hidden WPA secured network") : |
| 520 |
security_level == 1 ? _("Hidden WPS secured network") : |
| 521 |
_("Hidden unsecured network")); |
| 522 |
g_strfreev(security); |
| 523 |
return g_strdup(out); |
| 524 |
} |
| 525 |
|
| 526 |
gchar *service_get_property_string(struct service *serv, const char *key, |
| 527 |
const char *subkey) |
| 528 |
{ |
| 529 |
if(!serv) |
| 530 |
return g_strdup(""); |
| 531 |
|
| 532 |
if(key && subkey) { |
| 533 |
if(!strcmp(subkey, "PrefixLength") && |
| 534 |
(!strcmp(key, "IPv6") || |
| 535 |
!strcmp(key, "IPv6.Configuration"))) { |
| 536 |
int len = service_get_property_int(serv, key, subkey); |
| 537 |
if(!len) |
| 538 |
return g_strdup(""); |
| 539 |
return g_strdup_printf("%d", len); |
| 540 |
} else if(!strcmp(key, "Proxy") && !strcmp(subkey, "Method")) { |
| 541 |
gchar *str = service_get_property_string_raw(serv, key, |
| 542 |
subkey); |
| 543 |
const gchar *out; |
| 544 |
if(!strcmp(str, "direct")) |
| 545 |
out = _("Direct"); |
| 546 |
else if(!strcmp(str, "auto")) |
| 547 |
out = _("Automatic"); |
| 548 |
else |
| 549 |
out = _("None"); |
| 550 |
g_free(str); |
| 551 |
return g_strdup(out); |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
if(key) { |
| 556 |
if(!strcmp(key, "AutoConnect")) { |
| 557 |
gboolean ac; |
| 558 |
|
| 559 |
ac = service_get_property_boolean(serv, key, subkey); |
| 560 |
if(ac) |
| 561 |
return g_strdup(_("On")); |
| 562 |
return g_strdup(_("Off")); |
| 563 |
} else if(!strcmp(key, "State")) { |
| 564 |
gchar *str = service_get_property_string_raw(serv, key, |
| 565 |
subkey); |
| 566 |
gchar *out = g_strdup(status_localized(str)); |
| 567 |
g_free(str); |
| 568 |
return out; |
| 569 |
} else if(serv->type == CONNECTION_TYPE_ETHERNET && |
| 570 |
!strcmp(key, "Name")) { |
| 571 |
return service_get_property_string_raw(serv, "Ethernet", |
| 572 |
"Interface"); |
| 573 |
} else if(serv->type == CONNECTION_TYPE_WIRELESS && |
| 574 |
!strcmp(key, "Name")) { |
| 575 |
return wireless_name(serv); |
| 576 |
} |
| 577 |
|
| 578 |
} |
| 579 |
|
| 580 |
return service_get_property_string_raw(serv, key, subkey); |
| 581 |
} |
| 582 |
|
| 583 |
gchar **service_get_property_strv(struct service *serv, const char *key, |
| 584 |
const char *subkey) |
| 585 |
{ |
| 586 |
if(!serv || !key) |
| 587 |
return g_malloc0(sizeof(gchar *)); |
| 588 |
GVariant *prop = service_get_property(serv, key, subkey); |
| 589 |
if(!prop) |
| 590 |
return g_malloc0(sizeof(gchar *)); |
| 591 |
gchar **out = variant_to_strv(prop); |
| 592 |
if(prop) |
| 593 |
g_variant_unref(prop); |
| 594 |
return out; |
| 595 |
} |
| 596 |
|
| 597 |
gboolean service_get_property_boolean(struct service *serv, const char *key, |
| 598 |
const char *subkey) |
| 599 |
{ |
| 600 |
if(!serv || !key) |
| 601 |
return FALSE; |
| 602 |
GVariant *prop = service_get_property(serv, key, subkey); |
| 603 |
gboolean out = variant_to_bool(prop); |
| 604 |
if(prop) |
| 605 |
g_variant_unref(prop); |
| 606 |
return out; |
| 607 |
} |
| 608 |
|
| 609 |
int service_get_property_int(struct service *serv, const char *key, |
| 610 |
const char *subkey) |
| 611 |
{ |
| 612 |
if(!serv || !key) |
| 613 |
return 0; |
| 614 |
GVariant *prop = service_get_property(serv, key, subkey); |
| 615 |
int out = variant_to_int(prop); |
| 616 |
if(prop) |
| 617 |
g_variant_unref(prop); |
| 618 |
return out; |
| 619 |
} |
| 620 |
|
| 621 |
void service_set_property(struct service *serv, const char *key, |
| 622 |
GVariant *value) |
| 623 |
{ |
| 624 |
GVariant *ret; |
| 625 |
GVariant *parameters; |
| 626 |
GError *error = NULL; |
| 627 |
|
| 628 |
GVariant *old; |
| 629 |
gboolean equal = TRUE; |
| 630 |
if(strcmp(g_variant_get_type_string(value), "a{sv}")) { |
| 631 |
old = service_get_property(serv, key, NULL); |
| 632 |
equal = old && g_variant_equal(old, value); |
| 633 |
if(old) |
| 634 |
g_variant_unref(old); |
| 635 |
} else { |
| 636 |
gchar *subkey; |
| 637 |
GVariant *svalue; |
| 638 |
GVariantIter *iter = g_variant_iter_new(value); |
| 639 |
while(g_variant_iter_loop(iter, "{sv}", &subkey, &svalue)) { |
| 640 |
old = service_get_property(serv, key, subkey); |
| 641 |
equal = equal && old && g_variant_equal(old, svalue); |
| 642 |
if(old) |
| 643 |
g_variant_unref(old); |
| 644 |
} |
| 645 |
g_variant_iter_free(iter); |
| 646 |
} |
| 647 |
if(equal) |
| 648 |
return; |
| 649 |
|
| 650 |
parameters = g_variant_new("(sv)", key, value); |
| 651 |
ret = g_dbus_proxy_call_sync(serv->proxy, "SetProperty", parameters, |
| 652 |
G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); |
| 653 |
if(error) { |
| 654 |
g_warning("failed to set property %s: %s", key, error->message); |
| 655 |
g_error_free(error); |
| 656 |
return; |
| 657 |
} |
| 658 |
g_variant_unref(ret); |
| 659 |
} |
| 660 |
|
| 661 |
void service_clear_properties(struct service *serv) |
| 662 |
{ |
| 663 |
GVariantBuilder *b; |
| 664 |
|
| 665 |
if(serv->type == CONNECTION_TYPE_VPN) |
| 666 |
return; |
| 667 |
|
| 668 |
b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); |
| 669 |
g_variant_builder_add(b, "{sv}", "Method", |
| 670 |
g_variant_new_string("dhcp")); |
| 671 |
service_set_property(serv, "IPv4.Configuration", |
| 672 |
g_variant_builder_end(b)); |
| 673 |
g_variant_builder_unref(b); |
| 674 |
|
| 675 |
b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); |
| 676 |
g_variant_builder_add(b, "{sv}", "Method", |
| 677 |
g_variant_new_string("auto")); |
| 678 |
g_variant_builder_add(b, "{sv}", "Privacy", |
| 679 |
g_variant_new_string("disabled")); |
| 680 |
service_set_property(serv, "IPv6.Configuration", |
| 681 |
g_variant_builder_end(b)); |
| 682 |
g_variant_builder_unref(b); |
| 683 |
|
| 684 |
b = g_variant_builder_new(G_VARIANT_TYPE("as")); |
| 685 |
service_set_property(serv, "Nameservers.Configuration", |
| 686 |
g_variant_builder_end(b)); |
| 687 |
g_variant_builder_unref(b); |
| 688 |
|
| 689 |
b = g_variant_builder_new(G_VARIANT_TYPE("as")); |
| 690 |
service_set_property(serv, "Timeservers.Configuration", |
| 691 |
g_variant_builder_end(b)); |
| 692 |
g_variant_builder_unref(b); |
| 693 |
|
| 694 |
b = g_variant_builder_new(G_VARIANT_TYPE("as")); |
| 695 |
service_set_property(serv, "Domains.Configuration", |
| 696 |
g_variant_builder_end(b)); |
| 697 |
g_variant_builder_unref(b); |
| 698 |
|
| 699 |
b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); |
| 700 |
g_variant_builder_add(b, "{sv}", "Method", |
| 701 |
g_variant_new_string("direct")); |
| 702 |
service_set_property(serv, "Proxy.Configuration", |
| 703 |
g_variant_builder_end(b)); |
| 704 |
g_variant_builder_unref(b); |
| 705 |
} |
| 706 |
|
| 707 |
void service_remove(struct service *serv) |
| 708 |
{ |
| 709 |
GVariant *ret; |
| 710 |
GError *error = NULL; |
| 711 |
|
| 712 |
ret = g_dbus_proxy_call_sync(serv->proxy, "Remove", NULL, |
| 713 |
G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); |
| 714 |
if(error) { |
| 715 |
g_warning("failed to remove service: %s", error->message); |
| 716 |
g_error_free(error); |
| 717 |
return; |
| 718 |
} |
| 719 |
g_variant_unref(ret); |
| 720 |
} |
| 721 |
|
| 722 |
void service_set_properties(struct service *serv, GVariant *properties) |
| 723 |
{ |
| 724 |
GVariantIter *iter; |
| 725 |
gchar *key; |
| 726 |
GVariant *value; |
| 727 |
|
| 728 |
iter = g_variant_iter_new(properties); |
| 729 |
while(g_variant_iter_loop(iter, "{sv}", &key, &value)) |
| 730 |
service_set_property(serv, key, value); |
| 731 |
g_variant_iter_free(iter); |
| 732 |
} |