xlsxdocpropscore.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // xlsxdocpropscore.cpp
  2. #include <QtGlobal>
  3. #include <QXmlStreamWriter>
  4. #include <QXmlStreamReader>
  5. #include <QDir>
  6. #include <QFile>
  7. #include <QDateTime>
  8. #include <QDebug>
  9. #include <QBuffer>
  10. #include "xlsxdocpropscore_p.h"
  11. QT_BEGIN_NAMESPACE_XLSX
  12. DocPropsCore::DocPropsCore(CreateFlag flag)
  13. :AbstractOOXmlFile(flag)
  14. {
  15. }
  16. bool DocPropsCore::setProperty(const QString &name, const QString &value)
  17. {
  18. static const QStringList validKeys = {
  19. QStringLiteral("title"), QStringLiteral("subject"),
  20. QStringLiteral("keywords"), QStringLiteral("description"),
  21. QStringLiteral("category"), QStringLiteral("status"),
  22. QStringLiteral("created"), QStringLiteral("creator")
  23. };
  24. if (!validKeys.contains(name))
  25. return false;
  26. if (value.isEmpty())
  27. m_properties.remove(name);
  28. else
  29. m_properties[name] = value;
  30. return true;
  31. }
  32. QString DocPropsCore::property(const QString &name) const
  33. {
  34. auto it = m_properties.constFind(name);
  35. if (it != m_properties.constEnd())
  36. return it.value();
  37. return QString();
  38. }
  39. QStringList DocPropsCore::propertyNames() const
  40. {
  41. return m_properties.keys();
  42. }
  43. void DocPropsCore::saveToXmlFile(QIODevice *device) const
  44. {
  45. QXmlStreamWriter writer(device);
  46. const QString cp = QStringLiteral("http://schemas.openxmlformats.org/package/2006/metadata/core-properties");
  47. const QString dc = QStringLiteral("http://purl.org/dc/elements/1.1/");
  48. const QString dcterms = QStringLiteral("http://purl.org/dc/terms/");
  49. const QString dcmitype = QStringLiteral("http://purl.org/dc/dcmitype/");
  50. const QString xsi = QStringLiteral("http://www.w3.org/2001/XMLSchema-instance");
  51. writer.writeStartDocument(QStringLiteral("1.0"), true);
  52. writer.writeStartElement(QStringLiteral("cp:coreProperties"));
  53. writer.writeNamespace(cp, QStringLiteral("cp"));
  54. writer.writeNamespace(dc, QStringLiteral("dc"));
  55. writer.writeNamespace(dcterms, QStringLiteral("dcterms"));
  56. writer.writeNamespace(dcmitype, QStringLiteral("dcmitype"));
  57. writer.writeNamespace(xsi, QStringLiteral("xsi"));
  58. auto it = m_properties.constFind(QStringLiteral("title"));
  59. if (it != m_properties.constEnd())
  60. writer.writeTextElement(dc, QStringLiteral("title"), it.value());
  61. it = m_properties.constFind(QStringLiteral("subject"));
  62. if (it != m_properties.constEnd())
  63. writer.writeTextElement(dc, QStringLiteral("subject"), it.value());
  64. it = m_properties.constFind(QStringLiteral("creator"));
  65. writer.writeTextElement(dc, QStringLiteral("creator"), it != m_properties.constEnd() ? it.value() : QStringLiteral("Qt Xlsx Library"));
  66. it = m_properties.constFind(QStringLiteral("keywords"));
  67. if (it != m_properties.constEnd())
  68. writer.writeTextElement(cp, QStringLiteral("keywords"), it.value());
  69. it = m_properties.constFind(QStringLiteral("description"));
  70. if (it != m_properties.constEnd())
  71. writer.writeTextElement(dc, QStringLiteral("description"), it.value());
  72. it = m_properties.constFind(QStringLiteral("creator"));
  73. writer.writeTextElement(cp, QStringLiteral("lastModifiedBy"), it != m_properties.constEnd() ? it.value() : QStringLiteral("Qt Xlsx Library"));
  74. writer.writeStartElement(dcterms, QStringLiteral("created"));
  75. writer.writeAttribute(xsi, QStringLiteral("type"), QStringLiteral("dcterms:W3CDTF"));
  76. it = m_properties.constFind(QStringLiteral("created"));
  77. writer.writeCharacters(it != m_properties.constEnd() ? it.value() : QDateTime::currentDateTime().toString(Qt::ISODate));
  78. writer.writeEndElement();//dcterms:created
  79. writer.writeStartElement(dcterms, QStringLiteral("modified"));
  80. writer.writeAttribute(xsi, QStringLiteral("type"), QStringLiteral("dcterms:W3CDTF"));
  81. writer.writeCharacters(QDateTime::currentDateTime().toString(Qt::ISODate));
  82. writer.writeEndElement();//dcterms:created
  83. it = m_properties.constFind(QStringLiteral("category"));
  84. if (it != m_properties.constEnd())
  85. writer.writeTextElement(cp, QStringLiteral("category"), it.value());
  86. it = m_properties.constFind(QStringLiteral("status"));
  87. if (it != m_properties.constEnd())
  88. writer.writeTextElement(cp, QStringLiteral("contentStatus"), it.value());
  89. writer.writeEndElement(); //cp:coreProperties
  90. writer.writeEndDocument();
  91. }
  92. bool DocPropsCore::loadFromXmlFile(QIODevice *device)
  93. {
  94. QXmlStreamReader reader(device);
  95. const QString cp = QStringLiteral("http://schemas.openxmlformats.org/package/2006/metadata/core-properties");
  96. const QString dc = QStringLiteral("http://purl.org/dc/elements/1.1/");
  97. const QString dcterms = QStringLiteral("http://purl.org/dc/terms/");
  98. while (!reader.atEnd())
  99. {
  100. QXmlStreamReader::TokenType token = reader.readNext();
  101. if (token == QXmlStreamReader::StartElement)
  102. {
  103. const auto& nsUri = reader.namespaceUri();
  104. const auto& name = reader.name();
  105. if (name == QStringLiteral("subject") && nsUri == dc)
  106. {
  107. setProperty(QStringLiteral("subject"), reader.readElementText());
  108. }
  109. else if (name == QStringLiteral("title") && nsUri == dc)
  110. {
  111. setProperty(QStringLiteral("title"), reader.readElementText());
  112. }
  113. else if (name == QStringLiteral("creator") && nsUri == dc)
  114. {
  115. setProperty(QStringLiteral("creator"), reader.readElementText());
  116. }
  117. else if (name == QStringLiteral("description") && nsUri == dc)
  118. {
  119. setProperty(QStringLiteral("description"), reader.readElementText());
  120. }
  121. else if (name == QStringLiteral("keywords") && nsUri == cp)
  122. {
  123. setProperty(QStringLiteral("keywords"), reader.readElementText());
  124. }
  125. else if (name == QStringLiteral("created") && nsUri == dcterms)
  126. {
  127. setProperty(QStringLiteral("created"), reader.readElementText());
  128. }
  129. else if (name == QStringLiteral("category") && nsUri == cp)
  130. {
  131. setProperty(QStringLiteral("category"), reader.readElementText());
  132. }
  133. else if (name == QStringLiteral("contentStatus") && nsUri == cp)
  134. {
  135. setProperty(QStringLiteral("status"), reader.readElementText());
  136. }
  137. }
  138. if (reader.hasError())
  139. {
  140. qDebug() << "Error when read doc props core file." << reader.errorString();
  141. }
  142. }
  143. return true;
  144. }
  145. QT_END_NAMESPACE_XLSX