~ubuntu-branches/ubuntu/trusty/unity-control-center/trusty

« back to all changes in this revision

Viewing changes to panels/network/panel-common.c

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2014-01-08 16:29:18 UTC
  • Revision ID: package-import@ubuntu.com-20140108162918-g29dd08tr913y2qh
Tags: upstream-14.04.0
ImportĀ upstreamĀ versionĀ 14.04.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright (C) 2010 Richard Hughes <richard@hughsie.com>
 
4
 * Copyright (C) 2012 Thomas Bechtold <thomasbechtold@jpberlin.de>
 
5
 *
 
6
 * Licensed under the GNU General Public License Version 2
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or modify
 
9
 * it under the terms of the GNU General Public License as published by
 
10
 * the Free Software Foundation; either version 2 of the License, or
 
11
 * (at your option) any later version.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program; if not, write to the Free Software
 
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
21
 */
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#include <glib.h>
 
26
#include <glib/gi18n.h>
 
27
#include <gtk/gtk.h>
 
28
 
 
29
#include <nm-device-ethernet.h>
 
30
#include <nm-device-modem.h>
 
31
#include <nm-utils.h>
 
32
 
 
33
#include "panel-common.h"
 
34
 
 
35
/**
 
36
 * panel_device_to_icon_name:
 
37
 **/
 
38
const gchar *
 
39
panel_device_to_icon_name (NMDevice *device)
 
40
{
 
41
        const gchar *value = NULL;
 
42
        NMDeviceState state;
 
43
        NMDeviceModemCapabilities caps;
 
44
        switch (nm_device_get_device_type (device)) {
 
45
        case NM_DEVICE_TYPE_ETHERNET:
 
46
                state = nm_device_get_state (device);
 
47
                if (state == NM_DEVICE_STATE_UNAVAILABLE) {
 
48
                        value = "network-wired-disconnected";
 
49
                } else {
 
50
                        value = "network-wired";
 
51
                }
 
52
                break;
 
53
        case NM_DEVICE_TYPE_WIFI:
 
54
        case NM_DEVICE_TYPE_BT:
 
55
        case NM_DEVICE_TYPE_OLPC_MESH:
 
56
                value = "network-wireless";
 
57
                break;
 
58
        case NM_DEVICE_TYPE_MODEM:
 
59
                caps = nm_device_modem_get_current_capabilities (NM_DEVICE_MODEM (device));
 
60
                if ((caps & NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS) ||
 
61
                    (caps & NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO)) {
 
62
                        value = "network-wireless";
 
63
                }
 
64
                break;
 
65
        default:
 
66
                break;
 
67
        }
 
68
        return value;
 
69
}
 
70
 
 
71
/**
 
72
 * panel_device_to_localized_string:
 
73
 **/
 
74
const gchar *
 
75
panel_device_to_localized_string (NMDevice *device)
 
76
{
 
77
        const gchar *value = NULL;
 
78
        NMDeviceModemCapabilities caps;
 
79
        switch (nm_device_get_device_type (device)) {
 
80
        case NM_DEVICE_TYPE_UNKNOWN:
 
81
                /* TRANSLATORS: device type */
 
82
                value = _("Unknown");
 
83
                break;
 
84
        case NM_DEVICE_TYPE_ETHERNET:
 
85
                /* TRANSLATORS: device type */
 
86
                value = _("Wired");
 
87
                break;
 
88
        case NM_DEVICE_TYPE_WIFI:
 
89
                /* TRANSLATORS: device type */
 
90
                value = _("Wireless");
 
91
                break;
 
92
        case NM_DEVICE_TYPE_MODEM:
 
93
                caps = nm_device_modem_get_current_capabilities (NM_DEVICE_MODEM (device));
 
94
                if ((caps & NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS) ||
 
95
                    (caps & NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO)) {
 
96
                        /* TRANSLATORS: device type */
 
97
                        value = _("Mobile broadband");
 
98
                }
 
99
                break;
 
100
        case NM_DEVICE_TYPE_BT:
 
101
                /* TRANSLATORS: device type */
 
102
                value = _("Bluetooth");
 
103
                break;
 
104
        case NM_DEVICE_TYPE_OLPC_MESH:
 
105
                /* TRANSLATORS: device type */
 
106
                value = _("Mesh");
 
107
                break;
 
108
        default:
 
109
                break;
 
110
        }
 
111
        return value;
 
112
}
 
