[anjuta/newproject] Use new interface for adding group in check program



commit 9870e9e7ef4a53565ee689a1c1d1a01fc9dcf6cd
Author: Sébastien Granjoux <seb sfo free fr>
Date:   Sat Sep 11 11:05:03 2010 +0200

    Use new interface for adding group in check program

 plugins/am-project/am-project.c    |  262 ++++++++++++++++++++++++++++++++++--
 plugins/am-project/projectparser.c |   12 ++-
 2 files changed, 260 insertions(+), 14 deletions(-)
---
diff --git a/plugins/am-project/am-project.c b/plugins/am-project/am-project.c
index d8451f2..4d479f0 100644
--- a/plugins/am-project/am-project.c
+++ b/plugins/am-project/am-project.c
@@ -809,6 +809,16 @@ amp_variable_free (AmpVariable *variable)
  *---------------------------------------------------------------------------*/
 
 static void
+remove_config_file (gpointer data, GObject *object, gboolean is_last_ref)
+{
+	if (is_last_ref)
+	{
+		AmpProject *project = (AmpProject *)data;
+		g_hash_table_remove (project->files, anjuta_token_file_get_file (ANJUTA_TOKEN_FILE (object)));
+	}
+}
+
+static void
 amp_group_add_token (AmpGroup *node, AnjutaToken *token, AmpGroupTokenCategory category)
 {
     AmpGroupData *group;
@@ -936,8 +946,36 @@ static AmpGroup*
 amp_group_new (GFile *file, gboolean dist_only, GError **error)
 {
     AmpGroupData *group = NULL;
+	gchar *name;
 
 	g_return_val_if_fail (file != NULL, NULL);
+
+	/* Validate group name */
+	name = g_file_get_basename (file);
+	if (!name || strlen (name) <= 0)
+	{
+		g_free (name);
+		error_set (error, IANJUTA_PROJECT_ERROR_VALIDATION_FAILED,
+			   _("Please specify group name"));
+		return NULL;
+	}
+	{
+		gboolean failed = FALSE;
+		const gchar *ptr = name;
+		while (*ptr) {
+			if (!isalnum (*ptr) && *ptr != '.' && *ptr != '-' &&
+			    *ptr != '_')
+				failed = TRUE;
+			ptr++;
+		}
+		if (failed) {
+			g_free (name);
+			error_set (error, IANJUTA_PROJECT_ERROR_VALIDATION_FAILED,
+				   _("Group name can only contain alphanumeric, '_', '-' or '.' characters"));
+			return NULL;
+		}
+	}
+	g_free (name);
 	
 	group = g_slice_new0(AmpGroupData); 
 	group->base.type = ANJUTA_PROJECT_GROUP;
@@ -977,6 +1015,199 @@ amp_group_free (AmpGroup *node)
 	g_node_destroy (node);
 }
 
+static gboolean 
+amp_group_fill_token (AmpProject  *project, AmpGroup *group, GError **error)
+{
+	AmpGroup *last;
+	GFile *directory;
+	GFile *makefile;
+	AnjutaToken *list;
+	gchar *basename;
+	gchar *uri;
+	AnjutaTokenFile* tfile;
+	AmpTarget *sibling;
+	AmpGroup *parent;
+	gboolean after;
+	gchar *name;
+	
+	g_return_val_if_fail (name != NULL, NULL);
+	g_return_val_if_fail (parent != NULL, NULL);
+
+	/* Check that the new group doesn't already exist */
+	/*directory = g_file_get_child (AMP_GROUP_DATA (parent)->base.file, name);
+	uri = g_file_get_uri (directory);
+	if (g_hash_table_lookup (project->groups, uri) != NULL)
+	{
+		g_free (uri);
+		error_set (error, IANJUTA_PROJECT_ERROR_DOESNT_EXIST,
+			_("Group already exists"));
+		return NULL;
+	}*/
+
+	/* Get parent target */
+	parent = (AmpGroup *)(group->parent);
+	name = anjuta_project_node_get_name (group);
+	directory = g_file_get_child (AMP_GROUP_DATA (parent)->base.file, name);
+
+	/* Find a sibling if possible */
+	if (group->prev != NULL)
+	{
+		sibling = group->prev;
+		after = TRUE;
+	}
+	else if (group->next != NULL)
+	{
+		sibling = group->next;
+		after = FALSE;
+	}
+	else
+	{
+		sibling = NULL;
+		after = TRUE;
+	}
+	
+	/* Create directory */
+	g_file_make_directory (directory, NULL, NULL);
+
+	/* Create Makefile.am */
+	basename = AMP_GROUP_DATA (parent)->makefile != NULL ? g_file_get_basename (AMP_GROUP_DATA (parent)->makefile) : NULL;
+	if (basename != NULL)
+	{
+		makefile = g_file_get_child (directory, basename);
+		g_free (basename);
+	}
+	else
+	{
+		makefile = g_file_get_child (directory, "Makefile.am");
+	}
+	g_file_replace_contents (makefile, "", 0, NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL, NULL);
+	tfile = amp_group_set_makefile (group, makefile, project);
+	g_hash_table_insert (project->files, makefile, tfile);
+	g_object_add_toggle_ref (G_OBJECT (tfile), remove_config_file, project);
+
+	if (sibling == NULL)
+	{
+		/* Find a sibling before */
+		for (last = anjuta_project_node_prev_sibling (group); (last != NULL) && (anjuta_project_node_get_type (last) != ANJUTA_PROJECT_GROUP); last = anjuta_project_node_prev_sibling (last));
+		if (last != NULL)
+		{
+			sibling = last;
+			after = TRUE;
+		}
+		else
+		{
+			/* Find a sibling after */
+			for (last = anjuta_project_node_next_sibling (group); (last != NULL) && (anjuta_project_node_get_type (last) != ANJUTA_PROJECT_GROUP); last = anjuta_project_node_next_sibling (last));
+			if (last != NULL)
+			{
+				sibling = last;
+				after = FALSE;
+			}
+		}
+	}
+	
+	/* Add in configure */
+	list = NULL;
+	if (sibling) list = amp_group_get_first_token (sibling, AM_GROUP_TOKEN_CONFIGURE);
+	if (list == NULL) list= amp_group_get_first_token (parent, AM_GROUP_TOKEN_CONFIGURE);
+	if (list != NULL) list = anjuta_token_list (list);
+	if (list == NULL)
+	{
+		list = amp_project_write_config_list (project);
+		list = anjuta_token_next (list);
+	}
+	if (list != NULL)
+	{
+		gchar *relative_make;
+		gchar *ext;
+		AnjutaToken *prev = NULL;
+
+		if (sibling)
+		{
+			prev = amp_group_get_first_token (sibling, AM_GROUP_TOKEN_CONFIGURE);
+			/*if ((prev != NULL) && after)
+			{
+				prev = anjuta_token_next_word (prev);
+			}*/
+		}
+		//prev_token = (AnjutaToken *)token_list->data;
+
+		relative_make = g_file_get_relative_path (project->root_file, makefile);
+		ext = relative_make + strlen (relative_make) - 3;
+		if (strcmp (ext, ".am") == 0)
+		{
+			*ext = '\0';
+		}
+		//token = anjuta_token_new_string (ANJUTA_TOKEN_NAME | ANJUTA_TOKEN_ADDED,  relative_make);
+		amp_project_write_config_file (project, list, after, prev, relative_make);
+		g_free (relative_make);
+		
+		//style = anjuta_token_style_new (NULL," ","\n",NULL,0);
+		//anjuta_token_add_word (prev_token, token, style);
+		//anjuta_token_style_free (style);
+	}
+
+	/* Add in Makefile.am */
+	if (sibling == NULL)
+	{
+		AnjutaToken *pos;
+		static gint eol_type[] = {ANJUTA_TOKEN_EOL, ANJUTA_TOKEN_SPACE, ANJUTA_TOKEN_COMMENT, 0};
+	
+		pos = anjuta_token_find_type (AMP_GROUP_DATA (parent)->make_token, ANJUTA_TOKEN_SEARCH_NOT, eol_type);
+		if (pos == NULL)
+		{
+			pos = anjuta_token_prepend_child (AMP_GROUP_DATA (parent)->make_token, anjuta_token_new_static (ANJUTA_TOKEN_SPACE, "\n"));
+		}
+
+		list = anjuta_token_insert_token_list (FALSE, pos,
+		    	ANJUTA_TOKEN_SPACE, "\n");
+		list = anjuta_token_insert_token_list (FALSE, list,
+	    		AM_TOKEN_SUBDIRS, "SUBDIRS",
+		    	ANJUTA_TOKEN_SPACE, " ",
+		    	ANJUTA_TOKEN_OPERATOR, "=",
+	    		ANJUTA_TOKEN_LIST, NULL,
+	    		ANJUTA_TOKEN_LAST, NULL,
+	    		NULL);
+		list = anjuta_token_next (anjuta_token_next ( anjuta_token_next (list)));
+	}
+	else
+	{
+		AnjutaToken *prev;
+		
+		prev = amp_group_get_first_token (sibling, AM_GROUP_TOKEN_SUBDIRS);
+		list = anjuta_token_list (prev);
+	}
+
+	if (list != NULL)
+	{
+		AnjutaToken *token;
+		AnjutaToken *prev;
+
+		if (sibling)
+		{
+			prev = amp_group_get_first_token (sibling, AM_GROUP_TOKEN_SUBDIRS);
+		}		
+		
+		token = anjuta_token_new_string (ANJUTA_TOKEN_NAME | ANJUTA_TOKEN_ADDED, name);
+		if (after)
+		{
+			anjuta_token_insert_word_after (list, prev, token);
+		}
+		else
+		{
+			anjuta_token_insert_word_before (list, prev, token);
+		}
+	
+		anjuta_token_style_format (project->am_space_list, list);
+		anjuta_token_file_update (AMP_GROUP_DATA (parent)->tfile, token);
+		
+		amp_group_add_token (group, token, AM_GROUP_TOKEN_SUBDIRS);
+	}
+	g_free (name);
+
+	return TRUE;
+}
+
 /* Target objects
  *---------------------------------------------------------------------------*/
 
@@ -2203,16 +2434,6 @@ project_load_subdirs (AmpProject *project, AnjutaToken *list, AmpGroup *parent,
 	}
 }
 
