]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-selector-dialog.c
contact-selector-dialog: set a border of 12 as defined in the HIG
[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 gboolean
234 account_chooser_filter (TpAccount *account,
235     gpointer user_data)
236 {
237   EmpathyContactSelectorDialog *self = user_data;
238   EmpathyContactSelectorDialogClass *class = \
239       EMPATHY_CONTACT_SELECTOR_DIALOG_GET_CLASS (self);
240
241   if (class->account_filter == NULL)
242     return empathy_account_chooser_filter_is_connected (account, user_data);
243
244   return class->account_filter (self, account);
245 }
246
247 static void
248 empathy_contact_selector_dialog_init (EmpathyContactSelectorDialog *dialog)
249 {
250   EmpathyContactSelectorDialogPriv *priv = GET_PRIV (dialog);
251   GtkBuilder *gui;
252   gchar *filename;
253   GtkEntryCompletion *completion;
254   GtkListStore *model;
255   GtkWidget *content_area;
256
257   /* create a contact manager */
258   priv->contact_manager = empathy_contact_manager_dup_singleton ();
259
260   filename = empathy_file_lookup ("empathy-contact-selector-dialog.ui",
261           "libempathy-gtk");
262   gui = empathy_builder_get_file (filename,
263                 "table_contact", &dialog->table_contact,
264                 "entry_id", &priv->entry_id,
265                 NULL);
266   g_free (filename);
267
268   empathy_builder_connect (gui, dialog,
269       "entry_id", "activate", entry_activate_cb,
270       NULL);
271
272   content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
273   gtk_container_add (GTK_CONTAINER (content_area), dialog->table_contact);
274
275   gtk_dialog_add_button (GTK_DIALOG (dialog),
276     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
277
278   /* Tweak the dialog */
279   gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
280
281   gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
282   gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER_ON_PARENT);
283   gtk_window_set_type_hint (GTK_WINDOW (dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
284
285   gtk_container_set_border_width (GTK_CONTAINER (dialog), 12);
286
287   /* text completion */
288   completion = gtk_entry_completion_new ();
289   model = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
290   gtk_entry_completion_set_text_column (completion, COMPLETION_COL_TEXT);
291   gtk_entry_completion_set_match_func (completion,
292                contact_selector_dialog_match_func,
293                NULL, NULL);
294   gtk_entry_completion_set_model (completion, GTK_TREE_MODEL (model));
295   gtk_entry_set_completion (GTK_ENTRY (priv->entry_id), completion);
296   g_signal_connect (completion, "match-selected",
297         G_CALLBACK (contact_selector_dialog_match_selected_cb),
298         dialog);
299   g_object_unref (completion);
300   g_object_unref (model);
301
302   g_signal_connect (dialog, "response",
303         G_CALLBACK (contact_selector_dialog_response_cb), dialog);
304
305   empathy_builder_connect (gui, dialog,
306              "entry_id", "changed", contact_selector_change_state_button_cb,
307              NULL);
308
309   g_object_unref (gui);
310
311   /* Create account chooser */
312   priv->account_chooser = empathy_account_chooser_new ();
313   gtk_table_attach_defaults (GTK_TABLE (dialog->table_contact),
314            priv->account_chooser,
315            1, 2, 0, 1);
316   empathy_account_chooser_set_filter (
317       EMPATHY_ACCOUNT_CHOOSER (priv->account_chooser),
318       account_chooser_filter,
319       dialog);
320   gtk_widget_show (priv->account_chooser);
321
322   contact_selector_dialog_account_changed_cb (priv->account_chooser, dialog);
323   g_signal_connect (priv->account_chooser, "changed",
324         G_CALLBACK (contact_selector_dialog_account_changed_cb),
325         dialog);
326 }
327
328 static void
329 empathy_contact_selector_dialog_dispose (GObject *object)
330 {
331   EmpathyContactSelectorDialogPriv *priv = GET_PRIV (object);
332
333   if (priv->contact_manager != NULL) {
334     g_object_unref (priv->contact_manager);
335     priv->contact_manager = NULL;
336   }
337
338   if (G_OBJECT_CLASS (empathy_contact_selector_dialog_parent_class)->dispose)
339     G_OBJECT_CLASS (empathy_contact_selector_dialog_parent_class)->dispose (
340         object);
341 }
342
343 static void
344 empathy_contact_selector_dialog_class_init (
345     EmpathyContactSelectorDialogClass *class)
346 {
347   GObjectClass *object_class = G_OBJECT_CLASS (class);
348
349   g_type_class_add_private (class, sizeof (EmpathyContactSelectorDialogPriv));
350
351   object_class->dispose = empathy_contact_selector_dialog_dispose;
352 }