2 * Copyright (C) 2007-2009 Collabora Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Xavier Claessens <xclaesse@gmail.com>
19 * Authors: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
20 * Authors: Danielle Madeley <danielle.madeley@collabora.co.uk>
29 #include <glib/gi18n-lib.h>
31 #include <libempathy/empathy-tp-contact-factory.h>
32 #include <libempathy/empathy-contact-manager.h>
33 #include <libempathy/empathy-dispatcher.h>
34 #include <libempathy/empathy-utils.h>
36 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
37 #include <libempathy/empathy-debug.h>
39 #include <libempathy-gtk/empathy-ui-utils.h>
40 #include <libempathy-gtk/empathy-images.h>
42 #include "empathy-contact-selector-dialog.h"
43 #include "empathy-account-chooser.h"
45 G_DEFINE_ABSTRACT_TYPE (EmpathyContactSelectorDialog,
46 empathy_contact_selector_dialog,
49 typedef struct _EmpathyContactSelectorDialogPriv \
50 EmpathyContactSelectorDialogPriv;
52 struct _EmpathyContactSelectorDialogPriv {
54 GtkWidget *account_chooser_label;
55 GtkWidget *account_chooser;
57 EmpathyContactManager *contact_manager;
58 TpAccount *filter_account;
60 gboolean show_account_chooser;
64 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_CONTACT_SELECTOR_DIALOG, \
65 EmpathyContactSelectorDialogPriv))
69 PROP_SHOW_ACCOUNT_CHOOSER,
80 contact_selector_dialog_account_changed_cb (GtkWidget *widget,
81 EmpathyContactSelectorDialog *dialog)
83 EmpathyContactSelectorDialogPriv *priv = GET_PRIV (dialog);
84 EmpathyAccountChooser *chooser;
85 TpConnection *connection;
88 /* Remove completions */
89 gtk_list_store_clear (priv->store);
91 /* Get members of the new account */
92 chooser = EMPATHY_ACCOUNT_CHOOSER (priv->account_chooser);
93 connection = empathy_account_chooser_get_connection (chooser);
97 if (priv->show_account_chooser)
99 EmpathyTpContactList *contact_list;
101 contact_list = empathy_contact_manager_get_list (priv->contact_manager,
103 members = empathy_contact_list_get_members (
104 EMPATHY_CONTACT_LIST (contact_list));
108 if (priv->filter_account != NULL)
110 EmpathyTpContactList *contact_list;
112 connection = tp_account_get_connection (priv->filter_account);
113 if (connection == NULL)
116 contact_list = empathy_contact_manager_get_list (
117 priv->contact_manager, connection);
119 members = empathy_contact_list_get_members (
120 EMPATHY_CONTACT_LIST (contact_list));
124 members = empathy_contact_list_get_members (
125 EMPATHY_CONTACT_LIST (priv->contact_manager));
129 /* Add members to the completion */
132 EmpathyContact *contact = members->data;
136 DEBUG ("Adding contact ID %s, Name %s",
137 empathy_contact_get_id (contact),
138 empathy_contact_get_alias (contact));
140 tmpstr = g_strdup_printf ("%s (%s)",
141 empathy_contact_get_alias (contact),
142 empathy_contact_get_id (contact));
144 gtk_list_store_insert_with_values (priv->store, &iter, -1,
145 COMPLETION_COL_TEXT, tmpstr,
146 COMPLETION_COL_ID, empathy_contact_get_id (contact),
147 COMPLETION_COL_NAME, empathy_contact_get_alias (contact),
152 g_object_unref (contact);
153 members = g_list_delete_link (members, members);
158 contact_selector_dialog_match_selected_cb (GtkEntryCompletion *widget,
161 EmpathyContactSelectorDialog *dialog)
163 EmpathyContactSelectorDialogPriv *priv = GET_PRIV (dialog);
169 gtk_tree_model_get (model, iter, COMPLETION_COL_ID, &id, -1);
170 gtk_entry_set_text (GTK_ENTRY (priv->entry_id), id);
172 DEBUG ("Got selected match **%s**", id);
180 contact_selector_dialog_match_func (GtkEntryCompletion *completion,
189 model = gtk_entry_completion_get_model (completion);
193 gtk_tree_model_get (model, iter, COMPLETION_COL_NAME, &str, -1);
194 lower = g_utf8_strdown (str, -1);
195 if (strstr (lower, key))
197 DEBUG ("Key %s is matching name **%s**", key, str);
204 gtk_tree_model_get (model, iter, COMPLETION_COL_ID, &str, -1);
205 lower = g_utf8_strdown (str, -1);
206 if (strstr (lower, key))
208 DEBUG ("Key %s is matching ID **%s**", key, str);
221 contact_selector_change_state_button_cb (GtkEditable *editable,
222 EmpathyContactSelectorDialog *dialog)
227 id = gtk_entry_get_text (GTK_ENTRY (editable));
228 sensitive = !EMP_STR_EMPTY (id);
230 gtk_widget_set_sensitive (dialog->button_action, sensitive);
234 entry_activate_cb (GtkEntry *entry,
239 id = gtk_entry_get_text (entry);
240 if (EMP_STR_EMPTY (id))
243 gtk_dialog_response (GTK_DIALOG (self), GTK_RESPONSE_ACCEPT);
247 account_chooser_filter (TpAccount *account,
250 EmpathyContactSelectorDialog *self = user_data;
251 EmpathyContactSelectorDialogClass *class = \
252 EMPATHY_CONTACT_SELECTOR_DIALOG_GET_CLASS (self);
254 if (class->account_filter == NULL)
255 return empathy_account_chooser_filter_is_connected (account, user_data);
257 return class->account_filter (self, account);
261 contact_selector_dialog_filter_visible (GtkTreeModel *model,
265 EmpathyContactSelectorDialog *self = EMPATHY_CONTACT_SELECTOR_DIALOG (data);
269 gtk_tree_model_get (model, iter,
270 COMPLETION_COL_ID, &id,
273 /* this must be non-NULL for this function to get called */
274 r = EMPATHY_CONTACT_SELECTOR_DIALOG_GET_CLASS (self)->contact_filter (
283 empathy_contact_selector_dialog_init (EmpathyContactSelectorDialog *dialog)
285 EmpathyContactSelectorDialogPriv *priv = GET_PRIV (dialog);
288 GtkEntryCompletion *completion;
289 GtkWidget *content_area;
290 GtkWidget *table_contact;
292 dialog->vbox = gtk_vbox_new (FALSE, 3);
294 /* create a contact manager */
295 priv->contact_manager = empathy_contact_manager_dup_singleton ();
297 filename = empathy_file_lookup ("empathy-contact-selector-dialog.ui",
299 gui = empathy_builder_get_file (filename,
300 "table_contact", &table_contact,
301 "account_chooser_label", &priv->account_chooser_label,
302 "entry_id", &priv->entry_id,
306 empathy_builder_connect (gui, dialog,
307 "entry_id", "activate", entry_activate_cb,
310 content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
311 gtk_container_add (GTK_CONTAINER (content_area), dialog->vbox);
312 gtk_box_pack_start (GTK_BOX (dialog->vbox), table_contact, TRUE, TRUE, 0);
313 gtk_widget_show (dialog->vbox);
315 gtk_dialog_add_button (GTK_DIALOG (dialog),
316 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
318 /* Tweak the dialog */
319 gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
320 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER_ON_PARENT);
321 gtk_window_set_type_hint (GTK_WINDOW (dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
323 gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox), 6);
324 gtk_container_set_border_width (GTK_CONTAINER (dialog), 6);
326 /* text completion */
327 priv->store = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
329 completion = gtk_entry_completion_new ();
330 gtk_entry_completion_set_text_column (completion, COMPLETION_COL_TEXT);
331 gtk_entry_completion_set_match_func (completion,
332 contact_selector_dialog_match_func,
334 gtk_entry_completion_set_model (completion, GTK_TREE_MODEL (priv->store));
335 gtk_entry_set_completion (GTK_ENTRY (priv->entry_id), completion);
336 g_signal_connect (completion, "match-selected",
337 G_CALLBACK (contact_selector_dialog_match_selected_cb),
339 g_object_unref (completion);
340 g_object_unref (priv->store);
342 empathy_builder_connect (gui, dialog,
343 "entry_id", "changed", contact_selector_change_state_button_cb,
346 g_object_unref (gui);
348 /* Create account chooser */
349 priv->show_account_chooser = TRUE;
350 priv->account_chooser = empathy_account_chooser_new ();
351 gtk_table_attach_defaults (GTK_TABLE (table_contact),
352 priv->account_chooser,
354 empathy_account_chooser_set_filter (
355 EMPATHY_ACCOUNT_CHOOSER (priv->account_chooser),
356 account_chooser_filter,
358 gtk_widget_show (priv->account_chooser);
360 contact_selector_dialog_account_changed_cb (priv->account_chooser, dialog);
361 g_signal_connect (priv->account_chooser, "changed",
362 G_CALLBACK (contact_selector_dialog_account_changed_cb),
367 empathy_contact_selector_dialog_get_property (GObject *self,
372 EmpathyContactSelectorDialog *dialog = EMPATHY_CONTACT_SELECTOR_DIALOG (self);
376 case PROP_SHOW_ACCOUNT_CHOOSER:
377 g_value_set_boolean (value,
378 empathy_contact_selector_dialog_get_show_account_chooser (dialog));
381 case PROP_FILTER_ACCOUNT:
382 g_value_set_object (value,
383 empathy_contact_selector_dialog_get_filter_account (dialog));
387 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
393 empathy_contact_selector_dialog_set_property (GObject *self,
398 EmpathyContactSelectorDialog *dialog = EMPATHY_CONTACT_SELECTOR_DIALOG (self);
402 case PROP_SHOW_ACCOUNT_CHOOSER:
403 empathy_contact_selector_dialog_set_show_account_chooser (dialog,
404 g_value_get_boolean (value));
407 case PROP_FILTER_ACCOUNT:
408 empathy_contact_selector_dialog_set_filter_account (dialog,
409 g_value_get_object (value));
413 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
419 empathy_contact_selector_dialog_constructed (GObject *dialog)
421 EmpathyContactSelectorDialogPriv *priv = GET_PRIV (dialog);
423 if (EMPATHY_CONTACT_SELECTOR_DIALOG_GET_CLASS (dialog)->contact_filter)
425 GtkEntryCompletion *completion;
426 GtkTreeModel *filter;
428 completion = gtk_entry_get_completion (GTK_ENTRY (priv->entry_id));
429 filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store), NULL);
431 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (filter),
432 contact_selector_dialog_filter_visible, dialog, NULL);
434 gtk_entry_completion_set_model (completion, filter);
435 g_object_unref (filter);
440 empathy_contact_selector_dialog_dispose (GObject *object)
442 EmpathyContactSelectorDialogPriv *priv = GET_PRIV (object);
444 if (priv->contact_manager != NULL) {
445 g_object_unref (priv->contact_manager);
446 priv->contact_manager = NULL;
449 if (priv->filter_account != NULL) {
450 g_object_unref (priv->filter_account);
451 priv->filter_account = NULL;
454 if (G_OBJECT_CLASS (empathy_contact_selector_dialog_parent_class)->dispose)
455 G_OBJECT_CLASS (empathy_contact_selector_dialog_parent_class)->dispose (
460 empathy_contact_selector_dialog_class_init (
461 EmpathyContactSelectorDialogClass *class)
463 GObjectClass *object_class = G_OBJECT_CLASS (class);
465 g_type_class_add_private (class, sizeof (EmpathyContactSelectorDialogPriv));
467 class->contact_filter = NULL;
469 object_class->constructed = empathy_contact_selector_dialog_constructed;
470 object_class->dispose = empathy_contact_selector_dialog_dispose;
471 object_class->get_property = empathy_contact_selector_dialog_get_property;
472 object_class->set_property = empathy_contact_selector_dialog_set_property;
474 g_object_class_install_property (object_class, PROP_SHOW_ACCOUNT_CHOOSER,
475 g_param_spec_boolean ("show-account-chooser",
476 "Show Account Chooser",
477 "Whether or not this dialog should show an account chooser",
481 g_object_class_install_property (object_class, PROP_FILTER_ACCOUNT,
482 g_param_spec_object ("filter-account",
483 "Account to filter contacts",
484 "if 'show-account-chooser' is unset, only the contacts from this "
485 "account are displayed",
487 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
491 empathy_contact_selector_dialog_get_selected (
492 EmpathyContactSelectorDialog *self,
493 TpConnection **connection,
496 EmpathyContactSelectorDialogPriv *priv;
499 g_return_val_if_fail (EMPATHY_IS_CONTACT_SELECTOR_DIALOG (self), NULL);
501 priv = GET_PRIV (self);
503 if (connection != NULL)
505 if (priv->show_account_chooser)
506 *connection = empathy_account_chooser_get_connection (
507 EMPATHY_ACCOUNT_CHOOSER (priv->account_chooser));
514 if (priv->show_account_chooser)
515 *account = empathy_account_chooser_get_account (
516 EMPATHY_ACCOUNT_CHOOSER (priv->account_chooser));
522 id = gtk_entry_get_text (GTK_ENTRY (priv->entry_id));
527 empathy_contact_selector_dialog_set_show_account_chooser (
528 EmpathyContactSelectorDialog *self,
529 gboolean show_account_chooser)
531 EmpathyContactSelectorDialogPriv *priv;
533 g_return_if_fail (EMPATHY_IS_CONTACT_SELECTOR_DIALOG (self));
535 priv = GET_PRIV (self);
536 priv->show_account_chooser = show_account_chooser;
538 gtk_widget_set_visible (priv->account_chooser_label, show_account_chooser);
539 gtk_widget_set_visible (priv->account_chooser, show_account_chooser);
540 contact_selector_dialog_account_changed_cb (priv->account_chooser, self);
542 g_object_notify (G_OBJECT (self), "show-account-chooser");
546 empathy_contact_selector_dialog_get_show_account_chooser (
547 EmpathyContactSelectorDialog *self)
549 EmpathyContactSelectorDialogPriv *priv;
551 g_return_val_if_fail (EMPATHY_IS_CONTACT_SELECTOR_DIALOG (self), FALSE);
553 priv = GET_PRIV (self);
554 return priv->show_account_chooser;
558 empathy_contact_selector_dialog_set_filter_account (
559 EmpathyContactSelectorDialog *self,
562 EmpathyContactSelectorDialogPriv *priv;
564 g_return_if_fail (EMPATHY_IS_CONTACT_SELECTOR_DIALOG (self));
566 priv = GET_PRIV (self);
567 priv->filter_account = g_object_ref (account);
569 g_object_notify (G_OBJECT (self), "filter-account");
573 empathy_contact_selector_dialog_get_filter_account (
574 EmpathyContactSelectorDialog *self)
576 EmpathyContactSelectorDialogPriv *priv;
578 g_return_val_if_fail (EMPATHY_IS_CONTACT_SELECTOR_DIALOG (self), NULL);
580 priv = GET_PRIV (self);
581 return priv->filter_account;