mirror of
https://github.com/ankitects/anki.git
synced 2025-09-21 15:32:23 -04:00
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:
commit
a53aac40f8
2 changed files with 74 additions and 1 deletions
|
@ -33,7 +33,7 @@ appUpdate = "https://ankiweb.net/update/desktop"
|
||||||
appHelpSite = HELP_SITE
|
appHelpSite = HELP_SITE
|
||||||
|
|
||||||
from aqt.main import AnkiQt # isort:skip
|
from aqt.main import AnkiQt # isort:skip
|
||||||
from aqt.profiles import ProfileManager # isort:skip
|
from aqt.profiles import ProfileManager, AnkiRestart # isort:skip
|
||||||
|
|
||||||
profiler = None
|
profiler = None
|
||||||
mw: Optional[AnkiQt] = None # set on init
|
mw: Optional[AnkiQt] = None # set on init
|
||||||
|
@ -399,6 +399,10 @@ def _run(argv=None, exec=True):
|
||||||
try:
|
try:
|
||||||
pm = ProfileManager(opts.base)
|
pm = ProfileManager(opts.base)
|
||||||
pmLoadResult = pm.setupMeta()
|
pmLoadResult = pm.setupMeta()
|
||||||
|
except AnkiRestart as error:
|
||||||
|
if error.exitcode:
|
||||||
|
sys.exit(error.exitcode)
|
||||||
|
return
|
||||||
except:
|
except:
|
||||||
# will handle below
|
# will handle below
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
|
@ -67,6 +67,12 @@ class LoadMetaResult:
|
||||||
loadError: bool
|
loadError: bool
|
||||||
|
|
||||||
|
|
||||||
|
class AnkiRestart(SystemExit):
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
self.exitcode = kwargs.pop("exitcode", 0)
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class ProfileManager:
|
class ProfileManager:
|
||||||
def __init__(self, base=None):
|
def __init__(self, base=None):
|
||||||
self.name = None
|
self.name = None
|
||||||
|
@ -114,11 +120,74 @@ class ProfileManager:
|
||||||
return os.path.expanduser("~/Documents/Anki")
|
return os.path.expanduser("~/Documents/Anki")
|
||||||
|
|
||||||
def maybeMigrateFolder(self):
|
def maybeMigrateFolder(self):
|
||||||
|
newBase = self.base
|
||||||
oldBase = self._oldFolderLocation()
|
oldBase = self._oldFolderLocation()
|
||||||
|
|
||||||
if oldBase and not os.path.exists(self.base) and os.path.isdir(oldBase):
|
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)
|
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
|
# Profile load/save
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue