Merge pull request #610 from evandroforks/ask_confirmation_before_moving_anki_collection

Ask user confirmation before moving the Anki directory
This commit is contained in:
Damien Elmes 2020-05-18 19:44:27 +10:00 committed by GitHub
commit a53aac40f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 1 deletions

View File

@ -33,7 +33,7 @@ appUpdate = "https://ankiweb.net/update/desktop"
appHelpSite = HELP_SITE
from aqt.main import AnkiQt # isort:skip
from aqt.profiles import ProfileManager # isort:skip
from aqt.profiles import ProfileManager, AnkiRestart # isort:skip
profiler = None
mw: Optional[AnkiQt] = None # set on init
@ -399,6 +399,10 @@ def _run(argv=None, exec=True):
try:
pm = ProfileManager(opts.base)
pmLoadResult = pm.setupMeta()
except AnkiRestart as error:
if error.exitcode:
sys.exit(error.exitcode)
return
except:
# will handle below
traceback.print_exc()

View File

@ -67,6 +67,12 @@ class LoadMetaResult:
loadError: bool
class AnkiRestart(SystemExit):
def __init__(self, *args, **kwargs):
self.exitcode = kwargs.pop("exitcode", 0)
super().__init__(*args, **kwargs)
class ProfileManager:
def __init__(self, base=None):
self.name = None
@ -114,10 +120,73 @@ class ProfileManager:
return os.path.expanduser("~/Documents/Anki")
def maybeMigrateFolder(self):
newBase = self.base
oldBase = self._oldFolderLocation()
if oldBase and not os.path.exists(self.base) and os.path.isdir(oldBase):
try:
# if anything goes wrong with UI, reset to the old behavior of always migrating
self._tryToMigrateFolder(oldBase)
except AnkiRestart:
raise
except:
self.base = newBase
shutil.move(oldBase, self.base)
def _tryToMigrateFolder(self, oldBase):
from PyQt5 import QtWidgets, QtGui
app = QtWidgets.QApplication([])
icon = QtGui.QIcon()
icon.addPixmap(
QtGui.QPixmap(":/icons/anki.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off,
)
window_title = "Anki Base Directory Migration"
migration_directories = f"\n\n {oldBase}\n\nto\n\n {self.base}"
conformation = QMessageBox()
conformation.setIcon(QMessageBox.Warning)
conformation.setWindowIcon(icon)
conformation.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
conformation.setWindowTitle(window_title)
conformation.setText("Confirm Anki Collection base directory migration?")
conformation.setInformativeText(
f"The Anki Collection directory should be migrated from {migration_directories}\n\n"
f"If you would like to keep using the old location, consult the Startup Options "
f"on the Anki documentation on website\n\n{appHelpSite}"
)
conformation.setDefaultButton(QMessageBox.Cancel)
retval = conformation.exec()
if retval == QMessageBox.Ok:
progress = QMessageBox()
progress.setIcon(QMessageBox.Information)
progress.setStandardButtons(QMessageBox.NoButton)
progress.setWindowIcon(icon)
progress.setWindowTitle(window_title)
progress.setText(
f"Please wait while your Anki collection is moved from {migration_directories}"
)
progress.show()
app.processEvents()
shutil.move(oldBase, self.base)
progress.hide()
completion = QMessageBox()
completion.setIcon(QMessageBox.Information)
completion.setStandardButtons(QMessageBox.Ok)
completion.setWindowIcon(icon)
completion.setWindowTitle(window_title)
completion.setText(
f"Your Anki Collection was successfully moved from {migration_directories}\n\n"
f"Now Anki needs to restart.\n\n"
f"Click OK to exit Anki and open it again."
)
completion.show()
completion.exec()
raise AnkiRestart(exitcode=0)
else:
self.base = oldBase
# Profile load/save
######################################################################