empathy-account-chooser.c 18.1 KB
Newer Older
1 2 3
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2005-2007 Imendio AB
4
 * Copyright (C) 2007-2008 Collabora Ltd.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 * 
 * Authors: Martyn Russell <martyn@imendio.com>
22
 *          Xavier Claessens <xclaesse@gmail.com>
23 24 25 26 27 28
 */

#include "config.h"

#include <string.h>

29
#include <glib/gi18n-lib.h>
30 31 32 33 34
#include <gtk/gtk.h>
#include <glade/glade.h>

#include <libmissioncontrol/mission-control.h>

35
#include <libempathy/empathy-account-manager.h>
36
#include <libempathy/empathy-utils.h>
37

38 39
#include "empathy-ui-utils.h"
#include "empathy-account-chooser.h"
40

41
#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountChooser)
42
typedef struct {
43
	EmpathyAccountManager          *manager;
44 45 46 47
	gboolean                        set_active_item;
	gboolean                        has_all_option;
	EmpathyAccountChooserFilterFunc filter;
	gpointer                        filter_data;
48
} EmpathyAccountChooserPriv;
49 50

typedef struct {
51
	EmpathyAccountChooser *chooser;
52 53
	McAccount             *account;
	gboolean               set;
54 55 56 57 58 59 60 61 62 63
} SetAccountData;

enum {
	COL_ACCOUNT_IMAGE,
	COL_ACCOUNT_TEXT,
	COL_ACCOUNT_ENABLED, /* Usually tied to connected state */
	COL_ACCOUNT_POINTER,
	COL_ACCOUNT_COUNT
};

Xavier Claessens's avatar
Xavier Claessens committed
64 65 66 67 68 69 70 71 72 73
static void     account_chooser_finalize               (GObject                  *object);
static void     account_chooser_get_property           (GObject                  *object,
							guint                     param_id,
							GValue                   *value,
							GParamSpec               *pspec);
static void     account_chooser_set_property           (GObject                  *object,
							guint                     param_id,
							const GValue             *value,
							GParamSpec               *pspec);
static void     account_chooser_setup                  (EmpathyAccountChooser    *chooser);
74 75
static void     account_chooser_account_created_cb     (EmpathyAccountManager    *manager,
							McAccount                *account,
Xavier Claessens's avatar
Xavier Claessens committed
76 77 78
							EmpathyAccountChooser    *chooser);
static void     account_chooser_account_add_foreach    (McAccount                *account,
							EmpathyAccountChooser    *chooser);
79 80
static void     account_chooser_account_deleted_cb     (EmpathyAccountManager    *manager,
							McAccount                *account,
Xavier Claessens's avatar
Xavier Claessens committed
81 82 83 84 85
							EmpathyAccountChooser    *chooser);
static void     account_chooser_account_remove_foreach (McAccount                *account,
							EmpathyAccountChooser    *chooser);
static void     account_chooser_update_iter            (EmpathyAccountChooser    *chooser,
							GtkTreeIter              *iter);
86 87
static void     account_chooser_connection_changed_cb  (EmpathyAccountManager    *manager,
							McAccount                *account,
Xavier Claessens's avatar
Xavier Claessens committed
88
							TpConnectionStatusReason  reason,
89 90
							TpConnectionStatus        new_status,
							TpConnectionStatus        old_status,
Xavier Claessens's avatar
Xavier Claessens committed
91 92 93 94 95 96 97 98
							EmpathyAccountChooser    *chooser);
static gboolean account_chooser_separator_func         (GtkTreeModel             *model,
							GtkTreeIter              *iter,
							EmpathyAccountChooser    *chooser);
static gboolean account_chooser_set_account_foreach    (GtkTreeModel             *model,
							GtkTreePath              *path,
							GtkTreeIter              *iter,
							SetAccountData           *data);
99 100 101 102 103 104

enum {
	PROP_0,
	PROP_HAS_ALL_OPTION,
};

