entity.h
00001 /* 00002 Copyright (c) 2008 Tobias Koenig <tokoe@kde.org> 00003 00004 This library is free software; you can redistribute it and/or modify it 00005 under the terms of the GNU Library General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or (at your 00007 option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, but WITHOUT 00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00012 License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to the 00016 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00017 02110-1301, USA. 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 Collection; 00046 class EntityPrivate; 00047 00058 class AKONADI_EXPORT Entity 00059 { 00060 public: 00064 typedef qint64 Id; 00065 00069 void setId( Id identifier ); 00070 00074 Id id() const; 00075 00079 void setRemoteId( const QString& id ); 00080 00084 QString remoteId() const; 00085 00095 void setRemoteRevision( const QString& revision ); 00096 00103 QString remoteRevision() const; 00104 00108 bool isValid() const; 00109 00114 bool operator==( const Entity &other ) const; 00115 00120 bool operator!=( const Entity &other ) const; 00121 00125 Entity& operator=( const Entity &other ); 00126 00132 bool operator<( const Entity &other ) const; 00133 00140 Collection parentCollection() const; 00141 00148 Collection& parentCollection(); 00149 00159 void setParentCollection( const Collection &parent ); 00160 00171 void addAttribute( Attribute *attribute ); 00172 00176 void removeAttribute( const QByteArray &name ); 00177 00182 bool hasAttribute( const QByteArray &name ) const; 00183 00187 Attribute::List attributes() const; 00188 00192 void clearAttributes(); 00193 00197 Attribute* attribute( const QByteArray &name ) const; 00198 00202 enum CreateOption 00203 { 00204 AddIfMissing 00205 }; 00206 00214 template <typename T> inline T* attribute( CreateOption option ) 00215 { 00216 Q_UNUSED( option ); 00217 00218 const T dummy; 00219 if ( hasAttribute( dummy.type() ) ) { 00220 T* attr = dynamic_cast<T*>( attribute( dummy.type() ) ); 00221 if ( attr ) 00222 return attr; 00223 kWarning( 5250 ) << "Found attribute of unknown type" << dummy.type() 00224 << ". Did you forget to call AttributeFactory::registerAttribute()?"; 00225 } 00226 00227 T* attr = new T(); 00228 addAttribute( attr ); 00229 return attr; 00230 } 00231 00235 template <typename T> inline T* attribute() const 00236 { 00237 const T dummy; 00238 if ( hasAttribute( dummy.type() ) ) { 00239 T* attr = dynamic_cast<T*>( attribute( dummy.type() ) ); 00240 if ( attr ) 00241 return attr; 00242 kWarning( 5250 ) << "Found attribute of unknown type" << dummy.type() 00243 << ". Did you forget to call AttributeFactory::registerAttribute()?"; 00244 } 00245 00246 return 0; 00247 } 00248 00252 template <typename T> inline void removeAttribute() 00253 { 00254 const T dummy; 00255 removeAttribute( dummy.type() ); 00256 } 00257 00261 template <typename T> inline bool hasAttribute() const 00262 { 00263 const T dummy; 00264 return hasAttribute( dummy.type() ); 00265 } 00266 00267 protected: 00271 Entity( const Entity &other ); 00272 00276 ~Entity(); 00277 00278 //@cond PRIVATE 00279 Entity( EntityPrivate *dd ); 00280 QSharedDataPointer<EntityPrivate> d_ptr; 00281 //@endcond 00282 00283 AKONADI_DECLARE_PRIVATE( Entity ) 00284 }; 00285 00286 } 00287 00288 #endif