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

« back to all changes in this revision

Viewing changes to panels/network/net-vpn.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:
23
23
 
24
24
#include <glib-object.h>
25
25
#include <glib/gi18n.h>
 
26
#include <NetworkManager.h>
26
27
 
27
28
#include "panel-common.h"
28
29
 
29
30
#include "net-vpn.h"
30
 
#include "nm-client.h"
31
 
#include "nm-remote-connection.h"
32
 
#include "nm-setting-vpn.h"
 
31
 
 
32
#include "connection-editor/net-connection-editor.h"
33
33
 
34
34
#define NET_VPN_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NET_TYPE_VPN, NetVpnPrivate))
35
35
 
37
37
{
38
38
        GtkBuilder              *builder;
39
39
        NMConnection            *connection;
 
40
        NMActiveConnection      *active_connection;
40
41
        gchar                   *service_type;
41
42
        gboolean                 valid;
42
43
        gboolean                 updating_device;
51
52
G_DEFINE_TYPE (NetVpn, net_vpn, NET_TYPE_OBJECT)
52
53
 
53
54
static void
54
 
connection_vpn_state_changed_cb (NMVPNConnection *connection,
55
 
                                 NMVPNConnectionState state,
56
 
                                 NMVPNConnectionStateReason reason,
 
55
connection_vpn_state_changed_cb (NMVpnConnection *connection,
 
56
                                 NMVpnConnectionState state,
 
57
                                 NMVpnConnectionStateReason reason,
57
58
                                 NetVpn *vpn)
58
59
{
59
60
        net_object_emit_changed (NET_OBJECT (vpn));
67
68
}
68
69
 
69
70
static void
70
 
connection_removed_cb (NMConnection *connection,
71
 
                       NetVpn *vpn)
 
71
connection_removed_cb (NMClient     *client,
 
72
                       NMConnection *connection,
 
73
                       NetVpn       *vpn)
72
74
{
73
 
        net_object_emit_removed (NET_OBJECT (vpn));
 
75
        NetVpnPrivate *priv = vpn->priv;
 
76
 
 
77
        if (priv->connection == connection)
 
78
                net_object_emit_removed (NET_OBJECT (vpn));
74
79
}
75
80
 
76
81
static char *
88
93
net_vpn_set_connection (NetVpn *vpn, NMConnection *connection)
89
94
{
90
95
        NetVpnPrivate *priv = vpn->priv;
 
96
        NMClient *client;
 
97
 
91
98
        /*
92
99
         * vpnc config exmaple:
93
100
         * key=IKE DH Group, value=dh2
99
106
         * key=Xauth username, value=rhughes
100
107
         */
101
108
        priv->connection = g_object_ref (connection);
102
 
        g_signal_connect (priv->connection,
103
 
                          NM_REMOTE_CONNECTION_REMOVED,
 
109
 
 
110
        client = net_object_get_client (NET_OBJECT (vpn));
 
111
        g_signal_connect (client,
 
112
                          NM_CLIENT_CONNECTION_REMOVED,
104
113
                          G_CALLBACK (connection_removed_cb),
105
114
                          vpn);
106
 
        g_signal_connect (priv->connection,
107
 
                          NM_REMOTE_CONNECTION_UPDATED,
 
115
        g_signal_connect (connection,
 
116
                          NM_CONNECTION_CHANGED,
108
117
                          G_CALLBACK (connection_changed_cb),
109
118
                          vpn);
 
119
 
110
120
        if (NM_IS_VPN_CONNECTION (priv->connection)) {
111
121
                g_signal_connect (priv->connection,
112
122
                                  NM_VPN_CONNECTION_VPN_STATE,
117
127
        priv->service_type = net_vpn_connection_to_type (priv->connection);
118
128
}
119
129
 
120
 
static NMVPNConnectionState
 
130
static NMVpnConnectionState
121
131
net_vpn_get_state (NetVpn *vpn)
122
132
{
123
133
        NetVpnPrivate *priv = vpn->priv;
222
232
vpn_proxy_delete (NetObject *object)
223
233
{
224
234
        NetVpn *vpn = NET_VPN (object);
225
 
        nm_remote_connection_delete (NM_REMOTE_CONNECTION (vpn->priv->connection),
226
 
                                     NULL, vpn);
 
235
        nm_remote_connection_delete_async (NM_REMOTE_CONNECTION (vpn->priv->connection),
 
236
                                           NULL, NULL, vpn);
227
237
}
228
238
 
229
239
static GtkWidget *
232
242
                           GtkSizeGroup *heading_size_group)
233
243
{
234
244
        GtkWidget *widget;
235
 
        GtkWindow *window;
236
245
        NetVpn *vpn = NET_VPN (object);
237
246
 
238
247
        /* add widgets to size group */
240
249
                                                     "heading_group_password"));
241
250
        gtk_size_group_add_widget (heading_size_group, widget);
242
251
 
243
 
        /* reparent */
244
 
        window = GTK_WINDOW (gtk_builder_get_object (vpn->priv->builder,
245
 
                                                     "window_tmp"));
246
252
        widget = GTK_WIDGET (gtk_builder_get_object (vpn->priv->builder,
247
253
                                                     "vbox9"));
248
 
        g_object_ref (widget);
249
 
        gtk_container_remove (GTK_CONTAINER (window), widget);
250
254
        gtk_notebook_append_page (notebook, widget, NULL);
251
 
        g_object_unref (widget);
252
255
        return widget;
253
256
}
254
257
 
262
265
        const GPtrArray *acs;
263
266
        NMActiveConnection *a;
264
267
        gint i;
265
 
        const gchar *path;
266
 
        const gchar *apath;
267
 
        NMVPNConnectionState state;
 
268
        NMVpnConnectionState state;
268
269
        gchar *title;
269
270
        NMClient *client;
270
271
 
275
276
        /* update title */
276
277
        widget = GTK_WIDGET (gtk_builder_get_object (priv->builder,
277
278
                                                     "label_device"));
 
279
        /* Translators: this is the title of the connection details
 
280
         * window for vpn connections, it is also used to display
 
281
         * vpn connections in the device list.
 
282
         */
278
283
        title = g_strdup_printf (_("%s VPN"), nm_connection_get_id (vpn->priv->connection));
279
284
        net_object_set_title (NET_OBJECT (vpn), title);
280
285
        gtk_label_set_label (GTK_LABEL (widget), title);
281
286
        g_free (title);
282
287
 
 
288
        if (priv->active_connection) {
 
289
                g_signal_handlers_disconnect_by_func (vpn->priv->active_connection,
 
290
                                                      nm_device_refresh_vpn_ui,
 
291
                                                      vpn);
 
292
                g_clear_object (&priv->active_connection);
 
293
        }
 
294
 
 
295
 
283
296
        /* use status */
284
297
        state = net_vpn_get_state (vpn);
285
298
        client = net_object_get_client (NET_OBJECT (vpn));
286
299
        acs = nm_client_get_active_connections (client);
287
300
        if (acs != NULL) {
288
 
                path = nm_connection_get_path (vpn->priv->connection);
 
301
                const gchar *uuid;
 
302
 
 
303
                uuid = nm_connection_get_uuid (vpn->priv->connection);
289
304
                for (i = 0; i < acs->len; i++) {
 
305
                        const gchar *auuid;
 
306
 
290
307
                        a = (NMActiveConnection*)acs->pdata[i];
291
308
 
292
 
                        apath = nm_active_connection_get_connection (a);
293
 
                        if (NM_IS_VPN_CONNECTION (a) && strcmp (apath, path) == 0) {
 
309
                        auuid = nm_active_connection_get_uuid (a);
 
310
                        if (NM_IS_VPN_CONNECTION (a) && strcmp (auuid, uuid) == 0) {
 
311
                                priv->active_connection = g_object_ref (a);
 
312
                                g_signal_connect_swapped (a, "notify::vpn-state",
 
313
                                                          G_CALLBACK (nm_device_refresh_vpn_ui),
 
314
                                                          vpn);
294
315
                                state = nm_vpn_connection_get_vpn_state (NM_VPN_CONNECTION (a));
295
316
                                break;
296
317
                        }
334
355
}
335
356
 
336
357
static void
 
358
nm_active_connections_changed (NetVpn *vpn)
 
359
{
 
360
        nm_device_refresh_vpn_ui (vpn);
 
361
}
 
362
 
 
363
static void
337
364
vpn_proxy_refresh (NetObject *object)
338
365
{
339
366
        NetVpn *vpn = NET_VPN (object);
345
372
                    GParamSpec *pspec,
346
373
                    NetVpn *vpn)
347
374
{
348
 
        const gchar *path;
349
375
        const GPtrArray *acs;
350
376
        gboolean active;
351
377
        gint i;
358
384
        active = gtk_switch_get_active (sw);
359
385
        if (active) {
360
386
                client = net_object_get_client (NET_OBJECT (vpn));
361
 
                nm_client_activate_connection (client,
362
 
                                               vpn->priv->connection, NULL, NULL,
363
 
                                               NULL, NULL);
 
387
                nm_client_activate_connection_async (client,
 
388
                                                     vpn->priv->connection, NULL, NULL,
 
389
                                                     NULL, NULL, NULL);
364
390
        } else {
365
 
                path = nm_connection_get_path (vpn->priv->connection);
 
391
                const gchar *uuid;
 
392
 
 
393
                uuid = nm_connection_get_uuid (vpn->priv->connection);
366
394
                client = net_object_get_client (NET_OBJECT (vpn));
367
395
                acs = nm_client_get_active_connections (client);
368
 
                for (i = 0; i < acs->len; i++) {
 
396
                for (i = 0; acs && i < acs->len; i++) {
369
397
                        a = (NMActiveConnection*)acs->pdata[i];
370
 
                        if (strcmp (nm_active_connection_get_connection (a), path) == 0) {
371
 
                                nm_client_deactivate_connection (client, a);
 
398
                        if (strcmp (nm_active_connection_get_uuid (a), uuid) == 0) {
 
399
                                nm_client_deactivate_connection (client, a, NULL, NULL);
372
400
                                break;
373
401
                        }
374
402
                }
382
410
}
383
411
 
384
412
static void
 
413
editor_done (NetConnectionEditor *editor,
 
414
             gboolean             success,
 
415
             NetVpn              *vpn)
 
416
{
 
417
        g_object_unref (editor);
 
418
        net_object_refresh (NET_OBJECT (vpn));
 
419
        g_object_unref (vpn);
 
420
}
 
421
 
 
422
static void
385
423
vpn_proxy_edit (NetObject *object)
386
424
{
387
 
        const gchar *uuid;
388
 
        gchar *cmdline;
389
 
        GError *error = NULL;
390
425
        NetVpn *vpn = NET_VPN (object);
391
 
 
392
 
        uuid = nm_connection_get_uuid (vpn->priv->connection);
393
 
        cmdline = g_strdup_printf ("nm-connection-editor --edit %s", uuid);
394
 
        g_debug ("Launching '%s'\n", cmdline);
395
 
        if (!g_spawn_command_line_async (cmdline, &error)) {
396
 
                g_warning ("Failed to launch nm-connection-editor: %s", error->message);
397
 
                g_error_free (error);
398
 
        }
399
 
        g_free (cmdline);
 
426
        GtkWidget *button, *window;
 
427
        NetConnectionEditor *editor;
 
428
        NMClient *client;
 
429
        gchar *title;
 
430
 
 
431
        button = GTK_WIDGET (gtk_builder_get_object (vpn->priv->builder,
 
432
                                                     "button_options"));
 
433
        window = gtk_widget_get_toplevel (button);
 
434
 
 
435
        client = net_object_get_client (object);
 
436
 
 
437
        editor = net_connection_editor_new (GTK_WINDOW (window),
 
438
                                            vpn->priv->connection,
 
439
                                            NULL, NULL, client);
 
440
        title = g_strdup_printf (_("%s VPN"), nm_connection_get_id (vpn->priv->connection));
 
441
        net_connection_editor_set_title (editor, title);
 
442
        g_free (title);
 
443
 
 
444
        g_signal_connect (editor, "done", G_CALLBACK (editor_done), g_object_ref (vpn));
 
445
        net_connection_editor_run (editor);
400
446
}
401
447
 
402
448
/**
446
492
net_vpn_constructed (GObject *object)
447
493
{
448
494
        NetVpn *vpn = NET_VPN (object);
 
495
        NMClient *client = net_object_get_client (NET_OBJECT (object));
449
496
 
450
497
        G_OBJECT_CLASS (net_vpn_parent_class)->constructed (object);
451
498
 
452
499
        nm_device_refresh_vpn_ui (vpn);
 
500
 
 
501
        g_signal_connect_swapped (client,
 
502
                                  "notify::active-connections",
 
503
                                  G_CALLBACK (nm_active_connections_changed),
 
504
                                  vpn);
 
505
 
453
506
}
454
507
 
455
508
static void
457
510
{
458
511
        NetVpn *vpn = NET_VPN (object);
459
512
        NetVpnPrivate *priv = vpn->priv;
460
 
 
 
513
        NMClient *client = net_object_get_client (NET_OBJECT (object));
 
514
 
 
515
        if (client) {
 
516
                g_signal_handlers_disconnect_by_func (client,
 
517
                                                      nm_active_connections_changed,
 
518
                                                      vpn);
 
519
        }
 
520
 
 
521
        if (priv->active_connection) {
 
522
                g_signal_handlers_disconnect_by_func (priv->active_connection,
 
523
                                                      nm_device_refresh_vpn_ui,
 
524
                                                      vpn);
 
525
                g_object_unref (priv->active_connection);
 
526
        }
 
527
 
 
528
        g_signal_handlers_disconnect_by_func (priv->connection,
 
529
                                              connection_vpn_state_changed_cb,
 
530
                                              vpn);
 
531
        g_signal_handlers_disconnect_by_func (priv->connection,
 
532
                                              connection_removed_cb,
 
533
                                              vpn);
 
534
        g_signal_handlers_disconnect_by_func (priv->connection,
 
535
                                              connection_changed_cb,
 
536
                                              vpn);
461
537
        g_object_unref (priv->connection);
462
538
        g_free (priv->service_type);
463
539
 
 
540
        g_clear_object (&priv->builder);
 
541
 
464
542
        G_OBJECT_CLASS (net_vpn_parent_class)->finalize (object);
465
543
}
466
544
 
497
575
        vpn->priv = NET_VPN_GET_PRIVATE (vpn);
498
576
 
499
577
        vpn->priv->builder = gtk_builder_new ();
500
 
        gtk_builder_add_from_file (vpn->priv->builder,
501
 
                                   GNOMECC_UI_DIR "/network-vpn.ui",
502
 
                                   &error);
 
578
        gtk_builder_add_from_resource (vpn->priv->builder,
 
579
                                       "/org/gnome/control-center/network/network-vpn.ui",
 
580
                                       &error);
503
581
        if (error != NULL) {
504
582
                g_warning ("Could not load interface file: %s", error->message);
505
583
                g_error_free (error);