]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-presence-chooser.c
Make custom messages dialog a transient of the window containing the presence selecto...
[empathy.git] / libempathy-gtk / empathy-presence-chooser.c
index e543a5f1fc14fb34aebd12c80b8908fe699f87fe..80ebe1faca08be73b672efe37e393c46e95de65a 100644 (file)
 #include <string.h>
 #include <stdlib.h>
 
-#include <glib/gi18n.h>
+#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>
 
 #include <libempathy/empathy-idle.h>
 #include <libempathy/empathy-utils.h>
-#include <libempathy/empathy-debug.h>
-#include <libempathy/empathy-marshal.h>
 #include <libempathy/empathy-status-presets.h>
 
+// FIXME - what's the correct debug flag?
+#define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
+#include <libempathy/empathy-debug.h>
+
 #include "empathy-ui-utils.h"
 #include "empathy-images.h"
 #include "empathy-presence-chooser.h"
 
-#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv))
-
-#define DEBUG_DOMAIN "PresenceChooser"
-
 /* Flashing delay for icons (milliseconds). */
 #define FLASH_TIMEOUT 500
 
+#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyPresenceChooser)
 typedef struct {
        EmpathyIdle *idle;
 
-       GtkWidget   *hbox;
-       GtkWidget   *image;
-       GtkWidget   *label;
-       GtkWidget   *menu;
+       gboolean     editing_status;
+       int          block_set_editing;
+       int          block_changed;
 
-       McPresence   last_state;
+       McPresence   state;
 
        McPresence   flash_state_1;
        McPresence   flash_state_2;
        guint        flash_timeout_id;
-
-       /* The handle the kind of unnessecary scroll support. */
-       guint        scroll_timeout_id;
-       McPresence   scroll_state;
-       gchar       *scroll_status;
 } EmpathyPresenceChooserPriv;
 
 typedef struct {
@@ -87,11 +81,6 @@ enum {
        COL_COUNT
 };
 
-typedef struct {
-       McPresence   state;
-       const gchar *status;
-} StateAndStatus;
-
 static CustomMessageDialog *message_dialog = NULL;
 /* States to be listed in the menu.
  * Each state has a boolean telling if it can have custom message */
@@ -101,42 +90,14 @@ static guint states[] = {MC_PRESENCE_AVAILABLE, TRUE,
                         MC_PRESENCE_HIDDEN, FALSE,
                         MC_PRESENCE_OFFLINE, FALSE};
 
-static void            empathy_presence_chooser_class_init      (EmpathyPresenceChooserClass *klass);
-static void            empathy_presence_chooser_init            (EmpathyPresenceChooser      *chooser);
 static void            presence_chooser_finalize               (GObject                    *object);
 static void            presence_chooser_presence_changed_cb    (EmpathyPresenceChooser      *chooser);
-static void            presence_chooser_reset_scroll_timeout   (EmpathyPresenceChooser      *chooser);
-static gboolean        presence_chooser_scroll_timeout_cb      (EmpathyPresenceChooser      *chooser);
-static gboolean        presence_chooser_scroll_event_cb        (EmpathyPresenceChooser      *chooser,
-                                                               GdkEventScroll             *event,
-                                                               gpointer                    user_data);
-static GList *         presence_chooser_get_presets            (EmpathyPresenceChooser      *chooser);
-static StateAndStatus *presence_chooser_state_and_status_new   (McPresence                  state,
-                                                               const gchar                *status);
 static gboolean        presence_chooser_flash_timeout_cb       (EmpathyPresenceChooser      *chooser);
 static void            presence_chooser_flash_start            (EmpathyPresenceChooser      *chooser,
                                                                McPresence                  state_1,
                                                                McPresence                  state_2);
 static void            presence_chooser_flash_stop             (EmpathyPresenceChooser      *chooser,
                                                                McPresence                  state);
-static gboolean        presence_chooser_button_press_event_cb  (GtkWidget                  *chooser,
-                                                               GdkEventButton             *event,
-                                                               gpointer                    user_data);
-static void            presence_chooser_toggled_cb             (GtkWidget                  *chooser,
-                                                               gpointer                    user_data);
-static void            presence_chooser_menu_popup             (EmpathyPresenceChooser      *chooser);
-static void            presence_chooser_menu_popdown           (EmpathyPresenceChooser      *chooser);
-static void            presence_chooser_menu_selection_done_cb (GtkMenuShell               *menushell,
-                                                               EmpathyPresenceChooser      *chooser);
-static void            presence_chooser_menu_destroy_cb        (GtkWidget                  *menu,
-                                                               EmpathyPresenceChooser      *chooser);
-static void            presence_chooser_menu_detach            (GtkWidget                  *attach_widget,
-                                                               GtkMenu                    *menu);
-static void            presence_chooser_menu_align_func        (GtkMenu                    *menu,
-                                                               gint                       *x,
-                                                               gint                       *y,
-                                                               gboolean                   *push_in,
-                                                               GtkWidget                  *widget);
 static void            presence_chooser_menu_add_item          (GtkWidget                  *menu,
                                                                const gchar                *str,
                                                                McPresence                  state);
@@ -146,9 +107,9 @@ 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_TOGGLE_BUTTON);
+G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_COMBO_BOX_ENTRY);
 
 static void
 empathy_presence_chooser_class_init (EmpathyPresenceChooserClass *klass)
