File indexing completed on 2022-09-20 12:32:07

0001 /*
0002     SPDX-FileCopyrightText: 2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "exportdialog.h"
0007 
0008 #include <KEduVocDocument>
0009 #include <KEduVocKVTML2Writer>
0010 #include <parleydocument.h>
0011 
0012 #include <KLocalizedString>
0013 #include <KMessageBox>
0014 #include <QDebug>
0015 #include <QFileDialog>
0016 #include <QStandardPaths>
0017 #include <QUrl>
0018 
0019 #include <QDialogButtonBox>
0020 
0021 #include <libxml/HTMLtree.h>
0022 #include <libxml/catalog.h>
0023 #include <libxml/debugXML.h>
0024 #include <libxml/xinclude.h>
0025 #include <libxml/xmlIO.h>
0026 #include <libxml/xmlmemory.h>
0027 #include <libxslt/transform.h>
0028 #include <libxslt/xslt.h>
0029 #include <libxslt/xsltInternals.h>
0030 #include <libxslt/xsltutils.h>
0031 #include <string.h>
0032 
0033 ExportDialog::ExportDialog(ParleyDocument *doc, QWidget *parent)
0034     : QDialog(parent)
0035     , m_doc{doc}
0036 {
0037     ui = new Ui::ExportOptions();
0038     QWidget *widget = new QWidget();
0039     ui->setupUi(widget);
0040 
0041     QDialogButtonBox *button_dialog = new QDialogButtonBox;
0042     button_dialog->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0043 
0044     QVBoxLayout *layout = new QVBoxLayout;
0045     layout->addWidget(widget);
0046     layout->addWidget(button_dialog);
0047 
0048     setLayout(layout);
0049 
0050     connect(button_dialog, &QDialogButtonBox::accepted, this, &ExportDialog::accept);
0051     connect(button_dialog, &QDialogButtonBox::rejected, this, &ExportDialog::reject);
0052 
0053     setWindowTitle(i18nc("@title:window", "Export"));
0054 }
0055 
0056 void ExportDialog::accept()
0057 {
0058     QDialog::accept();
0059 
0060     if (ui->csvRadio->isChecked()) {
0061         /// Find the CSV filter in the standard filter list
0062         //!@todo: good and clean solution
0063         QStringList defaultFilters = KEduVocDocument::pattern(KEduVocDocument::Writing).split(QStringLiteral(";;"));
0064         QString filter = defaultFilters.filter(QStringLiteral("csv")).join(QStringLiteral(";;"));
0065         QUrl filename = getFileName(filter);
0066         if (filename != QUrl()) {
0067             m_doc->saveAs(filename);
0068         }
0069         return;
0070     }
0071 
0072     QString xslFile;
0073     if (ui->flashCardRadio->isChecked()) {
0074         xslFile = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("parley/xslt/flashcards.xsl"));
0075     } else {
0076         xslFile = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("parley/xslt/table.xsl"));
0077     }
0078 
0079     QString filter = i18n("HTML document") + " (*.html)";
0080     QUrl filename = getFileName(filter);
0081     if (filename.isEmpty()) {
0082         return;
0083     }
0084 
0085     qDebug() << "XSLT starting";
0086 
0087     xsltStylesheetPtr cur = NULL;
0088     xmlDocPtr doc, res;
0089 
0090     xmlSubstituteEntitiesDefault(1);
0091     xmlLoadExtDtdDefaultValue = 1;
0092     cur = xsltParseStylesheetFile((const xmlChar *)xslFile.toLatin1().constData());
0093 
0094     doc = xmlParseDoc((const xmlChar *)m_doc->document()->toByteArray(m_doc->document()->generator()).constData());
0095 
0096     res = xsltApplyStylesheet(cur, doc, 0);
0097     FILE *result = fopen(QFile::encodeName(filename.toLocalFile()).constData(), "w");
0098     if (result != NULL) {
0099         xsltSaveResultToFile(result, res, cur);
0100         fclose(result);
0101     } else {
0102         KMessageBox::error(this, i18n("Could not write to file \"%1\"", filename.toLocalFile()));
0103     }
0104 
0105     xsltFreeStylesheet(cur);
0106     xmlFreeDoc(res);
0107     xmlFreeDoc(doc);
0108 
0109     xsltCleanupGlobals();
0110     xmlCleanupParser();
0111 
0112     qDebug() << "XSLT finished";
0113 }
0114 
0115 QUrl ExportDialog::getFileName(const QString &filter)
0116 {
0117     return QUrl::fromLocalFile(
0118         QFileDialog::getSaveFileName(parentWidget(),
0119                                      i18n("Export As"),
0120                                      (m_doc->document()->url().fileName() == i18n("Untitled")) ? QLatin1String("") : m_doc->document()->url().toLocalFile(),
0121                                      filter));
0122 }