]> 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 83475b5659fcbb30a98e1bf401b2fa0ec2bf2490..98bb44614e444b1a5a0a756d63cfa19e2ecfc889 100644 (file)
 #include <gtk/gtk.h>
 #include <gio/gio.h>
 
+#include <libempathy/empathy-gsettings.h>
 #include <libempathy/empathy-utils.h>
-#include <libempathy/empathy-tp-contact-factory.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
 
 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAvatarChooser)
 typedef struct {
-       EmpathyTpContactFactory *factory;
        TpConnection            *connection;
        GtkFileChooser          *chooser_dialog;
 
        gulong ready_handler_id;
 
        EmpathyAvatar *avatar;
+       GSettings *gsettings_ui;
 } EmpathyAvatarChooserPriv;
 
 static void       avatar_chooser_finalize              (GObject              *object);
@@ -227,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);
@@ -255,12 +257,13 @@ avatar_chooser_finalize (GObject *object)
 
        avatar_chooser_set_connection (EMPATHY_AVATAR_CHOOSER (object), NULL);
        g_assert (priv->connection == NULL);
-       g_assert (priv->factory == 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);
 }
 
@@ -273,14 +276,12 @@ avatar_chooser_set_connection (EmpathyAvatarChooser *self,
        if (priv->connection != NULL) {
                g_object_unref (priv->connection);
                priv->connection = NULL;
-
-               g_object_unref (priv->factory);
-               priv->factory = NULL;
        }
 
        if (connection != NULL) {
+               GQuark features[] = { TP_CONNECTION_FEATURE_AVATAR_REQUIREMENTS, 0 };
                priv->connection = g_object_ref (connection);
-               priv->factory = empathy_tp_contact_factory_dup_singleton (connection);
+               tp_proxy_prepare_async (priv->connection, features, NULL, NULL);
        }
 }
 
@@ -408,23 +409,22 @@ avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
                                        EmpathyAvatar        *avatar)
 {
        EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
-       guint                     max_width = 0, max_height = 0, max_size = 0;
-       gchar                   **mime_types = NULL;
+       TpAvatarRequirements     *req;
        gboolean                  needs_conversion = FALSE;
-       gint                      width, height;
+       guint                     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;
+       gchar                    *best_image_data = NULL;
+       gsize                     best_image_size = 0;
+       guint                     count = 0;
 
-       g_object_get (priv->factory,
-               "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);
+       req = tp_connection_get_avatar_requirements (priv->connection);
+       if (req == NULL) {
+               DEBUG ("Avatar requirements not ready");
+               return NULL;
+       }
 
        /* Smaller is the factor, smaller will be the image.
         * 0 is an empty image, 1 is the full size. */
@@ -434,7 +434,7 @@ avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
 
        /* Check if we need to convert to another image format */
        if (avatar_chooser_need_mime_type_conversion (avatar->format,
-                                                     mime_types,
+                                                     req->supported_mime_types,
                                                      &new_format_name,
                                                      &new_mime_type)) {
                DEBUG ("Format conversion needed, we'll use mime type '%s' "
@@ -442,12 +442,11 @@ avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
                       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 "
+                               _("None of the accepted image formats are "
                                  "supported on your system"));
                return NULL;
        }
@@ -455,26 +454,26 @@ avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
        /* 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)) {
+       if ((req->maximum_width > 0 && width > req->maximum_width) ||
+           (req->maximum_height > 0 && height > req->maximum_height)) {
                gdouble h_factor, v_factor;
 
-               h_factor = (gdouble) max_width / width;
-               v_factor = (gdouble) max_height / height;
+               h_factor = (gdouble) req->maximum_width / width;
+               v_factor = (gdouble) req->maximum_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);
+                      width, height, req->maximum_width, req->maximum_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) {
+       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, max_size);
+                      avatar->len, req->maximum_bytes);
 
                factor = 0.5;
                needs_conversion = TRUE;
@@ -491,10 +490,10 @@ avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
                GdkPixbuf *pixbuf_scaled = NULL;
                gboolean   saved;
                gint       new_width, new_height;
+               gchar     *converted_image_data;
+               gsize      converted_image_size;
                GError    *error = NULL;
 
-               g_free (converted_image_data);
-
                if (factor != 1) {
                        new_width = width * factor;
                        new_height = height * factor;
@@ -531,31 +530,49 @@ avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
                DEBUG ("Produced an image data of %"G_GSIZE_FORMAT" bytes.",
                        converted_image_size);
 
-               if (max_size == 0)
-                       break;
+               /* 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 > max_size)
+               if (converted_image_size > req->maximum_bytes)
                        max_factor = factor;
-               if (converted_image_size < max_size)
+               if (converted_image_size < req->maximum_bytes)
                        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);
+               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 converted_image_data */
-       avatar = empathy_avatar_new (converted_image_data,
-               converted_image_size, new_mime_type, NULL, NULL);
+       /* 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;
 }
@@ -571,7 +588,8 @@ avatar_chooser_clear_image (EmpathyAvatarChooser *chooser)
                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);
 }
@@ -594,11 +612,12 @@ avatar_chooser_set_image_from_data (EmpathyAvatarChooser *chooser,
        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, NULL);
+       avatar = empathy_avatar_new ((guchar *) data, size, mime_type, NULL);
 
        avatar_chooser_set_image (chooser, avatar, pixbuf, set_locally);
 }
@@ -613,7 +632,7 @@ avatar_chooser_set_image_from_avatar (EmpathyAvatarChooser *chooser,
 
        g_assert (avatar != NULL);
 
-       pixbuf = empathy_pixbuf_from_data_and_mime (avatar->data,
+       pixbuf = empathy_pixbuf_from_data_and_mime ((gchar *) avatar->data,
                                                    avatar->len,
                                                    &mime_type);
        if (pixbuf == NULL) {
@@ -703,7 +722,7 @@ avatar_chooser_drag_motion_cb (GtkWidget          *widget,
                              GdkDragContext     *context,
                              gint                x,
                              gint                y,
-                             guint               time,
+                             guint               time_,
                              EmpathyAvatarChooser *chooser)
 {
        EmpathyAvatarChooserPriv *priv;
@@ -711,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;
                }
@@ -732,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)
 {
 }
@@ -742,7 +762,7 @@ avatar_chooser_drag_drop_cb (GtkWidget          *widget,
                            GdkDragContext     *context,
                            gint                x,
                            gint                y,
-                           guint               time,
+                           guint               time_,
                            EmpathyAvatarChooser *chooser)
 {
        EmpathyAvatarChooserPriv *priv;
@@ -750,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));
@@ -762,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;
                }
@@ -780,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_from_data (
-                                               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
@@ -898,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);
                }
        }
@@ -944,9 +947,9 @@ avatar_chooser_clicked_cb (GtkWidget            *button,
        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;
@@ -1010,6 +1013,7 @@ avatar_chooser_clicked_cb (GtkWidget            *button,
                          chooser);
 
        gtk_widget_show (GTK_WIDGET (chooser_dialog));
+
        g_free (saved_dir);
 }
 
@@ -1021,7 +1025,7 @@ avatar_chooser_clicked_cb (GtkWidget            *button,
  * Return value: a new #EmpathyAvatarChooser
  */
 GtkWidget *
-empathy_avatar_chooser_new ()
+empathy_avatar_chooser_new (void)
 {
        return g_object_new (EMPATHY_TYPE_AVATAR_CHOOSER, NULL);
 }
@@ -1069,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;
@@ -1090,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");
+}