Browse Source

Split class DocProps to DocPropsCore and DocPropsApp

So that, one class can deal with only one xml file
master
Debao Zhang 11 years ago
parent
commit
b4fe4037e5
  1. 6
      src/xlsx/qtxlsx.pri
  2. 51
      src/xlsx/xlsxdocpropsapp.cpp
  3. 13
      src/xlsx/xlsxdocpropsapp_p.h
  4. 80
      src/xlsx/xlsxdocpropscore.cpp
  5. 48
      src/xlsx/xlsxdocpropscore_p.h
  6. 40
      src/xlsx/xlsxpackage.cpp
  7. 3
      src/xlsx/xlsxpackage_p.h

6
src/xlsx/qtxlsx.pri

@ -4,7 +4,8 @@ DEPENDPATH += $$PWD
QT += core gui gui-private QT += core gui gui-private
!build_xlsx_lib:DEFINES += XLSX_NO_LIB !build_xlsx_lib:DEFINES += XLSX_NO_LIB
HEADERS += $$PWD/xlsxdocprops_p.h \ HEADERS += $$PWD/xlsxdocpropscore_p.h \
$$PWD/xlsxdocpropsapp_p.h \
$$PWD/xlsxrelationships_p.h \ $$PWD/xlsxrelationships_p.h \
$$PWD/xlsxutility_p.h \ $$PWD/xlsxutility_p.h \
$$PWD/xlsxsharedstrings_p.h \ $$PWD/xlsxsharedstrings_p.h \
@ -22,7 +23,8 @@ HEADERS += $$PWD/xlsxdocprops_p.h \
$$PWD/xlsxformat_p.h \ $$PWD/xlsxformat_p.h \
$$PWD/xlsxglobal.h $$PWD/xlsxglobal.h
SOURCES += $$PWD/xlsxdocprops.cpp \ SOURCES += $$PWD/xlsxdocpropscore.cpp \
$$PWD/xlsxdocpropsapp.cpp \
$$PWD/xlsxrelationships.cpp \ $$PWD/xlsxrelationships.cpp \
$$PWD/xlsxutility.cpp \ $$PWD/xlsxutility.cpp \
$$PWD/xlsxsharedstrings.cpp \ $$PWD/xlsxsharedstrings.cpp \

51
src/xlsx/xlsxdocprops.cpp → src/xlsx/xlsxdocpropsapp.cpp

