diff options
author | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-03-01 19:17:32 +0000 |
---|---|---|
committer | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-03-01 19:17:32 +0000 |
commit | e38d2351b83fa65c66ccde443777647ef5cb6cff (patch) | |
tree | 1897fc20e9f73a81c520a5b9f76f8ed042124883 /src/translators/xsltimporter.cpp | |
download | tellico-e38d2351b83fa65c66ccde443777647ef5cb6cff.tar.gz tellico-e38d2351b83fa65c66ccde443777647ef5cb6cff.zip |
Added KDE3 version of Tellico
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/tellico@1097620 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'src/translators/xsltimporter.cpp')
-rw-r--r-- | src/translators/xsltimporter.cpp | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/src/translators/xsltimporter.cpp b/src/translators/xsltimporter.cpp new file mode 100644 index 0000000..67f1fd2 --- /dev/null +++ b/src/translators/xsltimporter.cpp @@ -0,0 +1,112 @@ +/*************************************************************************** + copyright : (C) 2003-2006 by Robby Stephenson + email : [email protected] + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of version 2 of the GNU General Public License as * + * published by the Free Software Foundation; * + * * + ***************************************************************************/ + +#include "xsltimporter.h" +#include "xslthandler.h" +#include "tellicoimporter.h" +#include "../filehandler.h" +#include "../collection.h" + +#include <klocale.h> +#include <kurlrequester.h> + +#include <qhbox.h> +#include <qlabel.h> +#include <qlayout.h> +#include <qgroupbox.h> + +#include <memory> + +using Tellico::Import::XSLTImporter; + +namespace { + +static bool isUTF8(const KURL& url_) { + // read first line to check encoding + std::auto_ptr<Tellico::FileHandler::FileRef> ref(Tellico::FileHandler::fileRef(url_)); + if(!ref->isValid()) { + return false; + } + + ref->open(); + QTextStream stream(ref->file()); + QString line = stream.readLine().lower(); + return line.find(QString::fromLatin1("utf-8")) > 0; +} + +} + +// always use utf8 for xslt +XSLTImporter::XSLTImporter(const KURL& url_) : Tellico::Import::TextImporter(url_, isUTF8(url_)), + m_coll(0), + m_widget(0), + m_URLRequester(0) { +} + +Tellico::Data::CollPtr XSLTImporter::collection() { + if(m_coll) { + return m_coll; + } + + if(m_xsltURL.isEmpty()) { + // if there's also no widget, then something went wrong + if(!m_widget) { + setStatusMessage(i18n("A valid XSLT file is needed to import the file.")); + return 0; + } + m_xsltURL = m_URLRequester->url(); + } + if(m_xsltURL.isEmpty() || !m_xsltURL.isValid()) { + setStatusMessage(i18n("A valid XSLT file is needed to import the file.")); + return 0; + } + + XSLTHandler handler(m_xsltURL); + if(!handler.isValid()) { + setStatusMessage(i18n("Tellico encountered an error in XSLT processing.")); + return 0; + } +// kdDebug() << text() << endl; + QString str = handler.applyStylesheet(text()); +// kdDebug() << str << endl; + + Import::TellicoImporter imp(str); + m_coll = imp.collection(); + setStatusMessage(imp.statusMessage()); + return m_coll; +} + +QWidget* XSLTImporter::widget(QWidget* parent_, const char* name_) { + // if the url has already been set, then there's no widget + if(!m_xsltURL.isEmpty()) { + return 0; + } + + m_widget = new QWidget(parent_, name_); + QVBoxLayout* l = new QVBoxLayout(m_widget); + + QGroupBox* box = new QGroupBox(1, Qt::Vertical, i18n("XSLT Options"), m_widget); + l->addWidget(box); + + (void) new QLabel(i18n("XSLT file:"), box); + m_URLRequester = new KURLRequester(box); + + QString filter = i18n("*.xsl|XSL Files (*.xsl)") + QChar('\n'); + filter += i18n("*|All Files"); + m_URLRequester->setFilter(filter); + + l->addStretch(1); + return m_widget; +} + +#include "xsltimporter.moc" |