akonadi
entity.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "entity.h"
00021 #include "entity_p.h"
00022 #include "collection.h"
00023
00024 #include <kglobal.h>
00025
00026 using namespace Akonadi;
00027
00028 K_GLOBAL_STATIC( Akonadi::Collection, s_defaultParentCollection )
00029
00030
00033 static void assignEntityPrivate( QSharedDataPointer<EntityPrivate> &one, const QSharedDataPointer<EntityPrivate> &other )
00034 {
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 QSharedDataPointer<EntityPrivate> temp = other;
00050 one = temp;
00051 }
00052
00053
00054 Entity::Entity( const Entity &other )
00055 {
00056 assignEntityPrivate( d_ptr, other.d_ptr );
00057 }
00058
00059 Entity::Entity( EntityPrivate *dd )
00060 : d_ptr( dd )
00061 {
00062 }
00063
00064 Entity::~Entity()
00065 {
00066 }
00067
00068 void Entity::setId( Id id )
00069 {
00070 d_ptr->mId = id;
00071 }
00072
00073 Entity::Id Entity::id() const
00074 {
00075 return d_ptr->mId;
00076 }
00077
00078 void Entity::setRemoteId( const QString& id )
00079 {
00080 d_ptr->mRemoteId = id;
00081 }
00082
00083 QString Entity::remoteId() const
00084 {
00085 return d_ptr->mRemoteId;
00086 }
00087
00088 bool Entity::isValid() const
00089 {
00090 return ( d_ptr->mId >= 0 );
00091 }
00092
00093 bool Entity::operator==( const Entity &other ) const
00094 {
00095 return ( d_ptr->mId == other.d_ptr->mId );
00096 }
00097
00098 bool Akonadi::Entity::operator!=(const Entity & other) const
00099 {
00100 return d_ptr->mId != other.d_ptr->mId;
00101 }
00102
00103 Entity& Entity ::operator=( const Entity &other )
00104 {
00105 if ( this != &other ) {
00106 assignEntityPrivate( d_ptr, other.d_ptr );
00107 }
00108
00109 return *this;
00110 }
00111
00112 void Entity::addAttribute(Attribute * attr)
00113 {
00114 if ( d_ptr->mAttributes.contains( attr->type() ) ) {
00115 Attribute *existing = d_ptr->mAttributes.value( attr->type() );
00116 if ( attr == existing ) {
00117 return;
00118 }
00119 d_ptr->mAttributes.remove( attr->type() );
00120 delete existing;
00121 }
00122 d_ptr->mAttributes.insert( attr->type(), attr );
00123 d_ptr->mDeletedAttributes.remove( attr->type() );
00124 }
00125
00126 void Entity::removeAttribute( const QByteArray &type )
00127 {
00128 if ( d_ptr->mAttributes.contains( type ) ) {
00129 d_ptr->mDeletedAttributes.insert( type );
00130 delete d_ptr->mAttributes.take( type );
00131 }
00132 }
00133
00134 bool Entity::hasAttribute(const QByteArray & type) const
00135 {
00136 return d_ptr->mAttributes.contains( type );
00137 }
00138
00139 Attribute::List Entity::attributes() const
00140 {
00141 return d_ptr->mAttributes.values();
00142 }
00143
00144 void Akonadi::Entity::clearAttributes()
00145 {
00146 foreach ( Attribute *attr, d_ptr->mAttributes ) {
00147 d_ptr->mDeletedAttributes.insert( attr->type() );
00148 delete attr;
00149 }
00150 d_ptr->mAttributes.clear();
00151 }
00152
00153 Attribute * Entity::attribute(const QByteArray & type) const
00154 {
00155 if ( d_ptr->mAttributes.contains( type ) )
00156 return d_ptr->mAttributes.value( type );
00157 return 0;
00158 }
00159
00160 uint qHash( const Akonadi::Entity &entity )
00161 {
00162 return qHash( entity.id() );
00163 }
00164
00165 Collection& Entity::parentCollection()
00166 {
00167 if ( !d_ptr->mParent )
00168 d_ptr->mParent = new Collection();
00169 return *(d_ptr->mParent);
00170 }
00171
00172 Collection Entity::parentCollection() const
00173 {
00174 if ( !d_ptr->mParent )
00175 return *(s_defaultParentCollection);
00176 else
00177 return *(d_ptr->mParent);
00178 }
00179
00180 void Entity::setParentCollection( const Collection &parent )
00181 {
00182 delete d_ptr->mParent;
00183 d_ptr->mParent = new Collection( parent );
00184 }
00185
00186 AKONADI_DEFINE_PRIVATE( Entity )