@ -22,7 +22,7 @@
** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
** **
****************************************************************************/ ****************************************************************************/
#include "xlsxdocprops_p.h" #include "xlsxdocpropsapp_p.h"
#include "xlsxxmlwriter_p.h" #include "xlsxxmlwriter_p.h"
#include <QDir> #include <QDir>
@ -31,23 +31,23 @@
#include <QVariant> #include <QVariant>
namespace QXlsx { namespace QXlsx {
DocProps::DocProps(QObject *parent) : DocPropsApp::DocPropsApp(QObject *parent) :
QObject(parent) QObject(parent)
{ {
} }
void DocProps::addPartTitle(const QString &title) void DocPropsApp::addPartTitle(const QString &title)
{ {
m_titlesOfPartsList.append(title); m_titlesOfPartsList.append(title);
} }
void DocProps::addHeadingPair(const QString &name, int value) void DocPropsApp::addHeadingPair(const QString &name, int value)
{ {
m_headingPairsList.append(qMakePair(name, value)); m_headingPairsList.append(qMakePair(name, value));
} }
void DocProps::saveToXmlFile_App(QIODevice *device) void DocPropsApp::saveToXmlFile(QIODevice *device)
{ {
XmlStreamWriter writer(device); XmlStreamWriter writer(device);
@ -97,45 +97,4 @@ void DocProps::saveToXmlFile_App(QIODevice *device)
writer.writeEndDocument(); writer.writeEndDocument();
} }
void DocProps::saveToXmlFile_Core(QIODevice *device)
{
XmlStreamWriter writer(device);
writer.writeStartDocument(QStringLiteral("1.0"), true);
writer.writeStartElement(QStringLiteral("cp:coreProperties"));
writer.writeAttribute(QStringLiteral("xmlns:cp"), QStringLiteral("http://schemas.openxmlformats.org/package/2006/metadata/core-properties"));
writer.writeAttribute(QStringLiteral("xmlns:dc"), QStringLiteral("http://purl.org/dc/elements/1.1/"));
writer.writeAttribute(QStringLiteral("xmlns:dcterms"), QStringLiteral("http://purl.org/dc/terms/"));
writer.writeAttribute(QStringLiteral("xmlns:dcmitype"), QStringLiteral("http://purl.org/dc/dcmitype/"));
writer.writeAttribute(QStringLiteral("xmlns:xsi"), QStringLiteral("http://www.w3.org/2001/XMLSchema-instance"));
if (property("title").isValid())
writer.writeTextElement(QStringLiteral("dc:title"), property("title").toString());
if (property("subject").isValid())
writer.writeTextElement(QStringLiteral("dc:subject"), property("subject").toString());
writer.writeTextElement(QStringLiteral("dc:creator"), property("creator").isValid() ? property("creator").toString() : QStringLiteral("Qt Xlsx Library"));
if (property("keywords").isValid())
writer.writeTextElement(QStringLiteral("cp:keywords"), property("keywords").toString());
if (property("description").isValid())
writer.writeTextElement(QStringLiteral("dc:description"), property("description").toString());
writer.writeTextElement(QStringLiteral("cp:lastModifiedBy"), property("creator").isValid() ? property("creator").toString() : QStringLiteral("Qt Xlsx Library"));
writer.writeStartElement(QStringLiteral("dcterms:created"));
writer.writeAttribute(QStringLiteral("xsi:type"), QStringLiteral("dcterms:W3CDTF"));
writer.writeCharacters(QDateTime::currentDateTime().toString(Qt::ISODate));
writer.writeEndElement();//dcterms:created
writer.writeStartElement(QStringLiteral("dcterms:modified"));
writer.writeAttribute(QStringLiteral("xsi:type"), QStringLiteral("dcterms:W3CDTF"));
writer.writeCharacters(QDateTime::currentDateTime().toString(Qt::ISODate));
writer.writeEndElement();//dcterms:created
if (property("category").isValid())
writer.writeTextElement(QStringLiteral("cp:category"), property("category").toString());
if (property("status").isValid())
writer.writeTextElement(QStringLiteral("cp:contentStatus"), property("status").toString());
writer.writeEndElement(); //cp:coreProperties
writer.writeEndDocument();
}
} //namespace } //namespace

13
src/xlsx/xlsxdocprops_p.h → src/xlsx/xlsxdocpropsapp_p.h

@ -22,8 +22,8 @@
** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
** **
****************************************************************************/ ****************************************************************************/
#ifndef XLSXDOCPROPS_H #ifndef XLSXDOCPROPSAPP_H
#define XLSXDOCPROPS_H #define XLSXDOCPROPSAPP_H
#include "xlsxglobal.h" #include "xlsxglobal.h"
#include <QObject> #include <QObject>
@ -35,19 +35,18 @@ class QIODevice;
namespace QXlsx { namespace QXlsx {
class XLSX_AUTOTEST_EXPORT DocProps : public QObject class XLSX_AUTOTEST_EXPORT DocPropsApp : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit DocProps(QObject *parent = 0); explicit DocPropsApp(QObject *parent = 0);
signals: signals:
public slots: public slots:
void addPartTitle(const QString &title); void addPartTitle(const QString &title);
void addHeadingPair(const QString &name, int value); void addHeadingPair(const QString &name, int value);
void saveToXmlFile_App(QIODevice *device); void saveToXmlFile(QIODevice *device);
void saveToXmlFile_Core(QIODevice *device);
private: private:
QStringList m_titlesOfPartsList; QStringList m_titlesOfPartsList;
@ -55,4 +54,4 @@ private:
}; };
} }
#endif // XLSXDOCPROPS_H #endif // XLSXDOCPROPSAPP_H

80
src/xlsx/xlsxdocpropscore.cpp