@@ -160,283 +121,435 @@ empathy_presence_chooser_class_init (EmpathyPresenceChooserClass *klass)
        g_type_class_add_private (object_class, sizeof (EmpathyPresenceChooserPriv));
 }
 
-static void
-empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
+enum
 {
-       EmpathyPresenceChooserPriv *priv;
-       GtkWidget                 *arrow;
-       GtkWidget                 *alignment;
-
-       priv = GET_PRIV (chooser);
-
-       gtk_button_set_relief (GTK_BUTTON (chooser), GTK_RELIEF_NONE);
-       gtk_button_set_focus_on_click (GTK_BUTTON (chooser), FALSE);
-
-       alignment = gtk_alignment_new (0.5, 0.5, 1, 1);
-       gtk_widget_show (alignment);
-       gtk_container_add (GTK_CONTAINER (chooser), alignment);
-       gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 1, 0);
+       COL_STATE_ICON_NAME,
+       COL_STATE,
+       COL_STATUS_TEXT,
+       COL_DISPLAY_MARKUP,
+       COL_TYPE,
+       N_COLUMNS
+};
 
-       priv->hbox = gtk_hbox_new (FALSE, 1);
-       gtk_widget_show (priv->hbox);
-       gtk_container_add (GTK_CONTAINER (alignment), priv->hbox);
+enum
+{
+       ENTRY_TYPE_BUILTIN,
+       ENTRY_TYPE_SAVED,
+       ENTRY_TYPE_CUSTOM,
+};
 
-       priv->image = gtk_image_new ();
-       gtk_widget_show (priv->image);
-       gtk_box_pack_start (GTK_BOX (priv->hbox), priv->image, FALSE, TRUE, 0);
+static GtkTreeModel *
+create_model (void)
+{
+       GtkListStore *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_INT);            /* COL_TYPE */
+       
+       GtkTreeIter iter;
+       
+       int i;
+       for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
+               GList       *list, *l;
 
-       priv->label = gtk_label_new (NULL);
-       gtk_widget_show (priv->label);
-       gtk_box_pack_start (GTK_BOX (priv->hbox), priv->label, TRUE, TRUE, 0);
-       gtk_label_set_ellipsize (GTK_LABEL (priv->label), PANGO_ELLIPSIZE_END);
-       gtk_misc_set_alignment (GTK_MISC (priv->label), 0, 0.5);
-       gtk_misc_set_padding (GTK_MISC (priv->label), 4, 1);
+               const char *status = empathy_presence_get_default_message (states[i]);
+               const char *icon_name = empathy_icon_name_for_presence (states[i]);
 
