]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-avatar-chooser.c
Add en_GB in gitignore
[empathy.git] / libempathy-gtk / empathy-avatar-chooser.c
index 08dbfad18a83d7f9f531ca97d378638067ce8cec..5d1c90fcba2b3c10886b48582d39a0ab630fba5e 100644 (file)
 
 #include <string.h>
 
-#include <glib/gi18n.h>
+#include <glib/gi18n-lib.h>
 #include <gtk/gtk.h>
 #include <gio/gio.h>
 
 #include <libempathy/empathy-utils.h>
+#include <libempathy/empathy-contact-factory.h>
+
 #include "empathy-avatar-chooser.h"
 #include "empathy-conf.h"
 #include "empathy-ui-utils.h"
 
 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAvatarChooser)
 typedef struct {
-       gchar *image_data;
-       gsize  image_data_size;
-       gchar *mime_type;
+       EmpathyContactFactory   *contact_factory;
+       McAccount               *account;
+       EmpathyTpContactFactory *tp_contact_factory;
+       GtkFileChooser          *chooser_dialog;
+
+       gulong ready_handler_id;
+
+       EmpathyAvatar *avatar;
 } EmpathyAvatarChooserPriv;
 
 static void       avatar_chooser_finalize              (GObject              *object);
-static void       avatar_chooser_set_image_from_data   (EmpathyAvatarChooser *chooser,
-                                                       gchar                *data,
-                                                       gsize                 size);
+static void       avatar_chooser_set_account           (EmpathyAvatarChooser *self,
+                                                       McAccount            *account);
+static void       avatar_chooser_set_image             (EmpathyAvatarChooser *chooser,
+                                                       EmpathyAvatar        *avatar,
+                                                       GdkPixbuf            *pixbuf,
+                                                       gboolean              set_locally);
 static gboolean   avatar_chooser_drag_motion_cb        (GtkWidget            *widget,
                                                        GdkDragContext       *context,
                                                        gint                  x,
@@ -84,6 +94,11 @@ enum {
        LAST_SIGNAL
 };
 
+enum {
+       PROP_0,
+       PROP_ACCOUNT
+};
+
 static guint signals [LAST_SIGNAL];
 
 G_DEFINE_TYPE (EmpathyAvatarChooser, empathy_avatar_chooser, GTK_TYPE_BUTTON);
@@ -101,12 +116,51 @@ static const GtkTargetEntry drop_types[] = {
        { URI_LIST_TYPE, 0, DND_TARGET_TYPE_URI_LIST },
 };
 
+static void
+avatar_chooser_get_property (GObject    *object,
+                            guint       param_id,
+                            GValue     *value,
+                            GParamSpec *pspec)
+{
+       EmpathyAvatarChooserPriv *priv = GET_PRIV (object);
+
+       switch (param_id) {
+       case PROP_ACCOUNT:
+               g_value_set_object (value, priv->account);
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+               break;
+       }
+}
+
+static void
+avatar_chooser_set_property (GObject      *object,
+                            guint         param_id,
+                            const GValue *value,
+                            GParamSpec   *pspec)
+{
+       EmpathyAvatarChooser *self = EMPATHY_AVATAR_CHOOSER (object);
+
+       switch (param_id) {
+       case PROP_ACCOUNT:
+               avatar_chooser_set_account (self, g_value_get_object (value));
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+               break;
+       }
+}
+
 static void
 empathy_avatar_chooser_class_init (EmpathyAvatarChooserClass *klass)
 {
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
+       GParamSpec *param_spec;
 
        object_class->finalize = avatar_chooser_finalize;
+       object_class->get_property = avatar_chooser_get_property;
+       object_class->set_property = avatar_chooser_set_property;
 
        signals[CHANGED] =
                g_signal_new ("changed",
@@ -117,6 +171,17 @@ empathy_avatar_chooser_class_init (EmpathyAvatarChooserClass *klass)
                              g_cclosure_marshal_VOID__VOID,
                              G_TYPE_NONE, 0);
 
+       param_spec = g_param_spec_object ("account",
+                                         "McAccount",
+                                         "McAccount whose avatar should be "
+                                         "shown and modified by this widget",
+                                         MC_TYPE_ACCOUNT,
+                                         G_PARAM_READWRITE |
+                                         G_PARAM_STATIC_STRINGS);
+       g_object_class_install_property (object_class,
+                                        PROP_ACCOUNT,
+                                        param_spec);
+
        g_type_class_add_private (object_class, sizeof (EmpathyAvatarChooserPriv));
 }
 
@@ -149,6 +214,8 @@ empathy_avatar_chooser_init (EmpathyAvatarChooser *chooser)
                          G_CALLBACK (avatar_chooser_clicked_cb),
                          chooser);
 
