2012-12-21 08:51:59 +01:00
|
|
|
# Copyright: Damien Elmes <anki@ichi2.net>
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
2013-10-04 00:37:19 +02:00
|
|
|
import getpass
|
2013-10-21 02:22:14 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import optparse
|
|
|
|
import tempfile
|
|
|
|
import __builtin__
|
|
|
|
import locale
|
|
|
|
import gettext
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
from aqt.qt import *
|
|
|
|
import anki.lang
|
2012-12-21 10:04:26 +01:00
|
|
|
from anki.consts import HELP_SITE
|
|
|
|
from anki.lang import langDir
|
2013-04-16 12:54:23 +02:00
|
|
|
from anki.utils import isMac
|
2013-10-01 20:50:19 +02:00
|
|
|
from anki import version as _version
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2013-10-01 20:50:19 +02:00
|
|
|
appVersion=_version
|
2012-12-21 08:51:59 +01:00
|
|
|
appWebsite="http://ankisrs.net/"
|
|
|
|
appChanges="http://ankisrs.net/docs/changes.html"
|
|
|
|
appDonate="http://ankisrs.net/support/"
|
|
|
|
appShared="https://ankiweb.net/shared/"
|
|
|
|
appUpdate="https://ankiweb.net/update/desktop"
|
2012-12-21 10:04:26 +01:00
|
|
|
appHelpSite=HELP_SITE
|
2012-12-21 08:51:59 +01:00
|
|
|
mw = None # set on init
|
|
|
|
|
|
|
|
moduleDir = os.path.split(os.path.dirname(os.path.abspath(__file__)))[0]
|
|
|
|
|
|
|
|
try:
|
|
|
|
import aqt.forms
|
|
|
|
except ImportError, e:
|
|
|
|
if "forms" in str(e):
|
|
|
|
print "If you're running from git, did you run build_ui.sh?"
|
|
|
|
print
|
|
|
|
raise
|
|
|
|
|
2013-10-04 00:37:19 +02:00
|
|
|
from anki.utils import checksum
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Dialog manager - manages modeless windows
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
class DialogManager(object):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
from aqt import addcards, browser, editcurrent
|
|
|
|
self._dialogs = {
|
|
|
|
"AddCards": [addcards.AddCards, None],
|
|
|
|
"Browser": [browser.Browser, None],
|
|
|
|
"EditCurrent": [editcurrent.EditCurrent, None],
|
|
|
|
}
|
|
|
|
|
|
|
|
def open(self, name, *args):
|
|
|
|
(creator, instance) = self._dialogs[name]
|
|
|
|
if instance:
|
2014-07-13 19:42:18 +02:00
|
|
|
instance.setWindowState(instance.windowState() | Qt.WindowActive)
|
2012-12-21 08:51:59 +01:00
|
|
|
instance.activateWindow()
|
|
|
|
instance.raise_()
|
|
|
|
return instance
|
|
|
|
else:
|
|
|
|
instance = creator(*args)
|
|
|
|
self._dialogs[name][1] = instance
|
|
|
|
return instance
|
|
|
|
|
|
|
|
def close(self, name):
|
|
|
|
self._dialogs[name] = [self._dialogs[name][0], None]
|
|
|
|
|
|
|
|
def closeAll(self):
|
2013-04-11 12:23:32 +02:00
|
|
|
"True if all closed successfully."
|
2012-12-21 08:51:59 +01:00
|
|
|
for (n, (creator, instance)) in self._dialogs.items():
|
|
|
|
if instance:
|
2013-04-11 12:23:32 +02:00
|
|
|
if not instance.canClose():
|
|
|
|
return False
|
2012-12-21 08:51:59 +01:00
|
|
|
instance.forceClose = True
|
|
|
|
instance.close()
|
|
|
|
self.close(n)
|
2013-04-11 12:23:32 +02:00
|
|
|
return True
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
dialogs = DialogManager()
|
|
|
|
|
|
|
|
# Language handling
|
|
|
|
##########################################################################
|
|
|
|
# Qt requires its translator to be installed before any GUI widgets are
|
|
|
|
# loaded, and we need the Qt language to match the gettext language or
|
|
|
|
# translated shortcuts will not work.
|
|
|
|
|
|
|
|
_gtrans = None
|
|
|
|
_qtrans = None
|
|
|
|
|
|
|
|
def setupLang(pm, app, force=None):
|
|
|
|
global _gtrans, _qtrans
|
|
|
|
try:
|
|
|
|
locale.setlocale(locale.LC_ALL, '')
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
lang = force or pm.meta["defaultLang"]
|
|
|
|
dir = langDir()
|
|
|
|
# gettext
|
|
|
|
_gtrans = gettext.translation(
|
2012-12-21 10:04:26 +01:00
|
|
|
'anki', dir, languages=[lang], fallback=True)
|
2012-12-21 08:51:59 +01:00
|
|
|
__builtin__.__dict__['_'] = _gtrans.ugettext
|
|
|
|
__builtin__.__dict__['ngettext'] = _gtrans.ungettext
|
|
|
|
anki.lang.setLang(lang, local=False)
|
|
|
|
if lang in ("he","ar","fa"):
|
|
|
|
app.setLayoutDirection(Qt.RightToLeft)
|
|
|
|
else:
|
|
|
|
app.setLayoutDirection(Qt.LeftToRight)
|
|
|
|
# qt
|
|
|
|
_qtrans = QTranslator()
|
|
|
|
if _qtrans.load("qt_" + lang, dir):
|
|
|
|
app.installTranslator(_qtrans)
|
|
|
|
|
|
|
|
# App initialisation
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
class AnkiApp(QApplication):
|
|
|
|
|
|
|
|
# Single instance support on Win32/Linux
|
|
|
|
##################################################
|
|
|
|
|
2013-10-04 00:37:19 +02:00
|
|
|
KEY = "anki"+checksum(getpass.getuser())
|
2012-12-21 08:51:59 +01:00
|
|
|
TMOUT = 5000
|
|
|
|
|
|
|
|
def __init__(self, argv):
|
|
|
|
QApplication.__init__(self, argv)
|
|
|
|
self._argv = argv
|
|
|
|
|
|
|
|
def secondInstance(self):
|
2013-10-04 00:37:19 +02:00
|
|
|
# we accept only one command line argument. if it's missing, send
|
|
|
|
# a blank screen to just raise the existing window
|
|
|
|
opts, args = parseArgs(self._argv)
|
|
|
|
buf = "raise"
|
|
|
|
if args and args[0]:
|
|
|
|
buf = os.path.abspath(args[0])
|
|
|
|
if self.sendMsg(buf):
|
|
|
|
print "Already running; reusing existing instance."
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
# send failed, so we're the first instance or the
|
|
|
|
# previous instance died
|
2012-12-21 08:51:59 +01:00
|
|
|
QLocalServer.removeServer(self.KEY)
|
|
|
|
self._srv = QLocalServer(self)
|
|
|
|
self.connect(self._srv, SIGNAL("newConnection()"), self.onRecv)
|
|
|
|
self._srv.listen(self.KEY)
|
2013-10-04 00:37:19 +02:00
|
|
|
return False
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def sendMsg(self, txt):
|
|
|
|
sock = QLocalSocket(self)
|
|
|
|
sock.connectToServer(self.KEY, QIODevice.WriteOnly)
|
|
|
|
if not sock.waitForConnected(self.TMOUT):
|
2013-10-04 00:37:19 +02:00
|
|
|
# first instance or previous instance dead
|
|
|
|
return False
|
2012-12-21 08:51:59 +01:00
|
|
|
sock.write(txt)
|
|
|
|
if not sock.waitForBytesWritten(self.TMOUT):
|
2014-08-26 08:25:22 +02:00
|
|
|
# existing instance running but hung
|
|
|
|
return False
|
2012-12-21 08:51:59 +01:00
|
|
|
sock.disconnectFromServer()
|
2013-10-04 00:37:19 +02:00
|
|
|
return True
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def onRecv(self):
|
|
|
|
sock = self._srv.nextPendingConnection()
|
|
|
|
if not sock.waitForReadyRead(self.TMOUT):
|
|
|
|
sys.stderr.write(sock.errorString())
|
|
|
|
return
|
|
|
|
buf = sock.readAll()
|
2013-10-21 02:22:14 +02:00
|
|
|
buf = unicode(buf, sys.getfilesystemencoding(), "ignore")
|
2012-12-21 08:51:59 +01:00
|
|
|
self.emit(SIGNAL("appMsg"), buf)
|
|
|
|
sock.disconnectFromServer()
|
|
|
|
|
|
|
|
# OS X file/url handler
|
|
|
|
##################################################
|
|
|
|
|
|
|
|
def event(self, evt):
|
|
|
|
if evt.type() == QEvent.FileOpen:
|
|
|
|
self.emit(SIGNAL("appMsg"), evt.file() or "raise")
|
|
|
|
return True
|
|
|
|
return QApplication.event(self, evt)
|
|
|
|
|
|
|
|
def parseArgs(argv):
|
|
|
|
"Returns (opts, args)."
|
2013-04-16 12:54:23 +02:00
|
|
|
# py2app fails to strip this in some instances, then anki dies
|
|
|
|
# as there's no such profile
|
|
|
|
if isMac and len(argv) > 1 and argv[1].startswith("-psn"):
|
|
|
|
argv = [argv[0]]
|
2013-02-21 20:50:08 +01:00
|
|
|
parser = optparse.OptionParser(version="%prog " + appVersion)
|
2012-12-21 08:51:59 +01:00
|
|
|
parser.usage = "%prog [OPTIONS] [file to import]"
|
|
|
|
parser.add_option("-b", "--base", help="path to base folder")
|
|
|
|
parser.add_option("-p", "--profile", help="profile name to load")
|
|
|
|
parser.add_option("-l", "--lang", help="interface language (en, de, etc)")
|
|
|
|
return parser.parse_args(argv[1:])
|
|
|
|
|
|
|
|
def run():
|
2014-07-07 03:41:56 +02:00
|
|
|
try:
|
|
|
|
_run()
|
|
|
|
except Exception, e:
|
|
|
|
QMessageBox.critical(None, "Startup Error",
|
|
|
|
"Please notify support of this error:\n\n"+
|
|
|
|
traceback.format_exc())
|
|
|
|
|
|
|
|
def _run():
|
2012-12-21 08:51:59 +01:00
|
|
|
global mw
|
|
|
|
|
2013-02-21 20:51:06 +01:00
|
|
|
# parse args
|
|
|
|
opts, args = parseArgs(sys.argv)
|
|
|
|
opts.base = unicode(opts.base or "", sys.getfilesystemencoding())
|
|
|
|
opts.profile = unicode(opts.profile or "", sys.getfilesystemencoding())
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# on osx we'll need to add the qt plugins to the search path
|
|
|
|
if isMac and getattr(sys, 'frozen', None):
|
|
|
|
rd = os.path.abspath(moduleDir + "/../../..")
|
|
|
|
QCoreApplication.setLibraryPaths([rd])
|
|
|
|
|
2013-10-26 03:53:27 +02:00
|
|
|
if isMac:
|
|
|
|
QFont.insertSubstitution(".Lucida Grande UI", "Lucida Grande")
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# create the app
|
|
|
|
app = AnkiApp(sys.argv)
|
|
|
|
QCoreApplication.setApplicationName("Anki")
|
|
|
|
if app.secondInstance():
|
|
|
|
# we've signaled the primary instance, so we should close
|
|
|
|
return
|
|
|
|
|
2013-04-15 06:46:07 +02:00
|
|
|
# disable icons on mac; this must be done before window created
|
|
|
|
if isMac:
|
|
|
|
app.setAttribute(Qt.AA_DontShowIconsInMenus)
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# we must have a usable temp dir
|
|
|
|
try:
|
|
|
|
tempfile.gettempdir()
|
|
|
|
except:
|
|
|
|
QMessageBox.critical(
|
|
|
|
None, "Error", """\
|
|
|
|
No usable temporary folder found. Make sure C:\\temp exists or TEMP in your \
|
|
|
|
environment points to a valid, writable folder.""")
|
|
|
|
return
|
|
|
|
|
|
|
|
# qt version must be up to date
|
|
|
|
if qtmajor <= 4 and qtminor <= 6:
|
2013-01-29 01:02:58 +01:00
|
|
|
QMessageBox.warning(
|
|
|
|
None, "Error", "Your Qt version is known to be buggy. Until you "
|
|
|
|
"upgrade to a newer Qt, you may experience issues such as images "
|
|
|
|
"failing to show up during review.")
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# profile manager
|
|
|
|
from aqt.profiles import ProfileManager
|
|
|
|
pm = ProfileManager(opts.base, opts.profile)
|
|
|
|
|
|
|
|
# i18n
|
|
|
|
setupLang(pm, app, opts.lang)
|
|
|
|
|
|
|
|
# remaining pm init
|
|
|
|
pm.ensureProfile()
|
|
|
|
|
|
|
|
# load the main window
|
|
|
|
import aqt.main
|
|
|
|
mw = aqt.main.AnkiQt(app, pm, args)
|
2014-07-13 19:42:18 +02:00
|
|
|
app.exec_()
|