empathy-presence-chooser.c 32.3 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) 2009 Collabora Ltd.
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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
18 19
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
20 21 22
 *
 * Authors: Richard Hult <richard@imendio.com>
 *          Martyn Russell <martyn@imendio.com>
23
 *          Xavier Claessens <xclaesse@gmail.com>
24
 *          Danielle Madeley <danielle.madeley@collabora.co.uk>
25 26 27
 */

#include "config.h"
28
#include "empathy-presence-chooser.h"
29

30
#include <glib/gi18n-lib.h>
31
#include <tp-account-widgets/tpaw-utils.h>
32

33 34
#include "empathy-presence-manager.h"
#include "empathy-status-presets.h"
35
#include "empathy-utils.h"
36

Davyd Madeley's avatar
Davyd Madeley committed
37
#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
38
#include "empathy-debug.h"
39

40 41
#include "empathy-ui-utils.h"
#include "empathy-presence-chooser.h"
42
#include "empathy-status-preset-dialog.h"
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
/**
 * SECTION:empathy-presence-chooser
 * @title:EmpathyPresenceChooser
 * @short_description: A widget used to change presence
 * @include: libempathy-gtk/empathy-presence-chooser.h
 *
 * #EmpathyPresenceChooser is a widget which extends #GtkComboBoxEntry
 * to change presence.
 */

/**
 * EmpathyAccountChooser:
 * @parent: parent object
 *
 * Widget which extends #GtkComboBoxEntry to change presence.
 */

61
#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyPresenceChooser)
Xavier Claessens's avatar
Xavier Claessens committed
62 63 64 65 66 67 68 69 70 71 72

/* For custom message dialog */
enum {
	COL_ICON,
	COL_LABEL,
	COL_PRESENCE,
	COL_COUNT
};

/* For combobox's model */
enum {
73
	COL_STATUS_TEXT,
Xavier Claessens's avatar
Xavier Claessens committed
74 75 76 77 78 79 80 81 82
	COL_STATE_ICON_NAME,
	COL_STATE,
	COL_DISPLAY_MARKUP,
	COL_STATUS_CUSTOMISABLE,
	COL_TYPE,
	N_COLUMNS
};

typedef enum  {
83 84 85 86 87
	ENTRY_TYPE_BUILTIN,
	ENTRY_TYPE_SAVED,
	ENTRY_TYPE_CUSTOM,
	ENTRY_TYPE_SEPARATOR,
	ENTRY_TYPE_EDIT_CUSTOM,
Xavier Claessens's avatar
Xavier Claessens committed
88
} PresenceChooserEntryType;
89

90
typedef struct {
91
	EmpathyPresenceManager *presence_mgr;
92
	GNetworkMonitor *connectivity;
93

94
	gboolean     editing_status;
95
	int          block_set_editing;
96
	int          block_changed;
97
	guint        focus_out_idle_source;
98

99
	TpConnectionPresenceType state;
Xavier Claessens's avatar
Xavier Claessens committed
100
	PresenceChooserEntryType previous_type;
101

102
	TpAccountManager *account_manager;
103
} EmpathyPresenceChooserPriv;
104

105 106
/* States to be listed in the menu.
 * Each state has a boolean telling if it can have custom message */
107 108 109 110 111 112 113 114 115
static struct { TpConnectionPresenceType state;
         gboolean customisable;
} states[] = { { TP_CONNECTION_PRESENCE_TYPE_AVAILABLE, TRUE } ,
			 { TP_CONNECTION_PRESENCE_TYPE_BUSY, TRUE },
			 { TP_CONNECTION_PRESENCE_TYPE_AWAY, TRUE },
			 { TP_CONNECTION_PRESENCE_TYPE_HIDDEN, FALSE },
			 { TP_CONNECTION_PRESENCE_TYPE_OFFLINE, FALSE},
			 { TP_CONNECTION_PRESENCE_TYPE_UNSET, },
			};
116

117
static void            presence_chooser_constructed            (GObject                    *object);
118
static void            presence_chooser_finalize               (GObject                    *object);
119
static void            presence_chooser_presence_changed_cb    (EmpathyPresenceChooser      *chooser);
120 121
static void            presence_chooser_menu_add_item          (GtkWidget                  *menu,
								const gchar                *str,
122
								TpConnectionPresenceType                  state);
123 124
static void            presence_chooser_noncustom_activate_cb  (GtkWidget                  *item,
								gpointer                    user_data);
125
static void            presence_chooser_set_state              (TpConnectionPresenceType                  state,
126
								const gchar                *status);
127 128
static void            presence_chooser_custom_activate_cb     (GtkWidget                  *item,
								gpointer                    user_data);
129

130
G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_COMBO_BOX);
131 132

static void
133
empathy_presence_chooser_class_init (EmpathyPresenceChooserClass *klass)
134 135 136
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

137
	object_class->constructed = presence_chooser_constructed;
138 139
	object_class->finalize = presence_chooser_finalize;

140
	g_type_class_add_private (object_class, sizeof (EmpathyPresenceChooserPriv));
141 142
}

