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>
21 #include <libempathy-gtk/empathy-individual-store-manager.h>
22 #include <libempathy-gtk/empathy-individual-view.h>
23 #include <libempathy-gtk/empathy-ui-utils.h>
25 G_DEFINE_TYPE (EmpathyContactChooser,
26 empathy_contact_chooser, GTK_TYPE_BOX);
29 SIG_SELECTION_CHANGED,
34 static guint signals[LAST_SIGNAL];
36 typedef struct _AddTemporaryIndividualCtx AddTemporaryIndividualCtx;
38 struct _EmpathyContactChooserPrivate
40 TpAccountManager *account_mgr;
42 EmpathyIndividualStore *store;
43 EmpathyIndividualView *view;
44 GtkWidget *search_entry;
45 GtkWidget *scroll_view;
47 GPtrArray *search_words;
50 /* Context representing the FolksIndividual which are added because of the
51 * current search from the user. */
52 AddTemporaryIndividualCtx *add_temp_ctx;
54 EmpathyContactChooserFilterFunc filter_func;
57 /* list of reffed TpContact */
61 struct _AddTemporaryIndividualCtx
63 EmpathyContactChooser *self;
64 /* List of owned FolksIndividual */
68 static AddTemporaryIndividualCtx *
69 add_temporary_individual_ctx_new (EmpathyContactChooser *self)
71 AddTemporaryIndividualCtx *ctx = g_slice_new0 (AddTemporaryIndividualCtx);
78 add_temporary_individual_ctx_free (AddTemporaryIndividualCtx *ctx)
82 /* Remove all the individuals from the model */
83 for (l = ctx->individuals; l != NULL; l = g_list_next (l))
85 FolksIndividual *individual = l->data;
87 individual_store_remove_individual_and_disconnect (ctx->self->priv->store,
90 g_object_unref (individual);
93 g_list_free (ctx->individuals);
94 g_slice_free (AddTemporaryIndividualCtx, ctx);
98 contact_chooser_dispose (GObject *object)
100 EmpathyContactChooser *self = (EmpathyContactChooser *)
103 tp_clear_pointer (&self->priv->add_temp_ctx,
104 add_temporary_individual_ctx_free);
106 tp_clear_object (&self->priv->store);
107 tp_clear_pointer (&self->priv->search_words, g_ptr_array_unref);
108 tp_clear_pointer (&self->priv->search_str, g_free);
110 tp_clear_object (&self->priv->account_mgr);
112 g_list_free_full (self->priv->tp_contacts, g_object_unref);
113 self->priv->tp_contacts = NULL;
115 G_OBJECT_CLASS (empathy_contact_chooser_parent_class)->dispose (
120 empathy_contact_chooser_class_init (
121 EmpathyContactChooserClass *klass)
123 GObjectClass *object_class = G_OBJECT_CLASS (klass);
125 object_class->dispose = contact_chooser_dispose;
127 g_type_class_add_private (object_class,
128 sizeof (EmpathyContactChooserPrivate));
130 signals[SIG_SELECTION_CHANGED] = g_signal_new ("selection-changed",
131 G_TYPE_FROM_CLASS (klass),
135 g_cclosure_marshal_generic,
137 1, FOLKS_TYPE_INDIVIDUAL);
139 signals[SIG_ACTIVATE] = g_signal_new ("activate",
140 G_TYPE_FROM_CLASS (klass),
144 g_cclosure_marshal_generic,
150 view_selection_changed_cb (GtkWidget *treeview,
151 EmpathyContactChooser *self)
153 FolksIndividual *individual;
155 individual = empathy_individual_view_dup_selected (self->priv->view);
157 g_signal_emit (self, signals[SIG_SELECTION_CHANGED], 0, individual);
159 tp_clear_object (&individual);
163 filter_func (GtkTreeModel *model,
167 EmpathyContactChooser *self = user_data;
168 FolksIndividual *individual;
170 gboolean display = FALSE;
171 gboolean searching = FALSE;
173 gtk_tree_model_get (model, iter,
174 EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual,
175 EMPATHY_INDIVIDUAL_STORE_COL_IS_ONLINE, &is_online,
178 if (individual == NULL)
181 if (self->priv->search_words != NULL)
185 /* Filter out the contact if we are searching and it doesn't match */
186 if (!empathy_individual_match_string (individual,
187 self->priv->search_str, self->priv->search_words))
191 if (self->priv->filter_func == NULL)
194 display = self->priv->filter_func (self, individual, is_online, searching,
195 self->priv->filter_data);
197 tp_clear_object (&individual);
202 contact_capabilities_changed (TpContact *contact,
204 EmpathyContactChooser *self)
206 empathy_individual_view_refilter (self->priv->view);
210 get_contacts_cb (TpConnection *connection,
212 TpContact * const *contacts,
213 const gchar * const *requested_ids,
214 GHashTable *failed_id_errors,
217 GObject *weak_object)
219 EmpathyContactChooser *self =
220 (EmpathyContactChooser *) weak_object;
221 AddTemporaryIndividualCtx *ctx = user_data;
222 FolksIndividual *individual;
225 if (self->priv->add_temp_ctx != ctx)
226 /* another request has been started */
232 contact = contacts[0];
234 individual = empathy_create_individual_from_tp_contact (contact);
235 if (individual == NULL)
238 /* tp-glib will unref the TpContact once we return from this callback
239 * but folks expect us to keep a reference on the TpContact.
240 * Ideally folks shouldn't force us to do that: bgo #666580 */
241 self->priv->tp_contacts = g_list_prepend (self->priv->tp_contacts,
242 g_object_ref (contact));
244 /* listen for updates to the capabilities */
245 tp_g_signal_connect_object (contact, "notify::capabilities",
246 G_CALLBACK (contact_capabilities_changed), self, 0);
248 /* Pass ownership to the list */
249 ctx->individuals = g_list_prepend (ctx->individuals, individual);
251 individual_store_add_individual_and_connect (self->priv->store, individual);
253 /* if nothing is selected, select the first matching node */
254 if (!gtk_tree_selection_get_selected (
255 gtk_tree_view_get_selection (GTK_TREE_VIEW (self->priv->view)),
257 empathy_individual_view_select_first (self->priv->view);
261 add_temporary_individuals (EmpathyContactChooser *self,
266 tp_clear_pointer (&self->priv->add_temp_ctx,
267 add_temporary_individual_ctx_free);
269 if (tp_str_empty (id))
272 self->priv->add_temp_ctx = add_temporary_individual_ctx_new (self);
274 /* Try to add an individual for each connected account */
275 accounts = tp_account_manager_get_valid_accounts (self->priv->account_mgr);
276 for (l = accounts; l != NULL; l = g_list_next (l))
278 TpAccount *account = l->data;
280 TpContactFeature features[] = { TP_CONTACT_FEATURE_ALIAS,
281 TP_CONTACT_FEATURE_AVATAR_DATA,
282 TP_CONTACT_FEATURE_PRESENCE,
283 TP_CONTACT_FEATURE_CAPABILITIES };
285 conn = tp_account_get_connection (account);
289 tp_connection_get_contacts_by_id (conn, 1, &id, G_N_ELEMENTS (features),
290 features, get_contacts_cb, self->priv->add_temp_ctx, NULL,
294 g_list_free (accounts);
298 search_text_changed (GtkEntry *entry,
299 EmpathyContactChooser *self)
303 tp_clear_pointer (&self->priv->search_words, g_ptr_array_unref);
304 tp_clear_pointer (&self->priv->search_str, g_free);
306 id = gtk_entry_get_text (entry);
308 self->priv->search_words = empathy_live_search_strip_utf8_string (id);
309 self->priv->search_str = g_strdup (id);
311 add_temporary_individuals (self, id);
313 empathy_individual_view_refilter (self->priv->view);
317 search_activate_cb (GtkEntry *entry,
318 EmpathyContactChooser *self)
320 g_signal_emit (self, signals[SIG_ACTIVATE], 0);
324 view_activate_cb (GtkTreeView *view,
326 GtkTreeViewColumn *column,
327 EmpathyContactChooser *self)
329 g_signal_emit (self, signals[SIG_ACTIVATE], 0);
333 search_key_press_cb (GtkEntry *entry,
335 EmpathyContactChooser *self)
337 GtkTreeSelection *selection;
341 if (event->state != 0)
344 switch (event->keyval)
347 case GDK_KEY_KP_Down:
356 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self->priv->view));
358 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
361 switch (event->keyval)
364 case GDK_KEY_KP_Down:
365 if (!gtk_tree_model_iter_next (model, &iter))
372 if (!gtk_tree_model_iter_previous (model, &iter))
378 g_assert_not_reached ();
381 gtk_tree_selection_select_iter (selection, &iter);
387 empathy_contact_chooser_init (EmpathyContactChooser *self)
389 EmpathyIndividualManager *mgr;
390 GtkTreeSelection *selection;
391 GQuark features[] = { TP_ACCOUNT_MANAGER_FEATURE_CORE, 0 };
393 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_CONTACT_CHOOSER,
394 EmpathyContactChooserPrivate);
396 self->priv->account_mgr = tp_account_manager_dup ();
398 /* We don't wait for the CORE feature to be prepared, which is fine as we
399 * won't use the account manager until user starts searching. Furthermore,
400 * the AM has probably already been prepared by another Empathy
402 tp_proxy_prepare_async (self->priv->account_mgr, features, NULL, NULL);
405 self->priv->search_entry = gtk_entry_new ();
406 gtk_box_pack_start (GTK_BOX (self), self->priv->search_entry, FALSE, TRUE, 6);
407 gtk_widget_show (self->priv->search_entry);
409 g_signal_connect (self->priv->search_entry, "changed",
410 G_CALLBACK (search_text_changed), self);
411 g_signal_connect (self->priv->search_entry, "activate",
412 G_CALLBACK (search_activate_cb), self);
413 g_signal_connect (self->priv->search_entry, "key-press-event",
414 G_CALLBACK (search_key_press_cb), self);
416 /* Add the treeview */
417 mgr = empathy_individual_manager_dup_singleton ();
418 self->priv->store = EMPATHY_INDIVIDUAL_STORE (
419 empathy_individual_store_manager_new (mgr));
420 g_object_unref (mgr);
422 empathy_individual_store_set_show_groups (self->priv->store, FALSE);
424 self->priv->view = empathy_individual_view_new (self->priv->store,
425 EMPATHY_INDIVIDUAL_VIEW_FEATURE_NONE, EMPATHY_INDIVIDUAL_FEATURE_NONE);
427 empathy_individual_view_set_custom_filter (self->priv->view,
430 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self->priv->view));
432 g_signal_connect (selection, "changed",
433 G_CALLBACK (view_selection_changed_cb), self);
434 g_signal_connect (self->priv->view, "row-activated",
435 G_CALLBACK (view_activate_cb), self);
437 self->priv->scroll_view = gtk_scrolled_window_new (NULL, NULL);
439 gtk_container_add (GTK_CONTAINER (self->priv->scroll_view),
440 GTK_WIDGET (self->priv->view));
442 gtk_box_pack_start (GTK_BOX (self), self->priv->scroll_view, TRUE, TRUE, 6);
443 gtk_widget_show (GTK_WIDGET (self->priv->view));
444 gtk_widget_show (self->priv->scroll_view);
448 empathy_contact_chooser_new (void)
450 return g_object_new (EMPATHY_TYPE_CONTACT_CHOOSER,
451 "orientation", GTK_ORIENTATION_VERTICAL,
455 /* Note that because of bgo #666580 the returned invidivdual is valid until
456 * @self is destroyed. To keep it around after that, the TpContact associated
457 * with the individual needs to be manually reffed. */
459 empathy_contact_chooser_dup_selected (EmpathyContactChooser *self)
461 return empathy_individual_view_dup_selected (self->priv->view);
465 empathy_contact_chooser_set_filter_func (EmpathyContactChooser *self,
466 EmpathyContactChooserFilterFunc func,
469 g_assert (self->priv->filter_func == NULL);
471 self->priv->filter_func = func;
472 self->priv->filter_data = user_data;
476 empathy_contact_chooser_show_search_entry (EmpathyContactChooser *self,
479 gtk_widget_set_visible (self->priv->search_entry, show);
483 empathy_contact_chooser_show_tree_view (EmpathyContactChooser *self,
486 gtk_widget_set_visible (GTK_WIDGET (self->priv->scroll_view), show);