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