Akonadi

xmlwriter.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Volker Krause <vkrause@kde.org>
3 SPDX-FileCopyrightText: 2009 Igor Trindade Oliveira <igor_trindade@yahoo.com.br>
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
15using 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
31template<typename T>
32static void writeAttributesImpl(const T &entity, QDomElement &parentElem)
33{
34 if (parentElem.isNull()) {
35 return;
36 }
37
38 QDomDocument doc = parentElem.ownerDocument();
39 const auto attributes = entity.attributes();
40 for (Attribute *attr : attributes) {
41 parentElem.appendChild(XmlWriter::attributeToElement(attr, doc));
42 }
43}
44
45void XmlWriter::writeAttributes(const Item &item, QDomElement &parentElem)
46{
47 writeAttributesImpl(item, parentElem);
48}
49
50void XmlWriter::writeAttributes(const Collection &collection, QDomElement &parentElem)
51{
52 writeAttributesImpl(collection, parentElem);
53}
54
56{
57 if (document.isNull()) {
58 return QDomElement();
59 }
60
61 QDomElement top = document.createElement(Format::Tag::collection());
62 top.setAttribute(Format::Attr::remoteId(), collection.remoteId());
63 top.setAttribute(Format::Attr::collectionName(), collection.name());
64 top.setAttribute(Format::Attr::collectionContentTypes(), collection.contentMimeTypes().join(QLatin1Char(',')));
65 writeAttributes(collection, top);
66
67 return top;
68}
69
71{
72 if (parentElem.isNull()) {
73 return QDomElement();
74 }
75
76 QDomDocument doc = parentElem.ownerDocument();
77 const QDomElement elem = collectionToElement(collection, doc);
78 parentElem.insertBefore(elem, QDomNode()); // collection need to be before items to pass schema validation
79 return elem;
80}
81
83{
84 if (document.isNull()) {
85 return QDomElement();
86 }
87
88 QDomElement top = document.createElement(Format::Tag::item());
89 top.setAttribute(Format::Attr::remoteId(), item.remoteId());
90 top.setAttribute(Format::Attr::itemMimeType(), item.mimeType());
91
92 if (item.hasPayload()) {
93 QDomElement payloadElem = document.createElement(Format::Tag::payload());
94 QDomText payloadText = document.createTextNode(QString::fromUtf8(item.payloadData()));
95 payloadElem.appendChild(payloadText);
96 top.appendChild(payloadElem);
97 }
98
99 writeAttributes(item, top);
100
101 const auto flags = item.flags();
102 for (const Item::Flag &flag : flags) {
103 QDomElement flagElem = document.createElement(Format::Tag::flag());
104 QDomText flagText = document.createTextNode(QString::fromUtf8(flag));
105 flagElem.appendChild(flagText);
106 top.appendChild(flagElem);
107 }
108
109 return top;
110}
111
113{
114 if (parentElem.isNull()) {
115 return QDomElement();
116 }
117
118 QDomDocument doc = parentElem.ownerDocument();
119 const QDomElement elem = itemToElement(item, doc);
120 parentElem.appendChild(elem);
121 return elem;
122}
Provides interface for custom attributes for Entity.
Definition attribute.h:132
virtual QByteArray serialized() const =0
Returns a QByteArray representation of the attribute which will be storaged.
virtual QByteArray type() const =0
Returns the type of the attribute.
Represents a collection of PIM items.
Definition collection.h:62
QString remoteId() const
Returns the remote id of the collection.
Represents a PIM item stored in Akonadi storage.
Definition item.h:100
QString mimeType() const
Returns the mime type of the item.
Definition item.cpp:326
Flags flags() const
Returns all flags of this item.
Definition item.cpp:175
bool hasPayload() const
Returns whether the item has a payload object.
QString remoteId() const
Returns the remote id of the item.
Definition item.cpp:73
QByteArray payloadData() const
Returns the full payload in its canonical representation, e.g.
Definition item.cpp:288
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.
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:45
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:70
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:82
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:55
AKONADI_XML_EXPORT QDomElement attributeToElement(Attribute *attr, QDomDocument &document)
Creates an attribute element for the given document.
Definition xmlwriter.cpp:17
Helper integration between Akonadi and Qt.
QDomElement createElement(const QString &tagName)
QDomText createTextNode(const QString &value)
void setAttribute(const QString &name, const QString &value)
QDomNode appendChild(const QDomNode &newChild)
QDomNode insertBefore(const QDomNode &newChild, const QDomNode &refChild)
bool isNull() const const
QDomDocument ownerDocument() const const
QString fromUtf8(QByteArrayView str)
QString join(QChar separator) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:42 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.