1 | /* |
2 | Copyright (c) 2014 Christian Mollekopf <mollekopf@kolabsys.com> |
3 | |
4 | This library is free software; you can redistribute it and/or modify it |
5 | under the terms of the GNU Library General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or (at your |
7 | option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, but WITHOUT |
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
12 | License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to the |
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
17 | 02110-1301, USA. |
18 | */ |
19 | |
20 | #ifndef AKONADI_ATTRIBUTEENTITY_H |
21 | #define AKONADI_ATTRIBUTEENTITY_H |
22 | |
23 | #include <akonadi/job.h> |
24 | #include <QVector> |
25 | #include <qsharedpointer.h> |
26 | #include "attribute.h" |
27 | #include <kdebug.h> |
28 | |
29 | namespace Akonadi { |
30 | |
31 | /** |
32 | * Parent class for entities that can have attributes. |
33 | * This is supposed to eventually share the code between Akonadi::Tag and Akonadi::Entity. |
34 | * |
35 | * In the current form using this in Akonadi::Entity would break the implicit sharing of it's private class, |
36 | * so AttributeEntity::Private would need to become a parent class of EntityPrivate and use the same clone() |
37 | * calls etc. |
38 | * An even better solution is probably ot make AttributeEntity a private member of Entity, with all Attribute related member functions forwarding to this class. |
39 | */ |
40 | class AKONADI_EXPORT AttributeEntity { |
41 | public: |
42 | AttributeEntity(); |
43 | |
44 | AttributeEntity(const AttributeEntity &other); |
45 | virtual ~AttributeEntity(); |
46 | |
47 | //Each subclass must override this to avoid slicing |
48 | virtual AttributeEntity &operator=(const AttributeEntity &other); |
49 | |
50 | /** |
51 | * Adds an attribute to the entity. |
52 | * |
53 | * If an attribute of the same type name already exists, it is deleted and |
54 | * replaced with the new one. |
55 | * |
56 | * @param attribute The new attribute. |
57 | * |
58 | * @note The entity takes the ownership of the attribute. |
59 | */ |
60 | void addAttribute(Attribute *attribute); |
61 | |
62 | /** |
63 | * Removes and deletes the attribute of the given type @p name. |
64 | */ |
65 | void removeAttribute(const QByteArray &name); |
66 | |
67 | /** |
68 | * Returns @c true if the entity has an attribute of the given type @p name, |
69 | * false otherwise. |
70 | */ |
71 | bool hasAttribute(const QByteArray &name) const; |
72 | |
73 | /** |
74 | * Returns a list of all attributes of the entity. |
75 | */ |
76 | Attribute::List attributes() const; |
77 | |
78 | /** |
79 | * Removes and deletes all attributes of the entity. |
80 | */ |
81 | void clearAttributes(); |
82 | |
83 | /** |
84 | * Returns the attribute of the given type @p name if available, 0 otherwise. |
85 | */ |
86 | Attribute *attribute(const QByteArray &name) const; |
87 | |
88 | /** |
89 | * Describes the options that can be passed to access attributes. |
90 | */ |
91 | enum CreateOption { |
92 | AddIfMissing ///< Creates the attribute if it is missing |
93 | }; |
94 | |
95 | /** |
96 | * Returns the attribute of the requested type. |
97 | * If the entity has no attribute of that type yet, a new one |
98 | * is created and added to the entity. |
99 | * |
100 | * @param option The create options. |
101 | */ |
102 | template <typename T> inline T *attribute(CreateOption option) |
103 | { |
104 | Q_UNUSED(option); |
105 | |
106 | const T dummy; |
107 | if (hasAttribute(dummy.type())) { |
108 | T *attr = dynamic_cast<T *>(attribute(dummy.type())); |
109 | if (attr) { |
110 | return attr; |
111 | } |
112 | kWarning(5250) << "Found attribute of unknown type" << dummy.type() |
113 | << ". Did you forget to call AttributeFactory::registerAttribute()?" ; |
114 | } |
115 | |
116 | T *attr = new T(); |
117 | addAttribute(attr); |
118 | return attr; |
119 | } |
120 | |
121 | /** |
122 | * Returns the attribute of the requested type or 0 if it is not available. |
123 | */ |
124 | template <typename T> inline T *attribute() const |
125 | { |
126 | const T dummy; |
127 | if (hasAttribute(dummy.type())) { |
128 | T *attr = dynamic_cast<T *>(attribute(dummy.type())); |
129 | if (attr) { |
130 | return attr; |
131 | } |
132 | kWarning(5250) << "Found attribute of unknown type" << dummy.type() |
133 | << ". Did you forget to call AttributeFactory::registerAttribute()?" ; |
134 | } |
135 | |
136 | return 0; |
137 | } |
138 | |
139 | /** |
140 | * Removes and deletes the attribute of the requested type. |
141 | */ |
142 | template <typename T> inline void removeAttribute() |
143 | { |
144 | const T dummy; |
145 | removeAttribute(dummy.type()); |
146 | } |
147 | |
148 | /** |
149 | * Returns whether the entity has an attribute of the requested type. |
150 | */ |
151 | template <typename T> inline bool hasAttribute() const |
152 | { |
153 | const T dummy; |
154 | return hasAttribute(dummy.type()); |
155 | } |
156 | private: |
157 | friend class TagModifyJob; |
158 | QSet<QByteArray> &removedAttributes() const; |
159 | |
160 | class Private; |
161 | QSharedPointer<Private> d_ptr; |
162 | }; |
163 | |
164 | } |
165 | |
166 | #endif |
167 | |