[gnome-control-center] network: add "virtual device" support, for bonds, bridges, and vlans



commit 52d28579d29534515a82e39710c1b6318ee90f1e
Author: Dan Winship <danw gnome org>
Date:   Tue Jan 8 08:47:14 2013 -0500

    network: add "virtual device" support, for bonds, bridges, and vlans
    
    Bond, bridge, and VLAN devices may not actually exist until their
    connections are brought up. So for those types, create device items
    (of type NetVirtualDevice or a subclass) as soon as we see the
    NMConnection, and then watch for the NMDevice being added later.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=677145

 panels/network/Makefile.am          |    4 +
 panels/network/cc-network-panel.c   |   84 +++++++-
 panels/network/net-device-bond.c    |   48 +++--
 panels/network/net-device-bond.h    |    6 +-
 panels/network/net-device-bridge.c  |  190 ++++++++++++++++
 panels/network/net-device-bridge.h  |   58 +++++
 panels/network/net-device-simple.c  |    2 +-
 panels/network/net-device.c         |   22 ++-
 panels/network/net-device.h         |    2 +
 panels/network/net-virtual-device.c |  429 +++++++++++++++++++++++++++++++++++
 panels/network/net-virtual-device.h |   68 ++++++
 11 files changed, 883 insertions(+), 30 deletions(-)
---
diff --git a/panels/network/Makefile.am b/panels/network/Makefile.am
index 00a0514..1b61259 100644
--- a/panels/network/Makefile.am
+++ b/panels/network/Makefile.am
@@ -34,6 +34,10 @@ libnetwork_la_SOURCES =					\
 	net-device-mobile.h				\
 	net-device-bond.c				\
 	net-device-bond.h				\
+	net-device-bridge.c				\
+	net-device-bridge.h				\
+	net-virtual-device.c				\
+	net-virtual-device.h				\
 	net-vpn.c					\
 	net-vpn.h					\
 	net-proxy.c					\
diff --git a/panels/network/cc-network-panel.c b/panels/network/cc-network-panel.c
index a7ba9be..ace0b0f 100644
--- a/panels/network/cc-network-panel.c
+++ b/panels/network/cc-network-panel.c
@@ -30,15 +30,17 @@
 #include "nm-client.h"
 #include "nm-device.h"
 #include "nm-device-modem.h"
-#include <libnm-gtk/nm-ui-utils.h>
+#include "nm-ui-utils.h"
 
 #include "net-device.h"
 #include "net-device-mobile.h"
 #include "net-device-wifi.h"
 #include "net-device-ethernet.h"
 #include "net-device-bond.h"
+#include "net-device-bridge.h"
 #include "net-object.h"
 #include "net-proxy.h"
+#include "net-virtual-device.h"
 #include "net-vpn.h"
 
 #include "rfkill-glib.h"
@@ -655,8 +657,9 @@ panel_add_device (CcNetworkPanel *panel, NMDevice *device)
                 device_g_type = NET_TYPE_DEVICE_WIFI;
                 break;
         case NM_DEVICE_TYPE_BOND:
-                device_g_type = NET_TYPE_DEVICE_BOND;
-                break;
+        case NM_DEVICE_TYPE_BRIDGE:
+        case NM_DEVICE_TYPE_VLAN:
+                goto out;
         default:
                 device_g_type = NET_TYPE_DEVICE_SIMPLE;
                 break;
@@ -1051,21 +1054,90 @@ panel_add_vpn_device (CcNetworkPanel *panel, NMConnection *connection)
 }
 
 static void
