[PATCH] libnm-glib, cli: add 'available-connections' property to libnm-glib and nmcli



Hello,

Two patches for adding 'available-connections' property of NMDevice to
* libnm-glib
* nmcli

Jirka
>From 44240b646fac7bc5093c58e6f1cb8cb09433dcf5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= <jklimes redhat com>
Date: Wed, 22 Aug 2012 14:38:24 +0200
Subject: [PATCH 1/2] libnm-glib: add 'available-connections' property to
 NMDevice
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit


Signed-off-by: Jiří Klimeš <jklimes redhat com>
---
 libnm-glib/libnm-glib.ver |  1 +
 libnm-glib/nm-device.c    | 65 +++++++++++++++++++++++++++++++++++++++++++++++
 libnm-glib/nm-device.h    |  2 ++
 3 files changed, 68 insertions(+)

diff --git a/libnm-glib/libnm-glib.ver b/libnm-glib/libnm-glib.ver
index 4bcbfb0..bb848a0 100644
--- a/libnm-glib/libnm-glib.ver
+++ b/libnm-glib/libnm-glib.ver
@@ -90,6 +90,7 @@ global:
 	nm_device_filter_connections;
 	nm_device_get_active_connection;
 	nm_device_get_autoconnect;
+	nm_device_get_available_connections;
 	nm_device_get_capabilities;
 	nm_device_get_device_type;
 	nm_device_get_dhcp4_config;
diff --git a/libnm-glib/nm-device.c b/libnm-glib/nm-device.c
index 865fc08..8ee6f86 100644
--- a/libnm-glib/nm-device.c
+++ b/libnm-glib/nm-device.c
@@ -43,6 +43,7 @@
 #include "nm-glib-marshal.h"
 #include "nm-dbus-glib-types.h"
 #include "nm-glib-compat.h"
+#include "nm-types.h"
 
 static GType _nm_device_type_for_path (DBusGConnection *connection,
                                        const char *path);
@@ -82,6 +83,7 @@ typedef struct {
 	NMDeviceStateReason reason;
 
 	NMActiveConnection *active_connection;
+	GPtrArray *available_connections;
 
 	GUdevClient *client;
 	char *product;
@@ -110,6 +112,7 @@ enum {
 	PROP_IP_INTERFACE,
 	PROP_DEVICE_TYPE,
 	PROP_ACTIVE_CONNECTION,
+	PROP_AVAILABLE_CONNECTIONS,
 
 	LAST_PROP
 };
@@ -149,6 +152,26 @@ demarshal_state_reason (NMObject *object, GParamSpec *pspec, GValue *value, gpoi
 	return TRUE;
 }
 
+static gboolean
+demarshal_available_connections (NMObject *object, GParamSpec *pspec, GValue *value, gpointer field)
+{
+	NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (object);
+
+	if (value && G_VALUE_HOLDS (value, DBUS_TYPE_G_ARRAY_OF_OBJECT_PATH)) {
+		GPtrArray *paths = g_value_get_boxed (value);
+		int i;
+
+		priv->available_connections = g_ptr_array_sized_new (paths->len);
+
+		for (i = 0; paths && (i < paths->len); i++)
+			g_ptr_array_add (priv->available_connections, g_strdup (g_ptr_array_index (paths, i)));
+	} else
+		return FALSE;
+
+	_nm_object_queue_notify (object, NM_DEVICE_AVAILABLE_CONNECTIONS);
+	return TRUE;
+}
+
 static void
 register_properties (NMDevice *device)
 {
@@ -171,6 +194,7 @@ register_properties (NMDevice *device)
 		{ NM_DEVICE_STATE,             &priv->state },
 		{ NM_DEVICE_STATE_REASON,      &priv->state, demarshal_state_reason },
 		{ NM_DEVICE_ACTIVE_CONNECTION, &priv->active_connection, NULL, NM_TYPE_ACTIVE_CONNECTION },
+		{ NM_DEVICE_AVAILABLE_CONNECTIONS, &priv->available_connections, demarshal_available_connections },
 
 		/* Properties that exist in D-Bus but that we don't track */
 		{ "ip4-address", NULL },
@@ -326,6 +350,9 @@ finalize (GObject *object)
 	g_free (priv->product);
 	g_free (priv->vendor);
 
+	if (priv->available_connections)
+		g_ptr_array_free (priv->available_connections, TRUE);
+
 	G_OBJECT_CLASS (nm_device_parent_class)->finalize (object);
 }
 
@@ -400,6 +427,9 @@ get_property (GObject *object,
 	case PROP_ACTIVE_CONNECTION:
 		g_value_set_object (value, nm_device_get_active_connection (device));
 		break;
+	case PROP_AVAILABLE_CONNECTIONS:
+		g_value_set_boxed (value, nm_device_get_available_connections (device));
+		break;
 	case PROP_PRODUCT:
 		g_value_set_string (value, nm_device_get_product (device));
 		break;
@@ -720,6 +750,20 @@ nm_device_class_init (NMDeviceClass *device_class)
 						  NULL,
 						  G_PARAM_READABLE));
 
