]> git.sur5r.net Git - glabels/blobdiff - glabels2/src/pixbuf-cache.c
2009-09-22 Jim Evins <evins@snaught.com>
[glabels] / glabels2 / src / pixbuf-cache.c
index cc2f08e4d27aaa8f80da838d3a479d38ddee3cd6..77b2fc59f9cc95c8be5d8e05851854cfa2872624 100644 (file)
@@ -1,30 +1,30 @@
 /*
- *  (GLABELS) Label and Business Card Creation program for GNOME
+ *  pixbuf-cache.c
+ *  Copyright (C) 2003-2009  Jim Evins <evins@snaught.com>.
  *
- *  pixbuf-cache.c:  GLabels pixbuf cache module
+ *  This file is part of gLabels.
  *
- *  Copyright (C) 2003  Jim Evins <evins@snaught.com>.
- *
- *  This program is free software; you can redistribute it and/or modify
+ *  gLabels 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
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
- *  This program is distributed in the hope that it will be useful,
+ *  gLabels 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, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+ *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
  */
+
 #include <config.h>
 
 #include "pixbuf-cache.h"
 
 #include "debug.h"
 
+
 /*========================================================*/
 /* Private types.                                         */
 /*========================================================*/
@@ -35,10 +35,12 @@ typedef struct {
        GdkPixbuf *pixbuf;
 } CacheRecord;
 
+
 /*========================================================*/
 /* Private globals.                                       */
 /*========================================================*/
 
+
 /*========================================================*/
 /* Private function prototypes.                           */
 /*========================================================*/
@@ -49,7 +51,7 @@ static void  add_name_to_list (gpointer key,
                               gpointer val,
                               gpointer user_data);
 
-\f
+
 /*---------------------------------------------------------------------------*/
 /* PRIVATE.  Destroy cache record.                                           */
 /*---------------------------------------------------------------------------*/
@@ -65,6 +67,7 @@ record_destroy (gpointer val)
        g_free (record);
 }
 
+
 /*****************************************************************************/
 /* Create a new hash table to keep track of cached pixbufs.                  */
 /*****************************************************************************/
@@ -85,6 +88,7 @@ gl_pixbuf_cache_new (void)
        return pixbuf_cache;
 }
 
+
 /*****************************************************************************/
 /* Free up previously allocated hash table and its contents.                 */
 /*****************************************************************************/
@@ -98,8 +102,9 @@ gl_pixbuf_cache_free (GHashTable *pixbuf_cache)
        gl_debug (DEBUG_PIXBUF_CACHE, "END");
 }
 
+
 /*****************************************************************************/
-/* Add pixbuf to cache explicitly.                                           */
+/* Add pixbuf to cache explicitly (not a reference).                         */
 /*****************************************************************************/
 void
 gl_pixbuf_cache_add_pixbuf (GHashTable *pixbuf_cache,
@@ -107,7 +112,6 @@ gl_pixbuf_cache_add_pixbuf (GHashTable *pixbuf_cache,
                            GdkPixbuf  *pixbuf)
 {
        CacheRecord *test_record, *record;
-       gchar       *key;
 
        gl_debug (DEBUG_PIXBUF_CACHE, "START");
 
@@ -120,14 +124,15 @@ gl_pixbuf_cache_add_pixbuf (GHashTable *pixbuf_cache,
 
        record = g_new0 (CacheRecord, 1);
        record->key        = g_strdup (name);
-       record->references = 0;
-       record->pixbuf     = pixbuf;
+       record->references = 0; /* Nobody has referenced it yet. */
+       record->pixbuf     = g_object_ref (G_OBJECT (pixbuf));
 
        g_hash_table_insert (pixbuf_cache, record->key, record);
 
        gl_debug (DEBUG_PIXBUF_CACHE, "END");
 }
 
+
 /*****************************************************************************/
 /* Get pixbuf.  If not in cache, read it and add to cache.                   */
 /*****************************************************************************/
@@ -143,8 +148,9 @@ gl_pixbuf_cache_get_pixbuf (GHashTable *pixbuf_cache,
        record = g_hash_table_lookup (pixbuf_cache, name);
 
        if (record != NULL) {
-               gl_debug (DEBUG_PIXBUF_CACHE, "END cached");
                record->references++;
+               gl_debug (DEBUG_PIXBUF_CACHE, "references=%d", record->references);
+               gl_debug (DEBUG_PIXBUF_CACHE, "END cached");
                return record->pixbuf;
        }
 
@@ -164,6 +170,7 @@ gl_pixbuf_cache_get_pixbuf (GHashTable *pixbuf_cache,
        return pixbuf;
 }
 
+
 /*****************************************************************************/
 /* Remove pixbuf, but only if no references left.                            */
 /*****************************************************************************/
@@ -173,6 +180,8 @@ gl_pixbuf_cache_remove_pixbuf (GHashTable *pixbuf_cache,
 {
        CacheRecord *record;
 
+       if (name == NULL) return;
+
        gl_debug (DEBUG_PIXBUF_CACHE, "START");
 
        record = g_hash_table_lookup (pixbuf_cache, name);
@@ -190,6 +199,7 @@ gl_pixbuf_cache_remove_pixbuf (GHashTable *pixbuf_cache,
        gl_debug (DEBUG_PIXBUF_CACHE, "END");
 }
 
+
 /*---------------------------------------------------------------------------*/
 /* PRIVATE.  Add a name to a GList while iterating over cache.               */
 /*---------------------------------------------------------------------------*/
@@ -210,6 +220,7 @@ add_name_to_list (gpointer key,
        gl_debug (DEBUG_PIXBUF_CACHE, "END");
 }
 
+
 /*****************************************************************************/
 /* Return a list of names for all pixbufs in the cache.                      */
 /*****************************************************************************/
@@ -227,6 +238,7 @@ gl_pixbuf_cache_get_name_list (GHashTable *pixbuf_cache)
        return name_list;
 }
 
+
 /*****************************************************************************/
 /* Free up a list of pixbuf names.                                           */
 /*****************************************************************************/
@@ -248,3 +260,13 @@ gl_pixbuf_cache_free_name_list (GList *name_list)
 }
 
 
+
+
+/*
+ * Local Variables:       -- emacs
+ * mode: C                -- emacs
+ * c-basic-offset: 8      -- emacs
+ * tab-width: 8           -- emacs
+ * indent-tabs-mode: nil  -- emacs
+ * End:                   -- emacs
+ */