File indexing completed on 2024-05-12 05:10:15

0001 /***************************************************************************
0002     Copyright (C) 2003-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 "xsltexporter.h"
0026 #include "xslthandler.h"
0027 #include "tellicoxmlexporter.h"
0028 #include "../collection.h"
0029 #include "../core/filehandler.h"
0030 
0031 #include <KLocalizedString>
0032 #include <KUrlRequester>
0033 #include <KUser>
0034 #include <KConfigGroup>
0035 
0036 #include <QLabel>
0037 #include <QGroupBox>
0038 #include <QDomDocument>
0039 #include <QHBoxLayout>
0040 
0041 using namespace Tellico;
0042 using Tellico::Export::XSLTExporter;
0043 
0044 XSLTExporter::XSLTExporter(Data::CollPtr coll_) : Export::Exporter(coll_),
0045     m_widget(nullptr),
0046     m_URLRequester(nullptr) {
0047 }
0048 
0049 QString XSLTExporter::formatString() const {
0050   return QStringLiteral("XSLT");
0051 }
0052 
0053 QString XSLTExporter::fileFilter() const {
0054   return i18n("All Files") + QLatin1String(" (*)");
0055 }
0056 
0057 bool XSLTExporter::exec() {
0058   QUrl u = m_URLRequester->url();
0059   if(u.isEmpty() || !u.isValid()) {
0060     return false;
0061   }
0062   //  XSLTHandler handler(FileHandler::readXMLFile(url));
0063   XSLTHandler handler(u);
0064   handler.addStringParam("date", QDate::currentDate().toString(Qt::ISODate).toLatin1());
0065   handler.addStringParam("time", QTime::currentTime().toString(Qt::ISODate).toLatin1());
0066   handler.addStringParam("user", KUser(KUser::UseRealUserID).loginName().toLatin1());
0067 
0068   TellicoXMLExporter exporter(collection());
0069   exporter.setEntries(entries());
0070   exporter.setFields(fields());
0071   exporter.setOptions(options());
0072   QDomDocument dom = exporter.exportXML();
0073   return FileHandler::writeTextURL(url(), handler.applyStylesheet(dom.toString()),
0074                                    options() & ExportUTF8, options() & Export::ExportForce);
0075 }
0076 
0077 QWidget* XSLTExporter::widget(QWidget* parent_) {
0078   if(m_widget) {
0079     return m_widget;
0080   }
0081 
0082   m_widget = new QWidget(parent_);
0083   QVBoxLayout* l = new QVBoxLayout(m_widget);
0084 
0085   QGroupBox* gbox = new QGroupBox(i18n("XSLT Options"), m_widget);
0086   QHBoxLayout* hlay = new QHBoxLayout(gbox);
0087 
0088   QLabel* label = new QLabel(i18n("XSLT file:"), gbox);
0089   m_URLRequester = new KUrlRequester(gbox);
0090   m_URLRequester->setWhatsThis(i18n("Choose the XSLT file used to transform the data."));
0091   label->setBuddy(m_URLRequester);
0092 
0093   hlay->addWidget(label);
0094   hlay->addWidget(m_URLRequester);
0095 
0096   l->addWidget(gbox);
0097 
0098   // these are in the old KDE4 filter format, not the Qt5 format
0099   QString filter = QLatin1String("*.xsl|") + i18n("XSL Files")
0100                  + QLatin1Char('\n')
0101                  + QLatin1String("*|") + i18n("All Files");
0102   m_URLRequester->setFilter(filter);
0103   m_URLRequester->setMode(KFile::File | KFile::ExistingOnly);
0104   if(!m_xsltFile.isEmpty()) {
0105     m_URLRequester->setUrl(m_xsltFile);
0106   }
0107 
0108   l->addStretch(1);
0109   return m_widget;
0110 }
0111 
0112 void XSLTExporter::readOptions(KSharedConfigPtr config_) {
0113   KConfigGroup group(config_, QStringLiteral("ExportOptions - %1").arg(formatString()));
0114   m_xsltFile = group.readEntry("Last File", QUrl());
0115 }
0116 
0117 void XSLTExporter::saveOptions(KSharedConfigPtr config_) {
0118   KConfigGroup group(config_, QStringLiteral("ExportOptions - %1").arg(formatString()));
0119   m_xsltFile = m_URLRequester->url();
0120   // TODO
0121   group.writeEntry("Last File", QUrl(m_xsltFile.url()));
0122 }