Xavier Claessens's avatar
Xavier Claessens committed
143 144
static void
presence_chooser_create_model (EmpathyPresenceChooser *self)
145
{
Davyd Madeley's avatar
Davyd Madeley committed
146 147
	GtkListStore *store;
	char *custom_message;
Xavier Claessens's avatar
Xavier Claessens committed
148
	int i;
Davyd Madeley's avatar
Davyd Madeley committed
149 150

	store = gtk_list_store_new (N_COLUMNS,
151
				    G_TYPE_STRING,    /* COL_STATUS_TEXT */
Xavier Claessens's avatar
Xavier Claessens committed
152
				    G_TYPE_STRING,    /* COL_STATE_ICON_NAME */
153
				    G_TYPE_UINT,      /* COL_STATE */
Xavier Claessens's avatar
Xavier Claessens committed
154 155 156
				    G_TYPE_STRING,    /* COL_DISPLAY_MARKUP */
				    G_TYPE_BOOLEAN,   /* COL_STATUS_CUSTOMISABLE */
				    G_TYPE_INT);      /* COL_TYPE */
Davyd Madeley's avatar
Davyd Madeley committed
157

158
	custom_message = g_strdup_printf ("<i>%s</i>", _("Custom Message…"));
Davyd Madeley's avatar
Davyd Madeley committed
159

160
	for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
161
		GList       *list, *l;
Davyd Madeley's avatar
Davyd Madeley committed
162
		const char *status, *icon_name;
163

164 165
		status = empathy_presence_get_default_message (states[i].state);
		icon_name = empathy_icon_name_for_presence (states[i].state);
166

Davyd Madeley's avatar
Davyd Madeley committed
167
		gtk_list_store_insert_with_values (store, NULL, -1,
168
			COL_STATUS_TEXT, status,
Xavier Claessens's avatar
Xavier Claessens committed
169
			COL_STATE_ICON_NAME, icon_name,
170
			COL_STATE, states[i].state,
Xavier Claessens's avatar
Xavier Claessens committed
171
			COL_DISPLAY_MARKUP, status,
172
			COL_STATUS_CUSTOMISABLE, states[i].customisable,
Xavier Claessens's avatar
Xavier Claessens committed
173 174
			COL_TYPE, ENTRY_TYPE_BUILTIN,
			-1);
175

176
		if (states[i].customisable) {
177
			/* Set custom messages if wanted */
178
			list = empathy_status_presets_get (states[i].state, -1);
179
			list = g_list_sort (list, (GCompareFunc) g_utf8_collate);
180
			for (l = list; l; l = l->next) {
Davyd Madeley's avatar
Davyd Madeley committed
181
				gtk_list_store_insert_with_values (store,
Xavier Claessens's avatar
Xavier Claessens committed
182
					NULL, -1,
183
					COL_STATUS_TEXT, l->data,
184
					COL_STATE_ICON_NAME, icon_name,
185
					COL_STATE, states[i].state,
Xavier Claessens's avatar
Xavier Claessens committed
186
					COL_DISPLAY_MARKUP, l->data,
187
					COL_STATUS_CUSTOMISABLE, TRUE,
Xavier Claessens's avatar
Xavier Claessens committed
188
					COL_TYPE, ENTRY_TYPE_SAVED,
189
					-1);
Xavier Claessens's avatar
Xavier Claessens committed
190 191 192 193
			}
			g_list_free (list);

			gtk_list_store_insert_with_values (store, NULL, -1,
194
				COL_STATUS_TEXT, _("Custom Message…"),
Xavier Claessens's avatar
Xavier Claessens committed
195
				COL_STATE_ICON_NAME, icon_name,
196
				COL_STATE, states[i].state,
Xavier Claessens's avatar
Xavier Claessens committed
197 198 199 200
				COL_DISPLAY_MARKUP, custom_message,
				COL_STATUS_CUSTOMISABLE, TRUE,
				COL_TYPE, ENTRY_TYPE_CUSTOM,
				-1);
201 202 203
		}

	}
Davyd Madeley's avatar
Davyd Madeley committed
204

205
	/* add a separator */
Davyd Madeley's avatar
Davyd Madeley committed
206
	gtk_list_store_insert_with_values (store, NULL, -1,
207 208
			COL_TYPE, ENTRY_TYPE_SEPARATOR,
			-1);
Davyd Madeley's avatar
Davyd Madeley committed
209 210

	gtk_list_store_insert_with_values (store, NULL, -1,
211
		COL_STATUS_TEXT, _("Edit Custom Messages…"),
Xavier Claessens's avatar
Xavier Claessens committed
212
		COL_STATE_ICON_NAME, GTK_STOCK_EDIT,
213
		COL_DISPLAY_MARKUP, _("Edit Custom Messages…"),
Xavier Claessens's avatar
Xavier Claessens committed
214 215
		COL_TYPE, ENTRY_TYPE_EDIT_CUSTOM,
		-1);
216

Davyd Madeley's avatar
Davyd Madeley committed
217 218
	g_free (custom_message);

Xavier Claessens's avatar
Xavier Claessens committed
219 220
	gtk_combo_box_set_model (GTK_COMBO_BOX (self), GTK_TREE_MODEL (store));
	g_object_unref (store);
221 222 223
}

static void
Davyd Madeley's avatar
Davyd Madeley committed
224 225 226
presence_chooser_popup_shown_cb (GObject *self,
                                 GParamSpec *pspec,
				 gpointer user_data)
227
{
228
	EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
229
	gboolean shown;
230

231
	g_object_get (self, "popup-shown", &shown, NULL);
Xavier Claessens's avatar
Xavier Claessens committed
232 233 234
	if (!shown) {
		return;
	}
235

236
	/* see presence_chooser_entry_focus_out_cb () for what this does */
Xavier Claessens's avatar
Xavier Claessens committed
237
	if (priv->focus_out_idle_source != 0) {
238 239 240 241
		g_source_remove (priv->focus_out_idle_source);
		priv->focus_out_idle_source = 0;
	}

Xavier Claessens's avatar
Xavier Claessens committed
242
	presence_chooser_create_model (EMPATHY_PRESENCE_CHOOSER (self));
243 244
}

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
static PresenceChooserEntryType
presence_chooser_get_entry_type (EmpathyPresenceChooser *self)
{
	GtkTreeIter iter;
	PresenceChooserEntryType type = -1;

	if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter)) {
		type = ENTRY_TYPE_CUSTOM;
	}
	else {
		GtkTreeModel *model;

		model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
		gtk_tree_model_get (model, &iter,
				    COL_TYPE, &type,
				    -1);
	}

	return type;
}