113
 
 
114
/**
 
115
 * panel_device_to_sortable_string:
 
116
 *
 
117
 * Try to return order of approximate connection speed.
 
118
 * But sort wifi first, since thats the common case.
 
119
 **/
 
120
const gchar *
 
121
panel_device_to_sortable_string (NMDevice *device)
 
122
{
 
123
        const gchar *value = NULL;
 
124
        NMDeviceModemCapabilities caps;
 
125
        switch (nm_device_get_device_type (device)) {
 
126
        case NM_DEVICE_TYPE_ETHERNET:
 
127
                value = "2";
 
128
                break;
 
129
        case NM_DEVICE_TYPE_WIFI:
 
130
                value = "1";
 
131
                break;
 
132
        case NM_DEVICE_TYPE_MODEM:
 
133
                caps = nm_device_modem_get_current_capabilities (NM_DEVICE_MODEM (device));
 
134
                if ((caps & NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS) ||
 
135
                    (caps & NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO)) {
 
136
                        value = "3";
 
137
                }
 
138
                break;
 
139
        case NM_DEVICE_TYPE_BT:
 
140
                value = "4";
 
141
                break;
 
142
        case NM_DEVICE_TYPE_OLPC_MESH:
 
143
                value = "5";
 
144
                break;
 
145
        default:
 
146
                value = "6";
 
147
                break;
 
148
        }
 
149
        return value;
 
150
}
 
151
 
 
152
/**
 
153
 * panel_ap_mode_to_localized_string:
 
154
 **/
 
155
const gchar *
 
156
panel_ap_mode_to_localized_string (NM80211Mode mode)
 
157
{
 
158
        const gchar *value = NULL;
 
159
        switch (mode) {
 
160
        case NM_802_11_MODE_UNKNOWN:
 
161
                /* TRANSLATORS: AP type */
 
162
                value = _("Unknown");
 
163
                break;
 
164
        case NM_802_11_MODE_ADHOC:
 
165
                /* TRANSLATORS: AP type */
 
166
                value = _("Ad-hoc");
 
167
                break;
 
168
        case NM_802_11_MODE_INFRA:
 
169
                /* TRANSLATORS: AP type */
 
170
                value = _("Infrastructure");
 
171
                break;
 
172
        default:
 
173
                break;
 
174
        }
 
175
        return value;
 
176
}
 
177
 
 
178
/**
 
179
 * panel_device_state_to_localized_string:
 
180
 **/
 
181
const gchar *
 
182
panel_device_state_to_localized_string (NMDevice *device)
 
