configurable document dir

This commit is contained in:
Damien Elmes 2011-04-16 09:36:22 +09:00
parent ed3774ef7d
commit 7de4552b0e
5 changed files with 50 additions and 15 deletions

View file

@ -222,7 +222,7 @@ Error was:<pre>%s</pre>""")
tit = tit[0:40] tit = tit[0:40]
if self.type == 0: if self.type == 0:
# deck # deck
dd = self.parent.documentDir dd = self.parent.config['documentDir']
p = os.path.join(dd, tit + ".anki") p = os.path.join(dd, tit + ".anki")
if os.path.exists(p): if os.path.exists(p):
tit += "%d" % time.time() tit += "%d" % time.time()

View file

@ -24,6 +24,8 @@ from aqt.utils import saveGeom, restoreGeom, showInfo, showWarning, \
config = aqt.config config = aqt.config
## fixme: open plugin folder broken on win32?
class AnkiQt(QMainWindow): class AnkiQt(QMainWindow):
def __init__(self, app, config, args, splash): def __init__(self, app, config, args, splash):
QMainWindow.__init__(self) QMainWindow.__init__(self)
@ -419,7 +421,7 @@ Debug info:\n%s""") % traceback.format_exc(), help="DeckErrors")
if not name.endswith(".anki"): if not name.endswith(".anki"):
name += ".anki" name += ".anki"
break break
path = os.path.join(self.documentDir, name) path = os.path.join(self.config['documentDir'], name)
if os.path.exists(path): if os.path.exists(path):
if askUser(_("That deck already exists. Overwrite?"), if askUser(_("That deck already exists. Overwrite?"),
defaultno=True): defaultno=True):
@ -515,7 +517,7 @@ Debug info:\n%s""") % traceback.format_exc(), help="DeckErrors")
if self.deck.path: if self.deck.path:
dir = os.path.dirname(self.deck.path) dir = os.path.dirname(self.deck.path)
else: else:
dir = self.documentDir dir = self.config['documentDir']
file = QFileDialog.getSaveFileName(self, title, file = QFileDialog.getSaveFileName(self, title,
dir, dir,
_("Deck files (*.anki)"), _("Deck files (*.anki)"),
@ -1191,23 +1193,25 @@ It can take a long time. Proceed?""")):
def setupDocumentDir(self): def setupDocumentDir(self):
if self.config['documentDir']: if self.config['documentDir']:
self.documentDir = self.config['documentDir'] return
elif sys.platform.startswith("win32"): if sys.platform.startswith("win32"):
s = QSettings(QSettings.UserScope, "Microsoft", "Windows") s = QSettings(QSettings.UserScope, "Microsoft", "Windows")
s.beginGroup("CurrentVersion/Explorer/Shell Folders") s.beginGroup("CurrentVersion/Explorer/Shell Folders")
self.documentDir = unicode(s.value("Personal").toString()) d = unicode(s.value("Personal").toString())
if os.path.exists(self.documentDir): if os.path.exists(d):
self.documentDir = os.path.join(self.documentDir, "Anki") d = os.path.join(d, "Anki")
else: else:
self.documentDir = os.path.expanduser("~/.anki/decks") d = os.path.expanduser("~/.anki/decks")
elif sys.platform.startswith("darwin"): elif sys.platform.startswith("darwin"):
self.documentDir = os.path.expanduser("~/Documents/Anki") d = os.path.expanduser("~/Documents/Anki")
else: else:
self.documentDir = os.path.expanduser("~/.anki/decks") d = os.path.expanduser("~/.anki/decks")
try: try:
os.mkdir(self.documentDir) os.mkdir(d)
except (OSError, IOError): except (OSError, IOError):
# already exists
pass pass
self.config['documentDir'] = d
# Proxy support # Proxy support
########################################################################## ##########################################################################

View file

