]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-presence-chooser.c
Coding style fixes
[empathy.git] / libempathy-gtk / empathy-presence-chooser.c
index 54be5083ec288d874108b9a4411931e2581a6a48..672cd981a5d6cb0868eb0ecff283de806f23a801 100644 (file)
@@ -1,6 +1,7 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 /*
  * Copyright (C) 2005-2007 Imendio AB
+ * Copyright (C) 2009 Collabora Ltd.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -20,6 +21,7 @@
  * Authors: Richard Hult <richard@imendio.com>
  *          Martyn Russell <martyn@imendio.com>
  *          Xavier Claessens <xclaesse@gmail.com>
+ *          Davyd Madeley <davyd.madeley@collabora.co.uk>
  */
 
 #include "config.h"
@@ -30,6 +32,7 @@
 #include <glib/gi18n-lib.h>
 #include <gtk/gtk.h>
 #include <glade/glade.h>
+#include <gdk/gdkkeysyms.h>
 
 #include <telepathy-glib/util.h>
 #include <libmissioncontrol/mc-enum-types.h>
@@ -38,6 +41,9 @@
 #include <libempathy/empathy-utils.h>
 #include <libempathy/empathy-status-presets.h>
 
+#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
+#include <libempathy/empathy-debug.h>
+
 #include "empathy-ui-utils.h"
 #include "empathy-images.h"
 #include "empathy-presence-chooser.h"
 typedef struct {
        EmpathyIdle *idle;
 
+       gboolean     editing_status;
        int          block_set_editing;
        int          block_changed;
+       guint        focus_out_idle_source;
 
        McPresence   state;
+       int          previous_type;
 
        McPresence   flash_state_1;
        McPresence   flash_state_2;
@@ -101,7 +110,7 @@ static void            presence_chooser_set_state              (McPresence
                                                                const gchar                *status);
 static void            presence_chooser_custom_activate_cb     (GtkWidget                  *item,
                                                                gpointer                    user_data);
-static void            presence_chooser_dialog_show            (void);
+static void            presence_chooser_dialog_show            (GtkWindow                  *parent);
 
 G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_COMBO_BOX_ENTRY);
 
@@ -121,6 +130,7 @@ enum
        COL_STATE,
        COL_STATUS_TEXT,
        COL_DISPLAY_MARKUP,
+       COL_STATUS_CUSTOMISABLE,
        COL_TYPE,
        N_COLUMNS
 };
@@ -130,135 +140,199 @@ enum
        ENTRY_TYPE_BUILTIN,
        ENTRY_TYPE_SAVED,
        ENTRY_TYPE_CUSTOM,
+       ENTRY_TYPE_SEPARATOR,
+       ENTRY_TYPE_EDIT_CUSTOM,
 };
 
 static GtkTreeModel *
