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);
349 presence_chooser_set_status_editing (self, FALSE);
353 presence_chooser_entry_icon_release_cb (EmpathyPresenceChooser *self,
354 GtkEntryIconPosition icon_pos,
358 presence_chooser_set_status_editing (self, FALSE);
359 mc_set_custom_state (self);
363 presence_chooser_entry_activate_cb (EmpathyPresenceChooser *self,
366 presence_chooser_set_status_editing (self, FALSE);
367 mc_set_custom_state (self);
371 presence_chooser_entry_key_press_event_cb (EmpathyPresenceChooser *self,
375 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
377 if (priv->editing_status && event->keyval == GDK_Escape)
379 /* the user pressed Escape, undo the editing */
380 presence_chooser_set_status_editing (self, FALSE);
381 presence_chooser_reset_status (self);
385 else if (event->keyval == GDK_Up || event->keyval == GDK_Down)
391 return FALSE; /* send this event elsewhere */
395 presence_chooser_entry_button_press_event_cb (EmpathyPresenceChooser *self,
396 GdkEventButton *event,
399 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
401 if (!priv->editing_status &&
402 event->button == 1 &&
403 !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) return;
422 /* the combo is being edited to a custom entry */
423 if (!priv->editing_status)
425 presence_chooser_set_status_editing (self, TRUE);
430 presence_chooser_changed_cb (GtkComboBox *self, gpointer user_data)
432 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
434 if (priv->block_changed) return;
438 McPresence new_state;
439 gboolean customisable = TRUE;
442 GtkTreeModel *model = gtk_combo_box_get_model (self);
443 if (!gtk_combo_box_get_active_iter (self, &iter))
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 GtkWidget *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)
462 gtk_editable_set_editable (GTK_EDITABLE (entry), customisable);
463 priv->state = new_state;
466 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 GtkWidget *window = gtk_widget_get_toplevel (GTK_WIDGET (self));
472 if (!GTK_WIDGET_TOPLEVEL (window) || !GTK_IS_WINDOW (window))
477 presence_chooser_dialog_show (GTK_WINDOW (window));
479 else if (type == ENTRY_TYPE_CUSTOM)
481 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
482 GTK_ENTRY_ICON_PRIMARY,
485 /* preseed the status */
486 if (priv->previous_type == ENTRY_TYPE_BUILTIN)
488 /* if their previous entry was a builtin, don't
490 gtk_entry_set_text (GTK_ENTRY (entry), "");
494 /* else preseed the text of their currently entered
496 const char *status = empathy_idle_get_status (priv->idle);
497 gtk_entry_set_text (GTK_ENTRY (entry), status);
501 gtk_widget_grab_focus (entry);
506 /* just in case we were setting a new status when
507 * things were changed */
508 presence_chooser_set_status_editing (
509 EMPATHY_PRESENCE_CHOOSER (self),
512 gtk_tree_model_get (model, &iter,
513 COL_STATUS_TEXT, &status,
516 empathy_idle_set_presence (priv->idle, priv->state, status);
521 if (type != ENTRY_TYPE_EDIT_CUSTOM)
523 priv->previous_type = type;
529 combo_row_separator_func (GtkTreeModel *model,
534 gtk_tree_model_get (model, iter,
538 return (type == ENTRY_TYPE_SEPARATOR);
542 presence_chooser_entry_focus_out_idle_cb (gpointer user_data)
544 DEBUG ("Autocommiting status message\n");
546 EmpathyPresenceChooser *chooser = EMPATHY_PRESENCE_CHOOSER (user_data);
547 GtkWidget *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)
563 /* this seems a bit evil and maybe it will be fragile,
564 * someone should think of a better way to do it.
566 * The entry has focused out, but we don't know where the focus
567 * has gone. If it goes to the combo box, we don't want to
568 * do anything. If it's gone anywhere else, we want to commit
571 * Thus we install this idle handler and store its source.
572 * If the source is scheduled when the popup handler runs,
573 * it will remove it, else the callback will commit the result.
575 priv->focus_out_idle_source = g_idle_add (
576 presence_chooser_entry_focus_out_idle_cb,
580 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
586 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
588 EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
589 EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
591 chooser->priv = priv;
593 GtkTreeModel *model = create_model ();
595 gtk_combo_box_set_model (GTK_COMBO_BOX (chooser), GTK_TREE_MODEL (model));
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 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
603 gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
604 GTK_ENTRY_ICON_PRIMARY, FALSE);
605 g_signal_connect_object (entry, "icon-release",
606 G_CALLBACK (presence_chooser_entry_icon_release_cb),
609 g_signal_connect_object (entry, "activate",
610 G_CALLBACK (presence_chooser_entry_activate_cb),
613 g_signal_connect_object (entry, "key-press-event",
614 G_CALLBACK (presence_chooser_entry_key_press_event_cb),
617 g_signal_connect_object (entry, "button-press-event",
618 G_CALLBACK (presence_chooser_entry_button_press_event_cb),
622 GtkCellRenderer *renderer;
623 gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
625 renderer = gtk_cell_renderer_pixbuf_new ();
626 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, FALSE);
627 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
628 "icon-name", COL_STATE_ICON_NAME,
630 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
632 renderer = gtk_cell_renderer_text_new ();
633 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, TRUE);
634 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
635 "markup", COL_DISPLAY_MARKUP,
638 g_object_unref (model);
640 g_signal_connect (chooser, "notify::popup-shown",
641 G_CALLBACK (presence_chooser_popup_shown_cb), NULL);
642 g_signal_connect (chooser, "changed",
643 G_CALLBACK (presence_chooser_changed_cb), NULL);
644 g_signal_connect_swapped (entry, "changed",
645 G_CALLBACK (presence_chooser_entry_changed_cb),
647 g_signal_connect_swapped (entry, "focus-out-event",
648 G_CALLBACK (presence_chooser_entry_focus_out_cb),
651 priv->idle = empathy_idle_dup_singleton ();
652 presence_chooser_presence_changed_cb (chooser);
653 g_signal_connect_swapped (priv->idle, "notify",
654 G_CALLBACK (presence_chooser_presence_changed_cb),
657 g_object_set (chooser,
658 // FIXME: this string sucks
659 "tooltip-text", _("Set your presence and current status"),
664 presence_chooser_finalize (GObject *object)
666 EmpathyPresenceChooserPriv *priv;
668 priv = GET_PRIV (object);
670 if (priv->flash_timeout_id) {
671 g_source_remove (priv->flash_timeout_id);
674 if (priv->focus_out_idle_source) {
675 g_source_remove (priv->focus_out_idle_source);
678 g_signal_handlers_disconnect_by_func (priv->idle,
679 presence_chooser_presence_changed_cb,
681 g_object_unref (priv->idle);
683 G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
687 empathy_presence_chooser_new (void)
691 chooser = g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER, NULL);
697 presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
699 EmpathyPresenceChooserPriv *priv;
701 McPresence flash_state;
704 priv = GET_PRIV (chooser);
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 GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
713 gboolean valid, match_state = FALSE, match = FALSE;
714 for (valid = gtk_tree_model_get_iter_first (model, &iter);
716 valid = gtk_tree_model_iter_next (model, &iter))
722 gtk_tree_model_get (model, &iter,
727 if (m_type == ENTRY_TYPE_CUSTOM ||
728 m_type == ENTRY_TYPE_SEPARATOR ||
729 m_type == ENTRY_TYPE_EDIT_CUSTOM)
733 else if (!match_state && state == m_state)
735 /* we are now in the section that can contain our
739 else if (match_state && state != m_state)
741 /* we have passed the section that can contain our
746 gtk_tree_model_get (model, &iter,
747 COL_STATUS_TEXT, &m_status,
750 match = !strcmp (status, m_status);
760 priv->block_changed++;
761 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
762 priv->block_changed--;
766 ui_set_custom_state (chooser, state, status);
769 if (flash_state != MC_PRESENCE_UNSET) {
770 presence_chooser_flash_start (chooser, state, flash_state);
772 presence_chooser_flash_stop (chooser, state);
777 presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser)
779 EmpathyPresenceChooserPriv *priv;
781 static gboolean on = FALSE;
783 priv = GET_PRIV (chooser);
786 state = priv->flash_state_1;
788 state = priv->flash_state_2;
791 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
792 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
793 GTK_ENTRY_ICON_PRIMARY,
794 empathy_icon_name_for_presence (state));
802 presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
806 EmpathyPresenceChooserPriv *priv;
808 g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
810 priv = GET_PRIV (chooser);
812 priv->flash_state_1 = state_1;
813 priv->flash_state_2 = state_2;
815 if (!priv->flash_timeout_id) {
816 priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
817 (GSourceFunc) presence_chooser_flash_timeout_cb,
823 presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
826 EmpathyPresenceChooserPriv *priv;
828 g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
830 priv = GET_PRIV (chooser);
832 if (priv->flash_timeout_id) {
833 g_source_remove (priv->flash_timeout_id);
834 priv->flash_timeout_id = 0;
836 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
838 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
839 GTK_ENTRY_ICON_PRIMARY,
840 empathy_icon_name_for_presence (state));
842 // FIXME - what does this do?
843 // priv->last_state = state;
847 empathy_presence_chooser_create_menu (void)
855 menu = gtk_menu_new ();
857 for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
860 status = empathy_presence_get_default_message (states[i]);
861 presence_chooser_menu_add_item (menu,
866 /* Set custom messages if wanted */
867 list = empathy_status_presets_get (states[i], 5);
868 for (l = list; l; l = l->next) {
869 presence_chooser_menu_add_item (menu,
879 item = gtk_menu_item_new ();
880 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
881 gtk_widget_show (item);
883 /* Custom messages */
884 item = gtk_image_menu_item_new_with_label (_("Custom messages..."));
885 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
886 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
887 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
888 gtk_widget_show (image);
889 gtk_widget_show (item);
891 g_signal_connect (item,
893 G_CALLBACK (presence_chooser_custom_activate_cb),
900 presence_chooser_menu_add_item (GtkWidget *menu,
906 const gchar *icon_name;
908 item = gtk_image_menu_item_new_with_label (str);
909 icon_name = empathy_icon_name_for_presence (state);
911 g_signal_connect (item, "activate",
912 G_CALLBACK (presence_chooser_noncustom_activate_cb),
915 image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
916 gtk_widget_show (image);
918 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
919 gtk_widget_show (item);
921 g_object_set_data_full (G_OBJECT (item),
922 "status", g_strdup (str),
923 (GDestroyNotify) g_free);
925 g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
927 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
931 presence_chooser_noncustom_activate_cb (GtkWidget *item,
937 status = g_object_get_data (G_OBJECT (item), "status");
938 state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
940 presence_chooser_set_state (state, status);
944 presence_chooser_set_state (McPresence state,
949 idle = empathy_idle_dup_singleton ();
950 empathy_idle_set_presence (idle, state, status);
951 g_object_unref (idle);
955 presence_chooser_custom_activate_cb (GtkWidget *item,
958 presence_chooser_dialog_show (NULL);
962 presence_chooser_dialog_get_selected (CustomMessageDialog *dialog)
966 McPresence presence = LAST_MC_PRESENCE;
968 model = gtk_combo_box_get_model (GTK_COMBO_BOX (dialog->combobox_status));
969 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (dialog->combobox_status), &iter)) {
970 gtk_tree_model_get (model, &iter,
971 COL_PRESENCE, &presence,
979 presence_chooser_dialog_status_changed_cb (GtkWidget *widget,
980 CustomMessageDialog *dialog)
984 McPresence presence = LAST_MC_PRESENCE;
987 presence = presence_chooser_dialog_get_selected (dialog);
989 store = gtk_list_store_new (1, G_TYPE_STRING);
990 messages = empathy_status_presets_get (presence, -1);
991 for (l = messages; l; l = l->next) {
992 gtk_list_store_append (store, &iter);
993 gtk_list_store_set (store, &iter, 0, l->data, -1);
996 gtk_entry_set_text (GTK_ENTRY (dialog->entry_message),
997 messages ? messages->data : "");
999 g_list_free (messages);
1001 gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->comboboxentry_message),
1002 GTK_TREE_MODEL (store));
1004 g_object_unref (store);
1008 presence_chooser_dialog_message_changed_cb (GtkWidget *widget,
1009 CustomMessageDialog *dialog)
1011 McPresence presence;
1012 GList *messages, *l;
1014 gboolean found = FALSE;
1016 presence = presence_chooser_dialog_get_selected (dialog);
1017 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1019 messages = empathy_status_presets_get (presence, -1);
1020 for (l = messages; l; l = l->next) {
1021 if (!tp_strdiff (text, l->data)) {
1026 g_list_free (messages);
1028 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_save),
1033 presence_chooser_dialog_save_toggled_cb (GtkWidget *widget,
1034 CustomMessageDialog *dialog)
1040 active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_save));
1041 state = presence_chooser_dialog_get_selected (dialog);
1042 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1045 empathy_status_presets_set_last (state, text);
1047 empathy_status_presets_remove (state, text);
1052 presence_chooser_dialog_setup (CustomMessageDialog *dialog)
1054 GtkListStore *store;
1055 GtkCellRenderer *renderer;
1059 store = gtk_list_store_new (COL_COUNT,
1060 G_TYPE_STRING, /* Icon name */
1061 G_TYPE_STRING, /* Label */
1062 MC_TYPE_PRESENCE); /* Presence */
1063 gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->combobox_status),
1064 GTK_TREE_MODEL (store));
1066 renderer = gtk_cell_renderer_pixbuf_new ();
1067 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (dialog->combobox_status), renderer, FALSE);
1068 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combobox_status), renderer,
1069 "icon-name", COL_ICON,
1071 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
1073 renderer = gtk_cell_renderer_text_new ();
1074 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (dialog->combobox_status), renderer, TRUE);
1075 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combobox_status), renderer,
1079 for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
1084 gtk_list_store_append (store, &iter);
1085 gtk_list_store_set (store, &iter,
1086 COL_ICON, empathy_icon_name_for_presence (states[i]),
1087 COL_LABEL, empathy_presence_get_default_message (states[i]),
1088 COL_PRESENCE, states[i],
1092 gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combobox_status), 0);
1096 presence_chooser_dialog_response_cb (GtkWidget *widget,
1098 CustomMessageDialog *dialog)
1100 if (response == GTK_RESPONSE_APPLY) {
1104 state = presence_chooser_dialog_get_selected (dialog);
1105 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1107 presence_chooser_set_state (state, text);
1110 gtk_widget_destroy (widget);
1114 presence_chooser_dialog_destroy_cb (GtkWidget *widget,
1115 CustomMessageDialog *dialog)
1119 message_dialog = NULL;
1123 presence_chooser_dialog_show (GtkWindow *parent)
1128 if (message_dialog) {
1129 gtk_window_present (GTK_WINDOW (message_dialog->dialog));
1133 message_dialog = g_new0 (CustomMessageDialog, 1);
1135 filename = empathy_file_lookup ("empathy-presence-chooser.glade",
1137 glade = empathy_glade_get_file (filename,
1138 "custom_message_dialog",
1140 "custom_message_dialog", &message_dialog->dialog,
1141 "checkbutton_save", &message_dialog->checkbutton_save,
1142 "comboboxentry_message", &message_dialog->comboboxentry_message,
1143 "combobox_status", &message_dialog->combobox_status,
1147 empathy_glade_connect (glade,
1149 "custom_message_dialog", "destroy", presence_chooser_dialog_destroy_cb,
1150 "custom_message_dialog", "response", presence_chooser_dialog_response_cb,
1151 "combobox_status", "changed", presence_chooser_dialog_status_changed_cb,
1152 "checkbutton_save", "toggled", presence_chooser_dialog_save_toggled_cb,
1155 g_object_unref (glade);
1157 /* Setup the message combobox */
1158 message_dialog->entry_message = GTK_BIN (message_dialog->comboboxentry_message)->child;
1159 gtk_entry_set_activates_default (GTK_ENTRY (message_dialog->entry_message), TRUE);
1160 gtk_entry_set_width_chars (GTK_ENTRY (message_dialog->entry_message), 25);
1161 g_signal_connect (message_dialog->entry_message, "changed",
1162 G_CALLBACK (presence_chooser_dialog_message_changed_cb),
1165 presence_chooser_dialog_setup (message_dialog);
1167 gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (message_dialog->comboboxentry_message), 0);
1171 gtk_window_set_transient_for (
1172 GTK_WINDOW (message_dialog->dialog),
1176 gtk_widget_show_all (message_dialog->dialog);