~khurshid-alam/unity-control-center/use-usd-schemas

« back to all changes in this revision

Viewing changes to panels/network/net-device-mobile.c

  • Committer: Sebastien Bacher
  • Author(s): Khurshid Alam
  • Date: 2019-05-17 07:34:33 UTC
  • mfrom: (12920.1.1 unity-control-center)
  • Revision ID: seb128@ubuntu.com-20190517073433-ch2kybdhhzpkmvq4
Network: Port to libnm 1.2 (lp: #1744619)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
2
2
 *
3
3
 * Copyright (C) 2011-2012 Richard Hughes <richard@hughsie.com>
 
4
 * Copyright (C) 2013 Aleksander Morgado <aleksander@gnu.org>
4
5
 *
5
6
 * Licensed under the GNU General Public License Version 2
6
7
 *
24
25
#include <glib-object.h>
25
26
#include <glib/gi18n.h>
26
27
 
27
 
#include <nm-client.h>
28
 
#include <nm-device.h>
29
 
#include <nm-device-modem.h>
30
 
#include <nm-remote-connection.h>
 
28
#include <NetworkManager.h>
 
29
#include <libmm-glib.h>
 
30
#include <nma-mobile-providers.h>
31
31
 
32
32
#include "panel-common.h"
33
33
#include "network-dialogs.h"
34
 
 
35
34
#include "net-device-mobile.h"
36
35
 
37
36
#define NET_DEVICE_MOBILE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NET_TYPE_DEVICE_MOBILE, NetDeviceMobilePrivate))
40
39
 
41
40
struct _NetDeviceMobilePrivate
42
41
{
43
 
        GtkBuilder              *builder;
44
 
        gboolean                 updating_device;
 
42
        GtkBuilder *builder;
 
43
        gboolean    updating_device;
 
44
 
 
45
        /* Old MM < 0.7 support */
 
46
        GDBusProxy *gsm_proxy;
 
47
        GDBusProxy *cdma_proxy;
 
48
 
 
49
        /* New MM >= 0.7 support */
 
50
        MMObject   *mm_object;
 
51
        guint       operator_name_updated;
 
52
 
 
53
        NMAMobileProvidersDatabase *mpd;
45
54
};
46
55
 
47
56
enum {
50
59
        COLUMN_LAST
51
60
};
52
61
 
 
62
enum {
 
63
        PROP_0,
 
64
        PROP_MODEM_OBJECT,
 
65
        PROP_LAST
 
66
};
 
67
 
53
68
G_DEFINE_TYPE (NetDeviceMobile, net_device_mobile, NET_TYPE_DEVICE)
54
69
 
55
70
static GtkWidget *
58
73
                                     GtkSizeGroup *heading_size_group)
59
74
{
60
75
        GtkWidget *widget;
61
 
        GtkWindow *window;
62
76
        NetDeviceMobile *device_mobile = NET_DEVICE_MOBILE (object);
63
77
 
64
78
        /* add widgets to size group */
69
83
                                                     "heading_network"));
70
84
        gtk_size_group_add_widget (heading_size_group, widget);
71
85
 
72
 
        /* reparent */
73
 
        window = GTK_WINDOW (gtk_builder_get_object (device_mobile->priv->builder,
74
 
                                                     "window_tmp"));
75
86
        widget = GTK_WIDGET (gtk_builder_get_object (device_mobile->priv->builder,
76
87
                                                     "vbox7"));
77
 
        g_object_ref (widget);
78
 
        gtk_container_remove (GTK_CONTAINER (window), widget);
79
88
        gtk_notebook_append_page (notebook, widget, NULL);
80
 
        g_object_unref (widget);
81
89
        return widget;
82
90
}
83
91
 
84
92
static void
85
 
