Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prompt to do a legacy import dynamically #39

Merged
merged 1 commit into from
Oct 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions Specialfields21/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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")