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 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (chooser), &iter)) {
251 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
257 empathy_account_chooser_set_account (EmpathyAccountChooser *chooser,
260 GtkComboBox *combobox;
265 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
267 combobox = GTK_COMBO_BOX (chooser);
268 model = gtk_combo_box_get_model (combobox);
269 gtk_combo_box_get_active_iter (combobox, &iter);
271 data.chooser = chooser;
272 data.account = account;
274 gtk_tree_model_foreach (model,
275 (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
282 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *chooser)
284 EmpathyAccountChooserPriv *priv;
286 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
288 priv = GET_PRIV (chooser);
290 return priv->has_all_option;
294 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *chooser,
295 gboolean has_all_option)
297 EmpathyAccountChooserPriv *priv;
298 GtkComboBox *combobox;
303 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
305 priv = GET_PRIV (chooser);
307 if (priv->has_all_option == has_all_option) {
311 combobox = GTK_COMBO_BOX (chooser);
312 model = gtk_combo_box_get_model (combobox);
313 store = GTK_LIST_STORE (model);
315 priv->has_all_option = has_all_option;
318 * The first 2 options are the ALL and separator
321 if (has_all_option) {
322 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
323 (GtkTreeViewRowSeparatorFunc)
324 account_chooser_separator_func,
328 gtk_list_store_prepend (store, &iter);
329 gtk_list_store_set (store, &iter,
330 COL_ACCOUNT_TEXT, NULL,
331 COL_ACCOUNT_ENABLED, TRUE,
332 COL_ACCOUNT_POINTER, NULL,
335 gtk_list_store_prepend (store, &iter);
336 gtk_list_store_set (store, &iter,
337 COL_ACCOUNT_TEXT, _("All"),
338 COL_ACCOUNT_ENABLED, TRUE,
339 COL_ACCOUNT_POINTER, NULL,
342 if (gtk_tree_model_get_iter_first (model, &iter)) {
343 if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter)) {
344 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
348 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
349 (GtkTreeViewRowSeparatorFunc)
355 g_object_notify (G_OBJECT (chooser), "has-all-option");
359 account_chooser_setup (EmpathyAccountChooser *chooser)
361 EmpathyAccountChooserPriv *priv;
364 GtkCellRenderer *renderer;
365 GtkComboBox *combobox;
367 priv = GET_PRIV (chooser);
369 /* Set up combo box with new store */
370 combobox = GTK_COMBO_BOX (chooser);
372 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
374 store = gtk_list_store_new (COL_ACCOUNT_COUNT,
375 G_TYPE_STRING, /* Image */
376 G_TYPE_STRING, /* Name */
377 G_TYPE_BOOLEAN, /* Enabled */
380 gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
382 renderer = gtk_cell_renderer_pixbuf_new ();
383 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
384 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
385 "icon-name", COL_ACCOUNT_IMAGE,
386 "sensitive", COL_ACCOUNT_ENABLED,
388 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
390 renderer = gtk_cell_renderer_text_new ();
391 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
392 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
393 "text", COL_ACCOUNT_TEXT,
394 "sensitive", COL_ACCOUNT_ENABLED,
397 /* Populate accounts */
398 accounts = mc_accounts_list ();
399 g_list_foreach (accounts,
400 (GFunc) account_chooser_account_add_foreach,
403 mc_accounts_list_free (accounts);
404 g_object_unref (store);
408 account_chooser_account_created_cb (McAccountMonitor *monitor,
409 const gchar *unique_name,
410 EmpathyAccountChooser *chooser)
414 account = mc_account_lookup (unique_name);
415 account_chooser_account_add_foreach (account, chooser);
416 g_object_unref (account);
420 account_chooser_account_add_foreach (McAccount *account,
421 EmpathyAccountChooser *chooser)
424 GtkComboBox *combobox;
428 combobox = GTK_COMBO_BOX (chooser);
429 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
431 position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
432 gtk_list_store_insert_with_values (store, &iter, position,
433 COL_ACCOUNT_POINTER, account,
435 account_chooser_update_iter (chooser, &iter);
439 account_chooser_account_deleted_cb (McAccountMonitor *monitor,
440 const gchar *unique_name,
441 EmpathyAccountChooser *chooser)
445 account = mc_account_lookup (unique_name);
446 account_chooser_account_remove_foreach (account, chooser);
447 g_object_unref (account);
457 account_chooser_find_account_foreach (GtkTreeModel *model,
462 FindAccountData *data = user_data;
465 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
467 if (empathy_account_equal (account, data->account)) {
469 *(data->iter) = *iter;
470 g_object_unref (account);
475 g_object_unref (account);
481 account_chooser_find_account (EmpathyAccountChooser *chooser,
486 GtkComboBox *combobox;
487 FindAccountData data;
489 combobox = GTK_COMBO_BOX (chooser);
490 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
492 data.account = account;
494 gtk_tree_model_foreach (GTK_TREE_MODEL (store),
495 account_chooser_find_account_foreach,
502 account_chooser_account_remove_foreach (McAccount *account,
503 EmpathyAccountChooser *chooser)
506 GtkComboBox *combobox;
509 combobox = GTK_COMBO_BOX (chooser);
510 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
512 if (account_chooser_find_account (chooser, account, &iter)) {
513 gtk_list_store_remove (store, &iter);
518 account_chooser_update_iter (EmpathyAccountChooser *chooser,
521 EmpathyAccountChooserPriv *priv;
523 GtkComboBox *combobox;
525 const gchar *icon_name;
526 gboolean is_enabled = TRUE;
528 priv = GET_PRIV (chooser);
530 combobox = GTK_COMBO_BOX (chooser);
531 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
533 gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
534 COL_ACCOUNT_POINTER, &account,
537 icon_name = empathy_icon_name_from_account (account);
539 is_enabled = priv->filter (account, priv->filter_data);
542 gtk_list_store_set (store, iter,
543 COL_ACCOUNT_IMAGE, icon_name,
544 COL_ACCOUNT_TEXT, mc_account_get_display_name (account),
545 COL_ACCOUNT_ENABLED, is_enabled,
548 /* set first connected account as active account */
549 if (priv->set_active_item == FALSE && is_enabled) {
550 priv->set_active_item = TRUE;
551 gtk_combo_box_set_active_iter (combobox, iter);
554 g_object_unref (account);
558 account_chooser_status_changed_cb (MissionControl *mc,
559 TpConnectionStatus status,
561 TpConnectionStatusReason reason,
562 const gchar *unique_name,
563 EmpathyAccountChooser *chooser)
568 account = mc_account_lookup (unique_name);
569 if (account_chooser_find_account (chooser, account, &iter)) {
570 account_chooser_update_iter (chooser, &iter);
572 g_object_unref (account);
576 account_chooser_separator_func (GtkTreeModel *model,
578 EmpathyAccountChooser *chooser)
580 EmpathyAccountChooserPriv *priv;
582 gboolean is_separator;
584 priv = GET_PRIV (chooser);
586 if (!priv->has_all_option) {
590 gtk_tree_model_get (model, iter, COL_ACCOUNT_TEXT, &text, -1);
591 is_separator = text == NULL;
598 account_chooser_set_account_foreach (GtkTreeModel *model,
601 SetAccountData *data)
606 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
608 /* Special case so we can make it possible to select the All option */
609 if ((data->account == NULL) != (account == NULL)) {
612 else if (data->account == account) {
615 equal = empathy_account_equal (data->account, account);
619 g_object_unref (account);
623 GtkComboBox *combobox;
625 combobox = GTK_COMBO_BOX (data->chooser);
626 gtk_combo_box_set_active_iter (combobox, iter);
635 account_chooser_filter_foreach (GtkTreeModel *model,
640 account_chooser_update_iter (chooser, iter);
645 empathy_account_chooser_set_filter (EmpathyAccountChooser *chooser,
646 EmpathyAccountChooserFilterFunc filter,
649 EmpathyAccountChooserPriv *priv;
652 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
654 priv = GET_PRIV (chooser);
656 priv->filter = filter;
657 priv->filter_data = user_data;
659 /* Refilter existing data */
660 priv->set_active_item = FALSE;
661 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
662 gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
666 empathy_account_chooser_filter_is_connected (McAccount *account,
670 TpConnectionStatus status;
672 g_return_val_if_fail (MC_IS_ACCOUNT (account), FALSE);
674 mc = empathy_mission_control_new ();
675 status = mission_control_get_connection_status (mc, account, NULL);
678 return status == TP_CONNECTION_STATUS_CONNECTED;