akonadi
entity.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef AKONADI_ENTITY_H
00021 #define AKONADI_ENTITY_H
00022
00023 #include "akonadi_export.h"
00024
00025 namespace Akonadi {
00026 class Entity;
00027 }
00028
00029 AKONADI_EXPORT uint qHash( const Akonadi::Entity& );
00030
00031 #include <akonadi/attribute.h>
00032
00033 #include <KDE/KDebug>
00034
00035 #include <QtCore/QHash>
00036 #include <QtCore/QSharedDataPointer>
00037
00038 #define AKONADI_DECLARE_PRIVATE( Class ) \
00039 Class##Private* d_func(); \
00040 const Class##Private* d_func() const; \
00041 friend class Class##Private;
00042
00043 namespace Akonadi {
00044
00045 class EntityPrivate;
00046
00057 class AKONADI_EXPORT Entity
00058 {
00059 public:
00063 typedef qint64 Id;
00064
00068 ~Entity();
00069
00073 void setId( Id identifier );
00074
00078 Id id() const;
00079
00083 void setRemoteId( const QString& id );
00084
00088 QString remoteId() const;
00089
00093 bool isValid() const;
00094
00099 bool operator==( const Entity &other ) const;
00100
00105 bool operator!=( const Entity &other ) const;
00106
00110 Entity& operator=( const Entity &other );
00111
00122 void addAttribute( Attribute *attribute );
00123
00127 void removeAttribute( const QByteArray &name );
00128
00133 bool hasAttribute( const QByteArray &name ) const;
00134
00138 Attribute::List attributes() const;
00139
00143 void clearAttributes();
00144
00148 Attribute* attribute( const QByteArray &name ) const;
00149
00153 enum CreateOption
00154 {
00155 AddIfMissing
00156 };
00157
00165 template <typename T> inline T* attribute( CreateOption option )
00166 {
00167 Q_UNUSED( option );
00168
00169 const T dummy;
00170 if ( hasAttribute( dummy.type() ) ) {
00171 T* attr = dynamic_cast<T*>( attribute( dummy.type() ) );
00172 if ( attr )
00173 return attr;
00174 kWarning( 5250 ) << "Found attribute of unknown type" << dummy.type()
00175 << ". Did you forget to call AttributeFactory::registerAttribute()?";
00176 }
00177
00178 T* attr = new T();
00179 addAttribute( attr );
00180 return attr;
00181 }
00182
00186 template <typename T> inline T* attribute() const
00187 {
00188 const T dummy;
00189 if ( hasAttribute( dummy.type() ) ) {
00190 T* attr = dynamic_cast<T*>( attribute( dummy.type() ) );
00191 if ( attr )
00192 return attr;
00193 kWarning( 5250 ) << "Found attribute of unknown type" << dummy.type()
00194 << ". Did you forget to call AttributeFactory::registerAttribute()?";
00195 }
00196
00197 return 0;
00198 }
00199
00203 template <typename T> inline void removeAttribute()
00204 {
00205 const T dummy;
00206 removeAttribute( dummy.type() );
00207 }
00208
00212 template <typename T> inline bool hasAttribute() const
00213 {
00214 const T dummy;
00215 return hasAttribute( dummy.type() );
00216 }
00217
00218 protected:
00222 Entity( const Entity &other );
00223
00224
00225 Entity( EntityPrivate *dd );
00226 QSharedDataPointer<EntityPrivate> d_ptr;
00227
00228
00229 AKONADI_DECLARE_PRIVATE( Entity )
00230 };
00231
00232 }
00233
00234 #endif