diff --git a/examples/xlsx/documentproperty/main.cpp b/examples/xlsx/documentproperty/main.cpp index a87c222..5f6e54a 100644 --- a/examples/xlsx/documentproperty/main.cpp +++ b/examples/xlsx/documentproperty/main.cpp @@ -10,11 +10,9 @@ int main() { QXlsx::Document xlsx; -/* - These properties are visible when you use the - Office Button -> Prepare -> Properties option in Excel and are also - available to external applications that read or index windows files -*/ + xlsx.write("A1", "View the properties through:"); + xlsx.write("A2", "Office Button -> Prepare -> Properties option in Excel"); + xlsx.setDocumentProperty("title", "This is an example spreadsheet"); xlsx.setDocumentProperty("subject", "With document properties"); xlsx.setDocumentProperty("creator", "Debao Zhang"); diff --git a/src/xlsx/xlsxdocpropsapp.cpp b/src/xlsx/xlsxdocpropsapp.cpp index 052e255..e07bce6 100755 --- a/src/xlsx/xlsxdocpropsapp.cpp +++ b/src/xlsx/xlsxdocpropsapp.cpp @@ -24,15 +24,17 @@ ****************************************************************************/ #include "xlsxdocpropsapp_p.h" #include "xlsxxmlwriter_p.h" +#include "xlsxxmlreader_p.h" #include #include #include #include +#include + namespace QXlsx { -DocPropsApp::DocPropsApp(QObject *parent) : - QObject(parent) +DocPropsApp::DocPropsApp() { } @@ -46,6 +48,36 @@ void DocPropsApp::addHeadingPair(const QString &name, int value) m_headingPairsList.append(qMakePair(name, value)); } +bool DocPropsApp::setProperty(const QString &name, const QString &value) +{ + static QStringList validKeys; + if (validKeys.isEmpty()) { + validKeys << QStringLiteral("manager") << QStringLiteral("company"); + } + + if (!validKeys.contains(name)) + return false; + + if (value.isEmpty()) + m_properties.remove(name); + else + m_properties[name] = value; + + return true; +} + +QString DocPropsApp::property(const QString &name) const +{ + if (m_properties.contains(name)) + return m_properties[name]; + + return QString(); +} + +QStringList DocPropsApp::propertyNames() const +{ + return m_properties.keys(); +} void DocPropsApp::saveToXmlFile(QIODevice *device) { @@ -84,10 +116,10 @@ void DocPropsApp::saveToXmlFile(QIODevice *device) writer.writeEndElement();//vt:vector writer.writeEndElement();//TitlesOfParts - if (property("manager").isValid()) - writer.writeTextElement(QStringLiteral("Manager"), property("manager").toString()); + if (m_properties.contains(QStringLiteral("manager"))) + writer.writeTextElement(QStringLiteral("Manager"), m_properties[QStringLiteral("manager")]); //Not like "manager", "company" always exists for Excel generated file. - writer.writeTextElement(QStringLiteral("Company"), property("company").isValid() ? property("company").toString() : QString()); + writer.writeTextElement(QStringLiteral("Company"), m_properties.contains(QStringLiteral("company")) ? m_properties[QStringLiteral("company")]: QString()); writer.writeTextElement(QStringLiteral("LinksUpToDate"), QStringLiteral("false")); writer.writeTextElement(QStringLiteral("SharedDoc"), QStringLiteral("false")); writer.writeTextElement(QStringLiteral("HyperlinksChanged"), QStringLiteral("false")); @@ -97,4 +129,46 @@ void DocPropsApp::saveToXmlFile(QIODevice *device) writer.writeEndDocument(); } +QByteArray DocPropsApp::saveToXmlData() +{ + QByteArray data; + QBuffer buffer(&data); + buffer.open(QIODevice::WriteOnly); + saveToXmlFile(&buffer); + + return data; +} + +DocPropsApp DocPropsApp::loadFromXmlFile(QIODevice *device) +{ + DocPropsApp props; + XmlStreamReader reader(device); + while(!reader.atEnd()) { + QXmlStreamReader::TokenType token = reader.readNext(); + if (token == QXmlStreamReader::StartElement) { + if (reader.name() == QLatin1String("Properties")) + continue; + + if (reader.name() == QStringLiteral("Manager")) { + props.setProperty(QStringLiteral("manager"), reader.readElementText()); + } else if (reader.qualifiedName() == QStringLiteral("Company")) { + props.setProperty(QStringLiteral("company"), reader.readElementText()); + } + } + + if (reader.hasError()) { + qDebug("Error when read doc props app file."); + } + } + return props; +} + +DocPropsApp DocPropsApp::loadFromXmlData(const QByteArray &data) +{ + QBuffer buffer; + buffer.setData(data); + buffer.open(QIODevice::ReadOnly); + return loadFromXmlFile(&buffer); +} + } //namespace diff --git a/src/xlsx/xlsxdocpropsapp_p.h b/src/xlsx/xlsxdocpropsapp_p.h index ac75875..73015fb 100755 --- a/src/xlsx/xlsxdocpropsapp_p.h +++ b/src/xlsx/xlsxdocpropsapp_p.h @@ -26,31 +26,36 @@ #define XLSXDOCPROPSAPP_H #include "xlsxglobal.h" -#include #include #include #include +#include class QIODevice; namespace QXlsx { -class XLSX_AUTOTEST_EXPORT DocPropsApp : public QObject +class XLSX_AUTOTEST_EXPORT DocPropsApp { - Q_OBJECT public: - explicit DocPropsApp(QObject *parent = 0); + DocPropsApp(); -signals: - -public slots: void addPartTitle(const QString &title); void addHeadingPair(const QString &name, int value); + + bool setProperty(const QString &name, const QString &value); + QString property(const QString &name) const; + QStringList propertyNames() const; + + QByteArray saveToXmlData(); void saveToXmlFile(QIODevice *device); + static DocPropsApp loadFromXmlFile(QIODevice *device); + static DocPropsApp loadFromXmlData(const QByteArray &data); private: QStringList m_titlesOfPartsList; QList > m_headingPairsList; + QMap m_properties; }; } diff --git a/src/xlsx/xlsxdocpropscore.cpp b/src/xlsx/xlsxdocpropscore.cpp index 3e6f633..9f57ae8 100644 --- a/src/xlsx/xlsxdocpropscore.cpp +++ b/src/xlsx/xlsxdocpropscore.cpp @@ -24,18 +24,54 @@ ****************************************************************************/ #include "xlsxdocpropscore_p.h" #include "xlsxxmlwriter_p.h" +#include "xlsxxmlreader_p.h" #include #include #include -#include +#include +#include + namespace QXlsx { -DocPropsCore::DocPropsCore(QObject *parent) : - QObject(parent) +DocPropsCore::DocPropsCore() { } +bool DocPropsCore::setProperty(const QString &name, const QString &value) +{ + static QStringList validKeys; + if (validKeys.isEmpty()) { + validKeys << QStringLiteral("title") << QStringLiteral("subject") + << QStringLiteral("keywords") << QStringLiteral("description") + << QStringLiteral("category") << QStringLiteral("status") + << QStringLiteral("created") << QStringLiteral("creator"); + } + + if (!validKeys.contains(name)) + return false; + + if (value.isEmpty()) + m_properties.remove(name); + else + m_properties[name] = value; + + return true; +} + +QString DocPropsCore::property(const QString &name) const +{ + if (m_properties.contains(name)) + return m_properties[name]; + + return QString(); +} + +QStringList DocPropsCore::propertyNames() const +{ + return m_properties.keys(); +} + void DocPropsCore::saveToXmlFile(QIODevice *device) { XmlStreamWriter writer(device); @@ -47,21 +83,26 @@ void DocPropsCore::saveToXmlFile(QIODevice *device) 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")); + + if (m_properties.contains(QStringLiteral("title"))) + writer.writeTextElement(QStringLiteral("dc:title"), m_properties[QStringLiteral("title")]); + + if (m_properties.contains(QStringLiteral("subject"))) + writer.writeTextElement(QStringLiteral("dc:subject"), m_properties[QStringLiteral("subject")]); + + writer.writeTextElement(QStringLiteral("dc:creator"), m_properties.contains(QStringLiteral("creator")) ? m_properties[QStringLiteral("creator")] : QStringLiteral("Qt Xlsx Library")); + + if (m_properties.contains(QStringLiteral("keywords"))) + writer.writeTextElement(QStringLiteral("cp:keywords"), m_properties[QStringLiteral("keywords")]); + + if (m_properties.contains(QStringLiteral("description"))) + writer.writeTextElement(QStringLiteral("dc:description"), m_properties[QStringLiteral("description")]); + + writer.writeTextElement(QStringLiteral("cp:lastModifiedBy"), m_properties.contains(QStringLiteral("creator")) ? m_properties[QStringLiteral("creator")] : 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.writeCharacters(m_properties.contains(QStringLiteral("created")) ? m_properties[QStringLiteral("created")] : QDateTime::currentDateTime().toString(Qt::ISODate)); writer.writeEndElement();//dcterms:created writer.writeStartElement(QStringLiteral("dcterms:modified")); @@ -69,12 +110,71 @@ void DocPropsCore::saveToXmlFile(QIODevice *device) 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()); + if (m_properties.contains(QStringLiteral("category"))) + writer.writeTextElement(QStringLiteral("cp:category"), m_properties[QStringLiteral("category")]); + + if (m_properties.contains(QStringLiteral("status"))) + writer.writeTextElement(QStringLiteral("cp:contentStatus"), m_properties[QStringLiteral("status")]); + writer.writeEndElement(); //cp:coreProperties writer.writeEndDocument(); } +QByteArray DocPropsCore::saveToXmlData() +{ + QByteArray data; + QBuffer buffer(&data); + buffer.open(QIODevice::WriteOnly); + saveToXmlFile(&buffer); + + return data; +} + +DocPropsCore DocPropsCore::loadFromXmlFile(QIODevice *device) +{ + DocPropsCore props; + XmlStreamReader reader(device); + while(!reader.atEnd()) { + QXmlStreamReader::TokenType token = reader.readNext(); + if (token == QXmlStreamReader::StartElement) { + if (reader.qualifiedName() == QLatin1String("cp:coreProperties")) + continue; + + QString text = reader.readElementText(); + if (reader.qualifiedName() == QStringLiteral("dc:subject")) { + props.setProperty(QStringLiteral("subject"), text); + } else if (reader.qualifiedName() == QStringLiteral("dc:title")) { + props.setProperty(QStringLiteral("title"), text); + } else if (reader.qualifiedName() == QStringLiteral("dc:creator")) { + props.setProperty(QStringLiteral("creator"), text); + } else if (reader.qualifiedName() == QStringLiteral("dc:description")) { + props.setProperty(QStringLiteral("description"), text); + } else if (reader.qualifiedName() == QStringLiteral("cp:keywords")) { + props.setProperty(QStringLiteral("keywords"), text); + } else if (reader.qualifiedName() == QStringLiteral("dcterms:created")) { + props.setProperty(QStringLiteral("created"), text); + } else if (reader.qualifiedName() == QStringLiteral("cp:category")) { + props.setProperty(QStringLiteral("category"), text); + } else if (reader.qualifiedName() == QStringLiteral("cp:contentStatus")) { + props.setProperty(QStringLiteral("status"), text); + } + } + + if (reader.hasError()) { + qDebug()<<"Error when read doc props core file."; + + } + } + return props; +} + +DocPropsCore DocPropsCore::loadFromXmlData(const QByteArray &data) +{ + QBuffer buffer; + buffer.setData(data); + buffer.open(QIODevice::ReadOnly); + return loadFromXmlFile(&buffer); +} + + } //namespace diff --git a/src/xlsx/xlsxdocpropscore_p.h b/src/xlsx/xlsxdocpropscore_p.h index 63749e7..62b3be3 100644 --- a/src/xlsx/xlsxdocpropscore_p.h +++ b/src/xlsx/xlsxdocpropscore_p.h @@ -26,22 +26,29 @@ #define XLSXDOCPROPSCORE_H #include "xlsxglobal.h" -#include -#include -#include +#include #include class QIODevice; namespace QXlsx { -class XLSX_AUTOTEST_EXPORT DocPropsCore : public QObject +class XLSX_AUTOTEST_EXPORT DocPropsCore { - Q_OBJECT public: - explicit DocPropsCore(QObject *parent = 0); + explicit DocPropsCore(); + + bool setProperty(const QString &name, const QString &value); + QString property(const QString &name) const; + QStringList propertyNames() const; void saveToXmlFile(QIODevice *device); + QByteArray saveToXmlData(); + static DocPropsCore loadFromXmlFile(QIODevice *device); + static DocPropsCore loadFromXmlData(const QByteArray &data); + +private: + QMap m_properties; }; } diff --git a/src/xlsx/xlsxdocument.cpp b/src/xlsx/xlsxdocument.cpp index 3a63722..88720bc 100644 --- a/src/xlsx/xlsxdocument.cpp +++ b/src/xlsx/xlsxdocument.cpp @@ -23,7 +23,9 @@ void DocumentPrivate::init() bool DocumentPrivate::loadPackage(QIODevice *device) { - return false; + Q_Q(Document); + Package package(q); + return package.parsePackage(device); } diff --git a/src/xlsx/xlsxpackage.cpp b/src/xlsx/xlsxpackage.cpp index 88f619e..085aaa7 100644 --- a/src/xlsx/xlsxpackage.cpp +++ b/src/xlsx/xlsxpackage.cpp @@ -95,23 +95,46 @@ bool Package::parsePackage(QIODevice *packageDevice) ZipReader zipReader(packageDevice); QStringList filePaths = zipReader.filePaths(); - if (!filePaths.contains(QLatin1String("_rels/.rel"))) + if (!filePaths.contains(QLatin1String("_rels/.rels"))) return false; - Relationships rootRels = readRelsFile(zipReader, QStringLiteral("_rels/.rel")); + Relationships rootRels = Relationships::loadFromXmlData(zipReader.fileData(QStringLiteral("_rels/.rels"))); - return false; -} + //load core property + QList rels_core = rootRels.packageRelationships(QStringLiteral("/metadata/core-properties")); + if (!rels_core.isEmpty()) { + //Get the core property file name if it exists. + //In normal case, this should be "docProps/core.xml" + QString docPropsCore_Name = rels_core[0].target; -Relationships Package::readRelsFile(ZipReader &zipReader, const QString &filePath) -{ - QByteArray relsData = zipReader.fileData(filePath); - QBuffer buffer(&relsData); - buffer.open(QIODevice::ReadOnly); + DocPropsCore props = DocPropsCore::loadFromXmlData(zipReader.fileData(docPropsCore_Name)); + foreach (QString name, props.propertyNames()) + m_document->setDocumentProperty(name, props.property(name)); + } - Relationships rels; - rels.loadFromXmlFile(&buffer); - return rels; + //load app property + QList rels_app = rootRels.documentRelationships(QStringLiteral("/extended-properties")); + if (!rels_app.isEmpty()) { + //Get the app property file name if it exists. + //In normal case, this should be "docProps/app.xml" + QString docPropsApp_Name = rels_app[0].target; + + DocPropsApp props = DocPropsApp::loadFromXmlData(zipReader.fileData(docPropsApp_Name)); + foreach (QString name, props.propertyNames()) + m_document->setDocumentProperty(name, props.property(name)); + } + + //load workbook now + QList rels_xl = rootRels.documentRelationships(QStringLiteral("/officeDocument")); + if (!rels_xl.isEmpty()) { + //Get the app property file name if it exists. + //In normal case, this should be "xl/workbook.xml" + QString xlworkbook_Name = rels_xl[0].target; + + //ToDo: Read the workbook here! + } + + return false; } bool Package::createPackage(QIODevice *package) @@ -226,7 +249,7 @@ void Package::writeDocPropsAppFile(ZipWriter &zipWriter) DocPropsApp props; foreach (QString name, m_document->documentPropertyNames()) - props.setProperty(name.toUtf8().data(), m_document->documentProperty(name)); + props.setProperty(name, m_document->documentProperty(name)); if (m_worksheet_count) props.addHeadingPair(QStringLiteral("Worksheets"), m_worksheet_count); @@ -245,11 +268,7 @@ void Package::writeDocPropsAppFile(ZipWriter &zipWriter) props.addPartTitle(sheet->name()); } - QByteArray data; - QBuffer buffer(&data); - buffer.open(QIODevice::WriteOnly); - props.saveToXmlFile(&buffer); - zipWriter.addFile(QStringLiteral("docProps/app.xml"), data); + zipWriter.addFile(QStringLiteral("docProps/app.xml"), props.saveToXmlData()); } void Package::writeDocPropsCoreFile(ZipWriter &zipWriter) @@ -257,13 +276,9 @@ void Package::writeDocPropsCoreFile(ZipWriter &zipWriter) DocPropsCore props; foreach (QString name, m_document->documentPropertyNames()) - props.setProperty(name.toUtf8().data(), m_document->documentProperty(name)); + props.setProperty(name, m_document->documentProperty(name)); - QByteArray data; - QBuffer buffer(&data); - buffer.open(QIODevice::WriteOnly); - props.saveToXmlFile(&buffer); - zipWriter.addFile(QStringLiteral("docProps/core.xml"), data); + zipWriter.addFile(QStringLiteral("docProps/core.xml"), props.saveToXmlData()); } void Package::writeSharedStringsFile(ZipWriter &zipWriter) @@ -300,11 +315,7 @@ void Package::writeRootRelsFile(ZipWriter &zipWriter) rels.addPackageRelationship(QStringLiteral("/metadata/core-properties"), QStringLiteral("docProps/core.xml")); rels.addDocumentRelationship(QStringLiteral("/extended-properties"), QStringLiteral("docProps/app.xml")); - QByteArray data; - QBuffer buffer(&data); - buffer.open(QIODevice::WriteOnly); - rels.saveToXmlFile(&buffer); - zipWriter.addFile(QStringLiteral("_rels/.rels"), data); + zipWriter.addFile(QStringLiteral("_rels/.rels"), rels.saveToXmlData()); } void Package::writeWorkbookRelsFile(ZipWriter &zipWriter) @@ -329,11 +340,7 @@ void Package::writeWorkbookRelsFile(ZipWriter &zipWriter) if (m_workbook->sharedStrings()->count()) rels.addDocumentRelationship(QStringLiteral("/sharedStrings"), QStringLiteral("sharedStrings.xml")); - QByteArray data; - QBuffer buffer(&data); - buffer.open(QIODevice::WriteOnly); - rels.saveToXmlFile(&buffer); - zipWriter.addFile(QStringLiteral("xl/_rels/workbook.xml.rels"), data); + zipWriter.addFile(QStringLiteral("xl/_rels/workbook.xml.rels"), rels.saveToXmlData()); } void Package::writeWorksheetRelsFiles(ZipWriter &zipWriter) @@ -348,11 +355,8 @@ void Package::writeWorksheetRelsFiles(ZipWriter &zipWriter) rels.addWorksheetRelationship(QStringLiteral("/hyperlink"), link, QStringLiteral("External")); foreach (QString link, sheet->externDrawingList()) rels.addWorksheetRelationship(QStringLiteral("/drawing"), link); - QByteArray data; - QBuffer buffer(&data); - buffer.open(QIODevice::WriteOnly); - rels.saveToXmlFile(&buffer); - zipWriter.addFile(QStringLiteral("xl/worksheets/_rels/sheet%1.xml.rels").arg(index), data); + + zipWriter.addFile(QStringLiteral("xl/worksheets/_rels/sheet%1.xml.rels").arg(index), rels.saveToXmlData()); index += 1; } } @@ -369,11 +373,7 @@ void Package::writeDrawingRelsFiles(ZipWriter &zipWriter) foreach (PairType pair, sheet->drawingLinks()) rels.addDocumentRelationship(pair.first, pair.second); - QByteArray data; - QBuffer buffer(&data); - buffer.open(QIODevice::WriteOnly); - rels.saveToXmlFile(&buffer); - zipWriter.addFile(QStringLiteral("xl/drawings/_rels/drawing%1.xml.rels").arg(index), data); + zipWriter.addFile(QStringLiteral("xl/drawings/_rels/drawing%1.xml.rels").arg(index), rels.saveToXmlData()); index += 1; } } diff --git a/src/xlsx/xlsxpackage_p.h b/src/xlsx/xlsxpackage_p.h index e53ec8a..f3e9562 100644 --- a/src/xlsx/xlsxpackage_p.h +++ b/src/xlsx/xlsxpackage_p.h @@ -36,6 +36,8 @@ class ZipWriter; class ZipReader; class Document; class Relationships; +class DocPropsCore; +class DocPropsApp; class XLSX_AUTOTEST_EXPORT Package { @@ -46,7 +48,6 @@ public: bool createPackage(QIODevice *package); private: - Relationships readRelsFile(ZipReader &reader, const QString &filePath); void writeWorksheetFiles(ZipWriter &zipWriter); // void writeChartsheetFiles(ZipWriter &zipWriter); diff --git a/src/xlsx/xlsxrelationships.cpp b/src/xlsx/xlsxrelationships.cpp index a255197..422a87e 100755 --- a/src/xlsx/xlsxrelationships.cpp +++ b/src/xlsx/xlsxrelationships.cpp @@ -27,6 +27,7 @@ #include "xlsxxmlreader_p.h" #include #include +#include namespace QXlsx { @@ -119,9 +120,20 @@ void Relationships::saveToXmlFile(QIODevice *device) writer.writeEndDocument(); } -void Relationships::loadFromXmlFile(QIODevice *device) +QByteArray Relationships::saveToXmlData() { - m_relationships.clear(); + QByteArray data; + QBuffer buffer(&data); + buffer.open(QIODevice::WriteOnly); + saveToXmlFile(&buffer); + + return data; +} + +Relationships Relationships::loadFromXmlFile(QIODevice *device) +{ + Relationships rels; + XmlStreamReader reader(device); while(!reader.atEnd()) { QXmlStreamReader::TokenType token = reader.readNext(); @@ -133,7 +145,7 @@ void Relationships::loadFromXmlFile(QIODevice *device) relationship.type = attributes.value(QLatin1String("Type")).toString(); relationship.target = attributes.value(QLatin1String("Target")).toString(); relationship.targetMode = attributes.value(QLatin1String("TargetMode")).toString(); - m_relationships.append(relationship); + rels.m_relationships.append(relationship); } } @@ -141,6 +153,15 @@ void Relationships::loadFromXmlFile(QIODevice *device) } } + return rels; +} + +Relationships Relationships::loadFromXmlData(const QByteArray &data) +{ + QBuffer buffer; + buffer.setData(data); + buffer.open(QIODevice::ReadOnly); + return loadFromXmlFile(&buffer); } } //namespace diff --git a/src/xlsx/xlsxrelationships_p.h b/src/xlsx/xlsxrelationships_p.h index 2d78c02..ee0bab6 100755 --- a/src/xlsx/xlsxrelationships_p.h +++ b/src/xlsx/xlsxrelationships_p.h @@ -56,7 +56,9 @@ public: void addWorksheetRelationship(const QString &relativeType, const QString &target, const QString &targetMode=QString()); void saveToXmlFile(QIODevice *device); - void loadFromXmlFile(QIODevice *device); + QByteArray saveToXmlData(); + static Relationships loadFromXmlFile(QIODevice *device); + static Relationships loadFromXmlData(const QByteArray &data); private: QList relationships(const QString &type) const; diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index 2b253ba..b8a58f4 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -3,4 +3,7 @@ SUBDIRS=\ utility \ mergecell \ zipreader \ - relationships + relationships \ + propscore \ + propsapp \ + readdocument diff --git a/tests/auto/propsapp/propsapp.pro b/tests/auto/propsapp/propsapp.pro new file mode 100644 index 0000000..c4b7b05 --- /dev/null +++ b/tests/auto/propsapp/propsapp.pro @@ -0,0 +1,13 @@ +QT += testlib xlsx xlsx-private +CONFIG += testcase +DEFINES += XLSX_TEST + +TARGET = tst_docpropsapptest +CONFIG += console +CONFIG -= app_bundle + +TEMPLATE = app + + +SOURCES += tst_docpropsapptest.cpp +DEFINES += SRCDIR=\\\"$$PWD/\\\" diff --git a/tests/auto/propsapp/tst_docpropsapptest.cpp b/tests/auto/propsapp/tst_docpropsapptest.cpp new file mode 100644 index 0000000..b484e4a --- /dev/null +++ b/tests/auto/propsapp/tst_docpropsapptest.cpp @@ -0,0 +1,43 @@ +#include "private/xlsxdocpropsapp_p.h" +#include +#include + +class DocPropsAppTest : public QObject +{ + Q_OBJECT + +public: + DocPropsAppTest(); + +private Q_SLOTS: + void testCase1(); +}; + +DocPropsAppTest::DocPropsAppTest() +{ + +} + +void DocPropsAppTest::testCase1() +{ + QXlsx::DocPropsApp props; + + props.setProperty("company", "HMI CN"); + props.setProperty("manager", "Debao"); + + QFile f1("temp.xml"); + f1.open(QFile::WriteOnly); + props.saveToXmlFile(&f1); + f1.close(); + + f1.open(QFile::ReadOnly); + QXlsx::DocPropsApp props2 = QXlsx::DocPropsApp::loadFromXmlFile(&f1); + + QCOMPARE(props2.property("company"), QString("HMI CN")); + QCOMPARE(props2.property("manager"), QString("Debao")); + QFile::remove("temp.xml"); +} + +QTEST_APPLESS_MAIN(DocPropsAppTest) + +#include "tst_docpropsapptest.moc" diff --git a/tests/auto/propscore/propscore.pro b/tests/auto/propscore/propscore.pro new file mode 100644 index 0000000..ef20a0c --- /dev/null +++ b/tests/auto/propscore/propscore.pro @@ -0,0 +1,13 @@ +QT += testlib xlsx xlsx-private +CONFIG += testcase +DEFINES += XLSX_TEST + +TARGET = tst_propscoretest +CONFIG += console +CONFIG -= app_bundle + +TEMPLATE = app + + +SOURCES += tst_propscoretest.cpp +DEFINES += SRCDIR=\\\"$$PWD/\\\" diff --git a/tests/auto/propscore/tst_propscoretest.cpp b/tests/auto/propscore/tst_propscoretest.cpp new file mode 100644 index 0000000..aac729c --- /dev/null +++ b/tests/auto/propscore/tst_propscoretest.cpp @@ -0,0 +1,45 @@ +#include "private/xlsxdocpropscore_p.h" +#include +#include +#include + +class DocPropsCoreTest : public QObject +{ + Q_OBJECT + +public: + DocPropsCoreTest(); + +private Q_SLOTS: + void testCase1(); +}; + +DocPropsCoreTest::DocPropsCoreTest() +{ +} + +void DocPropsCoreTest::testCase1() +{ + QXlsx::DocPropsCore props; + + props.setProperty("creator", "Debao"); + props.setProperty("keywords", "Test, test, TEST"); + props.setProperty("title", "ABC"); + + QFile f1("temp.xml"); + f1.open(QFile::WriteOnly); + props.saveToXmlFile(&f1); + f1.close(); + + f1.open(QFile::ReadOnly); + QXlsx::DocPropsCore props2 = QXlsx::DocPropsCore::loadFromXmlFile(&f1); + + QCOMPARE(props2.property("creator"), QString("Debao")); + QCOMPARE(props2.property("keywords"), QString("Test, test, TEST")); + QCOMPARE(props2.property("title"), QString("ABC")); + QFile::remove("temp.xml"); +} + +QTEST_APPLESS_MAIN(DocPropsCoreTest) + +#include "tst_propscoretest.moc" diff --git a/tests/auto/readdocument/readdocument.pro b/tests/auto/readdocument/readdocument.pro new file mode 100644 index 0000000..238276e --- /dev/null +++ b/tests/auto/readdocument/readdocument.pro @@ -0,0 +1,13 @@ +QT += testlib xlsx xlsx-private +CONFIG += testcase +DEFINES += XLSX_TEST + +TARGET = tst_readdocumenttest +CONFIG += console +CONFIG -= app_bundle + +TEMPLATE = app + + +SOURCES += tst_readdocumenttest.cpp +DEFINES += SRCDIR=\\\"$$PWD/\\\" diff --git a/tests/auto/readdocument/tst_readdocumenttest.cpp b/tests/auto/readdocument/tst_readdocumenttest.cpp new file mode 100644 index 0000000..b3242af --- /dev/null +++ b/tests/auto/readdocument/tst_readdocumenttest.cpp @@ -0,0 +1,36 @@ +#include "xlsxdocument.h" +#include +#include + +class ReadDocumentTest : public QObject +{ + Q_OBJECT + +public: + ReadDocumentTest(); + +private Q_SLOTS: + void testDocProps(); +}; + +ReadDocumentTest::ReadDocumentTest() +{ +} + +void ReadDocumentTest::testDocProps() +{ + QXlsx::Document doc1; + doc1.setDocumentProperty("creator", "Debao"); + doc1.setDocumentProperty("company", "Test"); + doc1.saveAs("test.xlsx"); + + QXlsx::Document doc2("test.xlsx"); + QCOMPARE(doc2.documentProperty("creator"), QString("Debao")); + QCOMPARE(doc2.documentProperty("company"), QString("Test")); + + QFile::remove("test.xlsx"); +} + +QTEST_APPLESS_MAIN(ReadDocumentTest) + +#include "tst_readdocumenttest.moc"