Browse Source

Add load function for ContentTypes

master
Debao Zhang 11 years ago
parent
commit
945870596f
  1. 45
      src/xlsx/xlsxcontenttypes.cpp
  2. 4
      src/xlsx/xlsxcontenttypes_p.h

45
src/xlsx/xlsxcontenttypes.cpp

@ -24,9 +24,11 @@
****************************************************************************/
#include "xlsxcontenttypes_p.h"
#include <QXmlStreamWriter>
#include <QXmlStreamReader>
#include <QFile>
#include <QMapIterator>
#include <QBuffer>
#include <QDebug>
namespace QXlsx {
@ -112,6 +114,11 @@ void ContentTypes::addVbaProject()
addOverride(QStringLiteral("bin"), QStringLiteral("application/vnd.ms-office.vbaProject"));
}
void ContentTypes::clearOverrides()
{
m_overrides.clear();
}
QByteArray ContentTypes::saveToXmlData() const
{
QByteArray data;
@ -156,4 +163,42 @@ void ContentTypes::saveToXmlFile(QIODevice *device) const
}
bool ContentTypes::loadFromXmlFile(QIODevice *device)
{
m_defaults.clear();
m_overrides.clear();
QXmlStreamReader reader(device);
while (!reader.atEnd()) {
QXmlStreamReader::TokenType token = reader.readNext();
if (token == QXmlStreamReader::StartElement) {
if (reader.name() == QLatin1String("Default")) {
QXmlStreamAttributes attrs = reader.attributes();
QString extension = attrs.value(QLatin1String("Extension")).toString();
QString type = attrs.value(QLatin1String("ContentType")).toString();
m_defaults.insert(extension, type);
} else if (reader.name() == QLatin1String("Override")) {
QXmlStreamAttributes attrs = reader.attributes();
QString partName = attrs.value(QLatin1String("PartName")).toString();
QString type = attrs.value(QLatin1String("ContentType")).toString();
m_overrides.insert(partName, type);
}
}
if (reader.hasError()) {
qDebug()<<reader.errorString();
}
}
return true;
}
bool ContentTypes::loadFromXmlData(const QByteArray &data)
{
QBuffer buffer;
buffer.setData(data);
buffer.open(QIODevice::ReadOnly);
return loadFromXmlFile(&buffer);
}
} //namespace QXlsx

4
src/xlsx/xlsxcontenttypes_p.h

@ -65,8 +65,12 @@ public:
void addCalcChain();
void addVbaProject();
void clearOverrides();
QByteArray saveToXmlData() const;
void saveToXmlFile(QIODevice *device) const;
bool loadFromXmlFile(QIODevice *device);
bool loadFromXmlData(const QByteArray &data);
private:
QMap<QString, QString> m_defaults;

Loading…
Cancel
Save