Browse Source

Code refactoring: Remove magic things for ContentTypes

master
Debao Zhang 11 years ago
parent
commit
86046c48dc
  1. 37
      src/xlsx/xlsxcontenttypes.cpp
  2. 6
      src/xlsx/xlsxcontenttypes_p.h
  3. 17
      src/xlsx/xlsxdocument.cpp
  4. 2
      src/xlsx/xlsxdocument_p.h

37
src/xlsx/xlsxcontenttypes.cpp

@ -39,12 +39,6 @@ ContentTypes::ContentTypes()
m_defaults.insert(QStringLiteral("rels"), m_package_prefix + QStringLiteral("relationships+xml")); m_defaults.insert(QStringLiteral("rels"), m_package_prefix + QStringLiteral("relationships+xml"));
m_defaults.insert(QStringLiteral("xml"), QStringLiteral("application/xml")); m_defaults.insert(QStringLiteral("xml"), QStringLiteral("application/xml"));
m_overrides.insert(QStringLiteral("/docProps/app.xml"), m_document_prefix + QStringLiteral("extended-properties+xml"));
m_overrides.insert(QStringLiteral("/docProps/core.xml"), m_package_prefix + QStringLiteral("core-properties+xml"));
m_overrides.insert(QStringLiteral("/xl/styles.xml"), m_document_prefix + QStringLiteral("spreadsheetml.styles+xml"));
m_overrides.insert(QStringLiteral("/xl/theme/theme1.xml"), m_document_prefix + QStringLiteral("theme+xml"));
m_overrides.insert(QStringLiteral("/xl/workbook.xml"), m_document_prefix + QStringLiteral("spreadsheetml.sheet.main+xml"));
} }
void ContentTypes::addDefault(const QString &key, const QString &value) void ContentTypes::addDefault(const QString &key, const QString &value)
@ -57,6 +51,31 @@ void ContentTypes::addOverride(const QString &key, const QString &value)
m_overrides.insert(key, value); m_overrides.insert(key, value);
} }
void ContentTypes::addDocPropApp()
{
addOverride(QStringLiteral("/docProps/app.xml"), m_document_prefix + QStringLiteral("extended-properties+xml"));
}
void ContentTypes::addDocPropCore()
{
addOverride(QStringLiteral("/docProps/core.xml"), m_package_prefix + QStringLiteral("core-properties+xml"));
}
void ContentTypes::addStyles()
{
addOverride(QStringLiteral("/xl/styles.xml"), m_document_prefix + QStringLiteral("spreadsheetml.styles+xml"));
}
void ContentTypes::addTheme()
{
addOverride(QStringLiteral("/xl/theme/theme1.xml"), m_document_prefix + QStringLiteral("theme+xml"));
}
void ContentTypes::addWorkbook()
{
addOverride(QStringLiteral("/xl/workbook.xml"), m_document_prefix + QStringLiteral("spreadsheetml.sheet.main+xml"));
}
void ContentTypes::addWorksheetName(const QString &name) void ContentTypes::addWorksheetName(const QString &name)
{ {
addOverride(QStringLiteral("/xl/worksheets/%1.xml").arg(name), m_document_prefix + QStringLiteral("spreadsheetml.worksheet+xml")); addOverride(QStringLiteral("/xl/worksheets/%1.xml").arg(name), m_document_prefix + QStringLiteral("spreadsheetml.worksheet+xml"));
@ -82,12 +101,6 @@ void ContentTypes::addCommentName(const QString &name)
addOverride(QStringLiteral("/xl/%1.xml").arg(name), m_document_prefix + QStringLiteral("spreadsheetml.comments+xml")); addOverride(QStringLiteral("/xl/%1.xml").arg(name), m_document_prefix + QStringLiteral("spreadsheetml.comments+xml"));
} }
void ContentTypes::addImageTypes(const QStringList &imageTypes)
{
foreach (QString type, imageTypes)
addDefault(type, QStringLiteral("image/") + type);
}
void ContentTypes::addTableName(const QString &name) void ContentTypes::addTableName(const QString &name)
{ {
addOverride(QStringLiteral("/xl/tables/%1.xml").arg(name), m_document_prefix + QStringLiteral("spreadsheetml.table+xml")); addOverride(QStringLiteral("/xl/tables/%1.xml").arg(name), m_document_prefix + QStringLiteral("spreadsheetml.table+xml"));

6
src/xlsx/xlsxcontenttypes_p.h

@ -53,12 +53,16 @@ public:
void addOverride(const QString &key, const QString &value); void addOverride(const QString &key, const QString &value);
//Convenient funcation for addOverride() //Convenient funcation for addOverride()
void addDocPropCore();
void addDocPropApp();
void addStyles();
void addTheme();
void addWorkbook();
void addWorksheetName(const QString &name); void addWorksheetName(const QString &name);
void addChartsheetName(const QString &name); void addChartsheetName(const QString &name);
void addChartName(const QString &name); void addChartName(const QString &name);
void addDrawingName(const QString &name); void addDrawingName(const QString &name);
void addCommentName(const QString &name); void addCommentName(const QString &name);
void addImageTypes(const QStringList &imageTypes);
void addTableName(const QString &name); void addTableName(const QString &name);
void addSharedString(); void addSharedString();
void addVmlName(); void addVmlName();

17
src/xlsx/xlsxdocument.cpp

@ -99,9 +99,14 @@ bool DocumentPrivate::loadPackage(QIODevice *device)
ZipReader zipReader(device); ZipReader zipReader(device);
QStringList filePaths = zipReader.filePaths(); QStringList filePaths = zipReader.filePaths();
if (!filePaths.contains(QLatin1String("_rels/.rels"))) //Load the Content_Types file
if (!filePaths.contains(QLatin1String("[Content_Types].xml")))
return false; return false;
contentTypes.loadFromXmlData(zipReader.fileData(QStringLiteral("[Content_Types].xml")));
//Load root rels file
if (!filePaths.contains(QLatin1String("_rels/.rels")))
return false;
Relationships rootRels; Relationships rootRels;
rootRels.loadFromXmlData(zipReader.fileData(QStringLiteral("_rels/.rels"))); rootRels.loadFromXmlData(zipReader.fileData(QStringLiteral("_rels/.rels")));
@ -196,7 +201,8 @@ bool DocumentPrivate::savePackage(QIODevice *device) const
if (zipWriter.error()) if (zipWriter.error())
return false; return false;
ContentTypes contentTypes; contentTypes.clearOverrides();
DocPropsApp docPropsApp; DocPropsApp docPropsApp;
DocPropsCore docPropsCore; DocPropsCore docPropsCore;
@ -216,6 +222,7 @@ bool DocumentPrivate::savePackage(QIODevice *device) const
} }
// save workbook xml file // save workbook xml file
contentTypes.addWorkbook();
zipWriter.addFile(QStringLiteral("xl/workbook.xml"), workbook->saveToXmlData()); zipWriter.addFile(QStringLiteral("xl/workbook.xml"), workbook->saveToXmlData());
zipWriter.addFile(QStringLiteral("xl/_rels/workbook.xml.rels"), workbook->relationships().saveToXmlData()); zipWriter.addFile(QStringLiteral("xl/_rels/workbook.xml.rels"), workbook->relationships().saveToXmlData());
@ -247,6 +254,8 @@ bool DocumentPrivate::savePackage(QIODevice *device) const
} }
if (workbook->worksheetCount()) if (workbook->worksheetCount())
docPropsApp.addHeadingPair(QStringLiteral("Worksheets"), workbook->worksheetCount()); docPropsApp.addHeadingPair(QStringLiteral("Worksheets"), workbook->worksheetCount());
contentTypes.addDocPropApp();
contentTypes.addDocPropCore();
zipWriter.addFile(QStringLiteral("docProps/app.xml"), docPropsApp.saveToXmlData()); zipWriter.addFile(QStringLiteral("docProps/app.xml"), docPropsApp.saveToXmlData());
zipWriter.addFile(QStringLiteral("docProps/core.xml"), docPropsCore.saveToXmlData()); zipWriter.addFile(QStringLiteral("docProps/core.xml"), docPropsCore.saveToXmlData());
@ -257,14 +266,16 @@ bool DocumentPrivate::savePackage(QIODevice *device) const
} }
// save styles xml file // save styles xml file
contentTypes.addStyles();
zipWriter.addFile(QStringLiteral("xl/styles.xml"), workbook->styles()->saveToXmlData()); zipWriter.addFile(QStringLiteral("xl/styles.xml"), workbook->styles()->saveToXmlData());
// save theme xml file // save theme xml file
contentTypes.addTheme();
zipWriter.addFile(QStringLiteral("xl/theme/theme1.xml"), workbook->theme()->saveToXmlData()); zipWriter.addFile(QStringLiteral("xl/theme/theme1.xml"), workbook->theme()->saveToXmlData());
// save image files // save image files
if (!workbook->images().isEmpty()) if (!workbook->images().isEmpty())
contentTypes.addImageTypes(QStringList()<<QStringLiteral("png")); contentTypes.addDefault(QStringLiteral("png"), QStringLiteral("image/png"));
for (int i=0; i<workbook->images().size(); ++i) { for (int i=0; i<workbook->images().size(); ++i) {
QImage image = workbook->images()[i]; QImage image = workbook->images()[i];

2
src/xlsx/xlsxdocument_p.h

@ -39,6 +39,7 @@
#include "xlsxdocument.h" #include "xlsxdocument.h"
#include "xlsxworkbook.h" #include "xlsxworkbook.h"
#include "xlsxcontenttypes_p.h"
#include <QMap> #include <QMap>
@ -60,6 +61,7 @@ public:
QMap<QString, QString> documentProperties; //core, app and custom properties QMap<QString, QString> documentProperties; //core, app and custom properties
QSharedPointer<Workbook> workbook; QSharedPointer<Workbook> workbook;
mutable ContentTypes contentTypes;
}; };
} }

Loading…
Cancel
Save