]> git.sur5r.net Git - glabels/blob - src/mini-preview-pixbuf-cache.c
Provides barcode even if invalid for correct sizing
[glabels] / src / mini-preview-pixbuf-cache.c
1 /*
2  *  mini-preview-pixbuf-cache.c
3  *  Copyright (C) 2007-2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #include <config.h>
22
23 #include "mini-preview-pixbuf-cache.h"
24
25 #include <glib.h>
26
27 #include <libglabels.h>
28 #include "mini-preview-pixbuf.h"
29
30 #include "debug.h"
31
32 /*========================================================*/
33 /* Private types.                                         */
34 /*========================================================*/
35
36 /*========================================================*/
37 /* Private globals.                                       */
38 /*========================================================*/
39
40 static GHashTable *mini_preview_pixbuf_cache = NULL;
41
42 /*========================================================*/
43 /* Private function prototypes.                           */
44 /*========================================================*/
45
46
47 /*****************************************************************************/
48 /* Create a new hash table to keep track of cached mini preview pixbufs.     */
49 /*****************************************************************************/
50 void
51 gl_mini_preview_pixbuf_cache_init (void)
52 {
53         GList       *names = NULL;
54         GList       *p;
55         lglTemplate *template;
56
57         gl_debug (DEBUG_PIXBUF_CACHE, "START");
58
59         mini_preview_pixbuf_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
60
61         names = lgl_db_get_template_name_list_unique (NULL, NULL, NULL);
62         for ( p=names; p != NULL; p=p->next )
63         {
64                 gl_debug (DEBUG_PIXBUF_CACHE, "name = \"%s\"", p->data);
65
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);
69         }
70         lgl_db_free_template_name_list (names);
71
72         gl_debug (DEBUG_PIXBUF_CACHE, "END pixbuf_cache=%p", mini_preview_pixbuf_cache);
73 }
74
75
76 /*****************************************************************************/
77 /* Add pixbuf to cache by template.                                          */
78 /*****************************************************************************/
79 void
80 gl_mini_preview_pixbuf_cache_add_by_template (lglTemplate *template)
81 {
82         GdkPixbuf        *pixbuf;
83         GList            *p;
84         lglTemplateAlias *alias;
85         gchar            *name;
86
87         gl_debug (DEBUG_PIXBUF_CACHE, "START");
88
89         pixbuf = gl_mini_preview_pixbuf_new (template, 72, 72);
90
91         for ( p=template->aliases; p != NULL; p=p->next )
92         {
93                 alias = (lglTemplateAlias *)p->data;
94
95                 name = g_strdup_printf ("%s %s", alias->brand, alias->part);
96                 g_hash_table_insert (mini_preview_pixbuf_cache, name, g_object_ref (pixbuf));
97         }
98
99         g_object_unref (pixbuf);
100
101         gl_debug (DEBUG_PIXBUF_CACHE, "END");
102 }
103
104
105 /*****************************************************************************/
106 /* Add pixbuf to cache by name.                                              */
107 /*****************************************************************************/
108 void
109 gl_mini_preview_pixbuf_cache_add_by_name (gchar      *name)
110 {
111         lglTemplate *template;
112         GdkPixbuf   *pixbuf;
113
114         gl_debug (DEBUG_PIXBUF_CACHE, "START");
115
116         template = lgl_db_lookup_template_from_name (name);
117         pixbuf = gl_mini_preview_pixbuf_new (template, 72, 72);
118         lgl_template_free (template);
119
120         g_hash_table_insert (mini_preview_pixbuf_cache, g_strdup (name), pixbuf);
121
122         gl_debug (DEBUG_PIXBUF_CACHE, "END");
123 }
124
125
126 /*****************************************************************************/
127 /* Delete pixbuf from cache by name.                                         */
128 /*****************************************************************************/
129 void
130 gl_mini_preview_pixbuf_cache_delete_by_name (gchar *name)
131 {
132         gl_debug (DEBUG_PIXBUF_CACHE, "START");
133
134         g_hash_table_remove (mini_preview_pixbuf_cache, name);
135
136         gl_debug (DEBUG_PIXBUF_CACHE, "END");
137 }
138
139
140 /*****************************************************************************/
141 /* Get pixbuf.                                                               */
142 /*****************************************************************************/
143 GdkPixbuf *
144 gl_mini_preview_pixbuf_cache_get_pixbuf (gchar      *name)
145 {
146         GdkPixbuf   *pixbuf;
147
148         gl_debug (DEBUG_PIXBUF_CACHE, "START pixbuf_cache=%p", mini_preview_pixbuf_cache);
149
150         pixbuf = g_hash_table_lookup (mini_preview_pixbuf_cache, name);
151
152         if (!pixbuf)
153         {
154                 gl_mini_preview_pixbuf_cache_add_by_name (name);
155                 pixbuf = g_hash_table_lookup (mini_preview_pixbuf_cache, name);
156         }
157
158         gl_debug (DEBUG_PIXBUF_CACHE, "END");
159
160         return g_object_ref (pixbuf);
161 }
162
163
164
165 /*
166  * Local Variables:       -- emacs
167  * mode: C                -- emacs
168  * c-basic-offset: 8      -- emacs
169  * tab-width: 8           -- emacs
170  * indent-tabs-mode: nil  -- emacs
171  * End:                   -- emacs
172  */