Akonadi

xmlwriter.cpp
1 /*
2  Copyright (c) 2009 Volker Krause <[email protected]>
3  Copyright (c) 2009 Igor Trindade Oliveira <[email protected]>
4 
5  This library is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or (at your
8  option) any later version.
9 
10  This library is distributed in the hope that it will be useful, but WITHOUT
11  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13  License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to the
17  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  02110-1301, USA.
19 */
20 
21 #include "xmlwriter.h"
22 #include "format_p.h"
23 
24 #include "attribute.h"
25 #include "collection.h"
26 #include "item.h"
27 
28 using namespace Akonadi;
29 
31 {
32  if (document.isNull()) {
33  return QDomElement();
34  }
35 
36  QDomElement top = document.createElement(Format::Tag::attribute());
37  top.setAttribute(Format::Attr::attributeType(), QString::fromUtf8(attr->type()));
38  QDomText attrText = document.createTextNode(QString::fromUtf8(attr->serialized()));
39  top.appendChild(attrText);
40 
41  return top;
42 }
43 
44 template<typename T>
45 static void writeAttributesImpl(const T &entity, QDomElement &parentElem)
46 {
47  if (parentElem.isNull()) {
48  return;
49  }
50 
51  QDomDocument doc = parentElem.ownerDocument();
52  Q_FOREACH (Attribute *attr, entity.attributes()) {
53  parentElem.appendChild(XmlWriter::attributeToElement(attr, doc));
54  }
55 }
56 
57 void XmlWriter::writeAttributes(const Item &item, QDomElement &parentElem)
58 {
59  writeAttributesImpl(item, parentElem);
60 }
61 
62 void XmlWriter::writeAttributes(const Collection &collection, QDomElement &parentElem)
63 {
64  writeAttributesImpl(collection, parentElem);
65 }
66 
68 {
69  if (document.isNull()) {
70  return QDomElement();
71  }
72 
73  QDomElement top = document.createElement(Format::Tag::collection());
74  top.setAttribute(Format::Attr::remoteId(), collection.remoteId());
75  top.setAttribute(Format::Attr::collectionName(), collection.name());
76  top.setAttribute(Format::Attr::collectionContentTypes(), collection.contentMimeTypes().join(QLatin1Char(',')));
77  writeAttributes(collection, top);
78 
79  return top;
80 }
81 
83 {
84  if (parentElem.isNull()) {
85  return QDomElement();
86  }
87 
88  QDomDocument doc = parentElem.ownerDocument();
89  const QDomElement elem = collectionToElement(collection, doc);
90  parentElem.insertBefore(elem, QDomNode()); // collection need to be before items to pass schema validation
91  return elem;
92 }
93 
94 QDomElement XmlWriter::itemToElement(const Akonadi::Item &item, QDomDocument &document)
95 {
96  if (document.isNull()) {
97  return QDomElement();
98  }
99 
100  QDomElement top = document.createElement(Format::Tag::item());
101  top.setAttribute(Format::Attr::remoteId(), item.remoteId());
102  top.setAttribute(Format::Attr::itemMimeType(), item.mimeType());
103 
104  if (item.hasPayload()) {
105  QDomElement payloadElem = document.createElement(Format::Tag::payload());
106  QDomText payloadText = document.createTextNode(QString::fromUtf8(item.payloadData()));
107  payloadElem.appendChild(payloadText);
108  top.appendChild(payloadElem);
109  }
110 
111  writeAttributes(item, top);
112 
113  Q_FOREACH (const Item::Flag &flag, item.flags()) {
114  QDomElement flagElem = document.createElement(Format::Tag::flag());
115  QDomText flagText = document.createTextNode(QString::fromUtf8(flag));
116  flagElem.appendChild(flagText);
117  top.appendChild(flagElem);
118  }
119 
120  return top;
121 }
122 
123 QDomElement XmlWriter::writeItem(const Item &item, QDomElement &parentElem)
124 {
125  if (parentElem.isNull()) {
126  return QDomElement();
127  }
128 
129  QDomDocument doc = parentElem.ownerDocument();
130  const QDomElement elem = itemToElement(item, doc);
131  parentElem.appendChild(elem);
132  return elem;
133 }
134 
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:57
QString name() const
Returns the i18n&#39;ed name of the collection.
Definition: collection.cpp:225
QDomNode appendChild(const QDomNode &newChild)
Represents a collection of PIM items.
Definition: collection.h:76
QString join(const QString &separator) const const
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:82
Provides interface for custom attributes for Entity.
Definition: attribute.h:139
virtual QByteArray type() const =0
Returns the type of the attribute.
AKONADI_XML_EXPORT QDomElement attributeToElement(Attribute *attr, QDomDocument &document)
Creates an attribute element for the given document.
Definition: xmlwriter.cpp:30
QString fromUtf8(const char *str, int size)
QDomDocument ownerDocument() const const
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. ...
void setAttribute(const QString &name, const QString &value)
QDomText createTextNode(const QString &value)
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:123
bool isNull() const const
Helper integration between Akonadi and Qt.
QString remoteId() const
Returns the remote id of the collection.
Definition: collection.cpp:122
QStringList contentMimeTypes() const
Returns a list of possible content mimetypes, e.g.
Definition: collection.cpp:256
virtual QByteArray serialized() const =0
Returns a QByteArray representation of the attribute which will be storaged.
QDomElement createElement(const QString &tagName)
QDomNode insertBefore(const QDomNode &newChild, const QDomNode &refChild)
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:67
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Jul 1 2020 23:14:23 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.