[epiphany] Port save as to WebKit2



commit cf9c1a366b1424ee55e621302c5cba02803832a8
Author: Carlos Garcia Campos <cgarcia igalia com>
Date:   Thu Aug 16 10:46:42 2012 +0200

    Port save as to WebKit2
    
    Use webkit_web_view_save() API for HTML pages that saves the web page
    into a MHTML file. For any other MIME types supported by the web view,
    save the main resource data to a file.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=679368

 embed/ephy-web-view.c   |   73 +++++++++++++++++++++++++++++++++++++++++++---
 lib/ephy-file-chooser.c |    2 +
 src/window-commands.c   |    6 +++-
 3 files changed, 75 insertions(+), 6 deletions(-)
---
diff --git a/embed/ephy-web-view.c b/embed/ephy-web-view.c
index 5caa27a..ae99cf1 100644
--- a/embed/ephy-web-view.c
+++ b/embed/ephy-web-view.c
@@ -3749,7 +3749,56 @@ ephy_web_view_get_title_composite (EphyWebView *view)
 }
 
 #ifdef HAVE_WEBKIT2
-/* TODO: webkit_web_view_save() */
+static void
+web_resource_get_data_cb (WebKitWebResource *resource,
+                          GAsyncResult *result,
+                          GOutputStream *output_stream)
+{
+  guchar *data;
+  gsize data_length;
+  GInputStream *input_stream;
+  GError *error = NULL;
+
+  data = webkit_web_resource_get_data_finish (resource, result, &data_length, &error);
+  if (!data) {
+    g_printerr ("Failed to save page: %s", error->message);
+    g_error_free (error);
+    g_object_unref (output_stream);
+
+    return;
+  }
+
+  input_stream = g_memory_input_stream_new_from_data (data, data_length, g_free);
+  g_output_stream_splice_async (output_stream, input_stream,
+                                G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
+                                G_PRIORITY_DEFAULT,
+                                NULL, NULL, NULL);
+  g_object_unref (input_stream);
+  g_object_unref (output_stream);
+}
+
+static void
+ephy_web_view_save_main_resource_cb (GFile *file,
+                                     GAsyncResult *result,
+                                     WebKitWebView *view)
+{
+  GFileOutputStream *output_stream;
+  WebKitWebResource *resource;
+  GError *error = NULL;
+
+  output_stream = g_file_replace_finish (file, result, &error);
+  if (!output_stream) {
+    g_printerr ("Failed to save page: %s", error->message);
+    g_error_free (error);
+
+    return;
+  }
+
+  resource = webkit_web_view_get_main_resource (view);
+  webkit_web_resource_get_data (resource, NULL,
+                                (GAsyncReadyCallback)web_resource_get_data_cb,
+                                output_stream);
+}
 #else
 static void
 ephy_web_view_save_sub_resource_start (GList *subresources, char *destination_uri);
@@ -3972,6 +4021,7 @@ ephy_web_view_save_sub_resources (EphyWebView *view, const char *uri, GList *sub
   ephy_web_view_save_sub_resource_start (subresources, destination_uri);
 }
 #endif
+
 /**
  * ephy_web_view_save:
  * @view: an #EphyWebView
@@ -3982,15 +4032,28 @@ ephy_web_view_save_sub_resources (EphyWebView *view, const char *uri, GList *sub
 void
 ephy_web_view_save (EphyWebView *view, const char *uri)
 {
-#ifdef HAVE_WEBKIT2
-  /* TODO: webkit_web_view_save() */
-#else
+  GFile *file;
+#ifndef HAVE_WEBKIT2
   WebKitWebFrame *frame;
   WebKitWebDataSource *data_source;
   GList *subresources;
   const GString *data;
-  GFile *file;
+#endif
+
+  file = g_file_new_for_uri (uri);
 
+#ifdef HAVE_WEBKIT2
+  if (g_str_has_suffix (uri, ".mhtml"))
+    webkit_web_view_save_to_file (WEBKIT_WEB_VIEW (view), file, WEBKIT_SAVE_MODE_MHTML,
+                                  NULL, NULL, NULL);
+  else
+    g_file_replace_async (file, NULL, FALSE,
+                          G_FILE_CREATE_REPLACE_DESTINATION | G_FILE_CREATE_PRIVATE,
+                          G_PRIORITY_DEFAULT, NULL,
+                          (GAsyncReadyCallback)ephy_web_view_save_main_resource_cb,
+                          view);
+  g_object_unref (file);
+#else
   /* Save main resource */
   frame = webkit_web_view_get_main_frame (WEBKIT_WEB_VIEW(view));
   data_source = webkit_web_frame_get_data_source (frame);
diff --git a/lib/ephy-file-chooser.c b/lib/ephy-file-chooser.c
index a570246..2e22810 100644
--- a/lib/ephy-file-chooser.c
+++ b/lib/ephy-file-chooser.c
@@ -211,6 +211,7 @@ ephy_file_chooser_new (const char *title,
 				 "text/html",
 				 "application/xhtml+xml",
 				 "text/xml",
+                                 "message/rfc822", /* MHTML */
 				 "image/png",
 				 "image/jpeg",
 				 "image/gif",
@@ -222,6 +223,7 @@ ephy_file_chooser_new (const char *title,
 				 "text/html",
 				 "application/xhtml+xml",
 				 "text/xml",
+                                 "message/rfc822", /* MHTML */
 				 NULL);
 
 		filter[EPHY_FILE_FILTER_IMAGES] =
diff --git a/src/window-commands.c b/src/window-commands.c
index c1e1d78..0a6c464 100644
--- a/src/window-commands.c
+++ b/src/window-commands.c
@@ -302,8 +302,12 @@ get_suggested_filename (EphyWebView *view)
 
 	if ((g_ascii_strncasecmp (mimetype, "text/html", 9)) == 0)
 	{
-		/* Web Title will be used as suggested filename*/
+		/* Web Title will be used as suggested filename */
+#ifdef HAVE_WEBKIT2
+		suggested_filename = g_strconcat (ephy_web_view_get_title (view), ".mhtml", NULL);
+#else
 		suggested_filename = g_strconcat (ephy_web_view_get_title (view), ".html", NULL);
+#endif
 	}
 	else
 	{



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