• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 API Reference
  • KDE Home
  • Contact Us
 

kabc

  • kabc
contactgrouptool.cpp
1 /*
2  This file is part of libkabc.
3  Copyright (c) 2008 Tobias Koenig <tokoe@kde.org>
4  Copyright (c) 2008 Kevin Krammer <kevin.krammer@gmx.at>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include "contactgrouptool.h"
23 #include "contactgroup.h"
24 
25 #include <QtCore/QIODevice>
26 #include <QtCore/QString>
27 #include <QtCore/QDebug>
28 
29 #include <QtCore/QXmlStreamReader>
30 #include <QtCore/QXmlStreamWriter>
31 
32 using namespace KABC;
33 
34 class XmlContactGroupWriter : public QXmlStreamWriter
35 {
36  public:
37  XmlContactGroupWriter();
38 
39  void write( const ContactGroup &group, QIODevice *device );
40  void write( const QList<ContactGroup> &groupLis, QIODevice *device );
41 
42  private:
43  void writeGroup( const ContactGroup &group );
44  void writeContactReference( const ContactGroup::ContactReference & );
45  void writeContactGroupReference( const ContactGroup::ContactGroupReference & );
46  void writeData( const ContactGroup::Data & );
47 };
48 
49 XmlContactGroupWriter::XmlContactGroupWriter()
50 {
51  setAutoFormatting( true );
52 }
53 
54 void XmlContactGroupWriter::write( const ContactGroup &group, QIODevice *device )
55 {
56  setDevice( device );
57 
58  writeStartDocument();
59 
60  writeGroup( group );
61 
62  writeEndDocument();
63 }
64 
65 void XmlContactGroupWriter::write( const QList<ContactGroup> &groupList, QIODevice *device )
66 {
67  setDevice( device );
68 
69  writeStartDocument();
70 
71  writeStartElement( QLatin1String( "contactGroupList" ) );
72 
73  foreach ( const ContactGroup & group, groupList ) {
74  writeGroup( group );
75  }
76 
77  writeEndElement();
78 
79  writeEndDocument();
80 }
81 
82 void XmlContactGroupWriter::writeGroup( const ContactGroup &group )
83 {
84  writeStartElement( QLatin1String( "contactGroup" ) );
85  writeAttribute( QLatin1String( "uid" ), group.id() );
86  writeAttribute( QLatin1String( "name" ), group.name() );
87 
88  for ( uint i = 0; i < group.contactReferenceCount(); ++i ) {
89  writeContactReference( group.contactReference( i ) );
90  }
91 
92  for ( uint i = 0; i < group.contactGroupReferenceCount(); ++i ) {
93  writeContactGroupReference( group.contactGroupReference( i ) );
94  }
95 
96  for ( uint i = 0; i < group.dataCount(); ++i ) {
97  writeData( group.data( i ) );
98  }
99 
100  writeEndElement();
101 }
102 
103 void XmlContactGroupWriter::writeContactReference( const ContactGroup::ContactReference &reference )
104 {
105  writeStartElement( QLatin1String( "contactReference" ) );
106  writeAttribute( QLatin1String( "uid" ), reference.uid() );
107  if ( !reference.preferredEmail().isEmpty() ) {
108  writeAttribute( QLatin1String( "preferredEmail" ), reference.preferredEmail() );
109  }
110 
111  // TODO: customs
112 
113  writeEndElement();
114 }
115 
116 void XmlContactGroupWriter::writeContactGroupReference(
117  const ContactGroup::ContactGroupReference &reference )
118 {
119  writeStartElement( QLatin1String( "contactGroupReference" ) );
120  writeAttribute( QLatin1String( "uid" ), reference.uid() );
121 
122  // TODO: customs
123 
124  writeEndElement();
125 }
126 
127 void XmlContactGroupWriter::writeData( const ContactGroup::Data &data )
128 {
129  writeStartElement( QLatin1String( "contactData" ) );
130  writeAttribute( QLatin1String( "name" ), data.name() );
131  writeAttribute( QLatin1String( "email" ), data.email() );
132 
133  // TODO: customs
134 
135  writeEndElement();
136 }
137 
138 class XmlContactGroupReader : public QXmlStreamReader
139 {
140  public:
141  XmlContactGroupReader();
142 
143  bool read( QIODevice *device, ContactGroup &group );
144  bool read( QIODevice *device, QList<ContactGroup> &groupList );
145 
146  private:
147  bool readGroup( ContactGroup &group );
148  bool readContactReference( ContactGroup::ContactReference &reference );
149  bool readContactGroupReference( ContactGroup::ContactGroupReference &reference );
150  bool readData( ContactGroup::Data &data );
151 };
152 
153 XmlContactGroupReader::XmlContactGroupReader()
154 {
155 }
156 
157 bool XmlContactGroupReader::read( QIODevice *device, ContactGroup &group )
158 {
159  setDevice( device );
160 
161  while ( !atEnd() ) {
162  readNext();
163  if ( isStartElement() ) {
164  if ( name() == QLatin1String( "contactGroup" ) ) {
165  return readGroup( group );
166  } else {
167  raiseError( QLatin1String( "The document does not describe a ContactGroup" ) );
168  }
169  }
170  }
171 
172  return error() == NoError;
173 }
174 
175 bool XmlContactGroupReader::read( QIODevice *device, QList<ContactGroup> &groupList )
176 {
177  setDevice( device );
178 
179  int depth = 0;
180 
181  while ( !atEnd() ) {
182  readNext();
183  if ( isStartElement() ) {
184  ++depth;
185  if ( depth == 1 ) {
186  if ( name() == QLatin1String( "contactGroupList" ) ) {
187  continue;
188  } else {
189  raiseError( QLatin1String( "The document does not describe a list of ContactGroup" ) );
190  }
191  } else if ( depth == 2 ) {
192  if ( name() == QLatin1String( "contactGroup" ) ) {
193  ContactGroup group;
194  if ( !readGroup( group ) ) {
195  return false;
196  }
197 
198  groupList.append( group );
199  } else {
200  raiseError( QLatin1String( "The document does not describe a list of ContactGroup" ) );
201  }
202  }
203  }
204 
205  if ( isEndElement() ) {
206  --depth;
207  }
208  }
209 
210  return error() == NoError;
211 }
212 
213 bool XmlContactGroupReader::readGroup( ContactGroup &group )
214 {
215  const QXmlStreamAttributes elementAttributes = attributes();
216  const QStringRef uid = elementAttributes.value( QLatin1String( "uid" ) );
217  if ( uid.isEmpty() ) {
218  raiseError( QLatin1String( "ContactGroup is missing a uid" ) );
219  return false;
220  }
221 
222  const QStringRef groupName = elementAttributes.value( QLatin1String( "name" ) );
223  if ( groupName.isEmpty() ) {
224  raiseError( QLatin1String( "ContactGroup is missing a name" ) );
225  return false;
226  }
227 
228  group.setId( uid.toString() );
229  group.setName( groupName.toString() );
230 
231  while ( !atEnd() ) {
232  readNext();
233  if ( isStartElement() ) {
234  if ( name() == QLatin1String( "contactData" ) ) {
235  ContactGroup::Data data;
236  if ( !readData( data ) ) {
237  return false;
238  }
239  group.append( data );
240  } else if ( name() == QLatin1String( "contactReference" ) ) {
241  ContactGroup::ContactReference reference;
242  if ( !readContactReference( reference ) ) {
243  return false;
244  }
245  group.append( reference );
246  } else if ( name() == QLatin1String( "contactGroupReference" ) ) {
247  ContactGroup::ContactGroupReference reference;
248  if ( !readContactGroupReference( reference ) ) {
249  return false;
250  }
251  group.append( reference );
252  } else {
253  raiseError( QLatin1String( "The document does not describe a ContactGroup" ) );
254  }
255  }
256 
257  if ( isEndElement() ) {
258  if ( name() == QLatin1String( "contactGroup" ) ) {
259  return true;
260  }
261  }
262  }
263 
264  return false;
265 }
266 
267 bool XmlContactGroupReader::readData( ContactGroup::Data &data )
268 {
269  const QXmlStreamAttributes elementAttributes = attributes();
270  const QStringRef email = elementAttributes.value( QLatin1String( "email" ) );
271  if ( email.isEmpty() ) {
272  raiseError( QLatin1String( "ContactData is missing an email address" ) );
273  return false;
274  }
275 
276  const QStringRef name = elementAttributes.value( QLatin1String( "name" ) );
277 
278  data.setName( name.toString() );
279  data.setEmail( email.toString() );
280 
281  return true;
282 }
283 
284 bool XmlContactGroupReader::readContactReference( ContactGroup::ContactReference &reference )
285 {
286  const QXmlStreamAttributes elementAttributes = attributes();
287  const QStringRef uid = elementAttributes.value( QLatin1String( "uid" ) );
288  if ( uid.isEmpty() ) {
289  raiseError( QLatin1String( "ContactReference is missing a uid" ) );
290  return false;
291  }
292  const QStringRef preferredEmail = elementAttributes.value( QLatin1String( "preferredEmail" ) );
293 
294  reference.setUid( uid.toString() );
295  reference.setPreferredEmail( preferredEmail.toString() );
296 
297  return true;
298 }
299 
300 bool XmlContactGroupReader::readContactGroupReference(
301  ContactGroup::ContactGroupReference &reference )
302 {
303  const QXmlStreamAttributes elementAttributes = attributes();
304  const QStringRef uid = elementAttributes.value( QLatin1String( "uid" ) );
305  if ( uid.isEmpty() ) {
306  raiseError( QLatin1String( "ContactGroupReference is missing a uid" ) );
307  return false;
308  }
309 
310  reference.setUid( uid.toString() );
311 
312  return true;
313 }
314 
315 bool ContactGroupTool::convertFromXml( QIODevice *device, ContactGroup &group,
316  QString *errorMessage )
317 {
318  Q_UNUSED( errorMessage );
319 
320  XmlContactGroupReader reader;
321 
322  bool ok = reader.read( device, group );
323 
324  if ( !ok && errorMessage != 0 ) {
325  *errorMessage = reader.errorString();
326  }
327 
328  return ok;
329 }
330 
331 bool ContactGroupTool::convertToXml( const ContactGroup &group, QIODevice *device,
332  QString *errorMessage )
333 {
334  Q_UNUSED( errorMessage );
335 
336  XmlContactGroupWriter writer;
337  writer.write( group, device );
338 
339  return true;
340 }
341 
342 bool ContactGroupTool::convertFromXml( QIODevice *device, QList<ContactGroup> &groupList,
343  QString *errorMessage )
344 {
345  Q_UNUSED( errorMessage );
346 
347  XmlContactGroupReader reader;
348 
349  bool ok = reader.read( device, groupList );
350 
351  if ( !ok && errorMessage != 0 ) {
352  *errorMessage = reader.errorString();
353  }
354 
355  return ok;
356 }
357 
358 bool ContactGroupTool::convertToXml( const QList<ContactGroup> &groupList,
359  QIODevice *device, QString *errorMessage )
360 {
361  Q_UNUSED( errorMessage );
362 
363  XmlContactGroupWriter writer;
364  writer.write( groupList, device );
365 
366  return true;
367 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:29:40 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kabc

Skip menu "kabc"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal