]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-contact-widget.c
Merge branch 'sasl'
[empathy.git] / libempathy-gtk / empathy-contact-widget.c
index 4ff25b97469e67f542cba53a9f88024b95aa4b60..26713ebe421c34dbadf5f8f2f2b9dd00bba97bd1 100644 (file)
@@ -27,7 +27,7 @@
 #include <gtk/gtk.h>
 #include <glib/gi18n-lib.h>
 
-#if HAVE_LIBCHAMPLAIN
+#ifdef HAVE_LIBCHAMPLAIN
 #include <champlain/champlain.h>
 #include <champlain-gtk/champlain-gtk.h>
 #endif
@@ -47,9 +47,9 @@
 #include "empathy-account-chooser.h"
 #include "empathy-avatar-chooser.h"
 #include "empathy-avatar-image.h"
+#include "empathy-groups-widget.h"
 #include "empathy-ui-utils.h"
 #include "empathy-string-parser.h"
-#include "empathy-kludge-label.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
 #include <libempathy/empathy-debug.h>
@@ -107,17 +107,14 @@ typedef struct
   GtkWidget *subvbox_location;
   GtkWidget *table_location;
   GtkWidget *label_location;
-#if HAVE_LIBCHAMPLAIN
+#ifdef HAVE_LIBCHAMPLAIN
   GtkWidget *viewport_map;
   GtkWidget *map_view_embed;
   ChamplainView *map_view;
 #endif
 
   /* Groups */
-  GtkWidget *vbox_groups;
-  GtkWidget *entry_group;
-  GtkWidget *button_group;
-  GtkWidget *treeview_groups;
+  GtkWidget *groups_widget;
 
   /* Details */
   GtkWidget *vbox_details;
@@ -149,6 +146,42 @@ enum
   COL_COUNT
 };
 
