Changeset View
Changeset View
Standalone View
Standalone View
src/core/collection.h
Show First 20 Lines • Show All 247 Lines • ▼ Show 20 Line(s) | 78 | public: | |||
---|---|---|---|---|---|
248 | /** | 248 | /** | ||
249 | * Returns @c true if the collection has an attribute of the given type @p name, | 249 | * Returns @c true if the collection has an attribute of the given type @p name, | ||
250 | * false otherwise. | 250 | * false otherwise. | ||
251 | */ | 251 | */ | ||
252 | bool hasAttribute(const QByteArray &name) const; | 252 | bool hasAttribute(const QByteArray &name) const; | ||
253 | 253 | | |||
254 | /** | 254 | /** | ||
255 | * Returns a list of all attributes of the collection. | 255 | * Returns a list of all attributes of the collection. | ||
256 | * | ||||
257 | * @warning Do not modify the attributes returned from this method, | ||||
258 | * the change will not be reflected when updating the Collection | ||||
259 | * through CollectionModifyJob. | ||||
256 | */ | 260 | */ | ||
257 | Q_REQUIRED_RESULT Attribute::List attributes() const; | 261 | Q_REQUIRED_RESULT Attribute::List attributes() const; | ||
258 | 262 | | |||
259 | /** | 263 | /** | ||
260 | * Removes and deletes all attributes of the collection. | 264 | * Removes and deletes all attributes of the collection. | ||
261 | */ | 265 | */ | ||
262 | void clearAttributes(); | 266 | void clearAttributes(); | ||
263 | 267 | | |||
264 | /** | 268 | /** | ||
265 | * Returns the attribute of the given type @p name if available, 0 otherwise. | 269 | * Returns the attribute of the given type @p name if available, 0 otherwise. | ||
266 | */ | 270 | */ | ||
267 | Attribute *attribute(const QByteArray &name) const; | 271 | Attribute *attribute(const QByteArray &name); | ||
272 | const Attribute *attribute(const QByteArray &name) const; | ||||
268 | 273 | | |||
269 | /** | 274 | /** | ||
270 | * Describes the options that can be passed to access attributes. | 275 | * Describes the options that can be passed to access attributes. | ||
271 | */ | 276 | */ | ||
272 | enum CreateOption { | 277 | enum CreateOption { | ||
273 | AddIfMissing ///< Creates the attribute if it is missing | 278 | AddIfMissing, ///< Creates the attribute if it is missing | ||
279 | DontCreate ///< Default value | ||||
274 | }; | 280 | }; | ||
275 | 281 | | |||
276 | /** | 282 | /** | ||
277 | * Returns the attribute of the requested type. | 283 | * Returns the attribute of the requested type. | ||
278 | * If the collection has no attribute of that type yet, a new one | 284 | * If the collection has no attribute of that type yet, passing AddIfMissing | ||
279 | * is created and added to the entity. | 285 | * as an argument will create and add it to the entity | ||
280 | * | 286 | * | ||
281 | * @param option The create options. | 287 | * @param option The create options. | ||
282 | */ | 288 | */ | ||
283 | template <typename T> | 289 | template <typename T> | ||
284 | inline T *attribute(CreateOption option); | 290 | inline T *attribute(CreateOption option = DontCreate); | ||
285 | 291 | | |||
286 | /** | 292 | /** | ||
287 | * Returns the attribute of the requested type or 0 if it is not available. | 293 | * Returns the attribute of the requested type or 0 if it is not available. | ||
288 | */ | 294 | */ | ||
289 | template <typename T> | 295 | template <typename T> | ||
290 | inline T *attribute() const; | 296 | inline const T *attribute() const; | ||
291 | 297 | | |||
292 | /** | 298 | /** | ||
293 | * Removes and deletes the attribute of the requested type. | 299 | * Removes and deletes the attribute of the requested type. | ||
294 | */ | 300 | */ | ||
295 | template <typename T> | 301 | template <typename T> | ||
296 | inline void removeAttribute(); | 302 | inline void removeAttribute(); | ||
297 | 303 | | |||
298 | /** | 304 | /** | ||
▲ Show 20 Lines • Show All 258 Lines • ▼ Show 20 Line(s) | 552 | private: | |||
557 | //@endcond | 563 | //@endcond | ||
558 | }; | 564 | }; | ||
559 | 565 | | |||
560 | AKONADICORE_EXPORT uint qHash(const Akonadi::Collection &collection); | 566 | AKONADICORE_EXPORT uint qHash(const Akonadi::Collection &collection); | ||
561 | 567 | | |||
562 | template <typename T> | 568 | template <typename T> | ||
563 | inline T *Akonadi::Collection::attribute(Collection::CreateOption option) | 569 | inline T *Akonadi::Collection::attribute(Collection::CreateOption option) | ||
564 | { | 570 | { | ||
565 | Q_UNUSED(option); | | |||
566 | | ||||
567 | const QByteArray type = T().type(); | 571 | const QByteArray type = T().type(); | ||
568 | if (hasAttribute(type)) { | 572 | if (hasAttribute(type)) { | ||
569 | T *attr = dynamic_cast<T *>(attribute(type)); | 573 | if (T *attr = dynamic_cast<T *>(attribute(type))) { | ||
570 | if (attr) { | | |||
571 | markAttributeModified(type); | | |||
572 | return attr; | 574 | return attr; | ||
573 | } | 575 | } | ||
574 | //Reuse 5250 | | |||
575 | qWarning() << "Found attribute of unknown type" << type | 576 | qWarning() << "Found attribute of unknown type" << type | ||
576 | << ". Did you forget to call AttributeFactory::registerAttribute()?"; | 577 | << ". Did you forget to call AttributeFactory::registerAttribute()?"; | ||
577 | } | 578 | } else if (option == AddIfMissing) { | ||
578 | | ||||
579 | T *attr = new T(); | 579 | T *attr = new T(); | ||
580 | addAttribute(attr); | 580 | addAttribute(attr); | ||
581 | return attr; | 581 | return attr; | ||
582 | } | 582 | } | ||
583 | 583 | | |||
584 | return nullptr; | ||||
585 | } | ||||
586 | | ||||
584 | template <typename T> | 587 | template <typename T> | ||
585 | inline T *Akonadi::Collection::attribute() const | 588 | inline const T *Akonadi::Collection::attribute() const | ||
586 | { | 589 | { | ||
587 | const QByteArray type = T().type(); | 590 | const QByteArray type = T().type(); | ||
588 | if (hasAttribute(type)) { | 591 | if (hasAttribute(type)) { | ||
589 | T *attr = dynamic_cast<T *>(attribute(type)); | 592 | if (const T *attr = dynamic_cast<const T *>(attribute(type))) { | ||
590 | if (attr) { | | |||
591 | // FIXME: This method returns a non-const pointer, so callers may still modify the | | |||
592 | // attribute. Unfortunately, just making this function return a const pointer and | | |||
593 | // creating a non-const overload does not work, as many users of this function abuse the | | |||
594 | // non-const pointer and modify the attribute even on a const object. | | |||
595 | const_cast<Collection*>(this)->markAttributeModified(type); | | |||
596 | return attr; | 593 | return attr; | ||
597 | } | 594 | } | ||
598 | //reuse 5250 | | |||
599 | qWarning() << "Found attribute of unknown type" << type | 595 | qWarning() << "Found attribute of unknown type" << type | ||
600 | << ". Did you forget to call AttributeFactory::registerAttribute()?"; | 596 | << ". Did you forget to call AttributeFactory::registerAttribute()?"; | ||
601 | } | 597 | } | ||
602 | 598 | | |||
603 | return nullptr; | 599 | return nullptr; | ||
604 | } | 600 | } | ||
605 | 601 | | |||
606 | template <typename T> | 602 | template <typename T> | ||
607 | inline void Akonadi::Collection::removeAttribute() | 603 | inline void Akonadi::Collection::removeAttribute() | ||
608 | { | 604 | { | ||
609 | const T dummy; | 605 | removeAttribute(T().type()); | ||
610 | removeAttribute(dummy.type()); | | |||
611 | } | 606 | } | ||
612 | 607 | | |||
613 | template <typename T> | 608 | template <typename T> | ||
614 | inline bool Akonadi::Collection::hasAttribute() const | 609 | inline bool Akonadi::Collection::hasAttribute() const | ||
615 | { | 610 | { | ||
616 | const T dummy; | 611 | return hasAttribute(T().type()); | ||
617 | return hasAttribute(dummy.type()); | | |||
618 | } | 612 | } | ||
619 | 613 | | |||
620 | } // namespace Akonadi | 614 | } // namespace Akonadi | ||
621 | 615 | | |||
622 | /** | 616 | /** | ||
623 | * Allows to output a collection for debugging purposes. | 617 | * Allows to output a collection for debugging purposes. | ||
624 | */ | 618 | */ | ||
625 | AKONADICORE_EXPORT QDebug operator<<(QDebug d, const Akonadi::Collection &collection); | 619 | AKONADICORE_EXPORT QDebug operator<<(QDebug d, const Akonadi::Collection &collection); | ||
626 | 620 | | |||
627 | Q_DECLARE_METATYPE(Akonadi::Collection) | 621 | Q_DECLARE_METATYPE(Akonadi::Collection) | ||
628 | Q_DECLARE_METATYPE(Akonadi::Collection::List) | 622 | Q_DECLARE_METATYPE(Akonadi::Collection::List) | ||
629 | Q_DECLARE_OPERATORS_FOR_FLAGS(Akonadi::Collection::Rights) | 623 | Q_DECLARE_OPERATORS_FOR_FLAGS(Akonadi::Collection::Rights) | ||
630 | Q_DECLARE_TYPEINFO(Akonadi::Collection, Q_MOVABLE_TYPE); | 624 | Q_DECLARE_TYPEINFO(Akonadi::Collection, Q_MOVABLE_TYPE); | ||
631 | 625 | | |||
632 | #endif | 626 | #endif |