[Buoh-dev] CVS commit to /cvsroot/buoh/buoh/src by carlosgc



CVS commit to /cvsroot/buoh/buoh/src by carlosgc

Modified Files:
	buoh-comic-cache.c buoh-comic-cache.h buoh-comic-loader.c 
	buoh-comic-loader.h buoh-comic.c buoh-comic.h 
	buoh-view-comic.c 
Log Message:
2005-11-30  Carlos Garcia Campos  <carlosgc gnome org>
	* src/buoh-comic-cache.[ch]:
	* src/buoh-comic-loader.[ch]:
	* src/buoh-comic.[ch]: Add image property
	* src/buoh-view-comic.c:
	Store compressed images instead of pixbufs in cache. Reduce the cache
	size to 1MB. The memory consumption is really smaller right now. 

===================================================================
RCS file: /cvsroot/buoh/buoh/src/buoh-comic-cache.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- /cvsroot/buoh/buoh/src/buoh-comic-cache.c	2005/11/16 20:29:28	1.2
+++ /cvsroot/buoh/buoh/src/buoh-comic-cache.c	2005/11/30 10:48:02	1.3
@@ -18,6 +18,9 @@
 
 #include <glib.h>
 #include <glib/gstdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 #include <unistd.h>
 
 #include "buoh.h"
@@ -26,16 +29,19 @@
 struct _BuohComicCachePrivate {
 	gchar      *cache_dir;
 	
-	GHashTable *pixbuf_hash;
-	GList      *pixbuf_list;
-	GList      *pixbuf_disk;
+	GHashTable *image_hash;
+	GList      *image_list;
+	GList      *image_disk;
 	gulong      size;
+
+	GdkPixbuf  *current_pixbuf;
+	gchar      *current_uri;
 };
 
 #define BUOH_COMIC_CACHE_GET_PRIVATE(object) \
         (G_TYPE_INSTANCE_GET_PRIVATE ((object), BUOH_TYPE_COMIC_CACHE, BuohComicCachePrivate))
 
-#define CACHE_SIZE 5242880 /* 5MB */
+#define CACHE_SIZE 1048576 /* 1MB */
 
 static GObjectClass *parent_class = NULL;
 
@@ -76,13 +82,16 @@
 	buoh_comic_cache->priv->cache_dir =
 		g_build_filename (buoh_get_datadir (BUOH), "cache", NULL);
 	
-	buoh_comic_cache->priv->pixbuf_list = NULL;
-	buoh_comic_cache->priv->pixbuf_hash =
+	buoh_comic_cache->priv->image_list = NULL;
+	buoh_comic_cache->priv->image_hash =
 		g_hash_table_new_full (g_str_hash,
 				       g_str_equal,
 				       g_free,
-				       g_object_unref);
-	buoh_comic_cache->priv->pixbuf_disk = NULL;
+				       (GDestroyNotify)buoh_comic_image_free);
+	buoh_comic_cache->priv->image_disk = NULL;
+
+	buoh_comic_cache->priv->current_pixbuf = NULL;
+	buoh_comic_cache->priv->current_uri = NULL;
 	
 	buoh_comic_cache->priv->size = 0;
 }
@@ -119,22 +128,32 @@
 		comic_cache->priv->cache_dir = NULL;
 	}
 	
