Updated German help translation
[empathy/ppotvin.git] / libempathy-gtk / empathy-presence-chooser.c
bloba434936e960719a214d2fa93e6fb815579c859c7
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Copyright (C) 2005-2007 Imendio AB
4 * Copyright (C) 2009 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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Richard Hult <richard@imendio.com>
22 * Martyn Russell <martyn@imendio.com>
23 * Xavier Claessens <xclaesse@gmail.com>
24 * Davyd Madeley <davyd.madeley@collabora.co.uk>
27 #include "config.h"
29 #include <string.h>
30 #include <stdlib.h>
32 #include <glib/gi18n-lib.h>
33 #include <gtk/gtk.h>
34 #include <gdk/gdkkeysyms.h>
36 #include <telepathy-glib/account-manager.h>
37 #include <telepathy-glib/util.h>
39 #include <libempathy/empathy-connectivity.h>
40 #include <libempathy/empathy-idle.h>
41 #include <libempathy/empathy-utils.h>
42 #include <libempathy/empathy-status-presets.h>
44 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
45 #include <libempathy/empathy-debug.h>
47 #include "empathy-ui-utils.h"
48 #include "empathy-images.h"
49 #include "empathy-presence-chooser.h"
50 #include "empathy-status-preset-dialog.h"
52 /**
53 * SECTION:empathy-presence-chooser
54 * @title:EmpathyPresenceChooser
55 * @short_description: A widget used to change presence
56 * @include: libempathy-gtk/empathy-presence-chooser.h
58 * #EmpathyPresenceChooser is a widget which extends #GtkComboBoxEntry
59 * to change presence.
62 /**
63 * EmpathyAccountChooser:
64 * @parent: parent object
66 * Widget which extends #GtkComboBoxEntry to change presence.
69 /* Flashing delay for icons (milliseconds). */
70 #define FLASH_TIMEOUT 500
72 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyPresenceChooser)
74 /* For custom message dialog */
75 enum {
76 COL_ICON,
77 COL_LABEL,
78 COL_PRESENCE,
79 COL_COUNT
82 /* For combobox's model */
83 enum {
84 COL_STATUS_TEXT,
85 COL_STATE_ICON_NAME,
86 COL_STATE,
87 COL_DISPLAY_MARKUP,
88 COL_STATUS_CUSTOMISABLE,
89 COL_TYPE,
90 N_COLUMNS
93 typedef enum {
94 ENTRY_TYPE_BUILTIN,
95 ENTRY_TYPE_SAVED,
96 ENTRY_TYPE_CUSTOM,
97 ENTRY_TYPE_SEPARATOR,
98 ENTRY_TYPE_EDIT_CUSTOM,
99 } PresenceChooserEntryType;
101 typedef struct {
102 EmpathyIdle *idle;
103 EmpathyConnectivity *connectivity;
105 gboolean editing_status;
106 int block_set_editing;
107 int block_changed;
108 guint focus_out_idle_source;
110 TpConnectionPresenceType state;
111 PresenceChooserEntryType previous_type;
113 TpAccountManager *account_manager;
114 } EmpathyPresenceChooserPriv;
116 /* States to be listed in the menu.
117 * Each state has a boolean telling if it can have custom message */
118 static struct { TpConnectionPresenceType state;
119 gboolean customisable;
120 } states[] = { { TP_CONNECTION_PRESENCE_TYPE_AVAILABLE, TRUE } ,
121 { TP_CONNECTION_PRESENCE_TYPE_BUSY, TRUE },
122 { TP_CONNECTION_PRESENCE_TYPE_AWAY, TRUE },
123 { TP_CONNECTION_PRESENCE_TYPE_HIDDEN, FALSE },
124 { TP_CONNECTION_PRESENCE_TYPE_OFFLINE, FALSE},
125 { TP_CONNECTION_PRESENCE_TYPE_UNSET, },
128 static void presence_chooser_finalize (GObject *object);
129 static void presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser);
130 static void presence_chooser_menu_add_item (GtkWidget *menu,
131 const gchar *str,
132 TpConnectionPresenceType state);
133 static void presence_chooser_noncustom_activate_cb (GtkWidget *item,
134 gpointer user_data);
135 static void presence_chooser_set_state (TpConnectionPresenceType state,
136 const gchar *status);
137 static void presence_chooser_custom_activate_cb (GtkWidget *item,
138 gpointer user_data);
140 G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_COMBO_BOX_ENTRY);
142 static void
143 empathy_presence_chooser_class_init (EmpathyPresenceChooserClass *klass)
145 GObjectClass *object_class = G_OBJECT_CLASS (klass);
147 object_class->finalize = presence_chooser_finalize;
149 g_type_class_add_private (object_class, sizeof (EmpathyPresenceChooserPriv));
152 static void
153 presence_chooser_create_model (EmpathyPresenceChooser *self)
155 GtkListStore *store;
156 char *custom_message;
157 int i;
159 store = gtk_list_store_new (N_COLUMNS,
160 G_TYPE_STRING, /* COL_STATUS_TEXT */
161 G_TYPE_STRING, /* COL_STATE_ICON_NAME */
162 G_TYPE_UINT, /* COL_STATE */
163 G_TYPE_STRING, /* COL_DISPLAY_MARKUP */
164 G_TYPE_BOOLEAN, /* COL_STATUS_CUSTOMISABLE */
165 G_TYPE_INT); /* COL_TYPE */
167 custom_message = g_strdup_printf ("<i>%s</i>", _("Custom Message..."));
169 for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
170 GList *list, *l;
171 const char *status, *icon_name;
173 status = empathy_presence_get_default_message (states[i].state);
174 icon_name = empathy_icon_name_for_presence (states[i].state);
176 gtk_list_store_insert_with_values (store, NULL, -1,
177 COL_STATUS_TEXT, status,
178 COL_STATE_ICON_NAME, icon_name,
179 COL_STATE, states[i].state,
180 COL_DISPLAY_MARKUP, status,
181 COL_STATUS_CUSTOMISABLE, states[i].customisable,
182 COL_TYPE, ENTRY_TYPE_BUILTIN,
183 -1);
185 if (states[i].customisable) {
186 /* Set custom messages if wanted */
187 list = empathy_status_presets_get (states[i].state, -1);
188 list = g_list_sort (list, (GCompareFunc) g_utf8_collate);
189 for (l = list; l; l = l->next) {
190 gtk_list_store_insert_with_values (store,
191 NULL, -1,
192 COL_STATUS_TEXT, l->data,
193 COL_STATE_ICON_NAME, icon_name,
194 COL_STATE, states[i].state,
195 COL_DISPLAY_MARKUP, l->data,
196 COL_STATUS_CUSTOMISABLE, TRUE,
197 COL_TYPE, ENTRY_TYPE_SAVED,
198 -1);
200 g_list_free (list);
202 gtk_list_store_insert_with_values (store, NULL, -1,
203 COL_STATUS_TEXT, _("Custom Message..."),
204 COL_STATE_ICON_NAME, icon_name,
205 COL_STATE, states[i].state,
206 COL_DISPLAY_MARKUP, custom_message,
207 COL_STATUS_CUSTOMISABLE, TRUE,
208 COL_TYPE, ENTRY_TYPE_CUSTOM,
209 -1);
214 /* add a separator */
215 gtk_list_store_insert_with_values (store, NULL, -1,
216 COL_TYPE, ENTRY_TYPE_SEPARATOR,
217 -1);
219 gtk_list_store_insert_with_values (store, NULL, -1,
220 COL_STATUS_TEXT, _("Edit Custom Messages..."),
221 COL_STATE_ICON_NAME, GTK_STOCK_EDIT,
222 COL_DISPLAY_MARKUP, _("Edit Custom Messages..."),
223 COL_TYPE, ENTRY_TYPE_EDIT_CUSTOM,
224 -1);
226 g_free (custom_message);
228 gtk_combo_box_set_model (GTK_COMBO_BOX (self), GTK_TREE_MODEL (store));
229 g_object_unref (store);
232 static void
233 presence_chooser_popup_shown_cb (GObject *self,
234 GParamSpec *pspec,
235 gpointer user_data)
237 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
238 gboolean shown;
240 g_object_get (self, "popup-shown", &shown, NULL);
241 if (!shown) {
242 return;
245 /* see presence_chooser_entry_focus_out_cb () for what this does */
246 if (priv->focus_out_idle_source != 0) {
247 g_source_remove (priv->focus_out_idle_source);
248 priv->focus_out_idle_source = 0;
251 presence_chooser_create_model (EMPATHY_PRESENCE_CHOOSER (self));
254 static PresenceChooserEntryType
255 presence_chooser_get_entry_type (EmpathyPresenceChooser *self)
257 GtkTreeIter iter;
258 PresenceChooserEntryType type = -1;
260 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter)) {
261 type = ENTRY_TYPE_CUSTOM;
263 else {
264 GtkTreeModel *model;
266 model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
267 gtk_tree_model_get (model, &iter,
268 COL_TYPE, &type,
269 -1);
272 return type;
275 static TpConnectionPresenceType
276 get_state_and_status (EmpathyPresenceChooser *self,
277 gchar **status)
279 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
280 TpConnectionPresenceType state;
281 gchar *tmp;
283 state = tp_account_manager_get_most_available_presence (
284 priv->account_manager, NULL, &tmp);
285 if (EMP_STR_EMPTY (tmp)) {
286 /* no message, use the default message */
287 g_free (tmp);
288 tmp = g_strdup (empathy_presence_get_default_message (state));
291 if (status != NULL)
292 *status = tmp;
293 else
294 g_free (tmp);
296 return state;
299 static gboolean
300 presence_chooser_is_preset (EmpathyPresenceChooser *self)
302 TpConnectionPresenceType state;
303 char *status;
304 GList *presets, *l;
305 gboolean match = FALSE;
307 state = get_state_and_status (self, &status);
309 presets = empathy_status_presets_get (state, -1);
310 for (l = presets; l; l = l->next) {
311 char *preset = (char *) l->data;
313 if (!tp_strdiff (status, preset)) {
314 match = TRUE;
315 break;
319 g_list_free (presets);
321 DEBUG ("is_preset(%i, %s) = %i", state, status, match);
323 g_free (status);
324 return match;
327 static void
328 presence_chooser_set_favorite_icon (EmpathyPresenceChooser *self)
330 GtkWidget *entry;
331 PresenceChooserEntryType type;
333 entry = gtk_bin_get_child (GTK_BIN (self));
334 type = presence_chooser_get_entry_type (self);
336 if (type == ENTRY_TYPE_CUSTOM || type == ENTRY_TYPE_SAVED) {
337 if (presence_chooser_is_preset (self)) {
338 /* saved entries can be removed from the list */
339 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
340 GTK_ENTRY_ICON_SECONDARY,
341 "empathy-starred");
342 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
343 GTK_ENTRY_ICON_SECONDARY,
344 _("Click to remove this status as a favorite"));
346 else {
347 /* custom entries can be favorited */
348 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
349 GTK_ENTRY_ICON_SECONDARY,
350 "empathy-unstarred");
351 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
352 GTK_ENTRY_ICON_SECONDARY,
353 _("Click to make this status a favorite"));
356 else {
357 /* built-in entries cannot be favorited */
358 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
359 GTK_ENTRY_ICON_SECONDARY,
360 NULL);
361 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
362 GTK_ENTRY_ICON_SECONDARY,
363 NULL);
367 static void
368 presence_chooser_set_status_editing (EmpathyPresenceChooser *self,
369 gboolean editing)
371 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
372 GtkWidget *entry;
374 if (priv->block_set_editing) {
375 return;
378 entry = gtk_bin_get_child (GTK_BIN (self));
379 if (editing) {
380 priv->editing_status = TRUE;
382 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
383 GTK_ENTRY_ICON_SECONDARY,
384 GTK_STOCK_OK);
385 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
386 GTK_ENTRY_ICON_SECONDARY,
387 _("Set status"));
388 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
389 GTK_ENTRY_ICON_PRIMARY,
390 FALSE);
391 } else {
392 GtkWidget *window;
394 presence_chooser_set_favorite_icon (self);
395 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
396 GTK_ENTRY_ICON_PRIMARY,
397 TRUE);
399 /* attempt to get the toplevel for this widget */
400 window = gtk_widget_get_toplevel (GTK_WIDGET (self));
401 if (gtk_widget_is_toplevel (window) && GTK_IS_WINDOW (window)) {
402 /* unset the focus */
403 gtk_window_set_focus (GTK_WINDOW (window), NULL);
406 /* see presence_chooser_entry_focus_out_cb ()
407 * for what this does */
408 if (priv->focus_out_idle_source != 0) {
409 g_source_remove (priv->focus_out_idle_source);
410 priv->focus_out_idle_source = 0;
413 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
415 priv->editing_status = FALSE;
419 static void
420 mc_set_custom_state (EmpathyPresenceChooser *self)
422 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
423 GtkWidget *entry;
424 const char *status;
426 entry = gtk_bin_get_child (GTK_BIN (self));
427 /* update the status with MC */
428 status = gtk_entry_get_text (GTK_ENTRY (entry));
430 DEBUG ("Sending state to MC-> %d (%s)", priv->state, status);
432 empathy_idle_set_presence (priv->idle, priv->state, status);
435 static void
436 ui_set_custom_state (EmpathyPresenceChooser *self,
437 TpConnectionPresenceType state,
438 const char *status)
440 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
441 GtkWidget *entry;
442 const char *icon_name;
444 entry = gtk_bin_get_child (GTK_BIN (self));
446 priv->block_set_editing++;
447 priv->block_changed++;
449 icon_name = empathy_icon_name_for_presence (state);
450 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
451 GTK_ENTRY_ICON_PRIMARY,
452 icon_name);
453 gtk_entry_set_text (GTK_ENTRY (entry), status == NULL ? "" : status);
454 presence_chooser_set_favorite_icon (self);
456 priv->block_changed--;
457 priv->block_set_editing--;
460 static void
461 presence_chooser_reset_status (EmpathyPresenceChooser *self)
463 /* recover the status that was unset */
464 presence_chooser_set_status_editing (self, FALSE);
465 presence_chooser_presence_changed_cb (self);
468 static void
469 presence_chooser_entry_icon_release_cb (EmpathyPresenceChooser *self,
470 GtkEntryIconPosition icon_pos,
471 GdkEvent *event,
472 GtkEntry *entry)
474 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
476 if (priv->editing_status) {
477 presence_chooser_set_status_editing (self, FALSE);
478 mc_set_custom_state (self);
480 else {
481 PresenceChooserEntryType type;
482 TpConnectionPresenceType state;
483 char *status;
485 type = presence_chooser_get_entry_type (self);
486 state = get_state_and_status (self, &status);
488 if (!empathy_status_presets_is_valid (state)) {
489 /* It doesn't make sense to add such presence as favorite */
490 g_free (status);
491 return;
494 if (presence_chooser_is_preset (self)) {
495 /* remove the entry */
496 DEBUG ("REMOVING PRESET (%i, %s)", state, status);
497 empathy_status_presets_remove (state, status);
499 else {
500 /* save the entry */
501 DEBUG ("SAVING PRESET (%i, %s)", state, status);
502 empathy_status_presets_set_last (state, status);
505 /* update the icon */
506 presence_chooser_set_favorite_icon (self);
507 g_free (status);
511 static void
512 presence_chooser_entry_activate_cb (EmpathyPresenceChooser *self,
513 GtkEntry *entry)
515 presence_chooser_set_status_editing (self, FALSE);
516 mc_set_custom_state (self);
519 static gboolean
520 presence_chooser_entry_key_press_event_cb (EmpathyPresenceChooser *self,
521 GdkEventKey *event,
522 GtkWidget *entry)
524 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
526 if (priv->editing_status && event->keyval == GDK_Escape) {
527 /* the user pressed Escape, undo the editing */
528 presence_chooser_reset_status (self);
529 return TRUE;
531 else if (event->keyval == GDK_Up || event->keyval == GDK_Down) {
532 /* ignore */
533 return TRUE;
536 return FALSE; /* send this event elsewhere */
539 static gboolean
540 presence_chooser_entry_button_press_event_cb (EmpathyPresenceChooser *self,
541 GdkEventButton *event,
542 GtkWidget *entry)
544 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
546 if (!priv->editing_status &&
547 event->button == 1 &&
548 !gtk_widget_has_focus (entry)) {
549 gtk_widget_grab_focus (entry);
550 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
552 return TRUE;
555 return FALSE;
558 static void
559 presence_chooser_entry_changed_cb (EmpathyPresenceChooser *self,
560 GtkEntry *entry)
562 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
564 if (priv->block_changed){
565 return;
568 /* the combo is being edited to a custom entry */
569 if (!priv->editing_status) {
570 presence_chooser_set_status_editing (self, TRUE);
574 static void
575 presence_chooser_changed_cb (GtkComboBox *self, gpointer user_data)
577 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
578 GtkTreeIter iter;
579 char *icon_name;
580 TpConnectionPresenceType new_state;
581 gboolean customisable = TRUE;
582 PresenceChooserEntryType type = -1;
583 GtkWidget *entry;
584 GtkTreeModel *model;
586 if (priv->block_changed ||
587 !gtk_combo_box_get_active_iter (self, &iter)) {
588 return;
591 model = gtk_combo_box_get_model (self);
592 gtk_tree_model_get (model, &iter,
593 COL_STATE_ICON_NAME, &icon_name,
594 COL_STATE, &new_state,
595 COL_STATUS_CUSTOMISABLE, &customisable,
596 COL_TYPE, &type,
597 -1);
599 entry = gtk_bin_get_child (GTK_BIN (self));
601 /* some types of status aren't editable, set the editability of the
602 * entry appropriately. Unless we're just about to reset it anyway,
603 * in which case, don't fiddle with it */
604 if (type != ENTRY_TYPE_EDIT_CUSTOM) {
605 gtk_editable_set_editable (GTK_EDITABLE (entry), customisable);
606 priv->state = new_state;
609 if (type == ENTRY_TYPE_EDIT_CUSTOM) {
610 GtkWidget *window, *dialog;
612 presence_chooser_reset_status (EMPATHY_PRESENCE_CHOOSER (self));
614 /* attempt to get the toplevel for this widget */
615 window = gtk_widget_get_toplevel (GTK_WIDGET (self));
616 if (!gtk_widget_is_toplevel (window) || !GTK_IS_WINDOW (window)) {
617 window = NULL;
620 dialog = empathy_status_preset_dialog_new (GTK_WINDOW (window));
621 gtk_dialog_run (GTK_DIALOG (dialog));
622 gtk_widget_destroy (dialog);
624 else if (type == ENTRY_TYPE_CUSTOM) {
625 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
626 GTK_ENTRY_ICON_PRIMARY,
627 icon_name);
629 /* preseed the status */
630 if (priv->previous_type == ENTRY_TYPE_BUILTIN) {
631 /* if their previous entry was a builtin, don't
632 * preseed */
633 gtk_entry_set_text (GTK_ENTRY (entry), "");
634 } else {
635 /* else preseed the text of their currently entered
636 * status message */
637 char *status;
639 get_state_and_status (EMPATHY_PRESENCE_CHOOSER (self),
640 &status);
641 gtk_entry_set_text (GTK_ENTRY (entry), status);
642 g_free (status);
645 /* grab the focus */
646 gtk_widget_grab_focus (entry);
647 } else {
648 char *status;
650 /* just in case we were setting a new status when
651 * things were changed */
652 presence_chooser_set_status_editing (
653 EMPATHY_PRESENCE_CHOOSER (self),
654 FALSE);
655 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
656 GTK_ENTRY_ICON_PRIMARY,
657 icon_name);
659 gtk_tree_model_get (model, &iter,
660 COL_STATUS_TEXT, &status,
661 -1);
663 empathy_idle_set_presence (priv->idle, priv->state, status);
665 g_free (status);
668 if (type != ENTRY_TYPE_EDIT_CUSTOM) {
669 priv->previous_type = type;
671 g_free (icon_name);
674 static gboolean
675 combo_row_separator_func (GtkTreeModel *model,
676 GtkTreeIter *iter,
677 gpointer data)
679 PresenceChooserEntryType type;
681 gtk_tree_model_get (model, iter,
682 COL_TYPE, &type,
683 -1);
685 return (type == ENTRY_TYPE_SEPARATOR);
688 static gboolean
689 presence_chooser_entry_focus_out_idle_cb (gpointer user_data)
691 EmpathyPresenceChooser *chooser;
692 GtkWidget *entry;
694 DEBUG ("Autocommiting status message");
696 chooser = EMPATHY_PRESENCE_CHOOSER (user_data);
697 entry = gtk_bin_get_child (GTK_BIN (chooser));
699 presence_chooser_entry_activate_cb (chooser, GTK_ENTRY (entry));
701 return FALSE;
704 static gboolean
705 presence_chooser_entry_focus_out_cb (EmpathyPresenceChooser *chooser,
706 GdkEventFocus *event,
707 GtkEntry *entry)
709 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
711 if (priv->editing_status) {
712 /* this seems a bit evil and maybe it will be fragile,
713 * someone should think of a better way to do it.
715 * The entry has focused out, but we don't know where the focus
716 * has gone. If it goes to the combo box, we don't want to
717 * do anything. If it's gone anywhere else, we want to commit
718 * the result.
720 * Thus we install this idle handler and store its source.
721 * If the source is scheduled when the popup handler runs,
722 * it will remove it, else the callback will commit the result.
724 priv->focus_out_idle_source = g_idle_add (
725 presence_chooser_entry_focus_out_idle_cb,
726 chooser);
729 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
731 return FALSE;
734 static void
735 update_sensitivity_am_prepared_cb (GObject *source_object,
736 GAsyncResult *result,
737 gpointer user_data)
739 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
740 EmpathyPresenceChooser *chooser = user_data;
741 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
742 gboolean sensitive = FALSE;
743 GList *accounts, *l;
744 GError *error = NULL;
746 if (!tp_account_manager_prepare_finish (manager, result, &error)) {
747 DEBUG ("Failed to prepare account manager: %s", error->message);
748 g_error_free (error);
749 return;
752 accounts = tp_account_manager_get_valid_accounts (manager);
754 for (l = accounts ; l != NULL ; l = g_list_next (l)) {
755 TpAccount *a = TP_ACCOUNT (l->data);
757 if (tp_account_is_enabled (a)) {
758 sensitive = TRUE;
759 break;
763 g_list_free (accounts);
765 if (!empathy_connectivity_is_online (priv->connectivity))
766 sensitive = FALSE;
768 gtk_widget_set_sensitive (GTK_WIDGET (chooser), sensitive);
770 presence_chooser_presence_changed_cb (chooser);
773 static void
774 presence_chooser_update_sensitivity (EmpathyPresenceChooser *chooser)
776 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
778 tp_account_manager_prepare_async (priv->account_manager, NULL,
779 update_sensitivity_am_prepared_cb,
780 chooser);
783 static void
784 presence_chooser_account_manager_account_validity_changed_cb (
785 TpAccountManager *manager,
786 TpAccount *account,
787 gboolean valid,
788 EmpathyPresenceChooser *chooser)
790 presence_chooser_update_sensitivity (chooser);
793 static void
794 presence_chooser_account_manager_account_changed_cb (
795 TpAccountManager *manager,
796 TpAccount *account,
797 EmpathyPresenceChooser *chooser)
799 presence_chooser_update_sensitivity (chooser);
802 static void
803 presence_chooser_connectivity_state_change (EmpathyConnectivity *connectivity,
804 gboolean new_online,
805 EmpathyPresenceChooser *chooser)
807 presence_chooser_update_sensitivity (chooser);
810 static void
811 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
813 EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
814 EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
815 GtkWidget *entry;
816 GtkCellRenderer *renderer;
818 chooser->priv = priv;
820 presence_chooser_create_model (chooser);
822 gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (chooser),
823 COL_STATUS_TEXT);
824 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
825 combo_row_separator_func,
826 NULL, NULL);
828 entry = gtk_bin_get_child (GTK_BIN (chooser));
829 gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
830 GTK_ENTRY_ICON_PRIMARY,
831 FALSE);
833 g_signal_connect_swapped (entry, "icon-release",
834 G_CALLBACK (presence_chooser_entry_icon_release_cb),
835 chooser);
836 g_signal_connect_swapped (entry, "activate",
837 G_CALLBACK (presence_chooser_entry_activate_cb),
838 chooser);
839 g_signal_connect_swapped (entry, "key-press-event",
840 G_CALLBACK (presence_chooser_entry_key_press_event_cb),
841 chooser);
842 g_signal_connect_swapped (entry, "button-press-event",
843 G_CALLBACK (presence_chooser_entry_button_press_event_cb),
844 chooser);
846 gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
848 renderer = gtk_cell_renderer_pixbuf_new ();
849 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, FALSE);
850 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
851 "icon-name", COL_STATE_ICON_NAME,
852 NULL);
853 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
855 renderer = gtk_cell_renderer_text_new ();
856 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, TRUE);
857 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
858 "markup", COL_DISPLAY_MARKUP,
859 NULL);
861 g_signal_connect (chooser, "notify::popup-shown",
862 G_CALLBACK (presence_chooser_popup_shown_cb), NULL);
863 g_signal_connect (chooser, "changed",
864 G_CALLBACK (presence_chooser_changed_cb), NULL);
865 g_signal_connect_swapped (entry, "changed",
866 G_CALLBACK (presence_chooser_entry_changed_cb),
867 chooser);
868 g_signal_connect_swapped (entry, "focus-out-event",
869 G_CALLBACK (presence_chooser_entry_focus_out_cb),
870 chooser);
872 priv->idle = empathy_idle_dup_singleton ();
874 priv->account_manager = tp_account_manager_dup ();
875 g_signal_connect_swapped (priv->account_manager,
876 "most-available-presence-changed",
877 G_CALLBACK (presence_chooser_presence_changed_cb),
878 chooser);
880 empathy_signal_connect_weak (priv->account_manager, "account-validity-changed",
881 G_CALLBACK (presence_chooser_account_manager_account_validity_changed_cb),
882 G_OBJECT (chooser));
883 empathy_signal_connect_weak (priv->account_manager, "account-removed",
884 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
885 G_OBJECT (chooser));
886 empathy_signal_connect_weak (priv->account_manager, "account-enabled",
887 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
888 G_OBJECT (chooser));
889 empathy_signal_connect_weak (priv->account_manager, "account-disabled",
890 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
891 G_OBJECT (chooser));
893 /* FIXME: this string sucks */
894 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser),
895 _("Set your presence and current status"));
897 priv->connectivity = empathy_connectivity_dup_singleton ();
898 empathy_signal_connect_weak (priv->connectivity,
899 "state-change",
900 G_CALLBACK (presence_chooser_connectivity_state_change),
901 G_OBJECT (chooser));
903 presence_chooser_update_sensitivity (chooser);
906 static void
907 presence_chooser_finalize (GObject *object)
909 EmpathyPresenceChooserPriv *priv;
911 priv = GET_PRIV (object);
913 if (priv->focus_out_idle_source) {
914 g_source_remove (priv->focus_out_idle_source);
917 if (priv->account_manager != NULL)
918 g_object_unref (priv->account_manager);
920 g_signal_handlers_disconnect_by_func (priv->idle,
921 presence_chooser_presence_changed_cb,
922 object);
923 g_object_unref (priv->idle);
925 g_object_unref (priv->connectivity);
927 G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
931 * empathy_presence_chooser_new:
933 * Creates a new #EmpathyPresenceChooser widget.
935 * Return value: A new #EmpathyPresenceChooser widget
937 GtkWidget *
938 empathy_presence_chooser_new (void)
940 GtkWidget *chooser;
942 chooser = g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER, NULL);
944 return chooser;
947 static void
948 presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
950 EmpathyPresenceChooserPriv *priv;
951 TpConnectionPresenceType state;
952 gchar *status;
953 GtkTreeModel *model;
954 GtkTreeIter iter;
955 gboolean valid, match_state = FALSE, match = FALSE;
956 GtkWidget *entry;
958 priv = GET_PRIV (chooser);
960 if (priv->editing_status) {
961 return;
964 state = get_state_and_status (chooser, &status);
965 priv->state = state;
967 /* An unset presence here doesn't make any sense. Force it to appear as
968 * offline. */
969 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
970 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
973 /* look through the model and attempt to find a matching state */
974 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
975 for (valid = gtk_tree_model_get_iter_first (model, &iter);
976 valid;
977 valid = gtk_tree_model_iter_next (model, &iter)) {
978 int m_type;
979 TpConnectionPresenceType m_state;
980 char *m_status;
982 gtk_tree_model_get (model, &iter,
983 COL_STATE, &m_state,
984 COL_TYPE, &m_type,
985 -1);
987 if (m_type == ENTRY_TYPE_CUSTOM ||
988 m_type == ENTRY_TYPE_SEPARATOR ||
989 m_type == ENTRY_TYPE_EDIT_CUSTOM) {
990 continue;
992 else if (!match_state && state == m_state) {
993 /* we are now in the section that can contain our
994 * match */
995 match_state = TRUE;
997 else if (match_state && state != m_state) {
998 /* we have passed the section that can contain our
999 * match */
1000 break;
1003 gtk_tree_model_get (model, &iter,
1004 COL_STATUS_TEXT, &m_status,
1005 -1);
1007 match = !tp_strdiff (status, m_status);
1009 g_free (m_status);
1011 if (match) break;
1015 if (match) {
1016 priv->block_changed++;
1017 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
1018 presence_chooser_set_favorite_icon (chooser);
1019 priv->block_changed--;
1021 else {
1022 ui_set_custom_state (chooser, state, status);
1025 entry = gtk_bin_get_child (GTK_BIN (chooser));
1026 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
1027 GTK_ENTRY_ICON_PRIMARY,
1028 empathy_icon_name_for_presence (state));
1030 entry = gtk_bin_get_child (GTK_BIN (chooser));
1031 gtk_editable_set_editable (GTK_EDITABLE (entry),
1032 state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
1034 g_free (status);
1038 * empathy_presence_chooser_create_menu:
1040 * Creates a new #GtkMenu allowing users to change their presence from a menu.
1042 * Return value: a new #GtkMenu for changing presence in a menu.
1044 GtkWidget *
1045 empathy_presence_chooser_create_menu (void)
1047 const gchar *status;
1048 GtkWidget *menu;
1049 GtkWidget *item;
1050 GtkWidget *image;
1051 guint i;
1053 menu = gtk_menu_new ();
1055 for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
1056 GList *list, *l;
1058 status = empathy_presence_get_default_message (states[i].state);
1059 presence_chooser_menu_add_item (menu,
1060 status,
1061 states[i].state);
1063 if (states[i].customisable) {
1064 /* Set custom messages if wanted */
1065 list = empathy_status_presets_get (states[i].state, 5);
1066 for (l = list; l; l = l->next) {
1067 presence_chooser_menu_add_item (menu,
1068 l->data,
1069 states[i].state);
1071 g_list_free (list);
1076 /* Separator */
1077 item = gtk_menu_item_new ();
1078 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1079 gtk_widget_show (item);
1081 /* Custom messages */
1082 item = gtk_image_menu_item_new_with_label (_("Custom messages..."));
1083 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
1084 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1085 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1086 gtk_widget_show (image);
1087 gtk_widget_show (item);
1089 g_signal_connect (item,
1090 "activate",
1091 G_CALLBACK (presence_chooser_custom_activate_cb),
1092 NULL);
1094 return menu;
1097 static void
1098 presence_chooser_menu_add_item (GtkWidget *menu,
1099 const gchar *str,
1100 TpConnectionPresenceType state)
1102 GtkWidget *item;
1103 GtkWidget *image;
1104 const gchar *icon_name;
1106 item = gtk_image_menu_item_new_with_label (str);
1107 icon_name = empathy_icon_name_for_presence (state);
1109 g_signal_connect (item, "activate",
1110 G_CALLBACK (presence_chooser_noncustom_activate_cb),
1111 NULL);
1113 image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
1114 gtk_widget_show (image);
1116 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1117 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
1118 gtk_widget_show (item);
1120 g_object_set_data_full (G_OBJECT (item),
1121 "status", g_strdup (str),
1122 (GDestroyNotify) g_free);
1124 g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
1126 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1129 static void
1130 presence_chooser_noncustom_activate_cb (GtkWidget *item,
1131 gpointer user_data)
1133 TpConnectionPresenceType state;
1134 const gchar *status;
1136 status = g_object_get_data (G_OBJECT (item), "status");
1137 state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
1139 presence_chooser_set_state (state, status);
1142 static void
1143 presence_chooser_set_state (TpConnectionPresenceType state,
1144 const gchar *status)
1146 EmpathyIdle *idle;
1148 idle = empathy_idle_dup_singleton ();
1149 empathy_idle_set_presence (idle, state, status);
1150 g_object_unref (idle);
1153 static void
1154 presence_chooser_custom_activate_cb (GtkWidget *item,
1155 gpointer user_data)
1157 GtkWidget *dialog;
1159 dialog = empathy_status_preset_dialog_new (NULL);
1160 gtk_dialog_run (GTK_DIALOG (dialog));
1161 gtk_widget_destroy (dialog);