Debian Codegraph @

src/util.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 <arpa/inet.h>
22 #include <glib.h>
23 #include <glib/gi18n.h>
24 #include <gtk/gtk.h>
25 #include <string.h>
26 #include <gdk/gdkkeysyms.h>
27
28 #include "config.h"
29 #include "main.h"
30 #include "style.h"
31 #include "util.h"
32
33 gchar *variant_str_array_to_str(GVariant *variant)
34 {
35 GString *str;
36 const gchar **arr, **iter;
37 gchar *out;
38 arr = g_variant_get_strv(variant, NULL);
39 str = g_string_new(arr[0]);
40 iter = arr + 1;
41 while(*arr && *iter)
42 g_string_append_printf(str, ", %s", *iter++);
43 g_free(arr);
44 out = str->str;
45 g_string_free(str, FALSE);
46 return out;
47 }
48
49 gchar **variant_to_strv(GVariant *variant)
50 {
51 if(!variant)
52 return g_malloc(sizeof(gchar *));
53 return g_variant_dup_strv(variant, NULL);
54 }
55
56 gchar *variant_to_str(GVariant *variant)
57 {
58 if(!variant)
59 return g_strdup("");
60 if(!strcmp(g_variant_get_type_string(variant), "as"))
61 return variant_str_array_to_str(variant);
62 else
63 return g_variant_dup_string(variant, NULL);
64 }
65
66 gboolean variant_to_bool(GVariant *variant)
67 {
68 if(!variant)
69 return FALSE;
70 return g_variant_get_boolean(variant);
71 }
72
73 guint64 variant_to_uint(GVariant *variant)
74 {
75 if(!variant)
76 return 0;
77 if(g_variant_is_of_type(variant, G_VARIANT_TYPE_BYTE))
78 return g_variant_get_byte(variant);
79 if(g_variant_is_of_type(variant, G_VARIANT_TYPE_UINT16))
80 return g_variant_get_uint16(variant);
81 if(g_variant_is_of_type(variant, G_VARIANT_TYPE_UINT32))
82 return g_variant_get_uint32(variant);
83 if(g_variant_is_of_type(variant, G_VARIANT_TYPE_UINT64))
84 return g_variant_get_uint64(variant);
85 g_warning("Converting signed type to unsigned");
86 if(g_variant_is_of_type(variant, G_VARIANT_TYPE_INT16))
87 return g_variant_get_int16(variant);
88 if(g_variant_is_of_type(variant, G_VARIANT_TYPE_INT32))
89 return g_variant_get_int32(variant);
90 if(g_variant_is_of_type(variant, G_VARIANT_TYPE_INT64))
91 return g_variant_get_int64(variant);
92 return 0;
93 }
94
95 gint64 variant_to_int(GVariant *variant)
96 {
97 if(!variant)
98 return 0;
99 if(g_variant_is_of_type(variant, G_VARIANT_TYPE_BYTE))
100 return g_variant_get_byte(variant);
101 if(g_variant_is_of_type(variant, G_VARIANT_TYPE_INT16))
102 return g_variant_get_int16(variant);
103 if(g_variant_is_of_type(variant, G_VARIANT_TYPE_INT32))
104 return g_variant_get_int32(variant);
105 if(g_variant_is_of_type(variant, G_VARIANT_TYPE_INT64))
106 return g_variant_get_int64(variant);
107 if(g_variant_is_of_type(variant, G_VARIANT_TYPE_UINT16))
108 return g_variant_get_uint16(variant);
109 if(g_variant_is_of_type(variant, G_VARIANT_TYPE_UINT32))
110 return g_variant_get_uint32(variant);
111 if(g_variant_is_of_type(variant, G_VARIANT_TYPE_UINT64)) {
112 g_warning("Converting uint64 to int64");
113 return g_variant_get_uint64(variant);
114 }
115 return 0;
116 }
117
118 const gchar *status_localized(const gchar *state)
119 {
120 if(!strcmp(state, "idle"))
121 return _("Idle");
122 else if(!strcmp(state, "failure"))
123 return _("Failure");
124 else if(!strcmp(state, "association"))
125 return _("Association");
126 else if(!strcmp(state, "configuration"))
127 return _("Configuration");
128 else if(!strcmp(state, "ready"))
129 return _("Ready");
130 else if(!strcmp(state, "disconnect"))
131 return _("Disconnected");
132 else if(!strcmp(state, "online"))
133 return _("Online");
134 else
135 return _("Error");
136 }
137
138 const gchar *failure_localized(const gchar *state)
139 {
140 if(!strcmp(state, "out-of-range"))
141 return _("Out of range");
142 else if(!strcmp(state, "pin-missing"))
143 return _("PIN missing");
144 else if(!strcmp(state, "dhcp-failed"))
145 return _("DHCP failed");
146 else if(!strcmp(state, "connect-failed"))
147 return _("Connecting failed");
148 else if(!strcmp(state, "login-failed"))
149 return _("Login failed");
150 else if(!strcmp(state, "auth-failed"))
151 return _("Authentication failed");
152 else if(!strcmp(state, "invalid-key"))
153 return _("Invalid key");
154 else
155 return state;
156 }
157
158 gboolean valid_ipv4(const gchar *address)
159 {
160 char str[INET_ADDRSTRLEN];
161 return inet_pton(AF_INET, address, str) == 1;
162 }
163
164 gboolean valid_ipv6(const gchar *address)
165 {
166 char str[INET6_ADDRSTRLEN];
167 return inet_pton(AF_INET6, address, str) == 1;
168 }
169
170 static void item_selected(GtkWidget *notebook, GtkWidget *content)
171 {
172 gint num = gtk_notebook_page_num(GTK_NOTEBOOK(notebook), content);
173 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), num);
174 g_object_set_data(G_OBJECT(notebook), "selected", content);
175 g_object_set_data(G_OBJECT(content), "selected", content);
176 }
177
178 void list_item_selected(GtkListBox *box, GtkListBoxRow *row,
179 gpointer data)
180 {
181 if(!G_IS_OBJECT(row))
182 return;
183 GtkWidget *notebook = data;
184 GtkWidget *content = g_object_get_data(G_OBJECT(row), "content");
185 item_selected(notebook, content);
186 }
187
188 void combo_box_changed(GtkComboBox *box, gpointer data)
189 {
190 GtkWidget *notebook = data;
191 GtkWidget *content;
192 content = g_object_get_data(G_OBJECT(notebook), "selected");
193 if(content)
194 g_object_set_data(G_OBJECT(content), "selected", NULL);
195 GHashTable *table = g_object_get_data(G_OBJECT(box), "items");
196 GtkComboBoxText *b = GTK_COMBO_BOX_TEXT(box);
197 gchar *str = gtk_combo_box_text_get_active_text(b);
198 content = g_hash_table_lookup(table, str);
199 item_selected(notebook, content);
200 g_free(str);
201 }
202
203 struct DualHashTable_t {
204 GHashTable *table;
205 GDestroyNotify free_value;
206 gint refcount;
207 };
208
209 DualHashTable *dual_hash_table_new(GDestroyNotify free_value)
210 {
211 DualHashTable *dt = g_malloc(sizeof(*dt));
212 GDestroyNotify free_func = (GDestroyNotify)g_hash_table_unref;
213 GHashTable *table = g_hash_table_new_full(g_str_hash, g_str_equal,
214 g_free, free_func);
215 dt->table = table;
216 dt->free_value = free_value;
217 dt->refcount = 1;
218 return dt;
219 }
220
221 DualHashTable *dual_hash_table_ref(DualHashTable *table)
222 {
223 g_atomic_int_inc(&table->refcount);
224 g_hash_table_ref(table->table);
225 return table;
226 }
227
228 void dual_hash_table_unref(DualHashTable *table)
229 {
230 g_hash_table_unref(table->table);
231 if(g_atomic_int_dec_and_test(&table->refcount))
232 g_free(table);
233 }
234
235 void *hash_table_get_dual_key(DualHashTable *dtable, const gchar *key,
236 const gchar *subkey)
237 {
238 GHashTable *table = dtable->table;
239 GHashTable *t = g_hash_table_lookup(table, key);
240 if(!t)
241 return NULL;
242 if(!subkey)
243 subkey = "";
244 return g_hash_table_lookup(t, subkey);
245 }
246
247 void hash_table_set_dual_key(DualHashTable *dtable, const gchar *key,
248 const gchar *subkey, void *value)
249 {
250 GHashTable *table = dtable->table;
251 GHashTable *t;
252 t = g_hash_table_lookup(table, key);
253 if(!t) {
254 t = g_hash_table_new_full(g_str_hash, g_str_equal,
255 g_free, dtable->free_value);
256 g_hash_table_insert(table, g_strdup(key), t);
257 }
258 if(!subkey)
259 subkey = "";
260 g_hash_table_insert(t, g_strdup(subkey), value);
261 }
262
263 struct table_cb_data {
264 DualHashTableIter cb;
265 gpointer user_data;
266 const gchar *key;
267 };
268
269 static void iter_cb_cb(gpointer subkey, gpointer value, gpointer user_data)
270 {
271 struct table_cb_data *data = user_data;
272 if(!strcmp(subkey, ""))
273 subkey = NULL;
274 data->cb(data->key, subkey, value, data->user_data);
275 }
276
277 static void iter_cb(gpointer key, gpointer value, gpointer user_data)
278 {
279 struct table_cb_data *data = user_data;
280 data->key = key;
281 g_hash_table_foreach(value, iter_cb_cb, data);
282 }
283
284 void dual_hash_table_foreach(DualHashTable *table, DualHashTableIter cb,
285 gpointer user_data)
286 {
287 struct table_cb_data data = {cb, user_data};
288 g_hash_table_foreach(table->table, iter_cb, &data);
289 }
290
291 static void append_to_variant(const gchar *key, const gchar *subkey,
292 gpointer value, gpointer user_data)
293 {
294 GVariant *inner_v;
295 GVariantDict *inner;
296 if(!subkey) {
297 g_variant_dict_insert_value(user_data, key, value);
298 return;
299 }
300 inner_v = g_variant_dict_lookup_value(user_data, key, NULL);
301 if(!inner_v)
302 inner = g_variant_dict_new(NULL);
303 else
304 inner = g_variant_dict_new(inner_v);
305 g_variant_dict_insert_value(inner, subkey, value);
306 if(inner_v)
307 g_variant_unref(inner_v);
308 inner_v = g_variant_dict_end(inner);
309 g_variant_dict_insert_value(user_data, key, inner_v);
310 g_variant_dict_unref(inner);
311 }
312
313 GVariant *dual_hash_table_to_variant(DualHashTable *dtable)
314 {
315 GVariantDict *dict = g_variant_dict_new(NULL);
316 dual_hash_table_foreach(dtable, append_to_variant, dict);
317 GVariant *out = g_variant_dict_end(dict);
318 g_variant_dict_unref(dict);
319 return out;
320 }
321
322 gboolean handle_keyboard_shortcut(GtkWidget *widget, GdkEventKey *event, gpointer data) {
323 int isCtrl = event->state & GDK_CONTROL_MASK;
324
325 switch (event->keyval) {
326 case GDK_KEY_w:
327 if (isCtrl)
328 gtk_widget_destroy(widget);
329 break;
330 }
331
332 return FALSE;
333 }