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) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv))
45 McAccountMonitor *monitor;
46 gboolean set_active_item;
47 gboolean has_all_option;
48 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 empathy_disconnect_account_status_changed (priv->token);
156 g_object_unref (priv->mc);
157 g_object_unref (priv->monitor);
159 G_OBJECT_CLASS (empathy_account_chooser_parent_class)->finalize (object);
163 account_chooser_get_property (GObject *object,
168 EmpathyAccountChooserPriv *priv;
170 priv = GET_PRIV (object);
173 case PROP_HAS_ALL_OPTION:
174 g_value_set_boolean (value, priv->has_all_option);
177 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
183 account_chooser_set_property (GObject *object,
188 EmpathyAccountChooserPriv *priv;
190 priv = GET_PRIV (object);
193 case PROP_HAS_ALL_OPTION:
194 empathy_account_chooser_set_has_all_option (EMPATHY_ACCOUNT_CHOOSER (object),
195 g_value_get_boolean (value));
198 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
204 empathy_account_chooser_new (void)
206 EmpathyAccountChooserPriv *priv;
207 McAccountMonitor *monitor;
210 monitor = mc_account_monitor_new ();
211 chooser = g_object_new (EMPATHY_TYPE_ACCOUNT_CHOOSER, NULL);
213 priv = GET_PRIV (chooser);
215 priv->mc = empathy_mission_control_new ();
216 priv->monitor = mc_account_monitor_new ();
218 g_signal_connect (priv->monitor, "account-created",
219 G_CALLBACK (account_chooser_account_created_cb),
221 g_signal_connect (priv->monitor, "account-deleted",
222 G_CALLBACK (account_chooser_account_deleted_cb),
224 priv->token = empathy_connect_to_account_status_changed (priv->mc,
225 G_CALLBACK (account_chooser_status_changed_cb),
228 account_chooser_setup (EMPATHY_ACCOUNT_CHOOSER (chooser));
234 empathy_account_chooser_get_account (EmpathyAccountChooser *chooser)
236 EmpathyAccountChooserPriv *priv;
241 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
243 priv = GET_PRIV (chooser);
245 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
246 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (chooser), &iter);
248 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
254 empathy_account_chooser_set_account (EmpathyAccountChooser *chooser,
257 GtkComboBox *combobox;
262 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
264 combobox = GTK_COMBO_BOX (chooser);
265 model = gtk_combo_box_get_model (combobox);
266 gtk_combo_box_get_active_iter (combobox, &iter);
268 data.chooser = chooser;
269 data.account = account;
271 gtk_tree_model_foreach (model,
272 (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
279 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *chooser)
281 EmpathyAccountChooserPriv *priv;
283 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
285 priv = GET_PRIV (chooser);
287 return priv->has_all_option;
291 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *chooser,
292 gboolean has_all_option)
294 EmpathyAccountChooserPriv *priv;
295 GtkComboBox *combobox;
300 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
302 priv = GET_PRIV (chooser);
304 if (priv->has_all_option == has_all_option) {
308 combobox = GTK_COMBO_BOX (chooser);
309 model = gtk_combo_box_get_model (combobox);
310 store = GTK_LIST_STORE (model);
312 priv->has_all_option = has_all_option;
315 * The first 2 options are the ALL and separator
318 if (has_all_option) {
319 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
320 (GtkTreeViewRowSeparatorFunc)
321 account_chooser_separator_func,
325 gtk_list_store_prepend (store, &iter);
326 gtk_list_store_set (store, &iter,
327 COL_ACCOUNT_TEXT, NULL,
328 COL_ACCOUNT_ENABLED, TRUE,
329 COL_ACCOUNT_POINTER, NULL,
332 gtk_list_store_prepend (store, &iter);
333 gtk_list_store_set (store, &iter,
334 COL_ACCOUNT_TEXT, _("All"),
335 COL_ACCOUNT_ENABLED, TRUE,
336 COL_ACCOUNT_POINTER, NULL,
339 if (gtk_tree_model_get_iter_first (model, &iter)) {
340 if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter)) {
341 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
345 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
346 (GtkTreeViewRowSeparatorFunc)
352 g_object_notify (G_OBJECT (chooser), "has-all-option");
356 account_chooser_setup (EmpathyAccountChooser *chooser)
358 EmpathyAccountChooserPriv *priv;
361 GtkCellRenderer *renderer;
362 GtkComboBox *combobox;
364 priv = GET_PRIV (chooser);
366 /* Set up combo box with new store */
367 combobox = GTK_COMBO_BOX (chooser);
369 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
371 store = gtk_list_store_new (COL_ACCOUNT_COUNT,
372 G_TYPE_STRING, /* Image */
373 G_TYPE_STRING, /* Name */
374 G_TYPE_BOOLEAN, /* Enabled */
377 gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
379 renderer = gtk_cell_renderer_pixbuf_new ();
380 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
381 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
382 "icon-name", COL_ACCOUNT_IMAGE,
383 "sensitive", COL_ACCOUNT_ENABLED,
385 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
387 renderer = gtk_cell_renderer_text_new ();
388 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
389 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
390 "text", COL_ACCOUNT_TEXT,
391 "sensitive", COL_ACCOUNT_ENABLED,
394 /* Populate accounts */
395 accounts = mc_accounts_list ();
396 g_list_foreach (accounts,
397 (GFunc) account_chooser_account_add_foreach,
400 mc_accounts_list_free (accounts);
401 g_object_unref (store);
405 account_chooser_account_created_cb (McAccountMonitor *monitor,
406 const gchar *unique_name,
407 EmpathyAccountChooser *chooser)
411 account = mc_account_lookup (unique_name);
412 account_chooser_account_add_foreach (account, chooser);
413 g_object_unref (account);
417 account_chooser_account_add_foreach (McAccount *account,
418 EmpathyAccountChooser *chooser)
421 GtkComboBox *combobox;
425 combobox = GTK_COMBO_BOX (chooser);
426 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
428 position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
429 gtk_list_store_insert_with_values (store, &iter, position,
430 COL_ACCOUNT_POINTER, account,
432 account_chooser_update_iter (chooser, &iter);
436 account_chooser_account_deleted_cb (McAccountMonitor *monitor,
437 const gchar *unique_name,
438 EmpathyAccountChooser *chooser)
442 account = mc_account_lookup (unique_name);
443 account_chooser_account_remove_foreach (account, chooser);
444 g_object_unref (account);
454 account_chooser_find_account_foreach (GtkTreeModel *model,
459 FindAccountData *data = user_data;
462 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
464 if (empathy_account_equal (account, data->account)) {
466 *(data->iter) = *iter;
467 g_object_unref (account);
472 g_object_unref (account);
478 account_chooser_find_account (EmpathyAccountChooser *chooser,
483 GtkComboBox *combobox;
484 FindAccountData data;
486 combobox = GTK_COMBO_BOX (chooser);
487 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
489 data.account = account;
491 gtk_tree_model_foreach (GTK_TREE_MODEL (store),
492 account_chooser_find_account_foreach,
499 account_chooser_account_remove_foreach (McAccount *account,
500 EmpathyAccountChooser *chooser)
503 GtkComboBox *combobox;
506 combobox = GTK_COMBO_BOX (chooser);
507 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
509 if (account_chooser_find_account (chooser, account, &iter)) {
510 gtk_list_store_remove (store, &iter);
515 account_chooser_update_iter (EmpathyAccountChooser *chooser,
518 EmpathyAccountChooserPriv *priv;
520 GtkComboBox *combobox;
522 const gchar *icon_name;
523 gboolean is_enabled = TRUE;
525 priv = GET_PRIV (chooser);
527 combobox = GTK_COMBO_BOX (chooser);
528 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
530 gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
531 COL_ACCOUNT_POINTER, &account,
534 icon_name = empathy_icon_name_from_account (account);
536 is_enabled = priv->filter (account, priv->filter_data);
539 gtk_list_store_set (store, iter,
540 COL_ACCOUNT_IMAGE, icon_name,
541 COL_ACCOUNT_TEXT, mc_account_get_display_name (account),
542 COL_ACCOUNT_ENABLED, is_enabled,
545 /* set first connected account as active account */
546 if (priv->set_active_item == FALSE && is_enabled) {
547 priv->set_active_item = TRUE;
548 gtk_combo_box_set_active_iter (combobox, iter);
551 g_object_unref (account);
555 account_chooser_status_changed_cb (MissionControl *mc,
556 TpConnectionStatus status,
558 TpConnectionStatusReason reason,
559 const gchar *unique_name,
560 EmpathyAccountChooser *chooser)
565 account = mc_account_lookup (unique_name);
566 if (account_chooser_find_account (chooser, account, &iter)) {
567 account_chooser_update_iter (chooser, &iter);
569 g_object_unref (account);
573 account_chooser_separator_func (GtkTreeModel *model,
575 EmpathyAccountChooser *chooser)
577 EmpathyAccountChooserPriv *priv;
579 gboolean is_separator;
581 priv = GET_PRIV (chooser);
583 if (!priv->has_all_option) {
587 gtk_tree_model_get (model, iter, COL_ACCOUNT_TEXT, &text, -1);
588 is_separator = text == NULL;
595 account_chooser_set_account_foreach (GtkTreeModel *model,
598 SetAccountData *data)
603 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
605 /* Special case so we can make it possible to select the All option */
606 if ((data->account == NULL) != (account == NULL)) {
609 else if (data->account == account) {
612 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;