[nautilus/undo-manager: 2/3] undostack-manager: modernise object initialization and lifetime



commit a91bd2e3287a4b53fb99328c9bd6ea58273ea65f
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Fri Nov 26 17:03:37 2010 +0100

    undostack-manager: modernise object initialization and lifetime

 libnautilus-private/nautilus-undostack-manager.c |  312 +++++++++++-----------
 1 files changed, 150 insertions(+), 162 deletions(-)
---
diff --git a/libnautilus-private/nautilus-undostack-manager.c b/libnautilus-private/nautilus-undostack-manager.c
index 41b36f1..b9038af 100644
--- a/libnautilus-private/nautilus-undostack-manager.c
+++ b/libnautilus-private/nautilus-undostack-manager.c
@@ -25,9 +25,9 @@
 #include "nautilus-undostack-manager.h"
 #include "nautilus-file-operations.h"
 #include "nautilus-file.h"
+
 #include <gio/gio.h>
-#include <glib/gprintf.h>
-#include <glib-object.h>
+#include <glib.h>
 #include <glib/gi18n.h>
 #include <locale.h>
 #include <gdk/gdk.h>
@@ -109,15 +109,13 @@ struct _NautilusUndoStackManagerPrivate
 
 enum
 {
-	PROP_UNDOSTACK_MANAGER_0,
-	PROP_UNDO_LEVELS,
-	PROP_CONFIRM_DELETE
+	PROP_UNDO_LEVELS = 1,
+	PROP_CONFIRM_DELETE,
+	NUM_PROPERTIES,
 };
 
-static void nautilus_undo_stack_manager_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec);
-static void nautilus_undo_stack_manager_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec);
-static void nautilus_undo_stack_manager_finalize (GObject * object);
-static void nautilus_undo_stack_manager_dispose (GObject * object);
+static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
+
 G_DEFINE_TYPE (NautilusUndoStackManager, nautilus_undo_stack_manager, G_TYPE_OBJECT)
 
 static void stack_clear_n_oldest (GQueue * stack, guint n);
@@ -136,8 +134,6 @@ static char *get_redo_label (NautilusUndoStackActionData *action);
 static char *get_redo_description (NautilusUndoStackActionData *action);
 
 static void do_menu_update (NautilusUndoStackManager * manager);
-static void free_undo_stack_action_data (NautilusUndoStackActionData *data);
-static void undostack_dispose_all (GQueue * queue);
 static void undo_redo_done_transfer_callback (GHashTable * debuting_uris, gpointer data);
 static void undo_redo_op_callback (gpointer callback_data);
 static void undo_redo_done_rename_callback (NautilusFile * file, GFile * result_location, GError * error, gpointer callback_data);
@@ -150,51 +146,7 @@ static GList *construct_gfile_list_from_uri (char *uri);
 static GList *uri_list_to_gfile_list (GList * urilist);
 static GHashTable *retrieve_files_to_restore (GHashTable * trashed);
 
