summaryrefslogtreecommitdiff
path: root/panels/network/net-virtual-device.c
blob: 3d0ad95af90b197468f244ecf8a98afad0586801 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2012 Red Hat, Inc.
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <glib-object.h>
#include <glib/gi18n.h>

#include <nm-client.h>
#include <nm-device.h>
#include <nm-remote-connection.h>

#include "panel-common.h"
#include "cc-network-panel.h"

#include "net-virtual-device.h"

#define NET_VIRTUAL_DEVICE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NET_TYPE_VIRTUAL_DEVICE, NetVirtualDevicePrivate))

struct _NetVirtualDevicePrivate {
        NMConnection *connection;
        const char *iface;

        GtkBuilder *builder;
        gboolean updating_device;
};

enum {
        PROP_0,
        PROP_CONNECTION,
        PROP_LAST
};

enum {
        SIGNAL_DEVICE_SET,
        SIGNAL_DEVICE_UNSET,
        SIGNAL_LAST
};

static guint signals[SIGNAL_LAST] = { 0 };

G_DEFINE_TYPE (NetVirtualDevice, net_virtual_device, NET_TYPE_DEVICE)

static void
connection_changed_cb (NMConnection *connection,
                       NetObject    *object)
{
        net_object_emit_changed (object);
}

static void
connection_removed_cb (NMConnection *connection,
                       NetObject    *object)
{
        net_object_emit_removed (object);
}

static void
net_virtual_device_set_connection (NetVirtualDevice *virtual_device,
                                   NMConnection     *connection)
{
        NetVirtualDevicePrivate *priv = virtual_device->priv;

        priv->connection = g_object_ref (connection);
        priv->iface = nm_connection_get_virtual_iface_name (priv->connection);

        g_signal_connect (priv->connection,
                          NM_REMOTE_CONNECTION_REMOVED,
                          G_CALLBACK (connection_removed_cb),
                          virtual_device);
        g_signal_connect (priv->connection,
                          NM_REMOTE_CONNECTION_UPDATED,
                          G_CALLBACK (connection_changed_cb),
                          virtual_device);
}

static void
net_virtual_device_get_property (GObject *object,
                                 guint prop_id,
                                 GValue *value,
                                 GParamSpec *pspec)
{
        NetVirtualDevice *virtual_device = NET_VIRTUAL_DEVICE (object);
        NetVirtualDevicePrivate *priv = virtual_device->priv;

        switch (prop_id) {
        case PROP_CONNECTION:
                g_value_set_object (value, priv->connection);
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (virtual_device, prop_id, pspec);
                break;

        }
}

static void
net_virtual_device_set_property (GObject *object,
                                 guint prop_id,
                                 const GValue *value,
                                 GParamSpec *pspec)
{
        NetVirtualDevice *virtual_device = NET_VIRTUAL_DEVICE (object);
        NMConnection *connection;

        switch (prop_id) {
        case PROP_CONNECTION:
                connection = NM_CONNECTION (g_value_get_object (value));
                net_virtual_device_set_connection (virtual_device, connection);
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (virtual_device, prop_id, pspec);
                break;

        }
}

static GtkWidget *
net_virtual_device_add_to_notebook (NetObject *object,
                                    GtkNotebook *notebook,
                                    GtkSizeGroup *heading_size_group)
{
        NetVirtualDevice *virtual_device = NET_VIRTUAL_DEVICE (object);
        GtkWidget *widget;

        /* add widgets to size group */
        widget = GTK_WIDGET (gtk_builder_get_object (virtual_device->priv->builder,
                                                     "heading_ipv4"));
        gtk_size_group_add_widget (heading_size_group, widget);

        widget = GTK_WIDGET (gtk_builder_get_object (virtual_device->priv->builder,
                                                     "vbox6"));
        gtk_notebook_append_page (notebook, widget, NULL);
        return widget;
}

static void
update_off_switch_from_device_state (GtkSwitch *sw,
                                     NMDeviceState state,
                                     NetVirtualDevice *virtual_device)
{
        virtual_device->priv->updating_device = TRUE;
        switch (state) {
                case NM_DEVICE_STATE_UNMANAGED:
                case NM_DEVICE_STATE_UNAVAILABLE:
                case NM_DEVICE_STATE_DISCONNECTED:
                case NM_DEVICE_STATE_DEACTIVATING:
                case NM_DEVICE_STATE_FAILED:
                        gtk_switch_set_active (sw, FALSE);
                        break;
                default:
                        gtk_switch_set_active (sw, TRUE);
                        break;
        }
        virtual_device->priv->updating_device = FALSE;
}