-create_model (void)
+presence_chooser_create_model (void)
 {
-       GtkListStore *store = gtk_list_store_new (N_COLUMNS,
+       GtkListStore *store;
+       char *custom_message;
+
+       store = gtk_list_store_new (N_COLUMNS,
                        G_TYPE_STRING,          /* COL_STATE_ICON_NAME */
                        MC_TYPE_PRESENCE,       /* COL_STATE */
                        G_TYPE_STRING,          /* COL_STATUS_TEXT */
                        G_TYPE_STRING,          /* COL_DISPLAY_MARKUP */
+                       G_TYPE_BOOLEAN,         /* COL_STATUS_CUSTOMISABLE */
                        G_TYPE_INT);            /* COL_TYPE */
-       
-       GtkTreeIter iter;
-       
+
+       custom_message = g_strdup_printf ("<i>%s</i>", _("Custom Message..."));
+
        int i;
        for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
                GList       *list, *l;
+               const char *status, *icon_name;
 
-               const char *status = empathy_presence_get_default_message (states[i]);
-               const char *icon_name = empathy_icon_name_for_presence (states[i]);
+               status = empathy_presence_get_default_message (states[i]);
+               icon_name = empathy_icon_name_for_presence (states[i]);
 
-               gtk_list_store_append (store, &iter);
-               gtk_list_store_set (store, &iter,
+               gtk_list_store_insert_with_values (store, NULL, -1,
                                COL_STATE_ICON_NAME, icon_name,
                                COL_STATE, states[i],
                                COL_STATUS_TEXT, status,
                                COL_DISPLAY_MARKUP, status,
+                               COL_STATUS_CUSTOMISABLE, states[i+1],
                                COL_TYPE, ENTRY_TYPE_BUILTIN,
                                -1);
 
                if (states[i+1]) {
+
                        /* Set custom messages if wanted */
                        list = empathy_status_presets_get (states[i], 5);
                        for (l = list; l; l = l->next) {
-                               gtk_list_store_append (store, &iter);
-                               gtk_list_store_set (store, &iter,
+                               gtk_list_store_insert_with_values (store,
+                                               NULL, -1,
                                                COL_STATE_ICON_NAME, icon_name,
                                                COL_STATE, states[i],
                                                COL_STATUS_TEXT, l->data,
                                                COL_DISPLAY_MARKUP, l->data,
+                                               COL_STATUS_CUSTOMISABLE, TRUE,
                                                COL_TYPE, ENTRY_TYPE_SAVED,
                                                -1);
                        }
                        g_list_free (list);
-               
-                       gtk_list_store_append (store, &iter);
-                       gtk_list_store_set (store, &iter,
+
+                       gtk_list_store_insert_with_values (store, NULL, -1,
                                        COL_STATE_ICON_NAME, icon_name,
                                        COL_STATE, states[i],
                                        COL_STATUS_TEXT, "",
-                                       COL_DISPLAY_MARKUP, "<i>Custom Message...</i>",
+                                       COL_DISPLAY_MARKUP, custom_message,
+                                       COL_STATUS_CUSTOMISABLE, TRUE,
                                        COL_TYPE, ENTRY_TYPE_CUSTOM,
                                        -1);
                }
 
        }
 
+       /* add a separator */
+       gtk_list_store_insert_with_values (store, NULL, -1,
+                       COL_TYPE, ENTRY_TYPE_SEPARATOR,
+                       -1);
+
+       gtk_list_store_insert_with_values (store, NULL, -1,
+                       COL_STATE_ICON_NAME, GTK_STOCK_EDIT,
+                       COL_STATUS_TEXT, "",
+                       COL_DISPLAY_MARKUP, _("Edit Custom Messages..."),
+                       COL_TYPE, ENTRY_TYPE_EDIT_CUSTOM,
+                       -1);
+
+       g_free (custom_message);
+
        return GTK_TREE_MODEL (store);
 }
 
 static void
-popup_shown_cb (GObject *self, GParamSpec *pspec, gpointer user_data)
+presence_chooser_popup_shown_cb (GObject *self,
+                                 GParamSpec *pspec,
+                                gpointer user_data)
 {
+       EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
        gboolean shown;
+
        g_object_get (self, "popup-shown", &shown, NULL);
 
        if (!shown) return;
 
-       g_print ("popup shown\n");
+       /* see presence_chooser_entry_focus_out_cb() for what this does */
+       if (priv->focus_out_idle_source != 0)
+       {
+               g_source_remove (priv->focus_out_idle_source);
+               priv->focus_out_idle_source = 0;
+       }
 
-       GtkTreeModel *model = create_model ();
+       GtkTreeModel *model = presence_chooser_create_model ();
 
        gtk_combo_box_set_model (GTK_COMBO_BOX (self), GTK_TREE_MODEL (model));
-       
+
        g_object_unref (model);
 }
 
 static void
-set_status_editing (EmpathyPresenceChooser *self, gboolean editing)
+presence_chooser_set_status_editing (EmpathyPresenceChooser *self,
+                                     gboolean editing)
 {
        EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
        GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
 
        if (priv->block_set_editing) return;
 
-       if (editing)
-       {
+       if (editing) {
+               priv->editing_status = TRUE;
+
                gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
                                GTK_ENTRY_ICON_SECONDARY,
                                GTK_STOCK_OK);
                gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
                                GTK_ENTRY_ICON_SECONDARY,
-                               "Set status");
-               // FIXME - is this nice?
+                               _("Set status"));
                gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
                                GTK_ENTRY_ICON_PRIMARY,
                                FALSE);
        }
-       else
-       {
+       else {
                gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
                                GTK_ENTRY_ICON_SECONDARY,
                                NULL);
                gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
                                GTK_ENTRY_ICON_SECONDARY,
                                NULL);
-               // FIXME - also this
                gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
                                GTK_ENTRY_ICON_PRIMARY,
                                TRUE);
 
