soylent r81 - trunk/src



Author: treitter
Date: Thu Feb 14 06:42:57 2008
New Revision: 81
URL: http://svn.gnome.org/viewvc/soylent?rev=81&view=rev

Log:
clean up some functions in soylent-browser-person-view.c

Modified:
   trunk/src/soylent-browser-person-view.c
   trunk/src/soylent-utils.c
   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	Thu Feb 14 06:42:57 2008
@@ -30,8 +30,8 @@
 
 static gboolean soylent_browser_person_view_editor_hide
                                                       (SoylentBrowser *browser);
-static void soylent_browser_person_view_save_changes (GtkWidget *widget,
-                                                      gpointer user_data);
+static gboolean soylent_browser_person_view_save_changes (GtkWidget *widget,
+                                                          gpointer user_data);
 static gboolean soylent_browser_person_edit_save_scroll_cb
                                                       (EBookView *book_view,
                                                        const GList *e_contacts,
@@ -101,8 +101,9 @@
 static void soylent_browser_person_view_web_update (SoylentBrowser *browser,
                                                     SoylentPerson *person);
 
-/* FIXME: call soylent_browser_view_save_changes instead of duplicating code */
-/* Save unsaved changes to the person's details and return to Browse Mode */
+/* Save unsaved changes to the person's details and return to Browse Mode
+ *
+ * Return TRUE for success, FALSE for any failure. */
 gboolean
 soylent_browser_person_view_save_finalize_cb (GtkWidget *widget,
                                               GdkEvent *event,
@@ -110,105 +111,42 @@
 {
   gboolean retval = FALSE;
   SoylentBrowser *browser = NULL;
-  GladeXML *wtree = NULL;
+  EBookView *e_book_view = NULL;
 
   g_return_val_if_fail (user_data != NULL, retval);
 
-  /* FIXME: check these return values for sanity */
-  /* FIXME: when SoylentBrowser is a GObject, check SOYLENT_IS_BROWSER (browser)
-   * */
+  /* FIXME: when SoylentBrowser is a GObject, check SOYLENT_IS_BROWSER */
   browser = (SoylentBrowser*) user_data;
 
-  wtree = soylent_browser_get_widget_tree (browser);
-  if (wtree && GLADE_IS_XML (wtree))
+  e_book_view = soylent_browser_get_e_book_view (browser);
+  if (e_book_view)
     {
-      GHashTable *edits_pending = NULL;
-
-      edits_pending = soylent_browser_get_edits_pending (browser);
-      if (edits_pending)
-        {
-          EContact *e_contact = NULL;
-
-          g_hash_table_foreach_remove
-                            (edits_pending,
-                             (GHRFunc) person_apply_edits_from_widgets_switch,
-                             browser);
-
-          e_contact = soylent_browser_get_selected_person_e_contact (browser);
-          if (e_contact)
-            {
-              EBook *e_book = NULL;
-
-              e_book = soylent_browser_get_e_book (browser);
-              if (e_book)
-                {
-                  EBookView *e_book_view = NULL;
-
-                  e_book_view = soylent_browser_get_e_book_view (browser);
-                  if (e_book_view)
-                    {
-                      SoylentPerson *person = NULL;
-
-                      person = soylent_browser_get_person_from_e_contact
-                                                                    (browser,
-                                                                     e_contact);
-                      if (person)
-                        {
-                          gboolean commit_retval = FALSE;
-                          GError *error = NULL;
-
-                          /* Disconnected in callback */
-                          g_signal_connect_after
-                              (e_book_view, "contacts_changed",
-                        G_CALLBACK (soylent_browser_person_edit_save_scroll_cb),
-                               person);
-
-                          commit_retval = e_book_commit_contact (e_book,
-                                                                 e_contact,
-                                                                 &error);
-                          if (commit_retval)
-                            {
-                              retval = TRUE;
-                            }
-                          else
-                            {
-                              g_warning ("Could not commit changes to address "
-                                         "book: %s",
-                                          error ? error->message
-                                                : "no error given");
-                              g_clear_error (&error);
-                            }
-                        }
-                      else
-                        {
-                          g_warning ("failed to get SoylentPerson from "
-                                     "EContact");
-                        }
-                    }
-                  else
-                    {
-                      g_critical ("failed to get the EBookView for the "
-                                  "SoylentBrowser");
-                    }
-                }
-              else
-                {
-                  g_critical ("failed to get EBook for the SoylentBrowser");
-                }
-            }
-          else
+      SoylentPerson *person = NULL;
+      
+      /* FIXME: when SoylentPerson is a GObject, check SOYLENT_IS_PERSON */
+      person = soylent_browser_get_selected_person (browser);
+      if (person)
+        {
+          gint handler_id = -1;
+
+          /* Disconnected in callback */
+          handler_id = g_signal_connect_after
+                      (e_book_view, "contacts_changed",
+                       G_CALLBACK (soylent_browser_person_edit_save_scroll_cb),
+                       person);
+          if (handler_id >= G_SIGNAL_HANDLER_MIN_VALID)
             {
-              g_warning ("failed to get EContact for the person");
+              retval = soylent_browser_person_view_save_changes (NULL, browser);
             }
         }
       else
         {
-          g_critical ("failed to get the browser's pending edits hash");
+          g_warning ("failed to get the selected person");
         }
     }
   else
     {
-      g_critical ("SoylentBrowser's widget tree is invalid");
+      g_critical ("failed to get the EBookView for the SoylentBrowser");
     }
 
   soylent_browser_person_view_editor_hide (browser);
@@ -216,86 +154,121 @@
   return retval;
 }
 
-/* Save unsaved changes to the person's details and return to Browse Mode */
-
-/* Handle clicks on the Delete button in the Browse view */
+/* Handle clicks on the Delete button in the Browse view
+ *
+ * Return TRUE for success, FALSE for any failure. */
 gboolean
 soylent_browser_person_action_delete_selected_finalize_cb (GtkButton *btn,
                                                            gpointer user_data)
 {
+  gboolean retval = FALSE;
   SoylentBrowser *browser = NULL;
   GladeXML *wtree = NULL;
-  GError *error = NULL;
   const GList *i = NULL;
-  GtkWidget *dia_delete_person_confirm = NULL;
   GList *e_uids = NULL;
 
-  browser = (SoylentBrowser *) user_data;
-  wtree = soylent_browser_get_widget_tree (browser);
+  g_return_val_if_fail (user_data != NULL, retval);
+  /* FIXME: when SoylentBrowser is a GObject, check SOYLENT_IS_BROWSER */
+  browser = (SoylentBrowser*) user_data;
+
+  /* invert usual logic to simplify the loop below */
+  retval = TRUE;
 
   e_uids = soylent_browser_get_selected_people_e_uid (browser);
   for (i = (GList*) e_uids; i; i = g_list_next (i))
     {
       EBook *e_book = NULL;
       gchar *e_uid = NULL;
+      GError *error = NULL;
 
       e_book = soylent_browser_get_e_book (browser);
       e_uid = (gchar*) i->data;
 
-      if (!e_uid)
+      if (e_uid)
         {
-          g_warning ("list of selected people contains a NULL UID\n");
-          continue;
-        }
+          if (!e_book_remove_contact (e_book, (const gchar*) e_uid, &error))
+            {
+              g_warning ("Failed to remove contact: %s", error->message);
+              retval = FALSE;
+            } 
 
-      if (!e_book_remove_contact (e_book, (const gchar*) e_uid, &error))
+          if (error)
+            {
+              g_error_free (error);
+            }
+          g_free (e_uid);
+        }
+      else
         {
-          g_warning ("Failed to remove contact: %s", error->message);
-        } 
-
-      g_free (e_uid);
+          g_warning ("list of selected people contains a NULL UID");
+        }
     }
 
-  if (e_uids)
+  /* Hide the "Delete person?" dialog whether or not we actually deleted them;
+   * it shouldn't stick around (especially since it's modal) */
+  wtree = soylent_browser_get_widget_tree (browser);
+  if (wtree && GLADE_IS_XML (wtree))
     {
-      g_list_free (e_uids);
-    }
+      GtkWidget *dia_delete_person_confirm = NULL;
 
-  if (error)
+      dia_delete_person_confirm = glade_xml_get_widget
+                                                  (wtree,
+                                                   "dia_delete_person_confirm");
+      gtk_widget_hide (dia_delete_person_confirm);
+    }
+  else
     {
-      g_error_free (error);
+      g_critical ("failed to get 'Delete person?' dialog widget\n");
     }
 
-  dia_delete_person_confirm = glade_xml_get_widget (wtree,
-                                                  "dia_delete_person_confirm");
-  gtk_widget_hide (dia_delete_person_confirm);
+  g_list_free (e_uids);
 
-  return TRUE;
+  return retval;
 }
 