@ -6,7 +6,7 @@ import os
from PyQt4.QtGui import * from PyQt4.QtGui import *
from PyQt4.QtCore import * from PyQt4.QtCore import *
from anki.lang import langs from anki.lang import langs
from aqt.utils import openFolder from aqt.utils import openFolder, showWarning
import aqt import aqt
class Preferences(QDialog): class Preferences(QDialog):
@ -147,6 +147,9 @@ class Preferences(QDialog):
self.form.deleteMedia.setChecked(self.config['deleteMedia']) self.form.deleteMedia.setChecked(self.config['deleteMedia'])
self.form.stripHTML.setChecked(self.config['stripHTML']) self.form.stripHTML.setChecked(self.config['stripHTML'])
self.form.autoplaySounds.setChecked(self.config['autoplaySounds']) self.form.autoplaySounds.setChecked(self.config['autoplaySounds'])
self.connect(self.form.documentFolder,
SIGNAL("clicked()"),
self.onChangeFolder)
def updateOptions(self): def updateOptions(self):
self.config['suppressEstimates'] = not self.form.showEstimates.isChecked() self.config['suppressEstimates'] = not self.form.showEstimates.isChecked()
@ -165,3 +168,20 @@ class Preferences(QDialog):
n += 1 n += 1
# default to english # default to english
return self.codeToIndex("en") return self.codeToIndex("en")
def onChangeFolder(self):
d = QFileDialog(self)
d.setWindowModality(Qt.WindowModal)
d.setFileMode(QFileDialog.Directory)
d.setOption(QFileDialog.ShowDirsOnly, True)
d.setDirectory(self.config['documentDir'])
if d.exec_():
dir = unicode(list(d.selectedFiles())[0])
# make sure we can write into it
try:
f = os.path.join(dir, "test.txt")
open(f, "w").write("test")
os.unlink(f)
except (OSError, IOError):
return
self.config['documentDir'] = dir

View file

@ -160,7 +160,7 @@ Are you sure?""" % deckName),
elif self.loadAfterSync and self.deckPath: elif self.loadAfterSync and self.deckPath:
if self.loadAfterSync == 2: if self.loadAfterSync == 2:
name = re.sub("[<>]", "", self.syncName) name = re.sub("[<>]", "", self.syncName)
p = os.path.join(self.documentDir, name + ".anki") p = os.path.join(self.config['documentDir'], name + ".anki")
shutil.copy2(self.deckPath, p) shutil.copy2(self.deckPath, p)
self.deckPath = p self.deckPath = p
# since we've moved the deck, we have to set sync path # since we've moved the deck, we have to set sync path
@ -196,7 +196,7 @@ Are you sure?""" % deckName),
self.syncName = name self.syncName = name
if name: if name:
# name chosen # name chosen
p = os.path.join(self.documentDir, name + ".anki") p = os.path.join(self.config['documentDir'], name + ".anki")
if os.path.exists(p): if os.path.exists(p):
d = askUserDialog(_("""\ d = askUserDialog(_("""\
This deck already exists on your computer. Overwrite the local copy?"""), This deck already exists on your computer. Overwrite the local copy?"""),

View file

@ -453,6 +453,16 @@
</item> </item>
</layout> </layout>
</item> </item>
<item>
<widget class="QPushButton" name="documentFolder">
<property name="text">
<string>Change document folder...</string>
</property>
<property name="autoDefault">
<bool>false</bool>
</property>
</widget>
</item>
<item> <item>
<spacer> <spacer>
<property name="orientation"> <property name="orientation">
@ -513,6 +523,7 @@
<tabstop>stripHTML</tabstop> <tabstop>stripHTML</tabstop>
<tabstop>openLastDeck</tabstop> <tabstop>openLastDeck</tabstop>
<tabstop>deleteMedia</tabstop> <tabstop>deleteMedia</tabstop>
<tabstop>documentFolder</tabstop>
<tabstop>buttonBox</tabstop> <tabstop>buttonBox</tabstop>
</tabstops> </tabstops>
<resources/> <resources/>