105
G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser, GTK_TYPE_COMBO_BOX);
106 107

static void
108
empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->finalize = account_chooser_finalize;
	object_class->get_property = account_chooser_get_property;
	object_class->set_property = account_chooser_set_property;

	g_object_class_install_property (object_class,
					 PROP_HAS_ALL_OPTION,
					 g_param_spec_boolean ("has-all-option",
							       "Has All Option",
							       "Have a separate option in the list to mean ALL accounts",
							       FALSE,
							       G_PARAM_READWRITE));

124
	g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
125 126 127
}

static void
128
empathy_account_chooser_init (EmpathyAccountChooser *chooser)
129
{
130 131
	EmpathyAccountChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
		EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv);
132

133
	chooser->priv = priv;
134 135 136
	priv->set_active_item = FALSE;
	priv->filter = NULL;
	priv->filter_data = NULL;
137

138
	priv->manager = empathy_account_manager_dup_singleton ();
139 140 141 142 143 144 145 146 147 148 149 150

	g_signal_connect (priv->manager, "account-created",
			  G_CALLBACK (account_chooser_account_created_cb),
			  chooser);
	g_signal_connect (priv->manager, "account-deleted",
			  G_CALLBACK (account_chooser_account_deleted_cb),
			  chooser);
	g_signal_connect (priv->manager, "account-connection-changed",
			  G_CALLBACK (account_chooser_connection_changed_cb),
			  chooser);

	account_chooser_setup (EMPATHY_ACCOUNT_CHOOSER (chooser));
151 152 153 154 155
}

static void
account_chooser_finalize (GObject *object)
{
156
	EmpathyAccountChooserPriv *priv = GET_PRIV (object);
157

158 159 160
	g_signal_handlers_disconnect_by_func (priv->manager,
					      account_chooser_connection_changed_cb,
					      object);
161 162 163 164 165 166
	g_signal_handlers_disconnect_by_func (priv->manager,
					      account_chooser_account_created_cb,
					      object);
	g_signal_handlers_disconnect_by_func (priv->manager,
					      account_chooser_account_deleted_cb,
					      object);
167
	g_object_unref (priv->manager);
168

169
	G_OBJECT_CLASS (empathy_account_chooser_parent_class)->finalize (object);
170 171 172 173 174 175 176 177
}

static void
account_chooser_get_property (GObject    *object,
			      guint       param_id,
			      GValue     *value,
			      GParamSpec *pspec)
{
178
	EmpathyAccountChooserPriv *priv;
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

	priv = GET_PRIV (object);

	switch (param_id) {
	case PROP_HAS_ALL_OPTION:
		g_value_set_boolean (value, priv->has_all_option);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	};
}

static void
account_chooser_set_property (GObject      *object,
			      guint         param_id,
			      const GValue *value,
			      GParamSpec   *pspec)
{
198
	EmpathyAccountChooserPriv *priv;
199 200 201 202 203

	priv = GET_PRIV (object);

	switch (param_id) {
	case PROP_HAS_ALL_OPTION:
204
		empathy_account_chooser_set_has_all_option (EMPATHY_ACCOUNT_CHOOSER (object),
205 206 207 208 209 210 211 212 213
							   g_value_get_boolean (value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	};
}

GtkWidget *
214
empathy_account_chooser_new (void)
215 216 217
{
	GtkWidget                *chooser;

218
	chooser = g_object_new (EMPATHY_TYPE_ACCOUNT_CHOOSER, NULL);
219 220 221 222 223

	return chooser;
}

McAccount *
224
empathy_account_chooser_get_account (EmpathyAccountChooser *chooser)
225
{
226
	EmpathyAccountChooserPriv *priv;
227 228 229 230
	McAccount                *account;
	GtkTreeModel             *model;
	GtkTreeIter               iter;

231
	g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
232 233 234 235

	priv = GET_PRIV (chooser);

	model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
236 237 238
	if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (chooser), &iter)) {
		return NULL;
	}
239 240 241 242 243 244 245

	gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);

	return account;
}

