[tracker/tracker-0.6] Implement TrackerModuleMetadata on top of TrackerDataMetadata.



commit 5a19735295c6c15744e3653a1ceb5658d1985e16
Author: Carlos Garnacho <carlosg gnome org>
Date:   Wed Jun 24 12:45:47 2009 +0200

    Implement TrackerModuleMetadata on top of TrackerDataMetadata.
    
    Now, ideally, TrackerModuleMetadata should be used in tracker-indexer modules,
    and Tracker should use the TrackerDataMetadata API to deal with it.

 src/libtracker-data/tracker-data-metadata.c        |  201 +++++++++++++-----
 src/libtracker-data/tracker-data-metadata.h        |   26 +++-
 src/tracker-indexer/tracker-indexer.c              |   22 +-
 src/tracker-indexer/tracker-module-file.c          |    6 +-
 .../tracker-module-metadata-utils.c                |    7 +-
 src/tracker-indexer/tracker-module-metadata.c      |  227 ++++++--------------
 src/tracker-indexer/tracker-module-metadata.h      |    1 +
 src/tracker-indexer/tracker-removable-device.c     |    6 +-
 8 files changed, 265 insertions(+), 231 deletions(-)
---
diff --git a/src/libtracker-data/tracker-data-metadata.c b/src/libtracker-data/tracker-data-metadata.c
index a3babd2..1fe7e87 100644
--- a/src/libtracker-data/tracker-data-metadata.c
+++ b/src/libtracker-data/tracker-data-metadata.c
@@ -27,69 +27,153 @@
 
 #include "tracker-data-metadata.h"
 
