[anjuta] am-project: Move save code in node objects



commit 84098a2949253463cc3a8fb83a04a0e5f815c7d9
Author: Sébastien Granjoux <seb sfo free fr>
Date:   Sun Jan 2 22:00:41 2011 +0100

    am-project: Move save code in node objects

 libanjuta/anjuta-token-file.c     |   20 +++++-
 libanjuta/anjuta-token-file.h     |    1 +
 plugins/am-project/am-project.c   |  114 +++++++++----------------------------
 plugins/am-project/am-project.h   |    1 -
 plugins/am-project/amp-group.c    |   26 ++++++++-
 plugins/am-project/amp-node.c     |   24 ++++++++-
 plugins/am-project/amp-node.h     |   10 +++
 plugins/am-project/tests/group.at |    1 +
 8 files changed, 103 insertions(+), 94 deletions(-)
---
diff --git a/libanjuta/anjuta-token-file.c b/libanjuta/anjuta-token-file.c
index 045cb8f..cbc5053 100644
--- a/libanjuta/anjuta-token-file.c
+++ b/libanjuta/anjuta-token-file.c
@@ -1,5 +1,4 @@
 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
-/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
 /*
  * anjuta-token-file.c
  * Copyright (C) Sébastien Granjoux 2009 <seb sfo free fr>
@@ -39,6 +38,8 @@ struct _AnjutaTokenFile
 	AnjutaToken *content;		/* Current file content */
 
 	AnjutaToken *save;			/* List of memory block used */
+
+	gboolean dirty;					/* Set when the file has been modified */
 };
 
 struct _AnjutaTokenFileClass
@@ -111,7 +112,7 @@ anjuta_token_file_load (AnjutaTokenFile *file, GError **error)
 	
 	file->save = anjuta_token_new_static (ANJUTA_TOKEN_FILE,  NULL);
 	file->content = anjuta_token_new_static (ANJUTA_TOKEN_FILE,  NULL);
-	
+
 	if (g_file_load_contents (file->file, NULL, &content, &length, NULL, error))
 	{
 		AnjutaToken *token;
@@ -121,6 +122,7 @@ anjuta_token_file_load (AnjutaTokenFile *file, GError **error)
 		
 		token =	anjuta_token_new_static (ANJUTA_TOKEN_FILE, content);
 		anjuta_token_prepend_child (file->content, token);
+		file->dirty = FALSE;
 	}
 	
 	return file->content;
@@ -153,7 +155,7 @@ anjuta_token_file_save (AnjutaTokenFile *file, GError **error)
 		{
 			/* Perhaps parent directory is missing, try to create it */
 			GFile *parent = g_file_get_parent (file->file);
-			
+
 			if (g_file_make_directory_with_parents (parent, NULL, NULL))
 			{
 				g_object_unref (parent);
@@ -190,6 +192,7 @@ anjuta_token_file_save (AnjutaTokenFile *file, GError **error)
 		
 	ok = ok && g_output_stream_close (G_OUTPUT_STREAM (stream), NULL, NULL);
 	g_object_unref (stream);
+	file->dirty = FALSE;
 	
 	return ok;
 }
@@ -199,6 +202,7 @@ anjuta_token_file_move (AnjutaTokenFile *file, GFile *new_file)
 {
 	if (file->file) g_object_unref (file->file);
 	file->file = new_file != NULL ? g_object_ref (new_file) : NULL;
+	file->dirty = new_file != NULL;
 }
 
 static void
@@ -236,6 +240,7 @@ anjuta_token_file_remove_token (AnjutaTokenFile *file, AnjutaToken *token)
 	if (token != NULL) next = anjuta_token_foreach_post_order (token, remove_raw_token, file);
 
 	next = anjuta_token_free (token);
+	file->dirty = TRUE;
 
 	return next;
 }
@@ -366,7 +371,7 @@ anjuta_token_file_update (AnjutaTokenFile *file, AnjutaToken *token)
 			}
 		}
 	}
-
+	file->dirty = TRUE;
 	
 	return TRUE;
 }
@@ -447,6 +452,12 @@ anjuta_token_file_get_content (AnjutaTokenFile *file)
 	return file->content;
 }
 
+gboolean
+anjuta_token_file_is_dirty (AnjutaTokenFile *file)
+{
+	return file->dirty;
+}
+
 /* GObject functions
  *---------------------------------------------------------------------------*/
 
