• Skip to content
  • Skip to link menu
KDE 4.2 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 "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       // Register built-in attributes
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 }

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
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.7.1
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