]> 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 a004d2dd0c269a183a871717dc26e4562c07b2a9..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"
@@ -46,6 +48,7 @@ typedef struct {
        EmpathyContactFactory   *contact_factory;
        McAccount               *account;
        EmpathyTpContactFactory *tp_contact_factory;
+       GtkFileChooser          *chooser_dialog;
 
        gulong ready_handler_id;
 
@@ -211,7 +214,7 @@ empathy_avatar_chooser_init (EmpathyAvatarChooser *chooser)
                          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);
 }
@@ -285,8 +288,38 @@ avatar_chooser_set_account (EmpathyAvatarChooser *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 (gchar  *str,
+str_in_strv (const gchar  *str,
             gchar **strv)
 {
        if (strv == NULL) {
@@ -306,61 +339,71 @@ 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)
 {
-       gchar *name = NULL;
-       gchar *type = NULL;
+       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;
        }
 
-       /* 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/jpeg", accepted_mime_types)) {
-               name = g_strdup ("jpeg");
-               type = g_strdup ("image/jpeg");
-       } else if (str_in_strv ("image/png", accepted_mime_types)) {
-               name = g_strdup ("png");
-               type = g_strdup ("image/png");
-       } else {
-               GSList  *formats, *l;
-               gboolean found = 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;
+               }
+       }
 
-               formats = gdk_pixbuf_get_formats ();
-               for (l = formats; !found && l != NULL; l = l->next) {
-                       GdkPixbufFormat *format = l->data;
-                       gchar **format_mime_types;
-                       gchar **iter;
+       /* 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;
-                       }
+               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)) {
-                                       name = gdk_pixbuf_format_get_name (format);
-                                       type = g_strdup (*iter);
-                                       found = TRUE;
-                                       break;
-                               }
+               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);
+               g_strfreev (format_mime_types);
        }
+       g_slist_free (formats);
 
-       *satisfactory_format_name = name;
-       *satisfactory_mime_type = type;
-
-       return name != NULL;
+       return TRUE;
 }
 
 static EmpathyAvatar *
@@ -370,7 +413,7 @@ avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
 {
        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;
        gint                      width, height;
@@ -402,23 +445,26 @@ avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
        max_factor = 1;
        factor = 1;
 
-       if (!can_satisfy_mime_type_requirements (mime_types, &new_format_name,
-                                                &new_mime_type)) {
-               /* FIXME: That should be reported to the user */
-               DEBUG ("Mon dieu! Can't convert to any acceptable format!");
-               g_strfreev (mime_types);
-               return NULL;
-       }
-
-       /* If the avatar is not already in the right type, it needs converting. */
-       if (!str_in_strv (avatar->format, mime_types)) {
-               DEBUG ("Image format %s not supported by the protocol",
-                       avatar->format);
-
+       /* 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);
@@ -440,7 +486,7 @@ avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
         * 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 %"G_GSIZE_FORMAT" bytes), conversion needed.",
+                      "(max is %u bytes), conversion needed.",
                       avatar->len, max_size);
 
                factor = 0.5;
@@ -485,10 +531,12 @@ avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
                                                   &error, NULL);
 
                if (!saved) {
-                       DEBUG ("Couldn't convert image: %s", error->message);
-                       g_clear_error (&error);
                        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;
                }
 
@@ -498,15 +546,23 @@ avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
                if (max_size == 0)
                        break;
 
-               /* Make a dichotomic search for the optimal factor that produce
-                * an image data size close to max_size */
+               /* 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 - min_factor)/2;
+               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) > 1000);
+                ABS (max_size - converted_image_size) > 1024);
        g_free (new_format_name);
 
        /* Takes ownership of new_mime_type and converted_image_data */
@@ -546,7 +602,6 @@ avatar_chooser_set_image_from_data (EmpathyAvatarChooser *chooser,
 
        if (data == NULL) {
                avatar_chooser_clear_image (chooser);
-               g_free (data);
                return;
        }
 
@@ -609,8 +664,10 @@ avatar_chooser_set_image (EmpathyAvatarChooser *chooser,
        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) {
@@ -841,6 +898,8 @@ avatar_chooser_response_cb (GtkWidget            *widget,
 {
        EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
 
+       priv->chooser_dialog = NULL;
+
        if (response == GTK_RESPONSE_CANCEL) {
                goto out;
        }
@@ -886,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,
@@ -898,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 (),