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