gboolean
246
empathy_account_chooser_set_account (EmpathyAccountChooser *chooser,
247
				     McAccount             *account)
248 249 250 251 252 253
{
	GtkComboBox    *combobox;
	GtkTreeModel   *model;
	GtkTreeIter     iter;
	SetAccountData  data;

254
	g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270

	combobox = GTK_COMBO_BOX (chooser);
	model = gtk_combo_box_get_model (combobox);
	gtk_combo_box_get_active_iter (combobox, &iter);

	data.chooser = chooser;
	data.account = account;

	gtk_tree_model_foreach (model,
				(GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
				&data);

	return data.set;
}

gboolean
271
empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *chooser)
272
{
273
	EmpathyAccountChooserPriv *priv;
274

275
	g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
276 277 278 279 280 281 282

	priv = GET_PRIV (chooser);
	
	return priv->has_all_option;
}

void
283
empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *chooser,
284 285
					   gboolean              has_all_option)
{
286
	EmpathyAccountChooserPriv *priv;
287 288 289 290 291
	GtkComboBox              *combobox;
	GtkListStore             *store;
	GtkTreeModel             *model;
	GtkTreeIter               iter;

292
	g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347

	priv = GET_PRIV (chooser);

	if (priv->has_all_option == has_all_option) {
		return;
	}

	combobox = GTK_COMBO_BOX (chooser);
	model = gtk_combo_box_get_model (combobox);
	store = GTK_LIST_STORE (model);

	priv->has_all_option = has_all_option;

	/*
	 * The first 2 options are the ALL and separator
	 */

	if (has_all_option) {
		gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser), 
						      (GtkTreeViewRowSeparatorFunc)
						      account_chooser_separator_func,
						      chooser, 
						      NULL);

		gtk_list_store_prepend (store, &iter);
		gtk_list_store_set (store, &iter, 
				    COL_ACCOUNT_TEXT, NULL,
				    COL_ACCOUNT_ENABLED, TRUE,
				    COL_ACCOUNT_POINTER, NULL,
				    -1);

		gtk_list_store_prepend (store, &iter);
		gtk_list_store_set (store, &iter, 
				    COL_ACCOUNT_TEXT, _("All"), 
				    COL_ACCOUNT_ENABLED, TRUE,
				    COL_ACCOUNT_POINTER, NULL,
				    -1);
	} else {
		if (gtk_tree_model_get_iter_first (model, &iter)) {
			if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter)) {
				gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
			}
		}

		gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser), 
						      (GtkTreeViewRowSeparatorFunc)
						      NULL,
						      NULL, 
						      NULL);
	}

	g_object_notify (G_OBJECT (chooser), "has-all-option");
}

static void
348
account_chooser_setup (EmpathyAccountChooser *chooser)
349
{
350
	EmpathyAccountChooserPriv *priv;
351 352 353 354 355 356 357 358 359 360 361 362 363
	GList                    *accounts;
	GtkListStore             *store;
	GtkCellRenderer          *renderer;
	GtkComboBox              *combobox;

	priv = GET_PRIV (chooser);

	/* Set up combo box with new store */
	combobox = GTK_COMBO_BOX (chooser);

	gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));

	store = gtk_list_store_new (COL_ACCOUNT_COUNT,
364
				    G_TYPE_STRING,    /* Image */
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
				    G_TYPE_STRING,    /* Name */
				    G_TYPE_BOOLEAN,   /* Enabled */
				    MC_TYPE_ACCOUNT);

	gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));

	renderer = gtk_cell_renderer_pixbuf_new ();
	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
	gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
					"icon-name", COL_ACCOUNT_IMAGE,
					"sensitive", COL_ACCOUNT_ENABLED,
					NULL);
	g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);

	renderer = gtk_cell_renderer_text_new ();
	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
	gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
					"text", COL_ACCOUNT_TEXT,
					"sensitive", COL_ACCOUNT_ENABLED,
					NULL);

	/* Populate accounts */
	accounts = mc_accounts_list ();
	g_list_foreach (accounts,
			(GFunc) account_chooser_account_add_foreach,
			chooser);

	mc_accounts_list_free (accounts);
	g_object_unref (store);
}

