Changeset View
Changeset View
Standalone View
Standalone View
src/core/tag.h
Show First 20 Lines • Show All 110 Lines • ▼ Show 20 Line(s) | 41 | public: | |||
---|---|---|---|---|---|
111 | /** | 111 | /** | ||
112 | * Removes and deletes all attributes of the entity. | 112 | * Removes and deletes all attributes of the entity. | ||
113 | */ | 113 | */ | ||
114 | void clearAttributes(); | 114 | void clearAttributes(); | ||
115 | 115 | | |||
116 | /** | 116 | /** | ||
117 | * Returns the attribute of the given type @p name if available, 0 otherwise. | 117 | * Returns the attribute of the given type @p name if available, 0 otherwise. | ||
118 | */ | 118 | */ | ||
119 | Attribute *attribute(const QByteArray &name) const; | 119 | const Attribute *attribute(const QByteArray &name) const; | ||
120 | Attribute *attribute(const QByteArray &name); | ||||
120 | 121 | | |||
121 | /** | 122 | /** | ||
122 | * Describes the options that can be passed to access attributes. | 123 | * Describes the options that can be passed to access attributes. | ||
123 | */ | 124 | */ | ||
124 | enum CreateOption { | 125 | enum CreateOption { | ||
125 | AddIfMissing ///< Creates the attribute if it is missing | 126 | AddIfMissing, ///< Creates the attribute if it is missing | ||
127 | DontCreate ///< Does not create an attribute if it is missing (default) | ||||
126 | }; | 128 | }; | ||
127 | 129 | | |||
128 | /** | 130 | /** | ||
129 | * Returns the attribute of the requested type. | 131 | * Returns the attribute of the requested type. | ||
130 | * If the entity has no attribute of that type yet, a new one | 132 | * If the entity has no attribute of that type yet, a new one | ||
131 | * is created and added to the entity. | 133 | * is created and added to the entity. | ||
132 | * | 134 | * | ||
133 | * @param option The create options. | 135 | * @param option The create options. | ||
134 | */ | 136 | */ | ||
135 | template <typename T> | 137 | template <typename T> | ||
136 | inline T *attribute(CreateOption option); | 138 | inline T *attribute(CreateOption option = DontCreate); | ||
137 | 139 | | |||
138 | /** | 140 | /** | ||
139 | * Returns the attribute of the requested type or 0 if it is not available. | 141 | * Returns the attribute of the requested type or 0 if it is not available. | ||
140 | */ | 142 | */ | ||
141 | template <typename T> | 143 | template <typename T> | ||
142 | inline T *attribute() const; | 144 | inline const T *attribute() const; | ||
143 | 145 | | |||
144 | /** | 146 | /** | ||
145 | * Removes and deletes the attribute of the requested type. | 147 | * Removes and deletes the attribute of the requested type. | ||
146 | */ | 148 | */ | ||
147 | template <typename T> | 149 | template <typename T> | ||
148 | inline void removeAttribute(); | 150 | inline void removeAttribute(); | ||
149 | 151 | | |||
150 | /** | 152 | /** | ||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Line(s) | |||||
192 | bool isImmutable() const; | 194 | bool isImmutable() const; | ||
193 | 195 | | |||
194 | /** | 196 | /** | ||
195 | * Returns a GENERIC tag with the given name and a valid gid | 197 | * Returns a GENERIC tag with the given name and a valid gid | ||
196 | */ | 198 | */ | ||
197 | static Tag genericTag(const QString &name); | 199 | static Tag genericTag(const QString &name); | ||
198 | 200 | | |||
199 | private: | 201 | private: | ||
200 | bool checkAttribute(Attribute *attr, const QByteArray &type) const; | 202 | bool checkAttribute(const Attribute *attr, const QByteArray &type) const; | ||
201 | void markAttributeModified(const QByteArray &type); | 203 | void markAttributeModified(const QByteArray &type); | ||
202 | 204 | | |||
203 | //@cond PRIVATE | 205 | //@cond PRIVATE | ||
204 | friend class TagModifyJob; | 206 | friend class TagModifyJob; | ||
205 | friend class TagFetchJob; | 207 | friend class TagFetchJob; | ||
206 | friend class ProtocolHelper; | 208 | friend class ProtocolHelper; | ||
207 | 209 | | |||
208 | QSharedDataPointer<TagPrivate> d_ptr; | 210 | QSharedDataPointer<TagPrivate> d_ptr; | ||
209 | //@endcond | 211 | //@endcond | ||
210 | }; | 212 | }; | ||
211 | 213 | | |||
212 | AKONADICORE_EXPORT uint qHash(const Akonadi::Tag &); | 214 | AKONADICORE_EXPORT uint qHash(const Akonadi::Tag &); | ||
213 | 215 | | |||
214 | template <typename T> | 216 | template <typename T> | ||
215 | inline T *Tag::attribute(CreateOption option) | 217 | inline T *Tag::attribute(CreateOption option) | ||
216 | { | 218 | { | ||
217 | Q_UNUSED(option); | | |||
218 | | ||||
219 | const QByteArray type = T().type(); | 219 | const QByteArray type = T().type(); | ||
220 | markAttributeModified(type); | 220 | markAttributeModified(type); | ||
221 | if (hasAttribute(type)) { | 221 | if (hasAttribute(type)) { | ||
222 | T *attr = dynamic_cast<T *>(attribute(type)); | 222 | T *attr = dynamic_cast<T *>(attribute(type)); | ||
223 | if (checkAttribute(attr, type)) { | 223 | if (checkAttribute(attr, type)) { | ||
224 | return attr; | 224 | return attr; | ||
225 | } | 225 | } | ||
226 | } | 226 | } else if (option == AddIfMissing) { | ||
227 | | ||||
228 | T *attr = new T(); | 227 | T *attr = new T(); | ||
229 | addAttribute(attr); | 228 | addAttribute(attr); | ||
230 | return attr; | 229 | return attr; | ||
231 | } | 230 | } | ||
232 | 231 | | |||
232 | return nullptr; | ||||
233 | } | ||||
234 | | ||||
233 | template <typename T> | 235 | template <typename T> | ||
234 | inline T *Tag::attribute() const | 236 | inline const T *Tag::attribute() const | ||
235 | { | 237 | { | ||
236 | const QByteArray type = T().type(); | 238 | const QByteArray type = T().type(); | ||
237 | if (hasAttribute(type)) { | 239 | if (hasAttribute(type)) { | ||
238 | T *attr = dynamic_cast<T *>(attribute(type)); | 240 | const T *attr = dynamic_cast<const T *>(attribute(type)); | ||
239 | if (checkAttribute(attr, type)) { | 241 | if (checkAttribute(attr, type)) { | ||
240 | // FIXME: Make this a truly const method so that callers may not modify | | |||
241 | // the attribute returned from here. | | |||
242 | const_cast<Tag*>(this)->markAttributeModified(type); | | |||
243 | return attr; | 242 | return attr; | ||
244 | } | 243 | } | ||
245 | } | 244 | } | ||
246 | 245 | | |||
247 | return nullptr; | 246 | return nullptr; | ||
248 | } | 247 | } | ||
249 | 248 | | |||
250 | template <typename T> | 249 | template <typename T> | ||
Show All 23 Lines |