183
{
 
184
        NMDeviceType type;
 
185
        NMDeviceState state;
 
186
 
 
187
        type = nm_device_get_device_type (device);
 
188
        state = nm_device_get_state (device);
 
189
 
 
190
        const gchar *value = NULL;
 
191
        switch (state) {
 
192
        case NM_DEVICE_STATE_UNKNOWN:
 
193
                /* TRANSLATORS: device status */
 
194
                value = _("Status unknown");
 
195
                break;
 
196
        case NM_DEVICE_STATE_UNMANAGED:
 
197
                /* TRANSLATORS: device status */
 
198
                value = _("Unmanaged");
 
199
                break;
 
200
        case NM_DEVICE_STATE_UNAVAILABLE:
 
201
                /* TRANSLATORS: device status */
 
202
                if (nm_device_get_firmware_missing (device))
 
203
                        value = _("Firmware missing");
 
204
                else if (type == NM_DEVICE_TYPE_ETHERNET &&
 
205
                         !nm_device_ethernet_get_carrier (NM_DEVICE_ETHERNET (device)))
 
206
                        value = _("Cable unplugged");
 
207
                else
 
208
                        value = _("Unavailable");
 
209
                break;
 
210
        case NM_DEVICE_STATE_DISCONNECTED:
 
211
                /* TRANSLATORS: device status */
 
212
                value = _("Disconnected");
 
213
                break;
 
214
        case NM_DEVICE_STATE_PREPARE:
 
215
        case NM_DEVICE_STATE_CONFIG:
 
216
        case NM_DEVICE_STATE_IP_CONFIG:
 
217
        case NM_DEVICE_STATE_IP_CHECK:
 
218
                /* TRANSLATORS: device status */
 
219
                value = _("Connecting");
 
220
                break;
 
221
        case NM_DEVICE_STATE_NEED_AUTH:
 
222
                /* TRANSLATORS: device status */
 
223
                value = _("Authentication required");
 
224
                break;
 
225
        case NM_DEVICE_STATE_ACTIVATED:
 
226
                /* TRANSLATORS: device status */
 
227
                value = _("Connected");
 
228
                break;
 
229
        case NM_DEVICE_STATE_DEACTIVATING:
 
230
                /* TRANSLATORS: device status */
 
231
                value = _("Disconnecting");
 
232
                break;
 
233
        case NM_DEVICE_STATE_FAILED:
 
234
                /* TRANSLATORS: device status */
 
235
                value = _("Connection failed");
 
236
                break;
 
237
        default:
 
238
                /* TRANSLATORS: device status */
 
239
                value = _("Status unknown (missing)");
 
240
                break;
 
241
        }
 
242
        return value;
 
243
}
 
244
 
 
245
/**
 
246
 * panel_vpn_state_to_localized_string:
 
247
 **/
 
248
const gchar *
 
249
panel_vpn_state_to_localized_string (NMVPNConnectionState type)
 
250
{
 
251
        const gchar *value = NULL;
 
252
        switch (type) {
 
253
        case NM_DEVICE_STATE_UNKNOWN:
 
254
                /* TRANSLATORS: VPN status */
 
255
                value = _("Status unknown");
 
256
                break;
 
257
        case NM_VPN_CONNECTION_STATE_PREPARE:
 
258
        case NM_VPN_CONNECTION_STATE_CONNECT:
 
259
        case NM_VPN_CONNECTION_STATE_IP_CONFIG_GET:
 
260
                /* TRANSLATORS: VPN status */
 
261
                value = _("Connecting");
 
262
                break;
 
263
        case NM_VPN_CONNECTION_STATE_NEED_AUTH:
 
264
                /* TRANSLATORS: VPN status */
 
265
                value = _("Authentication required");
 
266
                break;
 
267
        case NM_VPN_CONNECTION_STATE_ACTIVATED:
 
268
                /* TRANSLATORS: VPN status */
 
269
                value = _("Connected");
 
270
                break;
 
271
        case NM_VPN_CONNECTION_STATE_FAILED:
 
272
                /* TRANSLATORS: VPN status */
 
273
                value = _("Connection failed");
 
274
                break;
 
275
        case NM_VPN_CONNECTION_STATE_DISCONNECTED:
 
276
                /* TRANSLATORS: VPN status */
 
277
                value = _("Not connected");
 
278
                break;
 
279
        default:
 
280
                /* TRANSLATORS: VPN status */
 
281
                value = _("Status unknown (missing)");
 
282
                break;
 
283
        }
 
284
        return value;
 
285
}
 
286
 
 
287
/**
 
288
 * panel_device_state_reason_to_localized_string:
 
289
 **/
 
290
const gchar *
 
