1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2005-2007 Imendio AB
4 * Copyright (C) 2009 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Richard Hult <richard@imendio.com>
22 * Martyn Russell <martyn@imendio.com>
23 * Xavier Claessens <xclaesse@gmail.com>
24 * Davyd Madeley <davyd.madeley@collabora.co.uk>
32 #include <glib/gi18n-lib.h>
34 #include <glade/glade.h>
35 #include <gdk/gdkkeysyms.h>
37 #include <telepathy-glib/util.h>
38 #include <libmissioncontrol/mc-enum-types.h>
40 #include <libempathy/empathy-idle.h>
41 #include <libempathy/empathy-utils.h>
42 #include <libempathy/empathy-status-presets.h>
44 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
45 #include <libempathy/empathy-debug.h>
47 #include "empathy-ui-utils.h"
48 #include "empathy-images.h"
49 #include "empathy-presence-chooser.h"
51 /* Flashing delay for icons (milliseconds). */
52 #define FLASH_TIMEOUT 500
54 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyPresenceChooser)
58 gboolean editing_status;
59 int block_set_editing;
61 guint focus_out_idle_source;
66 McPresence flash_state_1;
67 McPresence flash_state_2;
68 guint flash_timeout_id;
69 } EmpathyPresenceChooserPriv;
73 GtkWidget *checkbutton_save;
74 GtkWidget *comboboxentry_message;
75 GtkWidget *entry_message;
76 GtkWidget *combobox_status;
77 GtkTreeModel *model_status;
78 } CustomMessageDialog;
87 static CustomMessageDialog *message_dialog = NULL;
88 /* States to be listed in the menu.
89 * Each state has a boolean telling if it can have custom message */
90 static guint states[] = {MC_PRESENCE_AVAILABLE, TRUE,
91 MC_PRESENCE_DO_NOT_DISTURB, TRUE,
92 MC_PRESENCE_AWAY, TRUE,
93 MC_PRESENCE_HIDDEN, FALSE,
94 MC_PRESENCE_OFFLINE, FALSE};
96 static void presence_chooser_finalize (GObject *object);
97 static void presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser);
98 static gboolean presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser);
99 static void presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
102 static void presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
104 static void presence_chooser_menu_add_item (GtkWidget *menu,
107 static void presence_chooser_noncustom_activate_cb (GtkWidget *item,
109 static void presence_chooser_set_state (McPresence state,
110 const gchar *status);
111 static void presence_chooser_custom_activate_cb (GtkWidget *item,
113 static void presence_chooser_dialog_show (GtkWindow *parent);
115 G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_COMBO_BOX_ENTRY);
118 empathy_presence_chooser_class_init (EmpathyPresenceChooserClass *klass)
120 GObjectClass *object_class = G_OBJECT_CLASS (klass);
122 object_class->finalize = presence_chooser_finalize;
124 g_type_class_add_private (object_class, sizeof (EmpathyPresenceChooserPriv));
133 COL_STATUS_CUSTOMISABLE,
143 ENTRY_TYPE_SEPARATOR,
144 ENTRY_TYPE_EDIT_CUSTOM,
147 static GtkTreeModel *
148 presence_chooser_create_model (void)
151 char *custom_message;
153 store = gtk_list_store_new (N_COLUMNS,
154 G_TYPE_STRING, /* COL_STATE_ICON_NAME */
155 MC_TYPE_PRESENCE, /* COL_STATE */
156 G_TYPE_STRING, /* COL_STATUS_TEXT */
157 G_TYPE_STRING, /* COL_DISPLAY_MARKUP */
158 G_TYPE_BOOLEAN, /* COL_STATUS_CUSTOMISABLE */
159 G_TYPE_INT); /* COL_TYPE */
161 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 return GTK_TREE_MODEL (store);
227 presence_chooser_popup_shown_cb (GObject *self,
231 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
234 g_object_get (self, "popup-shown", &shown, NULL);
238 /* see presence_chooser_entry_focus_out_cb() for what this does */
239 if (priv->focus_out_idle_source != 0)
241 g_source_remove (priv->focus_out_idle_source);
242 priv->focus_out_idle_source = 0;
245 GtkTreeModel *model = presence_chooser_create_model ();
247 gtk_combo_box_set_model (GTK_COMBO_BOX (self), GTK_TREE_MODEL (model));
249 g_object_unref (model);
253 presence_chooser_set_status_editing (EmpathyPresenceChooser *self,
256 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
257 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
259 if (priv->block_set_editing) return;
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,
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)) {
288 /* unset the focus */
289 gtk_window_set_focus (GTK_WINDOW (window), NULL);
292 /* see presence_chooser_entry_focus_out_cb()
293 * for what this does */
294 if (priv->focus_out_idle_source != 0) {
295 g_source_remove (priv->focus_out_idle_source);
296 priv->focus_out_idle_source = 0;
299 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
301 priv->editing_status = FALSE;
306 mc_set_custom_state (EmpathyPresenceChooser *self)
308 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
312 entry = gtk_bin_get_child (GTK_BIN (self));
313 /* update the status with MC */
314 status = gtk_entry_get_text (GTK_ENTRY (entry));
316 DEBUG ("Sending state to MC-> %s (%s)\n",
317 g_enum_get_value (g_type_class_peek (MC_TYPE_PRESENCE),
318 priv->state)->value_name,
320 empathy_idle_set_presence (priv->idle, priv->state, status);
324 ui_set_custom_state (EmpathyPresenceChooser *self,
328 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
330 const char *icon_name;
332 entry = gtk_bin_get_child (GTK_BIN (self));
334 priv->block_set_editing++;
335 priv->block_changed++;
337 icon_name = empathy_icon_name_for_presence (state);
338 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
339 GTK_ENTRY_ICON_PRIMARY,
341 gtk_entry_set_text (GTK_ENTRY (entry), status);
343 priv->block_changed--;
344 priv->block_set_editing--;
348 presence_chooser_reset_status (EmpathyPresenceChooser *self)
350 /* recover the status that was unset */
351 presence_chooser_presence_changed_cb (self);
352 presence_chooser_set_status_editing (self, FALSE);
356 presence_chooser_entry_icon_release_cb (EmpathyPresenceChooser *self,
357 GtkEntryIconPosition icon_pos,
361 presence_chooser_set_status_editing (self, FALSE);
362 mc_set_custom_state (self);
366 presence_chooser_entry_activate_cb (EmpathyPresenceChooser *self,
369 presence_chooser_set_status_editing (self, FALSE);
370 mc_set_custom_state (self);
374 presence_chooser_entry_key_press_event_cb (EmpathyPresenceChooser *self,
378 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
380 if (priv->editing_status && event->keyval == GDK_Escape) {
381 /* the user pressed Escape, undo the editing */
382 presence_chooser_reset_status (self);
386 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)) {
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) {
423 presence_chooser_set_status_editing (self, TRUE);
428 presence_chooser_changed_cb (GtkComboBox *self, gpointer user_data)
430 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
432 if (priv->block_changed) return;
436 McPresence new_state;
437 gboolean customisable = TRUE;
441 GtkTreeModel *model = gtk_combo_box_get_model (self);
442 if (!gtk_combo_box_get_active_iter (self, &iter)) {
446 gtk_tree_model_get (model, &iter,
447 COL_STATE_ICON_NAME, &icon_name,
448 COL_STATE, &new_state,
449 COL_STATUS_CUSTOMISABLE, &customisable,
453 entry = gtk_bin_get_child (GTK_BIN (self));
455 /* some types of status aren't editable, set the editability of the
456 * entry appropriately. Unless we're just about to reset it anyway,
457 * in which case, don't fiddle with it */
458 if (type != ENTRY_TYPE_EDIT_CUSTOM) {
459 gtk_editable_set_editable (GTK_EDITABLE (entry), customisable);
460 priv->state = new_state;
463 if (type == ENTRY_TYPE_EDIT_CUSTOM) {
464 presence_chooser_reset_status (EMPATHY_PRESENCE_CHOOSER (self));
466 /* attempt to get the toplevel for this widget */
467 GtkWidget *window = gtk_widget_get_toplevel (GTK_WIDGET (self));
468 if (!GTK_WIDGET_TOPLEVEL (window) || !GTK_IS_WINDOW (window)) {
472 presence_chooser_dialog_show (GTK_WINDOW (window));
474 else if (type == ENTRY_TYPE_CUSTOM) {
475 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
476 GTK_ENTRY_ICON_PRIMARY,
479 /* preseed the status */
480 if (priv->previous_type == ENTRY_TYPE_BUILTIN) {
481 /* if their previous entry was a builtin, don't
483 gtk_entry_set_text (GTK_ENTRY (entry), "");
486 /* else preseed the text of their currently entered
490 status = empathy_idle_get_status (priv->idle);
491 gtk_entry_set_text (GTK_ENTRY (entry), status);
495 gtk_widget_grab_focus (entry);
499 /* just in case we were setting a new status when
500 * things were changed */
501 presence_chooser_set_status_editing (
502 EMPATHY_PRESENCE_CHOOSER (self),
505 gtk_tree_model_get (model, &iter,
506 COL_STATUS_TEXT, &status,
509 empathy_idle_set_presence (priv->idle, priv->state, status);
514 if (type != ENTRY_TYPE_EDIT_CUSTOM) {
515 priv->previous_type = type;
521 combo_row_separator_func (GtkTreeModel *model,
526 gtk_tree_model_get (model, iter,
530 return (type == ENTRY_TYPE_SEPARATOR);
534 presence_chooser_entry_focus_out_idle_cb (gpointer user_data)
536 EmpathyPresenceChooser *chooser;
539 DEBUG ("Autocommiting status message\n");
541 chooser = EMPATHY_PRESENCE_CHOOSER (user_data);
542 entry = gtk_bin_get_child (GTK_BIN (chooser));
544 presence_chooser_entry_activate_cb (chooser, GTK_ENTRY (entry));
550 presence_chooser_entry_focus_out_cb (EmpathyPresenceChooser *chooser,
551 GdkEventFocus *event,
554 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
556 if (priv->editing_status) {
557 /* this seems a bit evil and maybe it will be fragile,
558 * someone should think of a better way to do it.
560 * The entry has focused out, but we don't know where the focus
561 * has gone. If it goes to the combo box, we don't want to
562 * do anything. If it's gone anywhere else, we want to commit
565 * Thus we install this idle handler and store its source.
566 * If the source is scheduled when the popup handler runs,
567 * it will remove it, else the callback will commit the result.
569 priv->focus_out_idle_source = g_idle_add (
570 presence_chooser_entry_focus_out_idle_cb,
574 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
580 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
582 EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
583 EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
586 GtkCellRenderer *renderer;
588 chooser->priv = priv;
590 model = presence_chooser_create_model ();
592 gtk_combo_box_set_model (GTK_COMBO_BOX (chooser), GTK_TREE_MODEL (model));
593 gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (chooser),
595 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
596 combo_row_separator_func,
599 entry = gtk_bin_get_child (GTK_BIN (chooser));
600 gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
601 GTK_ENTRY_ICON_PRIMARY, FALSE);
602 g_signal_connect_object (entry, "icon-release",
603 G_CALLBACK (presence_chooser_entry_icon_release_cb),
606 g_signal_connect_object (entry, "activate",
607 G_CALLBACK (presence_chooser_entry_activate_cb),
610 g_signal_connect_object (entry, "key-press-event",
611 G_CALLBACK (presence_chooser_entry_key_press_event_cb),
614 g_signal_connect_object (entry, "button-press-event",
615 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_object_unref (model);
636 g_signal_connect (chooser, "notify::popup-shown",
637 G_CALLBACK (presence_chooser_popup_shown_cb), NULL);
638 g_signal_connect (chooser, "changed",
639 G_CALLBACK (presence_chooser_changed_cb), NULL);
640 g_signal_connect_swapped (entry, "changed",
641 G_CALLBACK (presence_chooser_entry_changed_cb),
643 g_signal_connect_swapped (entry, "focus-out-event",
644 G_CALLBACK (presence_chooser_entry_focus_out_cb),
647 priv->idle = empathy_idle_dup_singleton ();
648 presence_chooser_presence_changed_cb (chooser);
649 g_signal_connect_swapped (priv->idle, "notify",
650 G_CALLBACK (presence_chooser_presence_changed_cb),
653 g_object_set (chooser,
654 // FIXME: this string sucks
655 "tooltip-text", _("Set your presence and current status"),
660 presence_chooser_finalize (GObject *object)
662 EmpathyPresenceChooserPriv *priv;
664 priv = GET_PRIV (object);
666 if (priv->flash_timeout_id) {
667 g_source_remove (priv->flash_timeout_id);
670 if (priv->focus_out_idle_source) {
671 g_source_remove (priv->focus_out_idle_source);
674 g_signal_handlers_disconnect_by_func (priv->idle,
675 presence_chooser_presence_changed_cb,
677 g_object_unref (priv->idle);
679 G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
683 empathy_presence_chooser_new (void)
687 chooser = g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER, NULL);
693 presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
695 EmpathyPresenceChooserPriv *priv;
697 McPresence flash_state;
701 gboolean valid, match_state = FALSE, match = FALSE;
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 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
711 for (valid = gtk_tree_model_get_iter_first (model, &iter);
713 valid = gtk_tree_model_iter_next (model, &iter)) {
718 gtk_tree_model_get (model, &iter,
723 if (m_type == ENTRY_TYPE_CUSTOM ||
724 m_type == ENTRY_TYPE_SEPARATOR ||
725 m_type == ENTRY_TYPE_EDIT_CUSTOM) {
728 else if (!match_state && state == m_state) {
729 /* we are now in the section that can contain our
733 else if (match_state && state != m_state) {
734 /* we have passed the section that can contain our
739 gtk_tree_model_get (model, &iter,
740 COL_STATUS_TEXT, &m_status,
743 match = !strcmp (status, m_status);
752 priv->block_changed++;
753 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
754 priv->block_changed--;
757 ui_set_custom_state (chooser, state, status);
760 if (flash_state != MC_PRESENCE_UNSET) {
761 presence_chooser_flash_start (chooser, state, flash_state);
764 presence_chooser_flash_stop (chooser, state);
769 presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser)
771 EmpathyPresenceChooserPriv *priv;
773 static gboolean on = FALSE;
775 priv = GET_PRIV (chooser);
778 state = priv->flash_state_1;
781 state = priv->flash_state_2;
784 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
785 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
786 GTK_ENTRY_ICON_PRIMARY,
787 empathy_icon_name_for_presence (state));
795 presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
799 EmpathyPresenceChooserPriv *priv;
801 g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
803 priv = GET_PRIV (chooser);
805 priv->flash_state_1 = state_1;
806 priv->flash_state_2 = state_2;
808 if (!priv->flash_timeout_id) {
809 priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
810 (GSourceFunc) presence_chooser_flash_timeout_cb,
816 presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
819 EmpathyPresenceChooserPriv *priv;
821 g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
823 priv = GET_PRIV (chooser);
825 if (priv->flash_timeout_id) {
826 g_source_remove (priv->flash_timeout_id);
827 priv->flash_timeout_id = 0;
829 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
831 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
832 GTK_ENTRY_ICON_PRIMARY,
833 empathy_icon_name_for_presence (state));
837 empathy_presence_chooser_create_menu (void)
845 menu = gtk_menu_new ();
847 for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
850 status = empathy_presence_get_default_message (states[i]);
851 presence_chooser_menu_add_item (menu,
856 /* Set custom messages if wanted */
857 list = empathy_status_presets_get (states[i], 5);
858 for (l = list; l; l = l->next) {
859 presence_chooser_menu_add_item (menu,
869 item = gtk_menu_item_new ();
870 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
871 gtk_widget_show (item);
873 /* Custom messages */
874 item = gtk_image_menu_item_new_with_label (_("Custom messages..."));
875 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
876 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
877 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
878 gtk_widget_show (image);
879 gtk_widget_show (item);
881 g_signal_connect (item,
883 G_CALLBACK (presence_chooser_custom_activate_cb),
890 presence_chooser_menu_add_item (GtkWidget *menu,
896 const gchar *icon_name;
898 item = gtk_image_menu_item_new_with_label (str);
899 icon_name = empathy_icon_name_for_presence (state);
901 g_signal_connect (item, "activate",
902 G_CALLBACK (presence_chooser_noncustom_activate_cb),
905 image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
906 gtk_widget_show (image);
908 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
909 gtk_widget_show (item);
911 g_object_set_data_full (G_OBJECT (item),
912 "status", g_strdup (str),
913 (GDestroyNotify) g_free);
915 g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
917 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
921 presence_chooser_noncustom_activate_cb (GtkWidget *item,
927 status = g_object_get_data (G_OBJECT (item), "status");
928 state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
930 presence_chooser_set_state (state, status);
934 presence_chooser_set_state (McPresence state,
939 idle = empathy_idle_dup_singleton ();
940 empathy_idle_set_presence (idle, state, status);
941 g_object_unref (idle);
945 presence_chooser_custom_activate_cb (GtkWidget *item,
948 presence_chooser_dialog_show (NULL);
952 presence_chooser_dialog_get_selected (CustomMessageDialog *dialog)
956 McPresence presence = LAST_MC_PRESENCE;
958 model = gtk_combo_box_get_model (GTK_COMBO_BOX (dialog->combobox_status));
959 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (dialog->combobox_status), &iter)) {
960 gtk_tree_model_get (model, &iter,
961 COL_PRESENCE, &presence,
969 presence_chooser_dialog_status_changed_cb (GtkWidget *widget,
970 CustomMessageDialog *dialog)
974 McPresence presence = LAST_MC_PRESENCE;
977 presence = presence_chooser_dialog_get_selected (dialog);
979 store = gtk_list_store_new (1, G_TYPE_STRING);
980 messages = empathy_status_presets_get (presence, -1);
981 for (l = messages; l; l = l->next) {
982 gtk_list_store_append (store, &iter);
983 gtk_list_store_set (store, &iter, 0, l->data, -1);
986 gtk_entry_set_text (GTK_ENTRY (dialog->entry_message),
987 messages ? messages->data : "");
989 g_list_free (messages);
991 gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->comboboxentry_message),
992 GTK_TREE_MODEL (store));
994 g_object_unref (store);
998 presence_chooser_dialog_message_changed_cb (GtkWidget *widget,
999 CustomMessageDialog *dialog)
1001 McPresence presence;
1002 GList *messages, *l;
1004 gboolean found = FALSE;
1006 presence = presence_chooser_dialog_get_selected (dialog);
1007 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1009 messages = empathy_status_presets_get (presence, -1);
1010 for (l = messages; l; l = l->next) {
1011 if (!tp_strdiff (text, l->data)) {
1016 g_list_free (messages);
1018 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_save),
1023 presence_chooser_dialog_save_toggled_cb (GtkWidget *widget,
1024 CustomMessageDialog *dialog)
1030 active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_save));
1031 state = presence_chooser_dialog_get_selected (dialog);
1032 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1035 empathy_status_presets_set_last (state, text);
1038 empathy_status_presets_remove (state, text);
1043 presence_chooser_dialog_setup (CustomMessageDialog *dialog)
1045 GtkListStore *store;
1046 GtkCellRenderer *renderer;
1050 store = gtk_list_store_new (COL_COUNT,
1051 G_TYPE_STRING, /* Icon name */
1052 G_TYPE_STRING, /* Label */
1053 MC_TYPE_PRESENCE); /* Presence */
1054 gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->combobox_status),
1055 GTK_TREE_MODEL (store));
1057 renderer = gtk_cell_renderer_pixbuf_new ();
1058 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (dialog->combobox_status), renderer, FALSE);
1059 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combobox_status), renderer,
1060 "icon-name", COL_ICON,
1062 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
1064 renderer = gtk_cell_renderer_text_new ();
1065 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (dialog->combobox_status), renderer, TRUE);
1066 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combobox_status), renderer,
1070 for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
1075 gtk_list_store_append (store, &iter);
1076 gtk_list_store_set (store, &iter,
1077 COL_ICON, empathy_icon_name_for_presence (states[i]),
1078 COL_LABEL, empathy_presence_get_default_message (states[i]),
1079 COL_PRESENCE, states[i],
1083 gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combobox_status), 0);
1087 presence_chooser_dialog_response_cb (GtkWidget *widget,
1089 CustomMessageDialog *dialog)
1091 if (response == GTK_RESPONSE_APPLY) {
1095 state = presence_chooser_dialog_get_selected (dialog);
1096 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1098 presence_chooser_set_state (state, text);
1101 gtk_widget_destroy (widget);
1105 presence_chooser_dialog_destroy_cb (GtkWidget *widget,
1106 CustomMessageDialog *dialog)
1110 message_dialog = NULL;
1114 presence_chooser_dialog_show (GtkWindow *parent)
1119 if (message_dialog) {
1120 gtk_window_present (GTK_WINDOW (message_dialog->dialog));
1124 message_dialog = g_new0 (CustomMessageDialog, 1);
1126 filename = empathy_file_lookup ("empathy-presence-chooser.glade",
1128 glade = empathy_glade_get_file (filename,
1129 "custom_message_dialog",
1131 "custom_message_dialog", &message_dialog->dialog,
1132 "checkbutton_save", &message_dialog->checkbutton_save,
1133 "comboboxentry_message", &message_dialog->comboboxentry_message,
1134 "combobox_status", &message_dialog->combobox_status,
1138 empathy_glade_connect (glade,
1140 "custom_message_dialog", "destroy", presence_chooser_dialog_destroy_cb,
1141 "custom_message_dialog", "response", presence_chooser_dialog_response_cb,
1142 "combobox_status", "changed", presence_chooser_dialog_status_changed_cb,
1143 "checkbutton_save", "toggled", presence_chooser_dialog_save_toggled_cb,
1146 g_object_unref (glade);
1148 /* Setup the message combobox */
1149 message_dialog->entry_message = GTK_BIN (message_dialog->comboboxentry_message)->child;
1150 gtk_entry_set_activates_default (GTK_ENTRY (message_dialog->entry_message), TRUE);
1151 gtk_entry_set_width_chars (GTK_ENTRY (message_dialog->entry_message), 25);
1152 g_signal_connect (message_dialog->entry_message, "changed",
1153 G_CALLBACK (presence_chooser_dialog_message_changed_cb),
1156 presence_chooser_dialog_setup (message_dialog);
1158 gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (message_dialog->comboboxentry_message), 0);
1162 gtk_window_set_transient_for (
1163 GTK_WINDOW (message_dialog->dialog),
1167 gtk_widget_show_all (message_dialog->dialog);