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
|
|
|
|
|
|
|
|
import os
|
2013-05-19 17:34:55 +02:00
|
|
|
import re
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
from aqt.qt import *
|
2012-12-22 01:17:10 +01:00
|
|
|
import aqt
|
2013-02-20 07:12:07 +01:00
|
|
|
from aqt.utils import getSaveFile, tooltip, showWarning, askUser, \
|
|
|
|
checkInvalidFilename
|
2012-12-21 08:51:59 +01:00
|
|
|
from anki.exporting import exporters
|
2016-02-26 01:01:46 +01:00
|
|
|
from anki.hooks import addHook, remHook
|
|
|
|
from anki.lang import ngettext
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
class ExportDialog(QDialog):
|
|
|
|
|
2014-06-20 02:13:12 +02:00
|
|
|
def __init__(self, mw, did=None):
|
2012-12-21 08:51:59 +01:00
|
|
|
QDialog.__init__(self, mw, Qt.Window)
|
|
|
|
self.mw = mw
|
|
|
|
self.col = mw.col
|
|
|
|
self.frm = aqt.forms.exporting.Ui_ExportDialog()
|
|
|
|
self.frm.setupUi(self)
|
|
|
|
self.exporter = None
|
2014-06-20 02:13:12 +02:00
|
|
|
self.setup(did)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.exec_()
|
|
|
|
|
2014-06-20 02:13:12 +02:00
|
|
|
def setup(self, did):
|
2016-05-31 10:51:40 +02:00
|
|
|
self.frm.format.insertItems(0, list(zip(*exporters()))[0])
|
|
|
|
self.frm.format.activated.connect(self.exporterChanged)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.exporterChanged(0)
|
|
|
|
self.decks = [_("All Decks")] + sorted(self.col.decks.allNames())
|
|
|
|
self.frm.deck.addItems(self.decks)
|
|
|
|
# save button
|
|
|
|
b = QPushButton(_("Export..."))
|
|
|
|
self.frm.buttonBox.addButton(b, QDialogButtonBox.AcceptRole)
|
2014-06-20 02:13:12 +02:00
|
|
|
# set default option if accessed through deck button
|
|
|
|
if did:
|
|
|
|
name = self.mw.col.decks.get(did)['name']
|
|
|
|
index = self.frm.deck.findText(name)
|
|
|
|
self.frm.deck.setCurrentIndex(index)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def exporterChanged(self, idx):
|
|
|
|
self.exporter = exporters()[idx][1](self.col)
|
|
|
|
self.isApkg = hasattr(self.exporter, "includeSched")
|
2014-08-04 22:15:06 +02:00
|
|
|
self.isTextNote = hasattr(self.exporter, "includeTags")
|
2012-12-21 13:32:21 +01:00
|
|
|
self.hideTags = hasattr(self.exporter, "hideTags")
|
2013-04-15 06:46:07 +02:00
|
|
|
self.frm.includeSched.setVisible(self.isApkg)
|
|
|
|
self.frm.includeMedia.setVisible(self.isApkg)
|
|
|
|
self.frm.includeTags.setVisible(
|
2012-12-21 13:32:21 +01:00
|
|
|
not self.isApkg and not self.hideTags)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def accept(self):
|
|
|
|
self.exporter.includeSched = (
|
|
|
|
self.frm.includeSched.isChecked())
|
|
|
|
self.exporter.includeMedia = (
|
|
|
|
self.frm.includeMedia.isChecked())
|
|
|
|
self.exporter.includeTags = (
|
|
|
|
self.frm.includeTags.isChecked())
|
|
|
|
if not self.frm.deck.currentIndex():
|
|
|
|
self.exporter.did = None
|
|
|
|
else:
|
|
|
|
name = self.decks[self.frm.deck.currentIndex()]
|
|
|
|
self.exporter.did = self.col.decks.id(name)
|
|
|
|
if (self.isApkg and self.exporter.includeSched and not
|
|
|
|
self.exporter.did):
|
|
|
|
verbatim = True
|
|
|
|
# it's a verbatim apkg export, so place on desktop instead of
|
2014-08-05 18:25:24 +02:00
|
|
|
# choosing file; use homedir if no desktop
|
|
|
|
usingHomedir = False
|
2012-12-21 08:51:59 +01:00
|
|
|
file = os.path.join(QDesktopServices.storageLocation(
|
|
|
|
QDesktopServices.DesktopLocation), "collection.apkg")
|
2014-08-05 18:25:24 +02:00
|
|
|
if not os.path.exists(os.path.dirname(file)):
|
|
|
|
usingHomedir = True
|
|
|
|
file = os.path.join(QDesktopServices.storageLocation(
|
|
|
|
QDesktopServices.HomeLocation), "collection.apkg")
|
2012-12-21 08:51:59 +01:00
|
|
|
if os.path.exists(file):
|
2014-08-05 18:25:24 +02:00
|
|
|
if usingHomedir:
|
|
|
|
question = _("%s already exists in your home directory. Overwrite it?")
|
|
|
|
else:
|
|
|
|
question = _("%s already exists on your desktop. Overwrite it?")
|
|
|
|
if not askUser(question % "collection.apkg"):
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
verbatim = False
|
2013-05-19 17:34:55 +02:00
|
|
|
# Get deck name and remove invalid filename characters
|
2013-05-18 18:24:53 +02:00
|
|
|
deck_name = self.decks[self.frm.deck.currentIndex()]
|
2013-05-21 04:53:02 +02:00
|
|
|
deck_name = re.sub('[\\\\/?<>:*|"^]', '_', deck_name)
|
2013-05-18 18:24:53 +02:00
|
|
|
filename = os.path.join(aqt.mw.pm.base,
|
2016-05-12 06:45:35 +02:00
|
|
|
'{0}{1}'.format(deck_name, self.exporter.ext))
|
2013-02-20 07:12:07 +01:00
|
|
|
while 1:
|
2013-05-21 04:53:02 +02:00
|
|
|
file = getSaveFile(self, _("Export"), "export",
|
|
|
|
self.exporter.key, self.exporter.ext,
|
|
|
|
fname=filename)
|
2013-02-20 07:12:07 +01:00
|
|
|
if not file:
|
|
|
|
return
|
2013-05-14 13:47:31 +02:00
|
|
|
if checkInvalidFilename(os.path.basename(file), dirsep=False):
|
2013-02-20 07:12:07 +01:00
|
|
|
continue
|
|
|
|
break
|
2012-12-21 08:51:59 +01:00
|
|
|
self.hide()
|
|
|
|
if file:
|
|
|
|
self.mw.progress.start(immediate=True)
|
|
|
|
try:
|
|
|
|
f = open(file, "wb")
|
|
|
|
f.close()
|
2016-05-12 06:45:35 +02:00
|
|
|
except (OSError, IOError) as e:
|
|
|
|
showWarning(_("Couldn't save file: %s") % str(e))
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
|
|
|
os.unlink(file)
|
2016-02-26 01:01:46 +01:00
|
|
|
exportedMedia = lambda cnt: self.mw.progress.update(
|
|
|
|
label=ngettext("Exported %d media file",
|
|
|
|
"Exported %d media files", cnt) % cnt
|
|
|
|
)
|
|
|
|
addHook("exportedMediaFiles", exportedMedia)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.exporter.exportInto(file)
|
2016-02-26 01:01:46 +01:00
|
|
|
remHook("exportedMediaFiles", exportedMedia)
|
2012-12-21 08:51:59 +01:00
|
|
|
if verbatim:
|
2014-08-05 18:25:24 +02:00
|
|
|
if usingHomedir:
|
|
|
|
msg = _("A file called %s was saved in your home directory.")
|
|
|
|
else:
|
|
|
|
msg = _("A file called %s was saved on your desktop.")
|
|
|
|
msg = msg % "collection.apkg"
|
2012-12-21 08:51:59 +01:00
|
|
|
period = 5000
|
|
|
|
else:
|
|
|
|
period = 3000
|
2014-08-04 22:15:06 +02:00
|
|
|
if self.isTextNote:
|
|
|
|
msg = ngettext("%d note exported.", "%d notes exported.",
|
|
|
|
self.exporter.count) % self.exporter.count
|
|
|
|
else:
|
|
|
|
msg = ngettext("%d card exported.", "%d cards exported.",
|
|
|
|
self.exporter.count) % self.exporter.count
|
2012-12-21 08:51:59 +01:00
|
|
|
tooltip(msg, period=period)
|
|
|
|
finally:
|
|
|
|
self.mw.progress.finish()
|
|
|
|
QDialog.accept(self)
|