[epiphany] e-file-helpers: Add ephy_file_create_data_uri_for_filename()



commit 2b5cfe828cef29dd20225540fdd2bf5fcbf6b8fc
Author: Carlos Garcia Campos <cgarcia igalia com>
Date:   Tue May 29 14:30:49 2012 +0200

    e-file-helpers: Add ephy_file_create_data_uri_for_filename()
    
    It creates a data URI for the given filename. Use the new function when
    building error and applications pages.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=677025

 doc/reference/epiphany-sections.txt |    1 +
 embed/ephy-request-about.c          |   16 +++++------
 embed/ephy-web-view.c               |   33 +----------------------
 lib/ephy-file-helpers.c             |   51 +++++++++++++++++++++++++++++++++++
 lib/ephy-file-helpers.h             |    2 +
 5 files changed, 62 insertions(+), 41 deletions(-)
---
diff --git a/doc/reference/epiphany-sections.txt b/doc/reference/epiphany-sections.txt
index f0d044f..00881e0 100644
--- a/doc/reference/epiphany-sections.txt
+++ b/doc/reference/epiphany-sections.txt
@@ -193,6 +193,7 @@ ephy_file_launch_handler
 ephy_file_switch_temp_file
 ephy_file_tmp_dir
 ephy_file_tmp_filename
+ephy_file_create_data_uri_for_filename
 </SECTION>
 
 <SECTION>
diff --git a/embed/ephy-request-about.c b/embed/ephy-request-about.c
index e239c11..bfff22f 100644
--- a/embed/ephy-request-about.c
+++ b/embed/ephy-request-about.c
@@ -182,19 +182,17 @@ ephy_request_about_send (SoupRequest          *request,
 
     applications = ephy_web_application_get_application_list ();
     for (p = applications; p; p = p->next) {
-      char *img_data = NULL, *img_data_base64 = NULL;
-      gsize data_length;
+      char *icon_uri;
       EphyWebApplication *app = (EphyWebApplication*)p->data;
-      
-      if (g_file_get_contents (app->icon_url, &img_data, &data_length, NULL))
-        img_data_base64 = g_base64_encode ((guchar*)img_data, data_length);
-      g_string_append_printf (data_str, "<tbody><tr><td class=\"icon\"><img width=64 height=64 src=\"data:image/png;base64,%s\">" \
+
+      icon_uri = ephy_file_create_data_uri_for_filename (app->icon_url, "image/png");
+      g_string_append_printf (data_str, "<tbody><tr><td class=\"icon\"><img width=64 height=64 src=\"%s\">" \
                               " </img></td><td class=\"data\"><div class=\"appname\">%s</div><div class=\"appurl\">%s</div></td><td class=\"input\"><input type=\"submit\" value=\"Delete\" id=\"%s\"></td><td class=\"date\">%s <br /> %s</td></tr>",
-                              img_data_base64, app->name, app->url, app->name,
+                              icon_uri ? icon_uri : "",
+                              app->name, app->url, app->name,
                               /* Note for translators: this refers to the installation date. */
                               _("Installed on:"), app->install_date);
-      g_free (img_data_base64);
-      g_free (img_data);
+      g_free (icon_uri);
     }
 
     g_string_append (data_str, "</form></table></body>");
diff --git a/embed/ephy-web-view.c b/embed/ephy-web-view.c
index f0b3d8f..e4ae30f 100644
--- a/embed/ephy-web-view.c
+++ b/embed/ephy-web-view.c
@@ -2052,37 +2052,6 @@ load_status_cb (WebKitWebView *web_view,
   g_object_thaw_notify (object);
 }
 
-static char *
-get_file_content_as_base64 (const char *path)
-{
-  GFile *file;
-  GFileInfo *file_info;
-  const char *image_type;
-  char *image_raw;
-  gsize len;
-  char *image_data;
-  char *image64;
-
-  file = g_file_new_for_path (path);
-  file_info = g_file_query_info (file,
-                                 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
-                                 G_FILE_QUERY_INFO_NONE,
-                                 NULL, NULL);
-  image_type = g_file_info_get_content_type (file_info);
-
-  g_file_get_contents (path, &image_raw, &len, NULL);
-  image_data = g_base64_encode ((guchar *) image_raw, len);
-  image64 = g_strdup_printf ("data:%s;base64,%s", image_type, image_data);
-
-  g_free (image_raw);
-  g_free (image_data);
-
-  g_object_unref (file);
-  g_object_unref (file_info);
-
-  return image64;
-}
-
 /**
  * ephy_web_view_load_error_page:
  * @view: an #EphyWebView
@@ -2175,7 +2144,7 @@ ephy_web_view_load_error_page (EphyWebView *view,
                                           48,
                                           GTK_ICON_LOOKUP_GENERIC_FALLBACK);
 
-  image_data = icon_info ? get_file_content_as_base64 (gtk_icon_info_get_filename (icon_info)) : NULL;
+  image_data = icon_info ? ephy_file_create_data_uri_for_filename (gtk_icon_info_get_filename (icon_info), NULL) : NULL;
 
   g_file_get_contents (html_file, &template, NULL, NULL);
 
diff --git a/lib/ephy-file-helpers.c b/lib/ephy-file-helpers.c
index 13ae06d..4975811 100644
--- a/lib/ephy-file-helpers.c
+++ b/lib/ephy-file-helpers.c
@@ -970,3 +970,54 @@ ephy_file_delete_uri (const char *uri)
 	}
 	g_object_unref (file);
 }
+
+/**
+ * ephy_file_create_data_uri_for_filename:
+ * @filename: the filename of a local path
+ * @mime_type: the MIME type of the filename, or %NULL
+ *
+ * Create a data uri using the contents of @filename.
+ * If @mime_type is %NULL, the %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
+ * attribute of @filename will be used.
+ *
+ * Returns: a new allocated string containg the data uri, or %NULL if the
+ *   data uri could not be created
+ */
+char *ephy_file_create_data_uri_for_filename (const char *filename,
+					      const char *mime_type)
+{
+	gchar *data;
+	gsize data_length;
+	gchar *base64;
+	gchar *uri = NULL;
+	GFileInfo *file_info = NULL;
+
+	g_return_val_if_fail (filename != NULL, NULL);
+
+	if (!g_file_get_contents (filename, &data, &data_length, NULL))
+		return NULL;
+
+	base64 = g_base64_encode ((const guchar *)data, data_length);
+	g_free (data);
+
+	if (!mime_type) {
+		GFile *file;
+
+		file = g_file_new_for_path (filename);
+		file_info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
+					       G_FILE_QUERY_INFO_NONE, NULL, NULL);
+		if (file_info)
+			mime_type = g_file_info_get_content_type (file_info);
+
+		g_object_unref (file);
+	}
+
+	if (mime_type)
+		uri = g_strdup_printf ("data:%s;charset=utf8;base64,%s", mime_type, base64);
+	g_free(base64);
+
+	if (file_info)
+		g_object_unref (file_info);
+
+	return uri;
+}
diff --git a/lib/ephy-file-helpers.h b/lib/ephy-file-helpers.h
index bdabf87..4b7b8e2 100644
--- a/lib/ephy-file-helpers.h
+++ b/lib/ephy-file-helpers.h
@@ -88,6 +88,8 @@ gboolean           ephy_file_browse_to           (GFile       *file,
 gboolean           ephy_file_delete_dir_recursively (GFile *file,
                                                      GError      **error);
 void               ephy_file_delete_uri          (const char  *uri);
+char       *       ephy_file_create_data_uri_for_filename (const char *filename,
+                                                           const char *mime_type);
 
 G_END_DECLS
 



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