]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-contact-chooser.c
Move should_create_salut_account to local-xmpp-assistant-widget
[empathy.git] / libempathy-gtk / empathy-contact-chooser.c
index 7f31e711e5b1c8fca3f7a82847d0b2b936a36812..ccdd5fa638d41422f76d2da2b677434879fce5a2 100644 (file)
@@ -14,6 +14,9 @@
 
 #include "empathy-contact-chooser.h"
 
+#include <libempathy/empathy-utils.h>
+
+#include <libempathy-gtk/empathy-individual-store-manager.h>
 #include <libempathy-gtk/empathy-individual-view.h>
 #include <libempathy-gtk/empathy-ui-utils.h>
 
@@ -37,6 +40,7 @@ struct _EmpathyContactChooserPrivate
   EmpathyIndividualStore *store;
   EmpathyIndividualView *view;
   GtkWidget *search_entry;
+  GtkWidget *scroll_view;
 
   GPtrArray *search_words;
   gchar *search_str;
@@ -47,6 +51,9 @@ struct _EmpathyContactChooserPrivate
 
   EmpathyContactChooserFilterFunc filter_func;
   gpointer filter_data;
+
+  /* list of reffed TpContact */
+  GList *tp_contacts;
 };
 
 struct _AddTemporaryIndividualCtx
@@ -100,6 +107,9 @@ contact_chooser_dispose (GObject *object)
 
   tp_clear_object (&self->priv->account_mgr);
 
+  g_list_free_full (self->priv->tp_contacts, g_object_unref);
+  self->priv->tp_contacts = NULL;
+
   G_OBJECT_CLASS (empathy_contact_chooser_parent_class)->dispose (
       object);
 }
@@ -207,11 +217,8 @@ get_contacts_cb (TpConnection *connection,
   EmpathyContactChooser *self =
     (EmpathyContactChooser *) weak_object;
   AddTemporaryIndividualCtx *ctx = user_data;
-  TpAccount *account;
-  TpfPersonaStore *store;
   FolksIndividual *individual;
-  TpfPersona *persona_new;
-  GeeSet *personas;
+  TpContact *contact;
 
   if (self->priv->add_temp_ctx != ctx)
     /* another request has been started */
@@ -220,20 +227,20 @@ get_contacts_cb (TpConnection *connection,
   if (n_contacts != 1)
     return;
 
-  account = tp_connection_get_account (connection);
+  contact = contacts[0];
 
-  store = tpf_persona_store_new (account);
-  personas = GEE_SET (
-      gee_hash_set_new (FOLKS_TYPE_PERSONA, g_object_ref, g_object_unref,
-      g_direct_hash, g_direct_equal));
-  persona_new = tpf_persona_new (contacts[0], store);
-  gee_collection_add (GEE_COLLECTION (personas),
-      tpf_persona_new (contacts[0], store));
+  individual =  empathy_create_individual_from_tp_contact (contact);
+  if (individual == NULL)
+    return;
 
-  individual = folks_individual_new (personas);
+  /* tp-glib will unref the TpContact once we return from this callback
+   * but folks expect us to keep a reference on the TpContact.
+   * Ideally folks shouldn't force us to do that: bgo #666580 */
+  self->priv->tp_contacts = g_list_prepend (self->priv->tp_contacts,
+      g_object_ref (contact));
 
   /* listen for updates to the capabilities */
-  tp_g_signal_connect_object (contacts[0], "notify::capabilities",
+  tp_g_signal_connect_object (contact, "notify::capabilities",
       G_CALLBACK (contact_capabilities_changed), self, 0);
 
   /* Pass ownership to the list */
@@ -246,10 +253,6 @@ get_contacts_cb (TpConnection *connection,
         gtk_tree_view_get_selection (GTK_TREE_VIEW (self->priv->view)),
         NULL, NULL))
     empathy_individual_view_select_first (self->priv->view);
-
-  g_clear_object (&persona_new);
-  g_clear_object (&personas);
-  g_object_unref (store);
 }
 
 static void
@@ -383,7 +386,6 @@ empathy_contact_chooser_init (EmpathyContactChooser *self)
 {
   EmpathyIndividualManager *mgr;
   GtkTreeSelection *selection;
-  GtkWidget *scroll;
   GQuark features[] = { TP_ACCOUNT_MANAGER_FEATURE_CORE, 0 };
 
   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_CONTACT_CHOOSER,
@@ -411,7 +413,8 @@ empathy_contact_chooser_init (EmpathyContactChooser *self)
 
   /* Add the treeview */
   mgr = empathy_individual_manager_dup_singleton ();
-  self->priv->store = empathy_individual_store_new (mgr);
+  self->priv->store = EMPATHY_INDIVIDUAL_STORE (
+      empathy_individual_store_manager_new (mgr));
   g_object_unref (mgr);
 
   empathy_individual_store_set_show_groups (self->priv->store, FALSE);
@@ -429,13 +432,14 @@ empathy_contact_chooser_init (EmpathyContactChooser *self)
   g_signal_connect (self->priv->view, "row-activated",
       G_CALLBACK (view_activate_cb), self);
 
-  scroll = gtk_scrolled_window_new (NULL, NULL);
+  self->priv->scroll_view = gtk_scrolled_window_new (NULL, NULL);
 
-  gtk_container_add (GTK_CONTAINER (scroll), GTK_WIDGET (self->priv->view));
+  gtk_container_add (GTK_CONTAINER (self->priv->scroll_view),
+      GTK_WIDGET (self->priv->view));
 
-  gtk_box_pack_start (GTK_BOX (self), scroll, TRUE, TRUE, 6);
+  gtk_box_pack_start (GTK_BOX (self), self->priv->scroll_view, TRUE, TRUE, 6);
   gtk_widget_show (GTK_WIDGET (self->priv->view));
-  gtk_widget_show (scroll);
+  gtk_widget_show (self->priv->scroll_view);
 }
 
 GtkWidget *
@@ -446,6 +450,9 @@ empathy_contact_chooser_new (void)
       NULL);
 }
 
+/* Note that because of bgo #666580 the returned invidivdual is valid until
+ * @self is destroyed. To keep it around after that, the TpContact associated
+ * with the individual needs to be manually reffed. */
 FolksIndividual *
 empathy_contact_chooser_dup_selected (EmpathyContactChooser *self)
 {
@@ -469,3 +476,10 @@ empathy_contact_chooser_show_search_entry (EmpathyContactChooser *self,
 {
   gtk_widget_set_visible (self->priv->search_entry, show);
 }
+
+void
+empathy_contact_chooser_show_tree_view (EmpathyContactChooser *self,
+    gboolean show)
+{
+  gtk_widget_set_visible (GTK_WIDGET (self->priv->scroll_view), show);
+}