Changeset View
Changeset View
Standalone View
Standalone View
src/core/attributestorage.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (c) 2019 David Faure <faure@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 "attributestorage_p.h" | ||||
21 | | ||||
22 | using namespace Akonadi; | ||||
23 | | ||||
24 | AttributeStorage::AttributeStorage() | ||||
25 | { | ||||
26 | } | ||||
27 | | ||||
28 | AttributeStorage::AttributeStorage(const AttributeStorage &other) | ||||
29 | : mModifiedAttributes(other.mModifiedAttributes), | ||||
30 | mDeletedAttributes(other.mDeletedAttributes) | ||||
31 | { | ||||
32 | for (Attribute *attr : qAsConst(other.mAttributes)) { | ||||
33 | mAttributes.insert(attr->type(), attr->clone()); | ||||
34 | } | ||||
35 | } | ||||
36 | | ||||
37 | AttributeStorage &AttributeStorage::operator=(const AttributeStorage &other) | ||||
38 | { | ||||
39 | AttributeStorage copy(other); | ||||
40 | swap(copy); | ||||
41 | return *this; | ||||
42 | } | ||||
43 | | ||||
44 | void AttributeStorage::swap(AttributeStorage &other) noexcept | ||||
45 | { | ||||
46 | using std::swap; | ||||
47 | swap(other.mAttributes, mAttributes); | ||||
48 | swap(other.mModifiedAttributes, mModifiedAttributes); | ||||
49 | swap(other.mDeletedAttributes, mDeletedAttributes); | ||||
50 | } | ||||
51 | | ||||
52 | AttributeStorage::~AttributeStorage() | ||||
53 | { | ||||
54 | qDeleteAll(mAttributes); | ||||
55 | } | ||||
56 | | ||||
57 | void AttributeStorage::addAttribute(Attribute *attr) | ||||
58 | { | ||||
59 | Q_ASSERT(attr); | ||||
60 | const QByteArray type = attr->type(); | ||||
61 | Attribute *existing = mAttributes.value(type); | ||||
62 | if (existing) { | ||||
63 | if (attr == existing) { | ||||
64 | return; | ||||
65 | } | ||||
66 | mAttributes.remove(type); | ||||
67 | delete existing; | ||||
68 | } | ||||
69 | mAttributes.insert(type, attr); | ||||
70 | markAttributeModified(type); | ||||
71 | } | ||||
72 | | ||||
73 | void AttributeStorage::removeAttribute(const QByteArray &type) | ||||
74 | { | ||||
75 | mModifiedAttributes.erase(type); | ||||
76 | mDeletedAttributes.insert(type); | ||||
77 | delete mAttributes.take(type); | ||||
78 | } | ||||
79 | | ||||
80 | bool AttributeStorage::hasAttribute(const QByteArray &type) const | ||||
81 | { | ||||
82 | return mAttributes.contains(type); | ||||
83 | } | ||||
84 | | ||||
85 | Attribute::List AttributeStorage::attributes() const | ||||
86 | { | ||||
87 | return mAttributes.values(); | ||||
88 | } | ||||
89 | | ||||
90 | void AttributeStorage::clearAttributes() | ||||
91 | { | ||||
92 | for (Attribute *attr : qAsConst(mAttributes)) { | ||||
93 | mDeletedAttributes.insert(attr->type()); | ||||
94 | delete attr; | ||||
95 | } | ||||
96 | mAttributes.clear(); | ||||
97 | mModifiedAttributes.clear(); | ||||
98 | } | ||||
99 | | ||||
100 | Attribute *AttributeStorage::attribute(const QByteArray &type) const | ||||
101 | { | ||||
102 | return mAttributes.value(type); | ||||
103 | } | ||||
104 | | ||||
105 | void AttributeStorage::markAttributeModified(const QByteArray &type) | ||||
106 | { | ||||
107 | mDeletedAttributes.remove(type); | ||||
108 | mModifiedAttributes.insert(type); | ||||
109 | } | ||||
110 | | ||||
111 | void AttributeStorage::resetChangeLog() | ||||
112 | { | ||||
113 | mModifiedAttributes.clear(); | ||||
114 | mDeletedAttributes.clear(); | ||||
115 | } | ||||
116 | | ||||
117 | QSet<QByteArray> AttributeStorage::deletedAttributes() const | ||||
118 | { | ||||
119 | return mDeletedAttributes; | ||||
120 | } | ||||
121 | | ||||
122 | bool AttributeStorage::hasModifiedAttributes() const | ||||
123 | { | ||||
124 | return !mModifiedAttributes.empty(); | ||||
125 | } | ||||
126 | | ||||
127 | std::vector<Attribute *> AttributeStorage::modifiedAttributes() const | ||||
128 | { | ||||
129 | std::vector<Attribute *> ret; | ||||
130 | ret.reserve(mModifiedAttributes.size()); | ||||
131 | for (const auto &type : mModifiedAttributes) { | ||||
132 | Attribute *attr = mAttributes.value(type); | ||||
133 | Q_ASSERT(attr); | ||||
134 | ret.push_back(attr); | ||||
135 | } | ||||
136 | return ret; | ||||
137 | } |