File indexing completed on 2025-03-23 03:32:48

0001 // xlsxdocpropscore.cpp
0002 
0003 #include <QtGlobal>
0004 #include <QXmlStreamWriter>
0005 #include <QXmlStreamReader>
0006 #include <QDir>
0007 #include <QFile>
0008 #include <QDateTime>
0009 #include <QDebug>
0010 #include <QBuffer>
0011 
0012 #include "xlsxdocpropscore_p.h"
0013 
0014 QT_BEGIN_NAMESPACE_XLSX
0015 
0016 DocPropsCore::DocPropsCore(CreateFlag flag)
0017     :AbstractOOXmlFile(flag)
0018 {
0019 }
0020 
0021 bool DocPropsCore::setProperty(const QString &name, const QString &value)
0022 {
0023     static const QStringList validKeys = {
0024         QStringLiteral("title"), QStringLiteral("subject"),
0025         QStringLiteral("keywords"), QStringLiteral("description"),
0026         QStringLiteral("category"), QStringLiteral("status"),
0027         QStringLiteral("created"), QStringLiteral("creator")
0028     };
0029 
0030     if (!validKeys.contains(name))
0031         return false;
0032 
0033     if (value.isEmpty())
0034         m_properties.remove(name);
0035     else
0036         m_properties[name] = value;
0037 
0038     return true;
0039 }
0040 
0041 QString DocPropsCore::property(const QString &name) const
0042 {
0043     auto it = m_properties.constFind(name);
0044     if (it != m_properties.constEnd())
0045         return it.value();
0046 
0047     return QString();
0048 }
0049 
0050 QStringList DocPropsCore::propertyNames() const
0051 {
0052     return m_properties.keys();
0053 }
0054 
0055 void DocPropsCore::saveToXmlFile(QIODevice *device) const
0056 {
0057     QXmlStreamWriter writer(device);
0058     const QString cp = QStringLiteral("http://schemas.openxmlformats.org/package/2006/metadata/core-properties");
0059     const QString dc = QStringLiteral("http://purl.org/dc/elements/1.1/");
0060     const QString dcterms = QStringLiteral("http://purl.org/dc/terms/");
0061     const QString dcmitype = QStringLiteral("http://purl.org/dc/dcmitype/");
0062     const QString xsi = QStringLiteral("http://www.w3.org/2001/XMLSchema-instance");
0063     writer.writeStartDocument(QStringLiteral("1.0"), true);
0064     writer.writeStartElement(QStringLiteral("cp:coreProperties"));
0065     writer.writeNamespace(cp, QStringLiteral("cp"));
0066     writer.writeNamespace(dc, QStringLiteral("dc"));
0067     writer.writeNamespace(dcterms, QStringLiteral("dcterms"));
0068     writer.writeNamespace(dcmitype, QStringLiteral("dcmitype"));
0069     writer.writeNamespace(xsi, QStringLiteral("xsi"));
0070 
0071     auto it = m_properties.constFind(QStringLiteral("title"));
0072     if (it != m_properties.constEnd())
0073         writer.writeTextElement(dc, QStringLiteral("title"), it.value());
0074 
0075     it = m_properties.constFind(QStringLiteral("subject"));
0076     if (it != m_properties.constEnd())
0077         writer.writeTextElement(dc, QStringLiteral("subject"), it.value());
0078 
0079     it = m_properties.constFind(QStringLiteral("creator"));
0080     writer.writeTextElement(dc, QStringLiteral("creator"), it != m_properties.constEnd() ? it.value() : QStringLiteral("Qt Xlsx Library"));
0081 
0082     it = m_properties.constFind(QStringLiteral("keywords"));
0083     if (it != m_properties.constEnd())
0084         writer.writeTextElement(cp, QStringLiteral("keywords"), it.value());
0085 
0086     it = m_properties.constFind(QStringLiteral("description"));
0087     if (it != m_properties.constEnd())
0088         writer.writeTextElement(dc, QStringLiteral("description"), it.value());
0089 
0090     it = m_properties.constFind(QStringLiteral("creator"));
0091     writer.writeTextElement(cp, QStringLiteral("lastModifiedBy"), it != m_properties.constEnd() ? it.value() : QStringLiteral("Qt Xlsx Library"));
0092 
0093     writer.writeStartElement(dcterms, QStringLiteral("created"));
0094     writer.writeAttribute(xsi, QStringLiteral("type"), QStringLiteral("dcterms:W3CDTF"));
0095     it = m_properties.constFind(QStringLiteral("created"));
0096     writer.writeCharacters(it != m_properties.constEnd() ? it.value() : QDateTime::currentDateTime().toString(Qt::ISODate));
0097     writer.writeEndElement();//dcterms:created
0098 
0099     writer.writeStartElement(dcterms, QStringLiteral("modified"));
0100     writer.writeAttribute(xsi, QStringLiteral("type"), QStringLiteral("dcterms:W3CDTF"));
0101     writer.writeCharacters(QDateTime::currentDateTime().toString(Qt::ISODate));
0102     writer.writeEndElement();//dcterms:created
0103 
0104     it = m_properties.constFind(QStringLiteral("category"));
0105     if (it != m_properties.constEnd())
0106         writer.writeTextElement(cp, QStringLiteral("category"), it.value());
0107 
0108     it = m_properties.constFind(QStringLiteral("status"));
0109     if (it != m_properties.constEnd())
0110         writer.writeTextElement(cp, QStringLiteral("contentStatus"), it.value());
0111 
0112     writer.writeEndElement(); //cp:coreProperties
0113     writer.writeEndDocument();
0114 }
0115 
0116 bool DocPropsCore::loadFromXmlFile(QIODevice *device)
0117 {
0118     QXmlStreamReader reader(device);
0119 
0120     const QString cp = QStringLiteral("http://schemas.openxmlformats.org/package/2006/metadata/core-properties");
0121     const QString dc = QStringLiteral("http://purl.org/dc/elements/1.1/");
0122     const QString dcterms = QStringLiteral("http://purl.org/dc/terms/");
0123 
0124     while (!reader.atEnd())
0125     {
0126          QXmlStreamReader::TokenType token = reader.readNext();
0127 
0128          if (token == QXmlStreamReader::StartElement)
0129          {
0130 
0131              const auto& nsUri = reader.namespaceUri();
0132              const auto& name = reader.name();
0133 
0134              if (name == QStringLiteral("subject") && nsUri == dc)
0135              {
0136                  setProperty(QStringLiteral("subject"), reader.readElementText());
0137              }
0138              else if (name == QStringLiteral("title") && nsUri == dc)
0139              {
0140                  setProperty(QStringLiteral("title"), reader.readElementText());
0141              }
0142              else if (name == QStringLiteral("creator") && nsUri == dc)
0143              {
0144                  setProperty(QStringLiteral("creator"), reader.readElementText());
0145              }
0146              else if (name == QStringLiteral("description") && nsUri == dc)
0147              {
0148                  setProperty(QStringLiteral("description"), reader.readElementText());
0149              }
0150              else if (name == QStringLiteral("keywords") && nsUri == cp)
0151              {
0152                  setProperty(QStringLiteral("keywords"), reader.readElementText());
0153              }
0154              else if (name == QStringLiteral("created") && nsUri == dcterms)
0155              {
0156                  setProperty(QStringLiteral("created"), reader.readElementText());
0157              }
0158              else if (name == QStringLiteral("category") && nsUri == cp)
0159              {
0160                  setProperty(QStringLiteral("category"), reader.readElementText());
0161              }
0162              else if (name == QStringLiteral("contentStatus") && nsUri == cp)
0163              {
0164                  setProperty(QStringLiteral("status"), reader.readElementText());
0165              }
0166          }
0167 
0168          if (reader.hasError())
0169          {
0170              qDebug() << "Error when read doc props core file." << reader.errorString();
0171          }
0172     }
0173     return true;
0174 }
0175 
0176 QT_END_NAMESPACE_XLSX