Commit 1af1e98c authored by Volker Krause's avatar Volker Krause

- complete the attribute API in Entity

- remove CollectionModifyJob::setAttribute()

svn path=/trunk/KDE/kdepimlibs/; revision=788731
parent 66ae1701
......@@ -69,9 +69,8 @@ void CollectionModifyJob::doStart()
changes += " REMOTEID \"" + d->collection.remoteId().toUtf8() + '"';
if ( d->setPolicy )
changes += ' ' + ProtocolHelper::cachePolicyToByteArray( d->policy );
typedef QPair<QByteArray,QByteArray> QByteArrayPair;
foreach ( const QByteArrayPair bp, d->attributes )
changes += ' ' + bp.first + ' ' + bp.second;
if ( d->collection.attributes().count() > 0 )
changes += ' ' + ProtocolHelper::attributesToByteArray( d->collection );
foreach ( const QByteArray b, d->removeAttributes )
changes += " -" + b;
if ( changes.isEmpty() ) {
......@@ -104,14 +103,6 @@ void CollectionModifyJob::setParent(const Collection & parent)
d->parent = parent;
}
void CollectionModifyJob::setAttribute(Attribute * attr)
{
Q_ASSERT( !attr->type().isEmpty() );
QByteArray value = ImapParser::quote( attr->toByteArray() );
d->attributes.append( qMakePair( attr->type(), value ) );
}
void CollectionModifyJob::removeAttribute(const QByteArray & attrName)
{
d->removeAttributes << attrName;
......
......@@ -73,12 +73,6 @@ class AKONADI_EXPORT CollectionModifyJob : public Job
*/
void setParent( const Collection &parent );
/**
Sets the given collection attribute.
@param attr A collection attribute, ownership stays with the caller.
*/
void setAttribute( Attribute* attr );
/**
Removes the given collection attribute.
@param attrName The attribute name.
......
......@@ -61,8 +61,6 @@ class CollectionPropertiesDialog::Private
job->setName( collection.name() );
job->setContentTypes( collection.contentTypes() );
job->setCachePolicy( collection.cachePolicy() );
foreach ( Attribute *attr, collection.attributes() )
job->setAttribute( attr );
// TODO complete me
}
......
......@@ -86,6 +86,14 @@ void Entity::addAttribute(Attribute * attr)
d_ptr->mAttributes.insert( attr->type(), attr );
}
void Entity::removeAttribute(Attribute * attr)
{
if ( d_ptr->mAttributes.contains( attr->type() ) ) {
d_ptr->mDeletedAttributes.insert( attr->type() );
delete d_ptr->mAttributes.take( attr->type() );
}
}
bool Entity::hasAttribute(const QByteArray & type) const
{
return d_ptr->mAttributes.contains( type );
......@@ -96,6 +104,15 @@ QList< Attribute * > Entity::attributes() const
return d_ptr->mAttributes.values();
}
void Akonadi::Entity::clearAttributes()
{
foreach ( Attribute *attr, d_ptr->mAttributes ) {
d_ptr->mDeletedAttributes.insert( attr->type() );
delete attr;
}
d_ptr->mAttributes.clear();
}
Attribute * Entity::attribute(const QByteArray & type) const
{
if ( d_ptr->mAttributes.contains( type ) )
......
......@@ -104,6 +104,15 @@ class AKONADI_EXPORT Entity
*/
void addAttribute( Attribute *attr );
/**
Removes an attribute. If an attribute with the given type exists,
it will be deleted.
@param attr The attribute to remove. Note that a different attribute
of the same type would also be removed. In this case @p attr might
still be valid after this call.
*/
void removeAttribute( Attribute *attr );
/**
Returns true if the entity has the specified attribute.
@param type The attribute type.
......@@ -115,6 +124,11 @@ class AKONADI_EXPORT Entity
*/
QList<Attribute*> attributes() const;
/**
Removes all attributes of this object. The attributes are deleted.
*/
void clearAttributes();
/**
Returns the attribute of the given type if available, 0 otherwise.
@param type The attribute type.
......@@ -148,6 +162,15 @@ class AKONADI_EXPORT Entity
return 0;
}
/**
Removes and deletes the attribute of type @p T, if any.
*/
template <typename T> inline T* removeAttribute()
{
T dummy;
removeAttribute( dummy.type() );
}
protected:
/**
* Creates an entity from an @p other entity.
......
......@@ -20,10 +20,11 @@
#ifndef ENTITY_P_H
#define ENTITY_P_H
#include "entity.h"
#include <QtCore/QSharedData>
#include <QtCore/QString>
#include "entity.h"
#include <QSet>
#define AKONADI_DEFINE_PRIVATE( Class ) \
Class##Private* Class ::d_func() { return reinterpret_cast<Class##Private *>( d_ptr.data() ); } \
......@@ -53,11 +54,17 @@ class EntityPrivate : public QSharedData
mAttributes.insert( attr->type(), attr->clone() );
}
virtual void resetChangeLog()
{
mDeletedAttributes.clear();
}
virtual EntityPrivate *clone() const = 0;
Entity::Id mId;
QString mRemoteId;
QHash<QByteArray, Attribute*> mAttributes;
QSet<QByteArray> mDeletedAttributes;
};
}
......
......@@ -142,3 +142,13 @@ int ProtocolHelper::parseCollection(const QByteArray & data, Collection & collec
return pos;
}
QByteArray ProtocolHelper::attributesToByteArray(const Entity & entity)
{
QList<QByteArray> l;
foreach ( const Attribute *attr, entity.attributes() ) {
l << attr->type();
l << ImapParser::quote( attr->toByteArray() );
}
return ImapParser::join( l, " " );
}
......@@ -59,6 +59,11 @@ class ProtocolHelper
*/
static int parseCollection( const QByteArray &data, Collection &collection, int start = 0 );
/**
Convert attributes to their protocol representation.
*/
static QByteArray attributesToByteArray( const Entity &entity );
};
}
......
......@@ -109,9 +109,8 @@ void CollectionAttributeTest::testAttributes()
// modify a custom attribute
attr = new TestAttribute();
attr->setData( attr2 );
col.addAttribute( attr );
CollectionModifyJob *modify = new CollectionModifyJob( col, this );
modify->setAttribute( attr );
delete attr;
QVERIFY( modify->exec() );
list = new CollectionListJob( col, CollectionListJob::Local, this );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment