File indexing completed on 2024-05-12 05:10:12
0001 /*************************************************************************** 0002 Copyright (C) 2005-2009 Robby Stephenson <robby@periapsis.org> 0003 ***************************************************************************/ 0004 0005 /*************************************************************************** 0006 * * 0007 * This program is free software; you can redistribute it and/or * 0008 * modify it under the terms of the GNU General Public License as * 0009 * published by the Free Software Foundation; either version 2 of * 0010 * the License or (at your option) version 3 or any later version * 0011 * accepted by the membership of KDE e.V. (or its successor approved * 0012 * by the membership of KDE e.V.), which shall act as a proxy * 0013 * defined in Section 14 of version 3 of the license. * 0014 * * 0015 * This program is distributed in the hope that it will be useful, * 0016 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0018 * GNU General Public License for more details. * 0019 * * 0020 * You should have received a copy of the GNU General Public License * 0021 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 0022 * * 0023 ***************************************************************************/ 0024 0025 #include <config.h> 0026 0027 #include "onixexporter.h" 0028 #include "xslthandler.h" 0029 #include "tellicoxmlexporter.h" 0030 #include "../collection.h" 0031 #include "../core/filehandler.h" 0032 #include "../images/imagefactory.h" 0033 #include "../images/image.h" 0034 #include "../utils/cursorsaver.h" 0035 #include "../utils/datafileregistry.h" 0036 #include "../tellico_debug.h" 0037 0038 #include <KZip> 0039 #include <KConfigGroup> 0040 #include <KLocalizedString> 0041 0042 #include <QDomDocument> 0043 #include <QFile> 0044 #include <QDateTime> 0045 #include <QBuffer> 0046 #include <QCheckBox> 0047 #include <QGroupBox> 0048 #include <QTextStream> 0049 #include <QVBoxLayout> 0050 0051 using Tellico::Export::ONIXExporter; 0052 0053 ONIXExporter::ONIXExporter(Tellico::Data::CollPtr coll_) : Tellico::Export::Exporter(coll_), 0054 m_handler(nullptr), 0055 m_xsltFile(QStringLiteral("tellico2onix.xsl")), 0056 m_includeImages(true), 0057 m_widget(nullptr), 0058 m_checkIncludeImages(nullptr) { 0059 } 0060 0061 ONIXExporter::~ONIXExporter() { 0062 delete m_handler; 0063 m_handler = nullptr; 0064 } 0065 0066 QString ONIXExporter::formatString() const { 0067 return QStringLiteral("ONIX"); 0068 } 0069 0070 QString ONIXExporter::fileFilter() const { 0071 return i18n("Zip Files") + QLatin1String(" (*.zip)") + QLatin1String(";;") + i18n("All Files") + QLatin1String(" (*)"); 0072 } 0073 0074 bool ONIXExporter::exec() { 0075 Data::CollPtr coll = collection(); 0076 if(!coll) { 0077 return false; 0078 } 0079 0080 QByteArray xml = text().toUtf8(); // encoded in utf-8 0081 0082 QByteArray data; 0083 QBuffer buf(&data); 0084 0085 KZip zip(&buf); 0086 zip.open(QIODevice::WriteOnly); 0087 zip.writeFile(QStringLiteral("onix.xml"), xml); 0088 0089 // use a dict for fast random access to keep track of which images were written to the file 0090 if(m_includeImages) { // for now, we're ignoring (options() & Export::ExportImages) 0091 const QString cover = QStringLiteral("cover"); 0092 StringSet imageSet; 0093 foreach(Data::EntryPtr entry, entries()) { 0094 const Data::Image& img = ImageFactory::imageById(entry->field(cover)); 0095 if(!img.isNull() && !imageSet.has(img.id()) 0096 && (img.format() == "JPEG" || img.format() == "JPG" || img.format() == "GIF")) { /// onix only understands jpeg and gif 0097 QByteArray ba = img.byteArray(); 0098 zip.writeFile(QLatin1String("images/") + entry->field(cover), ba); 0099 imageSet.add(img.id()); 0100 } 0101 } 0102 } 0103 0104 zip.close(); 0105 return FileHandler::writeDataURL(url(), data, options() & Export::ExportForce); 0106 // return FileHandler::writeTextURL(url(), text(), options() & Export::ExportUTF8, options() & Export::ExportForce); 0107 } 0108 0109 QString ONIXExporter::text() { 0110 QString xsltFile = DataFileRegistry::self()->locate(m_xsltFile); 0111 if(xsltFile.isNull()) { 0112 myDebug() << "no xslt file for " << m_xsltFile; 0113 return QString(); 0114 } 0115 0116 Data::CollPtr coll = collection(); 0117 if(!coll) { 0118 myDebug() << "no collection pointer!"; 0119 return QString(); 0120 } 0121 0122 // notes about utf-8 encoding: 0123 // all params should be passed to XSLTHandler in utf8 0124 // input string to XSLTHandler should be in utf-8, EVEN IF DOM STRING SAYS OTHERWISE 0125 0126 QUrl u = QUrl::fromLocalFile(xsltFile); 0127 // do NOT do namespace processing, it messes up the XSL declaration since 0128 // QDom thinks there are no elements in the Tellico namespace and as a result 0129 // removes the namespace declaration 0130 QDomDocument dom = FileHandler::readXMLDocument(u, false); 0131 if(dom.isNull()) { 0132 myDebug() << "error loading xslt file: " << xsltFile; 0133 return QString(); 0134 } 0135 0136 // the stylesheet prints utf-8 by default, if using locale encoding, need 0137 // to change the encoding attribute on the xsl:output element 0138 if(!(options() & Export::ExportUTF8)) { 0139 XSLTHandler::setLocaleEncoding(dom); 0140 } 0141 0142 delete m_handler; 0143 m_handler = new XSLTHandler(dom, QFile::encodeName(xsltFile)); 0144 0145 QDateTime now = QDateTime::currentDateTime(); 0146 m_handler->addStringParam("sentDate", now.toString(QStringLiteral("yyyyMMddhhmm")).toUtf8()); 0147 0148 m_handler->addStringParam("version", TELLICO_VERSION); 0149 0150 GUI::CursorSaver cs(Qt::WaitCursor); 0151 0152 // now grab the XML 0153 TellicoXMLExporter exporter(coll); 0154 exporter.setEntries(entries()); 0155 exporter.setFields(fields()); 0156 exporter.setIncludeImages(false); // do not include images in XML 0157 // yes, this should be in utf8, always 0158 exporter.setOptions(options() | Export::ExportUTF8); 0159 QDomDocument output = exporter.exportXML(); 0160 #if 0 0161 QFile f(QLatin1String("/tmp/test.xml")); 0162 if(f.open(QIODevice::WriteOnly)) { 0163 QTextStream t(&f); 0164 t << output.toString(); 0165 } 0166 f.close(); 0167 #endif 0168 return m_handler->applyStylesheet(output.toString()); 0169 } 0170 0171 QWidget* ONIXExporter::widget(QWidget* parent_) { 0172 if(m_widget) { 0173 return m_widget; 0174 } 0175 0176 m_widget = new QWidget(parent_); 0177 QVBoxLayout* l = new QVBoxLayout(m_widget); 0178 0179 QGroupBox* gbox = new QGroupBox(i18n("ONIX Archive Options"), m_widget); 0180 QVBoxLayout* vlay = new QVBoxLayout(gbox); 0181 0182 m_checkIncludeImages = new QCheckBox(i18n("Include images in archive"), gbox); 0183 m_checkIncludeImages->setChecked(m_includeImages); 0184 m_checkIncludeImages->setWhatsThis(i18n("If checked, the images in the document will be included " 0185 "in the zipped ONIX archive.")); 0186 0187 vlay->addWidget(m_checkIncludeImages); 0188 0189 l->addWidget(gbox); 0190 l->addStretch(1); 0191 return m_widget; 0192 } 0193 0194 void ONIXExporter::readOptions(KSharedConfigPtr config_) { 0195 KConfigGroup group(config_, QStringLiteral("ExportOptions - %1").arg(formatString())); 0196 m_includeImages = group.readEntry("Include Images", m_includeImages); 0197 } 0198 0199 void ONIXExporter::saveOptions(KSharedConfigPtr config_) { 0200 m_includeImages = m_checkIncludeImages->isChecked(); 0201 0202 KConfigGroup group(config_, QStringLiteral("ExportOptions - %1").arg(formatString())); 0203 group.writeEntry("Include Images", m_includeImages); 0204 }