connection_activate_cb (NMClient *client,
86
 
                        NMActiveConnection *connection,
87
 
                        GError *error,
 
93
connection_activate_cb (GObject *source_object,
 
94
                        GAsyncResult *res,
88
95
                        gpointer user_data)
89
96
{
90
 
        NetDeviceMobile *device_mobile = NET_DEVICE_MOBILE (user_data);
 
97
        GError *error = NULL;
91
98
 
92
 
        if (connection == NULL) {
 
99
        if (!nm_client_activate_connection_finish (NM_CLIENT (source_object), res, &error)) {
93
100
                /* failed to activate */
94
 
                nm_device_mobile_refresh_ui (device_mobile);
 
101
                nm_device_mobile_refresh_ui (user_data);
 
102
                g_error_free (error);
95
103
        }
96
104
}
97
105
 
105
113
        NMConnection *connection;
106
114
        NMDevice *device;
107
115
        NMClient *client;
108
 
        NMRemoteSettings *remote_settings;
109
116
        CcNetworkPanel *panel;
 
117
        GtkWidget *toplevel;
110
118
 
111
119
        if (device_mobile->priv->updating_device)
112
120
                goto out;
119
127
        if (device == NULL)
120
128
                goto out;
121
129
        client = net_object_get_client (NET_OBJECT (device_mobile));
122
 
        remote_settings = net_object_get_remote_settings (NET_OBJECT (device_mobile));
123
130
 
124
131
        /* get entry */
125
132
        model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
128
135
                            -1);
129
136
        if (g_strcmp0 (object_path, NULL) == 0) {
130
137
                panel = net_object_get_panel (NET_OBJECT (device_mobile));
131
 
                cc_network_panel_connect_to_3g_network (panel,
 
138
                toplevel = cc_shell_get_toplevel (cc_panel_get_shell (CC_PANEL (panel)));
 
139
                cc_network_panel_connect_to_3g_network (toplevel,
132
140
                                                        client,
133
 
                                                        remote_settings,
134
141
                                                        device);
135
142
                goto out;
136
143
        }
137
144
 
138
145
        /* activate the connection */
139
146
        g_debug ("try to switch to connection %s", object_path);
140
 
        connection = (NMConnection*) nm_remote_settings_get_connection_by_path (remote_settings,
141
 
                                                                                object_path);
 
147
        connection = (NMConnection*) nm_client_get_connection_by_path (client, object_path);
142
148
        if (connection != NULL) {
143
149
                nm_device_disconnect (device, NULL, NULL);
144
 
                nm_client_activate_connection (client,
145
 
                                               connection,
146
 
                                               device, NULL,
147
 
                                               connection_activate_cb,
148
 
                                               device_mobile);
 
150
                nm_client_activate_connection_async (client,
 
151
                                                     connection,
 
152
                                                     device, NULL, NULL,
 
153
                                                     connection_activate_cb,
 
154
                                                     device_mobile);
149
155
                goto out;
150
156
        }
151
157
out:
165
171
        if (nm_device_get_device_type (device) != NM_DEVICE_TYPE_MODEM)
166
172
                return;
167
173
 
168
 
        enabled = nm_client_wwan_get_enabled (client);
 
174
        if (nm_client_wwan_get_enabled (client)) {
 
175
                NMDeviceState state;
 
176
 
 
177
                state = nm_device_get_state (device);
 
178
                if (state == NM_DEVICE_STATE_UNKNOWN ||
 
179
                    state == NM_DEVICE_STATE_UNMANAGED ||
 
180
                    state == NM_DEVICE_STATE_UNAVAILABLE ||
 
181
                    state == NM_DEVICE_STATE_DISCONNECTED ||
 
182
                    state == NM_DEVICE_STATE_DEACTIVATING ||
 
183
                    state == NM_DEVICE_STATE_FAILED) {
 
184
                        enabled = FALSE;
 
185
                } else {
 
186
                        enabled = TRUE;
 
187
                }
 
188
        }
 
189
 
169
190
        sw = GTK_SWITCH (gtk_builder_get_object (device_mobile->priv->builder,
170
191
                                                 "device_off_switch"));
171
192
 
181
202
                               GtkComboBox *combobox)
182
203
{
183
204
        NetDeviceMobilePrivate *priv = device_mobile->priv;
184
 
        GSList *filtered;
185
205
        GSList *list, *l;
186
206
        GtkTreeIter treeiter;
187
207
        NMActiveConnection *active_connection;
188
208
        NMConnection *connection;
189
 
        NMRemoteSettings *remote_settings;
190
209
 
191
210
        /* get the list of available connections for this device */
192
 
        remote_settings = net_object_get_remote_settings (NET_OBJECT (device_mobile));
193
 
        g_assert (remote_settings != NULL);
194
 
        list = nm_remote_settings_list_connections (remote_settings);
195
 
        filtered = nm_device_filter_connections (nm_device, list);
 
211
        list = net_device_get_valid_connections (NET_DEVICE (device_mobile));
196
212
        gtk_list_store_clear (liststore);
197
213
        active_connection = nm_device_get_active_connection (nm_device);
198
 
        for (l = filtered; l; l = g_slist_next (l)) {
 
214
        for (l = list; l; l = g_slist_next (l)) {
199
215
                connection = NM_CONNECTION (l->data);
200
216
                gtk_list_store_append (liststore, &treeiter);
201
217
                gtk_list_store_set (liststore,
206
222
 
207
223
                /* is this already activated? */
208
224
                if (active_connection != NULL &&
209
 
                    g_strcmp0 (nm_connection_get_path (connection),
210
 
                               nm_active_connection_get_connection (active_connection)) == 0) {
 
225
                    g_strcmp0 (nm_connection_get_uuid (connection),
 
226
                               nm_active_connection_get_uuid (active_connection)) == 0) {
211
227
                        priv->updating_device = TRUE;
212
228
                        gtk_combo_box_set_active_iter (combobox, &treeiter);
213
229
                        priv->updating_device = FALSE;
223
239
                            -1);
224
240
 
225
241
        g_slist_free (list);
226
 
        g_slist_free (filtered);
 
242
}
 
243
 
 
244
static void
 
245
device_mobile_refresh_equipment_id (NetDeviceMobile *device_mobile)
 
246
{
 
247
        const gchar *equipment_id = NULL;
 
248
 
 
249
        if (device_mobile->priv->mm_object != NULL) {
 
250
                MMModem *modem;
 
251
 
 
252
                /* Modem interface should always be present */
 
253
                modem = mm_object_peek_modem (device_mobile->priv->mm_object);
 
254
                equipment_id = mm_modem_get_equipment_identifier (modem);
 
255
 
 
256
                /* Set equipment ID */
 
257
                if (equipment_id != NULL) {
 
258
                        g_debug ("[%s] Equipment ID set to '%s'",
 
259
                                 mm_object_get_path (device_mobile->priv->mm_object),
 
260
                                 equipment_id);
 
261
                }
 
262
        } else {
 
263
                /* Assume old MM handling */
 
264
                equipment_id = g_object_get_data (G_OBJECT (device_mobile),
 
265
                                                  "ControlCenter::EquipmentIdentifier");
 
266
        }
 
267
 
 
268
        panel_set_device_widget_details (device_mobile->priv->builder, "imei", equipment_id);
 
269
}
 
270
 
 
271
static gchar *
 
272
device_mobile_find_provider (NetDeviceMobile *device_mobile,
 
273
                             const gchar     *mccmnc,
 
274
                             guint32          sid)
 
275
{
 
276
        NMAMobileProvider *provider;
 
277
        GString *name = NULL;
 
278
 
 
279
        if (device_mobile->priv->mpd == NULL) {
 
280
                GError *error = NULL;
 
281
 
 
282
                /* Use defaults */
 
283
                device_mobile->priv->mpd = nma_mobile_providers_database_new_sync (NULL, NULL, NULL, &error);
 
284
                if (device_mobile->priv->mpd == NULL) {
 
285
                        g_debug ("Couldn't load mobile providers database: %s",
 
286
                                 error ? error->message : "");
 
287
                        g_clear_error (&error);
 
288
                        return NULL;
 
289
                }
 
290
        }
 
291
 
 
292
        if (mccmnc != NULL) {
 
293
                provider = nma_mobile_providers_database_lookup_3gpp_mcc_mnc (device_mobile->priv->mpd, mccmnc);
 
294
                if (provider != NULL)
 
295
                        name = g_string_new (nma_mobile_provider_get_name (provider));
 
296
        }
 
297
 
 
298
        if (sid != 0) {
 
299
                provider = nma_mobile_providers_database_lookup_cdma_sid (device_mobile->priv->mpd, sid);
 
300
                if (provider != NULL) {
 
301
                        if (name == NULL)
 
302
                                name = g_string_new (nma_mobile_provider_get_name (provider));
 
303
                        else
 
304
                                g_string_append_printf (name, ", %s", nma_mobile_provider_get_name (provider));
 
305
                }
 
306
        }
 
307
 
 
308
        return (name != NULL ? g_string_free (name, FALSE) : NULL);
 
309
}
 
310
 
 
311
static void
 
312
device_mobile_refresh_operator_name (NetDeviceMobile *device_mobile)
 
313
{
 
314
        if (device_mobile->priv->mm_object != NULL) {
 
315
                gchar *operator_name = NULL;
 
316
                MMModem3gpp *modem_3gpp;
 
317
                MMModemCdma *modem_cdma;
 
318
 
 
319
                modem_3gpp = mm_object_peek_modem_3gpp (device_mobile->priv->mm_object);
 
320
                modem_cdma = mm_object_peek_modem_cdma (device_mobile->priv->mm_object);
 
321
 
 
322
                if (modem_3gpp != NULL) {
 
323
                        const gchar *operator_name_unsafe;
 
324
 
 
325
                        operator_name_unsafe = mm_modem_3gpp_get_operator_name (modem_3gpp);
 
326
                        if (operator_name_unsafe != NULL && operator_name_unsafe[0] != '\0')
 
327
                                operator_name = g_strescape (operator_name_unsafe, NULL);
 
328
                }
 
329
 
 
330
                /* If not directly given in the 3GPP interface, try to guess from
 
331
                 * MCCMNC/SID */
 
332
                if (operator_name == NULL) {
 
333
                        const gchar *mccmnc = NULL;
 
334
                        guint32 sid = 0;
 
335
 
 
336
                        if (modem_3gpp != NULL)
 
337
                                mccmnc = mm_modem_3gpp_get_operator_code (modem_3gpp);
 
338
                        if (modem_cdma != NULL)
 
339
                                sid = mm_modem_cdma_get_sid (modem_cdma);
 
340
                        operator_name = device_mobile_find_provider (device_mobile, mccmnc, sid);
 
341
                }
 
342
 
 
343
                /* Set operator name */
 
344
                if (operator_name != NULL) {
 
345
                        g_debug ("[%s] Operator name set to '%s'",
 
346
                                 mm_object_get_path (device_mobile->priv->mm_object),
 
347
                                 operator_name);
 
348
                }
 
349
 
 
350
                panel_set_device_widget_details (device_mobile->priv->builder, "provider", operator_name);
 
351
                g_free (operator_name);
 
352
        } else {
 
353
                const gchar *gsm;
 
354
                const gchar *cdma;
 
355
 
 
356
                /* Assume old MM handling */
 
357
                gsm = g_object_get_data (G_OBJECT (device_mobile),
 
358
                                         "ControlCenter::OperatorNameGsm");
 
359
                cdma = g_object_get_data (G_OBJECT (device_mobile),
 
360
                                          "ControlCenter::OperatorNameCdma");
 
361
 
 
362
                if (gsm != NULL && cdma != NULL) {
 
363
                        gchar *both;
 
364
 
 
365
                        both = g_strdup_printf ("%s, %s", gsm, cdma);
 
366
                        panel_set_device_widget_details (device_mobile->priv->builder, "provider", both);
 
367
                        g_free (both);
 
368
                } else if (gsm != NULL) {
 
369
                        panel_set_device_widget_details (device_mobile->priv->builder, "provider", gsm);
 
370
                } else if (cdma != NULL) {
 
371
                        panel_set_device_widget_details (device_mobile->priv->builder, "provider", cdma);
 
372
                } else {
 
373
                        panel_set_device_widget_details (device_mobile->priv->builder, "provider", NULL);
 
374
                }
 
375
        }
227
376
}
228
377
 
229
378
static void
230
379
nm_device_mobile_refresh_ui (NetDeviceMobile *device_mobile)
231
380
{
232
 
        const char *str;
233
381
        gboolean is_connected;
234
 
        GString *status;
235
382
        GtkListStore *liststore;
236
383
        GtkWidget *widget;
237
 
        guint speed = 0;
238
384
        NetDeviceMobilePrivate *priv = device_mobile->priv;
239
385
        NMClient *client;
240
386
        NMDeviceModemCapabilities caps;
241
387
        NMDevice *nm_device;
242
388
 
 
389
        nm_device = net_device_get_nm_device (NET_DEVICE (device_mobile));
 
390
 
243
391
        /* set device kind */
244
 
        nm_device = net_device_get_nm_device (NET_DEVICE (device_mobile));
245
392
        widget = GTK_WIDGET (gtk_builder_get_object (device_mobile->priv->builder, "label_device"));
246
 
        gtk_label_set_label (GTK_LABEL (widget),
247
 
                             panel_device_to_localized_string (nm_device));
 
393
        g_object_bind_property (device_mobile, "title", widget, "label", 0);
248
394
 
249
395
        /* set up the device on/off switch */
250
396
        widget = GTK_WIDGET (gtk_builder_get_object (device_mobile->priv->builder, "device_off_switch"));
252
398
        client = net_object_get_client (NET_OBJECT (device_mobile));
253
399
        mobilebb_enabled_toggled (client, NULL, device_mobile);
254
400
 
255
 
        /* set device state, with status and optionally speed */
256
 
        widget = GTK_WIDGET (gtk_builder_get_object (device_mobile->priv->builder, "label_status"));
257
 
        status = g_string_new (panel_device_state_to_localized_string (nm_device));
258
 
        if (speed  > 0) {
259
 
                g_string_append (status, " - ");
260
 
                /* Translators: network device speed */
261
 
                g_string_append_printf (status, _("%d Mb/s"), speed);
262
 
        }
263
 
        gtk_label_set_label (GTK_LABEL (widget), status->str);
264
 
        g_string_free (status, TRUE);
265
 
        gtk_widget_set_tooltip_text (widget, panel_device_state_reason_to_localized_string (nm_device));
 
401
        /* set device state, with status */
 
402
        panel_set_device_status (device_mobile->priv->builder, "label_status", nm_device, NULL);
266
403
 
267
404
        /* sensitive for other connection types if the device is currently connected */
268
405
        widget = GTK_WIDGET (gtk_builder_get_object (device_mobile->priv->builder,
272
409
 
273
410
        caps = nm_device_modem_get_current_capabilities (NM_DEVICE_MODEM (nm_device));
274
411
        if ((caps & NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS) ||
275
 
            (caps & NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO)) {
276
 
                /* IMEI */
277
 
                str = g_object_get_data (G_OBJECT (nm_device),
278
 
                                         "ControlCenter::EquipmentIdentifier");
279
 
                panel_set_device_widget_details (device_mobile->priv->builder,
280
 
                                                 "imei",
281
 
                                                 str);
282
 
 
283
 
                /* operator name */
284
 
                str = g_object_get_data (G_OBJECT (nm_device),
285
 
                                         "ControlCenter::OperatorName");
286
 
                panel_set_device_widget_details (device_mobile->priv->builder,
287
 
                                                 "provider",
288
 
                                                 str);
 
412
            (caps & NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO) ||
 
413
            (caps & NM_DEVICE_MODEM_CAPABILITY_LTE)) {
 
414
                device_mobile_refresh_operator_name (device_mobile);
 
415
                device_mobile_refresh_equipment_id (device_mobile);
289
416
        }
290
417
 
291
418
        /* add possible connections to device */
313
440
                    GParamSpec *pspec,
314
441
                    NetDeviceMobile *device_mobile)
315
442
{
316
 
        const gchar *path;
317
443
        const GPtrArray *acs;
318
444
        gboolean active;
319
445
        gint i;
330
456
                connection = net_device_get_find_connection (NET_DEVICE (device_mobile));
331
457
                if (connection == NULL)
332
458
                        return;
333
 
                nm_client_activate_connection (client,
334
 
                                               connection,
335
 
                                               net_device_get_nm_device (NET_DEVICE (device_mobile)),
336
 
                                               NULL, NULL, NULL);
 
459
                nm_client_activate_connection_async (client,
 
460
                                                     connection,
 
461
                                                     net_device_get_nm_device (NET_DEVICE (device_mobile)),
 
462
                                                     NULL, NULL, NULL, NULL);
337
463
        } else {
 
464
                const gchar *uuid;
 
465
 
338
466
                connection = net_device_get_find_connection (NET_DEVICE (device_mobile));
339
467
                if (connection == NULL)
340
468
                        return;
341
 
                path = nm_connection_get_path (connection);
 
469
                uuid = nm_connection_get_uuid (connection);
342
470
                client = net_object_get_client (NET_OBJECT (device_mobile));
343
471
                acs = nm_client_get_active_connections (client);
344
 
                for (i = 0; i < acs->len; i++) {
 
472
                for (i = 0; acs && i < acs->len; i++) {
345
473
                        a = (NMActiveConnection*)acs->pdata[i];
346
 
                        if (strcmp (nm_active_connection_get_connection (a), path) == 0) {
347
 
                                nm_client_deactivate_connection (client, a);
 
474
                        if (strcmp (nm_active_connection_get_uuid (a), uuid) == 0) {
 
475
                                nm_client_deactivate_connection (client, a, NULL, NULL);
348
476
                                break;
349
477
                        }
350
478
                }
359
487
 
360
488
static void
361
489
device_mobile_device_got_modem_manager_cb (GObject *source_object,
362
 
                                   GAsyncResult *res,
363
 
                                   gpointer user_data)
 
490
                                           GAsyncResult *res,
 
491
                                           gpointer user_data)
364
492
{
365
493
        GError *error = NULL;
366
494
        GVariant *result = NULL;
367
495
        GDBusProxy *proxy;
368
 
        NMDevice *device = (NMDevice *) user_data;
 
496
        NetDeviceMobile *device_mobile = (NetDeviceMobile *)user_data;
369
497
 
370
498
        proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
371
 
        if (proxy == NULL) {
 
499
        if (!proxy) {
372
500
                g_warning ("Error creating ModemManager proxy: %s",
373
501
                           error->message);
374
502
                g_error_free (error);
375
 
                goto out;
 
503
                return;
376
504
        }
377
505
 
378
506
        /* get the IMEI */
380
508
                                                   "EquipmentIdentifier");
381
509
 
382
510
        /* save */
383
 
        g_object_set_data_full (G_OBJECT (device),
384
 
                                "ControlCenter::EquipmentIdentifier",
385
 
                                g_variant_dup_string (result, NULL),
386
 
                                g_free);
387
 
out:
388
 
        if (result != NULL)
 
511
        if (result) {
 
512
                g_object_set_data_full (G_OBJECT (device_mobile),
 
513
                                        "ControlCenter::EquipmentIdentifier",
 
514
                                        g_variant_dup_string (result, NULL),
 
515
                                        g_free);
389
516
                g_variant_unref (result);
390
 
        if (proxy != NULL)
391
 
                g_object_unref (proxy);
 
517
        }
 
518
 
 
519
        device_mobile_refresh_equipment_id (device_mobile);
 
520
        g_object_unref (proxy);
392
521
}
393
522
 
394
523
static void
395
 
device_mobile_get_registration_info_cb (GObject *source_object, GAsyncResult *res, gpointer user_data)
 
524
device_mobile_save_operator_name (NetDeviceMobile *device_mobile,
 
525
                                  const gchar     *field,
 
526
                                  const gchar     *operator_name)
396
527
{
397
 
        gchar *operator_code = NULL;
398
 
        GError *error = NULL;
399
 
        guint registration_status;
400
 
        GVariant *result = NULL;
401
 
        gchar *operator_name = NULL;
402
528
        gchar *operator_name_safe = NULL;
403
 
        NMDevice *device = (NMDevice *) user_data;
404
 
 
405
 
        result = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object), res, &error);
406
 
        if (result == NULL) {
407
 
                g_warning ("Error getting registration info: %s\n",
408
 
                           error->message);
409
 
                g_error_free (error);
410
 
                return;
411
 
        }
412
 
 
413
 
        /* get values */
414
 
        g_variant_get (result, "((uss))",
415
 
                       &registration_status,
416
 
                       &operator_code,
417
 
                       &operator_name);
 
529
 
418
530
        if (operator_name != NULL && operator_name[0] != '\0')
419
531
                operator_name_safe = g_strescape (operator_name, NULL);
420
532
 
421
533
        /* save */
422
 
        g_object_set_data_full (G_OBJECT (device),
423
 
                                "ControlCenter::OperatorName",
 
534
        g_object_set_data_full (G_OBJECT (device_mobile),
 
535
                                field,
424
536
                                operator_name_safe,
425
537
                                g_free);
 
538
        /* refresh */
 
539
        device_mobile_refresh_operator_name (device_mobile);
 
540
}
 
541
 
 
542
static void
 
543
device_mobile_get_registration_info_cb (GObject      *source_object,
 
544
                                        GAsyncResult *res,
 
545
                                        gpointer      user_data)
 
546
{
 
547
        gchar *operator_code = NULL;
 
548
        GError *error = NULL;
 
549
        guint registration_status;
 
550
        GVariant *result = NULL;
 
551
        gchar *operator_name = NULL;
 
552
        NetDeviceMobile *device_mobile = (NetDeviceMobile *)user_data;
 
553
 
 
554
        result = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object), res, &error);
 
555
        if (result == NULL) {
 
556
                g_warning ("Error getting registration info: %s\n",
 
557
                           error->message);
 
558
                g_error_free (error);
 
559
                return;
 
560
        }
 
561
 
 
562
        /* get values */
 
563
        g_variant_get (result, "((uss))",
 
564
                       &registration_status,
 
565
                       &operator_code,
 
566
                       &operator_name);
 
567
 
 
568
        /* If none give, try to guess it */
 
569
        if (operator_name == NULL || operator_name[0] == '\0') {
 
570
                g_free (operator_name);
 
571
                operator_name = device_mobile_find_provider (device_mobile, operator_code, 0);
 
572
        }
 
573
 
 
574
        /* save and refresh */
 
575
        device_mobile_save_operator_name (device_mobile,
 
576
                                          "ControlCenter::OperatorNameGsm",
 
577
                                          operator_name);
426
578
 
427
579
        g_free (operator_name);
428
580
        g_free (operator_code);
430
582
}
431
583
 
432
584
static void
433
 
device_mobile_device_got_modem_manager_gsm_cb (GObject *source_object,
434
 
                                       GAsyncResult *res,
435
 
                                       gpointer user_data)
 
585
device_mobile_gsm_signal_cb (GDBusProxy *proxy,
 
586
                             gchar      *sender_name,
 
587
                             gchar      *signal_name,
 
588
                             GVariant   *parameters,
 
589
                             gpointer    user_data)
 
590
{
 
591
        guint registration_status = 0;
 
592
        gchar *operator_code = NULL;
 
593
        gchar *operator_name = NULL;
 
594
        NetDeviceMobile *device_mobile = (NetDeviceMobile *)user_data;
 
595
 
 
596
        if (!g_str_equal (signal_name, "RegistrationInfo"))
 
597
                return;
 
598
 
 
599
        g_variant_get (parameters,
 
600
                       "(uss)",
 
601
                       &registration_status,
 
602
                       &operator_code,
 
603
                       &operator_name);
 
604
 
 
605
        /* If none given, try to guess it */
 
606
        if (operator_name == NULL || operator_name[0] == '\0') {
 
607
                g_free (operator_name);
 
608
                operator_name = device_mobile_find_provider (device_mobile, operator_code, 0);
 
609
        }
 
610
 
 
611
        /* save and refresh */
 
612
        device_mobile_save_operator_name (device_mobile,
 
613
                                          "ControlCenter::OperatorNameGsm",
 
614
                                          operator_name);
 
615
 
 
616
        g_free (operator_code);
 
617
        g_free (operator_name);
 
618
}
 
619
 
 
620
static void
 
621
device_mobile_device_got_modem_manager_gsm_cb (GObject      *source_object,
 
622
                                               GAsyncResult *res,
 
623
                                               gpointer      user_data)
436
624
{
437
625
        GError *error = NULL;
438
 
        GDBusProxy *proxy;
439
 
        NMDevice *device = (NMDevice *) user_data;
 
626
        NetDeviceMobile *device_mobile = (NetDeviceMobile *)user_data;
440
627
 
441
 
        proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
442
 
        if (proxy == NULL) {
 
628
        device_mobile->priv->gsm_proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
 
629
        if (device_mobile->priv->gsm_proxy == NULL) {
443
630
                g_warning ("Error creating ModemManager GSM proxy: %s\n",
444
631
                           error->message);
445
632
                g_error_free (error);
446
 
                goto out;
 
633
                return;
447
634
        }
448
635
 
449
 
        g_dbus_proxy_call (proxy,
 
636
        /* Setup value updates */
 
637
        g_signal_connect (device_mobile->priv->gsm_proxy,
 
638
                          "g-signal",
 
639
                          G_CALLBACK (device_mobile_gsm_signal_cb),
 
640
                          device_mobile);
 
641
 
 
642
        /* Load initial value */
 
643
        g_dbus_proxy_call (device_mobile->priv->gsm_proxy,
450
644
                           "GetRegistrationInfo",
451
645
                           NULL,
452
646
                           G_DBUS_CALL_FLAGS_NONE,
453
647
                           -1,
454
648
                           NULL,
455
649
                           device_mobile_get_registration_info_cb,
456
 
                           device);
457
 
out:
458
 
        if (proxy != NULL)
459
 
                g_object_unref (proxy);
 
650
                           device_mobile);
 
651
}
 
652
 
 
653
static void
 
654
device_mobile_get_serving_system_cb (GObject      *source_object,
 
655
                                     GAsyncResult *res,
 
656
                                     gpointer      user_data)
 
657
{
 
658
        NetDeviceMobile *device_mobile = (NetDeviceMobile *)user_data;
 
659
        GVariant *result = NULL;
 
660
        GError *error = NULL;
 
661
 
 
662
        guint32 band_class;
 
663
        gchar *band;
 
664
        guint32 sid;
 
665
        gchar *operator_name;
 
666
 
 
667
        result = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object), res, &error);
 
668
        if (result == NULL) {
 
669
                g_warning ("Error getting serving system: %s\n",
 
670
                           error->message);
 
671
                g_error_free (error);
 
672
                return;
 
673
        }
 
674
 
 
675
        /* get values */
 
676
        g_variant_get (result, "((usu))",
 
677
                       &band_class,
 
678
                       &band,
 
679
                       &sid);
 
680
 
 
681
        operator_name = device_mobile_find_provider (device_mobile, NULL, sid);
 
682
 
 
683
        /* save and refresh */
 
684
        device_mobile_save_operator_name (device_mobile,
 
685
                                          "ControlCenter::OperatorNameCdma",
 
686
                                          operator_name);
 
687
 
 
688
        g_free (band);
 
689
        g_variant_unref (result);
 
690
}
 
691
 
 
692
static void
 
693
device_mobile_device_got_modem_manager_cdma_cb (GObject      *source_object,
 
694
                                                GAsyncResult *res,
 
695
                                                gpointer      user_data)
 
696
{
 
697
        GError *error = NULL;
 
698
        NetDeviceMobile *device_mobile = (NetDeviceMobile *)user_data;
 
699
 
 
700
        device_mobile->priv->cdma_proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
 
701
        if (device_mobile->priv->cdma_proxy == NULL) {
 
702
                g_warning ("Error creating ModemManager CDMA proxy: %s\n",
 
703
                           error->message);
 
704
                g_error_free (error);
 
705
                return;
 
706
        }
 
707
 
 
708
        /* Load initial value */
 
709
        g_dbus_proxy_call (device_mobile->priv->cdma_proxy,
 
710
                           "GetServingSystem",
 
711
                           NULL,
 
712
                           G_DBUS_CALL_FLAGS_NONE,
 
713
                           -1,
 
714
                           NULL,
 
715
                           device_mobile_get_serving_system_cb,
 
716
                           device_mobile);
460
717
}
461
718
 
462
719
static void
466
723
        NetDeviceMobile *device_mobile = NET_DEVICE_MOBILE (object);
467
724
        NMClient *client;
468
725
        NMDevice *device;
 
726
        NMDeviceModemCapabilities caps;
469
727
 
470
728
        G_OBJECT_CLASS (net_device_mobile_parent_class)->constructed (object);
471
729
 
472
730
        device = net_device_get_nm_device (NET_DEVICE (device_mobile));
473
731
        cancellable = net_object_get_cancellable (NET_OBJECT (device_mobile));
474
 
        g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
475
 
                                  G_DBUS_PROXY_FLAGS_NONE,
476
 
                                  NULL,
477
 
                                  "org.freedesktop.ModemManager",
478
 
                                  nm_device_get_udi (device),
479
 
                                  "org.freedesktop.ModemManager.Modem",
480
 
                                  cancellable,
481
 
                                  device_mobile_device_got_modem_manager_cb,
482
 
                                  device);
483
 
        g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
484
 
                                  G_DBUS_PROXY_FLAGS_NONE,
485
 
                                  NULL,
486
 
                                  "org.freedesktop.ModemManager",
487
 
                                  nm_device_get_udi (device),
488
 
                                  "org.freedesktop.ModemManager.Modem.Gsm.Network",
489
 
                                  cancellable,
490
 
                                  device_mobile_device_got_modem_manager_gsm_cb,
491
 
                                  device);
 
732
 
 
733
        caps = nm_device_modem_get_current_capabilities (NM_DEVICE_MODEM (device));
 
734
 
 
735
        /* Only load proxies if we have broadband modems of the OLD ModemManager interface */
 
736
        if (g_str_has_prefix (nm_device_get_udi (device), "/org/freedesktop/ModemManager/") &&
 
737
            ((caps & NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS) ||
 
738
             (caps & NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO) ||
 
739
             (caps & NM_DEVICE_MODEM_CAPABILITY_LTE))) {
 
740
                g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
 
741
                                          G_DBUS_PROXY_FLAGS_NONE,
 
742
                                          NULL,
 
743
                                          "org.freedesktop.ModemManager",
 
744
                                          nm_device_get_udi (device),
 
745
                                          "org.freedesktop.ModemManager.Modem",
 
746
                                          cancellable,
 
747
                                          device_mobile_device_got_modem_manager_cb,
 
748
                                          device_mobile);
 
749
 
 
750
                if ((caps & NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS) ||
 
751
                    (caps & NM_DEVICE_MODEM_CAPABILITY_LTE)) {
 
752
                        g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
 
753
                                                  G_DBUS_PROXY_FLAGS_NONE,
 
754
                                                  NULL,
 
755
                                                  "org.freedesktop.ModemManager",
 
756
                                                  nm_device_get_udi (device),
 
757
                                                  "org.freedesktop.ModemManager.Modem.Gsm.Network",
 
758
                                                  cancellable,
 
759
                                                  device_mobile_device_got_modem_manager_gsm_cb,
 
760
                                                  device_mobile);
 
761
                }
 
762
 
 
763
                if (caps & NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO) {
 
764
                        g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
 
765
                                                  G_DBUS_PROXY_FLAGS_NONE,
 
766
                                                  NULL,
 
767
                                                  "org.freedesktop.ModemManager",
 
768
                                                  nm_device_get_udi (device),
 
769
                                                  "org.freedesktop.ModemManager.Modem.Cdma",
 
770
                                                  cancellable,
 
771
                                                  device_mobile_device_got_modem_manager_cdma_cb,
 
772
                                                  device_mobile);
 
773
                }
 
