1 | /* |
2 | Copyright (c) 2014 Christian Mollekopf <mollekopf@kolabsys.com> |
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 | #include "attributeentity.h" |
20 | #include <QHash> |
21 | #include <QSet> |
22 | |
23 | using namespace Akonadi; |
24 | |
25 | struct Akonadi::AttributeEntity::Private |
26 | { |
27 | Private() |
28 | {} |
29 | |
30 | ~Private() |
31 | { |
32 | qDeleteAll(mAttributes); |
33 | } |
34 | |
35 | Private(const Private &other) |
36 | { |
37 | Q_FOREACH (Attribute *attr, other.mAttributes) { |
38 | mAttributes.insert(attr->type(), attr->clone()); |
39 | } |
40 | mDeletedAttributes = other.mDeletedAttributes; |
41 | } |
42 | |
43 | QHash<QByteArray, Attribute *> mAttributes; |
44 | QSet<QByteArray> mDeletedAttributes; |
45 | }; |
46 | |
47 | AttributeEntity::AttributeEntity() |
48 | : d_ptr(new Private) |
49 | { |
50 | |
51 | } |
52 | |
53 | AttributeEntity::AttributeEntity(const AttributeEntity &other) |
54 | : d_ptr(new Private) |
55 | { |
56 | operator=(other); |
57 | } |
58 | |
59 | AttributeEntity::~AttributeEntity() |
60 | { |
61 | |
62 | } |
63 | |
64 | Akonadi::AttributeEntity &Akonadi::AttributeEntity::operator=(const Akonadi::AttributeEntity &other) |
65 | { |
66 | QHash<QByteArray, Attribute *>::const_iterator it = other.d_ptr->mAttributes.constBegin(); |
67 | for (; it != other.d_ptr->mAttributes.constEnd(); it++) { |
68 | d_ptr->mAttributes.insert(it.key(), it.value()->clone()); |
69 | } |
70 | d_ptr->mDeletedAttributes = other.d_ptr->mDeletedAttributes; |
71 | return *this; |
72 | } |
73 | |
74 | void AttributeEntity::addAttribute(Attribute *attr) |
75 | { |
76 | if (d_ptr->mAttributes.contains(attr->type())) { |
77 | Attribute *existing = d_ptr->mAttributes.value(attr->type()); |
78 | if (attr == existing) { |
79 | return; |
80 | } |
81 | d_ptr->mAttributes.remove(attr->type()); |
82 | delete existing; |
83 | } |
84 | d_ptr->mAttributes.insert(attr->type(), attr); |
85 | d_ptr->mDeletedAttributes.remove(attr->type()); |
86 | } |
87 | |
88 | void AttributeEntity::removeAttribute(const QByteArray &type) |
89 | { |
90 | d_ptr->mDeletedAttributes.insert(type); |
91 | delete d_ptr->mAttributes.take(type); |
92 | } |
93 | |
94 | bool AttributeEntity::hasAttribute(const QByteArray &type) const |
95 | { |
96 | return d_ptr->mAttributes.contains(type); |
97 | } |
98 | |
99 | Attribute::List AttributeEntity::attributes() const |
100 | { |
101 | return d_ptr->mAttributes.values(); |
102 | } |
103 | |
104 | void Akonadi::AttributeEntity::clearAttributes() |
105 | { |
106 | Q_FOREACH (Attribute *attr, d_ptr->mAttributes) { |
107 | d_ptr->mDeletedAttributes.insert(attr->type()); |
108 | delete attr; |
109 | } |
110 | d_ptr->mAttributes.clear(); |
111 | } |
112 | |
113 | Attribute *AttributeEntity::attribute(const QByteArray &type) const |
114 | { |
115 | if (d_ptr->mAttributes.contains(type)) { |
116 | return d_ptr->mAttributes.value(type); |
117 | } |
118 | return 0; |
119 | } |
120 | |
121 | QSet<QByteArray> &AttributeEntity::removedAttributes() const |
122 | { |
123 | return d_ptr->mDeletedAttributes; |
124 | } |
125 | |