266 267 268 269 270 271 272 273 274 275
static TpConnectionPresenceType
get_state_and_status (EmpathyPresenceChooser *self,
	gchar **status)
{
	EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
	TpConnectionPresenceType state;
	gchar *tmp;

	state = tp_account_manager_get_most_available_presence (
		priv->account_manager, NULL, &tmp);
276
	if (TPAW_STR_EMPTY (tmp)) {
277 278 279 280 281 282 283 284 285 286 287 288 289
		/* no message, use the default message */
		g_free (tmp);
		tmp = g_strdup (empathy_presence_get_default_message (state));
	}

	if (status != NULL)
		*status = tmp;
	else
		g_free (tmp);

	return state;
}

290 291 292
static gboolean
presence_chooser_is_preset (EmpathyPresenceChooser *self)
{
293
	TpConnectionPresenceType state;
294
	char *status;
295 296 297
	GList *presets, *l;
	gboolean match = FALSE;

298
	state = get_state_and_status (self, &status);
299 300 301 302 303

	presets = empathy_status_presets_get (state, -1);
	for (l = presets; l; l = l->next) {
		char *preset = (char *) l->data;

304
		if (!tp_strdiff (status, preset)) {
305 306 307 308 309 310 311
			match = TRUE;
			break;
		}
	}

	g_list_free (presets);

312
	DEBUG ("is_preset(%i, %s) = %i", state, status, match);
313

314
	g_free (status);
315 316 317
	return match;
}

318
static void
Davyd Madeley's avatar
Davyd Madeley committed
319
presence_chooser_set_favorite_icon (EmpathyPresenceChooser *self)
320 321 322 323 324 325 326
{
	GtkWidget *entry;
	PresenceChooserEntryType type;

	entry = gtk_bin_get_child (GTK_BIN (self));
	type = presence_chooser_get_entry_type (self);

327 328 329 330
	if (type == ENTRY_TYPE_CUSTOM || type == ENTRY_TYPE_SAVED) {
		if (presence_chooser_is_preset (self)) {
			/* saved entries can be removed from the list */
			gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
331
				           GTK_ENTRY_ICON_SECONDARY,
332
					   "starred-symbolic");
333
			gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
334
					 GTK_ENTRY_ICON_SECONDARY,
Davyd Madeley's avatar
Davyd Madeley committed
335
					 _("Click to remove this status as a favorite"));
336
		}
337
		else {
Davyd Madeley's avatar
Davyd Madeley committed
338
			/* custom entries can be favorited */
339
			gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
340
				           GTK_ENTRY_ICON_SECONDARY,
341
					   "non-starred-symbolic");
342 343
			gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
					 GTK_ENTRY_ICON_SECONDARY,
Davyd Madeley's avatar
Davyd Madeley committed
344
					 _("Click to make this status a favorite"));
345
		}
346 347
	}
	else {
Davyd Madeley's avatar
Davyd Madeley committed
348
		/* built-in entries cannot be favorited */
349 350 351 352 353 354 355 356 357
		gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
				           GTK_ENTRY_ICON_SECONDARY,
					   NULL);
		gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
					 GTK_ENTRY_ICON_SECONDARY,
					 NULL);
	}
}

358
static void
Davyd Madeley's avatar
Davyd Madeley committed
359 360
presence_chooser_set_status_editing (EmpathyPresenceChooser *self,
                                     gboolean editing)
361
{
362
	EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
Xavier Claessens's avatar
Xavier Claessens committed
363
	GtkWidget *entry;
364

Xavier Claessens's avatar
Xavier Claessens committed
365 366 367
	if (priv->block_set_editing) {
		return;
	}
368

Xavier Claessens's avatar
Xavier Claessens committed
369
	entry = gtk_bin_get_child (GTK_BIN (self));
Davyd Madeley's avatar
Davyd Madeley committed
370
	if (editing) {
371 372 373
		gchar *tooltip_text;
		gchar *status;

374
		priv->editing_status = TRUE;
375

376
		get_state_and_status (self, &status);
377 378 379
		/* Translators: %s is a status message like 'At the pub' for example */
		tooltip_text = g_strdup_printf (_("<b>Current message: %s</b>\n"
			"<small><i>Press Enter to set the new message or Esc to cancel.</i></small>"),
380 381
		    status);
		gtk_widget_set_tooltip_markup (entry, tooltip_text);
382
		gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
Xavier Claessens's avatar
Xavier Claessens committed
383 384
					       GTK_ENTRY_ICON_SECONDARY,
					       GTK_STOCK_OK);
385
		gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
Xavier Claessens's avatar
Xavier Claessens committed
386 387
						 GTK_ENTRY_ICON_SECONDARY,
						 _("Set status"));
388
		gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
Xavier Claessens's avatar
Xavier Claessens committed
389 390
					      GTK_ENTRY_ICON_PRIMARY,
					      FALSE);
391 392
		g_free (status);
		g_free (tooltip_text);
Xavier Claessens's avatar
Xavier Claessens committed
393 394 395
	} else {
		GtkWidget *window;

Davyd Madeley's avatar
Davyd Madeley committed
396
		presence_chooser_set_favorite_icon (self);
397
		gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
Xavier Claessens's avatar
Xavier Claessens committed
398 399
					      GTK_ENTRY_ICON_PRIMARY,
					      TRUE);
400

401
		/* attempt to get the toplevel for this widget */
Xavier Claessens's avatar
Xavier Claessens committed
402
		window = gtk_widget_get_toplevel (GTK_WIDGET (self));
403
		if (gtk_widget_is_toplevel (window) && GTK_IS_WINDOW (window)) {
404 405 406
			/* unset the focus */
			gtk_window_set_focus (GTK_WINDOW (window), NULL);
		}
407

408
		/* see presence_chooser_entry_focus_out_cb ()
409
		 * for what this does */
