]> git.sur5r.net Git - glabels/blob - src/mini-preview-pixbuf-cache.c
Imported Upstream version 2.2.8
[glabels] / src / mini-preview-pixbuf-cache.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2
3 /*
4  *  (GLABELS) Label and Business Card Creation program for GNOME
5  *
6  *  mini-preview-pixbuf-cache.c:  GLabels mini-preview pixbuf cache module
7  *
8  *  Copyright (C) 2007  Jim Evins <evins@snaught.com>.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23  */
24 #include <config.h>
25
26 #include "mini-preview-pixbuf-cache.h"
27 #include "mini-preview-pixbuf.h"
28
29 #include "libglabels/db.h"
30
31 #include <glib/gmem.h>
32 #include <glib/ghash.h>
33
34 #include "debug.h"
35
36 /*========================================================*/
37 /* Private types.                                         */
38 /*========================================================*/
39
40 /*========================================================*/
41 /* Private globals.                                       */
42 /*========================================================*/
43
44 static GHashTable *mini_preview_pixbuf_cache = NULL;
45
46 /*========================================================*/
47 /* Private function prototypes.                           */
48 /*========================================================*/
49
50 \f
51 /*****************************************************************************/
52 /* Create a new hash table to keep track of cached mini preview pixbufs.     */
53 /*****************************************************************************/
54 void
55 gl_mini_preview_pixbuf_cache_init (void)
56 {
57         GList       *names = NULL;
58         GList       *p;
59         lglTemplate *template;
60
61         gl_debug (DEBUG_PIXBUF_CACHE, "START");
62
63         mini_preview_pixbuf_cache = g_hash_table_new (g_str_hash, g_str_equal);
64
65         names = lgl_db_get_template_name_list_unique (NULL, NULL, NULL);
66         for ( p=names; p != NULL; p=p->next )
67         {
68                 gl_debug (DEBUG_PIXBUF_CACHE, "name = \"%s\"", p->data);
69
70                 template = lgl_db_lookup_template_from_name (p->data);
71                 gl_mini_preview_pixbuf_cache_add_by_template (template);
72                 lgl_template_free (template);
73         }
74         lgl_db_free_template_name_list (names);
75
76         gl_debug (DEBUG_PIXBUF_CACHE, "END pixbuf_cache=%p", mini_preview_pixbuf_cache);
77 }
78
79 /*****************************************************************************/
80 /* Add pixbuf to cache by template.                                          */
81 /*****************************************************************************/
82 void
83 gl_mini_preview_pixbuf_cache_add_by_template (lglTemplate *template)
84 {
85         GdkPixbuf        *pixbuf;
86         GList            *p;
87         lglTemplateAlias *alias;
88         gchar            *name;
89
90         gl_debug (DEBUG_PIXBUF_CACHE, "START");
91
92         pixbuf = gl_mini_preview_pixbuf_new (template, 72, 72);
93
94         for ( p=template->aliases; p != NULL; p=p->next )
95         {
96                 alias = (lglTemplateAlias *)p->data;
97
98                 name = g_strdup_printf ("%s %s", alias->brand, alias->part);
99                 g_hash_table_insert (mini_preview_pixbuf_cache, name, g_object_ref (pixbuf));
100         }
101
102         g_object_unref (pixbuf);
103
104         gl_debug (DEBUG_PIXBUF_CACHE, "END");
105 }
106
107 /*****************************************************************************/
108 /* Add pixbuf to cache by name.                                              */
109 /*****************************************************************************/
110 void
111 gl_mini_preview_pixbuf_cache_add_by_name (gchar      *name)
112 {
113         lglTemplate *template;
114         GdkPixbuf   *pixbuf;
115
116         gl_debug (DEBUG_PIXBUF_CACHE, "START");
117
118         template = lgl_db_lookup_template_from_name (name);
119         pixbuf = gl_mini_preview_pixbuf_new (template, 72, 72);
120         lgl_template_free (template);
121
122         g_hash_table_insert (mini_preview_pixbuf_cache, g_strdup (name), pixbuf);
123
124         gl_debug (DEBUG_PIXBUF_CACHE, "END");
125 }
126
127 /*****************************************************************************/
128 /* Get pixbuf.                                                               */
129 /*****************************************************************************/
130 GdkPixbuf *
131 gl_mini_preview_pixbuf_cache_get_pixbuf (gchar      *name)
132 {
133         GdkPixbuf   *pixbuf;
134
135         gl_debug (DEBUG_PIXBUF_CACHE, "START pixbuf_cache=%p", mini_preview_pixbuf_cache);
136
137         pixbuf = g_hash_table_lookup (mini_preview_pixbuf_cache, name);
138
139         if (!pixbuf)
140         {
141                 gl_mini_preview_pixbuf_cache_add_by_name (name);
142                 pixbuf = g_hash_table_lookup (mini_preview_pixbuf_cache, name);
143         }
144
145         gl_debug (DEBUG_PIXBUF_CACHE, "END");
146
147         return g_object_ref (pixbuf);
148 }
149