1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2005-2007 Imendio AB
4 * Copyright (C) 2009 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Richard Hult <richard@imendio.com>
22 * Martyn Russell <martyn@imendio.com>
23 * Xavier Claessens <xclaesse@gmail.com>
24 * Danielle Madeley <danielle.madeley@collabora.co.uk>
32 #include <glib/gi18n-lib.h>
34 #include <gdk/gdkkeysyms.h>
36 #include <telepathy-glib/account-manager.h>
37 #include <telepathy-glib/util.h>
39 #include <libempathy/empathy-presence-manager.h>
40 #include <libempathy/empathy-utils.h>
41 #include <libempathy/empathy-status-presets.h>
43 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
44 #include <libempathy/empathy-debug.h>
46 #include "empathy-ui-utils.h"
47 #include "empathy-images.h"
48 #include "empathy-presence-chooser.h"
49 #include "empathy-status-preset-dialog.h"
52 * SECTION:empathy-presence-chooser
53 * @title:EmpathyPresenceChooser
54 * @short_description: A widget used to change presence
55 * @include: libempathy-gtk/empathy-presence-chooser.h
57 * #EmpathyPresenceChooser is a widget which extends #GtkComboBoxEntry
62 * EmpathyAccountChooser:
63 * @parent: parent object
65 * Widget which extends #GtkComboBoxEntry to change presence.
68 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyPresenceChooser)
70 /* For custom message dialog */
78 /* For combobox's model */
84 COL_STATUS_CUSTOMISABLE,
94 ENTRY_TYPE_EDIT_CUSTOM,
95 } PresenceChooserEntryType;
98 EmpathyPresenceManager *presence_mgr;
99 GNetworkMonitor *connectivity;
101 gboolean editing_status;
102 int block_set_editing;
104 guint focus_out_idle_source;
106 TpConnectionPresenceType state;
107 PresenceChooserEntryType previous_type;
109 TpAccountManager *account_manager;
110 } EmpathyPresenceChooserPriv;
112 /* States to be listed in the menu.
113 * Each state has a boolean telling if it can have custom message */
114 static struct { TpConnectionPresenceType state;
115 gboolean customisable;
116 } states[] = { { TP_CONNECTION_PRESENCE_TYPE_AVAILABLE, TRUE } ,
117 { TP_CONNECTION_PRESENCE_TYPE_BUSY, TRUE },
118 { TP_CONNECTION_PRESENCE_TYPE_AWAY, TRUE },
119 { TP_CONNECTION_PRESENCE_TYPE_HIDDEN, FALSE },
120 { TP_CONNECTION_PRESENCE_TYPE_OFFLINE, FALSE},
121 { TP_CONNECTION_PRESENCE_TYPE_UNSET, },
124 static void presence_chooser_constructed (GObject *object);
125 static void presence_chooser_finalize (GObject *object);
126 static void presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser);
127 static void presence_chooser_menu_add_item (GtkWidget *menu,
129 TpConnectionPresenceType state);
130 static void presence_chooser_noncustom_activate_cb (GtkWidget *item,
132 static void presence_chooser_set_state (TpConnectionPresenceType state,
133 const gchar *status);
134 static void presence_chooser_custom_activate_cb (GtkWidget *item,
137 G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_COMBO_BOX);
140 empathy_presence_chooser_class_init (EmpathyPresenceChooserClass *klass)
142 GObjectClass *object_class = G_OBJECT_CLASS (klass);
144 object_class->constructed = presence_chooser_constructed;
145 object_class->finalize = presence_chooser_finalize;
147 g_type_class_add_private (object_class, sizeof (EmpathyPresenceChooserPriv));
151 presence_chooser_create_model (EmpathyPresenceChooser *self)
154 char *custom_message;
157 store = gtk_list_store_new (N_COLUMNS,
158 G_TYPE_STRING, /* COL_STATUS_TEXT */
159 G_TYPE_STRING, /* COL_STATE_ICON_NAME */
160 G_TYPE_UINT, /* COL_STATE */
161 G_TYPE_STRING, /* COL_DISPLAY_MARKUP */
162 G_TYPE_BOOLEAN, /* COL_STATUS_CUSTOMISABLE */
163 G_TYPE_INT); /* COL_TYPE */
165 custom_message = g_strdup_printf ("<i>%s</i>", _("Custom Message…"));
167 for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
169 const char *status, *icon_name;
171 status = empathy_presence_get_default_message (states[i].state);
172 icon_name = empathy_icon_name_for_presence (states[i].state);
174 gtk_list_store_insert_with_values (store, NULL, -1,
175 COL_STATUS_TEXT, status,
176 COL_STATE_ICON_NAME, icon_name,
177 COL_STATE, states[i].state,
178 COL_DISPLAY_MARKUP, status,
179 COL_STATUS_CUSTOMISABLE, states[i].customisable,
180 COL_TYPE, ENTRY_TYPE_BUILTIN,
183 if (states[i].customisable) {
184 /* Set custom messages if wanted */
185 list = empathy_status_presets_get (states[i].state, -1);
186 list = g_list_sort (list, (GCompareFunc) g_utf8_collate);
187 for (l = list; l; l = l->next) {
188 gtk_list_store_insert_with_values (store,
190 COL_STATUS_TEXT, l->data,
191 COL_STATE_ICON_NAME, icon_name,
192 COL_STATE, states[i].state,
193 COL_DISPLAY_MARKUP, l->data,
194 COL_STATUS_CUSTOMISABLE, TRUE,
195 COL_TYPE, ENTRY_TYPE_SAVED,
200 gtk_list_store_insert_with_values (store, NULL, -1,
201 COL_STATUS_TEXT, _("Custom Message…"),
202 COL_STATE_ICON_NAME, icon_name,
203 COL_STATE, states[i].state,
204 COL_DISPLAY_MARKUP, custom_message,
205 COL_STATUS_CUSTOMISABLE, TRUE,
206 COL_TYPE, ENTRY_TYPE_CUSTOM,
212 /* add a separator */
213 gtk_list_store_insert_with_values (store, NULL, -1,
214 COL_TYPE, ENTRY_TYPE_SEPARATOR,
217 gtk_list_store_insert_with_values (store, NULL, -1,
218 COL_STATUS_TEXT, _("Edit Custom Messages…"),
219 COL_STATE_ICON_NAME, GTK_STOCK_EDIT,
220 COL_DISPLAY_MARKUP, _("Edit Custom Messages…"),
221 COL_TYPE, ENTRY_TYPE_EDIT_CUSTOM,
224 g_free (custom_message);
226 gtk_combo_box_set_model (GTK_COMBO_BOX (self), GTK_TREE_MODEL (store));
227 g_object_unref (store);
231 presence_chooser_popup_shown_cb (GObject *self,
235 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
238 g_object_get (self, "popup-shown", &shown, NULL);
243 /* see presence_chooser_entry_focus_out_cb () for what this does */
244 if (priv->focus_out_idle_source != 0) {
245 g_source_remove (priv->focus_out_idle_source);
246 priv->focus_out_idle_source = 0;
249 presence_chooser_create_model (EMPATHY_PRESENCE_CHOOSER (self));
252 static PresenceChooserEntryType
253 presence_chooser_get_entry_type (EmpathyPresenceChooser *self)
256 PresenceChooserEntryType type = -1;
258 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter)) {
259 type = ENTRY_TYPE_CUSTOM;
264 model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
265 gtk_tree_model_get (model, &iter,
273 static TpConnectionPresenceType
274 get_state_and_status (EmpathyPresenceChooser *self,
277 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
278 TpConnectionPresenceType state;
281 state = tp_account_manager_get_most_available_presence (
282 priv->account_manager, NULL, &tmp);
283 if (EMP_STR_EMPTY (tmp)) {
284 /* no message, use the default message */
286 tmp = g_strdup (empathy_presence_get_default_message (state));
298 presence_chooser_is_preset (EmpathyPresenceChooser *self)
300 TpConnectionPresenceType state;
303 gboolean match = FALSE;
305 state = get_state_and_status (self, &status);
307 presets = empathy_status_presets_get (state, -1);
308 for (l = presets; l; l = l->next) {
309 char *preset = (char *) l->data;
311 if (!tp_strdiff (status, preset)) {
317 g_list_free (presets);
319 DEBUG ("is_preset(%i, %s) = %i", state, status, match);
326 presence_chooser_set_favorite_icon (EmpathyPresenceChooser *self)
329 PresenceChooserEntryType type;
331 entry = gtk_bin_get_child (GTK_BIN (self));
332 type = presence_chooser_get_entry_type (self);
334 if (type == ENTRY_TYPE_CUSTOM || type == ENTRY_TYPE_SAVED) {
335 if (presence_chooser_is_preset (self)) {
336 /* saved entries can be removed from the list */
337 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
338 GTK_ENTRY_ICON_SECONDARY,
340 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
341 GTK_ENTRY_ICON_SECONDARY,
342 _("Click to remove this status as a favorite"));
345 /* custom entries can be favorited */
346 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
347 GTK_ENTRY_ICON_SECONDARY,
348 "non-starred-symbolic");
349 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
350 GTK_ENTRY_ICON_SECONDARY,
351 _("Click to make this status a favorite"));
355 /* built-in entries cannot be favorited */
356 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
357 GTK_ENTRY_ICON_SECONDARY,
359 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
360 GTK_ENTRY_ICON_SECONDARY,
366 presence_chooser_set_status_editing (EmpathyPresenceChooser *self,
369 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
372 if (priv->block_set_editing) {
376 entry = gtk_bin_get_child (GTK_BIN (self));
381 priv->editing_status = TRUE;
383 get_state_and_status (self, &status);
384 /* Translators: %s is a status message like 'At the pub' for example */
385 tooltip_text = g_strdup_printf (_("<b>Current message: %s</b>\n"
386 "<small><i>Press Enter to set the new message or Esc to cancel.</i></small>"),
388 gtk_widget_set_tooltip_markup (entry, tooltip_text);
389 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
390 GTK_ENTRY_ICON_SECONDARY,
392 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
393 GTK_ENTRY_ICON_SECONDARY,
395 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
396 GTK_ENTRY_ICON_PRIMARY,
399 g_free (tooltip_text);
403 presence_chooser_set_favorite_icon (self);
404 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
405 GTK_ENTRY_ICON_PRIMARY,
408 /* attempt to get the toplevel for this widget */
409 window = gtk_widget_get_toplevel (GTK_WIDGET (self));
410 if (gtk_widget_is_toplevel (window) && GTK_IS_WINDOW (window)) {
411 /* unset the focus */
412 gtk_window_set_focus (GTK_WINDOW (window), NULL);
415 /* see presence_chooser_entry_focus_out_cb ()
416 * for what this does */
417 if (priv->focus_out_idle_source != 0) {
418 g_source_remove (priv->focus_out_idle_source);
419 priv->focus_out_idle_source = 0;
422 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
424 priv->editing_status = FALSE;
429 mc_set_custom_state (EmpathyPresenceChooser *self)
431 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
435 entry = gtk_bin_get_child (GTK_BIN (self));
436 /* update the status with MC */
437 status = gtk_entry_get_text (GTK_ENTRY (entry));
439 DEBUG ("Sending state to MC-> %d (%s)", priv->state, status);
441 empathy_presence_manager_set_presence (priv->presence_mgr, priv->state, status);
445 ui_set_custom_state (EmpathyPresenceChooser *self,
446 TpConnectionPresenceType state,
449 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
451 const char *icon_name;
452 const gchar *status_tooltip;
454 entry = gtk_bin_get_child (GTK_BIN (self));
456 priv->block_set_editing++;
457 priv->block_changed++;
459 icon_name = empathy_icon_name_for_presence (state);
460 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
461 GTK_ENTRY_ICON_PRIMARY,
463 status_tooltip = status == NULL ? "" : status;
464 gtk_entry_set_text (GTK_ENTRY (entry), status_tooltip);
465 gtk_widget_set_tooltip_text (GTK_WIDGET (entry), status_tooltip);
466 presence_chooser_set_favorite_icon (self);
468 priv->block_changed--;
469 priv->block_set_editing--;
473 presence_chooser_reset_status (EmpathyPresenceChooser *self)
475 /* recover the status that was unset */
476 presence_chooser_set_status_editing (self, FALSE);
477 presence_chooser_presence_changed_cb (self);
481 presence_chooser_entry_icon_release_cb (EmpathyPresenceChooser *self,
482 GtkEntryIconPosition icon_pos,
486 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
488 if (priv->editing_status) {
489 presence_chooser_set_status_editing (self, FALSE);
490 mc_set_custom_state (self);
493 TpConnectionPresenceType state;
496 state = get_state_and_status (self, &status);
498 if (!empathy_status_presets_is_valid (state)) {
499 /* It doesn't make sense to add such presence as favorite */
504 if (presence_chooser_is_preset (self)) {
505 /* remove the entry */
506 DEBUG ("REMOVING PRESET (%i, %s)", state, status);
507 empathy_status_presets_remove (state, status);
511 DEBUG ("SAVING PRESET (%i, %s)", state, status);
512 empathy_status_presets_set_last (state, status);
515 /* update the icon */
516 presence_chooser_set_favorite_icon (self);
522 presence_chooser_entry_activate_cb (EmpathyPresenceChooser *self,
525 presence_chooser_set_status_editing (self, FALSE);
526 mc_set_custom_state (self);
530 presence_chooser_entry_key_press_event_cb (EmpathyPresenceChooser *self,
534 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
536 if (priv->editing_status && event->keyval == GDK_KEY_Escape) {
537 /* the user pressed Escape, undo the editing */
538 presence_chooser_reset_status (self);
541 else if (event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_Down) {
546 return FALSE; /* send this event elsewhere */
550 presence_chooser_entry_button_press_event_cb (EmpathyPresenceChooser *self,
551 GdkEventButton *event,
554 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
556 if (!priv->editing_status &&
557 event->button == 1 &&
558 !gtk_widget_has_focus (entry)) {
559 gtk_widget_grab_focus (entry);
560 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
569 presence_chooser_entry_changed_cb (EmpathyPresenceChooser *self,
572 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
574 if (priv->block_changed){
578 /* the combo is being edited to a custom entry */
579 if (!priv->editing_status) {
580 presence_chooser_set_status_editing (self, TRUE);
585 presence_chooser_changed_cb (GtkComboBox *self, gpointer user_data)
587 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
590 TpConnectionPresenceType new_state;
591 gboolean customisable = TRUE;
592 PresenceChooserEntryType type = -1;
596 if (priv->block_changed ||
597 !gtk_combo_box_get_active_iter (self, &iter)) {
601 model = gtk_combo_box_get_model (self);
602 gtk_tree_model_get (model, &iter,
603 COL_STATE_ICON_NAME, &icon_name,
604 COL_STATE, &new_state,
605 COL_STATUS_CUSTOMISABLE, &customisable,
609 entry = gtk_bin_get_child (GTK_BIN (self));
611 /* some types of status aren't editable, set the editability of the
612 * entry appropriately. Unless we're just about to reset it anyway,
613 * in which case, don't fiddle with it */
614 if (type != ENTRY_TYPE_EDIT_CUSTOM) {
615 gtk_editable_set_editable (GTK_EDITABLE (entry), customisable);
616 priv->state = new_state;
619 if (type == ENTRY_TYPE_EDIT_CUSTOM) {
620 GtkWidget *window, *dialog;
622 presence_chooser_reset_status (EMPATHY_PRESENCE_CHOOSER (self));
624 /* attempt to get the toplevel for this widget */
625 window = gtk_widget_get_toplevel (GTK_WIDGET (self));
626 if (!gtk_widget_is_toplevel (window) || !GTK_IS_WINDOW (window)) {
630 dialog = empathy_status_preset_dialog_new (GTK_WINDOW (window));
631 gtk_dialog_run (GTK_DIALOG (dialog));
632 gtk_widget_destroy (dialog);
634 else if (type == ENTRY_TYPE_CUSTOM) {
635 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
636 GTK_ENTRY_ICON_PRIMARY,
639 /* preseed the status */
640 if (priv->previous_type == ENTRY_TYPE_BUILTIN) {
641 /* if their previous entry was a builtin, don't
643 gtk_entry_set_text (GTK_ENTRY (entry), "");
645 /* else preseed the text of their currently entered
649 get_state_and_status (EMPATHY_PRESENCE_CHOOSER (self),
651 gtk_entry_set_text (GTK_ENTRY (entry), status);
656 gtk_widget_grab_focus (entry);
660 /* just in case we were setting a new status when
661 * things were changed */
662 presence_chooser_set_status_editing (
663 EMPATHY_PRESENCE_CHOOSER (self),
665 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
666 GTK_ENTRY_ICON_PRIMARY,
669 gtk_tree_model_get (model, &iter,
670 COL_STATUS_TEXT, &status,
673 empathy_presence_manager_set_presence (priv->presence_mgr, priv->state, status);
678 if (type != ENTRY_TYPE_EDIT_CUSTOM) {
679 priv->previous_type = type;
685 combo_row_separator_func (GtkTreeModel *model,
689 PresenceChooserEntryType type;
691 gtk_tree_model_get (model, iter,
695 return (type == ENTRY_TYPE_SEPARATOR);
699 presence_chooser_entry_focus_out_idle_cb (gpointer user_data)
701 EmpathyPresenceChooser *chooser;
704 DEBUG ("Autocommiting status message");
706 chooser = EMPATHY_PRESENCE_CHOOSER (user_data);
707 entry = gtk_bin_get_child (GTK_BIN (chooser));
709 presence_chooser_entry_activate_cb (chooser, GTK_ENTRY (entry));
715 presence_chooser_entry_focus_out_cb (EmpathyPresenceChooser *chooser,
716 GdkEventFocus *event,
719 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
721 if (priv->editing_status) {
722 /* this seems a bit evil and maybe it will be fragile,
723 * someone should think of a better way to do it.
725 * The entry has focused out, but we don't know where the focus
726 * has gone. If it goes to the combo box, we don't want to
727 * do anything. If it's gone anywhere else, we want to commit
730 * Thus we install this idle handler and store its source.
731 * If the source is scheduled when the popup handler runs,
732 * it will remove it, else the callback will commit the result.
734 priv->focus_out_idle_source = g_idle_add (
735 presence_chooser_entry_focus_out_idle_cb,
739 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
745 update_sensitivity_am_prepared_cb (GObject *source_object,
746 GAsyncResult *result,
749 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
750 EmpathyPresenceChooser *chooser = user_data;
751 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
752 gboolean sensitive = FALSE;
754 GError *error = NULL;
756 if (!tp_proxy_prepare_finish (manager, result, &error)) {
757 DEBUG ("Failed to prepare account manager: %s", error->message);
758 g_error_free (error);
762 accounts = tp_account_manager_dup_valid_accounts (manager);
764 for (l = accounts ; l != NULL ; l = g_list_next (l)) {
765 TpAccount *a = TP_ACCOUNT (l->data);
767 if (tp_account_is_enabled (a)) {
773 g_list_free_full (accounts, g_object_unref);
775 if (!g_network_monitor_get_network_available (priv->connectivity))
778 gtk_widget_set_sensitive (GTK_WIDGET (chooser), sensitive);
780 presence_chooser_presence_changed_cb (chooser);
784 presence_chooser_update_sensitivity (EmpathyPresenceChooser *chooser)
786 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
788 tp_proxy_prepare_async (priv->account_manager, NULL,
789 update_sensitivity_am_prepared_cb,
794 presence_chooser_account_manager_account_validity_changed_cb (
795 TpAccountManager *manager,
798 EmpathyPresenceChooser *chooser)
800 presence_chooser_update_sensitivity (chooser);
804 presence_chooser_account_manager_account_changed_cb (
805 TpAccountManager *manager,
807 EmpathyPresenceChooser *chooser)
809 presence_chooser_update_sensitivity (chooser);
813 presence_chooser_network_change (GNetworkMonitor *connectivity,
815 EmpathyPresenceChooser *chooser)
817 presence_chooser_update_sensitivity (chooser);
821 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
823 EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
824 EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
826 chooser->priv = priv;
830 presence_chooser_constructed (GObject *object)
832 EmpathyPresenceChooser *chooser = EMPATHY_PRESENCE_CHOOSER (object);
833 EmpathyPresenceChooserPriv *priv = chooser->priv;
835 GtkCellRenderer *renderer;
836 const gchar *status_tooltip;
838 presence_chooser_create_model (chooser);
840 gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX (chooser),
842 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
843 combo_row_separator_func,
846 entry = gtk_bin_get_child (GTK_BIN (chooser));
847 gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
848 GTK_ENTRY_ICON_PRIMARY,
851 g_signal_connect_swapped (entry, "icon-release",
852 G_CALLBACK (presence_chooser_entry_icon_release_cb),
854 g_signal_connect_swapped (entry, "activate",
855 G_CALLBACK (presence_chooser_entry_activate_cb),
857 g_signal_connect_swapped (entry, "key-press-event",
858 G_CALLBACK (presence_chooser_entry_key_press_event_cb),
860 g_signal_connect_swapped (entry, "button-press-event",
861 G_CALLBACK (presence_chooser_entry_button_press_event_cb),
864 gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
866 renderer = gtk_cell_renderer_pixbuf_new ();
867 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, FALSE);
868 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
869 "icon-name", COL_STATE_ICON_NAME,
871 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
873 renderer = gtk_cell_renderer_text_new ();
874 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, TRUE);
875 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
876 "markup", COL_DISPLAY_MARKUP,
878 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
880 g_signal_connect (chooser, "notify::popup-shown",
881 G_CALLBACK (presence_chooser_popup_shown_cb), NULL);
882 g_signal_connect (chooser, "changed",
883 G_CALLBACK (presence_chooser_changed_cb), NULL);
884 g_signal_connect_swapped (entry, "changed",
885 G_CALLBACK (presence_chooser_entry_changed_cb),
887 g_signal_connect_swapped (entry, "focus-out-event",
888 G_CALLBACK (presence_chooser_entry_focus_out_cb),
891 priv->presence_mgr = empathy_presence_manager_dup_singleton ();
893 priv->account_manager = tp_account_manager_dup ();
894 g_signal_connect_swapped (priv->account_manager,
895 "most-available-presence-changed",
896 G_CALLBACK (presence_chooser_presence_changed_cb),
899 tp_g_signal_connect_object (priv->account_manager, "account-validity-changed",
900 G_CALLBACK (presence_chooser_account_manager_account_validity_changed_cb),
902 tp_g_signal_connect_object (priv->account_manager, "account-removed",
903 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
905 tp_g_signal_connect_object (priv->account_manager, "account-enabled",
906 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
908 tp_g_signal_connect_object (priv->account_manager, "account-disabled",
909 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
912 status_tooltip = gtk_entry_get_text (GTK_ENTRY (entry));
913 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser), status_tooltip);
915 priv->connectivity = g_network_monitor_get_default ();
916 g_object_ref (priv->connectivity);
918 tp_g_signal_connect_object (priv->connectivity,
920 G_CALLBACK (presence_chooser_network_change),
923 presence_chooser_update_sensitivity (chooser);
927 presence_chooser_finalize (GObject *object)
929 EmpathyPresenceChooserPriv *priv;
931 priv = GET_PRIV (object);
933 if (priv->focus_out_idle_source) {
934 g_source_remove (priv->focus_out_idle_source);
937 if (priv->account_manager != NULL)
938 g_object_unref (priv->account_manager);
940 g_signal_handlers_disconnect_by_func (priv->presence_mgr,
941 presence_chooser_presence_changed_cb,
943 g_object_unref (priv->presence_mgr);
945 g_object_unref (priv->connectivity);
947 G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
951 * empathy_presence_chooser_new:
953 * Creates a new #EmpathyPresenceChooser widget.
955 * Return value: A new #EmpathyPresenceChooser widget
958 empathy_presence_chooser_new (void)
960 /* FIXME, why can't this go in init ()? */
961 return g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER,
967 presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
969 EmpathyPresenceChooserPriv *priv;
970 TpConnectionPresenceType state;
974 gboolean valid, match_state = FALSE, match = FALSE;
977 priv = GET_PRIV (chooser);
979 if (priv->editing_status) {
983 state = get_state_and_status (chooser, &status);
986 /* An unset presence here doesn't make any sense. Force it to appear as
988 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
989 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
992 /* look through the model and attempt to find a matching state */
993 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
994 for (valid = gtk_tree_model_get_iter_first (model, &iter);
996 valid = gtk_tree_model_iter_next (model, &iter)) {
998 TpConnectionPresenceType m_state;
1001 gtk_tree_model_get (model, &iter,
1002 COL_STATE, &m_state,
1006 if (m_type == ENTRY_TYPE_CUSTOM ||
1007 m_type == ENTRY_TYPE_SEPARATOR ||
1008 m_type == ENTRY_TYPE_EDIT_CUSTOM) {
1011 else if (!match_state && state == m_state) {
1012 /* we are now in the section that can contain our
1016 else if (match_state && state != m_state) {
1017 /* we have passed the section that can contain our
1022 gtk_tree_model_get (model, &iter,
1023 COL_STATUS_TEXT, &m_status,
1026 match = !tp_strdiff (status, m_status);
1035 priv->block_changed++;
1036 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
1037 presence_chooser_set_favorite_icon (chooser);
1038 priv->block_changed--;
1041 ui_set_custom_state (chooser, state, status);
1044 entry = gtk_bin_get_child (GTK_BIN (chooser));
1045 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
1046 GTK_ENTRY_ICON_PRIMARY,
1047 empathy_icon_name_for_presence (state));
1048 gtk_widget_set_tooltip_text (GTK_WIDGET (entry), status);
1050 entry = gtk_bin_get_child (GTK_BIN (chooser));
1051 gtk_editable_set_editable (GTK_EDITABLE (entry),
1052 state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
1058 * empathy_presence_chooser_create_menu:
1060 * Creates a new #GtkMenu allowing users to change their presence from a menu.
1062 * Return value: a new #GtkMenu for changing presence in a menu.
1065 empathy_presence_chooser_create_menu (void)
1067 const gchar *status;
1073 menu = gtk_menu_new ();
1075 for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
1078 status = empathy_presence_get_default_message (states[i].state);
1079 presence_chooser_menu_add_item (menu,
1083 if (states[i].customisable) {
1084 /* Set custom messages if wanted */
1085 list = empathy_status_presets_get (states[i].state, 5);
1086 for (l = list; l; l = l->next) {
1087 presence_chooser_menu_add_item (menu,
1097 item = gtk_menu_item_new ();
1098 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1099 gtk_widget_show (item);
1101 /* Custom messages */
1102 item = gtk_image_menu_item_new_with_label (_("Custom messages…"));
1103 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
1104 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1105 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1106 gtk_widget_show (image);
1107 gtk_widget_show (item);
1109 g_signal_connect (item,
1111 G_CALLBACK (presence_chooser_custom_activate_cb),
1118 presence_chooser_menu_add_item (GtkWidget *menu,
1120 TpConnectionPresenceType state)
1124 const gchar *icon_name;
1126 item = gtk_image_menu_item_new_with_label (str);
1127 icon_name = empathy_icon_name_for_presence (state);
1129 g_signal_connect (item, "activate",
1130 G_CALLBACK (presence_chooser_noncustom_activate_cb),
1133 image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
1134 gtk_widget_show (image);
1136 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1137 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
1138 gtk_widget_show (item);
1140 g_object_set_data_full (G_OBJECT (item),
1141 "status", g_strdup (str),
1142 (GDestroyNotify) g_free);
1144 g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
1146 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1150 presence_chooser_noncustom_activate_cb (GtkWidget *item,
1153 TpConnectionPresenceType state;
1154 const gchar *status;
1156 status = g_object_get_data (G_OBJECT (item), "status");
1157 state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
1159 presence_chooser_set_state (state, status);
1163 presence_chooser_set_state (TpConnectionPresenceType state,
1164 const gchar *status)
1166 EmpathyPresenceManager *presence_mgr;
1168 presence_mgr = empathy_presence_manager_dup_singleton ();
1169 empathy_presence_manager_set_presence (presence_mgr, state, status);
1170 g_object_unref (presence_mgr);
1174 presence_chooser_custom_activate_cb (GtkWidget *item,
1179 dialog = empathy_status_preset_dialog_new (NULL);
1180 gtk_dialog_run (GTK_DIALOG (dialog));
1181 gtk_widget_destroy (dialog);