[niepce] Fix crashes when the icon theme is not available.



commit 4b369ca9d72e6093b0b5451cc389b12e1d6a5cff
Author: Hub Figuiere <hub figuiere net>
Date:   Sun Nov 6 18:05:52 2011 -0800

    Fix crashes when the icon theme is not available.

 src/fwk/toolkit/gdkutils.cpp          |    5 +++
 src/niepce/ui/imageliststore.cpp      |   17 +++++++++---
 src/niepce/ui/librarycellrenderer.cpp |    4 +++
 src/niepce/ui/workspacecontroller.cpp |   44 ++++++++++++++++++---------------
 4 files changed, 46 insertions(+), 24 deletions(-)
---
diff --git a/src/fwk/toolkit/gdkutils.cpp b/src/fwk/toolkit/gdkutils.cpp
index 5bf0b48..e432e42 100644
--- a/src/fwk/toolkit/gdkutils.cpp
+++ b/src/fwk/toolkit/gdkutils.cpp
@@ -17,6 +17,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include "fwk/base/debug.hpp"
 #include "gdkutils.hpp"
 
 
@@ -28,6 +29,10 @@ namespace fwk {
 	{
 		int height, width;
 		int orig_h, orig_w;
+		if(!pix) {
+			ERR_OUT("NULL pixbuf");
+			return pix;
+		}
 		orig_h = pix->get_height();
 		orig_w = pix->get_width();
 		int orig_dim = std::max(orig_h, orig_w);
diff --git a/src/niepce/ui/imageliststore.cpp b/src/niepce/ui/imageliststore.cpp
index d46c98e..0890f2c 100644
--- a/src/niepce/ui/imageliststore.cpp
+++ b/src/niepce/ui/imageliststore.cpp
@@ -80,11 +80,20 @@ void ImageListStore::on_lib_notification(const eng::LibNotification &ln)
             Gtk::TreeModel::iterator riter = append();
             Gtk::TreeRow row = *riter;
             // locate it in local cache...
-            row[m_columns.m_pix] = icon_theme->load_icon(
-                Glib::ustring("image-loading"), 32,
-                Gtk::ICON_LOOKUP_USE_BUILTIN);
+            // this would avoid exception handling.
+            Glib::RefPtr<Gdk::Pixbuf> icon;
+            try {
+                icon = icon_theme->load_icon(
+                    Glib::ustring("image-loading"), 32,
+                    Gtk::ICON_LOOKUP_USE_BUILTIN);
+            }
+            catch(const Gtk::IconThemeError & e)
+            {
+                ERR_OUT("Exception %s.", e.what().c_str());
+            }
+            row[m_columns.m_pix] = icon;
             row[m_columns.m_libfile] = *iter;
-            row[m_columns.m_strip_thumb] = fwk::gdkpixbuf_scale_to_fit(row[m_columns.m_pix], 100);
+            row[m_columns.m_strip_thumb] = fwk::gdkpixbuf_scale_to_fit(icon, 100);
             m_idmap[(*iter)->id()] = riter;
         }
         // at that point clear the cache because the icon view is populated.
diff --git a/src/niepce/ui/librarycellrenderer.cpp b/src/niepce/ui/librarycellrenderer.cpp
index 002d283..e9e83ff 100644
--- a/src/niepce/ui/librarycellrenderer.cpp
+++ b/src/niepce/ui/librarycellrenderer.cpp
@@ -89,6 +89,10 @@ void LibraryCellRenderer::_drawThumbnail(const Cairo::RefPtr<Cairo::Context> & c
                                          Glib::RefPtr<Gdk::Pixbuf> & pixbuf,
                                          const GdkRectangle & r)
 {
+    if(!pixbuf) {
+        ERR_OUT("NULL pixbuf");
+        return;
+    }
     int w = pixbuf->get_width();
     int h = pixbuf->get_height();
     int offset_x = (m_size - w) / 2;
diff --git a/src/niepce/ui/workspacecontroller.cpp b/src/niepce/ui/workspacecontroller.cpp
index b10dd17..c202873 100644
--- a/src/niepce/ui/workspacecontroller.cpp
+++ b/src/niepce/ui/workspacecontroller.cpp
@@ -37,30 +37,34 @@ using fwk::Application;
 
 namespace ui {
 
+
 WorkspaceController::WorkspaceController()
     : fwk::UiController()
 {
+    static struct _Icons {
+        int icon_id;
+        const char *icon_name;
+    } icons[] = {
+        { ICON_FOLDER, "folder" },
+        { ICON_PROJECT, "applications-accessories" },
+        { ICON_ROLL, "emblem-photos" },
+        { ICON_TRASH, "user-trash" },
+        { ICON_KEYWORD, "application-certificate" },
+        { 0, NULL }
+    };
+    
     Glib::RefPtr< Gtk::IconTheme > icon_theme(Application::app()->getIconTheme());
-    try {
-        m_icons[ICON_FOLDER] = icon_theme->load_icon(
-            Glib::ustring("folder"), 16, Gtk::ICON_LOOKUP_USE_BUILTIN);
-        m_icons[ICON_PROJECT] = icon_theme->load_icon(
-            Glib::ustring("applications-accessories"), 16, 
-            Gtk::ICON_LOOKUP_USE_BUILTIN);
-        m_icons[ICON_ROLL] = icon_theme->load_icon(
-            Glib::ustring("emblem-photos"), 16,
-            Gtk::ICON_LOOKUP_USE_BUILTIN);
-        m_icons[ICON_TRASH] = icon_theme->load_icon(
-            Glib::ustring("user-trash"), 16,
-            Gtk::ICON_LOOKUP_USE_BUILTIN);
-        // FIXME use an icon that make more sense.
-        m_icons[ICON_KEYWORD] = icon_theme->load_icon(
-            Glib::ustring("application-certificate"), 16, 
-            Gtk::ICON_LOOKUP_USE_BUILTIN);
-    }
-    catch(const Gtk::IconThemeError & e)
-    {
-        ERR_OUT("Exception %s.", e.what().c_str());
+    int i = 0;
+    while(icons[i].icon_name) {
+        try {
+            m_icons[icons[i].icon_id] = icon_theme->load_icon(
+            Glib::ustring(icons[i].icon_name), 16, Gtk::ICON_LOOKUP_USE_BUILTIN);
+        }
+        catch(const Gtk::IconThemeError & e)
+        {
+            ERR_OUT("Exception %s.", e.what().c_str());
+        }
+        i++;
     }
 }
 



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