File: xmlreader.cpp

package info (click to toggle)
kdepimlibs 4%3A4.14.10-7
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 35,808 kB
  • ctags: 31,888
  • sloc: cpp: 269,393; xml: 4,188; ansic: 2,946; yacc: 1,904; perl: 381; ruby: 60; sh: 60; makefile: 16
file content (152 lines) | stat: -rw-r--r-- 5,278 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
    Copyright (c) 2009 Volker Krause <vkrause@kde.org>
    Copyright (c) 2009 Igor Trindade Oliveira <igor_trindade@yahoo.com.br>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include "xmlreader.h"
#include "format_p.h"

#include <akonadi/attributefactory.h>
#include <akonadi/tag.h>

#include <QStringList>

using namespace Akonadi;

Attribute* XmlReader::elementToAttribute(const QDomElement& elem)
{
  if ( elem.isNull() || elem.tagName() != Format::Tag::attribute() )
    return 0;
  Attribute *attr = AttributeFactory::createAttribute( elem.attribute( Format::Attr::attributeType() ).toUtf8() );
  Q_ASSERT( attr );
  attr->deserialize( elem.text().toUtf8() );
  return attr;
}

void XmlReader::readAttributes(const QDomElement& elem, Entity& entity)
{
  if ( elem.isNull() )
    return;
  const QDomNodeList children = elem.childNodes();
  for ( int i = 0; i < children.count(); ++i ) {
    const QDomElement attrElem = children.at( i ).toElement();
    Attribute *attr = elementToAttribute( attrElem );
    if ( attr )
      entity.addAttribute( attr );
  }
}

Collection XmlReader::elementToCollection(const QDomElement& elem)
{
  if ( elem.isNull() || elem.tagName() != Format::Tag::collection() )
    return Collection();

  Collection c;
  c.setRemoteId( elem.attribute( Format::Attr::remoteId() ) );
  c.setName( elem.attribute( Format::Attr::collectionName() ) );
  c.setContentMimeTypes( elem.attribute( Format::Attr::collectionContentTypes() ).split( QLatin1Char(',') ) );
  XmlReader::readAttributes( elem, c );

  const QDomElement parentElem = elem.parentNode().toElement();
  if ( !parentElem.isNull() && parentElem.tagName() == Format::Tag::collection() )
    c.parentCollection().setRemoteId( parentElem.attribute( Format::Attr::remoteId() ) );

  return c;
}

Collection::List XmlReader::readCollections(const QDomElement& elem)
{
  Collection::List rv;
  if ( elem.isNull() )
    return rv;
  if ( elem.tagName() == Format::Tag::collection() )
    rv += elementToCollection( elem );
  const QDomNodeList children = elem.childNodes();
  for ( int i = 0; i < children.count(); i++ ) {
    const QDomElement child = children.at( i ).toElement();
    if ( child.isNull() || child.tagName() != Format::Tag::collection() )
      continue;
    rv += readCollections( child );
  }
  return rv;
}

Tag XmlReader::elementToTag(const QDomElement& elem)
{
  if ( elem.isNull() || elem.tagName() != Format::Tag::tag() )
    return Tag();

  Tag t;
  t.setRemoteId( elem.attribute( Format::Attr::remoteId() ).toUtf8() );
  t.setName( elem.attribute( Format::Attr::name() ) );
  t.setGid( elem.attribute( Format::Attr::gid() ).toUtf8() );
  t.setType( elem.attribute( Format::Attr::type() ).toUtf8() );

  //TODO Implement rid parent support in TagCreateJob first
  // const QDomElement parentElem = elem.parentNode().toElement();
  // if ( !parentElem.isNull() && parentElem.tagName() == Format::Tag::tag() ) {
  //   Tag parent;
  //   parent.setRemoteId( parentElem.attribute( Format::Attr::remoteId() ).toLatin1() );
  //   t.setParent( parent );
  // }

  return t;
}

Tag::List XmlReader::readTags(const QDomElement& elem)
{
  Tag::List rv;
  if ( elem.isNull() )
    return rv;
  if ( elem.tagName() == Format::Tag::tag() )
    rv += elementToTag( elem );
  const QDomNodeList children = elem.childNodes();
  for ( int i = 0; i < children.count(); i++ ) {
    const QDomElement child = children.at( i ).toElement();
    if ( child.isNull() || child.tagName() != Format::Tag::tag() )
      continue;
    rv += readTags( child );
  }
  return rv;
}

Item XmlReader::elementToItem(const QDomElement& elem, bool includePayload)
{
  Item item( elem.attribute( Format::Attr::itemMimeType(), QLatin1String("application/octet-stream") ) );
  item.setRemoteId( elem.attribute( Format::Attr::remoteId() ) );
  XmlReader::readAttributes( elem, item );
  
  const QDomNodeList children = elem.childNodes();
  for ( int i = 0; i < children.count(); ++i ) {
    const QDomElement subElem = children.at( i ).toElement();
    if ( subElem.isNull() )
      continue;
    if ( subElem.tagName() == Format::Tag::flag() ) {
      item.setFlag( subElem.text().toUtf8() );
    } else if ( subElem.tagName() == Format::Tag::tag() ) {
      Tag tag;
      tag.setRemoteId( subElem.text().toUtf8() );
      item.setTag( tag );
    } else if ( includePayload && subElem.tagName() == Format::Tag::payload() ) {
      const QByteArray payloadData = subElem.text().toUtf8();
      item.setPayloadFromData( payloadData );
    }
  }

  return item;
}