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>
29 #include <glib/gi18n-lib.h>
31 #include <libempathy/empathy-presence-manager.h>
32 #include <libempathy/empathy-utils.h>
33 #include <libempathy/empathy-status-presets.h>
35 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
36 #include <libempathy/empathy-debug.h>
38 #include "empathy-ui-utils.h"
39 #include "empathy-presence-chooser.h"
40 #include "empathy-status-preset-dialog.h"
43 * SECTION:empathy-presence-chooser
44 * @title:EmpathyPresenceChooser
45 * @short_description: A widget used to change presence
46 * @include: libempathy-gtk/empathy-presence-chooser.h
48 * #EmpathyPresenceChooser is a widget which extends #GtkComboBoxEntry
53 * EmpathyAccountChooser:
54 * @parent: parent object
56 * Widget which extends #GtkComboBoxEntry to change presence.
59 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyPresenceChooser)
61 /* For custom message dialog */
69 /* For combobox's model */
75 COL_STATUS_CUSTOMISABLE,
85 ENTRY_TYPE_EDIT_CUSTOM,
86 } PresenceChooserEntryType;
89 EmpathyPresenceManager *presence_mgr;
90 GNetworkMonitor *connectivity;
92 gboolean editing_status;
93 int block_set_editing;
95 guint focus_out_idle_source;
97 TpConnectionPresenceType state;
98 PresenceChooserEntryType previous_type;
100 TpAccountManager *account_manager;
101 } EmpathyPresenceChooserPriv;
103 /* States to be listed in the menu.
104 * Each state has a boolean telling if it can have custom message */
105 static struct { TpConnectionPresenceType state;
106 gboolean customisable;
107 } states[] = { { TP_CONNECTION_PRESENCE_TYPE_AVAILABLE, TRUE } ,
108 { TP_CONNECTION_PRESENCE_TYPE_BUSY, TRUE },
109 { TP_CONNECTION_PRESENCE_TYPE_AWAY, TRUE },
110 { TP_CONNECTION_PRESENCE_TYPE_HIDDEN, FALSE },
111 { TP_CONNECTION_PRESENCE_TYPE_OFFLINE, FALSE},
112 { TP_CONNECTION_PRESENCE_TYPE_UNSET, },
115 static void presence_chooser_constructed (GObject *object);
116 static void presence_chooser_finalize (GObject *object);
117 static void presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser);
118 static void presence_chooser_menu_add_item (GtkWidget *menu,
120 TpConnectionPresenceType state);
121 static void presence_chooser_noncustom_activate_cb (GtkWidget *item,
123 static void presence_chooser_set_state (TpConnectionPresenceType state,
124 const gchar *status);
125 static void presence_chooser_custom_activate_cb (GtkWidget *item,
128 G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_COMBO_BOX);
131 empathy_presence_chooser_class_init (EmpathyPresenceChooserClass *klass)
133 GObjectClass *object_class = G_OBJECT_CLASS (klass);
135 object_class->constructed = presence_chooser_constructed;
136 object_class->finalize = presence_chooser_finalize;
138 g_type_class_add_private (object_class, sizeof (EmpathyPresenceChooserPriv));
142 presence_chooser_create_model (EmpathyPresenceChooser *self)
145 char *custom_message;
148 store = gtk_list_store_new (N_COLUMNS,
149 G_TYPE_STRING, /* COL_STATUS_TEXT */
150 G_TYPE_STRING, /* COL_STATE_ICON_NAME */
151 G_TYPE_UINT, /* COL_STATE */
152 G_TYPE_STRING, /* COL_DISPLAY_MARKUP */
153 G_TYPE_BOOLEAN, /* COL_STATUS_CUSTOMISABLE */
154 G_TYPE_INT); /* COL_TYPE */
156 custom_message = g_strdup_printf ("<i>%s</i>", _("Custom Messageā¦"));
158 for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
160 const char *status, *icon_name;
162 status = empathy_presence_get_default_message (states[i].state);
163 icon_name = empathy_icon_name_for_presence (states[i].state);
165 gtk_list_store_insert_with_values (store, NULL, -1,
166 COL_STATUS_TEXT, status,
167 COL_STATE_ICON_NAME, icon_name,
168 COL_STATE, states[i].state,
169 COL_DISPLAY_MARKUP, status,
170 COL_STATUS_CUSTOMISABLE, states[i].customisable,
171 COL_TYPE, ENTRY_TYPE_BUILTIN,
174 if (states[i].customisable) {
175 /* Set custom messages if wanted */
176 list = empathy_status_presets_get (states[i].state, -1);
177 list = g_list_sort (list, (GCompareFunc) g_utf8_collate);
178 for (l = list; l; l = l->next) {
179 gtk_list_store_insert_with_values (store,
181 COL_STATUS_TEXT, l->data,
182 COL_STATE_ICON_NAME, icon_name,
183 COL_STATE, states[i].state,
184 COL_DISPLAY_MARKUP, l->data,
185 COL_STATUS_CUSTOMISABLE, TRUE,
186 COL_TYPE, ENTRY_TYPE_SAVED,
191 gtk_list_store_insert_with_values (store, NULL, -1,
192 COL_STATUS_TEXT, _("Custom Messageā¦"),
193 COL_STATE_ICON_NAME, icon_name,
194 COL_STATE, states[i].state,
195 COL_DISPLAY_MARKUP, custom_message,
196 COL_STATUS_CUSTOMISABLE, TRUE,
197 COL_TYPE, ENTRY_TYPE_CUSTOM,
203 /* add a separator */
204 gtk_list_store_insert_with_values (store, NULL, -1,
205 COL_TYPE, ENTRY_TYPE_SEPARATOR,
208 gtk_list_store_insert_with_values (store, NULL, -1,
209 COL_STATUS_TEXT, _("Edit Custom Messagesā¦"),
210 COL_STATE_ICON_NAME, GTK_STOCK_EDIT,
211 COL_DISPLAY_MARKUP, _("Edit Custom Messagesā¦"),
212 COL_TYPE, ENTRY_TYPE_EDIT_CUSTOM,
215 g_free (custom_message);
217 gtk_combo_box_set_model (GTK_COMBO_BOX (self), GTK_TREE_MODEL (store));
218 g_object_unref (store);
222 presence_chooser_popup_shown_cb (GObject *self,
226 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
229 g_object_get (self, "popup-shown", &shown, NULL);
234 /* see presence_chooser_entry_focus_out_cb () for what this does */
235 if (priv->focus_out_idle_source != 0) {
236 g_source_remove (priv->focus_out_idle_source);
237 priv->focus_out_idle_source = 0;
240 presence_chooser_create_model (EMPATHY_PRESENCE_CHOOSER (self));
243 static PresenceChooserEntryType
244 presence_chooser_get_entry_type (EmpathyPresenceChooser *self)
247 PresenceChooserEntryType type = -1;
249 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter)) {
250 type = ENTRY_TYPE_CUSTOM;
255 model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
256 gtk_tree_model_get (model, &iter,
264 static TpConnectionPresenceType
265 get_state_and_status (EmpathyPresenceChooser *self,
268 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
269 TpConnectionPresenceType state;
272 state = tp_account_manager_get_most_available_presence (
273 priv->account_manager, NULL, &tmp);
274 if (EMP_STR_EMPTY (tmp)) {
275 /* no message, use the default message */
277 tmp = g_strdup (empathy_presence_get_default_message (state));
289 presence_chooser_is_preset (EmpathyPresenceChooser *self)
291 TpConnectionPresenceType state;
294 gboolean match = FALSE;
296 state = get_state_and_status (self, &status);
298 presets = empathy_status_presets_get (state, -1);
299 for (l = presets; l; l = l->next) {
300 char *preset = (char *) l->data;
302 if (!tp_strdiff (status, preset)) {
308 g_list_free (presets);
310 DEBUG ("is_preset(%i, %s) = %i", state, status, match);
317 presence_chooser_set_favorite_icon (EmpathyPresenceChooser *self)
320 PresenceChooserEntryType type;
322 entry = gtk_bin_get_child (GTK_BIN (self));
323 type = presence_chooser_get_entry_type (self);
325 if (type == ENTRY_TYPE_CUSTOM || type == ENTRY_TYPE_SAVED) {
326 if (presence_chooser_is_preset (self)) {
327 /* saved entries can be removed from the list */
328 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
329 GTK_ENTRY_ICON_SECONDARY,
331 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
332 GTK_ENTRY_ICON_SECONDARY,
333 _("Click to remove this status as a favorite"));
336 /* custom entries can be favorited */
337 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
338 GTK_ENTRY_ICON_SECONDARY,
339 "non-starred-symbolic");
340 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
341 GTK_ENTRY_ICON_SECONDARY,
342 _("Click to make this status a favorite"));
346 /* built-in entries cannot be favorited */
347 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
348 GTK_ENTRY_ICON_SECONDARY,
350 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
351 GTK_ENTRY_ICON_SECONDARY,
357 presence_chooser_set_status_editing (EmpathyPresenceChooser *self,
360 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
363 if (priv->block_set_editing) {
367 entry = gtk_bin_get_child (GTK_BIN (self));
372 priv->editing_status = TRUE;
374 get_state_and_status (self, &status);
375 /* Translators: %s is a status message like 'At the pub' for example */
376 tooltip_text = g_strdup_printf (_("<b>Current message: %s</b>\n"
377 "<small><i>Press Enter to set the new message or Esc to cancel.</i></small>"),
379 gtk_widget_set_tooltip_markup (entry, tooltip_text);
380 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
381 GTK_ENTRY_ICON_SECONDARY,
383 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
384 GTK_ENTRY_ICON_SECONDARY,
386 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
387 GTK_ENTRY_ICON_PRIMARY,
390 g_free (tooltip_text);
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_presence_manager_set_presence (priv->presence_mgr, priv->state, status);
436 ui_set_custom_state (EmpathyPresenceChooser *self,
437 TpConnectionPresenceType state,
440 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
442 const char *icon_name;
443 const gchar *status_tooltip;
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 status_tooltip = status == NULL ? "" : status;
455 gtk_entry_set_text (GTK_ENTRY (entry), status_tooltip);
456 gtk_widget_set_tooltip_text (GTK_WIDGET (entry), status_tooltip);
457 presence_chooser_set_favorite_icon (self);
459 priv->block_changed--;
460 priv->block_set_editing--;
464 presence_chooser_reset_status (EmpathyPresenceChooser *self)
466 /* recover the status that was unset */
467 presence_chooser_set_status_editing (self, FALSE);
468 presence_chooser_presence_changed_cb (self);
472 presence_chooser_entry_icon_release_cb (EmpathyPresenceChooser *self,
473 GtkEntryIconPosition icon_pos,
477 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
479 if (priv->editing_status) {
480 presence_chooser_set_status_editing (self, FALSE);
481 mc_set_custom_state (self);
484 TpConnectionPresenceType state;
487 state = get_state_and_status (self, &status);
489 if (!empathy_status_presets_is_valid (state)) {
490 /* It doesn't make sense to add such presence as favorite */
495 if (presence_chooser_is_preset (self)) {
496 /* remove the entry */
497 DEBUG ("REMOVING PRESET (%i, %s)", state, status);
498 empathy_status_presets_remove (state, status);
502 DEBUG ("SAVING PRESET (%i, %s)", state, status);
503 empathy_status_presets_set_last (state, status);
506 /* update the icon */
507 presence_chooser_set_favorite_icon (self);
513 presence_chooser_entry_activate_cb (EmpathyPresenceChooser *self,
516 presence_chooser_set_status_editing (self, FALSE);
517 mc_set_custom_state (self);
521 presence_chooser_entry_key_press_event_cb (EmpathyPresenceChooser *self,
525 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
527 if (priv->editing_status && event->keyval == GDK_KEY_Escape) {
528 /* the user pressed Escape, undo the editing */
529 presence_chooser_reset_status (self);
532 else if (event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_Down) {
537 return FALSE; /* send this event elsewhere */
541 presence_chooser_entry_button_press_event_cb (EmpathyPresenceChooser *self,
542 GdkEventButton *event,
545 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
547 if (!priv->editing_status &&
548 event->button == 1 &&
549 !gtk_widget_has_focus (entry)) {
550 gtk_widget_grab_focus (entry);
551 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
560 presence_chooser_entry_changed_cb (EmpathyPresenceChooser *self,
563 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
565 if (priv->block_changed){
569 /* the combo is being edited to a custom entry */
570 if (!priv->editing_status) {
571 presence_chooser_set_status_editing (self, TRUE);
576 presence_chooser_changed_cb (GtkComboBox *self, gpointer user_data)
578 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
581 TpConnectionPresenceType new_state;
582 gboolean customisable = TRUE;
583 PresenceChooserEntryType type = -1;
587 if (priv->block_changed ||
588 !gtk_combo_box_get_active_iter (self, &iter)) {
592 model = gtk_combo_box_get_model (self);
593 gtk_tree_model_get (model, &iter,
594 COL_STATE_ICON_NAME, &icon_name,
595 COL_STATE, &new_state,
596 COL_STATUS_CUSTOMISABLE, &customisable,
600 entry = gtk_bin_get_child (GTK_BIN (self));
602 /* some types of status aren't editable, set the editability of the
603 * entry appropriately. Unless we're just about to reset it anyway,
604 * in which case, don't fiddle with it */
605 if (type != ENTRY_TYPE_EDIT_CUSTOM) {
606 gtk_editable_set_editable (GTK_EDITABLE (entry), customisable);
607 priv->state = new_state;
610 if (type == ENTRY_TYPE_EDIT_CUSTOM) {
611 GtkWidget *window, *dialog;
613 presence_chooser_reset_status (EMPATHY_PRESENCE_CHOOSER (self));
615 /* attempt to get the toplevel for this widget */
616 window = gtk_widget_get_toplevel (GTK_WIDGET (self));
617 if (!gtk_widget_is_toplevel (window) || !GTK_IS_WINDOW (window)) {
621 dialog = empathy_status_preset_dialog_new (GTK_WINDOW (window));
622 gtk_dialog_run (GTK_DIALOG (dialog));
623 gtk_widget_destroy (dialog);
625 else if (type == ENTRY_TYPE_CUSTOM) {
626 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
627 GTK_ENTRY_ICON_PRIMARY,
630 /* preseed the status */
631 if (priv->previous_type == ENTRY_TYPE_BUILTIN) {
632 /* if their previous entry was a builtin, don't
634 gtk_entry_set_text (GTK_ENTRY (entry), "");
636 /* else preseed the text of their currently entered
640 get_state_and_status (EMPATHY_PRESENCE_CHOOSER (self),
642 gtk_entry_set_text (GTK_ENTRY (entry), status);
647 gtk_widget_grab_focus (entry);
651 /* just in case we were setting a new status when
652 * things were changed */
653 presence_chooser_set_status_editing (
654 EMPATHY_PRESENCE_CHOOSER (self),
656 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
657 GTK_ENTRY_ICON_PRIMARY,
660 gtk_tree_model_get (model, &iter,
661 COL_STATUS_TEXT, &status,
664 empathy_presence_manager_set_presence (priv->presence_mgr, priv->state, status);
669 if (type != ENTRY_TYPE_EDIT_CUSTOM) {
670 priv->previous_type = type;
676 combo_row_separator_func (GtkTreeModel *model,
680 PresenceChooserEntryType type;
682 gtk_tree_model_get (model, iter,
686 return (type == ENTRY_TYPE_SEPARATOR);
690 presence_chooser_entry_focus_out_idle_cb (gpointer user_data)
692 EmpathyPresenceChooser *chooser;
695 DEBUG ("Autocommiting status message");
697 chooser = EMPATHY_PRESENCE_CHOOSER (user_data);
698 entry = gtk_bin_get_child (GTK_BIN (chooser));
700 presence_chooser_entry_activate_cb (chooser, GTK_ENTRY (entry));
706 presence_chooser_entry_focus_out_cb (EmpathyPresenceChooser *chooser,
707 GdkEventFocus *event,
710 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
712 if (priv->editing_status) {
713 /* this seems a bit evil and maybe it will be fragile,
714 * someone should think of a better way to do it.
716 * The entry has focused out, but we don't know where the focus
717 * has gone. If it goes to the combo box, we don't want to
718 * do anything. If it's gone anywhere else, we want to commit
721 * Thus we install this idle handler and store its source.
722 * If the source is scheduled when the popup handler runs,
723 * it will remove it, else the callback will commit the result.
725 priv->focus_out_idle_source = g_idle_add (
726 presence_chooser_entry_focus_out_idle_cb,
730 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
736 update_sensitivity_am_prepared_cb (GObject *source_object,
737 GAsyncResult *result,
740 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
741 EmpathyPresenceChooser *chooser = user_data;
742 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
743 gboolean sensitive = FALSE;
745 GError *error = NULL;
747 if (!tp_proxy_prepare_finish (manager, result, &error)) {
748 DEBUG ("Failed to prepare account manager: %s", error->message);
749 g_error_free (error);
753 accounts = tp_account_manager_dup_valid_accounts (manager);
755 for (l = accounts ; l != NULL ; l = g_list_next (l)) {
756 TpAccount *a = TP_ACCOUNT (l->data);
758 if (tp_account_is_enabled (a)) {
764 g_list_free_full (accounts, g_object_unref);
766 if (!g_network_monitor_get_network_available (priv->connectivity))
769 gtk_widget_set_sensitive (GTK_WIDGET (chooser), sensitive);
771 presence_chooser_presence_changed_cb (chooser);
775 presence_chooser_update_sensitivity (EmpathyPresenceChooser *chooser)
777 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
779 tp_proxy_prepare_async (priv->account_manager, NULL,
780 update_sensitivity_am_prepared_cb,
785 presence_chooser_account_manager_account_validity_changed_cb (
786 TpAccountManager *manager,
789 EmpathyPresenceChooser *chooser)
791 presence_chooser_update_sensitivity (chooser);
795 presence_chooser_account_manager_account_changed_cb (
796 TpAccountManager *manager,
798 EmpathyPresenceChooser *chooser)
800 presence_chooser_update_sensitivity (chooser);
804 presence_chooser_network_change (GNetworkMonitor *connectivity,
806 EmpathyPresenceChooser *chooser)
808 presence_chooser_update_sensitivity (chooser);
812 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
814 EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
815 EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
817 chooser->priv = priv;
821 presence_chooser_constructed (GObject *object)
823 EmpathyPresenceChooser *chooser = EMPATHY_PRESENCE_CHOOSER (object);
824 EmpathyPresenceChooserPriv *priv = chooser->priv;
826 GtkCellRenderer *renderer;
827 const gchar *status_tooltip;
829 presence_chooser_create_model (chooser);
831 gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX (chooser),
833 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
834 combo_row_separator_func,
837 entry = gtk_bin_get_child (GTK_BIN (chooser));
838 gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
839 GTK_ENTRY_ICON_PRIMARY,
842 g_signal_connect_swapped (entry, "icon-release",
843 G_CALLBACK (presence_chooser_entry_icon_release_cb),
845 g_signal_connect_swapped (entry, "activate",
846 G_CALLBACK (presence_chooser_entry_activate_cb),
848 g_signal_connect_swapped (entry, "key-press-event",
849 G_CALLBACK (presence_chooser_entry_key_press_event_cb),
851 g_signal_connect_swapped (entry, "button-press-event",
852 G_CALLBACK (presence_chooser_entry_button_press_event_cb),
855 gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
857 renderer = gtk_cell_renderer_pixbuf_new ();
858 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, FALSE);
859 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
860 "icon-name", COL_STATE_ICON_NAME,
862 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
864 renderer = gtk_cell_renderer_text_new ();
865 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, TRUE);
866 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
867 "markup", COL_DISPLAY_MARKUP,
869 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
871 g_signal_connect (chooser, "notify::popup-shown",
872 G_CALLBACK (presence_chooser_popup_shown_cb), NULL);
873 g_signal_connect (chooser, "changed",
874 G_CALLBACK (presence_chooser_changed_cb), NULL);
875 g_signal_connect_swapped (entry, "changed",
876 G_CALLBACK (presence_chooser_entry_changed_cb),
878 g_signal_connect_swapped (entry, "focus-out-event",
879 G_CALLBACK (presence_chooser_entry_focus_out_cb),
882 priv->presence_mgr = empathy_presence_manager_dup_singleton ();
884 priv->account_manager = tp_account_manager_dup ();
885 g_signal_connect_swapped (priv->account_manager,
886 "most-available-presence-changed",
887 G_CALLBACK (presence_chooser_presence_changed_cb),
890 tp_g_signal_connect_object (priv->account_manager, "account-validity-changed",
891 G_CALLBACK (presence_chooser_account_manager_account_validity_changed_cb),
893 tp_g_signal_connect_object (priv->account_manager, "account-removed",
894 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
896 tp_g_signal_connect_object (priv->account_manager, "account-enabled",
897 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
899 tp_g_signal_connect_object (priv->account_manager, "account-disabled",
900 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
903 status_tooltip = gtk_entry_get_text (GTK_ENTRY (entry));
904 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser), status_tooltip);
906 priv->connectivity = g_network_monitor_get_default ();
907 g_object_ref (priv->connectivity);
909 tp_g_signal_connect_object (priv->connectivity,
911 G_CALLBACK (presence_chooser_network_change),
914 presence_chooser_update_sensitivity (chooser);
918 presence_chooser_finalize (GObject *object)
920 EmpathyPresenceChooserPriv *priv;
922 priv = GET_PRIV (object);
924 if (priv->focus_out_idle_source) {
925 g_source_remove (priv->focus_out_idle_source);
928 if (priv->account_manager != NULL)
929 g_object_unref (priv->account_manager);
931 g_signal_handlers_disconnect_by_func (priv->presence_mgr,
932 presence_chooser_presence_changed_cb,
934 g_object_unref (priv->presence_mgr);
936 g_object_unref (priv->connectivity);
938 G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
942 * empathy_presence_chooser_new:
944 * Creates a new #EmpathyPresenceChooser widget.
946 * Return value: A new #EmpathyPresenceChooser widget
949 empathy_presence_chooser_new (void)
951 /* FIXME, why can't this go in init ()? */
952 return g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER,
958 presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
960 EmpathyPresenceChooserPriv *priv;
961 TpConnectionPresenceType state;
965 gboolean valid, match_state = FALSE, match = FALSE;
968 priv = GET_PRIV (chooser);
970 if (priv->editing_status) {
974 state = get_state_and_status (chooser, &status);
977 /* An unset presence here doesn't make any sense. Force it to appear as
979 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
980 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
983 /* look through the model and attempt to find a matching state */
984 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
985 for (valid = gtk_tree_model_get_iter_first (model, &iter);
987 valid = gtk_tree_model_iter_next (model, &iter)) {
989 TpConnectionPresenceType m_state;
992 gtk_tree_model_get (model, &iter,
997 if (m_type == ENTRY_TYPE_CUSTOM ||
998 m_type == ENTRY_TYPE_SEPARATOR ||
999 m_type == ENTRY_TYPE_EDIT_CUSTOM) {
1002 else if (!match_state && state == m_state) {
1003 /* we are now in the section that can contain our
1007 else if (match_state && state != m_state) {
1008 /* we have passed the section that can contain our
1013 gtk_tree_model_get (model, &iter,
1014 COL_STATUS_TEXT, &m_status,
1017 match = !tp_strdiff (status, m_status);
1026 priv->block_changed++;
1027 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
1028 presence_chooser_set_favorite_icon (chooser);
1029 priv->block_changed--;
1032 ui_set_custom_state (chooser, state, status);
1035 entry = gtk_bin_get_child (GTK_BIN (chooser));
1036 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
1037 GTK_ENTRY_ICON_PRIMARY,
1038 empathy_icon_name_for_presence (state));
1039 gtk_widget_set_tooltip_text (GTK_WIDGET (entry), status);
1041 entry = gtk_bin_get_child (GTK_BIN (chooser));
1042 gtk_editable_set_editable (GTK_EDITABLE (entry),
1043 state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
1049 * empathy_presence_chooser_create_menu:
1051 * Creates a new #GtkMenu allowing users to change their presence from a menu.
1053 * Return value: a new #GtkMenu for changing presence in a menu.
1056 empathy_presence_chooser_create_menu (void)
1058 const gchar *status;
1064 menu = gtk_menu_new ();
1066 for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
1069 status = empathy_presence_get_default_message (states[i].state);
1070 presence_chooser_menu_add_item (menu,
1074 if (states[i].customisable) {
1075 /* Set custom messages if wanted */
1076 list = empathy_status_presets_get (states[i].state, 5);
1077 for (l = list; l; l = l->next) {
1078 presence_chooser_menu_add_item (menu,
1088 item = gtk_menu_item_new ();
1089 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1090 gtk_widget_show (item);
1092 /* Custom messages */
1093 item = gtk_image_menu_item_new_with_label (_("Custom messagesā¦"));
1094 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
1095 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1096 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1097 gtk_widget_show (image);
1098 gtk_widget_show (item);
1100 g_signal_connect (item,
1102 G_CALLBACK (presence_chooser_custom_activate_cb),
1109 presence_chooser_menu_add_item (GtkWidget *menu,
1111 TpConnectionPresenceType state)
1115 const gchar *icon_name;
1117 item = gtk_image_menu_item_new_with_label (str);
1118 icon_name = empathy_icon_name_for_presence (state);
1120 g_signal_connect (item, "activate",
1121 G_CALLBACK (presence_chooser_noncustom_activate_cb),
1124 image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
1125 gtk_widget_show (image);
1127 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1128 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
1129 gtk_widget_show (item);
1131 g_object_set_data_full (G_OBJECT (item),
1132 "status", g_strdup (str),
1133 (GDestroyNotify) g_free);
1135 g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
1137 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1141 presence_chooser_noncustom_activate_cb (GtkWidget *item,
1144 TpConnectionPresenceType state;
1145 const gchar *status;
1147 status = g_object_get_data (G_OBJECT (item), "status");
1148 state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
1150 presence_chooser_set_state (state, status);
1154 presence_chooser_set_state (TpConnectionPresenceType state,
1155 const gchar *status)
1157 EmpathyPresenceManager *presence_mgr;
1159 presence_mgr = empathy_presence_manager_dup_singleton ();
1160 empathy_presence_manager_set_presence (presence_mgr, state, status);
1161 g_object_unref (presence_mgr);
1165 presence_chooser_custom_activate_cb (GtkWidget *item,
1170 dialog = empathy_status_preset_dialog_new (NULL);
1171 gtk_dialog_run (GTK_DIALOG (dialog));
1172 gtk_widget_destroy (dialog);