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>
42 #include <libempathy/empathy-account-manager.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 EmpathyAccountManager *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\n", 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)\n", 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)\n", state, status);
483 empathy_status_presets_remove (state, status);
487 DEBUG ("SAVING PRESET (%i, %s)\n", 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\n");
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 presence_chooser_update_sensitivity (EmpathyPresenceChooser *chooser)
720 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
721 gboolean sensitive = FALSE;
724 accounts = empathy_account_manager_dup_accounts (priv->account_manager);
726 for (l = accounts ; l != NULL ; l = g_list_next (l)) {
727 EmpathyAccount *a = EMPATHY_ACCOUNT (l->data);
729 if (empathy_account_is_enabled (a))
735 g_list_free (accounts);
737 if (!empathy_connectivity_is_online (priv->connectivity))
740 gtk_widget_set_sensitive (GTK_WIDGET (chooser), sensitive);
744 presence_chooser_account_manager_account_changed_cb (
745 EmpathyAccountManager *manager,
746 EmpathyAccount *account,
747 EmpathyPresenceChooser *chooser)
749 presence_chooser_update_sensitivity (chooser);
753 presence_chooser_connectivity_state_change (EmpathyConnectivity *connectivity,
755 EmpathyPresenceChooser *chooser)
757 presence_chooser_update_sensitivity (chooser);
761 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
763 EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
764 EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
766 GtkCellRenderer *renderer;
768 chooser->priv = priv;
770 presence_chooser_create_model (chooser);
772 gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (chooser),
774 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
775 combo_row_separator_func,
778 entry = gtk_bin_get_child (GTK_BIN (chooser));
779 gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
780 GTK_ENTRY_ICON_PRIMARY,
783 g_signal_connect_swapped (entry, "icon-release",
784 G_CALLBACK (presence_chooser_entry_icon_release_cb),
786 g_signal_connect_swapped (entry, "activate",
787 G_CALLBACK (presence_chooser_entry_activate_cb),
789 g_signal_connect_swapped (entry, "key-press-event",
790 G_CALLBACK (presence_chooser_entry_key_press_event_cb),
792 g_signal_connect_swapped (entry, "button-press-event",
793 G_CALLBACK (presence_chooser_entry_button_press_event_cb),
796 gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
798 renderer = gtk_cell_renderer_pixbuf_new ();
799 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, FALSE);
800 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
801 "icon-name", COL_STATE_ICON_NAME,
803 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
805 renderer = gtk_cell_renderer_text_new ();
806 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, TRUE);
807 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
808 "markup", COL_DISPLAY_MARKUP,
811 g_signal_connect (chooser, "notify::popup-shown",
812 G_CALLBACK (presence_chooser_popup_shown_cb), NULL);
813 g_signal_connect (chooser, "changed",
814 G_CALLBACK (presence_chooser_changed_cb), NULL);
815 g_signal_connect_swapped (entry, "changed",
816 G_CALLBACK (presence_chooser_entry_changed_cb),
818 g_signal_connect_swapped (entry, "focus-out-event",
819 G_CALLBACK (presence_chooser_entry_focus_out_cb),
822 priv->idle = empathy_idle_dup_singleton ();
823 presence_chooser_presence_changed_cb (chooser);
824 g_signal_connect_swapped (priv->idle, "notify::state",
825 G_CALLBACK (presence_chooser_presence_changed_cb),
828 priv->account_manager = empathy_account_manager_dup_singleton ();
830 empathy_signal_connect_weak (priv->account_manager, "account-created",
831 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
833 empathy_signal_connect_weak (priv->account_manager, "account-deleted",
834 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
836 empathy_signal_connect_weak (priv->account_manager, "account-enabled",
837 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
839 empathy_signal_connect_weak (priv->account_manager, "account-disabled",
840 G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
843 /* FIXME: this string sucks */
844 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser),
845 _("Set your presence and current status"));
847 priv->connectivity = empathy_connectivity_dup_singleton ();
848 empathy_signal_connect_weak (priv->connectivity,
850 G_CALLBACK (presence_chooser_connectivity_state_change),
853 presence_chooser_update_sensitivity (chooser);
857 presence_chooser_finalize (GObject *object)
859 EmpathyPresenceChooserPriv *priv;
861 priv = GET_PRIV (object);
863 if (priv->flash_timeout_id) {
864 g_source_remove (priv->flash_timeout_id);
867 if (priv->focus_out_idle_source) {
868 g_source_remove (priv->focus_out_idle_source);
871 if (priv->account_manager != NULL)
872 g_object_unref (priv->account_manager);
874 g_signal_handlers_disconnect_by_func (priv->idle,
875 presence_chooser_presence_changed_cb,
877 g_object_unref (priv->idle);
879 g_object_unref (priv->connectivity);
881 G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
885 * empathy_presence_chooser_new:
887 * Creates a new #EmpathyPresenceChooser widget.
889 * Return value: A new #EmpathyPresenceChooser widget
892 empathy_presence_chooser_new (void)
896 chooser = g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER, NULL);
902 presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
904 EmpathyPresenceChooserPriv *priv;
905 TpConnectionPresenceType state;
906 TpConnectionPresenceType flash_state;
910 gboolean valid, match_state = FALSE, match = FALSE;
912 priv = GET_PRIV (chooser);
914 if (priv->editing_status) {
918 priv->state = state = empathy_idle_get_state (priv->idle);
919 status = empathy_idle_get_status (priv->idle);
920 flash_state = empathy_idle_get_flash_state (priv->idle);
922 /* look through the model and attempt to find a matching state */
923 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
924 for (valid = gtk_tree_model_get_iter_first (model, &iter);
926 valid = gtk_tree_model_iter_next (model, &iter)) {
928 TpConnectionPresenceType m_state;
931 gtk_tree_model_get (model, &iter,
936 if (m_type == ENTRY_TYPE_CUSTOM ||
937 m_type == ENTRY_TYPE_SEPARATOR ||
938 m_type == ENTRY_TYPE_EDIT_CUSTOM) {
941 else if (!match_state && state == m_state) {
942 /* we are now in the section that can contain our
946 else if (match_state && state != m_state) {
947 /* we have passed the section that can contain our
952 gtk_tree_model_get (model, &iter,
953 COL_STATUS_TEXT, &m_status,
956 match = !tp_strdiff (status, m_status);
965 priv->block_changed++;
966 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
967 presence_chooser_set_favorite_icon (chooser);
968 priv->block_changed--;
971 ui_set_custom_state (chooser, state, status);
974 if (flash_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
975 presence_chooser_flash_start (chooser, state, flash_state);
978 presence_chooser_flash_stop (chooser, state);
983 presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser)
985 EmpathyPresenceChooserPriv *priv;
986 TpConnectionPresenceType state;
987 static gboolean on = FALSE;
990 priv = GET_PRIV (chooser);
993 state = priv->flash_state_1;
996 state = priv->flash_state_2;
999 entry = gtk_bin_get_child (GTK_BIN (chooser));
1000 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
1001 GTK_ENTRY_ICON_PRIMARY,
1002 empathy_icon_name_for_presence (state));
1010 presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
1011 TpConnectionPresenceType state_1,
1012 TpConnectionPresenceType state_2)
1014 EmpathyPresenceChooserPriv *priv;
1016 g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
1018 priv = GET_PRIV (chooser);
1020 priv->flash_state_1 = state_1;
1021 priv->flash_state_2 = state_2;
1023 if (!priv->flash_timeout_id) {
1024 priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
1025 (GSourceFunc) presence_chooser_flash_timeout_cb,
1031 presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
1032 TpConnectionPresenceType state)
1034 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
1037 if (priv->flash_timeout_id) {
1038 g_source_remove (priv->flash_timeout_id);
1039 priv->flash_timeout_id = 0;
1042 entry = gtk_bin_get_child (GTK_BIN (chooser));
1043 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
1044 GTK_ENTRY_ICON_PRIMARY,
1045 empathy_icon_name_for_presence (state));
1049 * empathy_presence_chooser_create_menu:
1051 * Creates a new #GtkMenu allowing users to change their presence from a menu.
1053 * Return value: a new #GtkMenu for changing presence in a menu.
1056 empathy_presence_chooser_create_menu (void)
1058 const gchar *status;
1064 menu = gtk_menu_new ();
1066 for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
1069 status = empathy_presence_get_default_message (states[i].state);
1070 presence_chooser_menu_add_item (menu,
1074 if (states[i].customisable) {
1075 /* Set custom messages if wanted */
1076 list = empathy_status_presets_get (states[i].state, 5);
1077 for (l = list; l; l = l->next) {
1078 presence_chooser_menu_add_item (menu,
1088 item = gtk_menu_item_new ();
1089 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1090 gtk_widget_show (item);
1092 /* Custom messages */
1093 item = gtk_image_menu_item_new_with_label (_("Custom messages..."));
1094 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
1095 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1096 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1097 gtk_widget_show (image);
1098 gtk_widget_show (item);
1100 g_signal_connect (item,
1102 G_CALLBACK (presence_chooser_custom_activate_cb),
1109 presence_chooser_menu_add_item (GtkWidget *menu,
1111 TpConnectionPresenceType state)
1115 const gchar *icon_name;
1117 item = gtk_image_menu_item_new_with_label (str);
1118 icon_name = empathy_icon_name_for_presence (state);
1120 g_signal_connect (item, "activate",
1121 G_CALLBACK (presence_chooser_noncustom_activate_cb),
1124 image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
1125 gtk_widget_show (image);
1127 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1128 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
1129 gtk_widget_show (item);
1131 g_object_set_data_full (G_OBJECT (item),
1132 "status", g_strdup (str),
1133 (GDestroyNotify) g_free);
1135 g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
1137 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1141 presence_chooser_noncustom_activate_cb (GtkWidget *item,
1144 TpConnectionPresenceType state;
1145 const gchar *status;
1147 status = g_object_get_data (G_OBJECT (item), "status");
1148 state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
1150 presence_chooser_set_state (state, status);
1154 presence_chooser_set_state (TpConnectionPresenceType state,
1155 const gchar *status)
1159 idle = empathy_idle_dup_singleton ();
1160 empathy_idle_set_presence (idle, state, status);
1161 g_object_unref (idle);
1165 presence_chooser_custom_activate_cb (GtkWidget *item,
1170 dialog = empathy_status_preset_dialog_new (NULL);
1171 gtk_dialog_run (GTK_DIALOG (dialog));
1172 gtk_widget_destroy (dialog);