static void
net_virtual_device_refresh (NetObject *object)
{
        NetVirtualDevice *virtual_device = NET_VIRTUAL_DEVICE (object);
        NetVirtualDevicePrivate *priv = virtual_device->priv;
        char *hwaddr;
        GtkWidget *widget;
        NMDevice *nm_device;
        NMDeviceState state;
        gboolean disconnected;

        nm_device = net_device_get_nm_device (NET_DEVICE (virtual_device));
        state = nm_device ? nm_device_get_state (nm_device) : NM_DEVICE_STATE_DISCONNECTED;
        disconnected = (state == NM_DEVICE_STATE_DISCONNECTED || state == NM_DEVICE_STATE_UNAVAILABLE);

        /* set device kind */
        widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "image_device"));
        gtk_image_set_from_icon_name (GTK_IMAGE (widget),
                                      disconnected ? "network-wired-disconnected" : "network-wired",
                                      GTK_ICON_SIZE_DIALOG);

        /* set up the device on/off switch */
        widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "device_off_switch"));
        update_off_switch_from_device_state (GTK_SWITCH (widget), state, virtual_device);

        /* set device state, with status and optionally speed */
        if (nm_device) {
                panel_set_device_status (priv->builder, "label_status", nm_device, NULL);
        } else {
                widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "label_status"));
                gtk_label_set_label (GTK_LABEL (widget), "");
        }

        /* device MAC */
        if (nm_device) {
                g_object_get (G_OBJECT (nm_device),
                              "hw-address", &hwaddr,
                              NULL);
        } else
                hwaddr = g_strdup ("");
        panel_set_device_widget_details (priv->builder, "mac", hwaddr);
        g_free (hwaddr);

        /* set IP entries */
        if (nm_device)
                panel_set_device_widgets (priv->builder, nm_device);
        else
                panel_unset_device_widgets (priv->builder);
}

static void
net_virtual_device_delete (NetObject *object)
{
        NetVirtualDevice *virtual_device = NET_VIRTUAL_DEVICE (object);
        NetVirtualDevicePrivate *priv = virtual_device->priv;
        const char *path;
        NMRemoteSettings *settings;
        NMRemoteConnection *connection;

        settings = net_object_get_remote_settings (object);
        path = nm_connection_get_path (priv->connection);
        connection = nm_remote_settings_get_connection_by_path (settings, path);
        nm_remote_connection_delete (connection, NULL, NULL);
}

static void
device_added_cb (NMClient *client, NMDevice *nm_device, gpointer user_data)
{
        NetVirtualDevice *virtual_device = user_data;
        NetVirtualDevicePrivate *priv = virtual_device->priv;
        const char *iface;

        iface = nm_device_get_iface (nm_device);
        if (strcmp (iface, priv->iface) == 0) {
                g_object_set (G_OBJECT (virtual_device),
                              "nm-device", nm_device,
                              NULL);
                g_signal_emit (virtual_device, signals[SIGNAL_DEVICE_SET], 0, nm_device);
                net_object_emit_changed (NET_OBJECT (virtual_device));
                net_object_refresh (NET_OBJECT (virtual_device));
        }
}

static void
device_removed_cb (NMClient *client, NMDevice *nm_device, gpointer user_data)
{
        NetVirtualDevice *virtual_device = user_data;
        NetVirtualDevicePrivate *priv = virtual_device->priv;
        const char *iface;

        iface = nm_device_get_iface (nm_device);
        if (strcmp (iface, priv->iface) == 0) {
                g_object_set (G_OBJECT (virtual_device),
                              "nm-device", NULL,
                              NULL);
                g_signal_emit (virtual_device, signals[SIGNAL_DEVICE_UNSET], 0, nm_device);
                net_object_emit_changed (NET_OBJECT (virtual_device));
                net_object_refresh (NET_OBJECT (virtual_device));
        }
}

static void
device_off_toggled (GtkSwitch *sw,
                    GParamSpec *pspec,
                    gpointer user_data)
{
        NetVirtualDevice *virtual_device = user_data;
        gboolean active;
        NMActiveConnection *ac;
        NMClient *client;
        NMDevice *nm_device;

        if (virtual_device->priv->updating_device)
                return;

        client = net_object_get_client (NET_OBJECT (virtual_device));
        nm_device = net_device_get_nm_device (NET_DEVICE (virtual_device));

        active = gtk_switch_get_active (sw);
        if (active) {
                nm_client_activate_connection (client,
                                               virtual_device->priv->connection,
                                               nm_device,
                                               NULL, NULL, NULL);
        } else {
                g_return_if_fail (nm_device != NULL);

                ac = nm_device_get_active_connection (nm_device);
                g_return_if_fail (ac != NULL);

                nm_client_deactivate_connection (client, ac);
        }
}

static void
edit_connection (GtkButton *button, gpointer virtual_device)
{
        net_object_edit (NET_OBJECT (virtual_device));
}

static void
net_virtual_device_constructed (GObject *object)
{
        NetVirtualDevice *virtual_device = NET_VIRTUAL_DEVICE (object);
        NMClient *client;
        const GPtrArray *devices;
        int i;

        client = net_object_get_client (NET_OBJECT (virtual_device));

        g_signal_connect_object (client, "device-added",
                                 G_CALLBACK (device_added_cb), virtual_device, 0);
        g_signal_connect_object (client, "device-removed",
                                 G_CALLBACK (device_removed_cb), virtual_device, 0);
        devices = nm_client_get_devices (client);
        if (devices) {
                for (i = 0; i < devices->len; i++)
                        device_added_cb (client, devices->pdata[i], virtual_device);
        }

        net_object_refresh (NET_OBJECT (virtual_device));

        G_OBJECT_CLASS (net_virtual_device_parent_class)->constructed (object);
}

