[gedit] metadata-manager independent of gedit-dirs



commit e863e99fde28876e10b4436d84560f248ee0cc0a
Author: Sébastien Wilmet <sebastien wilmet gmail com>
Date:   Sun Jan 23 17:29:45 2011 +0100

    metadata-manager independent of gedit-dirs
    
    gedit_metadata_manager_init() is now public and is called in gedit.c.
    
    gtk-doc comments added.

 gedit/gedit-metadata-manager.c |   98 ++++++++++++++++++++-------------------
 gedit/gedit-metadata-manager.h |    2 +-
 gedit/gedit.c                  |   17 +++++++
 3 files changed, 68 insertions(+), 49 deletions(-)
---
diff --git a/gedit/gedit-metadata-manager.c b/gedit/gedit-metadata-manager.c
index c0c02f3..5d79115 100644
--- a/gedit/gedit-metadata-manager.c
+++ b/gedit/gedit-metadata-manager.c
@@ -27,23 +27,16 @@
  * See the ChangeLog files for a list of changes. 
  */
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
 #include <time.h>
 #include <stdlib.h>
 #include <libxml/xmlreader.h>
 #include "gedit-metadata-manager.h"
 #include "gedit-debug.h"
-#include "gedit-dirs.h"
 
 /*
 #define GEDIT_METADATA_VERBOSE_DEBUG	1
 */
 
-#define METADATA_FILE 	"gedit-metadata.xml"
-
 #define MAX_ITEMS	50
 
 typedef struct _GeditMetadataManager GeditMetadataManager;
@@ -65,6 +58,8 @@ struct _GeditMetadataManager
 	guint 		 timeout_id;
 
 	GHashTable	*items;
+
+	gchar		*metadata_filename;
 };
 
 static gboolean gedit_metadata_manager_save (gpointer data);
@@ -105,13 +100,20 @@ gedit_metadata_manager_arm_timeout (void)
 	}
 }
 
-static gboolean
-gedit_metadata_manager_init (void)
+/**
+ * gedit_metadata_manager_init:
+ * @metadata_filename: the filename where the metadata is.
+ *
+ * This function initializes the metadata manager.
+ * See also gedit_metadata_manager_shutdown().
+ */
+void
+gedit_metadata_manager_init (const gchar *metadata_filename)
 {
 	gedit_debug (DEBUG_METADATA);
 
 	if (gedit_metadata_manager != NULL)
-		return TRUE;
+		return;
 
 	gedit_metadata_manager = g_new0 (GeditMetadataManager, 1);
 
@@ -123,10 +125,17 @@ gedit_metadata_manager_init (void)
 				       g_free,
 				       item_free);
 
-	return TRUE;
+	gedit_metadata_manager->metadata_filename = g_strdup (metadata_filename);
+
+	return;
 }
 
-/* This function must be called before exiting gedit */
+/**
+ * gedit_metadata_manager_shutdown:
+ *
+ * This function frees the internal data of the metadata manager.
+ * See also gedit_metadata_manager_init().
+ */
 void
 gedit_metadata_manager_shutdown (void)
 {
@@ -145,6 +154,8 @@ gedit_metadata_manager_shutdown (void)
 	if (gedit_metadata_manager->items != NULL)
 		g_hash_table_destroy (gedit_metadata_manager->items);
 
+	g_free (gedit_metadata_manager->metadata_filename);
+
 	g_free (gedit_metadata_manager);
 	gedit_metadata_manager = NULL;
 }
@@ -220,29 +231,11 @@ parseItem (xmlDocPtr doc, xmlNodePtr cur)
 	xmlFree (atime);
 }
 
