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;
65 McPresence flash_state_1;
66 McPresence flash_state_2;
67 guint flash_timeout_id;
68 } EmpathyPresenceChooserPriv;
72 GtkWidget *checkbutton_save;
73 GtkWidget *comboboxentry_message;
74 GtkWidget *entry_message;
75 GtkWidget *combobox_status;
76 GtkTreeModel *model_status;
77 } CustomMessageDialog;
86 static CustomMessageDialog *message_dialog = NULL;
87 /* States to be listed in the menu.
88 * Each state has a boolean telling if it can have custom message */
89 static guint states[] = {MC_PRESENCE_AVAILABLE, TRUE,
90 MC_PRESENCE_DO_NOT_DISTURB, TRUE,
91 MC_PRESENCE_AWAY, TRUE,
92 MC_PRESENCE_HIDDEN, FALSE,
93 MC_PRESENCE_OFFLINE, FALSE};
95 static void presence_chooser_finalize (GObject *object);
96 static void presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser);
97 static gboolean presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser);
98 static void presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
101 static void presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
103 static void presence_chooser_menu_add_item (GtkWidget *menu,
106 static void presence_chooser_noncustom_activate_cb (GtkWidget *item,
108 static void presence_chooser_set_state (McPresence state,
109 const gchar *status);
110 static void presence_chooser_custom_activate_cb (GtkWidget *item,
112 static void presence_chooser_dialog_show (GtkWindow *parent);
114 G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_COMBO_BOX_ENTRY);
117 empathy_presence_chooser_class_init (EmpathyPresenceChooserClass *klass)
119 GObjectClass *object_class = G_OBJECT_CLASS (klass);
121 object_class->finalize = presence_chooser_finalize;
123 g_type_class_add_private (object_class, sizeof (EmpathyPresenceChooserPriv));
132 COL_STATUS_CUSTOMISABLE,
142 ENTRY_TYPE_SEPARATOR,
143 ENTRY_TYPE_EDIT_CUSTOM,
146 static GtkTreeModel *
149 GtkListStore *store = gtk_list_store_new (N_COLUMNS,
150 G_TYPE_STRING, /* COL_STATE_ICON_NAME */
151 MC_TYPE_PRESENCE, /* COL_STATE */
152 G_TYPE_STRING, /* COL_STATUS_TEXT */
153 G_TYPE_STRING, /* COL_DISPLAY_MARKUP */
154 G_TYPE_BOOLEAN, /* COL_STATUS_CUSTOMISABLE */
155 G_TYPE_INT); /* COL_TYPE */
160 for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
163 const char *status = empathy_presence_get_default_message (states[i]);
164 const char *icon_name = empathy_icon_name_for_presence (states[i]);
166 gtk_list_store_append (store, &iter);
167 gtk_list_store_set (store, &iter,
168 COL_STATE_ICON_NAME, icon_name,
169 COL_STATE, states[i],
170 COL_STATUS_TEXT, status,
171 COL_DISPLAY_MARKUP, status,
172 COL_STATUS_CUSTOMISABLE, states[i+1],
173 COL_TYPE, ENTRY_TYPE_BUILTIN,
177 /* Set custom messages if wanted */
178 list = empathy_status_presets_get (states[i], 5);
179 for (l = list; l; l = l->next) {
180 gtk_list_store_append (store, &iter);
181 gtk_list_store_set (store, &iter,
182 COL_STATE_ICON_NAME, icon_name,
183 COL_STATE, states[i],
184 COL_STATUS_TEXT, l->data,
185 COL_DISPLAY_MARKUP, l->data,
186 COL_STATUS_CUSTOMISABLE, TRUE,
187 COL_TYPE, ENTRY_TYPE_SAVED,
192 gtk_list_store_append (store, &iter);
193 gtk_list_store_set (store, &iter,
194 COL_STATE_ICON_NAME, icon_name,
195 COL_STATE, states[i],
197 COL_DISPLAY_MARKUP, "<i>Custom Message...</i>",
198 COL_STATUS_CUSTOMISABLE, TRUE,
199 COL_TYPE, ENTRY_TYPE_CUSTOM,
205 /* add a separator */
206 gtk_list_store_append (store, &iter);
207 gtk_list_store_set (store, &iter,
208 COL_TYPE, ENTRY_TYPE_SEPARATOR,
211 gtk_list_store_append (store, &iter);
212 gtk_list_store_set (store, &iter,
213 COL_STATE_ICON_NAME, GTK_STOCK_EDIT,
215 COL_DISPLAY_MARKUP, "Edit Custom Messages...",
216 COL_TYPE, ENTRY_TYPE_EDIT_CUSTOM,
219 return GTK_TREE_MODEL (store);
223 popup_shown_cb (GObject *self, GParamSpec *pspec, gpointer user_data)
226 g_object_get (self, "popup-shown", &shown, NULL);
230 GtkTreeModel *model = create_model ();
232 gtk_combo_box_set_model (GTK_COMBO_BOX (self), GTK_TREE_MODEL (model));
234 g_object_unref (model);
238 set_status_editing (EmpathyPresenceChooser *self, gboolean editing)
240 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
241 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
243 if (priv->block_set_editing) return;
247 priv->editing_status = TRUE;
248 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
249 GTK_ENTRY_ICON_SECONDARY,
251 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
252 GTK_ENTRY_ICON_SECONDARY,
254 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
255 GTK_ENTRY_ICON_PRIMARY,
260 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
261 GTK_ENTRY_ICON_SECONDARY,
263 gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
264 GTK_ENTRY_ICON_SECONDARY,
266 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
267 GTK_ENTRY_ICON_PRIMARY,
270 /* attempt to get the toplevel for this widget */
271 GtkWidget *window = gtk_widget_get_toplevel (GTK_WIDGET (self));
272 if (GTK_WIDGET_TOPLEVEL (window) && GTK_IS_WINDOW (window))
274 /* unset the focus */
275 gtk_window_set_focus (GTK_WINDOW (window), NULL);
277 gtk_editable_set_position (GTK_EDITABLE (entry), 0);
279 priv->editing_status = FALSE;
284 mc_set_custom_state (EmpathyPresenceChooser *self)
286 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
287 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
289 /* update the status with MC */
290 const char *status = gtk_entry_get_text (GTK_ENTRY (entry));
291 DEBUG ("Sending state to MC-> %s (%s)\n",
292 g_enum_get_value (g_type_class_peek (MC_TYPE_PRESENCE),
293 priv->state)->value_name,
295 empathy_idle_set_presence (priv->idle, priv->state, status);
299 ui_set_custom_state (EmpathyPresenceChooser *self,
303 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
304 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
305 const char *icon_name;
307 priv->block_set_editing++;
308 priv->block_changed++;
310 icon_name = empathy_icon_name_for_presence (state);
311 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
312 GTK_ENTRY_ICON_PRIMARY,
314 gtk_entry_set_text (GTK_ENTRY (entry), status);
316 priv->block_changed--;
317 priv->block_set_editing--;
321 entry_icon_release_cb (EmpathyPresenceChooser *self,
322 GtkEntryIconPosition icon_pos,
326 set_status_editing (self, FALSE);
327 mc_set_custom_state (self);
331 entry_activate_cb (EmpathyPresenceChooser *self,
334 set_status_editing (self, FALSE);
335 mc_set_custom_state (self);
339 entry_key_press_event_cb (EmpathyPresenceChooser *self,
343 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
345 if (priv->editing_status && event->keyval == GDK_Escape)
347 /* the user pressed Escape, undo the editing */
348 set_status_editing (self, FALSE);
349 presence_chooser_presence_changed_cb (self);
354 return FALSE; /* send this event elsewhere */
358 changed_cb (GtkComboBox *self, gpointer user_data)
360 EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
362 if (priv->block_changed) return;
366 gboolean customisable = TRUE;
369 GtkTreeModel *model = gtk_combo_box_get_model (self);
370 if (!gtk_combo_box_get_active_iter (self, &iter))
372 /* the combo is being edited to a custom entry */
373 if (!priv->editing_status)
375 set_status_editing (EMPATHY_PRESENCE_CHOOSER (self), TRUE);
380 gtk_tree_model_get (model, &iter,
381 COL_STATE_ICON_NAME, &icon_name,
382 COL_STATE, &priv->state,
383 COL_STATUS_CUSTOMISABLE, &customisable,
387 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
389 /* some types of status aren't editable, set the editability of the
390 * entry appropriately. Unless we're just about to reset it anyway,
391 * in which case, don't fiddle with it */
392 /* FIXME: there is a bug here, if we start in the Hidden state, it
393 * will be editable. It's not something that often occurs though. */
394 if (type != ENTRY_TYPE_EDIT_CUSTOM)
396 gtk_editable_set_editable (GTK_EDITABLE (entry), customisable);
399 if (type == ENTRY_TYPE_EDIT_CUSTOM)
401 /* recover the status that was unset because COL_STATUS_TEXT
402 * is "". Unfortunately if you try and set COL_STATUS_TEXT to
403 * NULL, it generates a g_critical. I wonder if there is a
404 * better way around this. */
405 const char *status = empathy_idle_get_status (priv->idle);
406 priv->block_set_editing++;
407 gtk_entry_set_text (GTK_ENTRY (entry), status);
408 priv->block_set_editing--;
410 /* attempt to get the toplevel for this widget */
411 GtkWidget *window = gtk_widget_get_toplevel (GTK_WIDGET (self));
412 if (!GTK_WIDGET_TOPLEVEL (window) || !GTK_IS_WINDOW (window))
417 presence_chooser_dialog_show (GTK_WINDOW (window));
419 else if (type == ENTRY_TYPE_CUSTOM)
421 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
422 GTK_ENTRY_ICON_PRIMARY,
425 /* preseed the status */
426 const char *status = empathy_idle_get_status (priv->idle);
427 priv->block_set_editing++;
428 gtk_entry_set_text (GTK_ENTRY (entry), status);
429 priv->block_set_editing--;
432 gtk_widget_grab_focus (entry);
434 set_status_editing (EMPATHY_PRESENCE_CHOOSER (self), TRUE);
439 /* just in case we were setting a new status when
440 * things were changed */
441 set_status_editing (EMPATHY_PRESENCE_CHOOSER (self), FALSE);
443 gtk_tree_model_get (model, &iter,
444 COL_STATUS_TEXT, &status,
447 empathy_idle_set_presence (priv->idle, priv->state, status);
456 combo_row_separator_func (GtkTreeModel *model,
461 gtk_tree_model_get (model, iter,
465 return (type == ENTRY_TYPE_SEPARATOR);
469 focus_out_cb (EmpathyPresenceChooser *chooser, GdkEventFocus *event,
472 EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
474 if (priv->editing_status)
476 entry_activate_cb (chooser, entry);
483 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
485 EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
486 EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
488 chooser->priv = priv;
490 GtkTreeModel *model = create_model ();
492 gtk_combo_box_set_model (GTK_COMBO_BOX (chooser), GTK_TREE_MODEL (model));
493 gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (chooser),
495 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
496 combo_row_separator_func,
499 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
500 gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
501 GTK_ENTRY_ICON_PRIMARY, FALSE);
502 g_signal_connect_object (entry, "icon-release",
503 G_CALLBACK (entry_icon_release_cb), chooser,
505 g_signal_connect_object (entry, "activate",
506 G_CALLBACK (entry_activate_cb), chooser,
508 g_signal_connect_object (entry, "key-press-event",
509 G_CALLBACK (entry_key_press_event_cb), chooser,
511 // FIXME - should this also happen when the user presses TAB ?
513 GtkCellRenderer *renderer;
514 gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
516 renderer = gtk_cell_renderer_pixbuf_new ();
517 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, FALSE);
518 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
519 "icon-name", COL_STATE_ICON_NAME,
521 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
523 renderer = gtk_cell_renderer_text_new ();
524 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, TRUE);
525 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
526 "markup", COL_DISPLAY_MARKUP,
529 g_object_unref (model);
531 g_signal_connect (chooser, "notify::popup-shown",
532 G_CALLBACK (popup_shown_cb), NULL);
533 g_signal_connect (chooser, "changed",
534 G_CALLBACK (changed_cb), NULL);
535 g_signal_connect_swapped (entry, "changed",
536 G_CALLBACK (changed_cb), chooser);
537 g_signal_connect_swapped (entry, "focus-out-event",
538 G_CALLBACK (focus_out_cb), chooser);
540 priv->idle = empathy_idle_dup_singleton ();
541 presence_chooser_presence_changed_cb (chooser);
542 g_signal_connect_swapped (priv->idle, "notify",
543 G_CALLBACK (presence_chooser_presence_changed_cb),
546 g_object_set (chooser,
547 // FIXME: this string sucks
548 "tooltip-text", _("Set your presence and current status"),
553 presence_chooser_finalize (GObject *object)
555 EmpathyPresenceChooserPriv *priv;
557 priv = GET_PRIV (object);
559 if (priv->flash_timeout_id) {
560 g_source_remove (priv->flash_timeout_id);
563 g_signal_handlers_disconnect_by_func (priv->idle,
564 presence_chooser_presence_changed_cb,
566 g_object_unref (priv->idle);
568 G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
572 empathy_presence_chooser_new (void)
576 chooser = g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER, NULL);
582 presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
584 EmpathyPresenceChooserPriv *priv;
586 McPresence flash_state;
589 priv = GET_PRIV (chooser);
591 priv->state = state = empathy_idle_get_state (priv->idle);
592 status = empathy_idle_get_status (priv->idle);
593 flash_state = empathy_idle_get_flash_state (priv->idle);
595 /* look through the model and attempt to find a matching state */
596 GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
598 gboolean valid, match_state = FALSE, match = FALSE;
599 for (valid = gtk_tree_model_get_iter_first (model, &iter);
601 valid = gtk_tree_model_iter_next (model, &iter))
607 gtk_tree_model_get (model, &iter,
612 if (m_type == ENTRY_TYPE_CUSTOM ||
613 m_type == ENTRY_TYPE_SEPARATOR ||
614 m_type == ENTRY_TYPE_EDIT_CUSTOM)
618 else if (!match_state && state == m_state)
620 /* we are now in the section that can contain our
624 else if (match_state && state != m_state)
626 /* we have passed the section that can contain our
631 gtk_tree_model_get (model, &iter,
632 COL_STATUS_TEXT, &m_status,
635 match = !strcmp (status, m_status);
645 priv->block_changed++;
646 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
647 priv->block_changed--;
651 // FIXME - do we insert the match into the menu?
652 ui_set_custom_state (chooser, state, status);
655 if (flash_state != MC_PRESENCE_UNSET) {
656 presence_chooser_flash_start (chooser, state, flash_state);
658 presence_chooser_flash_stop (chooser, state);
663 presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser)
665 EmpathyPresenceChooserPriv *priv;
667 static gboolean on = FALSE;
669 priv = GET_PRIV (chooser);
672 state = priv->flash_state_1;
674 state = priv->flash_state_2;
677 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
678 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
679 GTK_ENTRY_ICON_PRIMARY,
680 empathy_icon_name_for_presence (state));
688 presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
692 EmpathyPresenceChooserPriv *priv;
694 g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
696 priv = GET_PRIV (chooser);
698 priv->flash_state_1 = state_1;
699 priv->flash_state_2 = state_2;
701 if (!priv->flash_timeout_id) {
702 priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
703 (GSourceFunc) presence_chooser_flash_timeout_cb,
709 presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
712 EmpathyPresenceChooserPriv *priv;
714 g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
716 priv = GET_PRIV (chooser);
718 if (priv->flash_timeout_id) {
719 g_source_remove (priv->flash_timeout_id);
720 priv->flash_timeout_id = 0;
722 GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
724 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
725 GTK_ENTRY_ICON_PRIMARY,
726 empathy_icon_name_for_presence (state));
728 // FIXME - what does this do?
729 // priv->last_state = state;
733 empathy_presence_chooser_create_menu (void)
741 menu = gtk_menu_new ();
743 for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
746 status = empathy_presence_get_default_message (states[i]);
747 presence_chooser_menu_add_item (menu,
752 /* Set custom messages if wanted */
753 list = empathy_status_presets_get (states[i], 5);
754 for (l = list; l; l = l->next) {
755 presence_chooser_menu_add_item (menu,
765 item = gtk_menu_item_new ();
766 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
767 gtk_widget_show (item);
769 /* Custom messages */
770 item = gtk_image_menu_item_new_with_label (_("Custom messages..."));
771 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
772 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
773 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
774 gtk_widget_show (image);
775 gtk_widget_show (item);
777 g_signal_connect (item,
779 G_CALLBACK (presence_chooser_custom_activate_cb),
786 presence_chooser_menu_add_item (GtkWidget *menu,
792 const gchar *icon_name;
794 item = gtk_image_menu_item_new_with_label (str);
795 icon_name = empathy_icon_name_for_presence (state);
797 g_signal_connect (item, "activate",
798 G_CALLBACK (presence_chooser_noncustom_activate_cb),
801 image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
802 gtk_widget_show (image);
804 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
805 gtk_widget_show (item);
807 g_object_set_data_full (G_OBJECT (item),
808 "status", g_strdup (str),
809 (GDestroyNotify) g_free);
811 g_object_set_data (G_OBJECT (item), "state", GINT_TO_POINTER (state));
813 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
817 presence_chooser_noncustom_activate_cb (GtkWidget *item,
823 status = g_object_get_data (G_OBJECT (item), "status");
824 state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "state"));
826 presence_chooser_set_state (state, status);
830 presence_chooser_set_state (McPresence state,
835 idle = empathy_idle_dup_singleton ();
836 empathy_idle_set_presence (idle, state, status);
837 g_object_unref (idle);
841 presence_chooser_custom_activate_cb (GtkWidget *item,
844 presence_chooser_dialog_show (NULL);
848 presence_chooser_dialog_get_selected (CustomMessageDialog *dialog)
852 McPresence presence = LAST_MC_PRESENCE;
854 model = gtk_combo_box_get_model (GTK_COMBO_BOX (dialog->combobox_status));
855 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (dialog->combobox_status), &iter)) {
856 gtk_tree_model_get (model, &iter,
857 COL_PRESENCE, &presence,
865 presence_chooser_dialog_status_changed_cb (GtkWidget *widget,
866 CustomMessageDialog *dialog)
870 McPresence presence = LAST_MC_PRESENCE;
873 presence = presence_chooser_dialog_get_selected (dialog);
875 store = gtk_list_store_new (1, G_TYPE_STRING);
876 messages = empathy_status_presets_get (presence, -1);
877 for (l = messages; l; l = l->next) {
878 gtk_list_store_append (store, &iter);
879 gtk_list_store_set (store, &iter, 0, l->data, -1);
882 gtk_entry_set_text (GTK_ENTRY (dialog->entry_message),
883 messages ? messages->data : "");
885 g_list_free (messages);
887 gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->comboboxentry_message),
888 GTK_TREE_MODEL (store));
890 g_object_unref (store);
894 presence_chooser_dialog_message_changed_cb (GtkWidget *widget,
895 CustomMessageDialog *dialog)
900 gboolean found = FALSE;
902 presence = presence_chooser_dialog_get_selected (dialog);
903 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
905 messages = empathy_status_presets_get (presence, -1);
906 for (l = messages; l; l = l->next) {
907 if (!tp_strdiff (text, l->data)) {
912 g_list_free (messages);
914 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_save),
919 presence_chooser_dialog_save_toggled_cb (GtkWidget *widget,
920 CustomMessageDialog *dialog)
926 active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_save));
927 state = presence_chooser_dialog_get_selected (dialog);
928 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
931 empathy_status_presets_set_last (state, text);
933 empathy_status_presets_remove (state, text);
938 presence_chooser_dialog_setup (CustomMessageDialog *dialog)
941 GtkCellRenderer *renderer;
945 store = gtk_list_store_new (COL_COUNT,
946 G_TYPE_STRING, /* Icon name */
947 G_TYPE_STRING, /* Label */
948 MC_TYPE_PRESENCE); /* Presence */
949 gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->combobox_status),
950 GTK_TREE_MODEL (store));
952 renderer = gtk_cell_renderer_pixbuf_new ();
953 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (dialog->combobox_status), renderer, FALSE);
954 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combobox_status), renderer,
955 "icon-name", COL_ICON,
957 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
959 renderer = gtk_cell_renderer_text_new ();
960 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (dialog->combobox_status), renderer, TRUE);
961 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combobox_status), renderer,
965 for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
970 gtk_list_store_append (store, &iter);
971 gtk_list_store_set (store, &iter,
972 COL_ICON, empathy_icon_name_for_presence (states[i]),
973 COL_LABEL, empathy_presence_get_default_message (states[i]),
974 COL_PRESENCE, states[i],
978 gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combobox_status), 0);
982 presence_chooser_dialog_response_cb (GtkWidget *widget,
984 CustomMessageDialog *dialog)
986 if (response == GTK_RESPONSE_APPLY) {
990 state = presence_chooser_dialog_get_selected (dialog);
991 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_message));
993 presence_chooser_set_state (state, text);
996 gtk_widget_destroy (widget);
1000 presence_chooser_dialog_destroy_cb (GtkWidget *widget,
1001 CustomMessageDialog *dialog)
1005 message_dialog = NULL;
1009 presence_chooser_dialog_show (GtkWindow *parent)
1014 if (message_dialog) {
1015 gtk_window_present (GTK_WINDOW (message_dialog->dialog));
1019 message_dialog = g_new0 (CustomMessageDialog, 1);
1021 filename = empathy_file_lookup ("empathy-presence-chooser.glade",
1023 glade = empathy_glade_get_file (filename,
1024 "custom_message_dialog",
1026 "custom_message_dialog", &message_dialog->dialog,
1027 "checkbutton_save", &message_dialog->checkbutton_save,
1028 "comboboxentry_message", &message_dialog->comboboxentry_message,
1029 "combobox_status", &message_dialog->combobox_status,
1033 empathy_glade_connect (glade,
1035 "custom_message_dialog", "destroy", presence_chooser_dialog_destroy_cb,
1036 "custom_message_dialog", "response", presence_chooser_dialog_response_cb,
1037 "combobox_status", "changed", presence_chooser_dialog_status_changed_cb,
1038 "checkbutton_save", "toggled", presence_chooser_dialog_save_toggled_cb,
1041 g_object_unref (glade);
1043 /* Setup the message combobox */
1044 message_dialog->entry_message = GTK_BIN (message_dialog->comboboxentry_message)->child;
1045 gtk_entry_set_activates_default (GTK_ENTRY (message_dialog->entry_message), TRUE);
1046 gtk_entry_set_width_chars (GTK_ENTRY (message_dialog->entry_message), 25);
1047 g_signal_connect (message_dialog->entry_message, "changed",
1048 G_CALLBACK (presence_chooser_dialog_message_changed_cb),
1051 presence_chooser_dialog_setup (message_dialog);
1053 gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (message_dialog->comboboxentry_message), 0);
1057 gtk_window_set_transient_for (
1058 GTK_WINDOW (message_dialog->dialog),
1062 gtk_widget_show_all (message_dialog->dialog);