2 * mini-preview-pixbuf-cache.c
3 * Copyright (C) 2007-2009 Jim Evins <evins@snaught.com>.
5 * This file is part of gLabels.
7 * gLabels is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * gLabels is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with gLabels. If not, see <http://www.gnu.org/licenses/>.
23 #include "mini-preview-pixbuf-cache.h"
27 #include "libglabels/libglabels.h"
28 #include "mini-preview-pixbuf.h"
32 /*========================================================*/
34 /*========================================================*/
36 /*========================================================*/
37 /* Private globals. */
38 /*========================================================*/
40 static GHashTable *mini_preview_pixbuf_cache = NULL;
42 /*========================================================*/
43 /* Private function prototypes. */
44 /*========================================================*/
47 /*****************************************************************************/
48 /* Create a new hash table to keep track of cached mini preview pixbufs. */
49 /*****************************************************************************/
51 gl_mini_preview_pixbuf_cache_init (void)
55 lglTemplate *template;
57 gl_debug (DEBUG_PIXBUF_CACHE, "START");
59 mini_preview_pixbuf_cache = g_hash_table_new (g_str_hash, g_str_equal);
61 names = lgl_db_get_template_name_list_unique (NULL, NULL, NULL);
62 for ( p=names; p != NULL; p=p->next )
64 gl_debug (DEBUG_PIXBUF_CACHE, "name = \"%s\"", p->data);
66 template = lgl_db_lookup_template_from_name (p->data);
67 gl_mini_preview_pixbuf_cache_add_by_template (template);
68 lgl_template_free (template);
70 lgl_db_free_template_name_list (names);
72 gl_debug (DEBUG_PIXBUF_CACHE, "END pixbuf_cache=%p", mini_preview_pixbuf_cache);
75 /*****************************************************************************/
76 /* Add pixbuf to cache by template. */
77 /*****************************************************************************/
79 gl_mini_preview_pixbuf_cache_add_by_template (lglTemplate *template)
83 lglTemplateAlias *alias;
86 gl_debug (DEBUG_PIXBUF_CACHE, "START");
88 pixbuf = gl_mini_preview_pixbuf_new (template, 72, 72);
90 for ( p=template->aliases; p != NULL; p=p->next )
92 alias = (lglTemplateAlias *)p->data;
94 name = g_strdup_printf ("%s %s", alias->brand, alias->part);
95 g_hash_table_insert (mini_preview_pixbuf_cache, name, g_object_ref (pixbuf));
98 g_object_unref (pixbuf);
100 gl_debug (DEBUG_PIXBUF_CACHE, "END");
103 /*****************************************************************************/
104 /* Add pixbuf to cache by name. */
105 /*****************************************************************************/
107 gl_mini_preview_pixbuf_cache_add_by_name (gchar *name)
109 lglTemplate *template;
112 gl_debug (DEBUG_PIXBUF_CACHE, "START");
114 template = lgl_db_lookup_template_from_name (name);
115 pixbuf = gl_mini_preview_pixbuf_new (template, 72, 72);
116 lgl_template_free (template);
118 g_hash_table_insert (mini_preview_pixbuf_cache, g_strdup (name), pixbuf);
120 gl_debug (DEBUG_PIXBUF_CACHE, "END");
123 /*****************************************************************************/
125 /*****************************************************************************/
127 gl_mini_preview_pixbuf_cache_get_pixbuf (gchar *name)
131 gl_debug (DEBUG_PIXBUF_CACHE, "START pixbuf_cache=%p", mini_preview_pixbuf_cache);
133 pixbuf = g_hash_table_lookup (mini_preview_pixbuf_cache, name);
137 gl_mini_preview_pixbuf_cache_add_by_name (name);
138 pixbuf = g_hash_table_lookup (mini_preview_pixbuf_cache, name);
141 gl_debug (DEBUG_PIXBUF_CACHE, "END");
143 return g_object_ref (pixbuf);
149 * Local Variables: -- emacs
151 * c-basic-offset: 8 -- emacs
152 * tab-width: 8 -- emacs
153 * indent-tabs-mode: nil -- emacs