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.h>
31 #include <glade/glade.h>
33 #include <libmissioncontrol/mc-account-monitor.h>
34 #include <libmissioncontrol/mission-control.h>
36 #include <libempathy/empathy-utils.h>
38 #include "empathy-ui-utils.h"
39 #include "empathy-account-chooser.h"
41 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv))
45 McAccountMonitor *monitor;
46 gboolean set_active_item;
47 gboolean has_all_option;
48 EmpathyAccountChooserFilterFunc filter;
50 } EmpathyAccountChooserPriv;
53 EmpathyAccountChooser *chooser;
61 COL_ACCOUNT_ENABLED, /* Usually tied to connected state */
66 static void account_chooser_finalize (GObject *object);
67 static void account_chooser_get_property (GObject *object,
71 static void account_chooser_set_property (GObject *object,
75 static void account_chooser_setup (EmpathyAccountChooser *chooser);
76 static void account_chooser_account_created_cb (McAccountMonitor *monitor,
77 const gchar *unique_name,
78 EmpathyAccountChooser *chooser);
79 static void account_chooser_account_add_foreach (McAccount *account,
80 EmpathyAccountChooser *chooser);
81 static void account_chooser_account_deleted_cb (McAccountMonitor *monitor,
82 const gchar *unique_name,
83 EmpathyAccountChooser *chooser);
84 static void account_chooser_account_remove_foreach (McAccount *account,
85 EmpathyAccountChooser *chooser);
86 static void account_chooser_update_iter (EmpathyAccountChooser *chooser,
88 static void account_chooser_status_changed_cb (MissionControl *mc,
89 TpConnectionStatus status,
91 TpConnectionStatusReason reason,
92 const gchar *unique_name,
93 EmpathyAccountChooser *chooser);
94 static gboolean account_chooser_separator_func (GtkTreeModel *model,
96 EmpathyAccountChooser *chooser);
97 static gboolean account_chooser_set_account_foreach (GtkTreeModel *model,
100 SetAccountData *data);
107 G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser, GTK_TYPE_COMBO_BOX);
110 empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
112 GObjectClass *object_class = G_OBJECT_CLASS (klass);
114 object_class->finalize = account_chooser_finalize;
115 object_class->get_property = account_chooser_get_property;
116 object_class->set_property = account_chooser_set_property;
118 g_object_class_install_property (object_class,
120 g_param_spec_boolean ("has-all-option",
122 "Have a separate option in the list to mean ALL accounts",
126 g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
130 empathy_account_chooser_init (EmpathyAccountChooser *chooser)
132 EmpathyAccountChooserPriv *priv = GET_PRIV (chooser);
134 priv->set_active_item = FALSE;
136 priv->filter_data = NULL;
140 account_chooser_finalize (GObject *object)
142 EmpathyAccountChooser *chooser;
143 EmpathyAccountChooserPriv *priv;
145 chooser = EMPATHY_ACCOUNT_CHOOSER (object);
146 priv = GET_PRIV (object);
148 g_signal_handlers_disconnect_by_func (priv->monitor,
149 account_chooser_account_created_cb,
151 g_signal_handlers_disconnect_by_func (priv->monitor,
152 account_chooser_account_deleted_cb,
154 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
155 "AccountStatusChanged",
156 G_CALLBACK (account_chooser_status_changed_cb),
158 g_object_unref (priv->mc);
159 g_object_unref (priv->monitor);
161 G_OBJECT_CLASS (empathy_account_chooser_parent_class)->finalize (object);
165 account_chooser_get_property (GObject *object,
170 EmpathyAccountChooserPriv *priv;
172 priv = GET_PRIV (object);
175 case PROP_HAS_ALL_OPTION:
176 g_value_set_boolean (value, priv->has_all_option);
179 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
185 account_chooser_set_property (GObject *object,
190 EmpathyAccountChooserPriv *priv;
192 priv = GET_PRIV (object);
195 case PROP_HAS_ALL_OPTION:
196 empathy_account_chooser_set_has_all_option (EMPATHY_ACCOUNT_CHOOSER (object),
197 g_value_get_boolean (value));
200 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
206 empathy_account_chooser_new (void)
208 EmpathyAccountChooserPriv *priv;
209 McAccountMonitor *monitor;
212 monitor = mc_account_monitor_new ();
213 chooser = g_object_new (EMPATHY_TYPE_ACCOUNT_CHOOSER, NULL);
215 priv = GET_PRIV (chooser);
217 priv->mc = empathy_mission_control_new ();
218 priv->monitor = mc_account_monitor_new ();
220 g_signal_connect (priv->monitor, "account-created",
221 G_CALLBACK (account_chooser_account_created_cb),
223 g_signal_connect (priv->monitor, "account-deleted",
224 G_CALLBACK (account_chooser_account_deleted_cb),
226 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc), "AccountStatusChanged",
227 G_CALLBACK (account_chooser_status_changed_cb),
230 account_chooser_setup (EMPATHY_ACCOUNT_CHOOSER (chooser));
236 empathy_account_chooser_get_account (EmpathyAccountChooser *chooser)
238 EmpathyAccountChooserPriv *priv;
243 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
245 priv = GET_PRIV (chooser);
247 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
248 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (chooser), &iter);
250 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
256 empathy_account_chooser_set_account (EmpathyAccountChooser *chooser,
259 GtkComboBox *combobox;
264 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
266 combobox = GTK_COMBO_BOX (chooser);
267 model = gtk_combo_box_get_model (combobox);
268 gtk_combo_box_get_active_iter (combobox, &iter);
270 data.chooser = chooser;
271 data.account = account;
273 gtk_tree_model_foreach (model,
274 (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
281 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *chooser)
283 EmpathyAccountChooserPriv *priv;
285 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
287 priv = GET_PRIV (chooser);
289 return priv->has_all_option;
293 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *chooser,
294 gboolean has_all_option)
296 EmpathyAccountChooserPriv *priv;
297 GtkComboBox *combobox;
302 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
304 priv = GET_PRIV (chooser);
306 if (priv->has_all_option == has_all_option) {
310 combobox = GTK_COMBO_BOX (chooser);
311 model = gtk_combo_box_get_model (combobox);
312 store = GTK_LIST_STORE (model);
314 priv->has_all_option = has_all_option;
317 * The first 2 options are the ALL and separator
320 if (has_all_option) {
321 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
322 (GtkTreeViewRowSeparatorFunc)
323 account_chooser_separator_func,
327 gtk_list_store_prepend (store, &iter);
328 gtk_list_store_set (store, &iter,
329 COL_ACCOUNT_TEXT, NULL,
330 COL_ACCOUNT_ENABLED, TRUE,
331 COL_ACCOUNT_POINTER, NULL,
334 gtk_list_store_prepend (store, &iter);
335 gtk_list_store_set (store, &iter,
336 COL_ACCOUNT_TEXT, _("All"),
337 COL_ACCOUNT_ENABLED, TRUE,
338 COL_ACCOUNT_POINTER, NULL,
341 if (gtk_tree_model_get_iter_first (model, &iter)) {
342 if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter)) {
343 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
347 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
348 (GtkTreeViewRowSeparatorFunc)
354 g_object_notify (G_OBJECT (chooser), "has-all-option");
358 account_chooser_setup (EmpathyAccountChooser *chooser)
360 EmpathyAccountChooserPriv *priv;
363 GtkCellRenderer *renderer;
364 GtkComboBox *combobox;
366 priv = GET_PRIV (chooser);
368 /* Set up combo box with new store */
369 combobox = GTK_COMBO_BOX (chooser);
371 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
373 store = gtk_list_store_new (COL_ACCOUNT_COUNT,
374 G_TYPE_STRING, /* Image */
375 G_TYPE_STRING, /* Name */
376 G_TYPE_BOOLEAN, /* Enabled */
379 gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
381 renderer = gtk_cell_renderer_pixbuf_new ();
382 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
383 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
384 "icon-name", COL_ACCOUNT_IMAGE,
385 "sensitive", COL_ACCOUNT_ENABLED,
387 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
389 renderer = gtk_cell_renderer_text_new ();
390 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
391 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
392 "text", COL_ACCOUNT_TEXT,
393 "sensitive", COL_ACCOUNT_ENABLED,
396 /* Populate accounts */
397 accounts = mc_accounts_list ();
398 g_list_foreach (accounts,
399 (GFunc) account_chooser_account_add_foreach,
402 mc_accounts_list_free (accounts);
403 g_object_unref (store);
407 account_chooser_account_created_cb (McAccountMonitor *monitor,
408 const gchar *unique_name,
409 EmpathyAccountChooser *chooser)
413 account = mc_account_lookup (unique_name);
414 account_chooser_account_add_foreach (account, chooser);
415 g_object_unref (account);
419 account_chooser_account_add_foreach (McAccount *account,
420 EmpathyAccountChooser *chooser)
423 GtkComboBox *combobox;
427 combobox = GTK_COMBO_BOX (chooser);
428 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
430 position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
431 gtk_list_store_insert_with_values (store, &iter, position,
432 COL_ACCOUNT_POINTER, account,
434 account_chooser_update_iter (chooser, &iter);
438 account_chooser_account_deleted_cb (McAccountMonitor *monitor,
439 const gchar *unique_name,
440 EmpathyAccountChooser *chooser)
444 account = mc_account_lookup (unique_name);
445 account_chooser_account_remove_foreach (account, chooser);
446 g_object_unref (account);
456 account_chooser_find_account_foreach (GtkTreeModel *model,
461 FindAccountData *data = user_data;
464 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
466 if (empathy_account_equal (account, data->account)) {
468 *(data->iter) = *iter;
469 g_object_unref (account);
474 g_object_unref (account);
480 account_chooser_find_account (EmpathyAccountChooser *chooser,
485 GtkComboBox *combobox;
486 FindAccountData data;
488 combobox = GTK_COMBO_BOX (chooser);
489 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
491 data.account = account;
493 gtk_tree_model_foreach (GTK_TREE_MODEL (store),
494 account_chooser_find_account_foreach,
501 account_chooser_account_remove_foreach (McAccount *account,
502 EmpathyAccountChooser *chooser)
505 GtkComboBox *combobox;
508 combobox = GTK_COMBO_BOX (chooser);
509 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
511 if (account_chooser_find_account (chooser, account, &iter)) {
512 gtk_list_store_remove (store, &iter);
517 account_chooser_update_iter (EmpathyAccountChooser *chooser,
520 EmpathyAccountChooserPriv *priv;
522 GtkComboBox *combobox;
524 const gchar *icon_name;
525 gboolean is_enabled = TRUE;
527 priv = GET_PRIV (chooser);
529 combobox = GTK_COMBO_BOX (chooser);
530 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
532 gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
533 COL_ACCOUNT_POINTER, &account,
536 icon_name = empathy_icon_name_from_account (account);
538 is_enabled = priv->filter (account, priv->filter_data);
541 gtk_list_store_set (store, iter,
542 COL_ACCOUNT_IMAGE, icon_name,
543 COL_ACCOUNT_TEXT, mc_account_get_display_name (account),
544 COL_ACCOUNT_ENABLED, is_enabled,
547 /* set first connected account as active account */
548 if (priv->set_active_item == FALSE && is_enabled) {
549 priv->set_active_item = TRUE;
550 gtk_combo_box_set_active_iter (combobox, iter);
553 g_object_unref (account);
557 account_chooser_status_changed_cb (MissionControl *mc,
558 TpConnectionStatus status,
560 TpConnectionStatusReason reason,
561 const gchar *unique_name,
562 EmpathyAccountChooser *chooser)
567 account = mc_account_lookup (unique_name);
568 if (account_chooser_find_account (chooser, account, &iter)) {
569 account_chooser_update_iter (chooser, &iter);
571 g_object_unref (account);
575 account_chooser_separator_func (GtkTreeModel *model,
577 EmpathyAccountChooser *chooser)
579 EmpathyAccountChooserPriv *priv;
581 gboolean is_separator;
583 priv = GET_PRIV (chooser);
585 if (!priv->has_all_option) {
589 gtk_tree_model_get (model, iter, COL_ACCOUNT_TEXT, &text, -1);
590 is_separator = text == NULL;
597 account_chooser_set_account_foreach (GtkTreeModel *model,
600 SetAccountData *data)
605 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
607 /* Special case so we can make it possible to select the All option */
608 if ((data->account == NULL) != (account == NULL)) {
611 else if (data->account == account) {
614 equal = empathy_account_equal (data->account, account);
618 g_object_unref (account);
622 GtkComboBox *combobox;
624 combobox = GTK_COMBO_BOX (data->chooser);
625 gtk_combo_box_set_active_iter (combobox, iter);
634 account_chooser_filter_foreach (GtkTreeModel *model,
639 account_chooser_update_iter (chooser, iter);
644 empathy_account_chooser_set_filter (EmpathyAccountChooser *chooser,
645 EmpathyAccountChooserFilterFunc filter,
648 EmpathyAccountChooserPriv *priv;
651 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
653 priv = GET_PRIV (chooser);
655 priv->filter = filter;
656 priv->filter_data = user_data;
658 /* Refilter existing data */
659 priv->set_active_item = FALSE;
660 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
661 gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
665 empathy_account_chooser_filter_is_connected (McAccount *account,
669 TpConnectionStatus status;
671 g_return_val_if_fail (MC_IS_ACCOUNT (account), FALSE);
673 mc = empathy_mission_control_new ();
674 status = mission_control_get_connection_status (mc, account, NULL);
677 return status == TP_CONNECTION_STATUS_CONNECTED;