[PATCH 3/4] Convert NMPolicy into a real GObject.



---
 src/main.c      |    2 +-
 src/nm-policy.c |  389 ++++++++++++++++++++++++++++++++-----------------------
 src/nm-policy.h |   21 +++-
 3 files changed, 246 insertions(+), 166 deletions(-)

diff --git a/src/main.c b/src/main.c
index 328c253..109b352 100644
--- a/src/main.c
+++ b/src/main.c
@@ -778,7 +778,7 @@ main (int argc, char *argv[])
 
 done:
 	if (policy)
-		nm_policy_destroy (policy);
+		g_object_unref (policy);
 
 	if (manager)
 		g_object_unref (manager);
diff --git a/src/nm-policy.c b/src/nm-policy.c
index 20729fd..f84306a 100644
--- a/src/nm-policy.c
+++ b/src/nm-policy.c
@@ -48,7 +48,8 @@
 #include "nm-policy-hostname.h"
 #include "nm-manager-auth.h"
 
-struct NMPolicy {
+typedef struct {
+	gboolean disposed;
 	NMManager *manager;
 	guint update_state_id;
 	GSList *pending_activation_checks;
@@ -71,7 +72,12 @@ struct NMPolicy {
 
 	char *orig_hostname; /* hostname at NM start time */
 	char *cur_hostname;  /* hostname we want to assign */
-};
+} NMPolicyPrivate;
+
+
+G_DEFINE_TYPE (NMPolicy, nm_policy, G_TYPE_OBJECT)
+
+#define NM_POLICY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_POLICY, NMPolicyPrivate))
 
 #define RETRIES_TAG "autoconnect-retries"
 #define RETRIES_DEFAULT	4
@@ -227,23 +233,25 @@ get_best_ip6_device (NMManager *manager, NMActRequest **out_req)
 }
 
 static void
-_set_hostname (NMPolicy *policy,
+_set_hostname (NMPolicy *self,
                gboolean change_hostname,
                const char *new_hostname,
                const char *msg)
 {
+	NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self);
+
 	if (change_hostname) {
 		NMDnsManager *dns_mgr;
 
-		g_free (policy->cur_hostname);
-		policy->cur_hostname = g_strdup (new_hostname);
+		g_free (priv->cur_hostname);
+		priv->cur_hostname = g_strdup (new_hostname);
 
 		dns_mgr = nm_dns_manager_get (NULL);
-		nm_dns_manager_set_hostname (dns_mgr, policy->cur_hostname);
+		nm_dns_manager_set_hostname (dns_mgr, priv->cur_hostname);
 		g_object_unref (dns_mgr);
 	}
 
-	if (nm_policy_set_system_hostname (policy->cur_hostname, msg))
+	if (nm_policy_set_system_hostname (priv->cur_hostname, msg))
 		nm_utils_call_dispatcher ("hostname", NULL, NULL, NULL, NULL, NULL);
 }
 
@@ -253,36 +261,38 @@ lookup_callback (HostnameThread *thread,
                  const char *hostname,
                  gpointer user_data)
 {
-	NMPolicy *policy = (NMPolicy *) user_data;
+	NMPolicy *self = (NMPolicy *) user_data;
+	NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self);
 	char *msg;
 
 	/* Update the hostname if the calling lookup thread is the in-progress one */
-	if (!hostname_thread_is_dead (thread) && (thread == policy->lookup)) {
-		policy->lookup = NULL;
+	if (!hostname_thread_is_dead (thread) && (thread == priv->lookup)) {
+		priv->lookup = NULL;
 		if (!hostname) {
 			/* Fall back to localhost.localdomain */
 			msg = g_strdup_printf ("address lookup failed: %d", result);
-			_set_hostname (policy, TRUE, NULL, msg);
+			_set_hostname (self, TRUE, NULL, msg);
 			g_free (msg);
 		} else
-			_set_hostname (policy, TRUE, hostname, "from address lookup");
+			_set_hostname (self, TRUE, hostname, "from address lookup");
 	}
 	hostname_thread_free (thread);
 }
 
 static void
-update_system_hostname (NMPolicy *policy, NMDevice *best4, NMDevice *best6)
+update_system_hostname (NMPolicy *self, NMDevice *best4, NMDevice *best6)
 {
+	NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self);
 	char *configured_hostname = NULL;
 	NMActRequest *best_req4 = NULL;
 	NMActRequest *best_req6 = NULL;
 	const char *dhcp_hostname, *p;
 
-	g_return_if_fail (policy != NULL);
+	g_return_if_fail (self != NULL);
 
