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 "xsltimporter.h" 0026 #include "xslthandler.h" 0027 #include "tellicoimporter.h" 0028 #include "../core/filehandler.h" 0029 #include "../collection.h" 0030 #include "../tellico_debug.h" 0031 0032 #include <KLocalizedString> 0033 #include <KUrlRequester> 0034 0035 #include <QLabel> 0036 #include <QGroupBox> 0037 #include <QTextStream> 0038 #include <QHBoxLayout> 0039 #include <QScopedPointer> 0040 0041 using Tellico::Import::XSLTImporter; 0042 0043 namespace { 0044 0045 static bool isUTF8(const QUrl& url_) { 0046 // read first line to check encoding 0047 const QScopedPointer<Tellico::FileHandler::FileRef> ref(Tellico::FileHandler::fileRef(url_)); 0048 if(!ref->isValid()) { 0049 return false; 0050 } 0051 0052 ref->open(); 0053 QTextStream stream(ref->file()); 0054 QString line = stream.readLine().toLower(); 0055 return line.indexOf(QLatin1String("utf-8")) > 0; 0056 } 0057 0058 } 0059 0060 // always use utf8 for xslt 0061 XSLTImporter::XSLTImporter(const QUrl& url_) : Tellico::Import::TextImporter(url_, isUTF8(url_)), 0062 m_widget(nullptr), 0063 m_URLRequester(nullptr) { 0064 } 0065 0066 XSLTImporter::XSLTImporter(const QString& text_) : Tellico::Import::TextImporter(text_), 0067 m_widget(nullptr), 0068 m_URLRequester(nullptr) { 0069 } 0070 0071 Tellico::Data::CollPtr XSLTImporter::collection() { 0072 if(m_coll) { 0073 return m_coll; 0074 } 0075 0076 if(m_xsltURL.isEmpty()) { 0077 // if there's also no widget, then something went wrong 0078 if(!m_widget) { 0079 setStatusMessage(i18n("A valid XSLT file is needed to import the file.")); 0080 return Data::CollPtr(); 0081 } 0082 m_xsltURL = m_URLRequester->url(); 0083 } 0084 if(m_xsltURL.isEmpty() || !m_xsltURL.isValid()) { 0085 setStatusMessage(i18n("A valid XSLT file is needed to import the file.")); 0086 return Data::CollPtr(); 0087 } 0088 0089 XSLTHandler handler(m_xsltURL); 0090 if(!handler.isValid()) { 0091 myDebug() << "invalid xslt handler"; 0092 setStatusMessage(i18n("Tellico encountered an error in XSLT processing.")); 0093 return Data::CollPtr(); 0094 } 0095 beginXSLTHandler(&handler); 0096 // myDebug() << text(); 0097 const QString str = handler.applyStylesheet(text()); 0098 // myDebug() << str; 0099 0100 Import::TellicoImporter imp(str); 0101 imp.setBaseUrl(url()); // ignore case of multiple urls for now 0102 imp.setOptions(options()); 0103 m_coll = imp.collection(); 0104 setStatusMessage(imp.statusMessage()); 0105 return m_coll; 0106 } 0107 0108 QWidget* XSLTImporter::widget(QWidget* parent_) { 0109 // if the url has already been set, then there's no widget 0110 if(!m_xsltURL.isEmpty()) { 0111 return nullptr; 0112 } 0113 0114 m_widget = new QWidget(parent_); 0115 QVBoxLayout* l = new QVBoxLayout(m_widget); 0116 0117 QGroupBox* gbox = new QGroupBox(i18n("XSLT Options"), m_widget); 0118 QHBoxLayout* hlay = new QHBoxLayout(gbox); 0119 0120 QLabel* label = new QLabel(i18n("XSLT file:"), gbox); 0121 m_URLRequester = new KUrlRequester(gbox); 0122 m_URLRequester->setWhatsThis(i18n("Choose the XSLT file used to transform the data.")); 0123 label->setBuddy(m_URLRequester); 0124 0125 // these are in the old KDE4 filter format, not the Qt5 format 0126 QString filter = QLatin1String("*.xsl|") + i18n("XSL Files") 0127 + QLatin1Char('\n') 0128 + QLatin1String("*|") + i18n("All Files"); 0129 m_URLRequester->setFilter(filter); 0130 0131 hlay->addWidget(label); 0132 hlay->addWidget(m_URLRequester); 0133 0134 l->addWidget(gbox); 0135 l->addStretch(1); 0136 return m_widget; 0137 } 0138 0139 void XSLTImporter::slotCancel() { 0140 myDebug() << "unimplemented"; 0141 }