soylent r52 - trunk/src



Author: treitter
Date: Tue Jan 22 07:27:46 2008
New Revision: 52
URL: http://svn.gnome.org/viewvc/soylent?rev=52&view=rev

Log:
Clean up more soylent-browser.c functions; pushed some logic into soylent-person.c for better encapsulation and functionality exposure

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

Modified: trunk/src/soylent-browser.c
==============================================================================
--- trunk/src/soylent-browser.c	(original)
+++ trunk/src/soylent-browser.c	Tue Jan 22 07:27:46 2008
@@ -64,11 +64,16 @@
                                       gpointer user_data);
 
 static SoylentPerson* soylent_browser_live_contact_find_owner_person 
-                                                      (SoylentBrowser *browser,
-                                              EmpathyContact *empathy_contact);
-static gboolean soylent_browser_associate_live_contact (SoylentBrowser *browser,
-                                                        SoylentPerson *person,
+                                             (SoylentBrowser *browser,
                                               EmpathyContact *empathy_contact);
+static gboolean soylent_browser_associate_live_contact
+                                            (SoylentBrowser *browser,
+                                             SoylentPerson *person,
+                                             EmpathyContact *empathy_contact);
+static gboolean soylent_browser_dissociate_live_contact
+                                              (SoylentBrowser *browser,
+                                               SoylentPerson *person,
+                                               EmpathyContact *empathy_contact);
 
 static gboolean soylent_browser_widget_setup (SoylentBrowser *browser);
 static gboolean soylent_browser_ebook_setup (SoylentBrowser *browser);
@@ -1263,10 +1268,20 @@
   SoylentBrowser *browser = NULL;
 
   browser = (SoylentBrowser*) user_data;
+  /* FIXME: once SoylentBrowser is a GObject, also check SOYLENT_IS_BROWSER */
+  if (browser)
+    {
+      gboolean save_retval = FALSE;
 
-  /* FIXME: use the main window's "delete-event" to make sure that we save any
-   * unsaved changes upon main window deletion */
-  soylent_browser_person_view_save_finalize_cb (NULL, NULL, browser);
+      /* use the main window's "delete-event" to make sure that we save any
+       * unsaved changes upon main window deletion */
+      save_retval = soylent_browser_person_view_save_finalize_cb (NULL, NULL,
+                                                                  browser);
+      if (!save_retval)
+        {
+          g_warning ("failed to save changes to person");
+        }
+    }
 
   gtk_main_quit();
 }
@@ -1283,23 +1298,28 @@
                                          SoylentBrowser *browser)
 {
   gboolean retval = FALSE;
-  SoylentPerson *person;
+  SoylentPerson *person = NULL;
 
   person = soylent_browser_live_contact_find_owner_person (browser,
                                                            empathy_contact);
 
-  if (person)
-    {
-      if (is_member)
+  /* If we are being notified this contact is a new member to the roster, hook
+   * up signal callbacks and attach the EmpathyContact to the SoylentPerson */
+  if (is_member)
+    {
+      /* If we know a SoylentPerson who matches this contact, hook up
+       * notification signals (otherwise, wait until another signal would cause
+       * the association to hook up these Empathy notification signals (this
+       * saves us some effort in the meantime)) */
+      if (person)
         {
           g_signal_connect (empathy_contact, "notify::groups",
                             G_CALLBACK (soylent_person_live_groups_updated_cb),
                             (gpointer) person);
-
           g_signal_connect
-                          (empathy_contact, "notify::presence",
-                           G_CALLBACK (soylent_person_live_presence_updated_cb),
-                           (gpointer) person);
+                        (empathy_contact, "notify::presence",
+                         G_CALLBACK (soylent_person_live_presence_updated_cb),
+                         (gpointer) person);
           g_signal_connect (empathy_contact, "notify::name",
                             G_CALLBACK (soylent_person_live_name_updated_cb),
                             (gpointer) person);
@@ -1309,37 +1329,43 @@
           g_signal_connect (empathy_contact, "notify::type",
                             G_CALLBACK (soylent_person_live_type_updated_cb),
                             (gpointer) person);
-
-          soylent_browser_associate_live_contact (browser, person,
-                                                  empathy_contact);
         }
-      else
-        {
-          g_signal_handlers_disconnect_by_func
+
+      /* this handles NULL person pointers (adds the contact to a browser-wide
+       * pool) */
+      retval = soylent_browser_associate_live_contact (browser, person,
+                                                       empathy_contact);
+    }
+  /* Otherwise, this EmpathyContact was removed from our roster; drop them from
+   * our own structures */
+  else
+    {
+      g_signal_handlers_disconnect_by_func
+                      (empathy_contact,
+                        G_CALLBACK (soylent_person_live_groups_updated_cb),
+                        (gpointer) person);
+      g_signal_handlers_disconnect_by_func
+                      (empathy_contact,
+                        G_CALLBACK (soylent_person_live_presence_updated_cb),
+                        (gpointer) person);
+      g_signal_handlers_disconnect_by_func
                           (empathy_contact,
-                           G_CALLBACK (soylent_person_live_groups_updated_cb),
-                           (gpointer) person);
-          g_signal_handlers_disconnect_by_func
+                          G_CALLBACK (soylent_person_live_name_updated_cb),
+                          (gpointer) person);
+      g_signal_handlers_disconnect_by_func
                         (empathy_contact,
-                         G_CALLBACK (soylent_person_live_presence_updated_cb),
-                         (gpointer) person);
-          g_signal_handlers_disconnect_by_func
-                            (empathy_contact,
-                             G_CALLBACK (soylent_person_live_name_updated_cb),
-                             (gpointer) person);
-          g_signal_handlers_disconnect_by_func
+                        G_CALLBACK (soylent_person_live_avatar_updated_cb),
+                        (gpointer) person);
+      g_signal_handlers_disconnect_by_func
                           (empathy_contact,
-                           G_CALLBACK (soylent_person_live_avatar_updated_cb),
-                           (gpointer) person);
-          g_signal_handlers_disconnect_by_func
-                            (empathy_contact,
-                             G_CALLBACK (soylent_person_live_type_updated_cb),
-                             (gpointer) person);
-        }
-    }
+                          G_CALLBACK (soylent_person_live_type_updated_cb),
+                          (gpointer) person);
 