+panel_add_virtual_device (CcNetworkPanel *panel, NMConnection *connection)
+{
+        gchar *title;
+        GtkListStore *liststore_devices;
+        GtkTreeIter iter;
+        NetVirtualDevice *net_virt;
+        const gchar *id;
+        GtkNotebook *notebook;
+        GtkSizeGroup *size_group;
+        NMSettingConnection *s_con;
+        const gchar *connection_type;
+        GType device_g_type;
+
+        /* does already exist */
+        id = nm_connection_get_path (connection);
+        if (find_in_model_by_id (panel, id, NULL) != NULL)
+                return;
+
+        /* map the NMConnection to a NetDevice GType */
+        s_con = nm_connection_get_setting_connection (connection);
+        connection_type = nm_setting_connection_get_connection_type (s_con);
+        if (!strcmp (connection_type, NM_SETTING_BOND_SETTING_NAME))
+                device_g_type = NET_TYPE_DEVICE_BOND;
+        else if (!strcmp (connection_type, NM_SETTING_BRIDGE_SETTING_NAME))
+                device_g_type = NET_TYPE_DEVICE_BRIDGE;
+        else
+                device_g_type = NET_TYPE_VIRTUAL_DEVICE;
+
+        /* add as a virtual object */
+        net_virt = g_object_new (device_g_type,
+                                 "panel", panel,
+                                 "removable", TRUE,
+                                 "id", id,
+                                 "connection", connection,
+                                 "client", panel->priv->client,
+                                 NULL);
+        g_signal_connect_object (net_virt, "removed",
+                                 G_CALLBACK (object_removed_cb), panel, 0);
+
+        /* add as a panel */
+        notebook = GTK_NOTEBOOK (gtk_builder_get_object (panel->priv->builder,
+                                                         "notebook_types"));
+        size_group = GTK_SIZE_GROUP (gtk_builder_get_object (panel->priv->builder,
+                                                             "sizegroup1"));
+        net_object_add_to_notebook (NET_OBJECT (net_virt),
+                                    notebook,
+                                    size_group);
+
+        liststore_devices = GTK_LIST_STORE (gtk_builder_get_object (panel->priv->builder,
+                                            "liststore_devices"));
+        title = nma_utils_get_connection_device_name (connection);
+
+        net_object_set_title (NET_OBJECT (net_virt), title);
+        gtk_list_store_append (liststore_devices, &iter);
+        gtk_list_store_set (liststore_devices,
+                            &iter,
+                            PANEL_DEVICES_COLUMN_ICON, "network-wired",
+                            PANEL_DEVICES_COLUMN_SORT, "2",
+                            PANEL_DEVICES_COLUMN_OBJECT, net_virt,
+                            -1);
+        g_free (title);
+}
+
+static void
 add_connection (CcNetworkPanel *panel,
                 NMConnection *connection)
 {
         NMSettingConnection *s_con;
-        const gchar *type;
+        const gchar *type, *iface;
 
         s_con = NM_SETTING_CONNECTION (nm_connection_get_setting (connection,
                                                                   NM_TYPE_SETTING_CONNECTION));
         type = nm_setting_connection_get_connection_type (s_con);
-        if (g_strcmp0 (type, "vpn") != 0)
+        iface = nm_connection_get_virtual_iface_name (connection);
+        if (g_strcmp0 (type, "vpn") != 0 && iface == NULL)
                 return;
+
         g_debug ("add %s/%s remote connection: %s",
                  type, g_type_name_from_instance ((GTypeInstance*)connection),
                  nm_connection_get_path (connection));
-        panel_add_vpn_device (panel, connection);
+        if (iface)
+                panel_add_virtual_device (panel, connection);
+        else
+                panel_add_vpn_device (panel, connection);
 }
 
 static void
diff --git a/panels/network/net-device-bond.c b/panels/network/net-device-bond.c
index 9df0538..5fea872 100644
--- a/panels/network/net-device-bond.c
+++ b/panels/network/net-device-bond.c
@@ -46,7 +46,7 @@ enum {
         PROP_LAST
 };
 
