Changeset View
Changeset View
Standalone View
Standalone View
src/core/item.h
Show First 20 Lines • Show All 287 Lines • ▼ Show 20 Line(s) | 118 | public: | |||
---|---|---|---|---|---|
288 | /** | 288 | /** | ||
289 | * Returns @c true if the item has an attribute of the given type @p name, | 289 | * Returns @c true if the item has an attribute of the given type @p name, | ||
290 | * false otherwise. | 290 | * false otherwise. | ||
291 | */ | 291 | */ | ||
292 | bool hasAttribute(const QByteArray &name) const; | 292 | bool hasAttribute(const QByteArray &name) const; | ||
293 | 293 | | |||
294 | /** | 294 | /** | ||
295 | * Returns a list of all attributes of the item. | 295 | * Returns a list of all attributes of the item. | ||
296 | * | ||||
297 | * @warning Do not modify the attributes returned from this method, | ||||
298 | * the change will not be reflected when updating the Item through | ||||
299 | * ItemModifyJob. | ||||
296 | */ | 300 | */ | ||
297 | Attribute::List attributes() const; | 301 | Attribute::List attributes() const; | ||
298 | 302 | | |||
299 | /** | 303 | /** | ||
300 | * Removes and deletes all attributes of the item. | 304 | * Removes and deletes all attributes of the item. | ||
301 | */ | 305 | */ | ||
302 | void clearAttributes(); | 306 | void clearAttributes(); | ||
303 | 307 | | |||
304 | /** | 308 | /** | ||
305 | * Returns the attribute of the given type @p name if available, 0 otherwise. | 309 | * Returns the attribute of the given type @p name if available, 0 otherwise. | ||
306 | */ | 310 | */ | ||
307 | Attribute *attribute(const QByteArray &name) const; | 311 | Attribute *attribute(const QByteArray &name); | ||
312 | const Attribute *attribute(const QByteArray &name) const; | ||||
308 | 313 | | |||
309 | /** | 314 | /** | ||
310 | * Describes the options that can be passed to access attributes. | 315 | * Describes the options that can be passed to access attributes. | ||
311 | */ | 316 | */ | ||
312 | enum CreateOption { | 317 | enum CreateOption { | ||
313 | AddIfMissing ///< Creates the attribute if it is missing | 318 | AddIfMissing, ///< Creates the attribute if it is missing | ||
319 | DontCreate ///< Do not create the attribute if it is missing (default) | ||||
314 | }; | 320 | }; | ||
315 | 321 | | |||
316 | /** | 322 | /** | ||
317 | * Returns the attribute of the requested type. | 323 | * Returns the attribute of the requested type. | ||
318 | * If the item has no attribute of that type yet, a new one | 324 | * If the item has no attribute of that type yet, a new one | ||
319 | * is created and added to the entity. | 325 | * is created and added to the entity. | ||
320 | * | 326 | * | ||
321 | * @param option The create options. | 327 | * @param option The create options. | ||
322 | */ | 328 | */ | ||
323 | template <typename T> | 329 | template <typename T> | ||
324 | inline T *attribute(CreateOption option); | 330 | inline T *attribute(CreateOption option = DontCreate); | ||
325 | 331 | | |||
326 | /** | 332 | /** | ||
327 | * Returns the attribute of the requested type or 0 if it is not available. | 333 | * Returns the attribute of the requested type or 0 if it is not available. | ||
328 | */ | 334 | */ | ||
329 | template <typename T> | 335 | template <typename T> | ||
330 | inline T *attribute() const; | 336 | inline const T *attribute() const; | ||
331 | 337 | | |||
332 | /** | 338 | /** | ||
333 | * Removes and deletes the attribute of the requested type. | 339 | * Removes and deletes the attribute of the requested type. | ||
334 | */ | 340 | */ | ||
335 | template <typename T> | 341 | template <typename T> | ||
336 | inline void removeAttribute(); | 342 | inline void removeAttribute(); | ||
337 | 343 | | |||
338 | /** | 344 | /** | ||
▲ Show 20 Lines • Show All 443 Lines • ▼ Show 20 Line(s) | 784 | #endif | |||
782 | //@endcond | 788 | //@endcond | ||
783 | }; | 789 | }; | ||
784 | 790 | | |||
785 | AKONADICORE_EXPORT uint qHash(const Akonadi::Item &item); | 791 | AKONADICORE_EXPORT uint qHash(const Akonadi::Item &item); | ||
786 | 792 | | |||
787 | template <typename T> | 793 | template <typename T> | ||
788 | inline T *Item::attribute(Item::CreateOption option) | 794 | inline T *Item::attribute(Item::CreateOption option) | ||
789 | { | 795 | { | ||
790 | Q_UNUSED(option); | 796 | const QByteArray type = T().type(); | ||
791 | 797 | if (hasAttribute(type)) { | |||
792 | const T dummy; | 798 | if (T *attr = dynamic_cast<T *>(attribute(type))) { | ||
793 | if (hasAttribute(dummy.type())) { | | |||
794 | T *attr = dynamic_cast<T *>(attribute(dummy.type())); | | |||
795 | if (attr) { | | |||
796 | return attr; | 799 | return attr; | ||
797 | } | 800 | } | ||
798 | //Reuse 5250 | 801 | qWarning() << "Found attribute of unknown type" << type | ||
799 | qWarning() << "Found attribute of unknown type" << dummy.type() | | |||
800 | << ". Did you forget to call AttributeFactory::registerAttribute()?"; | 802 | << ". Did you forget to call AttributeFactory::registerAttribute()?"; | ||
801 | } | 803 | } else if (option == AddIfMissing) { | ||
802 | | ||||
803 | T *attr = new T(); | 804 | T *attr = new T(); | ||
804 | addAttribute(attr); | 805 | addAttribute(attr); | ||
805 | return attr; | 806 | return attr; | ||
806 | } | 807 | } | ||
807 | 808 | | |||
809 | return nullptr; | ||||
810 | } | ||||
811 | | ||||
808 | template <typename T> | 812 | template <typename T> | ||
809 | inline T *Item::attribute() const | 813 | inline const T *Item::attribute() const | ||
810 | { | 814 | { | ||
811 | const T dummy; | 815 | const QByteArray type = T().type(); | ||
812 | if (hasAttribute(dummy.type())) { | 816 | if (hasAttribute(type)) { | ||
813 | T *attr = dynamic_cast<T *>(attribute(dummy.type())); | 817 | if (const T *attr = dynamic_cast<const T *>(attribute(type))) { | ||
814 | if (attr) { | | |||
815 | | ||||
816 | return attr; | 818 | return attr; | ||
817 | } | 819 | } | ||
818 | //reuse 5250 | 820 | qWarning() << "Found attribute of unknown type" << type | ||
819 | qWarning() << "Found attribute of unknown type" << dummy.type() | | |||
820 | << ". Did you forget to call AttributeFactory::registerAttribute()?"; | 821 | << ". Did you forget to call AttributeFactory::registerAttribute()?"; | ||
821 | } | 822 | } | ||
822 | 823 | | |||
823 | return nullptr; | 824 | return nullptr; | ||
824 | } | 825 | } | ||
825 | 826 | | |||
826 | template <typename T> | 827 | template <typename T> | ||
827 | inline void Item::removeAttribute() | 828 | inline void Item::removeAttribute() | ||
828 | { | 829 | { | ||
829 | const T dummy; | 830 | removeAttribute(T().type()); | ||
830 | removeAttribute(dummy.type()); | | |||
831 | } | 831 | } | ||
832 | 832 | | |||
833 | template <typename T> | 833 | template <typename T> | ||
834 | inline bool Item::hasAttribute() const | 834 | inline bool Item::hasAttribute() const | ||
835 | { | 835 | { | ||
836 | const T dummy; | 836 | return hasAttribute(T().type()); | ||
837 | return hasAttribute(dummy.type()); | | |||
838 | } | 837 | } | ||
839 | 838 | | |||
840 | template <typename T> | 839 | template <typename T> | ||
841 | T Item::payload() const | 840 | T Item::payload() const | ||
842 | { | 841 | { | ||
843 | static_assert(!std::is_pointer<T>::value, "Payload must not be a pointer"); | 842 | static_assert(!std::is_pointer<T>::value, "Payload must not be a pointer"); | ||
844 | 843 | | |||
845 | if (!hasPayload()) { | 844 | if (!hasPayload()) { | ||
▲ Show 20 Lines • Show All 245 Lines • Show Last 20 Lines |