1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
4 * (GLABELS) Label and Business Card Creation program for GNOME
6 * mini-preview-pixbuf-cache.c: GLabels mini-preview pixbuf cache module
8 * Copyright (C) 2007 Jim Evins <evins@snaught.com>.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "mini-preview-pixbuf-cache.h"
27 #include "mini-preview-pixbuf.h"
29 #include "libglabels/db.h"
31 #include <glib/gmem.h>
32 #include <glib/ghash.h>
36 /*========================================================*/
38 /*========================================================*/
40 /*========================================================*/
41 /* Private globals. */
42 /*========================================================*/
44 static GHashTable *mini_preview_pixbuf_cache = NULL;
46 /*========================================================*/
47 /* Private function prototypes. */
48 /*========================================================*/
51 /*****************************************************************************/
52 /* Create a new hash table to keep track of cached mini preview pixbufs. */
53 /*****************************************************************************/
55 gl_mini_preview_pixbuf_cache_init (void)
59 lglTemplate *template;
61 gl_debug (DEBUG_PIXBUF_CACHE, "START");
63 mini_preview_pixbuf_cache = g_hash_table_new (g_str_hash, g_str_equal);
65 names = lgl_db_get_template_name_list_unique (NULL, NULL, NULL);
66 for ( p=names; p != NULL; p=p->next )
68 gl_debug (DEBUG_PIXBUF_CACHE, "name = \"%s\"", p->data);
70 template = lgl_db_lookup_template_from_name (p->data);
71 gl_mini_preview_pixbuf_cache_add_by_template (template);
72 lgl_template_free (template);
74 lgl_db_free_template_name_list (names);
76 gl_debug (DEBUG_PIXBUF_CACHE, "END pixbuf_cache=%p", mini_preview_pixbuf_cache);
79 /*****************************************************************************/
80 /* Add pixbuf to cache by template. */
81 /*****************************************************************************/
83 gl_mini_preview_pixbuf_cache_add_by_template (lglTemplate *template)
87 lglTemplateAlias *alias;
90 gl_debug (DEBUG_PIXBUF_CACHE, "START");
92 pixbuf = gl_mini_preview_pixbuf_new (template, 72, 72);
94 for ( p=template->aliases; p != NULL; p=p->next )
96 alias = (lglTemplateAlias *)p->data;
98 name = g_strdup_printf ("%s %s", alias->brand, alias->part);
99 g_hash_table_insert (mini_preview_pixbuf_cache, name, g_object_ref (pixbuf));
102 g_object_unref (pixbuf);
104 gl_debug (DEBUG_PIXBUF_CACHE, "END");
107 /*****************************************************************************/
108 /* Add pixbuf to cache by name. */
109 /*****************************************************************************/
111 gl_mini_preview_pixbuf_cache_add_by_name (gchar *name)
113 lglTemplate *template;
116 gl_debug (DEBUG_PIXBUF_CACHE, "START");
118 template = lgl_db_lookup_template_from_name (name);
119 pixbuf = gl_mini_preview_pixbuf_new (template, 72, 72);
120 lgl_template_free (template);
122 g_hash_table_insert (mini_preview_pixbuf_cache, g_strdup (name), pixbuf);
124 gl_debug (DEBUG_PIXBUF_CACHE, "END");
127 /*****************************************************************************/
129 /*****************************************************************************/
131 gl_mini_preview_pixbuf_cache_get_pixbuf (gchar *name)
135 gl_debug (DEBUG_PIXBUF_CACHE, "START pixbuf_cache=%p", mini_preview_pixbuf_cache);
137 pixbuf = g_hash_table_lookup (mini_preview_pixbuf_cache, name);
141 gl_mini_preview_pixbuf_cache_add_by_name (name);
142 pixbuf = g_hash_table_lookup (mini_preview_pixbuf_cache, name);
145 gl_debug (DEBUG_PIXBUF_CACHE, "END");
147 return g_object_ref (pixbuf);