[anjuta] snippet-manager: Basic import feature done.



commit 98f0306ac0803027c4a185afca68d0aecb6bcb06
Author: Dragos Dena <dragos dena gmail com>
Date:   Sat Aug 14 01:19:50 2010 +0300

    snippet-manager: Basic import feature done.

 plugins/snippets-manager/snippets-group.c         |    6 +-
 plugins/snippets-manager/snippets-import-export.c |   91 ++++++++++++++++++++-
 plugins/snippets-manager/snippets-provider.c      |    1 +
 3 files changed, 94 insertions(+), 4 deletions(-)
---
diff --git a/plugins/snippets-manager/snippets-group.c b/plugins/snippets-manager/snippets-group.c
index 5253d1d..a20421f 100644
--- a/plugins/snippets-manager/snippets-group.c
+++ b/plugins/snippets-manager/snippets-group.c
@@ -223,7 +223,11 @@ snippets_group_remove_snippet (AnjutaSnippetsGroup* snippets_group,
 		if (!g_strcmp0 (cur_snippet_trigger, trigger_key) &&
 		    snippet_has_language (cur_snippet, language))
 		{
-			if (remove_all_languages_support)
+
+			/* If we should remove all languages or if this is the last language,
+			   we remove the snippet. */
+			if (remove_all_languages_support ||
+			    g_list_length ((GList *)snippet_get_languages (cur_snippet)) == 1)
 			{
 				to_be_deleted_snippet = cur_snippet;
 				break;
diff --git a/plugins/snippets-manager/snippets-import-export.c b/plugins/snippets-manager/snippets-import-export.c
index 128a75c..36c4a46 100644
--- a/plugins/snippets-manager/snippets-import-export.c
+++ b/plugins/snippets-manager/snippets-import-export.c
@@ -19,24 +19,109 @@
 	Boston, MA  02110-1301  USA
 */
 
-
 #include "snippets-import-export.h"
+#include "snippets-xml-parser.h"
+#include "snippets-group.h"
 #include <glib.h>
 #include <glib-object.h>
 #include <gtk/gtk.h>
 #include <libanjuta/anjuta-utils.h>
 
+
+static void
+add_or_update_snippet (SnippetsDB *snippets_db,
+                       AnjutaSnippet *snippet,
+                       const gchar *group_name)
+{
+	const gchar *trigger = NULL;
+	GList *iter = NULL, *languages = NULL;
+	gchar *cur_lang = NULL;
+
+	/* Assertions */
+	g_return_if_fail (ANJUTA_IS_SNIPPETS_DB (snippets_db));
+	g_return_if_fail (ANJUTA_IS_SNIPPET (snippet));
+
+	/* Get the trigger-key and the languages for which the snippet is supported */
+	trigger = snippet_get_trigger_key (snippet);
+	languages = (GList *)snippet_get_languages (snippet);
+
+	/* Check if each (trigger, language) tuple exists in the database and update it 
+	   if yes, or add it if not. */
+	for (iter = g_list_first (languages); iter != NULL; iter = g_list_next (iter))
+	{
+		cur_lang = (gchar *)iter->data;
+
+		/* If there is already an entry for (trigger, cur_lang) we remove it so
+		   we can update it. */
+		if (snippets_db_get_snippet (snippets_db, trigger, cur_lang))
+			snippets_db_remove_snippet (snippets_db, trigger, cur_lang, FALSE);
+
+	}
+
+	snippets_db_add_snippet (snippets_db, snippet, group_name);
+}
+
+static void
+add_group_list_to_database (SnippetsDB *snippets_db,
+                            GList *snippets_groups)
+{
+	GList *iter = NULL, *iter2 = NULL, *snippets = NULL;
+	AnjutaSnippetsGroup *cur_group = NULL;
+	const gchar *cur_group_name = NULL;
+
+	/* Assertions */
+	g_return_if_fail (ANJUTA_IS_SNIPPETS_DB (snippets_db));
+	if (snippets_groups == NULL)
+		return;
+
+	for (iter = g_list_first (snippets_groups); iter != NULL; iter = g_list_next (iter))
+	{
+		if (!ANJUTA_IS_SNIPPETS_GROUP (iter->data))
+			continue;
+		cur_group = ANJUTA_SNIPPETS_GROUP (iter->data);
+		cur_group_name = snippets_group_get_name (cur_group);
+
+		/* If there isn't a snippets group with the same name we just add it */		
+		if (!snippets_db_has_snippets_group_name (snippets_db, cur_group_name))
+		{
+			snippets_db_add_snippets_group (snippets_db, cur_group, TRUE);
+			continue;
+		}
+
+		/* If there is already a group with the same name, we add the snippets inside
+		   the group */
+		snippets = snippets_group_get_snippets_list (cur_group);
+
+		for (iter2 = g_list_first (snippets); iter2 != NULL; iter2 = g_list_next (iter2))
+		{
+			if (!ANJUTA_IS_SNIPPET (iter2->data))
+				continue;
+
+			add_or_update_snippet (snippets_db, 
+			                       ANJUTA_SNIPPET (iter2->data), 
+			                       cur_group_name);
+		}
+	}
+}
+
 static void
 add_native_snippets_at_path (SnippetsDB *snippets_db,
                              const gchar *path)
 {
+	GList *snippets_groups = NULL;
 
 	/* Assertions */
 	g_return_if_fail (ANJUTA_IS_SNIPPETS_DB (snippets_db));
 
 	if (path == NULL)
 		return;
-	/* TODO - import the snippets */
+
+	/* Parse the snippets file */
+	snippets_groups = snippets_manager_parse_snippets_xml_file (path, NATIVE_FORMAT);
+
+	/* Add the snippets groups from the file to the database */
+	add_group_list_to_database (snippets_db, snippets_groups);
+
 }
 
 static void
@@ -100,4 +185,4 @@ void snippets_manager_export_snippets (SnippetsDB *snippets_db,
 	/* Assertions */
 	g_return_if_fail (ANJUTA_IS_SNIPPETS_DB (snippets_db));
 
-}
\ No newline at end of file
+}
diff --git a/plugins/snippets-manager/snippets-provider.c b/plugins/snippets-manager/snippets-provider.c
index 945e6d0..180bf8a 100644
--- a/plugins/snippets-manager/snippets-provider.c
+++ b/plugins/snippets-manager/snippets-provider.c
@@ -559,6 +559,7 @@ snippets_provider_load (SnippetsProvider *snippets_provider,
 
 	priv->request = FALSE;
 	priv->listening = FALSE;
+	
 
 }
 



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