]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-avatar-chooser.c
Merge branch 'sasl'
[empathy.git] / libempathy-gtk / empathy-avatar-chooser.c
index 7618eaee9054ad410b9d6b042f960e0d08cc3d4b..98bb44614e444b1a5a0a756d63cfa19e2ecfc889 100644 (file)
@@ -14,8 +14,8 @@
  *
  * You should have received a copy of the GNU General Public
  * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA  02110-1301  USA
  *
  * Authors: Based on Novell's e-image-chooser.
  *          Xavier Claessens <xclaesse@gmail.com>
 
 #include <string.h>
 
-#include <glib/gi18n.h>
+#include <glib/gi18n-lib.h>
 #include <gtk/gtk.h>
 #include <gio/gio.h>
 
+#include <libempathy/empathy-gsettings.h>
 #include <libempathy/empathy-utils.h>
+
 #include "empathy-avatar-chooser.h"
-#include "empathy-conf.h"
+#include "empathy-images.h"
 #include "empathy-ui-utils.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
 #include <libempathy/empathy-debug.h>
 
+/**
+ * SECTION:empathy-avatar-chooser
+ * @title: EmpathyAvatarChooser
+ * @short_description: A widget used to change avatar
+ * @include: libempathy-gtk/empathy-avatar-chooser.h
+ *
+ * #EmpathyAvatarChooser is a widget which extends #GtkButton to
+ * provide a way of changing avatar.
+ */
+
+/**
+ * EmpathyAvatarChooser:
+ * @parent: parent object
+ *
+ * Widget which extends #GtkButton to provide a way of changing avatar.
+ */
+
 #define AVATAR_SIZE_SAVE 96
 #define AVATAR_SIZE_VIEW 64
 #define DEFAULT_DIR DATADIR"/pixmaps/faces"
 
 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAvatarChooser)
 typedef struct {
-       EmpathyContactFactory   *contact_factory;
-       McAccount               *account;
-       EmpathyTpContactFactory *tp_contact_factory;
+       TpConnection            *connection;
+       GtkFileChooser          *chooser_dialog;
 
        gulong ready_handler_id;
 
        EmpathyAvatar *avatar;
+       GSettings *gsettings_ui;
 } EmpathyAvatarChooserPriv;
 
 static void       avatar_chooser_finalize              (GObject              *object);
