decide on add/replace via filename instead

This means users will be able to rename the file if they want to force either
adding or replacing.
This commit is contained in:
Damien Elmes 2012-10-24 22:59:49 +09:00
parent 418acb3365
commit 3acea038a2
2 changed files with 4 additions and 28 deletions

View file

@ -315,33 +315,10 @@ Unable to import from a read-only file."""))
mw.reset() mw.reset()
def setupApkgImport(mw, importer): def setupApkgImport(mw, importer):
meta = None base = os.path.basename(importer.file).lower()
forceFull = False full = (base == "collection.apkg") or re.match("backup-\d+.apkg", base)
try: if not full:
z = zipfile.ZipFile(importer.file) return True
meta = json.load(z.open("meta"))
if not meta['full']:
# add
return True
elif meta['full'] == "force":
forceFull = True
except:
# no meta attribute on broken file
pass
# if old file or full collection, we have to prompt user
if not forceFull:
diag = askUserDialog(_("""\
Would you like to add to your collection, or replace it?"""),
[_("Add"),
_("Replace"),
_("Cancel")])
diag.setIcon(QMessageBox.Question)
diag.setDefault(0)
ret = diag.run()
if ret == _("Add"):
return True
elif ret == _("Cancel"):
return False
if not askUser(_("""\ if not askUser(_("""\
This will delete your existing collection and replace it with the data in \ This will delete your existing collection and replace it with the data in \
the file you're importing. Are you sure?"""), msgfunc=QMessageBox.warning): the file you're importing. Are you sure?"""), msgfunc=QMessageBox.warning):

View file

@ -296,7 +296,6 @@ how to restore from a backup.""")
z = zipfile.ZipFile(newpath, "w", zipfile.ZIP_DEFLATED) z = zipfile.ZipFile(newpath, "w", zipfile.ZIP_DEFLATED)
z.write(path, "collection.anki2") z.write(path, "collection.anki2")
z.writestr("media", "{}") z.writestr("media", "{}")
z.writestr("meta", json.dumps(dict(full="force")))
z.close() z.close()
# remove if over # remove if over
if len(backups) + 1 > nbacks: if len(backups) + 1 > nbacks: