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->previous_type == ENTRY_TYPE_BUILTIN)
487 /* if their previous entry was a builtin, don't
489 gtk_entry_set_text (GTK_ENTRY (entry), "");
493 /* else preseed the text of their currently entered
495 const char *status = empathy_idle_get_status (priv->idle);
496 gtk_entry_set_text (GTK_ENTRY (entry), status);
500 gtk_widget_grab_focus (entry);
505 /* just in case we were setting a new status when
506 * things were changed */
507 presence_chooser_set_status_editing (
508 EMPATHY_PRESENCE_CHOOSER (self),
511 gtk_tree_model_get (model, &iter,
512 COL_STATUS_TEXT, &status,
515 empathy_idle_set_presence (priv->idle, priv->state, status);
520 if (type != ENTRY_TYPE_EDIT_CUSTOM)
522 priv->previous_type = type;
528 combo_row_separator_func (GtkTreeModel *model,
533 gtk_tree_model_get (model, iter,
537 return (type == ENTRY_TYPE_SEPARATOR);
541 presence_chooser_entry_focus_out_idle_cb (gpointer user_data)
543 DEBUG ("Autocommiting status message\n");
545 EmpathyPresenceChooser *chooser = EMPATHY_PRESENCE_CHOOSER (user_data);
546 GtkWidget *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)
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 chooser->priv = priv;
592 GtkTreeModel *model = create_model ();
594 gtk_combo_box_set_model (GTK_COMBO_BOX (chooser), GTK_TREE_MODEL (model));
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 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
602 gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
603 GTK_ENTRY_ICON_PRIMARY, FALSE);
604 g_signal_connect_object (entry, "icon-release",
605 G_CALLBACK (presence_chooser_entry_icon_release_cb),
608 g_signal_connect_object (entry, "activate",
609 G_CALLBACK (presence_chooser_entry_activate_cb),
612 g_signal_connect_object (entry, "key-press-event",
613 G_CALLBACK (presence_chooser_entry_key_press_event_cb),
616 g_signal_connect_object (entry, "button-press-event",
617 G_CALLBACK (presence_chooser_entry_button_press_event_cb),
621 GtkCellRenderer *renderer;
622 gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
624 renderer = gtk_cell_renderer_pixbuf_new ();
625 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, FALSE);
626 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
627 "icon-name", COL_STATE_ICON_NAME,
629 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
631 renderer = gtk_cell_renderer_text_new ();
632 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, TRUE);
633 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
634 "markup", COL_DISPLAY_MARKUP,
637 g_object_unref (model);
639 g_signal_connect (chooser, "notify::popup-shown",
640 G_CALLBACK (presence_chooser_popup_shown_cb), NULL);
641 g_signal_connect (chooser, "changed",
642 G_CALLBACK (presence_chooser_changed_cb), NULL);
643 g_signal_connect_swapped (entry, "changed",
644 G_CALLBACK (presence_chooser_entry_changed_cb),
646 g_signal_connect_swapped (entry, "focus-out-event",
647 G_CALLBACK (presence_chooser_entry_focus_out_cb),
650 priv->idle = empathy_idle_dup_singleton ();
651 presence_chooser_presence_changed_cb (chooser);
652 g_signal_connect_swapped (priv->idle, "notify",
653 G_CALLBACK (presence_chooser_presence_changed_cb),
656 g_object_set (chooser,
657 // FIXME: this string sucks
658 "tooltip-text", _("Set your presence and current status"),
663 presence_chooser_finalize (GObject *object)
665 EmpathyPresenceChooserPriv *priv;
667 priv = GET_PRIV (object);
669 if (priv->flash_timeout_id) {
670 g_source_remove (priv->flash_timeout_id);
673 if (priv->focus_out_idle_source) {
674 g_source_remove (priv->focus_out_idle_source);
677 g_signal_handlers_disconnect_by_func (priv->idle,
678 presence_chooser_presence_changed_cb,
680 g_object_unref (priv->idle);
682 G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
686 empathy_presence_chooser_new (void)
690 chooser = g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER, NULL);
696 presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
698 EmpathyPresenceChooserPriv *priv;
700 McPresence flash_state;
703 priv = GET_PRIV (chooser);
705 priv->state = state = empathy_idle_get_state (priv->idle);
706 status = empathy_idle_get_status (priv->idle);
707 flash_state = empathy_idle_get_flash_state (priv->idle);
709 /* look through the model and attempt to find a matching state */
710 GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
712 gboolean valid, match_state = FALSE, match = FALSE;
713 for (valid = gtk_tree_model_get_iter_first (model, &iter);
715 valid = gtk_tree_model_iter_next (model, &iter))
721 gtk_tree_model_get (model, &iter,
726 if (m_type == ENTRY_TYPE_CUSTOM ||
727 m_type == ENTRY_TYPE_SEPARATOR ||
728 m_type == ENTRY_TYPE_EDIT_CUSTOM)
732 else if (!match_state && state == m_state)
734 /* we are now in the section that can contain our
738 else if (match_state && state != m_state)
740 /* we have passed the section that can contain our
745 gtk_tree_model_get (model, &iter,
746 COL_STATUS_TEXT, &m_status,
749 match = !strcmp (status, m_status);
759 priv->block_changed++;
760 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
761 priv->block_changed--;
765 ui_set_custom_state (chooser, state, status);
768 if (flash_state != MC_PRESENCE_UNSET) {
769 presence_chooser_flash_start (chooser, state, flash_state);
771 presence_chooser_flash_stop (chooser, state);
776 presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser)
778 EmpathyPresenceChooserPriv *priv;
780 static gboolean on = FALSE;
782 priv = GET_PRIV (chooser);
785 state = priv->flash_state_1;
787 state = priv->flash_state_2;
790 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
791 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
792 GTK_ENTRY_ICON_PRIMARY,
793 empathy_icon_name_for_presence (state));
801 presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
805 EmpathyPresenceChooserPriv *priv;
807 g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
809 priv = GET_PRIV (chooser);
811 priv->flash_state_1 = state_1;
812 priv->flash_state_2 = state_2;
814 if (!priv->flash_timeout_id) {
815 priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
816 (GSourceFunc) presence_chooser_flash_timeout_cb,
822 presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
825 EmpathyPresenceChooserPriv *priv;
827 g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
829 priv = GET_PRIV (chooser);
831 if (priv->flash_timeout_id) {
832 g_source_remove (priv->flash_timeout_id);
833 priv->flash_timeout_id = 0;
835 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
837 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
838 GTK_ENTRY_ICON_PRIMARY,
839 empathy_icon_name_for_presence (state));
841 // FIXME - what does this do?
842 // priv->last_state = state;
846 empathy_presence_chooser_create_menu (void)
854 menu = gtk_menu_new ();
856 for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
859 status = empathy_presence_get_default_message (states[i]);
860 presence_chooser_menu_add_item (menu,
865 /* Set custom messages if wanted */
866 list = empathy_status_presets_get (states[i], 5);
867 for (l = list; l; l = l->next) {
868 presence_chooser_menu_add_item (menu,
878 item = gtk_menu_item_new ();
879 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
880 gtk_widget_show (item);
882 /* Custom messages */
883 item = gtk_image_menu_item_new_with_label (_("Custom messages..."));
884 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
885 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
886 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
887 gtk_widget_show (image);
888 gtk_widget_show (item);
890 g_signal_connect (item,
892 G_CALLBACK (presence_chooser_custom_activate_cb),
899 presence_chooser_menu_add_item (GtkWidget *menu,
905 const gchar *icon_name;
907 item = gtk_image_menu_item_new_with_label (str);
908 icon_name = empathy_icon_name_for_presence (state);
910 g_signal_connect (item, "activate",
911 G_CALLBACK (presence_chooser_noncustom_activate_cb),
914 image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
915 gtk_widget_show (image);
917 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
918 gtk_widget_show (item);
920 g_object_set_data_full (G_OBJECT (item),
921 "status", g_strdup (str),
922 (GDestroyNotify) g_free);
924 g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
926 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
930 presence_chooser_noncustom_activate_cb (GtkWidget *item,
936 status = g_object_get_data (G_OBJECT (item), "status");
937 state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
939 presence_chooser_set_state (state, status);
943 presence_chooser_set_state (McPresence state,
948 idle = empathy_idle_dup_singleton ();
949 empathy_idle_set_presence (idle, state, status);
950 g_object_unref (idle);
954 presence_chooser_custom_activate_cb (GtkWidget *item,
957 presence_chooser_dialog_show (NULL);
961 presence_chooser_dialog_get_selected (CustomMessageDialog *dialog)
965 McPresence presence = LAST_MC_PRESENCE;
967 model = gtk_combo_box_get_model (GTK_COMBO_BOX (dialog->combobox_status));
968 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (dialog->combobox_status), &iter)) {
969 gtk_tree_model_get (model, &iter,
970 COL_PRESENCE, &presence,
978 presence_chooser_dialog_status_changed_cb (GtkWidget *widget,
979 CustomMessageDialog *dialog)
983 McPresence presence = LAST_MC_PRESENCE;
986 presence = presence_chooser_dialog_get_selected (dialog);
988 store = gtk_list_store_new (1, G_TYPE_STRING);
989 messages = empathy_status_presets_get (presence, -1);
990 for (l = messages; l; l = l->next) {
991 gtk_list_store_append (store, &iter);
992 gtk_list_store_set (store, &iter, 0, l->data, -1);
995 gtk_entry_set_text (GTK_ENTRY (dialog->entry_message),
996 messages ? messages->data : "");
998 g_list_free (messages);
1000 gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->comboboxentry_message),
1001 GTK_TREE_MODEL (store));
1003 g_object_unref (store);
1007 presence_chooser_dialog_message_changed_cb (GtkWidget *widget,
1008 CustomMessageDialog *dialog)
1010 McPresence presence;
1011 GList *messages, *l;
1013 gboolean found = FALSE;
1015 presence = presence_chooser_dialog_get_selected (dialog);
1016 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1018 messages = empathy_status_presets_get (presence, -1);
1019 for (l = messages; l; l = l->next) {
1020 if (!tp_strdiff (text, l->data)) {
1025 g_list_free (messages);
1027 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_save),
1032 presence_chooser_dialog_save_toggled_cb (GtkWidget *widget,
1033 CustomMessageDialog *dialog)
1039 active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_save));
1040 state = presence_chooser_dialog_get_selected (dialog);
1041 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1044 empathy_status_presets_set_last (state, text);
1046 empathy_status_presets_remove (state, text);
1051 presence_chooser_dialog_setup (CustomMessageDialog *dialog)
1053 GtkListStore *store;
1054 GtkCellRenderer *renderer;
1058 store = gtk_list_store_new (COL_COUNT,
1059 G_TYPE_STRING, /* Icon name */
1060 G_TYPE_STRING, /* Label */
1061 MC_TYPE_PRESENCE); /* Presence */
1062 gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->combobox_status),
1063 GTK_TREE_MODEL (store));
1065 renderer = gtk_cell_renderer_pixbuf_new ();
1066 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (dialog->combobox_status), renderer, FALSE);
1067 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combobox_status), renderer,
1068 "icon-name", COL_ICON,
1070 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
1072 renderer = gtk_cell_renderer_text_new ();
1073 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (dialog->combobox_status), renderer, TRUE);
1074 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combobox_status), renderer,
1078 for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
1083 gtk_list_store_append (store, &iter);
1084 gtk_list_store_set (store, &iter,
1085 COL_ICON, empathy_icon_name_for_presence (states[i]),
1086 COL_LABEL, empathy_presence_get_default_message (states[i]),
1087 COL_PRESENCE, states[i],
1091 gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combobox_status), 0);
1095 presence_chooser_dialog_response_cb (GtkWidget *widget,
1097 CustomMessageDialog *dialog)
1099 if (response == GTK_RESPONSE_APPLY) {
1103 state = presence_chooser_dialog_get_selected (dialog);
1104 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1106 presence_chooser_set_state (state, text);
1109 gtk_widget_destroy (widget);
1113 presence_chooser_dialog_destroy_cb (GtkWidget *widget,
1114 CustomMessageDialog *dialog)
1118 message_dialog = NULL;
1122 presence_chooser_dialog_show (GtkWindow *parent)
1127 if (message_dialog) {
1128 gtk_window_present (GTK_WINDOW (message_dialog->dialog));
1132 message_dialog = g_new0 (CustomMessageDialog, 1);
1134 filename = empathy_file_lookup ("empathy-presence-chooser.glade",
1136 glade = empathy_glade_get_file (filename,
1137 "custom_message_dialog",
1139 "custom_message_dialog", &message_dialog->dialog,
1140 "checkbutton_save", &message_dialog->checkbutton_save,
1141 "comboboxentry_message", &message_dialog->comboboxentry_message,
1142 "combobox_status", &message_dialog->combobox_status,
1146 empathy_glade_connect (glade,
1148 "custom_message_dialog", "destroy", presence_chooser_dialog_destroy_cb,
1149 "custom_message_dialog", "response", presence_chooser_dialog_response_cb,
1150 "combobox_status", "changed", presence_chooser_dialog_status_changed_cb,
1151 "checkbutton_save", "toggled", presence_chooser_dialog_save_toggled_cb,
1154 g_object_unref (glade);
1156 /* Setup the message combobox */
1157 message_dialog->entry_message = GTK_BIN (message_dialog->comboboxentry_message)->child;
1158 gtk_entry_set_activates_default (GTK_ENTRY (message_dialog->entry_message), TRUE);
1159 gtk_entry_set_width_chars (GTK_ENTRY (message_dialog->entry_message), 25);
1160 g_signal_connect (message_dialog->entry_message, "changed",
1161 G_CALLBACK (presence_chooser_dialog_message_changed_cb),
1164 presence_chooser_dialog_setup (message_dialog);
1166 gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (message_dialog->comboboxentry_message), 0);
1170 gtk_window_set_transient_for (
1171 GTK_WINDOW (message_dialog->dialog),
1175 gtk_widget_show_all (message_dialog->dialog);