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
00320 private:
00321
00322 friend class ItemModifyJob;
00323 friend class ItemFetchJob;
00324 PayloadBase* payloadBase() const;
00325 void setPayloadBase( PayloadBase* );
00331 void setStorageCollectionId( Entity::Id collectionId);
00332
00333
00334
00335 AKONADI_DECLARE_PRIVATE( Item )
00336 };
00337
00338
00339 template <typename T>
00340 T Item::payload() const
00341 {
00342 BOOST_STATIC_ASSERT( !boost::is_pointer<T>::value );
00343
00344 if ( !payloadBase() )
00345 throw PayloadException( "No payload set" );
00346
00347 typedef Internal::PayloadTrait<T> PayloadType;
00348 if ( PayloadType::isPolymorphic ) {
00349 try {
00350 const typename PayloadType::SuperType sp = payload<typename PayloadType::SuperType>();
00351 return PayloadType::castFrom( sp );
00352 } catch ( const Akonadi::PayloadException& ) {}
00353 }
00354
00355 Payload<T> *p = Internal::payload_cast<T>( payloadBase() );
00356 if ( !p ) {
00357 throw PayloadException( QString::fromLatin1( "Wrong payload type (is '%1', requested '%2')" )
00358 .arg( QLatin1String( payloadBase()->typeName() ) )
00359 .arg( QLatin1String( typeid(p).name() ) ) );
00360 }
00361 return p->payload;
00362 }
00363
00364 template <typename T>
00365 bool Item::hasPayload() const
00366 {
00367 BOOST_STATIC_ASSERT( !boost::is_pointer<T>::value );
00368
00369 if ( !hasPayload() )
00370 return false;
00371
00372 typedef Internal::PayloadTrait<T> PayloadType;
00373 if ( PayloadType::isPolymorphic ) {
00374 try {
00375 const typename PayloadType::SuperType sp = payload<typename PayloadType::SuperType>();
00376 return PayloadType::canCastFrom( sp );
00377 } catch ( const Akonadi::PayloadException& ) {}
00378 }
00379
00380 return Internal::payload_cast<T>( payloadBase() );
00381 }
00382
00383 template <typename T>
00384 void Item::setPayload( const T &p )
00385 {
00386 typedef Internal::PayloadTrait<T> PayloadType;
00387 if ( PayloadType::isPolymorphic ) {
00388 const typename PayloadType::SuperType sp
00389 = PayloadType::template castTo<typename PayloadType::SuperElementType>( p );
00390 if ( !Internal::PayloadTrait<typename PayloadType::SuperType>::isNull( sp )
00391 || PayloadType::isNull( p ) )
00392 {
00393 setPayload( sp );
00394 return;
00395 }
00396 }
00397 setPayloadBase( new Payload<T>( p ) );
00398 }
00399
00400 template <typename T>
00401 void Item::setPayload( T* p )
00402 {
00403 p.You_MUST_NOT_use_a_pointer_as_payload;
00404 }
00405
00406 template <typename T>
00407 void Item::setPayload( std::auto_ptr<T> p )
00408 {
00409 p.Nice_try_but_a_std_auto_ptr_is_not_allowed_as_payload_either;
00410 }
00411
00412 }
00413
00414 Q_DECLARE_METATYPE(Akonadi::Item)
00415 Q_DECLARE_METATYPE(Akonadi::Item::List)
00416
00417 #endif