+	/**
+	 * NMDevice:available-connections:
+	 *
+	 * The connections available for this device.
+	 * Type: GPtrArray<char*>
+	 **/
+	g_object_class_install_property
+		(object_class, PROP_AVAILABLE_CONNECTIONS,
+		 g_param_spec_boxed (NM_DEVICE_AVAILABLE_CONNECTIONS,
+		                     "Available Connections",
+		                     "Available connections for the device",
+		                     DBUS_TYPE_G_ARRAY_OF_OBJECT_PATH,
+		                     G_PARAM_READABLE));
+
 	/* signals */
 
 	/**
@@ -1233,6 +1277,27 @@ nm_device_get_active_connection (NMDevice *device)
 	return NM_DEVICE_GET_PRIVATE (device)->active_connection;
 }
 
+/**
+ * nm_device_get_available_connections:
+ * @device: a #NMDevice
+ *
+ * Gets the available connections for the device.
+ *
+ * Returns: (transfer none) (element-type char*): a #GPtrArray
+ * containing object paths of all the available #NMConnection<!-- -->s.
+ *
+ * The returned array is owned by the device and should not be modified.
+ **/
+const GPtrArray *
+nm_device_get_available_connections (NMDevice *device)
+{
+	g_return_val_if_fail (NM_IS_DEVICE (device), NULL);
+
+	_nm_object_ensure_inited (NM_OBJECT (device));
+
+	return handle_ptr_array_return (NM_DEVICE_GET_PRIVATE (device)->available_connections);
+}
+
 /* From hostap, Copyright (c) 2002-2005, Jouni Malinen <jkmaline cc hut fi> */
 
 static int hex2num (char c)
diff --git a/libnm-glib/nm-device.h b/libnm-glib/nm-device.h
index 1726862..c6e70be 100644
--- a/libnm-glib/nm-device.h
+++ b/libnm-glib/nm-device.h
@@ -65,6 +65,7 @@ G_BEGIN_DECLS
 #define NM_DEVICE_ACTIVE_CONNECTION "active-connection"
 #define NM_DEVICE_VENDOR "vendor"
 #define NM_DEVICE_PRODUCT "product"
