/filters/libmsooxml/MsooXmlImport.h

https://github.com/KDE/calligra · C Header · 176 lines · 87 code · 42 blank · 47 comment · 0 complexity · 36d121a3afd75895446cfda29ae4062a MD5 · raw file

  1. /*
  2. * This file is part of Office 2007 Filters for Calligra
  3. * SPDX-FileCopyrightText: 2002 Laurent Montel <lmontel@mandrakesoft.com>
  4. * SPDX-FileCopyrightText: 2003 David Faure <faure@kde.org>
  5. * SPDX-FileCopyrightText: 2002, 2003, 2004 Nicolas GOUTTE <goutte@kde.org>
  6. * SPDX-FileCopyrightText: 2009 Nokia Corporation and /or its subsidiary(-ies).
  7. *
  8. * Contact: Suresh Chande suresh.chande@nokia.com
  9. *
  10. * SPDX-License-Identifier: LGPL-2.1-only
  11. *
  12. */
  13. #ifndef MSOOXMLIMPORT_H
  14. #define MSOOXMLIMPORT_H
  15. #include "komsooxml_export.h"
  16. #include <QByteArray>
  17. #include <QHash>
  18. #include <QVariant>
  19. #include <KoBorder.h>
  20. #include <KoOdfExporter.h>
  21. #include <KoXmlReader.h>
  22. #include "PredefinedShapeHelper.h"
  23. class QSize;
  24. class KZip;
  25. class QTemporaryFile;
  26. class KoStore;
  27. namespace MSOOXML
  28. {
  29. class MsooXmlReader;
  30. class MsooXmlReaderContext;
  31. class MsooXmlRelationships;
  32. //! A base class for MSOOXML-to-ODF import filters
  33. class KOMSOOXML_EXPORT MsooXmlImport : public KoOdfExporter
  34. {
  35. Q_OBJECT
  36. public:
  37. MsooXmlImport(const QString& bodyContentElement, QObject * parent);
  38. ~MsooXmlImport() override;
  39. KoStore* outputStore() const { return m_outputStore; }
  40. //! KoFilter::UsageError is returned if this method is called outside
  41. //! of the importing process, i.e. not from within parseParts().
  42. KoFilter::ConversionStatus loadAndParseDocument(MsooXmlReader *reader, const QString& path,
  43. MsooXmlReaderContext* context = 0);
  44. //! KoFilter::UsageError is returned if this method is called outside
  45. //! of the importing process, i.e. not from within parseParts().
  46. KoFilter::ConversionStatus loadAndParseDocument(MsooXmlReader *reader, const QString& path,
  47. QString& errorMessage,
  48. MsooXmlReaderContext* context = 0);
  49. //! Loads a file from a device
  50. KoFilter::ConversionStatus loadAndParseFromDevice(MsooXmlReader* reader, QIODevice* device,
  51. MsooXmlReaderContext* context);
  52. /*! Copies file @a sourceName from the input archive to the output document
  53. under @a destinationName name. @return KoFilter::OK on success.
  54. On failure @a errorMessage is set.
  55. KoFilter::UsageError is returned if this method is called outside
  56. of the importing process, i.e. not from within parseParts(). */
  57. KoFilter::ConversionStatus copyFile(const QString& sourceName,
  58. const QString& destinationName,
  59. bool oleFile);
  60. /* Creates an image to the resulting odf with the given name */
  61. KoFilter::ConversionStatus createImage(const QImage& source,
  62. const QString& destinationName);
  63. /*! @return image from the file for modifications */
  64. KoFilter::ConversionStatus imageFromFile(const QString& sourceName, QImage& image);
  65. /*! @return size of image file @a sourceName read from zip archive @a zip.
  66. Size of the image is returned in @a size.
  67. @return KoFilter::OK on success.
  68. On failure @a errorMessage is set. */
  69. KoFilter::ConversionStatus imageSize(const QString& sourceName, QSize& size);
  70. //! Helper class to get information about predefined ooxml shapes
  71. PredefinedShapeHelper m_shapeHelper;
  72. /*! report progress of the filter */
  73. void reportProgress(unsigned progress);
  74. protected:
  75. KoFilter::ConversionStatus createDocument(KoStore *outputStore,
  76. KoOdfWriters *writers) override;
  77. void writeConfigurationSettings(KoXmlWriter* settings) const override;
  78. bool isPasswordProtectedFile(QString &filename);
  79. QTemporaryFile* tryDecryptFile(QString &filename);
  80. virtual KoFilter::ConversionStatus parseParts(KoOdfWriters *writers,
  81. MsooXmlRelationships *relationships, QString& errorMessage) = 0;
  82. //! KoFilter::UsageError is returned if this method is called outside
  83. //! of the importing process, i.e. not from within parseParts().
  84. KoFilter::ConversionStatus loadAndParseDocument(
  85. const QByteArray& contentType, MsooXmlReader *reader, KoOdfWriters *writers,
  86. QString& errorMessage, MsooXmlReaderContext* context = 0);
  87. //! Like @ref loadAndParseDocument(const QByteArray&, MsooXmlReader*, KoOdfWriters*, QString&, MsooXmlReaderContext*)
  88. //! but file name is provided directly instead of content type
  89. KoFilter::ConversionStatus loadAndParseDocumentFromFile(
  90. const QString& fileName, MsooXmlReader *reader, KoOdfWriters *writers,
  91. QString& errorMessage, MsooXmlReaderContext* context);
  92. //! Like @ref loadAndParseDocument(const QByteArray&, MsooXmlReader*, KoOdfWriters*, QString&, MsooXmlReaderContext*)
  93. //! but return KoFilter::OK if the document for the content type is not found.
  94. KoFilter::ConversionStatus loadAndParseDocumentIfExists(
  95. const QByteArray& contentType, MsooXmlReader *reader, KoOdfWriters *writers,
  96. QString& errorMessage, MsooXmlReaderContext* context = 0);
  97. //! Like @ref loadAndParseDocumentIfExists(const QByteArray&, MsooXmlReader*, KoOdfWriters*, QString&, MsooXmlReaderContext*)
  98. //! but file name is provided directly instead of content type
  99. KoFilter::ConversionStatus loadAndParseDocumentFromFileIfExists(
  100. const QString& fileName, MsooXmlReader *reader, KoOdfWriters *writers,
  101. QString& errorMessage, MsooXmlReaderContext* context = 0);
  102. //! @return all part names.
  103. QMultiHash<QByteArray, QByteArray> partNames() const { return m_contentTypes; }
  104. //! @return part names associated with @a contentType
  105. QList<QByteArray> partNames(const QByteArray& contentType) const { return m_contentTypes.values(contentType); }
  106. QMap<QString, QVariant> documentProperties() const { return m_documentProperties; }
  107. QVariant documentProperty(const QString& propertyName) const { return m_documentProperties.value(propertyName); }
  108. protected:
  109. KoFilter::ConversionStatus loadAndParse(const QString& filename,
  110. KoXmlDocument& doc, QString& errorMessage);
  111. private:
  112. //! Opens file for converting and performs conversions.
  113. //! @return status of conversion.
  114. KoFilter::ConversionStatus openFile(KoOdfWriters *writers, QString& errorMessage);
  115. KoFilter::ConversionStatus loadAndParseDocumentInternal(
  116. const QByteArray& contentType, MsooXmlReader *reader, KoOdfWriters *writers,
  117. QString& errorMessage, MsooXmlReaderContext* context, bool *pathFound);
  118. KoFilter::ConversionStatus loadAndParseDocumentFromFileInternal(
  119. const QString& fileName, MsooXmlReader *reader, KoOdfWriters *writers,
  120. QString& errorMessage, MsooXmlReaderContext* context, bool *pathFound);
  121. KZip* m_zip; //!< Input zip file
  122. KoStore* m_outputStore; //!< output store used for copying files
  123. //! XML from "[Content_Types].xml" file.
  124. KoXmlDocument m_contentTypesXML;
  125. //! Content types from m_contentTypesXML: ContentType -> PartName mapping
  126. QMultiHash<QByteArray, QByteArray> m_contentTypes;
  127. //! Content types from m_appXML: tagName -> content mapping
  128. QMap<QString, QVariant> m_documentProperties;
  129. //! XML with document contents, typically /word/document.xml
  130. KoXmlDocument m_documentXML;
  131. QMap<QString, QSize> m_imageSizes; //!< collects image sizes to avoid multiple checks
  132. };
  133. } // namespace MSOOXML
  134. #endif