[empathy] factor out empathy_contact_info_create_channel_list_label()



commit caae76cdabb9939989d196cef7f367dcb26c8d44
Author: Guillaume Desmottes <guillaume desmottes collabora co uk>
Date:   Thu Nov 10 11:45:48 2011 +0100

    factor out empathy_contact_info_create_channel_list_label()
    
    Move it to empathy-contactinfo-utils so we'll be able to re-use it in
    empathy-individual-widget as well.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=663763

 libempathy-gtk/empathy-contact-widget.c    |  114 ++++++++--------------------
 libempathy-gtk/empathy-contactinfo-utils.c |   67 ++++++++++++++++
 libempathy-gtk/empathy-contactinfo-utils.h |    6 ++
 3 files changed, 106 insertions(+), 81 deletions(-)
---
diff --git a/libempathy-gtk/empathy-contact-widget.c b/libempathy-gtk/empathy-contact-widget.c
index 8e56665..4a6640e 100644
--- a/libempathy-gtk/empathy-contact-widget.c
+++ b/libempathy-gtk/empathy-contact-widget.c
@@ -40,7 +40,6 @@
 #include <libempathy/empathy-contact-manager.h>
 #include <libempathy/empathy-contact-list.h>
 #include <libempathy/empathy-location.h>
-#include <libempathy/empathy-request-util.h>
 #include <libempathy/empathy-time.h>
 #include <libempathy/empathy-utils.h>
 
@@ -479,64 +478,19 @@ contact_widget_details_update_edit (EmpathyContactWidget *information)
   return n_rows;
 }
 
-static gboolean
-channel_name_activated_cb (
-    GtkLabel *label,
-    gchar *uri,
-    EmpathyContactWidget *information)
-{
-  TpAccount *account = empathy_contact_get_account (information->contact);
-
-  empathy_join_muc (account, uri, empathy_get_current_action_time ());
-  return TRUE;
-}
-
 static void
-add_channel_list (
-    EmpathyContactWidget *information,
-    GPtrArray *channels,
-    guint row)
+add_row (GtkGrid *grid,
+    guint row,
+    GtkWidget *title,
+    GtkWidget *value)
 {
-  GtkWidget *w;
-  GString *label_markup = g_string_new ("");
-  guint i;
-
-  w = gtk_label_new (_("Channels:"));
-  gtk_grid_attach (GTK_GRID (information->grid_details),
-      w, 0, row, 1, 1);
-  gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
-  gtk_widget_show (w);
+  gtk_grid_attach (grid, title, 0, row, 1, 1);
+  gtk_misc_set_alignment (GTK_MISC (title), 0, 0.5);
+  gtk_widget_show (title);
 
-  for (i = 0; i < channels->len; i++)
-    {
-      const gchar *channel_name = g_ptr_array_index (channels, i);
-      /* We abuse the URI of the link to hold the channel name. It seems to
-       * be okay to just use it essentially verbatim, rather than trying to
-       * ensure it's actually a valid URI.  g_string_append_uri_escaped()
-       * escapes way more than we actually need to; so we're just using
-       * g_markup_escape_text directly.
-       */
-      gchar *escaped = g_markup_escape_text (channel_name, -1);
-
-      if (i > 0)
-        g_string_append (label_markup, ", ");
-
-      g_string_append_printf (label_markup, "<a href='%s'>%s</a>",
-          escaped, channel_name);
-      g_free (escaped);
-    }
-
-  w = gtk_label_new (NULL);
-  gtk_label_set_markup (GTK_LABEL (w), label_markup->str);
-  gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
-  g_signal_connect (w, "activate-link",
-      (GCallback) channel_name_activated_cb, information);
-  gtk_grid_attach (GTK_GRID (information->grid_details),
-      w, 1, row, 1, 1);
-  gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
-  gtk_widget_show (w);
-
-  g_string_free (label_markup, TRUE);
+  gtk_grid_attach (grid, value, 1, row, 1, 1);
+  gtk_misc_set_alignment (GTK_MISC (value), 0, 0.5);
+  gtk_widget_show (value);
 }
 
 static guint
@@ -545,7 +499,8 @@ contact_widget_details_update_show (EmpathyContactWidget *information)
   TpContact *contact;
   GList *info, *l;
   guint n_rows = 0;
-  GPtrArray *channels = g_ptr_array_new ();
+  GtkWidget *channels_label;
+  TpAccount *account;
 
   contact = empathy_contact_get_tp_contact (information->contact);
   info = tp_contact_get_contact_info (contact);
@@ -555,7 +510,7 @@ contact_widget_details_update_show (EmpathyContactWidget *information)
       TpContactInfoField *field = l->data;
       const gchar *value;
       gchar *markup = NULL, *title;
-      GtkWidget *w;
+      GtkWidget *title_widget, *value_widget;
       EmpathyContactInfoFormatFunc format;
 
       if (field->field_value == NULL || field->field_value[0] == NULL)
@@ -563,12 +518,6 @@ contact_widget_details_update_show (EmpathyContactWidget *information)
 
       value = field->field_value[0];
 
-      if (!tp_strdiff (field->field_name, "x-irc-channel"))
-        {
-          g_ptr_array_add (channels, (gpointer) field->field_value[0]);
-          continue;
-        }
-
       if (!empathy_contact_info_lookup_field (field->field_name, NULL, &format))
         {
           DEBUG ("Unhandled ContactInfo field: %s", field->field_name);
@@ -590,42 +539,45 @@ contact_widget_details_update_show (EmpathyContactWidget *information)
       /* Add Title */
       title = empathy_contact_info_field_label (field->field_name,
           field->parameters);
-      w = gtk_label_new (title);
+      title_widget = gtk_label_new (title);
       g_free (title);
 
-      gtk_grid_attach (GTK_GRID (information->grid_details),
-          w, 0, n_rows, 1, 1);
-      gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
-      gtk_widget_show (w);
-
       /* Add Value */
-      w = gtk_label_new (value);
+      value_widget = gtk_label_new (value);
       if (markup != NULL)
         {
-          gtk_label_set_markup (GTK_LABEL (w), markup);
+          gtk_label_set_markup (GTK_LABEL (value_widget), markup);
           g_free (markup);
         }
 
       if ((information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP) == 0)
-        gtk_label_set_selectable (GTK_LABEL (w), TRUE);
+        gtk_label_set_selectable (GTK_LABEL (value_widget), TRUE);
 
-      gtk_grid_attach (GTK_GRID (information->grid_details),
-          w, 1, n_rows, 1, 1);
-      gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
-      gtk_widget_show (w);
+      add_row (GTK_GRID (information->grid_details), n_rows, title_widget,
+          value_widget);
 
       n_rows++;
     }
 
-  g_list_free (info);
+  account = empathy_contact_get_account (information->contact);
 
-  if (channels->len > 0)
+  channels_label = empathy_contact_info_create_channel_list_label (account,
+      info, n_rows);
+
+  if (channels_label != NULL)
     {
-      add_channel_list (information, channels, n_rows);
+      GtkWidget *title_widget;
+
+      title_widget =  gtk_label_new (_("Channels:"));
+
+      add_row (GTK_GRID (information->grid_details), n_rows, title_widget,
+          channels_label);
+
       n_rows++;
     }
 
-  g_ptr_array_unref (channels);
+  g_list_free (info);
+
   return n_rows;
 }
 
diff --git a/libempathy-gtk/empathy-contactinfo-utils.c b/libempathy-gtk/empathy-contactinfo-utils.c
index 9972257..9d07f3f 100644
--- a/libempathy-gtk/empathy-contactinfo-utils.c
+++ b/libempathy-gtk/empathy-contactinfo-utils.c
@@ -30,9 +30,11 @@
 #include <telepathy-glib/util.h>
 
 #include <libempathy/empathy-time.h>
+#include <libempathy/empathy-request-util.h>
 
 #include "empathy-contactinfo-utils.h"
 #include "empathy-string-parser.h"
+#include "empathy-ui-utils.h"
 
 static gchar *
 linkify_first_value (GStrv values)
@@ -264,3 +266,68 @@ empathy_contact_info_field_spec_cmp (TpContactInfoFieldSpec *spec1,
 {
     return contact_info_field_name_cmp (spec1->name, spec2->name);
 }
+
+static gboolean
+channel_name_activated_cb (
+    GtkLabel *label,
+    gchar *uri,
+    TpAccount *account)
+{
+  empathy_join_muc (account, uri, empathy_get_current_action_time ());
+  return TRUE;
+}
+
+GtkWidget *
+empathy_contact_info_create_channel_list_label (TpAccount *account,
+    GList *info,
+    guint row)
+{
+  GtkWidget *label = NULL;
+  GString *label_markup = g_string_new ("");
+  guint i;
+  GPtrArray *channels;
+  GList *l;
+
+  /* Is there channels? */
+  channels = g_ptr_array_new ();
+
+  for (l = info; l != NULL; l = l->next)
+    {
+      TpContactInfoField *field = l->data;
+
+      if (!tp_strdiff (field->field_name, "x-irc-channel"))
+        g_ptr_array_add (channels, (gpointer) field->field_value[0]);
+    }
+
+  if (channels->len == 0)
+    goto out;
+
+  for (i = 0; i < channels->len; i++)
+    {
+      const gchar *channel_name = g_ptr_array_index (channels, i);
+      /* We abuse the URI of the link to hold the channel name. It seems to
+       * be okay to just use it essentially verbatim, rather than trying to
+       * ensure it's actually a valid URI. */
+      gchar *escaped = g_markup_escape_text (channel_name, -1);
+
+      if (i > 0)
+        g_string_append (label_markup, ", ");
+
+      g_string_append_printf (label_markup, "<a href='%s'>%s</a>",
+          escaped, channel_name);
+      g_free (escaped);
+    }
+
+  label = gtk_label_new (NULL);
+  gtk_label_set_markup (GTK_LABEL (label), label_markup->str);
+  gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
+
+  g_signal_connect (label, "activate-link",
+      (GCallback) channel_name_activated_cb, account);
+
+out:
+  g_ptr_array_unref (channels);
+  g_string_free (label_markup, TRUE);
+
+  return label;
+}
diff --git a/libempathy-gtk/empathy-contactinfo-utils.h b/libempathy-gtk/empathy-contactinfo-utils.h
index 1d89ff2..a49e807 100644
--- a/libempathy-gtk/empathy-contactinfo-utils.h
+++ b/libempathy-gtk/empathy-contactinfo-utils.h
@@ -22,6 +22,8 @@
 #define __EMPATHY_CONTACTINFO_UTILS_H__
 
 #include <glib.h>
+#include <gtk/gtk.h>
+
 #include <telepathy-glib/connection.h>
 
 G_BEGIN_DECLS
@@ -39,6 +41,10 @@ gint empathy_contact_info_field_cmp (TpContactInfoField *field1,
 gint empathy_contact_info_field_spec_cmp (TpContactInfoFieldSpec *spec1,
     TpContactInfoFieldSpec *spec2);
 
+GtkWidget * empathy_contact_info_create_channel_list_label (TpAccount *account,
+    GList *info,
+    guint row);
+
 G_END_DECLS
 
 #endif /*  __EMPATHY_UTILS_H__ */



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