774
        }
492
775
 
493
776
        client = net_object_get_client (NET_OBJECT (device_mobile));
494
 
        g_signal_connect (client, "notify::wwan-enabled",
495
 
                          G_CALLBACK (mobilebb_enabled_toggled),
496
 
                          device_mobile);
 
777
        g_signal_connect_object (client, "notify::wwan-enabled",
 
778
                                 G_CALLBACK (mobilebb_enabled_toggled),
 
779
                                 device_mobile, 0);
497
780
        nm_device_mobile_refresh_ui (device_mobile);
498
781
}
499
782
 
500
783
static void
501
 
net_device_mobile_finalize (GObject *object)
 
784
operator_name_updated (MMModem3gpp     *modem_3gpp_iface,
 
785
                       GParamSpec      *pspec,
 
786
                       NetDeviceMobile *self)
 
787
{
 
788
        device_mobile_refresh_operator_name (self);
 
789
}
 
790
 
 
791
static void
 
792
net_device_mobile_setup_modem_object (NetDeviceMobile *self)
 
793
{
 
794
        MMModem3gpp *modem_3gpp;
 
795
 
 
796
        if (self->priv->mm_object == NULL)
 
797
                return;
 
798
 
 
799
        /* Load equipment ID initially */
 
800
        device_mobile_refresh_equipment_id (self);
 
801
 
 
802
        /* Follow changes in operator name and load initial values */
 
803
        modem_3gpp = mm_object_peek_modem_3gpp (self->priv->mm_object);
 
804
        if (modem_3gpp != NULL) {
 
805
                g_assert (self->priv->operator_name_updated == 0);
 
806
                self->priv->operator_name_updated = g_signal_connect (modem_3gpp,
 
807
                                                                      "notify::operator-name",
 
808
                                                                      G_CALLBACK (operator_name_updated),
 
809
                                                                      self);
 
810
                device_mobile_refresh_operator_name (self);
 
811
        }
 
812
}
 
