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.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_full (g_str_hash, g_str_equal, g_free, g_object_unref);
61 names = lgl_db_get_template_name_list_all (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);
76 /*****************************************************************************/
77 /* Add pixbuf to cache by template. */
78 /*****************************************************************************/
80 gl_mini_preview_pixbuf_cache_add_by_template (lglTemplate *template)
85 gl_debug (DEBUG_PIXBUF_CACHE, "START");
87 pixbuf = gl_mini_preview_pixbuf_new (template, 72, 72);
89 name = g_strdup_printf ("%s %s", template->brand, template->part);
90 g_hash_table_insert (mini_preview_pixbuf_cache, name, g_object_ref (pixbuf));
92 g_object_unref (pixbuf);
94 gl_debug (DEBUG_PIXBUF_CACHE, "END");
98 /*****************************************************************************/
99 /* Add pixbuf to cache by name. */
100 /*****************************************************************************/
102 gl_mini_preview_pixbuf_cache_add_by_name (gchar *name)
104 lglTemplate *template;
107 gl_debug (DEBUG_PIXBUF_CACHE, "START");
109 template = lgl_db_lookup_template_from_name (name);
110 pixbuf = gl_mini_preview_pixbuf_new (template, 72, 72);
111 lgl_template_free (template);
113 g_hash_table_insert (mini_preview_pixbuf_cache, g_strdup (name), pixbuf);
115 gl_debug (DEBUG_PIXBUF_CACHE, "END");
119 /*****************************************************************************/
120 /* Delete pixbuf from cache by name. */
121 /*****************************************************************************/
123 gl_mini_preview_pixbuf_cache_delete_by_name (gchar *name)
125 gl_debug (DEBUG_PIXBUF_CACHE, "START");
127 g_hash_table_remove (mini_preview_pixbuf_cache, name);
129 gl_debug (DEBUG_PIXBUF_CACHE, "END");
133 /*****************************************************************************/
135 /*****************************************************************************/
137 gl_mini_preview_pixbuf_cache_get_pixbuf (gchar *name)
141 gl_debug (DEBUG_PIXBUF_CACHE, "START pixbuf_cache=%p", mini_preview_pixbuf_cache);
143 pixbuf = g_hash_table_lookup (mini_preview_pixbuf_cache, name);
147 gl_mini_preview_pixbuf_cache_add_by_name (name);
148 pixbuf = g_hash_table_lookup (mini_preview_pixbuf_cache, name);
151 gl_debug (DEBUG_PIXBUF_CACHE, "END");
153 return g_object_ref (pixbuf);
159 * Local Variables: -- emacs
161 * c-basic-offset: 8 -- emacs
162 * tab-width: 8 -- emacs
163 * indent-tabs-mode: nil -- emacs