Akonadi

xmlreader.cpp
1 /*
2  SPDX-FileCopyrightText: 2009 Volker Krause <[email protected]>
3  SPDX-FileCopyrightText: 2009 Igor Trindade Oliveira <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "xmlreader.h"
9 #include "format_p.h"
10 
11 #include "attributefactory.h"
12 #include <tag.h>
13 
14 using namespace Akonadi;
15 
17 {
18  if (elem.isNull() || elem.tagName() != Format::Tag::attribute()) {
19  return nullptr;
20  }
21  Attribute *attr = AttributeFactory::createAttribute(elem.attribute(Format::Attr::attributeType()).toUtf8());
22  Q_ASSERT(attr);
23  attr->deserialize(elem.text().toUtf8());
24  return attr;
25 }
26 
27 template<typename T> static void readAttributesImpl(const QDomElement &elem, T &entity)
28 {
29  if (elem.isNull()) {
30  return;
31  }
32  const QDomNodeList children = elem.childNodes();
33  for (int i = 0; i < children.count(); ++i) {
34  const QDomElement attrElem = children.at(i).toElement();
35  Attribute *attr = XmlReader::elementToAttribute(attrElem);
36  if (attr) {
37  entity.addAttribute(attr);
38  }
39  }
40 }
41 
42 void XmlReader::readAttributes(const QDomElement &elem, Item &item)
43 {
44  readAttributesImpl(elem, item);
45 }
46 
47 void XmlReader::readAttributes(const QDomElement &elem, Collection &collection)
48 {
49  readAttributesImpl(elem, collection);
50 }
51 
53 {
54  if (elem.isNull() || elem.tagName() != Format::Tag::collection()) {
55  return Collection();
56  }
57 
58  Collection c;
59  c.setRemoteId(elem.attribute(Format::Attr::remoteId()));
60  c.setName(elem.attribute(Format::Attr::collectionName()));
61  c.setContentMimeTypes(elem.attribute(Format::Attr::collectionContentTypes()).split(QLatin1Char(',')));
63 
64  const QDomElement parentElem = elem.parentNode().toElement();
65  if (!parentElem.isNull() && parentElem.tagName() == Format::Tag::collection()) {
66  c.parentCollection().setRemoteId(parentElem.attribute(Format::Attr::remoteId()));
67  }
68 
69  return c;
70 }
71 
73 {
75  if (elem.isNull()) {
76  return rv;
77  }
78  if (elem.tagName() == Format::Tag::collection()) {
79  rv += elementToCollection(elem);
80  }
81  const QDomNodeList children = elem.childNodes();
82  for (int i = 0; i < children.count(); i++) {
83  const QDomElement child = children.at(i).toElement();
84  if (child.isNull() || child.tagName() != Format::Tag::collection()) {
85  continue;
86  }
87  rv += readCollections(child);
88  }
89  return rv;
90 }
91 
93 {
94  if (elem.isNull() || elem.tagName() != Format::Tag::tag()) {
95  return Tag();
96  }
97 
98  Tag t;
99  t.setRemoteId(elem.attribute(Format::Attr::remoteId()).toUtf8());
100  t.setName(elem.attribute(Format::Attr::name()));
101  t.setGid(elem.attribute(Format::Attr::gid()).toUtf8());
102  t.setType(elem.attribute(Format::Attr::type()).toUtf8());
103 
104  // TODO Implement rid parent support in TagCreateJob first
105  // const QDomElement parentElem = elem.parentNode().toElement();
106  // if ( !parentElem.isNull() && parentElem.tagName() == Format::Tag::tag() ) {
107  // Tag parent;
108  // parent.setRemoteId( parentElem.attribute( Format::Attr::remoteId() ).toLatin1() );
109  // t.setParent( parent );
110  // }
111 
112  return t;
113 }
114 
116 {
117  Tag::List rv;
118  if (elem.isNull()) {
119  return rv;
120  }
121  if (elem.tagName() == Format::Tag::tag()) {
122  rv += elementToTag(elem);
123  }
124  const QDomNodeList children = elem.childNodes();
125  for (int i = 0; i < children.count(); i++) {
126  const QDomElement child = children.at(i).toElement();
127  if (child.isNull() || child.tagName() != Format::Tag::tag()) {
128  continue;
129  }
130  rv += readTags(child);
131  }
132  return rv;
133 }
134 
135 Item XmlReader::elementToItem(const QDomElement &elem, bool includePayload)
136 {
137  Item item(elem.attribute(Format::Attr::itemMimeType(), QStringLiteral("application/octet-stream")));
138  item.setRemoteId(elem.attribute(Format::Attr::remoteId()));
139  XmlReader::readAttributes(elem, item);
140 
141  const QDomNodeList children = elem.childNodes();
142  for (int i = 0; i < children.count(); ++i) {
143  const QDomElement subElem = children.at(i).toElement();
144  if (subElem.isNull()) {
145  continue;
146  }
147  if (subElem.tagName() == Format::Tag::flag()) {
148  item.setFlag(subElem.text().toUtf8());
149  } else if (subElem.tagName() == Format::Tag::tag()) {
150  Tag tag;
151  tag.setRemoteId(subElem.text().toUtf8());
152  item.setTag(tag);
153  } else if (includePayload && subElem.tagName() == Format::Tag::payload()) {
154  const QByteArray payloadData = subElem.text().toUtf8();
155  item.setPayloadFromData(payloadData);
156  }
157  }
158 
159  return item;
160 }
int count() const const
QString text() const const
QDomElement toElement() const const
AKONADI_XML_EXPORT Item elementToItem(const QDomElement &elem, bool includePayload=true)
Converts an item element.
Definition: xmlreader.cpp:135
void setPayloadFromData(const QByteArray &data)
Sets the payload based on the canonical representation normally used for data of this mime type.
Definition: item.cpp:301
QString tagName() const const
QDomNode parentNode() const const
An Akonadi Tag.
Definition: tag.h:25
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool isNull() const const
AKONADI_XML_EXPORT Collection elementToCollection(const QDomElement &elem)
Converts a collection element.
Definition: xmlreader.cpp:52
Represents a collection of PIM items.
Definition: collection.h:61
Provides interface for custom attributes for Entity.
Definition: attribute.h:124
QDomNode at(int index) const const
AKONADI_XML_EXPORT Attribute * elementToAttribute(const QDomElement &elem)
Converts an attribute element.
Definition: xmlreader.cpp:16
AKONADI_XML_EXPORT Tag::List readTags(const QDomElement &elem)
Reads recursively all tags starting from the given DOM element.
Definition: xmlreader.cpp:115
void setRemoteId(const QString &id)
Sets the remote id of the collection.
Definition: collection.cpp:101
QByteArray toUtf8() const const
AKONADI_XML_EXPORT void readAttributes(const QDomElement &elem, Item &item)
Reads all attributes that are immediate children of elem and adds them to item.
Definition: xmlreader.cpp:42
void setRemoteId(const QString &id)
Sets the remote id of the item.
Definition: item.cpp:68
AKONADI_XML_EXPORT Tag elementToTag(const QDomElement &elem)
Converts a tag element.
Definition: xmlreader.cpp:92
QDomNodeList childNodes() const const
void setContentMimeTypes(const QStringList &types)
Sets the list of possible content mime types.
Definition: collection.cpp:245
Collection parentCollection() const
Returns the parent collection of this object.
Definition: collection.cpp:187
static Attribute * createAttribute(const QByteArray &type)
Creates an entity attribute object of the given type.
QString attribute(const QString &name, const QString &defValue) const const
AKONADI_XML_EXPORT Collection::List readCollections(const QDomElement &elem)
Reads recursively all collections starting from the given DOM element.
Definition: xmlreader.cpp:72
void setFlag(const QByteArray &name)
Sets the flag with the given name in the item.
Definition: item.cpp:180
void setName(const QString &name)
Sets the i18n'ed name of the collection.
Definition: collection.cpp:221
virtual void deserialize(const QByteArray &data)=0
Sets the data of this attribute, using the same encoding as returned by toByteArray().
Represents a PIM item stored in Akonadi storage.
Definition: item.h:104
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jul 2 2022 06:41:49 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.