@@ -532,6 +543,7 @@ anjuta_token_file_new (GFile *gfile)
 	AnjutaTokenFile *file = g_object_new (ANJUTA_TOKEN_FILE_TYPE, NULL);
 
 	if (gfile) file->file =  g_object_ref (gfile);
+	file->dirty = gfile != NULL;
 	
 	return file;
 };
diff --git a/libanjuta/anjuta-token-file.h b/libanjuta/anjuta-token-file.h
index 1061d86..63a0cbf 100644
--- a/libanjuta/anjuta-token-file.h
+++ b/libanjuta/anjuta-token-file.h
@@ -59,6 +59,7 @@ gboolean anjuta_token_file_update (AnjutaTokenFile *file, AnjutaToken *token);
 gboolean anjuta_token_file_get_token_location (AnjutaTokenFile *file, AnjutaTokenFileLocation *location, AnjutaToken *token);
 GFile *anjuta_token_file_get_file (AnjutaTokenFile *file);
 AnjutaToken *anjuta_token_file_get_content (AnjutaTokenFile *file);
+gboolean anjuta_token_file_is_dirty (AnjutaTokenFile *file);
 
 
 G_END_DECLS
diff --git a/plugins/am-project/am-project.c b/plugins/am-project/am-project.c
index bc9a7dd..5036d9d 100644
--- a/plugins/am-project/am-project.c
+++ b/plugins/am-project/am-project.c
@@ -668,65 +668,6 @@ find_canonical_target (AnjutaProjectNode *node, gpointer data)
  * ---------------- Data structures managment
  */
 
