Akonadi

xmlreader.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 "xmlreader.h"
22 #include "format_p.h"
23 
24 #include "attributefactory.h"
25 #include <tag.h>
26 
27 using namespace Akonadi;
28 
30 {
31  if (elem.isNull() || elem.tagName() != Format::Tag::attribute()) {
32  return nullptr;
33  }
34  Attribute *attr = AttributeFactory::createAttribute(elem.attribute(Format::Attr::attributeType()).toUtf8());
35  Q_ASSERT(attr);
36  attr->deserialize(elem.text().toUtf8());
37  return attr;
38 }
39 
40 template<typename T>
41 static void readAttributesImpl(const QDomElement &elem, T &entity)
42 {
43  if (elem.isNull()) {
44  return;
45  }
46  const QDomNodeList children = elem.childNodes();
47  for (int i = 0; i < children.count(); ++i) {
48  const QDomElement attrElem = children.at(i).toElement();
49  Attribute *attr = XmlReader::elementToAttribute(attrElem);
50  if (attr) {
51  entity.addAttribute(attr);
52  }
53  }
54 }
55 
56 void XmlReader::readAttributes(const QDomElement &elem, Item &item)
57 {
58  readAttributesImpl(elem, item);
59 }
60 
61 void XmlReader::readAttributes(const QDomElement &elem, Collection &collection)
62 {
63  readAttributesImpl(elem, collection);
64 }
65 
67 {
68  if (elem.isNull() || elem.tagName() != Format::Tag::collection()) {
69  return Collection();
70  }
71 
72  Collection c;
73  c.setRemoteId(elem.attribute(Format::Attr::remoteId()));
74  c.setName(elem.attribute(Format::Attr::collectionName()));
75  c.setContentMimeTypes(elem.attribute(Format::Attr::collectionContentTypes()).split(QLatin1Char(',')));
77 
78  const QDomElement parentElem = elem.parentNode().toElement();
79  if (!parentElem.isNull() && parentElem.tagName() == Format::Tag::collection()) {
80  c.parentCollection().setRemoteId(parentElem.attribute(Format::Attr::remoteId()));
81  }
82 
83  return c;
84 }
85 
87 {
89  if (elem.isNull()) {
90  return rv;
91  }
92  if (elem.tagName() == Format::Tag::collection()) {
93  rv += elementToCollection(elem);
94  }
95  const QDomNodeList children = elem.childNodes();
96  for (int i = 0; i < children.count(); i++) {
97  const QDomElement child = children.at(i).toElement();
98  if (child.isNull() || child.tagName() != Format::Tag::collection()) {
99  continue;
100  }
101  rv += readCollections(child);
102  }
103  return rv;
104 }
105 
107 {
108  if (elem.isNull() || elem.tagName() != Format::Tag::tag()) {
109  return Tag();
110  }
111 
112  Tag t;
113  t.setRemoteId(elem.attribute(Format::Attr::remoteId()).toUtf8());
114  t.setName(elem.attribute(Format::Attr::name()));
115  t.setGid(elem.attribute(Format::Attr::gid()).toUtf8());
116  t.setType(elem.attribute(Format::Attr::type()).toUtf8());
117 
118  //TODO Implement rid parent support in TagCreateJob first
119  // const QDomElement parentElem = elem.parentNode().toElement();
120  // if ( !parentElem.isNull() && parentElem.tagName() == Format::Tag::tag() ) {
121  // Tag parent;
122  // parent.setRemoteId( parentElem.attribute( Format::Attr::remoteId() ).toLatin1() );
123  // t.setParent( parent );
124  // }
125 
126  return t;
127 }
128 
130 {
131  Tag::List rv;
132  if (elem.isNull()) {
133  return rv;
134  }
135  if (elem.tagName() == Format::Tag::tag()) {
136  rv += elementToTag(elem);
137  }
138  const QDomNodeList children = elem.childNodes();
139  for (int i = 0; i < children.count(); i++) {
140  const QDomElement child = children.at(i).toElement();
141  if (child.isNull() || child.tagName() != Format::Tag::tag()) {
142  continue;
143  }
144  rv += readTags(child);
145  }
146  return rv;
147 }
148 
149 Item XmlReader::elementToItem(const QDomElement &elem, bool includePayload)
150 {
151  Item item(elem.attribute(Format::Attr::itemMimeType(), QStringLiteral("application/octet-stream")));
152  item.setRemoteId(elem.attribute(Format::Attr::remoteId()));
153  XmlReader::readAttributes(elem, item);
154 
155  const QDomNodeList children = elem.childNodes();
156  for (int i = 0; i < children.count(); ++i) {
157  const QDomElement subElem = children.at(i).toElement();
158  if (subElem.isNull()) {
159  continue;
160  }
161  if (subElem.tagName() == Format::Tag::flag()) {
162  item.setFlag(subElem.text().toUtf8());
163  } else if (subElem.tagName() == Format::Tag::tag()) {
164  Tag tag;
165  tag.setRemoteId(subElem.text().toUtf8());
166  item.setTag(tag);
167  } else if (includePayload && subElem.tagName() == Format::Tag::payload()) {
168  const QByteArray payloadData = subElem.text().toUtf8();
169  item.setPayloadFromData(payloadData);
170  }
171  }
172 
173  return item;
174 }
AKONADI_XML_EXPORT Item elementToItem(const QDomElement &elem, bool includePayload=true)
Converts an item element.
Definition: xmlreader.cpp:149
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:86
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
Represents a collection of PIM items.
Definition: collection.h:76
AKONADI_XML_EXPORT Collection elementToCollection(const QDomElement &elem)
Converts a collection element.
Definition: xmlreader.cpp:66
virtual void deserialize(const QByteArray &data)=0
Sets the data of this attribute, using the same encoding as returned by toByteArray().
void setRemoteId(const QString &id)
Sets the remote id of the collection.
Definition: collection.cpp:117
Provides interface for custom attributes for Entity.
Definition: attribute.h:139
QDomNodeList childNodes() const const
void setName(const QString &name)
Sets the i18n&#39;ed name of the collection.
Definition: collection.cpp:237
QDomElement toElement() const const
int count() 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:56
QString text() const const
Collection parentCollection() const
Returns the parent collection of this object.
Definition: collection.cpp:211
bool isNull() const const
static Attribute * createAttribute(const QByteArray &type)
Creates an entity attribute object of the given type.
QDomNode parentNode() const const
Helper integration between Akonadi and Qt.
AKONADI_XML_EXPORT Attribute * elementToAttribute(const QDomElement &elem)
Converts an attribute element.
Definition: xmlreader.cpp:29
An Akonadi Tag.
Definition: tag.h:39
QString tagName() const const
AKONADI_XML_EXPORT Tag::List readTags(const QDomElement &elem)
Reads recursively all tags starting from the given DOM element.
Definition: xmlreader.cpp:129
void setContentMimeTypes(const QStringList &types)
Sets the list of possible content mime types.
Definition: collection.cpp:261
AKONADI_XML_EXPORT Tag elementToTag(const QDomElement &elem)
Converts a tag element.
Definition: xmlreader.cpp:106
QDomNode at(int index) const const
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed May 27 2020 22:43:41 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.