]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-contact-widget.c
use gtk_box_new() instead of gtk_[h,v]box_new()
[empathy.git] / libempathy-gtk / empathy-contact-widget.c
index 6c021c3ee16d2e8f79f6eba26b21e31631950b58..476fb6217b9f45861f1a8de2a12c3c5876c25888 100644 (file)
 #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>
 
 #include "empathy-contact-widget.h"
+#include "empathy-contactinfo-utils.h"
 #include "empathy-account-chooser.h"
 #include "empathy-avatar-chooser.h"
 #include "empathy-avatar-image.h"
@@ -101,14 +101,14 @@ typedef struct
   GtkWidget *hbox_presence;
   GtkWidget *image_state;
   GtkWidget *label_status;
-  GtkWidget *table_contact;
+  GtkWidget *grid_contact;
   GtkWidget *vbox_avatar;
   GtkWidget *favourite_checkbox;
 
   /* Location */
   GtkWidget *vbox_location;
   GtkWidget *subvbox_location;
-  GtkWidget *table_location;
+  GtkWidget *grid_location;
   GtkWidget *label_location;
 #ifdef HAVE_LIBCHAMPLAIN
   GtkWidget *viewport_map;
@@ -121,7 +121,7 @@ typedef struct
 
   /* Details */
   GtkWidget *vbox_details;
-  GtkWidget *table_details;
+  GtkWidget *grid_details;
   GtkWidget *hbox_details_requested;
   GtkWidget *spinner_details;
   GList *details_to_set;
@@ -130,7 +130,7 @@ typedef struct
 
   /* Client */
   GtkWidget *vbox_client;
-  GtkWidget *table_client;
+  GtkWidget *grid_client;
   GtkWidget *hbox_client_requested;
 } EmpathyContactWidget;
 
@@ -284,118 +284,6 @@ contact_widget_bday_changed_cb (GtkCalendar *calendar,
 
 static void contact_widget_details_notify_cb (EmpathyContactWidget *information);
 
-typedef gchar * (* FieldFormatFunc) (GStrv);
-
-typedef struct
-{
-  const gchar *field_name;
-  const gchar *title;
-  FieldFormatFunc format;
-} InfoFieldData;
-
-static gchar *
-linkify_first_value (GStrv values)
-{
-  return empathy_add_link_markup (values[0]);
-}
-
-static gchar *
-format_idle_time (GStrv values)
-{
-  const gchar *value = values[0];
-  int duration = strtol (value, NULL, 10);
-
-  if (duration <= 0)
-    return NULL;
-
-  return empathy_duration_to_string (duration);
-}
-
-static gchar *
-format_server (GStrv values)
-{
-  g_assert (values[0] != NULL);
-
-  if (values[1] == NULL)
-    return g_markup_escape_text (values[0], -1);
-  else
-    return g_markup_printf_escaped ("%s (%s)", values[0], values[1]);
-}
-
-static gchar *
-presence_hack (GStrv values)
-{
-  if (tp_str_empty (values[0]))
-    return NULL;
-
-  return g_markup_escape_text (values[0], -1);
-}
-
-static InfoFieldData info_field_datas[] =
-{
-  { "fn",    N_("Full name:"),      NULL },
-  { "tel",   N_("Phone number:"),   NULL },
-  { "email", N_("E-mail address:"), linkify_first_value },
-  { "url",   N_("Website:"),        linkify_first_value },
-  { "bday",  N_("Birthday:"),       NULL },
-
-  /* Note to translators: this is the caption for a string of the form "5
-   * minutes ago", and refers to the time since the contact last interacted
-   * with their IM client.
-   */
-  { "x-idle-time", N_("Last seen:"), format_idle_time },
-  { "x-irc-server", N_("Server:"), format_server },
-  { "x-host", N_("Connected from:"), format_server },
-
-  /* FIXME: once Idle implements SimplePresence using this information, we can
-   * and should bin this.
-   */
-  { "x-presence-status-message", N_("Away message:"), presence_hack },
-
-  { NULL, NULL }
-};
-
-static InfoFieldData *
-find_info_field_data (const gchar *field_name)
-{
-  guint i;
-
-  for (i = 0; info_field_datas[i].field_name != NULL; i++)
-    {
-      if (!tp_strdiff (info_field_datas[i].field_name, field_name))
-        return info_field_datas + i;
-    }
-  return NULL;
-}
-
-static gint
-contact_info_field_name_cmp (const gchar *name1,
-    const gchar *name2)
-{
-  guint i;
-
-  if (!tp_strdiff (name1, name2))
-    return 0;
-
-  /* We use the order of info_field_datas */
-  for (i = 0; info_field_datas[i].field_name != NULL; i++)
-    {
-      if (!tp_strdiff (info_field_datas[i].field_name, name1))
-        return -1;
-      if (!tp_strdiff (info_field_datas[i].field_name, name2))
-        return +1;
-    }
-
-  return g_strcmp0 (name1, name2);
-}
-
-static gint
-contact_info_field_cmp (TpContactInfoField *field1,
-    TpContactInfoField *field2)
-{
-  return contact_info_field_name_cmp (field1->field_name, field2->field_name);
-}
-
 static gboolean
 field_name_in_field_list (GList *list,
     const gchar *name)
@@ -438,6 +326,7 @@ contact_widget_details_update_edit (EmpathyContactWidget *information)
   GList *specs, *l;
   guint n_rows = 0;
   GList *info;
+  const char **field_names = empathy_contact_info_get_field_names (NULL);
   guint i;
 
   g_assert (information->details_to_set == NULL);
@@ -465,18 +354,18 @@ contact_widget_details_update_edit (EmpathyContactWidget *information)
     }
 
   /* Add fields which are supported but not in the vCard */