-	if (policy->lookup) {
-		hostname_thread_kill (policy->lookup);
-		policy->lookup = NULL;
+	if (priv->lookup) {
+		hostname_thread_kill (priv->lookup);
+		priv->lookup = NULL;
 	}
 
 	/* Hostname precedence order:
@@ -295,24 +305,24 @@ update_system_hostname (NMPolicy *policy, NMDevice *best4, NMDevice *best6)
 	 */
 
 	/* Try a persistent hostname first */
-	g_object_get (G_OBJECT (policy->manager), NM_MANAGER_HOSTNAME, &configured_hostname, NULL);
+	g_object_get (G_OBJECT (priv->manager), NM_MANAGER_HOSTNAME, &configured_hostname, NULL);
 	if (configured_hostname) {
-		_set_hostname (policy, TRUE, configured_hostname, "from system configuration");
+		_set_hostname (self, TRUE, configured_hostname, "from system configuration");
 		g_free (configured_hostname);
 		return;
 	}
 
 	/* Try automatically determined hostname from the best device's IP config */
 	if (!best4)
-		best4 = get_best_ip4_device (policy->manager, &best_req4);
+		best4 = get_best_ip4_device (priv->manager, &best_req4);
 	if (!best6)
-		best6 = get_best_ip6_device (policy->manager, &best_req6);
+		best6 = get_best_ip6_device (priv->manager, &best_req6);
 
 	if (!best4 && !best6) {
 		/* No best device; fall back to original hostname or if there wasn't
 		 * one, 'localhost.localdomain'
 		 */
-		_set_hostname (policy, TRUE, policy->orig_hostname, "no default device");
+		_set_hostname (self, TRUE, priv->orig_hostname, "no default device");
 		return;
 	}
 
@@ -327,7 +337,7 @@ update_system_hostname (NMPolicy *policy, NMDevice *best4, NMDevice *best6)
 				/* Sanity check; strip leading spaces */
 				while (*p) {
 					if (!isblank (*p++)) {
-						_set_hostname (policy, TRUE, p-1, "from DHCPv4");
+						_set_hostname (self, TRUE, p-1, "from DHCPv4");
 						return;
 					}
 				}
@@ -346,7 +356,7 @@ update_system_hostname (NMPolicy *policy, NMDevice *best4, NMDevice *best6)
 				/* Sanity check; strip leading spaces */
 				while (*p) {
 					if (!isblank (*p++)) {
-						_set_hostname (policy, TRUE, p-1, "from DHCPv6");
+						_set_hostname (self, TRUE, p-1, "from DHCPv6");
 						return;
 					}
 				}
@@ -359,8 +369,8 @@ update_system_hostname (NMPolicy *policy, NMDevice *best4, NMDevice *best6)
 	/* If no automatically-configured hostname, try using the hostname from
 	 * when NM started up.
 	 */
-	if (policy->orig_hostname) {
-		_set_hostname (policy, TRUE, policy->orig_hostname, "from system startup");
+	if (priv->orig_hostname) {
+		_set_hostname (self, TRUE, priv->orig_hostname, "from system startup");
 		return;
 	}
 
@@ -376,7 +386,7 @@ update_system_hostname (NMPolicy *policy, NMDevice *best4, NMDevice *best6)
 		    || (nm_ip4_config_get_num_nameservers (ip4_config) == 0)
 		    || (nm_ip4_config_get_num_addresses (ip4_config) == 0)) {
 			/* No valid IP4 config (!!); fall back to localhost.localdomain */
-			_set_hostname (policy, TRUE, NULL, "no IPv4 config");
+			_set_hostname (self, TRUE, NULL, "no IPv4 config");
 			return;
 		}
 
@@ -384,7 +394,7 @@ update_system_hostname (NMPolicy *policy, NMDevice *best4, NMDevice *best6)
 		g_assert (addr4); /* checked for > 1 address above */
 
 		/* Start the hostname lookup thread */
-		policy->lookup = hostname4_thread_new (nm_ip4_address_get_address (addr4), lookup_callback, policy);
+		priv->lookup = hostname4_thread_new (nm_ip4_address_get_address (addr4), lookup_callback, self);
 	} else if (best6) {
 		NMIP6Config *ip6_config;
 		NMIP6Address *addr6;
@@ -394,7 +404,7 @@ update_system_hostname (NMPolicy *policy, NMDevice *best4, NMDevice *best6)
 		    || (nm_ip6_config_get_num_nameservers (ip6_config) == 0)
 		    || (nm_ip6_config_get_num_addresses (ip6_config) == 0)) {
 			/* No valid IP6 config (!!); fall back to localhost.localdomain */
-			_set_hostname (policy, TRUE, NULL, "no IPv6 config");
+			_set_hostname (self, TRUE, NULL, "no IPv6 config");
 			return;
 		}
 
@@ -402,18 +412,19 @@ update_system_hostname (NMPolicy *policy, NMDevice *best4, NMDevice *best6)
 		g_assert (addr6); /* checked for > 1 address above */
 
 		/* Start the hostname lookup thread */
-		policy->lookup = hostname6_thread_new (nm_ip6_address_get_address (addr6), lookup_callback, policy);
+		priv->lookup = hostname6_thread_new (nm_ip6_address_get_address (addr6), lookup_callback, self);
 	}
 
-	if (!policy->lookup) {
+	if (!priv->lookup) {
 		/* Fall back to 'localhost.localdomain' */
-		_set_hostname (policy, TRUE, NULL, "error starting hostname thread");
+		_set_hostname (self, TRUE, NULL, "error starting hostname thread");
 	}
 }
 
 static void
-update_ip4_routing_and_dns (NMPolicy *policy, gboolean force_update)
+update_ip4_routing_and_dns (NMPolicy *self, gboolean force_update)
 {
+	NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self);
 	NMDnsIPConfigType dns_type = NM_DNS_IP_CONFIG_TYPE_BEST_DEVICE;
 	NMDevice *best = NULL;
 	NMActRequest *best_req = NULL;
