2019-02-05 04:59:03 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
2012-12-21 08:51:59 +01:00
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
# Profile handling
|
|
|
|
##########################################################################
|
|
|
|
# - Saves in pickles rather than json to easily store Qt window state.
|
|
|
|
# - Saves in sqlite rather than a flat file so the config can't be corrupted
|
|
|
|
|
2017-08-28 10:35:24 +02:00
|
|
|
import io
|
2013-11-13 07:29:50 +01:00
|
|
|
import locale
|
2019-12-20 10:19:03 +01:00
|
|
|
import pickle
|
|
|
|
import random
|
|
|
|
import shutil
|
2020-04-16 01:53:29 +02:00
|
|
|
from typing import Any, Dict, List, Optional
|
2013-11-13 07:29:50 +01:00
|
|
|
|
2019-12-20 10:19:03 +01:00
|
|
|
from send2trash import send2trash
|
2019-12-15 23:51:38 +01:00
|
|
|
|
2016-04-05 03:02:01 +02:00
|
|
|
import anki.lang
|
2019-12-20 10:19:03 +01:00
|
|
|
import aqt.forms
|
2020-01-02 10:43:19 +01:00
|
|
|
import aqt.sound
|
2020-04-16 01:00:49 +02:00
|
|
|
from anki import Collection
|
2019-12-20 10:19:03 +01:00
|
|
|
from anki.db import DB
|
2020-02-27 11:32:57 +01:00
|
|
|
from anki.lang import _, without_unicode_isolation
|
2020-05-30 04:28:22 +02:00
|
|
|
from anki.rsbackend import SyncAuth
|
2019-12-20 10:19:03 +01:00
|
|
|
from anki.utils import intTime, isMac, isWin
|
|
|
|
from aqt import appHelpSite
|
|
|
|
from aqt.qt import *
|
2020-02-27 11:32:57 +01:00
|
|
|
from aqt.utils import TR, locale_dir, showWarning, tr
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
metaConf = dict(
|
|
|
|
ver=0,
|
|
|
|
updates=True,
|
|
|
|
created=intTime(),
|
2019-12-23 01:34:10 +01:00
|
|
|
id=random.randrange(0, 2 ** 63),
|
2012-12-21 08:51:59 +01:00
|
|
|
lastMsg=-1,
|
|
|
|
suppressUpdate=False,
|
|
|
|
firstRun=True,
|
|
|
|
defaultLang=None,
|
|
|
|
)
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
profileConf: Dict[str, Any] = dict(
|
2012-12-21 08:51:59 +01:00
|
|
|
# profile
|
|
|
|
mainWindowGeom=None,
|
|
|
|
mainWindowState=None,
|
2017-10-05 06:39:47 +02:00
|
|
|
numBackups=50,
|
2012-12-21 08:51:59 +01:00
|
|
|
lastOptimize=intTime(),
|
|
|
|
# editing
|
|
|
|
fullSearch=False,
|
|
|
|
searchHistory=[],
|
|
|
|
lastColour="#00f",
|
|
|
|
stripHTML=True,
|
|
|
|
pastePNG=False,
|
|
|
|
# not exposed in gui
|
|
|
|
deleteMedia=False,
|
|
|
|
preserveKeyboard=True,
|
|
|
|
# syncing
|
|
|
|
syncKey=None,
|
|
|
|
syncMedia=True,
|
|
|
|
autoSync=True,
|
2012-12-22 05:18:28 +01:00
|
|
|
# importing
|
|
|
|
allowHTML=False,
|
2013-11-26 10:19:54 +01:00
|
|
|
importMode=1,
|
2012-12-21 08:51:59 +01:00
|
|
|
)
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2019-12-19 00:38:36 +01:00
|
|
|
class LoadMetaResult:
|
|
|
|
firstTime: bool
|
|
|
|
loadError: bool
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-05-10 23:28:06 +02:00
|
|
|
class AnkiRestart(SystemExit):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.exitcode = kwargs.pop("exitcode", 0)
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
class ProfileManager:
|
2018-08-08 15:48:25 +02:00
|
|
|
def __init__(self, base=None):
|
2020-06-01 17:47:46 +02:00
|
|
|
## Settings which should be forgotten each Anki restart
|
|
|
|
self.session = {}
|
2012-12-21 08:51:59 +01:00
|
|
|
self.name = None
|
2014-08-09 05:50:23 +02:00
|
|
|
self.db = None
|
2020-06-02 05:23:01 +02:00
|
|
|
self.profile: Optional[Dict] = None
|
2012-12-21 08:51:59 +01:00
|
|
|
# instantiate base folder
|
2017-02-15 04:41:19 +01:00
|
|
|
self._setBaseFolder(base)
|
2018-08-08 15:48:25 +02:00
|
|
|
|
2019-12-19 00:38:36 +01:00
|
|
|
def setupMeta(self) -> LoadMetaResult:
|
2012-12-21 08:51:59 +01:00
|
|
|
# load metadata
|
2019-12-19 00:38:36 +01:00
|
|
|
res = self._loadMeta()
|
|
|
|
self.firstRun = res.firstTime
|
|
|
|
return res
|
2018-08-08 15:48:25 +02:00
|
|
|
|
|
|
|
# profile load on startup
|
|
|
|
def openProfile(self, profile):
|
2012-12-21 08:51:59 +01:00
|
|
|
if profile:
|
2016-05-12 06:19:16 +02:00
|
|
|
if profile not in self.profiles():
|
|
|
|
QMessageBox.critical(None, "Error", "Requested profile does not exist.")
|
|
|
|
sys.exit(1)
|
2012-12-21 08:51:59 +01:00
|
|
|
try:
|
|
|
|
self.load(profile)
|
|
|
|
except TypeError:
|
|
|
|
raise Exception("Provided profile does not exist.")
|
|
|
|
|
|
|
|
# Base creation
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def ensureBaseExists(self):
|
2019-12-24 11:33:39 +01:00
|
|
|
self._ensureExists(self.base)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2017-01-13 12:14:04 +01:00
|
|
|
# Folder migration
|
|
|
|
######################################################################
|
|
|
|
|
2017-02-22 09:16:19 +01:00
|
|
|
def _oldFolderLocation(self):
|
|
|
|
if isMac:
|
|
|
|
return os.path.expanduser("~/Documents/Anki")
|
|
|
|
elif isWin:
|
2018-08-08 15:48:25 +02:00
|
|
|
from aqt.winpaths import get_personal
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2018-08-08 15:48:25 +02:00
|
|
|
return os.path.join(get_personal(), "Anki")
|
2017-02-22 09:16:19 +01:00
|
|
|
else:
|
|
|
|
p = os.path.expanduser("~/Anki")
|
2018-07-11 07:23:24 +02:00
|
|
|
if os.path.isdir(p):
|
2017-02-22 09:16:19 +01:00
|
|
|
return p
|
2018-08-08 15:48:25 +02:00
|
|
|
return os.path.expanduser("~/Documents/Anki")
|
2017-02-22 09:16:19 +01:00
|
|
|
|
2017-01-13 12:14:04 +01:00
|
|
|
def maybeMigrateFolder(self):
|
2020-05-10 23:28:06 +02:00
|
|
|
newBase = self.base
|
2017-02-22 09:16:19 +01:00
|
|
|
oldBase = self._oldFolderLocation()
|
2017-01-13 12:14:04 +01:00
|
|
|
|
2018-08-08 15:48:25 +02:00
|
|
|
if oldBase and not os.path.exists(self.base) and os.path.isdir(oldBase):
|
2020-05-10 23:28:06 +02:00
|
|
|
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,
|
|
|
|
)
|
2020-05-18 12:15:59 +02:00
|
|
|
window_title = "Data Folder Migration"
|
2020-05-10 23:28:06 +02:00
|
|
|
migration_directories = f"\n\n {oldBase}\n\nto\n\n {self.base}"
|
|
|
|
|
2020-05-18 12:15:59 +02:00
|
|
|
confirmation = QMessageBox()
|
|
|
|
confirmation.setIcon(QMessageBox.Warning)
|
|
|
|
confirmation.setWindowIcon(icon)
|
|
|
|
confirmation.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
|
|
|
|
confirmation.setWindowTitle(window_title)
|
|
|
|
confirmation.setText(
|
|
|
|
"Anki needs to move its data folder from Documents/Anki to a new location. Proceed?"
|
2020-05-10 23:28:06 +02:00
|
|
|
)
|
2020-05-18 12:15:59 +02:00
|
|
|
retval = confirmation.exec()
|
2020-05-10 23:28:06 +02:00
|
|
|
|
|
|
|
if retval == QMessageBox.Ok:
|
|
|
|
progress = QMessageBox()
|
|
|
|
progress.setIcon(QMessageBox.Information)
|
|
|
|
progress.setStandardButtons(QMessageBox.NoButton)
|
|
|
|
progress.setWindowIcon(icon)
|
|
|
|
progress.setWindowTitle(window_title)
|
2020-05-18 12:15:59 +02:00
|
|
|
progress.setText("Please wait...")
|
2020-05-10 23:28:06 +02:00
|
|
|
progress.show()
|
|
|
|
app.processEvents()
|
2020-05-18 12:15:59 +02:00
|
|
|
|
2020-05-10 23:28:06 +02:00
|
|
|
shutil.move(oldBase, self.base)
|
|
|
|
progress.hide()
|
|
|
|
|
|
|
|
completion = QMessageBox()
|
|
|
|
completion.setIcon(QMessageBox.Information)
|
|
|
|
completion.setStandardButtons(QMessageBox.Ok)
|
|
|
|
completion.setWindowIcon(icon)
|
|
|
|
completion.setWindowTitle(window_title)
|
2020-05-18 12:15:59 +02:00
|
|
|
completion.setText("Migration complete. Please start Anki again.")
|
2020-05-10 23:28:06 +02:00
|
|
|
completion.show()
|
|
|
|
completion.exec()
|
|
|
|
else:
|
2020-05-18 12:15:59 +02:00
|
|
|
diag = QMessageBox()
|
|
|
|
diag.setIcon(QMessageBox.Warning)
|
|
|
|
diag.setWindowIcon(icon)
|
|
|
|
diag.setStandardButtons(QMessageBox.Ok)
|
|
|
|
diag.setWindowTitle(window_title)
|
|
|
|
diag.setText(
|
|
|
|
"Migration aborted. If you would like to keep the old folder location, please "
|
|
|
|
"see the Startup Options section of the manual. Anki will now quit."
|
|
|
|
)
|
|
|
|
diag.exec()
|
|
|
|
|
|
|
|
raise AnkiRestart(exitcode=0)
|
2017-01-13 12:14:04 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Profile load/save
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def profiles(self):
|
2019-03-06 06:27:13 +01:00
|
|
|
def names():
|
|
|
|
return self.db.list("select name from profiles where name != '_global'")
|
|
|
|
|
|
|
|
n = names()
|
|
|
|
if not n:
|
|
|
|
self._ensureProfile()
|
|
|
|
n = names()
|
|
|
|
|
|
|
|
return n
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2017-08-28 10:35:24 +02:00
|
|
|
def _unpickle(self, data):
|
|
|
|
class Unpickler(pickle.Unpickler):
|
|
|
|
def find_class(self, module, name):
|
2018-10-29 12:06:11 +01:00
|
|
|
if module == "PyQt5.sip":
|
|
|
|
try:
|
2020-02-27 04:27:58 +01:00
|
|
|
import PyQt5.sip # pylint: disable=unused-import
|
2018-10-29 12:06:11 +01:00
|
|
|
except:
|
|
|
|
# use old sip location
|
|
|
|
module = "sip"
|
2017-08-28 10:35:24 +02:00
|
|
|
fn = super().find_class(module, name)
|
|
|
|
if module == "sip" and name == "_unpickle_type":
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2017-08-28 10:35:24 +02:00
|
|
|
def wrapper(mod, obj, args):
|
|
|
|
if mod.startswith("PyQt4") and obj == "QByteArray":
|
|
|
|
# can't trust str objects from python 2
|
|
|
|
return QByteArray()
|
|
|
|
return fn(mod, obj, args)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2017-08-28 10:35:24 +02:00
|
|
|
return wrapper
|
|
|
|
else:
|
|
|
|
return fn
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2017-08-28 10:35:24 +02:00
|
|
|
up = Unpickler(io.BytesIO(data), errors="ignore")
|
|
|
|
return up.load()
|
|
|
|
|
|
|
|
def _pickle(self, obj):
|
|
|
|
return pickle.dumps(obj, protocol=0)
|
|
|
|
|
2017-08-16 11:45:39 +02:00
|
|
|
def load(self, name):
|
2017-08-28 10:35:24 +02:00
|
|
|
assert name != "_global"
|
2019-12-23 01:34:10 +01:00
|
|
|
data = self.db.scalar(
|
|
|
|
"select cast(data as blob) from profiles where name = ?", name
|
|
|
|
)
|
2017-08-28 10:35:24 +02:00
|
|
|
self.name = name
|
|
|
|
try:
|
|
|
|
self.profile = self._unpickle(data)
|
|
|
|
except:
|
2018-10-29 12:06:33 +01:00
|
|
|
QMessageBox.warning(
|
2019-12-23 01:34:10 +01:00
|
|
|
None,
|
|
|
|
_("Profile Corrupt"),
|
|
|
|
_(
|
|
|
|
"""\
|
2018-10-29 12:06:33 +01:00
|
|
|
Anki could not read your profile data. Window sizes and your sync login \
|
2019-12-23 01:34:10 +01:00
|
|
|
details have been forgotten."""
|
|
|
|
),
|
|
|
|
)
|
2018-10-29 12:06:33 +01:00
|
|
|
|
2017-08-28 10:35:24 +02:00
|
|
|
print("resetting corrupt profile")
|
|
|
|
self.profile = profileConf.copy()
|
|
|
|
self.save()
|
2012-12-21 08:51:59 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
def save(self):
|
|
|
|
sql = "update profiles set data = ? where name = ?"
|
2017-08-28 10:35:24 +02:00
|
|
|
self.db.execute(sql, self._pickle(self.profile), self.name)
|
|
|
|
self.db.execute(sql, self._pickle(self.meta), "_global")
|
2012-12-21 08:51:59 +01:00
|
|
|
self.db.commit()
|
|
|
|
|
|
|
|
def create(self, name):
|
|
|
|
prof = profileConf.copy()
|
2019-12-23 01:34:10 +01:00
|
|
|
self.db.execute(
|
|
|
|
"insert or ignore into profiles values (?, ?)", name, self._pickle(prof)
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.db.commit()
|
|
|
|
|
|
|
|
def remove(self, name):
|
2013-05-23 07:03:21 +02:00
|
|
|
p = self.profileFolder()
|
|
|
|
if os.path.exists(p):
|
|
|
|
send2trash(p)
|
2016-05-12 06:19:16 +02:00
|
|
|
self.db.execute("delete from profiles where name = ?", name)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.db.commit()
|
|
|
|
|
2017-08-16 11:45:39 +02:00
|
|
|
def trashCollection(self):
|
|
|
|
p = self.collectionPath()
|
|
|
|
if os.path.exists(p):
|
|
|
|
send2trash(p)
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def rename(self, name):
|
|
|
|
oldName = self.name
|
|
|
|
oldFolder = self.profileFolder()
|
|
|
|
self.name = name
|
|
|
|
newFolder = self.profileFolder(create=False)
|
|
|
|
if os.path.exists(newFolder):
|
2019-12-23 01:34:10 +01:00
|
|
|
if (oldFolder != newFolder) and (oldFolder.lower() == newFolder.lower()):
|
2014-06-18 00:51:02 +02:00
|
|
|
# OS is telling us the folder exists because it does not take
|
|
|
|
# case into account; use a temporary folder location
|
2019-12-23 01:34:10 +01:00
|
|
|
midFolder = "".join([oldFolder, "-temp"])
|
2014-06-18 00:51:02 +02:00
|
|
|
if not os.path.exists(midFolder):
|
|
|
|
os.rename(oldFolder, midFolder)
|
|
|
|
oldFolder = midFolder
|
|
|
|
else:
|
2019-12-23 01:34:10 +01:00
|
|
|
showWarning(
|
|
|
|
_("Please remove the folder %s and try again.") % midFolder
|
|
|
|
)
|
2014-06-18 00:51:02 +02:00
|
|
|
self.name = oldName
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
showWarning(_("Folder already exists."))
|
|
|
|
self.name = oldName
|
|
|
|
return
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# update name
|
2019-12-23 01:34:10 +01:00
|
|
|
self.db.execute("update profiles set name = ? where name = ?", name, oldName)
|
2012-12-21 08:51:59 +01:00
|
|
|
# rename folder
|
2014-06-28 18:22:07 +02:00
|
|
|
try:
|
|
|
|
os.rename(oldFolder, newFolder)
|
2019-03-04 03:29:55 +01:00
|
|
|
except Exception as e:
|
2014-06-28 18:22:07 +02:00
|
|
|
self.db.rollback()
|
2019-02-06 00:02:56 +01:00
|
|
|
if "WinError 5" in str(e):
|
2019-12-23 01:34:10 +01:00
|
|
|
showWarning(
|
|
|
|
_(
|
|
|
|
"""\
|
2014-06-28 18:22:07 +02:00
|
|
|
Anki could not rename your profile because it could not rename the profile \
|
|
|
|
folder on disk. Please ensure you have permission to write to Documents/Anki \
|
2019-12-23 01:34:10 +01:00
|
|
|
and no other programs are accessing your profile folders, then try again."""
|
|
|
|
)
|
|
|
|
)
|
2014-06-28 18:22:07 +02:00
|
|
|
else:
|
|
|
|
raise
|
|
|
|
except:
|
|
|
|
self.db.rollback()
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
self.db.commit()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Folder handling
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def profileFolder(self, create=True):
|
|
|
|
path = os.path.join(self.base, self.name)
|
|
|
|
if create:
|
|
|
|
self._ensureExists(path)
|
|
|
|
return path
|
|
|
|
|
|
|
|
def addonFolder(self):
|
2016-05-31 10:51:40 +02:00
|
|
|
return self._ensureExists(os.path.join(self.base, "addons21"))
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def backupFolder(self):
|
2019-12-23 01:34:10 +01:00
|
|
|
return self._ensureExists(os.path.join(self.profileFolder(), "backups"))
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def collectionPath(self):
|
|
|
|
return os.path.join(self.profileFolder(), "collection.anki2")
|
|
|
|
|
2020-04-16 01:00:49 +02:00
|
|
|
# Downgrade
|
|
|
|
######################################################################
|
|
|
|
|
2020-04-16 01:53:29 +02:00
|
|
|
def downgrade(self, profiles=List[str]) -> List[str]:
|
|
|
|
"Downgrade all profiles. Return a list of profiles that couldn't be opened."
|
|
|
|
problem_profiles = []
|
2020-04-16 01:00:49 +02:00
|
|
|
for name in profiles:
|
|
|
|
path = os.path.join(self.base, name, "collection.anki2")
|
|
|
|
if not os.path.exists(path):
|
|
|
|
continue
|
|
|
|
with DB(path) as db:
|
|
|
|
if db.scalar("select ver from col") == 11:
|
|
|
|
# nothing to do
|
|
|
|
continue
|
2020-04-16 01:53:29 +02:00
|
|
|
try:
|
|
|
|
c = Collection(path)
|
|
|
|
c.close(save=False, downgrade=True)
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
problem_profiles.append(name)
|
|
|
|
return problem_profiles
|
2020-04-16 01:00:49 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Helpers
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def _ensureExists(self, path):
|
|
|
|
if not os.path.exists(path):
|
|
|
|
os.makedirs(path)
|
|
|
|
return path
|
|
|
|
|
2017-02-15 04:41:19 +01:00
|
|
|
def _setBaseFolder(self, cmdlineBase):
|
|
|
|
if cmdlineBase:
|
|
|
|
self.base = os.path.abspath(cmdlineBase)
|
|
|
|
elif os.environ.get("ANKI_BASE"):
|
|
|
|
self.base = os.path.abspath(os.environ["ANKI_BASE"])
|
|
|
|
else:
|
|
|
|
self.base = self._defaultBase()
|
|
|
|
self.maybeMigrateFolder()
|
|
|
|
self.ensureBaseExists()
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def _defaultBase(self):
|
|
|
|
if isWin:
|
2018-08-08 15:48:25 +02:00
|
|
|
from aqt.winpaths import get_appdata
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2018-08-08 15:48:25 +02:00
|
|
|
return os.path.join(get_appdata(), "Anki2")
|
2012-12-21 08:51:59 +01:00
|
|
|
elif isMac:
|
2017-01-13 12:14:04 +01:00
|
|
|
return os.path.expanduser("~/Library/Application Support/Anki2")
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2017-02-22 09:16:19 +01:00
|
|
|
dataDir = os.environ.get(
|
2019-12-23 01:34:10 +01:00
|
|
|
"XDG_DATA_HOME", os.path.expanduser("~/.local/share")
|
|
|
|
)
|
2017-02-22 09:16:19 +01:00
|
|
|
if not os.path.exists(dataDir):
|
|
|
|
os.makedirs(dataDir)
|
|
|
|
return os.path.join(dataDir, "Anki2")
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
def _loadMeta(self, retrying=False) -> LoadMetaResult:
|
2019-12-19 00:38:36 +01:00
|
|
|
result = LoadMetaResult()
|
|
|
|
result.firstTime = False
|
|
|
|
result.loadError = retrying
|
|
|
|
|
2017-08-28 10:35:24 +02:00
|
|
|
opath = os.path.join(self.base, "prefs.db")
|
2016-05-31 10:51:40 +02:00
|
|
|
path = os.path.join(self.base, "prefs21.db")
|
2020-01-14 05:59:46 +01:00
|
|
|
if not retrying and os.path.exists(opath) and not os.path.exists(path):
|
2017-08-28 10:35:24 +02:00
|
|
|
shutil.copy(opath, path)
|
|
|
|
|
2019-12-19 00:38:36 +01:00
|
|
|
result.firstTime = not os.path.exists(path)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2013-07-18 11:59:14 +02:00
|
|
|
def recover():
|
|
|
|
# if we can't load profile, start with a new one
|
2014-07-07 06:18:00 +02:00
|
|
|
if self.db:
|
|
|
|
try:
|
|
|
|
self.db.close()
|
|
|
|
except:
|
|
|
|
pass
|
2018-03-22 05:10:23 +01:00
|
|
|
for suffix in ("", "-journal"):
|
|
|
|
fpath = path + suffix
|
|
|
|
if os.path.exists(fpath):
|
|
|
|
os.unlink(fpath)
|
2019-12-19 00:38:36 +01:00
|
|
|
|
|
|
|
# open DB file and read data
|
2013-07-18 11:59:14 +02:00
|
|
|
try:
|
2016-05-12 06:19:16 +02:00
|
|
|
self.db = DB(path)
|
2018-01-14 08:47:21 +01:00
|
|
|
assert self.db.scalar("pragma integrity_check") == "ok"
|
2019-12-23 01:34:10 +01:00
|
|
|
self.db.execute(
|
|
|
|
"""
|
2012-12-21 08:51:59 +01:00
|
|
|
create table if not exists profiles
|
2019-12-23 01:34:10 +01:00
|
|
|
(name text primary key, data text not null);"""
|
|
|
|
)
|
2017-08-28 10:35:24 +02:00
|
|
|
data = self.db.scalar(
|
2019-12-23 01:34:10 +01:00
|
|
|
"select cast(data as blob) from profiles where name = '_global'"
|
|
|
|
)
|
2013-07-18 11:59:14 +02:00
|
|
|
except:
|
2019-12-19 00:38:36 +01:00
|
|
|
if result.loadError:
|
|
|
|
# already failed, prevent infinite loop
|
|
|
|
raise
|
|
|
|
# delete files and try again
|
2013-07-18 11:59:14 +02:00
|
|
|
recover()
|
2019-12-19 00:38:36 +01:00
|
|
|
return self._loadMeta(retrying=True)
|
|
|
|
|
|
|
|
# try to read data
|
|
|
|
if not result.firstTime:
|
2013-02-15 00:59:44 +01:00
|
|
|
try:
|
2017-08-28 10:35:24 +02:00
|
|
|
self.meta = self._unpickle(data)
|
2019-12-19 00:38:36 +01:00
|
|
|
return result
|
2013-02-15 00:59:44 +01:00
|
|
|
except:
|
2017-08-28 10:35:24 +02:00
|
|
|
print("resetting corrupt _global")
|
2019-12-19 00:38:36 +01:00
|
|
|
result.loadError = True
|
2020-01-14 06:18:07 +01:00
|
|
|
result.firstTime = True
|
2019-12-19 00:38:36 +01:00
|
|
|
|
|
|
|
# if new or read failed, create a default global profile
|
2013-02-15 00:59:44 +01:00
|
|
|
self.meta = metaConf.copy()
|
2019-12-23 01:34:10 +01:00
|
|
|
self.db.execute(
|
|
|
|
"insert or replace into profiles values ('_global', ?)",
|
|
|
|
self._pickle(metaConf),
|
|
|
|
)
|
2019-12-19 00:38:36 +01:00
|
|
|
return result
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-03-06 06:27:13 +01:00
|
|
|
def _ensureProfile(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
"Create a new profile if none exists."
|
2019-03-06 06:27:13 +01:00
|
|
|
self.create(_("User 1"))
|
|
|
|
p = os.path.join(self.base, "README.txt")
|
2020-05-10 02:58:42 +02:00
|
|
|
with open(p, "w", encoding="utf8") as file:
|
|
|
|
file.write(
|
|
|
|
without_unicode_isolation(
|
|
|
|
tr(TR.PROFILES_FOLDER_README, link=appHelpSite + "#startupopts")
|
|
|
|
)
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Default language
|
|
|
|
######################################################################
|
|
|
|
# On first run, allow the user to choose the default language
|
|
|
|
|
2019-12-24 11:23:36 +01:00
|
|
|
def setDefaultLang(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
# create dialog
|
|
|
|
class NoCloseDiag(QDialog):
|
|
|
|
def reject(self):
|
|
|
|
pass
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
d = self.langDiag = NoCloseDiag()
|
|
|
|
f = self.langForm = aqt.forms.setlang.Ui_Dialog()
|
|
|
|
f.setupUi(d)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(d.accepted, self._onLangSelected)
|
|
|
|
qconnect(d.rejected, lambda: True)
|
2012-12-21 08:51:59 +01:00
|
|
|
# default to the system language
|
|
|
|
try:
|
|
|
|
(lang, enc) = locale.getdefaultlocale()
|
|
|
|
except:
|
|
|
|
# fails on osx
|
2019-09-23 13:18:03 +02:00
|
|
|
lang = "en_US"
|
2012-12-21 08:51:59 +01:00
|
|
|
# find index
|
|
|
|
idx = None
|
|
|
|
en = None
|
2016-04-05 03:02:01 +02:00
|
|
|
for c, (name, code) in enumerate(anki.lang.langs):
|
2019-09-23 13:18:03 +02:00
|
|
|
if code == "en_US":
|
2012-12-21 08:51:59 +01:00
|
|
|
en = c
|
|
|
|
if code == lang:
|
|
|
|
idx = c
|
|
|
|
# if the system language isn't available, revert to english
|
|
|
|
if idx is None:
|
|
|
|
idx = en
|
|
|
|
# update list
|
2016-04-05 03:02:01 +02:00
|
|
|
f.lang.addItems([x[0] for x in anki.lang.langs])
|
2012-12-21 08:51:59 +01:00
|
|
|
f.lang.setCurrentRow(idx)
|
|
|
|
d.exec_()
|
|
|
|
|
|
|
|
def _onLangSelected(self):
|
|
|
|
f = self.langForm
|
2016-04-05 03:02:01 +02:00
|
|
|
obj = anki.lang.langs[f.lang.currentRow()]
|
2014-01-14 06:40:45 +01:00
|
|
|
code = obj[1]
|
|
|
|
name = obj[0]
|
|
|
|
en = "Are you sure you wish to display Anki's interface in %s?"
|
|
|
|
r = QMessageBox.question(
|
2019-12-23 01:34:10 +01:00
|
|
|
None, "Anki", en % name, QMessageBox.Yes | QMessageBox.No, QMessageBox.No
|
|
|
|
)
|
2014-01-14 06:40:45 +01:00
|
|
|
if r != QMessageBox.Yes:
|
2019-12-24 11:23:36 +01:00
|
|
|
return self.setDefaultLang()
|
2016-04-05 03:02:01 +02:00
|
|
|
self.setLang(code)
|
|
|
|
|
|
|
|
def setLang(self, code):
|
2019-12-23 01:34:10 +01:00
|
|
|
self.meta["defaultLang"] = code
|
2012-12-21 08:51:59 +01:00
|
|
|
sql = "update profiles set data = ? where name = ?"
|
2017-08-28 10:35:24 +02:00
|
|
|
self.db.execute(sql, self._pickle(self.meta), "_global")
|
2012-12-21 08:51:59 +01:00
|
|
|
self.db.commit()
|
2020-02-16 06:46:06 +01:00
|
|
|
anki.lang.set_lang(code, locale_dir())
|
2018-08-08 15:48:25 +02:00
|
|
|
|
|
|
|
# OpenGL
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def _glPath(self):
|
|
|
|
return os.path.join(self.base, "gldriver")
|
|
|
|
|
|
|
|
def glMode(self):
|
2018-08-30 05:26:08 +02:00
|
|
|
if isMac:
|
|
|
|
return "auto"
|
2018-08-08 15:48:25 +02:00
|
|
|
|
|
|
|
path = self._glPath()
|
|
|
|
if not os.path.exists(path):
|
2019-01-06 00:59:25 +01:00
|
|
|
return "software"
|
2018-08-08 15:48:25 +02:00
|
|
|
|
2020-05-10 02:58:42 +02:00
|
|
|
with open(path, "r") as file:
|
|
|
|
mode = file.read().strip()
|
2018-08-08 15:48:25 +02:00
|
|
|
|
|
|
|
if mode == "angle" and isWin:
|
|
|
|
return mode
|
|
|
|
elif mode == "software":
|
|
|
|
return mode
|
|
|
|
return "auto"
|
|
|
|
|
|
|
|
def setGlMode(self, mode):
|
2020-05-10 02:58:42 +02:00
|
|
|
with open(self._glPath(), "w") as file:
|
|
|
|
file.write(mode)
|
2018-08-08 15:48:25 +02:00
|
|
|
|
|
|
|
def nextGlMode(self):
|
|
|
|
mode = self.glMode()
|
|
|
|
if mode == "software":
|
|
|
|
self.setGlMode("auto")
|
|
|
|
elif mode == "auto":
|
|
|
|
if isWin:
|
|
|
|
self.setGlMode("angle")
|
|
|
|
else:
|
|
|
|
self.setGlMode("software")
|
|
|
|
elif mode == "angle":
|
|
|
|
self.setGlMode("software")
|
2019-12-19 00:58:16 +01:00
|
|
|
|
2020-02-02 23:32:07 +01:00
|
|
|
# Shared options
|
2019-12-19 00:58:16 +01:00
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def uiScale(self) -> float:
|
2020-02-25 07:20:14 +01:00
|
|
|
scale = self.meta.get("uiScale", 1.0)
|
|
|
|
return max(scale, 1)
|
2019-12-19 00:58:16 +01:00
|
|
|
|
|
|
|
def setUiScale(self, scale: float) -> None:
|
|
|
|
self.meta["uiScale"] = scale
|
2020-01-19 02:31:09 +01:00
|
|
|
|
|
|
|
def last_addon_update_check(self) -> int:
|
|
|
|
return self.meta.get("last_addon_update_check", 0)
|
|
|
|
|
|
|
|
def set_last_addon_update_check(self, secs):
|
|
|
|
self.meta["last_addon_update_check"] = secs
|
2020-01-23 06:08:10 +01:00
|
|
|
|
|
|
|
def night_mode(self) -> bool:
|
|
|
|
return self.meta.get("night_mode", False)
|
|
|
|
|
|
|
|
def set_night_mode(self, on: bool) -> None:
|
|
|
|
self.meta["night_mode"] = on
|
2020-02-02 23:32:07 +01:00
|
|
|
|
2020-04-15 13:37:16 +02:00
|
|
|
def dark_mode_widgets(self) -> bool:
|
|
|
|
return self.meta.get("dark_mode_widgets", False)
|
|
|
|
|
2020-02-04 00:07:15 +01:00
|
|
|
# Profile-specific
|
2020-02-02 23:32:07 +01:00
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def interrupt_audio(self) -> bool:
|
|
|
|
return self.profile.get("interrupt_audio", True)
|
|
|
|
|
|
|
|
def set_interrupt_audio(self, val: bool) -> None:
|
|
|
|
self.profile["interrupt_audio"] = val
|
|
|
|
aqt.sound.av_player.interrupt_current_audio = val
|
|
|
|
|
2020-02-04 03:26:10 +01:00
|
|
|
def set_sync_key(self, val: Optional[str]) -> None:
|
|
|
|
self.profile["syncKey"] = val
|
|
|
|
|
2020-05-30 04:28:22 +02:00
|
|
|
def set_sync_username(self, val: Optional[str]) -> None:
|
|
|
|
self.profile["syncUser"] = val
|
|
|
|
|
|
|
|
def set_host_number(self, val: Optional[int]) -> None:
|
|
|
|
self.profile["hostNum"] = val or 0
|
|
|
|
|
2020-02-04 03:26:10 +01:00
|
|
|
def media_syncing_enabled(self) -> bool:
|
|
|
|
return self.profile["syncMedia"]
|
|
|
|
|
2020-05-31 02:53:54 +02:00
|
|
|
def auto_syncing_enabled(self) -> bool:
|
|
|
|
return self.profile["autoSync"]
|
|
|
|
|
2020-05-30 04:28:22 +02:00
|
|
|
def sync_auth(self) -> Optional[SyncAuth]:
|
|
|
|
hkey = self.profile.get("syncKey")
|
|
|
|
if not hkey:
|
|
|
|
return None
|
|
|
|
return SyncAuth(hkey=hkey, host_number=self.profile.get("hostNum", 0))
|
2020-02-04 03:46:57 +01:00
|
|
|
|
2020-05-31 03:46:40 +02:00
|
|
|
def clear_sync_auth(self) -> None:
|
|
|
|
self.profile["syncKey"] = None
|
|
|
|
self.profile["syncUser"] = None
|
|
|
|
self.profile["hostNum"] = 0
|
|
|
|
|
2020-07-01 03:35:24 +02:00
|
|
|
def auto_sync_media_minutes(self) -> int:
|
|
|
|
return self.profile.get("autoSyncMediaMinutes", 15)
|
|
|
|
|
|
|
|
def set_auto_sync_media_minutes(self, val: int):
|
|
|
|
self.profile["autoSyncMediaMinutes"] = val
|
|
|
|
|
2020-02-02 23:32:07 +01:00
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def apply_profile_options(self) -> None:
|
|
|
|
aqt.sound.av_player.interrupt_current_audio = self.interrupt_audio()
|