-       alignment = gtk_alignment_new (0.5, 0.5, 1, 1);
-       gtk_widget_show (alignment);
-       gtk_box_pack_start (GTK_BOX (priv->hbox), alignment, FALSE, FALSE, 0);
+               gtk_list_store_append (store, &iter);
+               gtk_list_store_set (store, &iter,
+                               COL_STATE_ICON_NAME, icon_name,
+                               COL_STATE, states[i],
+                               COL_STATUS_TEXT, status,
+                               COL_DISPLAY_MARKUP, status,
+                               COL_TYPE, ENTRY_TYPE_BUILTIN,
+                               -1);
 
-       arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
-       gtk_widget_show (arrow);
-       gtk_container_add (GTK_CONTAINER (alignment), arrow);
+               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,
+                                               COL_STATE_ICON_NAME, icon_name,
+                                               COL_STATE, states[i],
+                                               COL_STATUS_TEXT, l->data,
+                                               COL_DISPLAY_MARKUP, l->data,
+                                               COL_TYPE, ENTRY_TYPE_SAVED,
+                                               -1);
+                       }
+                       g_list_free (list);
+               
+                       gtk_list_store_append (store, &iter);
+                       gtk_list_store_set (store, &iter,
+                                       COL_STATE_ICON_NAME, icon_name,
+                                       COL_STATE, states[i],
+                                       COL_STATUS_TEXT, "",
+                                       COL_DISPLAY_MARKUP, "<i>Custom Message...</i>",
+                                       COL_TYPE, ENTRY_TYPE_CUSTOM,
+                                       -1);
+               }
 
-       g_signal_connect (chooser, "toggled",
-                         G_CALLBACK (presence_chooser_toggled_cb),
-                         NULL);
-       g_signal_connect (chooser, "button-press-event",
-                         G_CALLBACK (presence_chooser_button_press_event_cb),
-                         NULL);
-       g_signal_connect (chooser, "scroll-event",
-                         G_CALLBACK (presence_chooser_scroll_event_cb),
-                         NULL);
+       }
 
-       priv->idle = empathy_idle_new ();
-       presence_chooser_presence_changed_cb (chooser);
-       g_signal_connect_swapped (priv->idle, "notify",
-                                 G_CALLBACK (presence_chooser_presence_changed_cb),
-                                 chooser);
+       return GTK_TREE_MODEL (store);
 }
 
 static void
-presence_chooser_finalize (GObject *object)
+popup_shown_cb (GObject *self, GParamSpec *pspec, gpointer user_data)
 {
-       EmpathyPresenceChooserPriv *priv;
+       gboolean shown;
+       g_object_get (self, "popup-shown", &shown, NULL);
 
-       priv = GET_PRIV (object);
+       if (!shown) return;
 
-       if (priv->flash_timeout_id) {
-               g_source_remove (priv->flash_timeout_id);
-       }
+       GtkTreeModel *model = create_model ();
 
-       if (priv->scroll_timeout_id) {
-               g_source_remove (priv->scroll_timeout_id);
+       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)
+{
+       EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
+       GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
+
+       if (priv->block_set_editing) return;
+
+       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"));
+               gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
+                               GTK_ENTRY_ICON_PRIMARY,
+                               FALSE);
+       }
+       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);
+               gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
+                               GTK_ENTRY_ICON_PRIMARY,
+                               TRUE);
+
+               // FIXME - move the focus somewhere
+
+               priv->editing_status = FALSE;
        }
+}
 
-       g_signal_handlers_disconnect_by_func (priv->idle,
-                                             presence_chooser_presence_changed_cb,
-                                             object);
-       g_object_unref (priv->idle);
+static void
+mc_set_custom_state (EmpathyPresenceChooser *self)
+{
+       EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
+       GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
 
-       G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
+       /* update the status with MC */
+       const char *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);
 }
 