static void
397 398
account_chooser_account_created_cb (EmpathyAccountManager *manager,
				    McAccount             *account,
399
				    EmpathyAccountChooser *chooser)
400 401 402 403 404
{
	account_chooser_account_add_foreach (account, chooser);
}

static void
405
account_chooser_account_add_foreach (McAccount             *account,
406
				     EmpathyAccountChooser *chooser)
407
{
408 409 410 411
	GtkListStore *store;
	GtkComboBox  *combobox;
	GtkTreeIter   iter;
	gint          position;
412 413 414 415

	combobox = GTK_COMBO_BOX (chooser);
	store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));

416 417 418 419 420
	position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
	gtk_list_store_insert_with_values (store, &iter, position,
					   COL_ACCOUNT_POINTER, account,
					   -1);
	account_chooser_update_iter (chooser, &iter);
421 422 423
}

static void
424 425
account_chooser_account_deleted_cb (EmpathyAccountManager *manager,
				    McAccount             *account,
426
				    EmpathyAccountChooser *chooser)
427 428 429 430
{
	account_chooser_account_remove_foreach (account, chooser);
}

431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
typedef struct {
	McAccount   *account;
	GtkTreeIter *iter;
	gboolean     found;
} FindAccountData;

static gboolean
account_chooser_find_account_foreach (GtkTreeModel *model,
				      GtkTreePath  *path,
				      GtkTreeIter  *iter,
				      gpointer      user_data)
{
	FindAccountData *data = user_data;
	McAccount       *account;

	gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);

	if (empathy_account_equal (account, data->account)) {
		data->found = TRUE;
		*(data->iter) = *iter;
		g_object_unref (account);

		return TRUE;
	}

	g_object_unref (account);

	return FALSE;
}

static gboolean
account_chooser_find_account (EmpathyAccountChooser *chooser,
			      McAccount             *account,
			      GtkTreeIter           *iter)
{
	GtkListStore    *store;
	GtkComboBox     *combobox;
	FindAccountData  data;

	combobox = GTK_COMBO_BOX (chooser);
	store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));

	data.account = account;
	data.iter = iter;
	gtk_tree_model_foreach (GTK_TREE_MODEL (store),
				account_chooser_find_account_foreach,
				&data);

	return data.found;
}

482 483
static void
account_chooser_account_remove_foreach (McAccount            *account,
484
					EmpathyAccountChooser *chooser)
485
{
486 487 488 489 490 491 492 493 494 495
	GtkListStore *store;
	GtkComboBox  *combobox;
	GtkTreeIter   iter;

	combobox = GTK_COMBO_BOX (chooser);
	store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));

	if (account_chooser_find_account (chooser, account, &iter)) {
		gtk_list_store_remove (store, &iter);
	}
496 497 498
}

static void
499
account_chooser_update_iter (EmpathyAccountChooser *chooser,
500
			     GtkTreeIter           *iter)
501
{
502
	EmpathyAccountChooserPriv *priv;
503 504
	GtkListStore              *store;
	GtkComboBox               *combobox;
505
	McAccount                 *account;
506 507
	const gchar               *icon_name;
	gboolean                   is_enabled = TRUE;
508 509 510 511 512 513

	priv = GET_PRIV (chooser);

	combobox = GTK_COMBO_BOX (chooser);
	store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));

514 515 516 517
	gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
			    COL_ACCOUNT_POINTER, &account,
			    -1);

518
	icon_name = empathy_icon_name_from_account (account);
