akonadi
attributefactory.cpp
00001 /* 00002 Copyright (c) 2007 - 2008 Volker Krause <vkrause@kde.org> 00003 00004 This library is free software; you can redistribute it and/or modify it 00005 under the terms of the GNU Library General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or (at your 00007 option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, but WITHOUT 00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00012 License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to the 00016 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00017 02110-1301, USA. 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 "indexpolicyattribute.h" 00027 #include "persistentsearchattribute.h" 00028 00029 #include <KGlobal> 00030 00031 #include <QtCore/QHash> 00032 00033 using namespace Akonadi; 00034 00035 namespace Akonadi { 00036 namespace Internal { 00037 00041 class DefaultAttribute : public Attribute 00042 { 00043 public: 00044 explicit DefaultAttribute( const QByteArray &type, const QByteArray &value = QByteArray() ) : 00045 mType( type ), 00046 mValue( value ) 00047 {} 00048 00049 QByteArray type() const { return mType; } 00050 Attribute* clone() const 00051 { 00052 return new DefaultAttribute( mType, mValue ); 00053 } 00054 00055 QByteArray serialized() const { return mValue; } 00056 void deserialize( const QByteArray &data ) { mValue = data; } 00057 00058 private: 00059 QByteArray mType, mValue; 00060 }; 00061 00065 class StaticAttributeFactory : public AttributeFactory 00066 { 00067 public: 00068 StaticAttributeFactory() : AttributeFactory(), initialized( false ) {} 00069 void init() { 00070 if ( initialized ) 00071 return; 00072 initialized = true; 00073 00074 // Register built-in attributes 00075 AttributeFactory::registerAttribute<CollectionQuotaAttribute>(); 00076 AttributeFactory::registerAttribute<CollectionRightsAttribute>(); 00077 AttributeFactory::registerAttribute<EntityDisplayAttribute>(); 00078 AttributeFactory::registerAttribute<EntityHiddenAttribute>(); 00079 AttributeFactory::registerAttribute<IndexPolicyAttribute>(); 00080 AttributeFactory::registerAttribute<PersistentSearchAttribute>(); 00081 } 00082 bool initialized; 00083 }; 00084 00085 K_GLOBAL_STATIC( StaticAttributeFactory, s_attributeInstance ) 00086 00087 } 00088 00089 using Akonadi::Internal::s_attributeInstance; 00090 00094 class AttributeFactory::Private 00095 { 00096 public: 00097 QHash<QByteArray, Attribute*> attributes; 00098 }; 00099 00100 00101 AttributeFactory* AttributeFactory::self() 00102 { 00103 s_attributeInstance->init(); 00104 return s_attributeInstance; 00105 } 00106 00107 AttributeFactory::AttributeFactory() 00108 : d( new Private ) 00109 { 00110 } 00111 00112 AttributeFactory::~ AttributeFactory() 00113 { 00114 qDeleteAll( d->attributes ); 00115 delete d; 00116 } 00117 00118 void AttributeFactory::registerAttribute(Attribute *attr) 00119 { 00120 Q_ASSERT( attr ); 00121 Q_ASSERT( !attr->type().contains(' ') && !attr->type().contains('\'') && !attr->type().contains('"') ); 00122 QHash<QByteArray, Attribute*>::Iterator it = d->attributes.find( attr->type() ); 00123 if ( it != d->attributes.end() ) { 00124 delete *it; 00125 d->attributes.erase( it ); 00126 } 00127 d->attributes.insert( attr->type(), attr ); 00128 } 00129 00130 Attribute* AttributeFactory::createAttribute(const QByteArray &type) 00131 { 00132 Attribute* attr = self()->d->attributes.value( type ); 00133 if ( attr ) 00134 return attr->clone(); 00135 return new Internal::DefaultAttribute( type ); 00136 } 00137 00138 } 00139