File: pixbuf-cache.c

package info (click to toggle)
galeon 2.0.6-2.1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 19,156 kB
  • ctags: 9,680
  • sloc: ansic: 77,798; cpp: 13,928; sh: 9,000; xml: 5,761; makefile: 901
file content (201 lines) | stat: -rw-r--r-- 4,872 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 *  Copyright (C) 2001-2002 Jorn Baayen 
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "gul-general.h"
#include "gul-gui.h"
#include "pixbuf-cache.h"
#include "eel-gconf-extensions.h"

#include <stdlib.h>
#include <string.h>
#include <gdk/gdkpixbuf.h>
#include <gtk/gtkstock.h>
#include <gtk/gtkiconfactory.h>

#include <libgnome/gnome-util.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

/* cache of theme pixmaps */
static GHashTable *gul_pixbufs_cache = NULL;

/* Local prototypes */
static gboolean gul_pixbuf_free_cb (gpointer key, GdkPixbuf *value,
			        gpointer user_data);

/**
 * pixbuf_free_cb: free one pixbuf
 */
static gboolean
gul_pixbuf_free_cb (gpointer key, GdkPixbuf *value, gpointer user_data)
{
	g_free (key);
	g_object_unref (value);
	return TRUE;
}

/**
 * pixbuf_cache_reset: reset the pixbuf cache
 */
void
gul_pixbuf_cache_reset (void)
{
	if (gul_pixbufs_cache != NULL)
	{
		g_hash_table_foreach_remove (gul_pixbufs_cache,
					     (GHRFunc) gul_pixbuf_free_cb, NULL);
	}
}

/**
 * pixbuf_cache_get: get 'n cache a pixbuf
 */
GdkPixbuf *
gul_pixbuf_cache_get (const gchar *filename)
{
	GdkPixbuf *pixbuf = NULL;
	gchar *tmp;

	/* lookup in cache */
	if (gul_pixbufs_cache != NULL)
		pixbuf = g_hash_table_lookup (gul_pixbufs_cache, filename);

	if (pixbuf != NULL)
	{
		g_object_ref (G_OBJECT (pixbuf));
		return pixbuf;
	}

	if (g_path_is_absolute (filename))
	{
		tmp = g_strdup (filename);
	}
	else
	{
		tmp = gul_general_user_file (filename, FALSE);
	}

	if (tmp && g_file_test (tmp, G_FILE_TEST_EXISTS))
	{
		pixbuf = gdk_pixbuf_new_from_file (tmp, NULL);
	}

	g_free (tmp);

	if (pixbuf == NULL)
		return NULL;

	/* cache the created GdkPixbuf */
	if (gul_pixbufs_cache == NULL)
	{
		gul_pixbufs_cache = g_hash_table_new (g_str_hash,
						      g_str_equal);
	}

	g_hash_table_insert (gul_pixbufs_cache, g_strdup (filename),
			     pixbuf);
	
	g_object_ref (G_OBJECT (pixbuf));
	return pixbuf;
}

/**
 * pixbuf_cache_register_stocks: register galeon icons with the gnome stock
 * system so people can theme our icons
 */
void
gul_pixbuf_cache_register_stocks (void)
{
	static gboolean done = FALSE;
	GtkIconFactory *factory;
	GtkIconSet *icon_set;
	GtkIconSource *icon_source;
	gint i;

	GtkStockItem items[] = {
		{ GALEON_STOCK_SEPARATOR     ,"_Separator",  0, 0, NULL },
		{ GALEON_STOCK_FOLDER        ,"_Folder",     0, 0, NULL },
		{ GALEON_STOCK_DEFAULT       ,"_Default",    0, 0, NULL },
		{ GALEON_STOCK_HISTORY       ,"_History",    0, 0, NULL },
		{ GALEON_STOCK_FILTER        ,"_Filter",     0, 0, NULL },
		{ GALEON_STOCK_POPUP_BLOCKED ,"_Popup blocked", 0, 0, NULL },
		{ GALEON_STOCK_SECURE        ,"_Secure",      0, 0, NULL },
		{ GALEON_STOCK_INSECURE      ,"_Insecure",    0, 0, NULL },
		{ GALEON_STOCK_DOWNLOAD      ,"_Download",    0, 0, NULL },
		{ GALEON_STOCK_ENTRY         ,"_Entry",       0, 0, NULL }
	};

	const char * icon_theme_items[] = {
		STOCK_ZOOM,
		STOCK_NEW_TAB,
		STOCK_FULLSCREEN,
		STOCK_VIEW_SOURCE, 
		STOCK_SEND_MAIL,
		STOCK_ADD_BOOKMARK,
		STOCK_SPINNER_REST,
		STOCK_SELECT_ALL,
		STOCK_EDIT_BOOKMARK,
		STOCK_CONNECT,
		STOCK_DISCONNECT,
		STOCK_LOCK_BROKEN
	};


	if (done) 
	{
		return;
	}
	done = TRUE;

	gtk_stock_add (items, G_N_ELEMENTS (items));

	factory = gtk_icon_factory_new ();
	gtk_icon_factory_add_default (factory);

	for (i = 0; i < (gint) G_N_ELEMENTS (items); i++)
	{
		GdkPixbuf *pixbuf;
		gchar *fn;

		fn = g_strconcat (items[i].stock_id, ".png", NULL);
		pixbuf = gul_pixbuf_cache_get (fn);
		g_free (fn);

		icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
		gtk_icon_factory_add (factory, items[i].stock_id, icon_set);
		gtk_icon_set_unref (icon_set);
		
		g_object_unref (G_OBJECT (pixbuf));
	}

	for (i = 0; i < (int) G_N_ELEMENTS (icon_theme_items); i++)
	{
		icon_set = gtk_icon_set_new ();
		icon_source = gtk_icon_source_new ();
		gtk_icon_source_set_icon_name (icon_source, icon_theme_items[i]);
		gtk_icon_set_add_source (icon_set, icon_source);
		gtk_icon_factory_add (factory, icon_theme_items[i], icon_set);
		gtk_icon_set_unref (icon_set);
		gtk_icon_source_free (icon_source);
	}

	g_object_unref (G_OBJECT (factory));
}