xlsxdocpropscore.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /****************************************************************************
  2. ** Copyright (c) 2013-2014 Debao Zhang <hello@debao.me>
  3. ** All right reserved.
  4. **
  5. ** Permission is hereby granted, free of charge, to any person obtaining
  6. ** a copy of this software and associated documentation files (the
  7. ** "Software"), to deal in the Software without restriction, including
  8. ** without limitation the rights to use, copy, modify, merge, publish,
  9. ** distribute, sublicense, and/or sell copies of the Software, and to
  10. ** permit persons to whom the Software is furnished to do so, subject to
  11. ** the following conditions:
  12. **
  13. ** The above copyright notice and this permission notice shall be
  14. ** included in all copies or substantial portions of the Software.
  15. **
  16. ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. ** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. ** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. ** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. ** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. **
  24. ****************************************************************************/
  25. #include "xlsxdocpropscore_p.h"
  26. #include <QXmlStreamWriter>
  27. #include <QXmlStreamReader>
  28. #include <QDir>
  29. #include <QFile>
  30. #include <QDateTime>
  31. #include <QDebug>
  32. #include <QBuffer>
  33. namespace QXlsx {
  34. DocPropsCore::DocPropsCore(CreateFlag flag)
  35. :AbstractOOXmlFile(flag)
  36. {
  37. }
  38. bool DocPropsCore::setProperty(const QString &name, const QString &value)
  39. {
  40. static QStringList validKeys;
  41. if (validKeys.isEmpty()) {
  42. validKeys << QStringLiteral("title") << QStringLiteral("subject")
  43. << QStringLiteral("keywords") << QStringLiteral("description")
  44. << QStringLiteral("category") << QStringLiteral("status")
  45. << QStringLiteral("created") << QStringLiteral("creator");
  46. }
  47. if (!validKeys.contains(name))
  48. return false;
  49. if (value.isEmpty())
  50. m_properties.remove(name);
  51. else
  52. m_properties[name] = value;
  53. return true;
  54. }
  55. QString DocPropsCore::property(const QString &name) const
  56. {
  57. if (m_properties.contains(name))
  58. return m_properties[name];
  59. return QString();
  60. }
  61. QStringList DocPropsCore::propertyNames() const
  62. {
  63. return m_properties.keys();
  64. }
  65. void DocPropsCore::saveToXmlFile(QIODevice *device) const
  66. {
  67. QXmlStreamWriter writer(device);
  68. const QString cp = QStringLiteral("http://schemas.openxmlformats.org/package/2006/metadata/core-properties");
  69. const QString dc = QStringLiteral("http://purl.org/dc/elements/1.1/");
  70. const QString dcterms = QStringLiteral("http://purl.org/dc/terms/");
  71. const QString dcmitype = QStringLiteral("http://purl.org/dc/dcmitype/");
  72. const QString xsi = QStringLiteral("http://www.w3.org/2001/XMLSchema-instance");
  73. writer.writeStartDocument(QStringLiteral("1.0"), true);
  74. writer.writeStartElement(QStringLiteral("cp:coreProperties"));
  75. writer.writeNamespace(cp, QStringLiteral("cp"));
  76. writer.writeNamespace(dc, QStringLiteral("dc"));
  77. writer.writeNamespace(dcterms, QStringLiteral("dcterms"));
  78. writer.writeNamespace(dcmitype, QStringLiteral("dcmitype"));
  79. writer.writeNamespace(xsi, QStringLiteral("xsi"));
  80. if (m_properties.contains(QStringLiteral("title")))
  81. writer.writeTextElement(dc, QStringLiteral("title"), m_properties[QStringLiteral("title")]);
  82. if (m_properties.contains(QStringLiteral("subject")))
  83. writer.writeTextElement(dc, QStringLiteral("subject"), m_properties[QStringLiteral("subject")]);
  84. writer.writeTextElement(dc, QStringLiteral("creator"), m_properties.contains(QStringLiteral("creator")) ? m_properties[QStringLiteral("creator")] : QStringLiteral("Qt Xlsx Library"));
  85. if (m_properties.contains(QStringLiteral("keywords")))
  86. writer.writeTextElement(cp, QStringLiteral("keywords"), m_properties[QStringLiteral("keywords")]);
  87. if (m_properties.contains(QStringLiteral("description")))
  88. writer.writeTextElement(dc, QStringLiteral("description"), m_properties[QStringLiteral("description")]);
  89. writer.writeTextElement(cp, QStringLiteral("lastModifiedBy"), m_properties.contains(QStringLiteral("creator")) ? m_properties[QStringLiteral("creator")] : QStringLiteral("Qt Xlsx Library"));
  90. writer.writeStartElement(dcterms, QStringLiteral("created"));
  91. writer.writeAttribute(xsi, QStringLiteral("type"), QStringLiteral("dcterms:W3CDTF"));
  92. writer.writeCharacters(m_properties.contains(QStringLiteral("created")) ? m_properties[QStringLiteral("created")] : QDateTime::currentDateTime().toString(Qt::ISODate));
  93. writer.writeEndElement();//dcterms:created
  94. writer.writeStartElement(dcterms, QStringLiteral("modified"));
  95. writer.writeAttribute(xsi, QStringLiteral("type"), QStringLiteral("dcterms:W3CDTF"));
  96. writer.writeCharacters(QDateTime::currentDateTime().toString(Qt::ISODate));
  97. writer.writeEndElement();//dcterms:created
  98. if (m_properties.contains(QStringLiteral("category")))
  99. writer.writeTextElement(cp, QStringLiteral("category"), m_properties[QStringLiteral("category")]);
  100. if (m_properties.contains(QStringLiteral("status")))
  101. writer.writeTextElement(cp, QStringLiteral("contentStatus"), m_properties[QStringLiteral("status")]);
  102. writer.writeEndElement(); //cp:coreProperties
  103. writer.writeEndDocument();
  104. }
  105. bool DocPropsCore::loadFromXmlFile(QIODevice *device)
  106. {
  107. QXmlStreamReader reader(device);
  108. const QString cp = QStringLiteral("http://schemas.openxmlformats.org/package/2006/metadata/core-properties");
  109. const QString dc = QStringLiteral("http://purl.org/dc/elements/1.1/");
  110. const QString dcterms = QStringLiteral("http://purl.org/dc/terms/");
  111. while (!reader.atEnd()) {
  112. QXmlStreamReader::TokenType token = reader.readNext();
  113. if (token == QXmlStreamReader::StartElement) {
  114. const QStringRef nsUri = reader.namespaceUri();
  115. const QStringRef name = reader.name();
  116. if (name == QStringLiteral("subject") && nsUri == dc) {
  117. setProperty(QStringLiteral("subject"), reader.readElementText());
  118. } else if (name == QStringLiteral("title") && nsUri == dc) {
  119. setProperty(QStringLiteral("title"), reader.readElementText());
  120. } else if (name == QStringLiteral("creator") && nsUri == dc) {
  121. setProperty(QStringLiteral("creator"), reader.readElementText());
  122. } else if (name == QStringLiteral("description") && nsUri == dc) {
  123. setProperty(QStringLiteral("description"), reader.readElementText());
  124. } else if (name == QStringLiteral("keywords") && nsUri == cp) {
  125. setProperty(QStringLiteral("keywords"), reader.readElementText());
  126. } else if (name == QStringLiteral("created") && nsUri == dcterms) {
  127. setProperty(QStringLiteral("created"), reader.readElementText());
  128. } else if (name == QStringLiteral("category") && nsUri == cp) {
  129. setProperty(QStringLiteral("category"), reader.readElementText());
  130. } else if (name == QStringLiteral("contentStatus") && nsUri == cp) {
  131. setProperty(QStringLiteral("status"), reader.readElementText());
  132. }
  133. }
  134. if (reader.hasError()) {
  135. qDebug()<<"Error when read doc props core file."<<reader.errorString();
  136. }
  137. }
  138. return true;
  139. }
  140. } //namespace