+static gboolean
+field_value_is_empty (TpContactInfoField *field)
+{
+  guint i;
+
+  if (field->field_value == NULL)
+    return TRUE;
+
+  /* Field is empty if all its values are empty */
+  for (i = 0; field->field_value[i] != NULL; i++)
+    {
+      if (!tp_str_empty (field->field_value[i]))
+        return FALSE;
+    }
+
+  return TRUE;
+}
+
+static void
+set_contact_info_cb (GObject *source,
+    GAsyncResult *result,
+    gpointer user_data)
+{
+  GError *error = NULL;
+
+  if (!tp_connection_set_contact_info_finish (TP_CONNECTION (source), result,
+        &error))
+    {
+      DEBUG ("SetContactInfo() failed: %s", error->message);
+      g_error_free (error);
+      return;
+    }
+
+  DEBUG ("SetContactInfo() succeeded");
+}
+
 static void
 contact_widget_save (EmpathyContactWidget *information)
 {
@@ -163,7 +196,7 @@ contact_widget_save (EmpathyContactWidget *information)
       TpContactInfoField *field = l->data;
 
       next = l->next;
-      if (field->field_value == NULL || EMP_STR_EMPTY (field->field_value[0]))
+      if (field_value_is_empty (field))
         {
           DEBUG ("Drop empty field: %s", field->field_name);
           tp_contact_info_field_free (field);
@@ -175,7 +208,7 @@ contact_widget_save (EmpathyContactWidget *information)
   if (information->details_to_set != NULL)
     {
       tp_connection_set_contact_info_async (connection,
-          information->details_to_set, NULL, NULL);
+          information->details_to_set, set_contact_info_cb, NULL);
       tp_contact_info_list_free (information->details_to_set);
       information->details_to_set = NULL;
     }
@@ -265,11 +298,38 @@ contact_info_field_cmp (TpContactInfoField *field1,
   return contact_info_field_name_cmp (field1->field_name, field2->field_name);
 }
 
-static gint
-contact_info_field_spec_cmp (TpContactInfoFieldSpec *spec1,
-    TpContactInfoFieldSpec *spec2)
+static gboolean
+field_name_in_field_list (GList *list,
+    const gchar *name)
 {
-  return contact_info_field_name_cmp (spec1->name, spec2->name);
+  GList *l;
+
+  for (l = list; l != NULL; l = g_list_next (l))
+    {
+      TpContactInfoField *field = l->data;
+
+      if (!tp_strdiff (field->field_name, name))
+        return TRUE;
+    }
+
+  return FALSE;
+}
+
+static TpContactInfoFieldSpec *
+get_spec_from_list (GList *list,
+    const gchar *name)
+{
+  GList *l;
+
+  for (l = list; l != NULL; l = g_list_next (l))
+    {
+      TpContactInfoFieldSpec *spec = l->data;
+
+      if (!tp_strdiff (spec->name, name))
+        return spec;
+    }
+
+  return NULL;
 }
 
 static guint
@@ -279,46 +339,74 @@ contact_widget_details_update_edit (EmpathyContactWidget *information)
   TpConnection *connection;
   GList *specs, *l;
   guint n_rows = 0;
+  GList *info;
+  guint i;
 
   g_assert (information->details_to_set == NULL);
 
   contact = empathy_contact_get_tp_contact (information->contact);
   connection = tp_contact_get_connection (contact);
 
+  info = tp_contact_get_contact_info (contact);
+
   specs = tp_connection_get_contact_info_supported_fields (connection);
-  specs = g_list_sort (specs, (GCompareFunc) contact_info_field_spec_cmp);
-  for (l = specs; l != NULL; l = l->next)
+
+  /* Look at the fields set in our vCard */
+  for (l = info; l != NULL; l = l->next)
     {
-      TpContactInfoFieldSpec *spec = l->data;
+      TpContactInfoField *field = l->data;
+
+      /* make a copy for the details_to_set list */
+      field = tp_contact_info_field_copy (field);
+      DEBUG ("Field %s is in our vCard", field->field_name);
+
+      information->details_to_set = g_list_prepend (information->details_to_set,
+          field);
+    }
+
+  /* Add fields which are supported but not in the vCard */
+  for (i = 0; info_field_datas[i].field_name != 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))
+        continue;
+
+      /* Check if the CM supports the field */
+      spec = get_spec_from_list (specs, info_field_datas[i].field_name);
+      if (spec == NULL)
+        continue;
+
+      /* add an empty field so user can set a value */
+      field = tp_contact_info_field_new (spec->name, spec->parameters, NULL);
+
+      information->details_to_set = g_list_prepend (information->details_to_set,
+          field);
+    }
+
+  /* Add widgets for supported fields */
+  information->details_to_set = g_list_sort (information->details_to_set,
+      (GCompareFunc) contact_info_field_cmp);
+
+  for (l = information->details_to_set; l != NULL; l= g_list_next (l))
+    {
+      TpContactInfoField *field = l->data;
       InfoFieldData *field_data;
-      GList *info, *ll;
-      GStrv value = NULL;
       GtkWidget *w;
 
-      field_data = find_info_field_data (spec->name);
+      field_data = find_info_field_data (field->field_name);
       if (field_data == NULL)
         {
-          DEBUG ("Unhandled ContactInfo field spec: %s", spec->name);
+          /* 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
+           * when calling SetContactInfo (bgo #630427) */
+          DEBUG ("Unhandled ContactInfo field spec: %s", field->field_name);
           continue;
         }
 
-      /* Search initial value */
-      info = tp_contact_get_contact_info (contact);
-      for (ll = info; ll != NULL; ll = ll->next)
-        {
-          field = ll->data;
-          if (!tp_strdiff (field->field_name, spec->name))
-            {
-              value = field->field_value;
-              break;
-            }
-        }
-
-      field = tp_contact_info_field_new (spec->name, spec->parameters, value);
-      information->details_to_set = g_list_prepend (information->details_to_set,
-          field);
-
       /* Add Title */
       w = gtk_label_new (_(field_data->title));
       gtk_table_attach (GTK_TABLE (information->table_details),
@@ -339,7 +427,9 @@ contact_widget_details_update_edit (EmpathyContactWidget *information)
 
       n_rows++;
     }
+
   g_list_free (specs);
+  g_list_free (info);
 
   return n_rows;
 }
@@ -467,8 +557,7 @@ contact_widget_details_request_cb (GObject *object,
           G_CALLBACK (contact_widget_details_notify_cb), information);
     }
 
-  g_object_unref (information->details_cancellable);
-  information->details_cancellable = NULL;
+  tp_clear_object (&information->details_cancellable);
 }
 
 static void
@@ -549,288 +638,28 @@ contact_widget_client_setup (EmpathyContactWidget *information)
   gtk_widget_hide (information->vbox_client);
 }
 