-               // FIXME - move the focus somewhere
+               /* attempt to get the toplevel for this widget */
+               GtkWidget *window = gtk_widget_get_toplevel (GTK_WIDGET (self));
+               if (GTK_WIDGET_TOPLEVEL (window) && GTK_IS_WINDOW (window)) {
+                       /* unset the focus */
+                       gtk_window_set_focus (GTK_WINDOW (window), NULL);
+               }
 
-               /* update the status with MC */
-               const char *status = gtk_entry_get_text (GTK_ENTRY (entry));
-               empathy_idle_set_presence (priv->idle, priv->state, status);
+               /* see presence_chooser_entry_focus_out_cb()
+                * for what this does */
+               if (priv->focus_out_idle_source != 0) {
+                       g_source_remove (priv->focus_out_idle_source);
+                       priv->focus_out_idle_source = 0;
+               }
+
+               gtk_editable_set_position (GTK_EDITABLE (entry), 0);
+
+               priv->editing_status = FALSE;
        }
 }
 
+static void
+mc_set_custom_state (EmpathyPresenceChooser *self)
+{
+       EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
+       GtkWidget *entry;
+       const char *status;
+
+       entry = gtk_bin_get_child (GTK_BIN (self));
+       /* update the status with MC */
+       status = gtk_entry_get_text (GTK_ENTRY (entry));
+
+       DEBUG ("Sending state to MC-> %s (%s)\n",
+                       g_enum_get_value (g_type_class_peek (MC_TYPE_PRESENCE),
+                               priv->state)->value_name,
+                       status);
+       empathy_idle_set_presence (priv->idle, priv->state, status);
+}
+
 static void
 ui_set_custom_state (EmpathyPresenceChooser *self,
                           McPresence state,
                           const char *status)
 {
        EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
-       GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
+       GtkWidget *entry;
        const char *icon_name;
 
+       entry = gtk_bin_get_child (GTK_BIN (self));
+
        priv->block_set_editing++;
+       priv->block_changed++;
 
        icon_name = empathy_icon_name_for_presence (state);
        gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
@@ -266,71 +340,167 @@ ui_set_custom_state (EmpathyPresenceChooser *self,
                        icon_name);
        gtk_entry_set_text (GTK_ENTRY (entry), status);
 
+       priv->block_changed--;
        priv->block_set_editing--;
 }
 
 static void
-entry_icon_release_cb (EmpathyPresenceChooser  *self,
-                      GtkEntryIconPosition      icon_pos,
-                      GdkEvent         *event,
-                      GtkEntry         *entry)
+presence_chooser_reset_status (EmpathyPresenceChooser *self)
 {
-       set_status_editing (self, FALSE);
+       /* recover the status that was unset */
+       presence_chooser_presence_changed_cb (self);
+       presence_chooser_set_status_editing (self, FALSE);
 }
 
 static void
-entry_activate_cb (EmpathyPresenceChooser      *self,
-                  GtkEntry                     *entry)
+presence_chooser_entry_icon_release_cb (EmpathyPresenceChooser *self,
+                                        GtkEntryIconPosition    icon_pos,
+                                        GdkEvent               *event,
+                                       GtkEntry                *entry)
 {
-       g_print ("ACTIVATE! (form of a dragon)\n");
-       
-       set_status_editing (self, FALSE);
+       presence_chooser_set_status_editing (self, FALSE);
+       mc_set_custom_state (self);
 }
 
 static void