-struct TrackerDataMetadata {
+#define GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TRACKER_TYPE_DATA_METADATA, TrackerDataMetadataPrivate))
+
+typedef struct TrackerDataMetadataPrivate TrackerDataMetadataPrivate;
+
+struct TrackerDataMetadataPrivate {
 	GHashTable *table;
 };
 
-/**
- * tracker_data_metadata_new:
- *
- * Creates a new #TrackerDataMetadata with no data in it.
- *
- * Returns: The newly created #TrackerDataMetadata
- **/
-TrackerDataMetadata *
-tracker_data_metadata_new (void)
+
+static void tracker_data_metadata_finalize (GObject *object);
+
+
+G_DEFINE_TYPE (TrackerDataMetadata, tracker_data_metadata, G_TYPE_OBJECT)
+
+
+static void
+tracker_data_metadata_class_init (TrackerDataMetadataClass *klass)
 {
-	TrackerDataMetadata *metadata;
-
-	metadata = g_slice_new (TrackerDataMetadata);
-	metadata->table = g_hash_table_new_full (g_direct_hash,
-						 g_direct_equal,
-						 (GDestroyNotify) g_object_unref,
-						 NULL);
-	return metadata;
+	GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+	object_class->finalize = tracker_data_metadata_finalize;
+
+	g_type_class_add_private (object_class,
+				  sizeof (TrackerDataMetadataPrivate));
 }
 
-static gboolean
-remove_metadata_foreach (gpointer key,
-			 gpointer value,
-			 gpointer user_data)
+static void
+tracker_data_metadata_init (TrackerDataMetadata *metadata)
 {
-	TrackerField *field;
+	TrackerDataMetadataPrivate *priv;
 
-	field = (TrackerField *) key;
+	priv = GET_PRIVATE (metadata);
+
+	priv->table = g_hash_table_new_full (g_direct_hash,
+					     g_direct_equal,
+					     (GDestroyNotify) g_object_unref,
+					     NULL);
+}
 
+static void
+free_metadata (gpointer      data,
+	       TrackerField *field)
+{
 	if (tracker_field_get_multiple_values (field)) {
 		GList *list;
 
-		list = (GList *) value;
+		list = (GList *) data;
 		g_list_foreach (list, (GFunc) g_free, NULL);
 		g_list_free (list);
 	} else {
-		g_free (value);
+		g_free (data);
 	}
 
+}
+
+static gboolean
+remove_metadata_foreach (gpointer key,
+			 gpointer value,
+			 gpointer user_data)
+{
+	TrackerField *field;
+
+	field = (TrackerField *) key;
+	free_metadata (value, field);
+
 	return TRUE;
 }
 
+static void
+tracker_data_metadata_finalize (GObject *object)
+{
+	TrackerDataMetadataPrivate *priv;
+
+	priv = GET_PRIVATE (object);
+
+	g_hash_table_foreach_remove (priv->table,
+				     remove_metadata_foreach,
+				     NULL);
+
+	g_hash_table_destroy (priv->table);
+
+	G_OBJECT_CLASS (tracker_data_metadata_parent_class)->finalize (object);
+}
+
 /**
- * tracker_data_metadata_free:
- * @metadata: A #TrackerDataMetadata
+ * tracker_data_metadata_new:
  *
- * Frees the #TrackerDataMetadata and any contained data.
+ * Creates a new #TrackerDataMetadata with no data in it.
+ *
+ * Returns: The newly created #TrackerDataMetadata
  **/
+TrackerDataMetadata *
+tracker_data_metadata_new (void)
+{
+	return g_object_new (TRACKER_TYPE_DATA_METADATA, NULL);
+}
+
 void
-tracker_data_metadata_free (TrackerDataMetadata *metadata)
+tracker_data_metadata_clear_field (TrackerDataMetadata *metadata,
+				   const gchar         *field_name)
 {
-	g_return_if_fail (metadata != NULL);
+	TrackerDataMetadataPrivate *priv;
+	TrackerField *field;
+	gpointer data;
 
-	g_hash_table_foreach_remove (metadata->table,
-				     remove_metadata_foreach,
-				     NULL);
+	field = tracker_ontology_get_field_by_name (field_name);
+
+	if (!field) {
+		g_warning ("Field name '%s' isn't described in the ontology", field_name);
+		return;
+	}
 
-	g_hash_table_destroy (metadata->table);
-	g_slice_free (TrackerDataMetadata, metadata);
+	priv = GET_PRIVATE (metadata);
+	data = g_hash_table_lookup (priv->table, field);
+
+	if (data) {
+		free_metadata (data, field);
+		g_hash_table_remove (priv->table, field);
+	}
+}
+
+gboolean
+tracker_data_metadata_insert_take_ownership (TrackerDataMetadata *metadata,
+					     const gchar         *field_name,
+					     gchar               *value)
+{
+	TrackerDataMetadataPrivate *priv;
+	TrackerField *field;
+	gchar *old_value;
+
+	g_return_val_if_fail (TRACKER_IS_DATA_METADATA (metadata), FALSE);
+	g_return_val_if_fail (field_name != NULL, FALSE);
+	g_return_val_if_fail (value != NULL, FALSE);
+
+	priv = GET_PRIVATE (metadata);
+	field = tracker_ontology_get_field_by_name (field_name);
+
+	g_return_val_if_fail (TRACKER_IS_FIELD (field), FALSE);
+	g_return_val_if_fail (tracker_field_get_multiple_values (field) == FALSE, FALSE);
+
+	old_value = g_hash_table_lookup (priv->table, field);
+	g_free (old_value);
+
+	g_hash_table_replace (priv->table,
+			      g_object_ref (field),
+			      value);
+	return TRUE;
 }
 
 /**
@@ -105,22 +189,24 @@ tracker_data_metadata_insert (TrackerDataMetadata *metadata,
 			      const gchar	  *field_name,
 			      const gchar         *value)
 {
+	TrackerDataMetadataPrivate *priv;
 	TrackerField *field;
 	gchar *old_value;
 
-	g_return_if_fail (metadata != NULL);
+	g_return_if_fail (TRACKER_IS_DATA_METADATA (metadata));
 	g_return_if_fail (field_name != NULL);
 	g_return_if_fail (value != NULL);
 
+	priv = GET_PRIVATE (metadata);
 	field = tracker_ontology_get_field_by_name (field_name);
 
 	g_return_if_fail (TRACKER_IS_FIELD (field));
 	g_return_if_fail (tracker_field_get_multiple_values (field) == FALSE);
 
-	old_value = g_hash_table_lookup (metadata->table, field);
+	old_value = g_hash_table_lookup (priv->table, field);
 	g_free (old_value);
 
-	g_hash_table_replace (metadata->table,
+	g_hash_table_replace (priv->table,
 			      g_object_ref (field),
 			      g_strdup (value));
 }
@@ -144,16 +230,18 @@ tracker_data_metadata_insert_values (TrackerDataMetadata *metadata,
 				     const gchar         *field_name,
 				     const GList	 *list)
 {
+	TrackerDataMetadataPrivate *priv;
 	TrackerField *field;
 	GList        *old_values, *copy;
 
-	g_return_if_fail (metadata != NULL);
+	g_return_if_fail (TRACKER_IS_DATA_METADATA (metadata));
 	g_return_if_fail (field_name != NULL);
 
 	if (!list) {
 		return;
 	}
 
+	priv = GET_PRIVATE (metadata);
 	field = tracker_ontology_get_field_by_name (field_name);
 
 	if (!field) {
@@ -166,14 +254,14 @@ tracker_data_metadata_insert_values (TrackerDataMetadata *metadata,
 
 	copy = tracker_glist_copy_with_string_data ((GList *)list);
 
-	old_values = g_hash_table_lookup (metadata->table, field);
+	old_values = g_hash_table_lookup (priv->table, field);
 
 	if (old_values) {
 		g_list_foreach (old_values, (GFunc) g_free, NULL);
 		g_list_free (old_values);
 	}
 
-	g_hash_table_replace (metadata->table,
+	g_hash_table_replace (priv->table,
 			      g_object_ref (field),
 			      copy);
 }
@@ -193,17 +281,19 @@ G_CONST_RETURN gchar *
 tracker_data_metadata_lookup (TrackerDataMetadata *metadata,
 			      const gchar	  *field_name)
 {
+	TrackerDataMetadataPrivate *priv;
 	TrackerField *field;
 
-	g_return_val_if_fail (metadata != NULL, NULL);
+	g_return_val_if_fail (TRACKER_IS_DATA_METADATA (metadata), NULL);
 	g_return_val_if_fail (field_name != NULL, NULL);
 
+	priv = GET_PRIVATE (metadata);
 	field = tracker_ontology_get_field_by_name (field_name);
 
 	g_return_val_if_fail (TRACKER_IS_FIELD (field), NULL);
 	g_return_val_if_fail (tracker_field_get_multiple_values (field) == FALSE, NULL);
 
-	return g_hash_table_lookup (metadata->table, field);
+	return g_hash_table_lookup (priv->table, field);
 }
 
 /**
@@ -222,17 +312,19 @@ G_CONST_RETURN GList *
 tracker_data_metadata_lookup_values (TrackerDataMetadata *metadata,
 				     const gchar         *field_name)
 {
+	TrackerDataMetadataPrivate *priv;
 	TrackerField *field;
 
-	g_return_val_if_fail (metadata != NULL, NULL);
+	g_return_val_if_fail (TRACKER_IS_DATA_METADATA (metadata), NULL);
 	g_return_val_if_fail (field_name != NULL, NULL);
 
+	priv = GET_PRIVATE (metadata);
 	field = tracker_ontology_get_field_by_name (field_name);
 
 	g_return_val_if_fail (TRACKER_IS_FIELD (field), NULL);
 	g_return_val_if_fail (tracker_field_get_multiple_values (field) == TRUE, NULL);
 
-	return g_hash_table_lookup (metadata->table, field);
+	return g_hash_table_lookup (priv->table, field);
 }
 
 /**
@@ -248,10 +340,14 @@ tracker_data_metadata_foreach (TrackerDataMetadata	  *metadata,
 			       TrackerDataMetadataForeach  func,
 			       gpointer			   user_data)
 {
-	g_return_if_fail (metadata != NULL);
+	TrackerDataMetadataPrivate *priv;
+
+	g_return_if_fail (TRACKER_IS_DATA_METADATA (metadata));
 	g_return_if_fail (func != NULL);
 
-	g_hash_table_foreach (metadata->table,
+	priv = GET_PRIVATE (metadata);
+
+	g_hash_table_foreach (priv->table,
 			      (GHFunc) func,
 			      user_data);
 }
@@ -270,11 +366,14 @@ tracker_data_metadata_foreach_remove (TrackerDataMetadata       *metadata,
 				      TrackerDataMetadataRemove  func,
 				      gpointer		         user_data)
 {
-	g_return_if_fail (metadata != NULL);
+	TrackerDataMetadataPrivate *priv;
+
+	g_return_if_fail (TRACKER_IS_DATA_METADATA (metadata));
 	g_return_if_fail (func != NULL);
 
-	g_hash_table_foreach_remove (metadata->table,
+	priv = GET_PRIVATE (metadata);
+
+	g_hash_table_foreach_remove (priv->table,
 				     (GHRFunc) func,
 				     user_data);
 }
-
diff --git a/src/libtracker-data/tracker-data-metadata.h b/src/libtracker-data/tracker-data-metadata.h
index 11439c9..9cd0502 100644
--- a/src/libtracker-data/tracker-data-metadata.h
+++ b/src/libtracker-data/tracker-data-metadata.h
@@ -26,7 +26,23 @@
 
 #include <libtracker-common/tracker-common.h>
 
+#define TRACKER_TYPE_DATA_METADATA	   (tracker_data_metadata_get_type())
+#define TRACKER_DATA_METADATA(o)	   (G_TYPE_CHECK_INSTANCE_CAST ((o), TRACKER_TYPE_DATA_METADATA, TrackerDataMetadata))
+#define TRACKER_DATA_METADATA_CLASS(c)     (G_TYPE_CHECK_CLASS_CAST ((c),    TRACKER_TYPE_DATA_METADATA, TrackerDataMetadataClass))
+#define TRACKER_IS_DATA_METADATA(o)	   (G_TYPE_CHECK_INSTANCE_TYPE ((o), TRACKER_TYPE_DATA_METADATA))
+#define TRACKER_IS_DATA_METADATA_CLASS(c)  (G_TYPE_CHECK_CLASS_TYPE ((c),    TRACKER_TYPE_DATA_METADATA))
+#define TRACKER_DATA_METADATA_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o),  TRACKER_TYPE_DATA_METADATA, TrackerDataMetadataClass))
+
 typedef struct TrackerDataMetadata TrackerDataMetadata;
+typedef struct TrackerDataMetadataClass TrackerDataMetadataClass;
+
+struct TrackerDataMetadata {
+	GObject parent_instance;
+};
+
+struct TrackerDataMetadataClass {
+	GObjectClass parent_class;
+};
 
 typedef void (* TrackerDataMetadataForeach) (TrackerField *field,
 					     gpointer      value,
@@ -35,8 +51,16 @@ typedef gboolean (* TrackerDataMetadataRemove) (TrackerField *field,
 						gpointer      value,
 						gpointer      user_data);
 
+GType                 tracker_data_metadata_get_type       (void) G_GNUC_CONST;
+
 TrackerDataMetadata * tracker_data_metadata_new            (void);
-void                  tracker_data_metadata_free           (TrackerDataMetadata        *metadata);
+
+void                  tracker_data_metadata_clear_field    (TrackerDataMetadata        *metadata,
+							    const gchar                *field_name);
+gboolean              tracker_data_metadata_insert_take_ownership
+                                                           (TrackerDataMetadata        *metadata,
+							    const gchar                *field_name,
+							    gchar                      *value);
 void                  tracker_data_metadata_insert         (TrackerDataMetadata        *metadata,
 							    const gchar                *field_name,
 							    const gchar                *value);
diff --git a/src/tracker-indexer/tracker-indexer.c b/src/tracker-indexer/tracker-indexer.c
index b18face..3da24e6 100644
--- a/src/tracker-indexer/tracker-indexer.c
+++ b/src/tracker-indexer/tracker-indexer.c
@@ -1288,7 +1288,8 @@ index_metadata (TrackerIndexer	      *indexer,
 	data.id = id;
 	data.add = TRUE;
 
-	tracker_module_metadata_foreach (metadata, index_metadata_foreach, &data);
+	tracker_data_metadata_foreach (TRACKER_DATA_METADATA (metadata),
+				       index_metadata_foreach, &data);
 
 	schedule_flush (indexer, FALSE);
 }
@@ -1645,7 +1646,8 @@ generate_item_thumbnail (TrackerIndexer        *indexer,
 {
 	const gchar *mime_type;
 
-	mime_type = tracker_module_metadata_lookup (metadata, METADATA_FILE_MIMETYPE, FALSE);
+	mime_type = tracker_data_metadata_lookup (TRACKER_DATA_METADATA (metadata),
+						  METADATA_FILE_MIMETYPE);
 
 	if (dirname && 
 	    dirname[0] == G_DIR_SEPARATOR && 
@@ -1733,9 +1735,9 @@ item_add_or_update (TrackerIndexer        *indexer,
 
 		unindex_metadata (indexer, id, service, old_metadata_emb);
 
-		tracker_module_metadata_foreach_remove (metadata,
-							remove_existing_non_emb_metadata,
-							old_metadata_non_emb);
+		tracker_data_metadata_foreach_remove (TRACKER_DATA_METADATA (metadata),
+						      remove_existing_non_emb_metadata,
+						      old_metadata_non_emb);
 
 		context = tracker_data_update_metadata_context_new (TRACKER_CONTEXT_TYPE_UPDATE,
 								    service, id);
@@ -1761,8 +1763,8 @@ item_add_or_update (TrackerIndexer        *indexer,
 		}
 
 		g_free (old_text);
-		tracker_data_metadata_free (old_metadata_emb);
-		tracker_data_metadata_free (old_metadata_non_emb);
+		g_object_unref (old_metadata_emb);
+		g_object_unref (old_metadata_non_emb);
 	} else {
 		TrackerDataUpdateMetadataContext *context;
 		GHashTable *data;
@@ -1996,7 +1998,7 @@ item_erase (TrackerIndexer *indexer,
 		mime_type = tracker_data_metadata_lookup (data_metadata, "File:Mime");
 		tracker_thumbnailer_remove (uri, mime_type);
 
-		tracker_data_metadata_free (data_metadata);
+		g_object_unref (data_metadata);
 		g_free (uri);
 		g_free (path);
 	}
@@ -2122,7 +2124,7 @@ item_move (TrackerIndexer  *indexer,
 		g_free (source_path);
 
 		if (old_metadata) {
-			tracker_data_metadata_free (old_metadata);
+			g_object_unref (old_metadata);
 		}
 
 		return FALSE;
@@ -2161,7 +2163,7 @@ item_move (TrackerIndexer  *indexer,
 	}
 
 	if (old_metadata) {
-		tracker_data_metadata_free (old_metadata);
+		g_object_unref (old_metadata);
 	}
 
 	g_free (source_path);
diff --git a/src/tracker-indexer/tracker-module-file.c b/src/tracker-indexer/tracker-module-file.c
index 5470f4b..3704afa 100644
--- a/src/tracker-indexer/tracker-module-file.c
+++ b/src/tracker-indexer/tracker-module-file.c
@@ -287,8 +287,8 @@ tracker_module_file_get_metadata (TrackerModuleFile *file)
                 return NULL;
         }
 
-        if (!tracker_module_metadata_lookup (metadata, METADATA_FILE_PATH, NULL) &&
-            !tracker_module_metadata_lookup (metadata, METADATA_FILE_NAME, NULL)) {
+        if (!tracker_data_metadata_lookup (TRACKER_DATA_METADATA (metadata), METADATA_FILE_PATH) &&
+            !tracker_data_metadata_lookup (TRACKER_DATA_METADATA (metadata), METADATA_FILE_NAME)) {
                 gchar *uri, *dirname, *basename;
 
                 uri = tracker_module_file_get_uri (file);
@@ -302,7 +302,7 @@ tracker_module_file_get_metadata (TrackerModuleFile *file)
                 g_free (uri);
         }
 
-        if (!tracker_module_metadata_lookup (metadata, METADATA_FILE_MODIFIED, NULL)) {
+        if (!tracker_data_metadata_lookup (TRACKER_DATA_METADATA (metadata), METADATA_FILE_MODIFIED)) {
                 tracker_module_metadata_add_date (metadata, METADATA_FILE_MODIFIED, time (NULL));
         }
 
diff --git a/src/tracker-indexer/tracker-module-metadata-utils.c b/src/tracker-indexer/tracker-module-metadata-utils.c
index ddc7a6f..a6178dc 100644
--- a/src/tracker-indexer/tracker-module-metadata-utils.c
+++ b/src/tracker-indexer/tracker-module-metadata-utils.c
@@ -328,8 +328,11 @@ metadata_utils_add_embedded_data (TrackerModuleMetadata *metadata,
 
                         /* Dates come in ISO 8601 format, we handle them as time_t */
                         time_str = tracker_date_to_time_string (utf_value);
-                        tracker_module_metadata_add_string (metadata, name, time_str);
-                        g_free (time_str);
+
+			if (time_str) {
+				tracker_module_metadata_add_string (metadata, name, time_str);
+				g_free (time_str);
+			}
                 } else {
                         tracker_module_metadata_add_string (metadata, name, utf_value);
                 }
