kabc
contactgrouptool.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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( "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( "contactGroup" );
00085 writeAttribute( "uid", group.id() );
00086 writeAttribute( "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( "contactReference" );
00106 writeAttribute( "uid", reference.uid() );
00107 if ( !reference.preferredEmail().isEmpty() ) {
00108 writeAttribute( "preferredEmail", reference.preferredEmail() );
00109 }
00110
00111
00112
00113 writeEndElement();
00114 }
00115
00116 void XmlContactGroupWriter::writeContactGroupReference( const ContactGroup::ContactGroupReference &reference )
00117 {
00118 writeStartElement( "contactGroupReference" );
00119 writeAttribute( "uid", reference.uid() );
00120
00121
00122
00123 writeEndElement();
00124 }
00125
00126 void XmlContactGroupWriter::writeData( const ContactGroup::Data &data )
00127 {
00128 writeStartElement( "contactData" );
00129 writeAttribute( "name", data.name() );
00130 writeAttribute( "email", data.email() );
00131
00132
00133
00134 writeEndElement();
00135 }
00136
00137 class XmlContactGroupReader : public QXmlStreamReader
00138 {
00139 public:
00140 XmlContactGroupReader();
00141
00142 bool read( QIODevice *device, ContactGroup &group );
00143 bool read( QIODevice *device, QList<ContactGroup> &groupList );
00144
00145 private:
00146 bool readGroup( ContactGroup &group );
00147 bool readContactReference( ContactGroup::ContactReference &reference );
00148 bool readContactGroupReference( ContactGroup::ContactGroupReference &reference );
00149 bool readData( ContactGroup::Data &data );
00150 };
00151
00152 XmlContactGroupReader::XmlContactGroupReader()
00153 {
00154 }
00155
00156 bool XmlContactGroupReader::read( QIODevice *device, ContactGroup &group )
00157 {
00158 setDevice( device );
00159
00160 while ( !atEnd() ) {
00161 readNext();
00162 if ( isStartElement() ) {
00163 if ( name() == QLatin1String( "contactGroup" ) ) {
00164 return readGroup( group );
00165 } else {
00166 raiseError( "The document does not describe a ContactGroup" );
00167 }
00168 }
00169 }
00170
00171 return error() == NoError;
00172 }
00173
00174 bool XmlContactGroupReader::read( QIODevice *device, QList<ContactGroup> &groupList )
00175 {
00176 setDevice( device );
00177
00178 int depth = 0;
00179
00180 while ( !atEnd() ) {
00181 readNext();
00182 if ( isStartElement() ) {
00183 ++depth;
00184 if ( depth == 1 ) {
00185 if ( name() == QLatin1String( "contactGroupList" ) ) {
00186 continue;
00187 } else {
00188 raiseError( "The document does not describe a list of ContactGroup" );
00189 }
00190 } else if ( depth == 2 ) {
00191 if ( name() == QLatin1String( "contactGroup" ) ) {
00192 ContactGroup group;
00193 if ( !readGroup( group ) ) {
00194 return false;
00195 }
00196
00197 groupList.append( group );
00198 } else {
00199 raiseError( "The document does not describe a list of ContactGroup" );
00200 }
00201 }
00202 }
00203
00204 if ( isEndElement() ) {
00205 --depth;
00206 }
00207 }
00208
00209 return error() == NoError;
00210 }
00211
00212 bool XmlContactGroupReader::readGroup( ContactGroup &group )
00213 {
00214 const QXmlStreamAttributes elementAttributes = attributes();
00215 const QStringRef uid = elementAttributes.value( "uid" );
00216 if ( uid.isEmpty() ) {
00217 raiseError( "ContactGroup is missing a uid" );
00218 return false;
00219 }
00220
00221 const QStringRef groupName = elementAttributes.value( "name" );
00222 if ( groupName.isEmpty() ) {
00223 raiseError( "ContactGroup is missing a name" );
00224 return false;
00225 }
00226
00227 group.setId( uid.toString() );
00228 group.setName( groupName.toString() );
00229
00230 while ( !atEnd() ) {
00231 readNext();
00232 if ( isStartElement() ) {
00233 if ( name() == QLatin1String( "contactData" ) ) {
00234 ContactGroup::Data data;
00235 if ( !readData( data ) ) {
00236 return false;
00237 }
00238 group.append( data );
00239 } else if ( name() == QLatin1String( "contactReference" ) ) {
00240 ContactGroup::ContactReference reference;
00241 if ( !readContactReference( reference ) ) {
00242 return false;
00243 }
00244 group.append( reference );
00245 } else if ( name() == QLatin1String( "contactGroupReference" ) ) {
00246 ContactGroup::ContactGroupReference reference;
00247 if ( !readContactGroupReference( reference ) ) {
00248 return false;
00249 }
00250 group.append( reference );
00251 } else {
00252 raiseError( "The document does not describe a ContactGroup" );
00253 }
00254 }
00255
00256 if ( isEndElement() ) {
00257 if ( name() == QLatin1String( "contactGroup" ) ) {
00258 return true;
00259 }
00260 }
00261 }
00262
00263 return false;
00264 }
00265
00266 bool XmlContactGroupReader::readData( ContactGroup::Data &data )
00267 {
00268 const QXmlStreamAttributes elementAttributes = attributes();
00269 const QStringRef email = elementAttributes.value( "email" );
00270 if ( email.isEmpty() ) {
00271 raiseError( "ContactData is missing an email address" );
00272 return false;
00273 }
00274
00275 const QStringRef name = elementAttributes.value( "name" );
00276 if ( name.isEmpty() ) {
00277 raiseError( "ContactData is missing a name" );
00278 return false;
00279 }
00280
00281 data.setName( name.toString() );
00282 data.setEmail( email.toString() );
00283
00284 return true;
00285 }
00286
00287 bool XmlContactGroupReader::readContactReference( ContactGroup::ContactReference &reference )
00288 {
00289 const QXmlStreamAttributes elementAttributes = attributes();
00290 const QStringRef uid = elementAttributes.value( "uid" );
00291 if ( uid.isEmpty() ) {
00292 raiseError( "ContactReference is missing a uid" );
00293 return false;
00294 }
00295
00296 reference.setUid( uid.toString() );
00297
00298 return true;
00299 }
00300
00301 bool XmlContactGroupReader::readContactGroupReference( ContactGroup::ContactGroupReference &reference )
00302 {
00303 const QXmlStreamAttributes elementAttributes = attributes();
00304 const QStringRef uid = elementAttributes.value( "uid" );
00305 if ( uid.isEmpty() ) {
00306 raiseError( "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 }