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 "collectionrightsattribute.h"
00023
00024 #include <KGlobal>
00025
00026 #include <QtCore/QHash>
00027
00028 using namespace Akonadi;
00029
00033 class DefaultAttribute : public Attribute
00034 {
00035 public:
00036 explicit DefaultAttribute( const QByteArray &type, const QByteArray &value = QByteArray() ) :
00037 mType( type ),
00038 mValue( value )
00039 {}
00040
00041 QByteArray type() const { return mType; }
00042 Attribute* clone() const
00043 {
00044 return new DefaultAttribute( mType, mValue );
00045 }
00046
00047 QByteArray serialized() const { return mValue; }
00048 void deserialize( const QByteArray &data ) { mValue = data; }
00049
00050 private:
00051 QByteArray mType, mValue;
00052 };
00053
00057 class AttributeFactory::Private
00058 {
00059 public:
00060 QHash<QByteArray, Attribute*> attributes;
00061 };
00062
00066 class StaticAttributeFactory : public AttributeFactory
00067 {
00068 public:
00069 StaticAttributeFactory() : AttributeFactory(), initialized( false ) {}
00070 void init() {
00071 if ( initialized )
00072 return;
00073 initialized = true;
00074
00075
00076 AttributeFactory::registerAttribute<CollectionRightsAttribute>();
00077 }
00078 bool initialized;
00079 };
00080
00081 K_GLOBAL_STATIC( StaticAttributeFactory, s_attributeInstance )
00082
00083
00084 AttributeFactory* AttributeFactory::self()
00085 {
00086 s_attributeInstance->init();
00087 return s_attributeInstance;
00088 }
00089
00090 AttributeFactory::AttributeFactory()
00091 : d( new Private )
00092 {
00093 }
00094
00095 AttributeFactory::~ AttributeFactory()
00096 {
00097 qDeleteAll( d->attributes );
00098 delete d;
00099 }
00100
00101 void AttributeFactory::registerAttribute(Attribute *attr)
00102 {
00103 Q_ASSERT( !d->attributes.contains( attr->type() ) );
00104 d->attributes.insert( attr->type(), attr );
00105 }
00106
00107 Attribute* AttributeFactory::createAttribute(const QByteArray &type)
00108 {
00109 Attribute* attr = self()->d->attributes.value( type );
00110 if ( attr )
00111 return attr->clone();
00112 return new DefaultAttribute( type );
00113 }