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
36using namespace Akonadi;
37
38namespace Akonadi {
39namespace Internal {
40
41/**
42 * @internal
43 */
44class DefaultAttribute : public Attribute
45{
46public:
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
70private:
71 QByteArray mType, mValue;
72};
73
74/**
75 * @internal
76 */
77class StaticAttributeFactory : public AttributeFactory
78{
79public:
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
103K_GLOBAL_STATIC(StaticAttributeFactory, s_attributeInstance)
104
105}
106
107using Akonadi::Internal::s_attributeInstance;
108
109/**
110 * @internal
111 */
112class AttributeFactory::Private
113{
114public:
115 QHash<QByteArray, Attribute *> attributes;
116};
117
118AttributeFactory *AttributeFactory::self()
119{
120 s_attributeInstance->init();
121 return s_attributeInstance;
122}
123
124AttributeFactory::AttributeFactory()
125 : d(new Private)
126{
127}
128
129AttributeFactory::~ AttributeFactory()
130{
131 qDeleteAll(d->attributes);
132 delete d;
133}
134
135void 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
147Attribute *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}
157