]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-search-dialog.c
Use the special "" search field if the CM supports it
[empathy.git] / libempathy-gtk / empathy-contact-search-dialog.c
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3  * empathy-contact-search-dialog.c
4  *
5  * Copyright (C) 2010-2011 Collabora Ltd.
6  *
7  * The code contained in this file is free software; you can redistribute
8  * it and/or modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either version
10  * 2.1 of the License, or (at your option) any later version.
11  *
12  * This file is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this code; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  * Authors:
22  *     Danielle Madeley <danielle.madeley@collabora.co.uk>
23  *     Emilio Pozuelo Monfort <emilio.pozuelo@collabora.co.uk>
24  */
25
26 #include <glib/gi18n.h>
27
28 #include <telepathy-glib/telepathy-glib.h>
29
30 #include <libempathy/empathy-contact-manager.h>
31 #include <libempathy/empathy-tp-contact-factory.h>
32
33 #include <libempathy-gtk/empathy-account-chooser.h>
34 #include <libempathy-gtk/empathy-cell-renderer-text.h>
35
36 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
37 #include <libempathy/empathy-debug.h>
38
39 #include "empathy-contact-search-dialog.h"
40
41 #define GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CONTACT_SEARCH_DIALOG, EmpathyContactSearchDialogPrivate))
42
43 G_DEFINE_TYPE (EmpathyContactSearchDialog, empathy_contact_search_dialog, GTK_TYPE_DIALOG);
44
45 enum
46 {
47    NAME_COLUMN,
48    LOGIN_COLUMN,
49    N_COLUMNS
50 };
51
52 enum {
53    PAGE_SEARCH_RESULTS,
54    PAGE_NO_MATCH
55 };
56
57 typedef struct _EmpathyContactSearchDialogPrivate EmpathyContactSearchDialogPrivate;
58 struct _EmpathyContactSearchDialogPrivate
59 {
60   TpContactSearch *searcher;
61   GtkListStore *store;
62
63   GtkWidget *chooser;
64   GtkWidget *notebook;
65   GtkWidget *tree_view;
66   GtkWidget *spinner;
67   GtkWidget *add_button;
68   GtkWidget *find_button;
69   GtkWidget *no_contact_found;
70   GtkWidget *search_entry;
71   /* GtkWidget *server_entry; */
72 };
73
74 static void
75 empathy_contact_search_dialog_dispose (GObject *self)
76 {
77   EmpathyContactSearchDialogPrivate *priv = GET_PRIVATE (self);
78
79   tp_clear_object (&priv->searcher);
80
81   G_OBJECT_CLASS (empathy_contact_search_dialog_parent_class)->dispose (self);
82 }
83
84 static void
85 on_searcher_reset (GObject *source_object,
86     GAsyncResult *result,
87     gpointer user_data)
88 {
89   EmpathyContactSearchDialog *self = EMPATHY_CONTACT_SEARCH_DIALOG (user_data);
90   EmpathyContactSearchDialogPrivate *priv = GET_PRIVATE (self);
91   TpContactSearch *searcher = TP_CONTACT_SEARCH (source_object);
92   GError *error = NULL;
93   GHashTable *search;
94   const gchar *search_criteria;
95
96   tp_contact_search_reset_finish (searcher, result, &error);
97   if (error != NULL)
98     {
99       DEBUG ("Failed to reset the TpContactSearch: %s", error->message);
100       g_error_free (error);
101       return;
102     }
103
104   search = g_hash_table_new (g_str_hash, g_str_equal);
105
106   search_criteria = gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
107
108   if (tp_strv_contains (tp_contact_search_get_search_keys (searcher), ""))
109     g_hash_table_insert (search, "", (gpointer) search_criteria);
110   else
111     g_hash_table_insert (search, "fn", (gpointer) search_criteria);
112
113   gtk_list_store_clear (priv->store);
114   tp_contact_search_start (priv->searcher, search);
115
116   g_hash_table_destroy (search);
117 }
118
119 static void
120 empathy_contact_search_dialog_do_search (EmpathyContactSearchDialog *self)
121 {
122   EmpathyContactSearchDialogPrivate *priv = GET_PRIVATE (self);
123
124   tp_contact_search_reset_async (priv->searcher,
125       NULL, /* gtk_entry_get_text (GTK_ENTRY (priv->server_entry)), */
126       0,
127       on_searcher_reset,
128       self);
129 }
130
131 static void
132 on_get_contact_factory_get_from_id_cb (TpConnection *connection,
133     EmpathyContact *contact,
134     const GError *error,
135     gpointer user_data,
136     GObject *object)
137 {
138     EmpathyContactManager *manager = empathy_contact_manager_dup_singleton ();
139
140     if (error != NULL)
141       {
142         g_warning ("Error while getting the contact: %s", error->message);
143         return;
144       }
145
146     empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager), contact, "");
147 }
148
149 static void
150 add_selected_contact (EmpathyContactSearchDialog *self)
151 {
152   EmpathyContactSearchDialogPrivate *priv = GET_PRIVATE (self);
153   GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
154   TpConnection *conn;
155   GtkTreeIter iter;
156   GtkTreeModel *model;
157   gboolean sel;
158   gchar *id;
159
160   conn = empathy_account_chooser_get_connection (EMPATHY_ACCOUNT_CHOOSER (priv->chooser));
161
162   sel = gtk_tree_selection_get_selected (selection, &model, &iter);
163   g_return_if_fail (sel == TRUE);
164
165   gtk_tree_model_get (model, &iter, LOGIN_COLUMN, &id, -1);
166
167   DEBUG ("Requested to add contact: %s", id);
168
169   empathy_tp_contact_factory_get_from_id (conn, id,
170       on_get_contact_factory_get_from_id_cb, NULL,
171       NULL, NULL);
172 }
173
174 static void
175 empathy_contact_search_dialog_response (GtkDialog *self,
176     gint response)
177 {
178   switch (response)
179     {
180       case GTK_RESPONSE_APPLY:
181         add_selected_contact (EMPATHY_CONTACT_SEARCH_DIALOG (self));
182         break;
183       default:
184         gtk_widget_destroy (GTK_WIDGET (self));
185         break;
186     }
187 }
188
189 static void
190 empathy_contact_search_dialog_class_init (
191     EmpathyContactSearchDialogClass *klass)
192 {
193   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
194   GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (klass);
195
196   gobject_class->dispose = empathy_contact_search_dialog_dispose;
197
198   dialog_class->response = empathy_contact_search_dialog_response;
199
200   g_type_class_add_private (gobject_class,
201       sizeof (EmpathyContactSearchDialogPrivate));
202 }
203
204 static void
205 _on_search_state_changed_cb (TpContactSearch *searcher,
206     GParamSpec *pspec,
207     gpointer user_data)
208 {
209   EmpathyContactSearchDialog *self = EMPATHY_CONTACT_SEARCH_DIALOG (user_data);
210   EmpathyContactSearchDialogPrivate *priv = GET_PRIVATE (self);
211   TpChannelContactSearchState state;
212
213   g_object_get (searcher, "state", &state, NULL);
214
215   DEBUG ("new search status: %d", state);
216
217   if (state == TP_CHANNEL_CONTACT_SEARCH_STATE_IN_PROGRESS)
218     {
219       gtk_widget_show (priv->spinner);
220       gtk_spinner_start (GTK_SPINNER (priv->spinner));
221     }
222   else
223     {
224       gtk_widget_hide (priv->spinner);
225       gtk_spinner_stop (GTK_SPINNER (priv->spinner));
226     }
227
228   if (state == TP_CHANNEL_CONTACT_SEARCH_STATE_NOT_STARTED
229       || state == TP_CHANNEL_CONTACT_SEARCH_STATE_IN_PROGRESS)
230     {
231       gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
232           PAGE_SEARCH_RESULTS);
233     }
234   else
235     {
236       GtkTreeIter help_iter;
237
238       if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->store),
239           &help_iter))
240         {
241           /* No results found, display a helpful message. */
242           gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
243               PAGE_NO_MATCH);
244         }
245     }
246 }
247
248 static void
249 _search_results_received (TpContactSearch *searcher,
250     GList *results,
251     EmpathyContactSearchDialog *self)
252 {
253   EmpathyContactSearchDialogPrivate *priv = GET_PRIVATE (self);
254   const TpContactInfoField *name;
255   GList *l;
256
257   for (l = results; l != NULL; l = l->next)
258     {
259       TpContactSearchResult *result = l->data;
260       GtkTreeIter iter;
261
262       gtk_list_store_append (priv->store, &iter);
263
264       name = tp_contact_search_result_get_field (result, "fn");
265
266       gtk_list_store_set (priv->store, &iter,
267           NAME_COLUMN, name ? name->field_value[0] : NULL,
268           LOGIN_COLUMN, tp_contact_search_result_get_identifier (result),
269           -1);
270     }
271 }
272
273 static void
274 on_searcher_created (GObject *source_object,
275     GAsyncResult *result,
276     gpointer user_data)
277 {
278   EmpathyContactSearchDialog *self;
279   EmpathyContactSearchDialogPrivate *priv;
280   GError *error = NULL;
281
282   if (EMPATHY_IS_CONTACT_SEARCH_DIALOG (user_data) == FALSE)
283     /* This happens if the dialog is closed before the callback is called */
284     return;
285
286   self = EMPATHY_CONTACT_SEARCH_DIALOG (user_data);
287   priv = GET_PRIVATE (self);
288
289   priv->searcher = tp_contact_search_new_finish (result, &error);
290   if (error != NULL)
291     {
292       DEBUG ("Failed to create a TpContactSearch: %s", error->message);
293       g_error_free (error);
294       return;
295     }
296
297   g_signal_connect (priv->searcher, "search-results-received",
298       G_CALLBACK (_search_results_received), self);
299   g_signal_connect (priv->searcher, "notify::state",
300       G_CALLBACK (_on_search_state_changed_cb), self);
301
302   gtk_widget_set_sensitive (priv->find_button, TRUE);
303 }
304
305 static void
306 on_selection_changed (GtkTreeSelection *selection,
307     gpointer user_data)
308 {
309   EmpathyContactSearchDialog *self;
310   EmpathyContactSearchDialogPrivate *priv;
311   gboolean sel;
312
313   self = EMPATHY_CONTACT_SEARCH_DIALOG (user_data);
314   priv = GET_PRIVATE (self);
315   sel = gtk_tree_selection_get_selected (selection, NULL, NULL);
316
317   gtk_widget_set_sensitive (priv->add_button, sel);
318 }
319
320
321 static void
322 _account_chooser_changed (EmpathyAccountChooser *chooser,
323     EmpathyContactSearchDialog *self)
324 {
325   EmpathyContactSearchDialogPrivate *priv = GET_PRIVATE (self);
326   TpAccount *account = empathy_account_chooser_get_account (chooser);
327   TpConnection *conn = empathy_account_chooser_get_connection (chooser);
328   TpCapabilities *caps = tp_connection_get_capabilities (conn);
329   gboolean can_cs, can_set_limit, can_set_server;
330
331   can_cs = tp_capabilities_supports_contact_search (caps,
332       &can_set_limit, &can_set_server);
333   DEBUG ("The server supports cs|limit|server: %s|%s|%s",
334       can_cs ? "yes" : "no",
335       can_set_limit ? "yes" : "no",
336       can_set_server ? "yes" : "no");
337
338   /* gtk_widget_set_sensitive (priv->server_entry, can_set_server); */
339   gtk_widget_set_sensitive (priv->find_button, FALSE);
340
341   DEBUG ("New account is %s", tp_proxy_get_object_path (account));
342
343   tp_clear_object (&priv->searcher);
344   tp_contact_search_new_async (account,
345       NULL, /* gtk_entry_get_text (GTK_ENTRY (priv->server_entry)), */
346       0,
347       on_searcher_created, self);
348 }
349
350 static void
351 _on_button_search_clicked (GtkWidget *widget,
352     EmpathyContactSearchDialog *self)
353 {
354   empathy_contact_search_dialog_do_search (self);
355 }
356
357 #if 0
358 static void
359 on_server_changed_cb (GtkEditable *editable,
360     gpointer user_data)
361 {
362   EmpathyContactSearchDialog *self = EMPATHY_CONTACT_SEARCH_DIALOG (user_data);
363   EmpathyContactSearchDialogPrivate *priv = GET_PRIVATE (self);
364
365   g_return_if_fail (priv->searcher != NULL);
366
367   tp_contact_search_reset_async (priv->searcher,
368       gtk_entry_get_text (GTK_ENTRY (editable)),
369       0,
370       on_searcher_reset,
371       self);
372 }
373 #endif
374
375 typedef struct
376 {
377     EmpathyAccountChooserFilterResultCallback callback;
378     gpointer                                  user_data;
379 } FilterCallbackData;
380
381 static void
382 supports_contact_search_cb (GObject *conn,
383     GAsyncResult *result,
384     gpointer user_data)
385 {
386   FilterCallbackData *data = user_data;
387   GError *myerr = NULL;
388   TpCapabilities *caps;
389
390   if (!tp_proxy_prepare_finish (conn, result, &myerr))
391     {
392       data->callback (FALSE, data->user_data);
393       g_slice_free (FilterCallbackData, data);
394       return;
395     }
396
397   caps = tp_connection_get_capabilities (TP_CONNECTION (conn));
398   data->callback (tp_capabilities_supports_contact_search (caps, NULL, NULL),
399     data->user_data);
400
401   g_slice_free (FilterCallbackData, data);
402 }
403
404 static void
405 empathy_account_chooser_filter_supports_contact_search (
406     TpAccount *account,
407     EmpathyAccountChooserFilterResultCallback callback,
408     gpointer callback_data,
409     gpointer user_data)
410 {
411   TpConnection *connection;
412   FilterCallbackData *cb_data;
413   GQuark features[] = { TP_CONNECTION_FEATURE_CAPABILITIES, 0 };
414
415   if (tp_account_get_connection_status (account, NULL)
416       != TP_CONNECTION_STATUS_CONNECTED)
417     {
418       callback (FALSE, callback_data);
419       return;
420     }
421
422   /* check if CM supports contact search */
423   connection = tp_account_get_connection (account);
424   if (connection == NULL)
425     {
426       callback (FALSE, callback_data);
427       return;
428     }
429
430   cb_data = g_slice_new0 (FilterCallbackData);
431   cb_data->callback = callback;
432   cb_data->user_data = callback_data;
433
434   tp_proxy_prepare_async (connection, features, supports_contact_search_cb,
435       cb_data);
436 }
437
438 static void
439 contact_search_dialog_row_activated_cb (GtkTreeView *tv,
440     GtkTreePath *path,
441     GtkTreeViewColumn *column,
442     EmpathyContactSearchDialog *self)
443 {
444   /* just emit the same response as the Add Button */
445   gtk_dialog_response (GTK_DIALOG (self), GTK_RESPONSE_APPLY);
446 }
447
448 static void
449 empathy_contact_search_dialog_init (EmpathyContactSearchDialog *self)
450 {
451   EmpathyContactSearchDialogPrivate *priv = GET_PRIVATE (self);
452   GtkWidget *vbox, *hbox, *scrolled_window, *label;
453   GtkCellRenderer *cell;
454   GtkTreeViewColumn *col;
455   GtkTreeSelection *selection;
456   GtkSizeGroup *size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
457   gchar *tmp;
458
459   /* Title */
460   gtk_window_set_title (GTK_WINDOW (self), _("Search contacts"));
461
462   vbox = gtk_vbox_new (FALSE, 3);
463   gtk_container_set_border_width (GTK_CONTAINER (vbox), 6);
464
465   /* Account chooser */
466   hbox = gtk_hbox_new (FALSE, 6);
467   label = gtk_label_new (_("Account:"));
468   gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 0);
469   gtk_size_group_add_widget (size_group, label);
470
471   priv->chooser = empathy_account_chooser_new ();
472   empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (priv->chooser),
473       empathy_account_chooser_filter_supports_contact_search, NULL);
474   gtk_box_pack_start (GTK_BOX (hbox), priv->chooser, TRUE, TRUE, 0);
475   g_signal_connect (priv->chooser, "changed",
476       G_CALLBACK (_account_chooser_changed), self);
477
478   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 0);
479
480 #if 0
481   /* Server entry */
482   priv->server_entry = gtk_entry_new ();
483   gtk_box_pack_start (GTK_BOX (vbox), priv->server_entry, FALSE, TRUE, 6);
484   g_signal_connect (GTK_EDITABLE (priv->server_entry), "changed",
485       G_CALLBACK (on_server_changed_cb), self);
486 #endif
487
488   /* Search input */
489   hbox = gtk_hbox_new (FALSE, 6);
490   label = gtk_label_new (_("Search: "));
491   gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 0);
492   gtk_size_group_add_widget (size_group, label);
493
494   priv->search_entry = gtk_entry_new ();
495   gtk_box_pack_start (GTK_BOX (hbox), priv->search_entry, TRUE, TRUE, 0);
496   g_signal_connect (priv->search_entry, "activate",
497       G_CALLBACK (_on_button_search_clicked), self);
498
499   priv->find_button = gtk_button_new_from_stock (GTK_STOCK_FIND);
500   g_signal_connect (priv->find_button, "clicked",
501       G_CALLBACK (_on_button_search_clicked), self);
502   gtk_box_pack_end (GTK_BOX (hbox), priv->find_button, FALSE, TRUE, 0);
503
504   priv->spinner = gtk_spinner_new ();
505   gtk_box_pack_end (GTK_BOX (hbox), priv->spinner, FALSE, TRUE, 0);
506   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 0);
507
508   /* Search results */
509   priv->store = gtk_list_store_new (N_COLUMNS,
510                                     G_TYPE_STRING,  /* Name */
511                                     G_TYPE_STRING); /* Login */
512
513   priv->tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->store));
514   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
515   gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
516
517   g_signal_connect (priv->tree_view, "row-activated",
518       G_CALLBACK (contact_search_dialog_row_activated_cb), self);
519   g_signal_connect (selection, "changed",
520       G_CALLBACK (on_selection_changed), self);
521
522   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
523
524   col = gtk_tree_view_column_new ();
525   cell = empathy_cell_renderer_text_new ();
526   gtk_tree_view_column_pack_start (col, cell, TRUE);
527   /* EmpathyCellRendererText displays "name" above and "status" below.
528    * We want the login above since it'll always be available, and the
529    * name below since we won't always have it. */
530   gtk_tree_view_column_add_attribute (col, cell,
531       "name", LOGIN_COLUMN);
532   gtk_tree_view_column_add_attribute (col, cell,
533       "status", NAME_COLUMN);
534
535   gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), col);
536
537   gtk_dialog_add_button (GTK_DIALOG (self),
538       GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
539
540   priv->add_button = gtk_dialog_add_button (GTK_DIALOG (self),
541       _("_Add Contact"), GTK_RESPONSE_APPLY);
542   gtk_widget_set_sensitive (priv->add_button, FALSE);
543   gtk_button_set_image (GTK_BUTTON (priv->add_button),
544       gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_BUTTON));
545
546   /* Pack the dialog */
547   priv->notebook = gtk_notebook_new ();
548   gtk_notebook_set_show_tabs (GTK_NOTEBOOK (priv->notebook), FALSE);
549   g_object_set (priv->notebook, "margin", 6, NULL);
550
551   scrolled_window = gtk_scrolled_window_new (NULL, NULL);
552   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
553       GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
554
555   gtk_container_add (GTK_CONTAINER (scrolled_window), priv->tree_view);
556
557   priv->no_contact_found = gtk_label_new (NULL);
558   tmp = g_strdup_printf ("<b><span size='xx-large'>%s</span></b>",
559       _("No contacts found"));
560   gtk_label_set_markup (GTK_LABEL (priv->no_contact_found), tmp);
561   g_free (tmp);
562
563   gtk_label_set_ellipsize (GTK_LABEL (priv->no_contact_found),
564       PANGO_ELLIPSIZE_END);
565
566   gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook), scrolled_window,
567       NULL);
568   gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook),
569       priv->no_contact_found, NULL);
570
571   gtk_box_pack_start (GTK_BOX (vbox), priv->notebook, TRUE, TRUE, 3);
572
573   gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (
574           GTK_DIALOG (self))), vbox, TRUE, TRUE, 0);
575
576   gtk_window_set_default_size (GTK_WINDOW (self), 200, 400);
577   gtk_widget_show_all (vbox);
578   gtk_widget_hide (priv->spinner);
579 }
580
581 GtkWidget *
582 empathy_contact_search_dialog_new (GtkWindow *parent)
583 {
584   GtkWidget *self;
585
586   g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
587
588   self = g_object_new (EMPATHY_TYPE_CONTACT_SEARCH_DIALOG, NULL);
589
590   if (parent != NULL)
591     {
592       gtk_window_set_transient_for (GTK_WINDOW (self), parent);
593     }
594
595   return self;
596 }