+       priv->contact_factory = empathy_contact_factory_dup_singleton ();
+
        empathy_avatar_chooser_set (chooser, NULL);
 }
 
@@ -159,87 +226,490 @@ avatar_chooser_finalize (GObject *object)
 
        priv = GET_PRIV (object);
 
-       g_free (priv->image_data);
-       g_free (priv->mime_type);
+       avatar_chooser_set_account (EMPATHY_AVATAR_CHOOSER (object), NULL);
+       g_assert (priv->account == NULL);
+       g_assert (priv->tp_contact_factory == NULL);
+
+       g_object_unref (priv->contact_factory);
+
+       if (priv->avatar != NULL) {
+               empathy_avatar_unref (priv->avatar);
+       }
 
        G_OBJECT_CLASS (empathy_avatar_chooser_parent_class)->finalize (object);
 }
 
 static void
-avatar_chooser_set_pixbuf (EmpathyAvatarChooser *chooser,
-                          GdkPixbuf            *pixbuf)
+avatar_chooser_tp_cf_ready_cb (EmpathyTpContactFactory *tp_cf,
+                              GParamSpec              *unused,
+                              EmpathyAvatarChooser    *self)
+{
+       EmpathyAvatarChooserPriv *priv = GET_PRIV (self);
+       gboolean ready;
+
+       /* sanity check that we're listening on the right ETpCF */
+       g_assert (priv->tp_contact_factory == tp_cf);
+
+       ready = empathy_tp_contact_factory_is_ready (tp_cf);
+       gtk_widget_set_sensitive (GTK_WIDGET (self), ready);
+}
+
+static void
+avatar_chooser_set_account (EmpathyAvatarChooser *self,
+                           McAccount            *account)
+{
+       EmpathyAvatarChooserPriv *priv = GET_PRIV (self);
+
+       if (priv->account != NULL) {
+               g_object_unref (priv->account);
+               priv->account = NULL;
+
+               g_assert (priv->tp_contact_factory != NULL);
+
+               g_signal_handler_disconnect (priv->tp_contact_factory,
+                       priv->ready_handler_id);
+               priv->ready_handler_id = 0;
+
+               g_object_unref (priv->tp_contact_factory);
+               priv->tp_contact_factory = NULL;
+       }
+
+       if (account != NULL) {
+               priv->account = g_object_ref (account);
+               priv->tp_contact_factory = g_object_ref (
+                       empathy_contact_factory_get_tp_factory (
+                               priv->contact_factory, priv->account));
+
+               priv->ready_handler_id = g_signal_connect (
+                       priv->tp_contact_factory, "notify::ready",
+                       G_CALLBACK (avatar_chooser_tp_cf_ready_cb), self);
+               avatar_chooser_tp_cf_ready_cb (priv->tp_contact_factory, NULL,
+                       self);
+       }
+}
+
+static void
+avatar_chooser_error_show (EmpathyAvatarChooser *chooser,
+                          const gchar          *primary_text,
+                          const gchar          *secondary_text)
+{
+       GtkWidget *parent;
+       GtkWidget *dialog;
+
+       parent = gtk_widget_get_toplevel (GTK_WIDGET (chooser));
+       if (!GTK_IS_WINDOW (parent)) {
+               parent = NULL;
+       }
+
+       dialog = gtk_message_dialog_new (parent ? GTK_WINDOW (parent) : NULL,
+                                        GTK_DIALOG_MODAL,
+                                        GTK_MESSAGE_WARNING,
+                                        GTK_BUTTONS_CLOSE,
+                                        "%s", primary_text);
+
+       if (secondary_text != NULL) {
+               gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
+                                                         "%s", secondary_text);
+       }
+
+       g_signal_connect (dialog, "response",
+                         G_CALLBACK (gtk_widget_destroy), NULL);
+       gtk_widget_show (dialog);
+
+}
+
+static gboolean
+str_in_strv (const gchar  *str,
+            gchar **strv)
+{
+       if (strv == NULL) {
+               return FALSE;
+       }
+
+       while (*strv != NULL) {
+               if (g_str_equal (str, *strv)) {
+                       return TRUE;
+               }
+               strv++;
+       }
+       return FALSE;
+}
+
+/* The caller must free the strings stored in satisfactory_format_name and
+ * satisfactory_mime_type.
+ */
+static gboolean
+avatar_chooser_need_mime_type_conversion (const gchar *current_mime_type,
+                                         gchar      **accepted_mime_types,
+                                         gchar      **satisfactory_format_name,
+                                         gchar      **satisfactory_mime_type)
+{
+       gchar   *good_mime_types[] = {"image/jpeg", "image/png", NULL};
+       guint    i;
+       GSList  *formats, *l;
+       gboolean found = FALSE;
+
+       *satisfactory_format_name = NULL;
+       *satisfactory_mime_type = NULL;
+
+       /* If there is no accepted format there is nothing we can do */
+       if (accepted_mime_types == NULL || *accepted_mime_types == NULL) {
+               return TRUE;
+       }
+
+       /* If the current mime type is good and accepted, don't change it!
+        * jpeg is compress better pictures, but png is better for logos and
+        * could have an alpha layer. */
+       if (str_in_strv (current_mime_type, good_mime_types) &&
+           str_in_strv (current_mime_type, accepted_mime_types)) {
+               *satisfactory_mime_type = g_strdup (current_mime_type);
+               *satisfactory_format_name = g_strdup (current_mime_type +
+                                                     strlen ("image/"));
+               return FALSE;
+       }
+
+       /* The current mime type is either not accepted or not good to use.
+        * Check if one of the good format is supported... */
+       for (i = 0; good_mime_types[i] != NULL;  i++) {
+               if (str_in_strv (good_mime_types[i], accepted_mime_types)) {
+                       *satisfactory_mime_type = g_strdup (good_mime_types[i]);
+                       *satisfactory_format_name = g_strdup (good_mime_types[i] +
+                                                             strlen ("image/"));
+                       return TRUE;
+               }
+       }
+
+       /* Pick the first supported format we can write */
+       formats = gdk_pixbuf_get_formats ();
+       for (l = formats; !found && l != NULL; l = l->next) {
+               GdkPixbufFormat *format = l->data;
+               gchar **format_mime_types;
+               gchar **iter;
+
+               if (!gdk_pixbuf_format_is_writable (format)) {
+                       continue;
+               }
+
+               format_mime_types = gdk_pixbuf_format_get_mime_types (format);
+               for (iter = format_mime_types; *iter != NULL; iter++) {
+                       if (str_in_strv (*iter, accepted_mime_types)) {
+                               *satisfactory_format_name = gdk_pixbuf_format_get_name (format);
+                               *satisfactory_mime_type = g_strdup (*iter);
+                               found = TRUE;
+                               break;
+                       }
+               }
+               g_strfreev (format_mime_types);
+       }
+       g_slist_free (formats);
+
+       return TRUE;
+}
+
+static EmpathyAvatar *
+avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
+                                       GdkPixbuf            *pixbuf,
+                                       EmpathyAvatar        *avatar)
 {
        EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
-       GtkWidget                *image;
-       GdkPixbuf                *pixbuf_view = NULL;
-       GdkPixbuf                *pixbuf_save = NULL;
-       GError                   *error = NULL;
-
-       g_free (priv->image_data);
-       priv->image_data = NULL;
-       priv->image_data_size = 0;
-       g_free (priv->mime_type);
-       priv->mime_type = NULL;
-
-       if (pixbuf) {
-               pixbuf_view = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_VIEW);
-               pixbuf_save = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_SAVE);
-
-               if (!gdk_pixbuf_save_to_buffer (pixbuf_save,
-                                               &priv->image_data,
-                                               &priv->image_data_size,
-                                               "png",
-                                               &error, NULL)) {
-                       DEBUG ("Failed to save pixbuf: %s",
-                               error ? error->message : "No error given");
-                       g_clear_error (&error);
+       EmpathyTpContactFactory  *tp_cf = priv->tp_contact_factory;
+       guint                     max_width = 0, max_height = 0, max_size = 0;
+       gchar                   **mime_types = NULL;
+       gboolean                  needs_conversion = FALSE;
+       gint                      width, height;
+       gchar                    *new_format_name = NULL;
+       gchar                    *new_mime_type = NULL;
+       gdouble                   min_factor, max_factor;
+       gdouble                   factor;
+       gchar                    *converted_image_data = NULL;
+       gsize                     converted_image_size = 0;
+
+       /* This should only be called if the user is setting a new avatar,
+        * which should only be allowed once the avatar requirements have been
+        * discovered.
+        */
+       g_return_val_if_fail (tp_cf != NULL, NULL);
+       g_return_val_if_fail (empathy_tp_contact_factory_is_ready (tp_cf),
+               NULL);
+
+       g_object_get (tp_cf,
+               "avatar-mime-types", &mime_types, /* Needs g_strfreev-ing */
+               "avatar-max-width", &max_width,
+               "avatar-max-height", &max_height,
+               "avatar-max-size", &max_size,
+               NULL);
+
+       /* Smaller is the factor, smaller will be the image.
+        * 0 is an empty image, 1 is the full size. */
+       min_factor = 0;
+       max_factor = 1;
+       factor = 1;
+
+       /* Check if we need to convert to another image format */
+       if (avatar_chooser_need_mime_type_conversion (avatar->format,
+                                                     mime_types,
+                                                     &new_format_name,
+                                                     &new_mime_type)) {
+               DEBUG ("Format conversion needed, we'll use mime type '%s' "
+                      "and format name '%s'. Current mime type is '%s'",
+                      new_mime_type, new_format_name, avatar->format);
+               needs_conversion = TRUE;
+       }
+       g_strfreev (mime_types);
+
+       /* If there is no format we can use, report error to the user. */
+       if (new_mime_type == NULL || new_format_name == NULL) {
+               avatar_chooser_error_show (chooser, _("Couldn't convert image"),
+                               _("None of the accepted image formats is "
+                                 "supported on your system"));
+               return NULL;
+       }
+
+       /* If width or height are too big, it needs converting. */
+       width = gdk_pixbuf_get_width (pixbuf);
+       height = gdk_pixbuf_get_height (pixbuf);
+       if ((max_width > 0 && width > max_width) ||
+           (max_height > 0 && height > max_height)) {
+               gdouble h_factor, v_factor;
+
+               h_factor = (gdouble) max_width / width;
+               v_factor = (gdouble) max_height / height;
+               factor = max_factor = MIN (h_factor, v_factor);
+
+               DEBUG ("Image dimensions (%dx%d) are too big. Max is %dx%d.",
+                      width, height, max_width, max_height);
+
+               needs_conversion = TRUE;
+       }
+
+       /* If the data len is too big and no other conversion is needed,
+        * try with a lower factor. */
+       if (max_size > 0 && avatar->len > max_size && !needs_conversion) {
+               DEBUG ("Image data (%"G_GSIZE_FORMAT" bytes) is too big "
+                      "(max is %u bytes), conversion needed.",
+                      avatar->len, max_size);
+
+               factor = 0.5;
+               needs_conversion = TRUE;
+       }
+
+       /* If no conversion is needed, return the avatar */
+       if (!needs_conversion) {
+               g_free (new_format_name);
+               g_free (new_mime_type);
+               return empathy_avatar_ref (avatar);
+       }
+
+       do {
+               GdkPixbuf *pixbuf_scaled = NULL;
+               gboolean   saved;
+               gint       new_width, new_height;
+               GError    *error = NULL;
+
+               g_free (converted_image_data);
+
+               if (factor != 1) {
+                       new_width = width * factor;
+                       new_height = height * factor;
+                       pixbuf_scaled = gdk_pixbuf_scale_simple (pixbuf,
+                                                                new_width,
+                                                                new_height,
+                                                                GDK_INTERP_HYPER);
                } else {
-                       priv->mime_type = "image/png";
+                       new_width = width;
+                       new_height = height;
+                       pixbuf_scaled = g_object_ref (pixbuf);
                }
-               image = gtk_image_new_from_pixbuf (pixbuf_view);
 
-               g_object_unref (pixbuf_save);
-               g_object_unref (pixbuf_view);
-       } else {
-               image = gtk_image_new_from_icon_name ("stock_person",
-                                                     GTK_ICON_SIZE_DIALOG);
+               DEBUG ("Trying with factor %f (%dx%d) and format %s...", factor,
+                       new_width, new_height, new_format_name);
+
+               saved = gdk_pixbuf_save_to_buffer (pixbuf_scaled,
+                                                  &converted_image_data,
+                                                  &converted_image_size,
+                                                  new_format_name,
+                                                  &error, NULL);
+
+               if (!saved) {
+                       g_free (new_format_name);
+                       g_free (new_mime_type);
+                       avatar_chooser_error_show (chooser,
+                               _("Couldn't convert image"),
+                               error ? error->message : NULL);
+                       g_clear_error (&error);
+                       return NULL;
+               }
+
+               DEBUG ("Produced an image data of %"G_GSIZE_FORMAT" bytes.",
+                       converted_image_size);
+
+               if (max_size == 0)
+                       break;
+
+               /* Make a binary search for the bigest factor that produce
+                * an image data size less than max_size */
+               if (converted_image_size > max_size)
+                       max_factor = factor;
+               if (converted_image_size < max_size)
+                       min_factor = factor;
+               factor = (min_factor + max_factor)/2;
+
+               /* We are done if either:
+                * - min_factor == max_factor. That happens if we resized to
+                *   the max required dimension and the produced data size is
+                *   less than max_size.
+                * - The data size is close enough to max_size. Here we accept
+                *   a difference of 1k.
+                */
+       } while (min_factor != max_factor &&
+                ABS (max_size - converted_image_size) > 1024);
+       g_free (new_format_name);
+
+       /* Takes ownership of new_mime_type and converted_image_data */
+       avatar = empathy_avatar_new (converted_image_data,
+               converted_image_size, new_mime_type, NULL);
+
+       return avatar;
+}
+
+static void
+avatar_chooser_clear_image (EmpathyAvatarChooser *chooser)
+{
+       EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
+       GtkWidget *image;
+
+       if (priv->avatar == NULL) {
+               return;
        }
 
+       empathy_avatar_unref (priv->avatar);
+       priv->avatar = NULL;
+
+       image = gtk_image_new_from_icon_name ("stock_person", GTK_ICON_SIZE_DIALOG);
        gtk_button_set_image (GTK_BUTTON (chooser), image);
        g_signal_emit (chooser, signals[CHANGED], 0);
 }
 
 static void
