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 "xlsxcontenttypes_p.h" 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: ContentTypes::ContentTypes(CreateFlag flag) andre@1: :AbstractOOXmlFile(flag) andre@1: { andre@1: m_package_prefix = QStringLiteral("application/vnd.openxmlformats-package."); andre@1: m_document_prefix = QStringLiteral("application/vnd.openxmlformats-officedocument."); andre@1: andre@1: m_defaults.insert(QStringLiteral("rels"), m_package_prefix + QStringLiteral("relationships+xml")); andre@1: m_defaults.insert(QStringLiteral("xml"), QStringLiteral("application/xml")); andre@1: } andre@1: andre@1: void ContentTypes::addDefault(const QString &key, const QString &value) andre@1: { andre@1: m_defaults.insert(key, value); andre@1: } andre@1: andre@1: void ContentTypes::addOverride(const QString &key, const QString &value) andre@1: { andre@1: m_overrides.insert(key, value); andre@1: } andre@1: andre@1: void ContentTypes::addDocPropApp() andre@1: { andre@1: addOverride(QStringLiteral("/docProps/app.xml"), m_document_prefix + QStringLiteral("extended-properties+xml")); andre@1: } andre@1: andre@1: void ContentTypes::addDocPropCore() andre@1: { andre@1: addOverride(QStringLiteral("/docProps/core.xml"), m_package_prefix + QStringLiteral("core-properties+xml")); andre@1: } andre@1: andre@1: void ContentTypes::addStyles() andre@1: { andre@1: addOverride(QStringLiteral("/xl/styles.xml"), m_document_prefix + QStringLiteral("spreadsheetml.styles+xml")); andre@1: } andre@1: andre@1: void ContentTypes::addTheme() andre@1: { andre@1: addOverride(QStringLiteral("/xl/theme/theme1.xml"), m_document_prefix + QStringLiteral("theme+xml")); andre@1: } andre@1: andre@1: void ContentTypes::addWorkbook() andre@1: { andre@1: addOverride(QStringLiteral("/xl/workbook.xml"), m_document_prefix + QStringLiteral("spreadsheetml.sheet.main+xml")); andre@1: } andre@1: andre@1: void ContentTypes::addWorksheetName(const QString &name) andre@1: { andre@1: addOverride(QStringLiteral("/xl/worksheets/%1.xml").arg(name), m_document_prefix + QStringLiteral("spreadsheetml.worksheet+xml")); andre@1: } andre@1: andre@1: void ContentTypes::addChartsheetName(const QString &name) andre@1: { andre@1: addOverride(QStringLiteral("/xl/chartsheets/%1.xml").arg(name), m_document_prefix + QStringLiteral("spreadsheetml.chartsheet+xml")); andre@1: } andre@1: andre@1: void ContentTypes::addDrawingName(const QString &name) andre@1: { andre@1: addOverride(QStringLiteral("/xl/drawings/%1.xml").arg(name), m_document_prefix + QStringLiteral("drawing+xml")); andre@1: } andre@1: andre@1: void ContentTypes::addChartName(const QString &name) andre@1: { andre@1: addOverride(QStringLiteral("/xl/charts/%1.xml").arg(name), m_document_prefix + QStringLiteral("drawingml.chart+xml")); andre@1: } andre@1: andre@1: void ContentTypes::addCommentName(const QString &name) andre@1: { andre@1: addOverride(QStringLiteral("/xl/%1.xml").arg(name), m_document_prefix + QStringLiteral("spreadsheetml.comments+xml")); andre@1: } andre@1: andre@1: void ContentTypes::addTableName(const QString &name) andre@1: { andre@1: addOverride(QStringLiteral("/xl/tables/%1.xml").arg(name), m_document_prefix + QStringLiteral("spreadsheetml.table+xml")); andre@1: } andre@1: andre@1: void ContentTypes::addExternalLinkName(const QString &name) andre@1: { andre@1: addOverride(QStringLiteral("/xl/externalLinks/%1.xml").arg(name), m_document_prefix + QStringLiteral("spreadsheetml.externalLink+xml")); andre@1: } andre@1: andre@1: void ContentTypes::addSharedString() andre@1: { andre@1: addOverride(QStringLiteral("/xl/sharedStrings.xml"), m_document_prefix + QStringLiteral("spreadsheetml.sharedStrings+xml")); andre@1: } andre@1: andre@1: void ContentTypes::addVmlName() andre@1: { andre@1: addOverride(QStringLiteral("vml"), m_document_prefix + QStringLiteral("vmlDrawing")); andre@1: } andre@1: andre@1: void ContentTypes::addCalcChain() andre@1: { andre@1: addOverride(QStringLiteral("/xl/calcChain.xml"), m_document_prefix + QStringLiteral("spreadsheetml.calcChain+xml")); andre@1: } andre@1: andre@1: void ContentTypes::addVbaProject() andre@1: { andre@1: //:TODO andre@1: addOverride(QStringLiteral("bin"), QStringLiteral("application/vnd.ms-office.vbaProject")); andre@1: } andre@1: andre@1: void ContentTypes::clearOverrides() andre@1: { andre@1: m_overrides.clear(); andre@1: } andre@1: andre@1: void ContentTypes::saveToXmlFile(QIODevice *device) const andre@1: { andre@1: QXmlStreamWriter writer(device); andre@1: andre@1: writer.writeStartDocument(QStringLiteral("1.0"), true); andre@1: writer.writeStartElement(QStringLiteral("Types")); andre@1: writer.writeAttribute(QStringLiteral("xmlns"), QStringLiteral("http://schemas.openxmlformats.org/package/2006/content-types")); andre@1: andre@1: { andre@1: QMapIterator it(m_defaults); andre@1: while (it.hasNext()) { andre@1: it.next(); andre@1: writer.writeStartElement(QStringLiteral("Default")); andre@1: writer.writeAttribute(QStringLiteral("Extension"), it.key()); andre@1: writer.writeAttribute(QStringLiteral("ContentType"), it.value()); andre@1: writer.writeEndElement();//Default andre@1: } andre@1: } andre@1: andre@1: { andre@1: QMapIterator it(m_overrides); andre@1: while (it.hasNext()) { andre@1: it.next(); andre@1: writer.writeStartElement(QStringLiteral("Override")); andre@1: writer.writeAttribute(QStringLiteral("PartName"), it.key()); andre@1: writer.writeAttribute(QStringLiteral("ContentType"), it.value()); andre@1: writer.writeEndElement(); //Override andre@1: } andre@1: } andre@1: andre@1: writer.writeEndElement();//Types andre@1: writer.writeEndDocument(); andre@1: andre@1: } andre@1: andre@1: bool ContentTypes::loadFromXmlFile(QIODevice *device) andre@1: { andre@1: m_defaults.clear(); andre@1: m_overrides.clear(); 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("Default")) { andre@1: QXmlStreamAttributes attrs = reader.attributes(); andre@1: QString extension = attrs.value(QLatin1String("Extension")).toString(); andre@1: QString type = attrs.value(QLatin1String("ContentType")).toString(); andre@1: m_defaults.insert(extension, type); andre@1: } else if (reader.name() == QLatin1String("Override")) { andre@1: QXmlStreamAttributes attrs = reader.attributes(); andre@1: QString partName = attrs.value(QLatin1String("PartName")).toString(); andre@1: QString type = attrs.value(QLatin1String("ContentType")).toString(); andre@1: m_overrides.insert(partName, type); andre@1: } andre@1: } andre@1: andre@1: if (reader.hasError()) { andre@1: qDebug()<