diff --git a/src/tracker-indexer/tracker-module-metadata.c b/src/tracker-indexer/tracker-module-metadata.c
index bd1b6af..2e228d2 100644
--- a/src/tracker-indexer/tracker-module-metadata.c
+++ b/src/tracker-indexer/tracker-module-metadata.c
@@ -21,128 +21,28 @@
 
 #include <glib.h>
 #include <libtracker-common/tracker-type-utils.h>
+#include <libtracker-data/tracker-data-metadata.h>
 #include "tracker-module-metadata-private.h"
 
 struct TrackerModuleMetadata {
-	GObject parent_instance;
-	GHashTable *table;
+	TrackerDataMetadata parent_instance;
 };
 
 struct TrackerModuleMetadataClass {
-	GObjectClass parent_class;
+	TrackerDataMetadataClass parent_class;
 };
 
 
-static void   tracker_module_metadata_finalize   (GObject *object);
-
-
-G_DEFINE_TYPE (TrackerModuleMetadata, tracker_module_metadata, G_TYPE_OBJECT)
+G_DEFINE_TYPE (TrackerModuleMetadata, tracker_module_metadata, TRACKER_TYPE_DATA_METADATA)
 
 static void
 tracker_module_metadata_class_init (TrackerModuleMetadataClass *klass)
 {
-	GObjectClass *object_class = G_OBJECT_CLASS (klass);
-
-	object_class->finalize = tracker_module_metadata_finalize;
 }
 
 static void
 tracker_module_metadata_init (TrackerModuleMetadata *metadata)
 {
-	metadata->table = g_hash_table_new_full (g_direct_hash,
-						 g_direct_equal,
-						 (GDestroyNotify) g_object_unref,
-						 NULL);
-}
-
-static void
-free_metadata (TrackerField *field,
-	       gpointer      data)
-{
-	if (tracker_field_get_multiple_values (field)) {
-		GList *list;
-
-		list = (GList *) data;
-		g_list_foreach (list, (GFunc) g_free, NULL);
-		g_list_free (list);
-	} else {
-		g_free (data);
-	}
-}
-
-static gboolean
-remove_metadata_foreach (gpointer key,
-			 gpointer value,
-			 gpointer user_data)
-{
-	TrackerField *field;
-
-	field = (TrackerField *) key;
-	free_metadata (field, value);
-
-	return TRUE;
-}
-
-static void
-tracker_module_metadata_finalize (GObject *object)
-{
-	TrackerModuleMetadata *metadata;
-
-	metadata = TRACKER_MODULE_METADATA (object);
-
-	g_hash_table_foreach_remove (metadata->table,
-				     remove_metadata_foreach,
-				     NULL);
-
-	g_hash_table_destroy (metadata->table);
-
-	G_OBJECT_CLASS (tracker_module_metadata_parent_class)->finalize (object);
-}
-
-gconstpointer
-tracker_module_metadata_lookup (TrackerModuleMetadata *metadata,
-				const gchar           *field_name,
-				gboolean              *multiple_values)
-{
-	TrackerField *field;
-
-	field = tracker_ontology_get_field_by_name (field_name);
-
-	if (multiple_values) {
-		*multiple_values = tracker_field_get_multiple_values (field);
-	}
-
-	return g_hash_table_lookup (metadata->table, field);
-}
-
-/**
- * tracker_module_metadata_clear_field:
- * @metadata: A #TrackerModuleMetadata
- * @field_name: Field name for the metadata to clear
- *
- * Clears any content for the given field name.
- **/
-void
-tracker_module_metadata_clear_field (TrackerModuleMetadata *metadata,
-				     const gchar           *field_name)
-{
-	TrackerField *field;
-
-	gpointer data;
-
-	field = tracker_ontology_get_field_by_name (field_name);
-
-	if (!field) {
-		g_warning ("Field name '%s' isn't described in the ontology", field_name);
-		return;
-	}
-
-	data = g_hash_table_lookup (metadata->table, field);
-
-	if (data) {
-		free_metadata (field, data);
-		g_hash_table_remove (metadata->table, field);
-	}
 }
 
 /**
@@ -168,39 +68,44 @@ tracker_module_metadata_add_take_string (TrackerModuleMetadata *metadata,
 					 gchar                 *value)
 {
 	TrackerField *field;
-	gpointer data;
 
-	g_return_val_if_fail (metadata != NULL, FALSE);
+	g_return_val_if_fail (TRACKER_IS_MODULE_METADATA (metadata), FALSE);
 	g_return_val_if_fail (field_name != NULL, FALSE);
 
-	if (!value) {
-		return FALSE;
-	}
-
 	field = tracker_ontology_get_field_by_name (field_name);
 
 	if (!field) {
 		g_warning ("Field name '%s' isn't described in the ontology", field_name);
 		return FALSE;
+
 	}
 
 	if (tracker_field_get_multiple_values (field)) {
-		GList *list;
+		const GList *list;
+		GList *copy = NULL;
 
-		list = g_hash_table_lookup (metadata->table, field);
-		list = g_list_prepend (list, value);
-		data = list;
-	} else {
-		data = g_hash_table_lookup (metadata->table, field);
-		g_free (data);
-		data = value;
-	}
+		list = tracker_data_metadata_lookup_values (TRACKER_DATA_METADATA (metadata),
+							    field_name);
+
+		while (list) {
+			copy = g_list_prepend (copy, g_strdup (list->data));
+			list = list->next;
+		}
+
+		copy = g_list_prepend (copy, value);
+		copy = g_list_reverse (copy);
 
-	g_hash_table_replace (metadata->table,
-			      g_object_ref (field),
-			      data);
+		tracker_data_metadata_insert_values (TRACKER_DATA_METADATA (metadata),
+						     field_name, copy);
 
-	return TRUE;
+		g_list_foreach (copy, (GFunc) g_free, NULL);
+		g_list_free (copy);
+
+		return TRUE;
+	} else {
+		return tracker_data_metadata_insert_take_ownership (TRACKER_DATA_METADATA (metadata),
+								    field_name, value);
+	}
 }
 
 /**
@@ -217,12 +122,40 @@ tracker_module_metadata_add_string (TrackerModuleMetadata *metadata,
 				    const gchar           *field_name,
 				    const gchar           *value)
 {
-	gchar *str;
+	TrackerField *field;
 
-	str = g_strdup (value);
+	g_return_if_fail (TRACKER_IS_MODULE_METADATA (metadata));
+	g_return_if_fail (field_name != NULL);
 
-	if (!tracker_module_metadata_add_take_string (metadata, field_name, str)) {
-		g_free (str);
+	field = tracker_ontology_get_field_by_name (field_name);
+
+	if (!field) {
+		g_warning ("Field name '%s' isn't described in the ontology", field_name);
+	}
+
+	if (tracker_field_get_multiple_values (field)) {
+		const GList *list;
+		GList *copy = NULL;
+
+		list = tracker_data_metadata_lookup_values (TRACKER_DATA_METADATA (metadata),
+							    field_name);
+
+		while (list) {
+			copy = g_list_prepend (copy, g_strdup (list->data));
+			list = list->next;
+		}
+
+		copy = g_list_prepend (copy, g_strdup (value));
+		copy = g_list_reverse (copy);
+
+		tracker_data_metadata_insert_values (TRACKER_DATA_METADATA (metadata),
+						     field_name, copy);
+
+		g_list_foreach (copy, (GFunc) g_free, NULL);
+		g_list_free (copy);
+	} else {
+		tracker_data_metadata_insert (TRACKER_DATA_METADATA (metadata),
+					      field_name, value);
 	}
 }
 
@@ -345,43 +278,13 @@ tracker_module_metadata_add_date (TrackerModuleMetadata *metadata,
 	}
 }
 
-/**
- * tracker_module_metadata_foreach:
- * @metadata: A #TrackerModuleMetadata.
- * @func: The function to call with each metadata.
- * @user_data: user data to pass to the function.
- *
- * Calls a function for each element in @metadata.
- **/
-void
-tracker_module_metadata_foreach (TrackerModuleMetadata        *metadata,
-				 TrackerModuleMetadataForeach  func,
-				 gpointer		       user_data)
-{
-	g_hash_table_foreach (metadata->table,
-			      (GHFunc) func,
-			      user_data);
-}
-
-void
-tracker_module_metadata_foreach_remove (TrackerModuleMetadata       *metadata,
-					TrackerModuleMetadataRemove  func,
-					gpointer                     user_data)
-{
-	g_hash_table_foreach_remove (metadata->table,
-				     (GHRFunc) func,
-				     user_data);
-}
-
 static void
