soylent r74 - trunk/src



Author: treitter
Date: Mon Feb 11 02:24:32 2008
New Revision: 74
URL: http://svn.gnome.org/viewvc/soylent?rev=74&view=rev

Log:
simplify the mail-edit signal setup function

Modified:
   trunk/src/soylent-browser.c
   trunk/src/soylent-utils.c

Modified: trunk/src/soylent-browser.c
==============================================================================
--- trunk/src/soylent-browser.c	(original)
+++ trunk/src/soylent-browser.c	Mon Feb 11 02:24:32 2008
@@ -84,9 +84,9 @@
                                                       (SoylentBrowser *browser);
 static gboolean soylent_browser_person_info_im_signals_setup
                                                       (SoylentBrowser *browser);
-static void soylent_browser_person_info_web_signals_setup
+static gboolean soylent_browser_person_info_web_signals_setup
                                                       (SoylentBrowser *browser);
-static void soylent_browser_person_info_mail_signals_setup
+static gboolean soylent_browser_person_info_mail_signals_setup
                                                       (SoylentBrowser *browser);
 static void soylent_browser_person_info_signals_setup
                                                       (SoylentBrowser *browser);
@@ -1928,95 +1928,165 @@
   return retval;
 }
 
-/* Hook up handlers for edits to peoples' existing Web URLS */
-static void
+/* Hook up handlers for edits to peoples' existing Web URLS
+ *
+ * Return TRUE for success, FALSE for any failure. */
+static gboolean
 soylent_browser_person_info_web_signals_setup (SoylentBrowser *browser)
 {
+  gboolean retval = FALSE;
   const guint NUM_TYPE = 4;
-  const gchar *type_list[] = {"homepage", "blog", "calendar", "video",};
-  const guint e_contact_fields[] = {E_CONTACT_HOMEPAGE_URL,
-                                    E_CONTACT_BLOG_URL,
-                                    E_CONTACT_CALENDAR_URI,
-                                    E_CONTACT_VIDEO_URL,};
   GladeXML *wtree = NULL;
-  guint type = 0;
+  guint i = 0;
+
+  g_return_val_if_fail (browser != NULL, retval);
+  /* FIXME: uncomment once SoylentBrowser is a GObject:
+  g_return_val_if_fail (SOYLENT_IS_BROWSER (browser), retval);
+   */
 
   wtree = browser->main_window;
 
-  for (type = 0; type < NUM_TYPE; type++)
+  /* invert usual logic to simplify the loop below */
+  retval = TRUE;
+
+  for (i = 0; i < NUM_TYPE; i++)
     {
-      gchar *widget_str = NULL;
-      GtkEntry *entry_web = NULL;
+      /* TODO: factor this out somewhere else, if possible */
+      const gchar *type_list[] = {"homepage", "blog", "calendar", "video",};
+      const guint e_contact_fields[] = {E_CONTACT_HOMEPAGE_URL,
+                                        E_CONTACT_BLOG_URL,
+                                        E_CONTACT_CALENDAR_URI,
+                                        E_CONTACT_VIDEO_URL,};
+      const gchar *signal_name = "changed";
+      const GCallback callback = G_CALLBACK (entry_changed_e_contact);
+      gboolean connect_retval = FALSE;
+      gchar *widget_name = NULL;
       cb_entry_changed_e_contact *cb_data = NULL;
 
-      widget_str = g_strdup_printf ("entry_person_web_%s", type_list[type]);
-      entry_web = GTK_ENTRY (glade_xml_get_widget (wtree, widget_str));
+      widget_name = g_strdup_printf ("entry_person_web_%s", type_list[i]);
 
       cb_data = g_new0 (cb_entry_changed_e_contact, 1);
       cb_data->browser = browser;
-      cb_data->widget_name = g_strdup (widget_str);
-      g_free (widget_str);
+      cb_data->widget_name = widget_name;
       cb_data->field = g_new0 (EContactField, 1);
-      *(cb_data->field) = e_contact_fields[type];
+      *(cb_data->field) = e_contact_fields[i];
 
-      /* FIXME - use g_signal_connect_data so we can eventually free
-       * each instance of cb_data */
-      g_signal_connect (G_OBJECT (entry_web), "changed",
-                        G_CALLBACK (entry_changed_e_contact), cb_data);
+      if (cb_data)
+        {
+          /* FIXME - use a version which uses g_signal_connect_data so we can
+          * eventually free each instance of cb_data */
+          connect_retval = widget_signal_connect (wtree, widget_name,
+                                                  signal_name, callback,
+                                                  cb_data);
+        }
+
+      if (connect_retval)
+        {
+          /* success */
+        }
+      else
+        {
+          g_critical ("failed to set up signal handler for widget "
+                      "%s::%s\n", widget_name, signal_name);
+
+          retval = FALSE;
+
+          if (cb_data)
+            {
+              g_free (cb_data->field);
+              g_free (cb_data);
+            }
+          g_free (widget_name);
+        }
     }
+
+  return retval;
 }
 
