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