+#define NM_DEVICE_AVAILABLE_CONNECTIONS "available-connections"
 
 typedef struct {
 	NMObject parent;
@@ -117,6 +118,7 @@ NMDeviceState        nm_device_get_state_reason     (NMDevice *device, NMDeviceS
 NMActiveConnection * nm_device_get_active_connection(NMDevice *device);
 const char *         nm_device_get_product          (NMDevice *device);
 const char *         nm_device_get_vendor           (NMDevice *device);
+const GPtrArray *    nm_device_get_available_connections (NMDevice *device);
 
 typedef void (*NMDeviceDeactivateFn) (NMDevice *device, GError *error, gpointer user_data);
 
-- 
1.7.11.4

>From ffdb14a54302d4cd5058c6247ef4700d0e092106 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= <jklimes redhat com>
Date: Fri, 31 Aug 2012 12:48:27 +0200
Subject: [PATCH 2/2] cli: print NMDevice's 'available-connections' property
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit


Signed-off-by: Jiří Klimeš <jklimes redhat com>
---
 cli/src/devices.c      | 24 +++++++++++++++++++++++-
 libnm-glib/nm-device.c |  3 ++-
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/cli/src/devices.c b/cli/src/devices.c
index 3d58679..7421ba3 100644
--- a/cli/src/devices.c
+++ b/cli/src/devices.c
@@ -119,9 +119,11 @@ static NmcOutputField nmc_fields_dev_list_general[] = {
 	{"AUTOCONNECT",       N_("AUTOCONNECT"),       15, NULL, 0},  /* 14 */
 	{"FIRMWARE-MISSING",  N_("FIRMWARE-MISSING"),  18, NULL, 0},  /* 15 */
 	{"CONNECTION",        N_("CONNECTION"),        51, NULL, 0},  /* 16 */
+	{"AVAIL-CON-PATHS",   N_("AVAIL-CON-PATHS"),   80, NULL, 0},  /* 17 */
 	{NULL, NULL, 0, NULL, 0}
 };
-#define NMC_FIELDS_DEV_LIST_GENERAL_ALL     "NAME,DEVICE,TYPE,VENDOR,PRODUCT,DRIVER,DRIVER-VERSION,FIRMWARE-VERSION,HWADDR,STATE,REASON,UDI,IP-IFACE,NM-MANAGED,AUTOCONNECT,FIRMWARE-MISSING,CONNECTION"
+#define NMC_FIELDS_DEV_LIST_GENERAL_ALL     "NAME,DEVICE,TYPE,VENDOR,PRODUCT,DRIVER,DRIVER-VERSION,FIRMWARE-VERSION,HWADDR,STATE,REASON,UDI,IP-IFACE,"\
+                                            "NM-MANAGED,AUTOCONNECT,FIRMWARE-MISSING,CONNECTION,AVAIL-CON-PATHS"
 #define NMC_FIELDS_DEV_LIST_GENERAL_COMMON  "NAME,DEVICE,TYPE,VENDOR,PRODUCT,DRIVER,HWADDR,STATE"
 
 /* Available fields for 'dev list' - CAPABILITIES part */
@@ -548,6 +550,10 @@ show_device_info (gpointer data, gpointer user_data)
 
 		/* section GENERAL */
 		if (!strcasecmp (nmc_fields_dev_list_sections[section_idx].name, nmc_fields_dev_list_sections[0].name)) {
+			const GPtrArray *avail_cons;
+			GString *ac_paths_str;
+			int i;
+
 			nmc->allowed_fields = nmc_fields_dev_list_general;
 			nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_FIELD_NAMES;
 			nmc->print_fields.indices = parse_output_fields (NMC_FIELDS_DEV_LIST_GENERAL_ALL, nmc->allowed_fields, NULL);
@@ -571,6 +577,20 @@ show_device_info (gpointer data, gpointer user_data)
 			state_str = g_strdup_printf ("%d (%s)", state, nmc_device_state_to_string (state));
 			reason_str = g_strdup_printf ("%d (%s)", reason, nmc_device_reason_to_string (reason));
 
+			avail_cons = nm_device_get_available_connections (device);
+			ac_paths_str = g_string_new (NULL);
+			for (i = 0; avail_cons && (i < avail_cons->len); i++) {
+				const char *con_path = g_ptr_array_index (avail_cons, i);
+
+				if (i == 0)
+					g_string_printf (ac_paths_str, "%s/{", NM_DBUS_PATH_SETTINGS);
+				else
+					g_string_append_c (ac_paths_str, ',');
+				g_string_append (ac_paths_str, strrchr(con_path, '/') + 1);
+			}
+			if (ac_paths_str->len > 0)
+				g_string_append_c (ac_paths_str, '}');
+
 			nmc->allowed_fields[0].value = nmc_fields_dev_list_sections[0].name;  /* "GENERAL"*/
 			nmc->allowed_fields[1].value = nm_device_get_iface (device);
 			nmc->allowed_fields[2].value = device_type_to_string (device);
@@ -589,11 +609,13 @@ show_device_info (gpointer data, gpointer user_data)
 			nmc->allowed_fields[15].value = nm_device_get_firmware_missing (device) ? _("yes") : _("no");
 			nmc->allowed_fields[16].value = (acon = nm_device_get_active_connection (device)) ?
 			                                   nm_object_get_path (NM_OBJECT (acon)) : _("not connected");
+			nmc->allowed_fields[17].value = ac_paths_str->str;
 
 			nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_SECTION_PREFIX;
 			print_fields (nmc->print_fields, nmc->allowed_fields); /* Print values */
 			g_free (state_str);
 			g_free (reason_str);
+			g_string_free (ac_paths_str, TRUE);
 			was_output = TRUE;
 		}
 
diff --git a/libnm-glib/nm-device.c b/libnm-glib/nm-device.c
index 8ee6f86..45b5787 100644
--- a/libnm-glib/nm-device.c
+++ b/libnm-glib/nm-device.c
@@ -1284,7 +1284,8 @@ nm_device_get_active_connection (NMDevice *device)
  * Gets the available connections for the device.
  *
  * Returns: (transfer none) (element-type char*): a #GPtrArray
- * containing object paths of all the available #NMConnection<!-- -->s.
+ * containing object paths of all the available #NMConnection<!-- -->s
+ * for the device.
  *
  * The returned array is owned by the device and should not be modified.
  **/
-- 
1.7.11.4



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