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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, 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 <glade/glade.h>
35 #include <gdk/gdkkeysyms.h>
37 #include <telepathy-glib/util.h>
38 #include <libmissioncontrol/mc-enum-types.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"
51 /* Flashing delay for icons (milliseconds). */
52 #define FLASH_TIMEOUT 500
54 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyPresenceChooser)
56 /* For custom message dialog */
64 /* For combobox's model */
70 COL_STATUS_CUSTOMISABLE,
80 ENTRY_TYPE_EDIT_CUSTOM,
81 } PresenceChooserEntryType;
86 gboolean editing_status;
87 int block_set_editing;
89 guint focus_out_idle_source;
92 PresenceChooserEntryType previous_type;
94 McPresence flash_state_1;
95 McPresence flash_state_2;
96 guint flash_timeout_id;
97 } EmpathyPresenceChooserPriv;
101 GtkWidget *checkbutton_save;
102 GtkWidget *comboboxentry_message;
103 GtkWidget *entry_message;
104 GtkWidget *combobox_status;
105 GtkTreeModel *model_status;
106 } CustomMessageDialog;
108 static CustomMessageDialog *message_dialog = NULL;
109 /* States to be listed in the menu.
110 * Each state has a boolean telling if it can have custom message */
111 static guint states[] = {MC_PRESENCE_AVAILABLE, TRUE,
112 MC_PRESENCE_DO_NOT_DISTURB, TRUE,
113 MC_PRESENCE_AWAY, TRUE,
114 MC_PRESENCE_HIDDEN, FALSE,
115 MC_PRESENCE_OFFLINE, FALSE};
117 static void presence_chooser_finalize (GObject *object);
118 static void presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser);
119 static gboolean presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser);
120 static void presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
123 static void presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
125 static void presence_chooser_menu_add_item (GtkWidget *menu,
128 static void presence_chooser_noncustom_activate_cb (GtkWidget *item,
130 static void presence_chooser_set_state (McPresence state,
131 const gchar *status);
132 static void presence_chooser_custom_activate_cb (GtkWidget *item,
134 static void presence_chooser_dialog_show (GtkWindow *parent);
136 G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_COMBO_BOX_ENTRY);
139 empathy_presence_chooser_class_init (EmpathyPresenceChooserClass *klass)
141 GObjectClass *object_class = G_OBJECT_CLASS (klass);
143 object_class->finalize = presence_chooser_finalize;
145 g_type_class_add_private (object_class, sizeof (EmpathyPresenceChooserPriv));
149 presence_chooser_create_model (EmpathyPresenceChooser *self)
152 char *custom_message;
155 store = gtk_list_store_new (N_COLUMNS,
156 G_TYPE_STRING, /* COL_STATE_ICON_NAME */
157 MC_TYPE_PRESENCE, /* COL_STATE */
158 G_TYPE_STRING, /* COL_STATUS_TEXT */
159 G_TYPE_STRING, /* COL_DISPLAY_MARKUP */
160 G_TYPE_BOOLEAN, /* COL_STATUS_CUSTOMISABLE */
161 G_TYPE_INT); /* COL_TYPE */
163 custom_message = g_strdup_printf ("<i>%s</i>", _("Custom Message..."));
165 for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
167 const char *status, *icon_name;
169 status = empathy_presence_get_default_message (states[i]);
170 icon_name = empathy_icon_name_for_presence (states[i]);
172 gtk_list_store_insert_with_values (store, NULL, -1,
173 COL_STATE_ICON_NAME, icon_name,
174 COL_STATE, states[i],
175 COL_STATUS_TEXT, status,
176 COL_DISPLAY_MARKUP, status,
177 COL_STATUS_CUSTOMISABLE, states[i+1],
178 COL_TYPE, ENTRY_TYPE_BUILTIN,
183 /* Set custom messages if wanted */
184 list = empathy_status_presets_get (states[i], 5);
185 for (l = list; l; l = l->next) {
186 gtk_list_store_insert_with_values (store,
188 COL_STATE_ICON_NAME, icon_name,
189 COL_STATE, states[i],
190 COL_STATUS_TEXT, l->data,
191 COL_DISPLAY_MARKUP, l->data,
192 COL_STATUS_CUSTOMISABLE, TRUE,
193 COL_TYPE, ENTRY_TYPE_SAVED,
198 gtk_list_store_insert_with_values (store, NULL, -1,
199 COL_STATE_ICON_NAME, icon_name,
200 COL_STATE, states[i],
202 COL_DISPLAY_MARKUP, custom_message,
203 COL_STATUS_CUSTOMISABLE, TRUE,
204 COL_TYPE, ENTRY_TYPE_CUSTOM,
210 /* add a separator */
211 gtk_list_store_insert_with_values (store, NULL, -1,
212 COL_TYPE, ENTRY_TYPE_SEPARATOR,
215 gtk_list_store_insert_with_values (store, NULL, -1,
216 COL_STATE_ICON_NAME, GTK_STOCK_EDIT,
218 COL_DISPLAY_MARKUP, _("Edit Custom Messages..."),
219 COL_TYPE, ENTRY_TYPE_EDIT_CUSTOM,
222 g_free (custom_message);
224 gtk_combo_box_set_model (GTK_COMBO_BOX (self), GTK_TREE_MODEL (store));
225 g_object_unref (store);
229 presence_chooser_popup_shown_cb (GObject *self,
233 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
236 g_object_get (self, "popup-shown", &shown, NULL);
241 /* see presence_chooser_entry_focus_out_cb() for what this does */
242 if (priv->focus_out_idle_source != 0) {
243 g_source_remove (priv->focus_out_idle_source);
244 priv->focus_out_idle_source = 0;
247 presence_chooser_create_model (EMPATHY_PRESENCE_CHOOSER (self));
251 presence_chooser_set_status_editing (EmpathyPresenceChooser *self,
254 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
257 if (priv->block_set_editing) {
261 entry = gtk_bin_get_child (GTK_BIN (self));
263 priv->editing_status = TRUE;
265 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
266 GTK_ENTRY_ICON_SECONDARY,
268 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
269 GTK_ENTRY_ICON_SECONDARY,
271 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
272 GTK_ENTRY_ICON_PRIMARY,
277 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
278 GTK_ENTRY_ICON_SECONDARY,
280 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
281 GTK_ENTRY_ICON_SECONDARY,
283 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
284 GTK_ENTRY_ICON_PRIMARY,
287 /* attempt to get the toplevel for this widget */
288 window = gtk_widget_get_toplevel (GTK_WIDGET (self));
289 if (GTK_WIDGET_TOPLEVEL (window) && GTK_IS_WINDOW (window)) {
290 /* unset the focus */
291 gtk_window_set_focus (GTK_WINDOW (window), NULL);
294 /* see presence_chooser_entry_focus_out_cb()
295 * for what this does */
296 if (priv->focus_out_idle_source != 0) {
297 g_source_remove (priv->focus_out_idle_source);
298 priv->focus_out_idle_source = 0;
301 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
303 priv->editing_status = FALSE;
308 mc_set_custom_state (EmpathyPresenceChooser *self)
310 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
314 entry = gtk_bin_get_child (GTK_BIN (self));
315 /* update the status with MC */
316 status = gtk_entry_get_text (GTK_ENTRY (entry));
318 DEBUG ("Sending state to MC-> %s (%s)\n",
319 g_enum_get_value (g_type_class_peek (MC_TYPE_PRESENCE),
320 priv->state)->value_name,
323 empathy_idle_set_presence (priv->idle, priv->state, status);
327 ui_set_custom_state (EmpathyPresenceChooser *self,
331 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
333 const char *icon_name;
335 entry = gtk_bin_get_child (GTK_BIN (self));
337 priv->block_set_editing++;
338 priv->block_changed++;
340 icon_name = empathy_icon_name_for_presence (state);
341 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
342 GTK_ENTRY_ICON_PRIMARY,
344 gtk_entry_set_text (GTK_ENTRY (entry), status);
346 priv->block_changed--;
347 priv->block_set_editing--;
351 presence_chooser_reset_status (EmpathyPresenceChooser *self)
353 /* recover the status that was unset */
354 presence_chooser_set_status_editing (self, FALSE);
355 presence_chooser_presence_changed_cb (self);
359 presence_chooser_entry_icon_release_cb (EmpathyPresenceChooser *self,
360 GtkEntryIconPosition icon_pos,
364 presence_chooser_set_status_editing (self, FALSE);
365 mc_set_custom_state (self);
369 presence_chooser_entry_activate_cb (EmpathyPresenceChooser *self,
372 presence_chooser_set_status_editing (self, FALSE);
373 mc_set_custom_state (self);
377 presence_chooser_entry_key_press_event_cb (EmpathyPresenceChooser *self,
381 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
383 if (priv->editing_status && event->keyval == GDK_Escape) {
384 /* the user pressed Escape, undo the editing */
385 presence_chooser_reset_status (self);
388 else if (event->keyval == GDK_Up || event->keyval == GDK_Down) {
393 return FALSE; /* send this event elsewhere */
397 presence_chooser_entry_button_press_event_cb (EmpathyPresenceChooser *self,
398 GdkEventButton *event,
401 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
403 if (!priv->editing_status &&
404 event->button == 1 &&
405 !GTK_WIDGET_HAS_FOCUS (entry)) {
406 gtk_widget_grab_focus (entry);
407 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
416 presence_chooser_entry_changed_cb (EmpathyPresenceChooser *self,
419 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
421 if (priv->block_changed){
425 /* the combo is being edited to a custom entry */
426 if (!priv->editing_status) {
427 presence_chooser_set_status_editing (self, TRUE);
432 presence_chooser_changed_cb (GtkComboBox *self, gpointer user_data)
434 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
437 McPresence new_state;
438 gboolean customisable = TRUE;
439 PresenceChooserEntryType type = -1;
443 if (priv->block_changed ||
444 !gtk_combo_box_get_active_iter (self, &iter)) {
448 model = gtk_combo_box_get_model (self);
449 gtk_tree_model_get (model, &iter,
450 COL_STATE_ICON_NAME, &icon_name,
451 COL_STATE, &new_state,
452 COL_STATUS_CUSTOMISABLE, &customisable,
456 entry = gtk_bin_get_child (GTK_BIN (self));
458 /* some types of status aren't editable, set the editability of the
459 * entry appropriately. Unless we're just about to reset it anyway,
460 * in which case, don't fiddle with it */
461 if (type != ENTRY_TYPE_EDIT_CUSTOM) {
462 gtk_editable_set_editable (GTK_EDITABLE (entry), customisable);
463 priv->state = new_state;
466 if (type == ENTRY_TYPE_EDIT_CUSTOM) {
469 presence_chooser_reset_status (EMPATHY_PRESENCE_CHOOSER (self));
471 /* attempt to get the toplevel for this widget */
472 window = gtk_widget_get_toplevel (GTK_WIDGET (self));
473 if (!GTK_WIDGET_TOPLEVEL (window) || !GTK_IS_WINDOW (window)) {
477 presence_chooser_dialog_show (GTK_WINDOW (window));
479 else if (type == ENTRY_TYPE_CUSTOM) {
480 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
481 GTK_ENTRY_ICON_PRIMARY,
484 /* preseed the status */
485 if (priv->previous_type == ENTRY_TYPE_BUILTIN) {
486 /* if their previous entry was a builtin, don't
488 gtk_entry_set_text (GTK_ENTRY (entry), "");
490 /* else preseed the text of their currently entered
494 status = empathy_idle_get_status (priv->idle);
495 gtk_entry_set_text (GTK_ENTRY (entry), status);
499 gtk_widget_grab_focus (entry);
503 /* just in case we were setting a new status when
504 * things were changed */
505 presence_chooser_set_status_editing (
506 EMPATHY_PRESENCE_CHOOSER (self),
509 gtk_tree_model_get (model, &iter,
510 COL_STATUS_TEXT, &status,
513 empathy_idle_set_presence (priv->idle, priv->state, status);
518 if (type != ENTRY_TYPE_EDIT_CUSTOM) {
519 priv->previous_type = type;
525 combo_row_separator_func (GtkTreeModel *model,
529 PresenceChooserEntryType type;
531 gtk_tree_model_get (model, iter,
535 return (type == ENTRY_TYPE_SEPARATOR);
539 presence_chooser_entry_focus_out_idle_cb (gpointer user_data)
541 EmpathyPresenceChooser *chooser;
544 DEBUG ("Autocommiting status message\n");
546 chooser = EMPATHY_PRESENCE_CHOOSER (user_data);
547 entry = gtk_bin_get_child (GTK_BIN (chooser));
549 presence_chooser_entry_activate_cb (chooser, GTK_ENTRY (entry));
555 presence_chooser_entry_focus_out_cb (EmpathyPresenceChooser *chooser,
556 GdkEventFocus *event,
559 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
561 if (priv->editing_status) {
562 /* this seems a bit evil and maybe it will be fragile,
563 * someone should think of a better way to do it.
565 * The entry has focused out, but we don't know where the focus
566 * has gone. If it goes to the combo box, we don't want to
567 * do anything. If it's gone anywhere else, we want to commit
570 * Thus we install this idle handler and store its source.
571 * If the source is scheduled when the popup handler runs,
572 * it will remove it, else the callback will commit the result.
574 priv->focus_out_idle_source = g_idle_add (
575 presence_chooser_entry_focus_out_idle_cb,
579 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
585 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
587 EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
588 EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
590 GtkCellRenderer *renderer;
592 chooser->priv = priv;
594 presence_chooser_create_model (chooser);
596 gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (chooser),
598 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
599 combo_row_separator_func,
602 entry = gtk_bin_get_child (GTK_BIN (chooser));
603 gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
604 GTK_ENTRY_ICON_PRIMARY,
607 g_signal_connect_swapped (entry, "icon-release",
608 G_CALLBACK (presence_chooser_entry_icon_release_cb),
610 g_signal_connect_swapped (entry, "activate",
611 G_CALLBACK (presence_chooser_entry_activate_cb),
613 g_signal_connect_swapped (entry, "key-press-event",
614 G_CALLBACK (presence_chooser_entry_key_press_event_cb),
616 g_signal_connect_swapped (entry, "button-press-event",
617 G_CALLBACK (presence_chooser_entry_button_press_event_cb),
620 gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
622 renderer = gtk_cell_renderer_pixbuf_new ();
623 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, FALSE);
624 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
625 "icon-name", COL_STATE_ICON_NAME,
627 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
629 renderer = gtk_cell_renderer_text_new ();
630 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, TRUE);
631 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
632 "markup", COL_DISPLAY_MARKUP,
635 g_signal_connect (chooser, "notify::popup-shown",
636 G_CALLBACK (presence_chooser_popup_shown_cb), NULL);
637 g_signal_connect (chooser, "changed",
638 G_CALLBACK (presence_chooser_changed_cb), NULL);
639 g_signal_connect_swapped (entry, "changed",
640 G_CALLBACK (presence_chooser_entry_changed_cb),
642 g_signal_connect_swapped (entry, "focus-out-event",
643 G_CALLBACK (presence_chooser_entry_focus_out_cb),
646 priv->idle = empathy_idle_dup_singleton ();
647 presence_chooser_presence_changed_cb (chooser);
648 g_signal_connect_swapped (priv->idle, "notify",
649 G_CALLBACK (presence_chooser_presence_changed_cb),
652 /* FIXME: this string sucks */
653 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser),
654 _("Set your presence and current status"));
658 presence_chooser_finalize (GObject *object)
660 EmpathyPresenceChooserPriv *priv;
662 priv = GET_PRIV (object);
664 if (priv->flash_timeout_id) {
665 g_source_remove (priv->flash_timeout_id);
668 if (priv->focus_out_idle_source) {
669 g_source_remove (priv->focus_out_idle_source);
672 g_signal_handlers_disconnect_by_func (priv->idle,
673 presence_chooser_presence_changed_cb,
675 g_object_unref (priv->idle);
677 G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
681 empathy_presence_chooser_new (void)
685 chooser = g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER, NULL);
691 presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
693 EmpathyPresenceChooserPriv *priv;
695 McPresence flash_state;
699 gboolean valid, match_state = FALSE, match = FALSE;
701 priv = GET_PRIV (chooser);
703 if (priv->editing_status) {
707 priv->state = state = empathy_idle_get_state (priv->idle);
708 status = empathy_idle_get_status (priv->idle);
709 flash_state = empathy_idle_get_flash_state (priv->idle);
711 /* look through the model and attempt to find a matching state */
712 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
713 for (valid = gtk_tree_model_get_iter_first (model, &iter);
715 valid = gtk_tree_model_iter_next (model, &iter)) {
720 gtk_tree_model_get (model, &iter,
725 if (m_type == ENTRY_TYPE_CUSTOM ||
726 m_type == ENTRY_TYPE_SEPARATOR ||
727 m_type == ENTRY_TYPE_EDIT_CUSTOM) {
730 else if (!match_state && state == m_state) {
731 /* we are now in the section that can contain our
735 else if (match_state && state != m_state) {
736 /* we have passed the section that can contain our
741 gtk_tree_model_get (model, &iter,
742 COL_STATUS_TEXT, &m_status,
745 match = !strcmp (status, m_status);
754 priv->block_changed++;
755 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
756 priv->block_changed--;
759 ui_set_custom_state (chooser, state, status);
762 if (flash_state != MC_PRESENCE_UNSET) {
763 presence_chooser_flash_start (chooser, state, flash_state);
766 presence_chooser_flash_stop (chooser, state);
771 presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser)
773 EmpathyPresenceChooserPriv *priv;
775 static gboolean on = FALSE;
778 priv = GET_PRIV (chooser);
781 state = priv->flash_state_1;
784 state = priv->flash_state_2;
787 entry = gtk_bin_get_child (GTK_BIN (chooser));
788 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
789 GTK_ENTRY_ICON_PRIMARY,
790 empathy_icon_name_for_presence (state));
798 presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
802 EmpathyPresenceChooserPriv *priv;
804 g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
806 priv = GET_PRIV (chooser);
808 priv->flash_state_1 = state_1;
809 priv->flash_state_2 = state_2;
811 if (!priv->flash_timeout_id) {
812 priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
813 (GSourceFunc) presence_chooser_flash_timeout_cb,
819 presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
822 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
825 if (priv->flash_timeout_id) {
826 g_source_remove (priv->flash_timeout_id);
827 priv->flash_timeout_id = 0;
830 entry = gtk_bin_get_child (GTK_BIN (chooser));
831 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
832 GTK_ENTRY_ICON_PRIMARY,
833 empathy_icon_name_for_presence (state));
837 empathy_presence_chooser_create_menu (void)
845 menu = gtk_menu_new ();
847 for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
850 status = empathy_presence_get_default_message (states[i]);
851 presence_chooser_menu_add_item (menu,
856 /* Set custom messages if wanted */
857 list = empathy_status_presets_get (states[i], 5);
858 for (l = list; l; l = l->next) {
859 presence_chooser_menu_add_item (menu,
869 item = gtk_menu_item_new ();
870 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
871 gtk_widget_show (item);
873 /* Custom messages */
874 item = gtk_image_menu_item_new_with_label (_("Custom messages..."));
875 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
876 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
877 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
878 gtk_widget_show (image);
879 gtk_widget_show (item);
881 g_signal_connect (item,
883 G_CALLBACK (presence_chooser_custom_activate_cb),
890 presence_chooser_menu_add_item (GtkWidget *menu,
896 const gchar *icon_name;
898 item = gtk_image_menu_item_new_with_label (str);
899 icon_name = empathy_icon_name_for_presence (state);
901 g_signal_connect (item, "activate",
902 G_CALLBACK (presence_chooser_noncustom_activate_cb),
905 image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
906 gtk_widget_show (image);
908 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
909 gtk_widget_show (item);
911 g_object_set_data_full (G_OBJECT (item),
912 "status", g_strdup (str),
913 (GDestroyNotify) g_free);
915 g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
917 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
921 presence_chooser_noncustom_activate_cb (GtkWidget *item,
927 status = g_object_get_data (G_OBJECT (item), "status");
928 state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
930 presence_chooser_set_state (state, status);
934 presence_chooser_set_state (McPresence state,
939 idle = empathy_idle_dup_singleton ();
940 empathy_idle_set_presence (idle, state, status);
941 g_object_unref (idle);
945 presence_chooser_custom_activate_cb (GtkWidget *item,
948 presence_chooser_dialog_show (NULL);
952 presence_chooser_dialog_get_selected (CustomMessageDialog *dialog)
956 McPresence presence = LAST_MC_PRESENCE;
958 model = gtk_combo_box_get_model (GTK_COMBO_BOX (dialog->combobox_status));
959 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (dialog->combobox_status), &iter)) {
960 gtk_tree_model_get (model, &iter,
961 COL_PRESENCE, &presence,
969 presence_chooser_dialog_status_changed_cb (GtkWidget *widget,
970 CustomMessageDialog *dialog)
974 McPresence presence = LAST_MC_PRESENCE;
977 presence = presence_chooser_dialog_get_selected (dialog);
979 store = gtk_list_store_new (1, G_TYPE_STRING);
980 messages = empathy_status_presets_get (presence, -1);
981 for (l = messages; l; l = l->next) {
982 gtk_list_store_append (store, &iter);
983 gtk_list_store_set (store, &iter, 0, l->data, -1);
986 gtk_entry_set_text (GTK_ENTRY (dialog->entry_message),
987 messages ? messages->data : "");
989 g_list_free (messages);
991 gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->comboboxentry_message),
992 GTK_TREE_MODEL (store));
994 g_object_unref (store);
998 presence_chooser_dialog_message_changed_cb (GtkWidget *widget,
999 CustomMessageDialog *dialog)
1001 McPresence presence;
1002 GList *messages, *l;
1004 gboolean found = FALSE;
1006 presence = presence_chooser_dialog_get_selected (dialog);
1007 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1009 messages = empathy_status_presets_get (presence, -1);
1010 for (l = messages; l; l = l->next) {
1011 if (!tp_strdiff (text, l->data)) {
1016 g_list_free (messages);
1018 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_save),
1023 presence_chooser_dialog_save_toggled_cb (GtkWidget *widget,
1024 CustomMessageDialog *dialog)
1030 active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_save));
1031 state = presence_chooser_dialog_get_selected (dialog);
1032 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1035 empathy_status_presets_set_last (state, text);
1037 empathy_status_presets_remove (state, text);
1042 presence_chooser_dialog_setup (CustomMessageDialog *dialog)
1044 GtkListStore *store;
1045 GtkCellRenderer *renderer;
1049 store = gtk_list_store_new (COL_COUNT,
1050 G_TYPE_STRING, /* Icon name */
1051 G_TYPE_STRING, /* Label */
1052 MC_TYPE_PRESENCE); /* Presence */
1053 gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->combobox_status),
1054 GTK_TREE_MODEL (store));
1056 renderer = gtk_cell_renderer_pixbuf_new ();
1057 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (dialog->combobox_status), renderer, FALSE);
1058 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combobox_status), renderer,
1059 "icon-name", COL_ICON,
1061 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
1063 renderer = gtk_cell_renderer_text_new ();
1064 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (dialog->combobox_status), renderer, TRUE);
1065 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combobox_status), renderer,
1069 for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
1074 gtk_list_store_append (store, &iter);
1075 gtk_list_store_set (store, &iter,
1076 COL_ICON, empathy_icon_name_for_presence (states[i]),
1077 COL_LABEL, empathy_presence_get_default_message (states[i]),
1078 COL_PRESENCE, states[i],
1082 gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combobox_status), 0);
1086 presence_chooser_dialog_response_cb (GtkWidget *widget,
1088 CustomMessageDialog *dialog)
1090 if (response == GTK_RESPONSE_APPLY) {
1094 state = presence_chooser_dialog_get_selected (dialog);
1095 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1097 presence_chooser_set_state (state, text);
1100 gtk_widget_destroy (widget);
1104 presence_chooser_dialog_destroy_cb (GtkWidget *widget,
1105 CustomMessageDialog *dialog)
1109 message_dialog = NULL;
1113 presence_chooser_dialog_show (GtkWindow *parent)
1118 if (message_dialog) {
1119 gtk_window_present (GTK_WINDOW (message_dialog->dialog));
1123 message_dialog = g_new0 (CustomMessageDialog, 1);
1125 filename = empathy_file_lookup ("empathy-presence-chooser.glade",
1127 glade = empathy_glade_get_file (filename,
1128 "custom_message_dialog",
1130 "custom_message_dialog", &message_dialog->dialog,
1131 "checkbutton_save", &message_dialog->checkbutton_save,
1132 "comboboxentry_message", &message_dialog->comboboxentry_message,
1133 "combobox_status", &message_dialog->combobox_status,
1137 empathy_glade_connect (glade,
1139 "custom_message_dialog", "destroy", presence_chooser_dialog_destroy_cb,
1140 "custom_message_dialog", "response", presence_chooser_dialog_response_cb,
1141 "combobox_status", "changed", presence_chooser_dialog_status_changed_cb,
1142 "checkbutton_save", "toggled", presence_chooser_dialog_save_toggled_cb,
1145 g_object_unref (glade);
1147 /* Setup the message combobox */
1148 message_dialog->entry_message = GTK_BIN (message_dialog->comboboxentry_message)->child;
1149 gtk_entry_set_activates_default (GTK_ENTRY (message_dialog->entry_message), TRUE);
1150 gtk_entry_set_width_chars (GTK_ENTRY (message_dialog->entry_message), 25);
1151 g_signal_connect (message_dialog->entry_message, "changed",
1152 G_CALLBACK (presence_chooser_dialog_message_changed_cb),
1155 presence_chooser_dialog_setup (message_dialog);
1157 gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (message_dialog->comboboxentry_message), 0);
1160 gtk_window_set_transient_for (
1161 GTK_WINDOW (message_dialog->dialog),
1165 gtk_widget_show_all (message_dialog->dialog);