-  for (i = 0; info_field_datas[i].field_name != NULL; i++)
+  for (i = 0; field_names[i] != NULL; i++)
     {
       TpContactInfoFieldSpec *spec;
       TpContactInfoField *field;
 
       /* Check if the field was in the vCard */
       if (field_name_in_field_list (information->details_to_set,
-            info_field_datas[i].field_name))
+            field_names[i]))
         continue;
 
       /* Check if the CM supports the field */
-      spec = get_spec_from_list (specs, info_field_datas[i].field_name);
+      spec = get_spec_from_list (specs, field_names[i]);
       if (spec == NULL)
         continue;
 
@@ -489,17 +378,19 @@ contact_widget_details_update_edit (EmpathyContactWidget *information)
 
   /* Add widgets for supported fields */
   information->details_to_set = g_list_sort (information->details_to_set,
-      (GCompareFunc) contact_info_field_cmp);
+      (GCompareFunc) empathy_contact_info_field_spec_cmp);
 
   for (l = information->details_to_set; l != NULL; l= g_list_next (l))
     {
       TpContactInfoField *field = l->data;
-      InfoFieldData *field_data;
       GtkWidget *w;
       TpContactInfoFieldSpec *spec;
+      gboolean has_field;
+      char *title;
 
-      field_data = find_info_field_data (field->field_name);
-      if (field_data == NULL)
+      has_field = empathy_contact_info_lookup_field (field->field_name,
+          NULL, NULL);
+      if (!has_field)
         {
           /* Empathy doesn't display this field so we can't change it.
            * But we put it in the details_to_set list so it won't be erased
@@ -521,9 +412,14 @@ contact_widget_details_update_edit (EmpathyContactWidget *information)
         }
 
       /* Add Title */
-      w = gtk_label_new (_(field_data->title));
-      gtk_table_attach (GTK_TABLE (information->table_details),
-          w, 0, 1, n_rows, n_rows + 1, GTK_FILL, 0, 0, 0);
+      title = empathy_contact_info_field_label (field->field_name,
+          field->parameters);
+      w = 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);
 
@@ -546,8 +442,9 @@ contact_widget_details_update_edit (EmpathyContactWidget *information)
                       g_date_get_day (&date));
                 }
             }
-          gtk_table_attach_defaults (GTK_TABLE (information->table_details),
-              w, 1, 2, n_rows, n_rows + 1);
+
+          gtk_grid_attach (GTK_GRID (information->grid_details),
+              w, 1, n_rows, 1, 1);
           gtk_widget_show (w);
 
           g_object_set_data ((GObject *) w, DATA_FIELD, field);
@@ -562,8 +459,8 @@ contact_widget_details_update_edit (EmpathyContactWidget *information)
           w = gtk_entry_new ();
           gtk_entry_set_text (GTK_ENTRY (w),
               field->field_value[0] ? field->field_value[0] : "");
-          gtk_table_attach_defaults (GTK_TABLE (information->table_details),
-              w, 1, 2, n_rows, n_rows + 1);
+          gtk_grid_attach (GTK_GRID (information->grid_details),
+              w, 1, n_rows, 1, 1);
           gtk_widget_show (w);
 
           g_object_set_data ((GObject *) w, DATA_FIELD, field);
