Tagged for release 2.26.0.
[empathy-mirror.git] / gnome-2-26 / libempathy-gtk / empathy-account-chooser.c
blob9bda7c6db221e34f34ef85267fbbe2456f468b66
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.
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.
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.
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.
21 * Authors: Martyn Russell <martyn@imendio.com>
22 * Xavier Claessens <xclaesse@gmail.com>
25 #include "config.h"
27 #include <string.h>
29 #include <glib/gi18n-lib.h>
30 #include <gtk/gtk.h>
31 #include <glade/glade.h>
33 #include <libmissioncontrol/mission-control.h>
35 #include <libempathy/empathy-account-manager.h>
36 #include <libempathy/empathy-utils.h>
38 #include "empathy-ui-utils.h"
39 #include "empathy-account-chooser.h"
41 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountChooser)
42 typedef struct {
43 EmpathyAccountManager *manager;
44 gboolean set_active_item;
45 gboolean has_all_option;
46 EmpathyAccountChooserFilterFunc filter;
47 gpointer filter_data;
48 } EmpathyAccountChooserPriv;
50 typedef struct {
51 EmpathyAccountChooser *chooser;
52 McAccount *account;
53 gboolean set;
54 } SetAccountData;
56 enum {
57 COL_ACCOUNT_IMAGE,
58 COL_ACCOUNT_TEXT,
59 COL_ACCOUNT_ENABLED, /* Usually tied to connected state */
60 COL_ACCOUNT_POINTER,
61 COL_ACCOUNT_COUNT
64 static void account_chooser_finalize (GObject *object);
65 static void account_chooser_get_property (GObject *object,
66 guint param_id,
67 GValue *value,
68 GParamSpec *pspec);
69 static void account_chooser_set_property (GObject *object,
70 guint param_id,
71 const GValue *value,
72 GParamSpec *pspec);
73 static void account_chooser_setup (EmpathyAccountChooser *chooser);
74 static void account_chooser_account_created_cb (EmpathyAccountManager *manager,
75 McAccount *account,
76 EmpathyAccountChooser *chooser);
77 static void account_chooser_account_add_foreach (McAccount *account,
78 EmpathyAccountChooser *chooser);
79 static void account_chooser_account_deleted_cb (EmpathyAccountManager *manager,
80 McAccount *account,
81 EmpathyAccountChooser *chooser);
82 static void account_chooser_account_remove_foreach (McAccount *account,
83 EmpathyAccountChooser *chooser);
84 static void account_chooser_update_iter (EmpathyAccountChooser *chooser,
85 GtkTreeIter *iter);
86 static void account_chooser_connection_changed_cb (EmpathyAccountManager *manager,
87 McAccount *account,
88 TpConnectionStatusReason reason,
89 TpConnectionStatus new_status,
90 TpConnectionStatus old_status,
91 EmpathyAccountChooser *chooser);
92 static gboolean account_chooser_separator_func (GtkTreeModel *model,
93 GtkTreeIter *iter,
94 EmpathyAccountChooser *chooser);
95 static gboolean account_chooser_set_account_foreach (GtkTreeModel *model,
96 GtkTreePath *path,
97 GtkTreeIter *iter,
98 SetAccountData *data);
100 enum {
101 PROP_0,
102 PROP_HAS_ALL_OPTION,
105 G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser, GTK_TYPE_COMBO_BOX);
107 static void
108 empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
110 GObjectClass *object_class = G_OBJECT_CLASS (klass);
112 object_class->finalize = account_chooser_finalize;
113 object_class->get_property = account_chooser_get_property;
114 object_class->set_property = account_chooser_set_property;
116 g_object_class_install_property (object_class,
117 PROP_HAS_ALL_OPTION,
118 g_param_spec_boolean ("has-all-option",
119 "Has All Option",
120 "Have a separate option in the list to mean ALL accounts",
121 FALSE,
122 G_PARAM_READWRITE));
124 g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
127 static void
128 empathy_account_chooser_init (EmpathyAccountChooser *chooser)
130 EmpathyAccountChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
131 EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv);
133 chooser->priv = priv;
134 priv->set_active_item = FALSE;
135 priv->filter = NULL;
136 priv->filter_data = NULL;
138 priv->manager = empathy_account_manager_dup_singleton ();
140 g_signal_connect (priv->manager, "account-created",
141 G_CALLBACK (account_chooser_account_created_cb),
142 chooser);
143 g_signal_connect (priv->manager, "account-deleted",
144 G_CALLBACK (account_chooser_account_deleted_cb),
145 chooser);
146 g_signal_connect (priv->manager, "account-connection-changed",
147 G_CALLBACK (account_chooser_connection_changed_cb),
148 chooser);
150 account_chooser_setup (EMPATHY_ACCOUNT_CHOOSER (chooser));
153 static void
154 account_chooser_finalize (GObject *object)
156 EmpathyAccountChooserPriv *priv = GET_PRIV (object);
158 g_signal_handlers_disconnect_by_func (priv->manager,
159 account_chooser_connection_changed_cb,
160 object);
161 g_signal_handlers_disconnect_by_func (priv->manager,
162 account_chooser_account_created_cb,
163 object);
164 g_signal_handlers_disconnect_by_func (priv->manager,
165 account_chooser_account_deleted_cb,
166 object);
167 g_object_unref (priv->manager);
169 G_OBJECT_CLASS (empathy_account_chooser_parent_class)->finalize (object);
172 static void
173 account_chooser_get_property (GObject *object,
174 guint param_id,
175 GValue *value,
176 GParamSpec *pspec)
178 EmpathyAccountChooserPriv *priv;
180 priv = GET_PRIV (object);
182 switch (param_id) {
183 case PROP_HAS_ALL_OPTION:
184 g_value_set_boolean (value, priv->has_all_option);
185 break;
186 default:
187 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
188 break;
192 static void
193 account_chooser_set_property (GObject *object,
194 guint param_id,
195 const GValue *value,
196 GParamSpec *pspec)
198 EmpathyAccountChooserPriv *priv;
200 priv = GET_PRIV (object);
202 switch (param_id) {
203 case PROP_HAS_ALL_OPTION:
204 empathy_account_chooser_set_has_all_option (EMPATHY_ACCOUNT_CHOOSER (object),
205 g_value_get_boolean (value));
206 break;
207 default:
208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
209 break;
213 GtkWidget *
214 empathy_account_chooser_new (void)
216 GtkWidget *chooser;
218 chooser = g_object_new (EMPATHY_TYPE_ACCOUNT_CHOOSER, NULL);
220 return chooser;
223 McAccount *
224 empathy_account_chooser_get_account (EmpathyAccountChooser *chooser)
226 EmpathyAccountChooserPriv *priv;
227 McAccount *account;
228 GtkTreeModel *model;
229 GtkTreeIter iter;
231 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
233 priv = GET_PRIV (chooser);
235 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
236 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (chooser), &iter)) {
237 return NULL;
240 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
242 return account;
245 gboolean
246 empathy_account_chooser_set_account (EmpathyAccountChooser *chooser,
247 McAccount *account)
249 GtkComboBox *combobox;
250 GtkTreeModel *model;
251 GtkTreeIter iter;
252 SetAccountData data;
254 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
256 combobox = GTK_COMBO_BOX (chooser);
257 model = gtk_combo_box_get_model (combobox);
258 gtk_combo_box_get_active_iter (combobox, &iter);
260 data.chooser = chooser;
261 data.account = account;
263 gtk_tree_model_foreach (model,
264 (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
265 &data);
267 return data.set;
270 gboolean
271 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *chooser)
273 EmpathyAccountChooserPriv *priv;
275 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
277 priv = GET_PRIV (chooser);
279 return priv->has_all_option;
282 void
283 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *chooser,
284 gboolean has_all_option)
286 EmpathyAccountChooserPriv *priv;
287 GtkComboBox *combobox;
288 GtkListStore *store;
289 GtkTreeModel *model;
290 GtkTreeIter iter;
292 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
294 priv = GET_PRIV (chooser);
296 if (priv->has_all_option == has_all_option) {
297 return;
300 combobox = GTK_COMBO_BOX (chooser);
301 model = gtk_combo_box_get_model (combobox);
302 store = GTK_LIST_STORE (model);
304 priv->has_all_option = has_all_option;
307 * The first 2 options are the ALL and separator
310 if (has_all_option) {
311 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
312 (GtkTreeViewRowSeparatorFunc)
313 account_chooser_separator_func,
314 chooser,
315 NULL);
317 gtk_list_store_prepend (store, &iter);
318 gtk_list_store_set (store, &iter,
319 COL_ACCOUNT_TEXT, NULL,
320 COL_ACCOUNT_ENABLED, TRUE,
321 COL_ACCOUNT_POINTER, NULL,
322 -1);
324 gtk_list_store_prepend (store, &iter);
325 gtk_list_store_set (store, &iter,
326 COL_ACCOUNT_TEXT, _("All"),
327 COL_ACCOUNT_ENABLED, TRUE,
328 COL_ACCOUNT_POINTER, NULL,
329 -1);
330 } else {
331 if (gtk_tree_model_get_iter_first (model, &iter)) {
332 if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter)) {
333 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
337 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
338 (GtkTreeViewRowSeparatorFunc)
339 NULL,
340 NULL,
341 NULL);
344 g_object_notify (G_OBJECT (chooser), "has-all-option");
347 static void
348 account_chooser_setup (EmpathyAccountChooser *chooser)
350 EmpathyAccountChooserPriv *priv;
351 GList *accounts;
352 GtkListStore *store;
353 GtkCellRenderer *renderer;
354 GtkComboBox *combobox;
356 priv = GET_PRIV (chooser);
358 /* Set up combo box with new store */
359 combobox = GTK_COMBO_BOX (chooser);
361 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
363 store = gtk_list_store_new (COL_ACCOUNT_COUNT,
364 G_TYPE_STRING, /* Image */
365 G_TYPE_STRING, /* Name */
366 G_TYPE_BOOLEAN, /* Enabled */
367 MC_TYPE_ACCOUNT);
369 gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
371 renderer = gtk_cell_renderer_pixbuf_new ();
372 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
373 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
374 "icon-name", COL_ACCOUNT_IMAGE,
375 "sensitive", COL_ACCOUNT_ENABLED,
376 NULL);
377 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
379 renderer = gtk_cell_renderer_text_new ();
380 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
381 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
382 "text", COL_ACCOUNT_TEXT,
383 "sensitive", COL_ACCOUNT_ENABLED,
384 NULL);
386 /* Populate accounts */
387 accounts = mc_accounts_list ();
388 g_list_foreach (accounts,
389 (GFunc) account_chooser_account_add_foreach,
390 chooser);
392 mc_accounts_list_free (accounts);
393 g_object_unref (store);
396 static void
397 account_chooser_account_created_cb (EmpathyAccountManager *manager,
398 McAccount *account,
399 EmpathyAccountChooser *chooser)
401 account_chooser_account_add_foreach (account, chooser);
404 static void
405 account_chooser_account_add_foreach (McAccount *account,
406 EmpathyAccountChooser *chooser)
408 GtkListStore *store;
409 GtkComboBox *combobox;
410 GtkTreeIter iter;
411 gint position;
413 combobox = GTK_COMBO_BOX (chooser);
414 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
416 position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
417 gtk_list_store_insert_with_values (store, &iter, position,
418 COL_ACCOUNT_POINTER, account,
419 -1);
420 account_chooser_update_iter (chooser, &iter);
423 static void
424 account_chooser_account_deleted_cb (EmpathyAccountManager *manager,
425 McAccount *account,
426 EmpathyAccountChooser *chooser)
428 account_chooser_account_remove_foreach (account, chooser);
431 typedef struct {
432 McAccount *account;
433 GtkTreeIter *iter;
434 gboolean found;
435 } FindAccountData;
437 static gboolean
438 account_chooser_find_account_foreach (GtkTreeModel *model,
439 GtkTreePath *path,
440 GtkTreeIter *iter,
441 gpointer user_data)
443 FindAccountData *data = user_data;
444 McAccount *account;
446 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
448 if (empathy_account_equal (account, data->account)) {
449 data->found = TRUE;
450 *(data->iter) = *iter;
451 g_object_unref (account);
453 return TRUE;
456 g_object_unref (account);
458 return FALSE;
461 static gboolean
462 account_chooser_find_account (EmpathyAccountChooser *chooser,
463 McAccount *account,
464 GtkTreeIter *iter)
466 GtkListStore *store;
467 GtkComboBox *combobox;
468 FindAccountData data;
470 combobox = GTK_COMBO_BOX (chooser);
471 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
473 data.account = account;
474 data.iter = iter;
475 gtk_tree_model_foreach (GTK_TREE_MODEL (store),
476 account_chooser_find_account_foreach,
477 &data);
479 return data.found;
482 static void
483 account_chooser_account_remove_foreach (McAccount *account,
484 EmpathyAccountChooser *chooser)
486 GtkListStore *store;
487 GtkComboBox *combobox;
488 GtkTreeIter iter;
490 combobox = GTK_COMBO_BOX (chooser);
491 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
493 if (account_chooser_find_account (chooser, account, &iter)) {
494 gtk_list_store_remove (store, &iter);
498 static void
499 account_chooser_update_iter (EmpathyAccountChooser *chooser,
500 GtkTreeIter *iter)
502 EmpathyAccountChooserPriv *priv;
503 GtkListStore *store;
504 GtkComboBox *combobox;
505 McAccount *account;
506 const gchar *icon_name;
507 gboolean is_enabled = TRUE;
509 priv = GET_PRIV (chooser);
511 combobox = GTK_COMBO_BOX (chooser);
512 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
514 gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
515 COL_ACCOUNT_POINTER, &account,
516 -1);
518 icon_name = empathy_icon_name_from_account (account);
519 if (priv->filter) {
520 is_enabled = priv->filter (account, priv->filter_data);
523 gtk_list_store_set (store, iter,
524 COL_ACCOUNT_IMAGE, icon_name,
525 COL_ACCOUNT_TEXT, mc_account_get_display_name (account),
526 COL_ACCOUNT_ENABLED, is_enabled,
527 -1);
529 /* set first connected account as active account */
530 if (priv->set_active_item == FALSE && is_enabled) {
531 priv->set_active_item = TRUE;
532 gtk_combo_box_set_active_iter (combobox, iter);
535 g_object_unref (account);
538 static void
539 account_chooser_connection_changed_cb (EmpathyAccountManager *manager,
540 McAccount *account,
541 TpConnectionStatusReason reason,
542 TpConnectionStatus new_status,
543 TpConnectionStatus old_status,
544 EmpathyAccountChooser *chooser)
546 GtkTreeIter iter;
548 if (account_chooser_find_account (chooser, account, &iter)) {
549 account_chooser_update_iter (chooser, &iter);
553 static gboolean
554 account_chooser_separator_func (GtkTreeModel *model,
555 GtkTreeIter *iter,
556 EmpathyAccountChooser *chooser)
558 EmpathyAccountChooserPriv *priv;
559 gchar *text;
560 gboolean is_separator;
562 priv = GET_PRIV (chooser);
564 if (!priv->has_all_option) {
565 return FALSE;
568 gtk_tree_model_get (model, iter, COL_ACCOUNT_TEXT, &text, -1);
569 is_separator = text == NULL;
570 g_free (text);
572 return is_separator;
575 static gboolean
576 account_chooser_set_account_foreach (GtkTreeModel *model,
577 GtkTreePath *path,
578 GtkTreeIter *iter,
579 SetAccountData *data)
581 McAccount *account;
582 gboolean equal;
584 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
586 /* Special case so we can make it possible to select the All option */
587 if ((data->account == NULL) != (account == NULL)) {
588 equal = FALSE;
590 else if (data->account == account) {
591 equal = TRUE;
592 } else {
593 equal = empathy_account_equal (data->account, account);
596 if (account) {
597 g_object_unref (account);
600 if (equal) {
601 GtkComboBox *combobox;
603 combobox = GTK_COMBO_BOX (data->chooser);
604 gtk_combo_box_set_active_iter (combobox, iter);
606 data->set = TRUE;
609 return equal;
612 static gboolean
613 account_chooser_filter_foreach (GtkTreeModel *model,
614 GtkTreePath *path,
615 GtkTreeIter *iter,
616 gpointer chooser)
618 account_chooser_update_iter (chooser, iter);
619 return FALSE;
622 void
623 empathy_account_chooser_set_filter (EmpathyAccountChooser *chooser,
624 EmpathyAccountChooserFilterFunc filter,
625 gpointer user_data)
627 EmpathyAccountChooserPriv *priv;
628 GtkTreeModel *model;
630 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
632 priv = GET_PRIV (chooser);
634 priv->filter = filter;
635 priv->filter_data = user_data;
637 /* Refilter existing data */
638 priv->set_active_item = FALSE;
639 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
640 gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
643 gboolean
644 empathy_account_chooser_filter_is_connected (McAccount *account,
645 gpointer user_data)
647 MissionControl *mc;
648 TpConnectionStatus status;
650 g_return_val_if_fail (MC_IS_ACCOUNT (account), FALSE);
652 mc = empathy_mission_control_dup_singleton ();
653 status = mission_control_get_connection_status (mc, account, NULL);
654 g_object_unref (mc);
656 return status == TP_CONNECTION_STATUS_CONNECTED;