File indexing completed on 2024-05-12 05:17:14

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