1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
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 * Danielle Madeley <danielle.madeley@collabora.co.uk>
32 #include <glib/gi18n-lib.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-presence-manager.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"
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
63 * EmpathyAccountChooser:
64 * @parent: parent object
66 * Widget which extends #GtkComboBoxEntry to change presence.
69 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyPresenceChooser)
71 /* For custom message dialog */
79 /* For combobox's model */
85 COL_STATUS_CUSTOMISABLE,
95 ENTRY_TYPE_EDIT_CUSTOM,
96 } PresenceChooserEntryType;
99 EmpathyPresenceManager *presence_mgr;
100 EmpathyConnectivity *connectivity;
102 gboolean editing_status;
103 int block_set_editing;
105 guint focus_out_idle_source;
107 TpConnectionPresenceType state;
108 PresenceChooserEntryType previous_type;
110 TpAccountManager *account_manager;
111 GdkPixbuf *not_favorite_pixbuf;
112 } EmpathyPresenceChooserPriv;
114 /* States to be listed in the menu.
115 * Each state has a boolean telling if it can have custom message */
116 static struct { TpConnectionPresenceType state;
117 gboolean customisable;
118 } states[] = { { TP_CONNECTION_PRESENCE_TYPE_AVAILABLE, TRUE } ,
119 { TP_CONNECTION_PRESENCE_TYPE_BUSY, TRUE },
120 { TP_CONNECTION_PRESENCE_TYPE_AWAY, TRUE },
121 { TP_CONNECTION_PRESENCE_TYPE_HIDDEN, FALSE },
122 { TP_CONNECTION_PRESENCE_TYPE_OFFLINE, FALSE},
123 { TP_CONNECTION_PRESENCE_TYPE_UNSET, },
126 static void presence_chooser_constructed (GObject *object);
127 static void presence_chooser_finalize (GObject *object);
128 static void presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser);
129 static void presence_chooser_menu_add_item (GtkWidget *menu,
131 TpConnectionPresenceType state);
132 static void presence_chooser_noncustom_activate_cb (GtkWidget *item,
134 static void presence_chooser_set_state (TpConnectionPresenceType state,
135 const gchar *status);
136 static void presence_chooser_custom_activate_cb (GtkWidget *item,
139 G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_COMBO_BOX);
142 empathy_presence_chooser_class_init (EmpathyPresenceChooserClass *klass)
144 GObjectClass *object_class = G_OBJECT_CLASS (klass);
146 object_class->constructed = presence_chooser_constructed;
147 object_class->finalize = presence_chooser_finalize;
149 g_type_class_add_private (object_class, sizeof (EmpathyPresenceChooserPriv));
153 presence_chooser_create_model (EmpathyPresenceChooser *self)
156 char *custom_message;
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++) {
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,
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,
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,
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,
214 /* add a separator */
215 gtk_list_store_insert_with_values (store, NULL, -1,
216 COL_TYPE, ENTRY_TYPE_SEPARATOR,
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,
226 g_free (custom_message);
228 gtk_combo_box_set_model (GTK_COMBO_BOX (self), GTK_TREE_MODEL (store));
229 g_object_unref (store);
233 presence_chooser_popup_shown_cb (GObject *self,
237 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
240 g_object_get (self, "popup-shown", &shown, NULL);
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)
258 PresenceChooserEntryType type = -1;
260 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter)) {
261 type = ENTRY_TYPE_CUSTOM;
266 model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
267 gtk_tree_model_get (model, &iter,
275 static TpConnectionPresenceType
276 get_state_and_status (EmpathyPresenceChooser *self,
279 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
280 TpConnectionPresenceType state;
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 */
288 tmp = g_strdup (empathy_presence_get_default_message (state));
300 presence_chooser_is_preset (EmpathyPresenceChooser *self)
302 TpConnectionPresenceType state;
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)) {
319 g_list_free (presets);
321 DEBUG ("is_preset(%i, %s) = %i", state, status, match);
328 presence_chooser_set_favorite_icon (EmpathyPresenceChooser *self)
330 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
332 PresenceChooserEntryType type;
334 entry = gtk_bin_get_child (GTK_BIN (self));
335 type = presence_chooser_get_entry_type (self);
337 if (type == ENTRY_TYPE_CUSTOM || type == ENTRY_TYPE_SAVED) {
338 if (presence_chooser_is_preset (self)) {
339 /* saved entries can be removed from the list */
340 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
341 GTK_ENTRY_ICON_SECONDARY,
343 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
344 GTK_ENTRY_ICON_SECONDARY,
345 _("Click to remove this status as a favorite"));
347 else if (priv->not_favorite_pixbuf != NULL) {
348 /* custom entries can be favorited */
349 gtk_entry_set_icon_from_pixbuf (GTK_ENTRY (entry),
350 GTK_ENTRY_ICON_SECONDARY,
351 priv->not_favorite_pixbuf);
352 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
353 GTK_ENTRY_ICON_SECONDARY,
354 _("Click to make this status a favorite"));
358 /* built-in entries cannot be favorited */
359 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
360 GTK_ENTRY_ICON_SECONDARY,
362 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
363 GTK_ENTRY_ICON_SECONDARY,
369 presence_chooser_set_status_editing (EmpathyPresenceChooser *self,
372 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
375 if (priv->block_set_editing) {
379 entry = gtk_bin_get_child (GTK_BIN (self));
381 priv->editing_status = TRUE;
383 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
384 GTK_ENTRY_ICON_SECONDARY,
386 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
387 GTK_ENTRY_ICON_SECONDARY,
389 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
390 GTK_ENTRY_ICON_PRIMARY,
395 presence_chooser_set_favorite_icon (self);
396 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
397 GTK_ENTRY_ICON_PRIMARY,
400 /* attempt to get the toplevel for this widget */
401 window = gtk_widget_get_toplevel (GTK_WIDGET (self));
402 if (gtk_widget_is_toplevel (window) && GTK_IS_WINDOW (window)) {
403 /* unset the focus */
404 gtk_window_set_focus (GTK_WINDOW (window), NULL);
407 /* see presence_chooser_entry_focus_out_cb ()
408 * for what this does */
409 if (priv->focus_out_idle_source != 0) {
410 g_source_remove (priv->focus_out_idle_source);
411 priv->focus_out_idle_source = 0;
414 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
416 priv->editing_status = FALSE;
421 mc_set_custom_state (EmpathyPresenceChooser *self)
423 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
427 entry = gtk_bin_get_child (GTK_BIN (self));
428 /* update the status with MC */
429 status = gtk_entry_get_text (GTK_ENTRY (entry));
431 DEBUG ("Sending state to MC-> %d (%s)", priv->state, status);
433 empathy_presence_manager_set_presence (priv->presence_mgr, priv->state, status);
437 ui_set_custom_state (EmpathyPresenceChooser *self,
438 TpConnectionPresenceType state,
441 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
443 const char *icon_name;
445 entry = gtk_bin_get_child (GTK_BIN (self));
447 priv->block_set_editing++;
448 priv->block_changed++;
450 icon_name = empathy_icon_name_for_presence (state);
451 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
452 GTK_ENTRY_ICON_PRIMARY,
454 gtk_entry_set_text (GTK_ENTRY (entry), status == NULL ? "" : status);
455 presence_chooser_set_favorite_icon (self);
457 priv->block_changed--;
458 priv->block_set_editing--;
462 presence_chooser_reset_status (EmpathyPresenceChooser *self)
464 /* recover the status that was unset */
465 presence_chooser_set_status_editing (self, FALSE);
466 presence_chooser_presence_changed_cb (self);
470 presence_chooser_entry_icon_release_cb (EmpathyPresenceChooser *self,
471 GtkEntryIconPosition icon_pos,
475 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
477 if (priv->editing_status) {
478 presence_chooser_set_status_editing (self, FALSE);
479 mc_set_custom_state (self);
482 TpConnectionPresenceType state;
485 state = get_state_and_status (self, &status);
487 if (!empathy_status_presets_is_valid (state)) {
488 /* It doesn't make sense to add such presence as favorite */
493 if (presence_chooser_is_preset (self)) {
494 /* remove the entry */
495 DEBUG ("REMOVING PRESET (%i, %s)", state, status);
496 empathy_status_presets_remove (state, status);
500 DEBUG ("SAVING PRESET (%i, %s)", state, status);
501 empathy_status_presets_set_last (state, status);
504 /* update the icon */
505 presence_chooser_set_favorite_icon (self);
511 presence_chooser_entry_activate_cb (EmpathyPresenceChooser *self,
514 presence_chooser_set_status_editing (self, FALSE);
515 mc_set_custom_state (self);
519 presence_chooser_entry_key_press_event_cb (EmpathyPresenceChooser *self,
523 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
525 if (priv->editing_status && event->keyval == GDK_KEY_Escape) {
526 /* the user pressed Escape, undo the editing */
527 presence_chooser_reset_status (self);
530 else if (event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_Down) {
535 return FALSE; /* send this event elsewhere */
539 presence_chooser_entry_button_press_event_cb (EmpathyPresenceChooser *self,
540 GdkEventButton *event,
543 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
545 if (!priv->editing_status &&
546 event->button == 1 &&
547 !gtk_widget_has_focus (entry)) {
548 gtk_widget_grab_focus (entry);
549 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
558 presence_chooser_entry_changed_cb (EmpathyPresenceChooser *self,
561 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
563 if (priv->block_changed){
567 /* the combo is being edited to a custom entry */
568 if (!priv->editing_status) {
569 presence_chooser_set_status_editing (self, TRUE);
574 presence_chooser_changed_cb (GtkComboBox *self, gpointer user_data)
576 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
579 TpConnectionPresenceType new_state;
580 gboolean customisable = TRUE;
581 PresenceChooserEntryType type = -1;
585 if (priv->block_changed ||
586 !gtk_combo_box_get_active_iter (self, &iter)) {
590 model = gtk_combo_box_get_model (self);
591 gtk_tree_model_get (model, &iter,
592 COL_STATE_ICON_NAME, &icon_name,
593 COL_STATE, &new_state,
594 COL_STATUS_CUSTOMISABLE, &customisable,
598 entry = gtk_bin_get_child (GTK_BIN (self));
600 /* some types of status aren't editable, set the editability of the
601 * entry appropriately. Unless we're just about to reset it anyway,
602 * in which case, don't fiddle with it */
603 if (type != ENTRY_TYPE_EDIT_CUSTOM) {
604 gtk_editable_set_editable (GTK_EDITABLE (entry), customisable);
605 priv->state = new_state;
608 if (type == ENTRY_TYPE_EDIT_CUSTOM) {
609 GtkWidget *window, *dialog;
611 presence_chooser_reset_status (EMPATHY_PRESENCE_CHOOSER (self));
613 /* attempt to get the toplevel for this widget */
614 window = gtk_widget_get_toplevel (GTK_WIDGET (self));
615 if (!gtk_widget_is_toplevel (window) || !GTK_IS_WINDOW (window)) {
619 dialog = empathy_status_preset_dialog_new (GTK_WINDOW (window));
620 gtk_dialog_run (GTK_DIALOG (dialog));
621 gtk_widget_destroy (dialog);
623 else if (type == ENTRY_TYPE_CUSTOM) {
624 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
625 GTK_ENTRY_ICON_PRIMARY,
628 /* preseed the status */
629 if (priv->previous_type == ENTRY_TYPE_BUILTIN) {
630 /* if their previous entry was a builtin, don't
632 gtk_entry_set_text (GTK_ENTRY (entry), "");
634 /* else preseed the text of their currently entered
638 get_state_and_status (EMPATHY_PRESENCE_CHOOSER (self),
640 gtk_entry_set_text (GTK_ENTRY (entry), status);
645 gtk_widget_grab_focus (entry);
649 /* just in case we were setting a new status when
650 * things were changed */
651 presence_chooser_set_status_editing (
652 EMPATHY_PRESENCE_CHOOSER (self),
654 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
655 GTK_ENTRY_ICON_PRIMARY,
658 gtk_tree_model_get (model, &iter,
659 COL_STATUS_TEXT, &status,
662 empathy_presence_manager_set_presence (priv->presence_mgr, priv->state, status);
667 if (type != ENTRY_TYPE_EDIT_CUSTOM) {
668 priv->previous_type = type;
674 combo_row_separator_func (GtkTreeModel *model,
678 PresenceChooserEntryType type;
680 gtk_tree_model_get (model, iter,
684 return (type == ENTRY_TYPE_SEPARATOR);
688 presence_chooser_entry_focus_out_idle_cb (gpointer user_data)
690 EmpathyPresenceChooser *chooser;
693 DEBUG ("Autocommiting status message");
695 chooser = EMPATHY_PRESENCE_CHOOSER (user_data);
696 entry = gtk_bin_get_child (GTK_BIN (chooser));
698 presence_chooser_entry_activate_cb (chooser, GTK_ENTRY (entry));
704 presence_chooser_entry_focus_out_cb (EmpathyPresenceChooser *chooser,
705 GdkEventFocus *event,
708 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
710 if (priv->editing_status) {
711 /* this seems a bit evil and maybe it will be fragile,
712 * someone should think of a better way to do it.
714 * The entry has focused out, but we don't know where the focus
715 * has gone. If it goes to the combo box, we don't want to
716 * do anything. If it's gone anywhere else, we want to commit
719 * Thus we install this idle handler and store its source.
720 * If the source is scheduled when the popup handler runs,
721 * it will remove it, else the callback will commit the result.
723 priv->focus_out_idle_source = g_idle_add (
724 presence_chooser_entry_focus_out_idle_cb,
728 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
734 update_sensitivity_am_prepared_cb (GObject *source_object,
735 GAsyncResult *result,
738 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
739 EmpathyPresenceChooser *chooser = user_data;
740 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
741 gboolean sensitive = FALSE;
743 GError *error = NULL;
745 if (!tp_proxy_prepare_finish (manager, result, &error)) {
746 DEBUG ("Failed to prepare account manager: %s", error->message);
747 g_error_free (error);
751 accounts = tp_account_manager_get_valid_accounts (manager);
753 for (l = accounts ; l != NULL ; l = g_list_next (l)) {
754 TpAccount *a = TP_ACCOUNT (l->data);
756 if (tp_account_is_enabled (a)) {
762 g_list_free (accounts);
764 if (!empathy_connectivity_is_online (priv->connectivity))
767 gtk_widget_set_sensitive (GTK_WIDGET (chooser), sensitive);
769 presence_chooser_presence_changed_cb (chooser);
773 presence_chooser_update_sensitivity (EmpathyPresenceChooser *chooser)
775 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
777 tp_proxy_prepare_async (priv->account_manager, NULL,
778 update_sensitivity_am_prepared_cb,
783 presence_chooser_account_manager_account_validity_changed_cb (
784 TpAccountManager *manager,
787 EmpathyPresenceChooser *chooser)
789 presence_chooser_update_sensitivity (chooser);
793 presence_chooser_account_manager_account_changed_cb (
794 TpAccountManager *manager,
796 EmpathyPresenceChooser *chooser)
798 presence_chooser_update_sensitivity (chooser);
802 presence_chooser_connectivity_state_change (EmpathyConnectivity *connectivity,
804 EmpathyPresenceChooser *chooser)
806 presence_chooser_update_sensitivity (chooser);
809 /* Create a greyed version of the 'favorite' icon */
811 create_not_favorite_pixbuf (void)
813 GdkPixbuf *favorite, *result;
815 favorite = empathy_pixbuf_from_icon_name ("emblem-favorite",
818 if (favorite == NULL)
821 result = gdk_pixbuf_copy (favorite);
822 gdk_pixbuf_saturate_and_pixelate (favorite, result, 1.0, TRUE);
824 g_object_unref (favorite);
829 icon_theme_changed_cb (GtkIconTheme *icon_theme,
830 EmpathyPresenceChooser *self)
832 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
834 /* Theme has changed, recreate the not-favorite icon */
835 if (priv->not_favorite_pixbuf != NULL)
836 g_object_unref (priv->not_favorite_pixbuf);
837 priv->not_favorite_pixbuf = create_not_favorite_pixbuf ();
839 /* Update the icon */
840 presence_chooser_set_favorite_icon (self);
844 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
846 EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
847 EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
849 chooser->priv = priv;
851 /* Create the not-favorite icon */
852 priv->not_favorite_pixbuf = create_not_favorite_pixbuf ();
856 presence_chooser_constructed (GObject *object)
858 EmpathyPresenceChooser *chooser = EMPATHY_PRESENCE_CHOOSER (object);
859 EmpathyPresenceChooserPriv *priv = chooser->priv;
861 GtkCellRenderer *renderer;
863 tp_g_signal_connect_object (gtk_icon_theme_get_default (), "changed",
864 G_CALLBACK (icon_theme_changed_cb),
867 presence_chooser_create_model (chooser);
869 gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX (chooser),
871 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
872 combo_row_separator_func,
875 entry = gtk_bin_get_child (GTK_BIN (chooser));
876 gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
877 GTK_ENTRY_ICON_PRIMARY,
880 g_signal_connect_swapped (entry, "icon-release",
881 G_CALLBACK (presence_chooser_entry_icon_release_cb),
883 g_signal_connect_swapped (entry, "activate",
884 G_CALLBACK (presence_chooser_entry_activate_cb),
886 g_signal_connect_swapped (entry, "key-press-event",
887 G_CALLBACK (presence_chooser_entry_key_press_event_cb),
889 g_signal_connect_swapped (entry, "button-press-event",
890 G_CALLBACK (presence_chooser_entry_button_press_event_cb),
893 gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
895 renderer = gtk_cell_renderer_pixbuf_new ();
896 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, FALSE);
897 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
898 "icon-name", COL_STATE_ICON_NAME,
900 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
902 renderer = gtk_cell_renderer_text_new ();
903 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, TRUE);
904 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
905 "markup", COL_DISPLAY_MARKUP,
907 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
909 g_signal_connect (chooser, "notify::popup-shown",
910 G_CALLBACK (presence_chooser_popup_shown_cb), NULL);
911 g_signal_connect (chooser, "changed",
912 G_CALLBACK (presence_chooser_changed_cb), NULL);
913 g_signal_connect_swapped (entry, "changed",
914 G_CALLBACK (presence_chooser_entry_changed_cb),
916 g_signal_connect_swapped (entry, "focus-out-event",
917 G_CALLBACK (presence_chooser_entry_focus_out_cb),
920 priv->presence_mgr = empathy_presence_manager_dup_singleton ();
922 priv->account_manager = tp_account_manager_dup ();
923 g_signal_connect_swapped (priv->account_manager,
924 "most-available-presence-changed",
925 G_CALLBACK (presence_chooser_presence_changed_cb),
928 tp_g_signal_connect_object (priv->account_manager, "account-validity-changed",
929 G_CALLBACK (presence_chooser_account_manager_account_validity_changed_cb),
931 tp_g_signal_connect_object (priv->account_manager, "account-removed",
932 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
934 tp_g_signal_connect_object (priv->account_manager, "account-enabled",
935 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
937 tp_g_signal_connect_object (priv->account_manager, "account-disabled",
938 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
941 /* FIXME: this string sucks */
942 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser),
943 _("Set your presence and current status"));
945 priv->connectivity = empathy_connectivity_dup_singleton ();
946 tp_g_signal_connect_object (priv->connectivity,
948 G_CALLBACK (presence_chooser_connectivity_state_change),
951 presence_chooser_update_sensitivity (chooser);
955 presence_chooser_finalize (GObject *object)
957 EmpathyPresenceChooserPriv *priv;
959 priv = GET_PRIV (object);
961 if (priv->focus_out_idle_source) {
962 g_source_remove (priv->focus_out_idle_source);
965 if (priv->account_manager != NULL)
966 g_object_unref (priv->account_manager);
968 g_signal_handlers_disconnect_by_func (priv->presence_mgr,
969 presence_chooser_presence_changed_cb,
971 g_object_unref (priv->presence_mgr);
973 g_object_unref (priv->connectivity);
974 if (priv->not_favorite_pixbuf != NULL)
975 g_object_unref (priv->not_favorite_pixbuf);
977 G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
981 * empathy_presence_chooser_new:
983 * Creates a new #EmpathyPresenceChooser widget.
985 * Return value: A new #EmpathyPresenceChooser widget
988 empathy_presence_chooser_new (void)
990 /* FIXME, why can't this go in init ()? */
991 return g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER,
997 presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
999 EmpathyPresenceChooserPriv *priv;
1000 TpConnectionPresenceType state;
1002 GtkTreeModel *model;
1004 gboolean valid, match_state = FALSE, match = FALSE;
1007 priv = GET_PRIV (chooser);
1009 if (priv->editing_status) {
1013 state = get_state_and_status (chooser, &status);
1014 priv->state = state;
1016 /* An unset presence here doesn't make any sense. Force it to appear as
1018 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
1019 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
1022 /* look through the model and attempt to find a matching state */
1023 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
1024 for (valid = gtk_tree_model_get_iter_first (model, &iter);
1026 valid = gtk_tree_model_iter_next (model, &iter)) {
1028 TpConnectionPresenceType m_state;
1031 gtk_tree_model_get (model, &iter,
1032 COL_STATE, &m_state,
1036 if (m_type == ENTRY_TYPE_CUSTOM ||
1037 m_type == ENTRY_TYPE_SEPARATOR ||
1038 m_type == ENTRY_TYPE_EDIT_CUSTOM) {
1041 else if (!match_state && state == m_state) {
1042 /* we are now in the section that can contain our
1046 else if (match_state && state != m_state) {
1047 /* we have passed the section that can contain our
1052 gtk_tree_model_get (model, &iter,
1053 COL_STATUS_TEXT, &m_status,
1056 match = !tp_strdiff (status, m_status);
1065 priv->block_changed++;
1066 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
1067 presence_chooser_set_favorite_icon (chooser);
1068 priv->block_changed--;
1071 ui_set_custom_state (chooser, state, status);
1074 entry = gtk_bin_get_child (GTK_BIN (chooser));
1075 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
1076 GTK_ENTRY_ICON_PRIMARY,
1077 empathy_icon_name_for_presence (state));
1079 entry = gtk_bin_get_child (GTK_BIN (chooser));
1080 gtk_editable_set_editable (GTK_EDITABLE (entry),
1081 state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
1087 * empathy_presence_chooser_create_menu:
1089 * Creates a new #GtkMenu allowing users to change their presence from a menu.
1091 * Return value: a new #GtkMenu for changing presence in a menu.
1094 empathy_presence_chooser_create_menu (void)
1096 const gchar *status;
1102 menu = gtk_menu_new ();
1104 for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
1107 status = empathy_presence_get_default_message (states[i].state);
1108 presence_chooser_menu_add_item (menu,
1112 if (states[i].customisable) {
1113 /* Set custom messages if wanted */
1114 list = empathy_status_presets_get (states[i].state, 5);
1115 for (l = list; l; l = l->next) {
1116 presence_chooser_menu_add_item (menu,
1126 item = gtk_menu_item_new ();
1127 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1128 gtk_widget_show (item);
1130 /* Custom messages */
1131 item = gtk_image_menu_item_new_with_label (_("Custom messagesā¦"));
1132 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
1133 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1134 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1135 gtk_widget_show (image);
1136 gtk_widget_show (item);
1138 g_signal_connect (item,
1140 G_CALLBACK (presence_chooser_custom_activate_cb),
1147 presence_chooser_menu_add_item (GtkWidget *menu,
1149 TpConnectionPresenceType state)
1153 const gchar *icon_name;
1155 item = gtk_image_menu_item_new_with_label (str);
1156 icon_name = empathy_icon_name_for_presence (state);
1158 g_signal_connect (item, "activate",
1159 G_CALLBACK (presence_chooser_noncustom_activate_cb),
1162 image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
1163 gtk_widget_show (image);
1165 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1166 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
1167 gtk_widget_show (item);
1169 g_object_set_data_full (G_OBJECT (item),
1170 "status", g_strdup (str),
1171 (GDestroyNotify) g_free);
1173 g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
1175 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1179 presence_chooser_noncustom_activate_cb (GtkWidget *item,
1182 TpConnectionPresenceType state;
1183 const gchar *status;
1185 status = g_object_get_data (G_OBJECT (item), "status");
1186 state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
1188 presence_chooser_set_state (state, status);
1192 presence_chooser_set_state (TpConnectionPresenceType state,
1193 const gchar *status)
1195 EmpathyPresenceManager *presence_mgr;
1197 presence_mgr = empathy_presence_manager_dup_singleton ();
1198 empathy_presence_manager_set_presence (presence_mgr, state, status);
1199 g_object_unref (presence_mgr);
1203 presence_chooser_custom_activate_cb (GtkWidget *item,
1208 dialog = empathy_status_preset_dialog_new (NULL);
1209 gtk_dialog_run (GTK_DIALOG (dialog));
1210 gtk_widget_destroy (dialog);