-  /* FIXME: actually set this above */
-  retval = TRUE;
+      /* this handles NULL person pointers (adds the contact to a browser-wide
+       * pool) */
+      retval = soylent_browser_dissociate_live_contact (browser, person,
+                                                        empathy_contact);
+    }
 
   return retval;
 }
@@ -1493,7 +1519,9 @@
 }
 
 /* Add the given EmpathyContact to the given SoylentPerson in the given
- * SoylentBrowser */
+ * SoylentBrowser
+ *
+ * Return TRUE for success, FALSE for any failure. */
 static gboolean
 soylent_browser_associate_live_contact (SoylentBrowser *browser,
                                         SoylentPerson *person,
@@ -1501,38 +1529,45 @@
 {
   gboolean retval = FALSE;
 
+  /* FIXME: once SoylentPerson is a GObject, check SOYLENT_IS_PERSON */
   if (person)
     {
-      GList *live_contacts = NULL;
-      gboolean set_success = FALSE;
+      retval = soylent_person_add_live_contact (person, empathy_contact);
+    }
+  else
+    {
+      browser->live_contacts_pending = g_list_prepend
+                                                (browser->live_contacts_pending,
+                                                 empathy_contact);
+      retval = TRUE;
+    }
 
-      live_contacts = soylent_person_get_live_contacts (person);
-      live_contacts = g_list_prepend (live_contacts, empathy_contact);
+  return retval;
+}
 
-      set_success = soylent_person_set_live_contacts (person, live_contacts);
-      if (set_success)
-        {
-          gboolean update_presence_retval = FALSE;
+/* Remove the given EmpathyContact from the given SoylentPerson in the given
+ * SoylentBrowser
+ *
+ * Return TRUE for success, FALSE for any failure. */
+static gboolean
+soylent_browser_dissociate_live_contact (SoylentBrowser *browser,
+                                         SoylentPerson *person,
+                                         EmpathyContact *empathy_contact)
+{
+  gboolean retval = FALSE;
 
-          update_presence_retval = soylent_person_live_presence_updated_cb
-                                                              (empathy_contact,
-                                                               NULL, person);
-          if (update_presence_retval)
-            {
-              retval = TRUE;
-            }
-          else
-            {
-              g_warning ("failed to update the online presence of a person");
-            }
-        }
+  /* FIXME: once SoylentPerson is a GObject, check SOYLENT_IS_PERSON */
+  if (person)
+    {
+      retval = soylent_person_remove_live_contact (person, empathy_contact);
     }
   else
     {
-      soylent_debug ("adding a contact to the 'contacts_pending' list");
-      browser->live_contacts_pending = g_list_prepend
+      browser->live_contacts_pending = g_list_remove_all
                                                 (browser->live_contacts_pending,
                                                  empathy_contact);
+
+      retval = TRUE;
     }
 
   return retval;

Modified: trunk/src/soylent-person.c
==============================================================================
--- trunk/src/soylent-person.c	(original)
+++ trunk/src/soylent-person.c	Tue Jan 22 07:27:46 2008
@@ -50,33 +50,33 @@
 SoylentPerson*
 soylent_person_new (SoylentBrowser *browser, EContact *e_contact)
 {
-  SoylentPerson *person = NULL;
+  SoylentPerson *retval = NULL;
 
-  g_return_val_if_fail (browser != NULL, NULL);
+  g_return_val_if_fail (browser != NULL, retval);
   /* FIXME: uncomment once SoylentBrowser is a GObject:
-  g_return_val_if_fail (SOYLENT_IS_BROWSER (browser), NULL);
+  g_return_val_if_fail (SOYLENT_IS_BROWSER (browser), retval);
    * */
-  g_return_val_if_fail (e_contact != NULL, NULL);
-  g_return_val_if_fail (E_IS_CONTACT (e_contact), NULL);
+  g_return_val_if_fail (e_contact != NULL, retval);
+  g_return_val_if_fail (E_IS_CONTACT (e_contact), retval);
 
-  person = g_new (SoylentPerson, 1);
-  if (person)
+  retval = g_new (SoylentPerson, 1);
+  if (retval)
     {
-      person->iter = g_new0 (GtkTreeIter, 1);
-      if (person->iter)
+      retval->iter = g_new0 (GtkTreeIter, 1);
+      if (retval->iter)
         {
-          person->iter->stamp = SP_ITER_UNSET;
-          person->e_contact = g_object_ref (e_contact);
-          person->presence = SP_PRESENCE_UNSET;
-          person->live_contacts = NULL;
-          person->browser = browser;
+          retval->iter->stamp = SP_ITER_UNSET;
+          retval->e_contact = g_object_ref (e_contact);
+          retval->presence = SP_PRESENCE_UNSET;
+          retval->live_contacts = NULL;
+          retval->browser = browser;
         }
       else
         {
           /* error: out of memory */
 
-          g_free (person);
-          person = NULL;
+          g_free (retval);
+          retval = NULL;
         }
     }
   else
@@ -84,7 +84,7 @@
       /* error: out of memory */
     }
 
-  return person;
+  return retval;
 }
 
 EContact*
@@ -159,7 +159,7 @@
   
   g_return_val_if_fail (person != NULL, retval);
   /* FIXME: uncomment once SoylentPerson is a GObject:
-  g_return_val_if_fail (SOYLENT_IS_PERSON (person), NULL);
+  g_return_val_if_fail (SOYLENT_IS_PERSON (person), retval);
    */
 
   e_contact = soylent_person_get_e_contact (person);
@@ -222,12 +222,12 @@
 {
   gboolean retval = FALSE;
 
-  g_return_val_if_fail (person != NULL, FALSE);
+  g_return_val_if_fail (person != NULL, retval);
   /* FIXME: uncomment once SoylentPerson is a GObject:
   g_return_val_if_fail (SOYLENT_IS_PERSON (person), NULL);
    * */
-  g_return_val_if_fail (e_contact != NULL, FALSE);
-  g_return_val_if_fail (E_IS_CONTACT (e_contact), FALSE);
+  g_return_val_if_fail (e_contact != NULL, retval);
+  g_return_val_if_fail (E_IS_CONTACT (e_contact), retval);
 
   if (person->e_contact)
     {
@@ -249,11 +249,11 @@
 {
   gboolean retval = FALSE;
 
-  g_return_val_if_fail (person != NULL, FALSE);
+  g_return_val_if_fail (person != NULL, retval);
   /* FIXME: uncomment once SoylentPerson is a GObject:
-  g_return_val_if_fail (SOYLENT_IS_PERSON (person), NULL);
+  g_return_val_if_fail (SOYLENT_IS_PERSON (person), retval);
    * */
-  g_return_val_if_fail (live_contacts != NULL, FALSE);
+  g_return_val_if_fail (live_contacts != NULL, retval);
 
   person->live_contacts = live_contacts;
 
@@ -263,6 +263,78 @@
   return retval;
 }
 
+/* Returns TRUE upon success, FALSE otherwise */
+gboolean
+soylent_person_add_live_contact (SoylentPerson *person,
+                                 EmpathyContact *empathy_contact)
+{
+  gboolean retval = FALSE;
+  GList *live_contact_existing = NULL;
+  gboolean update_presence_retval = FALSE;
+
+  g_return_val_if_fail (person != NULL, retval);
+  /* FIXME: uncomment once SoylentPerson is a GObject:
+  g_return_val_if_fail (SOYLENT_IS_PERSON (person), retval);
+   * */
+  g_return_val_if_fail (empathy_contact != NULL, retval);
+  g_return_val_if_fail (EMPATHY_IS_CONTACT (empathy_contact), retval);
+
+  /* Only add the contact if they aren't already among our live contacts */
+  live_contact_existing = g_list_find (person->live_contacts, empathy_contact);
+  if (!live_contact_existing)
+    {
+      person->live_contacts = g_list_prepend (person->live_contacts,
+                                              empathy_contact);
+    }
+
+  update_presence_retval = soylent_person_live_presence_updated_cb
+                                                              (empathy_contact,
+                                                               NULL, person);
+  if (update_presence_retval)
+    {
+      retval = TRUE;
+    }
+  else
+    {
+      g_warning ("failed to update the online presence of a person");
+    }
+
+  return retval;
+}
+
+/* Returns TRUE upon success, FALSE otherwise */
+gboolean
+soylent_person_remove_live_contact (SoylentPerson *person,
+                                    EmpathyContact *empathy_contact)
+{
+  gboolean retval = FALSE;
+  gboolean update_presence_retval = FALSE;
+
+  g_return_val_if_fail (person != NULL, retval);
+  /* FIXME: uncomment once SoylentPerson is a GObject:
+  g_return_val_if_fail (SOYLENT_IS_PERSON (person), retval);
+   * */
+  g_return_val_if_fail (empathy_contact != NULL, retval);
+  g_return_val_if_fail (EMPATHY_IS_CONTACT (empathy_contact), retval);
+
+  person->live_contacts = g_list_remove_all (person->live_contacts,
+                                             empathy_contact);
+
+  update_presence_retval = soylent_person_live_presence_updated_cb
+                                                              (empathy_contact,
+                                                               NULL, person);
+  if (update_presence_retval)
+    {
+      retval = TRUE;
+    }
+  else
+    {
+      g_warning ("failed to update the online presence of a person");
+    }
+
+  return retval;
+}
+
 /* Returns:
  * < 0      if presence1 is less available than presence2
  * = 0      if presence1 is equally available as presence2

Modified: trunk/src/soylent-person.h
==============================================================================
--- trunk/src/soylent-person.h	(original)
+++ trunk/src/soylent-person.h	Tue Jan 22 07:27:46 2008
@@ -53,20 +53,23 @@
 #include "soylent-browser.h"
 #include "soylent-defs.h"
 
-SoylentPerson*  soylent_person_new (SoylentBrowser *sb, EContact *e_contact);
-EContact*       soylent_person_get_e_contact      (SoylentPerson *person);
-GList*          soylent_person_get_live_contacts  (SoylentPerson *person);
-SoylentPresence soylent_person_get_presence       (SoylentPerson *person);
-GtkTreeIter*    soylent_person_get_icon_view_iter (SoylentPerson *person);
-SoylentBrowser* soylent_person_get_browser        (SoylentPerson *person);
-GdkPixbuf*      soylent_person_get_avatar         (SoylentPerson *person,
-                                                   guint icon_width_max,
-                                                   guint icon_height_max);
-gboolean        soylent_person_set_e_contact      (SoylentPerson *person,
-                                                   EContact *e_contact);
-gboolean        soylent_person_set_live_contacts  (SoylentPerson *person,
-                                                   GList *live_contacts);
-
+SoylentPerson* soylent_person_new (SoylentBrowser *sb, EContact *e_contact);
+EContact* soylent_person_get_e_contact (SoylentPerson *person);
+GList* soylent_person_get_live_contacts (SoylentPerson *person);
+SoylentPresence soylent_person_get_presence (SoylentPerson *person);
+GtkTreeIter* soylent_person_get_icon_view_iter (SoylentPerson *person);
+SoylentBrowser* soylent_person_get_browser (SoylentPerson *person);
+GdkPixbuf* soylent_person_get_avatar (SoylentPerson *person,
+                                      guint icon_width_max,
+                                      guint icon_height_max);
+gboolean soylent_person_set_e_contact (SoylentPerson *person,
+                                       EContact *e_contact);
+gboolean soylent_person_set_live_contacts (SoylentPerson *person,
+                                           GList *live_contacts);
+gboolean soylent_person_add_live_contact (SoylentPerson *person,
+                                          EmpathyContact *live_contact);
+gboolean soylent_person_remove_live_contact (SoylentPerson *person,
+                                             EmpathyContact *live_contact);
 
 /* FIXME: these should (almost?) all be static */
 gchar* get_file_as_name (const gchar *full_name);



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