[gthumb/ext] use an hash table to avoid duplicates in the catalog



commit 0ff9312e815e823a0a3f3109c0353cd24f474f1c
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Mon Dec 21 17:30:24 2009 +0100

    use an hash table to avoid duplicates in the catalog

 extensions/catalogs/gth-catalog.c           |   60 +++++++++++++++++----------
 extensions/catalogs/gth-catalog.h           |    6 +--
 extensions/catalogs/gth-organize-task.c     |    4 +-
 extensions/photo_importer/gth-import-task.c |    5 +-
 4 files changed, 45 insertions(+), 30 deletions(-)
---
diff --git a/extensions/catalogs/gth-catalog.c b/extensions/catalogs/gth-catalog.c
index d7bfd90..8340c9e 100644
--- a/extensions/catalogs/gth-catalog.c
+++ b/extensions/catalogs/gth-catalog.c
@@ -35,6 +35,7 @@ struct _GthCatalogPrivate {
 	GthCatalogType  type;
 	GFile          *file;
 	GList          *file_list;
+	GHashTable     *file_hash;  /* used to avoid duplicates */
 	char           *name;
 	GthDateTime    *date_time;
 	gboolean        active;
@@ -57,6 +58,7 @@ gth_catalog_finalize (GObject *object)
 			g_object_unref (catalog->priv->file);
 		g_free (catalog->priv->name);
 		_g_object_list_unref (catalog->priv->file_list);
+		g_hash_table_destroy (catalog->priv->file_hash);
 		gth_datetime_free (catalog->priv->date_time);
 		g_free (catalog->priv->order);
 		g_free (catalog->priv);
@@ -81,10 +83,10 @@ static void
 base_read_from_doc (GthCatalog  *catalog,
 		    DomElement *root)
 {
+	GList      *file_list;
 	DomElement *child;
 
-	gth_catalog_set_file_list (catalog, NULL);
-
+	file_list = NULL;
 	for (child = root->first_child; child; child = child->next_sibling) {
 		if (g_strcmp0 (child->tag_name, "files") == 0) {
 			DomElement *file;
@@ -94,8 +96,9 @@ base_read_from_doc (GthCatalog  *catalog,
 
 				uri = dom_element_get_attribute (file, "uri");
 				if (uri != NULL)
-					catalog->priv->file_list = g_list_prepend (catalog->priv->file_list, g_file_new_for_uri (uri));
+					file_list = g_list_prepend (file_list, g_file_new_for_uri (uri));
 			}
+			file_list = g_list_reverse (file_list);
 		}
 		if (g_strcmp0 (child->tag_name, "order") == 0)
 			gth_catalog_set_order (catalog,
@@ -106,7 +109,9 @@ base_read_from_doc (GthCatalog  *catalog,
 		if (g_strcmp0 (child->tag_name, "name") == 0)
 			gth_catalog_set_name (catalog, dom_element_get_inner_text (child));
 	}
-	catalog->priv->file_list = g_list_reverse (catalog->priv->file_list);
+	gth_catalog_set_file_list (catalog, file_list);
+
+	_g_object_list_unref (file_list);
 }
 
 
@@ -238,6 +243,7 @@ gth_catalog_init (GthCatalog *catalog)
 {
 	catalog->priv = g_new0 (GthCatalogPrivate, 1);
 	catalog->priv->date_time = gth_datetime_new ();
+	catalog->priv->file_hash = g_hash_table_new_full (g_file_hash, (GEqualFunc) g_file_equal, NULL, NULL);
 }
 
 
@@ -421,9 +427,24 @@ gth_catalog_set_file_list (GthCatalog *catalog,
 {
 	_g_object_list_unref (catalog->priv->file_list);
 	catalog->priv->file_list = NULL;
+	g_hash_table_remove_all (catalog->priv->file_hash);
+
+	if (file_list != NULL) {
+		GList *list;
+		GList *scan;
+
+		list = NULL;
+		for (scan = file_list; scan; scan = scan->next) {
+			GFile *file = scan->data;
 
-	if (file_list != NULL)
-		catalog->priv->file_list = _g_file_list_dup (file_list);
+			if (g_hash_table_lookup (catalog->priv->file_hash, file) != NULL)
+				continue;
+			file = g_file_dup (file);
+			list = g_list_prepend (list, file);
+			g_hash_table_insert (catalog->priv->file_hash, file, GINT_TO_POINTER (1));
+		}
+		catalog->priv->file_list = g_list_reverse (list);
+	}
 }
 
 
@@ -436,29 +457,20 @@ gth_catalog_get_file_list (GthCatalog *catalog)
 
 gboolean
 gth_catalog_insert_file (GthCatalog *catalog,
-			 int         pos,
-			 GFile      *file)
+			 GFile      *file,
+			 int         pos)
 {
-	GList *link;
-
-	link = g_list_find_custom (catalog->priv->file_list, file, (GCompareFunc) _g_file_cmp_uris);
-	if (link != NULL)
+	if (g_hash_table_lookup (catalog->priv->file_hash, file) != NULL)
 		return FALSE;
 
-	catalog->priv->file_list = g_list_insert (catalog->priv->file_list, g_file_dup (file), pos);
+	file = g_file_dup (file);
+	catalog->priv->file_list = g_list_insert (catalog->priv->file_list, file, pos);
+	g_hash_table_insert (catalog->priv->file_hash, file, GINT_TO_POINTER (1));
 
 	return TRUE;
 }
 
 
-void
-gth_catalog_append_file (GthCatalog *catalog,
-		         GFile      *file)
-{
-	catalog->priv->file_list = g_list_append (catalog->priv->file_list, g_file_dup (file));
-}
-
-
 int
 gth_catalog_remove_file (GthCatalog *catalog,
 			 GFile      *file)
@@ -477,6 +489,8 @@ gth_catalog_remove_file (GthCatalog *catalog,
 		return -1;
 
 	catalog->priv->file_list = g_list_remove_link (catalog->priv->file_list, scan);
+	g_hash_table_remove (catalog->priv->file_hash, file);
+
 	_g_object_list_unref (scan);
 
 	return i;
@@ -503,8 +517,10 @@ gth_catalog_list_done (ListData *list_data,
 	GthCatalog *catalog = list_data->catalog;
 
 	catalog->priv->active = FALSE;
-	if (list_data->list_ready_func != NULL)
+	if (list_data->list_ready_func != NULL) {
+		list_data->files = g_list_reverse (list_data->files);
 		list_data->list_ready_func (catalog, list_data->files, error, list_data->user_data);
+	}
 
 	_g_object_list_unref (list_data->files);
 	g_free (list_data);
diff --git a/extensions/catalogs/gth-catalog.h b/extensions/catalogs/gth-catalog.h
index 1edf0d2..a6dc470 100644
--- a/extensions/catalogs/gth-catalog.h
+++ b/extensions/catalogs/gth-catalog.h
@@ -99,10 +99,8 @@ void          gth_catalog_set_file_list   (GthCatalog           *catalog,
 					   GList                *file_list);
 GList *       gth_catalog_get_file_list   (GthCatalog           *catalog);
 gboolean      gth_catalog_insert_file     (GthCatalog           *catalog,
-					   int                   pos,
-					   GFile                *file);
-void          gth_catalog_append_file     (GthCatalog           *catalog,
-					   GFile                *file);
+					   GFile                *file,
+					   int                   pos);
 int           gth_catalog_remove_file     (GthCatalog           *catalog,
 					   GFile                *file);
 void          gth_catalog_list_async      (GthCatalog           *catalog,
diff --git a/extensions/catalogs/gth-organize-task.c b/extensions/catalogs/gth-organize-task.c
index c73a7d9..fa27173 100644
--- a/extensions/catalogs/gth-organize-task.c
+++ b/extensions/catalogs/gth-organize-task.c
@@ -173,7 +173,7 @@ done_func (GError   *error,
 					singletons++;
 					catalog = g_hash_table_lookup (self->priv->catalogs, key);
 					file_list = gth_catalog_get_file_list (catalog);
-					gth_catalog_append_file (self->priv->singletons_catalog, file_list->data);
+					gth_catalog_insert_file (self->priv->singletons_catalog, file_list->data, -1);
 					if (singletons == 1)
 						g_hash_table_insert (self->priv->catalogs,
 								     g_strdup (gth_catalog_get_name (self->priv->singletons_catalog)),
@@ -308,7 +308,7 @@ for_each_file_func (GFile     *file,
 			while (gtk_tree_model_iter_next (GTK_TREE_MODEL (self->priv->results_liststore), &iter));
 		}
 
-		gth_catalog_append_file (catalog, file_data->file);
+		gth_catalog_insert_file (catalog, file_data->file, -1);
 	}
 
 	g_object_unref (file_data);
diff --git a/extensions/photo_importer/gth-import-task.c b/extensions/photo_importer/gth-import-task.c
index 663fdd8..c9974b3 100644
--- a/extensions/photo_importer/gth-import-task.c
+++ b/extensions/photo_importer/gth-import-task.c
@@ -153,7 +153,7 @@ catalog_imported_file (GthImportTask *self)
 		g_object_unref (catalog_file);
 		gth_datetime_free (date_time);
 	}
-	gth_catalog_append_file (catalog, self->priv->destination_file->file);
+	gth_catalog_insert_file (catalog, self->priv->destination_file->file, -1);
 
 	catalog = g_hash_table_lookup (self->priv->catalogs, IMPORTED_KEY);
 	if (catalog == NULL) {
@@ -161,6 +161,7 @@ catalog_imported_file (GthImportTask *self)
 		char        *name;
 		char        *display_name;
 
+		g_get_current_time (&timeval);
 		date_time = gth_datetime_new ();
 		gth_datetime_from_timeval (date_time, &timeval);
 
@@ -184,7 +185,7 @@ catalog_imported_file (GthImportTask *self)
 		g_free (name);
 		gth_datetime_free (date_time);
 	}
-	gth_catalog_append_file (catalog, self->priv->destination_file->file);
+	gth_catalog_insert_file (catalog, self->priv->destination_file->file, -1);
 
 	import_next_file (self);
 



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