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::addAttribute(Attribute *attr)
73{
74 Q_ASSERT(attr);
75 const QByteArray type = attr->type();
76 Attribute *existing = d->atributes.value(type);
77 if (existing) {
78 if (attr == existing) {
79 return;
80 }
81 d->atributes.remove(type);
82 delete existing;
83 }
84 d->atributes.insert(type, attr);
85 markAttributeModified(type);
86}
87
88void AttributeStorage::removeAttribute(const QByteArray &type)
89{
90 d->modifiedAttributes.erase(type);
91 d->deletedAttributes.insert(type);
92 delete d->atributes.take(type);
93}
94
95bool AttributeStorage::hasAttribute(const QByteArray &type) const
96{
97 return d->atributes.contains(type);
98}
99
100Attribute::List AttributeStorage::attributes() const
101{
102 return d->atributes.values();
103}
104
105void AttributeStorage::clearAttributes()
106{
107 for (Attribute *attr : std::as_const(d->atributes)) {
108 d->deletedAttributes.insert(attr->type());
109 delete attr;
110 }
111 d->atributes.clear();
112 d->modifiedAttributes.clear();
113}
114
115const Attribute *AttributeStorage::attribute(const QByteArray &type) const
116{
117 return d->atributes.value(type);
118}
119
120Attribute *AttributeStorage::attribute(const QByteArray &type)
121{
122 Attribute *attr = d->atributes.value(type);
123 if (attr) {
124 markAttributeModified(type);
125 }
126 return attr;
127}
128
129void AttributeStorage::markAttributeModified(const QByteArray &type)
130{
131 if (d->atributes.contains(type)) {
132 d->deletedAttributes.remove(type);
133 d->modifiedAttributes.insert(type);
134 }
135}
136
137void AttributeStorage::resetChangeLog()
138{
139 d->modifiedAttributes.clear();
140 d->deletedAttributes.clear();
141}
142
143QSet<QByteArray> AttributeStorage::deletedAttributes() const
144{
145 return d->deletedAttributes;
146}
147
148bool AttributeStorage::hasModifiedAttributes() const
149{
150 return !d->modifiedAttributes.empty();
151}
152
153std::vector<Attribute *> AttributeStorage::modifiedAttributes() const
154{
155 std::vector<Attribute *> ret;
156 ret.reserve(d->modifiedAttributes.size());
157 for (const auto &type : d->modifiedAttributes) {
158 Attribute *attr = d->atributes.value(type);
159 Q_ASSERT(attr);
160 ret.push_back(attr);
161 }
162 return ret;
163}
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)
A glue between Qt and the standard library.
iterator insert(const_iterator before, parameter_type value)
bool empty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:38 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.