]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-selector-dialog.c
contact-selector-dialog: make table_contact a protected member
[empathy.git] / libempathy-gtk / empathy-contact-selector-dialog.c
1 /*
2  * Copyright (C) 2007-2009 Collabora Ltd.
3  *
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.
8  *
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.
13  *
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
17  *
18  * Authors: Xavier Claessens <xclaesse@gmail.com>
19  * Authors: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include <gtk/gtk.h>
28 #include <glib/gi18n-lib.h>
29
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>
34
35 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
36 #include <libempathy/empathy-debug.h>
37
38 #include <libempathy-gtk/empathy-ui-utils.h>
39 #include <libempathy-gtk/empathy-images.h>
40
41 #include "empathy-contact-selector-dialog.h"
42 #include "empathy-account-chooser.h"
43
44 G_DEFINE_TYPE(EmpathyContactSelectorDialog, empathy_contact_selector_dialog,
45     GTK_TYPE_DIALOG)
46
47 typedef struct _EmpathyContactSelectorDialogPriv \
48           EmpathyContactSelectorDialogPriv;
49
50 struct _EmpathyContactSelectorDialogPriv {
51   GtkWidget *account_chooser;
52   GtkWidget *entry_id;
53   EmpathyContactManager *contact_manager;
54 };
55
56 #define GET_PRIV(o) \
57   (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_CONTACT_SELECTOR_DIALOG, \
58     EmpathyContactSelectorDialogPriv))
59
60 enum {
61   COMPLETION_COL_TEXT,
62   COMPLETION_COL_ID,
63   COMPLETION_COL_NAME,
64 } CompletionCol;
65
66 static void
67 contact_selector_dialog_account_changed_cb (GtkWidget *widget,
68     EmpathyContactSelectorDialog *dialog)
69 {
70   EmpathyContactSelectorDialogPriv *priv = GET_PRIV (dialog);
71   EmpathyAccountChooser *chooser;
72   TpConnection *connection;
73   EmpathyTpContactList *contact_list;
74   GList *members;
75   GtkListStore *store;
76   GtkEntryCompletion *completion;
77   GtkTreeIter iter;
78   gchar *tmpstr;
79
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);
84
85   /* Get members of the new account */
86   chooser = EMPATHY_ACCOUNT_CHOOSER (priv->account_chooser);
87   connection = empathy_account_chooser_get_connection (chooser);
88   if (!connection)
89     return;
90
91   contact_list = empathy_contact_manager_get_list (priv->contact_manager,
92                connection);
93   members = empathy_contact_list_get_members (
94       EMPATHY_CONTACT_LIST (contact_list));
95
96   /* Add members to the completion */
97   while (members)
98     {
99       EmpathyContact *contact = members->data;
100
101       DEBUG ("Adding contact ID %s, Name %s",
102              empathy_contact_get_id (contact),
103              empathy_contact_get_name (contact));
104
105       tmpstr = g_strdup_printf ("%s (%s)",
106         empathy_contact_get_name (contact),
107         empathy_contact_get_id (contact));
108
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),
113         -1);
114
115       g_free (tmpstr);
116
117       g_object_unref (contact);
118       members = g_list_delete_link (members, members);
119   }
120 }
121
122 static gboolean
123 contact_selector_dialog_match_selected_cb (GtkEntryCompletion *widget,
124     GtkTreeModel *model,
125     GtkTreeIter *iter,
126     EmpathyContactSelectorDialog *dialog)
127 {
128   EmpathyContactSelectorDialogPriv *priv = GET_PRIV (dialog);
129   gchar *id;
130
131   if (!iter || !model)
132     return FALSE;
133
134   gtk_tree_model_get (model, iter, COMPLETION_COL_ID, &id, -1);
135   gtk_entry_set_text (GTK_ENTRY (priv->entry_id), id);
136
137   DEBUG ("Got selected match **%s**", id);
138
139   g_free (id);
140
141   return TRUE;
142 }
143
144 static gboolean
145 contact_selector_dialog_match_func (GtkEntryCompletion *completion,
146     const gchar *key,
147     GtkTreeIter *iter,
148     gpointer user_data)
149 {
150   GtkTreeModel *model;
151   gchar *id;
152   gchar *name;
153
154   model = gtk_entry_completion_get_model (completion);
155   if (!model || !iter)
156     return FALSE;
157
158   gtk_tree_model_get (model, iter, COMPLETION_COL_NAME, &name, -1);
159   if (strstr (name, key))
160     {
161       DEBUG ("Key %s is matching name **%s**", key, name);
162       g_free (name);
163       return TRUE;
164     }
165   g_free (name);
166
167   gtk_tree_model_get (model, iter, COMPLETION_COL_ID, &id, -1);
168   if (strstr (id, key))
169     {
170       DEBUG ("Key %s is matching ID **%s**", key, id);
171       g_free (id);
172       return TRUE;
173     }
174   g_free (id);
175
176   return FALSE;
177 }
178
179 static void
180 contact_selector_dialog_response_cb (GtkWidget *widget,
181     gint response,
182     EmpathyContactSelectorDialog *dialog)
183 {
184   EmpathyContactSelectorDialogPriv *priv = GET_PRIV (dialog);
185   TpConnection *connection;
186   const gchar *id;
187   EmpathyContactSelectorDialogClass *class = \
188     EMPATHY_CONTACT_SELECTOR_DIALOG_GET_CLASS (dialog);
189
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))
194     {
195       gtk_widget_destroy (widget);
196       return;
197     }
198
199   if (response == GTK_RESPONSE_ACCEPT)
200     {
201       class->got_response (dialog, connection, id);
202     }
203
204   gtk_widget_destroy (widget);
205 }
206
207 static void
208 contact_selector_change_state_button_cb  (GtkEditable *editable,
209     EmpathyContactSelectorDialog *dialog)
210 {
211   const gchar *id;
212   gboolean sensitive;
213
214   id = gtk_entry_get_text (GTK_ENTRY (editable));
215   sensitive = !EMP_STR_EMPTY (id);
216
217   gtk_widget_set_sensitive (dialog->button_action, sensitive);
218 }
219
220 static void
221 entry_activate_cb (GtkEntry *entry,
222     gpointer self)
223 {
224   const gchar *id;
225
226   id = gtk_entry_get_text (entry);
227   if (EMP_STR_EMPTY (id))
228     return;
229
230   gtk_dialog_response (GTK_DIALOG (self), GTK_RESPONSE_ACCEPT);
231 }
232
233 static void
234 empathy_contact_selector_dialog_init (EmpathyContactSelectorDialog *dialog)
235 {
236   EmpathyContactSelectorDialogPriv *priv = GET_PRIV (dialog);
237   GtkBuilder *gui;
238   gchar *filename;
239   GtkEntryCompletion *completion;
240   GtkListStore *model;
241   GtkWidget *content_area;
242
243   /* create a contact manager */
244   priv->contact_manager = empathy_contact_manager_dup_singleton ();
245
246   filename = empathy_file_lookup ("empathy-contact-selector-dialog.ui",
247           "libempathy-gtk");
248   gui = empathy_builder_get_file (filename,
249                 "table_contact", &dialog->table_contact,
250                 "entry_id", &priv->entry_id,
251                 NULL);
252   g_free (filename);
253
254   empathy_builder_connect (gui, dialog,
255       "entry_id", "activate", entry_activate_cb,
256       NULL);
257
258   content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
259   gtk_container_add (GTK_CONTAINER (content_area), dialog->table_contact);
260
261   gtk_dialog_add_button (GTK_DIALOG (dialog),
262     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
263
264   /* Tweak the dialog */
265   gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
266
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);
270
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,
277                NULL, NULL);
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),
282         dialog);
283   g_object_unref (completion);
284   g_object_unref (model);
285
286   g_signal_connect (dialog, "response",
287         G_CALLBACK (contact_selector_dialog_response_cb), dialog);
288
289   empathy_builder_connect (gui, dialog,
290              "entry_id", "changed", contact_selector_change_state_button_cb,
291              NULL);
292
293   g_object_unref (gui);
294
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,
299            1, 2, 0, 1);
300   empathy_account_chooser_set_filter (
301       EMPATHY_ACCOUNT_CHOOSER (priv->account_chooser),
302       empathy_account_chooser_filter_is_connected,
303       NULL);
304   gtk_widget_show (priv->account_chooser);
305
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),
309         dialog);
310 }
311
312 static void
313 empathy_contact_selector_dialog_dispose (GObject *object)
314 {
315   EmpathyContactSelectorDialogPriv *priv = GET_PRIV (object);
316
317   if (priv->contact_manager != NULL) {
318     g_object_unref (priv->contact_manager);
319     priv->contact_manager = NULL;
320   }
321
322   if (G_OBJECT_CLASS (empathy_contact_selector_dialog_parent_class)->dispose)
323     G_OBJECT_CLASS (empathy_contact_selector_dialog_parent_class)->dispose (
324         object);
325 }
326
327 static void
328 empathy_contact_selector_dialog_class_init (
329     EmpathyContactSelectorDialogClass *class)
330 {
331   GObjectClass *object_class = G_OBJECT_CLASS (class);
332
333   g_type_class_add_private (class, sizeof (EmpathyContactSelectorDialogPriv));
334
335   object_class->dispose = empathy_contact_selector_dialog_dispose;
336 }