Akonadi

attributestorage.cpp
1/*
2 SPDX-FileCopyrightText: 2019 David Faure <faure@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "attributestorage_p.h"
8
9#include <QSharedData>
10
11using namespace Akonadi;
12
13namespace Akonadi
14{
15
16class AttributeStoragePrivate : public QSharedData
17{
18public:
19 AttributeStoragePrivate() = default;
20 AttributeStoragePrivate(AttributeStoragePrivate &other)
21 : QSharedData(other)
22 , atributes(other.atributes)
23 , modifiedAttributes(other.modifiedAttributes)
24 , deletedAttributes(other.deletedAttributes)
25 {
26 for (Attribute *attr : std::as_const(atributes)) {
27 atributes.insert(attr->type(), attr->clone());
28 }
29 }
30
31 ~AttributeStoragePrivate()
32 {
33 qDeleteAll(atributes);
34 }
35
37 std::set<QByteArray> modifiedAttributes;
38 QSet<QByteArray> deletedAttributes;
39};
40
41} // namespace Akonadi
42
43AttributeStorage::AttributeStorage()
44 : d(new AttributeStoragePrivate)
45{
46}
47
48AttributeStorage::AttributeStorage(const AttributeStorage &other)
49 : d(other.d)
50{
51}
52
53AttributeStorage::AttributeStorage(AttributeStorage &&other) noexcept
54{
55 d.swap(other.d);
56}
57
58AttributeStorage &AttributeStorage::operator=(const AttributeStorage &other)
59{
60 d = other.d;
61 return *this;
62}
63
64AttributeStorage &AttributeStorage::operator=(AttributeStorage &&other) noexcept
65{
66 d.swap(other.d);
67 return *this;
68}
69
70AttributeStorage::~AttributeStorage() = default;
71
72void AttributeStorage::detach()
73{
74 d.detach();
75}
76
77void AttributeStorage::addAttribute(Attribute *attr)
78{
79 Q_ASSERT(attr);
80 const QByteArray type = attr->type();
81 Attribute *existing = d->atributes.value(type);
82 if (existing) {
83 if (attr == existing) {
84 return;
85 }
86 d->atributes.remove(type);
87 delete existing;
88 }
89 d->atributes.insert(type, attr);
90 markAttributeModified(type);
91}
92
93void AttributeStorage::removeAttribute(const QByteArray &type)
94{
95 d->modifiedAttributes.erase(type);
96 d->deletedAttributes.insert(type);
97 delete d->atributes.take(type);
98}
99
100bool AttributeStorage::hasAttribute(const QByteArray &type) const
101{
102 return d->atributes.contains(type);
103}
104
105Attribute::List AttributeStorage::attributes() const
106{
107 return d->atributes.values();
108}
109
110void AttributeStorage::clearAttributes()
111{
112 for (Attribute *attr : std::as_const(d->atributes)) {
113 d->deletedAttributes.insert(attr->type());
114 delete attr;
115 }
116 d->atributes.clear();
117 d->modifiedAttributes.clear();
118}
119
120const Attribute *AttributeStorage::attribute(const QByteArray &type) const
121{
122 return d->atributes.value(type);
123}
124
125Attribute *AttributeStorage::attribute(const QByteArray &type)
126{
127 Attribute *attr = d->atributes.value(type);
128 if (attr) {
129 markAttributeModified(type);
130 }
131 return attr;
132}
133
134void AttributeStorage::markAttributeModified(const QByteArray &type)
135{
136 if (d->atributes.contains(type)) {
137 d->deletedAttributes.remove(type);
138 d->modifiedAttributes.insert(type);
139 }
140}
141
142void AttributeStorage::resetChangeLog()
143{
144 d->modifiedAttributes.clear();
145 d->deletedAttributes.clear();
146}
147
148QSet<QByteArray> AttributeStorage::deletedAttributes() const
149{
150 return d->deletedAttributes;
151}
152
153bool AttributeStorage::hasModifiedAttributes() const
154{
155 return !d->modifiedAttributes.empty();
156}
157
158std::vector<Attribute *> AttributeStorage::modifiedAttributes() const
159{
160 std::vector<Attribute *> ret;
161 ret.reserve(d->modifiedAttributes.size());
162 for (const auto &type : d->modifiedAttributes) {
163 Attribute *attr = d->atributes.value(type);
164 Q_ASSERT(attr);
165 ret.push_back(attr);
166 }
167 return ret;
168}
Provides interface for custom attributes for Entity.
Definition attribute.h:132
virtual QByteArray type() const =0
Returns the type of the attribute.
Helper integration between Akonadi and Qt.
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
void clear()
bool empty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:29 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.