291
panel_device_state_reason_to_localized_string (NMDevice *device)
 
292
{
 
293
        const gchar *value = NULL;
 
294
        NMDeviceStateReason state_reason;
 
295
 
 
296
        /* we only want the StateReason's we care about */
 
297
        nm_device_get_state_reason (device, &state_reason);
 
298
        switch (state_reason) {
 
299
        case NM_DEVICE_STATE_REASON_CONFIG_FAILED:
 
300
                /* TRANSLATORS: device status reason */
 
301
                value = _("Configuration failed");
 
302
                break;
 
303
        case NM_DEVICE_STATE_REASON_IP_CONFIG_UNAVAILABLE:
 
304
                /* TRANSLATORS: device status reason */
 
305
                value = _("IP configuration failed");
 
306
                break;
 
307
        case NM_DEVICE_STATE_REASON_IP_CONFIG_EXPIRED:
 
308
                /* TRANSLATORS: device status reason */
 
309
                value = _("IP configuration expired");
 
310
                break;
 
311
        case NM_DEVICE_STATE_REASON_NO_SECRETS:
 
312
                /* TRANSLATORS: device status reason */
 
313
                value = _("Secrets were required, but not provided");
 
314
                break;
 
315
        case NM_DEVICE_STATE_REASON_SUPPLICANT_DISCONNECT:
 
316
                /* TRANSLATORS: device status reason */
 
317
                value = _("802.1x supplicant disconnected");
 
318
                break;
 
319
        case NM_DEVICE_STATE_REASON_SUPPLICANT_CONFIG_FAILED:
 
320
                /* TRANSLATORS: device status reason */
 
321
                value = _("802.1x supplicant configuration failed");
 
322
                break;
 
323
        case NM_DEVICE_STATE_REASON_SUPPLICANT_FAILED:
 
324
                /* TRANSLATORS: device status reason */
 
325
                value = _("802.1x supplicant failed");
 
326
                break;
 
327
        case NM_DEVICE_STATE_REASON_SUPPLICANT_TIMEOUT:
 
328
                /* TRANSLATORS: device status reason */
 
329
                value = _("802.1x supplicant took too long to authenticate");
 
330
                break;
 
331
        case NM_DEVICE_STATE_REASON_PPP_START_FAILED:
 
332
                /* TRANSLATORS: device status reason */
 
333
                value = _("PPP service failed to start");
 
334
                break;
 
335
        case NM_DEVICE_STATE_REASON_PPP_DISCONNECT:
 
336
                /* TRANSLATORS: device status reason */
 
337
                value = _("PPP service disconnected");
 
338
                break;
 
339
        case NM_DEVICE_STATE_REASON_PPP_FAILED:
 
340
                /* TRANSLATORS: device status reason */
 
341
                value = _("PPP failed");
 
342
                break;
 
343
        case NM_DEVICE_STATE_REASON_DHCP_START_FAILED:
 
344
                /* TRANSLATORS: device status reason */
 
345
                value = _("DHCP client failed to start");
 
346
                break;
 
347
        case NM_DEVICE_STATE_REASON_DHCP_ERROR:
 
348
                /* TRANSLATORS: device status reason */
 
349
                value = _("DHCP client error");
 
350
                break;
 
351
        case NM_DEVICE_STATE_REASON_DHCP_FAILED:
 
352
                /* TRANSLATORS: device status reason */
 
353
                value = _("DHCP client failed");
 
354
                break;
 
355
        case NM_DEVICE_STATE_REASON_SHARED_START_FAILED:
 
356
                /* TRANSLATORS: device status reason */
 
357
                value = _("Shared connection service failed to start");
 
358
                break;
 
359
        case NM_DEVICE_STATE_REASON_SHARED_FAILED:
 
360
                /* TRANSLATORS: device status reason */
 
361
                value = _("Shared connection service failed");
 
362
                break;
 
363
        case NM_DEVICE_STATE_REASON_AUTOIP_START_FAILED:
 
364
                /* TRANSLATORS: device status reason */
 
365
                value = _("AutoIP service failed to start");
 
366
                break;
 
367
        case NM_DEVICE_STATE_REASON_AUTOIP_ERROR:
 
368
                /* TRANSLATORS: device status reason */
 
369
                value = _("AutoIP service error");
 
370
                break;
 
371
        case NM_DEVICE_STATE_REASON_AUTOIP_FAILED:
 
372
                /* TRANSLATORS: device status reason */
 
373
                value = _("AutoIP service failed");
 
374
                break;
 
375
        case NM_DEVICE_STATE_REASON_MODEM_BUSY:
 
376
                /* TRANSLATORS: device status reason */
 
377
                value = _("Line busy");
 
378
                break;
 
379
        case NM_DEVICE_STATE_REASON_MODEM_NO_DIAL_TONE:
 
380
                /* TRANSLATORS: device status reason */
 
381
                value = _("No dial tone");
 
382
                break;
 
383
        case NM_DEVICE_STATE_REASON_MODEM_NO_CARRIER:
 
384
                /* TRANSLATORS: device status reason */
 
385
                value = _("No carrier could be established");
 
386
                break;
 
387
        case NM_DEVICE_STATE_REASON_MODEM_DIAL_TIMEOUT:
 
388
                /* TRANSLATORS: device status reason */
 
389
                value = _("Dialing request timed out");
 
390
                break;
 
391
        case NM_DEVICE_STATE_REASON_MODEM_DIAL_FAILED:
 
392
                /* TRANSLATORS: device status reason */
 
393
                value = _("Dialing attempt failed");
 
394
                break;
 
395
        case NM_DEVICE_STATE_REASON_MODEM_INIT_FAILED:
 
396
                /* TRANSLATORS: device status reason */
 
397
                value = _("Modem initialization failed");
 
398
                break;
 
399
        case NM_DEVICE_STATE_REASON_GSM_APN_FAILED:
 
400
                /* TRANSLATORS: device status reason */
 
401
                value = _("Failed to select the specified APN");
 
402
                break;
 
403
        case NM_DEVICE_STATE_REASON_GSM_REGISTRATION_NOT_SEARCHING:
 
404
                /* TRANSLATORS: device status reason */
 
405
                value = _("Not searching for networks");
 
406
                break;
 
407
        case NM_DEVICE_STATE_REASON_GSM_REGISTRATION_DENIED:
 
408
                /* TRANSLATORS: device status reason */
 
409
                value = _("Network registration denied");
 
410
                break;
 
411
        case NM_DEVICE_STATE_REASON_GSM_REGISTRATION_TIMEOUT:
 
412
                /* TRANSLATORS: device status reason */
 
413
                value = _("Network registration timed out");
 
414
                break;
 
415
        case NM_DEVICE_STATE_REASON_GSM_REGISTRATION_FAILED:
 
416
                /* TRANSLATORS: device status reason */
 
417
                value = _("Failed to register with the requested network");
 
418
                break;
 
419
        case NM_DEVICE_STATE_REASON_GSM_PIN_CHECK_FAILED:
 
420
                /* TRANSLATORS: device status reason */
 
421
                value = _("PIN check failed");
 
422
                break;
 
423
        case NM_DEVICE_STATE_REASON_FIRMWARE_MISSING:
 
424
                /* TRANSLATORS: device status reason */
 
425
                value = _("Firmware for the device may be missing");
 
426
                break;
 
427
        case NM_DEVICE_STATE_REASON_CONNECTION_REMOVED:
 
428
                /* TRANSLATORS: device status reason */
 
429
                value = _("Connection disappeared");
 
430
                break;
 
431
        case NM_DEVICE_STATE_REASON_CARRIER:
 
432
                /* TRANSLATORS: device status reason */
 
433
                value = _("Carrier/link changed");
 
434
                break;
 
435
        case NM_DEVICE_STATE_REASON_CONNECTION_ASSUMED:
 
436
                /* TRANSLATORS: device status reason */
 
437
                value = _("Existing connection was assumed");
 
438
                break;
 
439
        case NM_DEVICE_STATE_REASON_MODEM_NOT_FOUND:
 
440
                /* TRANSLATORS: device status reason */
 
441
                value = _("Modem not found");
 
442
                break;
 
443
        case NM_DEVICE_STATE_REASON_BT_FAILED:
 
444
                /* TRANSLATORS: device status reason */
 
445
                value = _("Bluetooth connection failed");
 
446
                break;
 
447
        case NM_DEVICE_STATE_REASON_GSM_SIM_NOT_INSERTED:
 
448
                /* TRANSLATORS: device status reason */
 
449
                value = _("SIM Card not inserted");
 
450
                break;
 
451
        case NM_DEVICE_STATE_REASON_GSM_SIM_PIN_REQUIRED:
 
452
                /* TRANSLATORS: device status reason */
 
453
                value = _("SIM Pin required");
 
454
                break;
 
455
        case NM_DEVICE_STATE_REASON_GSM_SIM_PUK_REQUIRED:
 
456
                /* TRANSLATORS: device status reason */
 
457
                value = _("SIM Puk required");
 
458
                break;
 
459
        case NM_DEVICE_STATE_REASON_GSM_SIM_WRONG:
 
460
                /* TRANSLATORS: device status reason */
 
461
                value = _("SIM wrong");
 
462
                break;
 
463
        case NM_DEVICE_STATE_REASON_INFINIBAND_MODE:
 
464
                /* TRANSLATORS: device status reason */
 
465
                value = _("InfiniBand device does not support connected mode");
 
466
                break;
 
467
        case NM_DEVICE_STATE_REASON_DEPENDENCY_FAILED:
 
468
                /* TRANSLATORS: device status reason */
 
469
                value = _("Connection dependency failed");
 
470
                break;
 
471
        default:
 
472
                /* no StateReason to show */
 
473
                value = "";
 
474
                break;
 
475
        }
 
476
        return value;
 
477
}
 
