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 <libtelepathy/tp-conn.h>
34 #include <libmissioncontrol/mc-account-monitor.h>
35 #include <libmissioncontrol/mission-control.h>
37 #include <libempathy/empathy-utils.h>
39 #include "empathy-ui-utils.h"
40 #include "empathy-account-chooser.h"
42 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv))
46 McAccountMonitor *monitor;
47 gboolean set_active_item;
48 gboolean has_all_option;
49 EmpathyAccountChooserFilterFunc filter;
51 } EmpathyAccountChooserPriv;
54 EmpathyAccountChooser *chooser;
62 COL_ACCOUNT_ENABLED, /* Usually tied to connected state */
67 static void account_chooser_finalize (GObject *object);
68 static void account_chooser_get_property (GObject *object,
72 static void account_chooser_set_property (GObject *object,
76 static void account_chooser_setup (EmpathyAccountChooser *chooser);
77 static void account_chooser_account_created_cb (McAccountMonitor *monitor,
78 const gchar *unique_name,
79 EmpathyAccountChooser *chooser);
80 static void account_chooser_account_add_foreach (McAccount *account,
81 EmpathyAccountChooser *chooser);
82 static void account_chooser_account_deleted_cb (McAccountMonitor *monitor,
83 const gchar *unique_name,
84 EmpathyAccountChooser *chooser);
85 static void account_chooser_account_remove_foreach (McAccount *account,
86 EmpathyAccountChooser *chooser);
87 static void account_chooser_update_iter (EmpathyAccountChooser *chooser,
89 static void account_chooser_status_changed_cb (MissionControl *mc,
90 TpConnectionStatus status,
92 TpConnectionStatusReason reason,
93 const gchar *unique_name,
94 EmpathyAccountChooser *chooser);
95 static gboolean account_chooser_separator_func (GtkTreeModel *model,
97 EmpathyAccountChooser *chooser);
98 static gboolean account_chooser_set_account_foreach (GtkTreeModel *model,
101 SetAccountData *data);
108 G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser, GTK_TYPE_COMBO_BOX);
111 empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
113 GObjectClass *object_class = G_OBJECT_CLASS (klass);
115 object_class->finalize = account_chooser_finalize;
116 object_class->get_property = account_chooser_get_property;
117 object_class->set_property = account_chooser_set_property;
119 g_object_class_install_property (object_class,
121 g_param_spec_boolean ("has-all-option",
123 "Have a separate option in the list to mean ALL accounts",
127 g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
131 empathy_account_chooser_init (EmpathyAccountChooser *chooser)
133 EmpathyAccountChooserPriv *priv = GET_PRIV (chooser);
135 priv->set_active_item = FALSE;
137 priv->filter_data = NULL;
141 account_chooser_finalize (GObject *object)
143 EmpathyAccountChooser *chooser;
144 EmpathyAccountChooserPriv *priv;
146 chooser = EMPATHY_ACCOUNT_CHOOSER (object);
147 priv = GET_PRIV (object);
149 g_signal_handlers_disconnect_by_func (priv->monitor,
150 account_chooser_account_created_cb,
152 g_signal_handlers_disconnect_by_func (priv->monitor,
153 account_chooser_account_deleted_cb,
155 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
156 "AccountStatusChanged",
157 G_CALLBACK (account_chooser_status_changed_cb),
159 g_object_unref (priv->mc);
160 g_object_unref (priv->monitor);
162 G_OBJECT_CLASS (empathy_account_chooser_parent_class)->finalize (object);
166 account_chooser_get_property (GObject *object,
171 EmpathyAccountChooserPriv *priv;
173 priv = GET_PRIV (object);
176 case PROP_HAS_ALL_OPTION:
177 g_value_set_boolean (value, priv->has_all_option);
180 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
186 account_chooser_set_property (GObject *object,
191 EmpathyAccountChooserPriv *priv;
193 priv = GET_PRIV (object);
196 case PROP_HAS_ALL_OPTION:
197 empathy_account_chooser_set_has_all_option (EMPATHY_ACCOUNT_CHOOSER (object),
198 g_value_get_boolean (value));
201 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
207 empathy_account_chooser_new (void)
209 EmpathyAccountChooserPriv *priv;
210 McAccountMonitor *monitor;
213 monitor = mc_account_monitor_new ();
214 chooser = g_object_new (EMPATHY_TYPE_ACCOUNT_CHOOSER, NULL);
216 priv = GET_PRIV (chooser);
218 priv->mc = empathy_mission_control_new ();
219 priv->monitor = mc_account_monitor_new ();
221 g_signal_connect (priv->monitor, "account-created",
222 G_CALLBACK (account_chooser_account_created_cb),
224 g_signal_connect (priv->monitor, "account-deleted",
225 G_CALLBACK (account_chooser_account_deleted_cb),
227 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc), "AccountStatusChanged",
228 G_CALLBACK (account_chooser_status_changed_cb),
231 account_chooser_setup (EMPATHY_ACCOUNT_CHOOSER (chooser));
237 empathy_account_chooser_get_account (EmpathyAccountChooser *chooser)
239 EmpathyAccountChooserPriv *priv;
244 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
246 priv = GET_PRIV (chooser);
248 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
249 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 && !account) {
612 else if ((data->account && !account) || (!data->account && account)) {
615 equal = empathy_account_equal (data->account, account);
616 g_object_unref (account);
620 GtkComboBox *combobox;
622 combobox = GTK_COMBO_BOX (data->chooser);
623 gtk_combo_box_set_active_iter (combobox, iter);
632 account_chooser_filter_foreach (GtkTreeModel *model,
637 account_chooser_update_iter (chooser, iter);
642 empathy_account_chooser_set_filter (EmpathyAccountChooser *chooser,
643 EmpathyAccountChooserFilterFunc filter,
646 EmpathyAccountChooserPriv *priv;
649 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
651 priv = GET_PRIV (chooser);
653 priv->filter = filter;
654 priv->filter_data = user_data;
656 /* Refilter existing data */
657 priv->set_active_item = FALSE;
658 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
659 gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
663 empathy_account_chooser_filter_is_connected (McAccount *account,
667 TpConnectionStatus status;
669 g_return_val_if_fail (MC_IS_ACCOUNT (account), FALSE);
671 mc = empathy_mission_control_new ();
672 status = mission_control_get_connection_status (mc, account, NULL);
675 return status == TP_CONNECTION_STATUS_CONNECTED;