Messagelib

messageitemsetmanager.cpp
1/******************************************************************************
2 *
3 * SPDX-FileCopyrightText: 2008 Szymon Tomasz Stefanek <pragma@kvirc.net>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 *
7 *******************************************************************************/
8
9#include "core/messageitemsetmanager.h"
10#include "core/messageitem.h"
11
12using namespace MessageList::Core;
13
14MessageItemSetManager::MessageItemSetManager()
15{
17}
18
19MessageItemSetManager::~MessageItemSetManager()
20{
21 clearAllSets();
22 delete mSets;
23}
24
25void MessageItemSetManager::clearAllSets()
26{
27 for (auto it = mSets->cbegin(), end = mSets->cend(); it != end; ++it) {
28 delete (*it);
29 }
30 mSets->clear();
31}
32
33int MessageItemSetManager::setCount() const
34{
35 return mSets->count();
36}
37
38void 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
48QList<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
58void MessageItemSetManager::removeMessageItemFromAllSets(MessageItem *mi)
59{
60 QList<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
74MessageItemSetReference 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
96bool 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}
The MessageItem class.
Definition messageitem.h:35
The implementation independent part of the MessageList library.
Definition aggregation.h:22
void append(QList< T > &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:33:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.