]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-account-chooser.c
Updated Basque translation.
[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         if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (chooser), &iter)) {
248                 return NULL;
249         }
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 == NULL) != (account == NULL)) {
610                 equal = FALSE;
611         }
612         else if (data->account == account) {
613                 equal = TRUE;
614         } else {
615                 equal = empathy_account_equal (data->account, account);
616         }
617
618         if (account) {
619                 g_object_unref (account);
620         }
621
622         if (equal) {
623                 GtkComboBox *combobox;
624
625                 combobox = GTK_COMBO_BOX (data->chooser);
626                 gtk_combo_box_set_active_iter (combobox, iter);
627
628                 data->set = TRUE;
629         }
630
631         return equal;
632 }
633
634 static gboolean
635 account_chooser_filter_foreach (GtkTreeModel *model,
636                                 GtkTreePath  *path,
637                                 GtkTreeIter  *iter,
638                                 gpointer      chooser)
639 {
640         account_chooser_update_iter (chooser, iter);
641         return FALSE;
642 }
643
644 void
645 empathy_account_chooser_set_filter (EmpathyAccountChooser           *chooser,
646                                     EmpathyAccountChooserFilterFunc  filter,
647                                     gpointer                         user_data)
648 {
649         EmpathyAccountChooserPriv *priv;
650         GtkTreeModel *model;
651
652         g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
653
654         priv = GET_PRIV (chooser);
655
656         priv->filter = filter;
657         priv->filter_data = user_data;
658
659         /* Refilter existing data */
660         priv->set_active_item = FALSE;
661         model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
662         gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
663 }
664
665 gboolean
666 empathy_account_chooser_filter_is_connected (McAccount *account,
667                                              gpointer   user_data)
668 {
669         MissionControl     *mc;
670         TpConnectionStatus  status;
671
672         g_return_val_if_fail (MC_IS_ACCOUNT (account), FALSE);
673
674         mc = empathy_mission_control_new ();
675         status = mission_control_get_connection_status (mc, account, NULL);
676         g_object_unref (mc);
677
678         return status == TP_CONNECTION_STATUS_CONNECTED;
679 }
680