File indexing completed on 2024-12-15 04:54:38
0001 /****************************************************************************** 0002 * 0003 * SPDX-FileCopyrightText: 2008 Szymon Tomasz Stefanek <pragma@kvirc.net> 0004 * 0005 * SPDX-License-Identifier: GPL-2.0-or-later 0006 * 0007 *******************************************************************************/ 0008 0009 #include "core/messageitemsetmanager.h" 0010 #include "core/messageitem.h" 0011 0012 using namespace MessageList::Core; 0013 0014 MessageItemSetManager::MessageItemSetManager() 0015 { 0016 mSets = new QHash<MessageItemSetReference, QHash<MessageItem *, MessageItem *> *>(); 0017 } 0018 0019 MessageItemSetManager::~MessageItemSetManager() 0020 { 0021 clearAllSets(); 0022 delete mSets; 0023 } 0024 0025 void MessageItemSetManager::clearAllSets() 0026 { 0027 for (auto it = mSets->cbegin(), end = mSets->cend(); it != end; ++it) { 0028 delete (*it); 0029 } 0030 mSets->clear(); 0031 } 0032 0033 int MessageItemSetManager::setCount() const 0034 { 0035 return mSets->count(); 0036 } 0037 0038 void MessageItemSetManager::removeSet(MessageItemSetReference ref) 0039 { 0040 auto set = mSets->value(static_cast<ulong>(ref), nullptr); 0041 if (!set) { 0042 return; 0043 } 0044 mSets->remove(static_cast<ulong>(ref)); 0045 delete set; 0046 } 0047 0048 QList<MessageItem *> MessageItemSetManager::messageItems(MessageItemSetReference ref) 0049 { 0050 auto set = mSets->value(static_cast<ulong>(ref), nullptr); 0051 if (!set) { 0052 return {}; 0053 } 0054 0055 return set->values(); 0056 } 0057 0058 void MessageItemSetManager::removeMessageItemFromAllSets(MessageItem *mi) 0059 { 0060 QList<MessageItemSetReference> setsToBeKilled; 0061 0062 for (auto it = mSets->cbegin(), end = mSets->cend(); it != end; ++it) { 0063 (*it)->remove(mi); 0064 if ((*it)->isEmpty()) { 0065 setsToBeKilled.append(it.key()); 0066 } 0067 } 0068 0069 for (const auto set : setsToBeKilled) { 0070 removeSet(set); 0071 } 0072 } 0073 0074 MessageItemSetReference MessageItemSetManager::createSet() 0075 { 0076 static unsigned long int uNextId = 0; 0077 uNextId++; 0078 0079 auto set = mSets->value(uNextId, nullptr); 0080 0081 // Here we assume that having 2^32 sets is impossible 0082 // (that would be _at_least_ 2^32 * 8 bytes of allocated memory which is 0083 // quite a lot...) 0084 0085 while (set) { 0086 uNextId++; 0087 set = mSets->value(uNextId, nullptr); 0088 } 0089 0090 set = new QHash<MessageItem *, MessageItem *>(); 0091 mSets->insert(uNextId, set); 0092 0093 return static_cast<MessageItemSetReference>(uNextId); 0094 } 0095 0096 bool MessageItemSetManager::addMessageItem(MessageItemSetReference ref, MessageItem *mi) 0097 { 0098 auto set = mSets->value(static_cast<ulong>(ref), nullptr); 0099 if (!set) { 0100 return false; 0101 } 0102 0103 set->insert(mi, mi); 0104 0105 return true; 0106 }