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