File indexing completed on 2024-05-12 16:29:10

0001 /*
0002  * This file is part of Office 2007 Filters for Calligra
0003  * Copyright (C) 2002 Laurent Montel <lmontel@mandrakesoft.com>
0004  * Copyright (C) 2003 David Faure <faure@kde.org>
0005  * Copyright (C) 2002, 2003, 2004 Nicolas GOUTTE <goutte@kde.org>
0006  * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
0007  *
0008  * Contact: Suresh Chande suresh.chande@nokia.com
0009  *
0010  * This library is free software; you can redistribute it and/or
0011  * modify it under the terms of the GNU Lesser General Public License
0012  * version 2.1 as published by the Free Software Foundation.
0013  *
0014  * This library is distributed in the hope that it will be useful, but
0015  * WITHOUT ANY WARRANTY; without even the implied warranty of
0016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0017  * Lesser General Public License for more details.
0018  *
0019  * You should have received a copy of the GNU Lesser General Public
0020  * License along with this library; if not, write to the Free Software
0021  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
0022  * 02110-1301 USA
0023  *
0024  */
0025 
0026 #ifndef MSOOXMLIMPORT_H
0027 #define MSOOXMLIMPORT_H
0028 
0029 #include "komsooxml_export.h"
0030 
0031 #include <QByteArray>
0032 #include <QHash>
0033 #include <QVariant>
0034 
0035 #include <KoBorder.h>
0036 #include <KoOdfExporter.h>
0037 #include <KoXmlReader.h>
0038 
0039 #include "PredefinedShapeHelper.h"
0040 
0041 class QSize;
0042 class KZip;
0043 class QTemporaryFile;
0044 class KoStore;
0045 
0046 namespace MSOOXML
0047 {
0048 
0049 class MsooXmlReader;
0050 class MsooXmlReaderContext;
0051 class MsooXmlRelationships;
0052 
0053 //! A base class for MSOOXML-to-ODF import filters
0054 class KOMSOOXML_EXPORT MsooXmlImport : public KoOdfExporter
0055 {
0056     Q_OBJECT
0057 public:
0058     MsooXmlImport(const QString& bodyContentElement, QObject * parent);
0059 
0060     ~MsooXmlImport() override;
0061 
0062     KoStore* outputStore() const { return m_outputStore; }
0063 
0064     //! KoFilter::UsageError is returned if this method is called outside
0065     //! of the importing process, i.e. not from within parseParts().
0066     KoFilter::ConversionStatus loadAndParseDocument(MsooXmlReader *reader, const QString& path,
0067             MsooXmlReaderContext* context = 0);
0068 
0069     //! KoFilter::UsageError is returned if this method is called outside
0070     //! of the importing process, i.e. not from within parseParts().
0071     KoFilter::ConversionStatus loadAndParseDocument(MsooXmlReader *reader, const QString& path,
0072             QString& errorMessage,
0073             MsooXmlReaderContext* context = 0);
0074 
0075     //! Loads a file from a device
0076     KoFilter::ConversionStatus loadAndParseFromDevice(MsooXmlReader* reader, QIODevice* device,
0077             MsooXmlReaderContext* context);
0078 
0079     /*! Copies file @a sourceName from the input archive to the output document
0080     under @a destinationName name. @return KoFilter::OK on success.
0081     On failure @a errorMessage is set.
0082     KoFilter::UsageError is returned if this method is called outside
0083     of the importing process, i.e. not from within parseParts(). */
0084     KoFilter::ConversionStatus copyFile(const QString& sourceName,
0085                                         const QString& destinationName,
0086                                         bool oleFile);
0087 
0088     /* Creates an image to the resulting odf with the given name */
0089     KoFilter::ConversionStatus createImage(const QImage& source,
0090                                            const QString& destinationName);
0091 
0092     /*! @return image from the file for modifications */
0093     KoFilter::ConversionStatus imageFromFile(const QString& sourceName, QImage& image);
0094 
0095     /*! @return size of image file @a sourceName read from zip archive @a zip.
0096     Size of the image is returned in @a size.
0097     @return KoFilter::OK on success.
0098     On failure @a errorMessage is set. */
0099     KoFilter::ConversionStatus imageSize(const QString& sourceName, QSize& size);
0100 
0101     //! Helper class to get information about predefined ooxml shapes
0102     PredefinedShapeHelper m_shapeHelper;
0103 
0104     /*! report progress of the filter */
0105     void reportProgress(unsigned progress);
0106 
0107 protected:
0108     KoFilter::ConversionStatus createDocument(KoStore *outputStore,
0109                                                       KoOdfWriters *writers) override;
0110 
0111     void writeConfigurationSettings(KoXmlWriter* settings) const override;
0112 
0113     bool isPasswordProtectedFile(QString &filename);
0114     QTemporaryFile* tryDecryptFile(QString &filename);
0115 
0116     virtual KoFilter::ConversionStatus parseParts(KoOdfWriters *writers,
0117             MsooXmlRelationships *relationships, QString& errorMessage) = 0;
0118 
0119     //! KoFilter::UsageError is returned if this method is called outside
0120     //! of the importing process, i.e. not from within parseParts().
0121     KoFilter::ConversionStatus loadAndParseDocument(
0122         const QByteArray& contentType, MsooXmlReader *reader, KoOdfWriters *writers,
0123         QString& errorMessage, MsooXmlReaderContext* context = 0);
0124 
0125     //! Like @ref loadAndParseDocument(const QByteArray&, MsooXmlReader*, KoOdfWriters*, QString&, MsooXmlReaderContext*)
0126     //! but file name is provided directly instead of content type
0127     KoFilter::ConversionStatus loadAndParseDocumentFromFile(
0128         const QString& fileName, MsooXmlReader *reader, KoOdfWriters *writers,
0129         QString& errorMessage, MsooXmlReaderContext* context);
0130 
0131     //! Like @ref loadAndParseDocument(const QByteArray&, MsooXmlReader*, KoOdfWriters*, QString&, MsooXmlReaderContext*)
0132     //! but return KoFilter::OK if the document for the content type is not found.
0133     KoFilter::ConversionStatus loadAndParseDocumentIfExists(
0134         const QByteArray& contentType, MsooXmlReader *reader, KoOdfWriters *writers,
0135         QString& errorMessage, MsooXmlReaderContext* context = 0);
0136 
0137     //! Like @ref loadAndParseDocumentIfExists(const QByteArray&, MsooXmlReader*, KoOdfWriters*, QString&, MsooXmlReaderContext*)
0138     //! but file name is provided directly instead of content type
0139     KoFilter::ConversionStatus loadAndParseDocumentFromFileIfExists(
0140         const QString& fileName, MsooXmlReader *reader, KoOdfWriters *writers,
0141         QString& errorMessage, MsooXmlReaderContext* context = 0);
0142 
0143     //! @return all part names.
0144     QMultiHash<QByteArray, QByteArray> partNames() const { return m_contentTypes; }
0145     //! @return part names associated with @a contentType
0146     QList<QByteArray> partNames(const QByteArray& contentType) const { return m_contentTypes.values(contentType); }
0147 
0148     QMap<QString, QVariant> documentProperties() const { return m_documentProperties; }
0149     QVariant documentProperty(const QString& propertyName) const { return m_documentProperties.value(propertyName); }
0150 
0151 protected:
0152     KoFilter::ConversionStatus loadAndParse(const QString& filename,
0153                                             KoXmlDocument& doc, QString& errorMessage);
0154 
0155 private:
0156     //! Opens file for converting and performs conversions.
0157     //! @return status of conversion.
0158     KoFilter::ConversionStatus openFile(KoOdfWriters *writers, QString& errorMessage);
0159 
0160     KoFilter::ConversionStatus loadAndParseDocumentInternal(
0161         const QByteArray& contentType, MsooXmlReader *reader, KoOdfWriters *writers,
0162         QString& errorMessage, MsooXmlReaderContext* context, bool *pathFound);
0163 
0164     KoFilter::ConversionStatus loadAndParseDocumentFromFileInternal(
0165         const QString& fileName, MsooXmlReader *reader, KoOdfWriters *writers,
0166         QString& errorMessage, MsooXmlReaderContext* context, bool *pathFound);
0167 
0168     KZip* m_zip; //!< Input zip file
0169 
0170     KoStore* m_outputStore; //!< output store used for copying files
0171 
0172     //! XML from "[Content_Types].xml" file.
0173     KoXmlDocument m_contentTypesXML;
0174     //! Content types from m_contentTypesXML: ContentType -> PartName mapping
0175     QMultiHash<QByteArray, QByteArray> m_contentTypes;
0176 
0177     //! Content types from m_appXML: tagName -> content mapping
0178     QMap<QString, QVariant> m_documentProperties;
0179 
0180     //! XML with document contents, typically /word/document.xml
0181     KoXmlDocument m_documentXML;
0182 
0183     QMap<QString, QSize> m_imageSizes; //!< collects image sizes to avoid multiple checks
0184 };
0185 
0186 } // namespace MSOOXML
0187 
0188 #endif