1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2005-2007 Imendio AB
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Martyn Russell <martyn@imendio.com>
27 #include <glib/gi18n.h>
29 #include <glade/glade.h>
31 #include <libtelepathy/tp-conn.h>
32 #include <libmissioncontrol/mc-account-monitor.h>
33 #include <libmissioncontrol/mission-control.h>
35 #include <libempathy/empathy-utils.h>
37 #include "empathy-ui-utils.h"
38 #include "empathy-account-chooser.h"
40 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv))
44 McAccountMonitor *monitor;
45 gboolean set_active_item;
46 gboolean has_all_option;
47 EmpathyAccountChooserFilterFunc filter;
49 } EmpathyAccountChooserPriv;
52 EmpathyAccountChooser *chooser;
60 COL_ACCOUNT_ENABLED, /* Usually tied to connected state */
65 static void account_chooser_finalize (GObject *object);
66 static void account_chooser_get_property (GObject *object,
70 static void account_chooser_set_property (GObject *object,
74 static void account_chooser_setup (EmpathyAccountChooser *chooser);
75 static void account_chooser_account_created_cb (McAccountMonitor *monitor,
76 const gchar *unique_name,
77 EmpathyAccountChooser *chooser);
78 static void account_chooser_account_add_foreach (McAccount *account,
79 EmpathyAccountChooser *chooser);
80 static void account_chooser_account_deleted_cb (McAccountMonitor *monitor,
81 const gchar *unique_name,
82 EmpathyAccountChooser *chooser);
83 static void account_chooser_account_remove_foreach (McAccount *account,
84 EmpathyAccountChooser *chooser);
85 static void account_chooser_update_iter (EmpathyAccountChooser *chooser,
88 static void account_chooser_status_changed_cb (MissionControl *mc,
89 TelepathyConnectionStatus status,
91 TelepathyConnectionStatusReason 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,
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)
422 EmpathyAccountChooserPriv *priv;
424 GtkComboBox *combobox;
427 priv = GET_PRIV (chooser);
429 combobox = GTK_COMBO_BOX (chooser);
430 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
432 gtk_list_store_append (store, &iter);
433 account_chooser_update_iter (chooser, &iter, account);
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);
449 account_chooser_account_remove_foreach (McAccount *account,
450 EmpathyAccountChooser *chooser)
456 account_chooser_update_iter (EmpathyAccountChooser *chooser,
460 EmpathyAccountChooserPriv *priv;
462 GtkComboBox *combobox;
463 const gchar *icon_name;
464 gboolean is_enabled = TRUE;
466 priv = GET_PRIV (chooser);
468 combobox = GTK_COMBO_BOX (chooser);
469 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
471 icon_name = empathy_icon_name_from_account (account);
473 is_enabled = priv->filter (account, priv->filter_data);
476 gtk_list_store_set (store, iter,
477 COL_ACCOUNT_IMAGE, icon_name,
478 COL_ACCOUNT_TEXT, mc_account_get_display_name (account),
479 COL_ACCOUNT_ENABLED, is_enabled,
480 COL_ACCOUNT_POINTER, account,
483 /* set first connected account as active account */
484 if (priv->set_active_item == FALSE && is_enabled) {
485 priv->set_active_item = TRUE;
486 gtk_combo_box_set_active_iter (combobox, iter);
491 account_chooser_status_changed_cb (MissionControl *mc,
492 TelepathyConnectionStatus status,
494 TelepathyConnectionStatusReason reason,
495 const gchar *unique_name,
496 EmpathyAccountChooser *chooser)
498 /* FIXME: implement */
502 account_chooser_separator_func (GtkTreeModel *model,
504 EmpathyAccountChooser *chooser)
506 EmpathyAccountChooserPriv *priv;
508 gboolean is_separator;
510 priv = GET_PRIV (chooser);
512 if (!priv->has_all_option) {
516 gtk_tree_model_get (model, iter, COL_ACCOUNT_TEXT, &text, -1);
517 is_separator = text == NULL;
524 account_chooser_set_account_foreach (GtkTreeModel *model,
527 SetAccountData *data)
532 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
534 /* Special case so we can make it possible to select the All option */
535 if (!data->account && !account) {
538 else if ((data->account && !account) || (!data->account && account)) {
541 equal = empathy_account_equal (data->account, account);
542 g_object_unref (account);
546 GtkComboBox *combobox;
548 combobox = GTK_COMBO_BOX (data->chooser);
549 gtk_combo_box_set_active_iter (combobox, iter);
558 account_chooser_filter_foreach (GtkTreeModel *model,
563 EmpathyAccountChooserPriv *priv;
565 gboolean is_enabled = TRUE;
567 priv = GET_PRIV (chooser);
569 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
572 is_enabled = priv->filter (account, priv->filter_data);
575 gtk_list_store_set (GTK_LIST_STORE (model), iter,
576 COL_ACCOUNT_ENABLED, is_enabled,
579 /* set first connected account as active account */
580 if (!priv->set_active_item && is_enabled) {
581 priv->set_active_item = TRUE;
582 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), iter);
585 g_object_unref (account);
591 empathy_account_chooser_set_filter (EmpathyAccountChooser *chooser,
592 EmpathyAccountChooserFilterFunc filter,
595 EmpathyAccountChooserPriv *priv;
598 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
600 priv = GET_PRIV (chooser);
602 priv->filter = filter;
603 priv->filter_data = user_data;
605 /* Refilter existing data */
606 priv->set_active_item = FALSE;
607 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
608 gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
612 empathy_account_chooser_filter_is_connected (McAccount *account,
618 g_return_val_if_fail (MC_IS_ACCOUNT (account), FALSE);
620 mc = empathy_mission_control_new ();
621 tp_conn = mission_control_get_connection (mc, account, NULL);
624 if (tp_conn == NULL) {
628 g_object_unref (tp_conn);