mirror of
https://github.com/ankitects/anki.git
synced 2025-09-20 06:52:21 -04:00
use the original filename if available when pasting
This commit is contained in:
parent
edd931b0ee
commit
6a204b2b9f
1 changed files with 15 additions and 10 deletions
|
@ -999,6 +999,7 @@ class FactEdit(QTextEdit):
|
||||||
def __init__(self, parent, *args):
|
def __init__(self, parent, *args):
|
||||||
QTextEdit.__init__(self, *args)
|
QTextEdit.__init__(self, *args)
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
|
self._tmpDir = None
|
||||||
if sys.platform.startswith("win32"):
|
if sys.platform.startswith("win32"):
|
||||||
self._ownLayout = None
|
self._ownLayout = None
|
||||||
|
|
||||||
|
@ -1032,10 +1033,10 @@ class FactEdit(QTextEdit):
|
||||||
ext = txt.split(".")[-1].lower()
|
ext = txt.split(".")[-1].lower()
|
||||||
try:
|
try:
|
||||||
if ext in pics:
|
if ext in pics:
|
||||||
name = self._retrieveURL(txt, ext)
|
name = self._retrieveURL(txt)
|
||||||
self.parent._addPicture(name, widget=self)
|
self.parent._addPicture(name, widget=self)
|
||||||
elif ext in audio:
|
elif ext in audio:
|
||||||
name = self._retrieveURL(txt, ext)
|
name = self._retrieveURL(txt)
|
||||||
self.parent._addSound(name, widget=self)
|
self.parent._addSound(name, widget=self)
|
||||||
else:
|
else:
|
||||||
# not image or sound, treat as plain text
|
# not image or sound, treat as plain text
|
||||||
|
@ -1064,10 +1065,10 @@ class FactEdit(QTextEdit):
|
||||||
ext = url.split(".")[-1].lower()
|
ext = url.split(".")[-1].lower()
|
||||||
try:
|
try:
|
||||||
if ext in pics:
|
if ext in pics:
|
||||||
name = self._retrieveURL(url, ext)
|
name = self._retrieveURL(url)
|
||||||
self.parent._addPicture(name, widget=self)
|
self.parent._addPicture(name, widget=self)
|
||||||
elif ext in audio:
|
elif ext in audio:
|
||||||
name = self._retrieveURL(url, ext)
|
name = self._retrieveURL(url)
|
||||||
self.parent._addSound(name, widget=self)
|
self.parent._addSound(name, widget=self)
|
||||||
except urllib2.URLError, e:
|
except urllib2.URLError, e:
|
||||||
ui.utils.showWarning(errtxt % e)
|
ui.utils.showWarning(errtxt % e)
|
||||||
|
@ -1076,17 +1077,21 @@ class FactEdit(QTextEdit):
|
||||||
self.insertHtml(self.simplifyHTML(unicode(source.html())))
|
self.insertHtml(self.simplifyHTML(unicode(source.html())))
|
||||||
return
|
return
|
||||||
|
|
||||||
def _retrieveURL(self, url, ext):
|
def _retrieveURL(self, url):
|
||||||
req = urllib2.Request(url, None, {
|
req = urllib2.Request(url, None, {
|
||||||
'User-Agent': 'Mozilla/5.0 (compatible; Anki/%s)' %
|
'User-Agent': 'Mozilla/5.0 (compatible; Anki/%s)' %
|
||||||
ankiqt.appVersion })
|
ankiqt.appVersion })
|
||||||
filecontents = urllib2.urlopen(req).read()
|
filecontents = urllib2.urlopen(req).read()
|
||||||
(fd, name) = tempfile.mkstemp(prefix="paste", suffix=".%s" %
|
path = os.path.join(self.tmpDir(), os.path.basename(url))
|
||||||
ext.encode("ascii"))
|
file = open(path, "wb")
|
||||||
file = os.fdopen(fd, "wb")
|
|
||||||
file.write(filecontents)
|
file.write(filecontents)
|
||||||
file.flush()
|
file.close()
|
||||||
return unicode(name, sys.getfilesystemencoding())
|
return path
|
||||||
|
|
||||||
|
def tmpDir(self):
|
||||||
|
if not self._tmpDir:
|
||||||
|
self._tmpDir = tempfile.mkdtemp(prefix="anki")
|
||||||
|
return self._tmpDir
|
||||||
|
|
||||||
def simplifyHTML(self, html):
|
def simplifyHTML(self, html):
|
||||||
"Remove all style information and P tags."
|
"Remove all style information and P tags."
|
||||||
|
|
Loading…
Reference in a new issue