File indexing completed on 2025-01-05 04:47:12
0001 /* 0002 SPDX-FileCopyrightText: 2009 Volker Krause <vkrause@kde.org> 0003 SPDX-FileCopyrightText: 2009 Igor Trindade Oliveira <igor_trindade@yahoo.com.br> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "xmlwriter.h" 0009 #include "format_p.h" 0010 0011 #include "attribute.h" 0012 #include "collection.h" 0013 #include "item.h" 0014 0015 using namespace Akonadi; 0016 0017 QDomElement XmlWriter::attributeToElement(Attribute *attr, QDomDocument &document) 0018 { 0019 if (document.isNull()) { 0020 return QDomElement(); 0021 } 0022 0023 QDomElement top = document.createElement(Format::Tag::attribute()); 0024 top.setAttribute(Format::Attr::attributeType(), QString::fromUtf8(attr->type())); 0025 QDomText attrText = document.createTextNode(QString::fromUtf8(attr->serialized())); 0026 top.appendChild(attrText); 0027 0028 return top; 0029 } 0030 0031 template<typename T> 0032 static void writeAttributesImpl(const T &entity, QDomElement &parentElem) 0033 { 0034 if (parentElem.isNull()) { 0035 return; 0036 } 0037 0038 QDomDocument doc = parentElem.ownerDocument(); 0039 const auto attributes = entity.attributes(); 0040 for (Attribute *attr : attributes) { 0041 parentElem.appendChild(XmlWriter::attributeToElement(attr, doc)); 0042 } 0043 } 0044 0045 void XmlWriter::writeAttributes(const Item &item, QDomElement &parentElem) 0046 { 0047 writeAttributesImpl(item, parentElem); 0048 } 0049 0050 void XmlWriter::writeAttributes(const Collection &collection, QDomElement &parentElem) 0051 { 0052 writeAttributesImpl(collection, parentElem); 0053 } 0054 0055 QDomElement XmlWriter::collectionToElement(const Akonadi::Collection &collection, QDomDocument &document) 0056 { 0057 if (document.isNull()) { 0058 return QDomElement(); 0059 } 0060 0061 QDomElement top = document.createElement(Format::Tag::collection()); 0062 top.setAttribute(Format::Attr::remoteId(), collection.remoteId()); 0063 top.setAttribute(Format::Attr::collectionName(), collection.name()); 0064 top.setAttribute(Format::Attr::collectionContentTypes(), collection.contentMimeTypes().join(QLatin1Char(','))); 0065 writeAttributes(collection, top); 0066 0067 return top; 0068 } 0069 0070 QDomElement XmlWriter::writeCollection(const Akonadi::Collection &collection, QDomElement &parentElem) 0071 { 0072 if (parentElem.isNull()) { 0073 return QDomElement(); 0074 } 0075 0076 QDomDocument doc = parentElem.ownerDocument(); 0077 const QDomElement elem = collectionToElement(collection, doc); 0078 parentElem.insertBefore(elem, QDomNode()); // collection need to be before items to pass schema validation 0079 return elem; 0080 } 0081 0082 QDomElement XmlWriter::itemToElement(const Akonadi::Item &item, QDomDocument &document) 0083 { 0084 if (document.isNull()) { 0085 return QDomElement(); 0086 } 0087 0088 QDomElement top = document.createElement(Format::Tag::item()); 0089 top.setAttribute(Format::Attr::remoteId(), item.remoteId()); 0090 top.setAttribute(Format::Attr::itemMimeType(), item.mimeType()); 0091 0092 if (item.hasPayload()) { 0093 QDomElement payloadElem = document.createElement(Format::Tag::payload()); 0094 QDomText payloadText = document.createTextNode(QString::fromUtf8(item.payloadData())); 0095 payloadElem.appendChild(payloadText); 0096 top.appendChild(payloadElem); 0097 } 0098 0099 writeAttributes(item, top); 0100 0101 const auto flags = item.flags(); 0102 for (const Item::Flag &flag : flags) { 0103 QDomElement flagElem = document.createElement(Format::Tag::flag()); 0104 QDomText flagText = document.createTextNode(QString::fromUtf8(flag)); 0105 flagElem.appendChild(flagText); 0106 top.appendChild(flagElem); 0107 } 0108 0109 return top; 0110 } 0111 0112 QDomElement XmlWriter::writeItem(const Item &item, QDomElement &parentElem) 0113 { 0114 if (parentElem.isNull()) { 0115 return QDomElement(); 0116 } 0117 0118 QDomDocument doc = parentElem.ownerDocument(); 0119 const QDomElement elem = itemToElement(item, doc); 0120 parentElem.appendChild(elem); 0121 return elem; 0122 }