813
 
 
814
 
 
815
static void
 
816
net_device_mobile_get_property (GObject    *device_,
 
817
                                guint       prop_id,
 
818
                                GValue     *value,
 
819
                                GParamSpec *pspec)
 
820
{
 
821
        NetDeviceMobile *self = NET_DEVICE_MOBILE (device_);
 
822
 
 
823
        switch (prop_id) {
 
824
        case PROP_MODEM_OBJECT:
 
825
                g_value_set_object (value, self->priv->mm_object);
 
826
                break;
 
827
        default:
 
828
                G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
 
829
                break;
 
830
        }
 
831
}
 
832
 
 
833
static void
 
834
net_device_mobile_set_property (GObject      *device_,
 
835
                                guint         prop_id,
 
836
                                const GValue *value,
 
837
                                GParamSpec   *pspec)
 
838
{
 
839
        NetDeviceMobile *self = NET_DEVICE_MOBILE (device_);
 
840
 
 
841
        switch (prop_id) {
 
842
        case PROP_MODEM_OBJECT:
 
843
                self->priv->mm_object = g_value_dup_object (value);
 
844
                net_device_mobile_setup_modem_object (self);
 
845
                break;
 
846
        default:
 
847
                G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
 
848
                break;
 
849
        }
 
850
}
 
