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