File indexing completed on 2024-12-01 13:11:46

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2013 Inge Wallin  <inge@lysator.liu.se>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018    Boston, MA 02110-1301, USA.
0019 */
0020 
0021 
0022 // Own
0023 #include "OdfReaderContext.h"
0024 
0025 // Calligra
0026 #include <KoXmlReader.h>
0027 #include <KoOdfStyleManager.h>
0028 
0029 // Odftraverse library
0030 #include "OdfParser.h"
0031 
0032 
0033 // ----------------------------------------------------------------
0034 //                 class OdfReaderContext::Private
0035 
0036 
0037 class Q_DECL_HIDDEN OdfReaderContext::Private
0038 {
0039 public:
0040     Private(KoStore *store);
0041     ~Private();
0042 
0043     KoStore *odfStore;
0044 
0045     // This data is used for conversion while traversing the content tree.
0046     // It's created from the store that is given to us at construction time.
0047     QHash<QString, QString>    metadata;
0048     QHash<QString, QString>    manifest;
0049     KoOdfStyleManager         *styleManager;
0050 
0051     // This data changes while the parsing proceeds.
0052     bool  isInsideParagraph;  // True while we are parsing paragraph contents.
0053 
0054     // This data is created during the reading and can be used after
0055     // it is finished.
0056     QHash<QString, QSizeF>   images;
0057     QHash<QString, QString>  mediaFiles;
0058 };
0059 
0060 
0061 OdfReaderContext::Private::Private(KoStore *store)
0062     : odfStore(store)
0063     , styleManager(new KoOdfStyleManager())
0064     , isInsideParagraph(false)
0065 {
0066 }
0067 
0068 OdfReaderContext::Private::~Private()
0069 {
0070     delete styleManager;
0071 }
0072 
0073 
0074 // ----------------------------------------------------------------
0075 //                     class OdfReaderContext
0076 
0077 
0078 OdfReaderContext::OdfReaderContext(KoStore *store)
0079     : d(new OdfReaderContext::Private(store))
0080 {
0081 }
0082 
0083 OdfReaderContext::~OdfReaderContext()
0084 {
0085     delete d;
0086 }
0087 
0088 
0089 KoFilter::ConversionStatus OdfReaderContext::analyzeOdfFile()
0090 {
0091     if (!d->odfStore) {
0092         return KoFilter::FileNotFound;
0093     }
0094 
0095     // ----------------------------------------------------------------
0096     // Parse input files
0097 
0098     OdfParser odfParser;
0099     KoFilter::ConversionStatus  status;
0100 
0101     // Parse meta.xml into m_metadata
0102     status = odfParser.parseMetadata(*d->odfStore, &d->metadata);
0103     if (status != KoFilter::OK) {
0104         return status;
0105     }
0106 
0107     // Parse manifest
0108     status = odfParser.parseManifest(*d->odfStore, &d->manifest);
0109     if (status != KoFilter::OK) {
0110         return status;
0111     }
0112 
0113     // Load the styles
0114     d->styleManager->loadStyles(d->odfStore);
0115 
0116     return KoFilter::OK;
0117 }
0118 
0119 
0120 KoStore *OdfReaderContext::odfStore() const
0121 {
0122     return d->odfStore;
0123 }
0124 
0125 KoOdfStyleManager *OdfReaderContext::styleManager() const
0126 {
0127     return d->styleManager;
0128 }
0129 
0130 QHash<QString, QString> OdfReaderContext::metadata() const
0131 {
0132     return d->metadata;
0133 }
0134 
0135 QHash<QString, QString> OdfReaderContext::manifest() const
0136 {
0137     return d->manifest;
0138 }
0139 
0140 
0141 bool OdfReaderContext::isInsideParagraph() const
0142 {
0143     return d->isInsideParagraph;
0144 }
0145 
0146 void OdfReaderContext::setIsInsideParagraph(bool isInside)
0147 {
0148     d->isInsideParagraph = isInside;
0149 }
0150 
0151 
0152 QHash<QString, QSizeF> OdfReaderContext::images() const
0153 {
0154     return d->images;
0155 }
0156 
0157 #if 0  // NYI
0158 QHash<QString, QString> OdfReaderContext::mediaFiles() const
0159 {
0160     return d->mediaFiles;
0161 }
0162 #endif