@@ -426,14 +437,14 @@ update_ip4_routing_and_dns (NMPolicy *policy, gboolean force_update)
 	NMSettingConnection *s_con = NULL;
 	const char *connection_id;
 
-	best = get_best_ip4_device (policy->manager, &best_req);
+	best = get_best_ip4_device (priv->manager, &best_req);
 	if (!best)
 		goto out;
-	if (!force_update && (best == policy->default_device4))
+	if (!force_update && (best == priv->default_device4))
 		goto out;
 
 	/* If a VPN connection is active, it is preferred */
-	vpns = nm_vpn_manager_get_active_connections (policy->vpn_manager);
+	vpns = nm_vpn_manager_get_active_connections (priv->vpn_manager);
 	for (iter = vpns; iter; iter = g_slist_next (iter)) {
 		NMVPNConnection *candidate = NM_VPN_CONNECTION (iter->data);
 		NMConnection *vpn_connection;
@@ -503,7 +514,7 @@ update_ip4_routing_and_dns (NMPolicy *policy, gboolean force_update)
 	 * first.  The order is important, we don't want two connections marked
 	 * default at the same time ever.
 	 */
-	devices = nm_manager_get_devices (policy->manager);
+	devices = nm_manager_get_devices (priv->manager);
 	for (iter = devices; iter; iter = g_slist_next (iter)) {
 		NMDevice *dev = NM_DEVICE (iter->data);
 		NMActRequest *req;
@@ -534,12 +545,13 @@ update_ip4_routing_and_dns (NMPolicy *policy, gboolean force_update)
 	}
 
 out:
-	policy->default_device4 = best;
+	priv->default_device4 = best;
 }
 
 static void
-update_ip6_routing_and_dns (NMPolicy *policy, gboolean force_update)
+update_ip6_routing_and_dns (NMPolicy *self, gboolean force_update)
 {
+	NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self);
 	NMDnsIPConfigType dns_type = NM_DNS_IP_CONFIG_TYPE_BEST_DEVICE;
 	NMDevice *best = NULL;
 	NMActRequest *best_req = NULL;
@@ -555,15 +567,15 @@ update_ip6_routing_and_dns (NMPolicy *policy, gboolean force_update)
 	NMSettingConnection *s_con = NULL;
 	const char *connection_id;
 
-	best = get_best_ip6_device (policy->manager, &best_req);
+	best = get_best_ip6_device (priv->manager, &best_req);
 	if (!best)
 		goto out;
-	if (!force_update && (best == policy->default_device6))
+	if (!force_update && (best == priv->default_device6))
 		goto out;
 
 #if 0
 	/* If a VPN connection is active, it is preferred */
-	vpns = nm_vpn_manager_get_active_connections (policy->vpn_manager);
+	vpns = nm_vpn_manager_get_active_connections (priv->vpn_manager);
 	for (iter = vpns; iter; iter = g_slist_next (iter)) {
 		NMVPNConnection *candidate = NM_VPN_CONNECTION (iter->data);
 		NMConnection *vpn_connection;
@@ -628,7 +640,7 @@ update_ip6_routing_and_dns (NMPolicy *policy, gboolean force_update)
 	 * first.  The order is important, we don't want two connections marked
 	 * default at the same time ever.
 	 */
-	devices = nm_manager_get_devices (policy->manager);
+	devices = nm_manager_get_devices (priv->manager);
 	for (iter = devices; iter; iter = g_slist_next (iter)) {
 		NMDevice *dev = NM_DEVICE (iter->data);
 		NMActRequest *req;
@@ -659,17 +671,18 @@ update_ip6_routing_and_dns (NMPolicy *policy, gboolean force_update)
 	}
 
 out:
-	policy->default_device6 = best;
+	priv->default_device6 = best;
 }
 
 static void
-update_routing_and_dns (NMPolicy *policy, gboolean force_update)
+update_routing_and_dns (NMPolicy *self, gboolean force_update)
 {
-	update_ip4_routing_and_dns (policy, force_update);
-	update_ip6_routing_and_dns (policy, force_update);
+	NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self);
+	update_ip4_routing_and_dns (self, force_update);
+	update_ip6_routing_and_dns (self, force_update);
 
 	/* Update the system hostname */
-	update_system_hostname (policy, policy->default_device4, policy->default_device6);
+	update_system_hostname (self, priv->default_device4, priv->default_device6);
 }
 
 static void