-/* Save a node. If the node doesn't correspond to a file, get its parent until
- * a file node is found and save it. The node returned correspond to the
- * file node */
-static AnjutaProjectNode *
-project_node_save (AmpProject *project, AnjutaProjectNode *node, GError **error)
-{
-	GList *list;
-	GHashTableIter iter;
-	GHashTable *files;
-	AnjutaProjectNode *parent;
-	gpointer key;
-	gpointer value;
-
-	g_return_val_if_fail (project != NULL, FALSE);
-
-	/* Create a hash table because some node can be split in several files and to
-	 * avoid duplicate, not sure if it is really necessary */
-	files = 	g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, NULL);
-	
-	switch (anjuta_project_node_get_node_type (node)) {
-		case ANJUTA_PROJECT_GROUP:
-			g_hash_table_insert (files, amp_group_node_get_make_token_file (AMP_GROUP_NODE (node)), NULL);
-			g_hash_table_insert (files, project->configure_file, NULL);
-			break;
-		case ANJUTA_PROJECT_TARGET:
-		case ANJUTA_PROJECT_SOURCE:
-			for (parent = node; anjuta_project_node_get_node_type (parent) != ANJUTA_PROJECT_GROUP; parent = anjuta_project_node_parent (parent));
-			g_hash_table_insert (files, amp_group_node_get_make_token_file (AMP_GROUP_NODE (parent)), NULL);
-			break;
-		case ANJUTA_PROJECT_MODULE:
-		case ANJUTA_PROJECT_PACKAGE:
-			/* Save only configure file */
-			g_hash_table_insert (files, project->configure_file, NULL);
-			break;
-		case ANJUTA_PROJECT_ROOT:
-			/* Get all project files */
-			for (list = project->files; list != NULL; list = g_list_next (list))
-			{
-				g_hash_table_insert (files, list->data, NULL);
-			}
-			break;
-		default:
-			g_assert_not_reached ();
-			break;
-	}
-
-	/* Save all files */
-	g_hash_table_iter_init (&iter, files);
-	while (g_hash_table_iter_next (&iter, &key, &value))
-	{
-		GError *error = NULL;
-		AnjutaTokenFile *tfile = (AnjutaTokenFile *)key;
-
-		anjuta_token_file_save (tfile, &error);
-	}
-
-	return node;
-}
-
 void
 amp_project_load_properties (AmpProject *project, AnjutaToken *macro, AnjutaToken *args)
 {
@@ -1793,24 +1734,6 @@ amp_project_get_node_info (AmpProject *project, GError **error)
 /* Public functions
  *---------------------------------------------------------------------------*/
 
-gboolean
-amp_project_save (AmpProject *project, GError **error)
-{
-	GList *list;
-
-	g_return_val_if_fail (project != NULL, FALSE);
-
-	for (list = project->files; list != NULL; list = g_list_next (list))
-	{
-		GError *error = NULL;
-		AnjutaTokenFile *tfile = (AnjutaTokenFile *)list->data;
-
-		anjuta_token_file_save (tfile, &error);
-	}
-
-	return TRUE;
-}
-
 typedef struct _AmpMovePacket {
 	AmpProject *project;
 	GFile *old_root_file;
@@ -2012,7 +1935,7 @@ amp_project_add_file (AmpProject *project, GFile *file, AnjutaTokenFile* token)
 gboolean
 amp_project_is_busy (AmpProject *project)
 {
-	if (project->queue == NULL) project->queue = pm_command_queue_new ();
+	if (project->queue == NULL) return FALSE;
 
 	return pm_command_queue_is_busy (project->queue);
 }
@@ -2079,15 +2002,8 @@ amp_save_setup (PmJob *job)
 static gboolean
 amp_save_work (PmJob *job)
 {
-	switch (anjuta_project_node_get_node_type (job->node))
-	{
-		case ANJUTA_PROJECT_ROOT:
-			amp_project_save (AMP_PROJECT (job->user_data), &job->error);
-			break;
-		default:
-			project_node_save (AMP_PROJECT (job->user_data), job->node, &job->error);
-			break;
-	}
+	/* It is difficult to save only a particular node, so the whole project is saved */
+	amp_node_save (AMP_NODE (job->user_data), NULL, AMP_PROJECT (job->user_data), &job->error);
 
 	return TRUE;
 }
@@ -2392,6 +2308,29 @@ amp_project_load (AmpNode *root, AmpNode *parent, AmpProject *project, GError **
 }
 
 static gboolean
+amp_project_save (AmpNode *root, AmpNode *parent, AmpProject *project, GError **error)
+{
+	AnjutaTokenFile *tfile;
+	AnjutaProjectNode *child;
+
+	/* Save node */
+	tfile = AMP_PROJECT (root)->configure_file;
+	if (anjuta_token_file_is_dirty (tfile))
+	{
+		if (!anjuta_token_file_save (tfile, error)) return FALSE;
+	}
+
+	/* Save all children */
+	for (child = anjuta_project_node_first_child (ANJUTA_PROJECT_NODE (root)); child != NULL; child = anjuta_project_node_next_sibling (child))
+	{
+		if (!amp_node_save (AMP_NODE (child), root, project, error)) return FALSE;
+	}
+
+	return TRUE;
+}
+
+
+static gboolean
 amp_project_update (AmpNode *node, AmpNode *new_node)
 {
 	amp_project_update_root (AMP_PROJECT (node), AMP_PROJECT (new_node));
@@ -2469,6 +2408,7 @@ amp_project_class_init (AmpProjectClass *klass)
 
 	node_class = AMP_NODE_CLASS (klass);
 	node_class->load = amp_project_load;
+	node_class->save = amp_project_save;
 	node_class->update = amp_project_update;
 }
 
diff --git a/plugins/am-project/am-project.h b/plugins/am-project/am-project.h
index dadf64c..e367094 100644
--- a/plugins/am-project/am-project.h
+++ b/plugins/am-project/am-project.h
@@ -83,7 +83,6 @@ AmpSourceNode *amp_project_get_source (AmpProject *project, const gchar *id);
 gboolean amp_project_get_token_location (AmpProject *project, AnjutaTokenFileLocation *location, AnjutaToken *token);
 
 gboolean amp_project_move (AmpProject *project, const gchar *path);
-gboolean amp_project_save (AmpProject *project, GError **error);
 
 gboolean amp_project_dump (AmpProject *project, AnjutaProjectNode *node);
 
diff --git a/plugins/am-project/amp-group.c b/plugins/am-project/amp-group.c
index a70c6f3..ded6c72 100644
--- a/plugins/am-project/amp-group.c
+++ b/plugins/am-project/amp-group.c
@@ -539,7 +539,7 @@ amp_group_node_new (GFile *file, gboolean dist_only, GError **error)
 		if (failed) {
 			g_free (name);
 			error_set (error, IANJUTA_PROJECT_ERROR_VALIDATION_FAILED,
-				   _("Group name can only contain alphanumeric or \"#$:%+,- = ^_`~\" characters"));
+			           _("Group name can only contain alphanumeric or \"#$:%+,- = ^_`~\" characters"));
 			return NULL;
 		}
 	}
@@ -577,6 +577,29 @@ amp_group_node_load (AmpNode *group, AmpNode *parent, AmpProject *project, GErro
 }
 
 static gboolean
