network: add support for showing bond slaves

https://bugzilla.gnome.org/show_bug.cgi?id=677147
This commit is contained in:
Dan Winship 2012-10-18 07:23:42 -04:00 committed by Bastien Nocera
parent cb2eacdfd4
commit 8ba8f2e033
8 changed files with 306 additions and 21 deletions

View file

@ -28,6 +28,8 @@ libnetwork_la_SOURCES = \
net-device-ethernet.h \
net-device-mobile.c \
net-device-mobile.h \
net-device-bond.c \
net-device-bond.h \
net-vpn.c \
net-vpn.h \
net-proxy.c \

View file

@ -35,6 +35,7 @@
#include "net-device-mobile.h"
#include "net-device-wifi.h"
#include "net-device-ethernet.h"
#include "net-device-bond.h"
#include "net-object.h"
#include "net-proxy.h"
#include "net-vpn.h"
@ -383,6 +384,32 @@ panel_refresh_killswitch_visibility (CcNetworkPanel *panel)
show_flight_toggle);
}
GPtrArray *
cc_network_panel_get_devices (CcNetworkPanel *panel)
{
GPtrArray *devices;
GtkTreeModel *model;
GtkTreeIter iter;
NetObject *object;
devices = g_ptr_array_new_with_free_func (g_object_unref);
model = GTK_TREE_MODEL (gtk_builder_get_object (panel->priv->builder,
"liststore_devices"));
if (!gtk_tree_model_get_iter_first (model, &iter))
return devices;
do {
gtk_tree_model_get (model, &iter,
PANEL_DEVICES_COLUMN_OBJECT, &object,
-1);
if (NET_IS_DEVICE (object))
g_ptr_array_add (devices, object);
} while (gtk_tree_model_iter_next (model, &iter));
return devices;
}
static void
panel_refresh_device_titles (CcNetworkPanel *panel)
{
@ -391,32 +418,17 @@ panel_refresh_device_titles (CcNetworkPanel *panel)
NMDevice **nm_devices;
gchar **titles;
gint i, num_devices;
NetObject *object;
GtkTreeIter iter;
GtkTreeModel *model;
model = GTK_TREE_MODEL (gtk_builder_get_object (panel->priv->builder,
"liststore_devices"));
if (!gtk_tree_model_get_iter_first (model, &iter))
return;
ndarray = g_ptr_array_new_with_free_func (g_object_unref);
nmdarray = g_ptr_array_new ();
do {
gtk_tree_model_get (model, &iter,
PANEL_DEVICES_COLUMN_OBJECT, &object,
-1);
if (NET_IS_DEVICE (object)) {
g_ptr_array_add (ndarray, object);
g_ptr_array_add (nmdarray, net_device_get_nm_device (NET_DEVICE (object)));
}
} while (gtk_tree_model_iter_next (model, &iter));
ndarray = cc_network_panel_get_devices (panel);
if (!ndarray->len) {
g_ptr_array_free (ndarray, TRUE);
g_ptr_array_free (nmdarray, TRUE);
return;
}
nmdarray = g_ptr_array_new ();
for (i = 0; i < ndarray->len; i++)
g_ptr_array_add (nmdarray, net_device_get_nm_device (ndarray->pdata[i]));
devices = (NetDevice **)ndarray->pdata;
nm_devices = (NMDevice **)nmdarray->pdata;
num_devices = ndarray->len;
@ -550,6 +562,9 @@ panel_add_device (CcNetworkPanel *panel, NMDevice *device)
case NM_DEVICE_TYPE_WIFI:
device_g_type = NET_TYPE_DEVICE_WIFI;
break;
case NM_DEVICE_TYPE_BOND:
device_g_type = NET_TYPE_DEVICE_BOND;
break;
default:
device_g_type = NET_TYPE_DEVICE_SIMPLE;
break;

View file

@ -67,6 +67,8 @@ GType cc_network_panel_get_type (void) G_GNUC_CONST;
void cc_network_panel_register (GIOModule *module);
GPtrArray *cc_network_panel_get_devices (CcNetworkPanel *panel);
G_END_DECLS
#endif /* _CC_NETWORK_PANEL_H */

View file

@ -0,0 +1,170 @@
/* -*- 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-bond.h>
#include <nm-remote-connection.h>
#include "panel-common.h"
#include "cc-network-panel.h"
#include "net-device-bond.h"
#define NET_DEVICE_BOND_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NET_TYPE_DEVICE_BOND, NetDeviceBondPrivate))
struct _NetDeviceBondPrivate {
char *slaves;
};
enum {
PROP_0,
PROP_SLAVES,
PROP_LAST
};
G_DEFINE_TYPE (NetDeviceBond, net_device_bond, NET_TYPE_DEVICE_SIMPLE)
static void
net_device_bond_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
NetDeviceBond *device_bond = NET_DEVICE_BOND (object);
NetDeviceBondPrivate *priv = device_bond->priv;
switch (prop_id) {
case PROP_SLAVES:
g_value_set_string (value, priv->slaves);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (device_bond, prop_id, pspec);
break;
}
}
static void
nm_device_slaves_changed (GObject *object,
GParamSpec *pspec,
gpointer user_data)
{
NetDeviceBond *device_bond = NET_DEVICE_BOND (user_data);
NetDeviceBondPrivate *priv = device_bond->priv;
NMDeviceBond *nm_device = NM_DEVICE_BOND (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_bond_get_slaves (nm_device);
if (!slaves) {
priv->slaves = g_strdup ("(none)");
g_object_notify (G_OBJECT (device_bond), "slaves");
return;
}
panel = net_object_get_panel (NET_OBJECT (device_bond));
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_bond), "slaves");
}
static void
net_device_bond_constructed (GObject *object)
{
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");
}
G_OBJECT_CLASS (net_device_bond_parent_class)->constructed (object);
}
static void
net_device_bond_finalize (GObject *object)
{
NetDeviceBond *device_bond = NET_DEVICE_BOND (object);
NetDeviceBondPrivate *priv = device_bond->priv;
g_free (priv->slaves);
G_OBJECT_CLASS (net_device_bond_parent_class)->finalize (object);
}
static void
net_device_bond_class_init (NetDeviceBondClass *klass)
{
GObjectClass *object_class = G_OBJECT_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;
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 (NetDeviceBondPrivate));
}
static void
net_device_bond_init (NetDeviceBond *device_bond)
{
device_bond->priv = NET_DEVICE_BOND_GET_PRIVATE (device_bond);
}

View file

@ -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_BOND_H
#define __NET_DEVICE_BOND_H
#include <glib-object.h>
#include "net-device-simple.h"
G_BEGIN_DECLS
#define NET_TYPE_DEVICE_BOND (net_device_bond_get_type ())
#define NET_DEVICE_BOND(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), NET_TYPE_DEVICE_BOND, NetDeviceBond))
#define NET_DEVICE_BOND_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), NET_TYPE_DEVICE_BOND, NetDeviceBondClass))
#define NET_IS_DEVICE_BOND(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), NET_TYPE_DEVICE_BOND))
#define NET_IS_DEVICE_BOND_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), NET_TYPE_DEVICE_BOND))
#define NET_DEVICE_BOND_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), NET_TYPE_DEVICE_BOND, NetDeviceBondClass))
typedef struct _NetDeviceBondPrivate NetDeviceBondPrivate;
typedef struct _NetDeviceBond NetDeviceBond;
typedef struct _NetDeviceBondClass NetDeviceBondClass;
struct _NetDeviceBond
{
NetDeviceSimple parent;
NetDeviceBondPrivate *priv;
};
struct _NetDeviceBondClass
{
NetDeviceSimpleClass parent_class;
};
GType net_device_bond_get_type (void);
G_END_DECLS
#endif /* __NET_DEVICE_BOND_H */

View file

@ -278,3 +278,36 @@ net_device_simple_get_speed (NetDeviceSimple *device_simple)
return klass->get_speed (device_simple);
}
void
net_device_simple_add_row (NetDeviceSimple *device_simple,
const char *label_string,
const char *property_name)
{
NetDeviceSimplePrivate *priv = device_simple->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 (device_simple, property_name, value, "label", 0);
gtk_grid_attach (grid, value, 1, top_attach, 1, 1);
gtk_widget_show (value);
}

View file

@ -57,6 +57,10 @@ GType net_device_simple_get_type (void);
char *net_device_simple_get_speed (NetDeviceSimple *device_simple);
void net_device_simple_add_row (NetDeviceSimple *device_simple,
const char *label,
const char *property_name);
G_END_DECLS
#endif /* __NET_DEVICE_SIMPLE_H */

View file

@ -10,10 +10,11 @@
<property name="border_width">12</property>
<property name="spacing">6</property>
<child>
<object class="GtkGrid" id="grid1">
<object class="GtkGrid" id="grid">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="valign">start</property>
<property name="orientation">vertical</property>
<property name="row_spacing">10</property>
<property name="column_spacing">6</property>
<child>