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 #include "persistentsearchattribute.h"
00027
00028 #include <KGlobal>
00029
00030 #include <QtCore/QHash>
00031
00032 using namespace Akonadi;
00033
00034 namespace Akonadi {
00035 namespace Internal {
00036
00040 class DefaultAttribute : public Attribute
00041 {
00042 public:
00043 explicit DefaultAttribute( const QByteArray &type, const QByteArray &value = QByteArray() ) :
00044 mType( type ),
00045 mValue( value )
00046 {}
00047
00048 QByteArray type() const { return mType; }
00049 Attribute* clone() const
00050 {
00051 return new DefaultAttribute( mType, mValue );
00052 }
00053
00054 QByteArray serialized() const { return mValue; }
00055 void deserialize( const QByteArray &data ) { mValue = data; }
00056
00057 private:
00058 QByteArray mType, mValue;
00059 };
00060
00064 class StaticAttributeFactory : public AttributeFactory
00065 {
00066 public:
00067 StaticAttributeFactory() : AttributeFactory(), initialized( false ) {}
00068 void init() {
00069 if ( initialized )
00070 return;
00071 initialized = true;
00072
00073
00074 AttributeFactory::registerAttribute<CollectionQuotaAttribute>();
00075 AttributeFactory::registerAttribute<CollectionRightsAttribute>();
00076 AttributeFactory::registerAttribute<EntityDisplayAttribute>();
00077 AttributeFactory::registerAttribute<EntityHiddenAttribute>();
00078 AttributeFactory::registerAttribute<PersistentSearchAttribute>();
00079 }
00080 bool initialized;
00081 };
00082
00083 K_GLOBAL_STATIC( StaticAttributeFactory, s_attributeInstance )
00084
00085 }
00086
00087 using Akonadi::Internal::s_attributeInstance;
00088
00092 class AttributeFactory::Private
00093 {
00094 public:
00095 QHash<QByteArray, Attribute*> attributes;
00096 };
00097
00098
00099 AttributeFactory* AttributeFactory::self()
00100 {
00101 s_attributeInstance->init();
00102 return s_attributeInstance;
00103 }
00104
00105 AttributeFactory::AttributeFactory()
00106 : d( new Private )
00107 {
00108 }
00109
00110 AttributeFactory::~ AttributeFactory()
00111 {
00112 qDeleteAll( d->attributes );
00113 delete d;
00114 }
00115
00116 void AttributeFactory::registerAttribute(Attribute *attr)
00117 {
00118 Q_ASSERT( attr );
00119 QHash<QByteArray, Attribute*>::Iterator it = d->attributes.find( attr->type() );
00120 if ( it != d->attributes.end() ) {
00121 delete *it;
00122 d->attributes.erase( it );
00123 }
00124 d->attributes.insert( attr->type(), attr );
00125 }
00126
00127 Attribute* AttributeFactory::createAttribute(const QByteArray &type)
00128 {
00129 Attribute* attr = self()->d->attributes.value( type );
00130 if ( attr )
00131 return attr->clone();
00132 return new Internal::DefaultAttribute( type );
00133 }
00134
00135 }
00136