Akonadi

attributestorage.cpp
1 /*
2  Copyright (c) 2019 David Faure <[email protected]>
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 const Attribute *AttributeStorage::attribute(const QByteArray &type) const
101 {
102  return mAttributes.value(type);
103 }
104 
105 Attribute *AttributeStorage::attribute(const QByteArray &type)
106 {
107  Attribute *attr = mAttributes.value(type);
108  if (attr)
109  markAttributeModified(type);
110  return attr;
111 }
112 
113 void AttributeStorage::markAttributeModified(const QByteArray &type)
114 {
115  if (mAttributes.contains(type)) {
116  mDeletedAttributes.remove(type);
117  mModifiedAttributes.insert(type);
118  }
119 }
120 
121 void AttributeStorage::resetChangeLog()
122 {
123  mModifiedAttributes.clear();
124  mDeletedAttributes.clear();
125 }
126 
127 QSet<QByteArray> AttributeStorage::deletedAttributes() const
128 {
129  return mDeletedAttributes;
130 }
131 
132 bool AttributeStorage::hasModifiedAttributes() const
133 {
134  return !mModifiedAttributes.empty();
135 }
136 
137 std::vector<Attribute *> AttributeStorage::modifiedAttributes() const
138 {
139  std::vector<Attribute *> ret;
140  ret.reserve(mModifiedAttributes.size());
141  for (const auto &type : mModifiedAttributes) {
142  Attribute *attr = mAttributes.value(type);
143  Q_ASSERT(attr);
144  ret.push_back(attr);
145  }
146  return ret;
147 }
Type type(const QString &mimeType)
Provides interface for custom attributes for Entity.
Definition: attribute.h:139
virtual QByteArray type() const =0
Returns the type of the attribute.
KIOCORE_EXPORT CopyJob * copy(const QUrl &src, const QUrl &dest, JobFlags flags=DefaultFlags)
bool empty() const const
void insert(int i, const T &value)
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 23:08:53 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.