-GtkWidget *
-empathy_presence_chooser_new (void)
+static void
+ui_set_custom_state (EmpathyPresenceChooser *self,
+                          McPresence state,
+                          const char *status)
 {
-       GtkWidget *chooser;
+       EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
+       GtkWidget *entry = gtk_bin_get_child (GTK_BIN (self));
+       const char *icon_name;
 
-       chooser = g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER, NULL);
+       priv->block_set_editing++;
+       priv->block_changed++;
 
-       return chooser;
+       icon_name = empathy_icon_name_for_presence (state);
+       gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
+                       GTK_ENTRY_ICON_PRIMARY,
+                       icon_name);
+       gtk_entry_set_text (GTK_ENTRY (entry), status);
+
+       priv->block_changed--;
+       priv->block_set_editing--;
 }
 
 static void
-presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
+entry_icon_release_cb (EmpathyPresenceChooser  *self,
+                      GtkEntryIconPosition      icon_pos,
+                      GdkEvent         *event,
+                      GtkEntry         *entry)
 {
-       EmpathyPresenceChooserPriv *priv;
-       McPresence                 state;
-       McPresence                 flash_state;
-       const gchar               *status;
+       set_status_editing (self, FALSE);
+       mc_set_custom_state (self);
+}
 
-       priv = GET_PRIV (chooser);
+static void
+entry_activate_cb (EmpathyPresenceChooser      *self,
+                  GtkEntry                     *entry)
+{
+       set_status_editing (self, FALSE);
+       mc_set_custom_state (self);
+}
 
-       state = empathy_idle_get_state (priv->idle);
-       status = empathy_idle_get_status (priv->idle);
-       flash_state = empathy_idle_get_flash_state (priv->idle);
+static gboolean
+entry_key_press_event_cb (EmpathyPresenceChooser       *self,
+                         GdkEventKey                   *event,
+                         GtkWidget                     *entry)
+{
+       EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
 
-       presence_chooser_reset_scroll_timeout (chooser);
-       gtk_label_set_text (GTK_LABEL (priv->label), status);
+       if (priv->editing_status && event->keyval == GDK_Escape)
+       {
+               /* the user pressed Escape, undo the editing */
+               set_status_editing (self, FALSE);
+               presence_chooser_presence_changed_cb (self);
 
-       if (flash_state != MC_PRESENCE_UNSET) {
-               presence_chooser_flash_start (chooser, state, flash_state);
-       } else {
-               presence_chooser_flash_stop (chooser, state);
+               return TRUE;
        }
+
+       return FALSE; /* send this event elsewhere */
 }
 
 static void
-presence_chooser_reset_scroll_timeout (EmpathyPresenceChooser *chooser)
+changed_cb (GtkComboBox *self, gpointer user_data)
 {
-       EmpathyPresenceChooserPriv *priv;
+       EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
 
-       priv = GET_PRIV (chooser);
+       if (priv->block_changed) return;
+
+       GtkTreeIter iter;
+       char *icon_name;
+       int type = -1;
 
-       if (priv->scroll_timeout_id) {
-               g_source_remove (priv->scroll_timeout_id);
-               priv->scroll_timeout_id = 0;
+       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 */
+               if (!priv->editing_status)
+               {
+                       set_status_editing (EMPATHY_PRESENCE_CHOOSER (self), TRUE);
+               }
+               return;
        }
 
-       g_free (priv->scroll_status);
-       priv->scroll_status = NULL;
+       gtk_tree_model_get (model, &iter,
+                       COL_STATE_ICON_NAME, &icon_name,
+                       COL_STATE, &priv->state,
+                       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);
+
+       if (type == ENTRY_TYPE_CUSTOM)
+       {
+               /* grab the focus */
+               gtk_widget_grab_focus (entry);
+               set_status_editing (EMPATHY_PRESENCE_CHOOSER (self), TRUE);
+       }
+       else
+       {
+               char *status;
+               /* just in case we were setting a new status when
+                * things were changed */
+               set_status_editing (EMPATHY_PRESENCE_CHOOSER (self), FALSE);
+
+               gtk_tree_model_get (model, &iter,
+                               COL_STATUS_TEXT, &status,
+                               -1);
+
+               empathy_idle_set_presence (priv->idle, priv->state, status);
+
+               g_free (status);
+       }
+
+       g_free (icon_name);
 }
 
-static gboolean
-presence_chooser_scroll_timeout_cb (EmpathyPresenceChooser *chooser)
+static void
+empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
 {
-       EmpathyPresenceChooserPriv *priv;
+       EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
+               EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
+
+       chooser->priv = priv;
+       
+       GtkTreeModel *model = 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_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_CONNECT_SWAPPED);
+       g_signal_connect_object (entry, "activate",
+                       G_CALLBACK (entry_activate_cb), chooser,
+                       G_CONNECT_SWAPPED);
+       g_signal_connect_object (entry, "key-press-event",
+                       G_CALLBACK (entry_key_press_event_cb), chooser,
+                       G_CONNECT_SWAPPED);
+       // FIXME - should this also happen when the user presses TAB ?
 
-       priv = GET_PRIV (chooser);
+       GtkCellRenderer *renderer;
+       gtk_cell_layout_clear (GTK_CELL_LAYOUT (chooser));
+
+       renderer = gtk_cell_renderer_pixbuf_new ();
+       gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, FALSE);
+       gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
+                       "icon-name", COL_STATE_ICON_NAME,
+                       NULL);
+       g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
 
