]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-chooser.c
factor out empathy_create_individual_from_tp_contact()
[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
56 struct _AddTemporaryIndividualCtx
57 {
58   EmpathyContactChooser *self;
59   /* List of owned FolksIndividual */
60   GList *individuals;
61 };
62
63 static AddTemporaryIndividualCtx *
64 add_temporary_individual_ctx_new (EmpathyContactChooser *self)
65 {
66   AddTemporaryIndividualCtx *ctx = g_slice_new0 (AddTemporaryIndividualCtx);
67
68   ctx->self = self;
69   return ctx;
70 }
71
72 static void
73 add_temporary_individual_ctx_free (AddTemporaryIndividualCtx *ctx)
74 {
75   GList *l;
76
77   /* Remove all the individuals from the model */
78   for (l = ctx->individuals; l != NULL; l = g_list_next (l))
79     {
80       FolksIndividual *individual = l->data;
81
82       individual_store_remove_individual_and_disconnect (ctx->self->priv->store,
83           individual);
84
85       g_object_unref (individual);
86     }
87
88   g_list_free (ctx->individuals);
89   g_slice_free (AddTemporaryIndividualCtx, ctx);
90 }
91
92 static void
93 contact_chooser_dispose (GObject *object)
94 {
95   EmpathyContactChooser *self = (EmpathyContactChooser *)
96     object;
97
98   tp_clear_pointer (&self->priv->add_temp_ctx,
99       add_temporary_individual_ctx_free);
100
101   tp_clear_object (&self->priv->store);
102   tp_clear_pointer (&self->priv->search_words, g_ptr_array_unref);
103   tp_clear_pointer (&self->priv->search_str, g_free);
104
105   tp_clear_object (&self->priv->account_mgr);
106
107   G_OBJECT_CLASS (empathy_contact_chooser_parent_class)->dispose (
108       object);
109 }
110
111 static void
112 empathy_contact_chooser_class_init (
113     EmpathyContactChooserClass *klass)
114 {
115   GObjectClass *object_class = G_OBJECT_CLASS (klass);
116
117   object_class->dispose = contact_chooser_dispose;
118
119   g_type_class_add_private (object_class,
120       sizeof (EmpathyContactChooserPrivate));
121
122   signals[SIG_SELECTION_CHANGED] = g_signal_new ("selection-changed",
123             G_TYPE_FROM_CLASS (klass),
124             G_SIGNAL_RUN_LAST,
125             0,
126             NULL, NULL,
127             g_cclosure_marshal_generic,
128             G_TYPE_NONE,
129             1, FOLKS_TYPE_INDIVIDUAL);
130
131   signals[SIG_ACTIVATE] = g_signal_new ("activate",
132             G_TYPE_FROM_CLASS (klass),
133             G_SIGNAL_RUN_LAST,
134             0,
135             NULL, NULL,
136             g_cclosure_marshal_generic,
137             G_TYPE_NONE,
138             0);
139 }
140
141 static void
142 view_selection_changed_cb (GtkWidget *treeview,
143     EmpathyContactChooser *self)
144 {
145   FolksIndividual *individual;
146
147   individual = empathy_individual_view_dup_selected (self->priv->view);
148
149   g_signal_emit (self, signals[SIG_SELECTION_CHANGED], 0, individual);
150
151   tp_clear_object (&individual);
152 }
153
154 static gboolean
155 filter_func (GtkTreeModel *model,
156     GtkTreeIter *iter,
157     gpointer user_data)
158 {
159   EmpathyContactChooser *self = user_data;
160   FolksIndividual *individual;
161   gboolean is_online;
162   gboolean display = FALSE;
163   gboolean searching = FALSE;
164
165   gtk_tree_model_get (model, iter,
166       EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual,
167       EMPATHY_INDIVIDUAL_STORE_COL_IS_ONLINE, &is_online,
168       -1);
169
170   if (individual == NULL)
171     goto out;
172
173   if (self->priv->search_words != NULL)
174     {
175       searching = TRUE;
176
177       /* Filter out the contact if we are searching and it doesn't match */
178       if (!empathy_individual_match_string (individual,
179             self->priv->search_str, self->priv->search_words))
180         goto out;
181     }
182
183   if (self->priv->filter_func == NULL)
184     display = TRUE;
185   else
186     display = self->priv->filter_func (self, individual, is_online, searching,
187       self->priv->filter_data);
188 out:
189   tp_clear_object (&individual);
190   return display;
191 }
192
193 static void
194 contact_capabilities_changed (TpContact *contact,
195     GParamSpec *pspec,
196     EmpathyContactChooser *self)
197 {
198   empathy_individual_view_refilter (self->priv->view);
199 }
200
201 static void
202 get_contacts_cb (TpConnection *connection,
203     guint n_contacts,
204     TpContact * const *contacts,
205     const gchar * const *requested_ids,
206     GHashTable *failed_id_errors,
207     const GError *error,
208     gpointer user_data,
209     GObject *weak_object)
210 {
211   EmpathyContactChooser *self =
212     (EmpathyContactChooser *) weak_object;
213   AddTemporaryIndividualCtx *ctx = user_data;
214   FolksIndividual *individual;
215
216   if (self->priv->add_temp_ctx != ctx)
217     /* another request has been started */
218     return;
219
220   if (n_contacts != 1)
221     return;
222
223   individual =  empathy_create_individual_from_tp_contact (contacts[0]);
224
225   /* listen for updates to the capabilities */
226   tp_g_signal_connect_object (contacts[0], "notify::capabilities",
227       G_CALLBACK (contact_capabilities_changed), self, 0);
228
229   /* Pass ownership to the list */
230   ctx->individuals = g_list_prepend (ctx->individuals, individual);
231
232   individual_store_add_individual_and_connect (self->priv->store, individual);
233
234   /* if nothing is selected, select the first matching node */
235   if (!gtk_tree_selection_get_selected (
236         gtk_tree_view_get_selection (GTK_TREE_VIEW (self->priv->view)),
237         NULL, NULL))
238     empathy_individual_view_select_first (self->priv->view);
239 }
240
241 static void
242 add_temporary_individuals (EmpathyContactChooser *self,
243     const gchar *id)
244 {
245   GList *accounts, *l;
246
247   tp_clear_pointer (&self->priv->add_temp_ctx,
248       add_temporary_individual_ctx_free);
249
250   if (tp_str_empty (id))
251     return;
252
253   self->priv->add_temp_ctx = add_temporary_individual_ctx_new (self);
254
255   /* Try to add an individual for each connected account */
256   accounts = tp_account_manager_get_valid_accounts (self->priv->account_mgr);
257   for (l = accounts; l != NULL; l = g_list_next (l))
258     {
259       TpAccount *account = l->data;
260       TpConnection *conn;
261       TpContactFeature features[] = { TP_CONTACT_FEATURE_ALIAS,
262           TP_CONTACT_FEATURE_AVATAR_DATA,
263           TP_CONTACT_FEATURE_PRESENCE,
264           TP_CONTACT_FEATURE_CAPABILITIES };
265
266       conn = tp_account_get_connection (account);
267       if (conn == NULL)
268         continue;
269
270       tp_connection_get_contacts_by_id (conn, 1, &id, G_N_ELEMENTS (features),
271           features, get_contacts_cb, self->priv->add_temp_ctx, NULL,
272           G_OBJECT (self));
273     }
274
275   g_list_free (accounts);
276 }
277
278 static void
279 search_text_changed (GtkEntry *entry,
280     EmpathyContactChooser *self)
281 {
282   const gchar *id;
283
284   tp_clear_pointer (&self->priv->search_words, g_ptr_array_unref);
285   tp_clear_pointer (&self->priv->search_str, g_free);
286
287   id = gtk_entry_get_text (entry);
288
289   self->priv->search_words = empathy_live_search_strip_utf8_string (id);
290   self->priv->search_str = g_strdup (id);
291
292   add_temporary_individuals (self, id);
293
294   empathy_individual_view_refilter (self->priv->view);
295 }
296
297 static void
298 search_activate_cb (GtkEntry *entry,
299     EmpathyContactChooser *self)
300 {
301   g_signal_emit (self, signals[SIG_ACTIVATE], 0);
302 }
303
304 static void
305 view_activate_cb (GtkTreeView *view,
306     GtkTreePath *path,
307     GtkTreeViewColumn *column,
308     EmpathyContactChooser *self)
309 {
310   g_signal_emit (self, signals[SIG_ACTIVATE], 0);
311 }
312
313 static gboolean
314 search_key_press_cb (GtkEntry *entry,
315     GdkEventKey *event,
316     EmpathyContactChooser *self)
317 {
318   GtkTreeSelection *selection;
319   GtkTreeModel *model;
320   GtkTreeIter iter;
321
322   if (event->state != 0)
323     return FALSE;
324
325   switch (event->keyval)
326     {
327       case GDK_KEY_Down:
328       case GDK_KEY_KP_Down:
329       case GDK_KEY_Up:
330       case GDK_KEY_KP_Up:
331         break;
332
333       default:
334         return FALSE;
335     }
336
337   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self->priv->view));
338
339   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
340     return TRUE;
341
342   switch (event->keyval)
343     {
344       case GDK_KEY_Down:
345       case GDK_KEY_KP_Down:
346         if (!gtk_tree_model_iter_next (model, &iter))
347           return TRUE;
348
349         break;
350
351       case GDK_KEY_Up:
352       case GDK_KEY_KP_Up:
353         if (!gtk_tree_model_iter_previous (model, &iter))
354           return TRUE;
355
356         break;
357
358       default:
359         g_assert_not_reached ();
360     }
361
362   gtk_tree_selection_select_iter (selection, &iter);
363
364   return TRUE;
365 }
366
367 static void
368 empathy_contact_chooser_init (EmpathyContactChooser *self)
369 {
370   EmpathyIndividualManager *mgr;
371   GtkTreeSelection *selection;
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 (
400       empathy_individual_store_manager_new (mgr));
401   g_object_unref (mgr);
402
403   empathy_individual_store_set_show_groups (self->priv->store, FALSE);
404
405   self->priv->view = empathy_individual_view_new (self->priv->store,
406       EMPATHY_INDIVIDUAL_VIEW_FEATURE_NONE, EMPATHY_INDIVIDUAL_FEATURE_NONE);
407
408   empathy_individual_view_set_custom_filter (self->priv->view,
409       filter_func, self);
410
411   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self->priv->view));
412
413   g_signal_connect (selection, "changed",
414       G_CALLBACK (view_selection_changed_cb), self);
415   g_signal_connect (self->priv->view, "row-activated",
416       G_CALLBACK (view_activate_cb), self);
417
418   self->priv->scroll_view = gtk_scrolled_window_new (NULL, NULL);
419
420   gtk_container_add (GTK_CONTAINER (self->priv->scroll_view),
421       GTK_WIDGET (self->priv->view));
422
423   gtk_box_pack_start (GTK_BOX (self), self->priv->scroll_view, TRUE, TRUE, 6);
424   gtk_widget_show (GTK_WIDGET (self->priv->view));
425   gtk_widget_show (self->priv->scroll_view);
426 }
427
428 GtkWidget *
429 empathy_contact_chooser_new (void)
430 {
431   return g_object_new (EMPATHY_TYPE_CONTACT_CHOOSER,
432       "orientation", GTK_ORIENTATION_VERTICAL,
433       NULL);
434 }
435
436 FolksIndividual *
437 empathy_contact_chooser_dup_selected (EmpathyContactChooser *self)
438 {
439   return empathy_individual_view_dup_selected (self->priv->view);
440 }
441
442 void
443 empathy_contact_chooser_set_filter_func (EmpathyContactChooser *self,
444     EmpathyContactChooserFilterFunc func,
445     gpointer user_data)
446 {
447   g_assert (self->priv->filter_func == NULL);
448
449   self->priv->filter_func = func;
450   self->priv->filter_data = user_data;
451 }
452
453 void
454 empathy_contact_chooser_show_search_entry (EmpathyContactChooser *self,
455     gboolean show)
456 {
457   gtk_widget_set_visible (self->priv->search_entry, show);
458 }
459
460 void
461 empathy_contact_chooser_show_tree_view (EmpathyContactChooser *self,
462     gboolean show)
463 {
464   gtk_widget_set_visible (GTK_WIDGET (self->priv->scroll_view), show);
465 }