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