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