519 520
	if (priv->filter) {
		is_enabled = priv->filter (account, priv->filter_data);
521 522 523 524 525 526 527 528 529 530 531 532 533
	}

	gtk_list_store_set (store, iter,
			    COL_ACCOUNT_IMAGE, icon_name,
			    COL_ACCOUNT_TEXT, mc_account_get_display_name (account),
			    COL_ACCOUNT_ENABLED, is_enabled,
			    -1);

	/* set first connected account as active account */
	if (priv->set_active_item == FALSE && is_enabled) {
		priv->set_active_item = TRUE;
		gtk_combo_box_set_active_iter (combobox, iter);
	}
534 535

	g_object_unref (account);
536 537 538
}

static void
539 540 541 542 543 544
account_chooser_connection_changed_cb (EmpathyAccountManager   *manager,
				       McAccount               *account,
				       TpConnectionStatusReason reason,
				       TpConnectionStatus       new_status,
				       TpConnectionStatus       old_status,
				       EmpathyAccountChooser   *chooser)
545
{
546
	GtkTreeIter iter;
547 548 549 550

	if (account_chooser_find_account (chooser, account, &iter)) {
		account_chooser_update_iter (chooser, &iter);
	}
551 552 553 554 555
}

static gboolean
account_chooser_separator_func (GtkTreeModel         *model,
				GtkTreeIter          *iter,
556
				EmpathyAccountChooser *chooser)
557
{
558
	EmpathyAccountChooserPriv *priv;
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
	gchar                    *text;
	gboolean                  is_separator;

	priv = GET_PRIV (chooser);
	
	if (!priv->has_all_option) {
		return FALSE;
	}
	
	gtk_tree_model_get (model, iter, COL_ACCOUNT_TEXT, &text, -1);
	is_separator = text == NULL;
	g_free (text);

	return is_separator;
}

static gboolean
account_chooser_set_account_foreach (GtkTreeModel   *model,
				     GtkTreePath    *path,
				     GtkTreeIter    *iter,
				     SetAccountData *data)
{
	McAccount *account;
	gboolean   equal;

	gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);

	/* Special case so we can make it possible to select the All option */
587
	if ((data->account == NULL) != (account == NULL)) {
588
		equal = FALSE;
589 590 591
	}
	else if (data->account == account) {
		equal = TRUE;
592
	} else {
593
		equal = empathy_account_equal (data->account, account);
594 595 596
	}

	if (account) {
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
		g_object_unref (account);
	}

	if (equal) {
		GtkComboBox *combobox;

		combobox = GTK_COMBO_BOX (data->chooser);
		gtk_combo_box_set_active_iter (combobox, iter);

		data->set = TRUE;
	}

	return equal;
}

static gboolean
613 614 615 616
account_chooser_filter_foreach (GtkTreeModel *model,
				GtkTreePath  *path,
				GtkTreeIter  *iter,
				gpointer      chooser)
617
{
618
	account_chooser_update_iter (chooser, iter);
619 620 621
	return FALSE;
}

622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
void
empathy_account_chooser_set_filter (EmpathyAccountChooser           *chooser,
                                    EmpathyAccountChooserFilterFunc  filter,
                                    gpointer                         user_data)
{
	EmpathyAccountChooserPriv *priv;
	GtkTreeModel *model;

	g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));

	priv = GET_PRIV (chooser);

	priv->filter = filter;
	priv->filter_data = user_data;

	/* Refilter existing data */
	priv->set_active_item = FALSE;
	model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
	gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
}

gboolean
empathy_account_chooser_filter_is_connected (McAccount *account,
					     gpointer   user_data)
{
Xavier Claessens's avatar
Xavier Claessens committed
647 648
	MissionControl     *mc;
	TpConnectionStatus  status;
649 650 651

	g_return_val_if_fail (MC_IS_ACCOUNT (account), FALSE);

652
	mc = empathy_mission_control_dup_singleton ();
653
	status = mission_control_get_connection_status (mc, account, NULL);
654 655
	g_object_unref (mc);

Xavier Claessens's avatar
Xavier Claessens committed
656
	return status == TP_CONNECTION_STATUS_CONNECTED;
657
}
658