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) EMPATHY_GET_PRIV (obj, EmpathyAccountChooser)
44 McAccountMonitor *monitor;
45 gboolean set_active_item;
46 gboolean has_all_option;
47 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 = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
133 EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv);
135 chooser->priv = priv;
136 priv->set_active_item = FALSE;
138 priv->filter_data = NULL;
142 account_chooser_finalize (GObject *object)
144 EmpathyAccountChooser *chooser;
145 EmpathyAccountChooserPriv *priv;
147 chooser = EMPATHY_ACCOUNT_CHOOSER (object);
148 priv = GET_PRIV (object);
150 g_signal_handlers_disconnect_by_func (priv->monitor,
151 account_chooser_account_created_cb,
153 g_signal_handlers_disconnect_by_func (priv->monitor,
154 account_chooser_account_deleted_cb,
156 empathy_disconnect_account_status_changed (priv->token);
157 g_object_unref (priv->mc);
158 g_object_unref (priv->monitor);
160 G_OBJECT_CLASS (empathy_account_chooser_parent_class)->finalize (object);
164 account_chooser_get_property (GObject *object,
169 EmpathyAccountChooserPriv *priv;
171 priv = GET_PRIV (object);
174 case PROP_HAS_ALL_OPTION:
175 g_value_set_boolean (value, priv->has_all_option);
178 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
184 account_chooser_set_property (GObject *object,
189 EmpathyAccountChooserPriv *priv;
191 priv = GET_PRIV (object);
194 case PROP_HAS_ALL_OPTION:
195 empathy_account_chooser_set_has_all_option (EMPATHY_ACCOUNT_CHOOSER (object),
196 g_value_get_boolean (value));
199 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
205 empathy_account_chooser_new (void)
207 EmpathyAccountChooserPriv *priv;
208 McAccountMonitor *monitor;
211 monitor = mc_account_monitor_new ();
212 chooser = g_object_new (EMPATHY_TYPE_ACCOUNT_CHOOSER, NULL);
214 priv = GET_PRIV (chooser);
216 priv->mc = empathy_mission_control_new ();
217 priv->monitor = mc_account_monitor_new ();
219 g_signal_connect (priv->monitor, "account-created",
220 G_CALLBACK (account_chooser_account_created_cb),
222 g_signal_connect (priv->monitor, "account-deleted",
223 G_CALLBACK (account_chooser_account_deleted_cb),
225 priv->token = empathy_connect_to_account_status_changed (priv->mc,
226 G_CALLBACK (account_chooser_status_changed_cb),
229 account_chooser_setup (EMPATHY_ACCOUNT_CHOOSER (chooser));
235 empathy_account_chooser_get_account (EmpathyAccountChooser *chooser)
237 EmpathyAccountChooserPriv *priv;
242 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
244 priv = GET_PRIV (chooser);
246 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
247 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (chooser), &iter);
249 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
255 empathy_account_chooser_set_account (EmpathyAccountChooser *chooser,
258 GtkComboBox *combobox;
263 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
265 combobox = GTK_COMBO_BOX (chooser);
266 model = gtk_combo_box_get_model (combobox);
267 gtk_combo_box_get_active_iter (combobox, &iter);
269 data.chooser = chooser;
270 data.account = account;
272 gtk_tree_model_foreach (model,
273 (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
280 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *chooser)
282 EmpathyAccountChooserPriv *priv;
284 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
286 priv = GET_PRIV (chooser);
288 return priv->has_all_option;
292 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *chooser,
293 gboolean has_all_option)
295 EmpathyAccountChooserPriv *priv;
296 GtkComboBox *combobox;
301 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
303 priv = GET_PRIV (chooser);
305 if (priv->has_all_option == has_all_option) {
309 combobox = GTK_COMBO_BOX (chooser);
310 model = gtk_combo_box_get_model (combobox);
311 store = GTK_LIST_STORE (model);
313 priv->has_all_option = has_all_option;
316 * The first 2 options are the ALL and separator
319 if (has_all_option) {
320 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
321 (GtkTreeViewRowSeparatorFunc)
322 account_chooser_separator_func,
326 gtk_list_store_prepend (store, &iter);
327 gtk_list_store_set (store, &iter,
328 COL_ACCOUNT_TEXT, NULL,
329 COL_ACCOUNT_ENABLED, TRUE,
330 COL_ACCOUNT_POINTER, NULL,
333 gtk_list_store_prepend (store, &iter);
334 gtk_list_store_set (store, &iter,
335 COL_ACCOUNT_TEXT, _("All"),
336 COL_ACCOUNT_ENABLED, TRUE,
337 COL_ACCOUNT_POINTER, NULL,
340 if (gtk_tree_model_get_iter_first (model, &iter)) {
341 if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter)) {
342 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
346 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
347 (GtkTreeViewRowSeparatorFunc)
353 g_object_notify (G_OBJECT (chooser), "has-all-option");
357 account_chooser_setup (EmpathyAccountChooser *chooser)
359 EmpathyAccountChooserPriv *priv;
362 GtkCellRenderer *renderer;
363 GtkComboBox *combobox;
365 priv = GET_PRIV (chooser);
367 /* Set up combo box with new store */
368 combobox = GTK_COMBO_BOX (chooser);
370 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
372 store = gtk_list_store_new (COL_ACCOUNT_COUNT,
373 G_TYPE_STRING, /* Image */
374 G_TYPE_STRING, /* Name */
375 G_TYPE_BOOLEAN, /* Enabled */
378 gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
380 renderer = gtk_cell_renderer_pixbuf_new ();
381 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
382 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
383 "icon-name", COL_ACCOUNT_IMAGE,
384 "sensitive", COL_ACCOUNT_ENABLED,
386 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
388 renderer = gtk_cell_renderer_text_new ();
389 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
390 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
391 "text", COL_ACCOUNT_TEXT,
392 "sensitive", COL_ACCOUNT_ENABLED,
395 /* Populate accounts */
396 accounts = mc_accounts_list ();
397 g_list_foreach (accounts,
398 (GFunc) account_chooser_account_add_foreach,
401 mc_accounts_list_free (accounts);
402 g_object_unref (store);
406 account_chooser_account_created_cb (McAccountMonitor *monitor,
407 const gchar *unique_name,
408 EmpathyAccountChooser *chooser)
412 account = mc_account_lookup (unique_name);
413 account_chooser_account_add_foreach (account, chooser);
414 g_object_unref (account);
418 account_chooser_account_add_foreach (McAccount *account,
419 EmpathyAccountChooser *chooser)
422 GtkComboBox *combobox;
426 combobox = GTK_COMBO_BOX (chooser);
427 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
429 position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
430 gtk_list_store_insert_with_values (store, &iter, position,
431 COL_ACCOUNT_POINTER, account,
433 account_chooser_update_iter (chooser, &iter);
437 account_chooser_account_deleted_cb (McAccountMonitor *monitor,
438 const gchar *unique_name,
439 EmpathyAccountChooser *chooser)
443 account = mc_account_lookup (unique_name);
444 account_chooser_account_remove_foreach (account, chooser);
445 g_object_unref (account);
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_status_changed_cb (MissionControl *mc,
557 TpConnectionStatus status,
559 TpConnectionStatusReason reason,
560 const gchar *unique_name,
561 EmpathyAccountChooser *chooser)
566 account = mc_account_lookup (unique_name);
567 if (account_chooser_find_account (chooser, account, &iter)) {
568 account_chooser_update_iter (chooser, &iter);
570 g_object_unref (account);
574 account_chooser_separator_func (GtkTreeModel *model,
576 EmpathyAccountChooser *chooser)
578 EmpathyAccountChooserPriv *priv;
580 gboolean is_separator;
582 priv = GET_PRIV (chooser);
584 if (!priv->has_all_option) {
588 gtk_tree_model_get (model, iter, COL_ACCOUNT_TEXT, &text, -1);
589 is_separator = text == NULL;
596 account_chooser_set_account_foreach (GtkTreeModel *model,
599 SetAccountData *data)
604 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
606 /* Special case so we can make it possible to select the All option */
607 if ((data->account == NULL) != (account == NULL)) {
610 else if (data->account == account) {
613 equal = empathy_account_equal (data->account, account);
617 g_object_unref (account);
621 GtkComboBox *combobox;
623 combobox = GTK_COMBO_BOX (data->chooser);
624 gtk_combo_box_set_active_iter (combobox, iter);
633 account_chooser_filter_foreach (GtkTreeModel *model,
638 account_chooser_update_iter (chooser, iter);
643 empathy_account_chooser_set_filter (EmpathyAccountChooser *chooser,
644 EmpathyAccountChooserFilterFunc filter,
647 EmpathyAccountChooserPriv *priv;
650 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
652 priv = GET_PRIV (chooser);
654 priv->filter = filter;
655 priv->filter_data = user_data;
657 /* Refilter existing data */
658 priv->set_active_item = FALSE;
659 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
660 gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
664 empathy_account_chooser_filter_is_connected (McAccount *account,
668 TpConnectionStatus status;
670 g_return_val_if_fail (MC_IS_ACCOUNT (account), FALSE);
672 mc = empathy_mission_control_new ();
673 status = mission_control_get_connection_status (mc, account, NULL);
676 return status == TP_CONNECTION_STATUS_CONNECTED;