• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • sources
  • kde-4.14
  • kdepimlibs
  • akonadi
attributefactory.cpp
1 /*
2  Copyright (c) 2007 - 2008 Volker Krause <vkrause@kde.org>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "attributefactory.h"
21 
22 #include "collectionquotaattribute.h"
23 #include "collectionrightsattribute_p.h"
24 #include "entitydisplayattribute.h"
25 #include "entityhiddenattribute.h"
26 #include "indexpolicyattribute.h"
27 #include "persistentsearchattribute.h"
28 #include "entitydeletedattribute.h"
29 #include "tagattribute.h"
30 #include "entityannotationsattribute.h"
31 
32 #include <KGlobal>
33 
34 #include <QtCore/QHash>
35 
36 using namespace Akonadi;
37 
38 namespace Akonadi {
39 namespace Internal {
40 
44 class DefaultAttribute : public Attribute
45 {
46 public:
47  explicit DefaultAttribute(const QByteArray &type, const QByteArray &value = QByteArray())
48  : mType(type)
49  , mValue(value)
50  {}
51 
52  QByteArray type() const
53  {
54  return mType;
55  }
56  Attribute *clone() const
57  {
58  return new DefaultAttribute(mType, mValue);
59  }
60 
61  QByteArray serialized() const
62  {
63  return mValue;
64  }
65  void deserialize(const QByteArray &data)
66  {
67  mValue = data;
68  }
69 
70 private:
71  QByteArray mType, mValue;
72 };
73 
77 class StaticAttributeFactory : public AttributeFactory
78 {
79 public:
80  StaticAttributeFactory()
81  : AttributeFactory()
82  , initialized(false) {}
83  void init() {
84  if (initialized) {
85  return;
86  }
87  initialized = true;
88 
89  // Register built-in attributes
90  AttributeFactory::registerAttribute<CollectionQuotaAttribute>();
91  AttributeFactory::registerAttribute<CollectionRightsAttribute>();
92  AttributeFactory::registerAttribute<EntityDisplayAttribute>();
93  AttributeFactory::registerAttribute<EntityHiddenAttribute>();
94  AttributeFactory::registerAttribute<IndexPolicyAttribute>();
95  AttributeFactory::registerAttribute<PersistentSearchAttribute>();
96  AttributeFactory::registerAttribute<EntityDeletedAttribute>();
97  AttributeFactory::registerAttribute<EntityAnnotationsAttribute>();
98  AttributeFactory::registerAttribute<TagAttribute>();
99  }
100  bool initialized;
101 };
102 
103 K_GLOBAL_STATIC(StaticAttributeFactory, s_attributeInstance)
104 
105 }
106 
107 using Akonadi::Internal::s_attributeInstance;
108 
112 class AttributeFactory::Private
113 {
114 public:
115  QHash<QByteArray, Attribute *> attributes;
116 };
117 
118 AttributeFactory *AttributeFactory::self()
119 {
120  s_attributeInstance->init();
121  return s_attributeInstance;
122 }
123 
124 AttributeFactory::AttributeFactory()
125  : d(new Private)
126 {
127 }
128 
129 AttributeFactory::~ AttributeFactory()
130 {
131  qDeleteAll(d->attributes);
132  delete d;
133 }
134 
135 void AttributeFactory::registerAttribute(Attribute *attr)
136 {
137  Q_ASSERT(attr);
138  Q_ASSERT(!attr->type().contains(' ') && !attr->type().contains('\'') && !attr->type().contains('"'));
139  QHash<QByteArray, Attribute *>::Iterator it = d->attributes.find(attr->type());
140  if (it != d->attributes.end()) {
141  delete *it;
142  d->attributes.erase(it);
143  }
144  d->attributes.insert(attr->type(), attr);
145 }
146 
147 Attribute *AttributeFactory::createAttribute(const QByteArray &type)
148 {
149  Attribute *attr = self()->d->attributes.value(type);
150  if (attr) {
151  return attr->clone();
152  }
153  return new Internal::DefaultAttribute(type);
154 }
155 
156 }
QByteArray
Akonadi::AttributeFactory
Provides the functionality of registering and creating arbitrary entity attributes.
Definition: attributefactory.h:48
Akonadi::Attribute
Provides interface for custom attributes for Entity.
Definition: attribute.h:138
Akonadi::AttributeFactory::registerAttribute
static void registerAttribute()
Registers a custom attribute of type T.
Definition: attributefactory.h:59
QHash
QHash::find
iterator find(const Key &key)
Akonadi::AttributeFactory::createAttribute
static Attribute * createAttribute(const QByteArray &type)
Creates an entity attribute object of the given type.
Definition: attributefactory.cpp:147
QByteArray::contains
bool contains(char ch) const
Akonadi::Attribute::clone
virtual Attribute * clone() const =0
Creates a copy of this attribute.
Akonadi::Attribute::type
virtual QByteArray type() const =0
Returns the type of the attribute.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:38:02 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

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

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal