soylent r40 - trunk/src



Author: treitter
Date: Mon Jan  7 01:52:18 2008
New Revision: 40
URL: http://svn.gnome.org/viewvc/soylent?rev=40&view=rev

Log:
Check return values properly for the eds-utils

Modified:
   trunk/src/eds-utils.c
   trunk/src/eds-utils.h
   trunk/src/soylent-browser-person-view.c
   trunk/src/soylent-utils.h

Modified: trunk/src/eds-utils.c
==============================================================================
--- trunk/src/eds-utils.c	(original)
+++ trunk/src/eds-utils.c	Mon Jan  7 01:52:18 2008
@@ -28,140 +28,220 @@
 #include <libebook/e-book.h>
 
 #include "eds-utils.h"
+#include "soylent-utils.h"
 
 /* Get the GdkPixbuf representation of a photo inlined in an EContact */
 GdkPixbuf*
 gdk_pixbuf_from_inline_photo (EContactPhoto *photo, guint icon_width_max,
                               guint icon_height_max)
 {
+  GdkPixbuf *retval = NULL;
   int width = -1;
   int height = -1;
   GdkPixbuf *pixbuf = NULL;
-  GdkPixbuf *ret = NULL;
   GdkPixbufLoader *loader = NULL;
 
-  if (photo->type != E_CONTACT_PHOTO_TYPE_INLINED)
-    {
-      return NULL;
-    }
+  g_return_val_if_fail (photo != NULL, retval);
+  g_return_val_if_fail (photo->type == E_CONTACT_PHOTO_TYPE_INLINED, retval);
+  g_return_val_if_fail (icon_width_max > 0, retval);
+  g_return_val_if_fail (icon_height_max > 0, retval);
 
   loader = gdk_pixbuf_loader_new ();
-  gdk_pixbuf_loader_write (loader, photo->data.inlined.data,
-                           photo->data.inlined.length, NULL);
-
-  pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
-
-  if (pixbuf != NULL)
+  if (loader)
     {
-      g_object_ref (G_OBJECT (pixbuf));
-    }
-
-  gdk_pixbuf_loader_close (loader, NULL);
-  g_object_unref (G_OBJECT (loader));
+      gboolean write_retval = FALSE;
 
-  width = gdk_pixbuf_get_width (pixbuf);
-  height = gdk_pixbuf_get_height (pixbuf);
+      write_retval = gdk_pixbuf_loader_write (loader, photo->data.inlined.data,
+                                              photo->data.inlined.length, NULL);
+      if (write_retval)
+        {
+          /* XXX: we may need to handle the remainder within a callback to the
+           * loader's "area-prepared" signal to avoid a race condition */
+          pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
+          if (pixbuf)
+            {
+              gboolean loader_close_retval = FALSE;
+
+              g_object_ref (G_OBJECT (pixbuf));
+
+              loader_close_retval = gdk_pixbuf_loader_close (loader, NULL);
+              if (loader_close_retval)
+                {
+                  width = gdk_pixbuf_get_width (pixbuf);
+                  height = gdk_pixbuf_get_height (pixbuf);
+
+                  if (width > icon_width_max)
+                    {
+                      height = (height * icon_width_max) / width;
+                      width = icon_width_max;
+                    }
+                  if (height > icon_height_max)
+                    {
+                      width = (width * icon_height_max) / height;
+                      height = icon_height_max;
+                    }
+
+                  retval = gdk_pixbuf_scale_simple (pixbuf, width, height,
+                                                    GDK_INTERP_BILINEAR);
+                }
+              else
+                {
+                  g_warning ("closed the GdkPixbufLoader before it finished "
+                             "writing data to the GdkPixbuf");
+                }
+
+              g_object_unref (pixbuf);
+            }
+          else
+            {
+              g_warning ("failed to get the GdkPixbuf from the "
+                         "GdkPixbufLoader");
+            }
+        }
+      else
+        {
+          g_warning ("failed to write image data to the GdkPixbufLoader");
+        }
 
-  if (width > icon_width_max)
-    {
-      height = (height * icon_width_max) / width;
-      width = icon_width_max;
+      g_object_unref (G_OBJECT (loader));
     }
-  if (height > icon_height_max)
+  else
     {
-      width = (width * icon_height_max) / height;
-      height = icon_height_max;
+      g_warning ("failed to create a new GdkPixbufLoader");
     }
 
-  ret = gdk_pixbuf_scale_simple (pixbuf, width, height, GDK_INTERP_BILINEAR);
-  g_object_unref (pixbuf);
-
-  return ret;
+  return retval;
 }
 
 /* Get a "pretty" version of an EContact's name */
 const gchar*
 display_name_from_e_contact (EContact *e_contact)
 {
-  const gchar *name = NULL;
+  const gchar *retval = NULL;
 
-  name = e_contact_get_const (e_contact, E_CONTACT_FULL_NAME);
-  if ((!name) || (g_utf8_strlen (name, -1) <= 0))
-    name = e_contact_get_const (e_contact, E_CONTACT_NICKNAME);
-  if ((!name) || (g_utf8_strlen (name, -1) <= 0))
-    name = "Unnamed";
+  g_return_val_if_fail (e_contact != NULL, retval);
+  g_return_val_if_fail (E_IS_CONTACT (e_contact), retval);
+
+  retval = e_contact_get_const (e_contact, E_CONTACT_FULL_NAME);
+  if (retval)
+    {
+      if (g_utf8_strlen (retval, -1) > 0)
+        {
+          /* success: got display name from Full Name */
+        }
+      else
+        {
+          retval = e_contact_get_const (e_contact, E_CONTACT_NICKNAME);
+          if (retval)
+            {
+              if (g_utf8_strlen (retval, -1) > 0)
+                {
+                  /* success: got display name from Nickname */
+                }
+            }
+          else
+            {
+              retval = "Unnamed";
+            }
+        }
+    }
 
-  return name;
+  return retval;
 }
 
 /* Set the value of an EContact's attribute with the given ID and context
  * ("type" in e-d-s terminology) */
 gboolean
