]> git.sur5r.net Git - glabels/blob - glabels2/src/mini-preview-pixbuf-cache.c
82077ad513603dfc5cdb5acb930220401abd735c
[glabels] / glabels2 / 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/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 \f
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 (g_str_hash, g_str_equal);
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 /* Add pixbuf to cache by template.                                          */
77 /*****************************************************************************/
78 void
79 gl_mini_preview_pixbuf_cache_add_by_template (lglTemplate *template)
80 {
81         GdkPixbuf        *pixbuf;
82         GList            *p;
83         lglTemplateAlias *alias;
84         gchar            *name;
85
86         gl_debug (DEBUG_PIXBUF_CACHE, "START");
87
88         pixbuf = gl_mini_preview_pixbuf_new (template, 72, 72);
89
90         for ( p=template->aliases; p != NULL; p=p->next )
91         {
92                 alias = (lglTemplateAlias *)p->data;
93
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));
96         }
97
98         g_object_unref (pixbuf);
99
100         gl_debug (DEBUG_PIXBUF_CACHE, "END");
101 }
102
103 /*****************************************************************************/
104 /* Add pixbuf to cache by name.                                              */
105 /*****************************************************************************/
106 void
107 gl_mini_preview_pixbuf_cache_add_by_name (gchar      *name)
108 {
109         lglTemplate *template;
110         GdkPixbuf   *pixbuf;
111
112         gl_debug (DEBUG_PIXBUF_CACHE, "START");
113
114         template = lgl_db_lookup_template_from_name (name);
115         pixbuf = gl_mini_preview_pixbuf_new (template, 72, 72);
116         lgl_template_free (template);
117
118         g_hash_table_insert (mini_preview_pixbuf_cache, g_strdup (name), pixbuf);
119
120         gl_debug (DEBUG_PIXBUF_CACHE, "END");
121 }
122
123 /*****************************************************************************/
124 /* Get pixbuf.                                                               */
125 /*****************************************************************************/
126 GdkPixbuf *
127 gl_mini_preview_pixbuf_cache_get_pixbuf (gchar      *name)
128 {
129         GdkPixbuf   *pixbuf;
130
131         gl_debug (DEBUG_PIXBUF_CACHE, "START pixbuf_cache=%p", mini_preview_pixbuf_cache);
132
133         pixbuf = g_hash_table_lookup (mini_preview_pixbuf_cache, name);
134
135         if (!pixbuf)
136         {
137                 gl_mini_preview_pixbuf_cache_add_by_name (name);
138                 pixbuf = g_hash_table_lookup (mini_preview_pixbuf_cache, name);
139         }
140
141         gl_debug (DEBUG_PIXBUF_CACHE, "END");
142
143         return g_object_ref (pixbuf);
144 }
145
146
147
148 /*
149  * Local Variables:       -- emacs
150  * mode: C                -- emacs
151  * c-basic-offset: 8      -- emacs
152  * tab-width: 8           -- emacs
153  * indent-tabs-mode: nil  -- emacs
154  * End:                   -- emacs
155  */