]> git.eshelyaron.com Git - emacs.git/commitdiff
Add utility functions and new xwidget commands
authorSungbin Jo <pcr910303@icloud.com>
Wed, 12 Aug 2020 10:39:50 +0000 (12:39 +0200)
committerLars Ingebrigtsen <larsi@gnus.org>
Wed, 12 Aug 2020 10:39:50 +0000 (12:39 +0200)
Co-authored-by: Jaesup Kwak <veshboo@gmail.com>
* lisp/xwidget.el (xwidget-webkit-callback): Add case for
'response-callback' event.
(xwidget-webkit-download-dir): New variable.
(xwidget-webkit-save-as-file): New function.
* src/nsxwidget.m (XwWebView::decidePolicyForNavigationResponse):
Store download event.
* src/xwidget.c src/xwidget.h (store_xwidget_download_callback_event):
New function.

etc/NEWS
lisp/xwidget.el
src/nsxwidget.m
src/xwidget.c
src/xwidget.h

index cfe180ff68833ccc35b52b1565a4d56c8c0d8d1e..b25e43bb9b01fc2a9737079d9088d3ef24a441ae 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -986,6 +986,9 @@ If Emacs was built with xwidget support, you can access the embedded
 webkit browser with 'M-x xwidget-webkit-browse-url'.  Viewing two
 instances of xwidget webkit is not supported.
 
+*** Downloading files from xwidget-webkit is now supported.
+The new variable 'xwidget-webkit-download-dir' says where to download to.
+
 *** New functions for xwidget-webkit mode
 'xwidget-webkit-clone-and-split-below',
 'xwidget-webkit-clone-and-split-right'.
index e38bd1b32fb5feddf18cabcf47ca894c3105f51c..074320855c5f726998aede12a633381142612141 100644 (file)
@@ -288,6 +288,12 @@ XWIDGET instance, XWIDGET-EVENT-TYPE depends on the originating xwidget."
                    (xwidget-webkit-show-id-or-named-element
                     xwidget
                     (match-string 1 strarg)))))
+            ;; TODO: Response handling other than download.
+            ((eq xwidget-event-type 'download-callback)
+             (let ((url  (nth 3 last-input-event))
+                   (mime-type (nth 4 last-input-event))
+                   (file-name (nth 5 last-input-event)))
+               (xwidget-webkit-save-as-file url mime-type file-name)))
             ((eq xwidget-event-type 'javascript-callback)
              (let ((proc (nth 3 last-input-event))
                    (arg  (nth 4 last-input-event)))
@@ -308,6 +314,32 @@ If non-nil, plugins are enabled.  Otherwise, disabled."))
   ;; Keep track of [vh]scroll when switching buffers
   (image-mode-setup-winprops))
 
+;;; Download, save as file.
+
+(defcustom xwidget-webkit-download-dir "~/Downloads/"
+  "Directory where download file saved."
+  :version "27.1"
+  :type 'file)
+
+(defun xwidget-webkit-save-as-file (url mime-type file-name)
+  "For XWIDGET webkit, save URL of MIME-TYPE to location specified by user.
+FILE-NAME combined with `xwidget-webkit-download-dir' is the default file name
+of the prompt when reading.  When the file name the user specified is a
+directory, URL is saved at the specified directory as FILE-NAME."
+  (let ((save-name (read-file-name
+                    (format "Save URL `%s' of type `%s' in file/directory: "
+                            url mime-type)
+                    xwidget-webkit-download-dir
+                    (when file-name
+                      (expand-file-name
+                       file-name
+                       xwidget-webkit-download-dir)))))
+    (if (file-directory-p save-name)
+        (setq save-name
+              (expand-file-name (file-name-nondirectory file-name) save-name)))
+    (setq xwidget-webkit-download-dir (file-name-directory save-name))
+    (url-copy-file url save-name t)))
+
 ;;; Bookmarks integration
 
 (defcustom xwidget-webkit-bookmark-jump-new-session nil
index 8643ba24d80cfd9823aba9b9e59c720d6d01e998..2277cc97d552b3137763ba101cd0f8eddac1a0d7 100644 (file)
@@ -121,6 +121,18 @@ decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
 decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse
 decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
 {
+  if (!navigationResponse.canShowMIMEType)
+    {
+      NSString *url = navigationResponse.response.URL.absoluteString;
+      NSString *mimetype = navigationResponse.response.MIMEType;
+      NSString *filename = navigationResponse.response.suggestedFilename;
+      decisionHandler (WKNavigationResponsePolicyCancel);
+      store_xwidget_download_callback_event (self.xw,
+                                             url.UTF8String,
+                                             mimetype.UTF8String,
+                                             filename.UTF8String);
+      return;
+    }
   decisionHandler (WKNavigationResponsePolicyAllow);
 
   self.urlScriptBlocked[navigationResponse.response.URL] =
index d5c229c2b19224a4ed420dd44312496f5ef71c47..c61f5bef88d4cb52a0236304799959159460bce3 100644 (file)
@@ -258,6 +258,26 @@ store_xwidget_event_string (struct xwidget *xw, const char *eventname,
   kbd_buffer_store_event (&event);
 }
 
+void
+store_xwidget_download_callback_event (struct xwidget *xw,
+                                       const char *url,
+                                       const char *mimetype,
+                                       const char *filename)
+{
+  struct input_event event;
+  Lisp_Object xwl;
+  XSETXWIDGET (xwl, xw);
+  EVENT_INIT (event);
+  event.kind = XWIDGET_EVENT;
+  event.frame_or_window = Qnil;
+  event.arg = list5 (intern ("download-callback"),
+                     xwl,
+                     build_string (url),
+                     build_string (mimetype),
+                     build_string (filename));
+  kbd_buffer_store_event (&event);
+}
+
 void
 store_xwidget_js_callback_event (struct xwidget *xw,
                                  Lisp_Object proc,
index 29f1153206f1ab4e576ffea0338cadb0707b5c42..40ad8ae8334cd37dc625f723f94100c75c2ee7a5 100644 (file)
@@ -154,6 +154,11 @@ void store_xwidget_event_string (struct xwidget *xw,
                                  const char *eventname,
                                  const char *eventstr);
 
+void store_xwidget_download_callback_event (struct xwidget *xw,
+                                            const char *url,
+                                            const char *mimetype,
+                                            const char *filename);
+
 void store_xwidget_js_callback_event (struct xwidget *xw,
                                       Lisp_Object proc,
                                       Lisp_Object argument);