Davyd Madeley's avatar
Davyd Madeley committed
410
		if (priv->focus_out_idle_source != 0) {
411 412 413 414
			g_source_remove (priv->focus_out_idle_source);
			priv->focus_out_idle_source = 0;
		}

415
		gtk_editable_set_position (GTK_EDITABLE (entry), 0);
416

417
		priv->editing_status = FALSE;
418 419 420
	}
}

421 422 423 424
static void
mc_set_custom_state (EmpathyPresenceChooser *self)
{
	EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
Davyd Madeley's avatar
Davyd Madeley committed
425 426
	GtkWidget *entry;
	const char *status;
427

Davyd Madeley's avatar
Davyd Madeley committed
428
	entry = gtk_bin_get_child (GTK_BIN (self));
429
	/* update the status with MC */
Davyd Madeley's avatar
Davyd Madeley committed
430 431
	status = gtk_entry_get_text (GTK_ENTRY (entry));

432
	DEBUG ("Sending state to MC-> %d (%s)", priv->state, status);
Xavier Claessens's avatar
Xavier Claessens committed
433

434
	empathy_presence_manager_set_presence (priv->presence_mgr, priv->state, status);
435 436
}

437 438
static void
ui_set_custom_state (EmpathyPresenceChooser *self,
439
		     TpConnectionPresenceType state,
Xavier Claessens's avatar
Xavier Claessens committed
440
		     const char *status)
441 442
{
	EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
Davyd Madeley's avatar
Davyd Madeley committed
443
	GtkWidget *entry;
444
	const char *icon_name;
445
	const gchar *status_tooltip;
446

Davyd Madeley's avatar
Davyd Madeley committed
447 448
	entry = gtk_bin_get_child (GTK_BIN (self));

449
	priv->block_set_editing++;
450
	priv->block_changed++;
451 452 453

	icon_name = empathy_icon_name_for_presence (state);
	gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
Xavier Claessens's avatar
Xavier Claessens committed
454 455
					   GTK_ENTRY_ICON_PRIMARY,
					   icon_name);
456 457 458
	status_tooltip = status == NULL ? "" : status;
	gtk_entry_set_text (GTK_ENTRY (entry), status_tooltip);
	gtk_widget_set_tooltip_text (GTK_WIDGET (entry), status_tooltip);
Davyd Madeley's avatar
Davyd Madeley committed
459
	presence_chooser_set_favorite_icon (self);
460

461
	priv->block_changed--;
462 463 464
	priv->block_set_editing--;
}

Davyd Madeley's avatar
Davyd Madeley committed
465
static void
Davyd Madeley's avatar
Davyd Madeley committed
466
presence_chooser_reset_status (EmpathyPresenceChooser *self)
Davyd Madeley's avatar
Davyd Madeley committed
467 468
{
	/* recover the status that was unset */
469
	presence_chooser_set_status_editing (self, FALSE);
470
	presence_chooser_presence_changed_cb (self);
Davyd Madeley's avatar
Davyd Madeley committed
471 472
}

473
static void
Xavier Claessens's avatar
Xavier Claessens committed
474 475 476 477
presence_chooser_entry_icon_release_cb (EmpathyPresenceChooser *self,
                                        GtkEntryIconPosition    icon_pos,
                                        GdkEvent               *event,
					GtkEntry               *entry)
478
{
479 480
	EmpathyPresenceChooserPriv *priv = GET_PRIV (self);

Davyd Madeley's avatar
Davyd Madeley committed
481
	if (priv->editing_status) {
482 483 484 485
		presence_chooser_set_status_editing (self, FALSE);
		mc_set_custom_state (self);
	}
	else {
486
		TpConnectionPresenceType state;
487
		char *status;
Davyd Madeley's avatar
Davyd Madeley committed
488

489
		state = get_state_and_status (self, &status);
Davyd Madeley's avatar
Davyd Madeley committed
490

491
		if (!empathy_status_presets_is_valid (state)) {
492
			/* It doesn't make sense to add such presence as favorite */
493
			g_free (status);
494
			return;
495
		}
496

497
		if (presence_chooser_is_preset (self)) {
Davyd Madeley's avatar
Davyd Madeley committed
498
			/* remove the entry */
499
			DEBUG ("REMOVING PRESET (%i, %s)", state, status);
Davyd Madeley's avatar
Davyd Madeley committed
500 501
			empathy_status_presets_remove (state, status);
		}
502 503
		else {
			/* save the entry */
504
			DEBUG ("SAVING PRESET (%i, %s)", state, status);
505 506
			empathy_status_presets_set_last (state, status);
		}
Davyd Madeley's avatar
Davyd Madeley committed
507 508

		/* update the icon */
Davyd Madeley's avatar
Davyd Madeley committed
509
		presence_chooser_set_favorite_icon (self);
510
		g_free (status);
511
	}
512 513 514
}

static void
Xavier Claessens's avatar
Xavier Claessens committed
515 516
presence_chooser_entry_activate_cb (EmpathyPresenceChooser *self,
                                    GtkEntry               *entry)
517
{
Davyd Madeley's avatar
Davyd Madeley committed
518
	presence_chooser_set_status_editing (self, FALSE);
519
	mc_set_custom_state (self);
520 521
}

522
static gboolean
Xavier Claessens's avatar
Xavier Claessens committed
523 524 525
presence_chooser_entry_key_press_event_cb (EmpathyPresenceChooser *self,
                                           GdkEventKey            *event,
					   GtkWidget              *entry)
526 527 528
{
	EmpathyPresenceChooserPriv *priv = GET_PRIV (self);

529
	if (priv->editing_status && event->keyval == GDK_KEY_Escape) {
530
		/* the user pressed Escape, undo the editing */
Davyd Madeley's avatar
Davyd Madeley committed
531
		presence_chooser_reset_status (self);
532 533
		return TRUE;
	}
534
	else if (event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_Down) {
535 536 537
		/* ignore */
		return TRUE;
	}
538 539 540 541

	return FALSE; /* send this event elsewhere */
}