-G_DEFINE_TYPE (NetDeviceBond, net_device_bond, NET_TYPE_DEVICE_SIMPLE)
+G_DEFINE_TYPE (NetDeviceBond, net_device_bond, NET_TYPE_VIRTUAL_DEVICE)
 
 static void
 net_device_bond_get_property (GObject *object,
@@ -69,6 +69,17 @@ net_device_bond_get_property (GObject *object,
 }
 
 static void
+net_device_bond_constructed (GObject *object)
+{
+        NetDeviceBond *device_bond = NET_DEVICE_BOND (object);
+
+        net_virtual_device_add_row (NET_VIRTUAL_DEVICE (device_bond),
+                                    _("Bond slaves"), "slaves");
+
+        G_OBJECT_CLASS (net_device_bond_parent_class)->constructed (object);
+}
+
+static void
 nm_device_slaves_changed (GObject    *object,
                           GParamSpec *pspec,
                           gpointer    user_data)
@@ -117,21 +128,26 @@ nm_device_slaves_changed (GObject    *object,
 }
 
 static void
-net_device_bond_constructed (GObject *object)
+net_device_bond_device_set (NetVirtualDevice *virtual_device,
+                            NMDevice         *nm_device)
 {
-        NetDeviceBond *device_bond = NET_DEVICE_BOND (object);
-        NMDevice *nm_device;
-
-        nm_device = net_device_get_nm_device (NET_DEVICE (device_bond));
-        if (nm_device) {
-                g_signal_connect (nm_device, "notify::slaves",
-                                  G_CALLBACK (nm_device_slaves_changed), device_bond);
-                nm_device_slaves_changed (G_OBJECT (nm_device), NULL, device_bond);
-                net_device_simple_add_row (NET_DEVICE_SIMPLE (device_bond),
-                                           _("Bond slaves"), "slaves");
-        }
+        NetDeviceBond *device_bond = NET_DEVICE_BOND (virtual_device);
 
-        G_OBJECT_CLASS (net_device_bond_parent_class)->constructed (object);
+        g_signal_connect (nm_device, "notify::slaves",
+                          G_CALLBACK (nm_device_slaves_changed), device_bond);
+        nm_device_slaves_changed (G_OBJECT (nm_device), NULL, device_bond);
+}
+
+static void
+net_device_bond_device_unset (NetVirtualDevice *virtual_device,
+                              NMDevice         *nm_device)
+{
+        NetDeviceBond *device_bond = NET_DEVICE_BOND (virtual_device);
+
+        g_signal_handlers_disconnect_by_func (nm_device,
+                                              G_CALLBACK (nm_device_slaves_changed),
+                                              device_bond);
+        nm_device_slaves_changed (G_OBJECT (nm_device), NULL, device_bond);
 }
 
 static void
@@ -149,12 +165,16 @@ static void
 net_device_bond_class_init (NetDeviceBondClass *klass)
 {
         GObjectClass *object_class = G_OBJECT_CLASS (klass);
+        NetVirtualDeviceClass *virtual_device_class = NET_VIRTUAL_DEVICE_CLASS (klass);
         GParamSpec *pspec;
 
         object_class->constructed = net_device_bond_constructed;
         object_class->finalize = net_device_bond_finalize;
         object_class->get_property = net_device_bond_get_property;
 
+        virtual_device_class->device_set = net_device_bond_device_set;
+        virtual_device_class->device_unset = net_device_bond_device_unset;
+
         pspec = g_param_spec_string ("slaves", NULL, NULL,
                                      NULL,
                                      G_PARAM_READABLE);
diff --git a/panels/network/net-device-bond.h b/panels/network/net-device-bond.h
index f79689e..cb4c550 100644
--- a/panels/network/net-device-bond.h
+++ b/panels/network/net-device-bond.h
@@ -24,7 +24,7 @@
 
 #include <glib-object.h>
 
-#include "net-device-simple.h"
+#include "net-virtual-device.h"
 
 G_BEGIN_DECLS
 
@@ -41,13 +41,13 @@ typedef struct _NetDeviceBondClass     NetDeviceBondClass;
 
 struct _NetDeviceBond
 {
-         NetDeviceSimple                parent;
+         NetVirtualDevice               parent;
          NetDeviceBondPrivate          *priv;
 };
 
 struct _NetDeviceBondClass
 {
-        NetDeviceSimpleClass             parent_class;
+        NetVirtualDeviceClass            parent_class;
 };
 
 GType            net_device_bond_get_type      (void);
diff --git a/panels/network/net-device-bridge.c b/panels/network/net-device-bridge.c
new file mode 100644
index 0000000..ad850e9
--- /dev/null
+++ b/panels/network/net-device-bridge.c
@@ -0,0 +1,190 @@
+/* -*- 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-device-bridge.h>
+#include <nm-remote-connection.h>
+
+#include "panel-common.h"
+#include "cc-network-panel.h"
+
+#include "net-device-bridge.h"
+
+#define NET_DEVICE_BRIDGE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NET_TYPE_DEVICE_BRIDGE, NetDeviceBridgePrivate))
+
+struct _NetDeviceBridgePrivate {
+        char *slaves;
+};
+
+enum {
+        PROP_0,
+        PROP_SLAVES,
+        PROP_LAST
+};
+
+G_DEFINE_TYPE (NetDeviceBridge, net_device_bridge, NET_TYPE_VIRTUAL_DEVICE)
+
+static void
+net_device_bridge_get_property (GObject *object,
+                              guint prop_id,
+                              GValue *value,
+                              GParamSpec *pspec)
+{
+        NetDeviceBridge *device_bridge = NET_DEVICE_BRIDGE (object);
+        NetDeviceBridgePrivate *priv = device_bridge->priv;
+
+        switch (prop_id) {
+        case PROP_SLAVES:
+                g_value_set_string (value, priv->slaves);
+                break;
+        default:
+                G_OBJECT_WARN_INVALID_PROPERTY_ID (device_bridge, prop_id, pspec);
+                break;
+
+        }
+}
+
+static void
+net_device_bridge_constructed (GObject *object)
+{
+        NetDeviceBridge *device_bridge = NET_DEVICE_BRIDGE (object);
+
+        net_virtual_device_add_row (NET_VIRTUAL_DEVICE (device_bridge),
+                                    _("Bridge slaves"), "slaves");
+
+        G_OBJECT_CLASS (net_device_bridge_parent_class)->constructed (object);
+}
+
+static void
+nm_device_slaves_changed (GObject    *object,
+                          GParamSpec *pspec,
+                          gpointer    user_data)
+{
+        NetDeviceBridge *device_bridge = NET_DEVICE_BRIDGE (user_data);
+        NetDeviceBridgePrivate *priv = device_bridge->priv;
+        NMDeviceBridge *nm_device = NM_DEVICE_BRIDGE (object);
+        CcNetworkPanel *panel;
+        GPtrArray *net_devices;
+        NetDevice *net_device;
+        NMDevice *slave;
+        const GPtrArray *slaves;
+        int i, j;
+        GString *str;
+
+        g_free (priv->slaves);
+
+        slaves = nm_device_bridge_get_slaves (nm_device);
+        if (!slaves) {
+                priv->slaves = g_strdup ("(none)");
+                g_object_notify (G_OBJECT (device_bridge), "slaves");
+                return;
+        }
+
+        panel = net_object_get_panel (NET_OBJECT (device_bridge));
+        net_devices = cc_network_panel_get_devices (panel);
+
+        str = g_string_new (NULL);
+        for (i = 0; i < slaves->len; i++) {
+                if (i > 0)
+                        g_string_append (str, ", ");
+                slave = slaves->pdata[i];
+
+                for (j = 0; j < net_devices->len; j++) {
+                        net_device = net_devices->pdata[j];
+                        if (slave == net_device_get_nm_device (net_device)) {
+                                g_string_append (str, net_object_get_title (NET_OBJECT (net_device)));
+                                break;
+                        }
+                }
+                if (j == net_devices->len)
+                        g_string_append (str, nm_device_get_iface (slave));
+        }
+        priv->slaves = g_string_free (str, FALSE);
+        g_object_notify (G_OBJECT (device_bridge), "slaves");
+}
+
+static void
+net_device_bridge_device_set (NetVirtualDevice *virtual_device,
+                            NMDevice         *nm_device)
+{
+        NetDeviceBridge *device_bridge = NET_DEVICE_BRIDGE (virtual_device);
+
+        g_signal_connect (nm_device, "notify::slaves",
+                          G_CALLBACK (nm_device_slaves_changed), device_bridge);
+        nm_device_slaves_changed (G_OBJECT (nm_device), NULL, device_bridge);
+}
+
+static void
+net_device_bridge_device_unset (NetVirtualDevice *virtual_device,
+                              NMDevice         *nm_device)
+{
+        NetDeviceBridge *device_bridge = NET_DEVICE_BRIDGE (virtual_device);
+
+        g_signal_handlers_disconnect_by_func (nm_device,
+                                              G_CALLBACK (nm_device_slaves_changed),
+                                              device_bridge);
+        nm_device_slaves_changed (G_OBJECT (nm_device), NULL, device_bridge);
+}
+
+static void
+net_device_bridge_finalize (GObject *object)
+{
+        NetDeviceBridge *device_bridge = NET_DEVICE_BRIDGE (object);
+        NetDeviceBridgePrivate *priv = device_bridge->priv;
+
+        g_free (priv->slaves);
+
+        G_OBJECT_CLASS (net_device_bridge_parent_class)->finalize (object);
+}
+
+static void
+net_device_bridge_class_init (NetDeviceBridgeClass *klass)
+{
+        GObjectClass *object_class = G_OBJECT_CLASS (klass);
+        NetVirtualDeviceClass *virtual_device_class = NET_VIRTUAL_DEVICE_CLASS (klass);
+        GParamSpec *pspec;
+
+        object_class->constructed = net_device_bridge_constructed;
+        object_class->finalize = net_device_bridge_finalize;
+        object_class->get_property = net_device_bridge_get_property;
+
+        virtual_device_class->device_set = net_device_bridge_device_set;
+        virtual_device_class->device_unset = net_device_bridge_device_unset;
+
+        pspec = g_param_spec_string ("slaves", NULL, NULL,
+                                     NULL,
+                                     G_PARAM_READABLE);
+        g_object_class_install_property (object_class, PROP_SLAVES, pspec);
+
+        g_type_class_add_private (klass, sizeof (NetDeviceBridgePrivate));
+}
+
+static void
+net_device_bridge_init (NetDeviceBridge *device_bridge)
+{
+        device_bridge->priv = NET_DEVICE_BRIDGE_GET_PRIVATE (device_bridge);
+}
diff --git a/panels/network/net-device-bridge.h b/panels/network/net-device-bridge.h
new file mode 100644
index 0000000..a53ef0d
--- /dev/null
+++ b/panels/network/net-device-bridge.h
@@ -0,0 +1,58 @@
+/* -*- 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.
+ */
+
+#ifndef __NET_DEVICE_BRIDGE_H
+#define __NET_DEVICE_BRIDGE_H
+
+#include <glib-object.h>
+
+#include "net-virtual-device.h"
+
+G_BEGIN_DECLS
+
+#define NET_TYPE_DEVICE_BRIDGE          (net_device_bridge_get_type ())
+#define NET_DEVICE_BRIDGE(o)            (G_TYPE_CHECK_INSTANCE_CAST ((o), NET_TYPE_DEVICE_BRIDGE, NetDeviceBridge))
+#define NET_DEVICE_BRIDGE_CLASS(k)      (G_TYPE_CHECK_CLASS_CAST((k), NET_TYPE_DEVICE_BRIDGE, NetDeviceBridgeClass))
+#define NET_IS_DEVICE_BRIDGE(o)         (G_TYPE_CHECK_INSTANCE_TYPE ((o), NET_TYPE_DEVICE_BRIDGE))
+#define NET_IS_DEVICE_BRIDGE_CLASS(k)   (G_TYPE_CHECK_CLASS_TYPE ((k), NET_TYPE_DEVICE_BRIDGE))
+#define NET_DEVICE_BRIDGE_GET_CLASS(o)  (G_TYPE_INSTANCE_GET_CLASS ((o), NET_TYPE_DEVICE_BRIDGE, NetDeviceBridgeClass))
+
+typedef struct _NetDeviceBridgePrivate   NetDeviceBridgePrivate;
+typedef struct _NetDeviceBridge          NetDeviceBridge;
+typedef struct _NetDeviceBridgeClass     NetDeviceBridgeClass;
+
+struct _NetDeviceBridge
+{
+         NetVirtualDevice               parent;
+         NetDeviceBridgePrivate          *priv;
+};
+
+struct _NetDeviceBridgeClass
+{
+        NetVirtualDeviceClass            parent_class;
+};
+
+GType            net_device_bridge_get_type      (void);
+
+G_END_DECLS
+
+#endif /* __NET_DEVICE_BRIDGE_H */
+
diff --git a/panels/network/net-device-simple.c b/panels/network/net-device-simple.c
index 2deeab5..2ebac02 100644
--- a/panels/network/net-device-simple.c
+++ b/panels/network/net-device-simple.c
@@ -207,7 +207,7 @@ net_device_simple_constructed (GObject *object)
 
         G_OBJECT_CLASS (net_device_simple_parent_class)->constructed (object);
 
-        nm_device_simple_refresh_ui (device_simple);
+        net_object_refresh (NET_OBJECT (device_simple));
 }
 
 static void
diff --git a/panels/network/net-device.c b/panels/network/net-device.c
index 0f5a0c0..453946f 100644
--- a/panels/network/net-device.c
+++ b/panels/network/net-device.c
@@ -194,8 +194,8 @@ valid_connections_for_device (NMRemoteSettings *remote_settings,
         return g_slist_reverse (valid);
 }
 
-NMConnection *
-net_device_get_find_connection (NetDevice *device)
+static NMConnection *
+net_device_real_get_find_connection (NetDevice *device)
 {
         GSList *list, *iterator;
         NMConnection *connection = NULL;
@@ -236,6 +236,12 @@ out:
         return connection;
 }
 
+NMConnection *
+net_device_get_find_connection (NetDevice *device)
+{
+        return NET_DEVICE_GET_CLASS (device)->get_find_connection (device);
+}
+
 static void
 state_changed_cb (NMDevice *device,
                   NMDeviceState new_state,
@@ -314,10 +320,13 @@ net_device_set_property (GObject *device_,
                                                      priv->changed_id);
                 }
                 priv->nm_device = g_value_dup_object (value);
-                priv->changed_id = g_signal_connect (priv->nm_device,
-                                                     "state-changed",
-                                                     G_CALLBACK (state_changed_cb),
-                                                     net_device);
+                if (priv->nm_device) {
+                        priv->changed_id = g_signal_connect (priv->nm_device,
+                                                             "state-changed",
+                                                             G_CALLBACK (state_changed_cb),
+                                                             net_device);
+                } else
+                        priv->changed_id = 0;
                 break;
         default:
                 G_OBJECT_WARN_INVALID_PROPERTY_ID (net_device, prop_id, pspec);
@@ -352,6 +361,7 @@ net_device_class_init (NetDeviceClass *klass)
         object_class->get_property = net_device_get_property;
         object_class->set_property = net_device_set_property;
         parent_class->edit = net_device_edit;
+        klass->get_find_connection = net_device_real_get_find_connection;
 
         pspec = g_param_spec_object ("nm-device", NULL, NULL,
                                      NM_TYPE_DEVICE,
diff --git a/panels/network/net-device.h b/panels/network/net-device.h
index b3951e5..f8e9dd0 100644
--- a/panels/network/net-device.h
+++ b/panels/network/net-device.h
@@ -49,6 +49,8 @@ struct _NetDevice
 struct _NetDeviceClass
 {
         NetObjectClass               parent_class;
+
+        NMConnection * (*get_find_connection) (NetDevice *device);
 };
 
 GType            net_device_get_type                    (void);
diff --git a/panels/network/net-virtual-device.c b/panels/network/net-virtual-device.c
new file mode 100644
index 0000000..bb32c3e
--- /dev/null
+++ b/panels/network/net-virtual-device.c
@@ -0,0 +1,429 @@
+/* -*- 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
+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);
+        NetVirtualDevicePrivate *priv = virtual_device->priv;
+
+        switch (prop_id) {
+        case PROP_CONNECTION:
+                priv->connection = g_value_dup_object (value);
+                priv->iface = nm_connection_get_virtual_iface_name (priv->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;
+        GtkWindow *window;
+
+        /* 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);
+
+        /* reparent */
+        window = GTK_WINDOW (gtk_builder_get_object (virtual_device->priv->builder,
+                                                     "window_tmp"));
+        widget = GTK_WIDGET (gtk_builder_get_object (virtual_device->priv->builder,
+                                                     "vbox6"));
+        g_object_ref (widget);
+        gtk_container_remove (GTK_CONTAINER (window), widget);
+        gtk_notebook_append_page (notebook, widget, NULL);
+        g_object_unref (widget);
+        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;
+        const char *status, *tooltip;
+        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 */
+        widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "label_status"));
+        if (nm_device) {
+                status = panel_device_state_to_localized_string (nm_device);
+                tooltip = panel_device_state_reason_to_localized_string (nm_device);
+        } else {
+                status = _("Disconnected");
+                tooltip = NULL;
+        }
+        gtk_label_set_label (GTK_LABEL (widget), status);
+        gtk_widget_set_tooltip_text (widget, tooltip);
+
+        /* 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
+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 (client, "device-added",
+                          G_CALLBACK (device_added_cb), virtual_device);
+        g_signal_connect (client, "device-removed",
+                          G_CALLBACK (device_removed_cb), virtual_device);
+        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;
+
+        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_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
+        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_misc_set_alignment (GTK_MISC (value), 0.0, 0.5);
+        g_object_bind_property (virtual_device, property_name, value, "label", 0);
+        gtk_grid_attach (grid, value, 1, top_attach, 1, 1);
+        gtk_widget_show (value);
+}
diff --git a/panels/network/net-virtual-device.h b/panels/network/net-virtual-device.h
new file mode 100644
index 0000000..fcd51a7
--- /dev/null
+++ b/panels/network/net-virtual-device.h
@@ -0,0 +1,68 @@
+/* -*- 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.
+ */
+
+#ifndef __NET_VIRTUAL_DEVICE_H
+#define __NET_VIRTUAL_DEVICE_H
+
+#include <glib-object.h>
+
+#include "net-device.h"
+
+G_BEGIN_DECLS
+
+#define NET_TYPE_VIRTUAL_DEVICE          (net_virtual_device_get_type ())
+#define NET_VIRTUAL_DEVICE(o)            (G_TYPE_CHECK_INSTANCE_CAST ((o), NET_TYPE_VIRTUAL_DEVICE, NetVirtualDevice))
+#define NET_VIRTUAL_DEVICE_CLASS(k)      (G_TYPE_CHECK_CLASS_CAST((k), NET_TYPE_VIRTUAL_DEVICE, NetVirtualDeviceClass))
+#define NET_IS_VIRTUAL_DEVICE(o)         (G_TYPE_CHECK_INSTANCE_TYPE ((o), NET_TYPE_VIRTUAL_DEVICE))
+#define NET_IS_VIRTUAL_DEVICE_CLASS(k)   (G_TYPE_CHECK_CLASS_TYPE ((k), NET_TYPE_VIRTUAL_DEVICE))
+#define NET_VIRTUAL_DEVICE_GET_CLASS(o)  (G_TYPE_INSTANCE_GET_CLASS ((o), NET_TYPE_VIRTUAL_DEVICE, NetVirtualDeviceClass))
+
+typedef struct _NetVirtualDevicePrivate   NetVirtualDevicePrivate;
+typedef struct _NetVirtualDevice          NetVirtualDevice;
+typedef struct _NetVirtualDeviceClass     NetVirtualDeviceClass;
+
+struct _NetVirtualDevice
+{
+	NetDevice                parent;
+	NetVirtualDevicePrivate *priv;
+};
+
+struct _NetVirtualDeviceClass
+{
+	NetDeviceClass           parent_class;
+
+        /* signals */
+        void (*device_set)   (NetVirtualDevice *virtual_device,
+                              NMDevice         *nm_device);
+        void (*device_unset) (NetVirtualDevice *virtual_device,
+                              NMDevice         *nm_device);
+};
+
+GType net_virtual_device_get_type (void);
+
+void  net_virtual_device_add_row  (NetVirtualDevice *virtual_device,
+                                   const char       *label_string,
+                                   const char       *property_name);
+
+G_END_DECLS
+
+#endif /* __NET_VIRTUAL_DEVICE_H */
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]