2 * empathy-contact-chooser.c
4 * EmpathyContactChooser
6 * (c) 2009, Collabora Ltd.
9 * Danielle Madeley <danielle.madeley@collabora.co.uk>
12 #include <glib/gi18n.h>
13 #include <folks/folks-telepathy.h>
15 #include "empathy-contact-chooser.h"
17 #include <libempathy-gtk/empathy-individual-view.h>
18 #include <libempathy-gtk/empathy-ui-utils.h>
20 G_DEFINE_TYPE (EmpathyContactChooser,
21 empathy_contact_chooser, GTK_TYPE_BOX);
24 SIG_SELECTION_CHANGED,
29 static guint signals[LAST_SIGNAL];
31 typedef struct _AddTemporaryIndividualCtx AddTemporaryIndividualCtx;
33 struct _EmpathyContactChooserPrivate
35 TpAccountManager *account_mgr;
37 EmpathyIndividualStore *store;
38 EmpathyIndividualView *view;
39 GtkWidget *search_entry;
41 GPtrArray *search_words;
44 /* Context representing the FolksIndividual which are added because of the
45 * current search from the user. */
46 AddTemporaryIndividualCtx *add_temp_ctx;
48 EmpathyContactChooserFilterFunc filter_func;
52 struct _AddTemporaryIndividualCtx
54 EmpathyContactChooser *self;
55 /* List of owned FolksIndividual */
59 static AddTemporaryIndividualCtx *
60 add_temporary_individual_ctx_new (EmpathyContactChooser *self)
62 AddTemporaryIndividualCtx *ctx = g_slice_new0 (AddTemporaryIndividualCtx);
69 add_temporary_individual_ctx_free (AddTemporaryIndividualCtx *ctx)
73 /* Remove all the individuals from the model */
74 for (l = ctx->individuals; l != NULL; l = g_list_next (l))
76 FolksIndividual *individual = l->data;
78 individual_store_remove_individual_and_disconnect (ctx->self->priv->store,
81 g_object_unref (individual);
84 g_list_free (ctx->individuals);
85 g_slice_free (AddTemporaryIndividualCtx, ctx);
89 contact_chooser_dispose (GObject *object)
91 EmpathyContactChooser *self = (EmpathyContactChooser *)
94 tp_clear_pointer (&self->priv->add_temp_ctx,
95 add_temporary_individual_ctx_free);
97 tp_clear_object (&self->priv->store);
98 tp_clear_pointer (&self->priv->search_words, g_ptr_array_unref);
99 tp_clear_pointer (&self->priv->search_str, g_free);
101 tp_clear_object (&self->priv->account_mgr);
103 G_OBJECT_CLASS (empathy_contact_chooser_parent_class)->dispose (
108 empathy_contact_chooser_class_init (
109 EmpathyContactChooserClass *klass)
111 GObjectClass *object_class = G_OBJECT_CLASS (klass);
113 object_class->dispose = contact_chooser_dispose;
115 g_type_class_add_private (object_class,
116 sizeof (EmpathyContactChooserPrivate));
118 signals[SIG_SELECTION_CHANGED] = g_signal_new ("selection-changed",
119 G_TYPE_FROM_CLASS (klass),
123 g_cclosure_marshal_generic,
125 1, FOLKS_TYPE_INDIVIDUAL);
127 signals[SIG_ACTIVATE] = g_signal_new ("activate",
128 G_TYPE_FROM_CLASS (klass),
132 g_cclosure_marshal_generic,
138 view_selection_changed_cb (GtkWidget *treeview,
139 EmpathyContactChooser *self)
141 FolksIndividual *individual;
143 individual = empathy_individual_view_dup_selected (self->priv->view);
145 g_signal_emit (self, signals[SIG_SELECTION_CHANGED], 0, individual);
147 tp_clear_object (&individual);
151 filter_func (GtkTreeModel *model,
155 EmpathyContactChooser *self = user_data;
156 FolksIndividual *individual;
158 gboolean display = FALSE;
159 gboolean searching = FALSE;
161 gtk_tree_model_get (model, iter,
162 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual,
163 EMPATHY_INDIVIDUAL_STORE_COL_IS_ONLINE, &is_online,
166 if (individual == NULL)
169 if (self->priv->search_words != NULL)
173 /* Filter out the contact if we are searching and it doesn't match */
174 if (!empathy_individual_match_string (individual,
175 self->priv->search_str, self->priv->search_words))
179 if (self->priv->filter_func == NULL)
182 display = self->priv->filter_func (self, individual, is_online, searching,
183 self->priv->filter_data);
185 tp_clear_object (&individual);
190 get_contacts_cb (TpConnection *connection,
192 TpContact * const *contacts,
193 const gchar * const *requested_ids,
194 GHashTable *failed_id_errors,
197 GObject *weak_object)
199 EmpathyContactChooser *self =
200 (EmpathyContactChooser *) weak_object;
201 AddTemporaryIndividualCtx *ctx = user_data;
203 TpfPersonaStore *store;
204 FolksIndividual *individual;
205 TpfPersona *persona_new;
208 if (self->priv->add_temp_ctx != ctx)
209 /* another request has been started */
215 account = tp_connection_get_account (connection);
217 store = tpf_persona_store_new (account);
219 gee_hash_set_new (FOLKS_TYPE_PERSONA, g_object_ref, g_object_unref,
220 g_direct_hash, g_direct_equal));
221 persona_new = tpf_persona_new (contacts[0], store);
222 gee_collection_add (GEE_COLLECTION (personas),
223 tpf_persona_new (contacts[0], store));
225 individual = folks_individual_new (personas);
227 /* Pass ownership to the list */
228 ctx->individuals = g_list_prepend (ctx->individuals, individual);
230 individual_store_add_individual_and_connect (self->priv->store, individual);
232 /* Make sure that the first matching item is selected */
233 empathy_individual_view_select_first (self->priv->view);
235 g_clear_object (&persona_new);
236 g_clear_object (&personas);
237 g_object_unref (store);
241 add_temporary_individuals (EmpathyContactChooser *self,
246 tp_clear_pointer (&self->priv->add_temp_ctx,
247 add_temporary_individual_ctx_free);
249 if (tp_str_empty (id))
252 self->priv->add_temp_ctx = add_temporary_individual_ctx_new (self);
254 /* Try to add an individual for each connected account */
255 accounts = tp_account_manager_get_valid_accounts (self->priv->account_mgr);
256 for (l = accounts; l != NULL; l = g_list_next (l))
258 TpAccount *account = l->data;
260 TpContactFeature features[] = { TP_CONTACT_FEATURE_ALIAS,
261 TP_CONTACT_FEATURE_AVATAR_DATA,
262 TP_CONTACT_FEATURE_PRESENCE,
263 TP_CONTACT_FEATURE_CAPABILITIES };
265 conn = tp_account_get_connection (account);
269 tp_connection_get_contacts_by_id (conn, 1, &id, G_N_ELEMENTS (features),
270 features, get_contacts_cb, self->priv->add_temp_ctx, NULL,
274 g_list_free (accounts);
278 search_text_changed (GtkEntry *entry,
279 EmpathyContactChooser *self)
283 tp_clear_pointer (&self->priv->search_words, g_ptr_array_unref);
284 tp_clear_pointer (&self->priv->search_str, g_free);
286 id = gtk_entry_get_text (entry);
288 self->priv->search_words = empathy_live_search_strip_utf8_string (id);
289 self->priv->search_str = g_strdup (id);
291 add_temporary_individuals (self, id);
293 empathy_individual_view_refilter (self->priv->view);
297 search_activate_cb (GtkEntry *entry,
298 EmpathyContactChooser *self)
300 g_signal_emit (self, signals[SIG_ACTIVATE], 0);
304 view_activate_cb (GtkTreeView *view,
306 GtkTreeViewColumn *column,
307 EmpathyContactChooser *self)
309 g_signal_emit (self, signals[SIG_ACTIVATE], 0);
313 empathy_contact_chooser_init (EmpathyContactChooser *self)
315 EmpathyIndividualManager *mgr;
316 GtkTreeSelection *selection;
318 GQuark features[] = { TP_ACCOUNT_MANAGER_FEATURE_CORE, 0 };
320 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_CONTACT_CHOOSER,
321 EmpathyContactChooserPrivate);
323 self->priv->account_mgr = tp_account_manager_dup ();
325 /* We don't wait for the CORE feature to be prepared, which is fine as we
326 * won't use the account manager until user starts searching. Furthermore,
327 * the AM has probably already been prepared by another Empathy
329 tp_proxy_prepare_async (self->priv->account_mgr, features, NULL, NULL);
332 self->priv->search_entry = gtk_entry_new ();
333 gtk_box_pack_start (GTK_BOX (self), self->priv->search_entry, FALSE, TRUE, 6);
334 gtk_widget_show (self->priv->search_entry);
336 g_signal_connect (self->priv->search_entry, "changed",
337 G_CALLBACK (search_text_changed), self);
338 g_signal_connect (self->priv->search_entry, "activate",
339 G_CALLBACK (search_activate_cb), self);
341 /* Add the treeview */
342 mgr = empathy_individual_manager_dup_singleton ();
343 self->priv->store = empathy_individual_store_new (mgr);
344 g_object_unref (mgr);
346 empathy_individual_store_set_show_groups (self->priv->store, FALSE);
348 self->priv->view = empathy_individual_view_new (self->priv->store,
349 EMPATHY_INDIVIDUAL_VIEW_FEATURE_NONE, EMPATHY_INDIVIDUAL_FEATURE_NONE);
351 empathy_individual_view_set_custom_filter (self->priv->view,
354 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self->priv->view));
356 g_signal_connect (selection, "changed",
357 G_CALLBACK (view_selection_changed_cb), self);
358 g_signal_connect (self->priv->view, "row-activated",
359 G_CALLBACK (view_activate_cb), self);
361 scroll = gtk_scrolled_window_new (NULL, NULL);
363 gtk_container_add (GTK_CONTAINER (scroll), GTK_WIDGET (self->priv->view));
365 gtk_box_pack_start (GTK_BOX (self), scroll, TRUE, TRUE, 6);
366 gtk_widget_show (GTK_WIDGET (self->priv->view));
367 gtk_widget_show (scroll);
371 empathy_contact_chooser_new (void)
373 return g_object_new (EMPATHY_TYPE_CONTACT_CHOOSER,
374 "orientation", GTK_ORIENTATION_VERTICAL,
379 empathy_contact_chooser_dup_selected (EmpathyContactChooser *self)
381 return empathy_individual_view_dup_selected (self->priv->view);
385 empathy_contact_chooser_set_filter_func (EmpathyContactChooser *self,
386 EmpathyContactChooserFilterFunc func,
389 g_assert (self->priv->filter_func == NULL);
391 self->priv->filter_func = func;
392 self->priv->filter_data = user_data;
396 empathy_contact_chooser_show_search_entry (EmpathyContactChooser *self,
399 gtk_widget_set_visible (self->priv->search_entry, show);