]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-selector-dialog.c
Merge branch 'dialpad-widget'
[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-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_ABSTRACT_TYPE (EmpathyContactSelectorDialog,
45     empathy_contact_selector_dialog,
46     GTK_TYPE_DIALOG)
47
48 typedef struct _EmpathyContactSelectorDialogPriv \
49           EmpathyContactSelectorDialogPriv;
50
51 struct _EmpathyContactSelectorDialogPriv {
52   GtkListStore *store;
53   GtkWidget *account_chooser_label;
54   GtkWidget *account_chooser;
55   GtkWidget *entry_id;
56   EmpathyContactManager *contact_manager;
57   TpAccount *filter_account;
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   PROP_FILTER_ACCOUNT,
70   PROP_SELECTED_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   g_object_notify (G_OBJECT (dialog), "selected-account");
157 }
158
159 static gboolean
160 contact_selector_dialog_match_selected_cb (GtkEntryCompletion *widget,
161     GtkTreeModel *model,
162     GtkTreeIter *iter,
163     EmpathyContactSelectorDialog *dialog)
164 {
165   EmpathyContactSelectorDialogPriv *priv = GET_PRIV (dialog);
166   gchar *id;
167
168   if (!iter || !model)
169     return FALSE;
170
171   gtk_tree_model_get (model, iter, COMPLETION_COL_ID, &id, -1);
172   gtk_entry_set_text (GTK_ENTRY (priv->entry_id), id);
173
174   DEBUG ("Got selected match **%s**", id);
175
176   g_free (id);
177
178   return TRUE;
179 }
180
181 static gboolean
182 contact_selector_dialog_match_func (GtkEntryCompletion *completion,
183     const gchar *key,
184     GtkTreeIter *iter,
185     gpointer user_data)
186 {
187   GtkTreeModel *model;
188   gchar *str, *lower;
189   gboolean v = FALSE;
190
191   model = gtk_entry_completion_get_model (completion);
192   if (!model || !iter)
193     return FALSE;
194
195   gtk_tree_model_get (model, iter, COMPLETION_COL_NAME, &str, -1);
196   lower = g_utf8_strdown (str, -1);
197   if (strstr (lower, key))
198     {
199       DEBUG ("Key %s is matching name **%s**", key, str);
200       v = TRUE;
201       goto out;
202     }
203   g_free (str);
204   g_free (lower);
205
206   gtk_tree_model_get (model, iter, COMPLETION_COL_ID, &str, -1);
207   lower = g_utf8_strdown (str, -1);
208   if (strstr (lower, key))
209     {
210       DEBUG ("Key %s is matching ID **%s**", key, str);
211       v = TRUE;
212       goto out;
213     }
214
215 out:
216   g_free (str);
217   g_free (lower);
218
219   return v;
220 }
221
222 static void
223 contact_selector_change_state_button_cb  (GtkEditable *editable,
224     EmpathyContactSelectorDialog *dialog)
225 {
226   const gchar *id;
227   gboolean sensitive;
228
229   id = gtk_entry_get_text (GTK_ENTRY (editable));
230   sensitive = !EMP_STR_EMPTY (id);
231
232   gtk_widget_set_sensitive (dialog->button_action, sensitive);
233 }
234
235 static void
236 account_chooser_filter (TpAccount *account,
237     EmpathyAccountChooserFilterResultCallback callback,
238     gpointer callback_data,
239     gpointer user_data)
240 {
241   EmpathyContactSelectorDialog *self = user_data;
242   EmpathyContactSelectorDialogClass *class = \
243       EMPATHY_CONTACT_SELECTOR_DIALOG_GET_CLASS (self);
244
245   if (class->account_filter == NULL)
246     {
247       empathy_account_chooser_filter_is_connected (
248           account,callback, callback_data, user_data);
249       return;
250     }
251
252   class->account_filter (self, callback, callback_data, account);
253 }
254
255 static gboolean
256 contact_selector_dialog_filter_visible (GtkTreeModel *model,
257                                         GtkTreeIter  *iter,
258                                         gpointer      data)
259 {
260   EmpathyContactSelectorDialog *self = EMPATHY_CONTACT_SELECTOR_DIALOG (data);
261   gboolean r;
262   char *id;
263
264   gtk_tree_model_get (model, iter,
265       COMPLETION_COL_ID, &id,
266       -1);
267
268   /* this must be non-NULL for this function to get called */
269   r = EMPATHY_CONTACT_SELECTOR_DIALOG_GET_CLASS (self)->contact_filter (
270       self, id);
271
272   g_free (id);
273
274   return r;
275 }
276
277 static void
278 empathy_contact_selector_dialog_init (EmpathyContactSelectorDialog *dialog)
279 {
280   EmpathyContactSelectorDialogPriv *priv = GET_PRIV (dialog);
281   GtkBuilder *gui;
282   gchar *filename;
283   GtkEntryCompletion *completion;
284   GtkWidget *content_area;
285   GtkWidget *table_grid;
286
287   dialog->vbox = gtk_vbox_new (FALSE, 3);
288
289   /* create a contact manager */
290   priv->contact_manager = empathy_contact_manager_dup_singleton ();
291
292   filename = empathy_file_lookup ("empathy-contact-selector-dialog.ui",
293           "libempathy-gtk");
294   gui = empathy_builder_get_file (filename,
295                 "table_grid", &table_grid,
296                 "account_chooser_label", &priv->account_chooser_label,
297                 "entry_id", &priv->entry_id,
298                 NULL);
299   g_free (filename);
300
301   gtk_entry_set_activates_default (GTK_ENTRY (priv->entry_id), TRUE);
302
303   content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
304   gtk_container_add (GTK_CONTAINER (content_area), dialog->vbox);
305   gtk_box_pack_start (GTK_BOX (dialog->vbox), table_grid, TRUE, TRUE, 0);
306   gtk_widget_show (dialog->vbox);
307
308   gtk_dialog_add_button (GTK_DIALOG (dialog),
309     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
310
311   /* Tweak the dialog */
312   gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
313   gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER_ON_PARENT);
314   gtk_window_set_type_hint (GTK_WINDOW (dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
315
316   gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox), 6);
317   gtk_container_set_border_width (GTK_CONTAINER (dialog), 6);
318
319   /* text completion */
320   priv->store = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
321
322   completion = gtk_entry_completion_new ();
323   gtk_entry_completion_set_text_column (completion, COMPLETION_COL_TEXT);
324   gtk_entry_completion_set_match_func (completion,
325                contact_selector_dialog_match_func,
326                NULL, NULL);
327   gtk_entry_completion_set_model (completion, GTK_TREE_MODEL (priv->store));
328   gtk_entry_set_completion (GTK_ENTRY (priv->entry_id), completion);
329   g_signal_connect (completion, "match-selected",
330         G_CALLBACK (contact_selector_dialog_match_selected_cb),
331         dialog);
332   g_object_unref (completion);
333   g_object_unref (priv->store);
334
335   empathy_builder_connect (gui, dialog,
336              "entry_id", "changed", contact_selector_change_state_button_cb,
337              NULL);
338
339   g_object_unref (gui);
340
341   /* Create account chooser */
342   priv->show_account_chooser = TRUE;
343   priv->account_chooser = empathy_account_chooser_new ();
344   gtk_grid_attach (GTK_GRID (table_grid),
345            priv->account_chooser,
346            1, 0, 1, 1);
347   empathy_account_chooser_set_filter (
348       EMPATHY_ACCOUNT_CHOOSER (priv->account_chooser),
349       account_chooser_filter,
350       dialog);
351   gtk_widget_show (priv->account_chooser);
352
353   contact_selector_dialog_account_changed_cb (priv->account_chooser, dialog);
354   g_signal_connect (priv->account_chooser, "changed",
355         G_CALLBACK (contact_selector_dialog_account_changed_cb),
356         dialog);
357 }
358
359 static void
360 empathy_contact_selector_dialog_get_property (GObject *self,
361     guint prop_id,
362     GValue *value,
363     GParamSpec *pspec)
364 {
365   EmpathyContactSelectorDialog *dialog = EMPATHY_CONTACT_SELECTOR_DIALOG (self);
366   EmpathyContactSelectorDialogPriv *priv = GET_PRIV (dialog);
367
368   switch (prop_id)
369     {
370       case PROP_SHOW_ACCOUNT_CHOOSER:
371         g_value_set_boolean (value,
372           empathy_contact_selector_dialog_get_show_account_chooser (dialog));
373         break;
374
375       case PROP_FILTER_ACCOUNT:
376         g_value_set_object (value,
377             empathy_contact_selector_dialog_get_filter_account (dialog));
378         break;
379
380       case PROP_SELECTED_ACCOUNT:
381         g_value_set_object (value, empathy_account_chooser_get_account (
382               EMPATHY_ACCOUNT_CHOOSER (priv->account_chooser)));
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_set_property (GObject *self,
393     guint prop_id,
394     const GValue *value,
395     GParamSpec *pspec)
396 {
397   EmpathyContactSelectorDialog *dialog = EMPATHY_CONTACT_SELECTOR_DIALOG (self);
398   EmpathyContactSelectorDialogPriv *priv = GET_PRIV (dialog);
399
400   switch (prop_id)
401     {
402       case PROP_SHOW_ACCOUNT_CHOOSER:
403         empathy_contact_selector_dialog_set_show_account_chooser (dialog,
404             g_value_get_boolean (value));
405         break;
406
407       case PROP_FILTER_ACCOUNT:
408         empathy_contact_selector_dialog_set_filter_account (dialog,
409             g_value_get_object (value));
410         break;
411
412       case PROP_SELECTED_ACCOUNT:
413         empathy_account_chooser_set_account (
414             EMPATHY_ACCOUNT_CHOOSER (priv->account_chooser),
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   EmpathyContactSelectorDialog *self = EMPATHY_CONTACT_SELECTOR_DIALOG (dialog);
428   EmpathyContactSelectorDialogPriv *priv = GET_PRIV (dialog);
429
430   gtk_widget_set_can_default (self->button_action, TRUE);
431   gtk_widget_grab_default (self->button_action);
432
433   if (EMPATHY_CONTACT_SELECTOR_DIALOG_GET_CLASS (dialog)->contact_filter)
434     {
435       GtkEntryCompletion *completion;
436       GtkTreeModel *filter;
437
438       completion = gtk_entry_get_completion (GTK_ENTRY (priv->entry_id));
439       filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store), NULL);
440
441       gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (filter),
442           contact_selector_dialog_filter_visible, dialog, NULL);
443
444       gtk_entry_completion_set_model (completion, filter);
445       g_object_unref (filter);
446     }
447 }
448
449 static void
450 empathy_contact_selector_dialog_dispose (GObject *object)
451 {
452   EmpathyContactSelectorDialogPriv *priv = GET_PRIV (object);
453
454   if (priv->contact_manager != NULL) {
455     g_object_unref (priv->contact_manager);
456     priv->contact_manager = NULL;
457   }
458
459   if (priv->filter_account != NULL) {
460     g_object_unref (priv->filter_account);
461     priv->filter_account = NULL;
462   }
463
464   if (G_OBJECT_CLASS (empathy_contact_selector_dialog_parent_class)->dispose)
465     G_OBJECT_CLASS (empathy_contact_selector_dialog_parent_class)->dispose (
466         object);
467 }
468
469 static void
470 empathy_contact_selector_dialog_class_init (
471     EmpathyContactSelectorDialogClass *class)
472 {
473   GObjectClass *object_class = G_OBJECT_CLASS (class);
474
475   g_type_class_add_private (class, sizeof (EmpathyContactSelectorDialogPriv));
476
477   class->contact_filter = NULL;
478
479   object_class->constructed = empathy_contact_selector_dialog_constructed;
480   object_class->dispose = empathy_contact_selector_dialog_dispose;
481   object_class->get_property = empathy_contact_selector_dialog_get_property;
482   object_class->set_property = empathy_contact_selector_dialog_set_property;
483
484   g_object_class_install_property (object_class, PROP_SHOW_ACCOUNT_CHOOSER,
485       g_param_spec_boolean ("show-account-chooser",
486         "Show Account Chooser",
487         "Whether or not this dialog should show an account chooser",
488         TRUE,
489         G_PARAM_READWRITE));
490
491   g_object_class_install_property (object_class, PROP_FILTER_ACCOUNT,
492       g_param_spec_object ("filter-account",
493         "Account to filter contacts",
494         "if 'show-account-chooser' is unset, only the contacts from this "
495         "account are displayed",
496         TP_TYPE_ACCOUNT,
497         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
498
499   g_object_class_install_property (object_class, PROP_SELECTED_ACCOUNT,
500       g_param_spec_object ("selected-account",
501         "Selected Account",
502         "Current account selected in the account-chooser",
503         TP_TYPE_ACCOUNT,
504         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
505 }
506
507 const gchar *
508 empathy_contact_selector_dialog_get_selected (
509     EmpathyContactSelectorDialog *self,
510     TpConnection **connection,
511     TpAccount **account)
512 {
513   EmpathyContactSelectorDialogPriv *priv;
514   const char *id;
515
516   g_return_val_if_fail (EMPATHY_IS_CONTACT_SELECTOR_DIALOG (self), NULL);
517
518   priv = GET_PRIV (self);
519
520   if (connection != NULL)
521     {
522       if (priv->show_account_chooser)
523         *connection = empathy_account_chooser_get_connection (
524             EMPATHY_ACCOUNT_CHOOSER (priv->account_chooser));
525       else
526         *connection = NULL;
527     }
528
529   if (account != NULL)
530     {
531       if (priv->show_account_chooser)
532         *account = empathy_account_chooser_get_account (
533             EMPATHY_ACCOUNT_CHOOSER (priv->account_chooser));
534       else
535         *account = NULL;
536     }
537
538
539   id = gtk_entry_get_text (GTK_ENTRY (priv->entry_id));
540   return id;
541 }
542
543 void
544 empathy_contact_selector_dialog_set_show_account_chooser (
545     EmpathyContactSelectorDialog *self,
546     gboolean show_account_chooser)
547 {
548   EmpathyContactSelectorDialogPriv *priv;
549
550   g_return_if_fail (EMPATHY_IS_CONTACT_SELECTOR_DIALOG (self));
551
552   priv = GET_PRIV (self);
553   priv->show_account_chooser = show_account_chooser;
554
555   gtk_widget_set_visible (priv->account_chooser_label, show_account_chooser);
556   gtk_widget_set_visible (priv->account_chooser, show_account_chooser);
557   contact_selector_dialog_account_changed_cb (priv->account_chooser, self);
558
559   g_object_notify (G_OBJECT (self), "show-account-chooser");
560 }
561
562 gboolean
563 empathy_contact_selector_dialog_get_show_account_chooser (
564     EmpathyContactSelectorDialog *self)
565 {
566   EmpathyContactSelectorDialogPriv *priv;
567
568   g_return_val_if_fail (EMPATHY_IS_CONTACT_SELECTOR_DIALOG (self), FALSE);
569
570   priv = GET_PRIV (self);
571   return priv->show_account_chooser;
572 }
573
574 void
575 empathy_contact_selector_dialog_set_filter_account (
576     EmpathyContactSelectorDialog *self,
577     TpAccount *account)
578 {
579   EmpathyContactSelectorDialogPriv *priv;
580
581   g_return_if_fail (EMPATHY_IS_CONTACT_SELECTOR_DIALOG (self));
582
583   priv = GET_PRIV (self);
584   priv->filter_account = g_object_ref (account);
585
586   g_object_notify (G_OBJECT (self), "filter-account");
587 }
588
589 TpAccount *
590 empathy_contact_selector_dialog_get_filter_account (
591     EmpathyContactSelectorDialog *self)
592 {
593   EmpathyContactSelectorDialogPriv *priv;
594
595   g_return_val_if_fail (EMPATHY_IS_CONTACT_SELECTOR_DIALOG (self), NULL);
596
597   priv = GET_PRIV (self);
598   return priv->filter_account;
599
600 }