851
 
 
852
static void
 
853
net_device_mobile_dispose (GObject *object)
502
854
{
503
855
        NetDeviceMobile *device_mobile = NET_DEVICE_MOBILE (object);
504
856
        NetDeviceMobilePrivate *priv = device_mobile->priv;
505
857
 
506
 
        g_object_unref (priv->builder);
507
 
 
508
 
        G_OBJECT_CLASS (net_device_mobile_parent_class)->finalize (object);
 
858
        g_clear_object (&priv->builder);
 
859
        g_clear_object (&priv->gsm_proxy);
 
860
        g_clear_object (&priv->cdma_proxy);
 
861
 
 
862
        if (priv->operator_name_updated) {
 
863
                g_assert (priv->mm_object != NULL);
 
864
                g_signal_handler_disconnect (mm_object_peek_modem_3gpp (priv->mm_object), priv->operator_name_updated);
 
865
                priv->operator_name_updated = 0;
 
866
        }
 
867
        g_clear_object (&priv->mm_object);
 
868
        g_clear_object (&priv->mpd);
 
869
 
 
870
        G_OBJECT_CLASS (net_device_mobile_parent_class)->dispose (object);
509
871
}
510
872
 
511
873
static void
514
876
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
515
877
        NetObjectClass *parent_class = NET_OBJECT_CLASS (klass);
