]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-selector-dialog.c
Merge branch 'ack-all'
[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  * Authors: Danielle Madeley <danielle.madeley@collabora.co.uk>
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26 #include <stdlib.h>
27
28 #include <gtk/gtk.h>
29 #include <glib/gi18n-lib.h>
30
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>
35
36 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
37 #include <libempathy/empathy-debug.h>
38
39 #include <libempathy-gtk/empathy-ui-utils.h>
40 #include <libempathy-gtk/empathy-images.h>
41
42 #include "empathy-contact-selector-dialog.h"
43 #include "empathy-account-chooser.h"
44
45 G_DEFINE_ABSTRACT_TYPE (EmpathyContactSelectorDialog,
46     empathy_contact_selector_dialog,
47     GTK_TYPE_DIALOG)
48
49 typedef struct _EmpathyContactSelectorDialogPriv \
50           EmpathyContactSelectorDialogPriv;
51
52 struct _EmpathyContactSelectorDialogPriv {
53   GtkListStore *store;
54   GtkWidget *account_chooser_label;
55   GtkWidget *account_chooser;
56   GtkWidget *entry_id;
57   EmpathyContactManager *contact_manager;
58
59   gboolean show_account_chooser;
60 };
61
62 #define GET_PRIV(o) \
63   (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_CONTACT_SELECTOR_DIALOG, \
64     EmpathyContactSelectorDialogPriv))
65
66 enum {
67   PROP_0,
68   PROP_SHOW_ACCOUNT_CHOOSER
69 };
70
71 enum {
72   COMPLETION_COL_TEXT,
73   COMPLETION_COL_ID,
74   COMPLETION_COL_NAME,
75 } CompletionCol;
76
77 static void
78 contact_selector_dialog_account_changed_cb (GtkWidget *widget,
79     EmpathyContactSelectorDialog *dialog)
80 {
81   EmpathyContactSelectorDialogPriv *priv = GET_PRIV (dialog);
82   EmpathyAccountChooser *chooser;
83   TpConnection *connection;
84   GList *members;
85
86   /* Remove completions */
87   gtk_list_store_clear (priv->store);
88
89   /* Get members of the new account */
90   chooser = EMPATHY_ACCOUNT_CHOOSER (priv->account_chooser);
91   connection = empathy_account_chooser_get_connection (chooser);
92   if (!connection)
93     return;
94
95   if (priv->show_account_chooser)
96     {
97       EmpathyTpContactList *contact_list;
98
99       contact_list = empathy_contact_manager_get_list (priv->contact_manager,
100                    connection);
101       members = empathy_contact_list_get_members (
102           EMPATHY_CONTACT_LIST (contact_list));
103     }
104   else
105     {
106       members = empathy_contact_list_get_members (
107           EMPATHY_CONTACT_LIST (priv->contact_manager));
108     }
109
110   /* Add members to the completion */
111   while (members)
112     {
113       EmpathyContact *contact = members->data;
114       GtkTreeIter iter;
115       gchar *tmpstr;
116
117       DEBUG ("Adding contact ID %s, Name %s",
118              empathy_contact_get_id (contact),
119              empathy_contact_get_name (contact));
120
121       tmpstr = g_strdup_printf ("%s (%s)",
122         empathy_contact_get_name (contact),
123         empathy_contact_get_id (contact));
124
125       gtk_list_store_insert_with_values (priv->store, &iter, -1,
126         COMPLETION_COL_TEXT, tmpstr,
127         COMPLETION_COL_ID, empathy_contact_get_id (contact),
128         COMPLETION_COL_NAME, empathy_contact_get_name (contact),
129         -1);
130
131       g_free (tmpstr);
132
133       g_object_unref (contact);
134       members = g_list_delete_link (members, members);
135   }
136 }
137
138 static gboolean
139 contact_selector_dialog_match_selected_cb (GtkEntryCompletion *widget,
140     GtkTreeModel *model,
141     GtkTreeIter *iter,
142     EmpathyContactSelectorDialog *dialog)
143 {
144   EmpathyContactSelectorDialogPriv *priv = GET_PRIV (dialog);
145   gchar *id;
146
147   if (!iter || !model)
148     return FALSE;
149
150   gtk_tree_model_get (model, iter, COMPLETION_COL_ID, &id, -1);
151   gtk_entry_set_text (GTK_ENTRY (priv->entry_id), id);
152
153   DEBUG ("Got selected match **%s**", id);
154
155   g_free (id);
156
157   return TRUE;
158 }
159
160 static gboolean
161 contact_selector_dialog_match_func (GtkEntryCompletion *completion,
162     const gchar *key,
163     GtkTreeIter *iter,
164     gpointer user_data)
165 {
166   GtkTreeModel *model;
167   gchar *str, *lower;
168   gboolean v = FALSE;
169
170   model = gtk_entry_completion_get_model (completion);
171   if (!model || !iter)
172     return FALSE;
173
174   gtk_tree_model_get (model, iter, COMPLETION_COL_NAME, &str, -1);
175   lower = g_utf8_strdown (str, -1);
176   if (strstr (lower, key))
177     {
178       DEBUG ("Key %s is matching name **%s**", key, str);
179       v = TRUE;
180       goto out;
181     }
182   g_free (str);
183   g_free (lower);
184
185   gtk_tree_model_get (model, iter, COMPLETION_COL_ID, &str, -1);
186   lower = g_utf8_strdown (str, -1);
187   if (strstr (lower, key))
188     {
189       DEBUG ("Key %s is matching ID **%s**", key, str);
190       v = TRUE;
191       goto out;
192     }
193
194 out:
195   g_free (str);
196   g_free (lower);
197
198   return v;
199 }
200
201 static void
202 contact_selector_change_state_button_cb  (GtkEditable *editable,
203     EmpathyContactSelectorDialog *dialog)
204 {
205   const gchar *id;
206   gboolean sensitive;
207
208   id = gtk_entry_get_text (GTK_ENTRY (editable));
209   sensitive = !EMP_STR_EMPTY (id);
210
211   gtk_widget_set_sensitive (dialog->button_action, sensitive);
212 }
213
214 static void
215 entry_activate_cb (GtkEntry *entry,
216     gpointer self)
217 {
218   const gchar *id;
219
220   id = gtk_entry_get_text (entry);
221   if (EMP_STR_EMPTY (id))
222     return;
223
224   gtk_dialog_response (GTK_DIALOG (self), GTK_RESPONSE_ACCEPT);
225 }
226
227 static gboolean
228 account_chooser_filter (TpAccount *account,
229     gpointer user_data)
230 {
231   EmpathyContactSelectorDialog *self = user_data;
232   EmpathyContactSelectorDialogClass *class = \
233       EMPATHY_CONTACT_SELECTOR_DIALOG_GET_CLASS (self);
234
235   if (class->account_filter == NULL)
236     return empathy_account_chooser_filter_is_connected (account, user_data);
237
238   return class->account_filter (self, account);
239 }
240
241 static gboolean
242 contact_selector_dialog_filter_visible (GtkTreeModel *model,
243                                         GtkTreeIter  *iter,
244                                         gpointer      data)
245 {
246   EmpathyContactSelectorDialog *self = EMPATHY_CONTACT_SELECTOR_DIALOG (data);
247   gboolean r;
248   char *id;
249
250   gtk_tree_model_get (model, iter,
251       COMPLETION_COL_ID, &id,
252       -1);
253
254   /* this must be non-NULL for this function to get called */
255   r = EMPATHY_CONTACT_SELECTOR_DIALOG_GET_CLASS (self)->contact_filter (
256       self, id);
257
258   g_free (id);
259
260   return r;
261 }
262
263 static void
264 empathy_contact_selector_dialog_init (EmpathyContactSelectorDialog *dialog)
265 {
266   EmpathyContactSelectorDialogPriv *priv = GET_PRIV (dialog);
267   GtkBuilder *gui;
268   gchar *filename;
269   GtkEntryCompletion *completion;
270   GtkWidget *content_area;
271   GtkWidget *table_contact;
272
273   dialog->vbox = gtk_vbox_new (FALSE, 3);
274
275   /* create a contact manager */
276   priv->contact_manager = empathy_contact_manager_dup_singleton ();
277
278   filename = empathy_file_lookup ("empathy-contact-selector-dialog.ui",
279           "libempathy-gtk");
280   gui = empathy_builder_get_file (filename,
281                 "table_contact", &table_contact,
282                 "account_chooser_label", &priv->account_chooser_label,
283                 "entry_id", &priv->entry_id,
284                 NULL);
285   g_free (filename);
286
287   empathy_builder_connect (gui, dialog,
288       "entry_id", "activate", entry_activate_cb,
289       NULL);
290
291   content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
292   gtk_container_add (GTK_CONTAINER (content_area), dialog->vbox);
293   gtk_box_pack_start (GTK_BOX (dialog->vbox), table_contact, TRUE, TRUE, 0);
294   gtk_widget_show (dialog->vbox);
295
296   gtk_dialog_add_button (GTK_DIALOG (dialog),
297     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
298
299   /* Tweak the dialog */
300   gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
301
302   gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
303   gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER_ON_PARENT);
304   gtk_window_set_type_hint (GTK_WINDOW (dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
305
306   gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox), 6);
307   gtk_container_set_border_width (GTK_CONTAINER (dialog), 6);
308
309   /* text completion */
310   priv->store = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
311
312   completion = gtk_entry_completion_new ();
313   gtk_entry_completion_set_text_column (completion, COMPLETION_COL_TEXT);
314   gtk_entry_completion_set_match_func (completion,
315                contact_selector_dialog_match_func,
316                NULL, NULL);
317   gtk_entry_completion_set_model (completion, GTK_TREE_MODEL (priv->store));
318   gtk_entry_set_completion (GTK_ENTRY (priv->entry_id), completion);
319   g_signal_connect (completion, "match-selected",
320         G_CALLBACK (contact_selector_dialog_match_selected_cb),
321         dialog);
322   g_object_unref (completion);
323   g_object_unref (priv->store);
324
325   empathy_builder_connect (gui, dialog,
326              "entry_id", "changed", contact_selector_change_state_button_cb,
327              NULL);
328
329   g_object_unref (gui);
330
331   /* Create account chooser */
332   priv->show_account_chooser = TRUE;
333   priv->account_chooser = empathy_account_chooser_new ();
334   gtk_table_attach_defaults (GTK_TABLE (table_contact),
335            priv->account_chooser,
336            1, 2, 0, 1);
337   empathy_account_chooser_set_filter (
338       EMPATHY_ACCOUNT_CHOOSER (priv->account_chooser),
339       account_chooser_filter,
340       dialog);
341   gtk_widget_show (priv->account_chooser);
342
343   contact_selector_dialog_account_changed_cb (priv->account_chooser, dialog);
344   g_signal_connect (priv->account_chooser, "changed",
345         G_CALLBACK (contact_selector_dialog_account_changed_cb),
346         dialog);
347 }
348
349 static void
350 empathy_contact_selector_dialog_get_property (GObject *self,
351     guint prop_id,
352     GValue *value,
353     GParamSpec *pspec)
354 {
355   EmpathyContactSelectorDialog *dialog = EMPATHY_CONTACT_SELECTOR_DIALOG (self);
356
357   switch (prop_id)
358     {
359       case PROP_SHOW_ACCOUNT_CHOOSER:
360         g_value_set_boolean (value,
361           empathy_contact_selector_dialog_get_show_account_chooser (dialog));
362         break;
363
364       default:
365         G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
366         break;
367     }
368 }
369
370 static void
371 empathy_contact_selector_dialog_set_property (GObject *self,
372     guint prop_id,
373     const GValue *value,
374     GParamSpec *pspec)
375 {
376   EmpathyContactSelectorDialog *dialog = EMPATHY_CONTACT_SELECTOR_DIALOG (self);
377
378   switch (prop_id)
379     {
380       case PROP_SHOW_ACCOUNT_CHOOSER:
381         empathy_contact_selector_dialog_set_show_account_chooser (dialog,
382             g_value_get_boolean (value));
383         break;
384
385       default:
386         G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
387         break;
388     }
389 }
390
391 static void
392 empathy_contact_selector_dialog_constructed (GObject *dialog)
393 {
394   EmpathyContactSelectorDialogPriv *priv = GET_PRIV (dialog);
395
396   if (EMPATHY_CONTACT_SELECTOR_DIALOG_GET_CLASS (dialog)->contact_filter)
397     {
398       GtkEntryCompletion *completion;
399       GtkTreeModel *filter;
400
401       completion = gtk_entry_get_completion (GTK_ENTRY (priv->entry_id));
402       filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store), NULL);
403
404       gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (filter),
405           contact_selector_dialog_filter_visible, dialog, NULL);
406
407       gtk_entry_completion_set_model (completion, filter);
408       g_object_unref (filter);
409     }
410 }
411
412 static void
413 empathy_contact_selector_dialog_dispose (GObject *object)
414 {
415   EmpathyContactSelectorDialogPriv *priv = GET_PRIV (object);
416
417   if (priv->contact_manager != NULL) {
418     g_object_unref (priv->contact_manager);
419     priv->contact_manager = NULL;
420   }
421
422   if (G_OBJECT_CLASS (empathy_contact_selector_dialog_parent_class)->dispose)
423     G_OBJECT_CLASS (empathy_contact_selector_dialog_parent_class)->dispose (
424         object);
425 }
426
427 static void
428 empathy_contact_selector_dialog_class_init (
429     EmpathyContactSelectorDialogClass *class)
430 {
431   GObjectClass *object_class = G_OBJECT_CLASS (class);
432
433   g_type_class_add_private (class, sizeof (EmpathyContactSelectorDialogPriv));
434
435   class->contact_filter = NULL;
436
437   object_class->constructed = empathy_contact_selector_dialog_constructed;
438   object_class->dispose = empathy_contact_selector_dialog_dispose;
439   object_class->get_property = empathy_contact_selector_dialog_get_property;
440   object_class->set_property = empathy_contact_selector_dialog_set_property;
441
442   g_object_class_install_property (object_class, PROP_SHOW_ACCOUNT_CHOOSER,
443       g_param_spec_boolean ("show-account-chooser",
444         "Show Account Chooser",
445         "Whether or not this dialog should show an account chooser",
446         TRUE,
447         G_PARAM_READWRITE));
448 }
449
450 const gchar *
451 empathy_contact_selector_dialog_get_selected (
452     EmpathyContactSelectorDialog *self,
453     TpConnection **connection)
454 {
455   EmpathyContactSelectorDialogPriv *priv;
456   const char *id;
457
458   g_return_val_if_fail (EMPATHY_IS_CONTACT_SELECTOR_DIALOG (self), NULL);
459
460   priv = GET_PRIV (self);
461
462   if (connection)
463     {
464       if (priv->show_account_chooser)
465         *connection = empathy_account_chooser_get_connection (
466             EMPATHY_ACCOUNT_CHOOSER (priv->account_chooser));
467       else
468         *connection = NULL;
469     }
470
471   id = gtk_entry_get_text (GTK_ENTRY (priv->entry_id));
472   return id;
473 }
474
475 void
476 empathy_contact_selector_dialog_set_show_account_chooser (
477     EmpathyContactSelectorDialog *self,
478     gboolean show_account_chooser)
479 {
480   EmpathyContactSelectorDialogPriv *priv;
481
482   g_return_if_fail (EMPATHY_IS_CONTACT_SELECTOR_DIALOG (self));
483
484   priv = GET_PRIV (self);
485   priv->show_account_chooser = show_account_chooser;
486
487   gtk_widget_set_visible (priv->account_chooser_label, show_account_chooser);
488   gtk_widget_set_visible (priv->account_chooser, show_account_chooser);
489   contact_selector_dialog_account_changed_cb (priv->account_chooser, self);
490
491   g_object_notify (G_OBJECT (self), "show-account-chooser");
492 }
493
494 gboolean
495 empathy_contact_selector_dialog_get_show_account_chooser (
496     EmpathyContactSelectorDialog *self)
497 {
498   EmpathyContactSelectorDialogPriv *priv;
499
500   g_return_val_if_fail (EMPATHY_IS_CONTACT_SELECTOR_DIALOG (self), FALSE);
501
502   priv = GET_PRIV (self);
503   return priv->show_account_chooser;
504 }