mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 06:22:22 -04:00
Set to restart Anki application after profile directory migration
Ask user confirmation before moving the Anki directory https://github.com/ankitects/anki/pull 610
This commit is contained in:
parent
9f5c69ee94
commit
5f9dbf9b7d
2 changed files with 70 additions and 53 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
|
||||||
|
@ -405,6 +405,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,66 +120,73 @@ 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):
|
||||||
window_title = "Anki Base Directory Migration"
|
try:
|
||||||
migration_directories = f"\n\n {oldBase}\n\nto\n\n {self.base}"
|
# 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 messageBox():
|
def _tryToMigrateFolder(self, oldBase):
|
||||||
icon = QtGui.QIcon()
|
from PyQt5 import QtWidgets, QtGui
|
||||||
icon.addPixmap(
|
|
||||||
QtGui.QPixmap(":/icons/anki.png"),
|
|
||||||
QtGui.QIcon.Normal,
|
|
||||||
QtGui.QIcon.Off,
|
|
||||||
)
|
|
||||||
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:
|
app = QtWidgets.QApplication([])
|
||||||
progress = QMessageBox()
|
icon = QtGui.QIcon()
|
||||||
progress.setIcon(QMessageBox.Information)
|
icon.addPixmap(
|
||||||
progress.setStandardButtons(QMessageBox.NoButton)
|
QtGui.QPixmap(":/icons/anki.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off,
|
||||||
progress.setWindowIcon(icon)
|
)
|
||||||
progress.setWindowTitle(window_title)
|
window_title = "Anki Base Directory Migration"
|
||||||
progress.setText(
|
migration_directories = f"\n\n {oldBase}\n\nto\n\n {self.base}"
|
||||||
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()
|
conformation = QMessageBox()
|
||||||
completion.setIcon(QMessageBox.Information)
|
conformation.setIcon(QMessageBox.Warning)
|
||||||
completion.setStandardButtons(QMessageBox.Ok)
|
conformation.setWindowIcon(icon)
|
||||||
completion.setWindowIcon(icon)
|
conformation.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
|
||||||
completion.setWindowTitle(window_title)
|
conformation.setWindowTitle(window_title)
|
||||||
completion.setText(
|
conformation.setText("Confirm Anki Collection base directory migration?")
|
||||||
f"Your Anki Collection was successfully moved from {migration_directories}"
|
conformation.setInformativeText(
|
||||||
)
|
f"The Anki Collection directory should be migrated from {migration_directories}\n\n"
|
||||||
completion.show()
|
f"If you would like to keep using the old location, consult the Startup Options "
|
||||||
completion.exec()
|
f"on the Anki documentation on website\n\n{appHelpSite}"
|
||||||
else:
|
)
|
||||||
self.base = oldBase
|
conformation.setDefaultButton(QMessageBox.Cancel)
|
||||||
|
retval = conformation.exec()
|
||||||
|
|
||||||
from PyQt5 import QtWidgets, QtGui
|
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()
|
||||||
|
|
||||||
app = QtWidgets.QApplication([])
|
completion = QMessageBox()
|
||||||
messageBox()
|
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