From 398a253779d9b8cc4913d5b7b932e5da8ba059b2 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Tue, 6 Jun 2006 08:37:52 +0000 Subject: [PATCH] * add ModestIconFactory implementation pmo-trunk-r146 --- src/modest-icon-factory.c | 82 +++++++++++++++++++++++++++++++++++++++++++++ src/modest-icon-factory.h | 36 ++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 src/modest-icon-factory.c create mode 100644 src/modest-icon-factory.h diff --git a/src/modest-icon-factory.c b/src/modest-icon-factory.c new file mode 100644 index 0000000..f454f96 --- /dev/null +++ b/src/modest-icon-factory.c @@ -0,0 +1,82 @@ +/* modest-icon-factory.c */ + +#include +#include "modest-icon-factory.h" + +static GHashTable *icon_hash = NULL; + +static +gboolean equal_func (const gchar *s1, const gchar *s2) +{ + return strcmp (s1, s2) == 0; +} + +static +void free_pixbuf (GdkPixbuf *pixbuf) +{ + if (pixbuf) + g_object_unref (G_OBJECT(pixbuf)); +} + + +void +modest_icon_factory_init (void) +{ + if (icon_hash) { + g_warning ("modest_icon_factory_init " + "should be called only once"); + return; + } + + icon_hash = g_hash_table_new_full (g_str_hash, + (GEqualFunc)equal_func, + (GDestroyNotify)g_free, + (GDestroyNotify)free_pixbuf); +} + + +void +modest_icon_factory_uninit (void) +{ + if (!icon_hash) { + g_warning ("modest_icon_factory_uninit " + "must only be called with initialized " + "ModestIconFactories"); + return; + } + + g_hash_table_destroy (icon_hash); + icon_hash = NULL; +} + + + +GdkPixbuf* +modest_icon_factory_get_icon (const gchar *name) +{ + GError *err = NULL; + GdkPixbuf *pixbuf; + gpointer orig_key; + + if (!icon_hash) { + g_warning ("ModestIconFactory must be initialized first"); + return NULL; + } + + /* is it already in the hashtable? + * note: this can be NULL + */ + if (!g_hash_table_lookup_extended (icon_hash, name, &orig_key, + (gpointer*)&pixbuf)) { + pixbuf = gdk_pixbuf_new_from_file (name, &err); + if (!pixbuf) { + g_warning (err->message); + g_error_free (err); + } + /* if we cannot find it, we still insert, so we get the error + * only once */ + g_hash_table_insert (icon_hash, g_strdup(name), + (gpointer)pixbuf); + } + return pixbuf; +} diff --git a/src/modest-icon-factory.h b/src/modest-icon-factory.h new file mode 100644 index 0000000..6dc718c --- /dev/null +++ b/src/modest-icon-factory.h @@ -0,0 +1,36 @@ +/* modest-icon-factory.h */ + +#ifndef __MODEST_ICON_FACTORY_H__ +#define __MODEST_ICON_FACTORY_H__ + +#include + +/** + * modest_icon_factory_init + * + * initialize the modest_icon_factory, which is a runtime-wide singleton + * this should be called only once, before doing anything else with the icon + * factory + */ +void modest_icon_factory_init (void); + + +/** + * modest_icon_factory_uninit + * + * uninitialize the modest_icon_factory. this should be done after the icon + * factory is no longer needed. + */ +void modest_icon_factory_uninit (void); + + +/** + * modest_icon_factory_get_icon: + * @name: the filename of a certain icon + * + * Returns: a GdkPixBuf for this icon, or NULL in case of error + * You should NOT unref or modify the pixbuf in any way + */ +GdkPixbuf* modest_icon_factory_get_icon (const gchar *name); + +#endif /*__MODEST_ICON_FACTORY_H__ */ -- 1.7.9.5