@@ -706,16 +719,18 @@ static gboolean
 auto_activate_device (gpointer user_data)
 {
 	ActivateData *data = (ActivateData *) user_data;
-	NMPolicy *policy;
+	NMPolicy *self;
+	NMPolicyPrivate *priv;
 	NMConnection *best_connection;
 	char *specific_object = NULL;
 	GSList *connections, *iter;
 
 	g_assert (data);
-	policy = data->policy;
+	self = data->policy;
+	priv = NM_POLICY_GET_PRIVATE (self);
 
 	data->id = 0;
-	policy->pending_activation_checks = g_slist_remove (policy->pending_activation_checks, data);
+	priv->pending_activation_checks = g_slist_remove (priv->pending_activation_checks, data);
 
 	// FIXME: if a device is already activating (or activated) with a connection
 	// but another connection now overrides the current one for that device,
@@ -724,7 +739,7 @@ auto_activate_device (gpointer user_data)
 	if (nm_device_get_act_request (data->device))
 		goto out;
 
-	iter = connections = nm_settings_get_connections (policy->settings);
+	iter = connections = nm_settings_get_connections (priv->settings);
 
 	/* Remove connections that shouldn't be auto-activated */
 	while (iter) {
@@ -760,7 +775,7 @@ auto_activate_device (gpointer user_data)
 
 		nm_log_info (LOGD_DEVICE, "Auto-activating connection '%s'.",
 		             nm_connection_get_id (best_connection));
-		if (!nm_manager_activate_connection (policy->manager,
+		if (!nm_manager_activate_connection (priv->manager,
 		                                     best_connection,
 		                                     specific_object,
 		                                     nm_device_get_path (data->device),
@@ -780,12 +795,12 @@ auto_activate_device (gpointer user_data)
 }
 
 static ActivateData *
-activate_data_new (NMPolicy *policy, NMDevice *device, guint delay_seconds)
+activate_data_new (NMPolicy *self, NMDevice *device, guint delay_seconds)
 {
 	ActivateData *data;
 
 	data = g_malloc0 (sizeof (ActivateData));
-	data->policy = policy;
+	data->policy = self;
 	data->device = g_object_ref (device);
 	if (delay_seconds > 0)
 		data->id = g_timeout_add_seconds (delay_seconds, auto_activate_device, data);
@@ -872,7 +887,8 @@ reset_retries_for_failed_secrets (NMSettings *settings)
 static void
 sleeping_changed (NMManager *manager, GParamSpec *pspec, gpointer user_data)
 {
-	NMPolicy *policy = user_data;
+	NMPolicy *self = user_data;
+	NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self);
 	gboolean sleeping = FALSE, enabled = FALSE;
 
 	g_object_get (G_OBJECT (manager), NM_MANAGER_SLEEPING, &sleeping, NULL);
@@ -880,16 +896,17 @@ sleeping_changed (NMManager *manager, GParamSpec *pspec, gpointer user_data)
 
 	/* Reset retries on all connections so they'll checked on wakeup */
 	if (sleeping || !enabled)
-		reset_retries_all (policy->settings, NULL);
+		reset_retries_all (priv->settings, NULL);
 }
 
 static void
-schedule_activate_check (NMPolicy *policy, NMDevice *device, guint delay_seconds)
+schedule_activate_check (NMPolicy *self, NMDevice *device, guint delay_seconds)
 {
+	NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self);
 	ActivateData *data;
 	NMDeviceState state;
 
-	if (nm_manager_get_state (policy->manager) == NM_STATE_ASLEEP)
+	if (nm_manager_get_state (priv->manager) == NM_STATE_ASLEEP)
 		return;
 
 	state = nm_device_interface_get_state (NM_DEVICE_INTERFACE (device));
@@ -900,23 +917,24 @@ schedule_activate_check (NMPolicy *policy, NMDevice *device, guint delay_seconds
 		return;
 
 	/* Schedule an auto-activation if there isn't one already for this device */
-	if (find_pending_activation (policy->pending_activation_checks, device) == NULL) {
-		data = activate_data_new (policy, device, delay_seconds);
-		policy->pending_activation_checks = g_slist_append (policy->pending_activation_checks, data);
+	if (find_pending_activation (priv->pending_activation_checks, device) == NULL) {
+		data = activate_data_new (self, device, delay_seconds);
+		priv->pending_activation_checks = g_slist_append (priv->pending_activation_checks, data);
 	}
 }
 
 static gboolean
 reset_connections_retries (gpointer user_data)
 {
-	NMPolicy *policy = (NMPolicy *) user_data;
+	NMPolicy *self = (NMPolicy *) user_data;
+	NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self);
 	GSList *connections, *iter;
 	time_t con_stamp, min_stamp, now;
 
-	policy->reset_retries_id = 0;
+	priv->reset_retries_id = 0;
 
 	min_stamp = now = time (NULL);
-	connections = nm_settings_get_connections (policy->settings);
+	connections = nm_settings_get_connections (priv->settings);
 	for (iter = connections; iter; iter = g_slist_next (iter)) {
 		con_stamp = GPOINTER_TO_SIZE (g_object_get_data (G_OBJECT (iter->data), RESET_RETRIES_TIMESTAMP_TAG));
 		if (con_stamp == 0)
@@ -933,7 +951,7 @@ reset_connections_retries (gpointer user_data)
 
 	/* Schedule the handler again if there are some stamps left */
 	if (min_stamp != now)
-		policy->reset_retries_id = g_timeout_add_seconds (RESET_RETRIES_TIMER - (now - min_stamp), reset_connections_retries, policy);
+		priv->reset_retries_id = g_timeout_add_seconds (RESET_RETRIES_TIMER - (now - min_stamp), reset_connections_retries, self);
 	return FALSE;
 }
 
@@ -956,7 +974,8 @@ device_state_changed (NMDevice *device,
                       NMDeviceStateReason reason,
                       gpointer user_data)
 {
-	NMPolicy *policy = (NMPolicy *) user_data;
+	NMPolicy *self = (NMPolicy *) user_data;
+	NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self);
 	NMConnection *connection = get_device_connection (device);
 
 	if (connection)
@@ -993,12 +1012,12 @@ device_state_changed (NMDevice *device,
 				nm_log_info (LOGD_DEVICE, "Marking connection '%s' invalid.", nm_connection_get_id (connection));
 				/* Schedule a handler to reset retries count */
 				g_object_set_data (G_OBJECT (connection), RESET_RETRIES_TIMESTAMP_TAG, GSIZE_TO_POINTER ((gsize) time (NULL)));
-				if (!policy->reset_retries_id)
-					policy->reset_retries_id = g_timeout_add_seconds (RESET_RETRIES_TIMER, reset_connections_retries, policy);
+				if (!priv->reset_retries_id)
+					priv->reset_retries_id = g_timeout_add_seconds (RESET_RETRIES_TIMER, reset_connections_retries, self);
 			}
 			nm_connection_clear_secrets (connection);
 		}
-		schedule_activate_check (policy, device, 3);
+		schedule_activate_check (self, device, 3);
 		break;
 	case NM_DEVICE_STATE_ACTIVATED:
 		if (connection) {
@@ -1011,21 +1030,21 @@ device_state_changed (NMDevice *device,
 			nm_connection_clear_secrets (connection);
 		}
 
-		update_routing_and_dns (policy, FALSE);
+		update_routing_and_dns (self, FALSE);
 		break;
 	case NM_DEVICE_STATE_UNMANAGED:
 	case NM_DEVICE_STATE_UNAVAILABLE:
-		update_routing_and_dns (policy, FALSE);
+		update_routing_and_dns (self, FALSE);
 		break;
 	case NM_DEVICE_STATE_DISCONNECTED:
 		/* Reset RETRIES_TAG when carrier on. If cable was unplugged
 		 * and plugged again, we should try to reconnect */
 		if (reason == NM_DEVICE_STATE_REASON_CARRIER && old_state == NM_DEVICE_STATE_UNAVAILABLE)
-			reset_retries_all (policy->settings, device);
+			reset_retries_all (priv->settings, device);
 
 		/* Device is now available for auto-activation */
-		update_routing_and_dns (policy, FALSE);
-		schedule_activate_check (policy, device, 0);
+		update_routing_and_dns (self, FALSE);
+		schedule_activate_check (self, device, 0);
 		break;
 	default:
 		break;
@@ -1060,33 +1079,34 @@ typedef struct {
 } DeviceSignalId;
 
 static void
-_connect_device_signal (NMPolicy *policy, NMDevice *device, const char *name, gpointer callback)
+_connect_device_signal (NMPolicy *self, NMDevice *device, const char *name, gpointer callback)
 {
+	NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self);
 	DeviceSignalId *data;
 
 	data = g_slice_new0 (DeviceSignalId);
 	g_assert (data);
-	data->id = g_signal_connect (device, name, callback, policy);
+	data->id = g_signal_connect (device, name, callback, self);
 	data->device = device;
-	policy->dev_ids = g_slist_prepend (policy->dev_ids, data);
+	priv->dev_ids = g_slist_prepend (priv->dev_ids, data);
 }
 
 static void
 device_added (NMManager *manager, NMDevice *device, gpointer user_data)
 {
-	NMPolicy *policy = (NMPolicy *) user_data;
+	NMPolicy *self = (NMPolicy *) user_data;
 
-	_connect_device_signal (policy, device, "state-changed", device_state_changed);
-	_connect_device_signal (policy, device, "notify::" NM_DEVICE_INTERFACE_IP4_CONFIG, device_ip_config_changed);
-	_connect_device_signal (policy, device, "notify::" NM_DEVICE_INTERFACE_IP6_CONFIG, device_ip_config_changed);
+	_connect_device_signal (self, device, "state-changed", device_state_changed);
+	_connect_device_signal (self, device, "notify::" NM_DEVICE_INTERFACE_IP4_CONFIG, device_ip_config_changed);
+	_connect_device_signal (self, device, "notify::" NM_DEVICE_INTERFACE_IP6_CONFIG, device_ip_config_changed);
 
 	if (NM_IS_DEVICE_WIFI (device)) {
-		_connect_device_signal (policy, device, "access-point-added", wireless_networks_changed);
-		_connect_device_signal (policy, device, "access-point-removed", wireless_networks_changed);
+		_connect_device_signal (self, device, "access-point-added", wireless_networks_changed);
+		_connect_device_signal (self, device, "access-point-removed", wireless_networks_changed);
 #if WITH_WIMAX
 	} else if (NM_IS_DEVICE_WIMAX (device)) {
-		_connect_device_signal (policy, device, "nsp-added", nsps_changed);
-		_connect_device_signal (policy, device, "nsp-removed", nsps_changed);
+		_connect_device_signal (self, device, "nsp-added", nsps_changed);
+		_connect_device_signal (self, device, "nsp-removed", nsps_changed);
 #endif
 	}
 }
@@ -1094,19 +1114,20 @@ device_added (NMManager *manager, NMDevice *device, gpointer user_data)
 static void
 device_removed (NMManager *manager, NMDevice *device, gpointer user_data)
 {
-	NMPolicy *policy = (NMPolicy *) user_data;
+	NMPolicy *self = (NMPolicy *) user_data;
+	NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self);
 	ActivateData *tmp;
 	GSList *iter;
 
 	/* Clear any idle callbacks for this device */
-	tmp = find_pending_activation (policy->pending_activation_checks, device);
+	tmp = find_pending_activation (priv->pending_activation_checks, device);
 	if (tmp) {
-		policy->pending_activation_checks = g_slist_remove (policy->pending_activation_checks, tmp);
+		priv->pending_activation_checks = g_slist_remove (priv->pending_activation_checks, tmp);
 		activate_data_free (tmp);
 	}
 
 	/* Clear any signal handlers for this device */
-	iter = policy->dev_ids;
+	iter = priv->dev_ids;
 	while (iter) {
 		DeviceSignalId *data = iter->data;
 		GSList *next = g_slist_next (iter);
@@ -1114,22 +1135,23 @@ device_removed (NMManager *manager, NMDevice *device, gpointer user_data)
 		if (data->device == device) {
 			g_signal_handler_disconnect (data->device, data->id);
 			g_slice_free (DeviceSignalId, data);
-			policy->dev_ids = g_slist_delete_link (policy->dev_ids, iter);
+			priv->dev_ids = g_slist_delete_link (priv->dev_ids, iter);
 		}
 		iter = next;
 	}
 
-	update_routing_and_dns (policy, FALSE);
+	update_routing_and_dns (self, FALSE);
 }
 
 static void
-schedule_activate_all (NMPolicy *policy)
+schedule_activate_all (NMPolicy *self)
 {
+	NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self);
 	GSList *iter, *devices;
 
-	devices = nm_manager_get_devices (policy->manager);
+	devices = nm_manager_get_devices (priv->manager);
 	for (iter = devices; iter; iter = g_slist_next (iter))
-		schedule_activate_check (policy, NM_DEVICE (iter->data), 0);
+		schedule_activate_check (self, NM_DEVICE (iter->data), 0);
 }
 
 static void
@@ -1194,9 +1216,10 @@ connection_removed (NMSettings *settings,
                     NMConnection *connection,
                     gpointer user_data)
 {
-	NMPolicy *policy = user_data;
+	NMPolicy *self = user_data;
+	NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self);
 
-	_deactivate_if_active (policy->manager, connection);
+	_deactivate_if_active (priv->manager, connection);
 }
 
 static void
@@ -1204,12 +1227,13 @@ connection_visibility_changed (NMSettings *settings,
                                NMSettingsConnection *connection,
                                gpointer user_data)
 {
-	NMPolicy *policy = user_data;
+	NMPolicy *self = user_data;
+	NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self);
 
 	if (nm_settings_connection_is_visible (connection))
-		schedule_activate_all (policy);
+		schedule_activate_all (self);
 	else
-		_deactivate_if_active (policy->manager, NM_CONNECTION (connection));
+		_deactivate_if_active (priv->manager, NM_CONNECTION (connection));
 }
 
 static void
@@ -1226,21 +1250,23 @@ secret_agent_registered (NMSettings *settings,
 }
 
 static void
-_connect_manager_signal (NMPolicy *policy, const char *name, gpointer callback)
+_connect_manager_signal (NMPolicy *self, const char *name, gpointer callback)
 {
+	NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self);
 	guint id;
 
-	id = g_signal_connect (policy->manager, name, callback, policy);
-	policy->manager_ids = g_slist_prepend (policy->manager_ids, GUINT_TO_POINTER (id));
+	id = g_signal_connect (priv->manager, name, callback, self);
+	priv->manager_ids = g_slist_prepend (priv->manager_ids, GUINT_TO_POINTER (id));
 }
 
 static void