-/* Prompt "Confirm delete?" when we select one or more people and hit Delete */
+/* Prompt "Delete person?" when we select one or more people and hit Delete */
 gboolean
 soylent_browser_person_action_delete_selected_cb (GtkButton *btn,
                                                   gpointer user_data)
 {
+  gboolean retval = FALSE;
   SoylentBrowser *browser = NULL;
   GladeXML *wtree = NULL;
   GtkWidget *dia_delete_person_confirm = NULL;
 
-  browser = (SoylentBrowser *) user_data;
-  wtree = soylent_browser_get_widget_tree (browser);
+  g_return_val_if_fail (user_data != NULL, retval);
+  /* FIXME: when SoylentBrowser is a GObject, check SOYLENT_IS_BROWSER */
+  browser = (SoylentBrowser*) user_data;
 
-  dia_delete_person_confirm = glade_xml_get_widget (wtree,
-                                                  "dia_delete_person_confirm");
-  gtk_widget_show (dia_delete_person_confirm);
+  wtree = soylent_browser_get_widget_tree (browser);
+  if (wtree && GLADE_IS_XML (wtree))
+    {
+      dia_delete_person_confirm = glade_xml_get_widget
+                                                (wtree,
+                                                 "dia_delete_person_confirm");
+      if (dia_delete_person_confirm
+          && GTK_IS_DIALOG (dia_delete_person_confirm))
+        {
+          gtk_widget_show (dia_delete_person_confirm);
+        }
+      else
+        {
+          g_critical ("failed to get 'Delete person?' dialog widget");
+        }
+    }
+  else
+    {
+      g_critical ("SoylentBrowser's widget tree is invalid");
+    }
 
-  return TRUE;
+  return retval;
 }
 
 /* Hide the "Confirm delete?" window when we click the "Cancel" button */
 gboolean