-e_vcard_attr_list_set_value (EContact *e_contact,
-                              EContactField field_id,
-                              const gchar *type,
-                              guint abs_num,
-                              const gchar *value)
+e_vcard_attr_list_set_value (EContact *e_contact, EContactField field_id,
+                             const gchar *type, guint abs_num,
+                             const gchar *value)
 {
+  gboolean retval = FALSE;
   GList *attr_list = NULL;
   GList *attr_list_head = NULL;
   EVCardAttribute *attr = NULL;
   GList *type_existing = NULL;
 
-  attr_list_head = e_contact_get_attributes (e_contact, field_id);
-
-  /* FIXME: there's got to be a cleaner way to do this */
-  for (attr_list = attr_list_head;
-       attr_list;
-       attr_list = g_list_next (attr_list))
-    {
-      attr = attr_list->data;
-      type_existing = e_vcard_attribute_get_param (attr, EVC_TYPE);
-      /* FIXME: apparently an attribute could have multiple types; handle it */
-      if (type_existing && g_str_equal (type, type_existing->data))
-        abs_num--;
-
-      if (abs_num <= 0)
-        break;
-    }
-
-  if (! attr || abs_num < 0)
-    {
-      g_warning ("invalid attribute (context, number); skipping changes");
-      return FALSE;
-    }
-
+  g_return_val_if_fail (e_contact != NULL, retval);
+  g_return_val_if_fail (E_IS_CONTACT (e_contact), retval);
+  g_return_val_if_fail (field_id >= E_CONTACT_FIELD_FIRST, retval);
+  g_return_val_if_fail (field_id < E_CONTACT_FIELD_LAST, retval);
+  g_return_val_if_fail (type != NULL, retval);
+  g_return_val_if_fail (!g_str_equal (type, ""), retval);
+  g_return_val_if_fail (abs_num >= 0, retval);
+  /* value may be NULL */
 
-  if (value && !g_str_equal (value, ""))
+  attr_list_head = e_contact_get_attributes (e_contact, field_id);
+  if (attr_list_head)
     {
-      e_vcard_attribute_remove_values (attr);
-      e_vcard_attribute_add_value (attr, value);
+      /* FIXME: there's got to be a cleaner way to do this */
+      for (attr_list = attr_list_head;
+           attr_list && (abs_num >= 0);
+           attr_list = g_list_next (attr_list))
+        {
+          attr = attr_list->data;
+          type_existing = e_vcard_attribute_get_param (attr, EVC_TYPE);
+          /* FIXME: apparently an attribute could have multiple types; handle it
+           * */
+          if (type_existing && g_str_equal (type, type_existing->data))
+            {
+              abs_num--;
+            }
+        }
+
+      if (attr && abs_num > 0)
+        {
+          if (value && !g_str_equal (value, ""))
+            {
+              e_vcard_attribute_remove_values (attr);
+              e_vcard_attribute_add_value (attr, value);
+            }
+          else
+            {
+              attr_list_head = g_list_delete_link (attr_list_head, attr_list);
+            }
+
+          e_contact_set_attributes (e_contact, field_id, attr_list_head);
+
+          /* FIXME: supposedly we own this -- why does freeing it give us a
+          * double-free segfault? 
+          g_free (attr_list_head);
+          */
+          retval = TRUE;
+        }
+      else
+        {
+          g_warning ("invalid attribute (context, number); skipping changes");
+        }
     }
   else
     {
-      attr_list_head = g_list_delete_link (attr_list_head, attr_list);
+      g_warning ("unable to get the list of attributes for EContact field ID "
+                 "%d", field_id);
     }
 
-  e_contact_set_attributes (e_contact, field_id, attr_list_head);
-
-  /* FIXME: supposedly we own this -- why does freeing it give us a double-free
-   * segfault? 
-  g_free (attr_list_head);
-  */
-  return TRUE;
+  return retval;
 }
 