Davyd Madeley's avatar
Davyd Madeley committed
542
static gboolean
Davyd Madeley's avatar
Davyd Madeley committed
543 544 545
presence_chooser_entry_button_press_event_cb (EmpathyPresenceChooser *self,
                                              GdkEventButton         *event,
					      GtkWidget              *entry)
Davyd Madeley's avatar
Davyd Madeley committed
546 547 548
{
	EmpathyPresenceChooserPriv *priv = GET_PRIV (self);

549 550
	if (!priv->editing_status &&
	    event->button == 1 &&
551
	    !gtk_widget_has_focus (entry)) {
Davyd Madeley's avatar
Davyd Madeley committed
552 553 554 555 556 557 558 559 560
		gtk_widget_grab_focus (entry);
		gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);

		return TRUE;
	}

	return FALSE;
}

561
static void
Davyd Madeley's avatar
Davyd Madeley committed
562
presence_chooser_entry_changed_cb (EmpathyPresenceChooser *self,
563
				   GtkEntry               *entry)
564 565 566
{
	EmpathyPresenceChooserPriv *priv = GET_PRIV (self);

Xavier Claessens's avatar
Xavier Claessens committed
567 568 569
	if (priv->block_changed){
		return;
	}
570 571

	/* the combo is being edited to a custom entry */
Davyd Madeley's avatar
Davyd Madeley committed
572
	if (!priv->editing_status) {
Davyd Madeley's avatar
Davyd Madeley committed
573
		presence_chooser_set_status_editing (self, TRUE);
574 575 576
	}
}

577
static void
Davyd Madeley's avatar
Davyd Madeley committed
578
presence_chooser_changed_cb (GtkComboBox *self, gpointer user_data)
579
{
580
	EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
581 582
	GtkTreeIter iter;
	char *icon_name;
583
	TpConnectionPresenceType new_state;
584
	gboolean customisable = TRUE;
585
	PresenceChooserEntryType type = -1;
Davyd Madeley's avatar
Davyd Madeley committed
586
	GtkWidget *entry;
Xavier Claessens's avatar
Xavier Claessens committed
587
	GtkTreeModel *model;
588

Xavier Claessens's avatar
Xavier Claessens committed
589 590
	if (priv->block_changed ||
	    !gtk_combo_box_get_active_iter (self, &iter)) {
591 592 593
		return;
	}

Xavier Claessens's avatar
Xavier Claessens committed
594
	model = gtk_combo_box_get_model (self);
595
	gtk_tree_model_get (model, &iter,
Xavier Claessens's avatar
Xavier Claessens committed
596 597 598 599 600
			    COL_STATE_ICON_NAME, &icon_name,
			    COL_STATE, &new_state,
			    COL_STATUS_CUSTOMISABLE, &customisable,
			    COL_TYPE, &type,
			    -1);
601

Davyd Madeley's avatar
Davyd Madeley committed
602
	entry = gtk_bin_get_child (GTK_BIN (self));
603

604 605 606
	/* some types of status aren't editable, set the editability of the
	 * entry appropriately. Unless we're just about to reset it anyway,
	 * in which case, don't fiddle with it */
Davyd Madeley's avatar
Davyd Madeley committed
607
	if (type != ENTRY_TYPE_EDIT_CUSTOM) {
608
		gtk_editable_set_editable (GTK_EDITABLE (entry), customisable);
Davyd Madeley's avatar
Davyd Madeley committed
609
		priv->state = new_state;
610 611
	}

Davyd Madeley's avatar
Davyd Madeley committed
612
	if (type == ENTRY_TYPE_EDIT_CUSTOM) {
613
		GtkWidget *window, *dialog;
Xavier Claessens's avatar
Xavier Claessens committed
614

Davyd Madeley's avatar
Davyd Madeley committed
615
		presence_chooser_reset_status (EMPATHY_PRESENCE_CHOOSER (self));
616 617

		/* attempt to get the toplevel for this widget */
Xavier Claessens's avatar
Xavier Claessens committed
618
		window = gtk_widget_get_toplevel (GTK_WIDGET (self));
619
		if (!gtk_widget_is_toplevel (window) || !GTK_IS_WINDOW (window)) {
620 621 622
			window = NULL;
		}

623 624 625
		dialog = empathy_status_preset_dialog_new (GTK_WINDOW (window));
		gtk_dialog_run (GTK_DIALOG (dialog));
		gtk_widget_destroy (dialog);
626
	}
Davyd Madeley's avatar
Davyd Madeley committed
627
	else if (type == ENTRY_TYPE_CUSTOM) {
628
		gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
Xavier Claessens's avatar
Xavier Claessens committed
629 630
						   GTK_ENTRY_ICON_PRIMARY,
						   icon_name);
631

632
		/* preseed the status */
Davyd Madeley's avatar
Davyd Madeley committed
633
		if (priv->previous_type == ENTRY_TYPE_BUILTIN) {
634 635
			/* if their previous entry was a builtin, don't
			 * preseed */
636
			gtk_entry_set_text (GTK_ENTRY (entry), "");
Xavier Claessens's avatar
Xavier Claessens committed
637
		} else {
638 639
			/* else preseed the text of their currently entered
			 * status message */
640
			char *status;
Davyd Madeley's avatar
Davyd Madeley committed
641

642 643
			get_state_and_status (EMPATHY_PRESENCE_CHOOSER (self),
				&status);
644
			gtk_entry_set_text (GTK_ENTRY (entry), status);
645
			g_free (status);
646
		}
647

648 649
		/* grab the focus */
		gtk_widget_grab_focus (entry);
Xavier Claessens's avatar
Xavier Claessens committed
650
	} else {
651
		char *status;
Xavier Claessens's avatar
Xavier Claessens committed
652

653 654
		/* just in case we were setting a new status when
		 * things were changed */
Davyd Madeley's avatar
Davyd Madeley committed
655
		presence_chooser_set_status_editing (
Xavier Claessens's avatar
Xavier Claessens committed
656 657
			EMPATHY_PRESENCE_CHOOSER (self),
			FALSE);
658 659 660
		gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
					   GTK_ENTRY_ICON_PRIMARY,
					   icon_name);
