* add ModestIconFactory implementation
authorDirk-Jan C. Binnema <dirk-jan.binnema@nokia.com>
Tue, 6 Jun 2006 08:37:52 +0000 (08:37 +0000)
committerDirk-Jan C. Binnema <dirk-jan.binnema@nokia.com>
Tue, 6 Jun 2006 08:37:52 +0000 (08:37 +0000)
pmo-trunk-r146

src/modest-icon-factory.c [new file with mode: 0644]
src/modest-icon-factory.h [new file with mode: 0644]

diff --git a/src/modest-icon-factory.c b/src/modest-icon-factory.c
new file mode 100644 (file)
index 0000000..f454f96
--- /dev/null
@@ -0,0 +1,82 @@
+/* modest-icon-factory.c */
+
+#include <string.h>
+#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 (file)
index 0000000..6dc718c
--- /dev/null
@@ -0,0 +1,36 @@
+/* modest-icon-factory.h */
+
+#ifndef __MODEST_ICON_FACTORY_H__
+#define __MODEST_ICON_FACTORY_H__
+
+#include <gtk/gtk.h>
+
+/**
+ * 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__ */