mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Windows: Fix drag and drop from SMB shares to the edit metadata dialog formats list not working. Fixes #1869471 [Drag and drop from NAS](https://bugs.launchpad.net/calibre/+bug/1869471)
This commit is contained in:
parent
0ff41ac649
commit
c2d5b2a576
@ -12,12 +12,12 @@ from threading import Thread
|
||||
from PyQt5.Qt import QPixmap, Qt, QDialog, QLabel, QVBoxLayout, \
|
||||
QDialogButtonBox, QProgressBar, QTimer, QUrl, QImageReader
|
||||
|
||||
from calibre.constants import DEBUG, iswindows
|
||||
from calibre.constants import DEBUG
|
||||
from calibre.ptempfile import PersistentTemporaryFile
|
||||
from calibre import browser, as_unicode, prints
|
||||
from calibre.gui2 import error_dialog
|
||||
from calibre.utils.imghdr import what
|
||||
from polyglot.builtins import unicode_type, as_unicode as as_unicode_polyglot
|
||||
from polyglot.builtins import unicode_type
|
||||
from polyglot.urllib import unquote, urlparse
|
||||
from polyglot.queue import Queue, Empty
|
||||
|
||||
@ -158,13 +158,7 @@ def urls_from_md(md):
|
||||
|
||||
|
||||
def path_from_qurl(qurl):
|
||||
raw = bytes(qurl.toEncoded(
|
||||
QUrl.PreferLocalFile | QUrl.RemoveScheme | QUrl.RemovePassword | QUrl.RemoveUserInfo |
|
||||
QUrl.RemovePort | QUrl.RemoveAuthority | QUrl.RemoveQuery | QUrl.RemoveFragment))
|
||||
ans = as_unicode_polyglot(unquote(raw), errors='replace')
|
||||
if iswindows and ans.startswith('/'):
|
||||
ans = ans[1:]
|
||||
return ans
|
||||
return qurl.toLocalFile()
|
||||
|
||||
|
||||
def remote_urls_from_qurl(qurls, allowed_exts):
|
||||
@ -174,6 +168,10 @@ def remote_urls_from_qurl(qurls, allowed_exts):
|
||||
yield bytes(qurl.toEncoded()).decode('utf-8'), posixpath.basename(qurl.path())
|
||||
|
||||
|
||||
def extension(path):
|
||||
return path.rpartition('.')[-1].lower()
|
||||
|
||||
|
||||
def dnd_has_extension(md, extensions, allow_all_extensions=False):
|
||||
if DEBUG:
|
||||
prints('\nDebugging DND event')
|
||||
@ -186,7 +184,7 @@ def dnd_has_extension(md, extensions, allow_all_extensions=False):
|
||||
return True
|
||||
urls = urls_from_md(md)
|
||||
paths = [path_from_qurl(u) for u in urls]
|
||||
exts = frozenset([posixpath.splitext(u)[1][1:].lower() for u in paths if u])
|
||||
exts = frozenset(filter(None, (extension(u) for u in paths if u)))
|
||||
if DEBUG:
|
||||
repr_urls = [bytes(u.toEncoded()).decode('utf-8') for u in urls]
|
||||
prints('URLS:', repr(repr_urls))
|
||||
@ -222,9 +220,7 @@ def dnd_get_local_image_and_pixmap(md, image_exts=None):
|
||||
urls = urls_from_md(md)
|
||||
paths = [path_from_qurl(u) for u in urls]
|
||||
# Look for a local file
|
||||
images = [xi for xi in paths if
|
||||
posixpath.splitext(unquote(xi))[1][1:].lower() in
|
||||
image_exts]
|
||||
images = [xi for xi in paths if extension(xi) in image_exts]
|
||||
images = [xi for xi in images if os.path.exists(xi)]
|
||||
for path in images:
|
||||
try:
|
||||
@ -282,7 +278,7 @@ def dnd_get_files(md, exts, allow_all_extensions=False, filter_exts=()):
|
||||
local_files = [path_from_qurl(x) for x in urls]
|
||||
|
||||
def is_ok(path):
|
||||
ext = posixpath.splitext(path)[1][1:].lower()
|
||||
ext = extension(path)
|
||||
if allow_all_extensions and ext and ext not in filter_exts:
|
||||
return True
|
||||
return ext in exts and ext not in filter_exts
|
||||
|
Loading…
x
Reference in New Issue
Block a user