Able to read the document properties back now!
This commit is contained in:
+118
-18
@@ -24,18 +24,54 @@
|
||||
****************************************************************************/
|
||||
#include "xlsxdocpropscore_p.h"
|
||||
#include "xlsxxmlwriter_p.h"
|
||||
#include "xlsxxmlreader_p.h"
|
||||
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QDateTime>
|
||||
#include <QVariant>
|
||||
#include <QDebug>
|
||||
#include <QBuffer>
|
||||
|
||||
namespace QXlsx {
|
||||
|
||||
DocPropsCore::DocPropsCore(QObject *parent) :
|
||||
QObject(parent)
|
||||
DocPropsCore::DocPropsCore()
|
||||
{
|
||||
}
|
||||
|
||||
bool DocPropsCore::setProperty(const QString &name, const QString &value)
|
||||
{
|
||||
static QStringList validKeys;
|
||||
if (validKeys.isEmpty()) {
|
||||
validKeys << QStringLiteral("title") << QStringLiteral("subject")
|
||||
<< QStringLiteral("keywords") << QStringLiteral("description")
|
||||
<< QStringLiteral("category") << QStringLiteral("status")
|
||||
<< QStringLiteral("created") << QStringLiteral("creator");
|
||||
}
|
||||
|
||||
if (!validKeys.contains(name))
|
||||
return false;
|
||||
|
||||
if (value.isEmpty())
|
||||
m_properties.remove(name);
|
||||
else
|
||||
m_properties[name] = value;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
QString DocPropsCore::property(const QString &name) const
|
||||
{
|
||||
if (m_properties.contains(name))
|
||||
return m_properties[name];
|
||||
|
||||
return QString();
|
||||
}
|
||||
|
||||
QStringList DocPropsCore::propertyNames() const
|
||||
{
|
||||
return m_properties.keys();
|
||||
}
|
||||
|
||||
void DocPropsCore::saveToXmlFile(QIODevice *device)
|
||||
{
|
||||
XmlStreamWriter writer(device);
|
||||
@@ -47,21 +83,26 @@ void DocPropsCore::saveToXmlFile(QIODevice *device)
|
||||
writer.writeAttribute(QStringLiteral("xmlns:dcterms"), QStringLiteral("http://purl.org/dc/terms/"));
|
||||
writer.writeAttribute(QStringLiteral("xmlns:dcmitype"), QStringLiteral("http://purl.org/dc/dcmitype/"));
|
||||
writer.writeAttribute(QStringLiteral("xmlns:xsi"), QStringLiteral("http://www.w3.org/2001/XMLSchema-instance"));
|
||||
if (property("title").isValid())
|
||||
writer.writeTextElement(QStringLiteral("dc:title"), property("title").toString());
|
||||
if (property("subject").isValid())
|
||||
writer.writeTextElement(QStringLiteral("dc:subject"), property("subject").toString());
|
||||
writer.writeTextElement(QStringLiteral("dc:creator"), property("creator").isValid() ? property("creator").toString() : QStringLiteral("Qt Xlsx Library"));
|
||||
|
||||
if (property("keywords").isValid())
|
||||
writer.writeTextElement(QStringLiteral("cp:keywords"), property("keywords").toString());
|
||||
if (property("description").isValid())
|
||||
writer.writeTextElement(QStringLiteral("dc:description"), property("description").toString());
|
||||
writer.writeTextElement(QStringLiteral("cp:lastModifiedBy"), property("creator").isValid() ? property("creator").toString() : QStringLiteral("Qt Xlsx Library"));
|
||||
if (m_properties.contains(QStringLiteral("title")))
|
||||
writer.writeTextElement(QStringLiteral("dc:title"), m_properties[QStringLiteral("title")]);
|
||||
|
||||
if (m_properties.contains(QStringLiteral("subject")))
|
||||
writer.writeTextElement(QStringLiteral("dc:subject"), m_properties[QStringLiteral("subject")]);
|
||||
|
||||
writer.writeTextElement(QStringLiteral("dc:creator"), m_properties.contains(QStringLiteral("creator")) ? m_properties[QStringLiteral("creator")] : QStringLiteral("Qt Xlsx Library"));
|
||||
|
||||
if (m_properties.contains(QStringLiteral("keywords")))
|
||||
writer.writeTextElement(QStringLiteral("cp:keywords"), m_properties[QStringLiteral("keywords")]);
|
||||
|
||||
if (m_properties.contains(QStringLiteral("description")))
|
||||
writer.writeTextElement(QStringLiteral("dc:description"), m_properties[QStringLiteral("description")]);
|
||||
|
||||
writer.writeTextElement(QStringLiteral("cp:lastModifiedBy"), m_properties.contains(QStringLiteral("creator")) ? m_properties[QStringLiteral("creator")] : QStringLiteral("Qt Xlsx Library"));
|
||||
|
||||
writer.writeStartElement(QStringLiteral("dcterms:created"));
|
||||
writer.writeAttribute(QStringLiteral("xsi:type"), QStringLiteral("dcterms:W3CDTF"));
|
||||
writer.writeCharacters(QDateTime::currentDateTime().toString(Qt::ISODate));
|
||||
writer.writeCharacters(m_properties.contains(QStringLiteral("created")) ? m_properties[QStringLiteral("created")] : QDateTime::currentDateTime().toString(Qt::ISODate));
|
||||
writer.writeEndElement();//dcterms:created
|
||||
|
||||
writer.writeStartElement(QStringLiteral("dcterms:modified"));
|
||||
@@ -69,12 +110,71 @@ void DocPropsCore::saveToXmlFile(QIODevice *device)
|
||||
writer.writeCharacters(QDateTime::currentDateTime().toString(Qt::ISODate));
|
||||
writer.writeEndElement();//dcterms:created
|
||||
|
||||
if (property("category").isValid())
|
||||
writer.writeTextElement(QStringLiteral("cp:category"), property("category").toString());
|
||||
if (property("status").isValid())
|
||||
writer.writeTextElement(QStringLiteral("cp:contentStatus"), property("status").toString());
|
||||
if (m_properties.contains(QStringLiteral("category")))
|
||||
writer.writeTextElement(QStringLiteral("cp:category"), m_properties[QStringLiteral("category")]);
|
||||
|
||||
if (m_properties.contains(QStringLiteral("status")))
|
||||
writer.writeTextElement(QStringLiteral("cp:contentStatus"), m_properties[QStringLiteral("status")]);
|
||||
|
||||
writer.writeEndElement(); //cp:coreProperties
|
||||
writer.writeEndDocument();
|
||||
}
|
||||
|
||||
QByteArray DocPropsCore::saveToXmlData()
|
||||
{
|
||||
QByteArray data;
|
||||
QBuffer buffer(&data);
|
||||
buffer.open(QIODevice::WriteOnly);
|
||||
saveToXmlFile(&buffer);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
DocPropsCore DocPropsCore::loadFromXmlFile(QIODevice *device)
|
||||
{
|
||||
DocPropsCore props;
|
||||
XmlStreamReader reader(device);
|
||||
while(!reader.atEnd()) {
|
||||
QXmlStreamReader::TokenType token = reader.readNext();
|
||||
if (token == QXmlStreamReader::StartElement) {
|
||||
if (reader.qualifiedName() == QLatin1String("cp:coreProperties"))
|
||||
continue;
|
||||
|
||||
QString text = reader.readElementText();
|
||||
if (reader.qualifiedName() == QStringLiteral("dc:subject")) {
|
||||
props.setProperty(QStringLiteral("subject"), text);
|
||||
} else if (reader.qualifiedName() == QStringLiteral("dc:title")) {
|
||||
props.setProperty(QStringLiteral("title"), text);
|
||||
} else if (reader.qualifiedName() == QStringLiteral("dc:creator")) {
|
||||
props.setProperty(QStringLiteral("creator"), text);
|
||||
} else if (reader.qualifiedName() == QStringLiteral("dc:description")) {
|
||||
props.setProperty(QStringLiteral("description"), text);
|
||||
} else if (reader.qualifiedName() == QStringLiteral("cp:keywords")) {
|
||||
props.setProperty(QStringLiteral("keywords"), text);
|
||||
} else if (reader.qualifiedName() == QStringLiteral("dcterms:created")) {
|
||||
props.setProperty(QStringLiteral("created"), text);
|
||||
} else if (reader.qualifiedName() == QStringLiteral("cp:category")) {
|
||||
props.setProperty(QStringLiteral("category"), text);
|
||||
} else if (reader.qualifiedName() == QStringLiteral("cp:contentStatus")) {
|
||||
props.setProperty(QStringLiteral("status"), text);
|
||||
}
|
||||
}
|
||||
|
||||
if (reader.hasError()) {
|
||||
qDebug()<<"Error when read doc props core file.";
|
||||
|
||||
}
|
||||
}
|
||||
return props;
|
||||
}
|
||||
|
||||
DocPropsCore DocPropsCore::loadFromXmlData(const QByteArray &data)
|
||||
{
|
||||
QBuffer buffer;
|
||||
buffer.setData(data);
|
||||
buffer.open(QIODevice::ReadOnly);
|
||||
return loadFromXmlFile(&buffer);
|
||||
}
|
||||
|
||||
|
||||
} //namespace
|
||||
|
||||
Reference in New Issue
Block a user