661 662

		gtk_tree_model_get (model, &iter,
Xavier Claessens's avatar
Xavier Claessens committed
663 664
				    COL_STATUS_TEXT, &status,
				    -1);
665

666
		empathy_presence_manager_set_presence (priv->presence_mgr, priv->state, status);
667 668

		g_free (status);
669
	}
670

Davyd Madeley's avatar
Davyd Madeley committed
671
	if (type != ENTRY_TYPE_EDIT_CUSTOM) {
672 673
		priv->previous_type = type;
	}
674 675 676
	g_free (icon_name);
}

677 678 679 680 681
static gboolean
combo_row_separator_func (GtkTreeModel	*model,
			  GtkTreeIter	*iter,
			  gpointer	 data)
{
682
	PresenceChooserEntryType type;
Xavier Claessens's avatar
Xavier Claessens committed
683

684
	gtk_tree_model_get (model, iter,
Xavier Claessens's avatar
Xavier Claessens committed
685 686
			    COL_TYPE, &type,
			    -1);
687 688 689 690

	return (type == ENTRY_TYPE_SEPARATOR);
}

691 692 693
static gboolean
presence_chooser_entry_focus_out_idle_cb (gpointer user_data)
{
Davyd Madeley's avatar
Davyd Madeley committed
694 695 696
	EmpathyPresenceChooser *chooser;
	GtkWidget *entry;

697
	DEBUG ("Autocommiting status message");
698

Davyd Madeley's avatar
Davyd Madeley committed
699 700
	chooser = EMPATHY_PRESENCE_CHOOSER (user_data);
	entry = gtk_bin_get_child (GTK_BIN (chooser));
701 702 703 704 705 706

	presence_chooser_entry_activate_cb (chooser, GTK_ENTRY (entry));

	return FALSE;
}

707
static gboolean
Davyd Madeley's avatar
Davyd Madeley committed
708 709 710
presence_chooser_entry_focus_out_cb (EmpathyPresenceChooser *chooser,
                                     GdkEventFocus *event,
				     GtkEntry *entry)
711 712 713
{
	EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);

Davyd Madeley's avatar
Davyd Madeley committed
714
	if (priv->editing_status) {
715 716 717 718 719 720 721 722 723 724 725 726 727
		/* this seems a bit evil and maybe it will be fragile,
		 * someone should think of a better way to do it.
		 *
		 * The entry has focused out, but we don't know where the focus
		 * has gone. If it goes to the combo box, we don't want to
		 * do anything. If it's gone anywhere else, we want to commit
		 * the result.
		 *
		 * Thus we install this idle handler and store its source.
		 * If the source is scheduled when the popup handler runs,
		 * it will remove it, else the callback will commit the result.
		 */
		priv->focus_out_idle_source = g_idle_add (
Xavier Claessens's avatar
Xavier Claessens committed
728 729
			presence_chooser_entry_focus_out_idle_cb,
			chooser);
730 731
	}

732 733
	gtk_editable_set_position (GTK_EDITABLE (entry), 0);

734 735 736
	return FALSE;
}

737
static void
738 739 740
update_sensitivity_am_prepared_cb (GObject *source_object,
				   GAsyncResult *result,
				   gpointer user_data)
741
{
742 743
	TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
	EmpathyPresenceChooser *chooser = user_data;
744 745 746
	EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
	gboolean sensitive = FALSE;
	GList *accounts, *l;
747
	GError *error = NULL;
748

749
	if (!tp_proxy_prepare_finish (manager, result, &error)) {
750 751
		DEBUG ("Failed to prepare account manager: %s", error->message);
		g_error_free (error);
752 753 754
		return;
	}

755
	accounts = tp_account_manager_dup_valid_accounts (manager);
756 757

	for (l = accounts ; l != NULL ; l = g_list_next (l)) {
758
		TpAccount *a = TP_ACCOUNT (l->data);
759

760
		if (tp_account_is_enabled (a)) {
761
			sensitive = TRUE;
762 763
			break;
		}
764 765
	}

766
	g_list_free_full (accounts, g_object_unref);
767

768
	if (!g_network_monitor_get_network_available (priv->connectivity))
769 770 771
		sensitive = FALSE;

	gtk_widget_set_sensitive (GTK_WIDGET (chooser), sensitive);
772 773

	presence_chooser_presence_changed_cb (chooser);
774 775
}

776 777 778 779 780
static void
presence_chooser_update_sensitivity (EmpathyPresenceChooser *chooser)
{
	EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);

781
	tp_proxy_prepare_async (priv->account_manager, NULL,
782 783 784 785 786 787 788 789 790 791 792 793 794 795
					  update_sensitivity_am_prepared_cb,
					  chooser);
}

static void
presence_chooser_account_manager_account_validity_changed_cb (
	TpAccountManager *manager,
	TpAccount *account,
	gboolean valid,
	EmpathyPresenceChooser *chooser)
{
	presence_chooser_update_sensitivity (chooser);
}

796
static void
797
presence_chooser_account_manager_account_changed_cb (
798 799
	TpAccountManager *manager,
	TpAccount *account,
800 801 802 803 804
	EmpathyPresenceChooser *chooser)
{
	presence_chooser_update_sensitivity (chooser);
}

805
static void
806
presence_chooser_network_change (GNetworkMonitor *connectivity,
807 808 809
					    gboolean new_online,
					    EmpathyPresenceChooser *chooser)
{
810
	presence_chooser_update_sensitivity (chooser);
811 812
}

813
static void
814
empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
815
{
816 817
	EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
		EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
818

819
	chooser->priv = priv;
820 821 822 823 824 825 826 827 828
}