-       priv->scroll_timeout_id = 0;
+       renderer = gtk_cell_renderer_text_new ();
+       gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (chooser), renderer, TRUE);
+       gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
+                       "markup", COL_DISPLAY_MARKUP,
+                       NULL);
 
-       empathy_idle_set_presence (priv->idle,
-                                  priv->scroll_state,
-                                  priv->scroll_status);
+       g_object_unref (model);
 
-       g_free (priv->scroll_status);
-       priv->scroll_status = NULL;
+       g_signal_connect (chooser, "notify::popup-shown",
+                       G_CALLBACK (popup_shown_cb), NULL);
+       g_signal_connect (chooser, "changed",
+                       G_CALLBACK (changed_cb), NULL);
 
-       return FALSE;
+       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_object_set (chooser,
+                       // FIXME: this string sucks
+                       "tooltip-text", _("Set your presence and current status"),
+                       NULL);
 }
 
-static gboolean
-presence_chooser_scroll_event_cb (EmpathyPresenceChooser *chooser,
-                                 GdkEventScroll        *event,
-                                 gpointer               user_data)
+static void
+presence_chooser_finalize (GObject *object)
 {
        EmpathyPresenceChooserPriv *priv;
-       GList                     *list, *l;
-       const gchar               *current_status;
-       StateAndStatus            *sas;
-       gboolean                   match;
 
-       priv = GET_PRIV (chooser);
-
-       switch (event->direction) {
-       case GDK_SCROLL_UP:
-               break;
-       case GDK_SCROLL_DOWN:
-               break;
-       default:
-               return FALSE;
-       }
-
-       current_status = gtk_label_get_text (GTK_LABEL (priv->label));
-
-       /* Get the list of presets, which in this context means all the items
-        * without a trailing "...".
-        */
-       list = presence_chooser_get_presets (chooser);
-       sas = NULL;
-       match = FALSE;
-       for (l = list; l; l = l->next) {
-               sas = l->data;
-
-               if (sas->state == priv->last_state &&
-                   strcmp (sas->status, current_status) == 0) {
-                       sas = NULL;
-                       match = TRUE;
-                       if (event->direction == GDK_SCROLL_UP) {
-                               if (l->prev) {
-                                       sas = l->prev->data;
-                               }
-                       }
-                       else if (event->direction == GDK_SCROLL_DOWN) {
-                               if (l->next) {
-                                       sas = l->next->data;
-                               }
-                       }
-                       break;
-               }
+       priv = GET_PRIV (object);
 
-               sas = NULL;
+       if (priv->flash_timeout_id) {
+               g_source_remove (priv->flash_timeout_id);
        }
 
-       if (sas) {
-               presence_chooser_reset_scroll_timeout (chooser);
-
-               priv->scroll_status = g_strdup (sas->status);
-               priv->scroll_state = sas->state;
+       g_signal_handlers_disconnect_by_func (priv->idle,
+                                             presence_chooser_presence_changed_cb,
+                                             object);
+       g_object_unref (priv->idle);
 
-               priv->scroll_timeout_id =
-                       g_timeout_add_seconds (1,
-                                              (GSourceFunc) presence_chooser_scroll_timeout_cb,
-                                              chooser);
+       G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
+}
 
-               presence_chooser_flash_stop (chooser, sas->state);
-               gtk_label_set_text (GTK_LABEL (priv->label), sas->status);      
-       }
-       else if (!match) {
-               const gchar *status;
-               /* If we didn't get any match at all, it means the last state
-                * was a custom one. Just switch to the first one.
-                */
-               status = empathy_presence_get_default_message (states[0]);
-
-               presence_chooser_reset_scroll_timeout (chooser);
-               empathy_idle_set_presence (priv->idle, states[0], status);
-       }
+GtkWidget *
+empathy_presence_chooser_new (void)
+{
+       GtkWidget *chooser;
 
-       g_list_foreach (list, (GFunc) g_free, NULL);
-       g_list_free (list);
+       chooser = g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER, NULL);
 
-       return TRUE;
+       return chooser;
 }
 
