• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

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 
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       // Register built-in attributes
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 }

akonadi

Skip menu "akonadi"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.6.2-20100208
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal