akonadi
item.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef AKONADI_ITEM_H
00022 #define AKONADI_ITEM_H
00023
00024 #include "akonadi_export.h"
00025
00026 #include <akonadi/entity.h>
00027 #include <akonadi/exception.h>
00028 #include "itempayloadinternals_p.h"
00029
00030 #include <QtCore/QByteArray>
00031 #include <QtCore/QMetaType>
00032 #include <QtCore/QSet>
00033
00034 #include <boost/static_assert.hpp>
00035 #include <boost/type_traits/is_pointer.hpp>
00036 #include <typeinfo>
00037
00038 class KUrl;
00039
00040 namespace std {
00041 template <typename T> class auto_ptr;
00042 }
00043
00044 namespace Akonadi {
00045
00046 class ItemPrivate;
00047
00086 class AKONADI_EXPORT Item : public Entity
00087 {
00088 public:
00092 typedef QList<Item> List;
00093
00097 typedef QByteArray Flag;
00098
00102 typedef QSet<QByteArray> Flags;
00103
00108 static const char* FullPayload;
00109
00113 Item();
00114
00118 explicit Item( Id id );
00119
00125 explicit Item( const QString &mimeType );
00126
00130 Item( const Item &other );
00131
00135 ~Item();
00136
00140 static Item fromUrl( const KUrl &url );
00141
00145 Flags flags() const;
00146
00151 QDateTime modificationTime() const;
00152
00160 void setModificationTime( const QDateTime &datetime );
00161
00166 bool hasFlag( const QByteArray &name ) const;
00167
00171 void setFlag( const QByteArray &name );
00172
00176 void clearFlag( const QByteArray &name );
00177
00181 void setFlags( const Flags &flags );
00182
00186 void clearFlags();
00187
00195 void setPayloadFromData( const QByteArray &data );
00196
00203 QByteArray payloadData() const;
00204
00209 QSet<QByteArray> loadedPayloadParts() const;
00210
00217 void setRevision( int revision );
00218
00222 int revision() const;
00223
00232 Entity::Id storageCollectionId() const;
00233
00239 void setSize( qint64 size );
00240
00246 qint64 size() const;
00247
00251 void setMimeType( const QString &mimeType );
00252
00256 QString mimeType() const;
00257
00269 template <typename T> void setPayload( const T &p );
00270
00271 template <typename T> void setPayload( T* p );
00272 template <typename T> void setPayload( std::auto_ptr<T> p );
00273
00274
00288 template <typename T> T payload() const;
00289
00293 bool hasPayload() const;
00294
00304 template <typename T> bool hasPayload() const;
00305
00309 enum UrlType
00310 {
00311 UrlShort = 0,
00312 UrlWithMimeType = 1
00313 };
00314
00318 KUrl url( UrlType type = UrlShort ) const;
00319
00328 QSet<QByteArray> availablePayloadParts() const;
00329
00343 void apply( const Item &other );
00344
00345 private:
00346
00347 friend class ItemCreateJob;
00348 friend class ItemModifyJob;
00349 friend class ProtocolHelper;
00350 PayloadBase* payloadBase() const;
00351 void setPayloadBase( PayloadBase* );
00357 void setStorageCollectionId( Entity::Id collectionId);
00358
00359
00360
00361 AKONADI_DECLARE_PRIVATE( Item )
00362 };
00363
00364
00365 template <typename T>
00366 T Item::payload() const
00367 {
00368 BOOST_STATIC_ASSERT( !boost::is_pointer<T>::value );
00369
00370 if ( !payloadBase() )
00371 throw PayloadException( "No payload set" );
00372
00373 typedef Internal::PayloadTrait<T> PayloadType;
00374 if ( PayloadType::isPolymorphic ) {
00375 try {
00376 const typename PayloadType::SuperType sp = payload<typename PayloadType::SuperType>();
00377 return PayloadType::castFrom( sp );
00378 } catch ( const Akonadi::PayloadException& ) {}
00379 }
00380
00381 Payload<T> *p = Internal::payload_cast<T>( payloadBase() );
00382 if ( !p ) {
00383 throw PayloadException( QString::fromLatin1( "Wrong payload type (is '%1', requested '%2')" )
00384 .arg( QLatin1String( payloadBase()->typeName() ) )
00385 .arg( QLatin1String( typeid(p).name() ) ) );
00386 }
00387 return p->payload;
00388 }
00389
00390 template <typename T>
00391 bool Item::hasPayload() const
00392 {
00393 BOOST_STATIC_ASSERT( !boost::is_pointer<T>::value );
00394
00395 if ( !hasPayload() )
00396 return false;
00397
00398 typedef Internal::PayloadTrait<T> PayloadType;
00399 if ( PayloadType::isPolymorphic ) {
00400 try {
00401 const typename PayloadType::SuperType sp = payload<typename PayloadType::SuperType>();
00402 return PayloadType::canCastFrom( sp );
00403 } catch ( const Akonadi::PayloadException& ) {}
00404 }
00405
00406 return Internal::payload_cast<T>( payloadBase() );
00407 }
00408
00409 template <typename T>
00410 void Item::setPayload( const T &p )
00411 {
00412 typedef Internal::PayloadTrait<T> PayloadType;
00413 if ( PayloadType::isPolymorphic ) {
00414 const typename PayloadType::SuperType sp
00415 = PayloadType::template castTo<typename PayloadType::SuperElementType>( p );
00416 if ( !Internal::PayloadTrait<typename PayloadType::SuperType>::isNull( sp )
00417 || PayloadType::isNull( p ) )
00418 {
00419 setPayload( sp );
00420 return;
00421 }
00422 }
00423 setPayloadBase( new Payload<T>( p ) );
00424 }
00425
00426 template <typename T>
00427 void Item::setPayload( T* p )
00428 {
00429 p.You_MUST_NOT_use_a_pointer_as_payload;
00430 }
00431
00432 template <typename T>
00433 void Item::setPayload( std::auto_ptr<T> p )
00434 {
00435 p.Nice_try_but_a_std_auto_ptr_is_not_allowed_as_payload_either;
00436 }
00437
00438 }
00439
00440 Q_DECLARE_METATYPE(Akonadi::Item)
00441 Q_DECLARE_METATYPE(Akonadi::Item::List)
00442
00443 #endif