@ -0,0 +1,80 @@
/****************************************************************************
** Copyright (c) 2013 Debao Zhang <hello@debao.me>
** All right reserved.
**
** Permission is hereby granted, free of charge, to any person obtaining
** a copy of this software and associated documentation files (the
** "Software"), to deal in the Software without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
****************************************************************************/
#include "xlsxdocpropscore_p.h"
#include "xlsxxmlwriter_p.h"
#include <QDir>
#include <QFile>
#include <QDateTime>
#include <QVariant>
namespace QXlsx {
DocPropsCore::DocPropsCore(QObject *parent) :
QObject(parent)
{
}
void DocPropsCore::saveToXmlFile(QIODevice *device)
{
XmlStreamWriter writer(device);
writer.writeStartDocument(QStringLiteral("1.0"), true);
writer.writeStartElement(QStringLiteral("cp:coreProperties"));
writer.writeAttribute(QStringLiteral("xmlns:cp"), QStringLiteral("http://schemas.openxmlformats.org/package/2006/metadata/core-properties"));
writer.writeAttribute(QStringLiteral("xmlns:dc"), QStringLiteral("http://purl.org/dc/elements/1.1/"));
writer.writeAttribute(QStringLiteral("xmlns:dcterms"), QStringLiteral("http://purl.org/dc/terms/"));
writer.writeAttribute(QStringLiteral("xmlns:dcmitype"), QStringLiteral("http://purl.org/dc/dcmitype/"));
writer.writeAttribute(QStringLiteral("xmlns:xsi"), QStringLiteral("http://www.w3.org/2001/XMLSchema-instance"));
if (property("title").isValid())
writer.writeTextElement(QStringLiteral("dc:title"), property("title").toString());
if (property("subject").isValid())
writer.writeTextElement(QStringLiteral("dc:subject"), property("subject").toString());
writer.writeTextElement(QStringLiteral("dc:creator"), property("creator").isValid() ? property("creator").toString() : QStringLiteral("Qt Xlsx Library"));
if (property("keywords").isValid())
writer.writeTextElement(QStringLiteral("cp:keywords"), property("keywords").toString());
if (property("description").isValid())
writer.writeTextElement(QStringLiteral("dc:description"), property("description").toString());
writer.writeTextElement(QStringLiteral("cp:lastModifiedBy"), property("creator").isValid() ? property("creator").toString() : QStringLiteral("Qt Xlsx Library"));
writer.writeStartElement(QStringLiteral("dcterms:created"));
writer.writeAttribute(QStringLiteral("xsi:type"), QStringLiteral("dcterms:W3CDTF"));
writer.writeCharacters(QDateTime::currentDateTime().toString(Qt::ISODate));
writer.writeEndElement();//dcterms:created
writer.writeStartElement(QStringLiteral("dcterms:modified"));
writer.writeAttribute(QStringLiteral("xsi:type"), QStringLiteral("dcterms:W3CDTF"));
writer.writeCharacters(QDateTime::currentDateTime().toString(Qt::ISODate));
writer.writeEndElement();//dcterms:created
if (property("category").isValid())
writer.writeTextElement(QStringLiteral("cp:category"), property("category").toString());
if (property("status").isValid())
writer.writeTextElement(QStringLiteral("cp:contentStatus"), property("status").toString());
writer.writeEndElement(); //cp:coreProperties
writer.writeEndDocument();
}
} //namespace

48
src/xlsx/xlsxdocpropscore_p.h

@ -0,0 +1,48 @@
/****************************************************************************
** Copyright (c) 2013 Debao Zhang <hello@debao.me>
** All right reserved.
**
** Permission is hereby granted, free of charge, to any person obtaining
** a copy of this software and associated documentation files (the
** "Software"), to deal in the Software without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
****************************************************************************/
#ifndef XLSXDOCPROPSCORE_H
#define XLSXDOCPROPSCORE_H
#include "xlsxglobal.h"
#include <QObject>
#include <QList>
#include <QPair>
#include <QStringList>
class QIODevice;
namespace QXlsx {
class XLSX_AUTOTEST_EXPORT DocPropsCore : public QObject
{
Q_OBJECT
public:
explicit DocPropsCore(QObject *parent = 0);
void saveToXmlFile(QIODevice *device);
};
}
#endif // XLSXDOCPROPSCORE_H

40
src/xlsx/xlsxpackage.cpp

