2012-12-21 08:51:59 +01:00
|
|
|
# Copyright: Damien Elmes <anki@ichi2.net>
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2013-10-18 00:48:45 +02:00
|
|
|
import re
|
|
|
|
import signal
|
2013-12-18 23:41:29 +01:00
|
|
|
import zipfile
|
2016-07-04 05:22:35 +02:00
|
|
|
import gc
|
|
|
|
import time
|
2017-01-08 04:38:12 +01:00
|
|
|
import faulthandler
|
2017-01-08 10:29:57 +01:00
|
|
|
from threading import Thread
|
2013-10-18 00:48:45 +02:00
|
|
|
|
2013-05-23 07:04:40 +02:00
|
|
|
from send2trash import send2trash
|
2012-12-21 08:51:59 +01:00
|
|
|
from aqt.qt import *
|
|
|
|
from anki import Collection
|
2017-08-01 06:28:13 +02:00
|
|
|
from anki.utils import isWin, isMac, intTime, splitFields, ids2str, \
|
|
|
|
devMode
|
2013-10-18 00:48:45 +02:00
|
|
|
from anki.hooks import runHook, addHook
|
|
|
|
import aqt
|
|
|
|
import aqt.progress
|
|
|
|
import aqt.webview
|
|
|
|
import aqt.toolbar
|
|
|
|
import aqt.stats
|
2016-07-07 15:39:48 +02:00
|
|
|
import aqt.mediasrv
|
2017-10-05 05:48:24 +02:00
|
|
|
from aqt.utils import showWarning
|
|
|
|
import anki.sound
|
2014-06-18 20:47:45 +02:00
|
|
|
from aqt.utils import saveGeom, restoreGeom, showInfo, showWarning, \
|
2013-02-20 07:12:07 +01:00
|
|
|
restoreState, getOnlyText, askUser, applyStyles, showText, tooltip, \
|
2017-08-16 11:45:39 +02:00
|
|
|
openHelp, openLink, checkInvalidFilename, getFile
|
2017-06-22 08:36:54 +02:00
|
|
|
import sip
|
2013-12-06 05:27:13 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
class AnkiQt(QMainWindow):
|
2017-10-03 04:12:57 +02:00
|
|
|
def __init__(self, app, profileManager, opts, args):
|
2012-12-21 08:51:59 +01:00
|
|
|
QMainWindow.__init__(self)
|
|
|
|
self.state = "startup"
|
2017-10-03 04:12:57 +02:00
|
|
|
self.opts = opts
|
2012-12-21 08:51:59 +01:00
|
|
|
aqt.mw = self
|
|
|
|
self.app = app
|
|
|
|
self.pm = profileManager
|
|
|
|
# running 2.0 for the first time?
|
|
|
|
if self.pm.meta['firstRun']:
|
|
|
|
# load the new deck user profile
|
|
|
|
self.pm.load(self.pm.profiles()[0])
|
|
|
|
self.pm.meta['firstRun'] = False
|
|
|
|
self.pm.save()
|
|
|
|
# init rest of app
|
2017-01-17 04:53:02 +01:00
|
|
|
self.safeMode = self.app.queryKeyboardModifiers() & Qt.ShiftModifier
|
2012-12-21 08:51:59 +01:00
|
|
|
try:
|
|
|
|
self.setupUI()
|
|
|
|
self.setupAddons()
|
|
|
|
except:
|
|
|
|
showInfo(_("Error during startup:\n%s") % traceback.format_exc())
|
|
|
|
sys.exit(1)
|
2013-05-17 08:32:17 +02:00
|
|
|
# must call this after ui set up
|
|
|
|
if self.safeMode:
|
|
|
|
tooltip(_("Shift key was held down. Skipping automatic "
|
|
|
|
"syncing and add-on loading."))
|
2012-12-21 08:51:59 +01:00
|
|
|
# were we given a file to import?
|
|
|
|
if args and args[0]:
|
2016-05-12 06:45:35 +02:00
|
|
|
self.onAppMsg(args[0])
|
2012-12-21 08:51:59 +01:00
|
|
|
# Load profile in a timer so we can let the window finish init and not
|
|
|
|
# close on profile load error.
|
|
|
|
self.progress.timer(10, self.setupProfile, False)
|
|
|
|
|
|
|
|
def setupUI(self):
|
|
|
|
self.col = None
|
2017-01-08 04:38:12 +01:00
|
|
|
self.setupCrashLog()
|
2017-01-13 07:20:39 +01:00
|
|
|
self.disableGC()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.setupAppMsg()
|
|
|
|
self.setupKeys()
|
|
|
|
self.setupThreads()
|
2017-07-28 08:19:06 +02:00
|
|
|
self.setupMediaServer()
|
2017-10-05 05:48:24 +02:00
|
|
|
self.setupSound()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.setupMainWindow()
|
|
|
|
self.setupSystemSpecific()
|
|
|
|
self.setupStyle()
|
|
|
|
self.setupMenus()
|
|
|
|
self.setupProgress()
|
|
|
|
self.setupErrorHandler()
|
|
|
|
self.setupSignals()
|
|
|
|
self.setupAutoUpdate()
|
2013-05-22 05:27:37 +02:00
|
|
|
self.setupHooks()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.setupRefreshTimer()
|
|
|
|
self.updateTitleBar()
|
|
|
|
# screens
|
|
|
|
self.setupDeckBrowser()
|
|
|
|
self.setupOverview()
|
|
|
|
self.setupReviewer()
|
|
|
|
|
|
|
|
# Profiles
|
|
|
|
##########################################################################
|
|
|
|
|
2017-08-16 11:45:39 +02:00
|
|
|
class ProfileManager(QMainWindow):
|
|
|
|
onClose = pyqtSignal()
|
|
|
|
closeFires = True
|
|
|
|
|
|
|
|
def closeEvent(self, evt):
|
|
|
|
if self.closeFires:
|
|
|
|
self.onClose.emit()
|
|
|
|
evt.accept()
|
|
|
|
|
|
|
|
def closeWithoutQuitting(self):
|
|
|
|
self.closeFires = False
|
|
|
|
self.close()
|
|
|
|
self.closeFires = True
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def setupProfile(self):
|
|
|
|
self.pendingImport = None
|
2017-08-16 11:45:39 +02:00
|
|
|
self.restoringBackup = False
|
2012-12-21 08:51:59 +01:00
|
|
|
# profile not provided on command line?
|
|
|
|
if not self.pm.name:
|
|
|
|
# if there's a single profile, load it automatically
|
|
|
|
profs = self.pm.profiles()
|
|
|
|
if len(profs) == 1:
|
2017-08-16 11:45:39 +02:00
|
|
|
self.pm.load(profs[0])
|
2012-12-21 08:51:59 +01:00
|
|
|
if not self.pm.name:
|
|
|
|
self.showProfileManager()
|
|
|
|
else:
|
|
|
|
self.loadProfile()
|
|
|
|
|
|
|
|
def showProfileManager(self):
|
2017-08-16 06:38:55 +02:00
|
|
|
self.pm.profile = None
|
2012-12-21 08:51:59 +01:00
|
|
|
self.state = "profileManager"
|
2017-08-16 11:45:39 +02:00
|
|
|
d = self.profileDiag = self.ProfileManager()
|
|
|
|
f = self.profileForm = aqt.forms.profiles.Ui_MainWindow()
|
2012-12-21 08:51:59 +01:00
|
|
|
f.setupUi(d)
|
2016-05-31 10:51:40 +02:00
|
|
|
f.login.clicked.connect(self.onOpenProfile)
|
|
|
|
f.profiles.itemDoubleClicked.connect(self.onOpenProfile)
|
2017-08-16 11:45:39 +02:00
|
|
|
f.openBackup.clicked.connect(self.onOpenBackup)
|
|
|
|
f.quit.clicked.connect(d.close)
|
|
|
|
d.onClose.connect(self.cleanupAndExit)
|
2016-05-31 10:51:40 +02:00
|
|
|
f.add.clicked.connect(self.onAddProfile)
|
|
|
|
f.rename.clicked.connect(self.onRenameProfile)
|
|
|
|
f.delete_2.clicked.connect(self.onRemProfile)
|
|
|
|
f.profiles.currentRowChanged.connect(self.onProfileRowChange)
|
2017-08-16 11:45:39 +02:00
|
|
|
f.statusbar.setVisible(False)
|
|
|
|
# enter key opens profile
|
|
|
|
QShortcut(QKeySequence("Return"), d, activated=self.onOpenProfile)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.refreshProfilesList()
|
|
|
|
# raise first, for osx testing
|
|
|
|
d.show()
|
|
|
|
|
|
|
|
def refreshProfilesList(self):
|
|
|
|
f = self.profileForm
|
|
|
|
f.profiles.clear()
|
|
|
|
profs = self.pm.profiles()
|
|
|
|
f.profiles.addItems(profs)
|
|
|
|
try:
|
|
|
|
idx = profs.index(self.pm.name)
|
|
|
|
except:
|
|
|
|
idx = 0
|
|
|
|
f.profiles.setCurrentRow(idx)
|
|
|
|
|
|
|
|
def onProfileRowChange(self, n):
|
|
|
|
if n < 0:
|
|
|
|
# called on .clear()
|
|
|
|
return
|
|
|
|
name = self.pm.profiles()[n]
|
|
|
|
f = self.profileForm
|
2017-08-16 11:45:39 +02:00
|
|
|
self.pm.load(name)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def openProfile(self):
|
|
|
|
name = self.pm.profiles()[self.profileForm.profiles.currentRow()]
|
2017-08-16 11:45:39 +02:00
|
|
|
return self.pm.load(name)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def onOpenProfile(self):
|
2017-08-16 11:45:39 +02:00
|
|
|
self.loadProfile(self.profileDiag.closeWithoutQuitting)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def profileNameOk(self, str):
|
2013-02-20 07:12:07 +01:00
|
|
|
return not checkInvalidFilename(str)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def onAddProfile(self):
|
|
|
|
name = getOnlyText(_("Name:"))
|
|
|
|
if name:
|
|
|
|
name = name.strip()
|
|
|
|
if name in self.pm.profiles():
|
|
|
|
return showWarning(_("Name exists."))
|
|
|
|
if not self.profileNameOk(name):
|
|
|
|
return
|
|
|
|
self.pm.create(name)
|
|
|
|
self.pm.name = name
|
|
|
|
self.refreshProfilesList()
|
|
|
|
|
|
|
|
def onRenameProfile(self):
|
|
|
|
name = getOnlyText(_("New name:"), default=self.pm.name)
|
|
|
|
if not name:
|
|
|
|
return
|
|
|
|
if name == self.pm.name:
|
|
|
|
return
|
|
|
|
if name in self.pm.profiles():
|
|
|
|
return showWarning(_("Name exists."))
|
|
|
|
if not self.profileNameOk(name):
|
|
|
|
return
|
|
|
|
self.pm.rename(name)
|
|
|
|
self.refreshProfilesList()
|
|
|
|
|
|
|
|
def onRemProfile(self):
|
|
|
|
profs = self.pm.profiles()
|
|
|
|
if len(profs) < 2:
|
|
|
|
return showWarning(_("There must be at least one profile."))
|
|
|
|
# sure?
|
|
|
|
if not askUser(_("""\
|
|
|
|
All cards, notes, and media for this profile will be deleted. \
|
2017-08-16 11:45:39 +02:00
|
|
|
Are you sure?"""), msgfunc=QMessageBox.warning, defaultno=True):
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
|
|
|
self.pm.remove(self.pm.name)
|
|
|
|
self.refreshProfilesList()
|
|
|
|
|
2017-08-16 11:45:39 +02:00
|
|
|
def onOpenBackup(self):
|
|
|
|
if not askUser(_("""\
|
|
|
|
Replace your collection with an earlier backup?"""),
|
|
|
|
msgfunc=QMessageBox.warning,
|
|
|
|
defaultno=True):
|
|
|
|
return
|
|
|
|
def doOpen(path):
|
|
|
|
self._openBackup(path)
|
|
|
|
getFile(self.profileDiag, _("Revert to backup"),
|
2017-09-10 08:58:55 +02:00
|
|
|
cb=doOpen, filter="*.colpkg", dir=self.pm.backupFolder())
|
2017-08-16 11:45:39 +02:00
|
|
|
|
|
|
|
def _openBackup(self, path):
|
|
|
|
try:
|
|
|
|
# move the existing collection to the trash, as it may not open
|
|
|
|
self.pm.trashCollection()
|
|
|
|
except:
|
|
|
|
showWarning(_("Unable to move existing file to trash - please try restarting your computer."))
|
|
|
|
return
|
|
|
|
|
|
|
|
self.pendingImport = path
|
|
|
|
self.restoringBackup = True
|
|
|
|
|
|
|
|
showInfo(_("""\
|
|
|
|
Automatic syncing and backups have been disabled while restoring. To enable them again, \
|
|
|
|
close the profile or restart Anki."""))
|
|
|
|
|
|
|
|
self.onOpenProfile()
|
|
|
|
|
|
|
|
def loadProfile(self, onsuccess=None):
|
2017-08-16 06:38:55 +02:00
|
|
|
self.maybeAutoSync()
|
|
|
|
|
|
|
|
if not self.loadCollection():
|
|
|
|
return
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# show main window
|
|
|
|
if self.pm.profile['mainWindowState']:
|
|
|
|
restoreGeom(self, "mainWindow")
|
|
|
|
restoreState(self, "mainWindow")
|
2013-05-22 06:04:45 +02:00
|
|
|
# titlebar
|
|
|
|
self.setWindowTitle("Anki - " + self.pm.name)
|
2012-12-21 08:51:59 +01:00
|
|
|
# show and raise window for osx
|
|
|
|
self.show()
|
|
|
|
self.activateWindow()
|
|
|
|
self.raise_()
|
2017-08-16 06:38:55 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# import pending?
|
|
|
|
if self.pendingImport:
|
2017-08-16 11:45:39 +02:00
|
|
|
self.handleImport(self.pendingImport)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.pendingImport = None
|
|
|
|
runHook("profileLoaded")
|
2017-08-16 11:45:39 +02:00
|
|
|
if onsuccess:
|
|
|
|
onsuccess()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2017-08-16 06:38:55 +02:00
|
|
|
def unloadProfile(self, onsuccess):
|
|
|
|
def callback():
|
|
|
|
self._unloadProfile()
|
|
|
|
onsuccess()
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
runHook("unloadProfile")
|
2017-08-16 06:38:55 +02:00
|
|
|
self.unloadCollection(callback)
|
|
|
|
|
|
|
|
def _unloadProfile(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
self.pm.profile['mainWindowGeom'] = self.saveGeometry()
|
|
|
|
self.pm.profile['mainWindowState'] = self.saveState()
|
|
|
|
self.pm.save()
|
|
|
|
self.hide()
|
2017-08-16 06:38:55 +02:00
|
|
|
|
2017-08-16 11:45:39 +02:00
|
|
|
self.restoringBackup = False
|
|
|
|
|
2017-08-16 06:38:55 +02:00
|
|
|
# at this point there should be no windows left
|
|
|
|
self._checkForUnclosedWidgets()
|
|
|
|
|
|
|
|
self.maybeAutoSync()
|
|
|
|
|
|
|
|
def _checkForUnclosedWidgets(self):
|
|
|
|
for w in self.app.topLevelWidgets():
|
|
|
|
if w.isVisible():
|
2017-08-25 04:14:59 +02:00
|
|
|
# windows with this property are safe to close immediately
|
2017-09-08 10:42:26 +02:00
|
|
|
if getattr(w, "silentlyClose", None):
|
2017-08-25 04:14:59 +02:00
|
|
|
w.close()
|
|
|
|
else:
|
|
|
|
showWarning(f"Window should have been closed: {w}")
|
2017-08-16 06:38:55 +02:00
|
|
|
|
|
|
|
def unloadProfileAndExit(self):
|
|
|
|
self.unloadProfile(self.cleanupAndExit)
|
|
|
|
|
|
|
|
def unloadProfileAndShowProfileManager(self):
|
|
|
|
self.unloadProfile(self.showProfileManager)
|
2017-08-08 04:55:30 +02:00
|
|
|
|
|
|
|
def cleanupAndExit(self):
|
|
|
|
self.errorHandler.unload()
|
|
|
|
self.mediaServer.shutdown()
|
2017-10-05 05:48:24 +02:00
|
|
|
anki.sound.cleanupMPV()
|
2017-08-08 04:55:30 +02:00
|
|
|
self.app.exit(0)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2017-10-05 05:48:24 +02:00
|
|
|
# Sound/video
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def setupSound(self):
|
|
|
|
try:
|
|
|
|
anki.sound.setupMPV()
|
|
|
|
except FileNotFoundError:
|
|
|
|
showWarning(_("mpv is not installed - audio and video on cards will not work."))
|
|
|
|
anki.sound._player = lambda file: 1
|
|
|
|
anki.sound._queueEraser = lambda: 1
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Collection load/unload
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def loadCollection(self):
|
2014-01-14 07:24:42 +01:00
|
|
|
cpath = self.pm.collectionPath()
|
2012-12-21 08:51:59 +01:00
|
|
|
try:
|
2014-01-14 07:24:42 +01:00
|
|
|
self.col = Collection(cpath, log=True)
|
2017-08-16 06:38:55 +02:00
|
|
|
except Exception as e:
|
2014-06-26 21:25:40 +02:00
|
|
|
showWarning(_("""\
|
2017-08-16 06:38:55 +02:00
|
|
|
Anki was unable to open your collection file. If problems persist after \
|
2017-08-16 11:45:39 +02:00
|
|
|
restarting your computer, please use the Open Backup button in the profile \
|
|
|
|
manager.
|
2014-06-26 21:25:40 +02:00
|
|
|
|
|
|
|
Debug info:
|
|
|
|
""")+traceback.format_exc())
|
2017-08-16 06:38:55 +02:00
|
|
|
self.showProfileManager()
|
|
|
|
return False
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
self.progress.setupDB(self.col.db)
|
2012-12-21 12:55:57 +01:00
|
|
|
self.maybeEnableUndo()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.moveToState("deckBrowser")
|
2017-08-16 06:38:55 +02:00
|
|
|
return True
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2017-08-16 06:38:55 +02:00
|
|
|
def unloadCollection(self, onsuccess):
|
|
|
|
def callback():
|
|
|
|
self._unloadCollection()
|
|
|
|
onsuccess()
|
2013-04-23 15:37:21 +02:00
|
|
|
|
2017-08-16 06:38:55 +02:00
|
|
|
self.closeAllWindows(callback)
|
|
|
|
|
|
|
|
def _unloadCollection(self):
|
2017-08-16 11:45:39 +02:00
|
|
|
if not self.col:
|
|
|
|
return
|
|
|
|
if self.restoringBackup:
|
|
|
|
label = _("Closing...")
|
|
|
|
else:
|
|
|
|
label = _("Backing Up...")
|
|
|
|
self.progress.start(label=label, immediate=True)
|
2017-08-16 06:38:55 +02:00
|
|
|
corrupt = False
|
|
|
|
try:
|
|
|
|
self.maybeOptimize()
|
|
|
|
if not devMode:
|
|
|
|
corrupt = self.col.db.scalar("pragma integrity_check") != "ok"
|
|
|
|
except:
|
|
|
|
corrupt = True
|
|
|
|
try:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.col.close()
|
2017-08-16 13:06:50 +02:00
|
|
|
except:
|
|
|
|
corrupt = True
|
2017-08-16 11:45:39 +02:00
|
|
|
finally:
|
|
|
|
self.col = None
|
2017-08-16 13:06:50 +02:00
|
|
|
if corrupt:
|
|
|
|
showWarning(_("Your collection file appears to be corrupt. \
|
|
|
|
This can happen when the file is copied or moved while Anki is open, or \
|
|
|
|
when the collection is stored on a network or cloud drive. If problems \
|
|
|
|
persist after restarting your computer, please open an automatic backup \
|
|
|
|
from the profile screen."))
|
2017-08-16 11:45:39 +02:00
|
|
|
if not corrupt and not self.restoringBackup:
|
2017-08-16 06:38:55 +02:00
|
|
|
self.backup()
|
2017-08-16 11:45:39 +02:00
|
|
|
|
2017-08-16 06:38:55 +02:00
|
|
|
self.progress.finish()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Backup and auto-optimize
|
|
|
|
##########################################################################
|
|
|
|
|
2017-01-08 10:29:57 +01:00
|
|
|
class BackupThread(Thread):
|
|
|
|
def __init__(self, path, data):
|
|
|
|
Thread.__init__(self)
|
|
|
|
self.path = path
|
|
|
|
self.data = data
|
|
|
|
# create the file in calling thread to ensure the same
|
|
|
|
# file is not created twice
|
|
|
|
open(self.path, "wb").close()
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
z = zipfile.ZipFile(self.path, "w", zipfile.ZIP_DEFLATED)
|
|
|
|
z.writestr("collection.anki2", self.data)
|
|
|
|
z.writestr("media", "{}")
|
|
|
|
z.close()
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def backup(self):
|
|
|
|
nbacks = self.pm.profile['numBackups']
|
2017-08-01 06:28:13 +02:00
|
|
|
if not nbacks or devMode:
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
|
|
|
dir = self.pm.backupFolder()
|
|
|
|
path = self.pm.collectionPath()
|
2017-08-14 10:53:39 +02:00
|
|
|
|
|
|
|
# do backup
|
2017-09-10 08:58:55 +02:00
|
|
|
fname = time.strftime("backup-%Y-%m-%d-%H.%M.%S.colpkg", time.localtime(time.time()))
|
2017-08-14 10:53:39 +02:00
|
|
|
newpath = os.path.join(dir, fname)
|
|
|
|
data = open(path, "rb").read()
|
|
|
|
b = self.BackupThread(newpath, data)
|
|
|
|
b.start()
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# find existing backups
|
|
|
|
backups = []
|
|
|
|
for file in os.listdir(dir):
|
2017-08-14 10:53:39 +02:00
|
|
|
# only look for new-style format
|
2017-09-10 08:58:55 +02:00
|
|
|
m = re.match("backup-\d{4}-\d{2}-.+.colpkg", file)
|
2012-12-21 08:51:59 +01:00
|
|
|
if not m:
|
|
|
|
continue
|
2017-08-14 10:53:39 +02:00
|
|
|
backups.append(file)
|
2012-12-21 08:51:59 +01:00
|
|
|
backups.sort()
|
2017-08-14 10:53:39 +02:00
|
|
|
|
|
|
|
# remove old ones
|
|
|
|
while len(backups) > nbacks:
|
|
|
|
fname = backups.pop(0)
|
|
|
|
path = os.path.join(dir, fname)
|
|
|
|
send2trash(path)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def maybeOptimize(self):
|
2013-05-07 08:17:46 +02:00
|
|
|
# have two weeks passed?
|
2012-12-21 08:51:59 +01:00
|
|
|
if (intTime() - self.pm.profile['lastOptimize']) < 86400*14:
|
|
|
|
return
|
|
|
|
self.progress.start(label=_("Optimizing..."), immediate=True)
|
|
|
|
self.col.optimize()
|
|
|
|
self.pm.profile['lastOptimize'] = intTime()
|
|
|
|
self.pm.save()
|
|
|
|
self.progress.finish()
|
|
|
|
|
|
|
|
# State machine
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def moveToState(self, state, *args):
|
2017-01-17 05:05:05 +01:00
|
|
|
#print("-> move from", self.state, "to", state)
|
2012-12-21 08:51:59 +01:00
|
|
|
oldState = self.state or "dummy"
|
|
|
|
cleanup = getattr(self, "_"+oldState+"Cleanup", None)
|
|
|
|
if cleanup:
|
|
|
|
cleanup(state)
|
2017-06-22 08:36:54 +02:00
|
|
|
self.clearStateShortcuts()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.state = state
|
2014-04-22 00:16:39 +02:00
|
|
|
runHook('beforeStateChange', state, oldState, *args)
|
2012-12-21 08:51:59 +01:00
|
|
|
getattr(self, "_"+state+"State")(oldState, *args)
|
2017-01-17 05:05:05 +01:00
|
|
|
if state != "resetRequired":
|
|
|
|
self.bottomWeb.show()
|
2014-04-22 00:16:39 +02:00
|
|
|
runHook('afterStateChange', state, oldState, *args)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def _deckBrowserState(self, oldState):
|
|
|
|
self.deckBrowser.show()
|
|
|
|
|
|
|
|
def _colLoadingState(self, oldState):
|
|
|
|
"Run once, when col is loaded."
|
|
|
|
self.enableColMenuItems()
|
|
|
|
# ensure cwd is set if media dir exists
|
|
|
|
self.col.media.dir()
|
|
|
|
runHook("colLoading", self.col)
|
|
|
|
self.moveToState("overview")
|
|
|
|
|
|
|
|
def _selectedDeck(self):
|
|
|
|
did = self.col.decks.selected()
|
|
|
|
if not self.col.decks.nameOrNone(did):
|
|
|
|
showInfo(_("Please select a deck."))
|
|
|
|
return
|
|
|
|
return self.col.decks.get(did)
|
|
|
|
|
|
|
|
def _overviewState(self, oldState):
|
|
|
|
if not self._selectedDeck():
|
|
|
|
return self.moveToState("deckBrowser")
|
|
|
|
self.col.reset()
|
|
|
|
self.overview.show()
|
|
|
|
|
|
|
|
def _reviewState(self, oldState):
|
|
|
|
self.reviewer.show()
|
|
|
|
|
|
|
|
def _reviewCleanup(self, newState):
|
|
|
|
if newState != "resetRequired" and newState != "review":
|
|
|
|
self.reviewer.cleanup()
|
|
|
|
|
|
|
|
def noteChanged(self, nid):
|
|
|
|
"Called when a card or note is edited (but not deleted)."
|
|
|
|
runHook("noteChanged", nid)
|
|
|
|
|
|
|
|
# Resetting state
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def reset(self, guiOnly=False):
|
|
|
|
"Called for non-trivial edits. Rebuilds queue and updates UI."
|
|
|
|
if self.col:
|
|
|
|
if not guiOnly:
|
|
|
|
self.col.reset()
|
|
|
|
runHook("reset")
|
|
|
|
self.maybeEnableUndo()
|
|
|
|
self.moveToState(self.state)
|
|
|
|
|
|
|
|
def requireReset(self, modal=False):
|
|
|
|
"Signal queue needs to be rebuilt when edits are finished or by user."
|
|
|
|
self.autosave()
|
|
|
|
self.resetModal = modal
|
|
|
|
if self.interactiveState():
|
|
|
|
self.moveToState("resetRequired")
|
|
|
|
|
|
|
|
def interactiveState(self):
|
|
|
|
"True if not in profile manager, syncing, etc."
|
|
|
|
return self.state in ("overview", "review", "deckBrowser")
|
|
|
|
|
|
|
|
def maybeReset(self):
|
|
|
|
self.autosave()
|
|
|
|
if self.state == "resetRequired":
|
|
|
|
self.state = self.returnState
|
|
|
|
self.reset()
|
|
|
|
|
|
|
|
def delayedMaybeReset(self):
|
|
|
|
# if we redraw the page in a button click event it will often crash on
|
|
|
|
# windows
|
|
|
|
self.progress.timer(100, self.maybeReset, False)
|
|
|
|
|
|
|
|
def _resetRequiredState(self, oldState):
|
|
|
|
if oldState != "resetRequired":
|
|
|
|
self.returnState = oldState
|
|
|
|
if self.resetModal:
|
|
|
|
# we don't have to change the webview, as we have a covering window
|
|
|
|
return
|
2016-05-31 10:51:40 +02:00
|
|
|
self.web.resetHandlers()
|
2016-06-06 07:50:03 +02:00
|
|
|
self.web.onBridgeCmd = lambda url: self.delayedMaybeReset()
|
2012-12-21 08:51:59 +01:00
|
|
|
i = _("Waiting for editing to finish.")
|
|
|
|
b = self.button("refresh", _("Resume Now"), id="resume")
|
|
|
|
self.web.stdHtml("""
|
|
|
|
<center><div style="height: 100%%">
|
|
|
|
<div style="position:relative; vertical-align: middle;">
|
|
|
|
%s<br>
|
|
|
|
%s</div></div></center>
|
2017-08-01 05:57:15 +02:00
|
|
|
<script>$('#resume').focus()</script>
|
2017-08-10 11:02:32 +02:00
|
|
|
""" % (i, b))
|
2012-12-21 08:51:59 +01:00
|
|
|
self.bottomWeb.hide()
|
|
|
|
self.web.setFocus()
|
|
|
|
|
|
|
|
# HTML helpers
|
|
|
|
##########################################################################
|
|
|
|
|
2016-05-31 10:51:40 +02:00
|
|
|
def button(self, link, name, key=None, class_="", id="", extra=""):
|
2012-12-21 08:51:59 +01:00
|
|
|
class_ = "but "+ class_
|
|
|
|
if key:
|
|
|
|
key = _("Shortcut key: %s") % key
|
|
|
|
else:
|
|
|
|
key = ""
|
|
|
|
return '''
|
2016-06-06 07:50:03 +02:00
|
|
|
<button id="%s" class="%s" onclick="pycmd('%s');return false;"
|
2016-05-31 10:51:40 +02:00
|
|
|
title="%s" %s>%s</button>''' % (
|
|
|
|
id, class_, link, key, extra, name)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Main window setup
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def setupMainWindow(self):
|
|
|
|
# main window
|
|
|
|
self.form = aqt.forms.main.Ui_MainWindow()
|
|
|
|
self.form.setupUi(self)
|
|
|
|
# toolbar
|
2017-04-21 19:21:05 +02:00
|
|
|
tweb = self.toolbarWeb = aqt.webview.AnkiWebView()
|
2016-07-07 09:23:13 +02:00
|
|
|
tweb.title = "top toolbar"
|
2012-12-21 08:51:59 +01:00
|
|
|
tweb.setFocusPolicy(Qt.WheelFocus)
|
|
|
|
self.toolbar = aqt.toolbar.Toolbar(self, tweb)
|
|
|
|
self.toolbar.draw()
|
|
|
|
# main area
|
2014-09-15 08:04:14 +02:00
|
|
|
self.web = aqt.webview.AnkiWebView()
|
2016-07-07 09:23:13 +02:00
|
|
|
self.web.title = "main webview"
|
2012-12-21 08:51:59 +01:00
|
|
|
self.web.setFocusPolicy(Qt.WheelFocus)
|
|
|
|
self.web.setMinimumWidth(400)
|
|
|
|
# bottom area
|
|
|
|
sweb = self.bottomWeb = aqt.webview.AnkiWebView()
|
2016-07-07 09:23:13 +02:00
|
|
|
sweb.title = "bottom toolbar"
|
2012-12-21 08:51:59 +01:00
|
|
|
sweb.setFocusPolicy(Qt.WheelFocus)
|
|
|
|
# add in a layout
|
|
|
|
self.mainLayout = QVBoxLayout()
|
|
|
|
self.mainLayout.setContentsMargins(0,0,0,0)
|
|
|
|
self.mainLayout.setSpacing(0)
|
|
|
|
self.mainLayout.addWidget(tweb)
|
|
|
|
self.mainLayout.addWidget(self.web)
|
|
|
|
self.mainLayout.addWidget(sweb)
|
|
|
|
self.form.centralwidget.setLayout(self.mainLayout)
|
|
|
|
|
2017-08-16 06:38:55 +02:00
|
|
|
def closeAllWindows(self, onsuccess):
|
2017-08-16 11:45:39 +02:00
|
|
|
aqt.dialogs.closeAll(onsuccess)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Components
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def setupSignals(self):
|
|
|
|
signal.signal(signal.SIGINT, self.onSigInt)
|
|
|
|
|
|
|
|
def onSigInt(self, signum, frame):
|
|
|
|
# interrupt any current transaction and schedule a rollback & quit
|
2017-08-16 11:45:39 +02:00
|
|
|
if self.col:
|
|
|
|
self.col.db.interrupt()
|
2012-12-21 08:51:59 +01:00
|
|
|
def quit():
|
|
|
|
self.col.db.rollback()
|
|
|
|
self.close()
|
|
|
|
self.progress.timer(100, quit, False)
|
|
|
|
|
|
|
|
def setupProgress(self):
|
|
|
|
self.progress = aqt.progress.ProgressManager(self)
|
|
|
|
|
|
|
|
def setupErrorHandler(self):
|
|
|
|
import aqt.errors
|
|
|
|
self.errorHandler = aqt.errors.ErrorHandler(self)
|
|
|
|
|
|
|
|
def setupAddons(self):
|
|
|
|
import aqt.addons
|
|
|
|
self.addonManager = aqt.addons.AddonManager(self)
|
2017-08-28 12:51:43 +02:00
|
|
|
if not self.safeMode:
|
|
|
|
self.addonManager.loadAddons()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def setupThreads(self):
|
|
|
|
self._mainThread = QThread.currentThread()
|
|
|
|
|
|
|
|
def inMainThread(self):
|
|
|
|
return self._mainThread == QThread.currentThread()
|
|
|
|
|
|
|
|
def setupDeckBrowser(self):
|
|
|
|
from aqt.deckbrowser import DeckBrowser
|
|
|
|
self.deckBrowser = DeckBrowser(self)
|
|
|
|
|
|
|
|
def setupOverview(self):
|
|
|
|
from aqt.overview import Overview
|
|
|
|
self.overview = Overview(self)
|
|
|
|
|
|
|
|
def setupReviewer(self):
|
|
|
|
from aqt.reviewer import Reviewer
|
|
|
|
self.reviewer = Reviewer(self)
|
|
|
|
|
|
|
|
# Syncing
|
|
|
|
##########################################################################
|
|
|
|
|
2017-08-16 06:38:55 +02:00
|
|
|
# expects a current profile and a loaded collection; reloads
|
|
|
|
# collection after sync completes
|
|
|
|
def onSync(self):
|
|
|
|
self.unloadCollection(self._onSync)
|
|
|
|
|
|
|
|
def _onSync(self):
|
|
|
|
self._sync()
|
|
|
|
if not self.loadCollection():
|
|
|
|
return
|
|
|
|
|
|
|
|
# expects a current profile, but no collection loaded
|
|
|
|
def maybeAutoSync(self):
|
|
|
|
if (not self.pm.profile['syncKey']
|
|
|
|
or not self.pm.profile['autoSync']
|
2017-08-16 11:45:39 +02:00
|
|
|
or self.safeMode
|
|
|
|
or self.restoringBackup):
|
2017-08-16 06:38:55 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
# ok to sync
|
|
|
|
self._sync()
|
|
|
|
|
|
|
|
def _sync(self):
|
|
|
|
from aqt.sync import SyncManager
|
|
|
|
self.state = "sync"
|
|
|
|
self.syncer = SyncManager(self, self.pm)
|
|
|
|
self.syncer.sync()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Tools
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def raiseMain(self):
|
|
|
|
if not self.app.activeWindow():
|
|
|
|
# make sure window is shown
|
|
|
|
self.setWindowState(self.windowState() & ~Qt.WindowMinimized)
|
|
|
|
return True
|
|
|
|
|
|
|
|
def setStatus(self, text, timeout=3000):
|
|
|
|
self.form.statusbar.showMessage(text, timeout)
|
|
|
|
|
|
|
|
def setupStyle(self):
|
|
|
|
applyStyles(self)
|
|
|
|
|
|
|
|
# Key handling
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def setupKeys(self):
|
2017-06-22 08:36:54 +02:00
|
|
|
globalShortcuts = [
|
|
|
|
("Ctrl+Shift+;", self.onDebug),
|
|
|
|
("d", lambda: self.moveToState("deckBrowser")),
|
|
|
|
("s", self.onStudyKey),
|
|
|
|
("a", self.onAddCard),
|
|
|
|
("b", self.onBrowse),
|
|
|
|
("Shift+s", self.onStats),
|
|
|
|
("y", self.onSync)
|
|
|
|
]
|
|
|
|
self.applyShortcuts(globalShortcuts)
|
|
|
|
|
|
|
|
self.stateShortcuts = []
|
|
|
|
|
|
|
|
def applyShortcuts(self, shortcuts):
|
|
|
|
qshortcuts = []
|
|
|
|
for key, fn in shortcuts:
|
|
|
|
qshortcuts.append(QShortcut(QKeySequence(key), self, activated=fn))
|
|
|
|
return qshortcuts
|
|
|
|
|
|
|
|
def setStateShortcuts(self, shortcuts):
|
2017-08-08 02:09:12 +02:00
|
|
|
runHook(self.state+"StateShortcuts", shortcuts)
|
2017-06-22 08:36:54 +02:00
|
|
|
self.stateShortcuts = self.applyShortcuts(shortcuts)
|
|
|
|
|
|
|
|
def clearStateShortcuts(self):
|
|
|
|
for qs in self.stateShortcuts:
|
|
|
|
sip.delete(qs)
|
|
|
|
self.stateShortcuts = []
|
|
|
|
|
|
|
|
def onStudyKey(self):
|
|
|
|
if self.state == "overview":
|
|
|
|
self.col.startTimebox()
|
|
|
|
self.moveToState("review")
|
2017-06-06 07:56:21 +02:00
|
|
|
else:
|
2017-06-22 08:36:54 +02:00
|
|
|
self.moveToState("overview")
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# App exit
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def closeEvent(self, event):
|
2017-08-16 11:45:39 +02:00
|
|
|
if self.state == "profileManager":
|
|
|
|
# if profile manager active, this event may fire via OS X menu bar's
|
|
|
|
# quit option
|
|
|
|
self.profileDiag.close()
|
|
|
|
event.accept()
|
|
|
|
else:
|
|
|
|
# ignore the event for now, as we need time to clean up
|
|
|
|
event.ignore()
|
|
|
|
self.unloadProfileAndExit()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Undo & autosave
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def onUndo(self):
|
2012-12-21 23:42:52 +01:00
|
|
|
n = self.col.undoName()
|
2017-01-08 11:47:26 +01:00
|
|
|
if not n:
|
|
|
|
return
|
2012-12-21 08:51:59 +01:00
|
|
|
cid = self.col.undo()
|
|
|
|
if cid and self.state == "review":
|
|
|
|
card = self.col.getCard(cid)
|
|
|
|
self.reviewer.cardQueue.append(card)
|
2012-12-21 23:42:52 +01:00
|
|
|
else:
|
|
|
|
tooltip(_("Reverted to state prior to '%s'.") % n.lower())
|
2012-12-21 08:51:59 +01:00
|
|
|
self.reset()
|
|
|
|
self.maybeEnableUndo()
|
|
|
|
|
|
|
|
def maybeEnableUndo(self):
|
|
|
|
if self.col and self.col.undoName():
|
|
|
|
self.form.actionUndo.setText(_("Undo %s") %
|
|
|
|
self.col.undoName())
|
|
|
|
self.form.actionUndo.setEnabled(True)
|
|
|
|
runHook("undoState", True)
|
|
|
|
else:
|
|
|
|
self.form.actionUndo.setText(_("Undo"))
|
|
|
|
self.form.actionUndo.setEnabled(False)
|
|
|
|
runHook("undoState", False)
|
|
|
|
|
|
|
|
def checkpoint(self, name):
|
|
|
|
self.col.save(name)
|
|
|
|
self.maybeEnableUndo()
|
|
|
|
|
|
|
|
def autosave(self):
|
2017-01-13 07:20:39 +01:00
|
|
|
saved = self.col.autosave()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.maybeEnableUndo()
|
2017-01-13 07:20:39 +01:00
|
|
|
if saved:
|
|
|
|
self.doGC()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Other menu operations
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def onAddCard(self):
|
|
|
|
aqt.dialogs.open("AddCards", self)
|
|
|
|
|
|
|
|
def onBrowse(self):
|
|
|
|
aqt.dialogs.open("Browser", self)
|
|
|
|
|
|
|
|
def onEditCurrent(self):
|
|
|
|
aqt.dialogs.open("EditCurrent", self)
|
|
|
|
|
|
|
|
def onDeckConf(self, deck=None):
|
|
|
|
if not deck:
|
|
|
|
deck = self.col.decks.current()
|
|
|
|
if deck['dyn']:
|
|
|
|
import aqt.dyndeckconf
|
|
|
|
aqt.dyndeckconf.DeckConf(self, deck=deck)
|
|
|
|
else:
|
|
|
|
import aqt.deckconf
|
|
|
|
aqt.deckconf.DeckConf(self, deck)
|
|
|
|
|
|
|
|
def onOverview(self):
|
|
|
|
self.col.reset()
|
|
|
|
self.moveToState("overview")
|
|
|
|
|
|
|
|
def onStats(self):
|
|
|
|
deck = self._selectedDeck()
|
|
|
|
if not deck:
|
|
|
|
return
|
2017-06-22 08:49:53 +02:00
|
|
|
aqt.dialogs.open("DeckStats", self)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def onPrefs(self):
|
2017-09-10 07:15:12 +02:00
|
|
|
aqt.dialogs.open("Preferences", self)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2012-12-22 00:21:24 +01:00
|
|
|
def onNoteTypes(self):
|
|
|
|
import aqt.models
|
|
|
|
aqt.models.Models(self, self, fromMain=True)
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def onAbout(self):
|
2017-06-26 05:05:11 +02:00
|
|
|
aqt.dialogs.open("About", self)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def onDonate(self):
|
|
|
|
openLink(aqt.appDonate)
|
|
|
|
|
|
|
|
def onDocumentation(self):
|
|
|
|
openHelp("")
|
|
|
|
|
|
|
|
# Importing & exporting
|
|
|
|
##########################################################################
|
|
|
|
|
2013-06-30 00:08:37 +02:00
|
|
|
def handleImport(self, path):
|
|
|
|
import aqt.importing
|
|
|
|
if not os.path.exists(path):
|
|
|
|
return showInfo(_("Please use File>Import to import this file."))
|
|
|
|
|
|
|
|
aqt.importing.importFile(self, path)
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def onImport(self):
|
|
|
|
import aqt.importing
|
|
|
|
aqt.importing.onImport(self)
|
|
|
|
|
2014-06-20 02:13:12 +02:00
|
|
|
def onExport(self, did=None):
|
2012-12-21 08:51:59 +01:00
|
|
|
import aqt.exporting
|
2014-06-20 02:13:12 +02:00
|
|
|
aqt.exporting.ExportDialog(self, did=did)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Cramming
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def onCram(self, search=""):
|
|
|
|
import aqt.dyndeckconf
|
|
|
|
n = 1
|
2013-01-30 10:27:34 +01:00
|
|
|
deck = self.col.decks.current()
|
2012-12-21 08:51:59 +01:00
|
|
|
if not search:
|
|
|
|
if not deck['dyn']:
|
2013-01-15 00:09:02 +01:00
|
|
|
search = 'deck:"%s" ' % deck['name']
|
2012-12-21 08:51:59 +01:00
|
|
|
decks = self.col.decks.allNames()
|
|
|
|
while _("Filtered Deck %d") % n in decks:
|
|
|
|
n += 1
|
|
|
|
name = _("Filtered Deck %d") % n
|
|
|
|
did = self.col.decks.newDyn(name)
|
|
|
|
diag = aqt.dyndeckconf.DeckConf(self, first=True, search=search)
|
|
|
|
if not diag.ok:
|
|
|
|
# user cancelled first config
|
|
|
|
self.col.decks.rem(did)
|
2013-01-30 10:27:34 +01:00
|
|
|
self.col.decks.select(deck['id'])
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
|
|
|
self.moveToState("overview")
|
|
|
|
|
|
|
|
# Menu, title bar & status
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def setupMenus(self):
|
|
|
|
m = self.form
|
2017-08-16 06:38:55 +02:00
|
|
|
m.actionSwitchProfile.triggered.connect(
|
|
|
|
self.unloadProfileAndShowProfileManager)
|
2016-05-31 10:51:40 +02:00
|
|
|
m.actionImport.triggered.connect(self.onImport)
|
|
|
|
m.actionExport.triggered.connect(self.onExport)
|
|
|
|
m.actionExit.triggered.connect(self.close)
|
|
|
|
m.actionPreferences.triggered.connect(self.onPrefs)
|
|
|
|
m.actionAbout.triggered.connect(self.onAbout)
|
|
|
|
m.actionUndo.triggered.connect(self.onUndo)
|
|
|
|
m.actionFullDatabaseCheck.triggered.connect(self.onCheckDB)
|
|
|
|
m.actionCheckMediaDatabase.triggered.connect(self.onCheckMediaDB)
|
|
|
|
m.actionDocumentation.triggered.connect(self.onDocumentation)
|
|
|
|
m.actionDonate.triggered.connect(self.onDonate)
|
|
|
|
m.actionStudyDeck.triggered.connect(self.onStudyDeck)
|
|
|
|
m.actionCreateFiltered.triggered.connect(self.onCram)
|
|
|
|
m.actionEmptyCards.triggered.connect(self.onEmptyCards)
|
|
|
|
m.actionNoteTypes.triggered.connect(self.onNoteTypes)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def updateTitleBar(self):
|
|
|
|
self.setWindowTitle("Anki")
|
|
|
|
|
|
|
|
# Auto update
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def setupAutoUpdate(self):
|
|
|
|
import aqt.update
|
|
|
|
self.autoUpdate = aqt.update.LatestVersionFinder(self)
|
2016-05-31 10:51:40 +02:00
|
|
|
self.autoUpdate.newVerAvail.connect(self.newVerAvail)
|
|
|
|
self.autoUpdate.newMsg.connect(self.newMsg)
|
|
|
|
self.autoUpdate.clockIsOff.connect(self.clockIsOff)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.autoUpdate.start()
|
|
|
|
|
|
|
|
def newVerAvail(self, ver):
|
2013-02-20 00:27:26 +01:00
|
|
|
if self.pm.meta.get('suppressUpdate', None) != ver:
|
2012-12-21 08:51:59 +01:00
|
|
|
aqt.update.askAndUpdate(self, ver)
|
|
|
|
|
|
|
|
def newMsg(self, data):
|
|
|
|
aqt.update.showMessages(self, data)
|
|
|
|
|
2013-10-20 03:26:11 +02:00
|
|
|
def clockIsOff(self, diff):
|
2013-12-06 05:27:13 +01:00
|
|
|
diffText = ngettext("%s second", "%s seconds", diff) % diff
|
2013-10-20 03:26:11 +02:00
|
|
|
warn = _("""\
|
2013-12-09 07:45:39 +01:00
|
|
|
In order to ensure your collection works correctly when moved between \
|
2013-10-20 03:26:11 +02:00
|
|
|
devices, Anki requires your computer's internal clock to be set correctly. \
|
|
|
|
The internal clock can be wrong even if your system is showing the correct \
|
|
|
|
local time.
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2013-10-20 03:26:11 +02:00
|
|
|
Please go to the time settings on your computer and check the following:
|
|
|
|
|
|
|
|
- AM/PM
|
|
|
|
- Clock drift
|
|
|
|
- Day, month and year
|
|
|
|
- Timezone
|
|
|
|
- Daylight savings
|
|
|
|
|
2013-10-21 09:52:54 +02:00
|
|
|
Difference to correct time: %s.""") % diffText
|
2013-10-20 03:26:11 +02:00
|
|
|
showWarning(warn)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.app.closeAllWindows()
|
|
|
|
|
|
|
|
# Count refreshing
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def setupRefreshTimer(self):
|
|
|
|
# every 10 minutes
|
|
|
|
self.progress.timer(10*60*1000, self.onRefreshTimer, True)
|
|
|
|
|
|
|
|
def onRefreshTimer(self):
|
|
|
|
if self.state == "deckBrowser":
|
|
|
|
self.deckBrowser.refresh()
|
|
|
|
elif self.state == "overview":
|
|
|
|
self.overview.refresh()
|
|
|
|
|
2013-05-22 05:27:37 +02:00
|
|
|
# Permanent libanki hooks
|
2012-12-21 08:51:59 +01:00
|
|
|
##########################################################################
|
|
|
|
|
2013-05-22 05:27:37 +02:00
|
|
|
def setupHooks(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
addHook("modSchema", self.onSchemaMod)
|
2013-05-22 05:27:37 +02:00
|
|
|
addHook("remNotes", self.onRemNotes)
|
2013-11-17 08:03:58 +01:00
|
|
|
addHook("odueInvalid", self.onOdueInvalid)
|
|
|
|
|
|
|
|
def onOdueInvalid(self):
|
|
|
|
showWarning(_("""\
|
|
|
|
Invalid property found on card. Please use Tools>Check Database, \
|
|
|
|
and if the problem comes up again, please ask on the support site."""))
|
2013-05-22 05:27:37 +02:00
|
|
|
|
|
|
|
# Log note deletion
|
|
|
|
##########################################################################
|
|
|
|
|
2013-05-31 03:42:24 +02:00
|
|
|
def onRemNotes(self, col, nids):
|
2013-05-22 05:27:37 +02:00
|
|
|
path = os.path.join(self.pm.profileFolder(), "deleted.txt")
|
|
|
|
existed = os.path.exists(path)
|
2017-01-08 11:44:52 +01:00
|
|
|
with open(path, "ab") as f:
|
2013-05-22 05:27:37 +02:00
|
|
|
if not existed:
|
2017-01-08 11:44:52 +01:00
|
|
|
f.write(b"nid\tmid\tfields\n")
|
2013-05-31 03:42:24 +02:00
|
|
|
for id, mid, flds in col.db.execute(
|
2013-05-22 05:27:37 +02:00
|
|
|
"select id, mid, flds from notes where id in %s" %
|
|
|
|
ids2str(nids)):
|
|
|
|
fields = splitFields(flds)
|
2017-01-08 11:44:52 +01:00
|
|
|
f.write(("\t".join([str(id), str(mid)] + fields)).encode("utf8"))
|
|
|
|
f.write(b"\n")
|
2013-05-22 05:27:37 +02:00
|
|
|
|
|
|
|
# Schema modifications
|
|
|
|
##########################################################################
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def onSchemaMod(self, arg):
|
|
|
|
return askUser(_("""\
|
|
|
|
The requested change will require a full upload of the database when \
|
|
|
|
you next synchronize your collection. If you have reviews or other changes \
|
|
|
|
waiting on another device that haven't been synchronized here yet, they \
|
|
|
|
will be lost. Continue?"""))
|
|
|
|
|
|
|
|
# Advanced features
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def onCheckDB(self):
|
|
|
|
"True if no problems"
|
|
|
|
self.progress.start(immediate=True)
|
|
|
|
ret, ok = self.col.fixIntegrity()
|
|
|
|
self.progress.finish()
|
|
|
|
if not ok:
|
|
|
|
showText(ret)
|
|
|
|
else:
|
|
|
|
tooltip(ret)
|
|
|
|
self.reset()
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def onCheckMediaDB(self):
|
|
|
|
self.progress.start(immediate=True)
|
2017-08-28 14:01:13 +02:00
|
|
|
(nohave, unused, warnings) = self.col.media.check()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.progress.finish()
|
|
|
|
# generate report
|
|
|
|
report = ""
|
2017-08-28 14:01:13 +02:00
|
|
|
if warnings:
|
|
|
|
report += "\n".join(warnings) + "\n"
|
2012-12-21 08:51:59 +01:00
|
|
|
if unused:
|
2013-11-13 09:19:25 +01:00
|
|
|
if report:
|
|
|
|
report += "\n\n\n"
|
2012-12-21 08:51:59 +01:00
|
|
|
report += _(
|
|
|
|
"In media folder but not used by any cards:")
|
|
|
|
report += "\n" + "\n".join(unused)
|
|
|
|
if nohave:
|
|
|
|
if report:
|
|
|
|
report += "\n\n\n"
|
|
|
|
report += _(
|
|
|
|
"Used on cards but missing from media folder:")
|
|
|
|
report += "\n" + "\n".join(nohave)
|
|
|
|
if not report:
|
2013-07-23 18:04:37 +02:00
|
|
|
tooltip(_("No unused or missing files found."))
|
|
|
|
return
|
2012-12-21 08:51:59 +01:00
|
|
|
# show report and offer to delete
|
|
|
|
diag = QDialog(self)
|
|
|
|
diag.setWindowTitle("Anki")
|
|
|
|
layout = QVBoxLayout(diag)
|
|
|
|
diag.setLayout(layout)
|
|
|
|
text = QTextEdit()
|
|
|
|
text.setReadOnly(True)
|
|
|
|
text.setPlainText(report)
|
|
|
|
layout.addWidget(text)
|
|
|
|
box = QDialogButtonBox(QDialogButtonBox.Close)
|
|
|
|
layout.addWidget(box)
|
|
|
|
b = QPushButton(_("Delete Unused"))
|
|
|
|
b.setAutoDefault(False)
|
|
|
|
box.addButton(b, QDialogButtonBox.ActionRole)
|
2016-05-31 10:51:40 +02:00
|
|
|
b.clicked.connect(
|
|
|
|
lambda c, u=unused, d=diag: self.deleteUnused(u, d))
|
|
|
|
box.rejected.connect(diag.reject)
|
2012-12-21 08:51:59 +01:00
|
|
|
diag.setMinimumHeight(400)
|
|
|
|
diag.setMinimumWidth(500)
|
2014-06-18 20:47:45 +02:00
|
|
|
restoreGeom(diag, "checkmediadb")
|
2012-12-21 08:51:59 +01:00
|
|
|
diag.exec_()
|
2014-06-18 20:47:45 +02:00
|
|
|
saveGeom(diag, "checkmediadb")
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def deleteUnused(self, unused, diag):
|
|
|
|
if not askUser(
|
2013-05-24 03:35:18 +02:00
|
|
|
_("Delete unused media?")):
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
|
|
|
mdir = self.col.media.dir()
|
|
|
|
for f in unused:
|
|
|
|
path = os.path.join(mdir, f)
|
2014-06-21 02:41:56 +02:00
|
|
|
if os.path.exists(path):
|
|
|
|
send2trash(path)
|
2012-12-21 08:51:59 +01:00
|
|
|
tooltip(_("Deleted."))
|
|
|
|
diag.close()
|
|
|
|
|
|
|
|
def onStudyDeck(self):
|
|
|
|
from aqt.studydeck import StudyDeck
|
2013-02-20 07:25:59 +01:00
|
|
|
ret = StudyDeck(
|
|
|
|
self, dyn=True, current=self.col.decks.current()['name'])
|
2012-12-21 08:51:59 +01:00
|
|
|
if ret.name:
|
|
|
|
self.col.decks.select(self.col.decks.id(ret.name))
|
|
|
|
self.moveToState("overview")
|
|
|
|
|
|
|
|
def onEmptyCards(self):
|
|
|
|
self.progress.start(immediate=True)
|
|
|
|
cids = self.col.emptyCids()
|
|
|
|
if not cids:
|
|
|
|
self.progress.finish()
|
|
|
|
tooltip(_("No empty cards."))
|
|
|
|
return
|
|
|
|
report = self.col.emptyCardReport(cids)
|
|
|
|
self.progress.finish()
|
|
|
|
part1 = ngettext("%d card", "%d cards", len(cids)) % len(cids)
|
|
|
|
part1 = _("%s to delete:") % part1
|
2014-06-18 20:47:45 +02:00
|
|
|
diag, box = showText(part1 + "\n\n" + report, run=False,
|
|
|
|
geomKey="emptyCards")
|
2012-12-21 08:51:59 +01:00
|
|
|
box.addButton(_("Delete Cards"), QDialogButtonBox.AcceptRole)
|
|
|
|
box.button(QDialogButtonBox.Close).setDefault(True)
|
|
|
|
def onDelete():
|
2014-06-18 20:47:45 +02:00
|
|
|
saveGeom(diag, "emptyCards")
|
2012-12-21 08:51:59 +01:00
|
|
|
QDialog.accept(diag)
|
|
|
|
self.checkpoint(_("Delete Empty"))
|
|
|
|
self.col.remCards(cids)
|
|
|
|
tooltip(ngettext("%d card deleted.", "%d cards deleted.", len(cids)) % len(cids))
|
|
|
|
self.reset()
|
2016-05-31 10:51:40 +02:00
|
|
|
box.accepted.connect(onDelete)
|
2012-12-21 08:51:59 +01:00
|
|
|
diag.show()
|
|
|
|
|
|
|
|
# Debugging
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def onDebug(self):
|
|
|
|
d = self.debugDiag = QDialog()
|
2017-08-25 04:14:59 +02:00
|
|
|
d.silentlyClose = True
|
2012-12-21 08:51:59 +01:00
|
|
|
frm = aqt.forms.debug.Ui_Dialog()
|
|
|
|
frm.setupUi(d)
|
|
|
|
s = self.debugDiagShort = QShortcut(QKeySequence("ctrl+return"), d)
|
2016-05-31 10:51:40 +02:00
|
|
|
s.activated.connect(lambda: self.onDebugRet(frm))
|
2012-12-21 08:51:59 +01:00
|
|
|
s = self.debugDiagShort = QShortcut(
|
|
|
|
QKeySequence("ctrl+shift+return"), d)
|
2016-05-31 10:51:40 +02:00
|
|
|
s.activated.connect(lambda: self.onDebugPrint(frm))
|
2012-12-21 08:51:59 +01:00
|
|
|
d.show()
|
|
|
|
|
|
|
|
def _captureOutput(self, on):
|
|
|
|
mw = self
|
2017-02-06 23:21:33 +01:00
|
|
|
class Stream:
|
2012-12-21 08:51:59 +01:00
|
|
|
def write(self, data):
|
|
|
|
mw._output += data
|
|
|
|
if on:
|
|
|
|
self._output = ""
|
|
|
|
self._oldStderr = sys.stderr
|
|
|
|
self._oldStdout = sys.stdout
|
|
|
|
s = Stream()
|
|
|
|
sys.stderr = s
|
|
|
|
sys.stdout = s
|
|
|
|
else:
|
|
|
|
sys.stderr = self._oldStderr
|
|
|
|
sys.stdout = self._oldStdout
|
|
|
|
|
|
|
|
def _debugCard(self):
|
|
|
|
return self.reviewer.card.__dict__
|
|
|
|
|
|
|
|
def _debugBrowserCard(self):
|
|
|
|
return aqt.dialogs._dialogs['Browser'][1].card.__dict__
|
|
|
|
|
|
|
|
def onDebugPrint(self, frm):
|
|
|
|
frm.text.setPlainText("pp(%s)" % frm.text.toPlainText())
|
|
|
|
self.onDebugRet(frm)
|
|
|
|
|
|
|
|
def onDebugRet(self, frm):
|
|
|
|
import pprint, traceback
|
|
|
|
text = frm.text.toPlainText()
|
|
|
|
card = self._debugCard
|
|
|
|
bcard = self._debugBrowserCard
|
|
|
|
mw = self
|
|
|
|
pp = pprint.pprint
|
|
|
|
self._captureOutput(True)
|
|
|
|
try:
|
2016-05-12 06:45:35 +02:00
|
|
|
exec(text)
|
2012-12-21 08:51:59 +01:00
|
|
|
except:
|
|
|
|
self._output += traceback.format_exc()
|
|
|
|
self._captureOutput(False)
|
|
|
|
buf = ""
|
|
|
|
for c, line in enumerate(text.strip().split("\n")):
|
|
|
|
if c == 0:
|
|
|
|
buf += ">>> %s\n" % line
|
|
|
|
else:
|
|
|
|
buf += "... %s\n" % line
|
2013-04-11 08:25:59 +02:00
|
|
|
try:
|
|
|
|
frm.log.appendPlainText(buf + (self._output or "<no output>"))
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
frm.log.appendPlainText(_("<non-unicode text>"))
|
2012-12-21 08:51:59 +01:00
|
|
|
frm.log.ensureCursorVisible()
|
|
|
|
|
|
|
|
# System specific code
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def setupSystemSpecific(self):
|
|
|
|
self.hideMenuAccels = False
|
|
|
|
if isMac:
|
|
|
|
# mac users expect a minimize option
|
|
|
|
self.minimizeShortcut = QShortcut("Ctrl+M", self)
|
2016-05-31 10:51:40 +02:00
|
|
|
self.minimizeShortcut.activated.connect(self.onMacMinimize)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.hideMenuAccels = True
|
|
|
|
self.maybeHideAccelerators()
|
|
|
|
self.hideStatusTips()
|
|
|
|
elif isWin:
|
|
|
|
# make sure ctypes is bundled
|
|
|
|
from ctypes import windll, wintypes
|
2013-10-18 00:48:45 +02:00
|
|
|
_dummy = windll
|
|
|
|
_dummy = wintypes
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def maybeHideAccelerators(self, tgt=None):
|
|
|
|
if not self.hideMenuAccels:
|
|
|
|
return
|
|
|
|
tgt = tgt or self
|
|
|
|
for action in tgt.findChildren(QAction):
|
2016-05-12 06:45:35 +02:00
|
|
|
txt = str(action.text())
|
2012-12-21 08:51:59 +01:00
|
|
|
m = re.match("^(.+)\(&.+\)(.+)?", txt)
|
|
|
|
if m:
|
|
|
|
action.setText(m.group(1) + (m.group(2) or ""))
|
|
|
|
|
|
|
|
def hideStatusTips(self):
|
|
|
|
for action in self.findChildren(QAction):
|
|
|
|
action.setStatusTip("")
|
|
|
|
|
|
|
|
def onMacMinimize(self):
|
|
|
|
self.setWindowState(self.windowState() | Qt.WindowMinimized)
|
|
|
|
|
|
|
|
# Single instance support
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def setupAppMsg(self):
|
2016-05-31 10:51:40 +02:00
|
|
|
self.app.appMsg.connect(self.onAppMsg)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def onAppMsg(self, buf):
|
|
|
|
if self.state == "startup":
|
|
|
|
# try again in a second
|
|
|
|
return self.progress.timer(1000, lambda: self.onAppMsg(buf), False)
|
|
|
|
elif self.state == "profileManager":
|
|
|
|
# can't raise window while in profile manager
|
|
|
|
if buf == "raise":
|
|
|
|
return
|
|
|
|
self.pendingImport = buf
|
|
|
|
return tooltip(_("Deck will be imported when a profile is opened."))
|
|
|
|
if not self.interactiveState() or self.progress.busy():
|
|
|
|
# we can't raise the main window while in profile dialog, syncing, etc
|
|
|
|
if buf != "raise":
|
|
|
|
showInfo(_("""\
|
|
|
|
Please ensure a profile is open and Anki is not busy, then try again."""),
|
|
|
|
parent=None)
|
|
|
|
return
|
|
|
|
# raise window
|
|
|
|
if isWin:
|
|
|
|
# on windows we can raise the window by minimizing and restoring
|
|
|
|
self.showMinimized()
|
|
|
|
self.setWindowState(Qt.WindowActive)
|
|
|
|
self.showNormal()
|
|
|
|
else:
|
|
|
|
# on osx we can raise the window. on unity the icon in the tray will just flash.
|
|
|
|
self.activateWindow()
|
|
|
|
self.raise_()
|
|
|
|
if buf == "raise":
|
|
|
|
return
|
|
|
|
# import
|
2013-06-30 00:08:37 +02:00
|
|
|
self.handleImport(buf)
|
2016-07-04 05:22:35 +02:00
|
|
|
|
|
|
|
# GC
|
|
|
|
##########################################################################
|
2017-01-08 05:42:50 +01:00
|
|
|
# ensure gc runs in main thread
|
2016-07-04 05:22:35 +02:00
|
|
|
|
|
|
|
def setupDialogGC(self, obj):
|
2017-08-02 03:34:49 +02:00
|
|
|
obj.finished.connect(lambda: self.gcWindow(obj))
|
2016-07-04 05:22:35 +02:00
|
|
|
|
|
|
|
def gcWindow(self, obj):
|
|
|
|
obj.deleteLater()
|
2017-07-08 04:28:11 +02:00
|
|
|
self.progress.timer(1000, self.doGC, False)
|
2016-07-04 05:22:35 +02:00
|
|
|
|
2017-01-13 07:20:39 +01:00
|
|
|
def disableGC(self):
|
2017-01-08 05:42:50 +01:00
|
|
|
gc.collect()
|
|
|
|
gc.disable()
|
|
|
|
|
2017-01-13 07:20:39 +01:00
|
|
|
def doGC(self):
|
|
|
|
assert not self.progress.inDB
|
2016-07-04 05:22:35 +02:00
|
|
|
gc.collect()
|
2016-07-07 15:39:48 +02:00
|
|
|
|
2017-01-08 04:38:12 +01:00
|
|
|
# Crash log
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def setupCrashLog(self):
|
|
|
|
p = os.path.join(self.pm.base, "crash.log")
|
2017-01-13 11:56:24 +01:00
|
|
|
self._crashLog = open(p, "ab", 0)
|
2017-01-08 04:38:12 +01:00
|
|
|
faulthandler.enable(self._crashLog)
|
|
|
|
|
2016-07-07 15:39:48 +02:00
|
|
|
# Media server
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def setupMediaServer(self):
|
|
|
|
self.mediaServer = aqt.mediasrv.MediaServer()
|
|
|
|
self.mediaServer.start()
|
|
|
|
|
|
|
|
def baseHTML(self):
|
2017-08-11 12:37:04 +02:00
|
|
|
return '<base href="%s">' % self.serverURL()
|
|
|
|
|
|
|
|
def serverURL(self):
|
|
|
|
return "http://127.0.0.1:%d/" % self.mediaServer.getPort()
|