Akonadi

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

KDE's Doxygen guidelines are available online.