[gthumb] metadata dialog: keep the inconsinstent tags if only_modified_fields is active



commit 68c8ab03100b6693f6891f5b9b6ccbdc53beb127
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Sun Jan 29 11:36:05 2012 +0100

    metadata dialog: keep the inconsinstent tags if only_modified_fields is active
    
    this is the same behaiour of the edit tags dialog.

 extensions/edit_metadata/Makefile.am             |    4 +-
 extensions/edit_metadata/gth-edit-general-page.c |  120 +++++++++++++++-------
 extensions/edit_metadata/gth-edit-tags-dialog.c  |   75 +-------------
 extensions/edit_metadata/utils.c                 |   99 ++++++++++++++++++
 extensions/edit_metadata/utils.h                 |   31 ++++++
 gthumb/gth-string-list.c                         |   14 +++
 gthumb/gth-string-list.h                         |    4 +
 7 files changed, 238 insertions(+), 109 deletions(-)
---
diff --git a/extensions/edit_metadata/Makefile.am b/extensions/edit_metadata/Makefile.am
index cca3a9c..e45a53e 100644
--- a/extensions/edit_metadata/Makefile.am
+++ b/extensions/edit_metadata/Makefile.am
@@ -22,7 +22,9 @@ libedit_metadata_la_SOURCES = 		\
 	gth-edit-tags-dialog.h		\
 	gth-tag-task.c			\
 	gth-tag-task.h			\
-	main.c
+	main.c				\
+	utils.c				\
+	utils.h
 
 libedit_metadata_la_CFLAGS = $(GTHUMB_CFLAGS) -I$(top_srcdir) -I$(top_builddir)/gthumb 
 libedit_metadata_la_LDFLAGS = $(EXTENSION_LIBTOOL_FLAGS)
diff --git a/extensions/edit_metadata/gth-edit-general-page.c b/extensions/edit_metadata/gth-edit-general-page.c
index 3c1e429..cd64a2f 100644
--- a/extensions/edit_metadata/gth-edit-general-page.c
+++ b/extensions/edit_metadata/gth-edit-general-page.c
@@ -24,6 +24,7 @@
 #include <gio/gio.h>
 #include "gth-edit-comment-dialog.h"
 #include "gth-edit-general-page.h"
+#include "utils.h"
 
 
 #define GET_WIDGET(name) _gtk_builder_get_widget (self->priv->builder, (name))