478
 
 
479
gboolean
 
480
panel_set_device_widget_details (GtkBuilder *builder,
 
481
                                 const gchar *widget_suffix,
 
482
                                 const gchar *value)
 
483
{
 
484
        gchar *heading_id;
 
485
        gchar *label_id;
 
486
        GtkWidget *heading;
 
487
        GtkWidget *widget;
 
488
 
 
489
        /* hide the row if there is no value */
 
490
        heading_id = g_strdup_printf ("heading_%s", widget_suffix);
 
491
        label_id = g_strdup_printf ("label_%s", widget_suffix);
 
492
        heading = GTK_WIDGET (gtk_builder_get_object (builder, heading_id));
 
493
        widget = GTK_WIDGET (gtk_builder_get_object (builder, label_id));
 
494
        if (heading == NULL || widget == NULL) {
 
495
                g_critical ("no widgets %s, %s found", heading_id, label_id);
 
496
                return FALSE;
 
497
        }
 
498
        g_free (heading_id);
 
499
        g_free (label_id);
 
500
 
 
501
        if (value == NULL) {
 
502
                gtk_widget_hide (heading);
 
503
                gtk_widget_hide (widget);
 
504
        } else {
 
505
                /* there exists a value */
 
506
                gtk_widget_show (heading);
 
507
                gtk_widget_show (widget);
 
508
                gtk_label_set_label (GTK_LABEL (widget), value);
 
509
        }
 
510
        return TRUE;
 
511
}
 