-static void
-remove_config_file (gpointer data, GObject *object, gboolean is_last_ref)
-{
-	if (is_last_ref)
-	{
-		AmpProject *project = (AmpProject *)data;
-		g_hash_table_remove (project->files, anjuta_token_file_get_file (ANJUTA_TOKEN_FILE (object)));
-	}
-}
-
 static AmpGroup*
 project_load_makefile (AmpProject *project, AmpGroup *group)
 {
@@ -3742,6 +3963,10 @@ iproject_save_node (IAnjutaProject *obj, AnjutaProjectNode *node, GError **error
 			amp_target_fill_token (AMP_PROJECT (obj), node, NULL);
 			node = project_node_save (AMP_PROJECT (obj), node, error);
 			break;
+		case ANJUTA_PROJECT_GROUP:
+			amp_group_fill_token (AMP_PROJECT (obj), node, NULL);
+			node = project_node_save (AMP_PROJECT (obj), node, error);
+			break;
 		default:
 			node = project_node_save (AMP_PROJECT (obj), node, error);
 			/*node = NULL;
@@ -3768,8 +3993,23 @@ static AnjutaProjectNode *
 iproject_new_node (IAnjutaProject *obj, AnjutaProjectNode *parent, AnjutaProjectNodeType type, GFile *file, const gchar *name, GError **err)
 {
 	AnjutaProjectNode *node;
+	GFile *directory = NULL;
 	
-	node = project_node_new (AMP_PROJECT (obj), type, file, name, err);
+	switch (type & ANJUTA_PROJECT_TYPE_MASK)
+	{
+		case ANJUTA_PROJECT_GROUP:
+			if ((file == NULL) && (name != NULL))
+			{
+				directory = g_file_get_child (AMP_GROUP_DATA (parent)->base.file, name);
+				file = directory;
+			}
+			node = project_node_new (AMP_PROJECT (obj), type, file, name, err);
+			if (directory != NULL) g_object_unref (directory);
+			break;
+		default:
+				node = project_node_new (AMP_PROJECT (obj), type, file, name, err);
+				break;
+	}
 	node->parent = parent;
 	
 	return node;
diff --git a/plugins/am-project/projectparser.c b/plugins/am-project/projectparser.c
index 33e10c6..636df15 100644
--- a/plugins/am-project/projectparser.c
+++ b/plugins/am-project/projectparser.c
@@ -574,18 +574,24 @@ main(int argc, char *argv[])
 				if ((command[4] != NULL) && (g_ascii_strcasecmp (command[4], "before") == 0))
 				{
 					sibling = get_node (project, root, command[5]);
-					amp_project_add_sibling_group (project, node, command[3], FALSE, sibling, NULL);
+					child = ianjuta_project_new_node (project, node, ANJUTA_PROJECT_GROUP, NULL, command[3], &error);
+					anjuta_project_node_insert_before (node, sibling, child);
+					ianjuta_project_save_node (project, child, NULL);
 					command += 2;
 				}
 				else if ((command[4] != NULL) && (g_ascii_strcasecmp (command[4], "after") == 0))
 				{
 					sibling = get_node (project, root, command[5]);
-					amp_project_add_sibling_group (project, node, command[3], TRUE, sibling, NULL);
+					child = ianjuta_project_new_node (project, node, ANJUTA_PROJECT_GROUP, NULL, command[3], &error);
+					anjuta_project_node_insert_after (node, sibling, child);
+					ianjuta_project_save_node (project, child, NULL);
 					command += 2;
 				}
 				else
 				{
-					ianjuta_project_add_group (project, node, command[3], NULL);
+					child = ianjuta_project_new_node (project, node, ANJUTA_PROJECT_GROUP, NULL, command[3], &error);
+					anjuta_project_node_insert_after (node, NULL, child);
+					ianjuta_project_save_node (project, child, NULL);
 				}
 			}
 			else if (g_ascii_strcasecmp (command[1], "target") == 0)



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