-avatar_chooser_set_image_from_file (EmpathyAvatarChooser *chooser,
-                                   const gchar          *filename)
+avatar_chooser_set_image_from_data (EmpathyAvatarChooser *chooser,
+                                   gchar                *data,
+                                   gsize                 size,
+                                   gboolean              set_locally)
 {
-       GdkPixbuf *pixbuf;
-       GError    *error = NULL;
+       GdkPixbuf     *pixbuf;
+       EmpathyAvatar *avatar = NULL;
+       gchar         *mime_type = NULL;
 
-       if (!(pixbuf = gdk_pixbuf_new_from_file (filename, &error))) {
-               DEBUG ("Failed to load pixbuf from file: %s",
-                       error ? error->message : "No error given");
-               g_clear_error (&error);
+       if (data == NULL) {
+               avatar_chooser_clear_image (chooser);
+               return;
        }
 
-       avatar_chooser_set_pixbuf (chooser, pixbuf);
-       if (pixbuf) {
-               g_object_unref (pixbuf);
+       pixbuf = empathy_pixbuf_from_data_and_mime (data, size, &mime_type);
+       if (pixbuf == NULL) {
+               g_free (data);
+               return;
        }
+
+       /* avatar takes ownership of data and mime_type */
+       avatar = empathy_avatar_new (data, size, mime_type, NULL);
+
+       avatar_chooser_set_image (chooser, avatar, pixbuf, set_locally);
 }
 
 static void
-avatar_chooser_set_image_from_data (EmpathyAvatarChooser *chooser,
-                                   gchar                *data,
-                                   gsize                 size)
+avatar_chooser_set_image_from_avatar (EmpathyAvatarChooser *chooser,
+                                     EmpathyAvatar        *avatar,
+                                     gboolean              set_locally)
 {
        GdkPixbuf *pixbuf;
+       gchar     *mime_type = NULL;
 
-       pixbuf = empathy_pixbuf_from_data (data, size);
-       avatar_chooser_set_pixbuf (chooser, pixbuf);
-       if (pixbuf) {
-               g_object_unref (pixbuf);
+       g_assert (avatar != NULL);
+
+       pixbuf = empathy_pixbuf_from_data_and_mime (avatar->data,
+                                                   avatar->len,
+                                                   &mime_type);
+       if (pixbuf == NULL) {
+               DEBUG ("couldn't make a pixbuf from avatar; giving up");
+               return;
+       }
+
+       if (avatar->format == NULL) {
+               avatar->format = mime_type;
+       } else {
+               if (strcmp (mime_type, avatar->format)) {
+                       DEBUG ("avatar->format is %s; gdkpixbuf yields %s!",
+                               avatar->format, mime_type);
+               }
+               g_free (mime_type);
        }
+
+       empathy_avatar_ref (avatar);
+
+       avatar_chooser_set_image (chooser, avatar, pixbuf, set_locally);
+}
+
+static void
+avatar_chooser_set_image (EmpathyAvatarChooser *chooser,
+                         EmpathyAvatar        *avatar,
+                         GdkPixbuf            *pixbuf,
+                         gboolean              set_locally)
+{
+       EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
+       GdkPixbuf                *pixbuf_view;
+       GtkWidget                *image;
+
+       g_assert (avatar != NULL);
+       g_assert (pixbuf != NULL);
+
+       if (set_locally) {
+               EmpathyAvatar *conv;
+
+               conv = avatar_chooser_maybe_convert_and_scale (chooser,
+                       pixbuf, avatar);
+               empathy_avatar_unref (avatar);
+
+               if (conv == NULL) {
+                       /* An error occured; don't change the avatar. */
+                       return;
+               }
+
+               avatar = conv;
+       }
+
+       if (priv->avatar != NULL) {
+               empathy_avatar_unref (priv->avatar);
+       }
+       priv->avatar = avatar;
+
+       pixbuf_view = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_VIEW);
+       image = gtk_image_new_from_pixbuf (pixbuf_view);
+
+       gtk_button_set_image (GTK_BUTTON (chooser), image);
+       g_signal_emit (chooser, signals[CHANGED], 0);
+
+       g_object_unref (pixbuf_view);
+       g_object_unref (pixbuf);
+}
+
+static void
+avatar_chooser_set_image_from_file (EmpathyAvatarChooser *chooser,
+                                   const gchar          *filename)
+{
+       gchar  *image_data = NULL;
+       gsize   image_size = 0;
+       GError *error = NULL;
+
+       if (!g_file_get_contents (filename, &image_data, &image_size, &error)) {
+               DEBUG ("Failed to load image from '%s': %s", filename,
+                       error ? error->message : "No error given");
+
+               g_clear_error (&error);
+               return;
+       }
+
+       avatar_chooser_set_image_from_data (chooser, image_data, image_size, TRUE);
 }
 
 static gboolean
@@ -369,9 +839,10 @@ avatar_chooser_drag_data_received_cb (GtkWidget          *widget,
                                                                  data, size,
                                                                  NULL, NULL);
                                if (bytes_read != -1) {
-                                       avatar_chooser_set_image_from_data (chooser,
-                                                                           data,
-                                                                           (gsize) bytes_read);
+                                       avatar_chooser_set_image_from_data (
+                                               chooser, data,
+                                               (gsize) bytes_read,
+                                               TRUE);
                                        handled = TRUE;
                                }
 
@@ -425,6 +896,20 @@ avatar_chooser_response_cb (GtkWidget            *widget,
                            gint                  response,
                            EmpathyAvatarChooser *chooser)
 {
+       EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
+
+       priv->chooser_dialog = NULL;
+
+       if (response == GTK_RESPONSE_CANCEL) {
+               goto out;
+       }
+
+       /* Check if we went non-ready since displaying the dialog. */
+       if (!empathy_tp_contact_factory_is_ready (priv->tp_contact_factory)) {
+               DEBUG ("Can't set avatar when contact factory isn't ready.");
+               goto out;
+       }
+
        if (response == GTK_RESPONSE_OK) {
                gchar *filename;
                gchar *path;
@@ -442,9 +927,11 @@ avatar_chooser_response_cb (GtkWidget            *widget,
                }
        }
        else if (response == GTK_RESPONSE_NO) {
-               avatar_chooser_set_image_from_data (chooser, NULL, 0);
+               /* This corresponds to "No Image", not to "Cancel" */
+               avatar_chooser_clear_image (chooser);
        }
 
+out:
        gtk_widget_destroy (widget);
 }
 
@@ -458,8 +945,14 @@ avatar_chooser_clicked_cb (GtkWidget            *button,
        const gchar    *default_dir = DEFAULT_DIR;
        const gchar    *pics_dir;
        GtkFileFilter  *filter;
+       EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
+
+       if (priv->chooser_dialog) {
+               gtk_window_present (GTK_WINDOW (priv->chooser_dialog));
+               return;
+       }
 
-       chooser_dialog = GTK_FILE_CHOOSER (
+       priv->chooser_dialog = GTK_FILE_CHOOSER (
                gtk_file_chooser_dialog_new (_("Select Your Avatar Image"),
                                             empathy_get_toplevel_window (GTK_WIDGET (chooser)),
                                             GTK_FILE_CHOOSER_ACTION_OPEN,
@@ -470,6 +963,8 @@ avatar_chooser_clicked_cb (GtkWidget            *button,
                                             GTK_STOCK_OPEN,
                                             GTK_RESPONSE_OK,
                                             NULL));
+       chooser_dialog = priv->chooser_dialog;
+       gtk_window_set_destroy_with_parent (GTK_WINDOW (chooser_dialog), TRUE);
 
        /* Get special dirs */
        empathy_conf_get_string (empathy_conf_get (),
@@ -542,7 +1037,7 @@ avatar_chooser_clicked_cb (GtkWidget            *button,
 }
 
 GtkWidget *
-empathy_avatar_chooser_new (void)
+empathy_avatar_chooser_new ()
 {
        return g_object_new (EMPATHY_TYPE_AVATAR_CHOOSER, NULL);
 }
@@ -553,9 +1048,11 @@ empathy_avatar_chooser_set (EmpathyAvatarChooser *chooser,
 {
        g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (chooser));
 
-       avatar_chooser_set_image_from_data (chooser,
-                                           avatar ? avatar->data : NULL,
-                                           avatar ? avatar->len : 0);
+       if (avatar != NULL) {
+               avatar_chooser_set_image_from_avatar (chooser, avatar, FALSE);
+       } else {
+               avatar_chooser_clear_image (chooser);
+       }
 }
 
 void
@@ -570,14 +1067,26 @@ empathy_avatar_chooser_get_image_data (EmpathyAvatarChooser  *chooser,
 
        priv = GET_PRIV (chooser);
 
-       if (data) {
-               *data = priv->image_data;
-       }
-       if (data_size) {
-               *data_size = priv->image_data_size;
-       }
-       if (mime_type) {
-               *mime_type = priv->mime_type;
+       if (priv->avatar != NULL) {
+               if (data != NULL) {
+                       *data = priv->avatar->data;
+               }
+               if (data_size != NULL) {
+                       *data_size = priv->avatar->len;
+               }
+               if (mime_type != NULL) {
+                       *mime_type = priv->avatar->format;
+               }
+       } else {
+               if (data != NULL) {
+                       *data = NULL;
+               }
+               if (data_size != NULL) {
+                       *data_size = 0;
+               }
+               if (mime_type != NULL) {
+                       *mime_type = NULL;
+               }
        }
 }