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