-static gchar *
-get_metadata_filename (void)
-{
-	gchar *cache_dir;
-	gchar *metadata;
-
-	cache_dir = gedit_dirs_get_user_cache_dir ();
-
-	metadata = g_build_filename (cache_dir,
-				     METADATA_FILE,
-				     NULL);
-
-	g_free (cache_dir);
-
-	return metadata;
-}
-
 static gboolean
 load_values (void)
 {
 	xmlDocPtr doc;
 	xmlNodePtr cur;
-	gchar *file_name;
 
 	gedit_debug (DEBUG_METADATA);
 
@@ -254,16 +247,13 @@ load_values (void)
 	xmlKeepBlanksDefault (0);
 
 	/* FIXME: file locking - Paolo */
-	file_name = get_metadata_filename ();
-	if ((file_name == NULL) ||
-	    (!g_file_test (file_name, G_FILE_TEST_EXISTS)))
+	if ((gedit_metadata_manager->metadata_filename == NULL) ||
+	    (!g_file_test (gedit_metadata_manager->metadata_filename, G_FILE_TEST_EXISTS)))
 	{
-		g_free (file_name);
 		return FALSE;
 	}
 
-	doc = xmlParseFile (file_name);
-	g_free (file_name);
+	doc = xmlParseFile (gedit_metadata_manager->metadata_filename);
 
 	if (doc == NULL)
 	{
@@ -273,7 +263,8 @@ load_values (void)
 	cur = xmlDocGetRootElement (doc);
 	if (cur == NULL) 
 	{
-		g_message ("The metadata file '%s' is empty", METADATA_FILE);
+		g_message ("The metadata file '%s' is empty",
+		           g_path_get_basename (gedit_metadata_manager->metadata_filename));
 		xmlFreeDoc (doc);
 	
 		return FALSE;
@@ -281,7 +272,8 @@ load_values (void)
 
 	if (xmlStrcmp (cur->name, (const xmlChar *) "metadata")) 
 	{
-		g_message ("File '%s' is of the wrong type", METADATA_FILE);
+		g_message ("File '%s' is of the wrong type",
+		           g_path_get_basename (gedit_metadata_manager->metadata_filename));
 		xmlFreeDoc (doc);
 		
 		return FALSE;
@@ -302,6 +294,13 @@ load_values (void)
 	return TRUE;
 }
 
+/**
+ * gedit_metadata_manager_get:
+ * @location: a #GFile.
+ * @key: a key.
+ *
+ * Gets the value associated with the specified @key for the file @location.
+ */
 gchar *
 gedit_metadata_manager_get (GFile       *location,
 			    const gchar *key)
@@ -317,8 +316,6 @@ gedit_metadata_manager_get (GFile       *location,
 
 	gedit_debug_message (DEBUG_METADATA, "URI: %s --- key: %s", uri, key );
 
-	gedit_metadata_manager_init ();
-
 	if (!gedit_metadata_manager->values_loaded)
 	{
 		gboolean res;
@@ -350,6 +347,14 @@ gedit_metadata_manager_get (GFile       *location,
 		return g_strdup (value);
 }
 
+/**
+ * gedit_metadata_manager_set:
+ * @location: a #GFile.
+ * @key: a key.
+ * @value: the value associated with the @key.
+ *
+ * Sets the @key to contain the given @value for the file @location.
+ */
 void
 gedit_metadata_manager_set (GFile       *location,
 			    const gchar *key,
@@ -364,8 +369,6 @@ gedit_metadata_manager_set (GFile       *location,
 	uri = g_file_get_uri (location);
 
 	gedit_debug_message (DEBUG_METADATA, "URI: %s --- key: %s --- value: %s", uri, key, value);
-	
-	gedit_metadata_manager_init ();
 
 	if (!gedit_metadata_manager->values_loaded)
 	{
@@ -532,7 +535,6 @@ gedit_metadata_manager_save (gpointer data)
 {	
 	xmlDocPtr  doc;
 	xmlNodePtr root;
-	gchar *file_name;
 
 	gedit_debug (DEBUG_METADATA);
 
@@ -555,22 +557,22 @@ gedit_metadata_manager_save (gpointer data)
 			      root);        
 
 	/* FIXME: lock file - Paolo */
-	file_name = get_metadata_filename ();
-	if (file_name != NULL)
+	if (gedit_metadata_manager->metadata_filename != NULL)
 	{
 		gchar *cache_dir;
 		int res;
 
 		/* make sure the cache dir exists */
-		cache_dir = gedit_dirs_get_user_cache_dir ();
+		cache_dir = g_path_get_dirname (gedit_metadata_manager->metadata_filename);
 		res = g_mkdir_with_parents (cache_dir, 0755);
 		if (res != -1)
 		{
-			xmlSaveFormatFile (file_name, doc, 1);
+			xmlSaveFormatFile (gedit_metadata_manager->metadata_filename,
+			                   doc,
+			                   1);
 		}
 
 		g_free (cache_dir);
-		g_free (file_name);
 	}
 
 	xmlFreeDoc (doc); 
diff --git a/gedit/gedit-metadata-manager.h b/gedit/gedit-metadata-manager.h
index f5c97cd..1208fcf 100644
--- a/gedit/gedit-metadata-manager.h
+++ b/gedit/gedit-metadata-manager.h
@@ -35,8 +35,8 @@
 
 G_BEGIN_DECLS
 
+void		 gedit_metadata_manager_init		(const gchar *metadata_filename);
 
-/* This function must be called before exiting gedit */
 void		 gedit_metadata_manager_shutdown 	(void);
 
 
diff --git a/gedit/gedit.c b/gedit/gedit.c
index af819a0..e4caec4 100644
--- a/gedit/gedit.c
+++ b/gedit/gedit.c
@@ -54,6 +54,7 @@
 
 #ifndef ENABLE_GVFS_METADATA
 #include "gedit-metadata-manager.h"
+#define METADATA_FILE "gedit-metadata.xml"
 #endif
 
 #ifdef G_OS_UNIX
@@ -218,6 +219,11 @@ main (int argc, char *argv[])
 	GeditDBusResult dbusret;
 	gboolean service = FALSE;
 
+#ifndef ENABLE_GVFS_METADATA
+	const gchar *cache_dir;
+	gchar *metadata_filename;
+#endif
+
 	/* Init type system as soon as possible */
 	g_type_init ();
 
@@ -239,6 +245,17 @@ main (int argc, char *argv[])
 	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 	textdomain (GETTEXT_PACKAGE);
 
+#ifndef ENABLE_GVFS_METADATA
+	/* Setup metadata-manager */
+	cache_dir = gedit_dirs_get_user_cache_dir ();
+
+	metadata_filename = g_build_filename (cache_dir, METADATA_FILE, NULL);
+
+	gedit_metadata_manager_init (metadata_filename);
+
+	g_free (metadata_filename);
+#endif
+
 	/* Parse command line arguments */
 	command_line = gedit_command_line_get_default ();
 



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