[empathy] empathy_avatar_new: stop stealing strings (#650939)



commit 7625bd2ae11f2c3064e3e208ea82fffa97dc7f18
Author: Guillaume Desmottes <guillaume desmottes collabora co uk>
Date:   Thu May 26 11:10:06 2011 +0200

    empathy_avatar_new: stop stealing strings (#650939)
    
    It was pretty confusing when reading/reviewing code.

 libempathy-gtk/empathy-avatar-chooser.c |   24 ++++++++++++----------
 libempathy/empathy-contact.c            |   33 ++++++++++++++++--------------
 libempathy/empathy-contact.h            |    6 ++--
 3 files changed, 34 insertions(+), 29 deletions(-)
---
diff --git a/libempathy-gtk/empathy-avatar-chooser.c b/libempathy-gtk/empathy-avatar-chooser.c
index ff27859..2d1c599 100644
--- a/libempathy-gtk/empathy-avatar-chooser.c
+++ b/libempathy-gtk/empathy-avatar-chooser.c
@@ -607,10 +607,12 @@ avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *self,
 
   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);
 
+  g_free (best_image_data);
+  g_free (new_mime_type);
+
   return avatar;
 }
 
@@ -655,6 +657,7 @@ avatar_chooser_set_image (EmpathyAvatarChooser *self,
   g_object_unref (pixbuf);
 }
 
+/* takes ownership of @data */
 static void
 avatar_chooser_set_image_from_data (EmpathyAvatarChooser *self,
     gchar *data,
@@ -679,10 +682,12 @@ avatar_chooser_set_image_from_data (EmpathyAvatarChooser *self,
       return;
     }
 
-  /* avatar takes ownership of data and mime_type */
   avatar = empathy_avatar_new ((guchar *) data, size, mime_type, NULL);
 
   avatar_chooser_set_image (self, avatar, pixbuf, set_locally);
+
+  g_free (mime_type);
+  g_free (data);
 }
 
 static void
@@ -730,12 +735,8 @@ avatar_chooser_drag_data_received_cb (GtkWidget          *widget,
 
       if (handled)
         {
-          /* this in turn calls empathy_avatar_new (), which assumes
-           * ownership of data.
-           */
-          avatar_chooser_set_image_from_data (self, data,
-                      bytes_read,
-                      TRUE);
+          /* pass data to the avatar_chooser_set_image_from_data */
+          avatar_chooser_set_image_from_data (self, data, bytes_read, TRUE);
         }
 
       g_object_unref (file);
@@ -801,6 +802,7 @@ avatar_chooser_set_image_from_file (EmpathyAvatarChooser *self,
       return;
     }
 
+  /* pass image_data to the avatar_chooser_set_image_from_data */
   avatar_chooser_set_image_from_data (self, image_data, image_size, TRUE);
 }
 
@@ -809,8 +811,6 @@ static void
 avatar_chooser_set_avatar_from_pixbuf (EmpathyAvatarChooser *self,
                GdkPixbuf *pb)
 {
-  /* dup the string as empathy_avatar_new steals ownership of the it */
-  gchar *mime = g_strdup ("image/png");
   gsize size;
   gchar *buf;
   EmpathyAvatar *avatar = NULL;
@@ -825,8 +825,10 @@ avatar_chooser_set_avatar_from_pixbuf (EmpathyAvatarChooser *self,
       return;
     }
 
-  avatar = empathy_avatar_new ((guchar *) buf, size, mime, NULL);
+  avatar = empathy_avatar_new ((guchar *) buf, size, "image/png", NULL);
   avatar_chooser_set_image (self, avatar, pb, TRUE);
+
+  g_free (buf);
 }
 
 static gboolean
diff --git a/libempathy/empathy-contact.c b/libempathy/empathy-contact.c
index f3e200e..7b23df0 100644
--- a/libempathy/empathy-contact.c
+++ b/libempathy/empathy-contact.c
@@ -1282,17 +1282,16 @@ contact_load_avatar_cache (EmpathyContact *contact,
         }
     }
 
-  if (data)
+  if (data != NULL)
     {
       DEBUG ("Avatar loaded from %s", filename);
       avatar = empathy_avatar_new ((guchar *) data, len, NULL, filename);
       contact_set_avatar (contact, avatar);
       empathy_avatar_unref (avatar);
     }
-  else
-    {
-      g_free (filename);
-    }
+
+  g_free (data);
+  g_free (filename);
 
   return data != NULL;
 }
@@ -1319,24 +1318,23 @@ empathy_avatar_get_type (void)
  * @format: the mime type of the avatar image
  * @filename: the filename where the avatar is stored in cache
  *
- * Create a #EmpathyAvatar from the provided data. This function takes the
- * ownership of @data, @format and @filename.
+ * Create a #EmpathyAvatar from the provided data.
  *
  * Returns: a new #EmpathyAvatar
  */
 EmpathyAvatar *
-empathy_avatar_new (guchar *data,
+empathy_avatar_new (const guchar *data,
                     gsize len,
-                    gchar *format,
-                    gchar *filename)
+                    const gchar *format,
+                    const gchar *filename)
 {
   EmpathyAvatar *avatar;
 
   avatar = g_slice_new0 (EmpathyAvatar);
-  avatar->data = data;
+  avatar->data = g_memdup (data, len);
   avatar->len = len;
-  avatar->format = format;
-  avatar->filename = filename;
+  avatar->format = g_strdup (format);
+  avatar->filename = g_strdup (filename);
   avatar->refcount = 1;
 
   return avatar;
@@ -1783,12 +1781,17 @@ contact_set_avatar_from_tp_contact (EmpathyContact *contact)
       EmpathyAvatar *avatar;
       gchar *data;
       gsize len;
+      gchar *path;
 
       g_file_load_contents (file, NULL, &data, &len, NULL, NULL);
-      avatar = empathy_avatar_new ((guchar *) data, len, g_strdup (mime),
-          g_file_get_path (file));
+      path = g_file_get_path (file);
+
+      avatar = empathy_avatar_new ((guchar *) data, len, mime, path);
+
       contact_set_avatar (contact, avatar);
       empathy_avatar_unref (avatar);
+      g_free (path);
+      g_free (data);
     }
   else
     {
diff --git a/libempathy/empathy-contact.h b/libempathy/empathy-contact.h
index 16b50e5..b32971a 100644
--- a/libempathy/empathy-contact.h
+++ b/libempathy/empathy-contact.h
@@ -117,10 +117,10 @@ gboolean empathy_contact_can_do_action (EmpathyContact *self,
 
 #define EMPATHY_TYPE_AVATAR (empathy_avatar_get_type ())
 GType empathy_avatar_get_type (void) G_GNUC_CONST;
-EmpathyAvatar * empathy_avatar_new (guchar *data,
+EmpathyAvatar * empathy_avatar_new (const guchar *data,
     gsize len,
-    gchar *format,
-    gchar *filename);
+    const gchar *format,
+    const gchar *filename);
 EmpathyAvatar * empathy_avatar_ref (EmpathyAvatar *avatar);
 void empathy_avatar_unref (EmpathyAvatar *avatar);
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]