soylent r104 - trunk/src



Author: treitter
Date: Sat Mar 15 22:56:36 2008
New Revision: 104
URL: http://svn.gnome.org/viewvc/soylent?rev=104&view=rev

Log:
split up soylent_browser_person_set_field_from_widget()

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

Modified: trunk/src/soylent-browser-person-view.c
==============================================================================
--- trunk/src/soylent-browser-person-view.c	(original)
+++ trunk/src/soylent-browser-person-view.c	Sat Mar 15 22:56:36 2008
@@ -75,6 +75,13 @@
                                                        EContact* e_contact,
                                                        const gchar* widget_name,
                                                        const gchar* value);
+static gboolean soylent_browser_person_set_field_email (EContact *e_contact,
+                                                        guint email_pos,
+                                                        gchar *contents_new);
+static gboolean soylent_browser_person_set_field_simple
+                                                (EContact *e_contact,
+                                                 EContactField e_contact_field,
+                                                 const gchar *contents_new);
 static gchar* soylent_browser_person_detail_add_get_cbox_val
                                                       (SoylentBrowser *browser,
                                                        const gchar *cbox_name);
@@ -1823,29 +1830,37 @@
 
 /* Save edits to the current person's existing details
  *
- * Return TRUE for success, FALSE for any failure. */
+ * Return TRUE unconditionally (to signify "remove this key/value" when used in
+ * g_hash_table_foreach_remove). */
 static gboolean
 soylent_browser_person_apply_edits_from_widgets_switch (gpointer key,
                                                         gpointer value,
                                                         gpointer user_data)
 {
-  gboolean ret_value = FALSE;
+  gboolean retval = TRUE;
+  GladeXML *wtree = NULL;
   SoylentBrowser *browser = NULL;
-  EContact *e_contact = NULL;
 
-  browser = (SoylentBrowser *) user_data;
-  e_contact = soylent_browser_get_selected_person_e_contact (browser);
+  g_return_val_if_fail (key != NULL, retval);
+  g_return_val_if_fail (user_data != NULL, retval);
+  /* FIXME: uncomment once SoylentBrowser is a GObject:
+  g_return_val_if_fail (SOYLENT_IS_BROWSER (user_data), retval);
+   */
+ 
+  browser = (SoylentBrowser*) user_data;
+  wtree = soylent_browser_get_widget_tree (browser);
 
   if (g_ascii_strncasecmp (key, "mail", 4) == 0)
-  {
-    ret_value = soylent_browser_person_set_mail_fields_from_widgets (key, value,
-                                                                     user_data);
-  }
-
-  ret_value = soylent_browser_person_set_field_from_widget (key, value,
-                                                            user_data);
+    {
+      soylent_browser_person_set_mail_fields_from_widgets (key, value,
+                                                           user_data);
+    }
+  else
+    {
+      soylent_browser_person_set_field_from_widget (key, value, user_data);
+    }
 
-  return ret_value;
+  return retval;
 }
 
 /* Save a single-valued detail edit for the person being edited
@@ -1855,6 +1870,7 @@
 soylent_browser_person_set_field_from_widget (gpointer key, gpointer value,
                                               gpointer user_data)
 {
+  gboolean retval = FALSE;
   SoylentBrowser *browser = NULL;
   GladeXML *wtree = NULL;
   const gchar *widget_name = NULL;
@@ -1863,12 +1879,26 @@
   GtkWidget *widget = NULL;
   EContact *e_contact = NULL;
 
+  g_return_val_if_fail (key != NULL, retval);
+  g_return_val_if_fail (user_data != NULL, retval);
+  /* FIXME: uncomment once SoylentBrowser is a GObject:
+  g_return_val_if_fail (SOYLENT_IS_BROWSER (user_data), retval);
+   */
+
   browser = (SoylentBrowser*) user_data;
   wtree = soylent_browser_get_widget_tree (browser);
 
   widget_name = (const gchar*) key;
-  e_contact_field = *((EContactField*) value);
   widget = glade_xml_get_widget (wtree, widget_name); 
+  e_contact_field = *((EContactField*) value);
+
+  e_contact = soylent_browser_get_selected_person_e_contact (browser);
+
+  g_return_val_if_fail (widget != NULL, retval);
+  g_return_val_if_fail (GTK_IS_ENTRY (widget) || GTK_IS_TEXT_VIEW (widget),
+                        retval);
+  g_return_val_if_fail (e_contact != NULL, retval);
+  g_return_val_if_fail (E_IS_CONTACT (e_contact), retval);
 
   if (GTK_IS_ENTRY (widget))
     {
@@ -1878,110 +1908,40 @@
     {
       GtkTextBuffer *buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget));
       GtkTextIter start, end;