-static GList *
-presence_chooser_get_presets (EmpathyPresenceChooser *chooser)
+static void
+presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
 {
-       GList      *list = NULL;
-       guint       i;
+       EmpathyPresenceChooserPriv *priv;
+       McPresence                 state;
+       McPresence                 flash_state;
+       const gchar               *status;
 
-       for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
-               GList          *presets, *p;
-               StateAndStatus *sas;
-               const gchar    *status;
+       priv = GET_PRIV (chooser);
 
-               status = empathy_presence_get_default_message (states[i]);
-               sas = presence_chooser_state_and_status_new (states[i], status);
-               list = g_list_prepend (list, sas);
+       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);
 
-               /* Go to next state if we don't want messages for that state */
-               if (!states[i+1]) {
+       /* look through the model and attempt to find a matching state */
+       GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
+       GtkTreeIter iter;
+       gboolean valid, match_state = FALSE, match = FALSE;
+       for (valid = gtk_tree_model_get_iter_first (model, &iter);
+            valid;
+            valid = gtk_tree_model_iter_next (model, &iter))
+       {
+               int m_type;
+               McPresence m_state;
+               char *m_status;
+
+               gtk_tree_model_get (model, &iter,
+                               COL_STATE, &m_state,
+                               COL_TYPE, &m_type,
+                               -1);
+
+               if (m_type == ENTRY_TYPE_CUSTOM)
+               {
                        continue;
                }
-
-               presets = empathy_status_presets_get (states[i], 5);
-               for (p = presets; p; p = p->next) {
-                       sas = presence_chooser_state_and_status_new (states[i], p->data);
-                       list = g_list_prepend (list, sas);
+               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)
+               {
+                       /* we have passed the section that can contain our
+                        * match */
+                       break;
                }
-               g_list_free (presets);
-       }
-       list = g_list_reverse (list);
 
-       return list;
-}
+               gtk_tree_model_get (model, &iter,
+                               COL_STATUS_TEXT, &m_status,
+                               -1);
 