static void
presence_chooser_constructed (GObject *object)
{
	EmpathyPresenceChooser *chooser = EMPATHY_PRESENCE_CHOOSER (object);
	EmpathyPresenceChooserPriv *priv = chooser->priv;
	GtkWidget *entry;
	GtkCellRenderer *renderer;
829
	const gchar *status_tooltip;
830

831 832 833
	if (G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->constructed)
		G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->constructed (object);

Xavier Claessens's avatar
Xavier Claessens committed
834
	presence_chooser_create_model (chooser);
835

836
	gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX (chooser),
Xavier Claessens's avatar
Xavier Claessens committed
837
					     COL_STATUS_TEXT);
838
	gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
Xavier Claessens's avatar
Xavier Claessens committed
839 840
					      combo_row_separator_func,
					      NULL, NULL);
Davyd Madeley's avatar
Davyd Madeley committed
841 842

	entry = gtk_bin_get_child (GTK_BIN (chooser));
843
	gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
Xavier Claessens's avatar
Xavier Claessens committed
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
					GTK_ENTRY_ICON_PRIMARY,
					FALSE);

	g_signal_connect_swapped (entry, "icon-release",
		G_CALLBACK (presence_chooser_entry_icon_release_cb),
		chooser);
	g_signal_connect_swapped (entry, "activate",
		G_CALLBACK (presence_chooser_entry_activate_cb),
		chooser);
	g_signal_connect_swapped (entry, "key-press-event",
		G_CALLBACK (presence_chooser_entry_key_press_event_cb),
		chooser);
	g_signal_connect_swapped (entry, "button-press-event",
		G_CALLBACK (presence_chooser_entry_button_press_event_cb),
		chooser);
859 860 861 862 863 864

	gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));

	renderer = gtk_cell_renderer_pixbuf_new ();
	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, FALSE);
	gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
Xavier Claessens's avatar
Xavier Claessens committed
865 866
					"icon-name", COL_STATE_ICON_NAME,
					NULL);
867 868 869 870 871
	g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);

	renderer = gtk_cell_renderer_text_new ();
	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, TRUE);
	gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
Xavier Claessens's avatar
Xavier Claessens committed
872 873
					"markup", COL_DISPLAY_MARKUP,
					NULL);
874
	g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
875 876

	g_signal_connect (chooser, "notify::popup-shown",
Davyd Madeley's avatar
Davyd Madeley committed
877
			G_CALLBACK (presence_chooser_popup_shown_cb), NULL);
878
	g_signal_connect (chooser, "changed",
Davyd Madeley's avatar
Davyd Madeley committed
879
			G_CALLBACK (presence_chooser_changed_cb), NULL);
880
	g_signal_connect_swapped (entry, "changed",
Davyd Madeley's avatar
Davyd Madeley committed
881 882
			G_CALLBACK (presence_chooser_entry_changed_cb),
			chooser);
883
	g_signal_connect_swapped (entry, "focus-out-event",
Davyd Madeley's avatar
Davyd Madeley committed
884 885
			G_CALLBACK (presence_chooser_entry_focus_out_cb),
			chooser);
886

887
	priv->presence_mgr = empathy_presence_manager_dup_singleton ();
888

889
	priv->account_manager = tp_account_manager_dup ();
890 891 892 893
	g_signal_connect_swapped (priv->account_manager,
		"most-available-presence-changed",
		G_CALLBACK (presence_chooser_presence_changed_cb),
		chooser);
894

895
	tp_g_signal_connect_object (priv->account_manager, "account-validity-changed",
896
		G_CALLBACK (presence_chooser_account_manager_account_validity_changed_cb),
897 898
		chooser, 0);
	tp_g_signal_connect_object (priv->account_manager, "account-removed",
899
		G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
900 901
		chooser, 0);
	tp_g_signal_connect_object (priv->account_manager, "account-enabled",
902
		G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
903 904
		chooser, 0);
	tp_g_signal_connect_object (priv->account_manager, "account-disabled",
905
		G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
906
		chooser, 0);
907

908 909
	status_tooltip = gtk_entry_get_text (GTK_ENTRY (entry));
	gtk_widget_set_tooltip_text (GTK_WIDGET (chooser), status_tooltip);
910

911 912 913
	priv->connectivity = g_network_monitor_get_default ();
	g_object_ref (priv->connectivity);

914
	tp_g_signal_connect_object (priv->connectivity,
915 916
		"network-changed",
		G_CALLBACK (presence_chooser_network_change),
917
		chooser, 0);
918 919

	presence_chooser_update_sensitivity (chooser);
920 921 922 923 924
}

static void
presence_chooser_finalize (GObject *object)
{
925
	EmpathyPresenceChooserPriv *priv;
926 927 928

	priv = GET_PRIV (object);

929 930 931 932
	if (priv->focus_out_idle_source) {
		g_source_remove (priv->focus_out_idle_source);
	}

933 934 935
	if (priv->account_manager != NULL)
		g_object_unref (priv->account_manager);

936
	g_signal_handlers_disconnect_by_func (priv->presence_mgr,
937 938
					      presence_chooser_presence_changed_cb,
					      object);
939
	g_object_unref (priv->presence_mgr);
940

941 942
	g_object_unref (priv->connectivity);

943
	G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
944 945
}

946 947 948 949 950 951 952
/**
 * empathy_presence_chooser_new:
 *
 * Creates a new #EmpathyPresenceChooser widget.
 *
 * Return value: A new #EmpathyPresenceChooser widget
 */
953
GtkWidget *
954
empathy_presence_chooser_new (void)
955
{
Guillaume Desmottes's avatar
Guillaume Desmottes committed
956
	/* FIXME, why can't this go in init ()? */
957 958 959
	return g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER,
		"has-entry", TRUE,
		NULL);
960 961 962
}

