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/util.h>
38 #include <libempathy/empathy-connectivity.h>
39 #include <libempathy/empathy-idle.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 /* Flashing delay for icons (milliseconds). */
69 #define FLASH_TIMEOUT 500
71 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyPresenceChooser)
73 /* For custom message dialog */
81 /* For combobox's model */
87 COL_STATUS_CUSTOMISABLE,
97 ENTRY_TYPE_EDIT_CUSTOM,
98 } PresenceChooserEntryType;
102 EmpathyConnectivity *connectivity;
104 gulong state_change_signal_id;
106 gboolean editing_status;
107 int block_set_editing;
109 guint focus_out_idle_source;
111 TpConnectionPresenceType state;
112 PresenceChooserEntryType previous_type;
114 TpConnectionPresenceType flash_state_1;
115 TpConnectionPresenceType flash_state_2;
116 guint flash_timeout_id;
117 } EmpathyPresenceChooserPriv;
119 /* States to be listed in the menu.
120 * Each state has a boolean telling if it can have custom message */
121 static struct { TpConnectionPresenceType state;
122 gboolean customisable;
123 } states[] = { { TP_CONNECTION_PRESENCE_TYPE_AVAILABLE, TRUE } ,
124 { TP_CONNECTION_PRESENCE_TYPE_BUSY, TRUE },
125 { TP_CONNECTION_PRESENCE_TYPE_AWAY, TRUE },
126 { TP_CONNECTION_PRESENCE_TYPE_HIDDEN, FALSE },
127 { TP_CONNECTION_PRESENCE_TYPE_OFFLINE, FALSE},
128 { TP_CONNECTION_PRESENCE_TYPE_UNSET, },
131 static void presence_chooser_finalize (GObject *object);
132 static void presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser);
133 static gboolean presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser);
134 static void presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
135 TpConnectionPresenceType state_1,
136 TpConnectionPresenceType state_2);
137 static void presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
138 TpConnectionPresenceType state);
139 static void presence_chooser_menu_add_item (GtkWidget *menu,
141 TpConnectionPresenceType state);
142 static void presence_chooser_noncustom_activate_cb (GtkWidget *item,
144 static void presence_chooser_set_state (TpConnectionPresenceType state,
145 const gchar *status);
146 static void presence_chooser_custom_activate_cb (GtkWidget *item,
149 G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_COMBO_BOX_ENTRY);
152 empathy_presence_chooser_class_init (EmpathyPresenceChooserClass *klass)
154 GObjectClass *object_class = G_OBJECT_CLASS (klass);
156 object_class->finalize = presence_chooser_finalize;
158 g_type_class_add_private (object_class, sizeof (EmpathyPresenceChooserPriv));
162 presence_chooser_create_model (EmpathyPresenceChooser *self)
165 char *custom_message;
168 store = gtk_list_store_new (N_COLUMNS,
169 G_TYPE_STRING, /* COL_STATUS_TEXT */
170 G_TYPE_STRING, /* COL_STATE_ICON_NAME */
171 G_TYPE_UINT, /* COL_STATE */
172 G_TYPE_STRING, /* COL_DISPLAY_MARKUP */
173 G_TYPE_BOOLEAN, /* COL_STATUS_CUSTOMISABLE */
174 G_TYPE_INT); /* COL_TYPE */
176 custom_message = g_strdup_printf ("<i>%s</i>", _("Custom Message..."));
178 for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
180 const char *status, *icon_name;
182 status = empathy_presence_get_default_message (states[i].state);
183 icon_name = empathy_icon_name_for_presence (states[i].state);
185 gtk_list_store_insert_with_values (store, NULL, -1,
186 COL_STATUS_TEXT, status,
187 COL_STATE_ICON_NAME, icon_name,
188 COL_STATE, states[i].state,
189 COL_DISPLAY_MARKUP, status,
190 COL_STATUS_CUSTOMISABLE, states[i].customisable,
191 COL_TYPE, ENTRY_TYPE_BUILTIN,
194 if (states[i].customisable) {
195 /* Set custom messages if wanted */
196 list = empathy_status_presets_get (states[i].state, -1);
197 list = g_list_sort (list, (GCompareFunc) g_utf8_collate);
198 for (l = list; l; l = l->next) {
199 gtk_list_store_insert_with_values (store,
201 COL_STATUS_TEXT, l->data,
202 COL_STATE_ICON_NAME, icon_name,
203 COL_STATE, states[i].state,
204 COL_DISPLAY_MARKUP, l->data,
205 COL_STATUS_CUSTOMISABLE, TRUE,
206 COL_TYPE, ENTRY_TYPE_SAVED,
211 gtk_list_store_insert_with_values (store, NULL, -1,
212 COL_STATUS_TEXT, _("Custom Message..."),
213 COL_STATE_ICON_NAME, icon_name,
214 COL_STATE, states[i].state,
215 COL_DISPLAY_MARKUP, custom_message,
216 COL_STATUS_CUSTOMISABLE, TRUE,
217 COL_TYPE, ENTRY_TYPE_CUSTOM,
223 /* add a separator */
224 gtk_list_store_insert_with_values (store, NULL, -1,
225 COL_TYPE, ENTRY_TYPE_SEPARATOR,
228 gtk_list_store_insert_with_values (store, NULL, -1,
229 COL_STATUS_TEXT, _("Edit Custom Messages..."),
230 COL_STATE_ICON_NAME, GTK_STOCK_EDIT,
231 COL_DISPLAY_MARKUP, _("Edit Custom Messages..."),
232 COL_TYPE, ENTRY_TYPE_EDIT_CUSTOM,
235 g_free (custom_message);
237 gtk_combo_box_set_model (GTK_COMBO_BOX (self), GTK_TREE_MODEL (store));
238 g_object_unref (store);
242 presence_chooser_popup_shown_cb (GObject *self,
246 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
249 g_object_get (self, "popup-shown", &shown, NULL);
254 /* see presence_chooser_entry_focus_out_cb () for what this does */
255 if (priv->focus_out_idle_source != 0) {
256 g_source_remove (priv->focus_out_idle_source);
257 priv->focus_out_idle_source = 0;
260 presence_chooser_create_model (EMPATHY_PRESENCE_CHOOSER (self));
263 static PresenceChooserEntryType
264 presence_chooser_get_entry_type (EmpathyPresenceChooser *self)
267 PresenceChooserEntryType type = -1;
269 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter)) {
270 type = ENTRY_TYPE_CUSTOM;
275 model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
276 gtk_tree_model_get (model, &iter,
285 presence_chooser_is_preset (EmpathyPresenceChooser *self)
287 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
288 TpConnectionPresenceType state;
291 gboolean match = FALSE;
293 state = empathy_idle_get_state (priv->idle);
294 status = empathy_idle_get_status (priv->idle);
296 presets = empathy_status_presets_get (state, -1);
297 for (l = presets; l; l = l->next) {
298 char *preset = (char *) l->data;
300 if (!tp_strdiff (status, preset)) {
306 g_list_free (presets);
308 DEBUG ("is_preset(%i, %s) = %i\n", state, status, match);
314 presence_chooser_set_favorite_icon (EmpathyPresenceChooser *self)
317 PresenceChooserEntryType type;
319 entry = gtk_bin_get_child (GTK_BIN (self));
320 type = presence_chooser_get_entry_type (self);
322 if (type == ENTRY_TYPE_CUSTOM || type == ENTRY_TYPE_SAVED) {
323 if (presence_chooser_is_preset (self)) {
324 /* saved entries can be removed from the list */
325 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
326 GTK_ENTRY_ICON_SECONDARY,
328 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
329 GTK_ENTRY_ICON_SECONDARY,
330 _("Click to remove this status as a favorite"));
333 /* custom entries can be favorited */
334 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
335 GTK_ENTRY_ICON_SECONDARY,
336 "empathy-unstarred");
337 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
338 GTK_ENTRY_ICON_SECONDARY,
339 _("Click to make this status a favorite"));
343 /* built-in entries cannot be favorited */
344 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
345 GTK_ENTRY_ICON_SECONDARY,
347 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
348 GTK_ENTRY_ICON_SECONDARY,
354 presence_chooser_set_status_editing (EmpathyPresenceChooser *self,
357 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
360 if (priv->block_set_editing) {
364 entry = gtk_bin_get_child (GTK_BIN (self));
366 priv->editing_status = TRUE;
368 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
369 GTK_ENTRY_ICON_SECONDARY,
371 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
372 GTK_ENTRY_ICON_SECONDARY,
374 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
375 GTK_ENTRY_ICON_PRIMARY,
380 presence_chooser_set_favorite_icon (self);
381 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
382 GTK_ENTRY_ICON_PRIMARY,
385 /* attempt to get the toplevel for this widget */
386 window = gtk_widget_get_toplevel (GTK_WIDGET (self));
387 if (GTK_WIDGET_TOPLEVEL (window) && GTK_IS_WINDOW (window)) {
388 /* unset the focus */
389 gtk_window_set_focus (GTK_WINDOW (window), NULL);
392 /* see presence_chooser_entry_focus_out_cb ()
393 * for what this does */
394 if (priv->focus_out_idle_source != 0) {
395 g_source_remove (priv->focus_out_idle_source);
396 priv->focus_out_idle_source = 0;
399 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
401 priv->editing_status = FALSE;
406 mc_set_custom_state (EmpathyPresenceChooser *self)
408 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
412 entry = gtk_bin_get_child (GTK_BIN (self));
413 /* update the status with MC */
414 status = gtk_entry_get_text (GTK_ENTRY (entry));
416 DEBUG ("Sending state to MC-> %d (%s)\n", priv->state, status);
418 empathy_idle_set_presence (priv->idle, priv->state, status);
422 ui_set_custom_state (EmpathyPresenceChooser *self,
423 TpConnectionPresenceType state,
426 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
428 const char *icon_name;
430 entry = gtk_bin_get_child (GTK_BIN (self));
432 priv->block_set_editing++;
433 priv->block_changed++;
435 icon_name = empathy_icon_name_for_presence (state);
436 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
437 GTK_ENTRY_ICON_PRIMARY,
439 gtk_entry_set_text (GTK_ENTRY (entry), status == NULL ? "" : status);
440 presence_chooser_set_favorite_icon (self);
442 priv->block_changed--;
443 priv->block_set_editing--;
447 presence_chooser_reset_status (EmpathyPresenceChooser *self)
449 /* recover the status that was unset */
450 presence_chooser_set_status_editing (self, FALSE);
451 presence_chooser_presence_changed_cb (self);
455 presence_chooser_entry_icon_release_cb (EmpathyPresenceChooser *self,
456 GtkEntryIconPosition icon_pos,
460 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
462 if (priv->editing_status) {
463 presence_chooser_set_status_editing (self, FALSE);
464 mc_set_custom_state (self);
467 PresenceChooserEntryType type;
468 TpConnectionPresenceType state;
471 type = presence_chooser_get_entry_type (self);
472 state = empathy_idle_get_state (priv->idle);
473 status = empathy_idle_get_status (priv->idle);
475 if (!empathy_status_presets_is_valid (state))
476 /* It doesn't make sense to add such presence as favorite */
479 if (presence_chooser_is_preset (self)) {
480 /* remove the entry */
481 DEBUG ("REMOVING PRESET (%i, %s)\n", state, status);
482 empathy_status_presets_remove (state, status);
486 DEBUG ("SAVING PRESET (%i, %s)\n", state, status);
487 empathy_status_presets_set_last (state, status);
490 /* update the icon */
491 presence_chooser_set_favorite_icon (self);
496 presence_chooser_entry_activate_cb (EmpathyPresenceChooser *self,
499 presence_chooser_set_status_editing (self, FALSE);
500 mc_set_custom_state (self);
504 presence_chooser_entry_key_press_event_cb (EmpathyPresenceChooser *self,
508 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
510 if (priv->editing_status && event->keyval == GDK_Escape) {
511 /* the user pressed Escape, undo the editing */
512 presence_chooser_reset_status (self);
515 else if (event->keyval == GDK_Up || event->keyval == GDK_Down) {
520 return FALSE; /* send this event elsewhere */
524 presence_chooser_entry_button_press_event_cb (EmpathyPresenceChooser *self,
525 GdkEventButton *event,
528 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
530 if (!priv->editing_status &&
531 event->button == 1 &&
532 !GTK_WIDGET_HAS_FOCUS (entry)) {
533 gtk_widget_grab_focus (entry);
534 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
543 presence_chooser_entry_changed_cb (EmpathyPresenceChooser *self,
546 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
548 if (priv->block_changed){
552 /* the combo is being edited to a custom entry */
553 if (!priv->editing_status) {
554 presence_chooser_set_status_editing (self, TRUE);
559 presence_chooser_changed_cb (GtkComboBox *self, gpointer user_data)
561 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
564 TpConnectionPresenceType new_state;
565 gboolean customisable = TRUE;
566 PresenceChooserEntryType type = -1;
570 if (priv->block_changed ||
571 !gtk_combo_box_get_active_iter (self, &iter)) {
575 model = gtk_combo_box_get_model (self);
576 gtk_tree_model_get (model, &iter,
577 COL_STATE_ICON_NAME, &icon_name,
578 COL_STATE, &new_state,
579 COL_STATUS_CUSTOMISABLE, &customisable,
583 entry = gtk_bin_get_child (GTK_BIN (self));
585 /* some types of status aren't editable, set the editability of the
586 * entry appropriately. Unless we're just about to reset it anyway,
587 * in which case, don't fiddle with it */
588 if (type != ENTRY_TYPE_EDIT_CUSTOM) {
589 gtk_editable_set_editable (GTK_EDITABLE (entry), customisable);
590 priv->state = new_state;
593 if (type == ENTRY_TYPE_EDIT_CUSTOM) {
594 GtkWidget *window, *dialog;
596 presence_chooser_reset_status (EMPATHY_PRESENCE_CHOOSER (self));
598 /* attempt to get the toplevel for this widget */
599 window = gtk_widget_get_toplevel (GTK_WIDGET (self));
600 if (!GTK_WIDGET_TOPLEVEL (window) || !GTK_IS_WINDOW (window)) {
604 dialog = empathy_status_preset_dialog_new (GTK_WINDOW (window));
605 gtk_dialog_run (GTK_DIALOG (dialog));
606 gtk_widget_destroy (dialog);
608 else if (type == ENTRY_TYPE_CUSTOM) {
609 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
610 GTK_ENTRY_ICON_PRIMARY,
613 /* preseed the status */
614 if (priv->previous_type == ENTRY_TYPE_BUILTIN) {
615 /* if their previous entry was a builtin, don't
617 gtk_entry_set_text (GTK_ENTRY (entry), "");
619 /* else preseed the text of their currently entered
623 status = empathy_idle_get_status (priv->idle);
624 gtk_entry_set_text (GTK_ENTRY (entry), status);
628 gtk_widget_grab_focus (entry);
632 /* just in case we were setting a new status when
633 * things were changed */
634 presence_chooser_set_status_editing (
635 EMPATHY_PRESENCE_CHOOSER (self),
637 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
638 GTK_ENTRY_ICON_PRIMARY,
641 gtk_tree_model_get (model, &iter,
642 COL_STATUS_TEXT, &status,
645 empathy_idle_set_presence (priv->idle, priv->state, status);
650 if (type != ENTRY_TYPE_EDIT_CUSTOM) {
651 priv->previous_type = type;
657 combo_row_separator_func (GtkTreeModel *model,
661 PresenceChooserEntryType type;
663 gtk_tree_model_get (model, iter,
667 return (type == ENTRY_TYPE_SEPARATOR);
671 presence_chooser_entry_focus_out_idle_cb (gpointer user_data)
673 EmpathyPresenceChooser *chooser;
676 DEBUG ("Autocommiting status message\n");
678 chooser = EMPATHY_PRESENCE_CHOOSER (user_data);
679 entry = gtk_bin_get_child (GTK_BIN (chooser));
681 presence_chooser_entry_activate_cb (chooser, GTK_ENTRY (entry));
687 presence_chooser_entry_focus_out_cb (EmpathyPresenceChooser *chooser,
688 GdkEventFocus *event,
691 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
693 if (priv->editing_status) {
694 /* this seems a bit evil and maybe it will be fragile,
695 * someone should think of a better way to do it.
697 * The entry has focused out, but we don't know where the focus
698 * has gone. If it goes to the combo box, we don't want to
699 * do anything. If it's gone anywhere else, we want to commit
702 * Thus we install this idle handler and store its source.
703 * If the source is scheduled when the popup handler runs,
704 * it will remove it, else the callback will commit the result.
706 priv->focus_out_idle_source = g_idle_add (
707 presence_chooser_entry_focus_out_idle_cb,
711 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
717 presence_chooser_connectivity_state_change (EmpathyConnectivity *connectivity,
719 EmpathyPresenceChooser *chooser)
721 gtk_widget_set_sensitive (GTK_WIDGET (chooser), new_online);
725 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
727 EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
728 EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
730 GtkCellRenderer *renderer;
732 chooser->priv = priv;
734 presence_chooser_create_model (chooser);
736 gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (chooser),
738 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
739 combo_row_separator_func,
742 entry = gtk_bin_get_child (GTK_BIN (chooser));
743 gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
744 GTK_ENTRY_ICON_PRIMARY,
747 g_signal_connect_swapped (entry, "icon-release",
748 G_CALLBACK (presence_chooser_entry_icon_release_cb),
750 g_signal_connect_swapped (entry, "activate",
751 G_CALLBACK (presence_chooser_entry_activate_cb),
753 g_signal_connect_swapped (entry, "key-press-event",
754 G_CALLBACK (presence_chooser_entry_key_press_event_cb),
756 g_signal_connect_swapped (entry, "button-press-event",
757 G_CALLBACK (presence_chooser_entry_button_press_event_cb),
760 gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
762 renderer = gtk_cell_renderer_pixbuf_new ();
763 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, FALSE);
764 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
765 "icon-name", COL_STATE_ICON_NAME,
767 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
769 renderer = gtk_cell_renderer_text_new ();
770 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, TRUE);
771 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
772 "markup", COL_DISPLAY_MARKUP,
775 g_signal_connect (chooser, "notify::popup-shown",
776 G_CALLBACK (presence_chooser_popup_shown_cb), NULL);
777 g_signal_connect (chooser, "changed",
778 G_CALLBACK (presence_chooser_changed_cb), NULL);
779 g_signal_connect_swapped (entry, "changed",
780 G_CALLBACK (presence_chooser_entry_changed_cb),
782 g_signal_connect_swapped (entry, "focus-out-event",
783 G_CALLBACK (presence_chooser_entry_focus_out_cb),
786 priv->idle = empathy_idle_dup_singleton ();
787 presence_chooser_presence_changed_cb (chooser);
788 g_signal_connect_swapped (priv->idle, "notify",
789 G_CALLBACK (presence_chooser_presence_changed_cb),
792 /* FIXME: this string sucks */
793 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser),
794 _("Set your presence and current status"));
796 priv->connectivity = empathy_connectivity_dup_singleton ();
797 priv->state_change_signal_id = g_signal_connect (priv->connectivity,
799 G_CALLBACK (presence_chooser_connectivity_state_change),
801 presence_chooser_connectivity_state_change (priv->connectivity,
802 empathy_connectivity_is_online (priv->connectivity), chooser);
806 presence_chooser_finalize (GObject *object)
808 EmpathyPresenceChooserPriv *priv;
810 priv = GET_PRIV (object);
812 if (priv->flash_timeout_id) {
813 g_source_remove (priv->flash_timeout_id);
816 if (priv->focus_out_idle_source) {
817 g_source_remove (priv->focus_out_idle_source);
820 g_signal_handlers_disconnect_by_func (priv->idle,
821 presence_chooser_presence_changed_cb,
823 g_object_unref (priv->idle);
825 g_signal_handler_disconnect (priv->connectivity,
826 priv->state_change_signal_id);
827 priv->state_change_signal_id = 0;
829 g_object_unref (priv->connectivity);
831 G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
835 * empathy_presence_chooser_new:
837 * Creates a new #EmpathyPresenceChooser widget.
839 * Return value: A new #EmpathyPresenceChooser widget
842 empathy_presence_chooser_new (void)
846 chooser = g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER, NULL);
852 presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
854 EmpathyPresenceChooserPriv *priv;
855 TpConnectionPresenceType state;
856 TpConnectionPresenceType flash_state;
860 gboolean valid, match_state = FALSE, match = FALSE;
862 priv = GET_PRIV (chooser);
864 if (priv->editing_status) {
868 priv->state = state = empathy_idle_get_state (priv->idle);
869 status = empathy_idle_get_status (priv->idle);
870 flash_state = empathy_idle_get_flash_state (priv->idle);
872 /* look through the model and attempt to find a matching state */
873 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
874 for (valid = gtk_tree_model_get_iter_first (model, &iter);
876 valid = gtk_tree_model_iter_next (model, &iter)) {
878 TpConnectionPresenceType m_state;
881 gtk_tree_model_get (model, &iter,
886 if (m_type == ENTRY_TYPE_CUSTOM ||
887 m_type == ENTRY_TYPE_SEPARATOR ||
888 m_type == ENTRY_TYPE_EDIT_CUSTOM) {
891 else if (!match_state && state == m_state) {
892 /* we are now in the section that can contain our
896 else if (match_state && state != m_state) {
897 /* we have passed the section that can contain our
902 gtk_tree_model_get (model, &iter,
903 COL_STATUS_TEXT, &m_status,
906 match = !tp_strdiff (status, m_status);
915 priv->block_changed++;
916 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
917 presence_chooser_set_favorite_icon (chooser);
918 priv->block_changed--;
921 ui_set_custom_state (chooser, state, status);
924 if (flash_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
925 presence_chooser_flash_start (chooser, state, flash_state);
928 presence_chooser_flash_stop (chooser, state);
933 presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser)
935 EmpathyPresenceChooserPriv *priv;
936 TpConnectionPresenceType state;
937 static gboolean on = FALSE;
940 priv = GET_PRIV (chooser);
943 state = priv->flash_state_1;
946 state = priv->flash_state_2;
949 entry = gtk_bin_get_child (GTK_BIN (chooser));
950 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
951 GTK_ENTRY_ICON_PRIMARY,
952 empathy_icon_name_for_presence (state));
960 presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
961 TpConnectionPresenceType state_1,
962 TpConnectionPresenceType state_2)
964 EmpathyPresenceChooserPriv *priv;
966 g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
968 priv = GET_PRIV (chooser);
970 priv->flash_state_1 = state_1;
971 priv->flash_state_2 = state_2;
973 if (!priv->flash_timeout_id) {
974 priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
975 (GSourceFunc) presence_chooser_flash_timeout_cb,
981 presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
982 TpConnectionPresenceType state)
984 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
987 if (priv->flash_timeout_id) {
988 g_source_remove (priv->flash_timeout_id);
989 priv->flash_timeout_id = 0;
992 entry = gtk_bin_get_child (GTK_BIN (chooser));
993 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
994 GTK_ENTRY_ICON_PRIMARY,
995 empathy_icon_name_for_presence (state));
999 * empathy_presence_chooser_create_menu:
1001 * Creates a new #GtkMenu allowing users to change their presence from a menu.
1003 * Return value: a new #GtkMenu for changing presence in a menu.
1006 empathy_presence_chooser_create_menu (void)
1008 const gchar *status;
1014 menu = gtk_menu_new ();
1016 for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
1019 status = empathy_presence_get_default_message (states[i].state);
1020 presence_chooser_menu_add_item (menu,
1024 if (states[i].customisable) {
1025 /* Set custom messages if wanted */
1026 list = empathy_status_presets_get (states[i].state, 5);
1027 for (l = list; l; l = l->next) {
1028 presence_chooser_menu_add_item (menu,
1038 item = gtk_menu_item_new ();
1039 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1040 gtk_widget_show (item);
1042 /* Custom messages */
1043 item = gtk_image_menu_item_new_with_label (_("Custom messages..."));
1044 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
1045 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1046 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1047 gtk_widget_show (image);
1048 gtk_widget_show (item);
1050 g_signal_connect (item,
1052 G_CALLBACK (presence_chooser_custom_activate_cb),
1059 presence_chooser_menu_add_item (GtkWidget *menu,
1061 TpConnectionPresenceType state)
1065 const gchar *icon_name;
1067 item = gtk_image_menu_item_new_with_label (str);
1068 icon_name = empathy_icon_name_for_presence (state);
1070 g_signal_connect (item, "activate",
1071 G_CALLBACK (presence_chooser_noncustom_activate_cb),
1074 image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
1075 gtk_widget_show (image);
1077 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1078 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
1079 gtk_widget_show (item);
1081 g_object_set_data_full (G_OBJECT (item),
1082 "status", g_strdup (str),
1083 (GDestroyNotify) g_free);
1085 g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
1087 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1091 presence_chooser_noncustom_activate_cb (GtkWidget *item,
1094 TpConnectionPresenceType state;
1095 const gchar *status;
1097 status = g_object_get_data (G_OBJECT (item), "status");
1098 state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
1100 presence_chooser_set_state (state, status);
1104 presence_chooser_set_state (TpConnectionPresenceType state,
1105 const gchar *status)
1109 idle = empathy_idle_dup_singleton ();
1110 empathy_idle_set_presence (idle, state, status);
1111 g_object_unref (idle);
1115 presence_chooser_custom_activate_cb (GtkWidget *item,
1120 dialog = empathy_status_preset_dialog_new (NULL);
1121 gtk_dialog_run (GTK_DIALOG (dialog));
1122 gtk_widget_destroy (dialog);