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 TpConnectionPresenceType flash_state_1;
114 TpConnectionPresenceType flash_state_2;
115 guint flash_timeout_id;
117 TpAccountManager *account_manager;
118 } EmpathyPresenceChooserPriv;
120 /* States to be listed in the menu.
121 * Each state has a boolean telling if it can have custom message */
122 static struct { TpConnectionPresenceType state;
123 gboolean customisable;
124 } states[] = { { TP_CONNECTION_PRESENCE_TYPE_AVAILABLE, TRUE } ,
125 { TP_CONNECTION_PRESENCE_TYPE_BUSY, TRUE },
126 { TP_CONNECTION_PRESENCE_TYPE_AWAY, TRUE },
127 { TP_CONNECTION_PRESENCE_TYPE_HIDDEN, FALSE },
128 { TP_CONNECTION_PRESENCE_TYPE_OFFLINE, FALSE},
129 { TP_CONNECTION_PRESENCE_TYPE_UNSET, },
132 static void presence_chooser_finalize (GObject *object);
133 static void presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser);
134 static gboolean presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser);
135 static void presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
136 TpConnectionPresenceType state_1,
137 TpConnectionPresenceType state_2);
138 static void presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
139 TpConnectionPresenceType state);
140 static void presence_chooser_menu_add_item (GtkWidget *menu,
142 TpConnectionPresenceType state);
143 static void presence_chooser_noncustom_activate_cb (GtkWidget *item,
145 static void presence_chooser_set_state (TpConnectionPresenceType state,
146 const gchar *status);
147 static void presence_chooser_custom_activate_cb (GtkWidget *item,
150 G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_COMBO_BOX_ENTRY);
153 empathy_presence_chooser_class_init (EmpathyPresenceChooserClass *klass)
155 GObjectClass *object_class = G_OBJECT_CLASS (klass);
157 object_class->finalize = presence_chooser_finalize;
159 g_type_class_add_private (object_class, sizeof (EmpathyPresenceChooserPriv));
163 presence_chooser_create_model (EmpathyPresenceChooser *self)
166 char *custom_message;
169 store = gtk_list_store_new (N_COLUMNS,
170 G_TYPE_STRING, /* COL_STATUS_TEXT */
171 G_TYPE_STRING, /* COL_STATE_ICON_NAME */
172 G_TYPE_UINT, /* COL_STATE */
173 G_TYPE_STRING, /* COL_DISPLAY_MARKUP */
174 G_TYPE_BOOLEAN, /* COL_STATUS_CUSTOMISABLE */
175 G_TYPE_INT); /* COL_TYPE */
177 custom_message = g_strdup_printf ("<i>%s</i>", _("Custom Message..."));
179 for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
181 const char *status, *icon_name;
183 status = empathy_presence_get_default_message (states[i].state);
184 icon_name = empathy_icon_name_for_presence (states[i].state);
186 gtk_list_store_insert_with_values (store, NULL, -1,
187 COL_STATUS_TEXT, status,
188 COL_STATE_ICON_NAME, icon_name,
189 COL_STATE, states[i].state,
190 COL_DISPLAY_MARKUP, status,
191 COL_STATUS_CUSTOMISABLE, states[i].customisable,
192 COL_TYPE, ENTRY_TYPE_BUILTIN,
195 if (states[i].customisable) {
196 /* Set custom messages if wanted */
197 list = empathy_status_presets_get (states[i].state, -1);
198 list = g_list_sort (list, (GCompareFunc) g_utf8_collate);
199 for (l = list; l; l = l->next) {
200 gtk_list_store_insert_with_values (store,
202 COL_STATUS_TEXT, l->data,
203 COL_STATE_ICON_NAME, icon_name,
204 COL_STATE, states[i].state,
205 COL_DISPLAY_MARKUP, l->data,
206 COL_STATUS_CUSTOMISABLE, TRUE,
207 COL_TYPE, ENTRY_TYPE_SAVED,
212 gtk_list_store_insert_with_values (store, NULL, -1,
213 COL_STATUS_TEXT, _("Custom Message..."),
214 COL_STATE_ICON_NAME, icon_name,
215 COL_STATE, states[i].state,
216 COL_DISPLAY_MARKUP, custom_message,
217 COL_STATUS_CUSTOMISABLE, TRUE,
218 COL_TYPE, ENTRY_TYPE_CUSTOM,
224 /* add a separator */
225 gtk_list_store_insert_with_values (store, NULL, -1,
226 COL_TYPE, ENTRY_TYPE_SEPARATOR,
229 gtk_list_store_insert_with_values (store, NULL, -1,
230 COL_STATUS_TEXT, _("Edit Custom Messages..."),
231 COL_STATE_ICON_NAME, GTK_STOCK_EDIT,
232 COL_DISPLAY_MARKUP, _("Edit Custom Messages..."),
233 COL_TYPE, ENTRY_TYPE_EDIT_CUSTOM,
236 g_free (custom_message);
238 gtk_combo_box_set_model (GTK_COMBO_BOX (self), GTK_TREE_MODEL (store));
239 g_object_unref (store);
243 presence_chooser_popup_shown_cb (GObject *self,
247 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
250 g_object_get (self, "popup-shown", &shown, NULL);
255 /* see presence_chooser_entry_focus_out_cb () for what this does */
256 if (priv->focus_out_idle_source != 0) {
257 g_source_remove (priv->focus_out_idle_source);
258 priv->focus_out_idle_source = 0;
261 presence_chooser_create_model (EMPATHY_PRESENCE_CHOOSER (self));
264 static PresenceChooserEntryType
265 presence_chooser_get_entry_type (EmpathyPresenceChooser *self)
268 PresenceChooserEntryType type = -1;
270 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter)) {
271 type = ENTRY_TYPE_CUSTOM;
276 model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
277 gtk_tree_model_get (model, &iter,
286 presence_chooser_is_preset (EmpathyPresenceChooser *self)
288 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
289 TpConnectionPresenceType state;
292 gboolean match = FALSE;
294 state = empathy_idle_get_state (priv->idle);
295 status = empathy_idle_get_status (priv->idle);
297 presets = empathy_status_presets_get (state, -1);
298 for (l = presets; l; l = l->next) {
299 char *preset = (char *) l->data;
301 if (!tp_strdiff (status, preset)) {
307 g_list_free (presets);
309 DEBUG ("is_preset(%i, %s) = %i", state, status, match);
315 presence_chooser_set_favorite_icon (EmpathyPresenceChooser *self)
318 PresenceChooserEntryType type;
320 entry = gtk_bin_get_child (GTK_BIN (self));
321 type = presence_chooser_get_entry_type (self);
323 if (type == ENTRY_TYPE_CUSTOM || type == ENTRY_TYPE_SAVED) {
324 if (presence_chooser_is_preset (self)) {
325 /* saved entries can be removed from the list */
326 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
327 GTK_ENTRY_ICON_SECONDARY,
329 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
330 GTK_ENTRY_ICON_SECONDARY,
331 _("Click to remove this status as a favorite"));
334 /* custom entries can be favorited */
335 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
336 GTK_ENTRY_ICON_SECONDARY,
337 "empathy-unstarred");
338 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
339 GTK_ENTRY_ICON_SECONDARY,
340 _("Click to make this status a favorite"));
344 /* built-in entries cannot be favorited */
345 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
346 GTK_ENTRY_ICON_SECONDARY,
348 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
349 GTK_ENTRY_ICON_SECONDARY,
355 presence_chooser_set_status_editing (EmpathyPresenceChooser *self,
358 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
361 if (priv->block_set_editing) {
365 entry = gtk_bin_get_child (GTK_BIN (self));
367 priv->editing_status = TRUE;
369 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
370 GTK_ENTRY_ICON_SECONDARY,
372 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
373 GTK_ENTRY_ICON_SECONDARY,
375 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
376 GTK_ENTRY_ICON_PRIMARY,
381 presence_chooser_set_favorite_icon (self);
382 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
383 GTK_ENTRY_ICON_PRIMARY,
386 /* attempt to get the toplevel for this widget */
387 window = gtk_widget_get_toplevel (GTK_WIDGET (self));
388 if (GTK_WIDGET_TOPLEVEL (window) && GTK_IS_WINDOW (window)) {
389 /* unset the focus */
390 gtk_window_set_focus (GTK_WINDOW (window), NULL);
393 /* see presence_chooser_entry_focus_out_cb ()
394 * for what this does */
395 if (priv->focus_out_idle_source != 0) {
396 g_source_remove (priv->focus_out_idle_source);
397 priv->focus_out_idle_source = 0;
400 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
402 priv->editing_status = FALSE;
407 mc_set_custom_state (EmpathyPresenceChooser *self)
409 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
413 entry = gtk_bin_get_child (GTK_BIN (self));
414 /* update the status with MC */
415 status = gtk_entry_get_text (GTK_ENTRY (entry));
417 DEBUG ("Sending state to MC-> %d (%s)", priv->state, status);
419 empathy_idle_set_presence (priv->idle, priv->state, status);
423 ui_set_custom_state (EmpathyPresenceChooser *self,
424 TpConnectionPresenceType state,
427 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
429 const char *icon_name;
431 entry = gtk_bin_get_child (GTK_BIN (self));
433 priv->block_set_editing++;
434 priv->block_changed++;
436 icon_name = empathy_icon_name_for_presence (state);
437 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
438 GTK_ENTRY_ICON_PRIMARY,
440 gtk_entry_set_text (GTK_ENTRY (entry), status == NULL ? "" : status);
441 presence_chooser_set_favorite_icon (self);
443 priv->block_changed--;
444 priv->block_set_editing--;
448 presence_chooser_reset_status (EmpathyPresenceChooser *self)
450 /* recover the status that was unset */
451 presence_chooser_set_status_editing (self, FALSE);
452 presence_chooser_presence_changed_cb (self);
456 presence_chooser_entry_icon_release_cb (EmpathyPresenceChooser *self,
457 GtkEntryIconPosition icon_pos,
461 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
463 if (priv->editing_status) {
464 presence_chooser_set_status_editing (self, FALSE);
465 mc_set_custom_state (self);
468 PresenceChooserEntryType type;
469 TpConnectionPresenceType state;
472 type = presence_chooser_get_entry_type (self);
473 state = empathy_idle_get_state (priv->idle);
474 status = empathy_idle_get_status (priv->idle);
476 if (!empathy_status_presets_is_valid (state))
477 /* It doesn't make sense to add such presence as favorite */
480 if (presence_chooser_is_preset (self)) {
481 /* remove the entry */
482 DEBUG ("REMOVING PRESET (%i, %s)", state, status);
483 empathy_status_presets_remove (state, status);
487 DEBUG ("SAVING PRESET (%i, %s)", state, status);
488 empathy_status_presets_set_last (state, status);
491 /* update the icon */
492 presence_chooser_set_favorite_icon (self);
497 presence_chooser_entry_activate_cb (EmpathyPresenceChooser *self,
500 presence_chooser_set_status_editing (self, FALSE);
501 mc_set_custom_state (self);
505 presence_chooser_entry_key_press_event_cb (EmpathyPresenceChooser *self,
509 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
511 if (priv->editing_status && event->keyval == GDK_Escape) {
512 /* the user pressed Escape, undo the editing */
513 presence_chooser_reset_status (self);
516 else if (event->keyval == GDK_Up || event->keyval == GDK_Down) {
521 return FALSE; /* send this event elsewhere */
525 presence_chooser_entry_button_press_event_cb (EmpathyPresenceChooser *self,
526 GdkEventButton *event,
529 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
531 if (!priv->editing_status &&
532 event->button == 1 &&
533 !GTK_WIDGET_HAS_FOCUS (entry)) {
534 gtk_widget_grab_focus (entry);
535 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
544 presence_chooser_entry_changed_cb (EmpathyPresenceChooser *self,
547 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
549 if (priv->block_changed){
553 /* the combo is being edited to a custom entry */
554 if (!priv->editing_status) {
555 presence_chooser_set_status_editing (self, TRUE);
560 presence_chooser_changed_cb (GtkComboBox *self, gpointer user_data)
562 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
565 TpConnectionPresenceType new_state;
566 gboolean customisable = TRUE;
567 PresenceChooserEntryType type = -1;
571 if (priv->block_changed ||
572 !gtk_combo_box_get_active_iter (self, &iter)) {
576 model = gtk_combo_box_get_model (self);
577 gtk_tree_model_get (model, &iter,
578 COL_STATE_ICON_NAME, &icon_name,
579 COL_STATE, &new_state,
580 COL_STATUS_CUSTOMISABLE, &customisable,
584 entry = gtk_bin_get_child (GTK_BIN (self));
586 /* some types of status aren't editable, set the editability of the
587 * entry appropriately. Unless we're just about to reset it anyway,
588 * in which case, don't fiddle with it */
589 if (type != ENTRY_TYPE_EDIT_CUSTOM) {
590 gtk_editable_set_editable (GTK_EDITABLE (entry), customisable);
591 priv->state = new_state;
594 if (type == ENTRY_TYPE_EDIT_CUSTOM) {
595 GtkWidget *window, *dialog;
597 presence_chooser_reset_status (EMPATHY_PRESENCE_CHOOSER (self));
599 /* attempt to get the toplevel for this widget */
600 window = gtk_widget_get_toplevel (GTK_WIDGET (self));
601 if (!GTK_WIDGET_TOPLEVEL (window) || !GTK_IS_WINDOW (window)) {
605 dialog = empathy_status_preset_dialog_new (GTK_WINDOW (window));
606 gtk_dialog_run (GTK_DIALOG (dialog));
607 gtk_widget_destroy (dialog);
609 else if (type == ENTRY_TYPE_CUSTOM) {
610 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
611 GTK_ENTRY_ICON_PRIMARY,
614 /* preseed the status */
615 if (priv->previous_type == ENTRY_TYPE_BUILTIN) {
616 /* if their previous entry was a builtin, don't
618 gtk_entry_set_text (GTK_ENTRY (entry), "");
620 /* else preseed the text of their currently entered
624 status = empathy_idle_get_status (priv->idle);
625 gtk_entry_set_text (GTK_ENTRY (entry), status);
629 gtk_widget_grab_focus (entry);
633 /* just in case we were setting a new status when
634 * things were changed */
635 presence_chooser_set_status_editing (
636 EMPATHY_PRESENCE_CHOOSER (self),
638 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
639 GTK_ENTRY_ICON_PRIMARY,
642 gtk_tree_model_get (model, &iter,
643 COL_STATUS_TEXT, &status,
646 empathy_idle_set_presence (priv->idle, priv->state, status);
651 if (type != ENTRY_TYPE_EDIT_CUSTOM) {
652 priv->previous_type = type;
658 combo_row_separator_func (GtkTreeModel *model,
662 PresenceChooserEntryType type;
664 gtk_tree_model_get (model, iter,
668 return (type == ENTRY_TYPE_SEPARATOR);
672 presence_chooser_entry_focus_out_idle_cb (gpointer user_data)
674 EmpathyPresenceChooser *chooser;
677 DEBUG ("Autocommiting status message");
679 chooser = EMPATHY_PRESENCE_CHOOSER (user_data);
680 entry = gtk_bin_get_child (GTK_BIN (chooser));
682 presence_chooser_entry_activate_cb (chooser, GTK_ENTRY (entry));
688 presence_chooser_entry_focus_out_cb (EmpathyPresenceChooser *chooser,
689 GdkEventFocus *event,
692 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
694 if (priv->editing_status) {
695 /* this seems a bit evil and maybe it will be fragile,
696 * someone should think of a better way to do it.
698 * The entry has focused out, but we don't know where the focus
699 * has gone. If it goes to the combo box, we don't want to
700 * do anything. If it's gone anywhere else, we want to commit
703 * Thus we install this idle handler and store its source.
704 * If the source is scheduled when the popup handler runs,
705 * it will remove it, else the callback will commit the result.
707 priv->focus_out_idle_source = g_idle_add (
708 presence_chooser_entry_focus_out_idle_cb,
712 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
718 update_sensitivity_am_prepared_cb (GObject *source_object,
719 GAsyncResult *result,
722 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
723 EmpathyPresenceChooser *chooser = user_data;
724 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
725 gboolean sensitive = FALSE;
727 GError *error = NULL;
729 if (!tp_account_manager_prepare_finish (manager, result, &error)) {
730 DEBUG ("Failed to prepare account manager: %s", error->message);
731 g_error_free (error);
735 accounts = tp_account_manager_get_valid_accounts (manager);
737 for (l = accounts ; l != NULL ; l = g_list_next (l)) {
738 TpAccount *a = TP_ACCOUNT (l->data);
740 if (tp_account_is_enabled (a)) {
746 g_list_free (accounts);
748 if (!empathy_connectivity_is_online (priv->connectivity))
751 gtk_widget_set_sensitive (GTK_WIDGET (chooser), sensitive);
755 presence_chooser_update_sensitivity (EmpathyPresenceChooser *chooser)
757 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
759 tp_account_manager_prepare_async (priv->account_manager, NULL,
760 update_sensitivity_am_prepared_cb,
765 presence_chooser_account_manager_account_validity_changed_cb (
766 TpAccountManager *manager,
769 EmpathyPresenceChooser *chooser)
771 presence_chooser_update_sensitivity (chooser);
775 presence_chooser_account_manager_account_changed_cb (
776 TpAccountManager *manager,
778 EmpathyPresenceChooser *chooser)
780 presence_chooser_update_sensitivity (chooser);
784 presence_chooser_connectivity_state_change (EmpathyConnectivity *connectivity,
786 EmpathyPresenceChooser *chooser)
788 presence_chooser_update_sensitivity (chooser);
792 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
794 EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
795 EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
797 GtkCellRenderer *renderer;
799 chooser->priv = priv;
801 presence_chooser_create_model (chooser);
803 gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (chooser),
805 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
806 combo_row_separator_func,
809 entry = gtk_bin_get_child (GTK_BIN (chooser));
810 gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
811 GTK_ENTRY_ICON_PRIMARY,
814 g_signal_connect_swapped (entry, "icon-release",
815 G_CALLBACK (presence_chooser_entry_icon_release_cb),
817 g_signal_connect_swapped (entry, "activate",
818 G_CALLBACK (presence_chooser_entry_activate_cb),
820 g_signal_connect_swapped (entry, "key-press-event",
821 G_CALLBACK (presence_chooser_entry_key_press_event_cb),
823 g_signal_connect_swapped (entry, "button-press-event",
824 G_CALLBACK (presence_chooser_entry_button_press_event_cb),
827 gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
829 renderer = gtk_cell_renderer_pixbuf_new ();
830 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, FALSE);
831 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
832 "icon-name", COL_STATE_ICON_NAME,
834 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
836 renderer = gtk_cell_renderer_text_new ();
837 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, TRUE);
838 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
839 "markup", COL_DISPLAY_MARKUP,
842 g_signal_connect (chooser, "notify::popup-shown",
843 G_CALLBACK (presence_chooser_popup_shown_cb), NULL);
844 g_signal_connect (chooser, "changed",
845 G_CALLBACK (presence_chooser_changed_cb), NULL);
846 g_signal_connect_swapped (entry, "changed",
847 G_CALLBACK (presence_chooser_entry_changed_cb),
849 g_signal_connect_swapped (entry, "focus-out-event",
850 G_CALLBACK (presence_chooser_entry_focus_out_cb),
853 priv->idle = empathy_idle_dup_singleton ();
854 presence_chooser_presence_changed_cb (chooser);
855 g_signal_connect_swapped (priv->idle, "notify::state",
856 G_CALLBACK (presence_chooser_presence_changed_cb),
859 priv->account_manager = tp_account_manager_dup ();
861 empathy_signal_connect_weak (priv->account_manager, "account-validity-changed",
862 G_CALLBACK (presence_chooser_account_manager_account_validity_changed_cb),
864 empathy_signal_connect_weak (priv->account_manager, "account-removed",
865 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
867 empathy_signal_connect_weak (priv->account_manager, "account-enabled",
868 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
870 empathy_signal_connect_weak (priv->account_manager, "account-disabled",
871 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
874 /* FIXME: this string sucks */
875 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser),
876 _("Set your presence and current status"));
878 priv->connectivity = empathy_connectivity_dup_singleton ();
879 empathy_signal_connect_weak (priv->connectivity,
881 G_CALLBACK (presence_chooser_connectivity_state_change),
884 presence_chooser_update_sensitivity (chooser);
888 presence_chooser_finalize (GObject *object)
890 EmpathyPresenceChooserPriv *priv;
892 priv = GET_PRIV (object);
894 if (priv->flash_timeout_id) {
895 g_source_remove (priv->flash_timeout_id);
898 if (priv->focus_out_idle_source) {
899 g_source_remove (priv->focus_out_idle_source);
902 if (priv->account_manager != NULL)
903 g_object_unref (priv->account_manager);
905 g_signal_handlers_disconnect_by_func (priv->idle,
906 presence_chooser_presence_changed_cb,
908 g_object_unref (priv->idle);
910 g_object_unref (priv->connectivity);
912 G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
916 * empathy_presence_chooser_new:
918 * Creates a new #EmpathyPresenceChooser widget.
920 * Return value: A new #EmpathyPresenceChooser widget
923 empathy_presence_chooser_new (void)
927 chooser = g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER, NULL);
933 presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
935 EmpathyPresenceChooserPriv *priv;
936 TpConnectionPresenceType state;
937 TpConnectionPresenceType flash_state;
941 gboolean valid, match_state = FALSE, match = FALSE;
944 priv = GET_PRIV (chooser);
946 if (priv->editing_status) {
950 priv->state = state = empathy_idle_get_state (priv->idle);
951 status = empathy_idle_get_status (priv->idle);
952 flash_state = empathy_idle_get_flash_state (priv->idle);
954 /* An unset presence here doesn't make any sense. Force it to appear as
956 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
957 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
960 /* look through the model and attempt to find a matching state */
961 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
962 for (valid = gtk_tree_model_get_iter_first (model, &iter);
964 valid = gtk_tree_model_iter_next (model, &iter)) {
966 TpConnectionPresenceType m_state;
969 gtk_tree_model_get (model, &iter,
974 if (m_type == ENTRY_TYPE_CUSTOM ||
975 m_type == ENTRY_TYPE_SEPARATOR ||
976 m_type == ENTRY_TYPE_EDIT_CUSTOM) {
979 else if (!match_state && state == m_state) {
980 /* we are now in the section that can contain our
984 else if (match_state && state != m_state) {
985 /* we have passed the section that can contain our
990 gtk_tree_model_get (model, &iter,
991 COL_STATUS_TEXT, &m_status,
994 match = !tp_strdiff (status, m_status);
1003 priv->block_changed++;
1004 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
1005 presence_chooser_set_favorite_icon (chooser);
1006 priv->block_changed--;
1009 ui_set_custom_state (chooser, state, status);
1012 if (flash_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
1013 presence_chooser_flash_start (chooser, state, flash_state);
1016 presence_chooser_flash_stop (chooser, state);
1019 entry = gtk_bin_get_child (GTK_BIN (chooser));
1020 gtk_editable_set_editable (GTK_EDITABLE (entry),
1021 state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
1025 presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser)
1027 EmpathyPresenceChooserPriv *priv;
1028 TpConnectionPresenceType state;
1029 static gboolean on = FALSE;
1032 priv = GET_PRIV (chooser);
1035 state = priv->flash_state_1;
1038 state = priv->flash_state_2;
1041 entry = gtk_bin_get_child (GTK_BIN (chooser));
1042 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
1043 GTK_ENTRY_ICON_PRIMARY,
1044 empathy_icon_name_for_presence (state));
1052 presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
1053 TpConnectionPresenceType state_1,
1054 TpConnectionPresenceType state_2)
1056 EmpathyPresenceChooserPriv *priv;
1058 g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
1060 priv = GET_PRIV (chooser);
1062 priv->flash_state_1 = state_1;
1063 priv->flash_state_2 = state_2;
1065 if (!priv->flash_timeout_id) {
1066 priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
1067 (GSourceFunc) presence_chooser_flash_timeout_cb,
1073 presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
1074 TpConnectionPresenceType state)
1076 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
1079 if (priv->flash_timeout_id) {
1080 g_source_remove (priv->flash_timeout_id);
1081 priv->flash_timeout_id = 0;
1084 entry = gtk_bin_get_child (GTK_BIN (chooser));
1085 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
1086 GTK_ENTRY_ICON_PRIMARY,
1087 empathy_icon_name_for_presence (state));
1091 * empathy_presence_chooser_create_menu:
1093 * Creates a new #GtkMenu allowing users to change their presence from a menu.
1095 * Return value: a new #GtkMenu for changing presence in a menu.
1098 empathy_presence_chooser_create_menu (void)
1100 const gchar *status;
1106 menu = gtk_menu_new ();
1108 for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
1111 status = empathy_presence_get_default_message (states[i].state);
1112 presence_chooser_menu_add_item (menu,
1116 if (states[i].customisable) {
1117 /* Set custom messages if wanted */
1118 list = empathy_status_presets_get (states[i].state, 5);
1119 for (l = list; l; l = l->next) {
1120 presence_chooser_menu_add_item (menu,
1130 item = gtk_menu_item_new ();
1131 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1132 gtk_widget_show (item);
1134 /* Custom messages */
1135 item = gtk_image_menu_item_new_with_label (_("Custom messages..."));
1136 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
1137 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1138 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1139 gtk_widget_show (image);
1140 gtk_widget_show (item);
1142 g_signal_connect (item,
1144 G_CALLBACK (presence_chooser_custom_activate_cb),
1151 presence_chooser_menu_add_item (GtkWidget *menu,
1153 TpConnectionPresenceType state)
1157 const gchar *icon_name;
1159 item = gtk_image_menu_item_new_with_label (str);
1160 icon_name = empathy_icon_name_for_presence (state);
1162 g_signal_connect (item, "activate",
1163 G_CALLBACK (presence_chooser_noncustom_activate_cb),
1166 image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
1167 gtk_widget_show (image);
1169 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1170 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
1171 gtk_widget_show (item);
1173 g_object_set_data_full (G_OBJECT (item),
1174 "status", g_strdup (str),
1175 (GDestroyNotify) g_free);
1177 g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
1179 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1183 presence_chooser_noncustom_activate_cb (GtkWidget *item,
1186 TpConnectionPresenceType state;
1187 const gchar *status;
1189 status = g_object_get_data (G_OBJECT (item), "status");
1190 state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
1192 presence_chooser_set_state (state, status);
1196 presence_chooser_set_state (TpConnectionPresenceType state,
1197 const gchar *status)
1201 idle = empathy_idle_dup_singleton ();
1202 empathy_idle_set_presence (idle, state, status);
1203 g_object_unref (idle);
1207 presence_chooser_custom_activate_cb (GtkWidget *item,
1212 dialog = empathy_status_preset_dialog_new (NULL);
1213 gtk_dialog_run (GTK_DIALOG (dialog));
1214 gtk_widget_destroy (dialog);