@@ -581,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)
-{
-  GtkWidget *w;
-  GString *label_markup = g_string_new ("");
-  guint i;
-
-  w = gtk_label_new (_("Channels:"));
-  gtk_table_attach (GTK_TABLE (information->table_details),
-      w, 0, 1, row, row + 1, GTK_FILL, 0, 0, 0);
-  gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
-  gtk_widget_show (w);
-
-  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_table_attach_defaults (GTK_TABLE (information->table_details),
-      w, 1, 2, row, row + 1);
-  gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
-  gtk_widget_show (w);
-
-  g_string_free (label_markup, TRUE);
+add_row (GtkGrid *grid,
+    guint row,
+    GtkWidget *title,
+    GtkWidget *value)
+{
+  gtk_grid_attach (grid, title, 0, row, 1, 1);
+  gtk_misc_set_alignment (GTK_MISC (title), 0, 0.5);
+  gtk_widget_show (title);
+
+  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
@@ -647,40 +499,34 @@ 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);
-  info = g_list_sort (info, (GCompareFunc) contact_info_field_cmp);
+  info = g_list_sort (info, (GCompareFunc) empathy_contact_info_field_cmp);
   for (l = info; l != NULL; l = l->next)
     {
       TpContactInfoField *field = l->data;
-      InfoFieldData *field_data;
       const gchar *value;
-      gchar *markup = NULL;
-      GtkWidget *w;
+      gchar *markup = NULL, *title;
+      GtkWidget *title_widget, *value_widget;
+      EmpathyContactInfoFormatFunc format;
 
       if (field->field_value == NULL || field->field_value[0] == NULL)
         continue;
 
       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;
-        }
-
-      field_data = find_info_field_data (field->field_name);
-      if (field_data == NULL)
+      if (!empathy_contact_info_lookup_field (field->field_name, NULL, &format))
         {
           DEBUG ("Unhandled ContactInfo field: %s", field->field_name);
           continue;
         }
 
-      if (field_data->format != NULL)
+      if (format != NULL)
         {
-          markup = field_data->format (field->field_value);
+          markup = format (field->field_value);
 
           if (markup == NULL)
             {
@@ -691,39 +537,47 @@ contact_widget_details_update_show (EmpathyContactWidget *information)
         }
 
       /* Add Title */
-      w = gtk_label_new (_(field_data->title));
-      gtk_table_attach (GTK_TABLE (information->table_details),
-          w, 0, 1, n_rows, n_rows + 1, GTK_FILL, 0, 0, 0);
-      gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
-      gtk_widget_show (w);
+      title = empathy_contact_info_field_label (field->field_name,
+          field->parameters);
+      title_widget = gtk_label_new (title);
+      g_free (title);
 
       /* 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_table_attach_defaults (GTK_TABLE (information->table_details),
-          w, 1, 2, n_rows, n_rows + 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);
 
-  if (channels->len > 0)
+  account = empathy_contact_get_account (information->contact);
+
+  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;
 }
 
@@ -732,7 +586,7 @@ contact_widget_details_notify_cb (EmpathyContactWidget *information)
 {
   guint n_rows;
 
-  gtk_container_foreach (GTK_CONTAINER (information->table_details),
+  gtk_container_foreach (GTK_CONTAINER (information->grid_details),
       (GtkCallback) gtk_widget_destroy, NULL);
 
   if ((information->flags & EMPATHY_CONTACT_WIDGET_EDIT_DETAILS) != 0)
@@ -743,7 +597,7 @@ contact_widget_details_notify_cb (EmpathyContactWidget *information)
   if (n_rows > 0)
     {
       gtk_widget_show (information->vbox_details);
-      gtk_widget_show (information->table_details);
+      gtk_widget_show (information->grid_details);
     }
   else
     {
@@ -817,7 +671,7 @@ fetch_contact_information (EmpathyContactWidget *information,
   /* Request the contact's info */
   gtk_widget_show (information->vbox_details);
   gtk_widget_show (information->hbox_details_requested);
-  gtk_widget_hide (information->table_details);
+  gtk_widget_hide (information->grid_details);
   gtk_spinner_start (GTK_SPINNER (information->spinner_details));
 
   contact = empathy_contact_get_tp_contact (information->contact);
@@ -1018,15 +872,15 @@ contact_widget_location_update (EmpathyContactWidget *information)
     }
 
 
-  /* Prepare the location information table */
-  if (information->table_location != NULL)
+  /* Prepare the location information grid */
+  if (information->grid_location != NULL)
     {
-      gtk_widget_destroy (information->table_location);
+      gtk_widget_destroy (information->grid_location);
     }
 
-  information->table_location = gtk_table_new (1, 2, FALSE);
+  information->grid_location = gtk_grid_new ();
   gtk_box_pack_start (GTK_BOX (information->subvbox_location),
-      information->table_location, FALSE, FALSE, 5);
+      information->grid_location, FALSE, FALSE, 5);
 
 
   for (i = 0; (skey = ordered_geolocation_keys[i]); i++)
@@ -1043,8 +897,8 @@ contact_widget_location_update (EmpathyContactWidget *information)
 
       label = gtk_label_new (user_label);
       gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
-      gtk_table_attach (GTK_TABLE (information->table_location),
-          label, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, 10, 0);
+      gtk_grid_attach (GTK_GRID (information->grid_location),
+          label, 0, row, 1, 1);
       gtk_widget_show (label);
 
       if (G_VALUE_TYPE (gvalue) == G_TYPE_DOUBLE)
