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>
28 static void readAttributesImpl(const QDomElement &elem, T &entity)
29 {
30  if (elem.isNull()) {
31  return;
32  }
33  const QDomNodeList children = elem.childNodes();
34  for (int i = 0; i < children.count(); ++i) {
35  const QDomElement attrElem = children.at(i).toElement();
36  Attribute *attr = XmlReader::elementToAttribute(attrElem);
37  if (attr) {
38  entity.addAttribute(attr);
39  }
40  }
41 }
42 
43 void XmlReader::readAttributes(const QDomElement &elem, Item &item)
44 {
45  readAttributesImpl(elem, item);
46 }
47 
48 void XmlReader::readAttributes(const QDomElement &elem, Collection &collection)
49 {
50  readAttributesImpl(elem, collection);
51 }
52 
54 {
55  if (elem.isNull() || elem.tagName() != Format::Tag::collection()) {
56  return Collection();
57  }
58 
59  Collection c;
60  c.setRemoteId(elem.attribute(Format::Attr::remoteId()));
61  c.setName(elem.attribute(Format::Attr::collectionName()));
62  c.setContentMimeTypes(elem.attribute(Format::Attr::collectionContentTypes()).split(QLatin1Char(',')));
64 
65  const QDomElement parentElem = elem.parentNode().toElement();
66  if (!parentElem.isNull() && parentElem.tagName() == Format::Tag::collection()) {
67  c.parentCollection().setRemoteId(parentElem.attribute(Format::Attr::remoteId()));
68  }
69 
70  return c;
71 }
72 
74 {
76  if (elem.isNull()) {
77  return rv;
78  }
79  if (elem.tagName() == Format::Tag::collection()) {
80  rv += elementToCollection(elem);
81  }
82  const QDomNodeList children = elem.childNodes();
83  for (int i = 0; i < children.count(); i++) {
84  const QDomElement child = children.at(i).toElement();
85  if (child.isNull() || child.tagName() != Format::Tag::collection()) {
86  continue;
87  }
88  rv += readCollections(child);
89  }
90  return rv;
91 }
92 
94 {
95  if (elem.isNull() || elem.tagName() != Format::Tag::tag()) {
96  return Tag();
97  }
98 
99  Tag t;
100  t.setRemoteId(elem.attribute(Format::Attr::remoteId()).toUtf8());
101  t.setName(elem.attribute(Format::Attr::name()));
102  t.setGid(elem.attribute(Format::Attr::gid()).toUtf8());
103  t.setType(elem.attribute(Format::Attr::type()).toUtf8());
104 
105  // TODO Implement rid parent support in TagCreateJob first
106  // const QDomElement parentElem = elem.parentNode().toElement();
107  // if ( !parentElem.isNull() && parentElem.tagName() == Format::Tag::tag() ) {
108  // Tag parent;
109  // parent.setRemoteId( parentElem.attribute( Format::Attr::remoteId() ).toLatin1() );
110  // t.setParent( parent );
111  // }
112 
113  return t;
114 }
115 
117 {
118  Tag::List rv;
119  if (elem.isNull()) {
120  return rv;
121  }
122  if (elem.tagName() == Format::Tag::tag()) {
123  rv += elementToTag(elem);
124  }
125  const QDomNodeList children = elem.childNodes();
126  for (int i = 0; i < children.count(); i++) {
127  const QDomElement child = children.at(i).toElement();
128  if (child.isNull() || child.tagName() != Format::Tag::tag()) {
129  continue;
130  }
131  rv += readTags(child);
132  }
133  return rv;
134 }
135 
136 Item XmlReader::elementToItem(const QDomElement &elem, bool includePayload)
137 {
138  Item item(elem.attribute(Format::Attr::itemMimeType(), QStringLiteral("application/octet-stream")));
139  item.setRemoteId(elem.attribute(Format::Attr::remoteId()));
140  XmlReader::readAttributes(elem, item);
141 
142  const QDomNodeList children = elem.childNodes();
143  for (int i = 0; i < children.count(); ++i) {
144  const QDomElement subElem = children.at(i).toElement();
145  if (subElem.isNull()) {
146  continue;
147  }
148  if (subElem.tagName() == Format::Tag::flag()) {
149  item.setFlag(subElem.text().toUtf8());
150  } else if (subElem.tagName() == Format::Tag::tag()) {
151  Tag tag;
152  tag.setRemoteId(subElem.text().toUtf8());
153  item.setTag(tag);
154  } else if (includePayload && subElem.tagName() == Format::Tag::payload()) {
155  const QByteArray payloadData = subElem.text().toUtf8();
156  item.setPayloadFromData(payloadData);
157  }
158  }
159 
160  return item;
161 }
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:136
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:53
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:116
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:43
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:93
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:73
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:105
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Jun 4 2023 03:52:48 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.