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>
28 #include "empathy-presence-chooser.h"
30 #include <glib/gi18n-lib.h>
32 #include "empathy-presence-manager.h"
33 #include "empathy-status-presets.h"
34 #include "empathy-utils.h"
36 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
37 #include "empathy-debug.h"
39 #include "empathy-ui-utils.h"
40 #include "empathy-presence-chooser.h"
41 #include "empathy-status-preset-dialog.h"
44 * SECTION:empathy-presence-chooser
45 * @title:EmpathyPresenceChooser
46 * @short_description: A widget used to change presence
47 * @include: libempathy-gtk/empathy-presence-chooser.h
49 * #EmpathyPresenceChooser is a widget which extends #GtkComboBoxEntry
54 * EmpathyAccountChooser:
55 * @parent: parent object
57 * Widget which extends #GtkComboBoxEntry to change presence.
60 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyPresenceChooser)
62 /* For custom message dialog */
70 /* For combobox's model */
76 COL_STATUS_CUSTOMISABLE,
86 ENTRY_TYPE_EDIT_CUSTOM,
87 } PresenceChooserEntryType;
90 EmpathyPresenceManager *presence_mgr;
91 GNetworkMonitor *connectivity;
93 gboolean editing_status;
94 int block_set_editing;
96 guint focus_out_idle_source;
98 TpConnectionPresenceType state;
99 PresenceChooserEntryType previous_type;
101 TpAccountManager *account_manager;
102 } EmpathyPresenceChooserPriv;
104 /* States to be listed in the menu.
105 * Each state has a boolean telling if it can have custom message */
106 static struct { TpConnectionPresenceType state;
107 gboolean customisable;
108 } states[] = { { TP_CONNECTION_PRESENCE_TYPE_AVAILABLE, TRUE } ,
109 { TP_CONNECTION_PRESENCE_TYPE_BUSY, TRUE },
110 { TP_CONNECTION_PRESENCE_TYPE_AWAY, TRUE },
111 { TP_CONNECTION_PRESENCE_TYPE_HIDDEN, FALSE },
112 { TP_CONNECTION_PRESENCE_TYPE_OFFLINE, FALSE},
113 { TP_CONNECTION_PRESENCE_TYPE_UNSET, },
116 static void presence_chooser_constructed (GObject *object);
117 static void presence_chooser_finalize (GObject *object);
118 static void presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser);
119 static void presence_chooser_menu_add_item (GtkWidget *menu,
121 TpConnectionPresenceType state);
122 static void presence_chooser_noncustom_activate_cb (GtkWidget *item,
124 static void presence_chooser_set_state (TpConnectionPresenceType state,
125 const gchar *status);
126 static void presence_chooser_custom_activate_cb (GtkWidget *item,
129 G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_COMBO_BOX);
132 empathy_presence_chooser_class_init (EmpathyPresenceChooserClass *klass)
134 GObjectClass *object_class = G_OBJECT_CLASS (klass);
136 object_class->constructed = presence_chooser_constructed;
137 object_class->finalize = presence_chooser_finalize;
139 g_type_class_add_private (object_class, sizeof (EmpathyPresenceChooserPriv));
143 presence_chooser_create_model (EmpathyPresenceChooser *self)
146 char *custom_message;
149 store = gtk_list_store_new (N_COLUMNS,
150 G_TYPE_STRING, /* COL_STATUS_TEXT */
151 G_TYPE_STRING, /* COL_STATE_ICON_NAME */
152 G_TYPE_UINT, /* COL_STATE */
153 G_TYPE_STRING, /* COL_DISPLAY_MARKUP */
154 G_TYPE_BOOLEAN, /* COL_STATUS_CUSTOMISABLE */
155 G_TYPE_INT); /* COL_TYPE */
157 custom_message = g_strdup_printf ("<i>%s</i>", _("Custom Messageā¦"));
159 for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
161 const char *status, *icon_name;
163 status = empathy_presence_get_default_message (states[i].state);
164 icon_name = empathy_icon_name_for_presence (states[i].state);
166 gtk_list_store_insert_with_values (store, NULL, -1,
167 COL_STATUS_TEXT, status,
168 COL_STATE_ICON_NAME, icon_name,
169 COL_STATE, states[i].state,
170 COL_DISPLAY_MARKUP, status,
171 COL_STATUS_CUSTOMISABLE, states[i].customisable,
172 COL_TYPE, ENTRY_TYPE_BUILTIN,
175 if (states[i].customisable) {
176 /* Set custom messages if wanted */
177 list = empathy_status_presets_get (states[i].state, -1);
178 list = g_list_sort (list, (GCompareFunc) g_utf8_collate);
179 for (l = list; l; l = l->next) {
180 gtk_list_store_insert_with_values (store,
182 COL_STATUS_TEXT, l->data,
183 COL_STATE_ICON_NAME, icon_name,
184 COL_STATE, states[i].state,
185 COL_DISPLAY_MARKUP, l->data,
186 COL_STATUS_CUSTOMISABLE, TRUE,
187 COL_TYPE, ENTRY_TYPE_SAVED,
192 gtk_list_store_insert_with_values (store, NULL, -1,
193 COL_STATUS_TEXT, _("Custom Messageā¦"),
194 COL_STATE_ICON_NAME, icon_name,
195 COL_STATE, states[i].state,
196 COL_DISPLAY_MARKUP, custom_message,
197 COL_STATUS_CUSTOMISABLE, TRUE,
198 COL_TYPE, ENTRY_TYPE_CUSTOM,
204 /* add a separator */
205 gtk_list_store_insert_with_values (store, NULL, -1,
206 COL_TYPE, ENTRY_TYPE_SEPARATOR,
209 gtk_list_store_insert_with_values (store, NULL, -1,
210 COL_STATUS_TEXT, _("Edit Custom Messagesā¦"),
211 COL_STATE_ICON_NAME, GTK_STOCK_EDIT,
212 COL_DISPLAY_MARKUP, _("Edit Custom Messagesā¦"),
213 COL_TYPE, ENTRY_TYPE_EDIT_CUSTOM,
216 g_free (custom_message);
218 gtk_combo_box_set_model (GTK_COMBO_BOX (self), GTK_TREE_MODEL (store));
219 g_object_unref (store);
223 presence_chooser_popup_shown_cb (GObject *self,
227 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
230 g_object_get (self, "popup-shown", &shown, NULL);
235 /* see presence_chooser_entry_focus_out_cb () for what this does */
236 if (priv->focus_out_idle_source != 0) {
237 g_source_remove (priv->focus_out_idle_source);
238 priv->focus_out_idle_source = 0;
241 presence_chooser_create_model (EMPATHY_PRESENCE_CHOOSER (self));
244 static PresenceChooserEntryType
245 presence_chooser_get_entry_type (EmpathyPresenceChooser *self)
248 PresenceChooserEntryType type = -1;
250 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter)) {
251 type = ENTRY_TYPE_CUSTOM;
256 model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
257 gtk_tree_model_get (model, &iter,
265 static TpConnectionPresenceType
266 get_state_and_status (EmpathyPresenceChooser *self,
269 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
270 TpConnectionPresenceType state;
273 state = tp_account_manager_get_most_available_presence (
274 priv->account_manager, NULL, &tmp);
275 if (EMP_STR_EMPTY (tmp)) {
276 /* no message, use the default message */
278 tmp = g_strdup (empathy_presence_get_default_message (state));
290 presence_chooser_is_preset (EmpathyPresenceChooser *self)
292 TpConnectionPresenceType state;
295 gboolean match = FALSE;
297 state = get_state_and_status (self, &status);
299 presets = empathy_status_presets_get (state, -1);
300 for (l = presets; l; l = l->next) {
301 char *preset = (char *) l->data;
303 if (!tp_strdiff (status, preset)) {
309 g_list_free (presets);
311 DEBUG ("is_preset(%i, %s) = %i", state, status, match);
318 presence_chooser_set_favorite_icon (EmpathyPresenceChooser *self)
321 PresenceChooserEntryType type;
323 entry = gtk_bin_get_child (GTK_BIN (self));
324 type = presence_chooser_get_entry_type (self);
326 if (type == ENTRY_TYPE_CUSTOM || type == ENTRY_TYPE_SAVED) {
327 if (presence_chooser_is_preset (self)) {
328 /* saved entries can be removed from the list */
329 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
330 GTK_ENTRY_ICON_SECONDARY,
332 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
333 GTK_ENTRY_ICON_SECONDARY,
334 _("Click to remove this status as a favorite"));
337 /* custom entries can be favorited */
338 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
339 GTK_ENTRY_ICON_SECONDARY,
340 "non-starred-symbolic");
341 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
342 GTK_ENTRY_ICON_SECONDARY,
343 _("Click to make this status a favorite"));
347 /* built-in entries cannot be favorited */
348 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
349 GTK_ENTRY_ICON_SECONDARY,
351 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
352 GTK_ENTRY_ICON_SECONDARY,
358 presence_chooser_set_status_editing (EmpathyPresenceChooser *self,
361 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
364 if (priv->block_set_editing) {
368 entry = gtk_bin_get_child (GTK_BIN (self));
373 priv->editing_status = TRUE;
375 get_state_and_status (self, &status);
376 /* Translators: %s is a status message like 'At the pub' for example */
377 tooltip_text = g_strdup_printf (_("<b>Current message: %s</b>\n"
378 "<small><i>Press Enter to set the new message or Esc to cancel.</i></small>"),
380 gtk_widget_set_tooltip_markup (entry, tooltip_text);
381 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
382 GTK_ENTRY_ICON_SECONDARY,
384 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
385 GTK_ENTRY_ICON_SECONDARY,
387 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
388 GTK_ENTRY_ICON_PRIMARY,
391 g_free (tooltip_text);
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;
444 const gchar *status_tooltip;
446 entry = gtk_bin_get_child (GTK_BIN (self));
448 priv->block_set_editing++;
449 priv->block_changed++;
451 icon_name = empathy_icon_name_for_presence (state);
452 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
453 GTK_ENTRY_ICON_PRIMARY,
455 status_tooltip = status == NULL ? "" : status;
456 gtk_entry_set_text (GTK_ENTRY (entry), status_tooltip);
457 gtk_widget_set_tooltip_text (GTK_WIDGET (entry), status_tooltip);
458 presence_chooser_set_favorite_icon (self);
460 priv->block_changed--;
461 priv->block_set_editing--;
465 presence_chooser_reset_status (EmpathyPresenceChooser *self)
467 /* recover the status that was unset */
468 presence_chooser_set_status_editing (self, FALSE);
469 presence_chooser_presence_changed_cb (self);
473 presence_chooser_entry_icon_release_cb (EmpathyPresenceChooser *self,
474 GtkEntryIconPosition icon_pos,
478 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
480 if (priv->editing_status) {
481 presence_chooser_set_status_editing (self, FALSE);
482 mc_set_custom_state (self);
485 TpConnectionPresenceType state;
488 state = get_state_and_status (self, &status);
490 if (!empathy_status_presets_is_valid (state)) {
491 /* It doesn't make sense to add such presence as favorite */
496 if (presence_chooser_is_preset (self)) {
497 /* remove the entry */
498 DEBUG ("REMOVING PRESET (%i, %s)", state, status);
499 empathy_status_presets_remove (state, status);
503 DEBUG ("SAVING PRESET (%i, %s)", state, status);
504 empathy_status_presets_set_last (state, status);
507 /* update the icon */
508 presence_chooser_set_favorite_icon (self);
514 presence_chooser_entry_activate_cb (EmpathyPresenceChooser *self,
517 presence_chooser_set_status_editing (self, FALSE);
518 mc_set_custom_state (self);
522 presence_chooser_entry_key_press_event_cb (EmpathyPresenceChooser *self,
526 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
528 if (priv->editing_status && event->keyval == GDK_KEY_Escape) {
529 /* the user pressed Escape, undo the editing */
530 presence_chooser_reset_status (self);
533 else if (event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_Down) {
538 return FALSE; /* send this event elsewhere */
542 presence_chooser_entry_button_press_event_cb (EmpathyPresenceChooser *self,
543 GdkEventButton *event,
546 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
548 if (!priv->editing_status &&
549 event->button == 1 &&
550 !gtk_widget_has_focus (entry)) {
551 gtk_widget_grab_focus (entry);
552 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
561 presence_chooser_entry_changed_cb (EmpathyPresenceChooser *self,
564 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
566 if (priv->block_changed){
570 /* the combo is being edited to a custom entry */
571 if (!priv->editing_status) {
572 presence_chooser_set_status_editing (self, TRUE);
577 presence_chooser_changed_cb (GtkComboBox *self, gpointer user_data)
579 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
582 TpConnectionPresenceType new_state;
583 gboolean customisable = TRUE;
584 PresenceChooserEntryType type = -1;
588 if (priv->block_changed ||
589 !gtk_combo_box_get_active_iter (self, &iter)) {
593 model = gtk_combo_box_get_model (self);
594 gtk_tree_model_get (model, &iter,
595 COL_STATE_ICON_NAME, &icon_name,
596 COL_STATE, &new_state,
597 COL_STATUS_CUSTOMISABLE, &customisable,
601 entry = gtk_bin_get_child (GTK_BIN (self));
603 /* some types of status aren't editable, set the editability of the
604 * entry appropriately. Unless we're just about to reset it anyway,
605 * in which case, don't fiddle with it */
606 if (type != ENTRY_TYPE_EDIT_CUSTOM) {
607 gtk_editable_set_editable (GTK_EDITABLE (entry), customisable);
608 priv->state = new_state;
611 if (type == ENTRY_TYPE_EDIT_CUSTOM) {
612 GtkWidget *window, *dialog;
614 presence_chooser_reset_status (EMPATHY_PRESENCE_CHOOSER (self));
616 /* attempt to get the toplevel for this widget */
617 window = gtk_widget_get_toplevel (GTK_WIDGET (self));
618 if (!gtk_widget_is_toplevel (window) || !GTK_IS_WINDOW (window)) {
622 dialog = empathy_status_preset_dialog_new (GTK_WINDOW (window));
623 gtk_dialog_run (GTK_DIALOG (dialog));
624 gtk_widget_destroy (dialog);
626 else if (type == ENTRY_TYPE_CUSTOM) {
627 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
628 GTK_ENTRY_ICON_PRIMARY,
631 /* preseed the status */
632 if (priv->previous_type == ENTRY_TYPE_BUILTIN) {
633 /* if their previous entry was a builtin, don't
635 gtk_entry_set_text (GTK_ENTRY (entry), "");
637 /* else preseed the text of their currently entered
641 get_state_and_status (EMPATHY_PRESENCE_CHOOSER (self),
643 gtk_entry_set_text (GTK_ENTRY (entry), status);
648 gtk_widget_grab_focus (entry);
652 /* just in case we were setting a new status when
653 * things were changed */
654 presence_chooser_set_status_editing (
655 EMPATHY_PRESENCE_CHOOSER (self),
657 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
658 GTK_ENTRY_ICON_PRIMARY,
661 gtk_tree_model_get (model, &iter,
662 COL_STATUS_TEXT, &status,
665 empathy_presence_manager_set_presence (priv->presence_mgr, priv->state, status);
670 if (type != ENTRY_TYPE_EDIT_CUSTOM) {
671 priv->previous_type = type;
677 combo_row_separator_func (GtkTreeModel *model,
681 PresenceChooserEntryType type;
683 gtk_tree_model_get (model, iter,
687 return (type == ENTRY_TYPE_SEPARATOR);
691 presence_chooser_entry_focus_out_idle_cb (gpointer user_data)
693 EmpathyPresenceChooser *chooser;
696 DEBUG ("Autocommiting status message");
698 chooser = EMPATHY_PRESENCE_CHOOSER (user_data);
699 entry = gtk_bin_get_child (GTK_BIN (chooser));
701 presence_chooser_entry_activate_cb (chooser, GTK_ENTRY (entry));
707 presence_chooser_entry_focus_out_cb (EmpathyPresenceChooser *chooser,
708 GdkEventFocus *event,
711 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
713 if (priv->editing_status) {
714 /* this seems a bit evil and maybe it will be fragile,
715 * someone should think of a better way to do it.
717 * The entry has focused out, but we don't know where the focus
718 * has gone. If it goes to the combo box, we don't want to
719 * do anything. If it's gone anywhere else, we want to commit
722 * Thus we install this idle handler and store its source.
723 * If the source is scheduled when the popup handler runs,
724 * it will remove it, else the callback will commit the result.
726 priv->focus_out_idle_source = g_idle_add (
727 presence_chooser_entry_focus_out_idle_cb,
731 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
737 update_sensitivity_am_prepared_cb (GObject *source_object,
738 GAsyncResult *result,
741 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
742 EmpathyPresenceChooser *chooser = user_data;
743 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
744 gboolean sensitive = FALSE;
746 GError *error = NULL;
748 if (!tp_proxy_prepare_finish (manager, result, &error)) {
749 DEBUG ("Failed to prepare account manager: %s", error->message);
750 g_error_free (error);
754 accounts = tp_account_manager_dup_valid_accounts (manager);
756 for (l = accounts ; l != NULL ; l = g_list_next (l)) {
757 TpAccount *a = TP_ACCOUNT (l->data);
759 if (tp_account_is_enabled (a)) {
765 g_list_free_full (accounts, g_object_unref);
767 if (!g_network_monitor_get_network_available (priv->connectivity))
770 gtk_widget_set_sensitive (GTK_WIDGET (chooser), sensitive);
772 presence_chooser_presence_changed_cb (chooser);
776 presence_chooser_update_sensitivity (EmpathyPresenceChooser *chooser)
778 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
780 tp_proxy_prepare_async (priv->account_manager, NULL,
781 update_sensitivity_am_prepared_cb,
786 presence_chooser_account_manager_account_validity_changed_cb (
787 TpAccountManager *manager,
790 EmpathyPresenceChooser *chooser)
792 presence_chooser_update_sensitivity (chooser);
796 presence_chooser_account_manager_account_changed_cb (
797 TpAccountManager *manager,
799 EmpathyPresenceChooser *chooser)
801 presence_chooser_update_sensitivity (chooser);
805 presence_chooser_network_change (GNetworkMonitor *connectivity,
807 EmpathyPresenceChooser *chooser)
809 presence_chooser_update_sensitivity (chooser);
813 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
815 EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
816 EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
818 chooser->priv = priv;
822 presence_chooser_constructed (GObject *object)
824 EmpathyPresenceChooser *chooser = EMPATHY_PRESENCE_CHOOSER (object);
825 EmpathyPresenceChooserPriv *priv = chooser->priv;
827 GtkCellRenderer *renderer;
828 const gchar *status_tooltip;
830 presence_chooser_create_model (chooser);
832 gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX (chooser),
834 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
835 combo_row_separator_func,
838 entry = gtk_bin_get_child (GTK_BIN (chooser));
839 gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
840 GTK_ENTRY_ICON_PRIMARY,
843 g_signal_connect_swapped (entry, "icon-release",
844 G_CALLBACK (presence_chooser_entry_icon_release_cb),
846 g_signal_connect_swapped (entry, "activate",
847 G_CALLBACK (presence_chooser_entry_activate_cb),
849 g_signal_connect_swapped (entry, "key-press-event",
850 G_CALLBACK (presence_chooser_entry_key_press_event_cb),
852 g_signal_connect_swapped (entry, "button-press-event",
853 G_CALLBACK (presence_chooser_entry_button_press_event_cb),
856 gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
858 renderer = gtk_cell_renderer_pixbuf_new ();
859 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, FALSE);
860 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
861 "icon-name", COL_STATE_ICON_NAME,
863 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
865 renderer = gtk_cell_renderer_text_new ();
866 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, TRUE);
867 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
868 "markup", COL_DISPLAY_MARKUP,
870 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
872 g_signal_connect (chooser, "notify::popup-shown",
873 G_CALLBACK (presence_chooser_popup_shown_cb), NULL);
874 g_signal_connect (chooser, "changed",
875 G_CALLBACK (presence_chooser_changed_cb), NULL);
876 g_signal_connect_swapped (entry, "changed",
877 G_CALLBACK (presence_chooser_entry_changed_cb),
879 g_signal_connect_swapped (entry, "focus-out-event",
880 G_CALLBACK (presence_chooser_entry_focus_out_cb),
883 priv->presence_mgr = empathy_presence_manager_dup_singleton ();
885 priv->account_manager = tp_account_manager_dup ();
886 g_signal_connect_swapped (priv->account_manager,
887 "most-available-presence-changed",
888 G_CALLBACK (presence_chooser_presence_changed_cb),
891 tp_g_signal_connect_object (priv->account_manager, "account-validity-changed",
892 G_CALLBACK (presence_chooser_account_manager_account_validity_changed_cb),
894 tp_g_signal_connect_object (priv->account_manager, "account-removed",
895 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
897 tp_g_signal_connect_object (priv->account_manager, "account-enabled",
898 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
900 tp_g_signal_connect_object (priv->account_manager, "account-disabled",
901 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
904 status_tooltip = gtk_entry_get_text (GTK_ENTRY (entry));
905 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser), status_tooltip);
907 priv->connectivity = g_network_monitor_get_default ();
908 g_object_ref (priv->connectivity);
910 tp_g_signal_connect_object (priv->connectivity,
912 G_CALLBACK (presence_chooser_network_change),
915 presence_chooser_update_sensitivity (chooser);
919 presence_chooser_finalize (GObject *object)
921 EmpathyPresenceChooserPriv *priv;
923 priv = GET_PRIV (object);
925 if (priv->focus_out_idle_source) {
926 g_source_remove (priv->focus_out_idle_source);
929 if (priv->account_manager != NULL)
930 g_object_unref (priv->account_manager);
932 g_signal_handlers_disconnect_by_func (priv->presence_mgr,
933 presence_chooser_presence_changed_cb,
935 g_object_unref (priv->presence_mgr);
937 g_object_unref (priv->connectivity);
939 G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
943 * empathy_presence_chooser_new:
945 * Creates a new #EmpathyPresenceChooser widget.
947 * Return value: A new #EmpathyPresenceChooser widget
950 empathy_presence_chooser_new (void)
952 /* FIXME, why can't this go in init ()? */
953 return g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER,
959 presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
961 EmpathyPresenceChooserPriv *priv;
962 TpConnectionPresenceType state;
966 gboolean valid, match_state = FALSE, match = FALSE;
969 priv = GET_PRIV (chooser);
971 if (priv->editing_status) {
975 state = get_state_and_status (chooser, &status);
978 /* An unset presence here doesn't make any sense. Force it to appear as
980 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
981 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
984 /* look through the model and attempt to find a matching state */
985 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
986 for (valid = gtk_tree_model_get_iter_first (model, &iter);
988 valid = gtk_tree_model_iter_next (model, &iter)) {
990 TpConnectionPresenceType m_state;
993 gtk_tree_model_get (model, &iter,
998 if (m_type == ENTRY_TYPE_CUSTOM ||
999 m_type == ENTRY_TYPE_SEPARATOR ||
1000 m_type == ENTRY_TYPE_EDIT_CUSTOM) {
1003 else if (!match_state && state == m_state) {
1004 /* we are now in the section that can contain our
1008 else if (match_state && state != m_state) {
1009 /* we have passed the section that can contain our
1014 gtk_tree_model_get (model, &iter,
1015 COL_STATUS_TEXT, &m_status,
1018 match = !tp_strdiff (status, m_status);
1027 priv->block_changed++;
1028 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
1029 presence_chooser_set_favorite_icon (chooser);
1030 priv->block_changed--;
1033 ui_set_custom_state (chooser, state, status);
1036 entry = gtk_bin_get_child (GTK_BIN (chooser));
1037 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
1038 GTK_ENTRY_ICON_PRIMARY,
1039 empathy_icon_name_for_presence (state));
1040 gtk_widget_set_tooltip_text (GTK_WIDGET (entry), status);
1042 entry = gtk_bin_get_child (GTK_BIN (chooser));
1043 gtk_editable_set_editable (GTK_EDITABLE (entry),
1044 state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
1050 * empathy_presence_chooser_create_menu:
1052 * Creates a new #GtkMenu allowing users to change their presence from a menu.
1054 * Return value: a new #GtkMenu for changing presence in a menu.
1057 empathy_presence_chooser_create_menu (void)
1059 const gchar *status;
1065 menu = gtk_menu_new ();
1067 for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
1070 status = empathy_presence_get_default_message (states[i].state);
1071 presence_chooser_menu_add_item (menu,
1075 if (states[i].customisable) {
1076 /* Set custom messages if wanted */
1077 list = empathy_status_presets_get (states[i].state, 5);
1078 for (l = list; l; l = l->next) {
1079 presence_chooser_menu_add_item (menu,
1089 item = gtk_menu_item_new ();
1090 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1091 gtk_widget_show (item);
1093 /* Custom messages */
1094 item = gtk_image_menu_item_new_with_label (_("Custom messagesā¦"));
1095 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
1096 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1097 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1098 gtk_widget_show (image);
1099 gtk_widget_show (item);
1101 g_signal_connect (item,
1103 G_CALLBACK (presence_chooser_custom_activate_cb),
1110 presence_chooser_menu_add_item (GtkWidget *menu,
1112 TpConnectionPresenceType state)
1116 const gchar *icon_name;
1118 item = gtk_image_menu_item_new_with_label (str);
1119 icon_name = empathy_icon_name_for_presence (state);
1121 g_signal_connect (item, "activate",
1122 G_CALLBACK (presence_chooser_noncustom_activate_cb),
1125 image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
1126 gtk_widget_show (image);
1128 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1129 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
1130 gtk_widget_show (item);
1132 g_object_set_data_full (G_OBJECT (item),
1133 "status", g_strdup (str),
1134 (GDestroyNotify) g_free);
1136 g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
1138 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1142 presence_chooser_noncustom_activate_cb (GtkWidget *item,
1145 TpConnectionPresenceType state;
1146 const gchar *status;
1148 status = g_object_get_data (G_OBJECT (item), "status");
1149 state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
1151 presence_chooser_set_state (state, status);
1155 presence_chooser_set_state (TpConnectionPresenceType state,
1156 const gchar *status)
1158 EmpathyPresenceManager *presence_mgr;
1160 presence_mgr = empathy_presence_manager_dup_singleton ();
1161 empathy_presence_manager_set_presence (presence_mgr, state, status);
1162 g_object_unref (presence_mgr);
1166 presence_chooser_custom_activate_cb (GtkWidget *item,
1171 dialog = empathy_status_preset_dialog_new (NULL);
1172 gtk_dialog_run (GTK_DIALOG (dialog));
1173 gtk_widget_destroy (dialog);