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 * Davyd Madeley <davyd.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-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"
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 /* 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 */
82 /* For combobox's model */
88 COL_STATUS_CUSTOMISABLE
,
98 ENTRY_TYPE_EDIT_CUSTOM
,
99 } PresenceChooserEntryType
;
103 EmpathyConnectivity
*connectivity
;
105 gboolean editing_status
;
106 int block_set_editing
;
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
,
132 TpConnectionPresenceType state
);
133 static void presence_chooser_noncustom_activate_cb (GtkWidget
*item
,
135 static void presence_chooser_set_state (TpConnectionPresenceType state
,
136 const gchar
*status
);
137 static void presence_chooser_custom_activate_cb (GtkWidget
*item
,
140 G_DEFINE_TYPE (EmpathyPresenceChooser
, empathy_presence_chooser
, GTK_TYPE_COMBO_BOX_ENTRY
);
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
));
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
)
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
,
342 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry
),
343 GTK_ENTRY_ICON_SECONDARY
,
344 _("Click to remove this status as a favorite"));
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"));
357 /* built-in entries cannot be favorited */
358 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry
),
359 GTK_ENTRY_ICON_SECONDARY
,
361 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry
),
362 GTK_ENTRY_ICON_SECONDARY
,
368 presence_chooser_set_status_editing (EmpathyPresenceChooser
*self
,
371 EmpathyPresenceChooserPriv
*priv
= GET_PRIV (self
);
374 if (priv
->block_set_editing
) {
378 entry
= gtk_bin_get_child (GTK_BIN (self
));
380 priv
->editing_status
= TRUE
;
382 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry
),
383 GTK_ENTRY_ICON_SECONDARY
,
385 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry
),
386 GTK_ENTRY_ICON_SECONDARY
,
388 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry
),
389 GTK_ENTRY_ICON_PRIMARY
,
394 presence_chooser_set_favorite_icon (self
);
395 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry
),
396 GTK_ENTRY_ICON_PRIMARY
,
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
;
420 mc_set_custom_state (EmpathyPresenceChooser
*self
)
422 EmpathyPresenceChooserPriv
*priv
= GET_PRIV (self
);
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
);
436 ui_set_custom_state (EmpathyPresenceChooser
*self
,
437 TpConnectionPresenceType state
,
440 EmpathyPresenceChooserPriv
*priv
= GET_PRIV (self
);
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
,
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
--;
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
);
469 presence_chooser_entry_icon_release_cb (EmpathyPresenceChooser
*self
,
470 GtkEntryIconPosition icon_pos
,
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
);
481 PresenceChooserEntryType type
;
482 TpConnectionPresenceType state
;
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 */
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
);
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
);
512 presence_chooser_entry_activate_cb (EmpathyPresenceChooser
*self
,
515 presence_chooser_set_status_editing (self
, FALSE
);
516 mc_set_custom_state (self
);
520 presence_chooser_entry_key_press_event_cb (EmpathyPresenceChooser
*self
,
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
);
531 else if (event
->keyval
== GDK_Up
|| event
->keyval
== GDK_Down
) {
536 return FALSE
; /* send this event elsewhere */
540 presence_chooser_entry_button_press_event_cb (EmpathyPresenceChooser
*self
,
541 GdkEventButton
*event
,
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);
559 presence_chooser_entry_changed_cb (EmpathyPresenceChooser
*self
,
562 EmpathyPresenceChooserPriv
*priv
= GET_PRIV (self
);
564 if (priv
->block_changed
){
568 /* the combo is being edited to a custom entry */
569 if (!priv
->editing_status
) {
570 presence_chooser_set_status_editing (self
, TRUE
);
575 presence_chooser_changed_cb (GtkComboBox
*self
, gpointer user_data
)
577 EmpathyPresenceChooserPriv
*priv
= GET_PRIV (self
);
580 TpConnectionPresenceType new_state
;
581 gboolean customisable
= TRUE
;
582 PresenceChooserEntryType type
= -1;
586 if (priv
->block_changed
||
587 !gtk_combo_box_get_active_iter (self
, &iter
)) {
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
,
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
)) {
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
,
629 /* preseed the status */
630 if (priv
->previous_type
== ENTRY_TYPE_BUILTIN
) {
631 /* if their previous entry was a builtin, don't
633 gtk_entry_set_text (GTK_ENTRY (entry
), "");
635 /* else preseed the text of their currently entered
639 get_state_and_status (EMPATHY_PRESENCE_CHOOSER (self
),
641 gtk_entry_set_text (GTK_ENTRY (entry
), status
);
646 gtk_widget_grab_focus (entry
);
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
),
655 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry
),
656 GTK_ENTRY_ICON_PRIMARY
,
659 gtk_tree_model_get (model
, &iter
,
660 COL_STATUS_TEXT
, &status
,
663 empathy_idle_set_presence (priv
->idle
, priv
->state
, status
);
668 if (type
!= ENTRY_TYPE_EDIT_CUSTOM
) {
669 priv
->previous_type
= type
;
675 combo_row_separator_func (GtkTreeModel
*model
,
679 PresenceChooserEntryType type
;
681 gtk_tree_model_get (model
, iter
,
685 return (type
== ENTRY_TYPE_SEPARATOR
);
689 presence_chooser_entry_focus_out_idle_cb (gpointer user_data
)
691 EmpathyPresenceChooser
*chooser
;
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
));
705 presence_chooser_entry_focus_out_cb (EmpathyPresenceChooser
*chooser
,
706 GdkEventFocus
*event
,
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
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
,
729 gtk_editable_set_position (GTK_EDITABLE (entry
), 0);
735 update_sensitivity_am_prepared_cb (GObject
*source_object
,
736 GAsyncResult
*result
,
739 TpAccountManager
*manager
= TP_ACCOUNT_MANAGER (source_object
);
740 EmpathyPresenceChooser
*chooser
= user_data
;
741 EmpathyPresenceChooserPriv
*priv
= GET_PRIV (chooser
);
742 gboolean sensitive
= FALSE
;
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
);
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
)) {
763 g_list_free (accounts
);
765 if (!empathy_connectivity_is_online (priv
->connectivity
))
768 gtk_widget_set_sensitive (GTK_WIDGET (chooser
), sensitive
);
770 presence_chooser_presence_changed_cb (chooser
);
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
,
784 presence_chooser_account_manager_account_validity_changed_cb (
785 TpAccountManager
*manager
,
788 EmpathyPresenceChooser
*chooser
)
790 presence_chooser_update_sensitivity (chooser
);
794 presence_chooser_account_manager_account_changed_cb (
795 TpAccountManager
*manager
,
797 EmpathyPresenceChooser
*chooser
)
799 presence_chooser_update_sensitivity (chooser
);
803 presence_chooser_connectivity_state_change (EmpathyConnectivity
*connectivity
,
805 EmpathyPresenceChooser
*chooser
)
807 presence_chooser_update_sensitivity (chooser
);
811 empathy_presence_chooser_init (EmpathyPresenceChooser
*chooser
)
813 EmpathyPresenceChooserPriv
*priv
= G_TYPE_INSTANCE_GET_PRIVATE (chooser
,
814 EMPATHY_TYPE_PRESENCE_CHOOSER
, EmpathyPresenceChooserPriv
);
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
),
824 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser
),
825 combo_row_separator_func
,
828 entry
= gtk_bin_get_child (GTK_BIN (chooser
));
829 gtk_entry_set_icon_activatable (GTK_ENTRY (entry
),
830 GTK_ENTRY_ICON_PRIMARY
,
833 g_signal_connect_swapped (entry
, "icon-release",
834 G_CALLBACK (presence_chooser_entry_icon_release_cb
),
836 g_signal_connect_swapped (entry
, "activate",
837 G_CALLBACK (presence_chooser_entry_activate_cb
),
839 g_signal_connect_swapped (entry
, "key-press-event",
840 G_CALLBACK (presence_chooser_entry_key_press_event_cb
),
842 g_signal_connect_swapped (entry
, "button-press-event",
843 G_CALLBACK (presence_chooser_entry_button_press_event_cb
),
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
,
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
,
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
),
868 g_signal_connect_swapped (entry
, "focus-out-event",
869 G_CALLBACK (presence_chooser_entry_focus_out_cb
),
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
),
880 empathy_signal_connect_weak (priv
->account_manager
, "account-validity-changed",
881 G_CALLBACK (presence_chooser_account_manager_account_validity_changed_cb
),
883 empathy_signal_connect_weak (priv
->account_manager
, "account-removed",
884 G_CALLBACK (presence_chooser_account_manager_account_changed_cb
),
886 empathy_signal_connect_weak (priv
->account_manager
, "account-enabled",
887 G_CALLBACK (presence_chooser_account_manager_account_changed_cb
),
889 empathy_signal_connect_weak (priv
->account_manager
, "account-disabled",
890 G_CALLBACK (presence_chooser_account_manager_account_changed_cb
),
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
,
900 G_CALLBACK (presence_chooser_connectivity_state_change
),
903 presence_chooser_update_sensitivity (chooser
);
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
,
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
938 empathy_presence_chooser_new (void)
942 chooser
= g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER
, NULL
);
948 presence_chooser_presence_changed_cb (EmpathyPresenceChooser
*chooser
)
950 EmpathyPresenceChooserPriv
*priv
;
951 TpConnectionPresenceType state
;
955 gboolean valid
, match_state
= FALSE
, match
= FALSE
;
958 priv
= GET_PRIV (chooser
);
960 if (priv
->editing_status
) {
964 state
= get_state_and_status (chooser
, &status
);
967 /* An unset presence here doesn't make any sense. Force it to appear as
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
);
977 valid
= gtk_tree_model_iter_next (model
, &iter
)) {
979 TpConnectionPresenceType m_state
;
982 gtk_tree_model_get (model
, &iter
,
987 if (m_type
== ENTRY_TYPE_CUSTOM
||
988 m_type
== ENTRY_TYPE_SEPARATOR
||
989 m_type
== ENTRY_TYPE_EDIT_CUSTOM
) {
992 else if (!match_state
&& state
== m_state
) {
993 /* we are now in the section that can contain our
997 else if (match_state
&& state
!= m_state
) {
998 /* we have passed the section that can contain our
1003 gtk_tree_model_get (model
, &iter
,
1004 COL_STATUS_TEXT
, &m_status
,
1007 match
= !tp_strdiff (status
, m_status
);
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
--;
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
);
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.
1045 empathy_presence_chooser_create_menu (void)
1047 const gchar
*status
;
1053 menu
= gtk_menu_new ();
1055 for (i
= 0; states
[i
].state
!= TP_CONNECTION_PRESENCE_TYPE_UNSET
; i
++) {
1058 status
= empathy_presence_get_default_message (states
[i
].state
);
1059 presence_chooser_menu_add_item (menu
,
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
,
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
,
1091 G_CALLBACK (presence_chooser_custom_activate_cb
),
1098 presence_chooser_menu_add_item (GtkWidget
*menu
,
1100 TpConnectionPresenceType state
)
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
),
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
);
1130 presence_chooser_noncustom_activate_cb (GtkWidget
*item
,
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
);
1143 presence_chooser_set_state (TpConnectionPresenceType state
,
1144 const gchar
*status
)
1148 idle
= empathy_idle_dup_singleton ();
1149 empathy_idle_set_presence (idle
, state
, status
);
1150 g_object_unref (idle
);
1154 presence_chooser_custom_activate_cb (GtkWidget
*item
,
1159 dialog
= empathy_status_preset_dialog_new (NULL
);
1160 gtk_dialog_run (GTK_DIALOG (dialog
));
1161 gtk_widget_destroy (dialog
);