]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-presence-chooser.c
Merge branch 'sasl'
[empathy.git] / libempathy-gtk / empathy-presence-chooser.c
index f57c5d34f5b2d7fed90a3f59f7707d3d618b6c9e..33bf02811bf37a47954e5ca7444c49c321384d01 100644 (file)
@@ -21,7 +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>
+ *          Danielle Madeley <danielle.madeley@collabora.co.uk>
  */
 
 #include "config.h"
 #include <gtk/gtk.h>
 #include <gdk/gdkkeysyms.h>
 
+#include <telepathy-glib/account-manager.h>
 #include <telepathy-glib/util.h>
-#include <libmissioncontrol/mc-enum-types.h>
 
+#include <libempathy/empathy-connectivity.h>
 #include <libempathy/empathy-idle.h>
 #include <libempathy/empathy-utils.h>
 #include <libempathy/empathy-status-presets.h>
@@ -80,9 +81,9 @@ enum {
 
 /* For combobox's model */
 enum {
+       COL_STATUS_TEXT,
        COL_STATE_ICON_NAME,
        COL_STATE,
-       COL_STATUS_TEXT,
        COL_DISPLAY_MARKUP,
        COL_STATUS_CUSTOMISABLE,
        COL_TYPE,
@@ -99,53 +100,53 @@ typedef enum  {
 
 typedef struct {
        EmpathyIdle *idle;
+       EmpathyConnectivity *connectivity;
 
        gboolean     editing_status;
        int          block_set_editing;
        int          block_changed;
        guint        focus_out_idle_source;
 
-       McPresence   state;
+       TpConnectionPresenceType state;
        PresenceChooserEntryType previous_type;
 
-       McPresence   flash_state_1;
-       McPresence   flash_state_2;
-       guint        flash_timeout_id;
+       TpAccountManager *account_manager;
+       GdkPixbuf *not_favorite_pixbuf;
 } EmpathyPresenceChooserPriv;
 
 /* States to be listed in the menu.
  * Each state has a boolean telling if it can have custom message */
-static guint states[] = {MC_PRESENCE_AVAILABLE, TRUE,
-                        MC_PRESENCE_DO_NOT_DISTURB, TRUE,
-                        MC_PRESENCE_AWAY, TRUE,
-                        MC_PRESENCE_HIDDEN, FALSE,
-                        MC_PRESENCE_OFFLINE, FALSE};
-
+static struct { TpConnectionPresenceType state;
+         gboolean customisable;
+} states[] = { { TP_CONNECTION_PRESENCE_TYPE_AVAILABLE, TRUE } ,
+                        { TP_CONNECTION_PRESENCE_TYPE_BUSY, TRUE },
+                        { TP_CONNECTION_PRESENCE_TYPE_AWAY, TRUE },
+                        { TP_CONNECTION_PRESENCE_TYPE_HIDDEN, FALSE },
+                        { TP_CONNECTION_PRESENCE_TYPE_OFFLINE, FALSE},
+                        { TP_CONNECTION_PRESENCE_TYPE_UNSET, },
+                       };
+
+static void            presence_chooser_constructed            (GObject                    *object);
 static void            presence_chooser_finalize               (GObject                    *object);
 static void            presence_chooser_presence_changed_cb    (EmpathyPresenceChooser      *chooser);
-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 void            presence_chooser_menu_add_item          (GtkWidget                  *menu,
                                                                const gchar                *str,
-                                                               McPresence                  state);
+                                                               TpConnectionPresenceType                  state);
 static void            presence_chooser_noncustom_activate_cb  (GtkWidget                  *item,
                                                                gpointer                    user_data);
-static void            presence_chooser_set_state              (McPresence                  state,
+static void            presence_chooser_set_state              (TpConnectionPresenceType                  state,
                                                                const gchar                *status);
 static void            presence_chooser_custom_activate_cb     (GtkWidget                  *item,
                                                                gpointer                    user_data);
 
-G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_COMBO_BOX_ENTRY);
+G_DEFINE_TYPE (EmpathyPresenceChooser, empathy_presence_chooser, GTK_TYPE_COMBO_BOX);
 
 static void
 empathy_presence_chooser_class_init (EmpathyPresenceChooserClass *klass)
 {
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
+       object_class->constructed = presence_chooser_constructed;
        object_class->finalize = presence_chooser_finalize;
 
        g_type_class_add_private (object_class, sizeof (EmpathyPresenceChooserPriv));
@@ -159,42 +160,41 @@ presence_chooser_create_model (EmpathyPresenceChooser *self)
        int i;
 
        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_STATE_ICON_NAME */
+                                   G_TYPE_UINT,      /* COL_STATE */
                                    G_TYPE_STRING,    /* COL_DISPLAY_MARKUP */
                                    G_TYPE_BOOLEAN,   /* COL_STATUS_CUSTOMISABLE */
                                    G_TYPE_INT);      /* COL_TYPE */
 
-       custom_message = g_strdup_printf ("<i>%s</i>", _("Custom Message..."));
+       custom_message = g_strdup_printf ("<i>%s</i>", _("Custom Message"));
 
-       for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
+       for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
                GList       *list, *l;
                const char *status, *icon_name;
 
-               status = empathy_presence_get_default_message (states[i]);
-               icon_name = empathy_icon_name_for_presence (states[i]);
+               status = empathy_presence_get_default_message (states[i].state);
+               icon_name = empathy_icon_name_for_presence (states[i].state);
 
                gtk_list_store_insert_with_values (store, NULL, -1,
-                       COL_STATE_ICON_NAME, icon_name,
-                       COL_STATE, states[i],
                        COL_STATUS_TEXT, status,
+                       COL_STATE_ICON_NAME, icon_name,
+                       COL_STATE, states[i].state,
                        COL_DISPLAY_MARKUP, status,
-                       COL_STATUS_CUSTOMISABLE, states[i+1],
+                       COL_STATUS_CUSTOMISABLE, states[i].customisable,
                        COL_TYPE, ENTRY_TYPE_BUILTIN,
                        -1);
 
-               if (states[i+1]) {
-
+               if (states[i].customisable) {
                        /* Set custom messages if wanted */
-                       list = empathy_status_presets_get (states[i], -1);
+                       list = empathy_status_presets_get (states[i].state, -1);
                        list = g_list_sort (list, (GCompareFunc) g_utf8_collate);
                        for (l = list; l; l = l->next) {
                                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_STATE_ICON_NAME, icon_name,
+                                       COL_STATE, states[i].state,
                                        COL_DISPLAY_MARKUP, l->data,
                                        COL_STATUS_CUSTOMISABLE, TRUE,
                                        COL_TYPE, ENTRY_TYPE_SAVED,
@@ -203,9 +203,9 @@ presence_chooser_create_model (EmpathyPresenceChooser *self)
                        g_list_free (list);
 
                        gtk_list_store_insert_with_values (store, NULL, -1,
+                               COL_STATUS_TEXT, _("Custom Message…"),
                                COL_STATE_ICON_NAME, icon_name,
-                               COL_STATE, states[i],
-                               COL_STATUS_TEXT, "",
+                               COL_STATE, states[i].state,
                                COL_DISPLAY_MARKUP, custom_message,
                                COL_STATUS_CUSTOMISABLE, TRUE,
                                COL_TYPE, ENTRY_TYPE_CUSTOM,
@@ -220,9 +220,9 @@ presence_chooser_create_model (EmpathyPresenceChooser *self)
                        -1);
 
        gtk_list_store_insert_with_values (store, NULL, -1,
+               COL_STATUS_TEXT, _("Edit Custom Messages…"),
                COL_STATE_ICON_NAME, GTK_STOCK_EDIT,
-               COL_STATUS_TEXT, "",
-               COL_DISPLAY_MARKUP, _("Edit Custom Messages..."),
+               COL_DISPLAY_MARKUP, _("Edit Custom Messages…"),
                COL_TYPE, ENTRY_TYPE_EDIT_CUSTOM,
                -1);
 
@@ -275,23 +275,45 @@ presence_chooser_get_entry_type (EmpathyPresenceChooser *self)
        return type;
 }
 
+static TpConnectionPresenceType
+get_state_and_status (EmpathyPresenceChooser *self,
+       gchar **status)
+{
+       EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
+       TpConnectionPresenceType state;
+       gchar *tmp;
+
+       state = tp_account_manager_get_most_available_presence (
+               priv->account_manager, NULL, &tmp);
+       if (EMP_STR_EMPTY (tmp)) {
+               /* no message, use the default message */
+               g_free (tmp);
+               tmp = g_strdup (empathy_presence_get_default_message (state));
+       }
+
+       if (status != NULL)
+               *status = tmp;
+       else
+               g_free (tmp);
+
+       return state;
+}
+
 static gboolean
 presence_chooser_is_preset (EmpathyPresenceChooser *self)
 {
-       EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
-       McPresence state;
-       const char *status;
+       TpConnectionPresenceType state;
+       char *status;
        GList *presets, *l;
        gboolean match = FALSE;
 
-       state = empathy_idle_get_state (priv->idle);
-       status = empathy_idle_get_status (priv->idle);
+       state = get_state_and_status (self, &status);
 
        presets = empathy_status_presets_get (state, -1);
        for (l = presets; l; l = l->next) {
                char *preset = (char *) l->data;
 
-               if (!strcmp (status, preset)) {
+               if (!tp_strdiff (status, preset)) {
                        match = TRUE;
                        break;
                }
@@ -299,14 +321,16 @@ presence_chooser_is_preset (EmpathyPresenceChooser *self)
 
        g_list_free (presets);
 
-       DEBUG ("is_preset(%i, %s) = %i\n", state, status, match);
+       DEBUG ("is_preset(%i, %s) = %i", state, status, match);
 
+       g_free (status);
        return match;
 }
 
 static void
 presence_chooser_set_favorite_icon (EmpathyPresenceChooser *self)
 {
+       EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
        GtkWidget *entry;
        PresenceChooserEntryType type;
 
@@ -318,16 +342,16 @@ presence_chooser_set_favorite_icon (EmpathyPresenceChooser *self)
                        /* saved entries can be removed from the list */
                        gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
                                           GTK_ENTRY_ICON_SECONDARY,
-                                          "empathy-starred");
+                                          "emblem-favorite");
                        gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
                                         GTK_ENTRY_ICON_SECONDARY,
                                         _("Click to remove this status as a favorite"));
                }
-               else {
+               else if (priv->not_favorite_pixbuf != NULL) {
                        /* custom entries can be favorited */
-                       gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
+                       gtk_entry_set_icon_from_pixbuf (GTK_ENTRY (entry),
                                           GTK_ENTRY_ICON_SECONDARY,
-                                          "empathy-unstarred");
+                                          priv->not_favorite_pixbuf);
                        gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
                                         GTK_ENTRY_ICON_SECONDARY,
                                         _("Click to make this status a favorite"));
@@ -378,7 +402,7 @@ presence_chooser_set_status_editing (EmpathyPresenceChooser *self,
 
                /* attempt to get the toplevel for this widget */
                window = gtk_widget_get_toplevel (GTK_WIDGET (self));
-               if (GTK_WIDGET_TOPLEVEL (window) && GTK_IS_WINDOW (window)) {
+               if (gtk_widget_is_toplevel (window) && GTK_IS_WINDOW (window)) {
                        /* unset the focus */
                        gtk_window_set_focus (GTK_WINDOW (window), NULL);
                }
@@ -407,17 +431,14 @@ mc_set_custom_state (EmpathyPresenceChooser *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);
+       DEBUG ("Sending state to MC-> %d (%s)", priv->state, status);
 
        empathy_idle_set_presence (priv->idle, priv->state, status);
 }
 
 static void
 ui_set_custom_state (EmpathyPresenceChooser *self,
-                    McPresence state,
+                    TpConnectionPresenceType state,
                     const char *status)
 {
        EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
@@ -433,7 +454,7 @@ ui_set_custom_state (EmpathyPresenceChooser *self,
        gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
                                           GTK_ENTRY_ICON_PRIMARY,
                                           icon_name);
-       gtk_entry_set_text (GTK_ENTRY (entry), status);
+       gtk_entry_set_text (GTK_ENTRY (entry), status == NULL ? "" : status);
        presence_chooser_set_favorite_icon (self);
 
        priv->block_changed--;
@@ -462,26 +483,32 @@ presence_chooser_entry_icon_release_cb (EmpathyPresenceChooser *self,
        }
        else {
                PresenceChooserEntryType type;
-               McPresence state;
-               const char *status;
+               TpConnectionPresenceType state;
+               char *status;
 
                type = presence_chooser_get_entry_type (self);
-               state = empathy_idle_get_state (priv->idle);
-               status = empathy_idle_get_status (priv->idle);
+               state = get_state_and_status (self, &status);
+
+               if (!empathy_status_presets_is_valid (state)) {
+                       /* It doesn't make sense to add such presence as favorite */
+                       g_free (status);
+                       return;
+               }
 
                if (presence_chooser_is_preset (self)) {
                        /* remove the entry */
-                       DEBUG ("REMOVING PRESET (%i, %s)\n", state, status);
+                       DEBUG ("REMOVING PRESET (%i, %s)", state, status);
                        empathy_status_presets_remove (state, status);
                }
                else {
                        /* save the entry */
-                       DEBUG ("SAVING PRESET (%i, %s)\n", state, status);
+                       DEBUG ("SAVING PRESET (%i, %s)", state, status);
                        empathy_status_presets_set_last (state, status);
                }
 
                /* update the icon */
                presence_chooser_set_favorite_icon (self);
+               g_free (status);
        }
 }
 
@@ -500,12 +527,12 @@ presence_chooser_entry_key_press_event_cb (EmpathyPresenceChooser *self,
 {
        EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
 
-       if (priv->editing_status && event->keyval == GDK_Escape) {
+       if (priv->editing_status && event->keyval == GDK_KEY_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) {
+       else if (event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_Down) {
                /* ignore */
                return TRUE;
        }
@@ -522,7 +549,7 @@ presence_chooser_entry_button_press_event_cb (EmpathyPresenceChooser *self,
 
        if (!priv->editing_status &&
            event->button == 1 &&
-           !GTK_WIDGET_HAS_FOCUS (entry)) {
+           !gtk_widget_has_focus (entry)) {
                gtk_widget_grab_focus (entry);
                gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
 
@@ -554,7 +581,7 @@ presence_chooser_changed_cb (GtkComboBox *self, gpointer user_data)
        EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
        GtkTreeIter iter;
        char *icon_name;
-       McPresence new_state;
+       TpConnectionPresenceType new_state;
        gboolean customisable = TRUE;
        PresenceChooserEntryType type = -1;
        GtkWidget *entry;
@@ -590,7 +617,7 @@ presence_chooser_changed_cb (GtkComboBox *self, gpointer user_data)
 
                /* attempt to get the toplevel for this widget */
                window = gtk_widget_get_toplevel (GTK_WIDGET (self));
-               if (!GTK_WIDGET_TOPLEVEL (window) || !GTK_IS_WINDOW (window)) {
+               if (!gtk_widget_is_toplevel (window) || !GTK_IS_WINDOW (window)) {
                        window = NULL;
                }
 
@@ -611,10 +638,12 @@ presence_chooser_changed_cb (GtkComboBox *self, gpointer user_data)
                } else {
                        /* else preseed the text of their currently entered
                         * status message */
-                       const char *status;
+                       char *status;
 
-                       status = empathy_idle_get_status (priv->idle);
+                       get_state_and_status (EMPATHY_PRESENCE_CHOOSER (self),
+                               &status);
                        gtk_entry_set_text (GTK_ENTRY (entry), status);
+                       g_free (status);
                }
 
                /* grab the focus */
@@ -627,6 +656,9 @@ presence_chooser_changed_cb (GtkComboBox *self, gpointer user_data)
                presence_chooser_set_status_editing (
                        EMPATHY_PRESENCE_CHOOSER (self),
                        FALSE);
+               gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
+                                          GTK_ENTRY_ICON_PRIMARY,
+                                          icon_name);
 
                gtk_tree_model_get (model, &iter,
                                    COL_STATUS_TEXT, &status,
@@ -663,7 +695,7 @@ presence_chooser_entry_focus_out_idle_cb (gpointer user_data)
        EmpathyPresenceChooser *chooser;
        GtkWidget *entry;
 
-       DEBUG ("Autocommiting status message\n");
+       DEBUG ("Autocommiting status message");
 
        chooser = EMPATHY_PRESENCE_CHOOSER (user_data);
        entry = gtk_bin_get_child (GTK_BIN (chooser));
@@ -703,19 +735,143 @@ presence_chooser_entry_focus_out_cb (EmpathyPresenceChooser *chooser,
        return FALSE;
 }
 
+static void
+update_sensitivity_am_prepared_cb (GObject *source_object,
+                                  GAsyncResult *result,
+                                  gpointer user_data)
+{
+       TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
+       EmpathyPresenceChooser *chooser = user_data;
+       EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
+       gboolean sensitive = FALSE;
+       GList *accounts, *l;
+       GError *error = NULL;
+
+       if (!tp_account_manager_prepare_finish (manager, result, &error)) {
+               DEBUG ("Failed to prepare account manager: %s", error->message);
+               g_error_free (error);
+               return;
+       }
+
+       accounts = tp_account_manager_get_valid_accounts (manager);
+
+       for (l = accounts ; l != NULL ; l = g_list_next (l)) {
+               TpAccount *a = TP_ACCOUNT (l->data);
+
+               if (tp_account_is_enabled (a)) {
+                       sensitive = TRUE;
+                       break;
+               }
+       }
+
+       g_list_free (accounts);
+
+       if (!empathy_connectivity_is_online (priv->connectivity))
+               sensitive = FALSE;
+
+       gtk_widget_set_sensitive (GTK_WIDGET (chooser), sensitive);
+
+       presence_chooser_presence_changed_cb (chooser);
+}
+
+static void
+presence_chooser_update_sensitivity (EmpathyPresenceChooser *chooser)
+{
+       EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
+
+       tp_account_manager_prepare_async (priv->account_manager, NULL,
+                                         update_sensitivity_am_prepared_cb,
+                                         chooser);
+}
+
+static void
+presence_chooser_account_manager_account_validity_changed_cb (
+       TpAccountManager *manager,
+       TpAccount *account,
+       gboolean valid,
+       EmpathyPresenceChooser *chooser)
+{
+       presence_chooser_update_sensitivity (chooser);
+}
+
+static void
+presence_chooser_account_manager_account_changed_cb (
+       TpAccountManager *manager,
+       TpAccount *account,
+       EmpathyPresenceChooser *chooser)
+{
+       presence_chooser_update_sensitivity (chooser);
+}
+
+static void
+presence_chooser_connectivity_state_change (EmpathyConnectivity *connectivity,
+                                           gboolean new_online,
+                                           EmpathyPresenceChooser *chooser)
+{
+       presence_chooser_update_sensitivity (chooser);
+}
+
+/* Create a greyed version of the 'favorite' icon */
+static GdkPixbuf *
+create_not_favorite_pixbuf (void)
+{
+       GdkPixbuf *favorite, *result;
+
+       favorite = empathy_pixbuf_from_icon_name ("emblem-favorite",
+                                                 GTK_ICON_SIZE_MENU);
+
+       if (favorite == NULL)
+               return NULL;
+
+       result = gdk_pixbuf_copy (favorite);
+       gdk_pixbuf_saturate_and_pixelate (favorite, result, 1.0, TRUE);
+
+       g_object_unref (favorite);
+       return result;
+}
+
+static void
+icon_theme_changed_cb (GtkIconTheme *icon_theme,
+                      EmpathyPresenceChooser *self)
+{
+       EmpathyPresenceChooserPriv *priv = GET_PRIV (self);
+
+       /* Theme has changed, recreate the not-favorite icon */
+       if (priv->not_favorite_pixbuf != NULL)
+               g_object_unref (priv->not_favorite_pixbuf);
+       priv->not_favorite_pixbuf = create_not_favorite_pixbuf ();
+
+       /* Update the icon */
+       presence_chooser_set_favorite_icon (self);
+}
+
 static void
 empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
 {
        EmpathyPresenceChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
                EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv);
+
+       chooser->priv = priv;
+
+       /* Create the not-favorite icon */
+       priv->not_favorite_pixbuf = create_not_favorite_pixbuf ();
+}
+
+static void
+presence_chooser_constructed (GObject *object)
+{
+       EmpathyPresenceChooser *chooser = EMPATHY_PRESENCE_CHOOSER (object);
+       EmpathyPresenceChooserPriv *priv = chooser->priv;
        GtkWidget *entry;
        GtkCellRenderer *renderer;
 
-       chooser->priv = priv;
+       tp_g_signal_connect_object (gtk_icon_theme_get_default (), "changed",
+                                    G_CALLBACK (icon_theme_changed_cb),
+                                    chooser, 0);
 
        presence_chooser_create_model (chooser);
 
-       gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (chooser),
+       gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX (chooser),
                                             COL_STATUS_TEXT);
        gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
                                              combo_row_separator_func,
@@ -753,6 +909,7 @@ empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
        gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (chooser), renderer,
                                        "markup", COL_DISPLAY_MARKUP,
                                        NULL);
+       g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
 
        g_signal_connect (chooser, "notify::popup-shown",
                        G_CALLBACK (presence_chooser_popup_shown_cb), NULL);
@@ -766,14 +923,37 @@ empathy_presence_chooser_init (EmpathyPresenceChooser *chooser)
                        chooser);
 
        priv->idle = empathy_idle_dup_singleton ();
-       presence_chooser_presence_changed_cb (chooser);
-       g_signal_connect_swapped (priv->idle, "notify",
+
+       priv->account_manager = tp_account_manager_dup ();
+       g_signal_connect_swapped (priv->account_manager,
+               "most-available-presence-changed",
                G_CALLBACK (presence_chooser_presence_changed_cb),
                chooser);
 
+       tp_g_signal_connect_object (priv->account_manager, "account-validity-changed",
+               G_CALLBACK (presence_chooser_account_manager_account_validity_changed_cb),
+               chooser, 0);
+       tp_g_signal_connect_object (priv->account_manager, "account-removed",
+               G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
+               chooser, 0);
+       tp_g_signal_connect_object (priv->account_manager, "account-enabled",
+               G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
+               chooser, 0);
+       tp_g_signal_connect_object (priv->account_manager, "account-disabled",
+               G_CALLBACK (presence_chooser_account_manager_account_changed_cb),
+               chooser, 0);
+
        /* FIXME: this string sucks */
        gtk_widget_set_tooltip_text (GTK_WIDGET (chooser),
                _("Set your presence and current status"));
+
+       priv->connectivity = empathy_connectivity_dup_singleton ();
+       tp_g_signal_connect_object (priv->connectivity,
+               "state-change",
+               G_CALLBACK (presence_chooser_connectivity_state_change),
+               chooser, 0);
+
+       presence_chooser_update_sensitivity (chooser);
 }
 
 static void
@@ -783,19 +963,22 @@ presence_chooser_finalize (GObject *object)
 
        priv = GET_PRIV (object);
 
-       if (priv->flash_timeout_id) {
-               g_source_remove (priv->flash_timeout_id);
-       }
-
        if (priv->focus_out_idle_source) {
                g_source_remove (priv->focus_out_idle_source);
        }
 
+       if (priv->account_manager != NULL)
+               g_object_unref (priv->account_manager);
+
        g_signal_handlers_disconnect_by_func (priv->idle,
                                              presence_chooser_presence_changed_cb,
                                              object);
        g_object_unref (priv->idle);
 
+       g_object_unref (priv->connectivity);
+       if (priv->not_favorite_pixbuf != NULL)
+               g_object_unref (priv->not_favorite_pixbuf);
+
        G_OBJECT_CLASS (empathy_presence_chooser_parent_class)->finalize (object);
 }
 
@@ -809,23 +992,22 @@ presence_chooser_finalize (GObject *object)
 GtkWidget *
 empathy_presence_chooser_new (void)
 {
-       GtkWidget *chooser;
-
-       chooser = g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER, NULL);
-
-       return chooser;
+       /* FIXME, why can't this go in init ()? */
+       return g_object_new (EMPATHY_TYPE_PRESENCE_CHOOSER,
+               "has-entry", TRUE,
+               NULL);
 }
 
 static void
 presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
 {
        EmpathyPresenceChooserPriv *priv;
-       McPresence                  state;
-       McPresence                  flash_state;
-       const gchar                *status;
+       TpConnectionPresenceType    state;
+       gchar                      *status;
        GtkTreeModel               *model;
        GtkTreeIter                 iter;
        gboolean valid, match_state = FALSE, match = FALSE;
+       GtkWidget                  *entry;
 
        priv = GET_PRIV (chooser);
 
@@ -833,9 +1015,14 @@ presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
                return;
        }
 
-       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);
+       state = get_state_and_status (chooser, &status);
+       priv->state = state;
+
+       /* An unset presence here doesn't make any sense. Force it to appear as
+        * offline. */
+       if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
+               state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
+       }
 
        /* look through the model and attempt to find a matching state */
        model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
@@ -843,7 +1030,7 @@ presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
             valid;
             valid = gtk_tree_model_iter_next (model, &iter)) {
                int m_type;
-               McPresence m_state;
+               TpConnectionPresenceType m_state;
                char *m_status;
 
                gtk_tree_model_get (model, &iter,
@@ -871,7 +1058,7 @@ presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
                                COL_STATUS_TEXT, &m_status,
                                -1);
 
-               match = !strcmp (status, m_status);
+               match = !tp_strdiff (status, m_status);
 
                g_free (m_status);
 
@@ -889,78 +1076,16 @@ presence_chooser_presence_changed_cb (EmpathyPresenceChooser *chooser)
                ui_set_custom_state (chooser, state, status);
        }
 
-       if (flash_state != MC_PRESENCE_UNSET) {
-               presence_chooser_flash_start (chooser, state, flash_state);
-       }
-       else {
-               presence_chooser_flash_stop (chooser, state);
-       }
-}
-
-static gboolean
-presence_chooser_flash_timeout_cb (EmpathyPresenceChooser *chooser)
-{
-       EmpathyPresenceChooserPriv *priv;
-       McPresence                  state;
-       static gboolean             on = FALSE;
-       GtkWidget                  *entry;
-
-       priv = GET_PRIV (chooser);
-
-       if (on) {
-               state = priv->flash_state_1;
-       }
-       else {
-               state = priv->flash_state_2;
-       }
-
        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;
-
-       return TRUE;
-}
-
-static void
-presence_chooser_flash_start (EmpathyPresenceChooser *chooser,
-                             McPresence              state_1,
-                             McPresence              state_2)
-{
-       EmpathyPresenceChooserPriv *priv;
-
-       g_return_if_fail (EMPATHY_IS_PRESENCE_CHOOSER (chooser));
-
-       priv = GET_PRIV (chooser);
-
-       priv->flash_state_1 = state_1;
-       priv->flash_state_2 = state_2;
-
-       if (!priv->flash_timeout_id) {
-               priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
-                       (GSourceFunc) presence_chooser_flash_timeout_cb,
-                       chooser);
-       }
-}
-
-static void
-presence_chooser_flash_stop (EmpathyPresenceChooser *chooser,
-                            McPresence             state)
-{
-       EmpathyPresenceChooserPriv *priv = GET_PRIV (chooser);
-       GtkWidget *entry;
-
-       if (priv->flash_timeout_id) {
-               g_source_remove (priv->flash_timeout_id);
-               priv->flash_timeout_id = 0;
-       }
+             GTK_ENTRY_ICON_PRIMARY,
+             empathy_icon_name_for_presence (state));
 
        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_editable_set_editable (GTK_EDITABLE (entry),
+           state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
+
+       g_free (status);
 }
 
 /**
@@ -981,21 +1106,21 @@ empathy_presence_chooser_create_menu (void)
 
        menu = gtk_menu_new ();
 
-       for (i = 0; i < G_N_ELEMENTS (states); i += 2) {
+       for (i = 0; states[i].state != TP_CONNECTION_PRESENCE_TYPE_UNSET; i++) {
                GList       *list, *l;
 
-               status = empathy_presence_get_default_message (states[i]);
+               status = empathy_presence_get_default_message (states[i].state);
                presence_chooser_menu_add_item (menu,
                                                status,
-                                               states[i]);
+                                               states[i].state);
 
-               if (states[i+1]) {
+               if (states[i].customisable) {
                        /* Set custom messages if wanted */
