]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-chooser.c
[contact-chooser] Move the selection up/down without defocusing the entry
[empathy.git] / libempathy-gtk / empathy-contact-chooser.c
1 /*
2  * empathy-contact-chooser.c
3  *
4  * EmpathyContactChooser
5  *
6  * (c) 2009, Collabora Ltd.
7  *
8  * Authors:
9  *    Danielle Madeley <danielle.madeley@collabora.co.uk>
10  */
11
12 #include <glib/gi18n.h>
13 #include <folks/folks-telepathy.h>
14
15 #include "empathy-contact-chooser.h"
16
17 #include <libempathy-gtk/empathy-individual-view.h>
18 #include <libempathy-gtk/empathy-ui-utils.h>
19
20 G_DEFINE_TYPE (EmpathyContactChooser,
21     empathy_contact_chooser, GTK_TYPE_BOX);
22
23 enum {
24   SIG_SELECTION_CHANGED,
25   SIG_ACTIVATE,
26   LAST_SIGNAL
27 };
28
29 static guint signals[LAST_SIGNAL];
30
31 typedef struct _AddTemporaryIndividualCtx AddTemporaryIndividualCtx;
32
33 struct _EmpathyContactChooserPrivate
34 {
35   TpAccountManager *account_mgr;
36
37   EmpathyIndividualStore *store;
38   EmpathyIndividualView *view;
39   GtkWidget *search_entry;
40
41   GPtrArray *search_words;
42   gchar *search_str;
43
44   /* Context representing the FolksIndividual which are added because of the
45    * current search from the user. */
46   AddTemporaryIndividualCtx *add_temp_ctx;
47
48   EmpathyContactChooserFilterFunc filter_func;
49   gpointer filter_data;
50 };
51
52 struct _AddTemporaryIndividualCtx
53 {
54   EmpathyContactChooser *self;
55   /* List of owned FolksIndividual */
56   GList *individuals;
57 };
58
59 static AddTemporaryIndividualCtx *
60 add_temporary_individual_ctx_new (EmpathyContactChooser *self)
61 {
62   AddTemporaryIndividualCtx *ctx = g_slice_new0 (AddTemporaryIndividualCtx);
63
64   ctx->self = self;
65   return ctx;
66 }
67
68 static void
69 add_temporary_individual_ctx_free (AddTemporaryIndividualCtx *ctx)
70 {
71   GList *l;
72
73   /* Remove all the individuals from the model */
74   for (l = ctx->individuals; l != NULL; l = g_list_next (l))
75     {
76       FolksIndividual *individual = l->data;
77
78       individual_store_remove_individual_and_disconnect (ctx->self->priv->store,
79           individual);
80
81       g_object_unref (individual);
82     }
83
84   g_list_free (ctx->individuals);
85   g_slice_free (AddTemporaryIndividualCtx, ctx);
86 }
87
88 static void
89 contact_chooser_dispose (GObject *object)
90 {
91   EmpathyContactChooser *self = (EmpathyContactChooser *)
92     object;
93
94   tp_clear_pointer (&self->priv->add_temp_ctx,
95       add_temporary_individual_ctx_free);
96
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);
100
101   tp_clear_object (&self->priv->account_mgr);
102
103   G_OBJECT_CLASS (empathy_contact_chooser_parent_class)->dispose (
104       object);
105 }
106
107 static void
108 empathy_contact_chooser_class_init (
109     EmpathyContactChooserClass *klass)
110 {
111   GObjectClass *object_class = G_OBJECT_CLASS (klass);
112
113   object_class->dispose = contact_chooser_dispose;
114
115   g_type_class_add_private (object_class,
116       sizeof (EmpathyContactChooserPrivate));
117
118   signals[SIG_SELECTION_CHANGED] = g_signal_new ("selection-changed",
119             G_TYPE_FROM_CLASS (klass),
120             G_SIGNAL_RUN_LAST,
121             0,
122             NULL, NULL,
123             g_cclosure_marshal_generic,
124             G_TYPE_NONE,
125             1, FOLKS_TYPE_INDIVIDUAL);
126
127   signals[SIG_ACTIVATE] = g_signal_new ("activate",
128             G_TYPE_FROM_CLASS (klass),
129             G_SIGNAL_RUN_LAST,
130             0,
131             NULL, NULL,
132             g_cclosure_marshal_generic,
133             G_TYPE_NONE,
134             0);
135 }
136
137 static void
138 view_selection_changed_cb (GtkWidget *treeview,
139     EmpathyContactChooser *self)
140 {
141   FolksIndividual *individual;
142
143   individual = empathy_individual_view_dup_selected (self->priv->view);
144
145   g_signal_emit (self, signals[SIG_SELECTION_CHANGED], 0, individual);
146
147   tp_clear_object (&individual);
148 }
149
150 static gboolean
151 filter_func (GtkTreeModel *model,
152     GtkTreeIter *iter,
153     gpointer user_data)
154 {
155   EmpathyContactChooser *self = user_data;
156   FolksIndividual *individual;
157   gboolean is_online;
158   gboolean display = FALSE;
159   gboolean searching = FALSE;
160
161   gtk_tree_model_get (model, iter,
162       EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual,
163       EMPATHY_INDIVIDUAL_STORE_COL_IS_ONLINE, &is_online,
164       -1);
165
166   if (individual == NULL)
167     goto out;
168
169   if (self->priv->search_words != NULL)
170     {
171       searching = TRUE;
172
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))
176         goto out;
177     }
178
179   if (self->priv->filter_func == NULL)
180     display = TRUE;
181   else
182     display = self->priv->filter_func (self, individual, is_online, searching,
183       self->priv->filter_data);
184 out:
185   tp_clear_object (&individual);
186   return display;
187 }
188
189 static void
190 get_contacts_cb (TpConnection *connection,
191     guint n_contacts,
192     TpContact * const *contacts,
193     const gchar * const *requested_ids,
194     GHashTable *failed_id_errors,
195     const GError *error,
196     gpointer user_data,
197     GObject *weak_object)
198 {
199   EmpathyContactChooser *self =
200     (EmpathyContactChooser *) weak_object;
201   AddTemporaryIndividualCtx *ctx = user_data;
202   TpAccount *account;
203   TpfPersonaStore *store;
204   FolksIndividual *individual;
205   TpfPersona *persona_new;
206   GeeSet *personas;
207
208   if (self->priv->add_temp_ctx != ctx)
209     /* another request has been started */
210     return;
211
212   if (n_contacts != 1)
213     return;
214
215   account = tp_connection_get_account (connection);
216
217   store = tpf_persona_store_new (account);
218   personas = GEE_SET (
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));
224
225   individual = folks_individual_new (personas);
226
227   /* Pass ownership to the list */
228   ctx->individuals = g_list_prepend (ctx->individuals, individual);
229
230   individual_store_add_individual_and_connect (self->priv->store, individual);
231
232   /* Make sure that the first matching item is selected */
233   empathy_individual_view_select_first (self->priv->view);
234
235   g_clear_object (&persona_new);
236   g_clear_object (&personas);
237   g_object_unref (store);
238 }
239
240 static void
241 add_temporary_individuals (EmpathyContactChooser *self,
242     const gchar *id)
243 {
244   GList *accounts, *l;
245
246   tp_clear_pointer (&self->priv->add_temp_ctx,
247       add_temporary_individual_ctx_free);
248
249   if (tp_str_empty (id))
250     return;
251
252   self->priv->add_temp_ctx = add_temporary_individual_ctx_new (self);
253
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))
257     {
258       TpAccount *account = l->data;
259       TpConnection *conn;
260       TpContactFeature features[] = { TP_CONTACT_FEATURE_ALIAS,
261           TP_CONTACT_FEATURE_AVATAR_DATA,
262           TP_CONTACT_FEATURE_PRESENCE,
263           TP_CONTACT_FEATURE_CAPABILITIES };
264
265       conn = tp_account_get_connection (account);
266       if (conn == NULL)
267         continue;
268
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,
271           G_OBJECT (self));
272     }
273
274   g_list_free (accounts);
275 }
276
277 static void
278 search_text_changed (GtkEntry *entry,
279     EmpathyContactChooser *self)
280 {
281   const gchar *id;
282
283   tp_clear_pointer (&self->priv->search_words, g_ptr_array_unref);
284   tp_clear_pointer (&self->priv->search_str, g_free);
285
286   id = gtk_entry_get_text (entry);
287
288   self->priv->search_words = empathy_live_search_strip_utf8_string (id);
289   self->priv->search_str = g_strdup (id);
290
291   add_temporary_individuals (self, id);
292
293   empathy_individual_view_refilter (self->priv->view);
294 }
295
296 static void
297 search_activate_cb (GtkEntry *entry,
298     EmpathyContactChooser *self)
299 {
300   g_signal_emit (self, signals[SIG_ACTIVATE], 0);
301 }
302
303 static void
304 view_activate_cb (GtkTreeView *view,
305     GtkTreePath *path,
306     GtkTreeViewColumn *column,
307     EmpathyContactChooser *self)
308 {
309   g_signal_emit (self, signals[SIG_ACTIVATE], 0);
310 }
311
312 static gboolean
313 search_key_press_cb (GtkEntry *entry,
314     GdkEventKey *event,
315     EmpathyContactChooser *self)
316 {
317   GtkTreeSelection *selection;
318   GtkTreeModel *model;
319   GtkTreeIter iter;
320
321   if (event->state != 0)
322     return FALSE;
323
324   switch (event->keyval)
325     {
326       case GDK_KEY_Down:
327       case GDK_KEY_KP_Down:
328       case GDK_KEY_Up:
329       case GDK_KEY_KP_Up:
330         break;
331
332       default:
333         return FALSE;
334     }
335
336   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self->priv->view));
337
338   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
339     return TRUE;
340
341   switch (event->keyval)
342     {
343       case GDK_KEY_Down:
344       case GDK_KEY_KP_Down:
345         if (!gtk_tree_model_iter_next (model, &iter))
346           return TRUE;
347
348         break;
349
350       case GDK_KEY_Up:
351       case GDK_KEY_KP_Up:
352         if (!gtk_tree_model_iter_previous (model, &iter))
353           return TRUE;
354
355         break;
356
357       default:
358         g_assert_not_reached ();
359     }
360
361   gtk_tree_selection_select_iter (selection, &iter);
362
363   return TRUE;
364 }
365
366 static void
367 empathy_contact_chooser_init (EmpathyContactChooser *self)
368 {
369   EmpathyIndividualManager *mgr;
370   GtkTreeSelection *selection;
371   GtkWidget *scroll;
372   GQuark features[] = { TP_ACCOUNT_MANAGER_FEATURE_CORE, 0 };
373
374   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_CONTACT_CHOOSER,
375       EmpathyContactChooserPrivate);
376
377   self->priv->account_mgr = tp_account_manager_dup ();
378
379   /* We don't wait for the CORE feature to be prepared, which is fine as we
380    * won't use the account manager until user starts searching. Furthermore,
381    * the AM has probably already been prepared by another Empathy
382    * component. */
383   tp_proxy_prepare_async (self->priv->account_mgr, features, NULL, NULL);
384
385   /* Search entry */
386   self->priv->search_entry = gtk_entry_new ();
387   gtk_box_pack_start (GTK_BOX (self), self->priv->search_entry, FALSE, TRUE, 6);
388   gtk_widget_show (self->priv->search_entry);
389
390   g_signal_connect (self->priv->search_entry, "changed",
391       G_CALLBACK (search_text_changed), self);
392   g_signal_connect (self->priv->search_entry, "activate",
393       G_CALLBACK (search_activate_cb), self);
394   g_signal_connect (self->priv->search_entry, "key-press-event",
395       G_CALLBACK (search_key_press_cb), self);
396
397   /* Add the treeview */
398   mgr = empathy_individual_manager_dup_singleton ();
399   self->priv->store = empathy_individual_store_new (mgr);
400   g_object_unref (mgr);
401
402   empathy_individual_store_set_show_groups (self->priv->store, FALSE);
403
404   self->priv->view = empathy_individual_view_new (self->priv->store,
405       EMPATHY_INDIVIDUAL_VIEW_FEATURE_NONE, EMPATHY_INDIVIDUAL_FEATURE_NONE);
406
407   empathy_individual_view_set_custom_filter (self->priv->view,
408       filter_func, self);
409
410   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self->priv->view));
411
412   g_signal_connect (selection, "changed",
413       G_CALLBACK (view_selection_changed_cb), self);
414   g_signal_connect (self->priv->view, "row-activated",
415       G_CALLBACK (view_activate_cb), self);
416
417   scroll = gtk_scrolled_window_new (NULL, NULL);
418
419   gtk_container_add (GTK_CONTAINER (scroll), GTK_WIDGET (self->priv->view));
420
421   gtk_box_pack_start (GTK_BOX (self), scroll, TRUE, TRUE, 6);
422   gtk_widget_show (GTK_WIDGET (self->priv->view));
423   gtk_widget_show (scroll);
424 }
425
426 GtkWidget *
427 empathy_contact_chooser_new (void)
428 {
429   return g_object_new (EMPATHY_TYPE_CONTACT_CHOOSER,
430       "orientation", GTK_ORIENTATION_VERTICAL,
431       NULL);
432 }
433
434 FolksIndividual *
435 empathy_contact_chooser_dup_selected (EmpathyContactChooser *self)
436 {
437   return empathy_individual_view_dup_selected (self->priv->view);
438 }
439
440 void
441 empathy_contact_chooser_set_filter_func (EmpathyContactChooser *self,
442     EmpathyContactChooserFilterFunc func,
443     gpointer user_data)
444 {
445   g_assert (self->priv->filter_func == NULL);
446
447   self->priv->filter_func = func;
448   self->priv->filter_data = user_data;
449 }
450
451 void
452 empathy_contact_chooser_show_search_entry (EmpathyContactChooser *self,
453     gboolean show)
454 {
455   gtk_widget_set_visible (self->priv->search_entry, show);
456 }