File indexing completed on 2024-04-14 05:12:20

0001 /*
0002     SPDX-FileCopyrightText: 2006-2007 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 /**
0007   @file
0008   This file is part of the API for handling @ref MIME data and
0009   defines the ContentIndex class.
0010 
0011   @brief
0012   Defines the ContentIndex class.
0013 
0014   @authors Volker Krause \<vkrause@kde.org\>
0015 */
0016 
0017 #include "kmime_contentindex.h"
0018 
0019 #include <QHash>
0020 #include <QList>
0021 #include <QSharedData>
0022 #include <QStringList>
0023 #include <QStringTokenizer>
0024 
0025 using namespace KMime;
0026 
0027 class ContentIndex::Private : public QSharedData
0028 {
0029 public:
0030   Private() = default;
0031   Private(const Private &other) : QSharedData(other) { index = other.index; }
0032 
0033   QList<unsigned int> index;
0034 };
0035 
0036 KMime::ContentIndex::ContentIndex() : d(new Private)
0037 {
0038 }
0039 
0040 KMime::ContentIndex::ContentIndex(QStringView index) : d(new Private)
0041 {
0042     for (auto s : QStringTokenizer{index, QLatin1Char('.')}) {
0043         bool ok;
0044         unsigned int i = s.toUInt(&ok);
0045         if (!ok) {
0046             d->index.clear();
0047             break;
0048         }
0049         d->index.append(i);
0050     }
0051 }
0052 
0053 ContentIndex::ContentIndex(const ContentIndex &other) = default;
0054 ContentIndex::ContentIndex(ContentIndex &&) noexcept = default;
0055 
0056 ContentIndex::~ContentIndex() = default;
0057 
0058 bool KMime::ContentIndex::isValid() const
0059 {
0060     return !d->index.isEmpty();
0061 }
0062 
0063 unsigned int KMime::ContentIndex::pop()
0064 {
0065     return d->index.takeFirst();
0066 }
0067 
0068 void KMime::ContentIndex::push(unsigned int index)
0069 {
0070     d->index.prepend(index);
0071 }
0072 
0073 unsigned int ContentIndex::up()
0074 {
0075     return d->index.takeLast();
0076 }
0077 
0078 QString KMime::ContentIndex::toString() const
0079 {
0080     QStringList l;
0081     l.reserve(d->index.count());
0082     for (unsigned int i : std::as_const(d->index)) {
0083         l.append(QString::number(i));
0084     }
0085     return l.join(QLatin1Char('.'));
0086 }
0087 
0088 bool KMime::ContentIndex::operator ==(const ContentIndex &index) const
0089 {
0090     return d->index == index.d->index;
0091 }
0092 
0093 bool KMime::ContentIndex::operator !=(const ContentIndex &index) const
0094 {
0095     return d->index != index.d->index;
0096 }
0097 
0098 ContentIndex &ContentIndex::operator =(const ContentIndex &other) = default;
0099 ContentIndex &ContentIndex::operator =(ContentIndex &&) noexcept = default;
0100 
0101 size_t KMime::qHash(const KMime::ContentIndex &index, size_t seed) noexcept
0102 {
0103     return qHash(index.toString(), seed);
0104 }