-static void
-contact_widget_cell_toggled (GtkCellRendererToggle *cell,
-                             gchar *path_string,
-                             EmpathyContactWidget *information)
-{
-  GtkTreeView *view;
-  GtkTreeModel *model;
-  GtkListStore *store;
-  GtkTreePath *path;
-  GtkTreeIter iter;
-  gboolean was_enabled;
-  gchar *group;
-
-  view = GTK_TREE_VIEW (information->treeview_groups);
-  model = gtk_tree_view_get_model (view);
-  store = GTK_LIST_STORE (model);
-
-  path = gtk_tree_path_new_from_string (path_string);
-
-  gtk_tree_model_get_iter (model, &iter, path);
-  gtk_tree_model_get (model, &iter,
-      COL_ENABLED, &was_enabled,
-      COL_NAME, &group,
-      -1);
-
-  gtk_list_store_set (store, &iter, COL_ENABLED, !was_enabled, -1);
-  gtk_tree_path_free (path);
-
-  if (group != NULL)
-    {
-      empathy_contact_change_group (information->contact, group, !was_enabled);
-      g_free (group);
-    }
-}
-
-static void
-contact_widget_model_populate_columns (EmpathyContactWidget *information)
-{
-  GtkTreeView *view;
-  GtkTreeModel *model;
-  GtkTreeViewColumn *column;
-  GtkCellRenderer  *renderer;
-  guint col_offset;
-
-  view = GTK_TREE_VIEW (information->treeview_groups);
-  model = gtk_tree_view_get_model (view);
-
-  renderer = gtk_cell_renderer_toggle_new ();
-  g_signal_connect (renderer, "toggled",
-      G_CALLBACK (contact_widget_cell_toggled), information);
-
-  column = gtk_tree_view_column_new_with_attributes (_("Select"), renderer,
-      "active", COL_ENABLED, NULL);
-
-  gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
-  gtk_tree_view_column_set_fixed_width (column, 50);
-  gtk_tree_view_append_column (view, column);
-
-  renderer = gtk_cell_renderer_text_new ();
-  col_offset = gtk_tree_view_insert_column_with_attributes (view,
-      -1, _("Group"),
-      renderer,
-      "text", COL_NAME,
-      /* "editable", COL_EDITABLE, */
-      NULL);
-
-  g_object_set_data (G_OBJECT (renderer),
-      "column", GINT_TO_POINTER (COL_NAME));
-
-  column = gtk_tree_view_get_column (view, col_offset - 1);
-  gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
-  gtk_tree_view_column_set_resizable (column,FALSE);
-  gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
-}
-
-static void
-contact_widget_model_setup (EmpathyContactWidget *information)
-{
-  GtkTreeView *view;
-  GtkListStore *store;
-  GtkTreeSelection *selection;
-
-  view = GTK_TREE_VIEW (information->treeview_groups);
-
-  store = gtk_list_store_new (COL_COUNT,
-      G_TYPE_STRING,   /* name */
-      G_TYPE_BOOLEAN,  /* enabled */
-      G_TYPE_BOOLEAN); /* editable */
-
-  gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
-
-  selection = gtk_tree_view_get_selection (view);
-  gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
-
-  contact_widget_model_populate_columns (information);
-
-  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
-      COL_NAME, GTK_SORT_ASCENDING);
-
-  g_object_unref (store);
-}
-
-static void
-contact_widget_groups_populate_data (EmpathyContactWidget *information)
-{
-  GtkTreeView *view;
-  GtkListStore *store;
-  GtkTreeIter iter;
-  GList *my_groups, *l;
-  GList *all_groups;
-
-  view = GTK_TREE_VIEW (information->treeview_groups);
-  store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
-  gtk_list_store_clear (store);
-
-  all_groups = empathy_contact_list_get_all_groups (
-      EMPATHY_CONTACT_LIST (information->manager));
-  my_groups = empathy_contact_list_get_groups (
-      EMPATHY_CONTACT_LIST (information->manager),
-      information->contact);
-
-  for (l = all_groups; l; l = l->next)
-    {
-      const gchar *group_str;
-      gboolean enabled;
-
-      group_str = l->data;
-
-      enabled = g_list_find_custom (my_groups,
-          group_str, (GCompareFunc) strcmp) != NULL;
-
-      gtk_list_store_append (store, &iter);
-      gtk_list_store_set (store, &iter,
-          COL_NAME, group_str,
-          COL_EDITABLE, TRUE,
-          COL_ENABLED, enabled,
-          -1);
-    }
-
-  g_list_foreach (all_groups, (GFunc) g_free, NULL);
-  g_list_foreach (my_groups, (GFunc) g_free, NULL);
-  g_list_free (all_groups);
-  g_list_free (my_groups);
-}
-
-static gboolean
-contact_widget_model_find_name_foreach (GtkTreeModel *model,
-                                        GtkTreePath *path,
-                                        GtkTreeIter *iter,
-                                        FindName *data)
-{
-  gchar *name;
-
-  gtk_tree_model_get (model, iter,
-      COL_NAME, &name,
-      -1);
-
-  if (!name)
-      return FALSE;
-
-  if (data->name && strcmp (data->name, name) == 0)
-    {
-      data->found = TRUE;
-      data->found_iter = *iter;
-
-      g_free (name);
-
-      return TRUE;
-    }
-
-  g_free (name);
-
-  return FALSE;
-}
-
-static gboolean
-contact_widget_model_find_name (EmpathyContactWidget *information,
-                                const gchar *name,
-                                GtkTreeIter *iter)
-{
-  GtkTreeView *view;
-  GtkTreeModel *model;
-  FindName data;
-
-  if (EMP_STR_EMPTY (name))
-      return FALSE;
-
-  data.information = information;
-  data.name = name;
-  data.found = FALSE;
-
-  view = GTK_TREE_VIEW (information->treeview_groups);
-  model = gtk_tree_view_get_model (view);
-
-  gtk_tree_model_foreach (model,
-      (GtkTreeModelForeachFunc) contact_widget_model_find_name_foreach,
-      &data);
-
-  if (data.found == TRUE)
-    {
-      *iter = data.found_iter;
-      return TRUE;
-    }
-
-  return FALSE;
-}
-
-static void
-contact_widget_entry_group_changed_cb (GtkEditable *editable,
-                                       EmpathyContactWidget *information)
-{
-  GtkTreeIter iter;
-  const gchar *group;
-
-  group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
-
-  if (contact_widget_model_find_name (information, group, &iter))
-      gtk_widget_set_sensitive (GTK_WIDGET (information->button_group), FALSE);
-  else
-      gtk_widget_set_sensitive (GTK_WIDGET (information->button_group),
-          !EMP_STR_EMPTY (group));
-}
-
-static void
-contact_widget_entry_group_activate_cb (GtkEntry *entry,
-                                        EmpathyContactWidget  *information)
-{
-  gtk_widget_activate (GTK_WIDGET (information->button_group));
-}
-
-static void
-contact_widget_button_group_clicked_cb (GtkButton *button,
-                                        EmpathyContactWidget *information)
-{
-  GtkTreeView *view;
-  GtkListStore *store;
-  GtkTreeIter iter;
-  const gchar *group;
-
-  view = GTK_TREE_VIEW (information->treeview_groups);
-  store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
-
-  group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
-
-  gtk_list_store_append (store, &iter);
-  gtk_list_store_set (store, &iter,
-      COL_NAME, group,
-      COL_ENABLED, TRUE,
-      -1);
-
-  empathy_contact_change_group (information->contact, group, TRUE);
-}
-
-static void
-contact_widget_groups_notify_cb (EmpathyContactWidget *information)
-{
-  /* FIXME: not implemented */
-}
-
-static void
-contact_widget_groups_setup (EmpathyContactWidget *information)
-{
-  if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS)
-    {
-      contact_widget_model_setup (information);
-    }
-}
-
 static void
 contact_widget_groups_update (EmpathyContactWidget *information)
 {
   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS &&
-      information->contact)
+      information->contact != NULL)
     {
-      g_signal_connect_swapped (information->contact, "notify::groups",
-          G_CALLBACK (contact_widget_groups_notify_cb), information);
-      contact_widget_groups_populate_data (information);
+      FolksPersona *persona =
+          empathy_contact_get_persona (information->contact);
+
+      if (FOLKS_IS_GROUPABLE (persona))
+        {
+          empathy_groups_widget_set_groupable (
+              EMPATHY_GROUPS_WIDGET (information->groups_widget),
+              FOLKS_GROUPABLE (persona));
+          gtk_widget_show (information->groups_widget);
 
-      gtk_widget_show (information->vbox_groups);
+          return;
+        }
     }
