]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-account-chooser.c
To know if an account is connected, use
[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  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  * 
20  * Authors: Martyn Russell <martyn@imendio.com>
21  */
22
23 #include "config.h"
24
25 #include <string.h>
26
27 #include <glib/gi18n.h>
28 #include <gtk/gtk.h>
29 #include <glade/glade.h>
30
31 #include <libtelepathy/tp-conn.h>
32 #include <libmissioncontrol/mc-account-monitor.h>
33 #include <libmissioncontrol/mission-control.h>
34
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) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv))
41
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 } EmpathyAccountChooserPriv;
50
51 typedef struct {
52         EmpathyAccountChooser *chooser;
53         McAccount             *account;
54         gboolean               set;
55 } SetAccountData;
56
57 enum {
58         COL_ACCOUNT_IMAGE,
59         COL_ACCOUNT_TEXT,
60         COL_ACCOUNT_ENABLED, /* Usually tied to connected state */
61         COL_ACCOUNT_POINTER,
62         COL_ACCOUNT_COUNT
63 };
64
65 static void     account_chooser_finalize               (GObject                         *object);
66 static void     account_chooser_get_property           (GObject                         *object,
67                                                         guint                            param_id,
68                                                         GValue                          *value,
69                                                         GParamSpec                      *pspec);
70 static void     account_chooser_set_property           (GObject                         *object,
71                                                         guint                            param_id,
72                                                         const GValue                    *value,
73                                                         GParamSpec                      *pspec);
74 static void     account_chooser_setup                  (EmpathyAccountChooser            *chooser);
75 static void     account_chooser_account_created_cb     (McAccountMonitor                *monitor,
76                                                         const gchar                     *unique_name,
77                                                         EmpathyAccountChooser            *chooser);
78 static void     account_chooser_account_add_foreach    (McAccount                       *account,
79                                                         EmpathyAccountChooser            *chooser);
80 static void     account_chooser_account_deleted_cb     (McAccountMonitor                *monitor,
81                                                         const gchar                     *unique_name,
82                                                         EmpathyAccountChooser            *chooser);
83 static void     account_chooser_account_remove_foreach (McAccount                       *account,
84                                                         EmpathyAccountChooser            *chooser);
85 static void     account_chooser_update_iter            (EmpathyAccountChooser            *chooser,
86                                                         GtkTreeIter                     *iter);
87 static void     account_chooser_status_changed_cb      (MissionControl                  *mc,
88                                                         TelepathyConnectionStatus        status,
89                                                         McPresence                       presence,
90                                                         TelepathyConnectionStatusReason  reason,
91                                                         const gchar                     *unique_name,
92                                                         EmpathyAccountChooser           *chooser);
93 static gboolean account_chooser_separator_func         (GtkTreeModel                    *model,
94                                                         GtkTreeIter                     *iter,
95                                                         EmpathyAccountChooser            *chooser);
96 static gboolean account_chooser_set_account_foreach    (GtkTreeModel                    *model,
97                                                         GtkTreePath                     *path,
98                                                         GtkTreeIter                     *iter,
99                                                         SetAccountData                  *data);
100
101 enum {
102         PROP_0,
103         PROP_HAS_ALL_OPTION,
104 };
105
106 G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser, GTK_TYPE_COMBO_BOX);
107
108 static void
109 empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
110 {
111         GObjectClass *object_class = G_OBJECT_CLASS (klass);
112
113         object_class->finalize = account_chooser_finalize;
114         object_class->get_property = account_chooser_get_property;
115         object_class->set_property = account_chooser_set_property;
116
117         g_object_class_install_property (object_class,
118                                          PROP_HAS_ALL_OPTION,
119                                          g_param_spec_boolean ("has-all-option",
120                                                                "Has All Option",
121                                                                "Have a separate option in the list to mean ALL accounts",
122                                                                FALSE,
123                                                                G_PARAM_READWRITE));
124
125         g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
126 }
127
128 static void
129 empathy_account_chooser_init (EmpathyAccountChooser *chooser)
130 {
131         EmpathyAccountChooserPriv *priv = GET_PRIV (chooser);
132
133         priv->set_active_item = FALSE;
134         priv->filter = NULL;
135         priv->filter_data = NULL;
136 }
137
138 static void
139 account_chooser_finalize (GObject *object)
140 {
141         EmpathyAccountChooser     *chooser;
142         EmpathyAccountChooserPriv *priv;
143
144         chooser = EMPATHY_ACCOUNT_CHOOSER (object);
145         priv = GET_PRIV (object);
146
147         g_signal_handlers_disconnect_by_func (priv->monitor,
148                                               account_chooser_account_created_cb,
149                                               chooser);
150         g_signal_handlers_disconnect_by_func (priv->monitor,
151                                               account_chooser_account_deleted_cb,
152                                               chooser);
153         dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
154                                         "AccountStatusChanged",
155                                         G_CALLBACK (account_chooser_status_changed_cb),
156                                         chooser);
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         dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc), "AccountStatusChanged",
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                                    TelepathyConnectionStatus        status,
558                                    McPresence                       presence,
559                                    TelepathyConnectionStatusReason  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 && !account) {
608                 equal = TRUE;
609         }
610         else if ((data->account && !account) || (!data->account && account)) {
611                 equal = FALSE;
612         } else {
613                 equal = empathy_account_equal (data->account, account);
614                 g_object_unref (account);
615         }
616
617         if (equal) {
618                 GtkComboBox *combobox;
619
620                 combobox = GTK_COMBO_BOX (data->chooser);
621                 gtk_combo_box_set_active_iter (combobox, iter);
622
623                 data->set = TRUE;
624         }
625
626         return equal;
627 }
628
629 static gboolean
630 account_chooser_filter_foreach (GtkTreeModel *model,
631                                 GtkTreePath  *path,
632                                 GtkTreeIter  *iter,
633                                 gpointer      chooser)
634 {
635         account_chooser_update_iter (chooser, iter);
636         return FALSE;
637 }
638
639 void
640 empathy_account_chooser_set_filter (EmpathyAccountChooser           *chooser,
641                                     EmpathyAccountChooserFilterFunc  filter,
642                                     gpointer                         user_data)
643 {
644         EmpathyAccountChooserPriv *priv;
645         GtkTreeModel *model;
646
647         g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
648
649         priv = GET_PRIV (chooser);
650
651         priv->filter = filter;
652         priv->filter_data = user_data;
653
654         /* Refilter existing data */
655         priv->set_active_item = FALSE;
656         model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
657         gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
658 }
659
660 gboolean
661 empathy_account_chooser_filter_is_connected (McAccount *account,
662                                              gpointer   user_data)
663 {
664         MissionControl            *mc;
665         TelepathyConnectionStatus  status;
666
667         g_return_val_if_fail (MC_IS_ACCOUNT (account), FALSE);
668
669         mc = empathy_mission_control_new ();
670         status = mission_control_get_connection_status (mc, account, NULL);
671         g_object_unref (mc);
672
673         return status == TP_CONN_STATUS_CONNECTED;
674 }
675