File indexing completed on 2025-03-23 03:32:48
0001 // xlsxdocpropsapp.cpp 0002 0003 #include "xlsxdocpropsapp_p.h" 0004 0005 #include <QXmlStreamWriter> 0006 #include <QXmlStreamReader> 0007 #include <QDir> 0008 #include <QFile> 0009 #include <QDateTime> 0010 #include <QVariant> 0011 #include <QBuffer> 0012 0013 QT_BEGIN_NAMESPACE_XLSX 0014 0015 DocPropsApp::DocPropsApp(CreateFlag flag) 0016 :AbstractOOXmlFile(flag) 0017 { 0018 } 0019 0020 void DocPropsApp::addPartTitle(const QString &title) 0021 { 0022 m_titlesOfPartsList.append(title); 0023 } 0024 0025 void DocPropsApp::addHeadingPair(const QString &name, int value) 0026 { 0027 m_headingPairsList.append({ name, value }); 0028 } 0029 0030 bool DocPropsApp::setProperty(const QString &name, const QString &value) 0031 { 0032 static const QStringList validKeys = { 0033 QStringLiteral("manager"), QStringLiteral("company") 0034 }; 0035 0036 if (!validKeys.contains(name)) 0037 return false; 0038 0039 if (value.isEmpty()) 0040 m_properties.remove(name); 0041 else 0042 m_properties[name] = value; 0043 0044 return true; 0045 } 0046 0047 QString DocPropsApp::property(const QString &name) const 0048 { 0049 auto it = m_properties.constFind(name); 0050 if (it != m_properties.constEnd()) 0051 return it.value(); 0052 0053 return QString(); 0054 } 0055 0056 QStringList DocPropsApp::propertyNames() const 0057 { 0058 return m_properties.keys(); 0059 } 0060 0061 void DocPropsApp::saveToXmlFile(QIODevice *device) const 0062 { 0063 QXmlStreamWriter writer(device); 0064 QString vt = QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"); 0065 0066 writer.writeStartDocument(QStringLiteral("1.0"), true); 0067 writer.writeStartElement(QStringLiteral("Properties")); 0068 writer.writeDefaultNamespace(QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/extended-properties")); 0069 writer.writeNamespace(vt, QStringLiteral("vt")); 0070 writer.writeTextElement(QStringLiteral("Application"), QStringLiteral("Microsoft Excel")); 0071 writer.writeTextElement(QStringLiteral("DocSecurity"), QStringLiteral("0")); 0072 writer.writeTextElement(QStringLiteral("ScaleCrop"), QStringLiteral("false")); 0073 0074 writer.writeStartElement(QStringLiteral("HeadingPairs")); 0075 writer.writeStartElement(vt, QStringLiteral("vector")); 0076 writer.writeAttribute(QStringLiteral("size"), QString::number(m_headingPairsList.size()*2)); 0077 writer.writeAttribute(QStringLiteral("baseType"), QStringLiteral("variant")); 0078 0079 for (const auto &pair : m_headingPairsList) { 0080 writer.writeStartElement(vt, QStringLiteral("variant")); 0081 writer.writeTextElement(vt, QStringLiteral("lpstr"), pair.first); 0082 writer.writeEndElement(); //vt:variant 0083 writer.writeStartElement(vt, QStringLiteral("variant")); 0084 writer.writeTextElement(vt, QStringLiteral("i4"), QString::number(pair.second)); 0085 writer.writeEndElement(); //vt:variant 0086 } 0087 writer.writeEndElement();//vt:vector 0088 writer.writeEndElement();//HeadingPairs 0089 0090 writer.writeStartElement(QStringLiteral("TitlesOfParts")); 0091 writer.writeStartElement(vt, QStringLiteral("vector")); 0092 writer.writeAttribute(QStringLiteral("size"), QString::number(m_titlesOfPartsList.size())); 0093 writer.writeAttribute(QStringLiteral("baseType"), QStringLiteral("lpstr")); 0094 for (const QString &title : m_titlesOfPartsList) 0095 writer.writeTextElement(vt, QStringLiteral("lpstr"), title); 0096 writer.writeEndElement();//vt:vector 0097 writer.writeEndElement();//TitlesOfParts 0098 0099 auto it = m_properties.constFind(QStringLiteral("manager")); 0100 if (it != m_properties.constEnd()) 0101 writer.writeTextElement(QStringLiteral("Manager"), it.value()); 0102 //Not like "manager", "company" always exists for Excel generated file. 0103 0104 it = m_properties.constFind(QStringLiteral("company")); 0105 writer.writeTextElement(QStringLiteral("Company"), it != m_properties.constEnd() ? it.value() : QString()); 0106 writer.writeTextElement(QStringLiteral("LinksUpToDate"), QStringLiteral("false")); 0107 writer.writeTextElement(QStringLiteral("SharedDoc"), QStringLiteral("false")); 0108 writer.writeTextElement(QStringLiteral("HyperlinksChanged"), QStringLiteral("false")); 0109 writer.writeTextElement(QStringLiteral("AppVersion"), QStringLiteral("12.0000")); 0110 0111 writer.writeEndElement(); //Properties 0112 writer.writeEndDocument(); 0113 } 0114 0115 bool DocPropsApp::loadFromXmlFile(QIODevice *device) 0116 { 0117 QXmlStreamReader reader(device); 0118 while (!reader.atEnd()) { 0119 QXmlStreamReader::TokenType token = reader.readNext(); 0120 if (token == QXmlStreamReader::StartElement) { 0121 if (reader.name() == QLatin1String("Properties")) 0122 continue; 0123 0124 if (reader.name() == QStringLiteral("Manager")) { 0125 setProperty(QStringLiteral("manager"), reader.readElementText()); 0126 } else if (reader.name() == QStringLiteral("Company")) { 0127 setProperty(QStringLiteral("company"), reader.readElementText()); 0128 } 0129 } 0130 0131 if (reader.hasError()) { 0132 qDebug("Error when read doc props app file."); 0133 } 0134 } 0135 return true; 0136 } 0137 0138 QT_END_NAMESPACE_XLSX