File indexing completed on 2024-11-10 04:40:27
0001 /* 0002 SPDX-FileCopyrightText: 2006-2008 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #pragma once 0008 0009 #include "akonadicore_export.h" 0010 0011 #include <QList> 0012 0013 namespace Akonadi 0014 { 0015 /** 0016 * @short Provides interface for custom attributes for Entity. 0017 * 0018 * This class is an interface for custom attributes, that can be stored 0019 * in an entity. Attributes should be meta data, e.g. ACLs, quotas etc. 0020 * that are not part of the entities' data itself. 0021 * 0022 * Note that attributes are per user, i.e. when an attribute is added to 0023 * an entity, it only applies to the current user. 0024 * 0025 * To provide custom attributes, you have to subclass from this interface 0026 * and reimplement the pure virtual methods. 0027 * 0028 * @code 0029 * 0030 * class SecrecyAttribute : public Akonadi::Attribute 0031 * { 0032 * Q_GADGET 0033 * 0034 * public: 0035 * enum Secrecy 0036 * { 0037 * Public, 0038 * Private, 0039 * Confidential 0040 * }; 0041 * Q_ENUM(Secrecy); 0042 * 0043 * SecrecyAttribute(Secrecy secrecy = Public) 0044 * : mSecrecy(secrecy) 0045 * { 0046 * } 0047 * 0048 * void setSecrecy(Secrecy secrecy) 0049 * { 0050 * mSecrecy = secrecy; 0051 * } 0052 * 0053 * Secrecy secrecy() const 0054 * { 0055 * return mSecrecy; 0056 * } 0057 * 0058 * virtual QByteArray type() const 0059 * { 0060 * return "secrecy"; 0061 * } 0062 * 0063 * virtual Attribute* clone() const 0064 * { 0065 * return new SecrecyAttribute(mSecrecy); 0066 * } 0067 * 0068 * virtual QByteArray serialized() const 0069 * { 0070 * switch (mSecrecy) { 0071 * case Public: 0072 * return "public"; 0073 * case Private: 0074 * return "private"; 0075 * case Confidential: 0076 * return "confidential"; 0077 * } 0078 * } 0079 * 0080 * virtual void deserialize(const QByteArray &data) 0081 * { 0082 * if (data == "public") { 0083 * mSecrecy = Public; 0084 * } else if (data == "private") { 0085 * mSecrecy = Private; 0086 * } else if (data == "confidential") { 0087 * mSecrecy = Confidential; 0088 * } 0089 * } 0090 * } 0091 * 0092 * @endcode 0093 * 0094 * Additionally, you need to register your attribute with Akonadi::AttributeFactory 0095 * for automatic deserialization during retrieving of collections or items: 0096 * 0097 * @code 0098 * AttributeFactory::registerAttribute<SecrecyAttribute>(); 0099 * @endcode 0100 * 0101 * Third party attributes need to be registered once by each application that uses 0102 * them. So the above snippet needs to be in the resource that adds the attribute, 0103 * and each application that uses the resource. This may be simplified in the future. 0104 * 0105 * The custom attributes can be used in the following way: 0106 * 0107 * @code 0108 * 0109 * Akonadi::Item item(QStringLiteral("text/directory")); 0110 * SecrecyAttribute* attr = item.attribute<SecrecyAttribute>(Item::AddIfMissing); 0111 * attr->setSecrecy(SecrecyAttribute::Confidential); 0112 * 0113 * @endcode 0114 * 0115 * and 0116 * 0117 * @code 0118 * 0119 * Akonadi::Item item = ... 0120 * 0121 * if (item.hasAttribute<SecrecyAttribute>()) { 0122 * SecrecyAttribute *attr = item.attribute<SecrecyAttribute>(); 0123 * 0124 * SecrecyAttribute::Secrecy secrecy = attr->secrecy(); 0125 * ... 0126 * } 0127 * @endcode 0128 * 0129 * @author Volker Krause <vkrause@kde.org> 0130 */ 0131 class AKONADICORE_EXPORT Attribute // clazy:exclude=copyable-polymorphic 0132 { 0133 public: 0134 /** 0135 * Describes a list of attributes. 0136 */ 0137 using List = QList<Attribute *>; 0138 0139 /** 0140 * Returns the type of the attribute. 0141 */ 0142 virtual QByteArray type() const = 0; 0143 0144 /** 0145 * Destroys this attribute. 0146 */ 0147 virtual ~Attribute(); 0148 0149 /** 0150 * Creates a copy of this attribute. 0151 */ 0152 virtual Attribute *clone() const = 0; 0153 0154 /** 0155 * Returns a QByteArray representation of the attribute which will be 0156 * storaged. This can be raw binary data, no encoding needs to be applied. 0157 */ 0158 virtual QByteArray serialized() const = 0; 0159 0160 /** 0161 * Sets the data of this attribute, using the same encoding 0162 * as returned by toByteArray(). 0163 * 0164 * @param data The encoded attribute data. 0165 */ 0166 virtual void deserialize(const QByteArray &data) = 0; 0167 0168 protected: 0169 explicit Attribute() = default; 0170 Attribute(const Attribute &) = default; 0171 }; 0172 0173 } // namespace Akonadi