From e1d41c2e5810938a2946756c6076b398017def69 Mon Sep 17 00:00:00 2001 From: Abdo Date: Sat, 26 Oct 2024 15:43:50 +0300 Subject: [PATCH] Prompt to do a legacy import dynamically https://github.com/ankitects/anki/issues/3493 --- Specialfields21/dialog.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/Specialfields21/dialog.py b/Specialfields21/dialog.py index ce3e358..91e73b3 100644 --- a/Specialfields21/dialog.py +++ b/Specialfields21/dialog.py @@ -2,11 +2,13 @@ import webbrowser import aqt +import aqt.importing from anki.consts import * +from anki.hooks import wrap +from anki.utils import pointVersion from aqt import mw from aqt.qt import * -from aqt.utils import getOnlyText, showInfo, showWarning, askUser -from anki.utils import pointVersion +from aqt.utils import askUser, getOnlyText, showInfo, showWarning from .config import getUserOption @@ -350,12 +352,6 @@ def returnTagsText(): def onFields(self): - if pointVersion() >= 55 and not mw.pm.legacy_import_export(): - yes = askUser( - 'Special Fields doesn\'t work without enabling the "Legacy import/export handling" preference. Do you want to enable it? This may make it so you cannot import newer files. If you experience errors, turn this setting off temporarily' - ) - if yes: - mw.pm.set_legacy_import_export(True) # Use existing FieldDialog as template for UI. fields = configs["current config"]["Special field"] FieldDialog(mw, fields, parent=self) @@ -364,9 +360,27 @@ def onFields(self): def onFieldsExecute(): onFields(mw) +def wants_legacy_import(): + return askUser('Import using the Special Fields add-on?') + +def prompt_for_file_then_import_override(mw, _old): + if wants_legacy_import(): + aqt.importing.onImport(mw) + else: + _old(mw) + +def import_file_override(mw, path, _old): + if wants_legacy_import(): + aqt.importing.importFile(mw, path) + else: + _old(mw, path) mw.addonManager.setConfigAction(__name__, onFieldsExecute) action = QAction("Special Fields", mw) action.setShortcut(QKeySequence("Ctrl+shift+s")) action.triggered.connect(onFields) mw.form.menuTools.addAction(action) +if pointVersion() >= 55: + aqt.main.prompt_for_file_then_import = wrap(aqt.main.prompt_for_file_then_import, prompt_for_file_then_import_override, "around") + # for drag & drop + aqt.main.import_file = wrap(aqt.main.import_file, import_file_override, "around")