static void
net_virtual_device_finalize (GObject *object)
{
        NetVirtualDevice *virtual_device = NET_VIRTUAL_DEVICE (object);
        NetVirtualDevicePrivate *priv = virtual_device->priv;

        if (priv->connection)
                g_object_unref (priv->connection);
        g_object_unref (priv->builder);

        G_OBJECT_CLASS (net_virtual_device_parent_class)->finalize (object);
}

static NMConnection *
net_virtual_device_get_find_connection (NetDevice *device)
{
        NetVirtualDevice *virtual_device = NET_VIRTUAL_DEVICE (device);

        return virtual_device->priv->connection;
}

static void
net_virtual_device_class_init (NetVirtualDeviceClass *klass)
{
        NetDeviceClass *device_class = NET_DEVICE_CLASS (klass);
        NetObjectClass *net_object_class = NET_OBJECT_CLASS (klass);
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
        GParamSpec *pspec;

        object_class->constructed = net_virtual_device_constructed;
        object_class->finalize = net_virtual_device_finalize;
        object_class->get_property = net_virtual_device_get_property;
        object_class->set_property = net_virtual_device_set_property;

        net_object_class->refresh = net_virtual_device_refresh;
        net_object_class->add_to_notebook = net_virtual_device_add_to_notebook;
        net_object_class->delete = net_virtual_device_delete;

        device_class->get_find_connection = net_virtual_device_get_find_connection;

        g_type_class_add_private (klass, sizeof (NetVirtualDevicePrivate));

        signals[SIGNAL_DEVICE_SET] =
                g_signal_new ("device-set",
                              G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
                              G_STRUCT_OFFSET (NetVirtualDeviceClass, device_set),
                              NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
                              G_TYPE_NONE, 1, G_TYPE_OBJECT);

        signals[SIGNAL_DEVICE_UNSET] =
                g_signal_new ("device-unset",
                              G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
                              G_STRUCT_OFFSET (NetVirtualDeviceClass, device_unset),
                              NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
                              G_TYPE_NONE, 1, G_TYPE_OBJECT);

        pspec = g_param_spec_object ("connection", NULL, NULL,
                                     NM_TYPE_CONNECTION,
                                     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
        g_object_class_install_property (object_class, PROP_CONNECTION, pspec);
}

static void
net_virtual_device_init (NetVirtualDevice *virtual_device)
{
        GError *error = NULL;
        GtkWidget *widget;

        virtual_device->priv = NET_VIRTUAL_DEVICE_GET_PRIVATE (virtual_device);

        virtual_device->priv->builder = gtk_builder_new ();
        gtk_builder_add_from_resource (virtual_device->priv->builder,
                                       "/org/gnome/control-center/network/network-simple.ui",
                                       &error);
        if (error != NULL) {
                g_warning ("Could not load interface file: %s", error->message);
                g_error_free (error);
                return;
        }

        widget = GTK_WIDGET (gtk_builder_get_object (virtual_device->priv->builder,
                                                     "label_device"));
        g_object_bind_property (virtual_device, "title", widget, "label", 0);

        widget = GTK_WIDGET (gtk_builder_get_object (virtual_device->priv->builder,
                                                     "device_off_switch"));
        g_signal_connect (widget, "notify::active",
                          G_CALLBACK (device_off_toggled), virtual_device);

        widget = GTK_WIDGET (gtk_builder_get_object (virtual_device->priv->builder,
                                                     "button_options"));
        g_signal_connect (widget, "clicked",
                          G_CALLBACK (edit_connection), virtual_device);
}

void
net_virtual_device_add_row (NetVirtualDevice *virtual_device,
                            const char       *label_string,
                            const char       *property_name)
{
        NetVirtualDevicePrivate *priv = virtual_device->priv;
        GtkGrid *grid;
        GtkWidget *label, *value;
        GtkStyleContext *context;
        gint top_attach;

        grid = GTK_GRID (gtk_builder_get_object (priv->builder, "grid"));

        label = gtk_label_new (label_string);
        gtk_widget_set_halign (label, GTK_ALIGN_END);
        gtk_container_add (GTK_CONTAINER (grid), label);

        context = gtk_widget_get_style_context (label);
        gtk_style_context_add_class (context, "dim-label");
        gtk_widget_show (label);

        gtk_container_child_get (GTK_CONTAINER (grid), label,
                                 "top-attach", &top_attach,
                                 NULL);

        value = gtk_label_new (NULL);
        gtk_widget_set_halign (value, GTK_ALIGN_START);
        g_object_bind_property (virtual_device, property_name, value, "label", 0);
        gtk_label_set_mnemonic_widget (GTK_LABEL (label), value);
        gtk_grid_attach (grid, value, 1, top_attach, 1, 1);
        gtk_widget_show (value);
}