bigboard r7438 - trunk



Author: otaylor
Date: Tue Oct 28 15:18:48 2008
New Revision: 7438
URL: http://svn.gnome.org/viewvc/bigboard?rev=7438&view=rev

Log:
Add some robustness against missing images

If loading an image by name fails, log it and return a 1x1 transparent surface
Cleanup: replace some instances of debug("msg" % (args,...)) with debug("msg", args)


Modified:
   trunk/main.py

Modified: trunk/main.py
==============================================================================
--- trunk/main.py	(original)
+++ trunk/main.py	Tue Oct 28 15:18:48 2008
@@ -37,6 +37,7 @@
 
 # TODO: figure out an algorithm for removing pixbufs from the cache
 _surfacecache = {}
+_emptysurface = None
 _mtimecache = {}
 def _get_mtime(filename):
     try:
@@ -62,19 +63,33 @@
         try:
             if img_name.find(os.sep) >= 0:
                 pixbuf = gtk.gdk.pixbuf_new_from_file(img_name)
-                _logger.debug("loaded from file '%s': %s" % (img_name,pixbuf))               
+                _logger.debug("loaded from file '%s': %s", img_name, pixbuf)
                 if pixbuf:
                     _mtimecache[img_name] = _get_mtime(img_name)
             else:
                 theme = gtk.icon_theme_get_default()
                 pixbuf = theme.load_icon(img_name, 60, gtk.ICON_LOOKUP_USE_BUILTIN)
-                _logger.debug("loaded from icon theme '%s': %s" % (img_name,pixbuf))
-        except gobject.GError:
-            return None
-            
-        surface = hippo.cairo_surface_from_gdk_pixbuf(pixbuf)
+                _logger.debug("loaded from icon theme '%s': %s", img_name,pixbuf)
+        except gobject.GError, e:
+            _logger.error("Failed to load icon: '%s'", e.message)
+            pixbuf = None
+
+        if pixbuf:
+            surface = hippo.cairo_surface_from_gdk_pixbuf(pixbuf)
+            _surfacecache[img_name] = surface
+
+    if not surface:
+        # Returning None will cause a crash, return a 1x1 transparent pixmap
+        global _emptysurface
+        if not _emptysurface:
+            _emptysurface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1, 1)
+            cr = cairo.Context(_emptysurface)
+            cr.set_operator(cairo.OPERATOR_CLEAR)
+            cr.paint()
+        surface = _emptysurface
+        # Cache negative result
         _surfacecache[img_name] = surface
-        
+
     return surface
 
 def on_name_lost(*args):



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]