-soylent_browser_person_action_delete_selected_hide_dialog_cb (GtkButton *btn,
-                                                            gpointer user_data)
+soylent_browser_person_action_delete_selected_hide_dialog_cb
+                                                          (GtkButton *btn,
+                                                           gpointer user_data)
 {
   SoylentBrowser *browser = NULL;
   GladeXML *wtree = NULL;
@@ -334,7 +307,7 @@
           }
         else
           {
-            g_warning ("attempted to edit a Person with a NULL e_contact\n");
+            g_warning ("attempted to edit a Person with a NULL e_contact");
           }
 
         if (e_uid)
@@ -361,7 +334,7 @@
           }
         else
           {
-            g_warning ("e-d-s doesn't know about this person\n");
+            g_warning ("e-d-s doesn't know about this person");
           }
       }
 #else
@@ -381,7 +354,7 @@
     }
   else
     {
-      g_warning ("Couldn't find the selected person(s) (!)\n");
+      g_warning ("Couldn't find the selected person(s) (!)");
     }
 
   return retval;
@@ -1047,31 +1020,95 @@
   return retval;
 }
 
-/* Save unsaved changes to the person's details */
-static void
+/* Save unsaved changes to the person's details
+ *
+ * Return TRUE for success, FALSE for any failure. */
+static gboolean
 soylent_browser_person_view_save_changes (GtkWidget *widget, gpointer user_data)
 {
+  gboolean retval = FALSE;
   SoylentBrowser *browser = NULL;
-  EBook *e_book = NULL;
   GHashTable *edits_pending = NULL;
-  EContact *e_contact = NULL;
-  GError *error = NULL;
 
+  g_return_val_if_fail (user_data != NULL, retval);
+  /* FIXME: when SoylentBrowser is a GObject, check SOYLENT_IS_BROWSER */
   browser = (SoylentBrowser*) user_data;
-  e_book = soylent_browser_get_e_book (browser);
+
   edits_pending = soylent_browser_get_edits_pending (browser);
-  g_hash_table_foreach_remove (edits_pending,
+  if (edits_pending)
+    {
+      EContact *e_contact = NULL;
+
+      g_hash_table_foreach_remove
+                              (edits_pending,
                                (GHRFunc) person_apply_edits_from_widgets_switch,
                                browser);
 
-  e_contact = soylent_browser_get_selected_person_e_contact (browser);
+      e_contact = soylent_browser_get_selected_person_e_contact (browser);
+      if (e_contact)
+        {
+          EBook *e_book = NULL;
 
-  if (e_book_commit_contact (e_book, e_contact, &error) == FALSE)
+          e_book = soylent_browser_get_e_book (browser);
+          if (e_book)
+            {
+              EBookView *e_book_view = NULL;
+
+              e_book_view = soylent_browser_get_e_book_view (browser);
+              if (e_book_view)
+                {
+                  SoylentPerson *person = NULL;
+
+                  person = soylent_browser_get_person_from_e_contact
+                                                                (browser,
+                                                                  e_contact);
+                  if (person)
+                    {
+                      gboolean commit_retval = FALSE;
+                      GError *error = NULL;
+
+                      commit_retval = e_book_commit_contact (e_book,
+                                                              e_contact,
+                                                              &error);
+                      if (commit_retval)
+                        {
+                          retval = TRUE;
+                        }
+                      else
+                        {
+                          g_warning ("Could not commit changes to address "
+                                     "book: %s",
+                                     error ? error->message : "no error given");
+                          g_clear_error (&error);
+                        }
+                    }
+                  else
+                    {
+                      g_warning ("failed to get SoylentPerson from EContact");
+                    }
+                }
+              else
+                {
+                  g_critical ("failed to get the EBookView for the "
+                              "SoylentBrowser");
+                }
+            }
+          else
+            {
+              g_critical ("failed to get EBook for the SoylentBrowser");
+            }
+        }
+      else
+        {
+          g_warning ("failed to get EContact for the person");
+        }
+    }
+  else
     {
-      g_warning ("Could not commit changes to address book, %s", 
-                  error ? error->message : "no error given");
-      g_clear_error (&error);
+      g_critical ("failed to get the browser's pending edits hash");
     }
+
+  return retval;
 }
 
 static gboolean

Modified: trunk/src/soylent-utils.c
==============================================================================
--- trunk/src/soylent-utils.c	(original)
+++ trunk/src/soylent-utils.c	Thu Feb 14 06:42:57 2008
@@ -25,8 +25,6 @@
 #include "soylent-defs.h"
 #include "soylent-utils.h"
 
-#define G_SIGNAL_HANDLER_MIN_VALID 1
-
 /* Retrieve the named widget from the wtree, then attach the callback and
  * user_data to the widget's given signal. If the widget is a GtkTextView, the
  * callback is instead attached to the given signal for its GtkTextBuffer

Modified: trunk/src/soylent-utils.h
==============================================================================
--- trunk/src/soylent-utils.h	(original)
+++ trunk/src/soylent-utils.h	Thu Feb 14 06:42:57 2008
@@ -22,6 +22,8 @@
 
 #include <glib.h>
 
+#define G_SIGNAL_HANDLER_MIN_VALID 1
+
 #if BUILD_DEV
 #  define soylent_debug(format, a...) g_debug(format, ##a)
 #else



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