pmo-trunk-r601
[modest] / src / modest-icon-factory.c
index f454f96..c1902a2 100644 (file)
@@ -1,3 +1,33 @@
+/* Copyright (c) 2006, Nokia Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * * Neither the name of the Nokia Corporation nor the names of its
+ *   contributors may be used to endorse or promote products derived from
+ *   this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+
 /* modest-icon-factory.c */
 
 #include <string.h>
@@ -23,11 +53,11 @@ void
 modest_icon_factory_init   (void)
 {
        if (icon_hash) {
-               g_warning ("modest_icon_factory_init "
-                          "should be called only once");
+               g_printerr ("modest: modest_icon_factory_init " 
+                           "should be called only once\n");
                return;
        }
-
+       
        icon_hash = g_hash_table_new_full (g_str_hash,
                                           (GEqualFunc)equal_func,
                                           (GDestroyNotify)g_free,
@@ -39,9 +69,9 @@ void
 modest_icon_factory_uninit (void)
 {
        if (!icon_hash) {
-               g_warning ("modest_icon_factory_uninit "
+               g_printerr ("modest: modest_icon_factory_uninit "
                           "must only be called with initialized "
-                          "ModestIconFactories");
+                          "ModestIconFactories\n");
                return;
        }
 
@@ -57,9 +87,11 @@ modest_icon_factory_get_icon (const gchar *name)
        GError *err = NULL;
        GdkPixbuf *pixbuf;
        gpointer orig_key;
+
+       g_return_val_if_fail (name, NULL);
        
        if (!icon_hash) {
-               g_warning ("ModestIconFactory must be initialized first");
+               g_printerr ("modest: ModestIconFactory must be initialized first\n");
                return NULL;
        }
 
@@ -70,7 +102,8 @@ modest_icon_factory_get_icon (const gchar *name)
                                           (gpointer*)&pixbuf)) {
                pixbuf = gdk_pixbuf_new_from_file (name, &err);
                if (!pixbuf) {
-                       g_warning (err->message);
+                       g_printerr ("modest: error in icon factory while loading '%s': %s\n",
+                                   name, err->message);
                        g_error_free (err);
                }
                /* if we cannot find it, we still insert, so we get the error
@@ -80,3 +113,34 @@ modest_icon_factory_get_icon (const gchar *name)
        }
        return pixbuf;
 }
+
+
+
+GdkPixbuf*
+modest_icon_factory_get_icon_at_size (const gchar *name, guint width, guint height)
+{
+       /* FIXME, somehow, cache scaled icons as well... */
+       GError *err = NULL;
+       GdkPixbuf *pixbuf = NULL;
+
+       g_return_val_if_fail (name, NULL);
+       
+       if (!icon_hash) {
+               g_printerr ("modest: ModestIconFactory must be initialized first\n");
+               return NULL;
+       }
+
+       pixbuf = gdk_pixbuf_new_from_file_at_size (name, width, height, &err);
+       if (!pixbuf) {
+               g_printerr ("modest: error in icon factory while loading '%s'@(%dx%d): %s\n",
+                           name, width, height, err->message);
+               g_error_free (err);
+       }
+       
+       /* we insert it, so it will be freed... FIXME... */
+       if (pixbuf)
+               g_hash_table_insert (icon_hash, g_strdup_printf ("%s-%d-%d",name,width,height),
+                                    (gpointer)pixbuf);
+       
+       return pixbuf;
+}