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;
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 *
150 GtkListStore *store = gtk_list_store_new (N_COLUMNS,
151 G_TYPE_STRING, /* COL_STATE_ICON_NAME */
152 MC_TYPE_PRESENCE, /* COL_STATE */
153 G_TYPE_STRING, /* COL_STATUS_TEXT */
154 G_TYPE_STRING, /* COL_DISPLAY_MARKUP */
155 G_TYPE_BOOLEAN, /* COL_STATUS_CUSTOMISABLE */
156 G_TYPE_INT); /* COL_TYPE */
161 for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
164 const char *status = empathy_presence_get_default_message (states[i]);
165 const char *icon_name = empathy_icon_name_for_presence (states[i]);
167 gtk_list_store_append (store, &iter);
168 gtk_list_store_set (store, &iter,
169 COL_STATE_ICON_NAME, icon_name,
170 COL_STATE, states[i],
171 COL_STATUS_TEXT, status,
172 COL_DISPLAY_MARKUP, status,
173 COL_STATUS_CUSTOMISABLE, states[i+1],
174 COL_TYPE, ENTRY_TYPE_BUILTIN,
178 /* Set custom messages if wanted */
179 list = empathy_status_presets_get (states[i], 5);
180 for (l = list; l; l = l->next) {
181 gtk_list_store_append (store, &iter);
182 gtk_list_store_set (store, &iter,
183 COL_STATE_ICON_NAME, icon_name,
184 COL_STATE, states[i],
185 COL_STATUS_TEXT, l->data,
186 COL_DISPLAY_MARKUP, l->data,
187 COL_STATUS_CUSTOMISABLE, TRUE,
188 COL_TYPE, ENTRY_TYPE_SAVED,
193 gtk_list_store_append (store, &iter);
194 gtk_list_store_set (store, &iter,
195 COL_STATE_ICON_NAME, icon_name,
196 COL_STATE, states[i],
198 COL_DISPLAY_MARKUP, "<i>Custom Message...</i>",
199 COL_STATUS_CUSTOMISABLE, TRUE,
200 COL_TYPE, ENTRY_TYPE_CUSTOM,
206 /* add a separator */
207 gtk_list_store_append (store, &iter);
208 gtk_list_store_set (store, &iter,
209 COL_TYPE, ENTRY_TYPE_SEPARATOR,
212 gtk_list_store_append (store, &iter);
213 gtk_list_store_set (store, &iter,
214 COL_STATE_ICON_NAME, GTK_STOCK_EDIT,
216 COL_DISPLAY_MARKUP, "Edit Custom Messages...",
217 COL_TYPE, ENTRY_TYPE_EDIT_CUSTOM,
220 return GTK_TREE_MODEL (store);
224 presence_chooser_popup_shown_cb (GObject *self,
229 g_object_get (self, "popup-shown", &shown, NULL);
233 GtkTreeModel *model = create_model ();
235 gtk_combo_box_set_model (GTK_COMBO_BOX (self), GTK_TREE_MODEL (model));
237 g_object_unref (model);
241 presence_chooser_set_status_editing (EmpathyPresenceChooser *self,
244 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
245 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
247 if (priv->block_set_editing) return;
251 priv->editing_status = TRUE;
252 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
253 GTK_ENTRY_ICON_SECONDARY,
255 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
256 GTK_ENTRY_ICON_SECONDARY,
258 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
259 GTK_ENTRY_ICON_PRIMARY,
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,
274 /* attempt to get the toplevel for this widget */
275 GtkWidget *window = gtk_widget_get_toplevel (GTK_WIDGET (self));
276 if (GTK_WIDGET_TOPLEVEL (window) && GTK_IS_WINDOW (window))
278 /* unset the focus */
279 gtk_window_set_focus (GTK_WINDOW (window), NULL);
281 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
283 priv->editing_status = FALSE;
288 mc_set_custom_state (EmpathyPresenceChooser *self)
290 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
291 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
293 /* update the status with MC */
294 const char *status = gtk_entry_get_text (GTK_ENTRY (entry));
295 DEBUG ("Sending state to MC-> %s (%s)\n",
296 g_enum_get_value (g_type_class_peek (MC_TYPE_PRESENCE),
297 priv->state)->value_name,
299 empathy_idle_set_presence (priv->idle, priv->state, status);
303 ui_set_custom_state (EmpathyPresenceChooser *self,
307 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
308 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
309 const char *icon_name;
311 priv->block_set_editing++;
312 priv->block_changed++;
314 icon_name = empathy_icon_name_for_presence (state);
315 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
316 GTK_ENTRY_ICON_PRIMARY,
318 gtk_entry_set_text (GTK_ENTRY (entry), status);
320 priv->block_changed--;
321 priv->block_set_editing--;
325 presence_chooser_reset_status (EmpathyPresenceChooser *self)
327 /* recover the status that was unset */
328 presence_chooser_presence_changed_cb (self);
332 presence_chooser_entry_icon_release_cb (EmpathyPresenceChooser *self,
333 GtkEntryIconPosition icon_pos,
337 presence_chooser_set_status_editing (self, FALSE);
338 mc_set_custom_state (self);
342 presence_chooser_entry_activate_cb (EmpathyPresenceChooser *self,
345 presence_chooser_set_status_editing (self, FALSE);
346 mc_set_custom_state (self);
350 presence_chooser_entry_key_press_event_cb (EmpathyPresenceChooser *self,
354 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
356 if (priv->editing_status && event->keyval == GDK_Escape)
358 /* the user pressed Escape, undo the editing */
359 presence_chooser_set_status_editing (self, FALSE);
360 presence_chooser_reset_status (self);
364 else if (event->keyval == GDK_Up || event->keyval == GDK_Down)
370 return FALSE; /* send this event elsewhere */
374 presence_chooser_entry_button_press_event_cb (EmpathyPresenceChooser *self,
375 GdkEventButton *event,
378 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
380 if (!priv->editing_status && event->button == 1)
382 presence_chooser_set_status_editing (self, TRUE);
383 gtk_widget_grab_focus (entry);
384 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
393 presence_chooser_entry_changed_cb (EmpathyPresenceChooser *self,
396 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
398 if (priv->block_changed) return;
400 /* the combo is being edited to a custom entry */
401 if (!priv->editing_status)
403 presence_chooser_set_status_editing (self, TRUE);
408 presence_chooser_changed_cb (GtkComboBox *self, gpointer user_data)
410 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
412 if (priv->block_changed) return;
416 McPresence new_state;
417 gboolean customisable = TRUE;
420 GtkTreeModel *model = gtk_combo_box_get_model (self);
421 if (!gtk_combo_box_get_active_iter (self, &iter))
426 gtk_tree_model_get (model, &iter,
427 COL_STATE_ICON_NAME, &icon_name,
428 COL_STATE, &new_state,
429 COL_STATUS_CUSTOMISABLE, &customisable,
433 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
435 /* some types of status aren't editable, set the editability of the
436 * entry appropriately. Unless we're just about to reset it anyway,
437 * in which case, don't fiddle with it */
438 if (type != ENTRY_TYPE_EDIT_CUSTOM)
440 gtk_editable_set_editable (GTK_EDITABLE (entry), customisable);
441 priv->state = new_state;
444 if (type == ENTRY_TYPE_EDIT_CUSTOM)
446 presence_chooser_reset_status (EMPATHY_PRESENCE_CHOOSER (self));
448 /* attempt to get the toplevel for this widget */
449 GtkWidget *window = gtk_widget_get_toplevel (GTK_WIDGET (self));
450 if (!GTK_WIDGET_TOPLEVEL (window) || !GTK_IS_WINDOW (window))
455 presence_chooser_dialog_show (GTK_WINDOW (window));
457 else if (type == ENTRY_TYPE_CUSTOM)
459 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
460 GTK_ENTRY_ICON_PRIMARY,
463 /* preseed the status */
464 if (priv->previous_type == ENTRY_TYPE_BUILTIN)
466 gtk_entry_set_text (GTK_ENTRY (entry), "");
470 const char *status = empathy_idle_get_status (priv->idle);
471 gtk_entry_set_text (GTK_ENTRY (entry), status);
475 gtk_widget_grab_focus (entry);
480 /* just in case we were setting a new status when
481 * things were changed */
482 presence_chooser_set_status_editing (
483 EMPATHY_PRESENCE_CHOOSER (self),
486 gtk_tree_model_get (model, &iter,
487 COL_STATUS_TEXT, &status,
490 empathy_idle_set_presence (priv->idle, priv->state, status);
495 if (type != ENTRY_TYPE_EDIT_CUSTOM)
497 priv->previous_type = type;
503 combo_row_separator_func (GtkTreeModel *model,
508 gtk_tree_model_get (model, iter,
512 return (type == ENTRY_TYPE_SEPARATOR);
516 presence_chooser_entry_focus_out_cb (EmpathyPresenceChooser *chooser,
517 GdkEventFocus *event,
520 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
522 if (priv->editing_status)
524 // entry_activate_cb (chooser, entry);
531 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
533 EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
534 EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
536 chooser->priv = priv;
538 GtkTreeModel *model = create_model ();
540 gtk_combo_box_set_model (GTK_COMBO_BOX (chooser), GTK_TREE_MODEL (model));
541 gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (chooser),
543 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
544 combo_row_separator_func,
547 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
548 gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
549 GTK_ENTRY_ICON_PRIMARY, FALSE);
550 g_signal_connect_object (entry, "icon-release",
551 G_CALLBACK (presence_chooser_entry_icon_release_cb),
554 g_signal_connect_object (entry, "activate",
555 G_CALLBACK (presence_chooser_entry_activate_cb),
558 g_signal_connect_object (entry, "key-press-event",
559 G_CALLBACK (presence_chooser_entry_key_press_event_cb),
562 g_signal_connect_object (entry, "button-press-event",
563 G_CALLBACK (presence_chooser_entry_button_press_event_cb),
567 GtkCellRenderer *renderer;
568 gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
570 renderer = gtk_cell_renderer_pixbuf_new ();
571 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, FALSE);
572 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
573 "icon-name", COL_STATE_ICON_NAME,
575 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
577 renderer = gtk_cell_renderer_text_new ();
578 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, TRUE);
579 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
580 "markup", COL_DISPLAY_MARKUP,
583 g_object_unref (model);
585 g_signal_connect (chooser, "notify::popup-shown",
586 G_CALLBACK (presence_chooser_popup_shown_cb), NULL);
587 g_signal_connect (chooser, "changed",
588 G_CALLBACK (presence_chooser_changed_cb), NULL);
589 g_signal_connect_swapped (entry, "changed",
590 G_CALLBACK (presence_chooser_entry_changed_cb),
592 g_signal_connect_swapped (entry, "focus-out-event",
593 G_CALLBACK (presence_chooser_entry_focus_out_cb),
596 priv->idle = empathy_idle_dup_singleton ();
597 presence_chooser_presence_changed_cb (chooser);
598 g_signal_connect_swapped (priv->idle, "notify",
599 G_CALLBACK (presence_chooser_presence_changed_cb),
602 g_object_set (chooser,
603 // FIXME: this string sucks
604 "tooltip-text", _("Set your presence and current status"),
609 presence_chooser_finalize (GObject *object)
611 EmpathyPresenceChooserPriv *priv;
613 priv = GET_PRIV (object);
615 if (priv->flash_timeout_id) {
616 g_source_remove (priv->flash_timeout_id);
619 g_signal_handlers_disconnect_by_func (priv->idle,
620 presence_chooser_presence_changed_cb,
622 g_object_unref (priv->idle);
624 G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
628 empathy_presence_chooser_new (void)
632 chooser = g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER, NULL);
638 presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
640 EmpathyPresenceChooserPriv *priv;
642 McPresence flash_state;
645 priv = GET_PRIV (chooser);
647 priv->state = state = empathy_idle_get_state (priv->idle);
648 status = empathy_idle_get_status (priv->idle);
649 flash_state = empathy_idle_get_flash_state (priv->idle);
651 /* look through the model and attempt to find a matching state */
652 GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
654 gboolean valid, match_state = FALSE, match = FALSE;
655 for (valid = gtk_tree_model_get_iter_first (model, &iter);
657 valid = gtk_tree_model_iter_next (model, &iter))
663 gtk_tree_model_get (model, &iter,
668 if (m_type == ENTRY_TYPE_CUSTOM ||
669 m_type == ENTRY_TYPE_SEPARATOR ||
670 m_type == ENTRY_TYPE_EDIT_CUSTOM)
674 else if (!match_state && state == m_state)
676 /* we are now in the section that can contain our
680 else if (match_state && state != m_state)
682 /* we have passed the section that can contain our
687 gtk_tree_model_get (model, &iter,
688 COL_STATUS_TEXT, &m_status,
691 match = !strcmp (status, m_status);
701 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
705 // FIXME - do we insert the match into the menu?
706 ui_set_custom_state (chooser, state, status);
709 if (flash_state != MC_PRESENCE_UNSET) {
710 presence_chooser_flash_start (chooser, state, flash_state);
712 presence_chooser_flash_stop (chooser, state);
717 presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser)
719 EmpathyPresenceChooserPriv *priv;
721 static gboolean on = FALSE;
723 priv = GET_PRIV (chooser);
726 state = priv->flash_state_1;
728 state = priv->flash_state_2;
731 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
732 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
733 GTK_ENTRY_ICON_PRIMARY,
734 empathy_icon_name_for_presence (state));
742 presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
746 EmpathyPresenceChooserPriv *priv;
748 g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
750 priv = GET_PRIV (chooser);
752 priv->flash_state_1 = state_1;
753 priv->flash_state_2 = state_2;
755 if (!priv->flash_timeout_id) {
756 priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
757 (GSourceFunc) presence_chooser_flash_timeout_cb,
763 presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
766 EmpathyPresenceChooserPriv *priv;
768 g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
770 priv = GET_PRIV (chooser);
772 if (priv->flash_timeout_id) {
773 g_source_remove (priv->flash_timeout_id);
774 priv->flash_timeout_id = 0;
776 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
778 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
779 GTK_ENTRY_ICON_PRIMARY,
780 empathy_icon_name_for_presence (state));
782 // FIXME - what does this do?
783 // priv->last_state = state;
787 empathy_presence_chooser_create_menu (void)
795 menu = gtk_menu_new ();
797 for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
800 status = empathy_presence_get_default_message (states[i]);
801 presence_chooser_menu_add_item (menu,
806 /* Set custom messages if wanted */
807 list = empathy_status_presets_get (states[i], 5);
808 for (l = list; l; l = l->next) {
809 presence_chooser_menu_add_item (menu,
819 item = gtk_menu_item_new ();
820 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
821 gtk_widget_show (item);
823 /* Custom messages */
824 item = gtk_image_menu_item_new_with_label (_("Custom messages..."));
825 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
826 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
827 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
828 gtk_widget_show (image);
829 gtk_widget_show (item);
831 g_signal_connect (item,
833 G_CALLBACK (presence_chooser_custom_activate_cb),
840 presence_chooser_menu_add_item (GtkWidget *menu,
846 const gchar *icon_name;
848 item = gtk_image_menu_item_new_with_label (str);
849 icon_name = empathy_icon_name_for_presence (state);
851 g_signal_connect (item, "activate",
852 G_CALLBACK (presence_chooser_noncustom_activate_cb),
855 image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
856 gtk_widget_show (image);
858 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
859 gtk_widget_show (item);
861 g_object_set_data_full (G_OBJECT (item),
862 "status", g_strdup (str),
863 (GDestroyNotify) g_free);
865 g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
867 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
871 presence_chooser_noncustom_activate_cb (GtkWidget *item,
877 status = g_object_get_data (G_OBJECT (item), "status");
878 state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
880 presence_chooser_set_state (state, status);
884 presence_chooser_set_state (McPresence state,
889 idle = empathy_idle_dup_singleton ();
890 empathy_idle_set_presence (idle, state, status);
891 g_object_unref (idle);
895 presence_chooser_custom_activate_cb (GtkWidget *item,
898 presence_chooser_dialog_show (NULL);
902 presence_chooser_dialog_get_selected (CustomMessageDialog *dialog)
906 McPresence presence = LAST_MC_PRESENCE;
908 model = gtk_combo_box_get_model (GTK_COMBO_BOX (dialog->combobox_status));
909 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (dialog->combobox_status), &iter)) {
910 gtk_tree_model_get (model, &iter,
911 COL_PRESENCE, &presence,
919 presence_chooser_dialog_status_changed_cb (GtkWidget *widget,
920 CustomMessageDialog *dialog)
924 McPresence presence = LAST_MC_PRESENCE;
927 presence = presence_chooser_dialog_get_selected (dialog);
929 store = gtk_list_store_new (1, G_TYPE_STRING);
930 messages = empathy_status_presets_get (presence, -1);
931 for (l = messages; l; l = l->next) {
932 gtk_list_store_append (store, &iter);
933 gtk_list_store_set (store, &iter, 0, l->data, -1);
936 gtk_entry_set_text (GTK_ENTRY (dialog->entry_message),
937 messages ? messages->data : "");
939 g_list_free (messages);
941 gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->comboboxentry_message),
942 GTK_TREE_MODEL (store));
944 g_object_unref (store);
948 presence_chooser_dialog_message_changed_cb (GtkWidget *widget,
949 CustomMessageDialog *dialog)
954 gboolean found = FALSE;
956 presence = presence_chooser_dialog_get_selected (dialog);
957 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
959 messages = empathy_status_presets_get (presence, -1);
960 for (l = messages; l; l = l->next) {
961 if (!tp_strdiff (text, l->data)) {
966 g_list_free (messages);
968 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_save),
973 presence_chooser_dialog_save_toggled_cb (GtkWidget *widget,
974 CustomMessageDialog *dialog)
980 active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_save));
981 state = presence_chooser_dialog_get_selected (dialog);
982 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
985 empathy_status_presets_set_last (state, text);
987 empathy_status_presets_remove (state, text);
992 presence_chooser_dialog_setup (CustomMessageDialog *dialog)
995 GtkCellRenderer *renderer;
999 store = gtk_list_store_new (COL_COUNT,
1000 G_TYPE_STRING, /* Icon name */
1001 G_TYPE_STRING, /* Label */
1002 MC_TYPE_PRESENCE); /* Presence */
1003 gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->combobox_status),
1004 GTK_TREE_MODEL (store));
1006 renderer = gtk_cell_renderer_pixbuf_new ();
1007 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (dialog->combobox_status), renderer, FALSE);
1008 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combobox_status), renderer,
1009 "icon-name", COL_ICON,
1011 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
1013 renderer = gtk_cell_renderer_text_new ();
1014 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (dialog->combobox_status), renderer, TRUE);
1015 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combobox_status), renderer,
1019 for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
1024 gtk_list_store_append (store, &iter);
1025 gtk_list_store_set (store, &iter,
1026 COL_ICON, empathy_icon_name_for_presence (states[i]),
1027 COL_LABEL, empathy_presence_get_default_message (states[i]),
1028 COL_PRESENCE, states[i],
1032 gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combobox_status), 0);
1036 presence_chooser_dialog_response_cb (GtkWidget *widget,
1038 CustomMessageDialog *dialog)
1040 if (response == GTK_RESPONSE_APPLY) {
1044 state = presence_chooser_dialog_get_selected (dialog);
1045 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
1047 presence_chooser_set_state (state, text);
1050 gtk_widget_destroy (widget);
1054 presence_chooser_dialog_destroy_cb (GtkWidget *widget,
1055 CustomMessageDialog *dialog)
1059 message_dialog = NULL;
1063 presence_chooser_dialog_show (GtkWindow *parent)
1068 if (message_dialog) {
1069 gtk_window_present (GTK_WINDOW (message_dialog->dialog));
1073 message_dialog = g_new0 (CustomMessageDialog, 1);
1075 filename = empathy_file_lookup ("empathy-presence-chooser.glade",
1077 glade = empathy_glade_get_file (filename,
1078 "custom_message_dialog",
1080 "custom_message_dialog", &message_dialog->dialog,
1081 "checkbutton_save", &message_dialog->checkbutton_save,
1082 "comboboxentry_message", &message_dialog->comboboxentry_message,
1083 "combobox_status", &message_dialog->combobox_status,
1087 empathy_glade_connect (glade,
1089 "custom_message_dialog", "destroy", presence_chooser_dialog_destroy_cb,
1090 "custom_message_dialog", "response", presence_chooser_dialog_response_cb,
1091 "combobox_status", "changed", presence_chooser_dialog_status_changed_cb,
1092 "checkbutton_save", "toggled", presence_chooser_dialog_save_toggled_cb,
1095 g_object_unref (glade);
1097 /* Setup the message combobox */
1098 message_dialog->entry_message = GTK_BIN (message_dialog->comboboxentry_message)->child;
1099 gtk_entry_set_activates_default (GTK_ENTRY (message_dialog->entry_message), TRUE);
1100 gtk_entry_set_width_chars (GTK_ENTRY (message_dialog->entry_message), 25);
1101 g_signal_connect (message_dialog->entry_message, "changed",
1102 G_CALLBACK (presence_chooser_dialog_message_changed_cb),
1105 presence_chooser_dialog_setup (message_dialog);
1107 gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (message_dialog->comboboxentry_message), 0);
1111 gtk_window_set_transient_for (
1112 GTK_WINDOW (message_dialog->dialog),
1116 gtk_widget_show_all (message_dialog->dialog);