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,
87 static void account_chooser_status_changed_cb (MissionControl *mc,
88 TelepathyConnectionStatus status,
90 TelepathyConnectionStatusReason reason,
91 const gchar *unique_name,
92 EmpathyAccountChooser *chooser);
93 static gboolean account_chooser_separator_func (GtkTreeModel *model,
95 EmpathyAccountChooser *chooser);
96 static gboolean account_chooser_set_account_foreach (GtkTreeModel *model,
99 SetAccountData *data);
106 G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser, GTK_TYPE_COMBO_BOX);
109 empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
111 GObjectClass *object_class = G_OBJECT_CLASS (klass);
113 object_class->finalize = account_chooser_finalize;
114 object_class->get_property = account_chooser_get_property;
115 object_class->set_property = account_chooser_set_property;
117 g_object_class_install_property (object_class,
119 g_param_spec_boolean ("has-all-option",
121 "Have a separate option in the list to mean ALL accounts",
125 g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
129 empathy_account_chooser_init (EmpathyAccountChooser *chooser)
131 EmpathyAccountChooserPriv *priv = GET_PRIV (chooser);
133 priv->set_active_item = FALSE;
135 priv->filter_data = NULL;
139 account_chooser_finalize (GObject *object)
141 EmpathyAccountChooser *chooser;
142 EmpathyAccountChooserPriv *priv;
144 chooser = EMPATHY_ACCOUNT_CHOOSER (object);
145 priv = GET_PRIV (object);
147 g_signal_handlers_disconnect_by_func (priv->monitor,
148 account_chooser_account_created_cb,
150 g_signal_handlers_disconnect_by_func (priv->monitor,
151 account_chooser_account_deleted_cb,
153 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
154 "AccountStatusChanged",
155 G_CALLBACK (account_chooser_status_changed_cb),
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 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc), "AccountStatusChanged",
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 TelepathyConnectionStatus status,
559 TelepathyConnectionStatusReason 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 && !account) {
610 else if ((data->account && !account) || (!data->account && account)) {
613 equal = empathy_account_equal (data->account, account);
614 g_object_unref (account);
618 GtkComboBox *combobox;
620 combobox = GTK_COMBO_BOX (data->chooser);
621 gtk_combo_box_set_active_iter (combobox, iter);
630 account_chooser_filter_foreach (GtkTreeModel *model,
635 account_chooser_update_iter (chooser, iter);
640 empathy_account_chooser_set_filter (EmpathyAccountChooser *chooser,
641 EmpathyAccountChooserFilterFunc filter,
644 EmpathyAccountChooserPriv *priv;
647 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
649 priv = GET_PRIV (chooser);
651 priv->filter = filter;
652 priv->filter_data = user_data;
654 /* Refilter existing data */
655 priv->set_active_item = FALSE;
656 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
657 gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
661 empathy_account_chooser_filter_is_connected (McAccount *account,
665 TelepathyConnectionStatus status;
667 g_return_val_if_fail (MC_IS_ACCOUNT (account), FALSE);
669 mc = empathy_mission_control_new ();
670 status = mission_control_get_connection_status (mc, account, NULL);
673 return status == TP_CONN_STATUS_CONNECTED;