File indexing completed on 2022-11-29 19:53:37

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 <QSharedData>
0021 #include <QStringList>
0022 #include <QVector>
0023 
0024 using namespace KMime;
0025 
0026 class ContentIndex::Private : public QSharedData
0027 {
0028 public:
0029   Private() = default;
0030   Private(const Private &other) : QSharedData(other) { index = other.index; }
0031 
0032   QVector<unsigned int> index;
0033 };
0034 
0035 KMime::ContentIndex::ContentIndex() : d(new Private)
0036 {
0037 }
0038 
0039 KMime::ContentIndex::ContentIndex(const QString &index) : d(new Private)
0040 {
0041     const QStringList l = index.split(QLatin1Char('.'));
0042     for (const QString &s : l) {
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 
0055 ContentIndex::~ContentIndex() = default;
0056 
0057 bool KMime::ContentIndex::isValid() const
0058 {
0059     return !d->index.isEmpty();
0060 }
0061 
0062 unsigned int KMime::ContentIndex::pop()
0063 {
0064     return d->index.takeFirst();
0065 }
0066 
0067 void KMime::ContentIndex::push(unsigned int index)
0068 {
0069     d->index.prepend(index);
0070 }
0071 
0072 unsigned int ContentIndex::up()
0073 {
0074     return d->index.takeLast();
0075 }
0076 
0077 QString KMime::ContentIndex::toString() const
0078 {
0079     QStringList l;
0080     l.reserve(d->index.count());
0081     for (unsigned int i : std::as_const(d->index)) {
0082         l.append(QString::number(i));
0083     }
0084     return l.join(QLatin1Char('.'));
0085 }
0086 
0087 bool KMime::ContentIndex::operator ==(const ContentIndex &index) const
0088 {
0089     return d->index == index.d->index;
0090 }
0091 
0092 bool KMime::ContentIndex::operator !=(const ContentIndex &index) const
0093 {
0094     return d->index != index.d->index;
0095 }
0096 
0097 ContentIndex &ContentIndex::operator =(const ContentIndex &other)
0098 {
0099     if (this != &other) {
0100         d = other.d;
0101     }
0102     return *this;
0103 }
0104 
0105 uint KMime::qHash(const KMime::ContentIndex &index)
0106 {
0107     return qHash(index.toString());
0108 }