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