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]
if self.type == 0:
# deck
dd = self.parent.documentDir
dd = self.parent.config['documentDir']
p = os.path.join(dd, tit + ".anki")
if os.path.exists(p):
tit += "%d" % time.time()

View file

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

View file

@ -6,7 +6,7 @@ import os
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from anki.lang import langs
from aqt.utils import openFolder
from aqt.utils import openFolder, showWarning
import aqt
class Preferences(QDialog):
@ -147,6 +147,9 @@ class Preferences(QDialog):
self.form.deleteMedia.setChecked(self.config['deleteMedia'])
self.form.stripHTML.setChecked(self.config['stripHTML'])
self.form.autoplaySounds.setChecked(self.config['autoplaySounds'])
self.connect(self.form.documentFolder,
SIGNAL("clicked()"),
self.onChangeFolder)
def updateOptions(self):
self.config['suppressEstimates'] = not self.form.showEstimates.isChecked()
@ -165,3 +168,20 @@ class Preferences(QDialog):
n += 1
# default to english
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:
if self.loadAfterSync == 2:
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)
self.deckPath = p
# since we've moved the deck, we have to set sync path
@ -196,7 +196,7 @@ Are you sure?""" % deckName),
self.syncName = name
if name:
# name chosen
p = os.path.join(self.documentDir, name + ".anki")
p = os.path.join(self.config['documentDir'], name + ".anki")
if os.path.exists(p):
d = askUserDialog(_("""\
This deck already exists on your computer. Overwrite the local copy?"""),

View file

@ -453,6 +453,16 @@
</item>
</layout>
</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>
<spacer>
<property name="orientation">
@ -513,6 +523,7 @@
<tabstop>stripHTML</tabstop>
<tabstop>openLastDeck</tabstop>
<tabstop>deleteMedia</tabstop>
<tabstop>documentFolder</tabstop>
<tabstop>buttonBox</tabstop>
</tabstops>
<resources/>