512
 
 
513
 
 
514
gboolean
 
515
panel_set_device_widget_header (GtkBuilder *builder,
 
516
                                const gchar *widget_suffix,
 
517
                                const gchar *heading)
 
518
{
 
519
        gchar *label_id = NULL;
 
520
        GtkWidget *widget;
 
521
 
 
522
        label_id = g_strdup_printf ("heading_%s", widget_suffix);
 
523
        widget = GTK_WIDGET (gtk_builder_get_object (builder, label_id));
 
524
        if (widget == NULL) {
 
525
                g_critical ("no widget %s found", label_id);
 
526
                return FALSE;
 
527
        }
 
528
        gtk_label_set_label (GTK_LABEL (widget), heading);
 
529
        g_free (label_id);
 
530
        return TRUE;
 
531
}
 
532
 
 
533
static gchar *
 
534
get_ipv4_config_address_as_string (NMIP4Config *ip4_config, const char *what)
 
535
{
 
536
        const GSList *list;
 
537
        struct in_addr addr;
 
538
        gchar *str = NULL;
 
539
        gchar tmp[INET_ADDRSTRLEN];
 
540
        NMIP4Address *address;
 
541
 
 
542
        /* get address */
 
543
        list = nm_ip4_config_get_addresses (ip4_config);
 
544
        if (list == NULL)
 
545
                goto out;
 
546
 
 
547
        /* we only care about one address */
 
548
        address = list->data;
 
549
        if (!strcmp (what, "address"))
 
550
                addr.s_addr = nm_ip4_address_get_address (address);
 
551
        else if (!strcmp (what, "gateway"))
 
552
                addr.s_addr = nm_ip4_address_get_gateway (address);
 
553
        else if (!strcmp (what, "netmask"))
 
554
                addr.s_addr = nm_utils_ip4_prefix_to_netmask (nm_ip4_address_get_prefix (address));
 
555
        else
 
556
                goto out;
 
557
 
 
558
        if (!inet_ntop (AF_INET, &addr, tmp, sizeof(tmp)))
 
559
                goto out;
 
560
        if (g_strcmp0 (tmp, "0.0.0.0") == 0)
 
561
                goto out;
 
562
        str = g_strdup (tmp);
 
563
out:
 
564
        return str;
 
565
}
 
