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_set_account (EmpathyAccountChooser *chooser,
248 GtkComboBox *combobox;
253 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
255 combobox = GTK_COMBO_BOX (chooser);
256 model = gtk_combo_box_get_model (combobox);
257 gtk_combo_box_get_active_iter (combobox, &iter);
259 data.chooser = chooser;
260 data.account = account;
262 gtk_tree_model_foreach (model,
263 (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
270 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *chooser)
272 EmpathyAccountChooserPriv *priv;
274 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
276 priv = GET_PRIV (chooser);
278 return priv->has_all_option;
282 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *chooser,
283 gboolean has_all_option)
285 EmpathyAccountChooserPriv *priv;
286 GtkComboBox *combobox;
291 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
293 priv = GET_PRIV (chooser);
295 if (priv->has_all_option == has_all_option) {
299 combobox = GTK_COMBO_BOX (chooser);
300 model = gtk_combo_box_get_model (combobox);
301 store = GTK_LIST_STORE (model);
303 priv->has_all_option = has_all_option;
306 * The first 2 options are the ALL and separator
309 if (has_all_option) {
310 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
311 (GtkTreeViewRowSeparatorFunc)
312 account_chooser_separator_func,
316 gtk_list_store_prepend (store, &iter);
317 gtk_list_store_set (store, &iter,
318 COL_ACCOUNT_TEXT, NULL,
319 COL_ACCOUNT_ENABLED, TRUE,
320 COL_ACCOUNT_POINTER, NULL,
323 gtk_list_store_prepend (store, &iter);
324 gtk_list_store_set (store, &iter,
325 COL_ACCOUNT_TEXT, _("All"),
326 COL_ACCOUNT_ENABLED, TRUE,
327 COL_ACCOUNT_POINTER, NULL,
330 if (gtk_tree_model_get_iter_first (model, &iter)) {
331 if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter)) {
332 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
336 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
337 (GtkTreeViewRowSeparatorFunc)
343 g_object_notify (G_OBJECT (chooser), "has-all-option");
347 account_chooser_setup (EmpathyAccountChooser *chooser)
349 EmpathyAccountChooserPriv *priv;
352 GtkCellRenderer *renderer;
353 GtkComboBox *combobox;
355 priv = GET_PRIV (chooser);
357 /* Set up combo box with new store */
358 combobox = GTK_COMBO_BOX (chooser);
360 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
362 store = gtk_list_store_new (COL_ACCOUNT_COUNT,
363 G_TYPE_STRING, /* Image */
364 G_TYPE_STRING, /* Name */
365 G_TYPE_BOOLEAN, /* Enabled */
368 gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
370 renderer = gtk_cell_renderer_pixbuf_new ();
371 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
372 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
373 "icon-name", COL_ACCOUNT_IMAGE,
374 "sensitive", COL_ACCOUNT_ENABLED,
376 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
378 renderer = gtk_cell_renderer_text_new ();
379 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
380 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
381 "text", COL_ACCOUNT_TEXT,
382 "sensitive", COL_ACCOUNT_ENABLED,
385 /* Populate accounts */
386 accounts = mc_accounts_list ();
387 g_list_foreach (accounts,
388 (GFunc) account_chooser_account_add_foreach,
391 mc_accounts_list_free (accounts);
392 g_object_unref (store);
396 account_chooser_account_created_cb (EmpathyAccountManager *manager,
398 EmpathyAccountChooser *chooser)
400 account_chooser_account_add_foreach (account, chooser);
404 account_chooser_account_add_foreach (McAccount *account,
405 EmpathyAccountChooser *chooser)
408 GtkComboBox *combobox;
412 combobox = GTK_COMBO_BOX (chooser);
413 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
415 position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
416 gtk_list_store_insert_with_values (store, &iter, position,
417 COL_ACCOUNT_POINTER, account,
419 account_chooser_update_iter (chooser, &iter);
423 account_chooser_account_deleted_cb (EmpathyAccountManager *manager,
425 EmpathyAccountChooser *chooser)
427 account_chooser_account_remove_foreach (account, chooser);
437 account_chooser_find_account_foreach (GtkTreeModel *model,
442 FindAccountData *data = user_data;
445 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
447 if (empathy_account_equal (account, data->account)) {
449 *(data->iter) = *iter;
450 g_object_unref (account);
455 g_object_unref (account);
461 account_chooser_find_account (EmpathyAccountChooser *chooser,
466 GtkComboBox *combobox;
467 FindAccountData data;
469 combobox = GTK_COMBO_BOX (chooser);
470 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
472 data.account = account;
474 gtk_tree_model_foreach (GTK_TREE_MODEL (store),
475 account_chooser_find_account_foreach,
482 account_chooser_account_remove_foreach (McAccount *account,
483 EmpathyAccountChooser *chooser)
486 GtkComboBox *combobox;
489 combobox = GTK_COMBO_BOX (chooser);
490 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
492 if (account_chooser_find_account (chooser, account, &iter)) {
493 gtk_list_store_remove (store, &iter);
498 account_chooser_update_iter (EmpathyAccountChooser *chooser,
501 EmpathyAccountChooserPriv *priv;
503 GtkComboBox *combobox;
505 const gchar *icon_name;
506 gboolean is_enabled = TRUE;
508 priv = GET_PRIV (chooser);
510 combobox = GTK_COMBO_BOX (chooser);
511 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
513 gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
514 COL_ACCOUNT_POINTER, &account,
517 icon_name = empathy_icon_name_from_account (account);
519 is_enabled = priv->filter (account, priv->filter_data);
522 gtk_list_store_set (store, iter,
523 COL_ACCOUNT_IMAGE, icon_name,
524 COL_ACCOUNT_TEXT, mc_account_get_display_name (account),
525 COL_ACCOUNT_ENABLED, is_enabled,
528 /* set first connected account as active account */
529 if (priv->set_active_item == FALSE && is_enabled) {
530 priv->set_active_item = TRUE;
531 gtk_combo_box_set_active_iter (combobox, iter);
534 g_object_unref (account);
538 account_chooser_connection_changed_cb (EmpathyAccountManager *manager,
540 TpConnectionStatusReason reason,
541 TpConnectionStatus new_status,
542 TpConnectionStatus old_status,
543 EmpathyAccountChooser *chooser)
547 if (account_chooser_find_account (chooser, account, &iter)) {
548 account_chooser_update_iter (chooser, &iter);
553 account_chooser_separator_func (GtkTreeModel *model,
555 EmpathyAccountChooser *chooser)
557 EmpathyAccountChooserPriv *priv;
559 gboolean is_separator;
561 priv = GET_PRIV (chooser);
563 if (!priv->has_all_option) {
567 gtk_tree_model_get (model, iter, COL_ACCOUNT_TEXT, &text, -1);
568 is_separator = text == NULL;
575 account_chooser_set_account_foreach (GtkTreeModel *model,
578 SetAccountData *data)
583 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
585 /* Special case so we can make it possible to select the All option */
586 if ((data->account == NULL) != (account == NULL)) {
589 else if (data->account == account) {
592 equal = empathy_account_equal (data->account, account);
596 g_object_unref (account);
600 GtkComboBox *combobox;
602 combobox = GTK_COMBO_BOX (data->chooser);
603 gtk_combo_box_set_active_iter (combobox, iter);
612 account_chooser_filter_foreach (GtkTreeModel *model,
617 account_chooser_update_iter (chooser, iter);
622 empathy_account_chooser_set_filter (EmpathyAccountChooser *chooser,
623 EmpathyAccountChooserFilterFunc filter,
626 EmpathyAccountChooserPriv *priv;
629 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
631 priv = GET_PRIV (chooser);
633 priv->filter = filter;
634 priv->filter_data = user_data;
636 /* Refilter existing data */
637 priv->set_active_item = FALSE;
638 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
639 gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
643 empathy_account_chooser_filter_is_connected (McAccount *account,
647 TpConnectionStatus status;
649 g_return_val_if_fail (MC_IS_ACCOUNT (account), FALSE);
651 mc = empathy_mission_control_dup_singleton ();
652 status = mission_control_get_connection_status (mc, account, NULL);
655 return status == TP_CONNECTION_STATUS_CONNECTED;