@ -27,7 +27,8 @@
#include "xlsxworksheet.h" #include "xlsxworksheet.h"
#include "xlsxcontenttypes_p.h" #include "xlsxcontenttypes_p.h"
#include "xlsxsharedstrings_p.h" #include "xlsxsharedstrings_p.h"
#include "xlsxdocprops_p.h" #include "xlsxdocpropscore_p.h"
#include "xlsxdocpropsapp_p.h"
#include "xlsxtheme_p.h" #include "xlsxtheme_p.h"
#include "xlsxstyles_p.h" #include "xlsxstyles_p.h"
#include "xlsxrelationships_p.h" #include "xlsxrelationships_p.h"
@ -101,7 +102,8 @@ bool Package::createPackage(const QString &packageName)
// writeCommentFiles(zipWriter); // writeCommentFiles(zipWriter);
// writeTableFiles(zipWriter); // writeTableFiles(zipWriter);
writeSharedStringsFile(zipWriter); writeSharedStringsFile(zipWriter);
writeDocPropsFiles(zipWriter); writeDocPropsAppFile(zipWriter);
writeDocPropsCoreFile(zipWriter);
writeContentTypesFiles(zipWriter); writeContentTypesFiles(zipWriter);
m_workbook->styles()->prepareStyles(); m_workbook->styles()->prepareStyles();
writeStylesFiles(zipWriter); writeStylesFiles(zipWriter);
@ -166,9 +168,9 @@ void Package::writeContentTypesFiles(ZipWriter &zipWriter)
zipWriter.addFile(QStringLiteral("[Content_Types].xml"), data); zipWriter.addFile(QStringLiteral("[Content_Types].xml"), data);
} }
void Package::writeDocPropsFiles(ZipWriter &zipWriter) void Package::writeDocPropsAppFile(ZipWriter &zipWriter)
{ {
DocProps props; DocPropsApp props;
foreach (QByteArray name, m_workbook->dynamicPropertyNames()) foreach (QByteArray name, m_workbook->dynamicPropertyNames())
props.setProperty(name.data(), m_workbook->property(name.data())); props.setProperty(name.data(), m_workbook->property(name.data()));
@ -190,17 +192,25 @@ void Package::writeDocPropsFiles(ZipWriter &zipWriter)
props.addPartTitle(sheet->name()); props.addPartTitle(sheet->name());
} }
QByteArray data1; QByteArray data;
QBuffer buffer1(&data1); QBuffer buffer(&data);
buffer1.open(QIODevice::WriteOnly); buffer.open(QIODevice::WriteOnly);
props.saveToXmlFile_App(&buffer1); props.saveToXmlFile(&buffer);
zipWriter.addFile(QStringLiteral("docProps/app.xml"), data1); zipWriter.addFile(QStringLiteral("docProps/app.xml"), data);
}
QByteArray data2;
QBuffer buffer2(&data2); void Package::writeDocPropsCoreFile(ZipWriter &zipWriter)
buffer2.open(QIODevice::WriteOnly); {
props.saveToXmlFile_Core(&buffer2); DocPropsCore props;
zipWriter.addFile(QStringLiteral("docProps/core.xml"), data2);
foreach (QByteArray name, m_workbook->dynamicPropertyNames())
props.setProperty(name.data(), m_workbook->property(name.data()));
QByteArray data;
QBuffer buffer(&data);
buffer.open(QIODevice::WriteOnly);
props.saveToXmlFile(&buffer);
zipWriter.addFile(QStringLiteral("docProps/core.xml"), data);
} }
void Package::writeSharedStringsFile(ZipWriter &zipWriter) void Package::writeSharedStringsFile(ZipWriter &zipWriter)

3
src/xlsx/xlsxpackage_p.h

@ -49,7 +49,8 @@ private:
// void writeCommentFiles(ZipWriter &zipWriter); // void writeCommentFiles(ZipWriter &zipWriter);
// void writeTableFiles(ZipWriter &zipWriter); // void writeTableFiles(ZipWriter &zipWriter);
void writeSharedStringsFile(ZipWriter &zipWriter); void writeSharedStringsFile(ZipWriter &zipWriter);
void writeDocPropsFiles(ZipWriter &zipWriter); void writeDocPropsAppFile(ZipWriter &zipWriter);
void writeDocPropsCoreFile(ZipWriter &zipWriter);
void writeContentTypesFiles(ZipWriter &zipWriter); void writeContentTypesFiles(ZipWriter &zipWriter);
void writeStylesFiles(ZipWriter &zipWriter); void writeStylesFiles(ZipWriter &zipWriter);
void writeThemeFile(ZipWriter &zipWriter); void writeThemeFile(ZipWriter &zipWriter);

Loading…
Cancel
Save