# class Attribute

### abstract

Module | akonadi
Namespace | Akonadi
Class | Attribute
Inherits |

Provides interface for custom attributes for Entity.

This class is an interface for custom attributes, that can be stored in an entity. Attributes should be meta data, e.g. ACLs, quotas etc. that are not part of the entities' data itself.

To provide custom attributes, you have to subclass from this interface and reimplement the pure virtual methods.

    class SecrecyAttribute : public Akonadi.Attribute
    {
    public:
    enum Secrecy
    {
    Public,
    Private,
    Confidential
    };

    SecrecyAttribute( Secrecy secrecy = Public )
    : mSecrecy( secrecy )
    {
    }

    void setSecrecy( Secrecy secrecy )
    {
    mSecrecy = secrecy;
    }

    Secrecy secrecy() const
    {
    return mSecrecy;
    }

    virtual QByteArray type() const
    {
    return "secrecy";
    }

    virtual Attribute* clone() const
    {
    return new SecrecyAttribute( mSecrecy );
    }

    virtual QByteArray serialized() const
    {
    switch ( mSecrecy ) {
    case Public: return "public"; break;
    case Private: return "private"; break;
    case Confidential: return "confidential"; break;
    }
    }

    virtual void deserialize( const QByteArray &data )
    {
    if ( data == "public" )
    mSecrecy = Public;
    else if ( data == "private" )
    mSecrecy = Private;
    else if ( data == "confidential" )
    mSecrecy = Confidential;
    }
    }

The custom attributes can be used in the following way:

    Akonadi.Item item( "text/directory" );

    item.addAttribute( new SecrecyAttribute( SecrecyAttribute.Confidential ) );

and

    Akonadi.Item item = ...

    if ( item.hasAttribute( "secrecy" ) ) {
    SecrecyAttribute *attr = item.attribute( "secrecy" );

    SecrecyAttribute.Secrecy secrecy = attr->secrecy();
    ...
    }

Author Volker Krause <vkrause@kde.org>

### methods

- clone ( )

  returns Akonadi.Attribute
  pure virtual

  Creates a copy of this attribute.

- deserialize (data)

  returns Nothing
  pure virtual

  Argument | Type | Default
  --- | --- | ---
  data | QByteArray |

  Sets the data of this attribute, using the same encoding as returned by toByteArray().

  data - The encoded attribute data.

- serialized ( )

  returns QByteArray
  pure virtual

  Returns a QByteArray representation of the attribute which will be storaged. This can be raw binary data, no encoding needs to be applied.

- type ( )

  returns QByteArray
  pure virtual

  Returns the type of the attribute.