21 #ifndef AKONADI_ITEM_H
22 #define AKONADI_ITEM_H
24 #include "akonadi_export.h"
26 #include <akonadi/entity.h>
27 #include <akonadi/exception.h>
28 #include "itempayloadinternals_p.h"
30 #include <QtCore/QByteArray>
31 #include <QtCore/QMetaType>
32 #include <QtCore/QSet>
34 #include <boost/static_assert.hpp>
35 #include <boost/type_traits/is_pointer.hpp>
36 #include <boost/utility/enable_if.hpp>
115 class AKONADI_EXPORT Item :
public Entity
121 typedef QList<Item> List;
126 typedef QByteArray Flag;
131 typedef QSet<QByteArray> Flags;
137 static const char* FullPayload;
147 explicit Item( Id
id );
154 explicit Item(
const QString &mimeType );
159 Item(
const Item &other );
169 static Item fromUrl(
const KUrl &url );
180 QDateTime modificationTime()
const;
189 void setModificationTime(
const QDateTime &datetime );
195 bool hasFlag(
const QByteArray &name )
const;
200 void setFlag(
const QByteArray &name );
205 void clearFlag(
const QByteArray &name );
210 void setFlags(
const Flags &flags );
224 void setPayloadFromData(
const QByteArray &data );
232 QByteArray payloadData()
const;
238 QSet<QByteArray> loadedPayloadParts()
const;
257 void setRevision(
int revision );
262 int revision()
const;
279 void setSize( qint64 size );
291 void setMimeType(
const QString &mimeType );
296 QString mimeType()
const;
303 void setGid(
const QString &gid );
318 QVector<int> availablePayloadMetaTypeIds()
const;
331 template <
typename T>
void setPayload(
const T &p );
333 template <
typename T>
void setPayload( T* p );
334 template <
typename T>
void setPayload( std::auto_ptr<T> p );
350 template <
typename T> T payload()
const;
355 bool hasPayload()
const;
366 template <
typename T>
bool hasPayload()
const;
380 KUrl url( UrlType type = UrlShort )
const;
390 QSet<QByteArray> availablePayloadParts()
const;
401 QSet<QByteArray> cachedPayloadParts()
const;
416 void apply(
const Item &other );
427 template <
typename T>
static void addToLegacyMapping(
const QString & mimeType );
428 void setCachedPayloadParts(
const QSet<QByteArray> &cachedParts);
432 friend class ItemCreateJob;
433 friend class ItemModifyJob;
434 friend class ItemModifyJobPrivate;
435 friend class ItemSync;
436 friend class ProtocolHelper;
437 PayloadBase* payloadBase()
const;
438 void setPayloadBase( PayloadBase* );
439 PayloadBase* payloadBaseV2(
int sharedPointerId,
int metaTypeId )
const;
441 void setPayloadBaseV2(
int sharedPointerId,
int metaTypeId, std::auto_ptr<PayloadBase> p );
442 void addPayloadBaseVariant(
int sharedPointerId,
int metaTypeId, std::auto_ptr<PayloadBase> p )
const;
443 static void addToLegacyMappingImpl(
const QString & mimeType,
int sharedPointerId,
int metaTypeId, std::auto_ptr<PayloadBase> p );
449 bool ensureMetaTypeId(
int mtid )
const;
451 template <
typename T>
452 typename boost::enable_if_c<Internal::PayloadTrait<T>::isPolymorphic,
void>::type
453 setPayloadImpl(
const T &,
const int * = 0 );
454 template <
typename T>
455 typename boost::disable_if_c<Internal::PayloadTrait<T>::isPolymorphic,
void>::type
456 setPayloadImpl(
const T & );
458 template <
typename T>
459 typename boost::enable_if_c<Internal::PayloadTrait<T>::isPolymorphic,T>::type
460 payloadImpl(
const int * = 0 )
const;
461 template <
typename T>
462 typename boost::disable_if_c<Internal::PayloadTrait<T>::isPolymorphic,T>::type
465 template <
typename T>
466 typename boost::enable_if_c<Internal::PayloadTrait<T>::isPolymorphic,
bool>::type
467 hasPayloadImpl(
const int * = 0 )
const;
468 template <
typename T>
469 typename boost::disable_if_c<Internal::PayloadTrait<T>::isPolymorphic,
bool>::type
470 hasPayloadImpl()
const;
472 template <
typename T>
473 typename boost::enable_if<Internal::is_shared_pointer<T>,
bool>::type
474 tryToClone( T *,
const int * = 0 )
const;
475 template <
typename T>
476 typename boost::disable_if<Internal::is_shared_pointer<T>,
bool>::type
477 tryToClone( T * )
const;
484 void setStorageCollectionId(
Entity::Id collectionId);
490 QString payloadExceptionText(
int spid,
int mtid )
const;
497 inline void throwPayloadException(
int spid,
int mtid )
const {
498 throw PayloadException( payloadExceptionText( spid, mtid ) );
501 void throwPayloadException(
int spid,
int mtid )
const;
505 AKONADI_DECLARE_PRIVATE( Item )
508 template <
typename T>
509 T Item::payload()
const
511 BOOST_STATIC_ASSERT( !boost::is_pointer<T>::value );
514 throwPayloadException( -1, -1 );
516 return payloadImpl<T>();
519 template <
typename T>
520 typename boost::enable_if_c<Internal::PayloadTrait<T>::isPolymorphic,T>::type
521 Item::payloadImpl(
const int * )
const
523 typedef Internal::PayloadTrait<T> PayloadType;
524 BOOST_STATIC_ASSERT(( PayloadType::isPolymorphic ));
526 typedef typename Internal::get_hierarchy_root<T>::type Root_T;
527 typedef Internal::PayloadTrait<Root_T> RootType;
528 BOOST_STATIC_ASSERT(( !RootType::isPolymorphic ));
530 return PayloadType::castFrom( payloadImpl<Root_T>() );
533 template <
typename T>
534 typename boost::disable_if_c<Internal::PayloadTrait<T>::isPolymorphic,T>::type
535 Item::payloadImpl()
const
537 typedef Internal::PayloadTrait<T> PayloadType;
538 BOOST_STATIC_ASSERT(( !PayloadType::isPolymorphic ));
540 const int metaTypeId = PayloadType::elementMetaTypeId();
543 if ( !ensureMetaTypeId( metaTypeId ) )
544 throwPayloadException( PayloadType::sharedPointerId, metaTypeId );
548 if (
const Payload<T> *
const p = Internal::payload_cast<T>( payloadBaseV2( PayloadType::sharedPointerId, metaTypeId ) ) )
552 if ( !tryToClone<T>( &ret ) )
553 throwPayloadException( PayloadType::sharedPointerId, metaTypeId );
557 template <
typename T>
558 typename boost::enable_if<Internal::is_shared_pointer<T>,
bool>::type
559 Item::tryToClone( T * ret,
const int * )
const
561 typedef Internal::PayloadTrait<T> PayloadType;
562 BOOST_STATIC_ASSERT(( !PayloadType::isPolymorphic ));
564 const int metaTypeId = PayloadType::elementMetaTypeId();
569 typedef typename Internal::shared_pointer_traits<T>::next_shared_ptr NewT;
570 typedef Internal::PayloadTrait<NewT> NewPayloadType;
572 if (
const Payload<NewT> *
const p = Internal::payload_cast<NewT>( payloadBaseV2( NewPayloadType::sharedPointerId, metaTypeId ) ) ) {
574 const T nt = PayloadType::clone( p->payload );
575 if ( !PayloadType::isNull( nt ) ) {
577 std::auto_ptr<PayloadBase> npb(
new Payload<T>( nt ) );
578 addPayloadBaseVariant( PayloadType::sharedPointerId, metaTypeId, npb );
590 template <
typename T>
591 typename boost::disable_if<Internal::is_shared_pointer<T>,
bool>::type
592 Item::tryToClone( T * )
const
594 typedef Internal::PayloadTrait<T> PayloadType;
595 BOOST_STATIC_ASSERT(( !PayloadType::isPolymorphic ));
600 template <
typename T>
601 bool Item::hasPayload()
const
603 BOOST_STATIC_ASSERT( !boost::is_pointer<T>::value );
604 return hasPayload() && hasPayloadImpl<T>();
607 template <
typename T>
608 typename boost::enable_if_c<Internal::PayloadTrait<T>::isPolymorphic,
bool>::type
609 Item::hasPayloadImpl(
const int * )
const
611 typedef Internal::PayloadTrait<T> PayloadType;
612 BOOST_STATIC_ASSERT(( PayloadType::isPolymorphic ));
614 typedef typename Internal::get_hierarchy_root<T>::type Root_T;
615 typedef Internal::PayloadTrait<Root_T> RootType;
616 BOOST_STATIC_ASSERT(( !RootType::isPolymorphic ));
619 return hasPayloadImpl<Root_T>()
620 && PayloadType::canCastFrom( payload<Root_T>() );
621 }
catch (
const Akonadi::PayloadException & ) {
626 template <
typename T>
627 typename boost::disable_if_c<Internal::PayloadTrait<T>::isPolymorphic,
bool>::type
628 Item::hasPayloadImpl()
const
630 typedef Internal::PayloadTrait<T> PayloadType;
631 BOOST_STATIC_ASSERT(( !PayloadType::isPolymorphic ));
633 const int metaTypeId = PayloadType::elementMetaTypeId();
636 if ( !ensureMetaTypeId( metaTypeId ) )
641 if (
const Payload<T> *
const p = Internal::payload_cast<T>( payloadBaseV2( PayloadType::sharedPointerId, metaTypeId ) ) )
644 return tryToClone<T>( 0 );
647 template <
typename T>
648 void Item::setPayload(
const T &p )
650 BOOST_STATIC_ASSERT(( !boost::is_pointer<T>::value ));
654 template <
typename T>
655 typename boost::enable_if_c<Internal::PayloadTrait<T>::isPolymorphic>::type
656 Item::setPayloadImpl(
const T & p,
const int * )
658 typedef Internal::PayloadTrait<T> PayloadType;
659 BOOST_STATIC_ASSERT(( PayloadType::isPolymorphic ));
661 typedef typename Internal::get_hierarchy_root<T>::type Root_T;
662 typedef Internal::PayloadTrait<Root_T> RootType;
663 BOOST_STATIC_ASSERT(( !RootType::isPolymorphic ));
665 setPayloadImpl<Root_T>( p );
668 template <
typename T>
669 typename boost::disable_if_c<Internal::PayloadTrait<T>::isPolymorphic>::type
670 Item::setPayloadImpl(
const T & p )
672 typedef Internal::PayloadTrait<T> PayloadType;
673 std::auto_ptr<PayloadBase> pb(
new Payload<T>( p ) );
674 setPayloadBaseV2( PayloadType::sharedPointerId,
675 PayloadType::elementMetaTypeId(),
679 template <
typename T>
680 void Item::setPayload( T* p )
682 p->You_MUST_NOT_use_a_pointer_as_payload;
685 template <
typename T>
686 void Item::setPayload( std::auto_ptr<T> p )
688 p.Nice_try_but_a_std_auto_ptr_is_not_allowed_as_payload_either;
691 template <
typename T>
692 void Item::addToLegacyMapping(
const QString & mimeType ) {
693 typedef Internal::PayloadTrait<T> PayloadType;
694 BOOST_STATIC_ASSERT(( !PayloadType::isPolymorphic ));
695 std::auto_ptr<PayloadBase> p(
new Payload<T> );
696 addToLegacyMappingImpl( mimeType, PayloadType::sharedPointerId, PayloadType::elementMetaTypeId(), p );
701 Q_DECLARE_METATYPE(Akonadi::Item)
702 Q_DECLARE_METATYPE(Akonadi::Item::List)
qint64 Id
Describes the unique id type.