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 // FIXME - what's the correct debug flag?
45 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
46 #include <libempathy/empathy-debug.h>
48 #include "empathy-ui-utils.h"
49 #include "empathy-images.h"
50 #include "empathy-presence-chooser.h"
52 /* Flashing delay for icons (milliseconds). */
53 #define FLASH_TIMEOUT 500
55 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyPresenceChooser)
59 gboolean editing_status;
60 int block_set_editing;
62 guint focus_out_idle_source;
67 McPresence flash_state_1;
68 McPresence flash_state_2;
69 guint flash_timeout_id;
70 } EmpathyPresenceChooserPriv;
74 GtkWidget *checkbutton_save;
75 GtkWidget *comboboxentry_message;
76 GtkWidget *entry_message;
77 GtkWidget *combobox_status;
78 GtkTreeModel *model_status;
79 } CustomMessageDialog;
88 static CustomMessageDialog *message_dialog = NULL;
89 /* States to be listed in the menu.
90 * Each state has a boolean telling if it can have custom message */
91 static guint states[] = {MC_PRESENCE_AVAILABLE, TRUE,
92 MC_PRESENCE_DO_NOT_DISTURB, TRUE,
93 MC_PRESENCE_AWAY, TRUE,
94 MC_PRESENCE_HIDDEN, FALSE,
95 MC_PRESENCE_OFFLINE, FALSE};
97 static void presence_chooser_finalize (GObject *object);
98 static void presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser);
99 static gboolean presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser);
100 static void presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
103 static void presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
105 static void presence_chooser_menu_add_item (GtkWidget *menu,
108 static void presence_chooser_noncustom_activate_cb (GtkWidget *item,
110 static void presence_chooser_set_state (McPresence state,
111 const gchar *status);
112 static void presence_chooser_custom_activate_cb (GtkWidget *item,
114 static void presence_chooser_dialog_show (GtkWindow *parent);
116 G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_COMBO_BOX_ENTRY);
119 empathy_presence_chooser_class_init (EmpathyPresenceChooserClass *klass)
121 GObjectClass *object_class = G_OBJECT_CLASS (klass);
123 object_class->finalize = presence_chooser_finalize;
125 g_type_class_add_private (object_class, sizeof (EmpathyPresenceChooserPriv));
134 COL_STATUS_CUSTOMISABLE,
144 ENTRY_TYPE_SEPARATOR,
145 ENTRY_TYPE_EDIT_CUSTOM,
148 static GtkTreeModel *
151 GtkListStore *store = gtk_list_store_new (N_COLUMNS,
152 G_TYPE_STRING, /* COL_STATE_ICON_NAME */
153 MC_TYPE_PRESENCE, /* COL_STATE */
154 G_TYPE_STRING, /* COL_STATUS_TEXT */
155 G_TYPE_STRING, /* COL_DISPLAY_MARKUP */
156 G_TYPE_BOOLEAN, /* COL_STATUS_CUSTOMISABLE */
157 G_TYPE_INT); /* COL_TYPE */
162 for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
165 const char *status = empathy_presence_get_default_message (states[i]);
166 const char *icon_name = empathy_icon_name_for_presence (states[i]);
168 gtk_list_store_append (store, &iter);
169 gtk_list_store_set (store, &iter,
170 COL_STATE_ICON_NAME, icon_name,
171 COL_STATE, states[i],
172 COL_STATUS_TEXT, status,
173 COL_DISPLAY_MARKUP, status,
174 COL_STATUS_CUSTOMISABLE, states[i+1],
175 COL_TYPE, ENTRY_TYPE_BUILTIN,
179 /* Set custom messages if wanted */
180 list = empathy_status_presets_get (states[i], 5);
181 for (l = list; l; l = l->next) {
182 gtk_list_store_append (store, &iter);
183 gtk_list_store_set (store, &iter,
184 COL_STATE_ICON_NAME, icon_name,
185 COL_STATE, states[i],
186 COL_STATUS_TEXT, l->data,
187 COL_DISPLAY_MARKUP, l->data,
188 COL_STATUS_CUSTOMISABLE, TRUE,
189 COL_TYPE, ENTRY_TYPE_SAVED,
194 gtk_list_store_append (store, &iter);
195 gtk_list_store_set (store, &iter,
196 COL_STATE_ICON_NAME, icon_name,
197 COL_STATE, states[i],
199 COL_DISPLAY_MARKUP, "<i>Custom Message...</i>",
200 COL_STATUS_CUSTOMISABLE, TRUE,
201 COL_TYPE, ENTRY_TYPE_CUSTOM,
207 /* add a separator */
208 gtk_list_store_append (store, &iter);
209 gtk_list_store_set (store, &iter,
210 COL_TYPE, ENTRY_TYPE_SEPARATOR,
213 gtk_list_store_append (store, &iter);
214 gtk_list_store_set (store, &iter,
215 COL_STATE_ICON_NAME, GTK_STOCK_EDIT,
217 COL_DISPLAY_MARKUP, "Edit Custom Messages...",
218 COL_TYPE, ENTRY_TYPE_EDIT_CUSTOM,
221 return GTK_TREE_MODEL (store);
225 presence_chooser_popup_shown_cb (GObject *self,
229 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
232 g_object_get (self, "popup-shown", &shown, NULL);
236 /* see presence_chooser_entry_focus_out_cb() for what this does */
237 if (priv->focus_out_idle_source != 0)
239 g_source_remove (priv->focus_out_idle_source);
240 priv->focus_out_idle_source = 0;
243 GtkTreeModel *model = create_model ();
245 gtk_combo_box_set_model (GTK_COMBO_BOX (self), GTK_TREE_MODEL (model));
247 g_object_unref (model);
251 presence_chooser_set_status_editing (EmpathyPresenceChooser *self,
254 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
255 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
257 if (priv->block_set_editing) return;
261 priv->editing_status = TRUE;
263 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
264 GTK_ENTRY_ICON_SECONDARY,
266 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
267 GTK_ENTRY_ICON_SECONDARY,
269 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
270 GTK_ENTRY_ICON_PRIMARY,
275 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
276 GTK_ENTRY_ICON_SECONDARY,
278 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
279 GTK_ENTRY_ICON_SECONDARY,
281 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
282 GTK_ENTRY_ICON_PRIMARY,
285 /* attempt to get the toplevel for this widget */
286 GtkWidget *window = gtk_widget_get_toplevel (GTK_WIDGET (self));
287 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)
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);
311 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
313 /* update the status with MC */
314 const char *status = gtk_entry_get_text (GTK_ENTRY (entry));
315 DEBUG ("Sending state to MC-> %s (%s)\n",
316 g_enum_get_value (g_type_class_peek (MC_TYPE_PRESENCE),
317 priv->state)->value_name,
319 empathy_idle_set_presence (priv->idle, priv->state, status);
323 ui_set_custom_state (EmpathyPresenceChooser *self,
327 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
328 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
329 const char *icon_name;
331 priv->block_set_editing++;
332 priv->block_changed++;
334 icon_name = empathy_icon_name_for_presence (state);
335 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
336 GTK_ENTRY_ICON_PRIMARY,
338 gtk_entry_set_text (GTK_ENTRY (entry), status);
340 priv->block_changed--;
341 priv->block_set_editing--;
345 presence_chooser_reset_status (EmpathyPresenceChooser *self)
347 /* recover the status that was unset */
348 presence_chooser_presence_changed_cb (self);
352 presence_chooser_entry_icon_release_cb (EmpathyPresenceChooser *self,
353 GtkEntryIconPosition icon_pos,
357 presence_chooser_set_status_editing (self, FALSE);
358 mc_set_custom_state (self);
362 presence_chooser_entry_activate_cb (EmpathyPresenceChooser *self,
365 presence_chooser_set_status_editing (self, FALSE);
366 mc_set_custom_state (self);
370 presence_chooser_entry_key_press_event_cb (EmpathyPresenceChooser *self,
374 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
376 if (priv->editing_status && event->keyval == GDK_Escape)
378 /* the user pressed Escape, undo the editing */
379 presence_chooser_set_status_editing (self, FALSE);
380 presence_chooser_reset_status (self);
384 else if (event->keyval == GDK_Up || event->keyval == GDK_Down)
390 return FALSE; /* send this event elsewhere */
394 presence_chooser_entry_button_press_event_cb (EmpathyPresenceChooser *self,
395 GdkEventButton *event,
398 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
400 if (!priv->editing_status &&
401 event->button == 1 &&
402 !GTK_WIDGET_HAS_FOCUS (entry))
404 gtk_widget_grab_focus (entry);
405 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
414 presence_chooser_entry_changed_cb (EmpathyPresenceChooser *self,
417 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
419 if (priv->block_changed) return;
421 /* the combo is being edited to a custom entry */
422 if (!priv->editing_status)
424 presence_chooser_set_status_editing (self, TRUE);
429 presence_chooser_changed_cb (GtkComboBox *self, gpointer user_data)
431 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
433 if (priv->block_changed) return;
437 McPresence new_state;
438 gboolean customisable = TRUE;
441 GtkTreeModel *model = gtk_combo_box_get_model (self);
442 if (!gtk_combo_box_get_active_iter (self, &iter))
447 gtk_tree_model_get (model, &iter,
448 COL_STATE_ICON_NAME, &icon_name,
449 COL_STATE, &new_state,
450 COL_STATUS_CUSTOMISABLE, &customisable,
454 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
456 /* some types of status aren't editable, set the editability of the
457 * entry appropriately. Unless we're just about to reset it anyway,
458 * in which case, don't fiddle with it */
459 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)
467 presence_chooser_reset_status (EMPATHY_PRESENCE_CHOOSER (self));
469 /* attempt to get the toplevel for this widget */
470 GtkWidget *window = gtk_widget_get_toplevel (GTK_WIDGET (self));
471 if (!GTK_WIDGET_TOPLEVEL (window) || !GTK_IS_WINDOW (window))
476 presence_chooser_dialog_show (GTK_WINDOW (window));
478 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->editing_status)
487 /* if the user is already in editing mode and changes
488 * the status type, preseed the text they've already
490 /* FIXME: make this work */
492 else if (priv->previous_type == ENTRY_TYPE_BUILTIN)
494 /* if their previous entry was a builtin, don't
496 gtk_entry_set_text (GTK_ENTRY (entry), "");
500 /* else preseed the text of their currently entered
502 const char *status = empathy_idle_get_status (priv->idle);
503 gtk_entry_set_text (GTK_ENTRY (entry), status);
507 gtk_widget_grab_focus (entry);
512 /* just in case we were setting a new status when
513 * things were changed */
514 presence_chooser_set_status_editing (
515 EMPATHY_PRESENCE_CHOOSER (self),
518 gtk_tree_model_get (model, &iter,
519 COL_STATUS_TEXT, &status,
522 empathy_idle_set_presence (priv->idle, priv->state, status);
527 if (type != ENTRY_TYPE_EDIT_CUSTOM)
529 priv->previous_type = type;
535 combo_row_separator_func (GtkTreeModel *model,
540 gtk_tree_model_get (model, iter,
544 return (type == ENTRY_TYPE_SEPARATOR);
548 presence_chooser_entry_focus_out_idle_cb (gpointer user_data)
550 DEBUG ("Autocommiting status message\n");
552 EmpathyPresenceChooser *chooser = EMPATHY_PRESENCE_CHOOSER (user_data);
553 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
555 presence_chooser_entry_activate_cb (chooser, GTK_ENTRY (entry));
561 presence_chooser_entry_focus_out_cb (EmpathyPresenceChooser *chooser,
562 GdkEventFocus *event,
565 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
567 if (priv->editing_status)
569 /* this seems a bit evil and maybe it will be fragile,
570 * someone should think of a better way to do it.
572 * The entry has focused out, but we don't know where the focus
573 * has gone. If it goes to the combo box, we don't want to
574 * do anything. If it's gone anywhere else, we want to commit
577 * Thus we install this idle handler and store its source.
578 * If the source is scheduled when the popup handler runs,
579 * it will remove it, else the callback will commit the result.
581 priv->focus_out_idle_source = g_idle_add (
582 presence_chooser_entry_focus_out_idle_cb,
586 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
592 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
594 EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
595 EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
597 chooser->priv = priv;
599 GtkTreeModel *model = create_model ();
601 gtk_combo_box_set_model (GTK_COMBO_BOX (chooser), GTK_TREE_MODEL (model));
602 gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (chooser),
604 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
605 combo_row_separator_func,
608 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
609 gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
610 GTK_ENTRY_ICON_PRIMARY, FALSE);
611 g_signal_connect_object (entry, "icon-release",
612 G_CALLBACK (presence_chooser_entry_icon_release_cb),
615 g_signal_connect_object (entry, "activate",
616 G_CALLBACK (presence_chooser_entry_activate_cb),
619 g_signal_connect_object (entry, "key-press-event",
620 G_CALLBACK (presence_chooser_entry_key_press_event_cb),
623 g_signal_connect_object (entry, "button-press-event",
624 G_CALLBACK (presence_chooser_entry_button_press_event_cb),
628 GtkCellRenderer *renderer;
629 gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
631 renderer = gtk_cell_renderer_pixbuf_new ();
632 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, FALSE);
633 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
634 "icon-name", COL_STATE_ICON_NAME,
636 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
638 renderer = gtk_cell_renderer_text_new ();
639 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, TRUE);
640 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
641 "markup", COL_DISPLAY_MARKUP,
644 g_object_unref (model);
646 g_signal_connect (chooser, "notify::popup-shown",
647 G_CALLBACK (presence_chooser_popup_shown_cb), NULL);
648 g_signal_connect (chooser, "changed",
649 G_CALLBACK (presence_chooser_changed_cb), NULL);
650 g_signal_connect_swapped (entry, "changed",
651 G_CALLBACK (presence_chooser_entry_changed_cb),
653 g_signal_connect_swapped (entry, "focus-out-event",
654 G_CALLBACK (presence_chooser_entry_focus_out_cb),
657 priv->idle = empathy_idle_dup_singleton ();
658 presence_chooser_presence_changed_cb (chooser);
659 g_signal_connect_swapped (priv->idle, "notify",
660 G_CALLBACK (presence_chooser_presence_changed_cb),
663 g_object_set (chooser,
664 // FIXME: this string sucks
665 "tooltip-text", _("Set your presence and current status"),
670 presence_chooser_finalize (GObject *object)
672 EmpathyPresenceChooserPriv *priv;
674 priv = GET_PRIV (object);
676 if (priv->flash_timeout_id) {
677 g_source_remove (priv->flash_timeout_id);
680 if (priv->focus_out_idle_source) {
681 g_source_remove (priv->focus_out_idle_source);
684 g_signal_handlers_disconnect_by_func (priv->idle,
685 presence_chooser_presence_changed_cb,
687 g_object_unref (priv->idle);
689 G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
693 empathy_presence_chooser_new (void)
697 chooser = g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER, NULL);
703 presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
705 EmpathyPresenceChooserPriv *priv;
707 McPresence flash_state;
710 priv = GET_PRIV (chooser);
712 priv->state = state = empathy_idle_get_state (priv->idle);
713 status = empathy_idle_get_status (priv->idle);
714 flash_state = empathy_idle_get_flash_state (priv->idle);
716 /* look through the model and attempt to find a matching state */
717 GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
719 gboolean valid, match_state = FALSE, match = FALSE;
720 for (valid = gtk_tree_model_get_iter_first (model, &iter);
722 valid = gtk_tree_model_iter_next (model, &iter))
728 gtk_tree_model_get (model, &iter,
733 if (m_type == ENTRY_TYPE_CUSTOM ||
734 m_type == ENTRY_TYPE_SEPARATOR ||
735 m_type == ENTRY_TYPE_EDIT_CUSTOM)
739 else if (!match_state && state == m_state)
741 /* we are now in the section that can contain our
745 else if (match_state && state != m_state)
747 /* we have passed the section that can contain our
752 gtk_tree_model_get (model, &iter,
753 COL_STATUS_TEXT, &m_status,
756 match = !strcmp (status, m_status);
766 priv->block_changed++;
767 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
768 priv->block_changed--;
772 ui_set_custom_state (chooser, state, status);
775 if (flash_state != MC_PRESENCE_UNSET) {
776 presence_chooser_flash_start (chooser, state, flash_state);
778 presence_chooser_flash_stop (chooser, state);
783 presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser)
785 EmpathyPresenceChooserPriv *priv;
787 static gboolean on = FALSE;
789 priv = GET_PRIV (chooser);
792 state = priv->flash_state_1;
794 state = priv->flash_state_2;
797 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
798 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
799 GTK_ENTRY_ICON_PRIMARY,
800 empathy_icon_name_for_presence (state));
808 presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
812 EmpathyPresenceChooserPriv *priv;
814 g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
816 priv = GET_PRIV (chooser);
818 priv->flash_state_1 = state_1;
819 priv->flash_state_2 = state_2;
821 if (!priv->flash_timeout_id) {
822 priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
823 (GSourceFunc) presence_chooser_flash_timeout_cb,
829 presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
832 EmpathyPresenceChooserPriv *priv;
834 g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
836 priv = GET_PRIV (chooser);
838 if (priv->flash_timeout_id) {
839 g_source_remove (priv->flash_timeout_id);
840 priv->flash_timeout_id = 0;
842 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
844 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
845 GTK_ENTRY_ICON_PRIMARY,
846 empathy_icon_name_for_presence (state));
848 // FIXME - what does this do?
849 // priv->last_state = state;
853 empathy_presence_chooser_create_menu (void)
861 menu = gtk_menu_new ();
863 for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
866 status = empathy_presence_get_default_message (states[i]);
867 presence_chooser_menu_add_item (menu,
872 /* Set custom messages if wanted */
873 list = empathy_status_presets_get (states[i], 5);
874 for (l = list; l; l = l->next) {
875 presence_chooser_menu_add_item (menu,
885 item = gtk_menu_item_new ();
886 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
887 gtk_widget_show (item);
889 /* Custom messages */
890 item = gtk_image_menu_item_new_with_label (_("Custom messages..."));
891 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
892 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
893 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
894 gtk_widget_show (image);
895 gtk_widget_show (item);
897 g_signal_connect (item,
899 G_CALLBACK (presence_chooser_custom_activate_cb),
906 presence_chooser_menu_add_item (GtkWidget *menu,
912 const gchar *icon_name;
914 item = gtk_image_menu_item_new_with_label (str);
915 icon_name = empathy_icon_name_for_presence (state);
917 g_signal_connect (item, "activate",
918 G_CALLBACK (presence_chooser_noncustom_activate_cb),
921 image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
922 gtk_widget_show (image);
924 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
925 gtk_widget_show (item);
927 g_object_set_data_full (G_OBJECT (item),
928 "status", g_strdup (str),
929 (GDestroyNotify) g_free);
931 g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
933 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
937 presence_chooser_noncustom_activate_cb (GtkWidget *item,
943 status = g_object_get_data (G_OBJECT (item), "status");
944 state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
946 presence_chooser_set_state (state, status);
950 presence_chooser_set_state (McPresence state,
955 idle = empathy_idle_dup_singleton ();
956 empathy_idle_set_presence (idle, state, status);
957 g_object_unref (idle);
961 presence_chooser_custom_activate_cb (GtkWidget *item,
964 presence_chooser_dialog_show (NULL);
968 presence_chooser_dialog_get_selected (CustomMessageDialog *dialog)
972 McPresence presence = LAST_MC_PRESENCE;
974 model = gtk_combo_box_get_model (GTK_COMBO_BOX (dialog->combobox_status));
975 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (dialog->combobox_status), &iter)) {
976 gtk_tree_model_get (model, &iter,
977 COL_PRESENCE, &presence,
985 presence_chooser_dialog_status_changed_cb (GtkWidget *widget,
986 CustomMessageDialog *dialog)
990 McPresence presence = LAST_MC_PRESENCE;
993 presence = presence_chooser_dialog_get_selected (dialog);
995 store = gtk_list_store_new (1, G_TYPE_STRING);
996 messages = empathy_status_presets_get (presence, -1);
997 for (l = messages; l; l = l->next) {
998 gtk_list_store_append (store, &iter);
999 gtk_list_store_set (store, &iter, 0, l->data, -1);
1002 gtk_entry_set_text (GTK_ENTRY (dialog->entry_message),
1003 messages ? messages->data : "");
1005 g_list_free (messages);
1007 gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->comboboxentry_message),
1008 GTK_TREE_MODEL (store));
1010 g_object_unref (store);
1014 presence_chooser_dialog_message_changed_cb (GtkWidget *widget,
1015 CustomMessageDialog *dialog)
1017 McPresence presence;
1018 GList *messages, *l;
1020 gboolean found = FALSE;
1022 presence = presence_chooser_dialog_get_selected (dialog);
1023 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1025 messages = empathy_status_presets_get (presence, -1);
1026 for (l = messages; l; l = l->next) {
1027 if (!tp_strdiff (text, l->data)) {
1032 g_list_free (messages);
1034 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_save),
1039 presence_chooser_dialog_save_toggled_cb (GtkWidget *widget,
1040 CustomMessageDialog *dialog)
1046 active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_save));
1047 state = presence_chooser_dialog_get_selected (dialog);
1048 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1051 empathy_status_presets_set_last (state, text);
1053 empathy_status_presets_remove (state, text);
1058 presence_chooser_dialog_setup (CustomMessageDialog *dialog)
1060 GtkListStore *store;
1061 GtkCellRenderer *renderer;
1065 store = gtk_list_store_new (COL_COUNT,
1066 G_TYPE_STRING, /* Icon name */
1067 G_TYPE_STRING, /* Label */
1068 MC_TYPE_PRESENCE); /* Presence */
1069 gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->combobox_status),
1070 GTK_TREE_MODEL (store));
1072 renderer = gtk_cell_renderer_pixbuf_new ();
1073 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (dialog->combobox_status), renderer, FALSE);
1074 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combobox_status), renderer,
1075 "icon-name", COL_ICON,
1077 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
1079 renderer = gtk_cell_renderer_text_new ();
1080 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (dialog->combobox_status), renderer, TRUE);
1081 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combobox_status), renderer,
1085 for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
1090 gtk_list_store_append (store, &iter);
1091 gtk_list_store_set (store, &iter,
1092 COL_ICON, empathy_icon_name_for_presence (states[i]),
1093 COL_LABEL, empathy_presence_get_default_message (states[i]),
1094 COL_PRESENCE, states[i],
1098 gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combobox_status), 0);
1102 presence_chooser_dialog_response_cb (GtkWidget *widget,
1104 CustomMessageDialog *dialog)
1106 if (response == GTK_RESPONSE_APPLY) {
1110 state = presence_chooser_dialog_get_selected (dialog);
1111 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1113 presence_chooser_set_state (state, text);
1116 gtk_widget_destroy (widget);
1120 presence_chooser_dialog_destroy_cb (GtkWidget *widget,
1121 CustomMessageDialog *dialog)
1125 message_dialog = NULL;
1129 presence_chooser_dialog_show (GtkWindow *parent)
1134 if (message_dialog) {
1135 gtk_window_present (GTK_WINDOW (message_dialog->dialog));
1139 message_dialog = g_new0 (CustomMessageDialog, 1);
1141 filename = empathy_file_lookup ("empathy-presence-chooser.glade",
1143 glade = empathy_glade_get_file (filename,
1144 "custom_message_dialog",
1146 "custom_message_dialog", &message_dialog->dialog,
1147 "checkbutton_save", &message_dialog->checkbutton_save,
1148 "comboboxentry_message", &message_dialog->comboboxentry_message,
1149 "combobox_status", &message_dialog->combobox_status,
1153 empathy_glade_connect (glade,
1155 "custom_message_dialog", "destroy", presence_chooser_dialog_destroy_cb,
1156 "custom_message_dialog", "response", presence_chooser_dialog_response_cb,
1157 "combobox_status", "changed", presence_chooser_dialog_status_changed_cb,
1158 "checkbutton_save", "toggled", presence_chooser_dialog_save_toggled_cb,
1161 g_object_unref (glade);
1163 /* Setup the message combobox */
1164 message_dialog->entry_message = GTK_BIN (message_dialog->comboboxentry_message)->child;
1165 gtk_entry_set_activates_default (GTK_ENTRY (message_dialog->entry_message), TRUE);
1166 gtk_entry_set_width_chars (GTK_ENTRY (message_dialog->entry_message), 25);
1167 g_signal_connect (message_dialog->entry_message, "changed",
1168 G_CALLBACK (presence_chooser_dialog_message_changed_cb),
1171 presence_chooser_dialog_setup (message_dialog);
1173 gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (message_dialog->comboboxentry_message), 0);
1177 gtk_window_set_transient_for (
1178 GTK_WINDOW (message_dialog->dialog),
1182 gtk_widget_show_all (message_dialog->dialog);