-static void
-nautilus_undo_stack_manager_class_init (NautilusUndoStackManagerClass * klass)
-{
-	GParamSpec *undo_levels;
-	GParamSpec *confirm_delete;
-	GObjectClass *g_object_class;
-
-	/* Add private structure */
-	g_type_class_add_private (klass, sizeof (NautilusUndoStackManagerPrivate));
-
-	/* Create properties */
-	undo_levels = g_param_spec_uint ("undo-levels",
-					 "undo levels",
-					 "Number of undo levels to be stored",
-					 1, UINT_MAX, DEFAULT_UNDO_DEPTH,
-					 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
-
-	confirm_delete = g_param_spec_boolean ("confirm-delete",
-					       "confirm delete",
-					       "Always confirm file deletion",
-					       FALSE,
-					       G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
-
-	/* Set properties get/set methods */
-	g_object_class = G_OBJECT_CLASS (klass);
-
-	g_object_class->set_property = nautilus_undo_stack_manager_set_property;
-	g_object_class->get_property = nautilus_undo_stack_manager_get_property;
-
-	/* Install properties */
-	g_object_class_install_property (g_object_class, PROP_UNDO_LEVELS, undo_levels);
-
-	g_object_class_install_property (g_object_class, PROP_CONFIRM_DELETE, confirm_delete);
-
-	/* The UI menu needs to update its status */
-	g_signal_new ("request-menu-update",
-			G_TYPE_FROM_CLASS (klass),
-			G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
-			0, NULL, NULL,
-			g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
-
-	/* Hook deconstructors */
-	g_object_class->dispose = nautilus_undo_stack_manager_dispose;
-	g_object_class->finalize = nautilus_undo_stack_manager_finalize;
-}
+static NautilusUndoStackManager *singleton = NULL;
 
 static void
 nautilus_undo_stack_manager_init (NautilusUndoStackManager * self)
@@ -215,40 +167,70 @@ nautilus_undo_stack_manager_init (NautilusUndoStackManager * self)
 }
 
 static void
-nautilus_undo_stack_manager_dispose (GObject * object)
+free_undo_stack_action_data (NautilusUndoStackActionData *action)
 {
-	NautilusUndoStackManager *self;
-	NautilusUndoStackManagerPrivate *priv;
+	g_return_if_fail (action != NULL);
 
-	self = NAUTILUS_UNDO_STACK_MANAGER (object);
-	priv = self->priv;
+	g_free (action->template);
 
-	if (priv->dispose_has_run)
-		return;
+	g_free (action->undo_label);
+	g_free (action->undo_description);
+	g_free (action->redo_label);
+	g_free (action->redo_description);
 
-	g_mutex_lock (priv->mutex);
+	g_free (action->original_group_name);
+	g_free (action->original_user_name);
+	g_free (action->new_group_name);
+	g_free (action->new_user_name);
 
-	/* Free each undoable action in the stack and the stack itself */
-	undostack_dispose_all (priv->stack);
-	g_queue_free (priv->stack);
+	if (action->sources) {
+		g_list_free_full (action->sources, g_free);
+	}
 
-	g_mutex_unlock (priv->mutex);
+	if (action->destinations) {
+		g_list_free_full (action->destinations, g_free);
+	}
 
-	g_mutex_free (priv->mutex);
+	if (action->trashed) {
+		g_hash_table_destroy (action->trashed);
+	}
+
+	if (action->original_permissions) {
+		g_hash_table_destroy (action->original_permissions);
+	}
+
+	g_clear_object (&action->old_file);
+	g_clear_object (&action->target_file);
+	g_clear_object (&action->src_dir);
+	g_clear_object (&action->dest_dir);
 
-	priv->dispose_has_run = TRUE;
+	g_free (action->new_file);
 
-	G_OBJECT_CLASS (nautilus_undo_stack_manager_parent_class)->dispose (object);
+	g_slice_free (NautilusUndoStackActionData, action);
 }
 
 static void
 nautilus_undo_stack_manager_finalize (GObject * object)
 {
+	NautilusUndoStackManager *self = NAUTILUS_UNDO_STACK_MANAGER (object);
+	NautilusUndoStackManagerPrivate *priv = self->priv;
+
+	g_mutex_lock (priv->mutex);
+	
+	g_queue_foreach (priv->stack, (GFunc) free_undo_stack_action_data, NULL);
+	g_queue_free (priv->stack);
+
+	g_mutex_unlock (priv->mutex);
+	g_mutex_free (priv->mutex);
+
 	G_OBJECT_CLASS (nautilus_undo_stack_manager_parent_class)->finalize (object);
 }
 
 static void
-nautilus_undo_stack_manager_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
+nautilus_undo_stack_manager_set_property (GObject *object,
+					  guint prop_id,
+					  const GValue *value,
+					  GParamSpec * pspec)
 {
 	NautilusUndoStackManager *manager;
 	NautilusUndoStackManagerPrivate *priv;
@@ -260,27 +242,30 @@ nautilus_undo_stack_manager_set_property (GObject * object, guint prop_id, const
 	priv = manager->priv;
 
 	switch (prop_id) {
-		case PROP_UNDO_LEVELS:
-			new_undo_levels = g_value_get_uint (value);
-			if (new_undo_levels > 0 && (priv->undo_levels != new_undo_levels)) {
-				priv->undo_levels = new_undo_levels;
-				g_mutex_lock (priv->mutex);
-				stack_fix_size (priv);
-				g_mutex_unlock (priv->mutex);
-				do_menu_update (manager);
-			}
-			break;
-		case PROP_CONFIRM_DELETE:
-			priv->confirm_delete = g_value_get_boolean (value);
-			break;
-		default:
-			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-			break;
+	case PROP_UNDO_LEVELS:
+		new_undo_levels = g_value_get_uint (value);
+		if (new_undo_levels > 0 && (priv->undo_levels != new_undo_levels)) {
+			priv->undo_levels = new_undo_levels;
+			g_mutex_lock (priv->mutex);
+			stack_fix_size (priv);
+			g_mutex_unlock (priv->mutex);
+			do_menu_update (manager);
+		}
+		break;
+	case PROP_CONFIRM_DELETE:
+		priv->confirm_delete = g_value_get_boolean (value);
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+		break;
 	}
 }
 
 static void
-nautilus_undo_stack_manager_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
+nautilus_undo_stack_manager_get_property (GObject *object,
+					  guint prop_id,
+					  GValue *value,
+					  GParamSpec *pspec)
 {
 	NautilusUndoStackManager *manager;
 	NautilusUndoStackManagerPrivate *priv;
@@ -291,14 +276,72 @@ nautilus_undo_stack_manager_get_property (GObject * object, guint prop_id, GValu
 	priv = manager->priv;
 
 	switch (prop_id) {
-		case PROP_UNDO_LEVELS:
-			g_value_set_uint (value, priv->undo_levels);
-			break;
+	case PROP_UNDO_LEVELS:
+		g_value_set_uint (value, priv->undo_levels);
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+		break;
+	}
+}
 
-		default:
-			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-			break;
+static GObject *
+nautilus_undo_stack_manager_constructor (GType type,
+					 guint n_construct_params,
+					 GObjectConstructParam *construct_params)
+{
+	GObject *retval;
+
+	if (singleton != NULL) {
+		return g_object_ref (singleton);
 	}
+
+	retval = G_OBJECT_CLASS (nautilus_undo_stack_manager_parent_class)->constructor
+		(type, n_construct_params, construct_params);
+
+	singleton = NAUTILUS_UNDO_STACK_MANAGER (retval);
+	g_object_add_weak_pointer (retval, (gpointer) &singleton);
+
+	return retval;
+}
+
+static void
+nautilus_undo_stack_manager_class_init (NautilusUndoStackManagerClass *klass)
+{
+	GObjectClass *oclass;
+
+	oclass = G_OBJECT_CLASS (klass);
+
+	oclass->constructor = nautilus_undo_stack_manager_constructor;
+	oclass->finalize = nautilus_undo_stack_manager_finalize;
+	oclass->set_property = nautilus_undo_stack_manager_set_property;
+	oclass->get_property = nautilus_undo_stack_manager_get_property;
+
+
+	/* Create properties */
+	properties[PROP_UNDO_LEVELS] =
+		g_param_spec_uint ("undo-levels",
+				   "undo levels",
+				   "Number of undo levels to be stored",
+				   1, UINT_MAX, DEFAULT_UNDO_DEPTH,
+				   G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
+	properties[PROP_CONFIRM_DELETE] =
+		g_param_spec_boolean ("confirm-delete",
+				      "confirm delete",
+				      "Always confirm file deletion",
+				      FALSE,
+				      G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
+
+	g_object_class_install_properties (oclass, NUM_PROPERTIES, properties);
+
+	/* The UI menu needs to update its status */
+	g_signal_new ("request-menu-update",
+			G_TYPE_FROM_CLASS (klass),
+			G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
+			0, NULL, NULL,
+			g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
+
+	g_type_class_add_private (klass, sizeof (NautilusUndoStackManagerPrivate));
 }
 
 /**
@@ -311,11 +354,11 @@ nautilus_undo_stack_manager_get_property (GObject * object, guint prop_id, GValu
 NautilusUndoStackManager *
 nautilus_undo_stack_manager_get (void)
 {
-	static NautilusUndoStackManager *manager = NULL;
+	NautilusUndoStackManager *manager;
 
-	if (manager == NULL) {
-		manager = g_object_new (NAUTILUS_TYPE_UNDO_STACK_MANAGER, "undo-levels", DEFAULT_UNDO_DEPTH, NULL);
-	}
+	manager = g_object_new (NAUTILUS_TYPE_UNDO_STACK_MANAGER,
+				"undo-levels", DEFAULT_UNDO_DEPTH,
+				NULL);
 
 	return manager;
 }
@@ -393,7 +436,7 @@ nautilus_undo_stack_manager_redo (NautilusUndoStackManager        *manager,
 			case NAUTILUS_UNDO_STACK_COPY:
 				uris = construct_gfile_list (action->sources, action->src_dir);
 				nautilus_file_operations_copy (uris, NULL, action->dest_dir, NULL, undo_redo_done_transfer_callback, action);
-				eel_g_object_list_free (uris);
+				g_list_free_full (uris, g_object_unref);
 				break;
 			case NAUTILUS_UNDO_STACK_CREATE_FILE_FROM_TEMPLATE:
 			{
@@ -413,13 +456,13 @@ nautilus_undo_stack_manager_redo (NautilusUndoStackManager        *manager,
 			case NAUTILUS_UNDO_STACK_DUPLICATE:
 				uris = construct_gfile_list (action->sources, action->src_dir);
 				nautilus_file_operations_duplicate (uris, NULL, NULL, undo_redo_done_transfer_callback, action);
-				eel_g_object_list_free (uris);
+				g_list_free_full (uris, g_object_unref);
 				break;
 			case NAUTILUS_UNDO_STACK_RESTORE_FROM_TRASH:
 			case NAUTILUS_UNDO_STACK_MOVE:
 				uris = construct_gfile_list (action->sources, action->src_dir);
 				nautilus_file_operations_move (uris, NULL, action->dest_dir, NULL, undo_redo_done_transfer_callback, action);
-				eel_g_object_list_free (uris);
+				g_list_free_full (uris, g_object_unref);
 				break;
 			case NAUTILUS_UNDO_STACK_RENAME:
 				new_name = g_file_get_basename (action->new_file);
@@ -458,13 +501,13 @@ nautilus_undo_stack_manager_redo (NautilusUndoStackManager        *manager,
 					uris = uri_list_to_gfile_list (uri_to_trash);
 					nautilus_file_operations_trash_or_delete (uris, NULL, undo_redo_done_delete_callback, action);
 					g_list_free (uri_to_trash);
-					eel_g_object_list_free (uris);
+					g_list_free_full (uris, g_object_unref);
 				}
 				break;
 			case NAUTILUS_UNDO_STACK_CREATE_LINK:
 				uris = construct_gfile_list (action->sources, action->src_dir);
 				nautilus_file_operations_link (uris, NULL, action->dest_dir, NULL, undo_redo_done_transfer_callback, action);
-				eel_g_object_list_free (uris);
+				g_list_free_full (uris, g_object_unref);
 				break;
 			case NAUTILUS_UNDO_STACK_SET_PERMISSIONS:
 				file = nautilus_file_get (action->target_file);
@@ -561,7 +604,7 @@ nautilus_undo_stack_manager_undo (NautilusUndoStackManager        *manager,
 				}
 				if (priv->confirm_delete) {
 					nautilus_file_operations_delete (uris, NULL, undo_redo_done_delete_callback, action);
-					eel_g_object_list_free (uris);
+					g_list_free_full (uris, g_object_unref);
 				} else {
 					priv->undo_redo_flag = FALSE;
 					/* We skip the confirmation message */
@@ -578,7 +621,7 @@ nautilus_undo_stack_manager_undo (NautilusUndoStackManager        *manager,
 			case NAUTILUS_UNDO_STACK_RESTORE_FROM_TRASH:
 				uris = construct_gfile_list (action->destinations, action->dest_dir);
 				nautilus_file_operations_trash_or_delete (uris, NULL, undo_redo_done_delete_callback, action);
-				eel_g_object_list_free (uris);
+				g_list_free_full (uris, g_object_unref);
 				break;
 			case NAUTILUS_UNDO_STACK_MOVE_TO_TRASH:
 				/* Internally managed op, clear the undo_redo_flag.
@@ -611,7 +654,7 @@ nautilus_undo_stack_manager_undo (NautilusUndoStackManager        *manager,
 			case NAUTILUS_UNDO_STACK_MOVE:
 				uris = construct_gfile_list (action->destinations, action->dest_dir);
 				nautilus_file_operations_move (uris, NULL, action->src_dir, NULL, undo_redo_done_transfer_callback, action);
-				eel_g_object_list_free (uris);
+				g_list_free_full (uris, g_object_unref);
 				break;
 			case NAUTILUS_UNDO_STACK_RENAME:
 				new_name = g_file_get_basename (action->old_file);
@@ -1707,61 +1750,6 @@ undo_redo_done_rename_callback (NautilusFile * file, GFile * result_location, GE
 	undo_redo_done_transfer_callback (NULL, callback_data);
 }
 
-static void
-free_undo_stack_action_data (NautilusUndoStackActionData *action)
-{
-	g_return_if_fail (action != NULL);
-
-	g_free (action->template);
-
-	g_free (action->undo_label);
-	g_free (action->undo_description);
-	g_free (action->redo_label);
-	g_free (action->redo_description);
-
-	g_free (action->original_group_name);
-	g_free (action->original_user_name);
-	g_free (action->new_group_name);
-	g_free (action->new_user_name);
-
-	if (action->sources) {
-		g_list_foreach (action->sources, (GFunc) g_free, NULL);
-		g_list_free (action->sources);
-	}
-	if (action->destinations) {
-		g_list_foreach (action->destinations, (GFunc) g_free, NULL);
-		g_list_free (action->destinations);
-	}
-
-	if (action->trashed) {
-		g_hash_table_destroy (action->trashed);
-	}
-
-	if (action->original_permissions) {
-		g_hash_table_destroy (action->original_permissions);
-	}
-
-	if (action->old_file)
-		g_object_unref (action->old_file);
-	if (action->new_file)
-		g_free (action->new_file);
-
-	if (action->target_file)
-		g_object_unref (action->target_file);
-	if (action->src_dir)
-		g_object_unref (action->src_dir);
-	if (action->dest_dir)
-		g_object_unref (action->dest_dir);
-
-	g_slice_free (NautilusUndoStackActionData, action);
-}
-
-static void
-undostack_dispose_all (GQueue * queue)
-{
-	g_queue_foreach (queue, (GFunc)free_undo_stack_action_data, NULL);
-}
-
 static gboolean
 can_undo (NautilusUndoStackManagerPrivate *priv)
 {



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