kabc
contactgrouptool.cpp
00001 /* 00002 This file is part of libkabc. 00003 Copyright (c) 2008 Tobias Koenig <tokoe@kde.org> 00004 Copyright (c) 2008 Kevin Krammer <kevin.krammer@gmx.at> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00021 00022 #include "contactgrouptool.h" 00023 #include "contactgroup.h" 00024 00025 #include <QtCore/QIODevice> 00026 #include <QtCore/QString> 00027 #include <QtCore/QDebug> 00028 00029 #include <QtCore/QXmlStreamReader> 00030 #include <QtCore/QXmlStreamWriter> 00031 00032 using namespace KABC; 00033 00034 class XmlContactGroupWriter : public QXmlStreamWriter 00035 { 00036 public: 00037 XmlContactGroupWriter(); 00038 00039 void write( const ContactGroup &group, QIODevice *device ); 00040 void write( const QList<ContactGroup> &groupLis, QIODevice *device ); 00041 00042 private: 00043 void writeGroup( const ContactGroup &group ); 00044 void writeContactReference( const ContactGroup::ContactReference & ); 00045 void writeContactGroupReference( const ContactGroup::ContactGroupReference & ); 00046 void writeData( const ContactGroup::Data & ); 00047 }; 00048 00049 XmlContactGroupWriter::XmlContactGroupWriter() 00050 { 00051 setAutoFormatting( true ); 00052 } 00053 00054 void XmlContactGroupWriter::write( const ContactGroup &group, QIODevice *device ) 00055 { 00056 setDevice( device ); 00057 00058 writeStartDocument(); 00059 00060 writeGroup( group ); 00061 00062 writeEndDocument(); 00063 } 00064 00065 void XmlContactGroupWriter::write( const QList<ContactGroup> &groupList, QIODevice *device ) 00066 { 00067 setDevice( device ); 00068 00069 writeStartDocument(); 00070 00071 writeStartElement( QLatin1String( "contactGroupList" ) ); 00072 00073 foreach ( const ContactGroup & group, groupList ) { 00074 writeGroup( group ); 00075 } 00076 00077 writeEndElement(); 00078 00079 writeEndDocument(); 00080 } 00081 00082 void XmlContactGroupWriter::writeGroup( const ContactGroup &group ) 00083 { 00084 writeStartElement( QLatin1String( "contactGroup" ) ); 00085 writeAttribute( QLatin1String( "uid" ), group.id() ); 00086 writeAttribute( QLatin1String( "name" ), group.name() ); 00087 00088 for ( uint i = 0; i < group.contactReferenceCount(); ++i ) { 00089 writeContactReference( group.contactReference( i ) ); 00090 } 00091 00092 for ( uint i = 0; i < group.contactGroupReferenceCount(); ++i ) { 00093 writeContactGroupReference( group.contactGroupReference( i ) ); 00094 } 00095 00096 for ( uint i = 0; i < group.dataCount(); ++i ) { 00097 writeData( group.data( i ) ); 00098 } 00099 00100 writeEndElement(); 00101 } 00102 00103 void XmlContactGroupWriter::writeContactReference( const ContactGroup::ContactReference &reference ) 00104 { 00105 writeStartElement( QLatin1String( "contactReference" ) ); 00106 writeAttribute( QLatin1String( "uid" ), reference.uid() ); 00107 if ( !reference.preferredEmail().isEmpty() ) { 00108 writeAttribute( QLatin1String( "preferredEmail" ), reference.preferredEmail() ); 00109 } 00110 00111 // TODO: customs 00112 00113 writeEndElement(); 00114 } 00115 00116 void XmlContactGroupWriter::writeContactGroupReference( 00117 const ContactGroup::ContactGroupReference &reference ) 00118 { 00119 writeStartElement( QLatin1String( "contactGroupReference" ) ); 00120 writeAttribute( QLatin1String( "uid" ), reference.uid() ); 00121 00122 // TODO: customs 00123 00124 writeEndElement(); 00125 } 00126 00127 void XmlContactGroupWriter::writeData( const ContactGroup::Data &data ) 00128 { 00129 writeStartElement( QLatin1String( "contactData" ) ); 00130 writeAttribute( QLatin1String( "name" ), data.name() ); 00131 writeAttribute( QLatin1String( "email" ), data.email() ); 00132 00133 // TODO: customs 00134 00135 writeEndElement(); 00136 } 00137 00138 class XmlContactGroupReader : public QXmlStreamReader 00139 { 00140 public: 00141 XmlContactGroupReader(); 00142 00143 bool read( QIODevice *device, ContactGroup &group ); 00144 bool read( QIODevice *device, QList<ContactGroup> &groupList ); 00145 00146 private: 00147 bool readGroup( ContactGroup &group ); 00148 bool readContactReference( ContactGroup::ContactReference &reference ); 00149 bool readContactGroupReference( ContactGroup::ContactGroupReference &reference ); 00150 bool readData( ContactGroup::Data &data ); 00151 }; 00152 00153 XmlContactGroupReader::XmlContactGroupReader() 00154 { 00155 } 00156 00157 bool XmlContactGroupReader::read( QIODevice *device, ContactGroup &group ) 00158 { 00159 setDevice( device ); 00160 00161 while ( !atEnd() ) { 00162 readNext(); 00163 if ( isStartElement() ) { 00164 if ( name() == QLatin1String( "contactGroup" ) ) { 00165 return readGroup( group ); 00166 } else { 00167 raiseError( QLatin1String( "The document does not describe a ContactGroup" ) ); 00168 } 00169 } 00170 } 00171 00172 return error() == NoError; 00173 } 00174 00175 bool XmlContactGroupReader::read( QIODevice *device, QList<ContactGroup> &groupList ) 00176 { 00177 setDevice( device ); 00178 00179 int depth = 0; 00180 00181 while ( !atEnd() ) { 00182 readNext(); 00183 if ( isStartElement() ) { 00184 ++depth; 00185 if ( depth == 1 ) { 00186 if ( name() == QLatin1String( "contactGroupList" ) ) { 00187 continue; 00188 } else { 00189 raiseError( QLatin1String( "The document does not describe a list of ContactGroup" ) ); 00190 } 00191 } else if ( depth == 2 ) { 00192 if ( name() == QLatin1String( "contactGroup" ) ) { 00193 ContactGroup group; 00194 if ( !readGroup( group ) ) { 00195 return false; 00196 } 00197 00198 groupList.append( group ); 00199 } else { 00200 raiseError( QLatin1String( "The document does not describe a list of ContactGroup" ) ); 00201 } 00202 } 00203 } 00204 00205 if ( isEndElement() ) { 00206 --depth; 00207 } 00208 } 00209 00210 return error() == NoError; 00211 } 00212 00213 bool XmlContactGroupReader::readGroup( ContactGroup &group ) 00214 { 00215 const QXmlStreamAttributes elementAttributes = attributes(); 00216 const QStringRef uid = elementAttributes.value( QLatin1String( "uid" ) ); 00217 if ( uid.isEmpty() ) { 00218 raiseError( QLatin1String( "ContactGroup is missing a uid" ) ); 00219 return false; 00220 } 00221 00222 const QStringRef groupName = elementAttributes.value( QLatin1String( "name" ) ); 00223 if ( groupName.isEmpty() ) { 00224 raiseError( QLatin1String( "ContactGroup is missing a name" ) ); 00225 return false; 00226 } 00227 00228 group.setId( uid.toString() ); 00229 group.setName( groupName.toString() ); 00230 00231 while ( !atEnd() ) { 00232 readNext(); 00233 if ( isStartElement() ) { 00234 if ( name() == QLatin1String( "contactData" ) ) { 00235 ContactGroup::Data data; 00236 if ( !readData( data ) ) { 00237 return false; 00238 } 00239 group.append( data ); 00240 } else if ( name() == QLatin1String( "contactReference" ) ) { 00241 ContactGroup::ContactReference reference; 00242 if ( !readContactReference( reference ) ) { 00243 return false; 00244 } 00245 group.append( reference ); 00246 } else if ( name() == QLatin1String( "contactGroupReference" ) ) { 00247 ContactGroup::ContactGroupReference reference; 00248 if ( !readContactGroupReference( reference ) ) { 00249 return false; 00250 } 00251 group.append( reference ); 00252 } else { 00253 raiseError( QLatin1String( "The document does not describe a ContactGroup" ) ); 00254 } 00255 } 00256 00257 if ( isEndElement() ) { 00258 if ( name() == QLatin1String( "contactGroup" ) ) { 00259 return true; 00260 } 00261 } 00262 } 00263 00264 return false; 00265 } 00266 00267 bool XmlContactGroupReader::readData( ContactGroup::Data &data ) 00268 { 00269 const QXmlStreamAttributes elementAttributes = attributes(); 00270 const QStringRef email = elementAttributes.value( QLatin1String( "email" ) ); 00271 if ( email.isEmpty() ) { 00272 raiseError( QLatin1String( "ContactData is missing an email address" ) ); 00273 return false; 00274 } 00275 00276 const QStringRef name = elementAttributes.value( QLatin1String( "name" ) ); 00277 00278 data.setName( name.toString() ); 00279 data.setEmail( email.toString() ); 00280 00281 return true; 00282 } 00283 00284 bool XmlContactGroupReader::readContactReference( ContactGroup::ContactReference &reference ) 00285 { 00286 const QXmlStreamAttributes elementAttributes = attributes(); 00287 const QStringRef uid = elementAttributes.value( QLatin1String( "uid" ) ); 00288 if ( uid.isEmpty() ) { 00289 raiseError( QLatin1String( "ContactReference is missing a uid" ) ); 00290 return false; 00291 } 00292 const QStringRef preferredEmail = elementAttributes.value( QLatin1String( "preferredEmail" ) ); 00293 00294 reference.setUid( uid.toString() ); 00295 reference.setPreferredEmail( preferredEmail.toString() ); 00296 00297 return true; 00298 } 00299 00300 bool XmlContactGroupReader::readContactGroupReference( 00301 ContactGroup::ContactGroupReference &reference ) 00302 { 00303 const QXmlStreamAttributes elementAttributes = attributes(); 00304 const QStringRef uid = elementAttributes.value( QLatin1String( "uid" ) ); 00305 if ( uid.isEmpty() ) { 00306 raiseError( QLatin1String( "ContactGroupReference is missing a uid" ) ); 00307 return false; 00308 } 00309 00310 reference.setUid( uid.toString() ); 00311 00312 return true; 00313 } 00314 00315 bool ContactGroupTool::convertFromXml( QIODevice *device, ContactGroup &group, 00316 QString *errorMessage ) 00317 { 00318 Q_UNUSED( errorMessage ); 00319 00320 XmlContactGroupReader reader; 00321 00322 bool ok = reader.read( device, group ); 00323 00324 if ( !ok && errorMessage != 0 ) { 00325 *errorMessage = reader.errorString(); 00326 } 00327 00328 return ok; 00329 } 00330 00331 bool ContactGroupTool::convertToXml( const ContactGroup &group, QIODevice *device, 00332 QString *errorMessage ) 00333 { 00334 Q_UNUSED( errorMessage ); 00335 00336 XmlContactGroupWriter writer; 00337 writer.write( group, device ); 00338 00339 return true; 00340 } 00341 00342 bool ContactGroupTool::convertFromXml( QIODevice *device, QList<ContactGroup> &groupList, 00343 QString *errorMessage ) 00344 { 00345 Q_UNUSED( errorMessage ); 00346 00347 XmlContactGroupReader reader; 00348 00349 bool ok = reader.read( device, groupList ); 00350 00351 if ( !ok && errorMessage != 0 ) { 00352 *errorMessage = reader.errorString(); 00353 } 00354 00355 return ok; 00356 } 00357 00358 bool ContactGroupTool::convertToXml( const QList<ContactGroup> &groupList, 00359 QIODevice *device, QString *errorMessage ) 00360 { 00361 Q_UNUSED( errorMessage ); 00362 00363 XmlContactGroupWriter writer; 00364 writer.write( groupList, device ); 00365 00366 return true; 00367 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 05:09:41 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 05:09:41 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.