File indexing completed on 2024-09-15 04:36:25

0001 /*
0002     SPDX-FileCopyrightText: 2007 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "akonadiprivate_export.h"
0010 
0011 #include <QByteArray>
0012 #include <QDebug>
0013 #include <QList>
0014 #include <QMetaType>
0015 #include <QSharedDataPointer>
0016 
0017 namespace Akonadi
0018 {
0019 namespace Protocol
0020 {
0021 class DataStream;
0022 }
0023 }
0024 
0025 namespace Akonadi
0026 {
0027 class ImapIntervalPrivate;
0028 
0029 /**
0030   Represents a single interval in an ImapSet.
0031   This class is implicitly shared.
0032 */
0033 class AKONADIPRIVATE_EXPORT ImapInterval
0034 {
0035 public:
0036     /**
0037      * Describes the ids stored in the interval.
0038      */
0039     using Id = qint64;
0040 
0041     /**
0042       A list of ImapInterval objects.
0043     */
0044     using List = QList<ImapInterval>;
0045 
0046     /**
0047       Constructs an interval that covers all positive numbers.
0048     */
0049     ImapInterval();
0050 
0051     /**
0052       Copy constructor.
0053     */
0054     ImapInterval(const ImapInterval &other);
0055 
0056     /**
0057       Create a new interval.
0058       @param begin The begin of the interval.
0059       @param end Keep default (0) to just set the interval begin
0060     */
0061     explicit ImapInterval(Id begin, Id end = 0);
0062 
0063     /**
0064       Destructor.
0065     */
0066     ~ImapInterval();
0067 
0068     /**
0069       Assignment operator.
0070     */
0071     ImapInterval &operator=(const ImapInterval &other);
0072 
0073     /**
0074       Comparison operator.
0075     */
0076     bool operator==(const ImapInterval &other) const;
0077 
0078     /**
0079       Returns the size of this interval.
0080       Size is only defined for finite intervals.
0081     */
0082     Id size() const;
0083 
0084     /**
0085       Returns true if this interval has a defined begin.
0086     */
0087     bool hasDefinedBegin() const;
0088 
0089     /**
0090       Returns the begin of this interval. The value is the smallest value part of the interval.
0091       Only valid if begin is defined.
0092     */
0093     Id begin() const;
0094 
0095     /**
0096       Returns true if this intercal has been defined.
0097     */
0098     bool hasDefinedEnd() const;
0099 
0100     /**
0101       Returns the end of this interval. This value is the largest value part of the interval.
0102       Only valid if hasDefinedEnd() returned true.
0103     */
0104     Id end() const;
0105 
0106     /**
0107       Sets the begin of the interval.
0108     */
0109     void setBegin(Id value);
0110 
0111     /**
0112       Sets the end of this interval.
0113     */
0114     void setEnd(Id value);
0115 
0116     /**
0117       Converts this set into an IMAP compatible sequence.
0118     */
0119     QByteArray toImapSequence() const;
0120 
0121 private:
0122     QSharedDataPointer<ImapIntervalPrivate> d;
0123 
0124     friend Protocol::DataStream &operator<<(Protocol::DataStream &stream, const Akonadi::ImapInterval &interval);
0125     friend Protocol::DataStream &operator>>(Protocol::DataStream &stream, Akonadi::ImapInterval &interval);
0126 };
0127 
0128 class ImapSetPrivate;
0129 
0130 /**
0131   Represents a set of natural numbers (1->\f$\infty\f$) in a as compact as possible form.
0132   Used to address Akonadi items via the IMAP protocol or in the database.
0133   This class is implicitly shared.
0134 */
0135 class AKONADIPRIVATE_EXPORT ImapSet
0136 {
0137 public:
0138     /**
0139      * Describes the ids stored in the set.
0140      */
0141     using Id = qint64;
0142 
0143     /**
0144       Constructs an empty set.
0145     */
0146     ImapSet();
0147 
0148     ImapSet(qint64 Id); // krazy:exclude=explicit
0149 
0150     ImapSet(const QList<qint64> &ids); // krazy:exclude=explicit
0151 
0152     ImapSet(const ImapInterval &interval); // krazy:exclude=explicit
0153 
0154     /**
0155       Copy constructor.
0156     */
0157     ImapSet(const ImapSet &other);
0158 
0159     /**
0160       Destructor.
0161     */
0162     ~ImapSet();
0163 
0164     /**
0165      * Returns ImapSet representing 1:*
0166      * */
0167     static ImapSet all();
0168 
0169     /**
0170       Assignment operator.
0171     */
0172     ImapSet &operator=(const ImapSet &other);
0173 
0174     bool operator==(const ImapSet &other) const;
0175 
0176     /**
0177       Adds the given list of positive integer numbers to the set.
0178       The list is sorted and split into as large as possible intervals.
0179       No interval merging is performed.
0180       @param values List of positive integer numbers in arbitrary order
0181     */
0182     void add(const QList<Id> &values);
0183 
0184     /**
0185      * @overload
0186      */
0187     void add(const QSet<Id> &values);
0188 
0189     /**
0190       Adds the given ImapInterval to this set.
0191       No interval merging is performed.
0192     */
0193     void add(const ImapInterval &interval);
0194 
0195     /**
0196       Returns a IMAP-compatible QByteArray representation of this set.
0197     */
0198     QByteArray toImapSequenceSet() const;
0199 
0200     /**
0201       Returns the intervals this set consists of.
0202     */
0203     ImapInterval::List intervals() const;
0204 
0205     /**
0206       Returns true if this set doesn't contains any values.
0207     */
0208     bool isEmpty() const;
0209 
0210     /**
0211      * Optimize the imap set to reduce the number of intervals.
0212      */
0213     void optimize();
0214 
0215 private:
0216     QSharedDataPointer<ImapSetPrivate> d;
0217 
0218     friend Protocol::DataStream &operator<<(Protocol::DataStream &stream, const Akonadi::ImapSet &set);
0219     friend Protocol::DataStream &operator>>(Protocol::DataStream &stream, Akonadi::ImapSet &set);
0220 };
0221 
0222 }
0223 
0224 AKONADIPRIVATE_EXPORT QDebug operator<<(QDebug d, const Akonadi::ImapInterval &interval);
0225 AKONADIPRIVATE_EXPORT QDebug operator<<(QDebug d, const Akonadi::ImapSet &set);
0226 
0227 Q_DECLARE_TYPEINFO(Akonadi::ImapInterval, Q_RELOCATABLE_TYPE);
0228 Q_DECLARE_TYPEINFO(Akonadi::ImapSet, Q_RELOCATABLE_TYPE);
0229 
0230 Q_DECLARE_METATYPE(Akonadi::ImapInterval)
0231 Q_DECLARE_METATYPE(Akonadi::ImapInterval::List)
0232 Q_DECLARE_METATYPE(Akonadi::ImapSet)