-/* Hook up handlers for edits to peoples' existing mailing addresses */
-static void
+/* Hook up handlers for edits to peoples' existing mailing addresses
+ *
+ * Return TRUE for success, FALSE for any failure. */
+static gboolean
 soylent_browser_person_info_mail_signals_setup (SoylentBrowser *browser)
 {
   const guint e_contact_fields[] = {E_CONTACT_ADDRESS_HOME, 
                                     E_CONTACT_ADDRESS_WORK,
                                     E_CONTACT_ADDRESS_OTHER,};
+  gboolean retval = FALSE;
   GladeXML *wtree = NULL;
-  guint context = 0;
-  guint fieldnum = 0;
+  guint i = 0;
+  guint j = 0;
+
+  g_return_val_if_fail (browser != NULL, retval);
+  /* FIXME: uncomment once SoylentBrowser is a GObject:
+  g_return_val_if_fail (SOYLENT_IS_BROWSER (browser), retval);
+   */
 
   wtree = browser->main_window;
 
-  /* FIXME: replace this sizeof calculation with ARRAY_LEN() */
-  for (context = 0; context < sizeof (e_contact_fields) / sizeof (const guint*);
-        context++)
-    {
-      for (fieldnum = 0; fieldnum < MAIL_FIELDS_NUM; fieldnum++)
-        {
-          GObject *widget_cur = NULL;
-          gchar *widget_name = NULL;
-          cb_entry_changed_e_contact *cb_data = NULL;
-
-          widget_name = g_strdup_printf ("%s_person_mail_%s_%s",
-                                         MAIL_WIDGET_TYPE_STRS[fieldnum],
-                                         CONTEXT_STRS[context],
-                                         MAIL_FIELD_STRS[fieldnum]);
-
-          cb_data = g_new0 (cb_entry_changed_e_contact, 1);
-          cb_data->browser = browser;
-          cb_data->widget_name = g_strdup_printf ("mail_%s",
-                                                  CONTEXT_STRS[context]);
-          cb_data->field = g_new0 (EContactField, 1);
-          *(cb_data->field) = e_contact_fields[context];
-          /* FIXME: the actual EContactField above ^^^ */
-          /* FIXME: we need to handle the EContactAddress type for this; may
-           * have to make a function similar to entry_changed_e_contact */
+  /* invert usual logic to simplify the loop below */
+  retval = TRUE;
 
-          widget_cur = G_OBJECT (glade_xml_get_widget (wtree, widget_name));
-          if (GTK_IS_TEXT_VIEW (widget_cur))
-            {
-              widget_cur = G_OBJECT (gtk_text_view_get_buffer (GTK_TEXT_VIEW
-                                                                (widget_cur)));
-            }
+  for (i = 0; i < ARRAY_LEN (e_contact_fields); i++)
+  {
+    for (j = 0; j < MAIL_FIELDS_NUM; j++)
+      {
+        /* TODO: factor this out somewhere else, if possible */
+        const gchar *context_list[] = {"home", "work", "other"};
+        const gchar *signal_name = "changed";
+        const GCallback callback = G_CALLBACK (multival_changed_e_contact);
+        gboolean connect_retval = FALSE;
+        gchar *widget_name = NULL;
+        cb_entry_changed_e_contact *cb_data = NULL;
 
-          g_signal_connect (widget_cur, "changed",
-                            G_CALLBACK (multival_changed_e_contact), cb_data);
+        widget_name = g_strdup_printf ("%s_person_mail_%s_%s",
+                                       MAIL_WIDGET_TYPE_STRS[j],
+                                       context_list[i], MAIL_FIELD_STRS[j]);
 
-          g_free (widget_name);
-        }
-    }
+        cb_data = g_new0 (cb_entry_changed_e_contact, 1);
+        cb_data->browser = browser;
+        /* FIXME: don't special-case this name (compared to the other similar
+         * signal-handler-setup functions) */
+        cb_data->widget_name = g_strdup_printf ("mail_%s",
+                                                context_list[i]);
+        /* Allocate dynamic memory for this field, since it's assumed to be
+         * dynamically-allocated later on */
+        cb_data->field = g_new0 (EContactField, 1);
+        *(cb_data->field) = e_contact_fields[j];
+
+        if (cb_data)
+          {
+            /* FIXME - use a version which uses g_signal_connect_data so we can
+            * eventually free each instance of cb_data */
+            connect_retval = widget_signal_connect (wtree, widget_name,
+                                                    signal_name, callback,
+                                                    cb_data);
+          }
+
+        if (connect_retval)
+          {
+            /* success */
+          }
+        else
+          {
+            g_critical ("failed to set up signal handler for widget "
+                        "%s::%s\n", widget_name, signal_name);
+
+            retval = FALSE;
+
+            if (cb_data)
+              {
+                g_free (cb_data->field);
+                g_free (cb_data);
+              }
+            g_free (cb_data->widget_name);
+          }
+
+        g_free (widget_name);
+      }
+  }
+
+  return retval;
 }
 
 /* Hook up handlers for actions on widgets in the Detail Add UI section */