566
 
 
567
static gchar *
 
568
get_ipv4_config_name_servers_as_string (NMIP4Config *ip4_config)
 
569
{
 
570
        const GArray *array;
 
571
        GString *dns;
 
572
        struct in_addr addr;
 
573
        gchar tmp[INET_ADDRSTRLEN];
 
574
        int i;
 
575
        gchar *str = NULL;
 
576
 
 
577
        array = nm_ip4_config_get_nameservers (ip4_config);
 
578
        if (array == NULL || array->len == 0)
 
579
                goto out;
 
580
 
 
581
        dns = g_string_new (NULL);
 
582
        for (i = 0; i < array->len; i++) {
 
583
                addr.s_addr = g_array_index (array, guint32, i);
 
584
                if (inet_ntop (AF_INET, &addr, tmp, sizeof(tmp)))
 
585
                        g_string_append_printf (dns, "%s ", tmp);
 
586
        }
 
587
        str = g_string_free (dns, FALSE);
 
588
out:
 
589
        return str;
 
590
}
 
591
 
 
592
static gchar *
 
593
get_ipv6_config_address_as_string (NMIP6Config *ip6_config)
 
594
{
 
595
        const GSList *list;
 
596
        const struct in6_addr *addr;
 
597
        gchar *str = NULL;
 
598
        gchar tmp[INET6_ADDRSTRLEN];
 
599
        NMIP6Address *address;
 
600
 
 
601
        /* get address */
 
602
        list = nm_ip6_config_get_addresses (ip6_config);
 
603
        if (list == NULL)
 
604
                goto out;
 
605
 
 
606
        /* we only care about one address */
 
607
        address = list->data;
 
608
        addr = nm_ip6_address_get_address (address);
 
609
        if (addr == NULL)
 
610
                goto out;
 
611
        inet_ntop (AF_INET6, addr, tmp, sizeof(tmp));
 
612
        str = g_strdup (tmp);
 
613
out:
 
614
        return str;
 
615
}
 
