File indexing completed on 2025-03-09 04:54:13

0001 /*
0002   SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
0003   SPDX-FileCopyrightText: 2009 Andras Mantia <andras@kdab.net>
0004   SPDX-FileCopyrightText: 2010 Leo Franchi <lfranchi@kde.org>
0005 
0006   SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "nodehelper.h"
0010 
0011 #include <KMime/Content>
0012 #include <KMime/KMimeMessage>
0013 
0014 KMime::Content *MessageCore::NodeHelper::nextSibling(const KMime::Content *node)
0015 {
0016     if (!node) {
0017         return nullptr;
0018     }
0019 
0020     KMime::Content *next = nullptr;
0021     KMime::Content *parent = node->parent();
0022     if (parent) {
0023         const auto contents = parent->contents();
0024         const int index = contents.indexOf(const_cast<KMime::Content *>(node)) + 1;
0025         if (index < contents.size()) { // next on the same level
0026             next = contents.at(index);
0027         }
0028     }
0029 
0030     return next;
0031 }
0032 
0033 KMime::Content *MessageCore::NodeHelper::next(KMime::Content *node, bool allowChildren)
0034 {
0035     if (allowChildren) {
0036         if (KMime::Content *child = firstChild(node)) {
0037             return child;
0038         }
0039     }
0040 
0041     if (KMime::Content *sibling = nextSibling(node)) {
0042         return sibling;
0043     }
0044 
0045     for (KMime::Content *parent = node->parent(); parent; parent = parent->parent()) {
0046         if (KMime::Content *sibling = nextSibling(parent)) {
0047             return sibling;
0048         }
0049     }
0050 
0051     return nullptr;
0052 }
0053 
0054 KMime::Content *MessageCore::NodeHelper::firstChild(const KMime::Content *node)
0055 {
0056     if (!node) {
0057         return nullptr;
0058     }
0059 
0060     KMime::Content *child = nullptr;
0061     if (!node->contents().isEmpty()) {
0062         child = node->contents().at(0);
0063     }
0064 
0065     return child;
0066 }