Modified: trunk/src/soylent-utils.c
==============================================================================
--- trunk/src/soylent-utils.c	(original)
+++ trunk/src/soylent-utils.c	Mon Feb 11 02:24:32 2008
@@ -28,7 +28,8 @@
 #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.
+ * 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
  *
  * Return TRUE for success, FALSE for any failure */
 gboolean
@@ -50,11 +51,28 @@
     {
       gint handler_id = -1;
 
-      handler_id = g_signal_connect (G_OBJECT (widget), signal,
-                                     G_CALLBACK (callback), user_data);
-      if (handler_id >= G_SIGNAL_HANDLER_MIN_VALID)
+      /* If the given widget is a GtkTextView, attach the callback to the signal
+       * of its GtkTextBuffer instead */
+      if (GTK_IS_TEXT_VIEW (widget))
         {
-          retval = TRUE;
+          GtkTextBuffer *text_buffer = NULL;
+
+          text_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget));
+          handler_id = g_signal_connect (G_OBJECT (text_buffer), signal,
+                                         G_CALLBACK (callback), user_data);
+          if (handler_id >= G_SIGNAL_HANDLER_MIN_VALID)
+            {
+              retval = TRUE;
+            }
+        }
+      else
+        {
+          handler_id = g_signal_connect (G_OBJECT (widget), signal,
+                                         G_CALLBACK (callback), user_data);
+          if (handler_id >= G_SIGNAL_HANDLER_MIN_VALID)
+            {
+              retval = TRUE;
+            }
         }
     }
 



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