-static void       avatar_chooser_set_account           (EmpathyAvatarChooser *self,
-                                                       McAccount            *account);
+static void       avatar_chooser_set_connection        (EmpathyAvatarChooser *self,
+                                                       TpConnection         *connection);
 static void       avatar_chooser_set_image             (EmpathyAvatarChooser *chooser,
-                                                       gchar                *data,
-                                                       gsize                 size,
+                                                       EmpathyAvatar        *avatar,
+                                                       GdkPixbuf            *pixbuf,
                                                        gboolean              set_locally);
 static gboolean   avatar_chooser_drag_motion_cb        (GtkWidget            *widget,
                                                        GdkDragContext       *context,
@@ -93,8 +112,7 @@ enum {
 
 enum {
        PROP_0,
-       PROP_CONTACT_FACTORY,
-       PROP_ACCOUNT
+       PROP_CONNECTION
 };
 
 static guint signals [LAST_SIGNAL];
@@ -115,20 +133,16 @@ static const GtkTargetEntry drop_types[] = {
 };
 
 static void
-empathy_avatar_chooser_get_property (GObject    *object,
-                                    guint       param_id,
-                                    GValue     *value,
-                                    GParamSpec *pspec)
+avatar_chooser_get_property (GObject    *object,
+                            guint       param_id,
+                            GValue     *value,
+                            GParamSpec *pspec)
 {
        EmpathyAvatarChooserPriv *priv = GET_PRIV (object);
 
        switch (param_id) {
-       case PROP_CONTACT_FACTORY:
-               g_assert (priv->contact_factory != NULL);
-               g_value_set_object (value, priv->contact_factory);
-               break;
-       case PROP_ACCOUNT:
-               g_value_set_object (value, priv->account);
+       case PROP_CONNECTION:
+               g_value_set_object (value, priv->connection);
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
@@ -137,24 +151,16 @@ empathy_avatar_chooser_get_property (GObject    *object,
 }
 
 static void
-empathy_avatar_chooser_set_property (GObject      *object,
-                                    guint         param_id,
-                                    const GValue *value,
-                                    GParamSpec   *pspec)
+avatar_chooser_set_property (GObject      *object,
+                            guint         param_id,
+                            const GValue *value,
+                            GParamSpec   *pspec)
 {
        EmpathyAvatarChooser *self = EMPATHY_AVATAR_CHOOSER (object);
-       EmpathyAvatarChooserPriv *priv = GET_PRIV (self);
 
        switch (param_id) {
-       case PROP_CONTACT_FACTORY:
-               priv->contact_factory = g_value_get_object (value);
-
-               g_assert (priv->contact_factory != NULL);
-               g_object_ref (priv->contact_factory);
-
-               break;
-       case PROP_ACCOUNT:
-               avatar_chooser_set_account (self, g_value_get_object (value));
+       case PROP_CONNECTION:
+               avatar_chooser_set_connection (self, g_value_get_object (value));
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
@@ -169,9 +175,16 @@ empathy_avatar_chooser_class_init (EmpathyAvatarChooserClass *klass)
        GParamSpec *param_spec;
 
        object_class->finalize = avatar_chooser_finalize;
-       object_class->get_property = empathy_avatar_chooser_get_property;
-       object_class->set_property = empathy_avatar_chooser_set_property;
-
+       object_class->get_property = avatar_chooser_get_property;
+       object_class->set_property = avatar_chooser_set_property;
+
+       /**
+        * EmpathyAvatarChooser::changed:
+        * @chooser: an #EmpathyAvatarChooser
+        *
+        * Emitted when the chosen avatar has changed.
+        *
+        */
        signals[CHANGED] =
                g_signal_new ("changed",
                              G_TYPE_FROM_CLASS (klass),
@@ -181,27 +194,21 @@ empathy_avatar_chooser_class_init (EmpathyAvatarChooserClass *klass)
                              g_cclosure_marshal_VOID__VOID,
                              G_TYPE_NONE, 0);
 
-       param_spec = g_param_spec_object ("contact-factory",
-                                         "EmpathyContactFactory instance",
-                                         "EmpathyContactFactory instance "
-                                         "(may not be NULL)",
-                                         EMPATHY_TYPE_CONTACT_FACTORY,
-                                         G_PARAM_CONSTRUCT_ONLY |
-                                         G_PARAM_READWRITE |
-                                         G_PARAM_STATIC_STRINGS);
-       g_object_class_install_property (object_class,
-                                        PROP_CONTACT_FACTORY,
-                                        param_spec);
-
-       param_spec = g_param_spec_object ("account",
-                                         "McAccount",
-                                         "McAccount whose avatar should be "
+       /**
+        * EmpathyAvatarChooser:connection:
+        *
+        * The #TpConnection whose avatar should be shown and modified by
+        * the #EmpathyAvatarChooser instance.
+        */
+       param_spec = g_param_spec_object ("connection",
+                                         "TpConnection",
+                                         "TpConnection whose avatar should be "
                                          "shown and modified by this widget",
-                                         MC_TYPE_ACCOUNT,
+                                         TP_TYPE_CONNECTION,
                                          G_PARAM_READWRITE |
                                          G_PARAM_STATIC_STRINGS);
        g_object_class_install_property (object_class,
-                                        PROP_ACCOUNT,
+                                        PROP_CONNECTION,
                                         param_spec);
 
        g_type_class_add_private (object_class, sizeof (EmpathyAvatarChooserPriv));
@@ -220,6 +227,8 @@ empathy_avatar_chooser_init (EmpathyAvatarChooser *chooser)
                           G_N_ELEMENTS (drop_types),
                           GDK_ACTION_COPY);
 
+       priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
+
        g_signal_connect (chooser, "drag-motion",
                          G_CALLBACK (avatar_chooser_drag_motion_cb),
                          chooser);
@@ -246,78 +255,75 @@ avatar_chooser_finalize (GObject *object)
 
        priv = GET_PRIV (object);
 
-       g_object_unref (priv->contact_factory);
-       priv->contact_factory = NULL;
-
-       avatar_chooser_set_account (EMPATHY_AVATAR_CHOOSER (object), NULL);
-       g_assert (priv->account == NULL && priv->tp_contact_factory == NULL);
+       avatar_chooser_set_connection (EMPATHY_AVATAR_CHOOSER (object), NULL);
+       g_assert (priv->connection == NULL);
 
        if (priv->avatar != NULL) {
                empathy_avatar_unref (priv->avatar);
        }
 
+       g_object_unref (priv->gsettings_ui);
+
        G_OBJECT_CLASS (empathy_avatar_chooser_parent_class)->finalize (object);
 }
 
 static void
-avatar_chooser_tp_cf_ready_cb (EmpathyTpContactFactory *tp_cf,
-                              GParamSpec              *unused,
-                              EmpathyAvatarChooser    *self)
+avatar_chooser_set_connection (EmpathyAvatarChooser *self,
+                              TpConnection         *connection)
 {
        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);
+       if (priv->connection != NULL) {
+               g_object_unref (priv->connection);
+               priv->connection = NULL;
+       }
 
-       ready = empathy_tp_contact_factory_is_ready (tp_cf);
-       gtk_widget_set_sensitive (GTK_WIDGET (self), ready);
+       if (connection != NULL) {
+               GQuark features[] = { TP_CONNECTION_FEATURE_AVATAR_REQUIREMENTS, 0 };
+               priv->connection = g_object_ref (connection);
+               tp_proxy_prepare_async (priv->connection, features, NULL, NULL);
+       }
 }
 
 static void
-avatar_chooser_set_account (EmpathyAvatarChooser *self,
-                           McAccount            *account)
+avatar_chooser_error_show (EmpathyAvatarChooser *chooser,
+                          const gchar          *primary_text,
+                          const gchar          *secondary_text)
 {
-       EmpathyAvatarChooserPriv *priv = GET_PRIV (self);
-
-       if (priv->account != NULL) {
-               g_object_unref (priv->account);
-               priv->account = NULL;
+       GtkWidget *parent;
+       GtkWidget *dialog;
 
-               g_assert (priv->tp_contact_factory != NULL);
+       parent = gtk_widget_get_toplevel (GTK_WIDGET (chooser));
+       if (!GTK_IS_WINDOW (parent)) {
+               parent = NULL;
+       }
 
-               g_signal_handler_disconnect (priv->tp_contact_factory,
-                       priv->ready_handler_id);
-               priv->ready_handler_id = 0;
+       dialog = gtk_message_dialog_new (parent ? GTK_WINDOW (parent) : NULL,
+                                        GTK_DIALOG_MODAL,
+                                        GTK_MESSAGE_WARNING,
+                                        GTK_BUTTONS_CLOSE,
+                                        "%s", primary_text);
 
-               g_object_unref (priv->tp_contact_factory);
-               priv->tp_contact_factory = NULL;
+       if (secondary_text != NULL) {
+               gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
+                                                         "%s", secondary_text);
        }
 
-       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));
+       g_signal_connect (dialog, "response",
+                         G_CALLBACK (gtk_widget_destroy), NULL);
+       gtk_widget_show (dialog);
 
-               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 gboolean
-str_in_strv (gchar  *str,
+str_in_strv (const gchar  *str,
             gchar **strv)
 {
        if (strv == NULL) {
                return FALSE;
        }
 
-       while (*strv != NULL)
-       {
+       while (*strv != NULL) {
                if (g_str_equal (str, *strv)) {
                        return TRUE;
                }
@@ -330,154 +336,244 @@ str_in_strv (gchar  *str,
  * satisfactory_mime_type.
  */
 static gboolean
-can_satisfy_mime_type_requirements (gchar **accepted_mime_types,
-                                   gchar **satisfactory_format_name,
-                                   gchar **satisfactory_mime_type)
+avatar_chooser_need_mime_type_conversion (const gchar *current_mime_type,
+                                         gchar      **accepted_mime_types,
+                                         gchar      **satisfactory_format_name,
+                                         gchar      **satisfactory_mime_type)
 {
-       GSList *formats;
-       GSList *i;
-       gchar **j;
-       gboolean done = FALSE;
+       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;
        }
 
-       g_assert (satisfactory_format_name != NULL);
-       g_assert (satisfactory_mime_type != NULL);
+       /* 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 (i = formats; !done && i != NULL; i = i->next) {
-               GdkPixbufFormat *format = i->data;
+       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 (j = accepted_mime_types; *j != NULL; j++) {
-                       if (str_in_strv (*j, format_mime_types)) {
+               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 (*j);
-                               done = TRUE;
+                               *satisfactory_mime_type = g_strdup (*iter);
+                               found = TRUE;
                                break;
                        }
                }
                g_strfreev (format_mime_types);
        }
-
        g_slist_free (formats);
 
-       return done;
+       return TRUE;
 }
 
 static EmpathyAvatar *
-avatar_chooser_convert (EmpathyAvatarChooser *chooser,
-                       GdkPixbuf            *pixbuf_scaled,
-                       gchar               **mime_types,
-                       gsize                 max_size)
+avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
+                                       GdkPixbuf            *pixbuf,
+                                       EmpathyAvatar        *avatar)
 {
-       gchar         *format_name = NULL, *new_mime_type = NULL;
-       gchar         *converted_image_data = NULL;
-       gsize          converted_image_size = 0;
-       EmpathyAvatar *converted_avatar = NULL;
-       gboolean       saved;
-       GError        *error = NULL;
-
-       if (!can_satisfy_mime_type_requirements (mime_types, &format_name,
-                                                &new_mime_type)) {
-               DEBUG ("Mon dieu! Can't convert to any acceptable format!");
+       EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
+       TpAvatarRequirements     *req;
+       gboolean                  needs_conversion = FALSE;
+       guint                     width, height;
+       gchar                    *new_format_name = NULL;
+       gchar                    *new_mime_type = NULL;
+       gdouble                   min_factor, max_factor;
+       gdouble                   factor;
+       gchar                    *best_image_data = NULL;
+       gsize                     best_image_size = 0;
+       guint                     count = 0;
+
+       req = tp_connection_get_avatar_requirements (priv->connection);
+       if (req == NULL) {
+               DEBUG ("Avatar requirements not ready");
                return NULL;
        }
 
-       saved = gdk_pixbuf_save_to_buffer (pixbuf_scaled, &converted_image_data,
-               &converted_image_size, format_name, &error, NULL);
-       g_free (format_name);
-
-       if (!saved) {
-               DEBUG ("Couldn't convert image: %s", error->message);
-               g_error_free (error);
+       /* 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,
+                                                     req->supported_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_free (new_mime_type);
+       /* 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 are "
+                                 "supported on your system"));
                return NULL;
        }
 
-       /* Takes ownership of new_mime_type */
-       converted_avatar = empathy_avatar_new (converted_image_data,
-               converted_image_size, new_mime_type, NULL);
-
-       if (max_size > 0 && converted_avatar->len > max_size) {
-               /* FIXME: We could try converting to a different format; in
-                *        particular, try converting to jpeg with increasingly
-                *        high compression (if jpeg is supported). Not sure how
-                *        much we care.
-                */
-               DEBUG ("Converted the image, but the new filesize is too big");
+       /* If width or height are too big, it needs converting. */
+       width = gdk_pixbuf_get_width (pixbuf);
+       height = gdk_pixbuf_get_height (pixbuf);
+       if ((req->maximum_width > 0 && width > req->maximum_width) ||
+           (req->maximum_height > 0 && height > req->maximum_height)) {
+               gdouble h_factor, v_factor;
 
-               empathy_avatar_unref (converted_avatar);
-               converted_avatar = NULL;
-       }
-
-       return converted_avatar;
-}
+               h_factor = (gdouble) req->maximum_width / width;
+               v_factor = (gdouble) req->maximum_height / height;
+               factor = max_factor = MIN (h_factor, v_factor);
 
-static EmpathyAvatar *
-avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
-                                       GdkPixbuf            *pixbuf,
-                                       EmpathyAvatar        *avatar)
-{
-       EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
-       EmpathyTpContactFactory  *tp_cf = priv->tp_contact_factory;
-       gint                      max_width = 0, max_height = 0, max_size = 0;
-       gchar                   **mime_types = NULL;
-       gboolean                  needs_conversion = FALSE;
-       GdkPixbuf                *pixbuf_scaled = NULL;
+               DEBUG ("Image dimensions (%dx%d) are too big. Max is %dx%d.",
+                      width, height, req->maximum_width, req->maximum_height);
 
-       /* 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);
-
-       /* If the avatar's not already the right type, it needs converting. */
-       if (!str_in_strv (avatar->format, mime_types)) {
                needs_conversion = TRUE;
        }
 
-       /* If scaling down the pixbuf to fit the dimensions yields a new
-        * pixbuf, then it needed scaling. if it's the same pixbuf, it did not.
-        */
-       pixbuf_scaled = empathy_pixbuf_scale_down_if_necessary (pixbuf,
-               MIN(max_width, max_height));
-       if (pixbuf_scaled != pixbuf) {
-               needs_conversion = TRUE;
-       }
+       /* If the data len is too big and no other conversion is needed,
+        * try with a lower factor. */
+       if (req->maximum_bytes > 0 && avatar->len > req->maximum_bytes && !needs_conversion) {
+               DEBUG ("Image data (%"G_GSIZE_FORMAT" bytes) is too big "
+                      "(max is %u bytes), conversion needed.",
+                      avatar->len, req->maximum_bytes);
 
-       if (max_size > 0 && avatar->len > max_size) {
+               factor = 0.5;
                needs_conversion = TRUE;
        }
 
-       if (needs_conversion) {
-               avatar = avatar_chooser_convert (chooser, pixbuf_scaled,
-                       mime_types, max_size);
-       } else {
-               /* Just return another reference to the avatar passed in. */
-               avatar = empathy_avatar_ref (avatar);
+       /* 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);
        }
 
-       g_object_unref (pixbuf_scaled);
-       g_strfreev (mime_types);
+       do {
+               GdkPixbuf *pixbuf_scaled = NULL;
+               gboolean   saved;
+               gint       new_width, new_height;
+               gchar     *converted_image_data;
+               gsize      converted_image_size;
+               GError    *error = NULL;
+
+               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 {
+                       new_width = width;
+                       new_height = height;
+                       pixbuf_scaled = g_object_ref (pixbuf);
+               }
+
+               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);
+               g_object_unref (pixbuf_scaled);
+
+               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 the new image satisfy the req, keep it as current best */
+               if (req->maximum_bytes == 0 ||
+                   converted_image_size <= req->maximum_bytes) {
+                       if (best_image_data)
+                               g_free (best_image_data);
+
+                       best_image_data = converted_image_data;
+                       best_image_size = converted_image_size;
+
+                       /* If this image is close enough to the optimal size,
+                        * stop searching */
+                       if (req->maximum_bytes == 0 ||
+                           req->maximum_bytes - converted_image_size <= 1024)
+                               break;
+               } else {
+                       g_free (converted_image_data);
+               }
+
+               /* Make a binary search for the bigest factor that produce
+                * an image data size less than max_size */
+               if (converted_image_size > req->maximum_bytes)
+                       max_factor = factor;
+               if (converted_image_size < req->maximum_bytes)
+                       min_factor = factor;
+               factor = (min_factor + max_factor)/2;
+
+               if ((int) (width * factor) == new_width ||
+                   (int) (height * factor) == new_height) {
+                       /* min_factor and max_factor are too close, so the new
+                        * factor will produce the same image as previous
+                        * iteration. No need to continue, we already found
+                        * the optimal size. */
+                       break;
+               }
+
+               /* Do 10 iterations in the worst case */
+       } while (++count < 10);
+
+       g_free (new_format_name);
+
+       /* Takes ownership of new_mime_type and best_image_data */
+       avatar = empathy_avatar_new ((guchar *) best_image_data,
+               best_image_size, new_mime_type, NULL);
+
        return avatar;
 }
 
@@ -487,56 +583,104 @@ avatar_chooser_clear_image (EmpathyAvatarChooser *chooser)
        EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
        GtkWidget *image;
 
-       if (priv->avatar == NULL) {
-               return;
+       if (priv->avatar != NULL) {
+               empathy_avatar_unref (priv->avatar);
+               priv->avatar = NULL;
        }
 
-       empathy_avatar_unref (priv->avatar);
-       priv->avatar = NULL;
-
-       image = gtk_image_new_from_icon_name ("stock_person", GTK_ICON_SIZE_DIALOG);
+       image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_AVATAR_DEFAULT,
+               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 (EmpathyAvatarChooser *chooser,
-                         gchar                *data,
-                         gsize                 size,
-                         gboolean              set_locally)
+avatar_chooser_set_image_from_data (EmpathyAvatarChooser *chooser,
+                                   gchar                *data,
+                                   gsize                 size,
+                                   gboolean              set_locally)
 {
-       EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
-
-       GdkPixbuf     *pixbuf, *pixbuf_view;
-       GtkWidget     *image;
+       GdkPixbuf     *pixbuf;
        EmpathyAvatar *avatar = NULL;
        gchar         *mime_type = NULL;
 
        if (data == NULL) {
                avatar_chooser_clear_image (chooser);
-               g_free (data);
                return;
        }
 
-       pixbuf = empathy_pixbuf_from_data (data, size, &mime_type);
+       pixbuf = empathy_pixbuf_from_data_and_mime (data, size, &mime_type);
        if (pixbuf == NULL) {
                g_free (data);
+               data = NULL;
                return;
        }
 
        /* avatar takes ownership of data and mime_type */
-       avatar = empathy_avatar_new (data, size, mime_type, NULL);
+       avatar = empathy_avatar_new ((guchar *) data, size, mime_type, NULL);
+
+       avatar_chooser_set_image (chooser, avatar, pixbuf, set_locally);
+}
+
+static void
+avatar_chooser_set_image_from_avatar (EmpathyAvatarChooser *chooser,
+                                     EmpathyAvatar        *avatar,
+                                     gboolean              set_locally)
+{
+       GdkPixbuf *pixbuf;
+       gchar     *mime_type = NULL;
+
+       g_assert (avatar != NULL);
+
+       pixbuf = empathy_pixbuf_from_data_and_mime ((gchar *) 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 = avatar_chooser_maybe_convert_and_scale (
-                       chooser, pixbuf, avatar);
+               EmpathyAvatar *conv;
+
+               conv = avatar_chooser_maybe_convert_and_scale (chooser,
+                       pixbuf, avatar);
                empathy_avatar_unref (avatar);
-               avatar = conv;
-       }
 
-       if (avatar == NULL) {
-               /* An error occured; don't change the avatar. */
-               return;
+               if (conv == NULL) {
+                       /* An error occured; don't change the avatar. */
+                       return;
+               }
+
+               avatar = conv;
        }
 
        if (priv->avatar != NULL) {
@@ -570,7 +714,7 @@ avatar_chooser_set_image_from_file (EmpathyAvatarChooser *chooser,
                return;
        }
 
-       avatar_chooser_set_image (chooser, image_data, image_size, TRUE);
+       avatar_chooser_set_image_from_data (chooser, image_data, image_size, TRUE);
 }
 
 static gboolean
@@ -578,7 +722,7 @@ avatar_chooser_drag_motion_cb (GtkWidget          *widget,
                              GdkDragContext     *context,
                              gint                x,
                              gint                y,
-                             guint               time,
+                             guint               time_,
                              EmpathyAvatarChooser *chooser)
 {
        EmpathyAvatarChooserPriv *priv;
@@ -586,14 +730,15 @@ avatar_chooser_drag_motion_cb (GtkWidget          *widget,
 
        priv = GET_PRIV (chooser);
 
-       for (p = context->targets; p != NULL; p = p->next) {
+       for (p = gdk_drag_context_list_targets (context); p != NULL;
+            p = p->next) {
                gchar *possible_type;
 
                possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
 
                if (!strcmp (possible_type, URI_LIST_TYPE)) {
                        g_free (possible_type);
-                       gdk_drag_status (context, GDK_ACTION_COPY, time);
+                       gdk_drag_status (context, GDK_ACTION_COPY, time_);
 
                        return TRUE;
                }
@@ -607,7 +752,7 @@ avatar_chooser_drag_motion_cb (GtkWidget          *widget,
 static void
 avatar_chooser_drag_leave_cb (GtkWidget          *widget,
                             GdkDragContext     *context,
-                            guint               time,
+                            guint               time_,
                             EmpathyAvatarChooser *chooser)
 {
 }
@@ -617,7 +762,7 @@ avatar_chooser_drag_drop_cb (GtkWidget          *widget,
                            GdkDragContext     *context,
                            gint                x,
                            gint                y,
-                           guint               time,
+                           guint               time_,
                            EmpathyAvatarChooser *chooser)
 {
        EmpathyAvatarChooserPriv *priv;
@@ -625,11 +770,12 @@ avatar_chooser_drag_drop_cb (GtkWidget          *widget,
 
        priv = GET_PRIV (chooser);
 
-       if (context->targets == NULL) {
+       if (gdk_drag_context_list_targets (context) == NULL) {
                return FALSE;
        }
 
-       for (p = context->targets; p != NULL; p = p->next) {
+       for (p = gdk_drag_context_list_targets (context);
+            p != NULL; p = p->next) {
                char *possible_type;
 
                possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
@@ -637,7 +783,7 @@ avatar_chooser_drag_drop_cb (GtkWidget          *widget,
                        g_free (possible_type);
                        gtk_drag_get_data (widget, context,
                                           GDK_POINTER_TO_ATOM (p->data),
-                                          time);
+                                          time_);
 
                        return TRUE;
                }
@@ -655,69 +801,50 @@ avatar_chooser_drag_data_received_cb (GtkWidget          *widget,
                                     gint                y,
                                     GtkSelectionData   *selection_data,
                                     guint               info,
-                                    guint               time,
+                                    guint               time_,
                                     EmpathyAvatarChooser *chooser)
 {
        gchar    *target_type;
        gboolean  handled = FALSE;
 
-       target_type = gdk_atom_name (selection_data->target);
+       target_type = gdk_atom_name (gtk_selection_data_get_target (selection_data));
        if (!strcmp (target_type, URI_LIST_TYPE)) {
                GFile            *file;
-               GFileInputStream *input_stream;
                gchar            *nl;
                gchar            *data = NULL;
+               gsize             bytes_read;
 
-               nl = strstr (selection_data->data, "\r\n");
+               nl = strstr ((gchar *) gtk_selection_data_get_data (selection_data),
+                                               "\r\n");
                if (nl) {
                        gchar *uri;
 
-                       uri = g_strndup (selection_data->data,
-                                        nl - (gchar*) selection_data->data);
+                       uri = g_strndup ((gchar *) gtk_selection_data_get_data (selection_data),
+                                        nl - (gchar *) gtk_selection_data_get_data (selection_data));
 
                        file = g_file_new_for_uri (uri);
                        g_free (uri);
                } else {
-                       file = g_file_new_for_uri (selection_data->data);
+                       file = g_file_new_for_uri ((gchar *) gtk_selection_data_get_data (
+                                               selection_data));
                }
 
-               input_stream = g_file_read (file, NULL, NULL);
-
-               if (input_stream != NULL) {
-                       GFileInfo *info;
-                       
-                       info = g_file_query_info (file,
-                                                 G_FILE_ATTRIBUTE_STANDARD_SIZE,
-                                                 0, NULL, NULL);
-                       if (info != NULL) {
-                               goffset size;
-                               gssize bytes_read;
-                               
-                               size = g_file_info_get_size (info);
-                               data = g_malloc (size);
-
-                               bytes_read = g_input_stream_read (G_INPUT_STREAM (input_stream),
-                                                                 data, size,
-                                                                 NULL, NULL);
-                               if (bytes_read != -1) {
-                                       avatar_chooser_set_image (chooser,
-                                                                 data,
-                                                                 (gsize) bytes_read,
-                                                                 TRUE);
-                                       handled = TRUE;
-                               }
-
-                               g_free (data);
-                               g_object_unref (info);
-                       }
+               handled = g_file_load_contents (file, NULL, &data, &bytes_read,
+                                               NULL, NULL);
 
-                       g_object_unref (input_stream);
+               if (handled) {
+                       /* this in turn calls empathy_avatar_new (), which assumes
+                        * ownership of data.
+                        */
+                       avatar_chooser_set_image_from_data (chooser, data,
+                                                           bytes_read,
+                                                           TRUE);
                }
-               
+
                g_object_unref (file);
        }
 
-       gtk_drag_finish (context, handled, FALSE, time);
+       gtk_drag_finish (context, handled, FALSE, time_);
 }
 
 static void
@@ -747,6 +874,8 @@ avatar_chooser_update_preview_cb (GtkFileChooser       *file_chooser,
                                                  "gtk-dialog-question",
                                                  GTK_ICON_SIZE_DIALOG);
                }
+
+               g_free (filename);
        }
 
        gtk_file_chooser_set_preview_widget_active (file_chooser, TRUE);
@@ -759,15 +888,7 @@ avatar_chooser_response_cb (GtkWidget            *widget,
 {
        EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
 
-       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;
-       }
+       priv->chooser_dialog = NULL;
 
        if (response == GTK_RESPONSE_OK) {
                gchar *filename;
@@ -779,9 +900,10 @@ avatar_chooser_response_cb (GtkWidget            *widget,
 
                path = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (widget));
                if (path) {
-                       empathy_conf_set_string (empathy_conf_get (),
-                                                EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
-                                                path);
+                       g_settings_set_string (priv->gsettings_ui,
+                                              EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
+                                              path);
+
                        g_free (path);
                }
        }
@@ -790,7 +912,6 @@ avatar_chooser_response_cb (GtkWidget            *widget,
                avatar_chooser_clear_image (chooser);
        }
 
-out:
        gtk_widget_destroy (widget);
 }
 
@@ -804,8 +925,14 @@ avatar_chooser_clicked_cb (GtkWidget            *button,
        const gchar    *default_dir = DEFAULT_DIR;
        const gchar    *pics_dir;
        GtkFileFilter  *filter;
+       EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
 
-       chooser_dialog = GTK_FILE_CHOOSER (
+       if (priv->chooser_dialog) {
+               gtk_window_present (GTK_WINDOW (priv->chooser_dialog));
+               return;
+       }
+
+       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,
@@ -816,11 +943,13 @@ 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 (),
-                                EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
-                                &saved_dir);
+       saved_dir = g_settings_get_string (priv->gsettings_ui,
+                                          EMPATHY_PREFS_UI_AVATAR_DIRECTORY);
+
        if (saved_dir && !g_file_test (saved_dir, G_FILE_TEST_IS_DIR)) {
                g_free (saved_dir);
                saved_dir = NULL;
@@ -874,7 +1003,7 @@ avatar_chooser_clicked_cb (GtkWidget            *button,
        gtk_file_chooser_add_filter (chooser_dialog, filter);
        filter = gtk_file_filter_new ();
        gtk_file_filter_set_name (filter, _("All Files"));
-       gtk_file_filter_add_pattern(filter, "*");
+       gtk_file_filter_add_pattern (filter, "*");
        gtk_file_chooser_add_filter (chooser_dialog, filter);
 
        /* Setup response */
@@ -884,19 +1013,29 @@ avatar_chooser_clicked_cb (GtkWidget            *button,
                          chooser);
 
        gtk_widget_show (GTK_WIDGET (chooser_dialog));
+
        g_free (saved_dir);
 }
 
+/**
+ * empathy_avatar_chooser_new:
+ *
+ * Creates a new #EmpathyAvatarChooser.
+ *
+ * Return value: a new #EmpathyAvatarChooser
+ */
 GtkWidget *
-empathy_avatar_chooser_new (EmpathyContactFactory *contact_factory)
+empathy_avatar_chooser_new (void)
 {
-       return g_object_new (EMPATHY_TYPE_AVATAR_CHOOSER,
-                            "contact-factory", contact_factory,
-                            NULL);
+       return g_object_new (EMPATHY_TYPE_AVATAR_CHOOSER, NULL);
 }
 
-/* FIXME: when the avatar passed to this function actually can be relied upon to
- * contain a mime type, we can probably just ref it and store it.
+/**
+ * empathy_avatar_chooser_set:
+ * @chooser: an #EmpathyAvatarChooser
+ * @avatar: a new #EmpathyAvatar
+ *
+ * Sets the @chooser to display the avatar indicated by @avatar.
  */
 void
 empathy_avatar_chooser_set (EmpathyAvatarChooser *chooser,
@@ -905,13 +1044,21 @@ empathy_avatar_chooser_set (EmpathyAvatarChooser *chooser,
        g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (chooser));
 
        if (avatar != NULL) {
-               gchar *data = g_memdup (avatar->data, avatar->len);
-               avatar_chooser_set_image (chooser, data, avatar->len, FALSE);
+               avatar_chooser_set_image_from_avatar (chooser, avatar, FALSE);
        } else {
                avatar_chooser_clear_image (chooser);
        }
 }
 
+/**
+ * empathy_avatar_chooser_get_image_data:
+ * @chooser: an #EmpathyAvatarChooser
+ * @data: avatar bytes
+ * @data_size: size of @data
+ * @mime_type: avatar mime-type
+ *
+ * Gets image data about the currently selected avatar.
+ */
 void
 empathy_avatar_chooser_get_image_data (EmpathyAvatarChooser  *chooser,
                                       const gchar          **data,
@@ -926,7 +1073,7 @@ empathy_avatar_chooser_get_image_data (EmpathyAvatarChooser  *chooser,
 
        if (priv->avatar != NULL) {
                if (data != NULL) {
-                       *data = priv->avatar->data;
+                       *data = (gchar *) priv->avatar->data;
                }
                if (data_size != NULL) {
                        *data_size = priv->avatar->len;
@@ -947,3 +1094,12 @@ empathy_avatar_chooser_get_image_data (EmpathyAvatarChooser  *chooser,
        }
 }
 
+void
+empathy_avatar_chooser_set_account (EmpathyAvatarChooser *self,
+                                      TpAccount *account)
+{
+       g_return_if_fail (account != NULL);
+
+       avatar_chooser_set_connection (self, tp_account_get_connection (account));
+       g_object_notify (G_OBJECT (self), "connection");
+}