akonadi
attributefactory.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "attributefactory.h"
00021
00022 #include "collectionquotaattribute.h"
00023 #include "collectionrightsattribute_p.h"
00024 #include "entitydisplayattribute.h"
00025 #include "entityhiddenattribute.h"
00026
00027 #include <KGlobal>
00028
00029 #include <QtCore/QHash>
00030
00031 using namespace Akonadi;
00032
00036 class DefaultAttribute : public Attribute
00037 {
00038 public:
00039 explicit DefaultAttribute( const QByteArray &type, const QByteArray &value = QByteArray() ) :
00040 mType( type ),
00041 mValue( value )
00042 {}
00043
00044 QByteArray type() const { return mType; }
00045 Attribute* clone() const
00046 {
00047 return new DefaultAttribute( mType, mValue );
00048 }
00049
00050 QByteArray serialized() const { return mValue; }
00051 void deserialize( const QByteArray &data ) { mValue = data; }
00052
00053 private:
00054 QByteArray mType, mValue;
00055 };
00056
00060 class AttributeFactory::Private
00061 {
00062 public:
00063 QHash<QByteArray, Attribute*> attributes;
00064 };
00065
00069 class StaticAttributeFactory : public AttributeFactory
00070 {
00071 public:
00072 StaticAttributeFactory() : AttributeFactory(), initialized( false ) {}
00073 void init() {
00074 if ( initialized )
00075 return;
00076 initialized = true;
00077
00078
00079 AttributeFactory::registerAttribute<CollectionQuotaAttribute>();
00080 AttributeFactory::registerAttribute<CollectionRightsAttribute>();
00081 AttributeFactory::registerAttribute<EntityDisplayAttribute>();
00082 AttributeFactory::registerAttribute<EntityHiddenAttribute>();
00083 }
00084 bool initialized;
00085 };
00086
00087 K_GLOBAL_STATIC( StaticAttributeFactory, s_attributeInstance )
00088
00089
00090 AttributeFactory* AttributeFactory::self()
00091 {
00092 s_attributeInstance->init();
00093 return s_attributeInstance;
00094 }
00095
00096 AttributeFactory::AttributeFactory()
00097 : d( new Private )
00098 {
00099 }
00100
00101 AttributeFactory::~ AttributeFactory()
00102 {
00103 qDeleteAll( d->attributes );
00104 delete d;
00105 }
00106
00107 void AttributeFactory::registerAttribute(Attribute *attr)
00108 {
00109 Q_ASSERT( attr );
00110 QHash<QByteArray, Attribute*>::Iterator it = d->attributes.find( attr->type() );
00111 if ( it != d->attributes.end() ) {
00112 delete *it;
00113 d->attributes.erase( it );
00114 }
00115 d->attributes.insert( attr->type(), attr );
00116 }
00117
00118 Attribute* AttributeFactory::createAttribute(const QByteArray &type)
00119 {
00120 Attribute* attr = self()->d->attributes.value( type );
00121 if ( attr )
00122 return attr->clone();
00123 return new DefaultAttribute( type );
00124 }