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