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/telepathy-glib.h>
38 #include <libempathy/empathy-presence-manager.h>
39 #include <libempathy/empathy-utils.h>
40 #include <libempathy/empathy-status-presets.h>
42 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
43 #include <libempathy/empathy-debug.h>
45 #include "empathy-ui-utils.h"
46 #include "empathy-images.h"
47 #include "empathy-presence-chooser.h"
48 #include "empathy-status-preset-dialog.h"
51 * SECTION:empathy-presence-chooser
52 * @title:EmpathyPresenceChooser
53 * @short_description: A widget used to change presence
54 * @include: libempathy-gtk/empathy-presence-chooser.h
56 * #EmpathyPresenceChooser is a widget which extends #GtkComboBoxEntry
61 * EmpathyAccountChooser:
62 * @parent: parent object
64 * Widget which extends #GtkComboBoxEntry to change presence.
67 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyPresenceChooser)
69 /* For custom message dialog */
77 /* For combobox's model */
83 COL_STATUS_CUSTOMISABLE,
93 ENTRY_TYPE_EDIT_CUSTOM,
94 } PresenceChooserEntryType;
97 EmpathyPresenceManager *presence_mgr;
98 GNetworkMonitor *connectivity;
100 gboolean editing_status;
101 int block_set_editing;
103 guint focus_out_idle_source;
105 TpConnectionPresenceType state;
106 PresenceChooserEntryType previous_type;
108 TpAccountManager *account_manager;
109 } EmpathyPresenceChooserPriv;
111 /* States to be listed in the menu.
112 * Each state has a boolean telling if it can have custom message */
113 static struct { TpConnectionPresenceType state;
114 gboolean customisable;
115 } states[] = { { TP_CONNECTION_PRESENCE_TYPE_AVAILABLE, TRUE } ,
116 { TP_CONNECTION_PRESENCE_TYPE_BUSY, TRUE },
117 { TP_CONNECTION_PRESENCE_TYPE_AWAY, TRUE },
118 { TP_CONNECTION_PRESENCE_TYPE_HIDDEN, FALSE },
119 { TP_CONNECTION_PRESENCE_TYPE_OFFLINE, FALSE},
120 { TP_CONNECTION_PRESENCE_TYPE_UNSET, },
123 static void presence_chooser_constructed (GObject *object);
124 static void presence_chooser_finalize (GObject *object);
125 static void presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser);
126 static void presence_chooser_menu_add_item (GtkWidget *menu,
128 TpConnectionPresenceType state);
129 static void presence_chooser_noncustom_activate_cb (GtkWidget *item,
131 static void presence_chooser_set_state (TpConnectionPresenceType state,
132 const gchar *status);
133 static void presence_chooser_custom_activate_cb (GtkWidget *item,
136 G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_COMBO_BOX);
139 empathy_presence_chooser_class_init (EmpathyPresenceChooserClass *klass)
141 GObjectClass *object_class = G_OBJECT_CLASS (klass);
143 object_class->constructed = presence_chooser_constructed;
144 object_class->finalize = presence_chooser_finalize;
146 g_type_class_add_private (object_class, sizeof (EmpathyPresenceChooserPriv));
150 presence_chooser_create_model (EmpathyPresenceChooser *self)
153 char *custom_message;
156 store = gtk_list_store_new (N_COLUMNS,
157 G_TYPE_STRING, /* COL_STATUS_TEXT */
158 G_TYPE_STRING, /* COL_STATE_ICON_NAME */
159 G_TYPE_UINT, /* COL_STATE */
160 G_TYPE_STRING, /* COL_DISPLAY_MARKUP */
161 G_TYPE_BOOLEAN, /* COL_STATUS_CUSTOMISABLE */
162 G_TYPE_INT); /* COL_TYPE */
164 custom_message = g_strdup_printf ("<i>%s</i>", _("Custom Messageā¦"));
166 for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
168 const char *status, *icon_name;
170 status = empathy_presence_get_default_message (states[i].state);
171 icon_name = empathy_icon_name_for_presence (states[i].state);
173 gtk_list_store_insert_with_values (store, NULL, -1,
174 COL_STATUS_TEXT, status,
175 COL_STATE_ICON_NAME, icon_name,
176 COL_STATE, states[i].state,
177 COL_DISPLAY_MARKUP, status,
178 COL_STATUS_CUSTOMISABLE, states[i].customisable,
179 COL_TYPE, ENTRY_TYPE_BUILTIN,
182 if (states[i].customisable) {
183 /* Set custom messages if wanted */
184 list = empathy_status_presets_get (states[i].state, -1);
185 list = g_list_sort (list, (GCompareFunc) g_utf8_collate);
186 for (l = list; l; l = l->next) {
187 gtk_list_store_insert_with_values (store,
189 COL_STATUS_TEXT, l->data,
190 COL_STATE_ICON_NAME, icon_name,
191 COL_STATE, states[i].state,
192 COL_DISPLAY_MARKUP, l->data,
193 COL_STATUS_CUSTOMISABLE, TRUE,
194 COL_TYPE, ENTRY_TYPE_SAVED,
199 gtk_list_store_insert_with_values (store, NULL, -1,
200 COL_STATUS_TEXT, _("Custom Messageā¦"),
201 COL_STATE_ICON_NAME, icon_name,
202 COL_STATE, states[i].state,
203 COL_DISPLAY_MARKUP, custom_message,
204 COL_STATUS_CUSTOMISABLE, TRUE,
205 COL_TYPE, ENTRY_TYPE_CUSTOM,
211 /* add a separator */
212 gtk_list_store_insert_with_values (store, NULL, -1,
213 COL_TYPE, ENTRY_TYPE_SEPARATOR,
216 gtk_list_store_insert_with_values (store, NULL, -1,
217 COL_STATUS_TEXT, _("Edit Custom Messagesā¦"),
218 COL_STATE_ICON_NAME, GTK_STOCK_EDIT,
219 COL_DISPLAY_MARKUP, _("Edit Custom Messagesā¦"),
220 COL_TYPE, ENTRY_TYPE_EDIT_CUSTOM,
223 g_free (custom_message);
225 gtk_combo_box_set_model (GTK_COMBO_BOX (self), GTK_TREE_MODEL (store));
226 g_object_unref (store);
230 presence_chooser_popup_shown_cb (GObject *self,
234 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
237 g_object_get (self, "popup-shown", &shown, NULL);
242 /* see presence_chooser_entry_focus_out_cb () for what this does */
243 if (priv->focus_out_idle_source != 0) {
244 g_source_remove (priv->focus_out_idle_source);
245 priv->focus_out_idle_source = 0;
248 presence_chooser_create_model (EMPATHY_PRESENCE_CHOOSER (self));
251 static PresenceChooserEntryType
252 presence_chooser_get_entry_type (EmpathyPresenceChooser *self)
255 PresenceChooserEntryType type = -1;
257 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter)) {
258 type = ENTRY_TYPE_CUSTOM;
263 model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
264 gtk_tree_model_get (model, &iter,
272 static TpConnectionPresenceType
273 get_state_and_status (EmpathyPresenceChooser *self,
276 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
277 TpConnectionPresenceType state;
280 state = tp_account_manager_get_most_available_presence (
281 priv->account_manager, NULL, &tmp);
282 if (EMP_STR_EMPTY (tmp)) {
283 /* no message, use the default message */
285 tmp = g_strdup (empathy_presence_get_default_message (state));
297 presence_chooser_is_preset (EmpathyPresenceChooser *self)
299 TpConnectionPresenceType state;
302 gboolean match = FALSE;
304 state = get_state_and_status (self, &status);
306 presets = empathy_status_presets_get (state, -1);
307 for (l = presets; l; l = l->next) {
308 char *preset = (char *) l->data;
310 if (!tp_strdiff (status, preset)) {
316 g_list_free (presets);
318 DEBUG ("is_preset(%i, %s) = %i", state, status, match);
325 presence_chooser_set_favorite_icon (EmpathyPresenceChooser *self)
328 PresenceChooserEntryType type;
330 entry = gtk_bin_get_child (GTK_BIN (self));
331 type = presence_chooser_get_entry_type (self);
333 if (type == ENTRY_TYPE_CUSTOM || type == ENTRY_TYPE_SAVED) {
334 if (presence_chooser_is_preset (self)) {
335 /* saved entries can be removed from the list */
336 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
337 GTK_ENTRY_ICON_SECONDARY,
339 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
340 GTK_ENTRY_ICON_SECONDARY,
341 _("Click to remove this status as a favorite"));
344 /* custom entries can be favorited */
345 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
346 GTK_ENTRY_ICON_SECONDARY,
347 "non-starred-symbolic");
348 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
349 GTK_ENTRY_ICON_SECONDARY,
350 _("Click to make this status a favorite"));
354 /* built-in entries cannot be favorited */
355 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
356 GTK_ENTRY_ICON_SECONDARY,
358 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
359 GTK_ENTRY_ICON_SECONDARY,
365 presence_chooser_set_status_editing (EmpathyPresenceChooser *self,
368 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
371 if (priv->block_set_editing) {
375 entry = gtk_bin_get_child (GTK_BIN (self));
380 priv->editing_status = TRUE;
382 get_state_and_status (self, &status);
383 /* Translators: %s is a status message like 'At the pub' for example */
384 tooltip_text = g_strdup_printf (_("<b>Current message: %s</b>\n"
385 "<small><i>Press Enter to set the new message or Esc to cancel.</i></small>"),
387 gtk_widget_set_tooltip_markup (entry, tooltip_text);
388 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
389 GTK_ENTRY_ICON_SECONDARY,
391 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
392 GTK_ENTRY_ICON_SECONDARY,
394 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
395 GTK_ENTRY_ICON_PRIMARY,
398 g_free (tooltip_text);
402 presence_chooser_set_favorite_icon (self);
403 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
404 GTK_ENTRY_ICON_PRIMARY,
407 /* attempt to get the toplevel for this widget */
408 window = gtk_widget_get_toplevel (GTK_WIDGET (self));
409 if (gtk_widget_is_toplevel (window) && GTK_IS_WINDOW (window)) {
410 /* unset the focus */
411 gtk_window_set_focus (GTK_WINDOW (window), NULL);
414 /* see presence_chooser_entry_focus_out_cb ()
415 * for what this does */
416 if (priv->focus_out_idle_source != 0) {
417 g_source_remove (priv->focus_out_idle_source);
418 priv->focus_out_idle_source = 0;
421 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
423 priv->editing_status = FALSE;
428 mc_set_custom_state (EmpathyPresenceChooser *self)
430 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
434 entry = gtk_bin_get_child (GTK_BIN (self));
435 /* update the status with MC */
436 status = gtk_entry_get_text (GTK_ENTRY (entry));
438 DEBUG ("Sending state to MC-> %d (%s)", priv->state, status);
440 empathy_presence_manager_set_presence (priv->presence_mgr, priv->state, status);
444 ui_set_custom_state (EmpathyPresenceChooser *self,
445 TpConnectionPresenceType state,
448 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
450 const char *icon_name;
451 const gchar *status_tooltip;
453 entry = gtk_bin_get_child (GTK_BIN (self));
455 priv->block_set_editing++;
456 priv->block_changed++;
458 icon_name = empathy_icon_name_for_presence (state);
459 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
460 GTK_ENTRY_ICON_PRIMARY,
462 status_tooltip = status == NULL ? "" : status;
463 gtk_entry_set_text (GTK_ENTRY (entry), status_tooltip);
464 gtk_widget_set_tooltip_text (GTK_WIDGET (entry), status_tooltip);
465 presence_chooser_set_favorite_icon (self);
467 priv->block_changed--;
468 priv->block_set_editing--;
472 presence_chooser_reset_status (EmpathyPresenceChooser *self)
474 /* recover the status that was unset */
475 presence_chooser_set_status_editing (self, FALSE);
476 presence_chooser_presence_changed_cb (self);
480 presence_chooser_entry_icon_release_cb (EmpathyPresenceChooser *self,
481 GtkEntryIconPosition icon_pos,
485 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
487 if (priv->editing_status) {
488 presence_chooser_set_status_editing (self, FALSE);
489 mc_set_custom_state (self);
492 TpConnectionPresenceType state;
495 state = get_state_and_status (self, &status);
497 if (!empathy_status_presets_is_valid (state)) {
498 /* It doesn't make sense to add such presence as favorite */
503 if (presence_chooser_is_preset (self)) {
504 /* remove the entry */
505 DEBUG ("REMOVING PRESET (%i, %s)", state, status);
506 empathy_status_presets_remove (state, status);
510 DEBUG ("SAVING PRESET (%i, %s)", state, status);
511 empathy_status_presets_set_last (state, status);
514 /* update the icon */
515 presence_chooser_set_favorite_icon (self);
521 presence_chooser_entry_activate_cb (EmpathyPresenceChooser *self,
524 presence_chooser_set_status_editing (self, FALSE);
525 mc_set_custom_state (self);
529 presence_chooser_entry_key_press_event_cb (EmpathyPresenceChooser *self,
533 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
535 if (priv->editing_status && event->keyval == GDK_KEY_Escape) {
536 /* the user pressed Escape, undo the editing */
537 presence_chooser_reset_status (self);
540 else if (event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_Down) {
545 return FALSE; /* send this event elsewhere */
549 presence_chooser_entry_button_press_event_cb (EmpathyPresenceChooser *self,
550 GdkEventButton *event,
553 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
555 if (!priv->editing_status &&
556 event->button == 1 &&
557 !gtk_widget_has_focus (entry)) {
558 gtk_widget_grab_focus (entry);
559 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
568 presence_chooser_entry_changed_cb (EmpathyPresenceChooser *self,
571 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
573 if (priv->block_changed){
577 /* the combo is being edited to a custom entry */
578 if (!priv->editing_status) {
579 presence_chooser_set_status_editing (self, TRUE);
584 presence_chooser_changed_cb (GtkComboBox *self, gpointer user_data)
586 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
589 TpConnectionPresenceType new_state;
590 gboolean customisable = TRUE;
591 PresenceChooserEntryType type = -1;
595 if (priv->block_changed ||
596 !gtk_combo_box_get_active_iter (self, &iter)) {
600 model = gtk_combo_box_get_model (self);
601 gtk_tree_model_get (model, &iter,
602 COL_STATE_ICON_NAME, &icon_name,
603 COL_STATE, &new_state,
604 COL_STATUS_CUSTOMISABLE, &customisable,
608 entry = gtk_bin_get_child (GTK_BIN (self));
610 /* some types of status aren't editable, set the editability of the
611 * entry appropriately. Unless we're just about to reset it anyway,
612 * in which case, don't fiddle with it */
613 if (type != ENTRY_TYPE_EDIT_CUSTOM) {
614 gtk_editable_set_editable (GTK_EDITABLE (entry), customisable);
615 priv->state = new_state;
618 if (type == ENTRY_TYPE_EDIT_CUSTOM) {
619 GtkWidget *window, *dialog;
621 presence_chooser_reset_status (EMPATHY_PRESENCE_CHOOSER (self));
623 /* attempt to get the toplevel for this widget */
624 window = gtk_widget_get_toplevel (GTK_WIDGET (self));
625 if (!gtk_widget_is_toplevel (window) || !GTK_IS_WINDOW (window)) {
629 dialog = empathy_status_preset_dialog_new (GTK_WINDOW (window));
630 gtk_dialog_run (GTK_DIALOG (dialog));
631 gtk_widget_destroy (dialog);
633 else if (type == ENTRY_TYPE_CUSTOM) {
634 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
635 GTK_ENTRY_ICON_PRIMARY,
638 /* preseed the status */
639 if (priv->previous_type == ENTRY_TYPE_BUILTIN) {
640 /* if their previous entry was a builtin, don't
642 gtk_entry_set_text (GTK_ENTRY (entry), "");
644 /* else preseed the text of their currently entered
648 get_state_and_status (EMPATHY_PRESENCE_CHOOSER (self),
650 gtk_entry_set_text (GTK_ENTRY (entry), status);
655 gtk_widget_grab_focus (entry);
659 /* just in case we were setting a new status when
660 * things were changed */
661 presence_chooser_set_status_editing (
662 EMPATHY_PRESENCE_CHOOSER (self),
664 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
665 GTK_ENTRY_ICON_PRIMARY,
668 gtk_tree_model_get (model, &iter,
669 COL_STATUS_TEXT, &status,
672 empathy_presence_manager_set_presence (priv->presence_mgr, priv->state, status);
677 if (type != ENTRY_TYPE_EDIT_CUSTOM) {
678 priv->previous_type = type;
684 combo_row_separator_func (GtkTreeModel *model,
688 PresenceChooserEntryType type;
690 gtk_tree_model_get (model, iter,
694 return (type == ENTRY_TYPE_SEPARATOR);
698 presence_chooser_entry_focus_out_idle_cb (gpointer user_data)
700 EmpathyPresenceChooser *chooser;
703 DEBUG ("Autocommiting status message");
705 chooser = EMPATHY_PRESENCE_CHOOSER (user_data);
706 entry = gtk_bin_get_child (GTK_BIN (chooser));
708 presence_chooser_entry_activate_cb (chooser, GTK_ENTRY (entry));
714 presence_chooser_entry_focus_out_cb (EmpathyPresenceChooser *chooser,
715 GdkEventFocus *event,
718 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
720 if (priv->editing_status) {
721 /* this seems a bit evil and maybe it will be fragile,
722 * someone should think of a better way to do it.
724 * The entry has focused out, but we don't know where the focus
725 * has gone. If it goes to the combo box, we don't want to
726 * do anything. If it's gone anywhere else, we want to commit
729 * Thus we install this idle handler and store its source.
730 * If the source is scheduled when the popup handler runs,
731 * it will remove it, else the callback will commit the result.
733 priv->focus_out_idle_source = g_idle_add (
734 presence_chooser_entry_focus_out_idle_cb,
738 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
744 update_sensitivity_am_prepared_cb (GObject *source_object,
745 GAsyncResult *result,
748 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
749 EmpathyPresenceChooser *chooser = user_data;
750 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
751 gboolean sensitive = FALSE;
753 GError *error = NULL;
755 if (!tp_proxy_prepare_finish (manager, result, &error)) {
756 DEBUG ("Failed to prepare account manager: %s", error->message);
757 g_error_free (error);
761 accounts = tp_account_manager_dup_valid_accounts (manager);
763 for (l = accounts ; l != NULL ; l = g_list_next (l)) {
764 TpAccount *a = TP_ACCOUNT (l->data);
766 if (tp_account_is_enabled (a)) {
772 g_list_free_full (accounts, g_object_unref);
774 if (!g_network_monitor_get_network_available (priv->connectivity))
777 gtk_widget_set_sensitive (GTK_WIDGET (chooser), sensitive);
779 presence_chooser_presence_changed_cb (chooser);
783 presence_chooser_update_sensitivity (EmpathyPresenceChooser *chooser)
785 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
787 tp_proxy_prepare_async (priv->account_manager, NULL,
788 update_sensitivity_am_prepared_cb,
793 presence_chooser_account_manager_account_validity_changed_cb (
794 TpAccountManager *manager,
797 EmpathyPresenceChooser *chooser)
799 presence_chooser_update_sensitivity (chooser);
803 presence_chooser_account_manager_account_changed_cb (
804 TpAccountManager *manager,
806 EmpathyPresenceChooser *chooser)
808 presence_chooser_update_sensitivity (chooser);
812 presence_chooser_network_change (GNetworkMonitor *connectivity,
814 EmpathyPresenceChooser *chooser)
816 presence_chooser_update_sensitivity (chooser);
820 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
822 EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
823 EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
825 chooser->priv = priv;
829 presence_chooser_constructed (GObject *object)
831 EmpathyPresenceChooser *chooser = EMPATHY_PRESENCE_CHOOSER (object);
832 EmpathyPresenceChooserPriv *priv = chooser->priv;
834 GtkCellRenderer *renderer;
835 const gchar *status_tooltip;
837 presence_chooser_create_model (chooser);
839 gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX (chooser),
841 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
842 combo_row_separator_func,
845 entry = gtk_bin_get_child (GTK_BIN (chooser));
846 gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
847 GTK_ENTRY_ICON_PRIMARY,
850 g_signal_connect_swapped (entry, "icon-release",
851 G_CALLBACK (presence_chooser_entry_icon_release_cb),
853 g_signal_connect_swapped (entry, "activate",
854 G_CALLBACK (presence_chooser_entry_activate_cb),
856 g_signal_connect_swapped (entry, "key-press-event",
857 G_CALLBACK (presence_chooser_entry_key_press_event_cb),
859 g_signal_connect_swapped (entry, "button-press-event",
860 G_CALLBACK (presence_chooser_entry_button_press_event_cb),
863 gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
865 renderer = gtk_cell_renderer_pixbuf_new ();
866 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, FALSE);
867 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
868 "icon-name", COL_STATE_ICON_NAME,
870 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
872 renderer = gtk_cell_renderer_text_new ();
873 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, TRUE);
874 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
875 "markup", COL_DISPLAY_MARKUP,
877 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
879 g_signal_connect (chooser, "notify::popup-shown",
880 G_CALLBACK (presence_chooser_popup_shown_cb), NULL);
881 g_signal_connect (chooser, "changed",
882 G_CALLBACK (presence_chooser_changed_cb), NULL);
883 g_signal_connect_swapped (entry, "changed",
884 G_CALLBACK (presence_chooser_entry_changed_cb),
886 g_signal_connect_swapped (entry, "focus-out-event",
887 G_CALLBACK (presence_chooser_entry_focus_out_cb),
890 priv->presence_mgr = empathy_presence_manager_dup_singleton ();
892 priv->account_manager = tp_account_manager_dup ();
893 g_signal_connect_swapped (priv->account_manager,
894 "most-available-presence-changed",
895 G_CALLBACK (presence_chooser_presence_changed_cb),
898 tp_g_signal_connect_object (priv->account_manager, "account-validity-changed",
899 G_CALLBACK (presence_chooser_account_manager_account_validity_changed_cb),
901 tp_g_signal_connect_object (priv->account_manager, "account-removed",
902 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
904 tp_g_signal_connect_object (priv->account_manager, "account-enabled",
905 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
907 tp_g_signal_connect_object (priv->account_manager, "account-disabled",
908 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
911 status_tooltip = gtk_entry_get_text (GTK_ENTRY (entry));
912 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser), status_tooltip);
914 priv->connectivity = g_network_monitor_get_default ();
915 g_object_ref (priv->connectivity);
917 tp_g_signal_connect_object (priv->connectivity,
919 G_CALLBACK (presence_chooser_network_change),
922 presence_chooser_update_sensitivity (chooser);
926 presence_chooser_finalize (GObject *object)
928 EmpathyPresenceChooserPriv *priv;
930 priv = GET_PRIV (object);
932 if (priv->focus_out_idle_source) {
933 g_source_remove (priv->focus_out_idle_source);
936 if (priv->account_manager != NULL)
937 g_object_unref (priv->account_manager);
939 g_signal_handlers_disconnect_by_func (priv->presence_mgr,
940 presence_chooser_presence_changed_cb,
942 g_object_unref (priv->presence_mgr);
944 g_object_unref (priv->connectivity);
946 G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
950 * empathy_presence_chooser_new:
952 * Creates a new #EmpathyPresenceChooser widget.
954 * Return value: A new #EmpathyPresenceChooser widget
957 empathy_presence_chooser_new (void)
959 /* FIXME, why can't this go in init ()? */
960 return g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER,
966 presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
968 EmpathyPresenceChooserPriv *priv;
969 TpConnectionPresenceType state;
973 gboolean valid, match_state = FALSE, match = FALSE;
976 priv = GET_PRIV (chooser);
978 if (priv->editing_status) {
982 state = get_state_and_status (chooser, &status);
985 /* An unset presence here doesn't make any sense. Force it to appear as
987 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
988 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
991 /* look through the model and attempt to find a matching state */
992 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
993 for (valid = gtk_tree_model_get_iter_first (model, &iter);
995 valid = gtk_tree_model_iter_next (model, &iter)) {
997 TpConnectionPresenceType m_state;
1000 gtk_tree_model_get (model, &iter,
1001 COL_STATE, &m_state,
1005 if (m_type == ENTRY_TYPE_CUSTOM ||
1006 m_type == ENTRY_TYPE_SEPARATOR ||
1007 m_type == ENTRY_TYPE_EDIT_CUSTOM) {
1010 else if (!match_state && state == m_state) {
1011 /* we are now in the section that can contain our
1015 else if (match_state && state != m_state) {
1016 /* we have passed the section that can contain our
1021 gtk_tree_model_get (model, &iter,
1022 COL_STATUS_TEXT, &m_status,
1025 match = !tp_strdiff (status, m_status);
1034 priv->block_changed++;
1035 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
1036 presence_chooser_set_favorite_icon (chooser);
1037 priv->block_changed--;
1040 ui_set_custom_state (chooser, state, status);
1043 entry = gtk_bin_get_child (GTK_BIN (chooser));
1044 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
1045 GTK_ENTRY_ICON_PRIMARY,
1046 empathy_icon_name_for_presence (state));
1047 gtk_widget_set_tooltip_text (GTK_WIDGET (entry), status);
1049 entry = gtk_bin_get_child (GTK_BIN (chooser));
1050 gtk_editable_set_editable (GTK_EDITABLE (entry),
1051 state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
1057 * empathy_presence_chooser_create_menu:
1059 * Creates a new #GtkMenu allowing users to change their presence from a menu.
1061 * Return value: a new #GtkMenu for changing presence in a menu.
1064 empathy_presence_chooser_create_menu (void)
1066 const gchar *status;
1072 menu = gtk_menu_new ();
1074 for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
1077 status = empathy_presence_get_default_message (states[i].state);
1078 presence_chooser_menu_add_item (menu,
1082 if (states[i].customisable) {
1083 /* Set custom messages if wanted */
1084 list = empathy_status_presets_get (states[i].state, 5);
1085 for (l = list; l; l = l->next) {
1086 presence_chooser_menu_add_item (menu,
1096 item = gtk_menu_item_new ();
1097 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1098 gtk_widget_show (item);
1100 /* Custom messages */
1101 item = gtk_image_menu_item_new_with_label (_("Custom messagesā¦"));
1102 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
1103 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1104 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1105 gtk_widget_show (image);
1106 gtk_widget_show (item);
1108 g_signal_connect (item,
1110 G_CALLBACK (presence_chooser_custom_activate_cb),
1117 presence_chooser_menu_add_item (GtkWidget *menu,
1119 TpConnectionPresenceType state)
1123 const gchar *icon_name;
1125 item = gtk_image_menu_item_new_with_label (str);
1126 icon_name = empathy_icon_name_for_presence (state);
1128 g_signal_connect (item, "activate",
1129 G_CALLBACK (presence_chooser_noncustom_activate_cb),
1132 image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
1133 gtk_widget_show (image);
1135 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1136 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
1137 gtk_widget_show (item);
1139 g_object_set_data_full (G_OBJECT (item),
1140 "status", g_strdup (str),
1141 (GDestroyNotify) g_free);
1143 g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
1145 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1149 presence_chooser_noncustom_activate_cb (GtkWidget *item,
1152 TpConnectionPresenceType state;
1153 const gchar *status;
1155 status = g_object_get_data (G_OBJECT (item), "status");
1156 state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
1158 presence_chooser_set_state (state, status);
1162 presence_chooser_set_state (TpConnectionPresenceType state,
1163 const gchar *status)
1165 EmpathyPresenceManager *presence_mgr;
1167 presence_mgr = empathy_presence_manager_dup_singleton ();
1168 empathy_presence_manager_set_presence (presence_mgr, state, status);
1169 g_object_unref (presence_mgr);
1173 presence_chooser_custom_activate_cb (GtkWidget *item,
1178 dialog = empathy_status_preset_dialog_new (NULL);
1179 gtk_dialog_run (GTK_DIALOG (dialog));
1180 gtk_widget_destroy (dialog);