]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-account-chooser.c
empathy-tube-handler: wait that tube is ready before announcing it
[empathy.git] / libempathy-gtk / empathy-account-chooser.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2005-2007 Imendio AB
4  * Copyright (C) 2007-2008 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  * 
21  * Authors: Martyn Russell <martyn@imendio.com>
22  *          Xavier Claessens <xclaesse@gmail.com>
23  */
24
25 #include "config.h"
26
27 #include <string.h>
28
29 #include <glib/gi18n-lib.h>
30 #include <gtk/gtk.h>
31
32 #include <libmissioncontrol/mission-control.h>
33
34 #include <libempathy/empathy-account-manager.h>
35 #include <libempathy/empathy-utils.h>
36
37 #include "empathy-ui-utils.h"
38 #include "empathy-account-chooser.h"
39
40 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountChooser)
41 typedef struct {
42         EmpathyAccountManager          *manager;
43         gboolean                        set_active_item;
44         gboolean                        has_all_option;
45         EmpathyAccountChooserFilterFunc filter;
46         gpointer                        filter_data;
47 } EmpathyAccountChooserPriv;
48
49 typedef struct {
50         EmpathyAccountChooser *chooser;
51         McAccount             *account;
52         gboolean               set;
53 } SetAccountData;
54
55 enum {
56         COL_ACCOUNT_IMAGE,
57         COL_ACCOUNT_TEXT,
58         COL_ACCOUNT_ENABLED, /* Usually tied to connected state */
59         COL_ACCOUNT_POINTER,
60         COL_ACCOUNT_COUNT
61 };
62
63 static void     account_chooser_finalize               (GObject                  *object);
64 static void     account_chooser_get_property           (GObject                  *object,
65                                                         guint                     param_id,
66                                                         GValue                   *value,
67                                                         GParamSpec               *pspec);
68 static void     account_chooser_set_property           (GObject                  *object,
69                                                         guint                     param_id,
70                                                         const GValue             *value,
71                                                         GParamSpec               *pspec);
72 static void     account_chooser_setup                  (EmpathyAccountChooser    *chooser);
73 static void     account_chooser_account_created_cb     (EmpathyAccountManager    *manager,
74                                                         McAccount                *account,
75                                                         EmpathyAccountChooser    *chooser);
76 static void     account_chooser_account_add_foreach    (McAccount                *account,
77                                                         EmpathyAccountChooser    *chooser);
78 static void     account_chooser_account_deleted_cb     (EmpathyAccountManager    *manager,
79                                                         McAccount                *account,
80                                                         EmpathyAccountChooser    *chooser);
81 static void     account_chooser_account_remove_foreach (McAccount                *account,
82                                                         EmpathyAccountChooser    *chooser);
83 static void     account_chooser_update_iter            (EmpathyAccountChooser    *chooser,
84                                                         GtkTreeIter              *iter);
85 static void     account_chooser_connection_changed_cb  (EmpathyAccountManager    *manager,
86                                                         McAccount                *account,
87                                                         TpConnectionStatusReason  reason,
88                                                         TpConnectionStatus        new_status,
89                                                         TpConnectionStatus        old_status,
90                                                         EmpathyAccountChooser    *chooser);
91 static gboolean account_chooser_separator_func         (GtkTreeModel             *model,
92                                                         GtkTreeIter              *iter,
93                                                         EmpathyAccountChooser    *chooser);
94 static gboolean account_chooser_set_account_foreach    (GtkTreeModel             *model,
95                                                         GtkTreePath              *path,
96                                                         GtkTreeIter              *iter,
97                                                         SetAccountData           *data);
98
99 enum {
100         PROP_0,
101         PROP_HAS_ALL_OPTION,
102 };
103
104 G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser, GTK_TYPE_COMBO_BOX);
105
106 static void
107 empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
108 {
109         GObjectClass *object_class = G_OBJECT_CLASS (klass);
110
111         object_class->finalize = account_chooser_finalize;
112         object_class->get_property = account_chooser_get_property;
113         object_class->set_property = account_chooser_set_property;
114
115         g_object_class_install_property (object_class,
116                                          PROP_HAS_ALL_OPTION,
117                                          g_param_spec_boolean ("has-all-option",
118                                                                "Has All Option",
119                                                                "Have a separate option in the list to mean ALL accounts",
120                                                                FALSE,
121                                                                G_PARAM_READWRITE));
122
123         g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
124 }
125
126 static void
127 empathy_account_chooser_init (EmpathyAccountChooser *chooser)
128 {
129         EmpathyAccountChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
130                 EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv);
131
132         chooser->priv = priv;
133         priv->set_active_item = FALSE;
134         priv->filter = NULL;
135         priv->filter_data = NULL;
136
137         priv->manager = empathy_account_manager_dup_singleton ();
138
139         g_signal_connect (priv->manager, "account-created",
140                           G_CALLBACK (account_chooser_account_created_cb),
141                           chooser);
142         g_signal_connect (priv->manager, "account-deleted",
143                           G_CALLBACK (account_chooser_account_deleted_cb),
144                           chooser);
145         g_signal_connect (priv->manager, "account-connection-changed",
146                           G_CALLBACK (account_chooser_connection_changed_cb),
147                           chooser);
148
149         account_chooser_setup (EMPATHY_ACCOUNT_CHOOSER (chooser));
150 }
151
152 static void
153 account_chooser_finalize (GObject *object)
154 {
155         EmpathyAccountChooserPriv *priv = GET_PRIV (object);
156
157         g_signal_handlers_disconnect_by_func (priv->manager,
158                                               account_chooser_connection_changed_cb,
159                                               object);
160         g_signal_handlers_disconnect_by_func (priv->manager,
161                                               account_chooser_account_created_cb,
162                                               object);
163         g_signal_handlers_disconnect_by_func (priv->manager,
164                                               account_chooser_account_deleted_cb,
165                                               object);
166         g_object_unref (priv->manager);
167
168         G_OBJECT_CLASS (empathy_account_chooser_parent_class)->finalize (object);
169 }
170
171 static void
172 account_chooser_get_property (GObject    *object,
173                               guint       param_id,
174                               GValue     *value,
175                               GParamSpec *pspec)
176 {
177         EmpathyAccountChooserPriv *priv;
178
179         priv = GET_PRIV (object);
180
181         switch (param_id) {
182         case PROP_HAS_ALL_OPTION:
183                 g_value_set_boolean (value, priv->has_all_option);
184                 break;
185         default:
186                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
187                 break;
188         };
189 }
190
191 static void
192 account_chooser_set_property (GObject      *object,
193                               guint         param_id,
194                               const GValue *value,
195                               GParamSpec   *pspec)
196 {
197         EmpathyAccountChooserPriv *priv;
198
199         priv = GET_PRIV (object);
200
201         switch (param_id) {
202         case PROP_HAS_ALL_OPTION:
203                 empathy_account_chooser_set_has_all_option (EMPATHY_ACCOUNT_CHOOSER (object),
204                                                            g_value_get_boolean (value));
205                 break;
206         default:
207                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
208                 break;
209         };
210 }
211
212 GtkWidget *
213 empathy_account_chooser_new (void)
214 {
215         GtkWidget                *chooser;
216
217         chooser = g_object_new (EMPATHY_TYPE_ACCOUNT_CHOOSER, NULL);
218
219         return chooser;
220 }
221
222 McAccount *
223 empathy_account_chooser_get_account (EmpathyAccountChooser *chooser)
224 {
225         EmpathyAccountChooserPriv *priv;
226         McAccount                *account;
227         GtkTreeModel             *model;
228         GtkTreeIter               iter;
229
230         g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
231
232         priv = GET_PRIV (chooser);
233
234         model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
235         if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (chooser), &iter)) {
236                 return NULL;
237         }
238
239         gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
240
241         return account;
242 }
243
244 gboolean
245 empathy_account_chooser_set_account (EmpathyAccountChooser *chooser,
246                                      McAccount             *account)
247 {
248         GtkComboBox    *combobox;
249         GtkTreeModel   *model;
250         GtkTreeIter     iter;
251         SetAccountData  data;
252
253         g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
254
255         combobox = GTK_COMBO_BOX (chooser);
256         model = gtk_combo_box_get_model (combobox);
257         gtk_combo_box_get_active_iter (combobox, &iter);
258
259         data.chooser = chooser;
260         data.account = account;
261
262         gtk_tree_model_foreach (model,
263                                 (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
264                                 &data);
265
266         return data.set;
267 }
268
269 gboolean
270 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *chooser)
271 {
272         EmpathyAccountChooserPriv *priv;
273
274         g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
275
276         priv = GET_PRIV (chooser);
277         
278         return priv->has_all_option;
279 }
280
281 void
282 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *chooser,
283                                            gboolean              has_all_option)
284 {
285         EmpathyAccountChooserPriv *priv;
286         GtkComboBox              *combobox;
287         GtkListStore             *store;
288         GtkTreeModel             *model;
289         GtkTreeIter               iter;
290
291         g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
292
293         priv = GET_PRIV (chooser);
294
295         if (priv->has_all_option == has_all_option) {
296                 return;
297         }
298
299         combobox = GTK_COMBO_BOX (chooser);
300         model = gtk_combo_box_get_model (combobox);
301         store = GTK_LIST_STORE (model);
302
303         priv->has_all_option = has_all_option;
304
305         /*
306          * The first 2 options are the ALL and separator
307          */
308
309         if (has_all_option) {
310                 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser), 
311                                                       (GtkTreeViewRowSeparatorFunc)
312                                                       account_chooser_separator_func,
313                                                       chooser, 
314                                                       NULL);
315
316                 gtk_list_store_prepend (store, &iter);
317                 gtk_list_store_set (store, &iter, 
318                                     COL_ACCOUNT_TEXT, NULL,
319                                     COL_ACCOUNT_ENABLED, TRUE,
320                                     COL_ACCOUNT_POINTER, NULL,
321                                     -1);
322
323                 gtk_list_store_prepend (store, &iter);
324                 gtk_list_store_set (store, &iter, 
325                                     COL_ACCOUNT_TEXT, _("All"), 
326                                     COL_ACCOUNT_ENABLED, TRUE,
327                                     COL_ACCOUNT_POINTER, NULL,
328                                     -1);
329         } else {
330                 if (gtk_tree_model_get_iter_first (model, &iter)) {
331                         if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter)) {
332                                 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
333                         }
334                 }
335
336                 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser), 
337                                                       (GtkTreeViewRowSeparatorFunc)
338                                                       NULL,
339                                                       NULL, 
340                                                       NULL);
341         }
342
343         g_object_notify (G_OBJECT (chooser), "has-all-option");
344 }
345
346 static void
347 account_chooser_setup (EmpathyAccountChooser *chooser)
348 {
349         EmpathyAccountChooserPriv *priv;
350         GList                    *accounts;
351         GtkListStore             *store;
352         GtkCellRenderer          *renderer;
353         GtkComboBox              *combobox;
354
355         priv = GET_PRIV (chooser);
356
357         /* Set up combo box with new store */
358         combobox = GTK_COMBO_BOX (chooser);
359
360         gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
361
362         store = gtk_list_store_new (COL_ACCOUNT_COUNT,
363                                     G_TYPE_STRING,    /* Image */
364                                     G_TYPE_STRING,    /* Name */
365                                     G_TYPE_BOOLEAN,   /* Enabled */
366                                     MC_TYPE_ACCOUNT);
367
368         gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
369
370         renderer = gtk_cell_renderer_pixbuf_new ();
371         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
372         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
373                                         "icon-name", COL_ACCOUNT_IMAGE,
374                                         "sensitive", COL_ACCOUNT_ENABLED,
375                                         NULL);
376         g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
377
378         renderer = gtk_cell_renderer_text_new ();
379         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
380         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
381                                         "text", COL_ACCOUNT_TEXT,
382                                         "sensitive", COL_ACCOUNT_ENABLED,
383                                         NULL);
384
385         /* Populate accounts */
386         accounts = mc_accounts_list ();
387         g_list_foreach (accounts,
388                         (GFunc) account_chooser_account_add_foreach,
389                         chooser);
390
391         mc_accounts_list_free (accounts);
392         g_object_unref (store);
393 }
394
395 static void
396 account_chooser_account_created_cb (EmpathyAccountManager *manager,
397                                     McAccount             *account,
398                                     EmpathyAccountChooser *chooser)
399 {
400         account_chooser_account_add_foreach (account, chooser);
401 }
402
403 static void
404 account_chooser_account_add_foreach (McAccount             *account,
405                                      EmpathyAccountChooser *chooser)
406 {
407         GtkListStore *store;
408         GtkComboBox  *combobox;
409         GtkTreeIter   iter;
410         gint          position;
411
412         combobox = GTK_COMBO_BOX (chooser);
413         store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
414
415         position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
416         gtk_list_store_insert_with_values (store, &iter, position,
417                                            COL_ACCOUNT_POINTER, account,
418                                            -1);
419         account_chooser_update_iter (chooser, &iter);
420 }
421
422 static void
423 account_chooser_account_deleted_cb (EmpathyAccountManager *manager,
424                                     McAccount             *account,
425                                     EmpathyAccountChooser *chooser)
426 {
427         account_chooser_account_remove_foreach (account, chooser);
428 }
429
430 typedef struct {
431         McAccount   *account;
432         GtkTreeIter *iter;
433         gboolean     found;
434 } FindAccountData;
435
436 static gboolean
437 account_chooser_find_account_foreach (GtkTreeModel *model,
438                                       GtkTreePath  *path,
439                                       GtkTreeIter  *iter,
440                                       gpointer      user_data)
441 {
442         FindAccountData *data = user_data;
443         McAccount       *account;
444
445         gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
446
447         if (empathy_account_equal (account, data->account)) {
448                 data->found = TRUE;
449                 *(data->iter) = *iter;
450                 g_object_unref (account);
451
452                 return TRUE;
453         }
454
455         g_object_unref (account);
456
457         return FALSE;
458 }
459
460 static gboolean
461 account_chooser_find_account (EmpathyAccountChooser *chooser,
462                               McAccount             *account,
463                               GtkTreeIter           *iter)
464 {
465         GtkListStore    *store;
466         GtkComboBox     *combobox;
467         FindAccountData  data;
468
469         combobox = GTK_COMBO_BOX (chooser);
470         store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
471
472         data.account = account;
473         data.iter = iter;
474         gtk_tree_model_foreach (GTK_TREE_MODEL (store),
475                                 account_chooser_find_account_foreach,
476                                 &data);
477
478         return data.found;
479 }
480
481 static void
482 account_chooser_account_remove_foreach (McAccount            *account,
483                                         EmpathyAccountChooser *chooser)
484 {
485         GtkListStore *store;
486         GtkComboBox  *combobox;
487         GtkTreeIter   iter;
488
489         combobox = GTK_COMBO_BOX (chooser);
490         store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
491
492         if (account_chooser_find_account (chooser, account, &iter)) {
493                 gtk_list_store_remove (store, &iter);
494         }
495 }
496
497 static void
498 account_chooser_update_iter (EmpathyAccountChooser *chooser,
499                              GtkTreeIter           *iter)
500 {
501         EmpathyAccountChooserPriv *priv;
502         GtkListStore              *store;
503         GtkComboBox               *combobox;
504         McAccount                 *account;
505         const gchar               *icon_name;
506         gboolean                   is_enabled = TRUE;
507
508         priv = GET_PRIV (chooser);
509
510         combobox = GTK_COMBO_BOX (chooser);
511         store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
512
513         gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
514                             COL_ACCOUNT_POINTER, &account,
515                             -1);
516
517         icon_name = empathy_icon_name_from_account (account);
518         if (priv->filter) {
519                 is_enabled = priv->filter (account, priv->filter_data);
520         }
521
522         gtk_list_store_set (store, iter,
523                             COL_ACCOUNT_IMAGE, icon_name,
524                             COL_ACCOUNT_TEXT, mc_account_get_display_name (account),
525                             COL_ACCOUNT_ENABLED, is_enabled,
526                             -1);
527
528         /* set first connected account as active account */
529         if (priv->set_active_item == FALSE && is_enabled) {
530                 priv->set_active_item = TRUE;
531                 gtk_combo_box_set_active_iter (combobox, iter);
532         }
533
534         g_object_unref (account);
535 }
536
537 static void
538 account_chooser_connection_changed_cb (EmpathyAccountManager   *manager,
539                                        McAccount               *account,
540                                        TpConnectionStatusReason reason,
541                                        TpConnectionStatus       new_status,
542                                        TpConnectionStatus       old_status,
543                                        EmpathyAccountChooser   *chooser)
544 {
545         GtkTreeIter iter;
546
547         if (account_chooser_find_account (chooser, account, &iter)) {
548                 account_chooser_update_iter (chooser, &iter);
549         }
550 }
551
552 static gboolean
553 account_chooser_separator_func (GtkTreeModel         *model,
554                                 GtkTreeIter          *iter,
555                                 EmpathyAccountChooser *chooser)
556 {
557         EmpathyAccountChooserPriv *priv;
558         gchar                    *text;
559         gboolean                  is_separator;
560
561         priv = GET_PRIV (chooser);
562         
563         if (!priv->has_all_option) {
564                 return FALSE;
565         }
566         
567         gtk_tree_model_get (model, iter, COL_ACCOUNT_TEXT, &text, -1);
568         is_separator = text == NULL;
569         g_free (text);
570
571         return is_separator;
572 }
573
574 static gboolean
575 account_chooser_set_account_foreach (GtkTreeModel   *model,
576                                      GtkTreePath    *path,
577                                      GtkTreeIter    *iter,
578                                      SetAccountData *data)
579 {
580         McAccount *account;
581         gboolean   equal;
582
583         gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
584
585         /* Special case so we can make it possible to select the All option */
586         if ((data->account == NULL) != (account == NULL)) {
587                 equal = FALSE;
588         }
589         else if (data->account == account) {
590                 equal = TRUE;
591         } else {
592                 equal = empathy_account_equal (data->account, account);
593         }
594
595         if (account) {
596                 g_object_unref (account);
597         }
598
599         if (equal) {
600                 GtkComboBox *combobox;
601
602                 combobox = GTK_COMBO_BOX (data->chooser);
603                 gtk_combo_box_set_active_iter (combobox, iter);
604
605                 data->set = TRUE;
606         }
607
608         return equal;
609 }
610
611 static gboolean
612 account_chooser_filter_foreach (GtkTreeModel *model,
613                                 GtkTreePath  *path,
614                                 GtkTreeIter  *iter,
615                                 gpointer      chooser)
616 {
617         account_chooser_update_iter (chooser, iter);
618         return FALSE;
619 }
620
621 void
622 empathy_account_chooser_set_filter (EmpathyAccountChooser           *chooser,
623                                     EmpathyAccountChooserFilterFunc  filter,
624                                     gpointer                         user_data)
625 {
626         EmpathyAccountChooserPriv *priv;
627         GtkTreeModel *model;
628
629         g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
630
631         priv = GET_PRIV (chooser);
632
633         priv->filter = filter;
634         priv->filter_data = user_data;
635
636         /* Refilter existing data */
637         priv->set_active_item = FALSE;
638         model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
639         gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
640 }
641
642 gboolean
643 empathy_account_chooser_filter_is_connected (McAccount *account,
644                                              gpointer   user_data)
645 {
646         MissionControl     *mc;
647         TpConnectionStatus  status;
648
649         g_return_val_if_fail (MC_IS_ACCOUNT (account), FALSE);
650
651         mc = empathy_mission_control_dup_singleton ();
652         status = mission_control_get_connection_status (mc, account, NULL);
653         g_object_unref (mc);
654
655         return status == TP_CONNECTION_STATUS_CONNECTED;
656 }
657