#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"
EmpathyContactFactory *contact_factory;
McAccount *account;
EmpathyTpContactFactory *tp_contact_factory;
+ GtkFileChooser *chooser_dialog;
gulong ready_handler_id;
G_CALLBACK (avatar_chooser_clicked_cb),
chooser);
- priv->contact_factory = empathy_contact_factory_new ();
+ priv->contact_factory = empathy_contact_factory_dup_singleton ();
empathy_avatar_chooser_set (chooser, NULL);
}
priv = GET_PRIV (object);
avatar_chooser_set_account (EMPATHY_AVATAR_CHOOSER (object), NULL);
- g_assert (priv->account == NULL && priv->tp_contact_factory == NULL);
+ g_assert (priv->account == NULL);
+ g_assert (priv->tp_contact_factory == NULL);
g_object_unref (priv->contact_factory);
}
}
+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 (gchar *str,
+str_in_strv (const gchar *str,
gchar **strv)
{
if (strv == NULL) {
* 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)
{
- gchar *name = NULL,
- *type = NULL;
- gboolean done = FALSE;
-
- if (accepted_mime_types == NULL || *accepted_mime_types == NULL) {
- return FALSE;
- }
+ gchar *good_mime_types[] = {"image/jpeg", "image/png", NULL};
+ guint i;
+ GSList *formats, *l;
+ gboolean found = FALSE;
- g_assert (satisfactory_format_name != NULL);
- g_assert (satisfactory_mime_type != NULL);
+ *satisfactory_format_name = NULL;
+ *satisfactory_mime_type = NULL;
- /* Special-case png and jpeg to avoid accidentally saving to something
- * uncompressed like bmp. This assumes that we can write image/png and
- * image/jpeg; if this isn't true then something's really wrong with
- * GdkPixbuf.
- */
- if (str_in_strv ("image/png", accepted_mime_types)) {
- name = g_strdup ("png");
- type = g_strdup ("image/png");
- done = TRUE;
- } else if (str_in_strv ("image/jpeg", accepted_mime_types)) {
- name = g_strdup ("jpeg");
- type = g_strdup ("image/jpeg");
- done = TRUE;
- } else {
- GSList *formats = gdk_pixbuf_get_formats ();
- GSList *l;
- gchar **strv;
-
- for (l = formats; !done && l != NULL; l = l->next) {
- GdkPixbufFormat *format = l->data;
- gchar **format_mime_types;
-
- if (!gdk_pixbuf_format_is_writable (format)) {
- continue;
- }
-
- format_mime_types = gdk_pixbuf_format_get_mime_types (format);
- for (strv = format_mime_types; *strv != NULL; strv++) {
- if (str_in_strv (*strv, accepted_mime_types)) {
- name = gdk_pixbuf_format_get_name (format);
- type = g_strdup (*strv);
- done = TRUE;
- break;
- }
- }
- g_strfreev (format_mime_types);
- }
-
- g_slist_free (formats);
- }
-
- if (done) {
- if (satisfactory_format_name != NULL) {
- *satisfactory_format_name = name;
- } else {
- g_free (name);
- }
- if (satisfactory_mime_type != NULL) {
- *satisfactory_mime_type = type;
- } else {
- g_free (type);
- }
+ /* If there is no accepted format there is nothing we can do */
+ if (accepted_mime_types == NULL || *accepted_mime_types == NULL) {
return TRUE;
- } else {
- /* check we're not leaking. */
- g_assert (name == NULL && type == NULL);
- return FALSE;
}
-}
-static EmpathyAvatar *
-avatar_chooser_convert (EmpathyAvatarChooser *chooser,
- GdkPixbuf *pixbuf_scaled,
- gchar **mime_types,
- gsize max_size)
-{
- 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!");
- return NULL;
+ /* 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;
}
- 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);
-
- g_free (new_mime_type);
- return 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;
+ }
}
- /* Takes ownership of new_mime_type */
- converted_avatar = empathy_avatar_new (converted_image_data,
- converted_image_size, new_mime_type, NULL);
+ /* 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 (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 (!gdk_pixbuf_format_is_writable (format)) {
+ continue;
+ }
- empathy_avatar_unref (converted_avatar);
- converted_avatar = NULL;
+ 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 converted_avatar;
+ return TRUE;
}
static EmpathyAvatar *
{
EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
EmpathyTpContactFactory *tp_cf = priv->tp_contact_factory;
- gint max_width = 0, max_height = 0, max_size = 0;
+ guint max_width = 0, max_height = 0, max_size = 0;
gchar **mime_types = NULL;
gboolean needs_conversion = FALSE;
- GdkPixbuf *pixbuf_scaled = NULL;
+ 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
"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)) {
+ /* 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);
- /* If _scale_down_if_necessary yields a new pixbuf, then it needed
- * scaling. If it's the same pixbuf, it did not, and did no extra work
- * beyond checking the dimensions. Hence, we call it even if
- * needs_conversion is already TRUE, rather than duplicating the
- * dimension checking code here, as it's no more expensive than
- * checking the dimensions first in the case where no scaling is
- * necessary.
- */
- pixbuf_scaled = empathy_pixbuf_scale_down_if_necessary (pixbuf,
- MIN(max_width, max_height));
- if (pixbuf_scaled != pixbuf) {
needs_conversion = TRUE;
}
- if (max_size > 0 && avatar->len > max_size) {
+ /* 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 (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;
+ 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 {
+ 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);
+
+ 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;
}
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);
return;
g_assert (avatar != NULL);
- pixbuf = empathy_pixbuf_from_data (avatar->data, avatar->len, &mime_type);
+ 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 had incorrect format! correcting");
- g_free (avatar->format);
- 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);
}
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);
if (conv == NULL) {
{
EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
+ priv->chooser_dialog = NULL;
+
if (response == GTK_RESPONSE_CANCEL) {
goto out;
}
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,
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 (),