@@ -67,8 +68,11 @@ gth_edit_general_page_real_set_file_list (GthEditCommentPage *base,
 	GthEditGeneralPage  *self;
 	GtkTextBuffer       *buffer;
 	GthMetadata         *metadata;
-	GthStringList       *tags;
 	GthMetadataProvider *provider;
+	GHashTable          *common_tags;
+	GHashTable          *no_common_tags;
+	GList               *common_tags_list;
+	GList               *no_common_tags_list;
 	gboolean             no_provider;
 	GthFileData         *file_data;
 	const char          *mime_type;
@@ -129,17 +133,17 @@ gth_edit_general_page_real_set_file_list (GthEditCommentPage *base,
 
 	/* tags */
 
-	tags = (GthStringList *) g_file_info_get_attribute_object (self->priv->info, "general::tags");
-	if (tags != NULL) {
-		char *value;
+	utils_get_common_tags (file_list, &common_tags, &no_common_tags);
+	common_tags_list = g_hash_table_get_keys (common_tags);
+	no_common_tags_list = g_hash_table_get_keys (no_common_tags);
+	gth_tags_entry_set_tag_list (GTH_TAGS_ENTRY (self->priv->tags_entry),
+				     common_tags_list,
+				     no_common_tags_list);
 
-		value = gth_string_list_join (tags, ",");
-		gth_tags_entry_set_tags_from_text (GTH_TAGS_ENTRY (self->priv->tags_entry), value);
-
-		g_free (value);
-	}
-	else
-		gth_tags_entry_set_tags_from_text (GTH_TAGS_ENTRY (self->priv->tags_entry), NULL);
+	g_list_free (no_common_tags_list);
+	g_list_free (common_tags_list);
+	g_hash_table_unref (no_common_tags);
+	g_hash_table_unref (common_tags);
 
 	/* rating */
 
@@ -282,17 +286,13 @@ gth_edit_general_page_real_update_info (GthEditCommentPage *base,
 					gboolean            only_modified_fields)
 {
 	GthEditGeneralPage  *self;
-	GthFileData            *file_data;
-	GtkTextBuffer          *buffer;
-	GtkTextIter             start;
-	GtkTextIter             end;
-	char                   *text;
-	GthMetadata            *metadata;
-	int                     i;
-	char                  **tagv;
-	GList                  *tags;
-	GthStringList          *string_list;
-	char                   *s;
+	GthFileData         *file_data;
+	GtkTextBuffer       *buffer;
+	GtkTextIter          start;
+	GtkTextIter          end;
+	char                *text;
+	GthMetadata         *metadata;
+	char                *s;
 
 	self = GTH_EDIT_GENERAL_PAGE (base);
 
@@ -376,20 +376,73 @@ gth_edit_general_page_real_update_info (GthEditCommentPage *base,
 
 	/* tags */
 
-	tagv = gth_tags_entry_get_tags (GTH_TAGS_ENTRY (self->priv->tags_entry), TRUE);
-	tags = NULL;
-	for (i = 0; tagv[i] != NULL; i++)
-		tags = g_list_prepend (tags, tagv[i]);
-	tags = g_list_reverse (tags);
-	if (tags != NULL)
-		string_list = gth_string_list_new (tags);
-	else
-		string_list = NULL;
-	if (! only_modified_fields || ! gth_file_data_attribute_equal_string_list (file_data, "general::tags", string_list)) {
+	if (only_modified_fields) {
+		GList       *checked_tags;
+		GList       *inconsistent_tags;
+		GList       *new_tags;
+		GHashTable  *old_tags;
+		GList       *scan_tags;
+
+		gth_tags_entry_get_tag_list (GTH_TAGS_ENTRY (self->priv->tags_entry),
+					     TRUE,
+					     &checked_tags,
+					     &inconsistent_tags);
+
+		new_tags = _g_string_list_dup (checked_tags);
+
+		/* keep the inconsistent tags */
+
+		old_tags = _g_hash_table_from_string_list ((GthStringList *) g_file_info_get_attribute_object (info, "general::tags"));
+		for (scan_tags = inconsistent_tags; scan_tags; scan_tags = scan_tags->next) {
+			char *inconsistent_tag = scan_tags->data;
+
+			if (g_hash_table_lookup (old_tags, inconsistent_tag) != NULL)
+				new_tags = g_list_prepend (new_tags, g_strdup (inconsistent_tag));
+		}
+		g_hash_table_unref (old_tags);
+
+		/* update the general::tags attribute */
+
+		if (new_tags != NULL) {
+			GthStringList *file_tags;
+
+			new_tags = g_list_sort (new_tags, (GCompareFunc) g_strcmp0);
+			file_tags = gth_string_list_new (new_tags);
+			g_file_info_set_attribute_object (info, "general::tags", G_OBJECT (file_tags));
+
+			_g_object_unref (file_tags);
+			_g_string_list_free (new_tags);
+		}
+		else
+			g_file_info_remove_attribute (info, "general::tags");
+
+		g_list_free (inconsistent_tags);
+		_g_string_list_free (checked_tags);
+	}
+	else {
+		char          **tagv;
+		GList          *tags;
+		int             i;
+		GthStringList  *string_list;
+
+		tagv = gth_tags_entry_get_tags (GTH_TAGS_ENTRY (self->priv->tags_entry), TRUE);
+		tags = NULL;
+		for (i = 0; tagv[i] != NULL; i++)
+			tags = g_list_prepend (tags, tagv[i]);
+		tags = g_list_reverse (tags);
+		if (tags != NULL)
+			string_list = gth_string_list_new (tags);
+		else
+			string_list = NULL;
+
 		if (string_list != NULL)
 			g_file_info_set_attribute_object (info, "general::tags", G_OBJECT (string_list));
 		else
 			g_file_info_remove_attribute (info, "general::tags");
+
+		_g_object_unref (string_list);
+		g_list_free (tags);
+		g_strfreev (tagv);
 	}
 
 	/* rating */
@@ -410,9 +463,6 @@ gth_edit_general_page_real_update_info (GthEditCommentPage *base,
 	}
 
 	g_free (s);
-	_g_object_unref (string_list);
-	g_strfreev (tagv);
-	g_list_free (tags);
 	g_object_unref (file_data);
 }
 
diff --git a/extensions/edit_metadata/gth-edit-tags-dialog.c b/extensions/edit_metadata/gth-edit-tags-dialog.c
index c5d559c..53b242e 100644
--- a/extensions/edit_metadata/gth-edit-tags-dialog.c
+++ b/extensions/edit_metadata/gth-edit-tags-dialog.c
@@ -23,6 +23,7 @@
 #include <glib/gi18n.h>
 #include "gth-edit-metadata-dialog.h"
 #include "gth-edit-tags-dialog.h"
+#include "utils.h"
 
 
 #define GET_WIDGET(name) _gtk_builder_get_widget (self->priv->builder, (name))
@@ -56,17 +57,6 @@ gth_edit_tags_dialog_finalize (GObject *object)
 }
 
 
-static gboolean
-remove_tag_if_not_present (gpointer key,
-                	   gpointer value,
-                	   gpointer user_data)
-{
-	GthStringList *file_tags = user_data;
-
-	return g_list_find_custom (gth_string_list_get_list (file_tags), key, (GCompareFunc) g_strcmp0) == NULL;
-}
-
-
 static void
 gth_edit_tags_dialog_set_file_list (GthEditMetadataDialog *base,
 				    GList                 *file_list)
@@ -74,12 +64,8 @@ gth_edit_tags_dialog_set_file_list (GthEditMetadataDialog *base,
 	GthEditTagsDialog *self = GTH_EDIT_TAGS_DIALOG (base);
 	int                n_files;
 	char              *title;
-	GList             *scan;
-	GHashTable        *all_tags;
 	GHashTable        *common_tags;
 	GHashTable        *no_common_tags;
-	GList             *all_tags_list;
-	GList             *scan_tags;
 	GList             *common_tags_list;
 	GList             *no_common_tags_list;
 
@@ -101,48 +87,7 @@ gth_edit_tags_dialog_set_file_list (GthEditMetadataDialog *base,
 
 	/* update the tag entry */
 
-	all_tags = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
-	common_tags = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
-	for (scan = file_list; scan; scan = scan->next) {
-		GthFileData   *file_data = scan->data;
-		GthStringList *file_tags;
-
-		file_tags = (GthStringList *) g_file_info_get_attribute_object (file_data->info, "general::tags");
-		if (file_tags != NULL) {
-			GList *scan_tags;
-
-			for (scan_tags = gth_string_list_get_list (file_tags);
-			     scan_tags != NULL;
-			     scan_tags = scan_tags->next)
-			{
-				char *tag = scan_tags->data;
-
-				/* update the all tags set */
-
-				if (g_hash_table_lookup (all_tags, tag) == NULL)
-					g_hash_table_insert (all_tags, g_strdup (tag), GINT_TO_POINTER (1));
-
-				/* update the common tags set */
-
-				if (scan == file_list)
-					g_hash_table_insert (common_tags, g_strdup (tag), GINT_TO_POINTER (1));
-				else
-					g_hash_table_foreach_remove (common_tags, remove_tag_if_not_present, file_tags);
-			}
-		}
-		else
-			g_hash_table_remove_all (common_tags);
-	}
-
-	no_common_tags = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
-	all_tags_list = g_hash_table_get_keys (all_tags);
-	for (scan_tags = all_tags_list; scan_tags; scan_tags = scan_tags->next) {
-		char *tag = scan_tags->data;
-
-		if (g_hash_table_lookup (common_tags, tag) == NULL)
-			g_hash_table_insert (no_common_tags, g_strdup (tag), GINT_TO_POINTER (1));
-	}
-
+	utils_get_common_tags (file_list, &common_tags, &no_common_tags);
 	common_tags_list = g_hash_table_get_keys (common_tags);
 	no_common_tags_list = g_hash_table_get_keys (no_common_tags);
 	gth_tags_entry_set_tag_list (GTH_TAGS_ENTRY (self->priv->tags_entry),
@@ -151,24 +96,8 @@ gth_edit_tags_dialog_set_file_list (GthEditMetadataDialog *base,
 
 	g_list_free (no_common_tags_list);
 	g_list_free (common_tags_list);
-	g_list_free (all_tags_list);
 	g_hash_table_unref (no_common_tags);
 	g_hash_table_unref (common_tags);
-	g_hash_table_unref (all_tags);
-}
-
-
-static GHashTable *
-_g_hash_table_from_string_list (GthStringList *list)
-{
-	GHashTable *h;
-	GList      *scan;
-
-	h = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
-	for (scan = gth_string_list_get_list (list); scan; scan = scan->next)
-		g_hash_table_insert (h, g_strdup (scan->data), GINT_TO_POINTER (1));
-
-	return h;
 }
 
 
diff --git a/extensions/edit_metadata/utils.c b/extensions/edit_metadata/utils.c
new file mode 100644
index 0000000..eddb9d2
--- /dev/null
+++ b/extensions/edit_metadata/utils.c
@@ -0,0 +1,99 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+/*
+ *  GThumb
+ *
+ *  Copyright (C) 2012 Free Software Foundation, Inc.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include "utils.h"
+
+
+static gboolean
+remove_tag_if_not_present (gpointer key,
+                	   gpointer value,
+                	   gpointer user_data)
+{
+	GthStringList *file_tags = user_data;
+
+	return g_list_find_custom (gth_string_list_get_list (file_tags), key, (GCompareFunc) g_strcmp0) == NULL;
+}
+
+
+void
+utils_get_common_tags (GList       *file_list, /* GthFileData list */
+		       GHashTable **common_tags_out,
+		       GHashTable **other_tags_out)
+{
+	GHashTable *all_tags;
+	GHashTable *common_tags;
+	GList      *scan;
+	GHashTable *no_common_tags;
+	GList      *all_tags_list;
+	GList      *scan_tags;
+
+	all_tags = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+	common_tags = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+	for (scan = file_list; scan; scan = scan->next) {
+		GthFileData   *file_data = scan->data;
+		GthStringList *file_tags;
+
+		file_tags = (GthStringList *) g_file_info_get_attribute_object (file_data->info, "general::tags");
+		if (file_tags != NULL) {
+			for (scan_tags = gth_string_list_get_list (file_tags);
+			     scan_tags != NULL;
+			     scan_tags = scan_tags->next)
+			{
+				char *tag = scan_tags->data;
+
+				/* update the all tags set */
+
+				if (g_hash_table_lookup (all_tags, tag) == NULL)
+					g_hash_table_insert (all_tags, g_strdup (tag), GINT_TO_POINTER (1));
+
+				/* update the common tags set */
+
+				if (scan == file_list)
+					g_hash_table_insert (common_tags, g_strdup (tag), GINT_TO_POINTER (1));
+				else
+					g_hash_table_foreach_remove (common_tags, remove_tag_if_not_present, file_tags);
+			}
+		}
+		else
+			g_hash_table_remove_all (common_tags);
+	}
+
+	no_common_tags = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+	all_tags_list = g_hash_table_get_keys (all_tags);
+	for (scan_tags = all_tags_list; scan_tags; scan_tags = scan_tags->next) {
+		char *tag = scan_tags->data;
+
+		if (g_hash_table_lookup (common_tags, tag) == NULL)
+			g_hash_table_insert (no_common_tags, g_strdup (tag), GINT_TO_POINTER (1));
+	}
+
+	if (common_tags_out != NULL)
+		*common_tags_out = g_hash_table_ref (common_tags);
+
+	if (other_tags_out != NULL)
+		*other_tags_out = g_hash_table_ref (no_common_tags);
+
+	g_list_free (all_tags_list);
+	g_hash_table_unref (no_common_tags);
+	g_hash_table_unref (common_tags);
+	g_hash_table_unref (all_tags);
+}
diff --git a/extensions/edit_metadata/utils.h b/extensions/edit_metadata/utils.h
new file mode 100644
index 0000000..1ecc584
--- /dev/null
+++ b/extensions/edit_metadata/utils.h
@@ -0,0 +1,31 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+/*
+ *  GThumb
+ *
+ *  Copyright (C) 2012 The Free Software Foundation, Inc.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef UTILS_H
+#define UTILS_H
+
+#include <gthumb.h>
+
+void   utils_get_common_tags (GList       *file_list, /* GthFileData list */
+			      GHashTable **common_tags_out,
+			      GHashTable **other_tags_out);
+
+#endif /* UTILS_H */
diff --git a/gthumb/gth-string-list.c b/gthumb/gth-string-list.c
index 421cf55..6343264 100644
--- a/gthumb/gth-string-list.c
+++ b/gthumb/gth-string-list.c
@@ -187,3 +187,17 @@ gth_string_list_append (GthStringList *list1,
 		if (! g_list_find_custom (list1->priv->list, scan->data, (GCompareFunc) strcmp))
 			list1->priv->list = g_list_append (list1->priv->list, g_strdup (scan->data));
 }
+
+
+GHashTable *
+_g_hash_table_from_string_list (GthStringList *list)
+{
+	GHashTable *h;
+	GList      *scan;
+
+	h = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+	for (scan = gth_string_list_get_list (list); scan; scan = scan->next)
+		g_hash_table_insert (h, g_strdup (scan->data), GINT_TO_POINTER (1));
+
+	return h;
+}
diff --git a/gthumb/gth-string-list.h b/gthumb/gth-string-list.h
index c8083fd..9706398 100644
--- a/gthumb/gth-string-list.h
+++ b/gthumb/gth-string-list.h
@@ -61,6 +61,10 @@ gboolean          gth_string_list_equal               (GthStringList  *list1,
 void              gth_string_list_append              (GthStringList  *list1,
 		       	       	       	       	       GthStringList  *list2);
 
+/* utilities */
+
+GHashTable *      _g_hash_table_from_string_list      (GthStringList *list);
+
 G_END_DECLS
 
 #endif /* GTH_STRING_LIST_H */



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