516
878
 
517
 
        object_class->finalize = net_device_mobile_finalize;
 
879
        object_class->dispose = net_device_mobile_dispose;
518
880
        object_class->constructed = net_device_mobile_constructed;
 
881
        object_class->get_property = net_device_mobile_get_property;
 
882
        object_class->set_property = net_device_mobile_set_property;
519
883
        parent_class->add_to_notebook = device_mobile_proxy_add_to_notebook;
520
884
        parent_class->refresh = device_mobile_refresh;
 
885
 
521
886
        g_type_class_add_private (klass, sizeof (NetDeviceMobilePrivate));
 
887
 
 
888
        g_object_class_install_property (object_class,
 
889
                                         PROP_MODEM_OBJECT,
 
890
                                         g_param_spec_object ("mm-object",
 
891
                                                              NULL,
 
892
                                                              NULL,
 
893
                                                              MM_TYPE_OBJECT,
 
894
                                                              G_PARAM_READWRITE));
522
895
}
523
896
 
524
897
static void
532
905
        device_mobile->priv = NET_DEVICE_MOBILE_GET_PRIVATE (device_mobile);
533
906
 
534
907
        device_mobile->priv->builder = gtk_builder_new ();
535
 
        gtk_builder_add_from_file (device_mobile->priv->builder,
536
 
                                   GNOMECC_UI_DIR "/network-mobile.ui",
537
 
                                   &error);
 
908
        gtk_builder_add_from_resource (device_mobile->priv->builder,
 
909
                                       "/org/gnome/control-center/network/network-mobile.ui",
 
910
                                       &error);
538
911
        if (error != NULL) {
539
912
                g_warning ("Could not load interface file: %s", error->message);
540
913
                g_error_free (error);
564
937
                                                     "button_options"));
565
938
        g_signal_connect (widget, "clicked",
566
939
                          G_CALLBACK (edit_connection), device_mobile);
567
 
 
568
 
        widget = GTK_WIDGET (gtk_builder_get_object (device_mobile->priv->builder,
569
 
                                                     "device_off_switch"));
570
 
        g_signal_connect (widget, "notify::active",
571
 
                          G_CALLBACK (device_off_toggled), device_mobile);
572
940
}