Akonadi

xmlwriter.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 "xmlwriter.h"
9 #include "format_p.h"
10 
11 #include "attribute.h"
12 #include "collection.h"
13 #include "item.h"
14 
15 using namespace Akonadi;
16 
18 {
19  if (document.isNull()) {
20  return QDomElement();
21  }
22 
23  QDomElement top = document.createElement(Format::Tag::attribute());
24  top.setAttribute(Format::Attr::attributeType(), QString::fromUtf8(attr->type()));
25  QDomText attrText = document.createTextNode(QString::fromUtf8(attr->serialized()));
26  top.appendChild(attrText);
27 
28  return top;
29 }
30 
31 template<typename T> static void writeAttributesImpl(const T &entity, QDomElement &parentElem)
32 {
33  if (parentElem.isNull()) {
34  return;
35  }
36 
37  QDomDocument doc = parentElem.ownerDocument();
38  const auto attributes = entity.attributes();
39  for (Attribute *attr : attributes) {
40  parentElem.appendChild(XmlWriter::attributeToElement(attr, doc));
41  }
42 }
43 
44 void XmlWriter::writeAttributes(const Item &item, QDomElement &parentElem)
45 {
46  writeAttributesImpl(item, parentElem);
47 }
48 
49 void XmlWriter::writeAttributes(const Collection &collection, QDomElement &parentElem)
50 {
51  writeAttributesImpl(collection, parentElem);
52 }
53 
55 {
56  if (document.isNull()) {
57  return QDomElement();
58  }
59 
60  QDomElement top = document.createElement(Format::Tag::collection());
61  top.setAttribute(Format::Attr::remoteId(), collection.remoteId());
62  top.setAttribute(Format::Attr::collectionName(), collection.name());
63  top.setAttribute(Format::Attr::collectionContentTypes(), collection.contentMimeTypes().join(QLatin1Char(',')));
64  writeAttributes(collection, top);
65 
66  return top;
67 }
68 
70 {
71  if (parentElem.isNull()) {
72  return QDomElement();
73  }
74 
75  QDomDocument doc = parentElem.ownerDocument();
76  const QDomElement elem = collectionToElement(collection, doc);
77  parentElem.insertBefore(elem, QDomNode()); // collection need to be before items to pass schema validation
78  return elem;
79 }
80 
82 {
83  if (document.isNull()) {
84  return QDomElement();
85  }
86 
87  QDomElement top = document.createElement(Format::Tag::item());
88  top.setAttribute(Format::Attr::remoteId(), item.remoteId());
89  top.setAttribute(Format::Attr::itemMimeType(), item.mimeType());
90 
91  if (item.hasPayload()) {
92  QDomElement payloadElem = document.createElement(Format::Tag::payload());
93  QDomText payloadText = document.createTextNode(QString::fromUtf8(item.payloadData()));
94  payloadElem.appendChild(payloadText);
95  top.appendChild(payloadElem);
96  }
97 
98  writeAttributes(item, top);
99 
100  const auto flags = item.flags();
101  for (const Item::Flag &flag : flags) {
102  QDomElement flagElem = document.createElement(Format::Tag::flag());
103  QDomText flagText = document.createTextNode(QString::fromUtf8(flag));
104  flagElem.appendChild(flagText);
105  top.appendChild(flagElem);
106  }
107 
108  return top;
109 }
110 
112 {
113  if (parentElem.isNull()) {
114  return QDomElement();
115  }
116 
117  QDomDocument doc = parentElem.ownerDocument();
118  const QDomElement elem = itemToElement(item, doc);
119  parentElem.appendChild(elem);
120  return elem;
121 }
QString fromUtf8(const char *str, int size)
Flags flags() const
Returns all flags of this item.
Definition: item.cpp:175
virtual QByteArray type() const =0
Returns the type of the attribute.
bool isNull() const const
QDomText createTextNode(const QString &value)
AKONADI_XML_EXPORT QDomElement collectionToElement(const Collection &collection, QDomDocument &document)
Creates a collection element for the given document, not yet attached to the DOM tree.
Definition: xmlwriter.cpp:54
Represents a collection of PIM items.
Definition: collection.h:61
QDomNamedNodeMap attributes() const const
Provides interface for custom attributes for Entity.
Definition: attribute.h:124
QDomElement createElement(const QString &tagName)
bool hasPayload() const
Returns whether the item has a payload object.
Definition: item.cpp:372
void setAttribute(const QString &name, const QString &value)
QString mimeType() const
Returns the mime type of the item.
Definition: item.cpp:331
virtual QByteArray serialized() const =0
Returns a QByteArray representation of the attribute which will be storaged.
AKONADI_XML_EXPORT QDomElement writeCollection(const Collection &collection, QDomElement &parentElem)
Serializes the given collection into a DOM element with the given parent.
Definition: xmlwriter.cpp:69
QDomDocument ownerDocument() const const
QDomNode insertBefore(const QDomNode &newChild, const QDomNode &refChild)
QString join(const QString &separator) const const
AKONADI_XML_EXPORT void writeAttributes(const Item &entity, QDomElement &parentElem)
Serializes all attributes of the given Akonadi object into the given parent element.
Definition: xmlwriter.cpp:44
QString remoteId() const
Returns the remote id of the collection.
Definition: collection.cpp:106
AKONADI_XML_EXPORT QDomElement itemToElement(const Item &item, QDomDocument &document)
Creates an item element for the given document, not yet attached to the DOM tree.
Definition: xmlwriter.cpp:81
QDomNode appendChild(const QDomNode &newChild)
QByteArray payloadData() const
Returns the full payload in its canonical representation, e.g.
Definition: item.cpp:293
AKONADI_XML_EXPORT QDomElement writeItem(const Item &item, QDomElement &parentElem)
Serializes the given item into a DOM element and attaches it to the given item.
Definition: xmlwriter.cpp:111
AKONADI_XML_EXPORT QDomElement attributeToElement(Attribute *attr, QDomDocument &document)
Creates an attribute element for the given document.
Definition: xmlwriter.cpp:17
QString remoteId() const
Returns the remote id of the item.
Definition: item.cpp:73
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.