2 * empathy-contact-chooser.c
4 * EmpathyContactChooser
6 * (c) 2009, Collabora Ltd.
9 * Danielle Madeley <danielle.madeley@collabora.co.uk>
14 #include <glib/gi18n.h>
15 #include <folks/folks-telepathy.h>
17 #include "empathy-contact-chooser.h"
19 #include <libempathy/empathy-utils.h>
20 #include <libempathy/empathy-client-factory.h>
22 #include <libempathy-gtk/empathy-individual-store-manager.h>
23 #include <libempathy-gtk/empathy-individual-view.h>
24 #include <libempathy-gtk/empathy-ui-utils.h>
26 G_DEFINE_TYPE (EmpathyContactChooser,
27 empathy_contact_chooser, GTK_TYPE_BOX);
30 SIG_SELECTION_CHANGED,
35 static guint signals[LAST_SIGNAL];
37 typedef struct _AddTemporaryIndividualCtx AddTemporaryIndividualCtx;
39 struct _EmpathyContactChooserPrivate
41 TpAccountManager *account_mgr;
43 EmpathyIndividualStore *store;
44 EmpathyIndividualView *view;
45 GtkWidget *search_entry;
46 GtkWidget *scroll_view;
48 GPtrArray *search_words;
51 /* Context representing the FolksIndividual which are added because of the
52 * current search from the user. */
53 AddTemporaryIndividualCtx *add_temp_ctx;
55 EmpathyContactChooserFilterFunc filter_func;
58 /* list of reffed TpContact */
62 struct _AddTemporaryIndividualCtx
64 EmpathyContactChooser *self;
65 /* List of owned FolksIndividual */
69 static AddTemporaryIndividualCtx *
70 add_temporary_individual_ctx_new (EmpathyContactChooser *self)
72 AddTemporaryIndividualCtx *ctx = g_slice_new0 (AddTemporaryIndividualCtx);
79 add_temporary_individual_ctx_free (AddTemporaryIndividualCtx *ctx)
83 /* Remove all the individuals from the model */
84 for (l = ctx->individuals; l != NULL; l = g_list_next (l))
86 FolksIndividual *individual = l->data;
88 individual_store_remove_individual_and_disconnect (ctx->self->priv->store,
91 g_object_unref (individual);
94 g_list_free (ctx->individuals);
95 g_slice_free (AddTemporaryIndividualCtx, ctx);
99 contact_chooser_dispose (GObject *object)
101 EmpathyContactChooser *self = (EmpathyContactChooser *)
104 tp_clear_pointer (&self->priv->add_temp_ctx,
105 add_temporary_individual_ctx_free);
107 tp_clear_object (&self->priv->store);
108 tp_clear_pointer (&self->priv->search_words, g_ptr_array_unref);
109 tp_clear_pointer (&self->priv->search_str, g_free);
111 tp_clear_object (&self->priv->account_mgr);
113 g_list_free_full (self->priv->tp_contacts, g_object_unref);
114 self->priv->tp_contacts = NULL;
116 G_OBJECT_CLASS (empathy_contact_chooser_parent_class)->dispose (
121 empathy_contact_chooser_class_init (
122 EmpathyContactChooserClass *klass)
124 GObjectClass *object_class = G_OBJECT_CLASS (klass);
126 object_class->dispose = contact_chooser_dispose;
128 g_type_class_add_private (object_class,
129 sizeof (EmpathyContactChooserPrivate));
131 signals[SIG_SELECTION_CHANGED] = g_signal_new ("selection-changed",
132 G_TYPE_FROM_CLASS (klass),
136 g_cclosure_marshal_generic,
138 1, FOLKS_TYPE_INDIVIDUAL);
140 signals[SIG_ACTIVATE] = g_signal_new ("activate",
141 G_TYPE_FROM_CLASS (klass),
145 g_cclosure_marshal_generic,
151 view_selection_changed_cb (GtkWidget *treeview,
152 EmpathyContactChooser *self)
154 FolksIndividual *individual;
156 individual = empathy_individual_view_dup_selected (self->priv->view);
158 g_signal_emit (self, signals[SIG_SELECTION_CHANGED], 0, individual);
160 tp_clear_object (&individual);
164 filter_func (GtkTreeModel *model,
168 EmpathyContactChooser *self = user_data;
169 FolksIndividual *individual;
171 gboolean display = FALSE;
172 gboolean searching = FALSE;
174 gtk_tree_model_get (model, iter,
175 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual,
176 EMPATHY_INDIVIDUAL_STORE_COL_IS_ONLINE, &is_online,
179 if (individual == NULL)
182 if (self->priv->search_words != NULL)
186 /* Filter out the contact if we are searching and it doesn't match */
187 if (!empathy_individual_match_string (individual,
188 self->priv->search_str, self->priv->search_words))
192 if (self->priv->filter_func == NULL)
195 display = self->priv->filter_func (self, individual, is_online, searching,
196 self->priv->filter_data);
198 tp_clear_object (&individual);
203 contact_capabilities_changed (TpContact *contact,
205 EmpathyContactChooser *self)
207 empathy_individual_view_refilter (self->priv->view);
211 get_contacts_cb (GObject *source,
212 GAsyncResult *result,
215 TpWeakRef *wr = user_data;
216 AddTemporaryIndividualCtx *ctx;
217 EmpathyContactChooser *self;
218 GError *error = NULL;
219 FolksIndividual *individual;
221 EmpathyContact *emp_contact;
223 self = tp_weak_ref_dup_object (wr);
227 ctx = tp_weak_ref_get_user_data (wr);
229 emp_contact = empathy_client_factory_dup_contact_by_id_finish (
230 EMPATHY_CLIENT_FACTORY (source), result, &error);
231 if (emp_contact == NULL)
234 contact = empathy_contact_get_tp_contact (emp_contact);
236 if (self->priv->add_temp_ctx != ctx)
237 /* another request has been started */
240 individual = empathy_create_individual_from_tp_contact (contact);
241 if (individual == NULL)
244 /* tp-glib will unref the TpContact once we return from this callback
245 * but folks expect us to keep a reference on the TpContact.
246 * Ideally folks shouldn't force us to do that: bgo #666580 */
247 self->priv->tp_contacts = g_list_prepend (self->priv->tp_contacts,
248 g_object_ref (contact));
250 /* listen for updates to the capabilities */
251 tp_g_signal_connect_object (contact, "notify::capabilities",
252 G_CALLBACK (contact_capabilities_changed), self, 0);
254 /* Pass ownership to the list */
255 ctx->individuals = g_list_prepend (ctx->individuals, individual);
257 individual_store_add_individual_and_connect (self->priv->store, individual);
259 /* if nothing is selected, select the first matching node */
260 if (!gtk_tree_selection_get_selected (
261 gtk_tree_view_get_selection (GTK_TREE_VIEW (self->priv->view)),
263 empathy_individual_view_select_first (self->priv->view);
266 g_clear_object (&emp_contact);
267 g_clear_object (&self);
268 tp_weak_ref_destroy (wr);
272 add_temporary_individuals (EmpathyContactChooser *self,
277 tp_clear_pointer (&self->priv->add_temp_ctx,
278 add_temporary_individual_ctx_free);
280 if (tp_str_empty (id))
283 self->priv->add_temp_ctx = add_temporary_individual_ctx_new (self);
285 /* Try to add an individual for each connected account */
286 accounts = tp_account_manager_get_valid_accounts (self->priv->account_mgr);
287 for (l = accounts; l != NULL; l = g_list_next (l))
289 TpAccount *account = l->data;
291 EmpathyClientFactory *factory;
293 conn = tp_account_get_connection (account);
297 factory = empathy_client_factory_dup ();
299 empathy_client_factory_dup_contact_by_id_async (factory, conn, id,
301 tp_weak_ref_new (self, self->priv->add_temp_ctx, NULL));
303 g_object_unref (factory);
306 g_list_free (accounts);
310 search_text_changed (GtkEntry *entry,
311 EmpathyContactChooser *self)
315 tp_clear_pointer (&self->priv->search_words, g_ptr_array_unref);
316 tp_clear_pointer (&self->priv->search_str, g_free);
318 id = gtk_entry_get_text (entry);
320 self->priv->search_words = empathy_live_search_strip_utf8_string (id);
321 self->priv->search_str = g_strdup (id);
323 add_temporary_individuals (self, id);
325 empathy_individual_view_refilter (self->priv->view);
329 search_activate_cb (GtkEntry *entry,
330 EmpathyContactChooser *self)
332 g_signal_emit (self, signals[SIG_ACTIVATE], 0);
336 view_activate_cb (GtkTreeView *view,
338 GtkTreeViewColumn *column,
339 EmpathyContactChooser *self)
341 g_signal_emit (self, signals[SIG_ACTIVATE], 0);
345 search_key_press_cb (GtkEntry *entry,
347 EmpathyContactChooser *self)
349 GtkTreeSelection *selection;
353 if (event->state != 0)
356 switch (event->keyval)
359 case GDK_KEY_KP_Down:
368 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self->priv->view));
370 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
373 switch (event->keyval)
376 case GDK_KEY_KP_Down:
377 if (!gtk_tree_model_iter_next (model, &iter))
384 if (!gtk_tree_model_iter_previous (model, &iter))
390 g_assert_not_reached ();
393 gtk_tree_selection_select_iter (selection, &iter);
399 empathy_contact_chooser_init (EmpathyContactChooser *self)
401 EmpathyIndividualManager *mgr;
402 GtkTreeSelection *selection;
403 GQuark features[] = { TP_ACCOUNT_MANAGER_FEATURE_CORE, 0 };
405 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_CONTACT_CHOOSER,
406 EmpathyContactChooserPrivate);
408 self->priv->account_mgr = tp_account_manager_dup ();
410 /* We don't wait for the CORE feature to be prepared, which is fine as we
411 * won't use the account manager until user starts searching. Furthermore,
412 * the AM has probably already been prepared by another Empathy
414 tp_proxy_prepare_async (self->priv->account_mgr, features, NULL, NULL);
417 self->priv->search_entry = gtk_entry_new ();
418 gtk_box_pack_start (GTK_BOX (self), self->priv->search_entry, FALSE, TRUE, 6);
419 gtk_widget_show (self->priv->search_entry);
421 g_signal_connect (self->priv->search_entry, "changed",
422 G_CALLBACK (search_text_changed), self);
423 g_signal_connect (self->priv->search_entry, "activate",
424 G_CALLBACK (search_activate_cb), self);
425 g_signal_connect (self->priv->search_entry, "key-press-event",
426 G_CALLBACK (search_key_press_cb), self);
428 /* Add the treeview */
429 mgr = empathy_individual_manager_dup_singleton ();
430 self->priv->store = EMPATHY_INDIVIDUAL_STORE (
431 empathy_individual_store_manager_new (mgr));
432 g_object_unref (mgr);
434 empathy_individual_store_set_show_groups (self->priv->store, FALSE);
436 self->priv->view = empathy_individual_view_new (self->priv->store,
437 EMPATHY_INDIVIDUAL_VIEW_FEATURE_NONE, EMPATHY_INDIVIDUAL_FEATURE_NONE);
439 empathy_individual_view_set_custom_filter (self->priv->view,
442 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self->priv->view));
444 g_signal_connect (selection, "changed",
445 G_CALLBACK (view_selection_changed_cb), self);
446 g_signal_connect (self->priv->view, "row-activated",
447 G_CALLBACK (view_activate_cb), self);
449 self->priv->scroll_view = gtk_scrolled_window_new (NULL, NULL);
451 gtk_container_add (GTK_CONTAINER (self->priv->scroll_view),
452 GTK_WIDGET (self->priv->view));
454 gtk_box_pack_start (GTK_BOX (self), self->priv->scroll_view, TRUE, TRUE, 6);
455 gtk_widget_show (GTK_WIDGET (self->priv->view));
456 gtk_widget_show (self->priv->scroll_view);
460 empathy_contact_chooser_new (void)
462 return g_object_new (EMPATHY_TYPE_CONTACT_CHOOSER,
463 "orientation", GTK_ORIENTATION_VERTICAL,
467 /* Note that because of bgo #666580 the returned invidivdual is valid until
468 * @self is destroyed. To keep it around after that, the TpContact associated
469 * with the individual needs to be manually reffed. */
471 empathy_contact_chooser_dup_selected (EmpathyContactChooser *self)
473 return empathy_individual_view_dup_selected (self->priv->view);
477 empathy_contact_chooser_set_filter_func (EmpathyContactChooser *self,
478 EmpathyContactChooserFilterFunc func,
481 g_assert (self->priv->filter_func == NULL);
483 self->priv->filter_func = func;
484 self->priv->filter_data = user_data;
488 empathy_contact_chooser_show_search_entry (EmpathyContactChooser *self,
491 gtk_widget_set_visible (self->priv->search_entry, show);
495 empathy_contact_chooser_show_tree_view (EmpathyContactChooser *self,
498 gtk_widget_set_visible (GTK_WIDGET (self->priv->scroll_view), show);