andre@1: /**************************************************************************** andre@1: ** Copyright (c) 2013-2014 Debao Zhang andre@1: ** All right reserved. andre@1: ** andre@1: ** Permission is hereby granted, free of charge, to any person obtaining andre@1: ** a copy of this software and associated documentation files (the andre@1: ** "Software"), to deal in the Software without restriction, including andre@1: ** without limitation the rights to use, copy, modify, merge, publish, andre@1: ** distribute, sublicense, and/or sell copies of the Software, and to andre@1: ** permit persons to whom the Software is furnished to do so, subject to andre@1: ** the following conditions: andre@1: ** andre@1: ** The above copyright notice and this permission notice shall be andre@1: ** included in all copies or substantial portions of the Software. andre@1: ** andre@1: ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, andre@1: ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF andre@1: ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND andre@1: ** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE andre@1: ** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION andre@1: ** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION andre@1: ** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. andre@1: ** andre@1: ****************************************************************************/ andre@1: #include "xlsxdocpropsapp_p.h" andre@1: andre@1: #include andre@1: #include andre@1: #include andre@1: #include andre@1: #include andre@1: #include andre@1: #include andre@1: andre@1: namespace QXlsx { andre@1: andre@1: DocPropsApp::DocPropsApp(CreateFlag flag) andre@1: :AbstractOOXmlFile(flag) andre@1: { andre@1: } andre@1: andre@1: void DocPropsApp::addPartTitle(const QString &title) andre@1: { andre@1: m_titlesOfPartsList.append(title); andre@1: } andre@1: andre@1: void DocPropsApp::addHeadingPair(const QString &name, int value) andre@1: { andre@1: m_headingPairsList.append(qMakePair(name, value)); andre@1: } andre@1: andre@1: bool DocPropsApp::setProperty(const QString &name, const QString &value) andre@1: { andre@1: static QStringList validKeys; andre@1: if (validKeys.isEmpty()) { andre@1: validKeys << QStringLiteral("manager") << QStringLiteral("company"); andre@1: } andre@1: andre@1: if (!validKeys.contains(name)) andre@1: return false; andre@1: andre@1: if (value.isEmpty()) andre@1: m_properties.remove(name); andre@1: else andre@1: m_properties[name] = value; andre@1: andre@1: return true; andre@1: } andre@1: andre@1: QString DocPropsApp::property(const QString &name) const andre@1: { andre@1: if (m_properties.contains(name)) andre@1: return m_properties[name]; andre@1: andre@1: return QString(); andre@1: } andre@1: andre@1: QStringList DocPropsApp::propertyNames() const andre@1: { andre@1: return m_properties.keys(); andre@1: } andre@1: andre@1: void DocPropsApp::saveToXmlFile(QIODevice *device) const andre@1: { andre@1: QXmlStreamWriter writer(device); andre@1: QString vt = QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"); andre@1: andre@1: writer.writeStartDocument(QStringLiteral("1.0"), true); andre@1: writer.writeStartElement(QStringLiteral("Properties")); andre@1: writer.writeDefaultNamespace(QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/extended-properties")); andre@1: writer.writeNamespace(vt, QStringLiteral("vt")); andre@1: writer.writeTextElement(QStringLiteral("Application"), QStringLiteral("Microsoft Excel")); andre@1: writer.writeTextElement(QStringLiteral("DocSecurity"), QStringLiteral("0")); andre@1: writer.writeTextElement(QStringLiteral("ScaleCrop"), QStringLiteral("false")); andre@1: andre@1: writer.writeStartElement(QStringLiteral("HeadingPairs")); andre@1: writer.writeStartElement(vt, QStringLiteral("vector")); andre@1: writer.writeAttribute(QStringLiteral("size"), QString::number(m_headingPairsList.size()*2)); andre@1: writer.writeAttribute(QStringLiteral("baseType"), QStringLiteral("variant")); andre@1: typedef QPair PairType; //Make foreach happy andre@1: foreach (PairType pair, m_headingPairsList) { andre@1: writer.writeStartElement(vt, QStringLiteral("variant")); andre@1: writer.writeTextElement(vt, QStringLiteral("lpstr"), pair.first); andre@1: writer.writeEndElement(); //vt:variant andre@1: writer.writeStartElement(vt, QStringLiteral("variant")); andre@1: writer.writeTextElement(vt, QStringLiteral("i4"), QString::number(pair.second)); andre@1: writer.writeEndElement(); //vt:variant andre@1: } andre@1: writer.writeEndElement();//vt:vector andre@1: writer.writeEndElement();//HeadingPairs andre@1: andre@1: writer.writeStartElement(QStringLiteral("TitlesOfParts")); andre@1: writer.writeStartElement(vt, QStringLiteral("vector")); andre@1: writer.writeAttribute(QStringLiteral("size"), QString::number(m_titlesOfPartsList.size())); andre@1: writer.writeAttribute(QStringLiteral("baseType"), QStringLiteral("lpstr")); andre@1: foreach (QString title, m_titlesOfPartsList) andre@1: writer.writeTextElement(vt, QStringLiteral("lpstr"), title); andre@1: writer.writeEndElement();//vt:vector andre@1: writer.writeEndElement();//TitlesOfParts andre@1: andre@1: if (m_properties.contains(QStringLiteral("manager"))) andre@1: writer.writeTextElement(QStringLiteral("Manager"), m_properties[QStringLiteral("manager")]); andre@1: //Not like "manager", "company" always exists for Excel generated file. andre@1: writer.writeTextElement(QStringLiteral("Company"), m_properties.contains(QStringLiteral("company")) ? m_properties[QStringLiteral("company")]: QString()); andre@1: writer.writeTextElement(QStringLiteral("LinksUpToDate"), QStringLiteral("false")); andre@1: writer.writeTextElement(QStringLiteral("SharedDoc"), QStringLiteral("false")); andre@1: writer.writeTextElement(QStringLiteral("HyperlinksChanged"), QStringLiteral("false")); andre@1: writer.writeTextElement(QStringLiteral("AppVersion"), QStringLiteral("12.0000")); andre@1: andre@1: writer.writeEndElement(); //Properties andre@1: writer.writeEndDocument(); andre@1: } andre@1: andre@1: bool DocPropsApp::loadFromXmlFile(QIODevice *device) andre@1: { andre@1: QXmlStreamReader reader(device); andre@1: while (!reader.atEnd()) { andre@1: QXmlStreamReader::TokenType token = reader.readNext(); andre@1: if (token == QXmlStreamReader::StartElement) { andre@1: if (reader.name() == QLatin1String("Properties")) andre@1: continue; andre@1: andre@1: if (reader.name() == QStringLiteral("Manager")) { andre@1: setProperty(QStringLiteral("manager"), reader.readElementText()); andre@1: } else if (reader.name() == QStringLiteral("Company")) { andre@1: setProperty(QStringLiteral("company"), reader.readElementText()); andre@1: } andre@1: } andre@1: andre@1: if (reader.hasError()) { andre@1: qDebug("Error when read doc props app file."); andre@1: } andre@1: } andre@1: return true; andre@1: } andre@1: andre@1: } //namespace