-/* Set an individual field within an EContactAddress */
-void
+/* Set an individual field within an EContactAddress.
+ * 
+ * Return TRUE for success, FALSE for failure. */
+gboolean
 e_contact_address_set_field (EContactAddress *addr, const gchar *field_name,
                              const gchar *value)
 {
+  gboolean retval = FALSE;
   gchar *value_copy = NULL;
 
+  g_return_val_if_fail (addr != NULL, retval);
+  g_return_val_if_fail (field_name != NULL, retval);
+
   value_copy = g_strdup (value);
 
-  /* FIXME: should we free any existing non-NULL value first? */
+  /* inverting standard retval logic to simplify the remaining code */
+  retval = TRUE;
 
+  /* FIXME: should we free any existing non-NULL value first? */
   if (g_str_equal (field_name, "street"))
     {
       addr->street = value_copy;
@@ -189,32 +269,40 @@
   else
     {
       g_warning ("unknown address field '%s'", field_name);
+
+      retval = FALSE;
       g_free (value_copy);
     }
+
+  return retval;
 }
 
-/* Determine whether an EContactAddress is empty */ 
+/* Return TRUE if an EContactAddress is completely empty, FALSE otherwise. */
 gboolean 
 e_contact_address_is_empty (EContactAddress *addr) 
 { 
-  if (!addr) 
-    {
-      return TRUE; 
-    }
- 
-  if ((!addr->address_format || g_str_equal (addr->address_format, "")) 
-       && (!addr->po         || g_str_equal (addr->po, "")) 
-       && (!addr->ext        || g_str_equal (addr->ext, "")) 
-       && (!addr->street     || g_str_equal (addr->street, "")) 
-       && (!addr->locality   || g_str_equal (addr->locality, "")) 
-       && (!addr->region     || g_str_equal (addr->region, "")) 
-       && (!addr->code       || g_str_equal (addr->code, "")) 
-       && (!addr->country    || g_str_equal (addr->country, ""))) 
+  gboolean retval = TRUE;
+
+  g_return_val_if_fail (addr != NULL, retval);
+
+  if (TRUE
+      && STRING_NULL_OR_EMPTY (addr->address_format) 
+      && STRING_NULL_OR_EMPTY (addr->po) 
+      && STRING_NULL_OR_EMPTY (addr->ext) 
+      && STRING_NULL_OR_EMPTY (addr->street) 
+      && STRING_NULL_OR_EMPTY (addr->locality) 
+      && STRING_NULL_OR_EMPTY (addr->region) 
+      && STRING_NULL_OR_EMPTY (addr->code) 
+      && STRING_NULL_OR_EMPTY (addr->country)) 
     { 
-      return TRUE; 
+      retval = TRUE; 
     } 
+  else
+    {
+      retval = FALSE;
+    }
  
-  return FALSE; 
+  return retval; 
 }
 
 /* FIXME: this is US-specific */
@@ -222,14 +310,13 @@
 gchar*
 e_contact_address_to_label (EContactAddress *addr)
 {
+  gchar *retval = NULL;
   GString *label = NULL;
   
+  g_return_val_if_fail (addr != NULL, retval);
   /* An empty address should get an empty label */
-  if (e_contact_address_is_empty (addr))
-    {
-      return NULL;
-    } 
-    
+  g_return_val_if_fail (!e_contact_address_is_empty (addr), retval);
+
   label = g_string_new (addr->street);
   
   if (!g_str_equal (addr->po, ""))
@@ -239,6 +326,8 @@
     
   g_string_append_printf (label, "\n%s, %s %s\n%s", addr->locality,
                           addr->region, addr->code, addr->country);
+
+  retval = g_string_free (label, FALSE);
                           
-  return g_string_free (label, FALSE);
+  return retval;
 }

Modified: trunk/src/eds-utils.h
==============================================================================
--- trunk/src/eds-utils.h	(original)
+++ trunk/src/eds-utils.h	Mon Jan  7 01:52:18 2008
@@ -35,7 +35,7 @@
 const gchar* display_name_from_e_contact (EContact *e_contact);
 
 /* Editting existing VCard/EContact details */
-void     e_contact_address_set_field (EContactAddress *addr,
+gboolean e_contact_address_set_field (EContactAddress *addr,
                                       const gchar *field_name,
                                       const gchar *value);
 gboolean e_contact_address_is_empty  (EContactAddress *addr);

Modified: trunk/src/soylent-browser-person-view.c
==============================================================================
--- trunk/src/soylent-browser-person-view.c	(original)
+++ trunk/src/soylent-browser-person-view.c	Mon Jan  7 01:52:18 2008
@@ -2062,17 +2062,16 @@
       icon_height_max = soylent_browser_get_icon_height_max (browser);
       icon_width_max  = soylent_browser_get_icon_width_max  (browser);
 
-      if (icon_height_max > 0 && icon_width_max > 0)
+      pixbuf = gdk_pixbuf_from_inline_photo (photo, icon_width_max,
+                                              icon_height_max);
+      if (pixbuf)
         {
-          pixbuf = gdk_pixbuf_from_inline_photo (photo, icon_width_max,
-                                                 icon_height_max);
           gtk_image_set_from_pixbuf (img_person_view, pixbuf);
           gtk_widget_show (GTK_WIDGET (img_person_view));
         }
       else
         {
-          g_warning ("invalid icon max width (%u) and/or height (%u)",
-                     icon_width_max, icon_height_max);
+          g_warning ("failed to get a person's avatar");
         }
     }
   else

Modified: trunk/src/soylent-utils.h
==============================================================================
--- trunk/src/soylent-utils.h	(original)
+++ trunk/src/soylent-utils.h	Mon Jan  7 01:52:18 2008
@@ -28,4 +28,6 @@
 #  define soylent_debug(format, ...)
 #endif
 
+#define STRING_NULL_OR_EMPTY(x) ((!(x)) || (g_str_equal ((x), "")))
+
 #endif /* _SOYLENT_UTILS_H_ */



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