[gthumb/ext: 7/15] save the search properties and update the search result.



commit a6881fdb18f9cc1db731a1194abe20265a93b435
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Fri Dec 11 20:44:37 2009 +0100

    save the search properties and update the search result.

 extensions/catalogs/dlg-catalog-properties.c |    9 +-
 extensions/catalogs/gth-catalog.c            |  140 +++++++++---------
 extensions/catalogs/gth-catalog.h            |   13 +-
 extensions/catalogs/main.c                   |   10 +-
 extensions/search/callbacks.c                |   40 +++++
 extensions/search/callbacks.h                |    6 +
 extensions/search/gth-search.c               |  202 +++++++++-----------------
 extensions/search/main.c                     |    2 +
 8 files changed, 207 insertions(+), 215 deletions(-)
---
diff --git a/extensions/catalogs/dlg-catalog-properties.c b/extensions/catalogs/dlg-catalog-properties.c
index f19398d..05df866 100644
--- a/extensions/catalogs/dlg-catalog-properties.c
+++ b/extensions/catalogs/dlg-catalog-properties.c
@@ -74,6 +74,8 @@ catalog_saved_cb (void     *buffer,
 		}
 		gth_catalog_update_metadata (data->catalog, data->file_data);
 		gth_monitor_metadata_changed (gth_main_get_default_monitor (), data->file_data);
+
+		gth_hook_invoke ("dlg-catalog-properties-saved", data->browser, data->file_data, data->catalog);
 	}
 	else
 		_gtk_error_dialog_from_gerror_show (GTK_WINDOW (data->browser), _("Could not save the catalog"), &error);
@@ -115,12 +117,7 @@ save_button_clicked_cb (GtkButton  *button,
 
 	/* invoke the hook to save derived catalogs such as searches */
 
-	if (gth_hook_invoke_get ("dlg-catalog-properties-save", data->builder, data->file_data, data->catalog) != NULL) {
-		gtk_widget_destroy (data->dialog);
-		return;
-	}
-
-	/* ...else save as a standard catalog */
+	gth_hook_invoke ("dlg-catalog-properties-save", data->builder, data->file_data, data->catalog);
 
 	gio_file = gth_catalog_file_to_gio_file (data->file_data->file);
 	buffer = gth_catalog_to_data (data->catalog, &buffer_size);
diff --git a/extensions/catalogs/gth-catalog.c b/extensions/catalogs/gth-catalog.c
index 2b51be1..b6c84f8 100644
--- a/extensions/catalogs/gth-catalog.c
+++ b/extensions/catalogs/gth-catalog.c
@@ -65,6 +65,47 @@ gth_catalog_finalize (GObject *object)
 }
 
 
