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 <gdk/gdkkeysyms.h>
36 #include <telepathy-glib/util.h>
37 #include <libmissioncontrol/mc-enum-types.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"
50 /* Flashing delay for icons (milliseconds). */
51 #define FLASH_TIMEOUT 500
53 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyPresenceChooser)
55 /* For custom message dialog */
63 /* For combobox's model */
69 COL_STATUS_CUSTOMISABLE,
79 ENTRY_TYPE_EDIT_CUSTOM,
80 } PresenceChooserEntryType;
85 gboolean editing_status;
86 int block_set_editing;
88 guint focus_out_idle_source;
91 PresenceChooserEntryType previous_type;
93 McPresence flash_state_1;
94 McPresence flash_state_2;
95 guint flash_timeout_id;
96 } EmpathyPresenceChooserPriv;
100 GtkWidget *checkbutton_save;
101 GtkWidget *comboboxentry_message;
102 GtkWidget *entry_message;
103 GtkWidget *combobox_status;
104 GtkTreeModel *model_status;
105 } CustomMessageDialog;
107 static CustomMessageDialog *message_dialog = NULL;
108 /* States to be listed in the menu.
109 * Each state has a boolean telling if it can have custom message */
110 static guint states[] = {MC_PRESENCE_AVAILABLE, TRUE,
111 MC_PRESENCE_DO_NOT_DISTURB, TRUE,
112 MC_PRESENCE_AWAY, TRUE,
113 MC_PRESENCE_HIDDEN, FALSE,
114 MC_PRESENCE_OFFLINE, FALSE};
116 static void presence_chooser_finalize (GObject *object);
117 static void presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser);
118 static gboolean presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser);
119 static void presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
122 static void presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
124 static void presence_chooser_menu_add_item (GtkWidget *menu,
127 static void presence_chooser_noncustom_activate_cb (GtkWidget *item,
129 static void presence_chooser_set_state (McPresence state,
130 const gchar *status);
131 static void presence_chooser_custom_activate_cb (GtkWidget *item,
133 static void presence_chooser_dialog_show (GtkWindow *parent);
135 G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_COMBO_BOX_ENTRY);
138 empathy_presence_chooser_class_init (EmpathyPresenceChooserClass *klass)
140 GObjectClass *object_class = G_OBJECT_CLASS (klass);
142 object_class->finalize = presence_chooser_finalize;
144 g_type_class_add_private (object_class, sizeof (EmpathyPresenceChooserPriv));
148 presence_chooser_create_model (EmpathyPresenceChooser *self)
151 char *custom_message;
154 store = gtk_list_store_new (N_COLUMNS,
155 G_TYPE_STRING, /* COL_STATE_ICON_NAME */
156 MC_TYPE_PRESENCE, /* COL_STATE */
157 G_TYPE_STRING, /* COL_STATUS_TEXT */
158 G_TYPE_STRING, /* COL_DISPLAY_MARKUP */
159 G_TYPE_BOOLEAN, /* COL_STATUS_CUSTOMISABLE */
160 G_TYPE_INT); /* COL_TYPE */
162 custom_message = g_strdup_printf ("<i>%s</i>", _("Custom Message..."));
164 for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
166 const char *status, *icon_name;
168 status = empathy_presence_get_default_message (states[i]);
169 icon_name = empathy_icon_name_for_presence (states[i]);
171 gtk_list_store_insert_with_values (store, NULL, -1,
172 COL_STATE_ICON_NAME, icon_name,
173 COL_STATE, states[i],
174 COL_STATUS_TEXT, status,
175 COL_DISPLAY_MARKUP, status,
176 COL_STATUS_CUSTOMISABLE, states[i+1],
177 COL_TYPE, ENTRY_TYPE_BUILTIN,
182 /* Set custom messages if wanted */
183 list = empathy_status_presets_get (states[i], 5);
184 for (l = list; l; l = l->next) {
185 gtk_list_store_insert_with_values (store,
187 COL_STATE_ICON_NAME, icon_name,
188 COL_STATE, states[i],
189 COL_STATUS_TEXT, l->data,
190 COL_DISPLAY_MARKUP, l->data,
191 COL_STATUS_CUSTOMISABLE, TRUE,
192 COL_TYPE, ENTRY_TYPE_SAVED,
197 gtk_list_store_insert_with_values (store, NULL, -1,
198 COL_STATE_ICON_NAME, icon_name,
199 COL_STATE, states[i],
201 COL_DISPLAY_MARKUP, custom_message,
202 COL_STATUS_CUSTOMISABLE, TRUE,
203 COL_TYPE, ENTRY_TYPE_CUSTOM,
209 /* add a separator */
210 gtk_list_store_insert_with_values (store, NULL, -1,
211 COL_TYPE, ENTRY_TYPE_SEPARATOR,
214 gtk_list_store_insert_with_values (store, NULL, -1,
215 COL_STATE_ICON_NAME, GTK_STOCK_EDIT,
217 COL_DISPLAY_MARKUP, _("Edit Custom Messages..."),
218 COL_TYPE, ENTRY_TYPE_EDIT_CUSTOM,
221 g_free (custom_message);
223 gtk_combo_box_set_model (GTK_COMBO_BOX (self), GTK_TREE_MODEL (store));
224 g_object_unref (store);
228 presence_chooser_popup_shown_cb (GObject *self,
232 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
235 g_object_get (self, "popup-shown", &shown, NULL);
240 /* see presence_chooser_entry_focus_out_cb() for what this does */
241 if (priv->focus_out_idle_source != 0) {
242 g_source_remove (priv->focus_out_idle_source);
243 priv->focus_out_idle_source = 0;
246 presence_chooser_create_model (EMPATHY_PRESENCE_CHOOSER (self));
250 presence_chooser_set_status_editing (EmpathyPresenceChooser *self,
253 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
256 if (priv->block_set_editing) {
260 entry = gtk_bin_get_child (GTK_BIN (self));
262 priv->editing_status = TRUE;
264 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
265 GTK_ENTRY_ICON_SECONDARY,
267 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
268 GTK_ENTRY_ICON_SECONDARY,
270 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
271 GTK_ENTRY_ICON_PRIMARY,
276 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
277 GTK_ENTRY_ICON_SECONDARY,
279 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
280 GTK_ENTRY_ICON_SECONDARY,
282 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
283 GTK_ENTRY_ICON_PRIMARY,
286 /* attempt to get the toplevel for this widget */
287 window = gtk_widget_get_toplevel (GTK_WIDGET (self));
288 if (GTK_WIDGET_TOPLEVEL (window) && GTK_IS_WINDOW (window)) {
289 /* unset the focus */
290 gtk_window_set_focus (GTK_WINDOW (window), NULL);
293 /* see presence_chooser_entry_focus_out_cb()
294 * for what this does */
295 if (priv->focus_out_idle_source != 0) {
296 g_source_remove (priv->focus_out_idle_source);
297 priv->focus_out_idle_source = 0;
300 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
302 priv->editing_status = FALSE;
307 mc_set_custom_state (EmpathyPresenceChooser *self)
309 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
313 entry = gtk_bin_get_child (GTK_BIN (self));
314 /* update the status with MC */
315 status = gtk_entry_get_text (GTK_ENTRY (entry));
317 DEBUG ("Sending state to MC-> %s (%s)\n",
318 g_enum_get_value (g_type_class_peek (MC_TYPE_PRESENCE),
319 priv->state)->value_name,
322 empathy_idle_set_presence (priv->idle, priv->state, status);
326 ui_set_custom_state (EmpathyPresenceChooser *self,
330 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
332 const char *icon_name;
334 entry = gtk_bin_get_child (GTK_BIN (self));
336 priv->block_set_editing++;
337 priv->block_changed++;
339 icon_name = empathy_icon_name_for_presence (state);
340 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
341 GTK_ENTRY_ICON_PRIMARY,
343 gtk_entry_set_text (GTK_ENTRY (entry), status);
345 priv->block_changed--;
346 priv->block_set_editing--;
350 presence_chooser_reset_status (EmpathyPresenceChooser *self)
352 /* recover the status that was unset */
353 presence_chooser_set_status_editing (self, FALSE);
354 presence_chooser_presence_changed_cb (self);
358 presence_chooser_entry_icon_release_cb (EmpathyPresenceChooser *self,
359 GtkEntryIconPosition icon_pos,
363 presence_chooser_set_status_editing (self, FALSE);
364 mc_set_custom_state (self);
368 presence_chooser_entry_activate_cb (EmpathyPresenceChooser *self,
371 presence_chooser_set_status_editing (self, FALSE);
372 mc_set_custom_state (self);
376 presence_chooser_entry_key_press_event_cb (EmpathyPresenceChooser *self,
380 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
382 if (priv->editing_status && event->keyval == GDK_Escape) {
383 /* the user pressed Escape, undo the editing */
384 presence_chooser_reset_status (self);
387 else if (event->keyval == GDK_Up || event->keyval == GDK_Down) {
392 return FALSE; /* send this event elsewhere */
396 presence_chooser_entry_button_press_event_cb (EmpathyPresenceChooser *self,
397 GdkEventButton *event,
400 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
402 if (!priv->editing_status &&
403 event->button == 1 &&
404 !GTK_WIDGET_HAS_FOCUS (entry)) {
405 gtk_widget_grab_focus (entry);
406 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
415 presence_chooser_entry_changed_cb (EmpathyPresenceChooser *self,
418 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
420 if (priv->block_changed){
424 /* the combo is being edited to a custom entry */
425 if (!priv->editing_status) {
426 presence_chooser_set_status_editing (self, TRUE);
431 presence_chooser_changed_cb (GtkComboBox *self, gpointer user_data)
433 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
436 McPresence new_state;
437 gboolean customisable = TRUE;
438 PresenceChooserEntryType type = -1;
442 if (priv->block_changed ||
443 !gtk_combo_box_get_active_iter (self, &iter)) {
447 model = gtk_combo_box_get_model (self);
448 gtk_tree_model_get (model, &iter,
449 COL_STATE_ICON_NAME, &icon_name,
450 COL_STATE, &new_state,
451 COL_STATUS_CUSTOMISABLE, &customisable,
455 entry = gtk_bin_get_child (GTK_BIN (self));
457 /* some types of status aren't editable, set the editability of the
458 * entry appropriately. Unless we're just about to reset it anyway,
459 * in which case, don't fiddle with it */
460 if (type != ENTRY_TYPE_EDIT_CUSTOM) {
461 gtk_editable_set_editable (GTK_EDITABLE (entry), customisable);
462 priv->state = new_state;
465 if (type == ENTRY_TYPE_EDIT_CUSTOM) {
468 presence_chooser_reset_status (EMPATHY_PRESENCE_CHOOSER (self));
470 /* attempt to get the toplevel for this widget */
471 window = gtk_widget_get_toplevel (GTK_WIDGET (self));
472 if (!GTK_WIDGET_TOPLEVEL (window) || !GTK_IS_WINDOW (window)) {
476 presence_chooser_dialog_show (GTK_WINDOW (window));
478 else if (type == ENTRY_TYPE_CUSTOM) {
479 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
480 GTK_ENTRY_ICON_PRIMARY,
483 /* preseed the status */
484 if (priv->previous_type == ENTRY_TYPE_BUILTIN) {
485 /* if their previous entry was a builtin, don't
487 gtk_entry_set_text (GTK_ENTRY (entry), "");
489 /* else preseed the text of their currently entered
493 status = empathy_idle_get_status (priv->idle);
494 gtk_entry_set_text (GTK_ENTRY (entry), status);
498 gtk_widget_grab_focus (entry);
502 /* just in case we were setting a new status when
503 * things were changed */
504 presence_chooser_set_status_editing (
505 EMPATHY_PRESENCE_CHOOSER (self),
508 gtk_tree_model_get (model, &iter,
509 COL_STATUS_TEXT, &status,
512 empathy_idle_set_presence (priv->idle, priv->state, status);
517 if (type != ENTRY_TYPE_EDIT_CUSTOM) {
518 priv->previous_type = type;
524 combo_row_separator_func (GtkTreeModel *model,
528 PresenceChooserEntryType type;
530 gtk_tree_model_get (model, iter,
534 return (type == ENTRY_TYPE_SEPARATOR);
538 presence_chooser_entry_focus_out_idle_cb (gpointer user_data)
540 EmpathyPresenceChooser *chooser;
543 DEBUG ("Autocommiting status message\n");
545 chooser = EMPATHY_PRESENCE_CHOOSER (user_data);
546 entry = gtk_bin_get_child (GTK_BIN (chooser));
548 presence_chooser_entry_activate_cb (chooser, GTK_ENTRY (entry));
554 presence_chooser_entry_focus_out_cb (EmpathyPresenceChooser *chooser,
555 GdkEventFocus *event,
558 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
560 if (priv->editing_status) {
561 /* this seems a bit evil and maybe it will be fragile,
562 * someone should think of a better way to do it.
564 * The entry has focused out, but we don't know where the focus
565 * has gone. If it goes to the combo box, we don't want to
566 * do anything. If it's gone anywhere else, we want to commit
569 * Thus we install this idle handler and store its source.
570 * If the source is scheduled when the popup handler runs,
571 * it will remove it, else the callback will commit the result.
573 priv->focus_out_idle_source = g_idle_add (
574 presence_chooser_entry_focus_out_idle_cb,
578 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
584 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
586 EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
587 EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
589 GtkCellRenderer *renderer;
591 chooser->priv = priv;
593 presence_chooser_create_model (chooser);
595 gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (chooser),
597 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
598 combo_row_separator_func,
601 entry = gtk_bin_get_child (GTK_BIN (chooser));
602 gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
603 GTK_ENTRY_ICON_PRIMARY,
606 g_signal_connect_swapped (entry, "icon-release",
607 G_CALLBACK (presence_chooser_entry_icon_release_cb),
609 g_signal_connect_swapped (entry, "activate",
610 G_CALLBACK (presence_chooser_entry_activate_cb),
612 g_signal_connect_swapped (entry, "key-press-event",
613 G_CALLBACK (presence_chooser_entry_key_press_event_cb),
615 g_signal_connect_swapped (entry, "button-press-event",
616 G_CALLBACK (presence_chooser_entry_button_press_event_cb),
619 gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
621 renderer = gtk_cell_renderer_pixbuf_new ();
622 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, FALSE);
623 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
624 "icon-name", COL_STATE_ICON_NAME,
626 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
628 renderer = gtk_cell_renderer_text_new ();
629 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, TRUE);
630 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
631 "markup", COL_DISPLAY_MARKUP,
634 g_signal_connect (chooser, "notify::popup-shown",
635 G_CALLBACK (presence_chooser_popup_shown_cb), NULL);
636 g_signal_connect (chooser, "changed",
637 G_CALLBACK (presence_chooser_changed_cb), NULL);
638 g_signal_connect_swapped (entry, "changed",
639 G_CALLBACK (presence_chooser_entry_changed_cb),
641 g_signal_connect_swapped (entry, "focus-out-event",
642 G_CALLBACK (presence_chooser_entry_focus_out_cb),
645 priv->idle = empathy_idle_dup_singleton ();
646 presence_chooser_presence_changed_cb (chooser);
647 g_signal_connect_swapped (priv->idle, "notify",
648 G_CALLBACK (presence_chooser_presence_changed_cb),
651 /* FIXME: this string sucks */
652 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser),
653 _("Set your presence and current status"));
657 presence_chooser_finalize (GObject *object)
659 EmpathyPresenceChooserPriv *priv;
661 priv = GET_PRIV (object);
663 if (priv->flash_timeout_id) {
664 g_source_remove (priv->flash_timeout_id);
667 if (priv->focus_out_idle_source) {
668 g_source_remove (priv->focus_out_idle_source);
671 g_signal_handlers_disconnect_by_func (priv->idle,
672 presence_chooser_presence_changed_cb,
674 g_object_unref (priv->idle);
676 G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
680 empathy_presence_chooser_new (void)
684 chooser = g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER, NULL);
690 presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
692 EmpathyPresenceChooserPriv *priv;
694 McPresence flash_state;
698 gboolean valid, match_state = FALSE, match = FALSE;
700 priv = GET_PRIV (chooser);
702 if (priv->editing_status) {
706 priv->state = state = empathy_idle_get_state (priv->idle);
707 status = empathy_idle_get_status (priv->idle);
708 flash_state = empathy_idle_get_flash_state (priv->idle);
710 /* look through the model and attempt to find a matching state */
711 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
712 for (valid = gtk_tree_model_get_iter_first (model, &iter);
714 valid = gtk_tree_model_iter_next (model, &iter)) {
719 gtk_tree_model_get (model, &iter,
724 if (m_type == ENTRY_TYPE_CUSTOM ||
725 m_type == ENTRY_TYPE_SEPARATOR ||
726 m_type == ENTRY_TYPE_EDIT_CUSTOM) {
729 else if (!match_state && state == m_state) {
730 /* we are now in the section that can contain our
734 else if (match_state && state != m_state) {
735 /* we have passed the section that can contain our
740 gtk_tree_model_get (model, &iter,
741 COL_STATUS_TEXT, &m_status,
744 match = !strcmp (status, m_status);
753 priv->block_changed++;
754 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
755 priv->block_changed--;
758 ui_set_custom_state (chooser, state, status);
761 if (flash_state != MC_PRESENCE_UNSET) {
762 presence_chooser_flash_start (chooser, state, flash_state);
765 presence_chooser_flash_stop (chooser, state);
770 presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser)
772 EmpathyPresenceChooserPriv *priv;
774 static gboolean on = FALSE;
777 priv = GET_PRIV (chooser);
780 state = priv->flash_state_1;
783 state = priv->flash_state_2;
786 entry = gtk_bin_get_child (GTK_BIN (chooser));
787 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
788 GTK_ENTRY_ICON_PRIMARY,
789 empathy_icon_name_for_presence (state));
797 presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
801 EmpathyPresenceChooserPriv *priv;
803 g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
805 priv = GET_PRIV (chooser);
807 priv->flash_state_1 = state_1;
808 priv->flash_state_2 = state_2;
810 if (!priv->flash_timeout_id) {
811 priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
812 (GSourceFunc) presence_chooser_flash_timeout_cb,
818 presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
821 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
824 if (priv->flash_timeout_id) {
825 g_source_remove (priv->flash_timeout_id);
826 priv->flash_timeout_id = 0;
829 entry = gtk_bin_get_child (GTK_BIN (chooser));
830 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
831 GTK_ENTRY_ICON_PRIMARY,
832 empathy_icon_name_for_presence (state));
836 empathy_presence_chooser_create_menu (void)
844 menu = gtk_menu_new ();
846 for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
849 status = empathy_presence_get_default_message (states[i]);
850 presence_chooser_menu_add_item (menu,
855 /* Set custom messages if wanted */
856 list = empathy_status_presets_get (states[i], 5);
857 for (l = list; l; l = l->next) {
858 presence_chooser_menu_add_item (menu,
868 item = gtk_menu_item_new ();
869 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
870 gtk_widget_show (item);
872 /* Custom messages */
873 item = gtk_image_menu_item_new_with_label (_("Custom messages..."));
874 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
875 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
876 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
877 gtk_widget_show (image);
878 gtk_widget_show (item);
880 g_signal_connect (item,
882 G_CALLBACK (presence_chooser_custom_activate_cb),
889 presence_chooser_menu_add_item (GtkWidget *menu,
895 const gchar *icon_name;
897 item = gtk_image_menu_item_new_with_label (str);
898 icon_name = empathy_icon_name_for_presence (state);
900 g_signal_connect (item, "activate",
901 G_CALLBACK (presence_chooser_noncustom_activate_cb),
904 image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
905 gtk_widget_show (image);
907 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
908 gtk_widget_show (item);
910 g_object_set_data_full (G_OBJECT (item),
911 "status", g_strdup (str),
912 (GDestroyNotify) g_free);
914 g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
916 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
920 presence_chooser_noncustom_activate_cb (GtkWidget *item,
926 status = g_object_get_data (G_OBJECT (item), "status");
927 state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
929 presence_chooser_set_state (state, status);
933 presence_chooser_set_state (McPresence state,
938 idle = empathy_idle_dup_singleton ();
939 empathy_idle_set_presence (idle, state, status);
940 g_object_unref (idle);
944 presence_chooser_custom_activate_cb (GtkWidget *item,
947 presence_chooser_dialog_show (NULL);
951 presence_chooser_dialog_get_selected (CustomMessageDialog *dialog)
955 McPresence presence = LAST_MC_PRESENCE;
957 model = gtk_combo_box_get_model (GTK_COMBO_BOX (dialog->combobox_status));
958 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (dialog->combobox_status), &iter)) {
959 gtk_tree_model_get (model, &iter,
960 COL_PRESENCE, &presence,
968 presence_chooser_dialog_status_changed_cb (GtkWidget *widget,
969 CustomMessageDialog *dialog)
973 McPresence presence = LAST_MC_PRESENCE;
976 presence = presence_chooser_dialog_get_selected (dialog);
978 store = gtk_list_store_new (1, G_TYPE_STRING);
979 messages = empathy_status_presets_get (presence, -1);
980 for (l = messages; l; l = l->next) {
981 gtk_list_store_append (store, &iter);
982 gtk_list_store_set (store, &iter, 0, l->data, -1);
985 gtk_entry_set_text (GTK_ENTRY (dialog->entry_message),
986 messages ? messages->data : "");
988 g_list_free (messages);
990 gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->comboboxentry_message),
991 GTK_TREE_MODEL (store));
993 g_object_unref (store);
997 presence_chooser_dialog_message_changed_cb (GtkWidget *widget,
998 CustomMessageDialog *dialog)
1000 McPresence presence;
1001 GList *messages, *l;
1003 gboolean found = FALSE;
1005 presence = presence_chooser_dialog_get_selected (dialog);
1006 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1008 messages = empathy_status_presets_get (presence, -1);
1009 for (l = messages; l; l = l->next) {
1010 if (!tp_strdiff (text, l->data)) {
1015 g_list_free (messages);
1017 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_save),
1022 presence_chooser_dialog_save_toggled_cb (GtkWidget *widget,
1023 CustomMessageDialog *dialog)
1029 active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_save));
1030 state = presence_chooser_dialog_get_selected (dialog);
1031 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1034 empathy_status_presets_set_last (state, text);
1036 empathy_status_presets_remove (state, text);
1041 presence_chooser_dialog_setup (CustomMessageDialog *dialog)
1043 GtkListStore *store;
1044 GtkCellRenderer *renderer;
1048 store = gtk_list_store_new (COL_COUNT,
1049 G_TYPE_STRING, /* Icon name */
1050 G_TYPE_STRING, /* Label */
1051 MC_TYPE_PRESENCE); /* Presence */
1052 gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->combobox_status),
1053 GTK_TREE_MODEL (store));
1055 renderer = gtk_cell_renderer_pixbuf_new ();
1056 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (dialog->combobox_status), renderer, FALSE);
1057 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combobox_status), renderer,
1058 "icon-name", COL_ICON,
1060 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
1062 renderer = gtk_cell_renderer_text_new ();
1063 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (dialog->combobox_status), renderer, TRUE);
1064 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combobox_status), renderer,
1068 for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
1073 gtk_list_store_append (store, &iter);
1074 gtk_list_store_set (store, &iter,
1075 COL_ICON, empathy_icon_name_for_presence (states[i]),
1076 COL_LABEL, empathy_presence_get_default_message (states[i]),
1077 COL_PRESENCE, states[i],
1081 gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combobox_status), 0);
1085 presence_chooser_dialog_response_cb (GtkWidget *widget,
1087 CustomMessageDialog *dialog)
1089 if (response == GTK_RESPONSE_APPLY) {
1093 state = presence_chooser_dialog_get_selected (dialog);
1094 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1096 presence_chooser_set_state (state, text);
1099 gtk_widget_destroy (widget);
1103 presence_chooser_dialog_destroy_cb (GtkWidget *widget,
1104 CustomMessageDialog *dialog)
1108 message_dialog = NULL;
1112 presence_chooser_dialog_show (GtkWindow *parent)
1117 if (message_dialog) {
1118 gtk_window_present (GTK_WINDOW (message_dialog->dialog));
1122 message_dialog = g_new0 (CustomMessageDialog, 1);
1124 filename = empathy_file_lookup ("empathy-presence-chooser.ui",
1126 gui = empathy_builder_get_file (filename,
1127 "custom_message_dialog", &message_dialog->dialog,
1128 "checkbutton_save", &message_dialog->checkbutton_save,
1129 "comboboxentry_message", &message_dialog->comboboxentry_message,
1130 "combobox_status", &message_dialog->combobox_status,
1134 empathy_builder_connect (gui, message_dialog,
1135 "custom_message_dialog", "destroy", presence_chooser_dialog_destroy_cb,
1136 "custom_message_dialog", "response", presence_chooser_dialog_response_cb,
1137 "combobox_status", "changed", presence_chooser_dialog_status_changed_cb,
1138 "checkbutton_save", "toggled", presence_chooser_dialog_save_toggled_cb,
1141 g_object_unref (gui);
1143 /* Setup the message combobox */
1144 message_dialog->entry_message = GTK_BIN (message_dialog->comboboxentry_message)->child;
1145 gtk_entry_set_activates_default (GTK_ENTRY (message_dialog->entry_message), TRUE);
1146 gtk_entry_set_width_chars (GTK_ENTRY (message_dialog->entry_message), 25);
1147 g_signal_connect (message_dialog->entry_message, "changed",
1148 G_CALLBACK (presence_chooser_dialog_message_changed_cb),
1151 presence_chooser_dialog_setup (message_dialog);
1153 gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (message_dialog->comboboxentry_message), 0);
1156 gtk_window_set_transient_for (
1157 GTK_WINDOW (message_dialog->dialog),
1161 gtk_widget_show_all (message_dialog->dialog);