xlsxdocpropsapp.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // xlsxdocpropsapp.cpp
  2. #include "xlsxdocpropsapp_p.h"
  3. #include <QXmlStreamWriter>
  4. #include <QXmlStreamReader>
  5. #include <QDir>
  6. #include <QFile>
  7. #include <QDateTime>
  8. #include <QVariant>
  9. #include <QBuffer>
  10. QT_BEGIN_NAMESPACE_XLSX
  11. DocPropsApp::DocPropsApp(CreateFlag flag)
  12. :AbstractOOXmlFile(flag)
  13. {
  14. }
  15. void DocPropsApp::addPartTitle(const QString &title)
  16. {
  17. m_titlesOfPartsList.append(title);
  18. }
  19. void DocPropsApp::addHeadingPair(const QString &name, int value)
  20. {
  21. m_headingPairsList.append({ name, value });
  22. }
  23. bool DocPropsApp::setProperty(const QString &name, const QString &value)
  24. {
  25. static const QStringList validKeys = {
  26. QStringLiteral("manager"), QStringLiteral("company")
  27. };
  28. if (!validKeys.contains(name))
  29. return false;
  30. if (value.isEmpty())
  31. m_properties.remove(name);
  32. else
  33. m_properties[name] = value;
  34. return true;
  35. }
  36. QString DocPropsApp::property(const QString &name) const
  37. {
  38. auto it = m_properties.constFind(name);
  39. if (it != m_properties.constEnd())
  40. return it.value();
  41. return QString();
  42. }
  43. QStringList DocPropsApp::propertyNames() const
  44. {
  45. return m_properties.keys();
  46. }
  47. void DocPropsApp::saveToXmlFile(QIODevice *device) const
  48. {
  49. QXmlStreamWriter writer(device);
  50. QString vt = QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes");
  51. writer.writeStartDocument(QStringLiteral("1.0"), true);
  52. writer.writeStartElement(QStringLiteral("Properties"));
  53. writer.writeDefaultNamespace(QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"));
  54. writer.writeNamespace(vt, QStringLiteral("vt"));
  55. writer.writeTextElement(QStringLiteral("Application"), QStringLiteral("Microsoft Excel"));
  56. writer.writeTextElement(QStringLiteral("DocSecurity"), QStringLiteral("0"));
  57. writer.writeTextElement(QStringLiteral("ScaleCrop"), QStringLiteral("false"));
  58. writer.writeStartElement(QStringLiteral("HeadingPairs"));
  59. writer.writeStartElement(vt, QStringLiteral("vector"));
  60. writer.writeAttribute(QStringLiteral("size"), QString::number(m_headingPairsList.size()*2));
  61. writer.writeAttribute(QStringLiteral("baseType"), QStringLiteral("variant"));
  62. for (const auto &pair : m_headingPairsList) {
  63. writer.writeStartElement(vt, QStringLiteral("variant"));
  64. writer.writeTextElement(vt, QStringLiteral("lpstr"), pair.first);
  65. writer.writeEndElement(); //vt:variant
  66. writer.writeStartElement(vt, QStringLiteral("variant"));
  67. writer.writeTextElement(vt, QStringLiteral("i4"), QString::number(pair.second));
  68. writer.writeEndElement(); //vt:variant
  69. }
  70. writer.writeEndElement();//vt:vector
  71. writer.writeEndElement();//HeadingPairs
  72. writer.writeStartElement(QStringLiteral("TitlesOfParts"));
  73. writer.writeStartElement(vt, QStringLiteral("vector"));
  74. writer.writeAttribute(QStringLiteral("size"), QString::number(m_titlesOfPartsList.size()));
  75. writer.writeAttribute(QStringLiteral("baseType"), QStringLiteral("lpstr"));
  76. for (const QString &title : m_titlesOfPartsList)
  77. writer.writeTextElement(vt, QStringLiteral("lpstr"), title);
  78. writer.writeEndElement();//vt:vector
  79. writer.writeEndElement();//TitlesOfParts
  80. auto it = m_properties.constFind(QStringLiteral("manager"));
  81. if (it != m_properties.constEnd())
  82. writer.writeTextElement(QStringLiteral("Manager"), it.value());
  83. //Not like "manager", "company" always exists for Excel generated file.
  84. it = m_properties.constFind(QStringLiteral("company"));
  85. writer.writeTextElement(QStringLiteral("Company"), it != m_properties.constEnd() ? it.value() : QString());
  86. writer.writeTextElement(QStringLiteral("LinksUpToDate"), QStringLiteral("false"));
  87. writer.writeTextElement(QStringLiteral("SharedDoc"), QStringLiteral("false"));
  88. writer.writeTextElement(QStringLiteral("HyperlinksChanged"), QStringLiteral("false"));
  89. writer.writeTextElement(QStringLiteral("AppVersion"), QStringLiteral("12.0000"));
  90. writer.writeEndElement(); //Properties
  91. writer.writeEndDocument();
  92. }
  93. bool DocPropsApp::loadFromXmlFile(QIODevice *device)
  94. {
  95. QXmlStreamReader reader(device);
  96. while (!reader.atEnd()) {
  97. QXmlStreamReader::TokenType token = reader.readNext();
  98. if (token == QXmlStreamReader::StartElement) {
  99. if (reader.name() == QLatin1String("Properties"))
  100. continue;
  101. if (reader.name() == QStringLiteral("Manager")) {
  102. setProperty(QStringLiteral("manager"), reader.readElementText());
  103. } else if (reader.name() == QStringLiteral("Company")) {
  104. setProperty(QStringLiteral("company"), reader.readElementText());
  105. }
  106. }
  107. if (reader.hasError()) {
  108. qDebug("Error when read doc props app file.");
  109. }
  110. }
  111. return true;
  112. }
  113. QT_END_NAMESPACE_XLSX