-changed_cb (GtkComboBox *self, gpointer user_data)
+presence_chooser_entry_activate_cb (EmpathyPresenceChooser     *self,
+                                    GtkEntry                   *entry)
+{
+       presence_chooser_set_status_editing (self, FALSE);
+       mc_set_custom_state (self);
+}
+
+static gboolean
+presence_chooser_entry_key_press_event_cb (EmpathyPresenceChooser      *self,
+                                           GdkEventKey                 *event,
+                                          GtkWidget                    *entry)
+{
+       EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
+
+       if (priv->editing_status && event->keyval == GDK_Escape) {
+               /* the user pressed Escape, undo the editing */
+               presence_chooser_reset_status (self);
+
+               return TRUE;
+       }
+       else if (event->keyval == GDK_Up || event->keyval == GDK_Down) {
+               /* ignore */
+               return TRUE;
+       }
+
+       return FALSE; /* send this event elsewhere */
+}
+
+static gboolean
+presence_chooser_entry_button_press_event_cb (EmpathyPresenceChooser *self,
+                                              GdkEventButton         *event,
+                                             GtkWidget              *entry)
+{
+       EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
+
+       if (!priv->editing_status &&
+           event->button == 1 &&
+           !GTK_WIDGET_HAS_FOCUS (entry)) {
+               gtk_widget_grab_focus (entry);
+               gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
+
+               return TRUE;
+       }
+
+       return FALSE;
+}
+
+static void
+presence_chooser_entry_changed_cb (EmpathyPresenceChooser *self,
+                                  GtkEntry               *entry)
 {
        EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
 
        if (priv->block_changed) return;
 
-       g_print ("Changed\n");
+       /* the combo is being edited to a custom entry */
+       if (!priv->editing_status) {
+               presence_chooser_set_status_editing (self, TRUE);
+       }
+}
+
+static void
+presence_chooser_changed_cb (GtkComboBox *self, gpointer user_data)
+{
+       EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
+
+       if (priv->block_changed) return;
 
        GtkTreeIter iter;
        char *icon_name;
+       McPresence new_state;
+       gboolean customisable = TRUE;
        int type = -1;
+       GtkWidget *entry;
 
        GtkTreeModel *model = gtk_combo_box_get_model (self);
-       if (!gtk_combo_box_get_active_iter (self, &iter))
-       {
-               /* the combo is being edited to a custom entry */
-               set_status_editing (EMPATHY_PRESENCE_CHOOSER (self), TRUE);
+       if (!gtk_combo_box_get_active_iter (self, &iter)) {
                return;
        }
 
        gtk_tree_model_get (model, &iter,
                        COL_STATE_ICON_NAME, &icon_name,
-                       COL_STATE, &priv->state,
+                       COL_STATE, &new_state,
+                       COL_STATUS_CUSTOMISABLE, &customisable,
                        COL_TYPE, &type,
                        -1);
 
-       GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
-       gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
-                       GTK_ENTRY_ICON_PRIMARY,
-                       icon_name);
+       entry = gtk_bin_get_child (GTK_BIN (self));
+
+       /* some types of status aren't editable, set the editability of the
+        * entry appropriately. Unless we're just about to reset it anyway,
+        * in which case, don't fiddle with it */
+       if (type != ENTRY_TYPE_EDIT_CUSTOM) {
+               gtk_editable_set_editable (GTK_EDITABLE (entry), customisable);
+               priv->state = new_state;
+       }
+
+       if (type == ENTRY_TYPE_EDIT_CUSTOM) {
+               presence_chooser_reset_status (EMPATHY_PRESENCE_CHOOSER (self));
+
+               /* attempt to get the toplevel for this widget */
+               GtkWidget *window = gtk_widget_get_toplevel (GTK_WIDGET (self));
+               if (!GTK_WIDGET_TOPLEVEL (window) || !GTK_IS_WINDOW (window)) {
+                       window = NULL;
+               }
+
+               presence_chooser_dialog_show (GTK_WINDOW (window));
+       }
+       else if (type == ENTRY_TYPE_CUSTOM) {
+               gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
+                               GTK_ENTRY_ICON_PRIMARY,
+                               icon_name);
+
+               /* preseed the status */
+               if (priv->previous_type == ENTRY_TYPE_BUILTIN) {
+                       /* if their previous entry was a builtin, don't
+                        * preseed */
+                       gtk_entry_set_text (GTK_ENTRY (entry), "");
+               }
+               else {
+                       /* else preseed the text of their currently entered
+                        * status message */
+                       const char *status;
+
+                       status = empathy_idle_get_status (priv->idle);
+                       gtk_entry_set_text (GTK_ENTRY (entry), status);
+               }
 
-       if (type == ENTRY_TYPE_CUSTOM)
-       {
                /* grab the focus */
                gtk_widget_grab_focus (entry);
-               set_status_editing (EMPATHY_PRESENCE_CHOOSER (self), TRUE);
        }
-       else
-       {
+       else {
                char *status;
                /* just in case we were setting a new status when
                 * things were changed */
-               set_status_editing (EMPATHY_PRESENCE_CHOOSER (self), FALSE);
+               presence_chooser_set_status_editing (
+                               EMPATHY_PRESENCE_CHOOSER (self),
+                               FALSE);
 
                gtk_tree_model_get (model, &iter,
                                COL_STATUS_TEXT, &status,
@@ -341,36 +511,111 @@ changed_cb (GtkComboBox *self, gpointer user_data)
                g_free (status);
        }
 
+       if (type != ENTRY_TYPE_EDIT_CUSTOM) {
+               priv->previous_type = type;
+       }
        g_free (icon_name);
 }
 
+static gboolean
+combo_row_separator_func (GtkTreeModel *model,
+                         GtkTreeIter   *iter,
+                         gpointer       data)
+{
+       int type;
+       gtk_tree_model_get (model, iter,
+                       COL_TYPE, &type,
+                       -1);
+
+       return (type == ENTRY_TYPE_SEPARATOR);
+}
+
+static gboolean
+presence_chooser_entry_focus_out_idle_cb (gpointer user_data)
+{
+       EmpathyPresenceChooser *chooser;
+       GtkWidget *entry;
+
+       DEBUG ("Autocommiting status message\n");
+
+       chooser = EMPATHY_PRESENCE_CHOOSER (user_data);
+       entry = gtk_bin_get_child (GTK_BIN (chooser));
+
+       presence_chooser_entry_activate_cb (chooser, GTK_ENTRY (entry));
+
+       return FALSE;
+}
+
+static gboolean
+presence_chooser_entry_focus_out_cb (EmpathyPresenceChooser *chooser,
+                                     GdkEventFocus *event,
+                                    GtkEntry *entry)
+{
+       EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
+
+       if (priv->editing_status) {
+               /* this seems a bit evil and maybe it will be fragile,
+                * someone should think of a better way to do it.
+                *
+                * The entry has focused out, but we don't know where the focus
+                * has gone. If it goes to the combo box, we don't want to
+                * do anything. If it's gone anywhere else, we want to commit
+                * the result.
+                *
+                * Thus we install this idle handler and store its source.
+                * If the source is scheduled when the popup handler runs,
+                * it will remove it, else the callback will commit the result.
+                */
+               priv->focus_out_idle_source = g_idle_add (
+                               presence_chooser_entry_focus_out_idle_cb,
+                               chooser);
+       }
+
+       gtk_editable_set_position (GTK_EDITABLE (entry), 0);
+
+       return FALSE;
+}
+
 static void
 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
 {
-       //GtkWidget                  *arrow;
-       //GtkWidget                  *alignment;
        EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
                EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
-
+       GtkTreeModel *model;
+       GtkWidget *entry;
+       GtkCellRenderer *renderer;
 
        chooser->priv = priv;
-       
-       GtkTreeModel *model = create_model ();
+
+       model = presence_chooser_create_model ();
 
        gtk_combo_box_set_model (GTK_COMBO_BOX (chooser), GTK_TREE_MODEL (model));
-       gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (chooser), COL_STATUS_TEXT);
-       
-       GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
+       gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (chooser),
+                       COL_STATUS_TEXT);
+       gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
+                       combo_row_separator_func,
+                       NULL, NULL);
+
+       entry = gtk_bin_get_child (GTK_BIN (chooser));
        gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
                        GTK_ENTRY_ICON_PRIMARY, FALSE);
        g_signal_connect_object (entry, "icon-release",
-                       G_CALLBACK (entry_icon_release_cb), chooser,
+                       G_CALLBACK (presence_chooser_entry_icon_release_cb),
+                       chooser,
                        G_CONNECT_SWAPPED);
        g_signal_connect_object (entry, "activate",
-                       G_CALLBACK (entry_activate_cb), chooser,
+                       G_CALLBACK (presence_chooser_entry_activate_cb),
+                       chooser,
+                       G_CONNECT_SWAPPED);
+       g_signal_connect_object (entry, "key-press-event",
+                       G_CALLBACK (presence_chooser_entry_key_press_event_cb),
+                       chooser,
+                       G_CONNECT_SWAPPED);
+       g_signal_connect_object (entry, "button-press-event",
+                       G_CALLBACK (presence_chooser_entry_button_press_event_cb),
+                       chooser,
                        G_CONNECT_SWAPPED);
 
