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>
32 #include <libmissioncontrol/mission-control.h>
34 #include <libempathy/empathy-account-manager.h>
35 #include <libempathy/empathy-utils.h>
37 #include "empathy-ui-utils.h"
38 #include "empathy-account-chooser.h"
40 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountChooser)
42 EmpathyAccountManager *manager;
43 gboolean set_active_item;
44 gboolean has_all_option;
45 EmpathyAccountChooserFilterFunc filter;
47 } EmpathyAccountChooserPriv;
50 EmpathyAccountChooser *chooser;
58 COL_ACCOUNT_ENABLED, /* Usually tied to connected state */
63 static void account_chooser_finalize (GObject *object);
64 static void account_chooser_get_property (GObject *object,
68 static void account_chooser_set_property (GObject *object,
72 static void account_chooser_setup (EmpathyAccountChooser *chooser);
73 static void account_chooser_account_created_cb (EmpathyAccountManager *manager,
75 EmpathyAccountChooser *chooser);
76 static void account_chooser_account_add_foreach (McAccount *account,
77 EmpathyAccountChooser *chooser);
78 static void account_chooser_account_deleted_cb (EmpathyAccountManager *manager,
80 EmpathyAccountChooser *chooser);
81 static void account_chooser_account_remove_foreach (McAccount *account,
82 EmpathyAccountChooser *chooser);
83 static void account_chooser_update_iter (EmpathyAccountChooser *chooser,
85 static void account_chooser_connection_changed_cb (EmpathyAccountManager *manager,
87 TpConnectionStatusReason reason,
88 TpConnectionStatus new_status,
89 TpConnectionStatus old_status,
90 EmpathyAccountChooser *chooser);
91 static gboolean account_chooser_separator_func (GtkTreeModel *model,
93 EmpathyAccountChooser *chooser);
94 static gboolean account_chooser_set_account_foreach (GtkTreeModel *model,
97 SetAccountData *data);
104 G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser, GTK_TYPE_COMBO_BOX);
107 empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
109 GObjectClass *object_class = G_OBJECT_CLASS (klass);
111 object_class->finalize = account_chooser_finalize;
112 object_class->get_property = account_chooser_get_property;
113 object_class->set_property = account_chooser_set_property;
115 g_object_class_install_property (object_class,
117 g_param_spec_boolean ("has-all-option",
119 "Have a separate option in the list to mean ALL accounts",
123 g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
127 empathy_account_chooser_init (EmpathyAccountChooser *chooser)
129 EmpathyAccountChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
130 EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv);
132 chooser->priv = priv;
133 priv->set_active_item = FALSE;
135 priv->filter_data = NULL;
137 priv->manager = empathy_account_manager_dup_singleton ();
139 g_signal_connect (priv->manager, "account-created",
140 G_CALLBACK (account_chooser_account_created_cb),
142 g_signal_connect (priv->manager, "account-deleted",
143 G_CALLBACK (account_chooser_account_deleted_cb),
145 g_signal_connect (priv->manager, "account-connection-changed",
146 G_CALLBACK (account_chooser_connection_changed_cb),
149 account_chooser_setup (EMPATHY_ACCOUNT_CHOOSER (chooser));
153 account_chooser_finalize (GObject *object)
155 EmpathyAccountChooserPriv *priv = GET_PRIV (object);
157 g_signal_handlers_disconnect_by_func (priv->manager,
158 account_chooser_connection_changed_cb,
160 g_signal_handlers_disconnect_by_func (priv->manager,
161 account_chooser_account_created_cb,
163 g_signal_handlers_disconnect_by_func (priv->manager,
164 account_chooser_account_deleted_cb,
166 g_object_unref (priv->manager);
168 G_OBJECT_CLASS (empathy_account_chooser_parent_class)->finalize (object);
172 account_chooser_get_property (GObject *object,
177 EmpathyAccountChooserPriv *priv;
179 priv = GET_PRIV (object);
182 case PROP_HAS_ALL_OPTION:
183 g_value_set_boolean (value, priv->has_all_option);
186 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
192 account_chooser_set_property (GObject *object,
197 EmpathyAccountChooserPriv *priv;
199 priv = GET_PRIV (object);
202 case PROP_HAS_ALL_OPTION:
203 empathy_account_chooser_set_has_all_option (EMPATHY_ACCOUNT_CHOOSER (object),
204 g_value_get_boolean (value));
207 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
213 empathy_account_chooser_new (void)
217 chooser = g_object_new (EMPATHY_TYPE_ACCOUNT_CHOOSER, NULL);
223 empathy_account_chooser_get_account (EmpathyAccountChooser *chooser)
225 EmpathyAccountChooserPriv *priv;
230 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
232 priv = GET_PRIV (chooser);
234 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
235 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (chooser), &iter)) {
239 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
245 empathy_account_chooser_get_connection (EmpathyAccountChooser *chooser)
247 EmpathyAccountChooserPriv *priv;
249 TpConnection *connection;
251 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
253 priv = GET_PRIV (chooser);
255 account = empathy_account_chooser_get_account (chooser);
256 connection = empathy_account_manager_get_connection (priv->manager, account);
257 g_object_unref (account);
263 empathy_account_chooser_set_account (EmpathyAccountChooser *chooser,
266 GtkComboBox *combobox;
271 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
273 combobox = GTK_COMBO_BOX (chooser);
274 model = gtk_combo_box_get_model (combobox);
275 gtk_combo_box_get_active_iter (combobox, &iter);
277 data.chooser = chooser;
278 data.account = account;
280 gtk_tree_model_foreach (model,
281 (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
288 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *chooser)
290 EmpathyAccountChooserPriv *priv;
292 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
294 priv = GET_PRIV (chooser);
296 return priv->has_all_option;
300 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *chooser,
301 gboolean has_all_option)
303 EmpathyAccountChooserPriv *priv;
304 GtkComboBox *combobox;
309 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
311 priv = GET_PRIV (chooser);
313 if (priv->has_all_option == has_all_option) {
317 combobox = GTK_COMBO_BOX (chooser);
318 model = gtk_combo_box_get_model (combobox);
319 store = GTK_LIST_STORE (model);
321 priv->has_all_option = has_all_option;
324 * The first 2 options are the ALL and separator
327 if (has_all_option) {
328 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
329 (GtkTreeViewRowSeparatorFunc)
330 account_chooser_separator_func,
334 gtk_list_store_prepend (store, &iter);
335 gtk_list_store_set (store, &iter,
336 COL_ACCOUNT_TEXT, NULL,
337 COL_ACCOUNT_ENABLED, TRUE,
338 COL_ACCOUNT_POINTER, NULL,
341 gtk_list_store_prepend (store, &iter);
342 gtk_list_store_set (store, &iter,
343 COL_ACCOUNT_TEXT, _("All"),
344 COL_ACCOUNT_ENABLED, TRUE,
345 COL_ACCOUNT_POINTER, NULL,
348 if (gtk_tree_model_get_iter_first (model, &iter)) {
349 if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter)) {
350 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
354 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
355 (GtkTreeViewRowSeparatorFunc)
361 g_object_notify (G_OBJECT (chooser), "has-all-option");
365 account_chooser_setup (EmpathyAccountChooser *chooser)
367 EmpathyAccountChooserPriv *priv;
370 GtkCellRenderer *renderer;
371 GtkComboBox *combobox;
373 priv = GET_PRIV (chooser);
375 /* Set up combo box with new store */
376 combobox = GTK_COMBO_BOX (chooser);
378 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
380 store = gtk_list_store_new (COL_ACCOUNT_COUNT,
381 G_TYPE_STRING, /* Image */
382 G_TYPE_STRING, /* Name */
383 G_TYPE_BOOLEAN, /* Enabled */
386 gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
388 renderer = gtk_cell_renderer_pixbuf_new ();
389 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
390 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
391 "icon-name", COL_ACCOUNT_IMAGE,
392 "sensitive", COL_ACCOUNT_ENABLED,
394 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
396 renderer = gtk_cell_renderer_text_new ();
397 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
398 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
399 "text", COL_ACCOUNT_TEXT,
400 "sensitive", COL_ACCOUNT_ENABLED,
403 /* Populate accounts */
404 accounts = mc_accounts_list ();
405 g_list_foreach (accounts,
406 (GFunc) account_chooser_account_add_foreach,
409 mc_accounts_list_free (accounts);
410 g_object_unref (store);
414 account_chooser_account_created_cb (EmpathyAccountManager *manager,
416 EmpathyAccountChooser *chooser)
418 account_chooser_account_add_foreach (account, chooser);
422 account_chooser_account_add_foreach (McAccount *account,
423 EmpathyAccountChooser *chooser)
426 GtkComboBox *combobox;
430 combobox = GTK_COMBO_BOX (chooser);
431 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
433 position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
434 gtk_list_store_insert_with_values (store, &iter, position,
435 COL_ACCOUNT_POINTER, account,
437 account_chooser_update_iter (chooser, &iter);
441 account_chooser_account_deleted_cb (EmpathyAccountManager *manager,
443 EmpathyAccountChooser *chooser)
445 account_chooser_account_remove_foreach (account, chooser);
455 account_chooser_find_account_foreach (GtkTreeModel *model,
460 FindAccountData *data = user_data;
463 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
465 if (empathy_account_equal (account, data->account)) {
467 *(data->iter) = *iter;
468 g_object_unref (account);
473 g_object_unref (account);
479 account_chooser_find_account (EmpathyAccountChooser *chooser,
484 GtkComboBox *combobox;
485 FindAccountData data;
487 combobox = GTK_COMBO_BOX (chooser);
488 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
490 data.account = account;
492 gtk_tree_model_foreach (GTK_TREE_MODEL (store),
493 account_chooser_find_account_foreach,
500 account_chooser_account_remove_foreach (McAccount *account,
501 EmpathyAccountChooser *chooser)
504 GtkComboBox *combobox;
507 combobox = GTK_COMBO_BOX (chooser);
508 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
510 if (account_chooser_find_account (chooser, account, &iter)) {
511 gtk_list_store_remove (store, &iter);
516 account_chooser_update_iter (EmpathyAccountChooser *chooser,
519 EmpathyAccountChooserPriv *priv;
521 GtkComboBox *combobox;
523 const gchar *icon_name;
524 gboolean is_enabled = TRUE;
526 priv = GET_PRIV (chooser);
528 combobox = GTK_COMBO_BOX (chooser);
529 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
531 gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
532 COL_ACCOUNT_POINTER, &account,
535 icon_name = empathy_icon_name_from_account (account);
537 is_enabled = priv->filter (account, priv->filter_data);
540 gtk_list_store_set (store, iter,
541 COL_ACCOUNT_IMAGE, icon_name,
542 COL_ACCOUNT_TEXT, mc_account_get_display_name (account),
543 COL_ACCOUNT_ENABLED, is_enabled,
546 /* set first connected account as active account */
547 if (priv->set_active_item == FALSE && is_enabled) {
548 priv->set_active_item = TRUE;
549 gtk_combo_box_set_active_iter (combobox, iter);
552 g_object_unref (account);
556 account_chooser_connection_changed_cb (EmpathyAccountManager *manager,
558 TpConnectionStatusReason reason,
559 TpConnectionStatus new_status,
560 TpConnectionStatus old_status,
561 EmpathyAccountChooser *chooser)
565 if (account_chooser_find_account (chooser, account, &iter)) {
566 account_chooser_update_iter (chooser, &iter);
571 account_chooser_separator_func (GtkTreeModel *model,
573 EmpathyAccountChooser *chooser)
575 EmpathyAccountChooserPriv *priv;
577 gboolean is_separator;
579 priv = GET_PRIV (chooser);
581 if (!priv->has_all_option) {
585 gtk_tree_model_get (model, iter, COL_ACCOUNT_TEXT, &text, -1);
586 is_separator = text == NULL;
593 account_chooser_set_account_foreach (GtkTreeModel *model,
596 SetAccountData *data)
601 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
603 /* Special case so we can make it possible to select the All option */
604 if ((data->account == NULL) != (account == NULL)) {
607 else if (data->account == account) {
610 equal = empathy_account_equal (data->account, account);
614 g_object_unref (account);
618 GtkComboBox *combobox;
620 combobox = GTK_COMBO_BOX (data->chooser);
621 gtk_combo_box_set_active_iter (combobox, iter);
630 account_chooser_filter_foreach (GtkTreeModel *model,
635 account_chooser_update_iter (chooser, iter);
640 empathy_account_chooser_set_filter (EmpathyAccountChooser *chooser,
641 EmpathyAccountChooserFilterFunc filter,
644 EmpathyAccountChooserPriv *priv;
647 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
649 priv = GET_PRIV (chooser);
651 priv->filter = filter;
652 priv->filter_data = user_data;
654 /* Refilter existing data */
655 priv->set_active_item = FALSE;
656 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
657 gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
661 empathy_account_chooser_filter_is_connected (McAccount *account,
665 TpConnectionStatus status;
667 g_return_val_if_fail (MC_IS_ACCOUNT (account), FALSE);
669 mc = empathy_mission_control_dup_singleton ();
670 status = mission_control_get_connection_status (mc, account, NULL);
673 return status == TP_CONNECTION_STATUS_CONNECTED;