static void
963
presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
964
{
965
	EmpathyPresenceChooserPriv *priv;
966
	TpConnectionPresenceType    state;
967
	gchar                      *status;
Davyd Madeley's avatar
Davyd Madeley committed
968
	GtkTreeModel               *model;
Xavier Claessens's avatar
Xavier Claessens committed
969
	GtkTreeIter                 iter;
Davyd Madeley's avatar
Davyd Madeley committed
970
	gboolean valid, match_state = FALSE, match = FALSE;
971
	GtkWidget                  *entry;
972 973 974

	priv = GET_PRIV (chooser);

Xavier Claessens's avatar
Xavier Claessens committed
975 976 977
	if (priv->editing_status) {
		return;
	}
978

979 980
	state = get_state_and_status (chooser, &status);
	priv->state = state;
981

982 983 984 985 986 987
	/* An unset presence here doesn't make any sense. Force it to appear as
	 * offline. */
	if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
		state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
	}

988
	/* look through the model and attempt to find a matching state */
Davyd Madeley's avatar
Davyd Madeley committed
989
	model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
990 991
	for (valid = gtk_tree_model_get_iter_first (model, &iter);
	     valid;
Davyd Madeley's avatar
Davyd Madeley committed
992
	     valid = gtk_tree_model_iter_next (model, &iter)) {
993
		int m_type;
994
		TpConnectionPresenceType m_state;
995 996 997 998 999 1000 1001
		char *m_status;

		gtk_tree_model_get (model, &iter,
				COL_STATE, &m_state,
				COL_TYPE, &m_type,
				-1);

1002 1003
		if (m_type == ENTRY_TYPE_CUSTOM ||
		    m_type == ENTRY_TYPE_SEPARATOR ||
Davyd Madeley's avatar
Davyd Madeley committed
1004
		    m_type == ENTRY_TYPE_EDIT_CUSTOM) {
1005 1006
			continue;
		}
Davyd Madeley's avatar
Davyd Madeley committed
1007
		else if (!match_state && state == m_state) {
1008 1009 1010 1011
			/* we are now in the section that can contain our
			 * match */
			match_state = TRUE;
		}
Davyd Madeley's avatar
Davyd Madeley committed
1012
		else if (match_state && state != m_state) {
1013 1014 1015 1016 1017 1018 1019 1020 1021
			/* we have passed the section that can contain our
			 * match */
			break;
		}

		gtk_tree_model_get (model, &iter,
				COL_STATUS_TEXT, &m_status,
				-1);

1022
		match = !tp_strdiff (status, m_status);
1023 1024 1025 1026 1027 1028 1029

		g_free (m_status);

		if (match) break;

	}

Davyd Madeley's avatar
Davyd Madeley committed
1030
	if (match) {
1031
		priv->block_changed++;
1032
		gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
Davyd Madeley's avatar
Davyd Madeley committed
1033
		presence_chooser_set_favorite_icon (chooser);
1034
		priv->block_changed--;
1035
	}
Davyd Madeley's avatar
Davyd Madeley committed
1036
	else {
1037
		ui_set_custom_state (chooser, state, status);
1038 1039
	}

Xavier Claessens's avatar
Xavier Claessens committed
1040
	entry = gtk_bin_get_child (GTK_BIN (chooser));
1041
	gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
1042 1043
	      GTK_ENTRY_ICON_PRIMARY,
	      empathy_icon_name_for_presence (state));
1044
	gtk_widget_set_tooltip_text (GTK_WIDGET (entry), status);
Davyd Madeley's avatar
Davyd Madeley committed
1045

Xavier Claessens's avatar
Xavier Claessens committed
1046
	entry = gtk_bin_get_child (GTK_BIN (chooser));
1047 1048
	gtk_editable_set_editable (GTK_EDITABLE (entry),
	    state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
1049 1050

	g_free (status);
1051 1052
}

1053 1054 1055 1056 1057 1058 1059
/**
 * empathy_presence_chooser_create_menu:
 *
 * Creates a new #GtkMenu allowing users to change their presence from a menu.
 *
 * Return value: a new #GtkMenu for changing presence in a menu.
 */
1060
GtkWidget *
1061
empathy_presence_chooser_create_menu (void)
1062
{
1063
	const gchar *status;
1064 1065 1066 1067
	GtkWidget   *menu;
	GtkWidget   *item;
	GtkWidget   *image;
	guint        i;
1068 1069 1070

	menu = gtk_menu_new ();

1071
	for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
1072
		GList       *list, *l;
1073

1074
		status = empathy_presence_get_default_message (states[i].state);
1075
		presence_chooser_menu_add_item (menu,
1076
						status,
1077
						states[i].state);
1078

1079
		if (states[i].customisable) {
1080
			/* Set custom messages if wanted */
1081
			list = empathy_status_presets_get (states[i].state, 5);
1082 1083 1084
			for (l = list; l; l = l->next) {
				presence_chooser_menu_add_item (menu,
								l->data,
1085
								states[i].state);
1086 1087
			}
			g_list_free (list);
1088 1089
		}

1090 1091
	}

Davyd Madeley's avatar
Davyd Madeley committed
1092
	/* Separator */
1093 1094 1095 1096
	item = gtk_menu_item_new ();
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
	gtk_widget_show (item);

1097
	/* Custom messages */
1098
	item = gtk_image_menu_item_new_with_label (_("Custom messages…"));
1099
	image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
1100
	gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1101
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1102
	gtk_widget_show (image);
1103 1104 1105 1106
	gtk_widget_show (item);

	g_signal_connect (item,
			  "activate",
1107
			  G_CALLBACK (presence_chooser_custom_activate_cb),
1108
			  NULL);
1109 1110 1111 1112

	return menu;
}

1113 1114 1115
static void
presence_chooser_menu_add_item (GtkWidget   *menu,
				const gchar *str,
1116
				TpConnectionPresenceType state)