Akonadi

attributestorage.cpp
1 /*
2  SPDX-FileCopyrightText: 2019 David Faure <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "attributestorage_p.h"
8 
9 using namespace Akonadi;
10 
11 AttributeStorage::AttributeStorage()
12 {
13 }
14 
15 AttributeStorage::AttributeStorage(const AttributeStorage &other)
16  : mModifiedAttributes(other.mModifiedAttributes)
17  , mDeletedAttributes(other.mDeletedAttributes)
18 {
19  for (Attribute *attr : std::as_const(other.mAttributes)) {
20  mAttributes.insert(attr->type(), attr->clone());
21  }
22 }
23 
24 AttributeStorage &AttributeStorage::operator=(const AttributeStorage &other)
25 {
26  AttributeStorage copy(other);
27  swap(copy);
28  return *this;
29 }
30 
31 void AttributeStorage::swap(AttributeStorage &other) noexcept
32 {
33  using std::swap;
34  swap(other.mAttributes, mAttributes);
35  swap(other.mModifiedAttributes, mModifiedAttributes);
36  swap(other.mDeletedAttributes, mDeletedAttributes);
37 }
38 
39 AttributeStorage::~AttributeStorage()
40 {
41  qDeleteAll(mAttributes);
42 }
43 
44 void AttributeStorage::addAttribute(Attribute *attr)
45 {
46  Q_ASSERT(attr);
47  const QByteArray type = attr->type();
48  Attribute *existing = mAttributes.value(type);
49  if (existing) {
50  if (attr == existing) {
51  return;
52  }
53  mAttributes.remove(type);
54  delete existing;
55  }
56  mAttributes.insert(type, attr);
57  markAttributeModified(type);
58 }
59 
60 void AttributeStorage::removeAttribute(const QByteArray &type)
61 {
62  mModifiedAttributes.erase(type);
63  mDeletedAttributes.insert(type);
64  delete mAttributes.take(type);
65 }
66 
67 bool AttributeStorage::hasAttribute(const QByteArray &type) const
68 {
69  return mAttributes.contains(type);
70 }
71 
72 Attribute::List AttributeStorage::attributes() const
73 {
74  return mAttributes.values();
75 }
76 
77 void AttributeStorage::clearAttributes()
78 {
79  for (Attribute *attr : std::as_const(mAttributes)) {
80  mDeletedAttributes.insert(attr->type());
81  delete attr;
82  }
83  mAttributes.clear();
84  mModifiedAttributes.clear();
85 }
86 
87 const Attribute *AttributeStorage::attribute(const QByteArray &type) const
88 {
89  return mAttributes.value(type);
90 }
91 
92 Attribute *AttributeStorage::attribute(const QByteArray &type)
93 {
94  Attribute *attr = mAttributes.value(type);
95  if (attr) {
96  markAttributeModified(type);
97  }
98  return attr;
99 }
100 
101 void AttributeStorage::markAttributeModified(const QByteArray &type)
102 {
103  if (mAttributes.contains(type)) {
104  mDeletedAttributes.remove(type);
105  mModifiedAttributes.insert(type);
106  }
107 }
108 
109 void AttributeStorage::resetChangeLog()
110 {
111  mModifiedAttributes.clear();
112  mDeletedAttributes.clear();
113 }
114 
115 QSet<QByteArray> AttributeStorage::deletedAttributes() const
116 {
117  return mDeletedAttributes;
118 }
119 
120 bool AttributeStorage::hasModifiedAttributes() const
121 {
122  return !mModifiedAttributes.empty();
123 }
124 
125 std::vector<Attribute *> AttributeStorage::modifiedAttributes() const
126 {
127  std::vector<Attribute *> ret;
128  ret.reserve(mModifiedAttributes.size());
129  for (const auto &type : mModifiedAttributes) {
130  Attribute *attr = mAttributes.value(type);
131  Q_ASSERT(attr);
132  ret.push_back(attr);
133  }
134  return ret;
135 }
virtual QByteArray type() const =0
Returns the type of the attribute.
Provides interface for custom attributes for Entity.
Definition: attribute.h:124
Definition: item.h:32
void insert(int i, const T &value)
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
bool empty() const const
const QList< QKeySequence > & copy()
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jun 25 2022 06:00:31 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.