-_connect_settings_signal (NMPolicy *policy, const char *name, gpointer callback)
+_connect_settings_signal (NMPolicy *self, const char *name, gpointer callback)
 {
+	NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self);
 	guint id;
 
-	id = g_signal_connect (policy->settings, name, callback, policy);
-	policy->settings_ids = g_slist_prepend (policy->settings_ids, GUINT_TO_POINTER (id));
+	id = g_signal_connect (priv->settings, name, callback, self);
+	priv->settings_ids = g_slist_prepend (priv->settings_ids, GUINT_TO_POINTER (id));
 }
 
 NMPolicy *
@@ -1248,7 +1274,8 @@ nm_policy_new (NMManager *manager,
                NMVPNManager *vpn_manager,
                NMSettings *settings)
 {
-	NMPolicy *policy;
+	NMPolicy *self;
+	NMPolicyPrivate *priv;
 	static gboolean initialized = FALSE;
 	gulong id;
 	char hostname[HOST_NAME_MAX + 2];
@@ -1256,10 +1283,15 @@ nm_policy_new (NMManager *manager,
 	g_return_val_if_fail (NM_IS_MANAGER (manager), NULL);
 	g_return_val_if_fail (initialized == FALSE, NULL);
 
-	policy = g_malloc0 (sizeof (NMPolicy));
-	policy->manager = g_object_ref (manager);
-	policy->settings = g_object_ref (settings);
-	policy->update_state_id = 0;
+	self = g_object_new (NM_TYPE_POLICY, NULL);
+	if (!self)
+		return NULL;
+
+	priv = NM_POLICY_GET_PRIVATE (self);
+
+	priv->manager = g_object_ref (manager);
+	priv->settings = g_object_ref (settings);
+	priv->update_state_id = 0;
 
 	/* Grab hostname on startup and use that if nothing provides one */
 	memset (hostname, 0, sizeof (hostname));
@@ -1269,85 +1301,118 @@ nm_policy_new (NMManager *manager,
 		    && strcmp (hostname, "localhost")
 		    && strcmp (hostname, "localhost.localdomain")
 		    && strcmp (hostname, "(none)"))
-			policy->orig_hostname = g_strdup (hostname);
+			priv->orig_hostname = g_strdup (hostname);
 	}
 
-	policy->vpn_manager = g_object_ref (vpn_manager);
-	id = g_signal_connect (policy->vpn_manager, "connection-activated",
-	                       G_CALLBACK (vpn_connection_activated), policy);
-	policy->vpn_activated_id = id;
-	id = g_signal_connect (policy->vpn_manager, "connection-deactivated",
-	                       G_CALLBACK (vpn_connection_deactivated), policy);
-	policy->vpn_deactivated_id = id;
-
-	_connect_manager_signal (policy, "state-changed", global_state_changed);
-	_connect_manager_signal (policy, "notify::" NM_MANAGER_HOSTNAME, hostname_changed);
-	_connect_manager_signal (policy, "notify::" NM_MANAGER_SLEEPING, sleeping_changed);
-	_connect_manager_signal (policy, "notify::" NM_MANAGER_NETWORKING_ENABLED, sleeping_changed);
-	_connect_manager_signal (policy, "device-added", device_added);
-	_connect_manager_signal (policy, "device-removed", device_removed);
-
-	_connect_settings_signal (policy, NM_SETTINGS_SIGNAL_CONNECTIONS_LOADED, connections_loaded);
-	_connect_settings_signal (policy, NM_SETTINGS_SIGNAL_CONNECTION_ADDED, connection_added);
-	_connect_settings_signal (policy, NM_SETTINGS_SIGNAL_CONNECTION_UPDATED, connection_updated);
-	_connect_settings_signal (policy, NM_SETTINGS_SIGNAL_CONNECTION_REMOVED, connection_removed);
-	_connect_settings_signal (policy, NM_SETTINGS_SIGNAL_CONNECTION_VISIBILITY_CHANGED,
+	priv->vpn_manager = g_object_ref (vpn_manager);
+	id = g_signal_connect (priv->vpn_manager, "connection-activated",
+	                       G_CALLBACK (vpn_connection_activated), self);
+	priv->vpn_activated_id = id;
+	id = g_signal_connect (priv->vpn_manager, "connection-deactivated",
+	                       G_CALLBACK (vpn_connection_deactivated), self);
+	priv->vpn_deactivated_id = id;
+
+	_connect_manager_signal (self, "state-changed", global_state_changed);
+	_connect_manager_signal (self, "notify::" NM_MANAGER_HOSTNAME, hostname_changed);
+	_connect_manager_signal (self, "notify::" NM_MANAGER_SLEEPING, sleeping_changed);
+	_connect_manager_signal (self, "notify::" NM_MANAGER_NETWORKING_ENABLED, sleeping_changed);
+	_connect_manager_signal (self, "device-added", device_added);
+	_connect_manager_signal (self, "device-removed", device_removed);
+
+	_connect_settings_signal (self, NM_SETTINGS_SIGNAL_CONNECTIONS_LOADED, connections_loaded);
+	_connect_settings_signal (self, NM_SETTINGS_SIGNAL_CONNECTION_ADDED, connection_added);
+	_connect_settings_signal (self, NM_SETTINGS_SIGNAL_CONNECTION_UPDATED, connection_updated);
+	_connect_settings_signal (self, NM_SETTINGS_SIGNAL_CONNECTION_REMOVED, connection_removed);
+	_connect_settings_signal (self, NM_SETTINGS_SIGNAL_CONNECTION_VISIBILITY_CHANGED,
 	                          connection_visibility_changed);
-	_connect_settings_signal (policy, NM_SETTINGS_SIGNAL_AGENT_REGISTERED, secret_agent_registered);
+	_connect_settings_signal (self, NM_SETTINGS_SIGNAL_AGENT_REGISTERED, secret_agent_registered);
 
 	/* Initialize connections' auto-retries */
-	reset_retries_all (policy->settings, NULL);
+	reset_retries_all (priv->settings, NULL);
 
 	initialized = TRUE;
-	return policy;
+	return self;
 }
 
-void
-nm_policy_destroy (NMPolicy *policy)
+static void
+nm_policy_init (NMPolicy *self)
 {
-	GSList *iter;
+	NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self);
+	priv->disposed = FALSE;
+}
 
