xlsxdocpropsapp.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 "xlsxdocpropsapp_p.h"
  26. #include <QXmlStreamWriter>
  27. #include <QXmlStreamReader>
  28. #include <QDir>
  29. #include <QFile>
  30. #include <QDateTime>
  31. #include <QVariant>
  32. #include <QBuffer>
  33. namespace QXlsx {
  34. DocPropsApp::DocPropsApp(CreateFlag flag)
  35. :AbstractOOXmlFile(flag)
  36. {
  37. }
  38. void DocPropsApp::addPartTitle(const QString &title)
  39. {
  40. m_titlesOfPartsList.append(title);
  41. }
  42. void DocPropsApp::addHeadingPair(const QString &name, int value)
  43. {
  44. m_headingPairsList.append(qMakePair(name, value));
  45. }
  46. bool DocPropsApp::setProperty(const QString &name, const QString &value)
  47. {
  48. static QStringList validKeys;
  49. if (validKeys.isEmpty()) {
  50. validKeys << QStringLiteral("manager") << QStringLiteral("company");
  51. }
  52. if (!validKeys.contains(name))
  53. return false;
  54. if (value.isEmpty())
  55. m_properties.remove(name);
  56. else
  57. m_properties[name] = value;
  58. return true;
  59. }
  60. QString DocPropsApp::property(const QString &name) const
  61. {
  62. if (m_properties.contains(name))
  63. return m_properties[name];
  64. return QString();
  65. }
  66. QStringList DocPropsApp::propertyNames() const
  67. {
  68. return m_properties.keys();
  69. }
  70. void DocPropsApp::saveToXmlFile(QIODevice *device) const
  71. {
  72. QXmlStreamWriter writer(device);
  73. QString vt = QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes");
  74. writer.writeStartDocument(QStringLiteral("1.0"), true);
  75. writer.writeStartElement(QStringLiteral("Properties"));
  76. writer.writeDefaultNamespace(QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"));
  77. writer.writeNamespace(vt, QStringLiteral("vt"));
  78. writer.writeTextElement(QStringLiteral("Application"), QStringLiteral("Microsoft Excel"));
  79. writer.writeTextElement(QStringLiteral("DocSecurity"), QStringLiteral("0"));
  80. writer.writeTextElement(QStringLiteral("ScaleCrop"), QStringLiteral("false"));
  81. writer.writeStartElement(QStringLiteral("HeadingPairs"));
  82. writer.writeStartElement(vt, QStringLiteral("vector"));
  83. writer.writeAttribute(QStringLiteral("size"), QString::number(m_headingPairsList.size()*2));
  84. writer.writeAttribute(QStringLiteral("baseType"), QStringLiteral("variant"));
  85. typedef QPair<QString,int> PairType; //Make foreach happy
  86. foreach (PairType pair, m_headingPairsList) {
  87. writer.writeStartElement(vt, QStringLiteral("variant"));
  88. writer.writeTextElement(vt, QStringLiteral("lpstr"), pair.first);
  89. writer.writeEndElement(); //vt:variant
  90. writer.writeStartElement(vt, QStringLiteral("variant"));
  91. writer.writeTextElement(vt, QStringLiteral("i4"), QString::number(pair.second));
  92. writer.writeEndElement(); //vt:variant
  93. }
  94. writer.writeEndElement();//vt:vector
  95. writer.writeEndElement();//HeadingPairs
  96. writer.writeStartElement(QStringLiteral("TitlesOfParts"));
  97. writer.writeStartElement(vt, QStringLiteral("vector"));
  98. writer.writeAttribute(QStringLiteral("size"), QString::number(m_titlesOfPartsList.size()));
  99. writer.writeAttribute(QStringLiteral("baseType"), QStringLiteral("lpstr"));
  100. foreach (QString title, m_titlesOfPartsList)
  101. writer.writeTextElement(vt, QStringLiteral("lpstr"), title);
  102. writer.writeEndElement();//vt:vector
  103. writer.writeEndElement();//TitlesOfParts
  104. if (m_properties.contains(QStringLiteral("manager")))
  105. writer.writeTextElement(QStringLiteral("Manager"), m_properties[QStringLiteral("manager")]);
  106. //Not like "manager", "company" always exists for Excel generated file.
  107. writer.writeTextElement(QStringLiteral("Company"), m_properties.contains(QStringLiteral("company")) ? m_properties[QStringLiteral("company")]: QString());
  108. writer.writeTextElement(QStringLiteral("LinksUpToDate"), QStringLiteral("false"));
  109. writer.writeTextElement(QStringLiteral("SharedDoc"), QStringLiteral("false"));
  110. writer.writeTextElement(QStringLiteral("HyperlinksChanged"), QStringLiteral("false"));
  111. writer.writeTextElement(QStringLiteral("AppVersion"), QStringLiteral("12.0000"));
  112. writer.writeEndElement(); //Properties
  113. writer.writeEndDocument();
  114. }
  115. bool DocPropsApp::loadFromXmlFile(QIODevice *device)
  116. {
  117. QXmlStreamReader reader(device);
  118. while (!reader.atEnd()) {
  119. QXmlStreamReader::TokenType token = reader.readNext();
  120. if (token == QXmlStreamReader::StartElement) {
  121. if (reader.name() == QLatin1String("Properties"))
  122. continue;
  123. if (reader.name() == QStringLiteral("Manager")) {
  124. setProperty(QStringLiteral("manager"), reader.readElementText());
  125. } else if (reader.name() == QStringLiteral("Company")) {
  126. setProperty(QStringLiteral("company"), reader.readElementText());
  127. }
  128. }
  129. if (reader.hasError()) {
  130. qDebug("Error when read doc props app file.");
  131. }
  132. }
  133. return true;
  134. }
  135. } //namespace