]> git.sur5r.net Git - glabels/blob - src/mini-preview-pixbuf-cache.c
Imported Upstream version 3.0.0
[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_all (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         gchar            *name;
84
85         gl_debug (DEBUG_PIXBUF_CACHE, "START");
86
87         pixbuf = gl_mini_preview_pixbuf_new (template, 72, 72);
88
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));
91
92         g_object_unref (pixbuf);
93
94         gl_debug (DEBUG_PIXBUF_CACHE, "END");
95 }
96
97
98 /*****************************************************************************/
99 /* Add pixbuf to cache by name.                                              */
100 /*****************************************************************************/
101 void
102 gl_mini_preview_pixbuf_cache_add_by_name (gchar      *name)
103 {
104         lglTemplate *template;
105         GdkPixbuf   *pixbuf;
106
107         gl_debug (DEBUG_PIXBUF_CACHE, "START");
108
109         template = lgl_db_lookup_template_from_name (name);
110         pixbuf = gl_mini_preview_pixbuf_new (template, 72, 72);
111         lgl_template_free (template);
112
113         g_hash_table_insert (mini_preview_pixbuf_cache, g_strdup (name), pixbuf);
114
115         gl_debug (DEBUG_PIXBUF_CACHE, "END");
116 }
117
118
119 /*****************************************************************************/
120 /* Delete pixbuf from cache by name.                                         */
121 /*****************************************************************************/
122 void
123 gl_mini_preview_pixbuf_cache_delete_by_name (gchar *name)
124 {
125         gl_debug (DEBUG_PIXBUF_CACHE, "START");
126
127         g_hash_table_remove (mini_preview_pixbuf_cache, name);
128
129         gl_debug (DEBUG_PIXBUF_CACHE, "END");
130 }
131
132
133 /*****************************************************************************/
134 /* Get pixbuf.                                                               */
135 /*****************************************************************************/
136 GdkPixbuf *
137 gl_mini_preview_pixbuf_cache_get_pixbuf (gchar      *name)
138 {
139         GdkPixbuf   *pixbuf;
140
141         gl_debug (DEBUG_PIXBUF_CACHE, "START pixbuf_cache=%p", mini_preview_pixbuf_cache);
142
143         pixbuf = g_hash_table_lookup (mini_preview_pixbuf_cache, name);
144
145         if (!pixbuf)
146         {
147                 gl_mini_preview_pixbuf_cache_add_by_name (name);
148                 pixbuf = g_hash_table_lookup (mini_preview_pixbuf_cache, name);
149         }
150
151         gl_debug (DEBUG_PIXBUF_CACHE, "END");
152
153         return g_object_ref (pixbuf);
154 }
155
156
157
158 /*
159  * Local Variables:       -- emacs
160  * mode: C                -- emacs
161  * c-basic-offset: 8      -- emacs
162  * tab-width: 8           -- emacs
163  * indent-tabs-mode: nil  -- emacs
164  * End:                   -- emacs
165  */