From 2fff30db2ff04d50adc8f7f0de074361c9ec9da1 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Fri, 20 Sep 2013 14:41:56 +0900 Subject: [PATCH] prioritize urls over text; fixes linux fm drop (#945) --- aqt/editor.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/aqt/editor.py b/aqt/editor.py index fc6115cc2..f804e6060 100644 --- a/aqt/editor.py +++ b/aqt/editor.py @@ -813,15 +813,18 @@ to a cloze type first, via Edit>Change Note Type.""")) s = s.lower() return (s.startswith("http://") or s.startswith("https://") - or s.startswith("ftp://")) + or s.startswith("ftp://") + or s.startswith("file://")) def _retrieveURL(self, url): "Download file into media folder and return local filename or None." - # urllib is picky with local file links + # urllib doesn't understand percent-escaped utf8, but requires things like + # '#' to be escaped. we don't try to unquote the incoming URL, because + # we should only be receiving file:// urls from url mime, which is unquoted if url.lower().startswith("file://"): url = url.replace("%", "%25") url = url.replace("#", "%23") - # fetch it into a temporary folder + # fetch it into a temporary folder self.mw.progress.start( immediate=True, parent=self.parentWindow) try: @@ -1088,16 +1091,20 @@ class EditorWebView(AnkiWebView): # print "text", mime.text() if mime.hasHtml(): return self._processHtml(mime) - elif mime.hasText(): - return self._processText(mime) elif mime.hasUrls(): return self._processUrls(mime) + elif mime.hasText(): + return self._processText(mime) elif mime.hasImage(): return self._processImage(mime) else: # nothing return QMimeData() + # when user is dragging a file from a file manager on any platform, the + # url type should be set, and it is not URL-encoded. on a mac no text type + # is returned, and on windows the text type is not returned in cases like + # "foo's bar.jpg" def _processUrls(self, mime): url = mime.urls()[0].toString() # chrome likes to give us the URL twice with a \n @@ -1108,6 +1115,10 @@ class EditorWebView(AnkiWebView): mime.setHtml(link) return mime + # if the user has used 'copy link location' in the browser, the clipboard + # will contain the URL as text, and no URLs or HTML. the URL will already + # be URL-encoded, and shouldn't be a file:// url unless they're browsing + # locally, which we don't support def _processText(self, mime): txt = unicode(mime.text()) html = None