-                       list = empathy_status_presets_get (states[i], 5);
+                       list = empathy_status_presets_get (states[i].state, 5);
                        for (l = list; l; l = l->next) {
                                presence_chooser_menu_add_item (menu,
                                                                l->data,
-                                                               states[i]);
+                                                               states[i].state);
                        }
                        g_list_free (list);
                }
@@ -1008,7 +1133,7 @@ empathy_presence_chooser_create_menu (void)
        gtk_widget_show (item);
 
        /* Custom messages */
-       item = gtk_image_menu_item_new_with_label (_("Custom messages..."));
+       item = gtk_image_menu_item_new_with_label (_("Custom messages"));
        image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
        gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
        gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
@@ -1026,7 +1151,7 @@ empathy_presence_chooser_create_menu (void)
 static void
 presence_chooser_menu_add_item (GtkWidget   *menu,
                                const gchar *str,
-                               McPresence   state)
+                               TpConnectionPresenceType state)
 {
        GtkWidget   *item;
        GtkWidget   *image;
@@ -1043,6 +1168,7 @@ presence_chooser_menu_add_item (GtkWidget   *menu,
        gtk_widget_show (image);
 
        gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
+       gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
        gtk_widget_show (item);
 
        g_object_set_data_full (G_OBJECT (item),
@@ -1058,7 +1184,7 @@ static void
 presence_chooser_noncustom_activate_cb (GtkWidget *item,
                                        gpointer   user_data)
 {
-       McPresence   state;
+       TpConnectionPresenceType state;
        const gchar *status;
 
        status = g_object_get_data (G_OBJECT (item), "status");
@@ -1068,7 +1194,7 @@ presence_chooser_noncustom_activate_cb (GtkWidget *item,
 }
 
 static void
-presence_chooser_set_state (McPresence   state,
+presence_chooser_set_state (TpConnectionPresenceType state,
                            const gchar *status)
 {
        EmpathyIdle *idle;