+static DomElement *
+base_create_root (GthCatalog  *catalog,
+		  DomDocument *doc)
+{
+	return dom_document_create_element (doc, "catalog",
+					    "version", CATALOG_FORMAT,
+					    NULL);
+}
+
+
+static void
+base_read_from_doc (GthCatalog  *catalog,
+		    DomElement *root)
+{
+	DomElement *child;
+
+	gth_catalog_set_file_list (catalog, NULL);
+
+	for (child = root->first_child; child; child = child->next_sibling) {
+		if (g_strcmp0 (child->tag_name, "files") == 0) {
+			DomElement *file;
+
+			for (file = child->first_child; file; file = file->next_sibling) {
+				const char *uri;
+
+				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));
+			}
+		}
+		if (g_strcmp0 (child->tag_name, "order") == 0)
+			gth_catalog_set_order (catalog,
+					       dom_element_get_attribute (child, "type"),
+					       g_strcmp0 (dom_element_get_attribute (child, "inverse"), "1") == 0);
+		if (g_strcmp0 (child->tag_name, "date") == 0)
+			gth_datetime_from_exif_date (catalog->priv->date_time, dom_element_get_attribute (child, "value"));
+	}
+	catalog->priv->file_list = g_list_reverse (catalog->priv->file_list);
+}
+
+
 static void
 read_catalog_data_from_xml (GthCatalog  *catalog,
 		   	    const char  *buffer,
@@ -73,35 +114,9 @@ read_catalog_data_from_xml (GthCatalog  *catalog,
 {
 	DomDocument *doc;
 
-	gth_catalog_set_file_list (catalog, NULL);
-
 	doc = dom_document_new ();
-	if (dom_document_load (doc, buffer, count, error)) {
-		DomElement *root;
-		DomElement *child;
-
-		root = DOM_ELEMENT (doc)->first_child;
-		for (child = root->first_child; child; child = child->next_sibling) {
-			if (g_strcmp0 (child->tag_name, "files") == 0) {
-				DomElement *file;
-
-				for (file = child->first_child; file; file = file->next_sibling) {
-					const char *uri;
-
-					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));
-				}
-			}
-			if (g_strcmp0 (child->tag_name, "order") == 0)
-				gth_catalog_set_order (catalog,
-						       dom_element_get_attribute (child, "type"),
-						       g_strcmp0 (dom_element_get_attribute (child, "inverse"), "1") == 0);
-			if (g_strcmp0 (child->tag_name, "date") == 0)
-				gth_datetime_from_exif_date (catalog->priv->date_time, dom_element_get_attribute (child, "value"));
-		}
-		catalog->priv->file_list = g_list_reverse (catalog->priv->file_list);
-	}
+	if (dom_document_load (doc, buffer, count, error))
+		GTH_CATALOG_GET_CLASS (catalog)->read_from_doc (catalog, DOM_ELEMENT (doc)->first_child);
 
 	g_object_unref (doc);
 }
