Support new import code in drag/drop + file double-click cases

https://forums.ankiweb.net/t/anki-2-1-52-release-candidate/19412/24
This commit is contained in:
Damien Elmes 2022-05-03 14:09:40 +10:00
parent 567b05b731
commit fdbe211539
2 changed files with 26 additions and 9 deletions

View file

@ -14,10 +14,7 @@ from aqt.qt import *
from aqt.utils import askUser, getFile, showInfo, showText, showWarning, tooltip, tr from aqt.utils import askUser, getFile, showInfo, showText, showWarning, tooltip, tr
def import_file(mw: aqt.main.AnkiQt) -> None: def import_file(mw: aqt.main.AnkiQt, path: str) -> None:
if not (path := get_file_path(mw)):
return
filename = os.path.basename(path).lower() filename = os.path.basename(path).lower()
if filename.endswith(".anki"): if filename.endswith(".anki"):
showInfo(tr.importing_anki_files_are_from_a_very()) showInfo(tr.importing_anki_files_are_from_a_very())
@ -31,6 +28,11 @@ def import_file(mw: aqt.main.AnkiQt) -> None:
raise NotImplementedError raise NotImplementedError
def prompt_for_file_then_import(mw: aqt.main.AnkiQt) -> None:
if path := get_file_path(mw):
import_file(mw, path)
def get_file_path(mw: aqt.main.AnkiQt) -> str | None: def get_file_path(mw: aqt.main.AnkiQt) -> str | None:
if file := getFile( if file := getFile(
mw, mw,

View file

@ -48,7 +48,11 @@ from aqt.dbcheck import check_db
from aqt.emptycards import show_empty_cards from aqt.emptycards import show_empty_cards
from aqt.flags import FlagManager from aqt.flags import FlagManager
from aqt.import_export.exporting import ExportDialog from aqt.import_export.exporting import ExportDialog
from aqt.import_export.importing import import_collection_package_op, import_file from aqt.import_export.importing import (
import_collection_package_op,
import_file,
prompt_for_file_then_import,
)
from aqt.legacy import install_pylib_legacy from aqt.legacy import install_pylib_legacy
from aqt.mediacheck import check_media_db from aqt.mediacheck import check_media_db
from aqt.mediasync import MediaSyncer from aqt.mediasync import MediaSyncer
@ -129,7 +133,11 @@ class MainWebView(AnkiWebView):
paths = [url.toLocalFile() for url in mime.urls()] paths = [url.toLocalFile() for url in mime.urls()]
deck_paths = filter(lambda p: not p.endswith(".colpkg"), paths) deck_paths = filter(lambda p: not p.endswith(".colpkg"), paths)
for path in deck_paths: for path in deck_paths:
aqt.importing.importFile(self.mw, path) if self.mw.pm.new_import_export():
import_file(self.mw, path)
else:
aqt.importing.importFile(self.mw, path)
# importing continues after the above call returns, so it is not # importing continues after the above call returns, so it is not
# currently safe for us to import more than one file at once # currently safe for us to import more than one file at once
return return
@ -1170,20 +1178,27 @@ title="{}" {}>{}</button>""".format(
########################################################################## ##########################################################################
def handleImport(self, path: str) -> None: def handleImport(self, path: str) -> None:
"Importing triggered via file double-click, or dragging file onto Anki icon."
import aqt.importing import aqt.importing
if not os.path.exists(path): if not os.path.exists(path):
# there were instances in the distant past where the received filename was not
# valid (encoding issues?), so this was added to direct users to try
# file>import instead.
showInfo(tr.qt_misc_please_use_fileimport_to_import_this()) showInfo(tr.qt_misc_please_use_fileimport_to_import_this())
return None return None
aqt.importing.importFile(self, path) if self.pm.new_import_export():
return None import_file(self, path)
else:
aqt.importing.importFile(self, path)
def onImport(self) -> None: def onImport(self) -> None:
"Importing triggered via File>Import."
import aqt.importing import aqt.importing
if self.pm.new_import_export(): if self.pm.new_import_export():
import_file(self) prompt_for_file_then_import(self)
else: else:
aqt.importing.onImport(self) aqt.importing.onImport(self)