-      gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (buf), &start, &end);
-
-      contents_new = gtk_text_buffer_get_text (GTK_TEXT_BUFFER (buf), &start,
-                                               &end, FALSE);
-    }
-  else
-    {
-      g_warning ("expected a GtkEntry or GtkTextView; not saving contents");
-      return TRUE;
-    }
 
-  if (widget == NULL)
-    {
-      g_warning ("invalid widget (trying to save contents): %s", widget_name);
-      return TRUE;
+      buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget));
+      gtk_text_buffer_get_bounds (buf, &start, &end);
+      contents_new = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
     }
 
-  if (contents_new != NULL && *contents_new == '\0')
+  if (value && (*((EContactField*) value) == CB_DATA_FIELD_IM))
     {
-      contents_new = NULL;
-    }
-
-  e_contact = soylent_browser_get_selected_person_e_contact (browser);
-
-  if (e_contact == NULL)
-    {
-      g_warning ("Unable to identify selected person; not saving changes");
-      return TRUE;
-    }
-
-  /* 
-   * TODO: split this detection logic out into
-   * soylent_browser_person_apply_edits_from_widgets_switch()
-   */
-  if (e_contact_field == CB_DATA_FIELD_IM)
-    {
-      soylent_browser_person_set_im_field_e_vcard (browser, e_contact,
-                                                   widget_name, contents_new);
+      retval = soylent_browser_person_set_im_field_e_vcard (browser, e_contact,
+                                                            widget_name,
+                                                            contents_new);
     }
   else
     {
-      gint e_contact_email_delta = e_contact_field - E_CONTACT_EMAIL_1;
+      gint email_pos = -1;
 
-      /* FIXME: handle this as an evcard directly to avoid off-by-one bugs in
-       * e-d-s' e_contact_* implementation(s) */
-      /* FIXME: this is pretty hideous, but we'll be integrating Contacts soon
-       * enough... */
-      if ((e_contact_email_delta >= 0)
-          && (e_contact_email_delta < MAX_GUI_EMAIL))
+      email_pos = e_contact_field - E_CONTACT_EMAIL_1;
+      if ((email_pos >= 0) && (email_pos < MAX_GUI_EMAIL))
         {
-          GList *email_list_head = NULL;
-          GList *existing_email = NULL;
-
-          email_list_head = e_contact_get (e_contact, E_CONTACT_EMAIL);
-
-          if (email_list_head)
-            {
-              existing_email = g_list_nth (email_list_head,
-                                           e_contact_email_delta);
-
-              if (existing_email)
-                {
-                  /* Delete empty email addresses */
-                  if (!contents_new || g_str_equal (contents_new, ""))
-                    {
-                      email_list_head = g_list_delete_link (email_list_head,
-                                                            existing_email);
-                    }
-                  else
-                    {
-                      existing_email->data = contents_new;
-                    }
-
-                  e_contact_set (e_contact, E_CONTACT_EMAIL, email_list_head);
-                }
-            }
+          retval = soylent_browser_person_set_field_email (e_contact, email_pos,
+                                                           contents_new);
         }
-      else /* if not email address - few options left... */
+      else
         {
-          e_contact_set (e_contact, e_contact_field, (gpointer) contents_new);
-
-          if (e_contact_field == E_CONTACT_FULL_NAME)
-            {
-              gchar *name_file_as = NULL;
-              gchar *name_first = NULL;
-              gchar *name_last = NULL;
-
-              name_file_as = get_file_as_name (contents_new);
-              name_first = get_first_name (contents_new);
-              name_last = get_last_name (contents_new);
-
-              e_contact_set (e_contact, E_CONTACT_FILE_AS, name_file_as);
-              e_contact_set (e_contact, E_CONTACT_GIVEN_NAME, name_first);
-              e_contact_set (e_contact, E_CONTACT_FAMILY_NAME, name_last);
-              
-              g_free (name_file_as);
-              g_free (name_first);
-              g_free (name_last);
-            }
+          retval = soylent_browser_person_set_field_simple (e_contact,
+                                                            e_contact_field,
+                                                            contents_new);
         }
     }
 