616
 
 
617
void
 
618
panel_set_device_widgets (GtkBuilder *builder, NMDevice *device)
 
619
{
 
620
        NMIP4Config *ip4_config = NULL;
 
621
        NMIP6Config *ip6_config = NULL;
 
622
        gboolean has_ip4;
 
623
        gboolean has_ip6;
 
624
        gchar *str_tmp;
 
625
 
 
626
        /* get IPv4 parameters */
 
627
        ip4_config = nm_device_get_ip4_config (device);
 
628
        if (ip4_config != NULL) {
 
629
 
 
630
                /* IPv4 address */
 
631
                str_tmp = get_ipv4_config_address_as_string (ip4_config, "address");
 
632
                panel_set_device_widget_details (builder,
 
633
                                                 "ipv4",
 
634
                                                 str_tmp);
 
635
                has_ip4 = str_tmp != NULL;
 
636
                g_free (str_tmp);
 
637
 
 
638
                /* IPv4 DNS */
 
639
                str_tmp = get_ipv4_config_name_servers_as_string (ip4_config);
 
640
                panel_set_device_widget_details (builder,
 
641
                                                 "dns",
 
642
                                                 str_tmp);
 
643
                g_free (str_tmp);
 
644
 
 
645
                /* IPv4 route */
 
646
                str_tmp = get_ipv4_config_address_as_string (ip4_config, "gateway");
 
647
                panel_set_device_widget_details (builder,
 
648
                                                 "route",
 
649
                                                 str_tmp);
 
650
                g_free (str_tmp);
 
651
 
 
652
        } else {
 
653
                /* IPv4 address */
 
654
                panel_set_device_widget_details (builder,
 
655
                                                 "ipv4",
 
656
                                                 NULL);
 
657
                has_ip4 = FALSE;
 
658
 
 
659
                /* IPv4 DNS */
 
660
                panel_set_device_widget_details (builder,
 
661
                                                 "dns",
 
662
                                                 NULL);
 
663
 
 
664
                /* IPv4 route */
 
665
                panel_set_device_widget_details (builder,
 
666
                                                 "route",
 
667
                                                 NULL);
 
668
        }
 
669
 
 
670
        /* get IPv6 parameters */
 
671
        ip6_config = nm_device_get_ip6_config (device);
 
672
        if (ip6_config != NULL) {
 
673
                str_tmp = get_ipv6_config_address_as_string (ip6_config);
 
674
                panel_set_device_widget_details (builder, "ipv6", str_tmp);
 
675
                has_ip6 = str_tmp != NULL;
 
676
                g_free (str_tmp);
 
677
        } else {
 
678
                panel_set_device_widget_details (builder, "ipv6", NULL);
 
679
                has_ip6 = FALSE;
 
680
        }
 
681
 
 
682
        if (has_ip4 && has_ip6) {
 
683
                panel_set_device_widget_header (builder, "ipv4", _("IPv4 Address"));
 
684
                panel_set_device_widget_header (builder, "ipv6", _("IPv6 Address"));
 
685
        } else if (has_ip4) {
 
686
                panel_set_device_widget_header (builder, "ipv4", _("IP Address"));
 
687
        } else if (has_ip6) {
 
688
                panel_set_device_widget_header (builder, "ipv6", _("IP Address"));
 
689
        }
 
690
}
 
691
 
 
692
void
 
693
panel_unset_device_widgets (GtkBuilder *builder)
 
694
{
 
695
        panel_set_device_widget_details (builder, "ipv4", NULL);
 
696
        panel_set_device_widget_details (builder, "ipv6", NULL);
 
697
        panel_set_device_widget_details (builder, "dns", NULL);
 
698
        panel_set_device_widget_details (builder, "route", NULL);
 
699
}