-static StateAndStatus *
-presence_chooser_state_and_status_new (McPresence   state,
-                                      const gchar *status)
-{
-       StateAndStatus *sas;
+               match = !strcmp (status, m_status);
+
+               g_free (m_status);
+
+               if (match) break;
 
-       sas = g_new0 (StateAndStatus, 1);
+       }
 
-       sas->state = state;
-       sas->status = status;
+       if (match)
+       {
+               priv->block_changed++;
+               gtk_combo_box_set_active_iter (GTK_COMBO_BOX (chooser), &iter);
+               priv->block_changed--;
+       }
+       else
+       {
+               // FIXME - do we insert the match into the menu?
+               ui_set_custom_state (chooser, state, status);
+       }
 
-       return sas;
+       if (flash_state != MC_PRESENCE_UNSET) {
+               presence_chooser_flash_start (chooser, state, flash_state);
+       } else {
+               presence_chooser_flash_stop (chooser, state);
+       }
 }
 
 static gboolean
@@ -454,9 +567,10 @@ presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser)
                state = priv->flash_state_2;
        }
 
-       gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
-                                     empathy_icon_name_for_presence (state),
-                                     GTK_ICON_SIZE_MENU);
+       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));
 
        on = !on;
 
@@ -498,157 +612,14 @@ presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
                g_source_remove (priv->flash_timeout_id);
                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));
 
-       gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
-                                     empathy_icon_name_for_presence (state),
-                                     GTK_ICON_SIZE_MENU);
-
-       priv->last_state = state;
-}
-
-static gboolean
-presence_chooser_button_press_event_cb (GtkWidget      *chooser,
-                                       GdkEventButton *event,
-                                       gpointer        user_data)
-{
-       if (event->button != 1 || event->type != GDK_BUTTON_PRESS) {
-               return FALSE;
-       }
-
-       if (!gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (chooser))) {
-                       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (chooser), TRUE);
-                       return TRUE;
-               }
-
-       return FALSE;
-}
-
-static void
-presence_chooser_toggled_cb (GtkWidget *chooser,
-                            gpointer   user_data)
-{
-       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (chooser))) {
-               presence_chooser_menu_popup (EMPATHY_PRESENCE_CHOOSER (chooser));
-       } else {
-               presence_chooser_menu_popdown (EMPATHY_PRESENCE_CHOOSER (chooser));
-       }
-}
-
-static void
-presence_chooser_menu_popup (EmpathyPresenceChooser *chooser)
-{
-       EmpathyPresenceChooserPriv *priv;
-       GtkWidget                 *menu;
-
-       priv = GET_PRIV (chooser);
-
-       if (priv->menu) {
-               return;
-       }
-
-       menu = empathy_presence_chooser_create_menu ();
-
-       g_signal_connect_after (menu, "selection-done",
-                               G_CALLBACK (presence_chooser_menu_selection_done_cb),
-                               chooser);
-
-       g_signal_connect (menu, "destroy",
-                         G_CALLBACK (presence_chooser_menu_destroy_cb),
-                         chooser);
-
-       gtk_menu_attach_to_widget (GTK_MENU (menu),
-                                  GTK_WIDGET (chooser),
-                                  presence_chooser_menu_detach);
-
-       gtk_menu_popup (GTK_MENU (menu),
-                       NULL, NULL,
-                       (GtkMenuPositionFunc) presence_chooser_menu_align_func,
-                       chooser,
-                       1,
-                       gtk_get_current_event_time ());
-
-       priv->menu = menu;
-}
-
-static void
-presence_chooser_menu_popdown (EmpathyPresenceChooser *chooser)
-{
-       EmpathyPresenceChooserPriv *priv;
-
-       priv = GET_PRIV (chooser);
-
-       if (priv->menu) {
-               gtk_widget_destroy (priv->menu);
-       }
-}
-
-static void
-presence_chooser_menu_selection_done_cb (GtkMenuShell          *menushell,
-                                        EmpathyPresenceChooser *chooser)
-{
-       gtk_widget_destroy (GTK_WIDGET (menushell));
-
-       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (chooser), FALSE);
-}
-
-static void
-presence_chooser_menu_destroy_cb (GtkWidget             *menu,
-                                 EmpathyPresenceChooser *chooser)
-{
-       EmpathyPresenceChooserPriv *priv;
-
-       priv = GET_PRIV (chooser);
-
-       priv->menu = NULL;
-}
-
-static void
-presence_chooser_menu_detach (GtkWidget *attach_widget,
-                             GtkMenu   *menu)
-{
-       /* We don't need to do anything, but attaching the menu means
-        * we don't own the ref count and it is cleaned up properly.
-        */
-}
-
-static void
-presence_chooser_menu_align_func (GtkMenu   *menu,
-                                 gint      *x,
-                                 gint      *y,
-                                 gboolean  *push_in,
-                                 GtkWidget *widget)
-{
-       GtkRequisition  req;
-       GdkScreen      *screen;
-       gint            screen_height;
-
-       gtk_widget_size_request (GTK_WIDGET (menu), &req);
-
-       gdk_window_get_origin (widget->window, x, y);
-
-       *x += widget->allocation.x + 1;
-       *y += widget->allocation.y;
-
-       screen = gtk_widget_get_screen (GTK_WIDGET (menu));
-       screen_height = gdk_screen_get_height (screen);
-
-       if (req.height > screen_height) {
-               /* Too big for screen height anyway. */
-               *y = 0;
-               return;
-       }
-
-       if ((*y + req.height + widget->allocation.height) > screen_height) {
-               /* Can't put it below the button. */
-               *y -= req.height;
-               *y += 1;
-       } else {
-               /* Put menu below button. */
-               *y += widget->allocation.height;
-               *y -= 1;
-       }
-
-       *push_in = FALSE;
+       // FIXME - what does this do?
+       // priv->last_state = state;
 }
 
 GtkWidget *