-	if (comic_cache->priv->pixbuf_list) {
-		g_list_free (comic_cache->priv->pixbuf_list);
-		comic_cache->priv->pixbuf_list = NULL;
+	if (comic_cache->priv->image_list) {
+		g_list_free (comic_cache->priv->image_list);
+		comic_cache->priv->image_list = NULL;
 	}
 
-	if (comic_cache->priv->pixbuf_hash) {
-		g_hash_table_destroy (comic_cache->priv->pixbuf_hash);
-		comic_cache->priv->pixbuf_hash = NULL;
+	if (comic_cache->priv->image_hash) {
+		g_hash_table_destroy (comic_cache->priv->image_hash);
+		comic_cache->priv->image_hash = NULL;
 	}
 
-	if (comic_cache->priv->pixbuf_disk) {
-		g_list_foreach (comic_cache->priv->pixbuf_disk,
+	if (comic_cache->priv->image_disk) {
+		g_list_foreach (comic_cache->priv->image_disk,
 				(GFunc) buoh_comic_cache_free_disk,
 				NULL);
-		g_list_free (comic_cache->priv->pixbuf_disk);
-		comic_cache->priv->pixbuf_disk = NULL;
+		g_list_free (comic_cache->priv->image_disk);
+		comic_cache->priv->image_disk = NULL;
+	}
+
+	if (comic_cache->priv->current_pixbuf) {
+		g_object_unref (comic_cache->priv->current_pixbuf);
+		comic_cache->priv->current_pixbuf = NULL;
+	}
+
+	if (comic_cache->priv->current_uri) {
+		g_free (comic_cache->priv->current_uri);
+		comic_cache->priv->current_uri = NULL;
 	}
 
 	if (G_OBJECT_CLASS (parent_class)->finalize)
@@ -176,154 +195,243 @@
 static void
 buoh_comic_cache_to_disk (BuohComicCache *cache,
 			  const gchar    *uri,
-			  GdkPixbuf      *pixbuf)
+			  BuohComicImage *image)
 {
 	gchar  *path;
-	GError *error = NULL;
+	gint    fd;
 
 	g_assert (uri != NULL);
-	g_assert (GDK_IS_PIXBUF (pixbuf));
+	g_assert (image != NULL);
 
 	path = buoh_comic_cache_uri_to_filename (cache, uri);
 	buoh_debug ("CACHE: caching (disk) %s", path);
 
-	if (g_file_test (path, G_FILE_TEST_EXISTS)) {
+	if (g_list_find_custom (cache->priv->image_disk,
+				(gconstpointer) path,
+				(GCompareFunc) g_ascii_strcasecmp)) {
+		/* Already on disk */
 		g_free (path);
 		return;
 	}
 	
-	gdk_pixbuf_save (pixbuf, path, "png", &error, NULL);
-	
-	if (error) {
-		g_warning (error->message);
-		g_error_free (error);
+	if ((fd = open (path, O_CREAT | O_WRONLY, 0644)) < 0) {
+		g_warning ("Error saving %s to disk", uri);
 		g_free (path);
+		return;
+	}
 
+	if (write (fd, image->data, image->size) < 0) {
+		g_warning ("Error saving %s to disk", uri);
+		close (fd);
+		g_free (path);
 		return;
 	}
 
-	if (!g_list_find_custom (cache->priv->pixbuf_disk,
-				 (gconstpointer) path,
-				 (GCompareFunc) g_ascii_strcasecmp)) {
-		cache->priv->pixbuf_disk = g_list_prepend (cache->priv->pixbuf_disk,
-							   g_strdup (path));
+	if (close (fd) < 0) {
+		g_free (path);
+		return;
 	}
 	
+	cache->priv->image_disk = g_list_prepend (cache->priv->image_disk,
+						  g_strdup (path));
 	g_free (path);
 }
 
-static gulong
-pixbuf_get_size (const GdkPixbuf *pixbuf)
-{
-	g_assert (GDK_IS_PIXBUF (pixbuf));
+static void
+buoh_comic_cache_set_current (BuohComicCache *cache,
+			      const gchar    *uri,
+			      BuohComicImage *image)
+{
+	GdkPixbufLoader *loader;
+	GError          *error = NULL;
+
+	if (cache->priv->current_uri &&
+	    (g_ascii_strcasecmp (uri, cache->priv->current_uri) == 0) &&
+	    GDK_IS_PIXBUF (cache->priv->current_pixbuf))
+		return;
+	
+	if (cache->priv->current_pixbuf)
+		g_object_unref (cache->priv->current_pixbuf);
+	if (cache->priv->current_uri)
+		g_free (cache->priv->current_uri);
+
+	loader = gdk_pixbuf_loader_new ();
+	gdk_pixbuf_loader_write (loader, image->data,
+				 image->size, &error);
+	if (error) {
+		g_warning (error->message);
+		g_clear_error (&error);
+		
+		cache->priv->current_pixbuf = NULL;
+		cache->priv->current_uri = NULL;
+		gdk_pixbuf_loader_close (loader, NULL);
+		g_object_unref (loader);
+		
+		return;
+	}
+	
+	cache->priv->current_pixbuf =
+		gdk_pixbuf_loader_get_pixbuf (loader);
+	g_object_ref (cache->priv->current_pixbuf);
+	gdk_pixbuf_loader_close (loader, &error);
+	g_object_unref (loader);
+
+	if (error) {
+		g_warning (error->message);
+		g_clear_error (&error);
 
-	gint height = gdk_pixbuf_get_height (pixbuf);
-	gint width = gdk_pixbuf_get_width (pixbuf);
-	gint rowstride = gdk_pixbuf_get_rowstride (pixbuf);
-	gint n_channels = gdk_pixbuf_get_n_channels (pixbuf);
+		cache->priv->current_pixbuf = NULL;
+		cache->priv->current_uri = NULL;
 
-	return ((height - 1) * rowstride + width * n_channels);
+		return;
+	}
+
+	cache->priv->current_uri = g_strdup (uri);
 }
 
 void
-buoh_comic_cache_set_pixbuf (BuohComicCache *cache,
-			     const gchar    *uri,
-			     GdkPixbuf      *pixbuf)
+buoh_comic_cache_set_image (BuohComicCache *cache,
+			    const gchar    *uri,
+			    BuohComicImage *image)
 {
-	gulong  size;
-	gchar  *key_uri;
+	gchar          *key_uri;
+	BuohComicImage *img;
 
 	g_return_if_fail (BUOH_IS_COMIC_CACHE (cache));
 	g_return_if_fail (uri != NULL);
-	g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
+	g_return_if_fail (image != NULL);
 
 	buoh_debug ("CACHE: uri %s", uri);
 	
-	if (g_hash_table_lookup (cache->priv->pixbuf_hash, uri))
+	if ((img = g_hash_table_lookup (cache->priv->image_hash, uri))) {
+		buoh_comic_cache_set_current (cache, uri, img);
 		return;
+	}
 
-	size = pixbuf_get_size (pixbuf);
-	buoh_debug ("CACHE: pixbuf size %d", size);
+	buoh_debug ("CACHE: image size %d", image->size);
 
-	if (size > CACHE_SIZE) {
-		buoh_comic_cache_to_disk (cache, uri, pixbuf);
+	if (image->size > CACHE_SIZE) {
+		buoh_comic_cache_to_disk (cache, uri, image);
+		buoh_comic_cache_set_current (cache, uri, image);
 		return;
 	}
 	
-	while (CACHE_SIZE - cache->priv->size < size) {
-		GList     *item;
-		gchar     *item_uri;
-		gulong     item_size;
-		GdkPixbuf *pix;
+	while (CACHE_SIZE - cache->priv->size < image->size) {
+		GList *item;
+		gchar *item_uri;
 
-		if (!cache->priv->pixbuf_list)
+		if (!cache->priv->image_list)
 			break;
 		
-		item = g_list_last (cache->priv->pixbuf_list);
+		item = g_list_last (cache->priv->image_list);
 		item_uri = (gchar *) item->data;
 		
-		pix = GDK_PIXBUF (g_hash_table_lookup (cache->priv->pixbuf_hash,
-						       item_uri));
-		item_size = pixbuf_get_size (pix);
-
-		buoh_comic_cache_to_disk (cache, item_uri, pix);
+		img = (BuohComicImage *) g_hash_table_lookup (cache->priv->image_hash,
+							      item_uri);
+		buoh_comic_cache_to_disk (cache, item_uri, img);
 
 		buoh_debug ("CACHE: removing %s", item_uri);
-		cache->priv->pixbuf_list = g_list_delete_link (cache->priv->pixbuf_list,
-							       item);
-		g_hash_table_remove (cache->priv->pixbuf_hash, item_uri);
+		cache->priv->image_list = g_list_delete_link (cache->priv->image_list,
+							      item);
+		g_hash_table_remove (cache->priv->image_hash, item_uri);
 
-		cache->priv->size -= item_size;
+		cache->priv->size -= img->size;
 		buoh_debug ("CACHE: cache size %d\n", cache->priv->size);
 	}
 
 	key_uri = g_strdup (uri);
 
 	buoh_debug ("CACHE: caching (memory) %s", key_uri);
-	cache->priv->pixbuf_list = g_list_prepend (cache->priv->pixbuf_list, key_uri);
-	g_hash_table_insert (cache->priv->pixbuf_hash, key_uri, g_object_ref (pixbuf));
+	cache->priv->image_list = g_list_prepend (cache->priv->image_list, key_uri);
+	g_hash_table_insert (cache->priv->image_hash, key_uri, image);
+	buoh_comic_cache_set_current (cache, uri, image);
 	
-	cache->priv->size += size;
+	cache->priv->size += image->size;
 	buoh_debug ("CACHE: cache size %d\n", cache->priv->size);
 }
 
-GdkPixbuf *
-buoh_comic_cache_get_pixbuf (BuohComicCache *cache,
-			     const gchar    *uri)
-{
-	GdkPixbuf *pixbuf = NULL;
-	gchar     *path;
-	GList     *item;
+BuohComicImage *
+buoh_comic_cache_get_image (BuohComicCache *cache,
+			    const gchar    *uri)
+{
+	BuohComicImage *image;
+	gchar          *path;
+	GList          *item;
 	
 	g_return_val_if_fail (BUOH_IS_COMIC_CACHE (cache), NULL);
 	g_return_val_if_fail (uri != NULL, NULL);
 
-	pixbuf = GDK_PIXBUF (g_hash_table_lookup (cache->priv->pixbuf_hash, uri));
-	if (pixbuf) {
+	image = (BuohComicImage *) g_hash_table_lookup (cache->priv->image_hash, uri);
+	if (image) {
 		/* keep items ordered by access time */
-		item = g_list_find_custom (cache->priv->pixbuf_list,
+		item = g_list_find_custom (cache->priv->image_list,
 					   (gconstpointer) uri,
 					   (GCompareFunc) g_ascii_strcasecmp);
-		cache->priv->pixbuf_list = g_list_remove_link (cache->priv->pixbuf_list,
-							       item);
-		cache->priv->pixbuf_list = g_list_prepend (cache->priv->pixbuf_list, item->data);
+		cache->priv->image_list = g_list_remove_link (cache->priv->image_list,
+							      item);
+		cache->priv->image_list = g_list_prepend (cache->priv->image_list, item->data);
 		g_list_free (item);
 		
-		buoh_debug ("CACHE: return pixbuf from memory");
-		return pixbuf;
+		buoh_debug ("CACHE: return image from memory");
+		return image;
 	}
 
 	path = buoh_comic_cache_uri_to_filename (cache, uri);
-	pixbuf = gdk_pixbuf_new_from_file (path, NULL);
+	image = g_new0 (BuohComicImage, 1);
+	if (g_file_get_contents (path, (gchar **)&image->data, &image->size, NULL)) {
+		buoh_comic_cache_set_image (cache, uri, image);
+		buoh_debug ("CACHE: return image from disk");
+		g_free (path);
 
-	if (pixbuf) {
-		buoh_comic_cache_set_pixbuf (cache, uri, pixbuf);
-		g_object_unref (pixbuf);
-		buoh_debug ("CACHE: return pixbuf from disk");
-		return pixbuf;
+		return image;
 	}
+	g_free (path);
+	g_free (image);
 
-	buoh_debug ("CACHE: pixbuf is not cached");
+	buoh_debug ("CACHE: image is not cached");
 	
 	return NULL;
 }
+
+void
+buoh_comic_cache_set_pixbuf (BuohComicCache *cache,
+			     const gchar    *uri,
+			     GdkPixbuf      *pixbuf)
+{
+	g_return_if_fail (BUOH_IS_COMIC_CACHE (cache));
+	g_return_if_fail (uri != NULL);
+	g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
+
+	if (cache->priv->current_uri)
+		g_free (cache->priv->current_uri);
+	if (cache->priv->current_pixbuf)
+		g_object_unref (cache->priv->current_pixbuf);
+
+	cache->priv->current_uri = g_strdup (uri);
+	cache->priv->current_pixbuf = g_object_ref (pixbuf);
+}
+
+GdkPixbuf *
+buoh_comic_cache_get_pixbuf (BuohComicCache *cache,
+			     const gchar    *uri)
+{
+	BuohComicImage *image;
+	
+	g_return_val_if_fail (BUOH_IS_COMIC_CACHE (cache), NULL);
+	g_return_val_if_fail (uri != NULL, NULL);
+
+	if (cache->priv->current_uri &&
+	    g_ascii_strcasecmp (uri, cache->priv->current_uri) == 0) {
+		buoh_debug ("is the current pixbuf");
+		return cache->priv->current_pixbuf;
+	}
+
+	image = buoh_comic_cache_get_image (cache, uri);
+
+	if (image) {
+		buoh_comic_cache_set_current (cache, uri, image);
+		return cache->priv->current_pixbuf;
+	}
+
+	return NULL;
+}
===================================================================
RCS file: /cvsroot/buoh/buoh/src/buoh-comic-cache.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- /cvsroot/buoh/buoh/src/buoh-comic-cache.h	2005/11/15 13:02:12	1.1
+++ /cvsroot/buoh/buoh/src/buoh-comic-cache.h	2005/11/30 10:48:02	1.2
@@ -22,6 +22,8 @@
 #include <glib-object.h>
 #include <gdk-pixbuf/gdk-pixbuf.h>
 
+#include "buoh-comic.h"
+
 G_BEGIN_DECLS
 
 typedef struct _BuohComicCache        BuohComicCache;
@@ -47,6 +49,11 @@
 GType           buoh_comic_cache_get_type   (void) G_GNUC_CONST;
 BuohComicCache *buoh_comic_cache_new        (void);
 
+void            buoh_comic_cache_set_image  (BuohComicCache *cache,
+					     const gchar    *uri,
+					     BuohComicImage *image);
+BuohComicImage *buoh_comic_cache_get_image  (BuohComicCache *cache,
+					     const gchar    *uri);
 void            buoh_comic_cache_set_pixbuf (BuohComicCache *cache,
 					     const gchar    *uri,
 					     GdkPixbuf      *pixbuf);
===================================================================
RCS file: /cvsroot/buoh/buoh/src/buoh-comic-loader.c,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- /cvsroot/buoh/buoh/src/buoh-comic-loader.c	2005/11/16 20:29:28	1.11
+++ /cvsroot/buoh/buoh/src/buoh-comic-loader.c	2005/11/30 10:48:02	1.12
@@ -16,6 +16,7 @@
  *  Authors : Carlos García Campos <carlosgc gnome org>
  */
 
+#include <string.h>
 #include <gconf/gconf-client.h>
 
 #include "buoh.h"
@@ -27,7 +28,10 @@
 	SoupSession     *session;
 	SoupMessage     *msg;
 
+	GString         *data;
+	
 	gchar           *uri;
+	gdouble          scale;
 	GdkPixbufLoader *pixbuf_loader;
 };
 
@@ -42,6 +46,8 @@
 #define GCONF_HTTP_PROXY_AUTHENTICATION_USER "/system/http_proxy/authentication_user"
 #define GCONF_HTTP_PROXY_AUTHENTICATION_PASSWORD "/system/http_proxy/authentication_password"
 
+#define DATA_SIZE 61440 /* 60K */
+
 static GObjectClass *parent_class = NULL;
 
 static void     buoh_comic_loader_init          (BuohComicLoader      *loader);
@@ -109,7 +115,8 @@
 	SoupUri *proxy_uri = NULL;
 	
 	loader->priv = BUOH_COMIC_LOADER_GET_PRIVATE (loader);
-	
+
+	loader->priv->data = g_string_sized_new (DATA_SIZE);
 	loader->priv->uri = NULL;
 	loader->priv->pixbuf_loader = NULL;
 
@@ -201,6 +208,11 @@
 		loader->priv->gconf_client = NULL;
 	}
 
+	if (loader->priv->data) {
+		g_string_free (loader->priv->data, TRUE);
+		loader->priv->data = NULL;
+	}
+	
 	if (loader->priv->uri) {
 		g_free (loader->priv->uri);
 		loader->priv->uri = NULL;
@@ -281,6 +293,18 @@
 }
 
 static void
+buoh_comic_loader_set_size (GdkPixbufLoader *pixbuf_loader,
+			    gint width, gint height, gpointer gdata)
+{
+	BuohComicLoader *loader = BUOH_COMIC_LOADER (gdata);
+
+	buoh_debug ("loader size-prepared");
+	gdk_pixbuf_loader_set_size (pixbuf_loader,
+				    width * loader->priv->scale,
+				    height * loader->priv->scale);
+}
+
+static void
 buoh_comic_loader_update (GdkPixbufLoader *pixbuf_loader, gint x, gint y,
 			  gint width, gint height, gpointer gdata)
 {
@@ -302,6 +326,14 @@
 
 	if (SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
 		loader->priv->pixbuf_loader = gdk_pixbuf_loader_new ();
+		
+		if (loader->priv->scale != 1.0) {
+			g_signal_connect (G_OBJECT (loader->priv->pixbuf_loader),
+					  "size_prepared",
+					  G_CALLBACK (buoh_comic_loader_set_size),
+					  (gpointer) loader);
+		}
+		
 		g_signal_connect (G_OBJECT (loader->priv->pixbuf_loader),
 				  "area-updated",
 				  G_CALLBACK (buoh_comic_loader_update),
@@ -333,6 +365,9 @@
 	}
 
 	if (SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
+		loader->priv->data = g_string_append_len (loader->priv->data,
+							  msg->response.body,
+							  msg->response.length);
 		gdk_pixbuf_loader_write (loader->priv->pixbuf_loader,
 					 (guchar *) msg->response.body,
 					 msg->response.length, NULL);
@@ -367,8 +402,14 @@
 			g_mutex_lock (loader->pixbuf_mutex);
 			pixbuf = gdk_pixbuf_loader_get_pixbuf (loader->priv->pixbuf_loader);
 			loader->pixbuf = GDK_IS_PIXBUF (pixbuf) ? g_object_ref (pixbuf) : NULL;
+			loader->image = g_new0 (BuohComicImage, 1);
+			loader->image->size = loader->priv->data->len;
+			loader->image->data = (guchar *) g_memdup (loader->priv->data->str,
+								   loader->priv->data->len);
 			g_mutex_unlock (loader->pixbuf_mutex);
 
+			loader->priv->data->len = 0;
+			
 			g_object_unref (loader->priv->pixbuf_loader);
 			loader->priv->pixbuf_loader = NULL;
 		}
@@ -388,6 +429,8 @@
 			loader->pixbuf = NULL;
 			g_mutex_unlock (loader->pixbuf_mutex);
 
+			loader->priv->data->len = 0;
+
 			g_object_unref (loader->priv->pixbuf_loader);
 			loader->priv->pixbuf_loader = NULL;
 		}
@@ -398,10 +441,10 @@
 			gdk_pixbuf_loader_close (loader->priv->pixbuf_loader, NULL);
 
 			g_mutex_lock (loader->pixbuf_mutex);
-			if (GDK_IS_PIXBUF (loader->pixbuf))
-				g_object_unref (loader->pixbuf);
 			loader->pixbuf = NULL;
 			g_mutex_unlock (loader->pixbuf_mutex);
+
+			loader->priv->data->len = 0;
 			
 			g_object_unref (loader->priv->pixbuf_loader);
 			loader->priv->pixbuf_loader = NULL;
@@ -464,7 +507,7 @@
 }
 
 void
-buoh_comic_loader_run (BuohComicLoader *loader, const gchar *uri)
+buoh_comic_loader_run (BuohComicLoader *loader, const gchar *uri, gdouble scale)
 {
 	g_return_if_fail (BUOH_IS_COMIC_LOADER (loader));
 	g_return_if_fail (uri != NULL);
@@ -475,9 +518,12 @@
 
 	g_mutex_lock (loader->pixbuf_mutex);
 	loader->pixbuf = NULL;
+	loader->image = NULL;
 	g_mutex_unlock (loader->pixbuf_mutex);
 
+	loader->priv->data->len = 0;
 	loader->priv->uri = g_strdup (uri);
+	loader->priv->scale = scale;
 
 	g_mutex_lock (loader->thread_mutex);
 
@@ -505,3 +551,11 @@
 	soup_message_set_status (loader->priv->msg, SOUP_STATUS_CANCELLED);
 	soup_session_cancel_message (loader->priv->session, loader->priv->msg);
 }
+
+void
+buoh_comic_loader_wait (BuohComicLoader *loader)
+{
+	g_return_if_fail (BUOH_IS_COMIC_LOADER (loader));
+
+	g_thread_join (loader->thread);
+}
===================================================================
RCS file: /cvsroot/buoh/buoh/src/buoh-comic-loader.h,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- /cvsroot/buoh/buoh/src/buoh-comic-loader.h	2005/11/16 20:29:28	1.6
+++ /cvsroot/buoh/buoh/src/buoh-comic-loader.h	2005/11/30 10:48:02	1.7
@@ -52,14 +52,15 @@
 struct _BuohComicLoader {
 	GObject    parent;
 
-	GMutex    *thread_mutex;
-	GMutex    *pixbuf_mutex;
-	GMutex    *status_mutex;
+	GMutex         *thread_mutex;
+	GMutex         *pixbuf_mutex;
+	GMutex         *status_mutex;
 	
-	GThread   *thread;
-	GdkPixbuf *pixbuf;
-	guint      status;
-	GError    *error;
+	GThread        *thread;
+	GdkPixbuf      *pixbuf;
+	BuohComicImage *image;
+	guint           status;
+	GError         *error;
 
 	BuohComicLoaderPrivate *priv;
 };
@@ -73,8 +74,10 @@
 BuohComicLoader *buoh_comic_loader_new         (void);
 
 void             buoh_comic_loader_run         (BuohComicLoader *loader,
-						const gchar     *uri);
+						const gchar     *uri,
+						gdouble          scale);
 void             buoh_comic_loader_stop        (BuohComicLoader *loader);
+void             buoh_comic_loader_wait        (BuohComicLoader *loader);
 
 G_END_DECLS
 
===================================================================
RCS file: /cvsroot/buoh/buoh/src/buoh-comic.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- /cvsroot/buoh/buoh/src/buoh-comic.c	2005/11/16 20:29:28	1.10
+++ /cvsroot/buoh/buoh/src/buoh-comic.c	2005/11/30 10:48:02	1.11
@@ -29,6 +29,7 @@
 	PROP_ID,
 	PROP_URI,
 	PROP_PIXBUF,
+	PROP_IMAGE,
 	PROP_DATE
 };
 
@@ -87,9 +88,9 @@
 {
 	buoh_comic->priv = BUOH_COMIC_GET_PRIVATE (buoh_comic);
 
-	buoh_comic->priv->id     = NULL;
-	buoh_comic->priv->uri    = NULL;
-	buoh_comic->priv->date   = NULL;
+	buoh_comic->priv->id = NULL;
+	buoh_comic->priv->uri = NULL;
+	buoh_comic->priv->date = NULL;
 
 	buoh_comic->priv->cache = buoh_comic_cache_new ();
 }
@@ -127,6 +128,12 @@
 							       "Pixbuf of the comic",
 							       G_PARAM_READWRITE));
 	g_object_class_install_property (object_class,
+					 PROP_IMAGE,
+					 g_param_spec_pointer ("image",
+							       "Image",
+							       "Compressed image of the comic",
+							       G_PARAM_READWRITE));
+	g_object_class_install_property (object_class,
 					 PROP_DATE,
 					 g_param_spec_pointer ("date",
 							       "Date",
@@ -223,6 +230,15 @@
 					     comic->priv->uri, pixbuf);
 	}
 		break;
+	case PROP_IMAGE: {
+		BuohComicImage *image;
+
+		image = (BuohComicImage *) g_value_get_pointer (value);
+		buoh_comic_cache_set_image (comic->priv->cache,
+					    comic->priv->uri, image);
+	}
+		
+		break;
 	case PROP_DATE:
 		if (comic->priv->date) {
 			g_date_free (comic->priv->date);
@@ -263,6 +279,14 @@
 		g_value_set_pointer (value, pixbuf);
 	}
 		break;
+	case PROP_IMAGE: {
+		BuohComicImage *image;
+
+		image = buoh_comic_cache_get_image (comic->priv->cache,
+						    comic->priv->uri);
+		g_value_set_pointer (value, image);
+	}
+		break;
 	case PROP_DATE:
 		g_value_set_pointer (value, comic->priv->date);
 		
@@ -291,6 +315,15 @@
 }
 
 void
+buoh_comic_set_image (BuohComic *comic, BuohComicImage *image)
+{
+	g_return_if_fail (BUOH_IS_COMIC (comic));
+	g_return_if_fail (image != NULL);
+
+	g_object_set (G_OBJECT (comic), "image", image, NULL);
+}
+
+void
 buoh_comic_set_date (BuohComic *comic, GDate *date)
 {
 	g_return_if_fail (BUOH_IS_COMIC (comic));
@@ -348,6 +381,18 @@
 	return pixbuf;
 }
 
+BuohComicImage *
+buoh_comic_get_image (BuohComic *comic)
+{
+	BuohComicImage *image = NULL;
+
+	g_return_val_if_fail (BUOH_IS_COMIC (comic), NULL);
+
+	g_object_get (G_OBJECT (comic), "image", &image, NULL);
+
+	return image;
+}
+
 GDate *
 buoh_comic_get_date (BuohComic *comic)
 {
@@ -403,3 +448,12 @@
 	
 	return filename;
 }
+
+void
+buoh_comic_image_free (BuohComicImage *image)
+{
+	if (image) {
+		g_free (image->data);
+		g_free (image);
+	}
+}
===================================================================
RCS file: /cvsroot/buoh/buoh/src/buoh-comic.h,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- /cvsroot/buoh/buoh/src/buoh-comic.h	2005/11/16 20:29:28	1.6
+++ /cvsroot/buoh/buoh/src/buoh-comic.h	2005/11/30 10:48:02	1.7
@@ -37,6 +37,11 @@
 #define BUOH_COMIC_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), BUOH_TYPE_COMIC, BuohComicClass))
 
 
+typedef struct {
+	guchar *data;
+	gsize   size;
+} BuohComicImage;
+
 struct _BuohComic {
 	GObject           parent;
 
@@ -49,27 +54,32 @@
 
 GType      buoh_comic_get_type             (void) G_GNUC_CONST; 
 BuohComic *buoh_comic_new                  (void);
-BuohComic *buoh_comic_new_with_info        (const gchar *id,
-					    const gchar *uri,
-					    const GDate *date);
-
-void       buoh_comic_set_id               (BuohComic   *comic,
-					    const gchar *id);
-void       buoh_comic_go_next              (BuohComic   *comic);
-void       buoh_comic_go_previous          (BuohComic   *comic);
-void       buoh_comic_set_pixbuf           (BuohComic   *comic,
-					    GdkPixbuf   *pixbuf);
-void	   buoh_comic_set_date             (BuohComic   *comic,
-					    GDate       *date);
-void       buoh_comic_set_pixbuf_from_file (BuohComic   *comic,
-					    const gchar *filename);
-
-gchar     *buoh_comic_get_uri              (BuohComic   *comic);
-gchar     *buoh_comic_get_id               (BuohComic   *comic);
-GdkPixbuf *buoh_comic_get_pixbuf           (BuohComic   *comic);
-GDate     *buoh_comic_get_date             (BuohComic   *comic);
-GdkPixbuf *buoh_comic_get_thumbnail        (BuohComic   *comic);
-gchar     *buoh_comic_get_filename         (BuohComic   *comic);
+BuohComic *buoh_comic_new_with_info        (const gchar    *id,
+					    const gchar    *uri,
+					    const GDate    *date);
+
+void       buoh_comic_set_id               (BuohComic      *comic,
+					    const gchar    *id);
+void       buoh_comic_go_next              (BuohComic      *comic);
+void       buoh_comic_go_previous          (BuohComic      *comic);
+void       buoh_comic_set_pixbuf           (BuohComic      *comic,
+					    GdkPixbuf      *pixbuf);
+void       buoh_comic_set_image            (BuohComic      *comic,
+					    BuohComicImage *image);
+void	   buoh_comic_set_date             (BuohComic      *comic,
+					    GDate          *date);
+void       buoh_comic_set_pixbuf_from_file (BuohComic      *comic,
+					    const gchar    *filename);
+
+gchar     *buoh_comic_get_uri              (BuohComic      *comic);
+gchar     *buoh_comic_get_id               (BuohComic      *comic);
+GdkPixbuf *buoh_comic_get_pixbuf           (BuohComic      *comic);
+BuohComicImage *buoh_comic_get_image       (BuohComic      *comic);
+GDate     *buoh_comic_get_date             (BuohComic      *comic);
+GdkPixbuf *buoh_comic_get_thumbnail        (BuohComic      *comic);
+gchar     *buoh_comic_get_filename         (BuohComic      *comic);
+
+void       buoh_comic_image_free           (BuohComicImage *image);
 
 G_END_DECLS
 
===================================================================
RCS file: /cvsroot/buoh/buoh/src/buoh-view-comic.c,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- /cvsroot/buoh/buoh/src/buoh-view-comic.c	2005/11/16 20:40:22	1.14
+++ /cvsroot/buoh/buoh/src/buoh-view-comic.c	2005/11/30 10:48:02	1.15
@@ -39,6 +39,7 @@
 struct _BuohViewComicPrivate {
 	BuohView        *view;
 	GtkWidget       *image;
+	GdkPixbuf       *current;
 
 	BuohComic       *comic;
 	gdouble          scale;
@@ -454,7 +455,7 @@
 						      gdk_pixbuf_get_width (pixbuf) * c_view->priv->scale,
 						      gdk_pixbuf_get_height (pixbuf) * c_view->priv->scale,
 						      GDK_INTERP_BILINEAR);
-
+		
 		gtk_image_set_from_pixbuf (GTK_IMAGE (c_view->priv->image), new_pixbuf);
 		g_object_unref (new_pixbuf);
 	} else {
@@ -488,10 +489,11 @@
 
 			if (new_height != height) {
 				g_mutex_lock (c_view->priv->comic_loader->pixbuf_mutex);
-				buoh_view_comic_set_image_from_pixbuf (
-					c_view,
-					c_view->priv->comic_loader->pixbuf);
+				gtk_image_set_from_pixbuf (GTK_IMAGE (c_view->priv->image),
+							   c_view->priv->comic_loader->pixbuf);
 				g_mutex_unlock (c_view->priv->comic_loader->pixbuf_mutex);
+				
+				gtk_widget_show (c_view->priv->image);
 				height = new_height;
 			}
 		}
@@ -500,12 +502,23 @@
 	case LOADER_STATE_FINISHED:
 		if (GDK_IS_PIXBUF (c_view->priv->comic_loader->pixbuf)) {
 			g_mutex_lock (c_view->priv->comic_loader->pixbuf_mutex);
-			buoh_view_comic_set_image_from_pixbuf (c_view,
-							       c_view->priv->comic_loader->pixbuf);
-			buoh_comic_set_pixbuf (c_view->priv->comic,
-					       c_view->priv->comic_loader->pixbuf);
+			gtk_image_set_from_pixbuf (GTK_IMAGE (c_view->priv->image),
+						   c_view->priv->comic_loader->pixbuf);
+			if (c_view->priv->scale == 1.0) {
+				/* We have both the compressed and uncompressed image.
+				 * Setting the pixbuf avoids the cache uncompressing
+				 * the image again and having a new pixbuf instead of a
+				 * reference.
+				 */
+				buoh_comic_set_pixbuf (c_view->priv->comic,
+						       c_view->priv->comic_loader->pixbuf);
+			}
+			buoh_comic_set_image (c_view->priv->comic,
+					      c_view->priv->comic_loader->image);
 			g_object_unref (c_view->priv->comic_loader->pixbuf);
 			g_mutex_unlock (c_view->priv->comic_loader->pixbuf_mutex);
+
+			gtk_widget_show (c_view->priv->image);
 			
 			g_object_set (G_OBJECT (c_view->priv->view),
 				      "status", STATE_COMIC_LOADED,
@@ -574,7 +587,7 @@
 		buoh_debug ("Load already running");
 		buoh_comic_loader_stop (c_view->priv->comic_loader);
 		buoh_debug ("waiting thread");
-		g_thread_join (c_view->priv->comic_loader->thread);
+		buoh_comic_loader_wait (c_view->priv->comic_loader);
 		buoh_debug ("died");
 	}
 
@@ -589,7 +602,9 @@
 			      "status", STATE_COMIC_LOADING,
 			      NULL);
 		uri = buoh_comic_get_uri (c_view->priv->comic);
-		buoh_comic_loader_run (c_view->priv->comic_loader, uri);
+		buoh_comic_loader_run (c_view->priv->comic_loader,
+				       uri,
+				       c_view->priv->scale);
 		g_free (uri);
 
 		if (c_view->priv->load_monitor > 0)



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