-get_hash_table_foreach (gpointer key,
-			gpointer value,
-			gpointer user_data)
+get_hash_table_foreach (TrackerField *field,
+			gpointer      value,
+			gpointer      user_data)
 {
-	TrackerField *field;
 	GHashTable *table;
 
-	field = TRACKER_FIELD (key);
 	table = user_data;
 
 	g_hash_table_insert (table,
@@ -395,8 +298,10 @@ tracker_module_metadata_get_hash_table (TrackerModuleMetadata *metadata)
 	GHashTable *table;
 
 	table = g_hash_table_new (g_str_hash, g_str_equal);
-	g_hash_table_foreach (metadata->table, (GHFunc) get_hash_table_foreach, table);
 
+	tracker_data_metadata_foreach (TRACKER_DATA_METADATA (metadata),
+				       get_hash_table_foreach,
+				       table);
 	return table;
 }
 
diff --git a/src/tracker-indexer/tracker-module-metadata.h b/src/tracker-indexer/tracker-module-metadata.h
index 831ddef..9a93536 100644
--- a/src/tracker-indexer/tracker-module-metadata.h
+++ b/src/tracker-indexer/tracker-module-metadata.h
@@ -24,6 +24,7 @@
 
 #include <glib.h>
 #include <glib-object.h>
+#include "libtracker-data/tracker-data-metadata.h"
 
 G_BEGIN_DECLS
 
diff --git a/src/tracker-indexer/tracker-removable-device.c b/src/tracker-indexer/tracker-removable-device.c
index 8b5fe17..1133f27 100644
--- a/src/tracker-indexer/tracker-removable-device.c
+++ b/src/tracker-indexer/tracker-removable-device.c
@@ -486,9 +486,9 @@ tracker_removable_device_add_metadata (TrackerIndexer        *indexer,
 
 	set_metadata ("rdf:type", rdf_type, info);
 
-	tracker_module_metadata_foreach (metadata, 
-					 foreach_in_metadata_set_metadata,
-					 info);
+	tracker_data_metadata_foreach (TRACKER_DATA_METADATA (metadata),
+				       foreach_in_metadata_set_metadata,
+				       info);
 
 	g_free (info->about_uri);
 



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