Akonadi

attributefactory.cpp
1/*
2 SPDX-FileCopyrightText: 2007-2008 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "attributefactory.h"
8
9#include "collectionannotationsattribute.h"
10#include "collectionidentificationattribute.h"
11#include "collectionquotaattribute.h"
12#include "collectionrightsattribute_p.h"
13#include "entityannotationsattribute.h"
14#include "entitydeletedattribute.h"
15#include "entitydisplayattribute.h"
16#include "entityhiddenattribute.h"
17#include "favoritecollectionattribute.h"
18#include "indexpolicyattribute.h"
19#include "persistentsearchattribute.h"
20#include "tagattribute.h"
21
22#include <QHash>
23
24#include <unordered_map>
25
26using namespace Akonadi;
27
28namespace Akonadi
29{
30namespace Internal
31{
32/**
33 * @internal
34 */
35class DefaultAttribute : public Attribute
36{
37public:
38 explicit DefaultAttribute(const QByteArray &type, const QByteArray &value = QByteArray())
39 : mType(type)
40 , mValue(value)
41 {
42 }
43
44 DefaultAttribute(const DefaultAttribute &) = delete;
45 DefaultAttribute &operator=(const DefaultAttribute &) = delete;
46
47 QByteArray type() const override
48 {
49 return mType;
50 }
51
52 Attribute *clone() const override
53 {
54 return new DefaultAttribute(mType, mValue);
55 }
56
57 QByteArray serialized() const override
58 {
59 return mValue;
60 }
61
62 void deserialize(const QByteArray &data) override
63 {
64 mValue = data;
65 }
66
67private:
68 QByteArray mType, mValue;
69};
70
71/**
72 * @internal
73 */
74class StaticAttributeFactory : public AttributeFactory
75{
76public:
77 void init()
78 {
79 if (!initialized) {
80 initialized = true;
81
82 // Register built-in attributes
95 }
96 }
97 bool initialized = false;
98};
99
100Q_GLOBAL_STATIC(StaticAttributeFactory, s_attributeInstance) // NOLINT(readability-redundant-member-init)
101
102} // namespace Internal
103
104using Akonadi::Internal::s_attributeInstance;
105
106/**
107 * @internal
108 */
109class AttributeFactoryPrivate
110{
111public:
112 std::unordered_map<QByteArray, std::unique_ptr<Attribute>> attributes;
113};
114
115AttributeFactory *AttributeFactory::self()
116{
117 s_attributeInstance->init();
118 return s_attributeInstance;
119}
120
121AttributeFactory::AttributeFactory()
122 : d(new AttributeFactoryPrivate())
123{
124}
125
126AttributeFactory::~AttributeFactory() = default;
127
128void AttributeFactory::registerAttribute(std::unique_ptr<Attribute> attr)
129{
130 Q_ASSERT(attr);
131 Q_ASSERT(!attr->type().contains(' ') && !attr->type().contains('\'') && !attr->type().contains('"'));
132 auto it = d->attributes.find(attr->type());
133 if (it != d->attributes.end()) {
134 d->attributes.erase(it);
135 }
136 d->attributes.emplace(attr->type(), std::move(attr));
137}
138
140{
141 auto attr = self()->d->attributes.find(type);
142 if (attr == self()->d->attributes.cend()) {
143 return new Internal::DefaultAttribute(type);
144 }
145
146 return attr->second->clone();
147}
148
149} // namespace Akonadi
Provides the functionality of registering and creating arbitrary entity attributes.
static Attribute * createAttribute(const QByteArray &type)
Creates an entity attribute object of the given type.
static void registerAttribute()
Registers a custom attribute of type T.
Provides interface for custom attributes for Entity.
Definition attribute.h:132
virtual Attribute * clone() const =0
Creates a copy of this attribute.
Helper integration between Akonadi and Qt.
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:42 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.