[libgda] Picture plugin corrections



commit 92d646682ecca05007ea4ff69c4b49a7d25bef2b
Author: Vivien Malerba <malerba gnome-db org>
Date:   Sat Sep 17 22:45:38 2011 +0200

    Picture plugin corrections

 libgda-ui/data-entries/plugins/common-pict.c       |   42 ++++++++++----------
 libgda-ui/data-entries/plugins/common-pict.h       |    6 ++-
 .../plugins/gdaui-data-cell-renderer-pict.c        |    5 +-
 libgda-ui/data-entries/plugins/gdaui-entry-pict.c  |   13 +++---
 4 files changed, 36 insertions(+), 30 deletions(-)
---
diff --git a/libgda-ui/data-entries/plugins/common-pict.c b/libgda-ui/data-entries/plugins/common-pict.c
index 3574742..9dc04e7 100644
--- a/libgda-ui/data-entries/plugins/common-pict.c
+++ b/libgda-ui/data-entries/plugins/common-pict.c
@@ -372,11 +372,9 @@ file_load_cb (GtkWidget *button, PictMenuData *menudata)
 		gdaui_set_default_path (gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dlg)));
 
 		if (g_file_get_contents (filename, &data, &length, &error)) {
-			if (menudata->bindata->data) {
-				g_free (menudata->bindata->data);
-				menudata->bindata->data = NULL;
-				menudata->bindata->data_length = 0;
-			}
+			g_free (menudata->bindata->data);
+			menudata->bindata->data = NULL;
+			menudata->bindata->data_length = 0;
 
 			if (menudata->options->serialize) {
 				GdkPixdata pixdata;
@@ -404,7 +402,9 @@ file_load_cb (GtkWidget *button, PictMenuData *menudata)
 
 			/* call the callback */
 			if (menudata->callback)
-				(menudata->callback) (menudata->data);
+				(menudata->callback) (menudata->bindata, menudata->data);
+			menudata->bindata->data = NULL;
+			menudata->bindata->data_length = 0;
 		}
 		else {
 			GtkWidget *msg;
@@ -597,23 +597,24 @@ void
 common_pict_init_cache (PictOptions *options)
 {
 	g_assert (!options->pixbuf_hash);
-	options->pixbuf_hash = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_object_unref);
+	options->pixbuf_hash = g_hash_table_new_full (g_int_hash, g_int_equal,
+						      g_free, g_object_unref);
 }
 
 /* 
  * computes a "hash" of the binary data 
  */
-static guint
+static gint
 compute_hash (guchar *data, glong data_length)
 {
-	guint result = 0;
+	gint result = 0;
 	guchar *ptr;
 
 	if (!data)
 		return 0;
 	for (ptr = data; ptr <= data + data_length - 1; ptr++)
 		result += *ptr;
-	
+
 	return result;
 }
 
@@ -623,18 +624,17 @@ compute_hash (guchar *data, glong data_length)
 void
 common_pict_add_cached_pixbuf (PictOptions *options, const GValue *value, GdkPixbuf *pixbuf)
 {
-	guint hash;
+	gint *hash;
 	g_return_if_fail (pixbuf);
 
 	if (!options->pixbuf_hash || !value)
 		return;
-
 	else if (GDA_VALUE_HOLDS_BINARY (value)) {
 		const GdaBinary *bin;
 		bin = gda_value_get_binary (value);
-		hash = compute_hash (bin->data, bin->binary_length);
-		g_hash_table_insert (options->pixbuf_hash, GUINT_TO_POINTER (hash), pixbuf);
-		g_object_ref (pixbuf);
+		hash = g_new (guint, 1);
+		*hash = compute_hash (bin->data, bin->binary_length);
+		g_hash_table_insert (options->pixbuf_hash, hash, g_object_ref (pixbuf));
 	}
 	else if (GDA_VALUE_HOLDS_BLOB (value)) {
 		const GdaBinary *bin;
@@ -644,9 +644,9 @@ common_pict_add_cached_pixbuf (PictOptions *options, const GValue *value, GdkPix
 		if (bin) {
 			if (!bin->data && blob->op)
 				gda_blob_op_read_all (blob->op, (GdaBlob*) blob);
-			hash = compute_hash (bin->data, bin->binary_length);
-			g_hash_table_insert (options->pixbuf_hash, GUINT_TO_POINTER (hash), pixbuf);
-			g_object_ref (pixbuf);
+			hash = g_new (guint, 1);
+			*hash = compute_hash (bin->data, bin->binary_length);
+			g_hash_table_insert (options->pixbuf_hash, hash, g_object_ref (pixbuf));
 		}
 	}
 }
@@ -658,7 +658,7 @@ GdkPixbuf *
 common_pict_fetch_cached_pixbuf (PictOptions *options, const GValue *value)
 {
 	GdkPixbuf *pixbuf = NULL;
-	guint hash;
+	gint hash;
 
 	if (!options->pixbuf_hash)
 		return NULL;
@@ -669,7 +669,7 @@ common_pict_fetch_cached_pixbuf (PictOptions *options, const GValue *value)
 		bin = gda_value_get_binary (value);
 		if (bin) {
 			hash = compute_hash (bin->data, bin->binary_length);
-			pixbuf = g_hash_table_lookup (options->pixbuf_hash, GUINT_TO_POINTER (hash));
+			pixbuf = g_hash_table_lookup (options->pixbuf_hash, &hash);
 		}
 	}
 	else if (GDA_VALUE_HOLDS_BLOB (value)) {
@@ -681,7 +681,7 @@ common_pict_fetch_cached_pixbuf (PictOptions *options, const GValue *value)
 			if (!bin->data && blob->op)
 				gda_blob_op_read_all (blob->op, (GdaBlob*) blob);
 			hash = compute_hash (bin->data, bin->binary_length);
-			pixbuf = g_hash_table_lookup (options->pixbuf_hash, GUINT_TO_POINTER (hash));
+			pixbuf = g_hash_table_lookup (options->pixbuf_hash, &hash);
 		}
 	}
 
diff --git a/libgda-ui/data-entries/plugins/common-pict.h b/libgda-ui/data-entries/plugins/common-pict.h
index 2b8e993..77a881a 100644
--- a/libgda-ui/data-entries/plugins/common-pict.h
+++ b/libgda-ui/data-entries/plugins/common-pict.h
@@ -47,7 +47,11 @@ typedef struct {
 	gint height;
 } PictAllocation;
 
-typedef void (*PictCallback) (gpointer);
+/*
+ * Notice: when the PictCallback function is called, the bindata's contents are
+ * given to the function, which means it is responsible to free @bindata->data
+ */
+typedef void (*PictCallback) (PictBinData *, gpointer);
 typedef struct {
 	GtkWidget    *menu; /* popup menu */
 	GtkWidget    *load_mitem;
diff --git a/libgda-ui/data-entries/plugins/gdaui-data-cell-renderer-pict.c b/libgda-ui/data-entries/plugins/gdaui-data-cell-renderer-pict.c
index d3e1cab..87caaab 100644
--- a/libgda-ui/data-entries/plugins/gdaui-data-cell-renderer-pict.c
+++ b/libgda-ui/data-entries/plugins/gdaui-data-cell-renderer-pict.c
@@ -420,12 +420,13 @@ gdaui_data_cell_renderer_pict_render (GtkCellRenderer      *cell,
 }
 
 static void
-pict_data_changed_cb (GdauiDataCellRendererPict *pictcell)
+pict_data_changed_cb (PictBinData *bindata, GdauiDataCellRendererPict *pictcell)
 {
 	GValue *value;
 
-	value = common_pict_get_value (&(pictcell->priv->bindata), &(pictcell->priv->options),
+	value = common_pict_get_value (bindata, &(pictcell->priv->options),
 				       pictcell->priv->type);
+	g_free (bindata->data);
 	g_signal_emit (G_OBJECT (pictcell), pixbuf_cell_signals[CHANGED], 0,
 		       g_object_get_data (G_OBJECT (pictcell), "last-path"), value);
 	gda_value_free (value);
diff --git a/libgda-ui/data-entries/plugins/gdaui-entry-pict.c b/libgda-ui/data-entries/plugins/gdaui-entry-pict.c
index 19af6da..9bdf3a8 100644
--- a/libgda-ui/data-entries/plugins/gdaui-entry-pict.c
+++ b/libgda-ui/data-entries/plugins/gdaui-entry-pict.c
@@ -276,8 +276,11 @@ size_allocate_cb (G_GNUC_UNUSED GtkWidget *wid, GtkAllocation *allocation, Gdaui
 }
 
 static void
-pict_data_changed_cb (GdauiEntryPict *mgpict)
+pict_data_changed_cb (PictBinData *bindata, GdauiEntryPict *mgpict)
 {
+	g_free (mgpict->priv->bindata.data);
+	mgpict->priv->bindata.data = bindata->data;
+	mgpict->priv->bindata.data_length = bindata->data_length;
 	display_image (mgpict, NULL, NULL, NULL);
 	gdaui_entry_wrapper_contents_changed (GDAUI_ENTRY_WRAPPER (mgpict));
 	gdaui_entry_wrapper_contents_activated (GDAUI_ENTRY_WRAPPER (mgpict));
@@ -358,11 +361,9 @@ real_set_value (GdauiEntryWrapper *mgwrap, const GValue *value)
 	mgpict = GDAUI_ENTRY_PICT (mgwrap);
 	g_return_if_fail (mgpict->priv);
 
-	if (mgpict->priv->bindata.data) {
-		g_free (mgpict->priv->bindata.data);
-		mgpict->priv->bindata.data = NULL;
-		mgpict->priv->bindata.data_length = 0;
-	}
+	g_free (mgpict->priv->bindata.data);
+	mgpict->priv->bindata.data = NULL;
+	mgpict->priv->bindata.data_length = 0;
 
 	/* fill in mgpict->priv->data */
 	if (!common_pict_load_data (&(mgpict->priv->options), value, &(mgpict->priv->bindata), &stock, &error)) {



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