-       GtkCellRenderer *renderer;
        gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
 
        renderer = gtk_cell_renderer_pixbuf_new ();
@@ -389,15 +634,26 @@ empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
        g_object_unref (model);
 
        g_signal_connect (chooser, "notify::popup-shown",
-                       G_CALLBACK (popup_shown_cb), NULL);
+                       G_CALLBACK (presence_chooser_popup_shown_cb), NULL);
        g_signal_connect (chooser, "changed",
-                       G_CALLBACK (changed_cb), NULL);
+                       G_CALLBACK (presence_chooser_changed_cb), NULL);
+       g_signal_connect_swapped (entry, "changed",
+                       G_CALLBACK (presence_chooser_entry_changed_cb),
+                       chooser);
+       g_signal_connect_swapped (entry, "focus-out-event",
+                       G_CALLBACK (presence_chooser_entry_focus_out_cb),
+                       chooser);
 
        priv->idle = empathy_idle_dup_singleton ();
        presence_chooser_presence_changed_cb (chooser);
        g_signal_connect_swapped (priv->idle, "notify",
-                                 G_CALLBACK (presence_chooser_presence_changed_cb),
-                                 chooser);
+                       G_CALLBACK (presence_chooser_presence_changed_cb),
+                       chooser);
+
+       g_object_set (chooser,
+                       // FIXME: this string sucks
+                       "tooltip-text", _("Set your presence and current status"),
+                       NULL);
 }
 
 static void
