[PATCH] libnm-glib: add StateReason property for NMDevice



The patch adds "StateReason" property to libnm-glib's NMDevice
and a getter function nm_device_get_state_reason().

Jirka
>From b7c3fde18a11ff9c9eae8476842001071d8394a1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= <jklimes redhat com>
Date: Tue, 7 Feb 2012 10:48:16 +0100
Subject: [PATCH] libnm-glib: add StateReason property for 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/nm-device.c |   93 +++++++++++++++++++++++++++++++++++++++++++++---
 libnm-glib/nm-device.h |   17 ++++++++-
 2 files changed, 104 insertions(+), 6 deletions(-)

diff --git a/libnm-glib/nm-device.c b/libnm-glib/nm-device.c
index 2a1e58f..19fb683 100644
--- a/libnm-glib/nm-device.c
+++ b/libnm-glib/nm-device.c
@@ -18,7 +18,7 @@
  * Boston, MA 02110-1301 USA.
  *
  * Copyright (C) 2007 - 2008 Novell, Inc.
- * Copyright (C) 2007 - 2011 Red Hat, Inc.
+ * Copyright (C) 2007 - 2012 Red Hat, Inc.
  */
 
 #include <string.h>
@@ -53,6 +53,34 @@ G_DEFINE_TYPE_WITH_CODE (NMDevice, nm_device, NM_TYPE_OBJECT,
 
 #define NM_DEVICE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE, NMDevicePrivate))
 
+#define DBUS_G_TYPE_UINT_STRUCT (dbus_g_type_get_struct ("GValueArray", G_TYPE_UINT, G_TYPE_UINT, G_TYPE_INVALID))
+#define NM_TYPE_STATE_REASON (_nm_state_reason_get_type ())
+
+static GType _nm_state_reason_get_type (void) G_GNUC_CONST;
+
+static gpointer
+_nm_state_reason_copy (NMDeviceStateReasonStruct *src)
+{
+	NMDeviceStateReasonStruct *dest;
+
+	dest = g_malloc (sizeof (NMDeviceStateReasonStruct));
+	*dest = *src;
+	return dest;
+}
+
+static GType
+_nm_state_reason_get_type (void)
+{
+	static GType our_type = 0;
+
+	if (our_type == 0)
+		our_type = g_boxed_type_register_static (g_intern_static_string ("NMStateReason"),
+		                                         (GBoxedCopyFunc) _nm_state_reason_copy,
+		                                         (GBoxedFreeFunc) g_free);
+	return our_type;
+}
+
+
 typedef struct {
 	gboolean disposed;
 	DBusGProxy *proxy;
@@ -69,7 +97,7 @@ typedef struct {
 	NMDHCP4Config *dhcp4_config;
 	NMIP6Config *ip6_config;
 	NMDHCP6Config *dhcp6_config;
-	NMDeviceState state;
+	NMDeviceStateReasonStruct state_reason;
 
 	NMActiveConnection *active_connection;
 
@@ -90,6 +118,7 @@ enum {
 	PROP_DHCP4_CONFIG,
 	PROP_IP6_CONFIG,
 	PROP_STATE,
+	PROP_STATE_REASON,
 	PROP_PRODUCT,
 	PROP_VENDOR,
 	PROP_DHCP6_CONFIG,
@@ -114,7 +143,25 @@ nm_device_init (NMDevice *device)
 {
 	NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (device);
 
-	priv->state = NM_DEVICE_STATE_UNKNOWN;
+	priv->state_reason.state = NM_DEVICE_STATE_UNKNOWN;
+	priv->state_reason.reason = NM_DEVICE_STATE_REASON_NONE;
+}
+
+static gboolean
+demarshal_state_reason (NMObject *object, GParamSpec *pspec, GValue *value, gpointer field)
+{
+	NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (object);
+
+	if (!G_VALUE_HOLDS (value, DBUS_G_TYPE_UINT_STRUCT))
+		return FALSE;
+
+	dbus_g_type_struct_get (value,
+	                        0, &priv->state_reason.state,
+	                        1, &priv->state_reason.reason,
+	                        G_MAXUINT);
+
+	_nm_object_queue_notify (object, NM_DEVICE_STATE_REASON);
+	return TRUE;
 }
 
 static void
@@ -133,7 +180,8 @@ register_properties (NMDevice *device)
 		{ NM_DEVICE_DHCP4_CONFIG,      &priv->dhcp4_config, NULL, NM_TYPE_DHCP4_CONFIG },
 		{ NM_DEVICE_IP6_CONFIG,        &priv->ip6_config, NULL, NM_TYPE_IP6_CONFIG },
 		{ NM_DEVICE_DHCP6_CONFIG,      &priv->dhcp6_config, NULL, NM_TYPE_DHCP6_CONFIG },
-		{ NM_DEVICE_STATE,             &priv->state },
+		{ NM_DEVICE_STATE,             &priv->state_reason.state },
+		{ NM_DEVICE_STATE_REASON,      &priv->state_reason, demarshal_state_reason},
 		{ NM_DEVICE_ACTIVE_CONNECTION, &priv->active_connection, NULL, NM_TYPE_ACTIVE_CONNECTION },
 
 		/* The D-Bus interface has this property, but we don't; register
@@ -286,6 +334,9 @@ get_property (GObject *object,
 	case PROP_STATE:
 		g_value_set_uint (value, nm_device_get_state (device));
 		break;
+	case PROP_STATE_REASON:
+		g_value_set_boxed (value, nm_device_get_state_reason (device));
+		break;
 	case PROP_ACTIVE_CONNECTION:
 		g_value_set_object (value, nm_device_get_active_connection (device));
 		break;
@@ -513,6 +564,19 @@ nm_device_class_init (NMDeviceClass *device_class)
 						  G_PARAM_READABLE));
 
 	/**
+	 * NMDevice:state-reason:
+	 *
+	 * The state and reason of the device.
+	 **/
+	g_object_class_install_property
+		(object_class, PROP_STATE_REASON,
+		 g_param_spec_boxed (NM_DEVICE_STATE_REASON,
+		                     "StateReason",
+		                     "StateReason",
+		                     NM_TYPE_STATE_REASON,
+		                     G_PARAM_READABLE));
+
+	/**
 	 * NMDevice:active-connection:
 	 *
 	 * The #NMActiveConnection object that "owns" this device during activation.
@@ -962,7 +1026,26 @@ nm_device_get_state (NMDevice *device)
 	g_return_val_if_fail (NM_IS_DEVICE (device), NM_DEVICE_STATE_UNKNOWN);
 
 	_nm_object_ensure_inited (NM_OBJECT (device));
-	return NM_DEVICE_GET_PRIVATE (device)->state;
+	return NM_DEVICE_GET_PRIVATE (device)->state_reason.state;
+}
+
+/**
+ * nm_device_get_state_reason:
+ * @device: a #NMDevice
+ *
+ * Gets the current #NMDevice state and reason.
+ *
+ * Returns: (transfer none): the #NMDeviceStateReasonStruct containing state
+ * and reason. The elements are #NMDeviceState and #NMDeviceStateReason types.
+ * This is the internal copy used by the device and must not be modified.
+ **/
+const NMDeviceStateReasonStruct *
+nm_device_get_state_reason (NMDevice *device)
+{
+	g_return_val_if_fail (NM_IS_DEVICE (device), NULL);
+
+	_nm_object_ensure_inited (NM_OBJECT (device));
+	return &NM_DEVICE_GET_PRIVATE (device)->state_reason;
 }
 
 /**
diff --git a/libnm-glib/nm-device.h b/libnm-glib/nm-device.h
index e21e71b..8425d3d 100644
--- a/libnm-glib/nm-device.h
+++ b/libnm-glib/nm-device.h
@@ -18,7 +18,7 @@
  * Boston, MA 02110-1301 USA.
  *
  * Copyright (C) 2007 - 2008 Novell, Inc.
- * Copyright (C) 2007 - 2011 Red Hat, Inc.
+ * Copyright (C) 2007 - 2012 Red Hat, Inc.
  */
 
 #ifndef NM_DEVICE_H
@@ -58,10 +58,24 @@ G_BEGIN_DECLS
 #define NM_DEVICE_IP6_CONFIG "ip6-config"
 #define NM_DEVICE_DHCP6_CONFIG "dhcp6-config"
 #define NM_DEVICE_STATE "state"
+#define NM_DEVICE_STATE_REASON "state-reason"
 #define NM_DEVICE_ACTIVE_CONNECTION "active-connection"
 #define NM_DEVICE_VENDOR "vendor"
 #define NM_DEVICE_PRODUCT "product"
 
+/**
+ * NMDeviceStateReasonStruct:
+ * @state: state of the device (#NMDeviceState)
+ * @reason: reason for entering the @state (#NMDeviceStateReason)
+ *
+ * Describes the type of "StateReason" property of the #NMDevice object.
+ *
+ **/
+typedef struct {
+	NMDeviceState state;
+	NMDeviceStateReason reason;
+} NMDeviceStateReasonStruct;
+
 typedef struct {
 	NMObject parent;
 } NMDevice;
@@ -104,6 +118,7 @@ NMDHCP4Config *      nm_device_get_dhcp4_config     (NMDevice *device);
 NMIP6Config *        nm_device_get_ip6_config       (NMDevice *device);
 NMDHCP6Config *      nm_device_get_dhcp6_config     (NMDevice *device);
 NMDeviceState        nm_device_get_state            (NMDevice *device);
+const NMDeviceStateReasonStruct* nm_device_get_state_reason (NMDevice *device);
 NMActiveConnection * nm_device_get_active_connection(NMDevice *device);
 const char *         nm_device_get_product          (NMDevice *device);
 const char *         nm_device_get_vendor           (NMDevice *device);
-- 
1.7.7.6



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