-	g_return_if_fail (policy != NULL);
+static void
+dispose (GObject *object)
+{
+	NMPolicy *self = NM_POLICY (object);
+	NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self);
+
+	if (!priv->disposed) {
+		priv->disposed = TRUE;
+		g_object_unref (priv->manager);
+		g_object_unref (priv->vpn_manager);
+		g_object_unref (priv->settings);
+	}
+	G_OBJECT_CLASS (nm_policy_parent_class)->dispose (object);
+}
+
+static void
+finalize (GObject *object)
+{
+	NMPolicy *self = NM_POLICY (object);
+	NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self);
+
+	GSList *iter;
 
 	/* Tell any existing hostname lookup thread to die, it'll get cleaned up
 	 * by the lookup thread callback.
 	  */
-	if (policy->lookup) {
-		hostname_thread_kill (policy->lookup);
-		policy->lookup = NULL;
+	if (priv->lookup) {
+		hostname_thread_kill (priv->lookup);
+		priv->lookup = NULL;
 	}
 
-	g_slist_foreach (policy->pending_activation_checks, (GFunc) activate_data_free, NULL);
-	g_slist_free (policy->pending_activation_checks);
+	g_slist_foreach (priv->pending_activation_checks, (GFunc) activate_data_free, NULL);
+	g_slist_free (priv->pending_activation_checks);
 
-	g_signal_handler_disconnect (policy->vpn_manager, policy->vpn_activated_id);
-	g_signal_handler_disconnect (policy->vpn_manager, policy->vpn_deactivated_id);
-	g_object_unref (policy->vpn_manager);
+	g_signal_handler_disconnect (priv->vpn_manager, priv->vpn_activated_id);
+	g_signal_handler_disconnect (priv->vpn_manager, priv->vpn_deactivated_id);
 
-	for (iter = policy->manager_ids; iter; iter = g_slist_next (iter))
-		g_signal_handler_disconnect (policy->manager, GPOINTER_TO_UINT (iter->data));
-	g_slist_free (policy->manager_ids);
+	for (iter = priv->manager_ids; iter; iter = g_slist_next (iter))
+		g_signal_handler_disconnect (priv->manager, GPOINTER_TO_UINT (iter->data));
+	g_slist_free (priv->manager_ids);
 
-	for (iter = policy->settings_ids; iter; iter = g_slist_next (iter))
-		g_signal_handler_disconnect (policy->settings, GPOINTER_TO_UINT (iter->data));
-	g_slist_free (policy->settings_ids);
+	for (iter = priv->settings_ids; iter; iter = g_slist_next (iter))
+		g_signal_handler_disconnect (priv->settings, GPOINTER_TO_UINT (iter->data));
+	g_slist_free (priv->settings_ids);
 
-	for (iter = policy->dev_ids; iter; iter = g_slist_next (iter)) {
+	for (iter = priv->dev_ids; iter; iter = g_slist_next (iter)) {
 		DeviceSignalId *data = iter->data;
 
 		g_signal_handler_disconnect (data->device, data->id);
 		g_slice_free (DeviceSignalId, data);
 	}
-	g_slist_free (policy->dev_ids);
+	g_slist_free (priv->dev_ids);
 
-	if (policy->reset_retries_id)
-		g_source_remove (policy->reset_retries_id);
+	if (priv->reset_retries_id)
+		g_source_remove (priv->reset_retries_id);
 
-	g_free (policy->orig_hostname);
-	g_free (policy->cur_hostname);
+	g_free (priv->orig_hostname);
+	g_free (priv->cur_hostname);
 
-	g_object_unref (policy->settings);
-	g_object_unref (policy->manager);
-	g_free (policy);
+	G_OBJECT_CLASS (nm_policy_parent_class)->finalize (object);
 }
 
+
+static void
+nm_policy_class_init (NMPolicyClass *class)
+{
+	GObjectClass *object_class = G_OBJECT_CLASS (class);
+
+	g_type_class_add_private (class, sizeof (NMPolicyPrivate));
+
+	/* virtual methods */
+	object_class->dispose = dispose;
+	object_class->finalize = finalize;
+
+}
diff --git a/src/nm-policy.h b/src/nm-policy.h
index 33796bc..6837103 100644
--- a/src/nm-policy.h
+++ b/src/nm-policy.h
@@ -15,7 +15,7 @@
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *
- * Copyright (C) 2004 - 2010 Red Hat, Inc.
+ * Copyright (C) 2004 - 2011 Red Hat, Inc.
  * Copyright (C) 2007 - 2008 Novell, Inc.
  */
 
@@ -26,11 +26,26 @@
 #include "nm-vpn-manager.h"
 #include "nm-settings.h"
 
-typedef struct NMPolicy NMPolicy;
+#define NM_TYPE_POLICY            (nm_policy_get_type ())
+#define NM_POLICY(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_POLICY, NMPolicy))
+#define NM_POLICY_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass),  NM_TYPE_POLICY, NMPolicyClass))
+#define NM_IS_POLICY(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_POLICY))
+#define NM_IS_POLICY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),  NM_TYPE_POLICY))
+#define NM_POLICY_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj),  NM_TYPE_POLICY, NMPolicyClass))
+
+typedef struct {
+	GObject parent_instance;
+} NMPolicy;
+
+typedef struct {
+	GObjectClass parent_class;
+
+} NMPolicyClass;
+
+GType nm_policy_get_type (void);
 
 NMPolicy *nm_policy_new (NMManager *manager,
                          NMVPNManager *vpn_manager,
                          NMSettings *settings);
-void nm_policy_destroy (NMPolicy *policy);
 
 #endif /* NM_POLICY_H */
-- 
1.7.6



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