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>
28 #include <glib/gi18n-lib.h>
30 #include <libempathy/empathy-tp-contact-factory.h>
31 #include <libempathy/empathy-contact-manager.h>
32 #include <libempathy/empathy-dispatcher.h>
33 #include <libempathy/empathy-utils.h>
35 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
36 #include <libempathy/empathy-debug.h>
38 #include <libempathy-gtk/empathy-ui-utils.h>
39 #include <libempathy-gtk/empathy-images.h>
41 #include "empathy-contact-selector-dialog.h"
42 #include "empathy-account-chooser.h"
44 G_DEFINE_TYPE(EmpathyContactSelectorDialog, empathy_contact_selector_dialog,
47 typedef struct _EmpathyContactSelectorDialogPriv \
48 EmpathyContactSelectorDialogPriv;
50 struct _EmpathyContactSelectorDialogPriv {
51 GtkWidget *account_chooser;
53 EmpathyContactManager *contact_manager;
57 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_CONTACT_SELECTOR_DIALOG, \
58 EmpathyContactSelectorDialogPriv))
67 contact_selector_dialog_account_changed_cb (GtkWidget *widget,
68 EmpathyContactSelectorDialog *dialog)
70 EmpathyContactSelectorDialogPriv *priv = GET_PRIV (dialog);
71 EmpathyAccountChooser *chooser;
72 TpConnection *connection;
73 EmpathyTpContactList *contact_list;
76 GtkEntryCompletion *completion;
80 /* Remove completions */
81 completion = gtk_entry_get_completion (GTK_ENTRY (priv->entry_id));
82 store = GTK_LIST_STORE (gtk_entry_completion_get_model (completion));
83 gtk_list_store_clear (store);
85 /* Get members of the new account */
86 chooser = EMPATHY_ACCOUNT_CHOOSER (priv->account_chooser);
87 connection = empathy_account_chooser_get_connection (chooser);
91 contact_list = empathy_contact_manager_get_list (priv->contact_manager,
93 members = empathy_contact_list_get_members (
94 EMPATHY_CONTACT_LIST (contact_list));
96 /* Add members to the completion */
99 EmpathyContact *contact = members->data;
101 DEBUG ("Adding contact ID %s, Name %s",
102 empathy_contact_get_id (contact),
103 empathy_contact_get_name (contact));
105 tmpstr = g_strdup_printf ("%s (%s)",
106 empathy_contact_get_name (contact),
107 empathy_contact_get_id (contact));
109 gtk_list_store_insert_with_values (store, &iter, -1,
110 COMPLETION_COL_TEXT, tmpstr,
111 COMPLETION_COL_ID, empathy_contact_get_id (contact),
112 COMPLETION_COL_NAME, empathy_contact_get_name (contact),
117 g_object_unref (contact);
118 members = g_list_delete_link (members, members);
123 contact_selector_dialog_match_selected_cb (GtkEntryCompletion *widget,
126 EmpathyContactSelectorDialog *dialog)
128 EmpathyContactSelectorDialogPriv *priv = GET_PRIV (dialog);
134 gtk_tree_model_get (model, iter, COMPLETION_COL_ID, &id, -1);
135 gtk_entry_set_text (GTK_ENTRY (priv->entry_id), id);
137 DEBUG ("Got selected match **%s**", id);
145 contact_selector_dialog_match_func (GtkEntryCompletion *completion,
154 model = gtk_entry_completion_get_model (completion);
158 gtk_tree_model_get (model, iter, COMPLETION_COL_NAME, &name, -1);
159 if (strstr (name, key))
161 DEBUG ("Key %s is matching name **%s**", key, name);
167 gtk_tree_model_get (model, iter, COMPLETION_COL_ID, &id, -1);
168 if (strstr (id, key))
170 DEBUG ("Key %s is matching ID **%s**", key, id);
180 contact_selector_dialog_response_cb (GtkWidget *widget,
182 EmpathyContactSelectorDialog *dialog)
184 EmpathyContactSelectorDialogPriv *priv = GET_PRIV (dialog);
185 TpConnection *connection;
187 EmpathyContactSelectorDialogClass *class = \
188 EMPATHY_CONTACT_SELECTOR_DIALOG_GET_CLASS (dialog);
190 connection = empathy_account_chooser_get_connection (
191 EMPATHY_ACCOUNT_CHOOSER (priv->account_chooser));
192 id = gtk_entry_get_text (GTK_ENTRY (priv->entry_id));
193 if (!connection || EMP_STR_EMPTY (id))
195 gtk_widget_destroy (widget);
199 if (response == GTK_RESPONSE_ACCEPT)
201 class->got_response (dialog, connection, id);
204 gtk_widget_destroy (widget);
208 contact_selector_change_state_button_cb (GtkEditable *editable,
209 EmpathyContactSelectorDialog *dialog)
214 id = gtk_entry_get_text (GTK_ENTRY (editable));
215 sensitive = !EMP_STR_EMPTY (id);
217 gtk_widget_set_sensitive (dialog->button_action, sensitive);
221 entry_activate_cb (GtkEntry *entry,
226 id = gtk_entry_get_text (entry);
227 if (EMP_STR_EMPTY (id))
230 gtk_dialog_response (GTK_DIALOG (self), GTK_RESPONSE_ACCEPT);
234 empathy_contact_selector_dialog_init (EmpathyContactSelectorDialog *dialog)
236 EmpathyContactSelectorDialogPriv *priv = GET_PRIV (dialog);
239 GtkEntryCompletion *completion;
241 GtkWidget *content_area;
243 /* create a contact manager */
244 priv->contact_manager = empathy_contact_manager_dup_singleton ();
246 filename = empathy_file_lookup ("empathy-contact-selector-dialog.ui",
248 gui = empathy_builder_get_file (filename,
249 "table_contact", &dialog->table_contact,
250 "entry_id", &priv->entry_id,
254 empathy_builder_connect (gui, dialog,
255 "entry_id", "activate", entry_activate_cb,
258 content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
259 gtk_container_add (GTK_CONTAINER (content_area), dialog->table_contact);
261 gtk_dialog_add_button (GTK_DIALOG (dialog),
262 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
264 /* Tweak the dialog */
265 gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
267 gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
268 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER_ON_PARENT);
269 gtk_window_set_type_hint (GTK_WINDOW (dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
271 /* text completion */
272 completion = gtk_entry_completion_new ();
273 model = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
274 gtk_entry_completion_set_text_column (completion, COMPLETION_COL_TEXT);
275 gtk_entry_completion_set_match_func (completion,
276 contact_selector_dialog_match_func,
278 gtk_entry_completion_set_model (completion, GTK_TREE_MODEL (model));
279 gtk_entry_set_completion (GTK_ENTRY (priv->entry_id), completion);
280 g_signal_connect (completion, "match-selected",
281 G_CALLBACK (contact_selector_dialog_match_selected_cb),
283 g_object_unref (completion);
284 g_object_unref (model);
286 g_signal_connect (dialog, "response",
287 G_CALLBACK (contact_selector_dialog_response_cb), dialog);
289 empathy_builder_connect (gui, dialog,
290 "entry_id", "changed", contact_selector_change_state_button_cb,
293 g_object_unref (gui);
295 /* Create account chooser */
296 priv->account_chooser = empathy_account_chooser_new ();
297 gtk_table_attach_defaults (GTK_TABLE (dialog->table_contact),
298 priv->account_chooser,
300 empathy_account_chooser_set_filter (
301 EMPATHY_ACCOUNT_CHOOSER (priv->account_chooser),
302 empathy_account_chooser_filter_is_connected,
304 gtk_widget_show (priv->account_chooser);
306 contact_selector_dialog_account_changed_cb (priv->account_chooser, dialog);
307 g_signal_connect (priv->account_chooser, "changed",
308 G_CALLBACK (contact_selector_dialog_account_changed_cb),
313 empathy_contact_selector_dialog_dispose (GObject *object)
315 EmpathyContactSelectorDialogPriv *priv = GET_PRIV (object);
317 if (priv->contact_manager != NULL) {
318 g_object_unref (priv->contact_manager);
319 priv->contact_manager = NULL;
322 if (G_OBJECT_CLASS (empathy_contact_selector_dialog_parent_class)->dispose)
323 G_OBJECT_CLASS (empathy_contact_selector_dialog_parent_class)->dispose (
328 empathy_contact_selector_dialog_class_init (
329 EmpathyContactSelectorDialogClass *class)
331 GObjectClass *object_class = G_OBJECT_CLASS (class);
333 g_type_class_add_private (class, sizeof (EmpathyContactSelectorDialogPriv));
335 object_class->dispose = empathy_contact_selector_dialog_dispose;