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