[anjuta] am-project: Fix adding source files not in the current directory



commit e335aeaf232881b7dad6680e3bed77756d2cd224
Author: Sébastien Granjoux <seb sfo free fr>
Date:   Sat Jan 8 19:23:39 2011 +0100

    am-project: Fix adding source files not in the current directory

 plugins/am-project/am-project.c   |   25 ++++++++++------
 plugins/am-project/am-writer.c    |    4 +-
 plugins/am-project/amp-group.c    |    2 +-
 plugins/am-project/amp-node.c     |   57 ++++++++++++++++++++++++++++++++----
 plugins/am-project/amp-package.c  |    4 +-
 plugins/project-manager/project.c |   11 ++++++-
 6 files changed, 80 insertions(+), 23 deletions(-)
---
diff --git a/plugins/am-project/am-project.c b/plugins/am-project/am-project.c
index 5036d9d..59e54ba 100644
--- a/plugins/am-project/am-project.c
+++ b/plugins/am-project/am-project.c
@@ -969,33 +969,34 @@ project_load_sources (AmpProject *project, AnjutaToken *name, AnjutaToken *list,
 	if (target_id)
 	{
 		gpointer find;
+		AnjutaProjectNode *target;
 		
 		find = target_id;
 		DEBUG_PRINT ("search for canonical %s", target_id);
 		anjuta_project_node_children_traverse (parent, find_canonical_target, &find);
-		parent = (gchar *)find != target_id ? (AnjutaProjectNode *)find : NULL;
+		target = (gchar *)find != target_id ? (AnjutaProjectNode *)find : NULL;
 
 		/* Get orphan buffer if there is no target */
-		if (parent == NULL)
+		if (target == NULL)
 		{
 			gchar *orig_key;
 			
-			if (g_hash_table_lookup_extended (orphan_properties, target_id, (gpointer *)&orig_key, (gpointer *)&parent))
+			if (g_hash_table_lookup_extended (orphan_properties, target_id, (gpointer *)&orig_key, (gpointer *)&target))
 			{
 				g_hash_table_steal (orphan_properties, target_id);
 				g_free (orig_key);
 			}
 			else
 			{
-				parent = ANJUTA_PROJECT_NODE (amp_target_node_new ("dummy", 0, NULL, 0, NULL));
+				target = ANJUTA_PROJECT_NODE (amp_target_node_new ("dummy", 0, NULL, 0, NULL));
 			}
-			g_hash_table_insert (orphan_properties, target_id, parent);
+			g_hash_table_insert (orphan_properties, target_id, target);
 		}
 		else
 		{
 			g_free (target_id);
 		}
-		amp_target_node_add_token (AMP_TARGET_NODE (parent), AM_TOKEN__SOURCES, name);
+		amp_target_node_add_token (AMP_TARGET_NODE (target), AM_TOKEN__SOURCES, name);
 		
 		for (arg = anjuta_token_first_word (list); arg != NULL; arg = anjuta_token_next_word (arg))
 		{
@@ -1008,13 +1009,13 @@ project_load_sources (AmpProject *project, AnjutaToken *name, AnjutaToken *list,
 
 			/* Create source */
 			src_file = g_file_get_child (parent_file, value);
-			source = amp_node_new (NULL, ANJUTA_PROJECT_SOURCE | ANJUTA_PROJECT_PROJECT, src_file, NULL, NULL);
+			source = amp_node_new (parent, ANJUTA_PROJECT_SOURCE | ANJUTA_PROJECT_PROJECT, src_file, NULL, NULL);
 			g_object_unref (src_file);
 			amp_source_node_add_token (AMP_SOURCE_NODE (source), arg);
 	
-			DEBUG_PRINT ("add target child %p", parent);
+			DEBUG_PRINT ("add target child %p", target);
 			/* Add as target child */
-			anjuta_project_node_append (parent, source);
+			anjuta_project_node_append (target, source);
 
 			g_free (value);
 		}
@@ -1078,6 +1079,8 @@ project_load_data (AmpProject *project, AnjutaToken *name, AnjutaToken *list, An
 		GFile *parent_file = g_object_ref (anjuta_project_node_get_file (parent));
 		
 		amp_target_node_add_token (AMP_TARGET_NODE (target), AM_TOKEN__DATA, name);
+		amp_target_node_add_token (AMP_TARGET_NODE (target), AM_TOKEN__SOURCES, name);
+		
 		for (arg = anjuta_token_first_word (list); arg != NULL; arg = anjuta_token_next_word (arg))
 		{
 			gchar *value;
@@ -1089,7 +1092,7 @@ project_load_data (AmpProject *project, AnjutaToken *name, AnjutaToken *list, An
 
 			/* Create source */
 			src_file = g_file_get_child (parent_file, value);
-			source = amp_node_new (NULL, ANJUTA_PROJECT_SOURCE | ANJUTA_PROJECT_PROJECT, src_file, NULL, NULL);
+			source = amp_node_new (parent, ANJUTA_PROJECT_SOURCE | ANJUTA_PROJECT_PROJECT, src_file, NULL, NULL);
 			g_object_unref (src_file);
 			amp_source_node_add_token (AMP_SOURCE_NODE(source), arg);
 
@@ -1492,6 +1495,8 @@ amp_project_duplicate_node (AnjutaProjectNode *old_node)
 	{
 		amp_target_node_set_type (AMP_TARGET_NODE (new_node), anjuta_project_node_get_full_type (old_node));
 	}
+	/* Keep old parent, Needed for source node to find project root node */
+	new_node->parent = old_node->parent;
 
 	return new_node;
 }
diff --git a/plugins/am-project/am-writer.c b/plugins/am-project/am-writer.c
index 3a73792..ca0ae28 100644
--- a/plugins/am-project/am-writer.c
+++ b/plugins/am-project/am-writer.c
@@ -676,8 +676,8 @@ amp_source_node_create_token (AmpProject  *project, AmpSourceNode *source, GErro
 	if ((target == NULL) || (anjuta_project_node_get_node_type (ANJUTA_PROJECT_NODE (target)) != ANJUTA_PROJECT_TARGET)) return FALSE;
 	
 	group = AMP_GROUP_NODE (anjuta_project_node_parent (ANJUTA_PROJECT_NODE (target)));
-	relative_name = g_file_get_relative_path (anjuta_project_node_get_file (ANJUTA_PROJECT_NODE (group)), anjuta_project_node_get_file (ANJUTA_PROJECT_NODE (source)));
-
+	relative_name = get_relative_path (anjuta_project_node_get_file (ANJUTA_PROJECT_NODE (group)), anjuta_project_node_get_file (ANJUTA_PROJECT_NODE (source)));
+	
 	/* Add in Makefile.am */
 	/* Find a sibling if possible */
 	after = TRUE;
diff --git a/plugins/am-project/amp-group.c b/plugins/am-project/amp-group.c
index ded6c72..3e35797 100644
--- a/plugins/am-project/amp-group.c
+++ b/plugins/am-project/amp-group.c
@@ -170,7 +170,7 @@ project_load_group_module (AmpProject *project, AmpGroupNode *group)
 								/* Add new module */
 								AnjutaProjectNode *new_module;
 
-								new_module = amp_node_new (NULL, ANJUTA_PROJECT_MODULE, NULL, name, NULL);
+								new_module = amp_node_new (target, ANJUTA_PROJECT_MODULE, NULL, name, NULL);
 								anjuta_project_node_append (target, new_module);
 							}
 							g_free (cpp_flags);
diff --git a/plugins/am-project/amp-node.c b/plugins/am-project/amp-node.c
index eb4f3fb..dfa308e 100644
--- a/plugins/am-project/amp-node.c
+++ b/plugins/am-project/amp-node.c
@@ -55,7 +55,14 @@ amp_node_new (AnjutaProjectNode *parent, AnjutaProjectNodeType type, GFile *file
 		case ANJUTA_PROJECT_GROUP:
 			if ((file == NULL) && (name != NULL))
 			{
-				new_file = g_file_get_child (anjuta_project_node_get_file (parent), name);
+				if (g_path_is_absolute (name))
+				{
+					new_file = g_file_new_for_path (name);
+				}
+				else
+				{
+					new_file = g_file_get_child (anjuta_project_node_get_file (parent), name);
+				}
 				file = new_file;
 			}
 			node = ANJUTA_PROJECT_NODE (amp_group_node_new (file, FALSE, error));
@@ -64,16 +71,25 @@ amp_node_new (AnjutaProjectNode *parent, AnjutaProjectNodeType type, GFile *file
 			node = ANJUTA_PROJECT_NODE (amp_target_node_new (name, type, NULL, 0, error));
 			break;
 		case ANJUTA_PROJECT_SOURCE:
+			/* Look for parent */
+			if (anjuta_project_node_get_node_type (parent) == ANJUTA_PROJECT_TARGET)
+			{
+				parent = anjuta_project_node_parent (parent);
+			}
+			
 			if ((file == NULL) && (name != NULL))
 			{
-				/* Look for parent */
-				if (anjuta_project_node_get_node_type (parent) == ANJUTA_PROJECT_TARGET)
-				{
-					parent = anjuta_project_node_parent (parent);
-				}
+				/* Find file from name */
 				if (anjuta_project_node_get_node_type (parent) == ANJUTA_PROJECT_GROUP)
 				{
-					new_file = g_file_get_child (anjuta_project_node_get_file (parent), name);
+					if (g_path_is_absolute (name))
+					{
+						new_file = g_file_new_for_path (name);
+					}
+					else
+					{
+						new_file = g_file_get_child (anjuta_project_node_get_file (parent), name);
+					}
 				}
 				else
 				{
@@ -81,6 +97,33 @@ amp_node_new (AnjutaProjectNode *parent, AnjutaProjectNodeType type, GFile *file
 				}
 				file = new_file;
 			}
+
+			/* Check that source file is inside the project if it is not belonging to a package */
+			if (anjuta_project_node_get_node_type (parent) == ANJUTA_PROJECT_GROUP)
+			{
+				AnjutaProjectNode *root;
+				gchar *relative;
+				
+				root = anjuta_project_node_root (parent);
+				relative = g_file_get_relative_path (anjuta_project_node_get_file (root), file);
+				g_free (relative);
+				if (relative == NULL)
+				{
+					/* Source outside the project directory, so copy the file */
+					GFile *dest;
+					gchar *basename;
+
+					basename = g_file_get_basename (file);
+					dest = g_file_get_child (anjuta_project_node_get_file (parent), basename);
+					g_free (basename);
+
+					g_file_copy_async (file, dest, G_FILE_COPY_BACKUP, G_PRIORITY_DEFAULT, NULL, NULL, NULL, NULL, NULL);
+					if (new_file != NULL) g_object_unref (new_file);
+					new_file = dest;
+					file = dest;
+				}
+			}
+	
 			node = ANJUTA_PROJECT_NODE (amp_source_node_new (file, error));
 			break;
 		case ANJUTA_PROJECT_MODULE:
diff --git a/plugins/am-project/amp-package.c b/plugins/am-project/amp-package.c
index ca6f9fe..28fd05f 100644
--- a/plugins/am-project/amp-package.c
+++ b/plugins/am-project/amp-package.c
@@ -168,7 +168,7 @@ amp_package_node_load (AmpNode *node, AmpNode *parent, AmpProject *project, GErr
 		/* Create a package node for the depedencies */
 		AnjutaProjectNode *pkg;
 
-		pkg = amp_node_new (NULL, ANJUTA_PROJECT_PACKAGE, NULL, dep->data, NULL);
+		pkg = amp_node_new (ANJUTA_PROJECT_NODE (parent), ANJUTA_PROJECT_PACKAGE, NULL, dep->data, NULL);
 		anjuta_project_node_append (ANJUTA_PROJECT_NODE (node), pkg);
 	}
 	anjuta_util_glist_strings_free (deps);
@@ -197,7 +197,7 @@ amp_package_node_load (AmpNode *node, AmpNode *parent, AmpProject *project, GErr
 				/* Create a source for files */
 				AnjutaProjectNode *source;
 
-				source = amp_node_new (NULL, ANJUTA_PROJECT_SOURCE, (GFile *)file->data, NULL, NULL);
+				source = amp_node_new (ANJUTA_PROJECT_NODE (parent), ANJUTA_PROJECT_SOURCE, (GFile *)file->data, NULL, NULL);
 				anjuta_project_node_append (ANJUTA_PROJECT_NODE (node), source);
 				g_object_unref ((GObject *)file->data);
 			}
diff --git a/plugins/project-manager/project.c b/plugins/project-manager/project.c
index a291b08..be83877 100644
--- a/plugins/project-manager/project.c
+++ b/plugins/project-manager/project.c
@@ -359,10 +359,19 @@ AnjutaProjectNode *
 anjuta_pm_project_add_source (AnjutaPmProject *project, AnjutaProjectNode *parent, AnjutaProjectNode *sibling, const gchar *name, GError **error)
 {
 	AnjutaProjectNode *node;
+	gchar *scheme;
+	GFile *file = NULL;
 
 	g_return_val_if_fail (project->project != NULL, NULL);
+
+	scheme = g_uri_parse_scheme (name);
+	if (scheme != NULL)
+	{
+		g_free (scheme);
+		file = g_file_new_for_uri (name);
+	}
 	
-	node = ianjuta_project_add_node_before (project->project, parent, sibling, ANJUTA_PROJECT_SOURCE, NULL, name, NULL);
+	node = ianjuta_project_add_node_before (project->project, parent, sibling, ANJUTA_PROJECT_SOURCE, file, file == NULL ? name : NULL, NULL);
 
 	return node;
 }



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