diff --git a/kabc/contactgroup.cpp b/kabc/contactgroup.cpp index ae660c9db..17cb4cfc0 100644 --- a/kabc/contactgroup.cpp +++ b/kabc/contactgroup.cpp @@ -1,395 +1,474 @@ /* This file is part of libkabc. Copyright (c) 2008 Tobias Koenig 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 "contactgroup.h" #include #include #include #include using namespace KABC; -class ContactGroup::Reference::ReferencePrivate : public QSharedData +class ContactGroup::ContactReference::ContactReferencePrivate : public QSharedData { public: - ReferencePrivate() + ContactReferencePrivate() : QSharedData() { } - ReferencePrivate( const ReferencePrivate &other ) + ContactReferencePrivate( const ContactReferencePrivate &other ) : QSharedData( other ) { mUid = other.mUid; mPreferredEmail = other.mPreferredEmail; mCustoms = other.mCustoms; } QString mUid; QString mPreferredEmail; QMap mCustoms; }; -ContactGroup::Reference::Reference() - : d( new ReferencePrivate ) +ContactGroup::ContactReference::ContactReference() + : d( new ContactReferencePrivate ) { } -ContactGroup::Reference::Reference( const Reference &other ) +ContactGroup::ContactReference::ContactReference( const ContactReference &other ) : d( other.d ) { } -ContactGroup::Reference::Reference( const QString &uid ) - : d( new ReferencePrivate ) +ContactGroup::ContactReference::ContactReference( const QString &uid ) + : d( new ContactReferencePrivate ) { d->mUid = uid; } -ContactGroup::Reference::~Reference() +ContactGroup::ContactReference::~ContactReference() { } -void ContactGroup::Reference::setUid( const QString &uid ) +void ContactGroup::ContactReference::setUid( const QString &uid ) { d->mUid = uid; } -QString ContactGroup::Reference::uid() const +QString ContactGroup::ContactReference::uid() const { return d->mUid; } -void ContactGroup::Reference::setPreferredEmail( const QString &email ) +void ContactGroup::ContactReference::setPreferredEmail( const QString &email ) { d->mPreferredEmail = email; } -QString ContactGroup::Reference::preferredEmail() const +QString ContactGroup::ContactReference::preferredEmail() const { return d->mPreferredEmail; } -void ContactGroup::Reference::insertCustom( const QString &key, const QString &value ) +void ContactGroup::ContactReference::insertCustom( const QString &key, const QString &value ) { d->mCustoms.insert( key, value ); } -void ContactGroup::Reference::removeCustom( const QString &key ) +void ContactGroup::ContactReference::removeCustom( const QString &key ) { d->mCustoms.remove( key ); } -QString ContactGroup::Reference::custom( const QString &key ) const +QString ContactGroup::ContactReference::custom( const QString &key ) const { return d->mCustoms.value( key ); } -ContactGroup::Reference &ContactGroup::Reference::operator=( const ContactGroup::Reference &other ) +ContactGroup::ContactReference &ContactGroup::ContactReference::operator=( const ContactGroup::ContactReference &other ) { if ( this != &other ) { d = other.d; } return *this; } -bool ContactGroup::Reference::operator==( const Reference &other ) const +bool ContactGroup::ContactReference::operator==( const ContactReference &other ) const { return d->mUid == other.d->mUid && d->mPreferredEmail == other.d->mPreferredEmail && d->mCustoms == other.d->mCustoms; } +class ContactGroup::ContactGroupReference::ContactGroupReferencePrivate : public QSharedData +{ + public: + ContactGroupReferencePrivate() + : QSharedData() + { + } + + ContactGroupReferencePrivate( const ContactGroupReferencePrivate &other ) + : QSharedData( other ) + { + mUid = other.mUid; + mCustoms = other.mCustoms; + } + + QString mUid; + QMap mCustoms; +}; + +ContactGroup::ContactGroupReference::ContactGroupReference() + : d( new ContactGroupReferencePrivate ) +{ +} + +ContactGroup::ContactGroupReference::ContactGroupReference( const ContactGroupReference &other ) + : d( other.d ) +{ +} + +ContactGroup::ContactGroupReference::ContactGroupReference( const QString &uid ) + : d( new ContactGroupReferencePrivate ) +{ + d->mUid = uid; +} + +ContactGroup::ContactGroupReference::~ContactGroupReference() +{ +} + +void ContactGroup::ContactGroupReference::setUid( const QString &uid ) +{ + d->mUid = uid; +} + +QString ContactGroup::ContactGroupReference::uid() const +{ + return d->mUid; +} + +void ContactGroup::ContactGroupReference::insertCustom( const QString &key, const QString &value ) +{ + d->mCustoms.insert( key, value ); +} + +void ContactGroup::ContactGroupReference::removeCustom( const QString &key ) +{ + d->mCustoms.remove( key ); +} + +QString ContactGroup::ContactGroupReference::custom( const QString &key ) const +{ + return d->mCustoms.value( key ); +} + +ContactGroup::ContactGroupReference &ContactGroup::ContactGroupReference::operator=( const ContactGroup::ContactGroupReference &other ) +{ + if ( this != &other ) { + d = other.d; + } + + return *this; +} + +bool ContactGroup::ContactGroupReference::operator==( const ContactGroupReference &other ) const +{ + return d->mUid == other.d->mUid && + d->mCustoms == other.d->mCustoms; +} + class ContactGroup::Data::DataPrivate : public QSharedData { public: DataPrivate() : QSharedData() { } DataPrivate( const DataPrivate &other ) : QSharedData( other ) { mName = other.mName; mEmail = other.mEmail; mCustoms = other.mCustoms; } QString mName; QString mEmail; QMap mCustoms; }; ContactGroup::Data::Data() : d( new DataPrivate ) { } ContactGroup::Data::Data( const Data &other ) : d( other.d ) { } ContactGroup::Data::Data( const QString &name, const QString &email ) : d( new DataPrivate ) { d->mName = name; d->mEmail = email; } ContactGroup::Data::~Data() { } void ContactGroup::Data::setName( const QString &name ) { d->mName = name; } QString ContactGroup::Data::name() const { return d->mName; } void ContactGroup::Data::setEmail( const QString &email ) { d->mEmail = email; } QString ContactGroup::Data::email() const { return d->mEmail; } void ContactGroup::Data::insertCustom( const QString &key, const QString &value ) { d->mCustoms.insert( key, value ); } void ContactGroup::Data::removeCustom( const QString &key ) { d->mCustoms.remove( key ); } QString ContactGroup::Data::custom( const QString &key ) const { return d->mCustoms.value( key ); } ContactGroup::Data &ContactGroup::Data::operator=( const ContactGroup::Data &other ) { if ( this != &other ) { d = other.d; } return *this; } bool ContactGroup::Data::operator==( const Data &other ) const { return d->mName == other.d->mName && d->mEmail == other.d->mEmail && d->mCustoms == other.d->mCustoms; } class ContactGroup::Private : public QSharedData { public: Private() : QSharedData(), mIdentifier( QUuid::createUuid().toString() ) { } Private( const Private &other ) : QSharedData( other ) { mIdentifier = other.mIdentifier; mName = other.mName; - mReferences = other.mReferences; + mContactReferences = other.mContactReferences; + mContactGroupReferences = other.mContactGroupReferences; mDataObjects = other.mDataObjects; - mSubgroups = other.mSubgroups; } QString mIdentifier; QString mName; - ContactGroup::Reference::List mReferences; + ContactGroup::ContactReference::List mContactReferences; + ContactGroup::ContactGroupReference::List mContactGroupReferences; ContactGroup::Data::List mDataObjects; - ContactGroup::List mSubgroups; }; ContactGroup::ContactGroup() : d( new Private ) { } ContactGroup::ContactGroup( const ContactGroup &other ) : d( other.d ) { } ContactGroup::ContactGroup( const QString &name ) : d( new Private ) { d->mName = name; } ContactGroup::~ContactGroup() { } void ContactGroup::setName( const QString &name ) { d->mName = name; } QString ContactGroup::name() const { return d->mName; } void ContactGroup::setId( const QString &id ) { d->mIdentifier = id; } QString ContactGroup::id() const { return d->mIdentifier; } unsigned int ContactGroup::count() const { - return d->mReferences.count() + d->mDataObjects.count(); + return d->mContactReferences.count() + d->mDataObjects.count(); +} + +unsigned int ContactGroup::contactReferenceCount() const +{ + return d->mContactReferences.count(); } -unsigned int ContactGroup::referencesCount() const +unsigned int ContactGroup::contactGroupReferenceCount() const { - return d->mReferences.count(); + return d->mContactGroupReferences.count(); } unsigned int ContactGroup::dataCount() const { return d->mDataObjects.count(); } -unsigned int ContactGroup::subgroupCount() const +ContactGroup::ContactReference &ContactGroup::contactReference( unsigned int index ) { - return d->mSubgroups.count(); + Q_ASSERT_X( index < (unsigned int)d->mContactReferences.count(), "contactReference()", "index out of range" ); + + return d->mContactReferences[ index ]; } -ContactGroup::Reference &ContactGroup::reference( unsigned int index ) +const ContactGroup::ContactReference &ContactGroup::contactReference( unsigned int index ) const { - Q_ASSERT_X( index < (unsigned int)d->mReferences.count(), "reference()", "index out of range" ); + Q_ASSERT_X( index < (unsigned int)d->mContactReferences.count(), "contactReference()", "index out of range" ); - return d->mReferences[ index ]; + return d->mContactReferences[ index ]; } -const ContactGroup::Reference &ContactGroup::reference( unsigned int index ) const +ContactGroup::ContactGroupReference &ContactGroup::contactGroupReference( unsigned int index ) { - Q_ASSERT_X( index < (unsigned int)d->mReferences.count(), "reference()", "index out of range" ); + Q_ASSERT_X( index < (unsigned int)d->mContactGroupReferences.count(), "contactGroupReference()", "index out of range" ); - return d->mReferences[ index ]; + return d->mContactGroupReferences[ index ]; } -ContactGroup::Data &ContactGroup::data( unsigned int index ) +const ContactGroup::ContactGroupReference &ContactGroup::contactGroupReference( unsigned int index ) const { - Q_ASSERT_X( index < (unsigned int)d->mDataObjects.count(), "data()", "index out of range" ); + Q_ASSERT_X( index < (unsigned int)d->mContactGroupReferences.count(), "contactGroupReference()", "index out of range" ); - return d->mDataObjects[ index ]; + return d->mContactGroupReferences[ index ]; } -const ContactGroup::Data &ContactGroup::data( unsigned int index ) const +ContactGroup::Data &ContactGroup::data( unsigned int index ) { Q_ASSERT_X( index < (unsigned int)d->mDataObjects.count(), "data()", "index out of range" ); return d->mDataObjects[ index ]; } -ContactGroup &ContactGroup::subgroup( unsigned int index ) +const ContactGroup::Data &ContactGroup::data( unsigned int index ) const { - Q_ASSERT_X( index < (unsigned int)d->mSubgroups.count(), "subgroup()", "index out of range" ); + Q_ASSERT_X( index < (unsigned int)d->mDataObjects.count(), "data()", "index out of range" ); - return d->mSubgroups[ index ]; + return d->mDataObjects[ index ]; } -const ContactGroup &ContactGroup::subgroup( unsigned int index ) const +void ContactGroup::append( const ContactReference &reference ) { - Q_ASSERT_X( index < (unsigned int)d->mSubgroups.count(), "subgroup()", "index out of range" ); - - return d->mSubgroups[ index ]; + d->mContactReferences.append( reference ); } -void ContactGroup::append( const Reference &reference ) +void ContactGroup::append( const ContactGroupReference &reference ) { - d->mReferences.append( reference ); + d->mContactGroupReferences.append( reference ); } void ContactGroup::append( const Data &data ) { d->mDataObjects.append( data ); } -void ContactGroup::append( const ContactGroup &subgroup ) +void ContactGroup::remove( const ContactReference &reference ) { - d->mSubgroups.append( subgroup ); + d->mContactReferences.removeOne( reference ); } -void ContactGroup::remove( const Reference &reference ) +void ContactGroup::remove( const ContactGroupReference &reference ) { - d->mReferences.removeOne( reference ); + d->mContactGroupReferences.removeOne( reference ); } void ContactGroup::remove( const Data &data ) { d->mDataObjects.removeOne( data ); } -void ContactGroup::remove( const ContactGroup &subgroup ) -{ - d->mSubgroups.removeOne( subgroup ); -} - ContactGroup &ContactGroup::operator=( const ContactGroup &other ) { if ( this != &other ) { d = other.d; } return *this; } bool ContactGroup::operator==( const ContactGroup &other ) const { return d->mIdentifier == other.d->mIdentifier && d->mName == other.d->mName && - d->mReferences == other.d->mReferences && - d->mDataObjects == other.d->mDataObjects && - d->mSubgroups == other.d->mSubgroups; + d->mContactReferences == other.d->mContactReferences && + d->mContactGroupReferences == other.d->mContactGroupReferences && + d->mDataObjects == other.d->mDataObjects; } QString ContactGroup::mimeType() { return QLatin1String( "application/x-vnd.kde.contactgroup" ); } diff --git a/kabc/contactgroup.h b/kabc/contactgroup.h index bca03e0d1..434461793 100644 --- a/kabc/contactgroup.h +++ b/kabc/contactgroup.h @@ -1,371 +1,448 @@ /* This file is part of libkabc. Copyright (c) 2008 Tobias Koenig 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. */ #ifndef KABC_CONTACTGROUP_H #define KABC_CONTACTGROUP_H #include #include #include "kabc_export.h" class QString; namespace KABC { /** * This class represents a group of contacts. * * It can contain two types of contacts, either a reference * or data. * The reference entry is just an unique identifier which * identifies the real contact in the system. * The data entry contains a name and an email address. * * @since 4.2 */ class KABC_EXPORT ContactGroup { public: /** * This class represents a contact reference */ - class KABC_EXPORT Reference + class KABC_EXPORT ContactReference { public: /** * A list of contact references. */ - typedef QList List; + typedef QList List; /** * Creates an empty contact reference. */ - Reference(); + ContactReference(); /** * Creates a contact reference from an @p other reference. */ - Reference( const Reference &other ); + ContactReference( const ContactReference &other ); /** * Creates a contact reference for the given contact @p uid. */ - Reference( const QString &uid ); + ContactReference( const QString &uid ); /** * Destroys the contact reference. */ - ~Reference(); + ~ContactReference(); /** * Sets the contact uid of the contact reference. */ void setUid( const QString &uid ); /** * Returns the contact uid of the contact reference. */ QString uid() const; /** * Sets the preferred email address. */ void setPreferredEmail( const QString &email ); /** * Returns the preferred email address, or an empty string * if no preferred email address is set. */ QString preferredEmail() const; /** * Inserts a custom entry. * If an entry with the same @p key already exists, it is * overwritten. * * @param key The unique key. * @param value The value. */ void insertCustom( const QString &key, const QString &value ); /** * Removes the custom entry with the given @p key. */ void removeCustom( const QString &key ); /** * Returns the value for the given @p key, or an empty string * if the entry for that key does not exists. */ QString custom( const QString &key ) const; /** * @internal */ - Reference &operator=( const Reference & ); + ContactReference &operator=( const ContactReference & ); /** * @internal */ - bool operator==( const Reference & ) const; + bool operator==( const ContactReference & ) const; private: - class ReferencePrivate; - QSharedDataPointer d; + class ContactReferencePrivate; + QSharedDataPointer d; + }; + + /** + * This class represents a contact group reference + */ + class KABC_EXPORT ContactGroupReference + { + public: + /** + * A list of contact group references. + */ + typedef QList List; + + /** + * Creates an empty contact group reference. + */ + ContactGroupReference(); + + /** + * Creates a contact group reference from an @p other reference. + */ + ContactGroupReference( const ContactGroupReference &other ); + + /** + * Creates a contact group reference for the given contact group @p uid. + */ + ContactGroupReference( const QString &uid ); + + /** + * Destroys the contact group reference. + */ + ~ContactGroupReference(); + + /** + * Sets the contact group uid of the contact group reference. + */ + void setUid( const QString &uid ); + + /** + * Returns the contact group uid of the contact group reference. + */ + QString uid() const; + + /** + * Inserts a custom entry. + * If an entry with the same @p key already exists, it is + * overwritten. + * + * @param key The unique key. + * @param value The value. + */ + void insertCustom( const QString &key, const QString &value ); + + /** + * Removes the custom entry with the given @p key. + */ + void removeCustom( const QString &key ); + + /** + * Returns the value for the given @p key, or an empty string + * if the entry for that key does not exists. + */ + QString custom( const QString &key ) const; + + /** + * @internal + */ + ContactGroupReference &operator=( const ContactGroupReference & ); + + /** + * @internal + */ + bool operator==( const ContactGroupReference & ) const; + + private: + class ContactGroupReferencePrivate; + QSharedDataPointer d; }; /** * This class represents a contact data object */ class KABC_EXPORT Data { public: /** * A list of contact data. */ typedef QList List; /** * Creates an empty contact data object. */ Data(); /** * Creates a contact data object from an @p other data object. */ Data( const Data &other ); /** * Creates a contact data object with the given @p name and @p email address. */ Data( const QString &name, const QString &email ); /** * Destroys the contact data object. */ ~Data(); /** * Sets the @p name of the contact data object. */ void setName( const QString &name ); /** * Returns the name of the contact data object. */ QString name() const; /** * Sets the @p email address of the contact data object. */ void setEmail( const QString &email ); /** * Returns the email address of the contact data object. */ QString email() const; /** * Inserts a custom entry. * If an entry with the same @p key already exists, it is * overwritten. * * @param key The unique key. * @param value The value. */ void insertCustom( const QString &key, const QString &value ); /** * Removes the custom entry with the given @p key. */ void removeCustom( const QString &key ); /** * Returns the value for the given @p key, or an empty string * if the entry for that key does not exists. */ QString custom( const QString &key ) const; /** * @internal */ Data &operator=( const Data & ); /** * @internal */ bool operator==( const Data & ) const; private: class DataPrivate; QSharedDataPointer d; }; /** * A list of contact groups. */ typedef QList List; /** * Creates an empty contact group. */ ContactGroup(); /** * Creates a contact group from an @p other group. */ ContactGroup( const ContactGroup &other ); /** * Creates a contact group with the given name. */ ContactGroup( const QString &name ); /** * Destroys the contact group. */ ~ContactGroup(); /** * Sets the unique @p id of the contact group. */ void setId( const QString &id ); /** * Returns the unique id of the contact group. */ QString id() const; /** * Sets the i18n'd @p name of the contact group. */ void setName( const QString &name ); /** * Returns the i18n'd name of the contact group. */ QString name() const; /** * Returns the number of contacts in this group. * That includes the contact references and contact data. */ unsigned int count() const; /** * Returns the number of contact references in this group. */ - unsigned int referencesCount() const; + unsigned int contactReferenceCount() const; + + /** + * Returns the number of group references in this group. + */ + unsigned int contactGroupReferenceCount() const; /** * Returns the number of contact data objects in this group. */ unsigned int dataCount() const; /** - * Returns the number of subgroups in this group. + * Returns the contact reference at the given @p index. */ - unsigned int subgroupCount() const; + ContactReference &contactReference( unsigned int index ); /** * Returns the contact reference at the given @p index. */ - Reference &reference( unsigned int index ); + const ContactReference &contactReference( unsigned int index ) const; /** - * Returns the contact reference at the given @p index. + * Returns the contact group reference at the given @p index. */ - const Reference &reference( unsigned int index ) const; + ContactGroupReference &contactGroupReference( unsigned int index ); /** - * Returns the contact data object at the given @p index. + * Returns the contact group reference at the given @p index. */ - Data &data( unsigned int index ); + const ContactGroupReference &contactGroupReference( unsigned int index ) const; /** * Returns the contact data object at the given @p index. */ - const Data &data( unsigned int index ) const; + Data &data( unsigned int index ); /** - * Returns the subgroup at the given @p index. + * Returns the contact data object at the given @p index. */ - ContactGroup &subgroup( unsigned int index ); + const Data &data( unsigned int index ) const; /** - * Returns the subgroup at the given @p index. + * Appends a new contact @p reference to the contact group. */ - const ContactGroup &subgroup( unsigned int index ) const; + void append( const ContactReference &reference ); /** - * Appends a new contact @p reference to the contact group. + * Appends a new contact group @p reference to the contact group. */ - void append( const Reference &reference ); + void append( const ContactGroupReference &reference ); /** * Appends a new contact @p data object to the contact group. */ void append( const Data &data ); /** - * Appends a new @p subgroup object to the contact group. + * Removes the given contact @p reference from the contact group. */ - void append( const ContactGroup &subgroup ); + void remove( const ContactReference &reference ); /** - * Removes the given contact @p reference from the contact group. + * Removes the given contact group @p reference from the contact group. */ - void remove( const Reference &reference ); + void remove( const ContactGroupReference &reference ); /** * Removes the given contact @p data object from the contact group. */ void remove( const Data &data ); - /** - * Removes the given @p subgroup object from the contact group. - */ - void remove( const ContactGroup &subgroup ); - /** * @internal */ ContactGroup &operator=( const ContactGroup & ); /** * @internal */ bool operator==( const ContactGroup & ) const; /** * Returns the MIME type used for Contact Groups */ static QString mimeType(); private: class Private; QSharedDataPointer d; }; } #endif diff --git a/kabc/contactgrouptool.cpp b/kabc/contactgrouptool.cpp index 99c22bb15..729c13008 100644 --- a/kabc/contactgrouptool.cpp +++ b/kabc/contactgrouptool.cpp @@ -1,341 +1,367 @@ /* This file is part of libkabc. Copyright (c) 2008 Tobias Koenig Copyright (c) 2008 Kevin Krammer 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 "contactgrouptool.h" #include "contactgroup.h" #include #include #include #include #include using namespace KABC; class XmlContactGroupWriter : public QXmlStreamWriter { public: XmlContactGroupWriter(); void write( const ContactGroup &group, QIODevice *device ); void write( const QList &groupLis, QIODevice *device ); private: void writeGroup( const ContactGroup &group ); - void writeReference( const ContactGroup::Reference & ); + void writeContactReference( const ContactGroup::ContactReference & ); + void writeContactGroupReference( const ContactGroup::ContactGroupReference & ); void writeData( const ContactGroup::Data & ); }; XmlContactGroupWriter::XmlContactGroupWriter() { setAutoFormatting( true ); } void XmlContactGroupWriter::write( const ContactGroup &group, QIODevice *device ) { setDevice( device ); writeStartDocument(); writeGroup( group ); writeEndDocument(); } void XmlContactGroupWriter::write( const QList &groupList, QIODevice *device ) { setDevice( device ); writeStartDocument(); writeStartElement( "contactGroupList" ); foreach ( const ContactGroup & group, groupList ) { writeGroup( group ); } writeEndElement(); writeEndDocument(); } void XmlContactGroupWriter::writeGroup( const ContactGroup &group ) { writeStartElement( "contactGroup" ); writeAttribute( "uid", group.id() ); writeAttribute( "name", group.name() ); - for ( uint i = 0; i < group.referencesCount(); ++i ) { - writeReference( group.reference( i ) ); + for ( uint i = 0; i < group.contactReferenceCount(); ++i ) { + writeContactReference( group.contactReference( i ) ); } - for ( uint i = 0; i < group.dataCount(); ++i ) { - writeData( group.data( i ) ); + for ( uint i = 0; i < group.contactGroupReferenceCount(); ++i ) { + writeContactGroupReference( group.contactGroupReference( i ) ); } - for ( uint i = 0; i < group.subgroupCount(); ++i ) { - writeGroup( group.subgroup( i ) ); + for ( uint i = 0; i < group.dataCount(); ++i ) { + writeData( group.data( i ) ); } writeEndElement(); } -void XmlContactGroupWriter::writeReference( const ContactGroup::Reference &reference ) +void XmlContactGroupWriter::writeContactReference( const ContactGroup::ContactReference &reference ) { writeStartElement( "contactReference" ); writeAttribute( "uid", reference.uid() ); if ( !reference.preferredEmail().isEmpty() ) { writeAttribute( "preferredEmail", reference.preferredEmail() ); } // TODO: customs writeEndElement(); } +void XmlContactGroupWriter::writeContactGroupReference( const ContactGroup::ContactGroupReference &reference ) +{ + writeStartElement( "contactGroupReference" ); + writeAttribute( "uid", reference.uid() ); + + // TODO: customs + + writeEndElement(); +} + void XmlContactGroupWriter::writeData( const ContactGroup::Data &data ) { writeStartElement( "contactData" ); writeAttribute( "name", data.name() ); writeAttribute( "email", data.email() ); // TODO: customs writeEndElement(); } class XmlContactGroupReader : public QXmlStreamReader { public: XmlContactGroupReader(); bool read( QIODevice *device, ContactGroup &group ); bool read( QIODevice *device, QList &groupList ); private: bool readGroup( ContactGroup &group ); - bool readReference( ContactGroup::Reference &reference ); + bool readContactReference( ContactGroup::ContactReference &reference ); + bool readContactGroupReference( ContactGroup::ContactGroupReference &reference ); bool readData( ContactGroup::Data &data ); }; XmlContactGroupReader::XmlContactGroupReader() { } bool XmlContactGroupReader::read( QIODevice *device, ContactGroup &group ) { setDevice( device ); while ( !atEnd() ) { readNext(); if ( isStartElement() ) { if ( name() == QLatin1String( "contactGroup" ) ) { return readGroup( group ); } else { raiseError( "The document does not describe a ContactGroup" ); } } } return error() == NoError; } bool XmlContactGroupReader::read( QIODevice *device, QList &groupList ) { setDevice( device ); int depth = 0; while ( !atEnd() ) { readNext(); if ( isStartElement() ) { ++depth; if ( depth == 1 ) { if ( name() == QLatin1String( "contactGroupList" ) ) { continue; } else { raiseError( "The document does not describe a list of ContactGroup" ); } } else if ( depth == 2 ) { if ( name() == QLatin1String( "contactGroup" ) ) { ContactGroup group; if ( !readGroup( group ) ) { return false; } groupList.append( group ); } else { raiseError( "The document does not describe a list of ContactGroup" ); } } } if ( isEndElement() ) { --depth; } } return error() == NoError; } bool XmlContactGroupReader::readGroup( ContactGroup &group ) { const QXmlStreamAttributes elementAttributes = attributes(); const QStringRef uid = elementAttributes.value( "uid" ); if ( uid.isEmpty() ) { raiseError( "ContactGroup is missing a uid" ); return false; } const QStringRef groupName = elementAttributes.value( "name" ); if ( groupName.isEmpty() ) { raiseError( "ContactGroup is missing a name" ); return false; } group.setId( uid.toString() ); group.setName( groupName.toString() ); while ( !atEnd() ) { readNext(); if ( isStartElement() ) { if ( name() == QLatin1String( "contactData" ) ) { ContactGroup::Data data; if ( !readData( data ) ) { return false; } group.append( data ); } else if ( name() == QLatin1String( "contactReference" ) ) { - ContactGroup::Reference reference; - if ( !readReference( reference ) ) { + ContactGroup::ContactReference reference; + if ( !readContactReference( reference ) ) { return false; } group.append( reference ); - } else if ( name() == QLatin1String( "contactGroup" ) ) { - ContactGroup subgroup; - if ( !readGroup( subgroup ) ) { + } else if ( name() == QLatin1String( "contactGroupReference" ) ) { + ContactGroup::ContactGroupReference reference; + if ( !readContactGroupReference( reference ) ) { return false; } - group.append( subgroup ); + group.append( reference ); } else { raiseError( "The document does not describe a ContactGroup" ); } } if ( isEndElement() ) { if ( name() == QLatin1String( "contactGroup" ) ) { return true; } } } return false; } bool XmlContactGroupReader::readData( ContactGroup::Data &data ) { const QXmlStreamAttributes elementAttributes = attributes(); const QStringRef email = elementAttributes.value( "email" ); if ( email.isEmpty() ) { raiseError( "ContactData is missing an email address" ); return false; } const QStringRef name = elementAttributes.value( "name" ); if ( name.isEmpty() ) { raiseError( "ContactData is missing a name" ); return false; } data.setName( name.toString() ); data.setEmail( email.toString() ); return true; } -bool XmlContactGroupReader::readReference( ContactGroup::Reference &reference ) +bool XmlContactGroupReader::readContactReference( ContactGroup::ContactReference &reference ) { const QXmlStreamAttributes elementAttributes = attributes(); const QStringRef uid = elementAttributes.value( "uid" ); if ( uid.isEmpty() ) { raiseError( "ContactReference is missing a uid" ); return false; } reference.setUid( uid.toString() ); return true; } +bool XmlContactGroupReader::readContactGroupReference( ContactGroup::ContactGroupReference &reference ) +{ + const QXmlStreamAttributes elementAttributes = attributes(); + const QStringRef uid = elementAttributes.value( "uid" ); + if ( uid.isEmpty() ) { + raiseError( "ContactGroupReference is missing a uid" ); + return false; + } + + reference.setUid( uid.toString() ); + + return true; +} + bool ContactGroupTool::convertFromXml( QIODevice *device, ContactGroup &group, QString *errorMessage ) { Q_UNUSED( errorMessage ); XmlContactGroupReader reader; bool ok = reader.read( device, group ); if ( !ok && errorMessage != 0 ) { *errorMessage = reader.errorString(); } return ok; } bool ContactGroupTool::convertToXml( const ContactGroup &group, QIODevice *device, QString *errorMessage ) { Q_UNUSED( errorMessage ); XmlContactGroupWriter writer; writer.write( group, device ); return true; } bool ContactGroupTool::convertFromXml( QIODevice *device, QList &groupList, QString *errorMessage ) { Q_UNUSED( errorMessage ); XmlContactGroupReader reader; bool ok = reader.read( device, groupList ); if ( !ok && errorMessage != 0 ) { *errorMessage = reader.errorString(); } return ok; } bool ContactGroupTool::convertToXml( const QList &groupList, QIODevice *device, QString *errorMessage ) { Q_UNUSED( errorMessage ); XmlContactGroupWriter writer; writer.write( groupList, device ); return true; } diff --git a/kabc/tests/contactgrouptest.cpp b/kabc/tests/contactgrouptest.cpp index e04a0a750..82741e66f 100644 --- a/kabc/tests/contactgrouptest.cpp +++ b/kabc/tests/contactgrouptest.cpp @@ -1,338 +1,374 @@ /* This file is part of libkabc. Copyright (c) 2008 Kevin Krammer 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 #include "kabc/contactgroup.h" #include "kabc/contactgrouptool.h" #include using namespace KABC; class ContactGroupTest : public QObject { Q_OBJECT private Q_SLOTS: - void contactGroupReference(); + void contactGroupContactReference(); + void contactGroupContactGroupReference(); void contactGroupData(); void contactGroup(); - void subgroup(); void testGroupRoundTrip(); void testGroupListRoundTrip(); }; QTEST_KDEMAIN( ContactGroupTest, NoGUI ) -void ContactGroupTest::contactGroupReference() +void ContactGroupTest::contactGroupContactReference() { const QString uid( "MyReferenceId" ); const QString preferredEMail( "tokoe@kde.org" ); const QString customKey( "MyCustomKey" ); const QString customValue( "MyCustomValue" ); // uid test { - ContactGroup::Reference ref( uid ); + ContactGroup::ContactReference ref( uid ); QCOMPARE( ref.uid(), uid ); } // uid test { - ContactGroup::Reference ref; + ContactGroup::ContactReference ref; ref.setUid( uid ); QCOMPARE( ref.uid(), uid ); } // preferredEmail test { - ContactGroup::Reference ref( uid ); + ContactGroup::ContactReference ref( uid ); ref.setPreferredEmail( preferredEMail ); QCOMPARE( ref.preferredEmail(), preferredEMail ); } // custom test { - ContactGroup::Reference ref( uid ); + ContactGroup::ContactReference ref( uid ); ref.insertCustom( customKey, customValue ); QCOMPARE( ref.custom( customKey ), customValue ); ref.removeCustom( customKey ); QCOMPARE( ref.custom( customKey ), QString() ); } // assignment test { - ContactGroup::Reference ref( uid ); + ContactGroup::ContactReference ref( uid ); ref.setPreferredEmail( preferredEMail ); ref.insertCustom( customKey, customValue ); - ContactGroup::Reference ref2( ref ); + ContactGroup::ContactReference ref2( ref ); QCOMPARE( ref.uid(), ref2.uid() ); QCOMPARE( ref.preferredEmail(), ref2.preferredEmail() ); QCOMPARE( ref.custom( customKey ), ref2.custom( customKey ) ); QVERIFY( ref == ref2 ); } // const test { - ContactGroup::Reference ref( uid ); + ContactGroup::ContactReference ref( uid ); ref.setPreferredEmail( preferredEMail ); ref.insertCustom( customKey, customValue ); - const ContactGroup::Reference constRef( ref ); + const ContactGroup::ContactReference constRef( ref ); constRef.uid(); constRef.preferredEmail(); constRef.custom( customKey ); } } +void ContactGroupTest::contactGroupContactGroupReference() +{ + const QString uid( "MyReferenceId" ); + const QString customKey( "MyCustomKey" ); + const QString customValue( "MyCustomValue" ); + + // uid test + { + ContactGroup::ContactGroupReference ref( uid ); + QCOMPARE( ref.uid(), uid ); + } + + // uid test + { + ContactGroup::ContactGroupReference ref; + ref.setUid( uid ); + QCOMPARE( ref.uid(), uid ); + } + + // custom test + { + ContactGroup::ContactGroupReference ref( uid ); + ref.insertCustom( customKey, customValue ); + QCOMPARE( ref.custom( customKey ), customValue ); + ref.removeCustom( customKey ); + QCOMPARE( ref.custom( customKey ), QString() ); + } + + // assignment test + { + ContactGroup::ContactGroupReference ref( uid ); + ref.insertCustom( customKey, customValue ); + + ContactGroup::ContactGroupReference ref2( ref ); + QCOMPARE( ref.uid(), ref2.uid() ); + QCOMPARE( ref.custom( customKey ), ref2.custom( customKey ) ); + + QVERIFY( ref == ref2 ); + } + + // const test + { + ContactGroup::ContactGroupReference ref( uid ); + ref.insertCustom( customKey, customValue ); + + const ContactGroup::ContactGroupReference constRef( ref ); + constRef.uid(); + constRef.custom( customKey ); + } +} + void ContactGroupTest::contactGroupData() { const QString name( "Tobias Koenig" ); const QString email( "tokoe@kde.org" ); const QString customKey( "MyCustomKey" ); const QString customValue( "MyCustomValue" ); // name/email test { ContactGroup::Data data( name, email ); QCOMPARE( data.name(), name ); QCOMPARE( data.email(), email ); } // name test { ContactGroup::Data data; data.setName( name ); QCOMPARE( data.name(), name ); } // email test { ContactGroup::Data data; data.setEmail( email ); QCOMPARE( data.email(), email ); } // custom test { ContactGroup::Data data( name, email ); data.insertCustom( customKey, customValue ); QCOMPARE( data.custom( customKey ), customValue ); data.removeCustom( customKey ); QCOMPARE( data.custom( customKey ), QString() ); } // assignment test { ContactGroup::Data data( name, email ); data.insertCustom( customKey, customValue ); ContactGroup::Data data2( data ); QCOMPARE( data.name(), data2.name() ); QCOMPARE( data.email(), data2.email() ); QCOMPARE( data.custom( customKey ), data2.custom( customKey ) ); QVERIFY( data == data2 ); } // const test { ContactGroup::Data data( name, email ); data.insertCustom( customKey, customValue ); const ContactGroup::Data constData( data ); constData.name(); constData.email(); constData.custom( customKey ); } } void ContactGroupTest::contactGroup() { const QString groupName( "MyGroupName" ); const QString groupId( "MyGroupID" ); const QString name( "Tobias Koenig" ); const QString email( "tokoe@kde.org" ); const QString uid( "MyUid" ); // name test { ContactGroup group( groupName ); QCOMPARE( group.name(), groupName ); } // id test { ContactGroup group( groupName ); group.setId( groupId ); QCOMPARE( group.id(), groupId ); } - // reference test + // contact reference test { ContactGroup group( groupName ); - QCOMPARE( group.referencesCount(), (unsigned int)0 ); + QCOMPARE( group.contactReferenceCount(), (unsigned int)0 ); - ContactGroup::Reference ref( uid ); + ContactGroup::ContactReference ref( uid ); ref.setPreferredEmail( email ); group.append( ref ); - QCOMPARE( group.referencesCount(), (unsigned int)1 ); + QCOMPARE( group.contactReferenceCount(), (unsigned int)1 ); + + const ContactGroup::ContactReference ref2 = group.contactReference( 0 ); + QCOMPARE( ref, ref2 ); + + group.remove( ref ); + QCOMPARE( group.contactReferenceCount(), (unsigned int)0 ); + } + + // contact group reference test + { + ContactGroup group( groupName ); + QCOMPARE( group.contactGroupReferenceCount(), (unsigned int)0 ); + + ContactGroup::ContactGroupReference ref( uid ); + + group.append( ref ); + QCOMPARE( group.contactGroupReferenceCount(), (unsigned int)1 ); - const ContactGroup::Reference ref2 = group.reference( 0 ); + const ContactGroup::ContactGroupReference ref2 = group.contactGroupReference( 0 ); QCOMPARE( ref, ref2 ); group.remove( ref ); - QCOMPARE( group.referencesCount(), (unsigned int)0 ); + QCOMPARE( group.contactGroupReferenceCount(), (unsigned int)0 ); } // data test { ContactGroup group( groupName ); QCOMPARE( group.dataCount(), (unsigned int)0 ); ContactGroup::Data data( name, email ); group.append( data ); QCOMPARE( group.dataCount(), (unsigned int)1 ); const ContactGroup::Data data2 = group.data( 0 ); QCOMPARE( data, data2 ); group.remove( data ); QCOMPARE( group.dataCount(), (unsigned int)0 ); } // mimetype test { ContactGroup group( groupName ); QCOMPARE( group.mimeType(), QLatin1String( "application/x-vnd.kde.contactgroup" ) ); } } -void ContactGroupTest::subgroup() -{ - ContactGroup subGroup( "SubGroup" ); - subGroup.append( ContactGroup::Reference( "334Fds" ) ); - subGroup.append( ContactGroup::Data( "John Doe", "doe@kde.org" ) ); - - ContactGroup group( "Toplevel" ); - QCOMPARE( group.referencesCount(), (unsigned int)0 ); - QCOMPARE( group.dataCount(), (unsigned int)0 ); - QCOMPARE( group.subgroupCount(), (unsigned int)0 ); - - group.append( ContactGroup::Reference( "Xggdjetw" ) ); - group.append( ContactGroup::Data( "Tobias Koenig", "tokoe@kde.org" ) ); - group.append( ContactGroup::Data( "Kevin Krammer", "kevin.krammer@gmx.at" ) ); - group.append( subGroup ); - - QCOMPARE( group.referencesCount(), (unsigned int)1 ); - QCOMPARE( group.dataCount(), (unsigned int)2 ); - QCOMPARE( group.subgroupCount(), (unsigned int)1 ); - QCOMPARE( group.count(), (unsigned int)3 ); - - const ContactGroup subGroup2 = group.subgroup( 0 ); - QCOMPARE( subGroup, subGroup2 ); - - group.remove( subGroup ); - QCOMPARE( group.subgroupCount(), (unsigned int)0 ); -} - void ContactGroupTest::testGroupRoundTrip() { // TODO should also test empty group - ContactGroup subGroup( "SubGroup" ); - subGroup.append( ContactGroup::Reference( "334Fds" ) ); - subGroup.append( ContactGroup::Data( "John Doe", "doe@kde.org" ) ); - ContactGroup group( "TestGroup" ); - group.append( ContactGroup::Reference( "Xggdjetw" ) ); + group.append( ContactGroup::ContactReference( "Xggdjetw" ) ); + group.append( ContactGroup::ContactGroupReference( "aaXggdjetw" ) ); group.append( ContactGroup::Data( "Tobias Koenig", "tokoe@kde.org" ) ); group.append( ContactGroup::Data( "Kevin Krammer", "kevin.krammer@gmx.at" ) ); - group.append( subGroup ); QBuffer buffer; buffer.open( QIODevice::WriteOnly ); QString errorMessage; bool result = ContactGroupTool::convertToXml( group, &buffer, &errorMessage ); QVERIFY( result ); QVERIFY( errorMessage.isEmpty() ); buffer.close(); QVERIFY( buffer.size() > 0 ); buffer.open( QIODevice::ReadOnly ); ContactGroup group2; result = ContactGroupTool::convertFromXml( &buffer, group2, &errorMessage ); QVERIFY( result ); QVERIFY( errorMessage.isEmpty() ); QCOMPARE( group, group2 ); } void ContactGroupTest::testGroupListRoundTrip() { // TODO should also test empty list ContactGroup::List list; ContactGroup group1( "TestGroup1" ); - group1.append( ContactGroup::Reference( "Xggdjetw" ) ); + group1.append( ContactGroup::ContactReference( "Xggdjetw" ) ); group1.append( ContactGroup::Data( "Tobias Koenig", "tokoe@kde.org" ) ); group1.append( ContactGroup::Data( "Kevin Krammer", "kevin.krammer@gmx.at" ) ); list.append( group1 ); ContactGroup group2( "TestGroup2" ); - group2.append( ContactGroup::Reference( "Xggdjetw" ) ); + group2.append( ContactGroup::ContactReference( "Xggdjetw" ) ); group2.append( ContactGroup::Data( "Tobias Koenig", "tokoe@kde.org" ) ); group2.append( ContactGroup::Data( "Kevin Krammer", "kevin.krammer@gmx.at" ) ); list.append( group2 ); QBuffer buffer; buffer.open( QIODevice::WriteOnly ); QString errorMessage; bool result = ContactGroupTool::convertToXml( list, &buffer, &errorMessage ); QVERIFY( result ); QVERIFY( errorMessage.isEmpty() ); buffer.close(); QVERIFY( buffer.size() > 0 ); buffer.open( QIODevice::ReadOnly ); ContactGroup::List list2; result = ContactGroupTool::convertFromXml( &buffer, list2, &errorMessage ); QVERIFY( result ); QVERIFY( errorMessage.isEmpty() ); QVERIFY( list2.size() == 2 ); QCOMPARE( list2[0], group1 ); QCOMPARE( list2[1], group2 ); } #include "contactgrouptest.moc" // kate: space-indent on; indent-width 2; replace-tabs on;