comparison src/xlsx/xlsxdocpropscore.cpp @ 1:93d3106bb9a4

Add qt xlsx library
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 22 Mar 2016 10:38:08 +0100
parents
children
comparison
equal deleted inserted replaced
0:49cd5cc0b072 1:93d3106bb9a4
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
27 #include <QXmlStreamWriter>
28 #include <QXmlStreamReader>
29 #include <QDir>
30 #include <QFile>
31 #include <QDateTime>
32 #include <QDebug>
33 #include <QBuffer>
34
35 namespace QXlsx {
36
37 DocPropsCore::DocPropsCore(CreateFlag flag)
38 :AbstractOOXmlFile(flag)
39 {
40 }
41
42 bool DocPropsCore::setProperty(const QString &name, const QString &value)
43 {
44 static QStringList validKeys;
45 if (validKeys.isEmpty()) {
46 validKeys << QStringLiteral("title") << QStringLiteral("subject")
47 << QStringLiteral("keywords") << QStringLiteral("description")
48 << QStringLiteral("category") << QStringLiteral("status")
49 << QStringLiteral("created") << QStringLiteral("creator");
50 }
51
52 if (!validKeys.contains(name))
53 return false;
54
55 if (value.isEmpty())
56 m_properties.remove(name);
57 else
58 m_properties[name] = value;
59
60 return true;
61 }
62
63 QString DocPropsCore::property(const QString &name) const
64 {
65 if (m_properties.contains(name))
66 return m_properties[name];
67
68 return QString();
69 }
70
71 QStringList DocPropsCore::propertyNames() const
72 {
73 return m_properties.keys();
74 }
75
76 void DocPropsCore::saveToXmlFile(QIODevice *device) const
77 {
78 QXmlStreamWriter writer(device);
79 const QString cp = QStringLiteral("http://schemas.openxmlformats.org/package/2006/metadata/core-properties");
80 const QString dc = QStringLiteral("http://purl.org/dc/elements/1.1/");
81 const QString dcterms = QStringLiteral("http://purl.org/dc/terms/");
82 const QString dcmitype = QStringLiteral("http://purl.org/dc/dcmitype/");
83 const QString xsi = QStringLiteral("http://www.w3.org/2001/XMLSchema-instance");
84 writer.writeStartDocument(QStringLiteral("1.0"), true);
85 writer.writeStartElement(QStringLiteral("cp:coreProperties"));
86 writer.writeNamespace(cp, QStringLiteral("cp"));
87 writer.writeNamespace(dc, QStringLiteral("dc"));
88 writer.writeNamespace(dcterms, QStringLiteral("dcterms"));
89 writer.writeNamespace(dcmitype, QStringLiteral("dcmitype"));
90 writer.writeNamespace(xsi, QStringLiteral("xsi"));
91
92 if (m_properties.contains(QStringLiteral("title")))
93 writer.writeTextElement(dc, QStringLiteral("title"), m_properties[QStringLiteral("title")]);
94
95 if (m_properties.contains(QStringLiteral("subject")))
96 writer.writeTextElement(dc, QStringLiteral("subject"), m_properties[QStringLiteral("subject")]);
97
98 writer.writeTextElement(dc, QStringLiteral("creator"), m_properties.contains(QStringLiteral("creator")) ? m_properties[QStringLiteral("creator")] : QStringLiteral("Qt Xlsx Library"));
99
100 if (m_properties.contains(QStringLiteral("keywords")))
101 writer.writeTextElement(cp, QStringLiteral("keywords"), m_properties[QStringLiteral("keywords")]);
102
103 if (m_properties.contains(QStringLiteral("description")))
104 writer.writeTextElement(dc, QStringLiteral("description"), m_properties[QStringLiteral("description")]);
105
106 writer.writeTextElement(cp, QStringLiteral("lastModifiedBy"), m_properties.contains(QStringLiteral("creator")) ? m_properties[QStringLiteral("creator")] : QStringLiteral("Qt Xlsx Library"));
107
108 writer.writeStartElement(dcterms, QStringLiteral("created"));
109 writer.writeAttribute(xsi, QStringLiteral("type"), QStringLiteral("dcterms:W3CDTF"));
110 writer.writeCharacters(m_properties.contains(QStringLiteral("created")) ? m_properties[QStringLiteral("created")] : QDateTime::currentDateTime().toString(Qt::ISODate));
111 writer.writeEndElement();//dcterms:created
112
113 writer.writeStartElement(dcterms, QStringLiteral("modified"));
114 writer.writeAttribute(xsi, QStringLiteral("type"), QStringLiteral("dcterms:W3CDTF"));
115 writer.writeCharacters(QDateTime::currentDateTime().toString(Qt::ISODate));
116 writer.writeEndElement();//dcterms:created
117
118 if (m_properties.contains(QStringLiteral("category")))
119 writer.writeTextElement(cp, QStringLiteral("category"), m_properties[QStringLiteral("category")]);
120
121 if (m_properties.contains(QStringLiteral("status")))
122 writer.writeTextElement(cp, QStringLiteral("contentStatus"), m_properties[QStringLiteral("status")]);
123
124 writer.writeEndElement(); //cp:coreProperties
125 writer.writeEndDocument();
126 }
127
128 bool DocPropsCore::loadFromXmlFile(QIODevice *device)
129 {
130 QXmlStreamReader reader(device);
131
132 const QString cp = QStringLiteral("http://schemas.openxmlformats.org/package/2006/metadata/core-properties");
133 const QString dc = QStringLiteral("http://purl.org/dc/elements/1.1/");
134 const QString dcterms = QStringLiteral("http://purl.org/dc/terms/");
135
136 while (!reader.atEnd()) {
137 QXmlStreamReader::TokenType token = reader.readNext();
138 if (token == QXmlStreamReader::StartElement) {
139 const QStringRef nsUri = reader.namespaceUri();
140 const QStringRef name = reader.name();
141 if (name == QStringLiteral("subject") && nsUri == dc) {
142 setProperty(QStringLiteral("subject"), reader.readElementText());
143 } else if (name == QStringLiteral("title") && nsUri == dc) {
144 setProperty(QStringLiteral("title"), reader.readElementText());
145 } else if (name == QStringLiteral("creator") && nsUri == dc) {
146 setProperty(QStringLiteral("creator"), reader.readElementText());
147 } else if (name == QStringLiteral("description") && nsUri == dc) {
148 setProperty(QStringLiteral("description"), reader.readElementText());
149 } else if (name == QStringLiteral("keywords") && nsUri == cp) {
150 setProperty(QStringLiteral("keywords"), reader.readElementText());
151 } else if (name == QStringLiteral("created") && nsUri == dcterms) {
152 setProperty(QStringLiteral("created"), reader.readElementText());
153 } else if (name == QStringLiteral("category") && nsUri == cp) {
154 setProperty(QStringLiteral("category"), reader.readElementText());
155 } else if (name == QStringLiteral("contentStatus") && nsUri == cp) {
156 setProperty(QStringLiteral("status"), reader.readElementText());
157 }
158 }
159
160 if (reader.hasError()) {
161 qDebug()<<"Error when read doc props core file."<<reader.errorString();
162
163 }
164 }
165 return true;
166 }
167
168 } //namespace
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)