@@ -754,7 +725,7 @@ presence_chooser_set_state (McPresence   state,
 {
        EmpathyIdle *idle;
 
-       idle = empathy_idle_new ();
+       idle = empathy_idle_dup_singleton ();
        empathy_idle_set_presence (idle, state, status);
        g_object_unref (idle);
 }
@@ -763,7 +734,7 @@ static void
 presence_chooser_custom_activate_cb (GtkWidget *item,
                                     gpointer   user_data)
 {
-       presence_chooser_dialog_show ();
+       presence_chooser_dialog_show (NULL);
 }
 
 static McPresence
@@ -928,9 +899,10 @@ presence_chooser_dialog_destroy_cb (GtkWidget           *widget,
 }
 
 static void
-presence_chooser_dialog_show (void)
+presence_chooser_dialog_show (GtkWindow *parent)
 {
        GladeXML *glade;
+       gchar    *filename;
 
        if (message_dialog) {
                gtk_window_present (GTK_WINDOW (message_dialog->dialog));
@@ -938,7 +910,10 @@ presence_chooser_dialog_show (void)
        }
 
        message_dialog = g_new0 (CustomMessageDialog, 1);
-       glade = empathy_glade_get_file ("empathy-presence-chooser.glade",
+
+       filename = empathy_file_lookup ("empathy-presence-chooser.glade",
+                                       "libempathy-gtk");
+       glade = empathy_glade_get_file (filename,
                                       "custom_message_dialog",
                                       NULL,
                                       "custom_message_dialog", &message_dialog->dialog,
@@ -946,6 +921,8 @@ presence_chooser_dialog_show (void)
                                       "comboboxentry_message", &message_dialog->comboboxentry_message,
                                       "combobox_status", &message_dialog->combobox_status,
                                       NULL);
+       g_free (filename);
+
        empathy_glade_connect (glade,
                               message_dialog,
                               "custom_message_dialog", "destroy", presence_chooser_dialog_destroy_cb,
@@ -968,7 +945,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);
 }