@@ -411,6 +667,10 @@ presence_chooser_finalize (GObject *object)
                g_source_remove (priv->flash_timeout_id);
        }
 
+       if (priv->focus_out_idle_source) {
+               g_source_remove (priv->focus_out_idle_source);
+       }
+
        g_signal_handlers_disconnect_by_func (priv->idle,
                                              presence_chooser_presence_changed_cb,
                                              object);
@@ -435,26 +695,22 @@ presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
        EmpathyPresenceChooserPriv *priv;
        McPresence                 state;
        McPresence                 flash_state;
-       const gchar               *status;
-
-       g_print (" > presence_chooser_presence_changed_cb\n");
+       const gchar                *status;
+       GtkTreeModel               *model;
+       GtkTreeIter                iter;
+       gboolean valid, match_state = FALSE, match = FALSE;
 
        priv = GET_PRIV (chooser);
 
-       state = empathy_idle_get_state (priv->idle);
+       priv->state = state = empathy_idle_get_state (priv->idle);
        status = empathy_idle_get_status (priv->idle);
        flash_state = empathy_idle_get_flash_state (priv->idle);
 
-       g_print ("status = %s\n", status);
-
-       // FIXME - we need to either find an entry in the model or add one
-       GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
-       GtkTreeIter iter;
-       gboolean valid, match_state = FALSE, match = FALSE;
+       /* look through the model and attempt to find a matching state */
+       model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
        for (valid = gtk_tree_model_get_iter_first (model, &iter);
             valid;
-            valid = gtk_tree_model_iter_next (model, &iter))
-       {
+            valid = gtk_tree_model_iter_next (model, &iter)) {
                int m_type;
                McPresence m_state;
                char *m_status;
@@ -464,18 +720,17 @@ presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
                                COL_TYPE, &m_type,
                                -1);
 