@@ -1068,8 +922,8 @@ contact_widget_location_update (EmpathyContactWidget *information)
       if (svalue != NULL)
         {
           label = gtk_label_new (svalue);
-          gtk_table_attach_defaults (GTK_TABLE (information->table_location),
-              label, 1, 2, row, row + 1);
+          gtk_grid_attach (GTK_GRID (information->grid_location),
+              label, 1, row, 1, 1);
           gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
           gtk_widget_show (label);
 
@@ -1094,7 +948,7 @@ contact_widget_location_update (EmpathyContactWidget *information)
   if (row > 0)
     {
       /* We can display some fields */
-      gtk_widget_show (information->table_location);
+      gtk_widget_show (information->grid_location);
     }
   else if (!display_map)
     {
@@ -1771,7 +1625,7 @@ contact_widget_contact_setup (EmpathyContactWidget *information)
   else
     {
       /* Pack the protocol icon with the account name in an hbox */
-      information->widget_account = gtk_hbox_new (FALSE, 6);
+      information->widget_account = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
 
       information->label_account = gtk_label_new (NULL);
       if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
@@ -1788,9 +1642,11 @@ contact_widget_contact_setup (EmpathyContactWidget *information)
       gtk_box_pack_start (GTK_BOX (information->widget_account),
           information->label_account, FALSE, TRUE, 0);
     }
-  gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
-           information->widget_account,
-           1, 2, 0, 1);
+
+  gtk_grid_attach (GTK_GRID (information->grid_contact),
+      information->widget_account,
+      1, 0, 1, 1);
+
   gtk_widget_show (information->widget_account);
 
   /* Set up avatar chooser/display */
@@ -1845,9 +1701,10 @@ contact_widget_contact_setup (EmpathyContactWidget *information)
       }
       gtk_misc_set_alignment (GTK_MISC (information->widget_id), 0, 0.5);
     }
-  gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
-           information->widget_id,
-           1, 2, 1, 2);
+
+  gtk_grid_attach (GTK_GRID (information->grid_contact), information->widget_id,
+      1, 1, 1, 1);
+
   gtk_widget_show (information->widget_id);
 
   /* Setup alias label/entry */
@@ -1872,9 +1729,10 @@ contact_widget_contact_setup (EmpathyContactWidget *information)
       }
       gtk_misc_set_alignment (GTK_MISC (information->widget_alias), 0, 0.5);
     }
-  gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
-           information->widget_alias,
-           1, 2, 2, 3);
+
+  gtk_grid_attach (GTK_GRID (information->grid_contact),
+      information->widget_alias, 1, 2, 1, 1);
+
   if (information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP) {
     gtk_label_set_selectable (GTK_LABEL (information->label_status), FALSE);
   }
@@ -1889,8 +1747,8 @@ contact_widget_contact_setup (EmpathyContactWidget *information)
       g_signal_connect (information->favourite_checkbox, "toggled",
           G_CALLBACK (favourite_toggled_cb), information);
 
-      gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
-           information->favourite_checkbox, 0, 2, 3, 4);
+      gtk_grid_attach (GTK_GRID (information->grid_contact),
+           information->favourite_checkbox, 0, 3, 1, 1);
 
       information->fav_sig_id = g_signal_connect (information->manager,
           "favourites-changed",
@@ -1949,7 +1807,7 @@ empathy_contact_widget_new (EmpathyContact *contact,
        "hbox_presence", &information->hbox_presence,
        "label_alias", &information->label_alias,
        "image_state", &information->image_state,
-       "table_contact", &information->table_contact,
+       "grid_contact", &information->grid_contact,
        "vbox_avatar", &information->vbox_avatar,
        "vbox_location", &information->vbox_location,
        "subvbox_location", &information->subvbox_location,
@@ -1959,10 +1817,10 @@ empathy_contact_widget_new (EmpathyContact *contact,
 #endif
        "groups_widget", &information->groups_widget,
        "vbox_details", &information->vbox_details,
-       "table_details", &information->table_details,
+       "grid_details", &information->grid_details,
        "hbox_details_requested", &information->hbox_details_requested,
        "vbox_client", &information->vbox_client,
-       "table_client", &information->table_client,
+       "grid_client", &information->grid_client,
        "hbox_client_requested", &information->hbox_client_requested,
        NULL);
   g_free (filename);
@@ -1970,7 +1828,7 @@ empathy_contact_widget_new (EmpathyContact *contact,
   empathy_builder_connect (gui, information,
       "vbox_contact_widget", "destroy", contact_widget_destroy_cb,
       NULL);
-  information->table_location = NULL;
+  information->grid_location = NULL;
 
   g_object_set_data (G_OBJECT (information->vbox_contact_widget),
       "EmpathyContactWidget",