andre@1: /**************************************************************************** andre@1: ** Copyright (c) 2013-2014 Debao Zhang andre@1: ** All right reserved. andre@1: ** andre@1: ** Permission is hereby granted, free of charge, to any person obtaining andre@1: ** a copy of this software and associated documentation files (the andre@1: ** "Software"), to deal in the Software without restriction, including andre@1: ** without limitation the rights to use, copy, modify, merge, publish, andre@1: ** distribute, sublicense, and/or sell copies of the Software, and to andre@1: ** permit persons to whom the Software is furnished to do so, subject to andre@1: ** the following conditions: andre@1: ** andre@1: ** The above copyright notice and this permission notice shall be andre@1: ** included in all copies or substantial portions of the Software. andre@1: ** andre@1: ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, andre@1: ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF andre@1: ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND andre@1: ** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE andre@1: ** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION andre@1: ** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION andre@1: ** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. andre@1: ** andre@1: ****************************************************************************/ andre@1: #include "xlsxdocpropscore_p.h" andre@1: andre@1: #include andre@1: #include andre@1: #include andre@1: #include andre@1: #include andre@1: #include andre@1: #include andre@1: andre@1: namespace QXlsx { andre@1: andre@1: DocPropsCore::DocPropsCore(CreateFlag flag) andre@1: :AbstractOOXmlFile(flag) andre@1: { andre@1: } andre@1: andre@1: bool DocPropsCore::setProperty(const QString &name, const QString &value) andre@1: { andre@1: static QStringList validKeys; andre@1: if (validKeys.isEmpty()) { andre@1: validKeys << QStringLiteral("title") << QStringLiteral("subject") andre@1: << QStringLiteral("keywords") << QStringLiteral("description") andre@1: << QStringLiteral("category") << QStringLiteral("status") andre@1: << QStringLiteral("created") << QStringLiteral("creator"); andre@1: } andre@1: andre@1: if (!validKeys.contains(name)) andre@1: return false; andre@1: andre@1: if (value.isEmpty()) andre@1: m_properties.remove(name); andre@1: else andre@1: m_properties[name] = value; andre@1: andre@1: return true; andre@1: } andre@1: andre@1: QString DocPropsCore::property(const QString &name) const andre@1: { andre@1: if (m_properties.contains(name)) andre@1: return m_properties[name]; andre@1: andre@1: return QString(); andre@1: } andre@1: andre@1: QStringList DocPropsCore::propertyNames() const andre@1: { andre@1: return m_properties.keys(); andre@1: } andre@1: andre@1: void DocPropsCore::saveToXmlFile(QIODevice *device) const andre@1: { andre@1: QXmlStreamWriter writer(device); andre@1: const QString cp = QStringLiteral("http://schemas.openxmlformats.org/package/2006/metadata/core-properties"); andre@1: const QString dc = QStringLiteral("http://purl.org/dc/elements/1.1/"); andre@1: const QString dcterms = QStringLiteral("http://purl.org/dc/terms/"); andre@1: const QString dcmitype = QStringLiteral("http://purl.org/dc/dcmitype/"); andre@1: const QString xsi = QStringLiteral("http://www.w3.org/2001/XMLSchema-instance"); andre@1: writer.writeStartDocument(QStringLiteral("1.0"), true); andre@1: writer.writeStartElement(QStringLiteral("cp:coreProperties")); andre@1: writer.writeNamespace(cp, QStringLiteral("cp")); andre@1: writer.writeNamespace(dc, QStringLiteral("dc")); andre@1: writer.writeNamespace(dcterms, QStringLiteral("dcterms")); andre@1: writer.writeNamespace(dcmitype, QStringLiteral("dcmitype")); andre@1: writer.writeNamespace(xsi, QStringLiteral("xsi")); andre@1: andre@1: if (m_properties.contains(QStringLiteral("title"))) andre@1: writer.writeTextElement(dc, QStringLiteral("title"), m_properties[QStringLiteral("title")]); andre@1: andre@1: if (m_properties.contains(QStringLiteral("subject"))) andre@1: writer.writeTextElement(dc, QStringLiteral("subject"), m_properties[QStringLiteral("subject")]); andre@1: andre@1: writer.writeTextElement(dc, QStringLiteral("creator"), m_properties.contains(QStringLiteral("creator")) ? m_properties[QStringLiteral("creator")] : QStringLiteral("Qt Xlsx Library")); andre@1: andre@1: if (m_properties.contains(QStringLiteral("keywords"))) andre@1: writer.writeTextElement(cp, QStringLiteral("keywords"), m_properties[QStringLiteral("keywords")]); andre@1: andre@1: if (m_properties.contains(QStringLiteral("description"))) andre@1: writer.writeTextElement(dc, QStringLiteral("description"), m_properties[QStringLiteral("description")]); andre@1: andre@1: writer.writeTextElement(cp, QStringLiteral("lastModifiedBy"), m_properties.contains(QStringLiteral("creator")) ? m_properties[QStringLiteral("creator")] : QStringLiteral("Qt Xlsx Library")); andre@1: andre@1: writer.writeStartElement(dcterms, QStringLiteral("created")); andre@1: writer.writeAttribute(xsi, QStringLiteral("type"), QStringLiteral("dcterms:W3CDTF")); andre@1: writer.writeCharacters(m_properties.contains(QStringLiteral("created")) ? m_properties[QStringLiteral("created")] : QDateTime::currentDateTime().toString(Qt::ISODate)); andre@1: writer.writeEndElement();//dcterms:created andre@1: andre@1: writer.writeStartElement(dcterms, QStringLiteral("modified")); andre@1: writer.writeAttribute(xsi, QStringLiteral("type"), QStringLiteral("dcterms:W3CDTF")); andre@1: writer.writeCharacters(QDateTime::currentDateTime().toString(Qt::ISODate)); andre@1: writer.writeEndElement();//dcterms:created andre@1: andre@1: if (m_properties.contains(QStringLiteral("category"))) andre@1: writer.writeTextElement(cp, QStringLiteral("category"), m_properties[QStringLiteral("category")]); andre@1: andre@1: if (m_properties.contains(QStringLiteral("status"))) andre@1: writer.writeTextElement(cp, QStringLiteral("contentStatus"), m_properties[QStringLiteral("status")]); andre@1: andre@1: writer.writeEndElement(); //cp:coreProperties andre@1: writer.writeEndDocument(); andre@1: } andre@1: andre@1: bool DocPropsCore::loadFromXmlFile(QIODevice *device) andre@1: { andre@1: QXmlStreamReader reader(device); andre@1: andre@1: const QString cp = QStringLiteral("http://schemas.openxmlformats.org/package/2006/metadata/core-properties"); andre@1: const QString dc = QStringLiteral("http://purl.org/dc/elements/1.1/"); andre@1: const QString dcterms = QStringLiteral("http://purl.org/dc/terms/"); andre@1: andre@1: while (!reader.atEnd()) { andre@1: QXmlStreamReader::TokenType token = reader.readNext(); andre@1: if (token == QXmlStreamReader::StartElement) { andre@1: const QStringRef nsUri = reader.namespaceUri(); andre@1: const QStringRef name = reader.name(); andre@1: if (name == QStringLiteral("subject") && nsUri == dc) { andre@1: setProperty(QStringLiteral("subject"), reader.readElementText()); andre@1: } else if (name == QStringLiteral("title") && nsUri == dc) { andre@1: setProperty(QStringLiteral("title"), reader.readElementText()); andre@1: } else if (name == QStringLiteral("creator") && nsUri == dc) { andre@1: setProperty(QStringLiteral("creator"), reader.readElementText()); andre@1: } else if (name == QStringLiteral("description") && nsUri == dc) { andre@1: setProperty(QStringLiteral("description"), reader.readElementText()); andre@1: } else if (name == QStringLiteral("keywords") && nsUri == cp) { andre@1: setProperty(QStringLiteral("keywords"), reader.readElementText()); andre@1: } else if (name == QStringLiteral("created") && nsUri == dcterms) { andre@1: setProperty(QStringLiteral("created"), reader.readElementText()); andre@1: } else if (name == QStringLiteral("category") && nsUri == cp) { andre@1: setProperty(QStringLiteral("category"), reader.readElementText()); andre@1: } else if (name == QStringLiteral("contentStatus") && nsUri == cp) { andre@1: setProperty(QStringLiteral("status"), reader.readElementText()); andre@1: } andre@1: } andre@1: andre@1: if (reader.hasError()) { andre@1: qDebug()<<"Error when read doc props core file."<