| 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 <gio/gio.h> |
| 22 |
#include <glib/gi18n.h> |
| 23 |
#include <gtk/gtk.h> |
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#include "config.h" |
| 27 |
#include "dialog.h" |
| 28 |
#include "main.h" |
| 29 |
#include "style.h" |
| 30 |
#include "technology.h" |
| 31 |
#include "wireless.h" |
| 32 |
|
| 33 |
struct wireless_service { |
| 34 |
struct service *parent; |
| 35 |
|
| 36 |
GtkWidget *favourite; |
| 37 |
GtkWidget *security; |
| 38 |
GtkWidget *signal; |
| 39 |
}; |
| 40 |
|
| 41 |
static void scan_cb_cb(GObject *source, GAsyncResult *res, gpointer user_data) |
| 42 |
{ |
| 43 |
GVariant *ret; |
| 44 |
GError *error = NULL; |
| 45 |
ret = g_dbus_proxy_call_finish((GDBusProxy *)user_data, res, &error); |
| 46 |
if(error) { |
| 47 |
g_warning("failed to scan wifi: %s", error->message); |
| 48 |
g_error_free(error); |
| 49 |
return; |
| 50 |
} |
| 51 |
g_variant_unref(ret); |
| 52 |
} |
| 53 |
|
| 54 |
static gboolean scan_cb(gpointer user_data) |
| 55 |
{ |
| 56 |
struct technology *tech = user_data; |
| 57 |
GHashTable *properties = tech->settings->properties; |
| 58 |
if(!variant_to_bool(g_hash_table_lookup(properties, "Powered"))) |
| 59 |
return TRUE; |
| 60 |
if(variant_to_bool(g_hash_table_lookup(properties, "Tethering"))) |
| 61 |
return TRUE; |
| 62 |
|
| 63 |
g_dbus_proxy_call(tech->settings->proxy, "Scan", NULL, |
| 64 |
G_DBUS_CALL_FLAGS_NONE, -1, NULL, scan_cb_cb, |
| 65 |
tech->settings->proxy); |
| 66 |
return TRUE; |
| 67 |
} |
| 68 |
|
| 69 |
void technology_wireless_free(struct technology *tech) |
| 70 |
{ |
| 71 |
g_source_remove(GPOINTER_TO_INT(tech->data)); |
| 72 |
} |
| 73 |
|
| 74 |
void technology_wireless_init(struct technology *tech, GVariant *properties, |
| 75 |
GDBusProxy *proxy) |
| 76 |
{ |
| 77 |
int id; |
| 78 |
|
| 79 |
scan_cb(tech); |
| 80 |
id = g_timeout_add_seconds(WIRELESS_SCAN_INTERVAL, |
| 81 |
scan_cb, tech); |
| 82 |
tech->data = GINT_TO_POINTER(id); |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
void service_wireless_free(struct service *tech) |
| 87 |
{ |
| 88 |
g_free(tech->data); |
| 89 |
} |
| 90 |
|
| 91 |
static gboolean check_ssid(GtkWidget *entry) |
| 92 |
{ |
| 93 |
const gchar *str = gtk_entry_get_text(GTK_ENTRY(entry)); |
| 94 |
int len = strlen(str); |
| 95 |
|
| 96 |
return len >= 1 && len <= 32; |
| 97 |
} |
| 98 |
|
| 99 |
static gboolean check_pass(GtkWidget *entry) |
| 100 |
{ |
| 101 |
const gchar *str = gtk_entry_get_text(GTK_ENTRY(entry)); |
| 102 |
int len = strlen(str); |
| 103 |
|
| 104 |
return len >= 8 && len <= 63; |
| 105 |
} |
| 106 |
|
| 107 |
static void toggle_entry_mode(GtkToggleButton *button, gpointer user_data) |
| 108 |
{ |
| 109 |
GtkEntry *entry = GTK_ENTRY(user_data); |
| 110 |
gboolean mode = gtk_toggle_button_get_active(button); |
| 111 |
|
| 112 |
if(mode) { |
| 113 |
gtk_entry_set_visibility(entry, TRUE); |
| 114 |
gtk_entry_set_input_purpose(entry, GTK_INPUT_PURPOSE_FREE_FORM); |
| 115 |
} else { |
| 116 |
gtk_entry_set_visibility(entry, FALSE); |
| 117 |
gtk_entry_set_input_purpose(entry, GTK_INPUT_PURPOSE_PASSWORD); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
void technology_wireless_tether(struct technology *tech) |
| 122 |
{ |
| 123 |
const gchar *title, *ssid, *pass; |
| 124 |
GVariant *old_ssid, *old_pass; |
| 125 |
struct token_element *ssid_e, *pass_e, *check; |
| 126 |
GPtrArray *tokens; |
| 127 |
gboolean success; |
| 128 |
|
| 129 |
ssid = pass = NULL; |
| 130 |
tokens = g_ptr_array_new_full(3, (GDestroyNotify)free_token_element); |
| 131 |
|
| 132 |
title = _("Set Access Point SSID and passphrase"); |
| 133 |
|
| 134 |
old_ssid = g_hash_table_lookup(tech->settings->properties, |
| 135 |
"TetheringIdentifier"); |
| 136 |
old_pass = g_hash_table_lookup(tech->settings->properties, |
| 137 |
"TetheringPassphrase"); |
| 138 |
|
| 139 |
if(old_ssid) |
| 140 |
ssid = g_variant_get_string(old_ssid, NULL); |
| 141 |
if(old_pass) |
| 142 |
pass = g_variant_get_string(old_pass, NULL); |
| 143 |
|
| 144 |
ssid_e = token_new_entry_full(_("SSID"), FALSE, ssid, check_ssid); |
| 145 |
pass_e = token_new_entry_full(_("Passphrase"), TRUE, pass, check_pass); |
| 146 |
check = token_new_checkbox(_("_Show passphrase"), FALSE); |
| 147 |
g_ptr_array_add(tokens, ssid_e); |
| 148 |
g_ptr_array_add(tokens, pass_e); |
| 149 |
g_ptr_array_add(tokens, check); |
| 150 |
|
| 151 |
g_signal_connect(check->content, "toggled", |
| 152 |
G_CALLBACK(toggle_entry_mode), pass_e->content); |
| 153 |
|
| 154 |
success = dialog_ask_tokens(title, tokens); |
| 155 |
if(!success) |
| 156 |
goto out; |
| 157 |
technology_set_property(tech, "TetheringIdentifier", |
| 158 |
g_variant_new("s", ssid_e->value)); |
| 159 |
technology_set_property(tech, "TetheringPassphrase", |
| 160 |
g_variant_new("s", pass_e->value)); |
| 161 |
technology_set_property(tech, "Tethering", |
| 162 |
g_variant_new("b", TRUE)); |
| 163 |
out: |
| 164 |
g_ptr_array_free(tokens, TRUE); |
| 165 |
} |
| 166 |
|
| 167 |
void service_wireless_init(struct service *serv, GDBusProxy *proxy, |
| 168 |
const gchar *path, GVariant *properties) |
| 169 |
{ |
| 170 |
struct wireless_service *item = g_malloc(sizeof(*serv)); |
| 171 |
|
| 172 |
serv->data = item; |
| 173 |
item->parent = serv; |
| 174 |
item->favourite = gtk_image_new_from_icon_name("object-select-symbolic", |
| 175 |
GTK_ICON_SIZE_MENU); |
| 176 |
item->security = gtk_image_new_from_icon_name("", GTK_ICON_SIZE_MENU); |
| 177 |
item->signal = gtk_image_new_from_icon_name("", GTK_ICON_SIZE_MENU); |
| 178 |
|
| 179 |
style_add_context(serv->title); |
| 180 |
style_set_margin(item->favourite, MARGIN_SMALL); |
| 181 |
style_set_margin(item->security, MARGIN_SMALL); |
| 182 |
style_set_margin(item->signal, MARGIN_SMALL); |
| 183 |
|
| 184 |
gtk_widget_set_halign(item->favourite, GTK_ALIGN_END); |
| 185 |
gtk_widget_set_halign(item->signal, GTK_ALIGN_END); |
| 186 |
gtk_widget_set_halign(item->security, GTK_ALIGN_END); |
| 187 |
gtk_widget_set_valign(item->favourite, GTK_ALIGN_CENTER); |
| 188 |
gtk_widget_set_valign(item->signal, GTK_ALIGN_CENTER); |
| 189 |
gtk_widget_set_valign(item->security, GTK_ALIGN_CENTER); |
| 190 |
|
| 191 |
gtk_grid_insert_column(GTK_GRID(serv->header), 1); |
| 192 |
gtk_grid_insert_column(GTK_GRID(serv->header), 1); |
| 193 |
gtk_grid_insert_column(GTK_GRID(serv->header), 1); |
| 194 |
gtk_grid_attach(GTK_GRID(serv->header), item->favourite, 1, 0, 1, 1); |
| 195 |
gtk_grid_attach(GTK_GRID(serv->header), item->security, 2, 0, 1, 1); |
| 196 |
gtk_grid_attach(GTK_GRID(serv->header), item->signal, 3, 0, 1, 1); |
| 197 |
|
| 198 |
gtk_widget_show_all(serv->header); |
| 199 |
gtk_widget_hide(serv->contents); |
| 200 |
|
| 201 |
service_wireless_update(serv); |
| 202 |
} |
| 203 |
|
| 204 |
void service_wireless_update(struct service *serv) |
| 205 |
{ |
| 206 |
struct wireless_service *item = serv->data; |
| 207 |
gchar *name; |
| 208 |
GtkStyleContext *context; |
| 209 |
int width; |
| 210 |
|
| 211 |
GVariant *variant; |
| 212 |
int strength; |
| 213 |
|
| 214 |
variant = service_get_property(serv, "Security", NULL); |
| 215 |
if(variant) { |
| 216 |
const gchar **value; |
| 217 |
const gchar **cur; |
| 218 |
int security = 0; |
| 219 |
const gchar *icon_name; |
| 220 |
value = g_variant_get_strv(variant, NULL); |
| 221 |
for(cur = value; *cur; cur++) { |
| 222 |
if(!strcmp("ieee8021x", *cur)) { |
| 223 |
security = 3; |
| 224 |
break; |
| 225 |
} |
| 226 |
if(!strcmp("psk", *cur)) |
| 227 |
security = 2; |
| 228 |
else if(security < 2 && !strcmp("wps", *cur)) |
| 229 |
security = 1; |
| 230 |
} |
| 231 |
g_free(value); |
| 232 |
icon_name = (security == 3 ? "security-high-symbolic" : |
| 233 |
security == 2 ? "security-medium-symbolic" : |
| 234 |
security == 1 ? "security-low-symbolic" : |
| 235 |
NULL); |
| 236 |
gtk_image_set_from_icon_name(GTK_IMAGE(item->security), |
| 237 |
icon_name, GTK_ICON_SIZE_MENU); |
| 238 |
if(!security) { |
| 239 |
gtk_widget_hide(item->security); |
| 240 |
style_set_margin_end(item->favourite, |
| 241 |
3*MARGIN_SMALL + 16); |
| 242 |
} else { |
| 243 |
gtk_widget_show(item->security); |
| 244 |
style_set_margin_end(item->favourite, |
| 245 |
MARGIN_SMALL); |
| 246 |
} |
| 247 |
g_variant_unref(variant); |
| 248 |
} else |
| 249 |
gtk_widget_hide(item->security); |
| 250 |
|
| 251 |
strength = service_get_property_int(serv, "Strength", NULL); |
| 252 |
gtk_image_set_from_icon_name(GTK_IMAGE(item->signal), |
| 253 |
SIGNAL_TO_ICON("wireless", strength), |
| 254 |
GTK_ICON_SIZE_MENU); |
| 255 |
|
| 256 |
if(service_get_property_boolean(serv, "Favorite", NULL)) |
| 257 |
gtk_widget_show(item->favourite); |
| 258 |
else |
| 259 |
gtk_widget_hide(item->favourite); |
| 260 |
|
| 261 |
context = gtk_widget_get_style_context(serv->title); |
| 262 |
name = service_get_property_string_raw(serv, "Name", NULL); |
| 263 |
if(strlen(name)) { |
| 264 |
style_set_margin_end(item->signal, MARGIN_SMALL); |
| 265 |
gtk_style_context_remove_class(context, "cm-wireless-hidden"); |
| 266 |
gtk_widget_show(serv->settings_button); |
| 267 |
g_free(name); |
| 268 |
return; |
| 269 |
} |
| 270 |
|
| 271 |
gtk_widget_show(serv->settings_button); |
| 272 |
gtk_widget_get_preferred_width(serv->settings_button, &width, NULL); |
| 273 |
gtk_widget_hide(serv->settings_button); |
| 274 |
|
| 275 |
gtk_style_context_add_class(context, "cm-wireless-hidden"); |
| 276 |
style_set_margin_end(item->signal, MARGIN_SMALL + width); |
| 277 |
gtk_widget_hide(serv->settings_button); |
| 278 |
|
| 279 |
g_free(name); |
| 280 |
} |