+amp_group_node_save (AmpNode *group, AmpNode *parent, AmpProject *project, GError **error)
+{
+	AnjutaTokenFile *tfile;
+	AnjutaProjectNode *child;
+
+	/* Save group */
+	tfile = AMP_GROUP_NODE (group)->tfile;
+	if (tfile == NULL) return FALSE;
+	if (anjuta_token_file_is_dirty (tfile))
+	{
+		if (!anjuta_token_file_save (tfile, error)) return FALSE;
+	}
+
+	/* Save all children */
+	for (child = anjuta_project_node_first_child (ANJUTA_PROJECT_NODE (group)); child != NULL; child = anjuta_project_node_next_sibling (child))
+	{
+		if (!amp_node_save (AMP_NODE (child), group, project, error)) return FALSE;
+	}
+
+	return TRUE;
+}
+
+static gboolean
 amp_group_node_update (AmpNode *node, AmpNode *new_node)
 {
 	amp_group_node_update_node (AMP_GROUP_NODE (node), AMP_GROUP_NODE (new_node));
@@ -669,6 +692,7 @@ amp_group_node_class_init (AmpGroupNodeClass *klass)
 
 	node_class = AMP_NODE_CLASS (klass);
 	node_class->load = amp_group_node_load;
+	node_class->save = amp_group_node_save;
 	node_class->update = amp_group_node_update;
 	node_class->write = amp_group_node_write;
 	node_class->erase = amp_group_node_erase;
diff --git a/plugins/am-project/amp-node.c b/plugins/am-project/amp-node.c
index 13e1d23..eb4f3fb 100644
--- a/plugins/am-project/amp-node.c
+++ b/plugins/am-project/amp-node.c
@@ -111,6 +111,17 @@ amp_node_load (AmpNode *node,
 }
 
 gboolean
+amp_node_save (AmpNode *node,
+               AmpNode *parent,
+               AmpProject *project,
+               GError **error)
+{
+  g_return_val_if_fail (AMP_IS_NODE (node), FALSE);
+
+  return AMP_NODE_GET_CLASS (node)->save (node, parent, project, error);
+}
+
+gboolean
 amp_node_update (AmpNode *node,
                  AmpNode *new_node)
 {
@@ -150,7 +161,17 @@ amp_node_real_load (AmpNode *node,
                     AmpProject *project,
                     GError **error)
 {
-		return FALSE;
+	return FALSE;
+}
+
+static gboolean 
+amp_node_real_save (AmpNode *node,
+                    AmpNode *parent,
+                    AmpProject *project,
+                    GError **error)
+{
+	/* Save nothing by default */
+	return TRUE;
 }
 
 static gboolean
@@ -207,6 +228,7 @@ amp_node_class_init (AmpNodeClass *klass)
 	object_class->finalize = amp_node_finalize;
 
 	klass->load = amp_node_real_load;
+	klass->save = amp_node_real_save;
 	klass->update = amp_node_real_update;
 	klass->write = amp_node_real_write;
 	klass->erase = amp_node_real_erase;
diff --git a/plugins/am-project/amp-node.h b/plugins/am-project/amp-node.h
index 3a9ecde..224b04e 100644
--- a/plugins/am-project/amp-node.h
+++ b/plugins/am-project/amp-node.h
@@ -65,6 +65,11 @@ struct _AmpNodeClass
 											                AmpProject *project,
 											                GError **error);
 
+	gboolean					(*save)					(AmpNode *node,
+											                AmpNode *parent,
+											                AmpProject *project,
+											                GError **error);
+	
 	gboolean					(*update)				(AmpNode *node,
 											                AmpNode *parent);
 
@@ -93,6 +98,11 @@ gboolean						amp_node_load					(AmpNode *node,
 											                      AmpProject *project,
 											                      GError **error);
 
+gboolean						amp_node_save					(AmpNode *node,
+											                      AmpNode *parent,
+											                      AmpProject *project,
+											                      GError **error);
+
 gboolean						amp_node_update					(AmpNode *node,
 											                      AmpNode *new_node);
 
diff --git a/plugins/am-project/tests/group.at b/plugins/am-project/tests/group.at
index 2ce88b8..428d935 100644
--- a/plugins/am-project/tests/group.at
+++ b/plugins/am-project/tests/group.at
@@ -30,6 +30,7 @@ AT_PARSER_CHECK([load empty \
 AT_CHECK([diff output expect])
 AT_CHECK([diff -b empty1/configure.ac reference.ac])
 AT_CHECK([diff -b empty1/Makefile.am reference.am])
+AT_CHECK([test -e empty1/group1/Makefile.am])
 AT_PARSER_CHECK([load empty1 \
 		 list])
 AT_CHECK([diff output expect])



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