-               if (m_type == ENTRY_TYPE_CUSTOM)
-               {
+               if (m_type == ENTRY_TYPE_CUSTOM ||
+                   m_type == ENTRY_TYPE_SEPARATOR ||
+                   m_type == ENTRY_TYPE_EDIT_CUSTOM) {
                        continue;
                }
-               else if (!match_state && state == m_state)
-               {
+               else if (!match_state && state == m_state) {
                        /* we are now in the section that can contain our
                         * match */
                        match_state = TRUE;
                }
-               else if (match_state && state != m_state)
-               {
+               else if (match_state && state != m_state) {
                        /* we have passed the section that can contain our
                         * match */
                        break;
@@ -493,23 +748,19 @@ presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
 
        }
 
-       if (match)
-       {
-               g_print ("GOT MATCH\n");
+       if (match) {
                priv->block_changed++;
                gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
                priv->block_changed--;
        }
-       else
-       {
-               g_print ("NO MATCH\n");
-               // FIXME - do we insert the match into the menu?
+       else {
                ui_set_custom_state (chooser, state, status);
        }
 
        if (flash_state != MC_PRESENCE_UNSET) {
                presence_chooser_flash_start (chooser, state, flash_state);
-       } else {
+       }
+       else {
                presence_chooser_flash_stop (chooser, state);
        }
 }
@@ -525,7 +776,8 @@ presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser)
 
        if (on) {
                state = priv->flash_state_1;
-       } else {
+       }
+       else {
                state = priv->flash_state_2;
        }
 
@@ -575,13 +827,10 @@ presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
                priv->flash_timeout_id = 0;
        }
        GtkWidget *entry = gtk_bin_get_child (GTK_BIN (chooser));
-       
+
        gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
                        GTK_ENTRY_ICON_PRIMARY,
                        empathy_icon_name_for_presence (state));
-
-       // FIXME - what does this do?
-       // priv->last_state = state;
 }
 
 GtkWidget *
@@ -616,7 +865,7 @@ empathy_presence_chooser_create_menu (void)
 
        }
 
-       /* Separator. */
+       /* Separator */
        item = gtk_menu_item_new ();
        gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
        gtk_widget_show (item);
@@ -696,7 +945,7 @@ static void
 presence_chooser_custom_activate_cb (GtkWidget *item,
                                     gpointer   user_data)
 {
-       presence_chooser_dialog_show ();
+       presence_chooser_dialog_show (NULL);
 }
 
 static McPresence
@@ -784,7 +1033,8 @@ presence_chooser_dialog_save_toggled_cb (GtkWidget           *widget,
 
        if (active) {
                empathy_status_presets_set_last (state, text);
-       } else {
+       }
+       else {
                empathy_status_presets_remove (state, text);
        }
 }
@@ -861,7 +1111,7 @@ presence_chooser_dialog_destroy_cb (GtkWidget           *widget,
 }
 
 static void
-presence_chooser_dialog_show (void)
+presence_chooser_dialog_show (GtkWindow *parent)
 {
        GladeXML *glade;
        gchar    *filename;
@@ -907,7 +1157,12 @@ presence_chooser_dialog_show (void)
 
        gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (message_dialog->comboboxentry_message), 0);
 
-       /* FIXME: Set transian for a window ? */
+       if (parent)
+       {
+               gtk_window_set_transient_for (
+                               GTK_WINDOW (message_dialog->dialog),
+                               parent);
+       }
 
        gtk_widget_show_all (message_dialog->dialog);
 }