1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
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>
29 #include <glib/gi18n-lib.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)
43 EmpathyAccountManager *manager;
44 gboolean set_active_item;
45 gboolean has_all_option;
46 EmpathyAccountChooserFilterFunc filter;
48 } EmpathyAccountChooserPriv;
51 EmpathyAccountChooser *chooser;
59 COL_ACCOUNT_ENABLED, /* Usually tied to connected state */
64 static void account_chooser_finalize (GObject *object);
65 static void account_chooser_get_property (GObject *object,
69 static void account_chooser_set_property (GObject *object,
73 static void account_chooser_setup (EmpathyAccountChooser *chooser);
74 static void account_chooser_account_created_cb (EmpathyAccountManager *manager,
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,
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,
86 static void account_chooser_connection_changed_cb (EmpathyAccountManager *manager,
88 TpConnectionStatusReason reason,
89 TpConnectionStatus new_status,
90 TpConnectionStatus old_status,
91 EmpathyAccountChooser *chooser);
92 static gboolean account_chooser_separator_func (GtkTreeModel *model,
94 EmpathyAccountChooser *chooser);
95 static gboolean account_chooser_set_account_foreach (GtkTreeModel *model,
98 SetAccountData *data);
105 G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser, GTK_TYPE_COMBO_BOX);
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,
118 g_param_spec_boolean ("has-all-option",
120 "Have a separate option in the list to mean ALL accounts",
124 g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
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;
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),
143 g_signal_connect (priv->manager, "account-deleted",
144 G_CALLBACK (account_chooser_account_deleted_cb),
146 g_signal_connect (priv->manager, "account-connection-changed",
147 G_CALLBACK (account_chooser_connection_changed_cb),
150 account_chooser_setup (EMPATHY_ACCOUNT_CHOOSER (chooser));
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,
161 g_signal_handlers_disconnect_by_func (priv->manager,
162 account_chooser_account_created_cb,
164 g_signal_handlers_disconnect_by_func (priv->manager,
165 account_chooser_account_deleted_cb,
167 g_object_unref (priv->manager);
169 G_OBJECT_CLASS (empathy_account_chooser_parent_class)->finalize (object);
173 account_chooser_get_property (GObject *object,
178 EmpathyAccountChooserPriv *priv;
180 priv = GET_PRIV (object);
183 case PROP_HAS_ALL_OPTION:
184 g_value_set_boolean (value, priv->has_all_option);
187 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
193 account_chooser_set_property (GObject *object,
198 EmpathyAccountChooserPriv *priv;
200 priv = GET_PRIV (object);
203 case PROP_HAS_ALL_OPTION:
204 empathy_account_chooser_set_has_all_option (EMPATHY_ACCOUNT_CHOOSER (object),
205 g_value_get_boolean (value));
208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
214 empathy_account_chooser_new (void)
218 chooser = g_object_new (EMPATHY_TYPE_ACCOUNT_CHOOSER, NULL);
224 empathy_account_chooser_get_account (EmpathyAccountChooser *chooser)
226 EmpathyAccountChooserPriv *priv;
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)) {
240 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
246 empathy_account_chooser_set_account (EmpathyAccountChooser *chooser,
249 GtkComboBox *combobox;
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,
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;
283 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *chooser,
284 gboolean has_all_option)
286 EmpathyAccountChooserPriv *priv;
287 GtkComboBox *combobox;
292 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
294 priv = GET_PRIV (chooser);
296 if (priv->has_all_option == has_all_option) {
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,
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,
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,
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)
344 g_object_notify (G_OBJECT (chooser), "has-all-option");
348 account_chooser_setup (EmpathyAccountChooser *chooser)
350 EmpathyAccountChooserPriv *priv;
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 */
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,
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,
386 /* Populate accounts */
387 accounts = mc_accounts_list ();
388 g_list_foreach (accounts,
389 (GFunc) account_chooser_account_add_foreach,
392 mc_accounts_list_free (accounts);
393 g_object_unref (store);
397 account_chooser_account_created_cb (EmpathyAccountManager *manager,
399 EmpathyAccountChooser *chooser)
401 account_chooser_account_add_foreach (account, chooser);
405 account_chooser_account_add_foreach (McAccount *account,
406 EmpathyAccountChooser *chooser)
409 GtkComboBox *combobox;
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,
420 account_chooser_update_iter (chooser, &iter);
424 account_chooser_account_deleted_cb (EmpathyAccountManager *manager,
426 EmpathyAccountChooser *chooser)
428 account_chooser_account_remove_foreach (account, chooser);
438 account_chooser_find_account_foreach (GtkTreeModel *model,
443 FindAccountData *data = user_data;
446 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
448 if (empathy_account_equal (account, data->account)) {
450 *(data->iter) = *iter;
451 g_object_unref (account);
456 g_object_unref (account);
462 account_chooser_find_account (EmpathyAccountChooser *chooser,
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;
475 gtk_tree_model_foreach (GTK_TREE_MODEL (store),
476 account_chooser_find_account_foreach,
483 account_chooser_account_remove_foreach (McAccount *account,
484 EmpathyAccountChooser *chooser)
487 GtkComboBox *combobox;
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);
499 account_chooser_update_iter (EmpathyAccountChooser *chooser,
502 EmpathyAccountChooserPriv *priv;
504 GtkComboBox *combobox;
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,
518 icon_name = empathy_icon_name_from_account (account);
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,
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);
539 account_chooser_connection_changed_cb (EmpathyAccountManager *manager,
541 TpConnectionStatusReason reason,
542 TpConnectionStatus new_status,
543 TpConnectionStatus old_status,
544 EmpathyAccountChooser *chooser)
548 if (account_chooser_find_account (chooser, account, &iter)) {
549 account_chooser_update_iter (chooser, &iter);
554 account_chooser_separator_func (GtkTreeModel *model,
556 EmpathyAccountChooser *chooser)
558 EmpathyAccountChooserPriv *priv;
560 gboolean is_separator;
562 priv = GET_PRIV (chooser);
564 if (!priv->has_all_option) {
568 gtk_tree_model_get (model, iter, COL_ACCOUNT_TEXT, &text, -1);
569 is_separator = text == NULL;
576 account_chooser_set_account_foreach (GtkTreeModel *model,
579 SetAccountData *data)
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)) {
590 else if (data->account == account) {
593 equal = empathy_account_equal (data->account, account);
597 g_object_unref (account);
601 GtkComboBox *combobox;
603 combobox = GTK_COMBO_BOX (data->chooser);
604 gtk_combo_box_set_active_iter (combobox, iter);
613 account_chooser_filter_foreach (GtkTreeModel *model,
618 account_chooser_update_iter (chooser, iter);
623 empathy_account_chooser_set_filter (EmpathyAccountChooser *chooser,
624 EmpathyAccountChooserFilterFunc filter,
627 EmpathyAccountChooserPriv *priv;
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);
644 empathy_account_chooser_filter_is_connected (McAccount *account,
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);
656 return status == TP_CONNECTION_STATUS_CONNECTED;