1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3 * empathy-contact-search-dialog.c
5 * Copyright (C) 2010-2011 Collabora Ltd.
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.
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.
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
22 * Danielle Madeley <danielle.madeley@collabora.co.uk>
23 * Emilio Pozuelo Monfort <emilio.pozuelo@collabora.co.uk>
27 #include <glib/gi18n-lib.h>
29 #include <telepathy-glib/telepathy-glib.h>
31 #include <libempathy/empathy-tp-contact-factory.h>
32 #include <libempathy/empathy-utils.h>
34 #include <libempathy-gtk/empathy-account-chooser.h>
35 #include <libempathy-gtk/empathy-cell-renderer-text.h>
36 #include <libempathy-gtk/empathy-cell-renderer-activatable.h>
37 #include <libempathy-gtk/empathy-contact-dialogs.h>
38 #include <libempathy-gtk/empathy-images.h>
40 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
41 #include <libempathy/empathy-debug.h>
43 #include "empathy-contact-search-dialog.h"
45 #define GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CONTACT_SEARCH_DIALOG, EmpathyContactSearchDialogPrivate))
47 G_DEFINE_TYPE (EmpathyContactSearchDialog
, empathy_contact_search_dialog
, GTK_TYPE_DIALOG
);
61 typedef struct _EmpathyContactSearchDialogPrivate EmpathyContactSearchDialogPrivate
;
62 struct _EmpathyContactSearchDialogPrivate
64 TpContactSearch
*searcher
;
71 GtkWidget
*add_button
;
72 GtkWidget
*find_button
;
73 GtkWidget
*no_contact_found
;
74 GtkWidget
*search_entry
;
75 /* GtkWidget *server_entry; */
77 GtkWidget
*message_window
;
78 GtkWidget
*message_label
;
82 empathy_contact_search_dialog_dispose (GObject
*self
)
84 EmpathyContactSearchDialogPrivate
*priv
= GET_PRIVATE (self
);
86 tp_clear_object (&priv
->searcher
);
88 G_OBJECT_CLASS (empathy_contact_search_dialog_parent_class
)->dispose (self
);
92 on_searcher_reset (GObject
*source_object
,
96 EmpathyContactSearchDialog
*self
= EMPATHY_CONTACT_SEARCH_DIALOG (user_data
);
97 EmpathyContactSearchDialogPrivate
*priv
= GET_PRIVATE (self
);
98 TpContactSearch
*searcher
= TP_CONTACT_SEARCH (source_object
);
101 const gchar
*search_criteria
;
103 tp_contact_search_reset_finish (searcher
, result
, &error
);
106 DEBUG ("Failed to reset the TpContactSearch: %s", error
->message
);
107 g_error_free (error
);
111 search
= g_hash_table_new (g_str_hash
, g_str_equal
);
113 search_criteria
= gtk_entry_get_text (GTK_ENTRY (priv
->search_entry
));
115 if (tp_strv_contains (tp_contact_search_get_search_keys (searcher
), ""))
116 g_hash_table_insert (search
, "", (gpointer
) search_criteria
);
118 g_hash_table_insert (search
, "fn", (gpointer
) search_criteria
);
120 gtk_list_store_clear (priv
->store
);
121 tp_contact_search_start (priv
->searcher
, search
);
123 g_hash_table_unref (search
);
127 empathy_contact_search_dialog_do_search (EmpathyContactSearchDialog
*self
)
129 EmpathyContactSearchDialogPrivate
*priv
= GET_PRIVATE (self
);
131 tp_contact_search_reset_async (priv
->searcher
,
132 NULL
, /* gtk_entry_get_text (GTK_ENTRY (priv->server_entry)), */
139 on_get_contact_factory_get_from_id_cb (TpConnection
*connection
,
140 EmpathyContact
*contact
,
145 const gchar
*message
= user_data
;
149 g_warning ("Error while getting the contact: %s", error
->message
);
153 empathy_contact_add_to_contact_list (contact
, message
);
157 add_selected_contact (EmpathyContactSearchDialog
*self
)
159 EmpathyContactSearchDialogPrivate
*priv
= GET_PRIVATE (self
);
160 GtkTreeSelection
*selection
= gtk_tree_view_get_selection (GTK_TREE_VIEW (priv
->tree_view
));
164 GtkTextBuffer
*buffer
;
165 GtkTextIter start
, end
;
170 conn
= empathy_account_chooser_get_connection (EMPATHY_ACCOUNT_CHOOSER (priv
->chooser
));
172 sel
= gtk_tree_selection_get_selected (selection
, &model
, &iter
);
173 g_return_if_fail (sel
== TRUE
);
175 gtk_tree_model_get (model
, &iter
, LOGIN_COLUMN
, &id
, -1);
177 DEBUG ("Requested to add contact: %s", id
);
179 buffer
= gtk_text_view_get_buffer
GTK_TEXT_VIEW (priv
->message
);
180 gtk_text_buffer_get_start_iter (buffer
, &start
);
181 gtk_text_buffer_get_end_iter (buffer
, &end
);
182 message
= gtk_text_buffer_get_text (buffer
, &start
, &end
, FALSE
);
184 empathy_tp_contact_factory_get_from_id (conn
, id
,
185 on_get_contact_factory_get_from_id_cb
,
186 message
, g_free
, NULL
);
188 /* Close the dialog */
189 gtk_dialog_response (GTK_DIALOG (self
), GTK_RESPONSE_CANCEL
);
193 empathy_contact_search_dialog_response (GtkDialog
*self
,
198 case GTK_RESPONSE_APPLY
:
199 add_selected_contact (EMPATHY_CONTACT_SEARCH_DIALOG (self
));
202 gtk_widget_destroy (GTK_WIDGET (self
));
208 empathy_contact_search_dialog_class_init (
209 EmpathyContactSearchDialogClass
*klass
)
211 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
212 GtkDialogClass
*dialog_class
= GTK_DIALOG_CLASS (klass
);
214 gobject_class
->dispose
= empathy_contact_search_dialog_dispose
;
216 dialog_class
->response
= empathy_contact_search_dialog_response
;
218 g_type_class_add_private (gobject_class
,
219 sizeof (EmpathyContactSearchDialogPrivate
));
223 _on_search_state_changed_cb (TpContactSearch
*searcher
,
227 EmpathyContactSearchDialog
*self
= EMPATHY_CONTACT_SEARCH_DIALOG (user_data
);
228 EmpathyContactSearchDialogPrivate
*priv
= GET_PRIVATE (self
);
229 TpChannelContactSearchState state
;
231 g_object_get (searcher
, "state", &state
, NULL
);
233 DEBUG ("new search status: %d", state
);
235 if (state
== TP_CHANNEL_CONTACT_SEARCH_STATE_IN_PROGRESS
)
237 gtk_widget_show (priv
->spinner
);
238 gtk_spinner_start (GTK_SPINNER (priv
->spinner
));
242 gtk_widget_hide (priv
->spinner
);
243 gtk_spinner_stop (GTK_SPINNER (priv
->spinner
));
246 if (state
== TP_CHANNEL_CONTACT_SEARCH_STATE_NOT_STARTED
247 || state
== TP_CHANNEL_CONTACT_SEARCH_STATE_IN_PROGRESS
)
249 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv
->notebook
),
250 PAGE_SEARCH_RESULTS
);
254 GtkTreeIter help_iter
;
256 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv
->store
),
259 /* No results found, display a helpful message. */
260 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv
->notebook
),
267 _search_results_received (TpContactSearch
*searcher
,
269 EmpathyContactSearchDialog
*self
)
271 EmpathyContactSearchDialogPrivate
*priv
= GET_PRIVATE (self
);
272 const TpContactInfoField
*name
;
275 for (l
= results
; l
!= NULL
; l
= l
->next
)
277 TpContactSearchResult
*result
= l
->data
;
280 gtk_list_store_append (priv
->store
, &iter
);
282 name
= tp_contact_search_result_get_field (result
, "fn");
284 gtk_list_store_set (priv
->store
, &iter
,
285 NAME_COLUMN
, name
? name
->field_value
[0] : NULL
,
286 LOGIN_COLUMN
, tp_contact_search_result_get_identifier (result
),
292 on_searcher_created (GObject
*source_object
,
293 GAsyncResult
*result
,
296 EmpathyContactSearchDialog
*self
;
297 EmpathyContactSearchDialogPrivate
*priv
;
298 GError
*error
= NULL
;
300 if (EMPATHY_IS_CONTACT_SEARCH_DIALOG (user_data
) == FALSE
)
301 /* This happens if the dialog is closed before the callback is called */
304 self
= EMPATHY_CONTACT_SEARCH_DIALOG (user_data
);
305 priv
= GET_PRIVATE (self
);
307 priv
->searcher
= tp_contact_search_new_finish (result
, &error
);
310 DEBUG ("Failed to create a TpContactSearch: %s", error
->message
);
311 g_error_free (error
);
315 g_signal_connect (priv
->searcher
, "search-results-received",
316 G_CALLBACK (_search_results_received
), self
);
317 g_signal_connect (priv
->searcher
, "notify::state",
318 G_CALLBACK (_on_search_state_changed_cb
), self
);
320 gtk_widget_set_sensitive (priv
->find_button
, TRUE
);
324 on_selection_changed (GtkTreeSelection
*selection
,
327 EmpathyContactSearchDialog
*self
;
328 EmpathyContactSearchDialogPrivate
*priv
;
331 self
= EMPATHY_CONTACT_SEARCH_DIALOG (user_data
);
332 priv
= GET_PRIVATE (self
);
333 sel
= gtk_tree_selection_get_selected (selection
, NULL
, NULL
);
335 gtk_widget_set_sensitive (priv
->add_button
, sel
);
339 check_request_message_available (EmpathyContactSearchDialog
*self
,
342 EmpathyContactSearchDialogPrivate
*priv
= GET_PRIVATE (self
);
344 gtk_widget_set_visible (priv
->message_window
,
345 tp_connection_get_can_change_contact_list (conn
));
346 gtk_widget_set_visible (priv
->message_label
,
347 tp_connection_get_can_change_contact_list (conn
));
351 _account_chooser_changed (EmpathyAccountChooser
*chooser
,
352 EmpathyContactSearchDialog
*self
)
354 EmpathyContactSearchDialogPrivate
*priv
= GET_PRIVATE (self
);
355 TpAccount
*account
= empathy_account_chooser_get_account (chooser
);
356 TpConnection
*conn
= empathy_account_chooser_get_connection (chooser
);
357 TpCapabilities
*caps
= tp_connection_get_capabilities (conn
);
358 gboolean can_cs
, can_set_limit
, can_set_server
;
360 can_cs
= tp_capabilities_supports_contact_search (caps
,
361 &can_set_limit
, &can_set_server
);
362 DEBUG ("The server supports cs|limit|server: %s|%s|%s",
363 can_cs
? "yes" : "no",
364 can_set_limit
? "yes" : "no",
365 can_set_server
? "yes" : "no");
367 /* gtk_widget_set_sensitive (priv->server_entry, can_set_server); */
368 gtk_widget_set_sensitive (priv
->find_button
, FALSE
);
370 DEBUG ("New account is %s", tp_proxy_get_object_path (account
));
372 tp_clear_object (&priv
->searcher
);
373 tp_contact_search_new_async (account
,
374 NULL
, /* gtk_entry_get_text (GTK_ENTRY (priv->server_entry)), */
376 on_searcher_created
, self
);
378 /* Make the request message textview sensitive if it can be used */
379 check_request_message_available (self
, conn
);
383 _on_button_search_clicked (GtkWidget
*widget
,
384 EmpathyContactSearchDialog
*self
)
386 empathy_contact_search_dialog_do_search (self
);
391 on_server_changed_cb (GtkEditable
*editable
,
394 EmpathyContactSearchDialog
*self
= EMPATHY_CONTACT_SEARCH_DIALOG (user_data
);
395 EmpathyContactSearchDialogPrivate
*priv
= GET_PRIVATE (self
);
397 g_return_if_fail (priv
->searcher
!= NULL
);
399 tp_contact_search_reset_async (priv
->searcher
,
400 gtk_entry_get_text (GTK_ENTRY (editable
)),
408 empathy_account_chooser_filter_supports_contact_search (
410 EmpathyAccountChooserFilterResultCallback callback
,
411 gpointer callback_data
,
414 TpConnection
*connection
;
415 gboolean supported
= FALSE
;
416 TpCapabilities
*caps
;
418 connection
= tp_account_get_connection (account
);
419 if (connection
== NULL
)
422 caps
= tp_connection_get_capabilities (connection
);
426 supported
= tp_capabilities_supports_contact_search (caps
, NULL
, NULL
);
429 callback (supported
, callback_data
);
433 contact_search_dialog_row_activated_cb (GtkTreeView
*tv
,
435 GtkTreeViewColumn
*column
,
436 EmpathyContactSearchDialog
*self
)
438 /* just emit the same response as the Add Button */
439 gtk_dialog_response (GTK_DIALOG (self
), GTK_RESPONSE_APPLY
);
443 on_profile_button_got_contact_cb (TpConnection
*connection
,
444 EmpathyContact
*contact
,
451 g_warning ("Error while getting the contact: %s", error
->message
);
455 empathy_contact_information_dialog_show (contact
, NULL
);
459 on_profile_button_clicked_cb (EmpathyCellRendererActivatable
*cell
,
460 const gchar
*path_string
,
461 EmpathyContactSearchDialog
*self
)
463 EmpathyContactSearchDialogPrivate
*priv
= GET_PRIVATE (self
);
470 model
= gtk_tree_view_get_model (GTK_TREE_VIEW (priv
->tree_view
));
472 conn
= empathy_account_chooser_get_connection (
473 EMPATHY_ACCOUNT_CHOOSER (priv
->chooser
));
475 valid
= gtk_tree_model_get_iter_from_string (model
, &iter
, path_string
);
476 g_return_if_fail (valid
== TRUE
);
478 gtk_tree_model_get (model
, &iter
, LOGIN_COLUMN
, &id
, -1);
480 DEBUG ("Requested to show profile for contact: %s", id
);
482 empathy_tp_contact_factory_get_from_id (conn
, id
,
483 on_profile_button_got_contact_cb
, NULL
,
488 empathy_contact_search_dialog_init (EmpathyContactSearchDialog
*self
)
490 EmpathyContactSearchDialogPrivate
*priv
= GET_PRIVATE (self
);
491 GtkWidget
*vbox
, *hbox
, *scrolled_window
, *label
;
492 GtkCellRenderer
*cell
;
493 GtkTreeViewColumn
*col
;
494 GtkTreeSelection
*selection
;
495 GtkSizeGroup
*size_group
= gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL
);
499 gtk_window_set_title (GTK_WINDOW (self
), _("Search contacts"));
501 vbox
= gtk_box_new (GTK_ORIENTATION_VERTICAL
, 3);
502 gtk_container_set_border_width (GTK_CONTAINER (vbox
), 6);
504 /* Account chooser */
505 hbox
= gtk_box_new (GTK_ORIENTATION_HORIZONTAL
, 6);
506 label
= gtk_label_new (_("Account:"));
507 gtk_box_pack_start (GTK_BOX (hbox
), label
, FALSE
, TRUE
, 0);
508 gtk_size_group_add_widget (size_group
, label
);
510 priv
->chooser
= empathy_account_chooser_new ();
511 empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (priv
->chooser
),
512 empathy_account_chooser_filter_supports_contact_search
, NULL
);
513 gtk_box_pack_start (GTK_BOX (hbox
), priv
->chooser
, TRUE
, TRUE
, 0);
514 g_signal_connect (priv
->chooser
, "changed",
515 G_CALLBACK (_account_chooser_changed
), self
);
517 gtk_box_pack_start (GTK_BOX (vbox
), hbox
, FALSE
, TRUE
, 0);
521 priv
->server_entry
= gtk_entry_new ();
522 gtk_box_pack_start (GTK_BOX (vbox
), priv
->server_entry
, FALSE
, TRUE
, 6);
523 g_signal_connect (GTK_EDITABLE (priv
->server_entry
), "changed",
524 G_CALLBACK (on_server_changed_cb
), self
);
528 hbox
= gtk_box_new (GTK_ORIENTATION_HORIZONTAL
, 6);
529 label
= gtk_label_new (_("Search: "));
530 gtk_box_pack_start (GTK_BOX (hbox
), label
, FALSE
, TRUE
, 0);
531 gtk_size_group_add_widget (size_group
, label
);
533 priv
->search_entry
= gtk_entry_new ();
534 gtk_box_pack_start (GTK_BOX (hbox
), priv
->search_entry
, TRUE
, TRUE
, 0);
535 g_signal_connect (priv
->search_entry
, "activate",
536 G_CALLBACK (_on_button_search_clicked
), self
);
538 priv
->find_button
= gtk_button_new_from_stock (GTK_STOCK_FIND
);
539 g_signal_connect (priv
->find_button
, "clicked",
540 G_CALLBACK (_on_button_search_clicked
), self
);
541 gtk_box_pack_end (GTK_BOX (hbox
), priv
->find_button
, FALSE
, TRUE
, 0);
543 priv
->spinner
= gtk_spinner_new ();
544 gtk_box_pack_end (GTK_BOX (hbox
), priv
->spinner
, FALSE
, TRUE
, 0);
545 gtk_box_pack_start (GTK_BOX (vbox
), hbox
, FALSE
, TRUE
, 0);
548 priv
->store
= gtk_list_store_new (N_COLUMNS
,
549 G_TYPE_STRING
, /* Name */
550 G_TYPE_STRING
); /* Login */
552 priv
->tree_view
= gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv
->store
));
553 selection
= gtk_tree_view_get_selection (GTK_TREE_VIEW (priv
->tree_view
));
554 gtk_tree_selection_set_mode (selection
, GTK_SELECTION_SINGLE
);
556 g_signal_connect (priv
->tree_view
, "row-activated",
557 G_CALLBACK (contact_search_dialog_row_activated_cb
), self
);
558 g_signal_connect (selection
, "changed",
559 G_CALLBACK (on_selection_changed
), self
);
561 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv
->tree_view
), FALSE
);
563 col
= gtk_tree_view_column_new ();
565 cell
= empathy_cell_renderer_text_new ();
566 gtk_tree_view_column_pack_start (col
, cell
, TRUE
);
567 /* EmpathyCellRendererText displays "name" above and "status" below.
568 * We want the login above since it'll always be available, and the
569 * name below since we won't always have it. */
570 gtk_tree_view_column_add_attribute (col
, cell
,
571 "name", LOGIN_COLUMN
);
572 gtk_tree_view_column_add_attribute (col
, cell
,
573 "status", NAME_COLUMN
);
575 cell
= empathy_cell_renderer_activatable_new ();
576 gtk_tree_view_column_pack_end (col
, cell
, FALSE
);
577 g_object_set (cell
, "stock-id", EMPATHY_IMAGE_CONTACT_INFORMATION
, NULL
);
578 g_signal_connect (cell
, "path-activated",
579 G_CALLBACK (on_profile_button_clicked_cb
), self
);
581 gtk_tree_view_append_column (GTK_TREE_VIEW (priv
->tree_view
), col
);
583 gtk_dialog_add_button (GTK_DIALOG (self
),
584 GTK_STOCK_CLOSE
, GTK_RESPONSE_CLOSE
);
586 priv
->add_button
= gtk_dialog_add_button (GTK_DIALOG (self
),
587 _("_Add Contact"), GTK_RESPONSE_APPLY
);
588 gtk_widget_set_sensitive (priv
->add_button
, FALSE
);
589 gtk_button_set_image (GTK_BUTTON (priv
->add_button
),
590 gtk_image_new_from_stock (GTK_STOCK_ADD
, GTK_ICON_SIZE_BUTTON
));
592 /* Pack the dialog */
593 priv
->notebook
= gtk_notebook_new ();
594 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (priv
->notebook
), FALSE
);
595 g_object_set (priv
->notebook
, "margin", 6, NULL
);
597 scrolled_window
= gtk_scrolled_window_new (NULL
, NULL
);
598 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window
),
599 GTK_POLICY_NEVER
, GTK_POLICY_AUTOMATIC
);
601 gtk_container_add (GTK_CONTAINER (scrolled_window
), priv
->tree_view
);
603 priv
->no_contact_found
= gtk_label_new (NULL
);
604 tmp
= g_strdup_printf ("<b><span size='xx-large'>%s</span></b>",
605 _("No contacts found"));
606 gtk_label_set_markup (GTK_LABEL (priv
->no_contact_found
), tmp
);
609 gtk_label_set_ellipsize (GTK_LABEL (priv
->no_contact_found
),
610 PANGO_ELLIPSIZE_END
);
612 gtk_notebook_append_page (GTK_NOTEBOOK (priv
->notebook
), scrolled_window
,
614 gtk_notebook_append_page (GTK_NOTEBOOK (priv
->notebook
),
615 priv
->no_contact_found
, NULL
);
617 gtk_box_pack_start (GTK_BOX (vbox
), priv
->notebook
, TRUE
, TRUE
, 3);
619 /* Request message textview */
620 priv
->message_label
= gtk_label_new (
621 _("Your message introducing yourself:"));
622 gtk_misc_set_alignment (GTK_MISC (priv
->message_label
), 0, 0.5);
624 priv
->message
= gtk_text_view_new ();
625 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (priv
->message
),
627 gtk_text_buffer_set_text (
628 gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv
->message
)),
629 _("Please let me see when you're online. Thanks!"), -1);
631 priv
->message_window
= gtk_scrolled_window_new (NULL
, NULL
);
632 gtk_scrolled_window_set_shadow_type (
633 GTK_SCROLLED_WINDOW (priv
->message_window
),
634 GTK_SHADOW_ETCHED_IN
);
635 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv
->message_window
),
636 GTK_POLICY_NEVER
, GTK_POLICY_AUTOMATIC
);
638 gtk_container_add (GTK_CONTAINER (priv
->message_window
), priv
->message
);
640 gtk_box_pack_start (GTK_BOX (vbox
), priv
->message_label
, FALSE
, TRUE
, 3);
641 gtk_box_pack_start (GTK_BOX (vbox
), priv
->message_window
, FALSE
, TRUE
, 3);
643 gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (
644 GTK_DIALOG (self
))), vbox
, TRUE
, TRUE
, 0);
646 gtk_window_set_default_size (GTK_WINDOW (self
), 200, 400);
647 gtk_widget_show_all (vbox
);
648 gtk_widget_hide (priv
->spinner
);
652 empathy_contact_search_dialog_new (GtkWindow
*parent
)
656 g_return_val_if_fail (parent
== NULL
|| GTK_IS_WINDOW (parent
), NULL
);
658 self
= g_object_new (EMPATHY_TYPE_CONTACT_SEARCH_DIALOG
, NULL
);
662 gtk_window_set_transient_for (GTK_WINDOW (self
), parent
);