-  if (GTK_IS_TEXT_VIEW (widget) && contents_new)
-    g_free (contents_new);
+  if (GTK_IS_TEXT_VIEW (widget))
+    {
+      g_free (contents_new);
+    }
 
   return TRUE;
 }
@@ -2151,6 +2111,91 @@
   return TRUE;
 }
 
+/* Save an email address for the person being edited
+ *
+ * Return TRUE for success, FALSE for any failure. */
+static gboolean
+soylent_browser_person_set_field_email (EContact *e_contact,
+                                        guint email_pos,
+                                        gchar *contents_new)
+{
+  gboolean retval = FALSE;
+  GList *email_list_head = NULL;
+
+  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 (email_pos >= 0, retval);
+  g_return_val_if_fail (email_pos < MAX_GUI_EMAIL, retval);
+
+  email_list_head = e_contact_get (e_contact, E_CONTACT_EMAIL);
+  if (email_list_head)
+    {
+      GList *existing_email = NULL;
+
+      existing_email = g_list_nth (email_list_head, email_pos);
+      if (existing_email)
+        {
+          /* Delete empty email addresses */
+          if (!contents_new || g_str_equal (contents_new, ""))
+            {
+              email_list_head = g_list_delete_link (email_list_head,
+                                                    existing_email);
+            }
+          else
+            {
+              existing_email->data = contents_new;
+            }
+
+          e_contact_set (e_contact, E_CONTACT_EMAIL, email_list_head);
+        }
+
+      retval = TRUE;
+    }
+
+  return retval;
+}
+
+/* Save a simple EContact field for the person being edited
+ *
+ * Return TRUE for success, FALSE for any failure. */
+static gboolean
+soylent_browser_person_set_field_simple (EContact *e_contact,
+                                         EContactField e_contact_field,
+                                         const gchar *contents_new)
+{
+  gboolean retval = 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 (e_contact_field >= E_CONTACT_FIELD_FIRST, retval);
+  g_return_val_if_fail (e_contact_field < E_CONTACT_FIELD_LAST, retval);
+
+  e_contact_set (e_contact, e_contact_field, (gpointer) contents_new);
+
+  if (e_contact_field == E_CONTACT_FULL_NAME)
+    {
+      gchar *name_file_as = NULL;
+      gchar *name_first = NULL;
+      gchar *name_last = NULL;
+
+      name_file_as = get_file_as_name (contents_new);
+      name_first = get_first_name (contents_new);
+      name_last = get_last_name (contents_new);
+
+      e_contact_set (e_contact, E_CONTACT_FILE_AS, name_file_as);
+      e_contact_set (e_contact, E_CONTACT_GIVEN_NAME, name_first);
+      e_contact_set (e_contact, E_CONTACT_FAMILY_NAME, name_last);
+      
+      g_free (name_file_as);
+      g_free (name_first);
+      g_free (name_last);
+    }
+
+  retval = TRUE;
+
+  return retval;
+}
+
 /* Update the entire Person View UI section */
 static void
 soylent_browser_person_view_update (SoylentBrowser *browser,

Modified: trunk/src/soylent-defs.h
==============================================================================
--- trunk/src/soylent-defs.h	(original)
+++ trunk/src/soylent-defs.h	Sat Mar 15 22:56:36 2008
@@ -47,6 +47,7 @@
 
 #define MAX_GUI_EMAIL 4
 
+/* Set to the first e-d-s IM field (hopefully won't change) */
 #define CB_DATA_FIELD_IM  E_CONTACT_IM_AIM
 /* TODO: put these in configure.ac */
 #define ICON_MAX_WIDTH  72

Modified: trunk/src/soylent-utils.h
==============================================================================
--- trunk/src/soylent-utils.h	(original)
+++ trunk/src/soylent-utils.h	Sat Mar 15 22:56:36 2008
@@ -32,6 +32,8 @@
 
 #define STRING_NULL_OR_EMPTY(x) ((!(x)) || (g_str_equal ((x), "")))
 
+#define SOYLENT_EMAIL_POS_FROM_E_CONTACT_FIELD (x) ((!(x)) || (g_str_equal ((x), "")))
+
 /* XXX: always assign this to a variable and use that variable, to avoid
  * unexpected signed-to-unsigned promotion of negative numbers (eg, a signed int
  * of -1 gets promoted to 4294967295U on 32-bit arches). */



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