-  else
-      gtk_widget_hide (information->vbox_groups);
+
+  /* In case of failure */
+  gtk_widget_hide (information->groups_widget);
 }
 
 /* Converts the Location's GHashTable's key to a user readable string */
@@ -957,13 +786,17 @@ contact_widget_location_update (EmpathyContactWidget *information)
       gchar *text;
       gint64 stamp;
       time_t time_;
+      gchar *tmp;
 
       stamp = g_value_get_int64 (value);
       time_ = stamp;
 
       user_date = empathy_time_to_string_relative (time_);
 
-      text = g_strconcat ( _("<b>Location</b>, "), user_date, NULL);
+      tmp = g_strdup_printf ("<b>%s</b>", _("Location"));
+      /* translators: format is "Location, $date" */
+      text = g_strdup_printf (_("%s, %s"), tmp, user_date);
+      g_free (tmp);
       gtk_label_set_markup (GTK_LABEL (information->label_location), text);
       g_free (user_date);
       g_free (text);
@@ -1033,7 +866,7 @@ contact_widget_location_update (EmpathyContactWidget *information)
       row++;
     }
 
-#if HAVE_LIBCHAMPLAIN
+#ifdef HAVE_LIBCHAMPLAIN
   if (has_position &&
       !(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
     {
@@ -1055,7 +888,7 @@ contact_widget_location_update (EmpathyContactWidget *information)
       return;
     }
 
-#if HAVE_LIBCHAMPLAIN
+#ifdef HAVE_LIBCHAMPLAIN
   if (display_map)
     {
       ClutterActor *marker;
@@ -1421,8 +1254,6 @@ contact_widget_remove_contact (EmpathyContactWidget *information)
           contact_widget_presence_notify_cb, information);
       g_signal_handlers_disconnect_by_func (information->contact,
           contact_widget_avatar_notify_cb, information);
-      g_signal_handlers_disconnect_by_func (information->contact,
-          contact_widget_groups_notify_cb, information);
 
       tp_contact = empathy_contact_get_tp_contact (information->contact);
       if (tp_contact != NULL)
@@ -1437,9 +1268,8 @@ contact_widget_remove_contact (EmpathyContactWidget *information)
 
   if (information->details_cancellable != NULL)
     {
-      /* The cancellable will be unreffed and cleared in
-       * contact_widget_details_request_cb */
       g_cancellable_cancel (information->details_cancellable);
+      tp_clear_object (&information->details_cancellable);
     }
 }
 
@@ -1659,8 +1489,7 @@ favourite_toggled_cb (GtkToggleButton *button,
 static void
 contact_widget_contact_setup (EmpathyContactWidget *information)
 {
-  /* Setup label_status as a KludgeLabel */
-  information->label_status = empathy_kludge_label_new ("");
+  information->label_status = gtk_label_new ("");
   gtk_label_set_line_wrap_mode (GTK_LABEL (information->label_status),
                                 PANGO_WRAP_WORD_CHAR);
   gtk_label_set_line_wrap (GTK_LABEL (information->label_status),
@@ -1868,13 +1697,10 @@ empathy_contact_widget_new (EmpathyContact *contact,
        "vbox_location", &information->vbox_location,
        "subvbox_location", &information->subvbox_location,
        "label_location", &information->label_location,
-#if HAVE_LIBCHAMPLAIN
+#ifdef HAVE_LIBCHAMPLAIN
        "viewport_map", &information->viewport_map,
 #endif
-       "vbox_groups", &information->vbox_groups,
-       "entry_group", &information->entry_group,
-       "button_group", &information->button_group,
-       "treeview_groups", &information->treeview_groups,
+       "groups_widget", &information->groups_widget,
        "vbox_details", &information->vbox_details,
        "table_details", &information->table_details,
        "hbox_details_requested", &information->hbox_details_requested,
@@ -1886,9 +1712,6 @@ empathy_contact_widget_new (EmpathyContact *contact,
 
   empathy_builder_connect (gui, information,
       "vbox_contact_widget", "destroy", contact_widget_destroy_cb,
-      "entry_group", "changed", contact_widget_entry_group_changed_cb,
-      "entry_group", "activate", contact_widget_entry_group_activate_cb,
-      "button_group", "clicked", contact_widget_button_group_clicked_cb,
       NULL);
   information->table_location = NULL;
 
@@ -1900,7 +1723,6 @@ empathy_contact_widget_new (EmpathyContact *contact,
 
   /* Create widgets */
   contact_widget_contact_setup (information);
-  contact_widget_groups_setup (information);
   contact_widget_details_setup (information);
   contact_widget_client_setup (information);