Messagelib

messageitemsetmanager.cpp
1 /******************************************************************************
2  *
3  * SPDX-FileCopyrightText: 2008 Szymon Tomasz Stefanek <[email protected]>
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  *
7  *******************************************************************************/
8 
9 #include "core/messageitemsetmanager.h"
10 #include "core/messageitem.h"
11 
12 using namespace MessageList::Core;
13 
14 MessageItemSetManager::MessageItemSetManager()
15 {
17 }
18 
19 MessageItemSetManager::~MessageItemSetManager()
20 {
21  clearAllSets();
22  delete mSets;
23 }
24 
25 void MessageItemSetManager::clearAllSets()
26 {
27  for (auto it = mSets->cbegin(), end = mSets->cend(); it != end; ++it) {
28  delete (*it);
29  }
30  mSets->clear();
31 }
32 
33 int MessageItemSetManager::setCount() const
34 {
35  return mSets->count();
36 }
37 
38 void MessageItemSetManager::removeSet(MessageItemSetReference ref)
39 {
40  auto set = mSets->value(static_cast<ulong>(ref), nullptr);
41  if (!set) {
42  return;
43  }
44  mSets->remove(static_cast<ulong>(ref));
45  delete set;
46 }
47 
48 QList<MessageItem *> MessageItemSetManager::messageItems(MessageItemSetReference ref)
49 {
50  auto set = mSets->value(static_cast<ulong>(ref), nullptr);
51  if (!set) {
52  return {};
53  }
54 
55  return set->values();
56 }
57 
58 void MessageItemSetManager::removeMessageItemFromAllSets(MessageItem *mi)
59 {
60  QVector<MessageItemSetReference> setsToBeKilled;
61 
62  for (auto it = mSets->cbegin(), end = mSets->cend(); it != end; ++it) {
63  (*it)->remove(mi);
64  if ((*it)->isEmpty()) {
65  setsToBeKilled.append(it.key());
66  }
67  }
68 
69  for (const auto set : setsToBeKilled) {
70  removeSet(set);
71  }
72 }
73 
74 MessageItemSetReference MessageItemSetManager::createSet()
75 {
76  static unsigned long int uNextId = 0;
77  uNextId++;
78 
79  auto set = mSets->value(uNextId, nullptr);
80 
81  // Here we assume that having 2^32 sets is impossible
82  // (that would be _at_least_ 2^32 * 8 bytes of allocated memory which is
83  // quite a lot...)
84 
85  while (set) {
86  uNextId++;
87  set = mSets->value(uNextId, nullptr);
88  }
89 
91  mSets->insert(uNextId, set);
92 
93  return static_cast<MessageItemSetReference>(uNextId);
94 }
95 
96 bool MessageItemSetManager::addMessageItem(MessageItemSetReference ref, MessageItem *mi)
97 {
98  auto set = mSets->value(static_cast<ulong>(ref), nullptr);
99  if (!set) {
100  return false;
101  }
102 
103  set->insert(mi, mi);
104 
105  return true;
106 }
void append(const T &value)
The MessageItem class.
Definition: messageitem.h:34
The implementation independent part of the MessageList library.
Definition: aggregation.h:21
void remove(int i)
const QList< QKeySequence > & end()
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Nov 30 2021 23:05:47 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.