@@ -157,38 +172,10 @@ read_catalog_data_old_format (GthCatalog *catalog,
 
 
 static void
-base_load_from_data (GthCatalog  *catalog,
-		     const void  *buffer,
-		     gsize        count,
-		     GError     **error)
-{
-	char *text_buffer;
-
-	if (buffer == NULL)
-		return;
-
-	text_buffer = (char*) buffer;
-	if (strncmp (text_buffer, "<?xml ", 6) == 0)
-		read_catalog_data_from_xml (catalog, text_buffer, count, error);
-	else
-		read_catalog_data_old_format (catalog, text_buffer, count);
-}
-
-
-static char *
-base_to_data (GthCatalog *catalog,
-	      gsize      *length)
+base_write_to_doc (GthCatalog  *catalog,
+		   DomDocument *doc,
+		   DomElement  *root)
 {
-	DomDocument *doc;
-	DomElement  *root;
-	char        *data;
-
-	doc = dom_document_new ();
-	root = dom_document_create_element (doc, "catalog",
-					    "version", CATALOG_FORMAT,
-					    NULL);
-	dom_element_append_child (DOM_ELEMENT (doc), root);
-
 	if (catalog->priv->order != NULL)
 		dom_element_append_child (root, dom_document_create_element (doc, "order",
 									     "type", catalog->priv->order,
@@ -222,11 +209,6 @@ base_to_data (GthCatalog *catalog,
 			g_free (uri);
 		}
 	}
-	data = dom_document_dump (doc, length);
-
-	g_object_unref (doc);
-
-	return data;
 }
 
 
@@ -240,8 +222,9 @@ gth_catalog_class_init (GthCatalogClass *class)
 
 	object_class->finalize = gth_catalog_finalize;
 
-	class->load_from_data = base_load_from_data;
-	class->to_data = base_to_data;
+	class->create_root = base_create_root;
+	class->read_from_doc = base_read_from_doc;
+	class->write_to_doc = base_write_to_doc;
 }
 
 
@@ -340,7 +323,16 @@ gth_catalog_load_from_data (GthCatalog  *catalog,
 			    gsize        count,
 			    GError     **error)
 {
-	GTH_CATALOG_GET_CLASS (catalog)->load_from_data (catalog, buffer, count, error);
+	char *text_buffer;
+
+	if (buffer == NULL)
+		return;
+
+	text_buffer = (char *) buffer;
+	if (strncmp (text_buffer, "<?xml ", 6) == 0)
+		read_catalog_data_from_xml (catalog, text_buffer, count, error);
+	else
+		read_catalog_data_old_format (catalog, text_buffer, count);
 }
 
 
@@ -348,7 +340,19 @@ char *
 gth_catalog_to_data (GthCatalog *catalog,
 		     gsize      *length)
 {
-	return GTH_CATALOG_GET_CLASS (catalog)->to_data (catalog, length);
+	DomDocument *doc;
+	DomElement  *root;
+	char        *data;
+
+	doc = dom_document_new ();
+	root = GTH_CATALOG_GET_CLASS (catalog)->create_root (catalog, doc);
+	dom_element_append_child (DOM_ELEMENT (doc), root);
+	GTH_CATALOG_GET_CLASS (catalog)->write_to_doc (catalog, doc, root);
+	data = dom_document_dump (doc, length);
+
+	g_object_unref (doc);
+
+	return data;
 }
 
 
diff --git a/extensions/catalogs/gth-catalog.h b/extensions/catalogs/gth-catalog.h
index ff42b5a..a60bd94 100644
--- a/extensions/catalogs/gth-catalog.h
+++ b/extensions/catalogs/gth-catalog.h
@@ -57,12 +57,13 @@ struct _GthCatalogClass
 
 	/*< virtual functions >*/
 
-	void   (*load_from_data) (GthCatalog  *catalog,
-				  const void  *buffer,
-				  gsize        count,
-				  GError     **error);
-	char * (*to_data)        (GthCatalog  *catalog,
-		     	          gsize       *length);
+	DomElement  * (*create_root)    (GthCatalog  *catalog,
+					 DomDocument *doc);
+	void          (*read_from_doc)  (GthCatalog  *catalog,
+					 DomElement  *root);
+	void          (*write_to_doc)   (GthCatalog  *catalog,
+					 DomDocument *doc,
+					 DomElement  *root);
 };
 
 typedef void (*CatalogReadyCallback) (GthCatalog *catalog,
diff --git a/extensions/catalogs/main.c b/extensions/catalogs/main.c
index 401e5df..a61dd31 100644
--- a/extensions/catalogs/main.c
+++ b/extensions/catalogs/main.c
@@ -55,10 +55,18 @@ gthumb_extension_activate (void)
 	 * @builder   (GtkBuilder *): the builder relative to the window
 	 * @file_data (GthFileData *): the catalog file
 	 * @catalog (GthCatalog *): the catalog data
-	 * @return (gboolean): TRUE if the catalog has been saved, FALSE otherwise.
 	 **/
 	gth_hook_register ("dlg-catalog-properties-save", 3);
 
+	/**
+	 * Called after saving the catalog properties.
+	 *
+	 * @browser (GthBrowser *): the main window
+	 * @file_data (GthFileData *): the catalog file
+	 * @catalog (GthCatalog *): the catalog data
+	 **/
+	gth_hook_register ("dlg-catalog-properties-saved", 3);
+
 	gth_hook_add_callback ("gth-catalog-load-from-data", 10, G_CALLBACK (catalogs__gth_catalog_load_from_data_cb), NULL);
 
 	gth_main_register_file_source (GTH_TYPE_FILE_SOURCE_CATALOGS);
diff --git a/extensions/search/callbacks.c b/extensions/search/callbacks.c
index b0e064e..75c13c0 100644
--- a/extensions/search/callbacks.c
+++ b/extensions/search/callbacks.c
@@ -29,6 +29,7 @@
 #include "actions.h"
 #include "gth-search.h"
 #include "gth-search-editor.h"
+#include "gth-search-task.h"
 
 
 #define BROWSER_DATA_KEY "search-browser-data"
@@ -187,4 +188,43 @@ search__dlg_catalog_properties (GtkBuilder  *builder,
 	search_editor = gth_search_editor_new (GTH_SEARCH (catalog));
 	gtk_widget_show (search_editor);
 	gtk_container_add (GTK_CONTAINER (alignment), search_editor);
+	g_object_set_data (G_OBJECT (builder), "search_editor", search_editor);
+}
+
+
+void
+search__dlg_catalog_properties_save (GtkBuilder  *builder,
+				     GthFileData *file_data,
+				     GthCatalog  *catalog)
+{
+	GthSearch *search;
+
+	if (! _g_content_type_is_a (g_file_info_get_content_type (file_data->info), "gthumb/search"))
+		return;
+
+	g_return_if_fail (GTH_IS_SEARCH (catalog));
+
+	search = gth_search_editor_get_search (GTH_SEARCH_EDITOR (g_object_get_data (G_OBJECT(builder), "search_editor")), NULL);
+	if (search != NULL) {
+		gth_search_set_folder (GTH_SEARCH (catalog), gth_search_get_folder (search));
+		gth_search_set_recursive (GTH_SEARCH (catalog), gth_search_is_recursive (search));
+		gth_search_set_test (GTH_SEARCH (catalog), gth_search_get_test (search));
+	}
+}
+
+
+void
+search__dlg_catalog_properties_saved (GthBrowser  *browser,
+				      GthFileData *file_data,
+				      GthCatalog  *catalog)
+{
+	GthTask *task;
+
+	if (! _g_content_type_is_a (g_file_info_get_content_type (file_data->info), "gthumb/search"))
+		return;
+
+	task = gth_search_task_new (browser, GTH_SEARCH (catalog), file_data->file);
+	gth_browser_exec_task (browser, task, TRUE);
+
+	g_object_unref (task);
 }
diff --git a/extensions/search/callbacks.h b/extensions/search/callbacks.h
index 7c5d25d..09f2871 100644
--- a/extensions/search/callbacks.h
+++ b/extensions/search/callbacks.h
@@ -33,5 +33,11 @@ GthCatalog * search__gth_catalog_load_from_data_cb       (const void  *buffer);
 void         search__dlg_catalog_properties              (GthBrowser  *browser,
 							  GtkBuilder  *builder,
 							  GthFileData *file_data);
+void         search__dlg_catalog_properties_save         (GtkBuilder  *builder,
+							  GthFileData *file_data,
+							  GthCatalog  *catalog);
+void         search__dlg_catalog_properties_saved        (GthBrowser  *browser,
+							  GthFileData *file_data,
+							  GthCatalog  *catalog);
 
 #endif /* CALLBACKS_H */
diff --git a/extensions/search/gth-search.c b/extensions/search/gth-search.c
index 69abb4f..3022054 100644
--- a/extensions/search/gth-search.c
+++ b/extensions/search/gth-search.c
@@ -41,115 +41,96 @@ static DomDomizableIface  *dom_domizable_parent_iface = NULL;
 static GthDuplicableIface *gth_duplicable_parent_iface = NULL;
 
 
-static DomElement*
-gth_search_real_create_element (DomDomizable *base,
-				DomDocument  *doc)
+static DomElement *
+gth_search_create_root (GthCatalog  *catalog,
+			DomDocument *doc)
 {
-	GthSearch  *self;
-	DomElement *element;
-	char       *uri;
-	const char *sort_order;
-	gboolean    sort_inverse;
-	GList      *file_list;
-	
-	g_return_val_if_fail (DOM_IS_DOCUMENT (doc), NULL);
-
-	self = GTH_SEARCH (base);
-	
-	element = dom_document_create_element (doc, "search",
-					       "version", SEARCH_FORMAT,
-					       NULL);
-	
-       	uri = g_file_get_uri (self->priv->folder);
-	dom_element_append_child (element,
-				  dom_document_create_element (doc, "folder",
-							       "uri", uri,
-							       "recursive", (self->priv->recursive ? "true" : "false"), 
-							       NULL));
-	g_free (uri);					       
-
-	dom_element_append_child (element, dom_domizable_create_element (DOM_DOMIZABLE (self->priv->test), doc));
-
-	sort_order = gth_catalog_get_order (GTH_CATALOG (self), &sort_inverse);
-	if (sort_order != NULL)
-		dom_element_append_child (element, dom_document_create_element (doc, "order",
-									       "type", sort_order,
-									       "inverse", (sort_inverse ? "1" : "0"),
-									       NULL));
-
-	file_list = gth_catalog_get_file_list (GTH_CATALOG (self));
-	if (file_list != NULL) {
-		DomElement *e_file_list;
-		GList      *scan;
-	
-		e_file_list = dom_document_create_element (doc, "files", NULL);
-		dom_element_append_child (element, e_file_list);
-		for (scan = file_list; scan; scan = scan->next) {
-			GFile *file = scan->data;
-			char  *uri;
-
-			uri = g_file_get_uri (file);
-			dom_element_append_child (e_file_list, dom_document_create_element (doc, "file", "uri", uri, NULL));
-			
-			g_free (uri);
-		}
-	}
-
-	return element;
+	return dom_document_create_element (doc, "search",
+					    "version", SEARCH_FORMAT,
+					    NULL);
 }
 
 
 static void
-gth_search_real_load_from_element (DomDomizable *base,
-				   DomElement   *element)
+gth_search_read_from_doc (GthCatalog *base,
+			  DomElement *root)
 {
 	GthSearch  *self;
 	DomElement *node;
-	GFile      *folder;
-	GList      *files = NULL;
-	
-	g_return_if_fail (DOM_IS_ELEMENT (element));
+
+	g_return_if_fail (DOM_IS_ELEMENT (root));
 
 	self = GTH_SEARCH (base);
-	
-	gth_search_set_test (self, NULL);
+	GTH_CATALOG_CLASS (parent_class)->read_from_doc (GTH_CATALOG (self), root);
 
-	for (node = element->first_child; node; node = node->next_sibling) {
+	gth_search_set_test (self, NULL);
+	for (node = root->first_child; node; node = node->next_sibling) {
 		if (g_strcmp0 (node->tag_name, "folder") == 0) {
+			GFile *folder;
+
 			folder = g_file_new_for_uri (dom_element_get_attribute (node, "uri"));
 			gth_search_set_folder (self, folder);
 			g_object_unref (folder);
-			
+
 			gth_search_set_recursive (self, (g_strcmp0 (dom_element_get_attribute (node, "recursive"), "true") == 0));
 		}
 		else if (g_strcmp0 (node->tag_name, "tests") == 0) {
 			GthTest *test;
-			
+
 			test = gth_test_chain_new (GTH_MATCH_TYPE_NONE, NULL);
 			dom_domizable_load_from_element (DOM_DOMIZABLE (test), node);
 			gth_search_set_test (self, GTH_TEST_CHAIN (test));
 		}
-		else if (g_strcmp0 (node->tag_name, "files") == 0) {
-			DomElement *file;
-			
-			for (file = node->first_child; file; file = file->next_sibling) {
-				const char *uri;
-				
-				uri = dom_element_get_attribute (file, "uri");
-				if (uri != NULL)
-					files = g_list_prepend (files, g_file_new_for_uri (uri));
-			}
-			files = g_list_reverse (files);
-		}
-		else if (g_strcmp0 (node->tag_name, "order") == 0)
-			gth_catalog_set_order (GTH_CATALOG (self),
-					       dom_element_get_attribute (node, "type"),
-					       g_strcmp0 (dom_element_get_attribute (node, "inverse"), "1") == 0);
-
 	}
-	gth_catalog_set_file_list (GTH_CATALOG (self), files);
+}
+
+
+static void
+gth_search_write_to_doc (GthCatalog  *catalog,
+			 DomDocument *doc,
+			 DomElement  *root)
+{
+	GthSearch  *self;
+	char       *uri;
+
+	GTH_CATALOG_CLASS (parent_class)->write_to_doc (catalog, doc, root);
+
+	self = GTH_SEARCH (catalog);
+
+       	uri = g_file_get_uri (self->priv->folder);
+	dom_element_append_child (root,
+				  dom_document_create_element (doc, "folder",
+							       "uri", uri,
+							       "recursive", (self->priv->recursive ? "true" : "false"),
+							       NULL));
+	g_free (uri);
+
+	dom_element_append_child (root, dom_domizable_create_element (DOM_DOMIZABLE (self->priv->test), doc));
+}
+
+
+static DomElement*
+gth_search_real_create_element (DomDomizable *base,
+				DomDocument  *doc)
+{
+	GthSearch  *self;
+	DomElement *element;
 	
-	_g_object_list_unref (files);
+	g_return_val_if_fail (DOM_IS_DOCUMENT (doc), NULL);
+
+	self = GTH_SEARCH (base);
+	element = gth_search_create_root (GTH_CATALOG (self), doc);
+	gth_search_write_to_doc (GTH_CATALOG (self), doc, element);
+	
+	return element;
+}
+
+
+static void
+gth_search_real_load_from_element (DomDomizable *base,
+				   DomElement   *element)
+{
+	gth_search_read_from_doc (GTH_CATALOG (base), element);
 }
 
 
@@ -204,54 +185,6 @@ gth_search_finalize (GObject *object)
 
 
 static void
-gth_search_load_from_data (GthCatalog  *catalog,
-			   const void  *buffer,
-			   gsize        count,
-			   GError     **error)
-{
-	GthSearch   *search = GTH_SEARCH (catalog);
-	DomDocument *doc;
-	DomElement  *root;
-	
-		
-	doc = dom_document_new ();
-	if (! dom_document_load (doc, (const char *) buffer, count, error)) 
-		return;
-	
-	root = DOM_ELEMENT (doc)->first_child;
-	if (g_strcmp0 (root->tag_name, "search") != 0) {
-		*error = g_error_new_literal (DOM_ERROR, DOM_ERROR_INVALID_FORMAT, _("Invalid file format"));
-		return;
-	}
-	
-	dom_domizable_load_from_element (DOM_DOMIZABLE (search), root);
-	
-	g_object_unref (doc);
-}
-
-
-static char *
-gth_search_to_data (GthCatalog *catalog, 
-	  	    gsize      *length)
-{
-	GthSearch   *search = GTH_SEARCH (catalog);
-	DomDocument *doc;
-	DomElement  *root;
-	char        *data;
-	
-	doc = dom_document_new ();
-	root = dom_domizable_create_element (DOM_DOMIZABLE (search), doc);
-	dom_element_append_child (DOM_ELEMENT (doc), root);
-	
-	data = dom_document_dump (doc, length);
-	
-	g_object_unref (doc);
-	
-	return data;
-}
-
-
-static void
 gth_search_class_init (GthSearchClass *class)
 {
 	GObjectClass    *object_class;
@@ -263,8 +196,9 @@ gth_search_class_init (GthSearchClass *class)
 	object_class->finalize = gth_search_finalize;
 	
 	catalog_class = GTH_CATALOG_CLASS (class);
-	catalog_class->load_from_data = gth_search_load_from_data;
-	catalog_class->to_data = gth_search_to_data;
+	catalog_class->create_root = gth_search_create_root;
+	catalog_class->read_from_doc = gth_search_read_from_doc;
+	catalog_class->write_to_doc = gth_search_write_to_doc;
 }
 
 
diff --git a/extensions/search/main.c b/extensions/search/main.c
index 2bf20de..3dbea45 100644
--- a/extensions/search/main.c
+++ b/extensions/search/main.c
@@ -34,6 +34,8 @@ gthumb_extension_activate (void)
 	gth_hook_add_callback ("gth-browser-construct", 10, G_CALLBACK (search__gth_browser_construct_cb), NULL);
 	gth_hook_add_callback ("gth-browser-update-extra-widget", 20, G_CALLBACK (search__gth_browser_update_extra_widget_cb), NULL);
 	gth_hook_add_callback ("dlg-catalog-properties", 10, G_CALLBACK (search__dlg_catalog_properties), NULL);
+	gth_hook_add_callback ("dlg-catalog-properties-save", 10, G_CALLBACK (search__dlg_catalog_properties_save), NULL);
+	gth_hook_add_callback ("dlg-catalog-properties-saved", 10, G_CALLBACK (search__dlg_catalog_properties_saved), NULL);
 }
 
 



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