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 "xmlreader.h" 0009 #include "format_p.h" 0010 0011 #include "attributefactory.h" 0012 #include "tag.h" 0013 0014 using namespace Akonadi; 0015 0016 Attribute *XmlReader::elementToAttribute(const QDomElement &elem) 0017 { 0018 if (elem.isNull() || elem.tagName() != Format::Tag::attribute()) { 0019 return nullptr; 0020 } 0021 Attribute *attr = AttributeFactory::createAttribute(elem.attribute(Format::Attr::attributeType()).toUtf8()); 0022 Q_ASSERT(attr); 0023 attr->deserialize(elem.text().toUtf8()); 0024 return attr; 0025 } 0026 0027 template<typename T> 0028 static void readAttributesImpl(const QDomElement &elem, T &entity) 0029 { 0030 if (elem.isNull()) { 0031 return; 0032 } 0033 const QDomNodeList children = elem.childNodes(); 0034 for (int i = 0; i < children.count(); ++i) { 0035 const QDomElement attrElem = children.at(i).toElement(); 0036 Attribute *attr = XmlReader::elementToAttribute(attrElem); 0037 if (attr) { 0038 entity.addAttribute(attr); 0039 } 0040 } 0041 } 0042 0043 void XmlReader::readAttributes(const QDomElement &elem, Item &item) 0044 { 0045 readAttributesImpl(elem, item); 0046 } 0047 0048 void XmlReader::readAttributes(const QDomElement &elem, Collection &collection) 0049 { 0050 readAttributesImpl(elem, collection); 0051 } 0052 0053 Collection XmlReader::elementToCollection(const QDomElement &elem) 0054 { 0055 if (elem.isNull() || elem.tagName() != Format::Tag::collection()) { 0056 return Collection(); 0057 } 0058 0059 Collection c; 0060 c.setRemoteId(elem.attribute(Format::Attr::remoteId())); 0061 c.setName(elem.attribute(Format::Attr::collectionName())); 0062 c.setContentMimeTypes(elem.attribute(Format::Attr::collectionContentTypes()).split(QLatin1Char(','))); 0063 XmlReader::readAttributes(elem, c); 0064 0065 const QDomElement parentElem = elem.parentNode().toElement(); 0066 if (!parentElem.isNull() && parentElem.tagName() == Format::Tag::collection()) { 0067 c.parentCollection().setRemoteId(parentElem.attribute(Format::Attr::remoteId())); 0068 } 0069 0070 return c; 0071 } 0072 0073 Collection::List XmlReader::readCollections(const QDomElement &elem) 0074 { 0075 Collection::List rv; 0076 if (elem.isNull()) { 0077 return rv; 0078 } 0079 if (elem.tagName() == Format::Tag::collection()) { 0080 rv += elementToCollection(elem); 0081 } 0082 const QDomNodeList children = elem.childNodes(); 0083 for (int i = 0; i < children.count(); i++) { 0084 const QDomElement child = children.at(i).toElement(); 0085 if (child.isNull() || child.tagName() != Format::Tag::collection()) { 0086 continue; 0087 } 0088 rv += readCollections(child); 0089 } 0090 return rv; 0091 } 0092 0093 Tag XmlReader::elementToTag(const QDomElement &elem) 0094 { 0095 if (elem.isNull() || elem.tagName() != Format::Tag::tag()) { 0096 return Tag(); 0097 } 0098 0099 Tag t; 0100 t.setRemoteId(elem.attribute(Format::Attr::remoteId()).toUtf8()); 0101 t.setName(elem.attribute(Format::Attr::name())); 0102 t.setGid(elem.attribute(Format::Attr::gid()).toUtf8()); 0103 t.setType(elem.attribute(Format::Attr::type()).toUtf8()); 0104 0105 // TODO Implement rid parent support in TagCreateJob first 0106 // const QDomElement parentElem = elem.parentNode().toElement(); 0107 // if ( !parentElem.isNull() && parentElem.tagName() == Format::Tag::tag() ) { 0108 // Tag parent; 0109 // parent.setRemoteId( parentElem.attribute( Format::Attr::remoteId() ).toLatin1() ); 0110 // t.setParent( parent ); 0111 // } 0112 0113 return t; 0114 } 0115 0116 Tag::List XmlReader::readTags(const QDomElement &elem) 0117 { 0118 Tag::List rv; 0119 if (elem.isNull()) { 0120 return rv; 0121 } 0122 if (elem.tagName() == Format::Tag::tag()) { 0123 rv += elementToTag(elem); 0124 } 0125 const QDomNodeList children = elem.childNodes(); 0126 for (int i = 0; i < children.count(); i++) { 0127 const QDomElement child = children.at(i).toElement(); 0128 if (child.isNull() || child.tagName() != Format::Tag::tag()) { 0129 continue; 0130 } 0131 rv += readTags(child); 0132 } 0133 return rv; 0134 } 0135 0136 Item XmlReader::elementToItem(const QDomElement &elem, bool includePayload) 0137 { 0138 Item item(elem.attribute(Format::Attr::itemMimeType(), QStringLiteral("application/octet-stream"))); 0139 item.setRemoteId(elem.attribute(Format::Attr::remoteId())); 0140 XmlReader::readAttributes(elem, item); 0141 0142 const QDomNodeList children = elem.childNodes(); 0143 for (int i = 0; i < children.count(); ++i) { 0144 const QDomElement subElem = children.at(i).toElement(); 0145 if (subElem.isNull()) { 0146 continue; 0147 } 0148 if (subElem.tagName() == Format::Tag::flag()) { 0149 item.setFlag(subElem.text().toUtf8()); 0150 } else if (subElem.tagName() == Format::Tag::tag()) { 0151 Tag tag; 0152 tag.setRemoteId(subElem.text().toUtf8()); 0153 item.setTag(tag); 0154 } else if (includePayload && subElem.tagName() == Format::Tag::payload()) { 0155 const QByteArray payloadData = subElem.text().toUtf8(); 0156 item.setPayloadFromData(payloadData); 0157 } 0158 } 0159 0160 return item; 0161 }