Akonadi

attributefactory.cpp
1 /*
2  Copyright (c) 2007 - 2008 Volker Krause <[email protected]>
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 #include "favoritecollectionattribute.h"
32 
33 #include <QHash>
34 
35 using namespace Akonadi;
36 
37 namespace Akonadi
38 {
39 namespace Internal
40 {
41 
45 class DefaultAttribute : public Attribute
46 {
47 public:
48  explicit DefaultAttribute(const QByteArray &type, const QByteArray &value = QByteArray())
49  : mType(type)
50  , mValue(value)
51  {
52  }
53 
54  QByteArray type() const override
55  {
56  return mType;
57  }
58  Attribute *clone() const override
59  {
60  return new DefaultAttribute(mType, mValue);
61  }
62 
63  QByteArray serialized() const override
64  {
65  return mValue;
66  }
67  void deserialize(const QByteArray &data) override {
68  mValue = data;
69  }
70 
71 private:
72  QByteArray mType, mValue;
73 };
74 
78 class StaticAttributeFactory : public AttributeFactory
79 {
80 public:
81  StaticAttributeFactory()
83  , initialized(false)
84  {
85  }
86  void init()
87  {
88  if (initialized) {
89  return;
90  }
91  initialized = true;
92 
93  // Register built-in attributes
94  AttributeFactory::registerAttribute<CollectionQuotaAttribute>();
95  AttributeFactory::registerAttribute<CollectionRightsAttribute>();
96  AttributeFactory::registerAttribute<EntityDisplayAttribute>();
97  AttributeFactory::registerAttribute<EntityHiddenAttribute>();
98  AttributeFactory::registerAttribute<IndexPolicyAttribute>();
99  AttributeFactory::registerAttribute<PersistentSearchAttribute>();
100  AttributeFactory::registerAttribute<EntityDeletedAttribute>();
101  AttributeFactory::registerAttribute<EntityAnnotationsAttribute>();
102  AttributeFactory::registerAttribute<TagAttribute>();
103  AttributeFactory::registerAttribute<FavoriteCollectionAttribute>();
104  }
105  bool initialized;
106 };
107 
108 Q_GLOBAL_STATIC(StaticAttributeFactory, s_attributeInstance)
109 
110 }
111 
112 using Akonadi::Internal::s_attributeInstance;
113 
117 class Q_DECL_HIDDEN AttributeFactory::Private
118 {
119 public:
121 };
122 
123 AttributeFactory *AttributeFactory::self()
124 {
125  s_attributeInstance->init();
126  return s_attributeInstance;
127 }
128 
129 AttributeFactory::AttributeFactory()
130  : d(new Private)
131 {
132 }
133 
134 AttributeFactory::~AttributeFactory()
135 {
136  qDeleteAll(d->attributes);
137  delete d;
138 }
139 
141 {
142  Q_ASSERT(attr);
143  Q_ASSERT(!attr->type().contains(' ') && !attr->type().contains('\'') && !attr->type().contains('"'));
144  QHash<QByteArray, Attribute *>::Iterator it = d->attributes.find(attr->type());
145  if (it != d->attributes.end()) {
146  delete *it;
147  d->attributes.erase(it);
148  }
149  d->attributes.insert(attr->type(), attr);
150 }
151 
153 {
154  Attribute *attr = self()->d->attributes.value(type);
155  if (attr) {
156  return attr->clone();
157  }
158  return new Internal::DefaultAttribute(type);
159 }
160 
161 }
Provides the functionality of registering and creating arbitrary entity attributes.
Provides interface for custom attributes for Entity.
Definition: attribute.h:139
virtual QByteArray type() const =0
Returns the type of the attribute.
QUICKADDONS_EXPORT void init()
static void registerAttribute()
Registers a custom attribute of type T.
QHash::iterator find(const Key &key)
static Attribute * createAttribute(const QByteArray &type)
Creates an entity attribute object of the given type.
virtual Attribute * clone() const =0
Creates a copy of this attribute.
Helper integration between Akonadi and Qt.
bool contains(char ch) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed May 27 2020 22:43:36 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.