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

0001 /*
0002     Copyright (c) 2007 Volker Krause <vkrause@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to the
0016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017     02110-1301, USA.
0018 */
0019 
0020 #ifndef KIMAP2_IMAPSET_H
0021 #define KIMAP2_IMAPSET_H
0022 
0023 #include "kimap2_export.h"
0024 
0025 #include <QtCore/QByteArray>
0026 #include <QtCore/QDebug>
0027 #include <QtCore/QList>
0028 #include <QtCore/QMetaType>
0029 #include <QtCore/QSharedDataPointer>
0030 
0031 namespace KIMAP2
0032 {
0033 
0034 /**
0035   Represents a single interval in an ImapSet.
0036   This class is implicitly shared.
0037 */
0038 class KIMAP2_EXPORT ImapInterval
0039 {
0040 public:
0041     /**
0042      * Describes the ids stored in the interval.
0043      */
0044     typedef qint64 Id;
0045 
0046     /**
0047       A list of ImapInterval objects.
0048     */
0049     typedef QList<ImapInterval> List;
0050 
0051     /**
0052       Constructs an interval that covers all positive numbers.
0053     */
0054     ImapInterval();
0055 
0056     /**
0057       Copy constructor.
0058     */
0059     ImapInterval(const ImapInterval &other);
0060 
0061     /**
0062       Create a new interval.
0063       @param begin The begin of the interval.
0064       @param end Keep default (0) to just set the interval begin
0065     */
0066     explicit ImapInterval(Id begin, Id end = 0);
0067 
0068     /**
0069       Destructor.
0070     */
0071     ~ImapInterval();
0072 
0073     /**
0074       Assignment operator.
0075     */
0076     ImapInterval &operator=(const ImapInterval &other);
0077 
0078     /**
0079       Comparison operator.
0080     */
0081     bool operator==(const ImapInterval &other) const;
0082 
0083     /**
0084       Returns the size of this interval.
0085       Size is only defined for finite intervals.
0086     */
0087     Id size() const;
0088 
0089     /**
0090       Returns true if this interval has a defined begin.
0091     */
0092     bool hasDefinedBegin() const;
0093 
0094     /**
0095       Returns the begin of this interval. The value is the smallest value part of the interval.
0096       Only valid if begin is defined.
0097     */
0098     Id begin() const;
0099 
0100     /**
0101       Returns true if this intercal has been defined.
0102     */
0103     bool hasDefinedEnd() const;
0104 
0105     /**
0106       Returns the end of this interval. This value is the largest value part of the interval.
0107       Only valid if hasDefinedEnd() returned true.
0108     */
0109     Id end() const;
0110 
0111     /**
0112       Sets the begin of the interval.
0113     */
0114     void setBegin(Id value);
0115 
0116     /**
0117       Sets the end of this interval.
0118     */
0119     void setEnd(Id value);
0120 
0121     /**
0122       Converts this set into an IMAP compatible sequence.
0123     */
0124     QByteArray toImapSequence() const;
0125 
0126     /**
0127       Return the interval corresponding to the given IMAP-compatible QByteArray representation
0128     */
0129     static ImapInterval fromImapSequence(const QByteArray &sequence);
0130 
0131 private:
0132     class Private;
0133     QSharedDataPointer<Private> d;
0134 };
0135 
0136 /**
0137   Represents a set of natural numbers (1->∞) in a as compact as possible form.
0138   Used to address Akonadi items via the IMAP protocol or in the database.
0139   This class is implicitly shared.
0140 */
0141 class KIMAP2_EXPORT ImapSet
0142 {
0143 public:
0144     /**
0145      * Describes the ids stored in the set.
0146      */
0147     typedef qint64 Id;
0148 
0149     /**
0150       Constructs an empty set.
0151     */
0152     ImapSet();
0153 
0154     /**
0155       Constructs a set containing a single interval.
0156     */
0157     ImapSet(Id begin, Id end);
0158 
0159     /**
0160       Constructs a set containing a single value.
0161     */
0162     explicit ImapSet(Id value);
0163 
0164     /**
0165       Copy constructor.
0166     */
0167     ImapSet(const ImapSet &other);
0168 
0169     /**
0170       Destructor.
0171     */
0172     ~ImapSet();
0173 
0174     /**
0175       Assignment operator.
0176     */
0177     ImapSet &operator=(const ImapSet &other);
0178 
0179     /**
0180       Comparison operator.
0181     */
0182     bool operator==(const ImapSet &other) const;
0183 
0184     /**
0185       Adds a single positive integer numbers to the set.
0186       The list is sorted and split into as large as possible intervals.
0187       No interval merging is performed.
0188       @param value A positive integer number
0189     */
0190     void add(Id value);
0191 
0192     /**
0193       Adds the given list of positive integer numbers to the set.
0194       The list is sorted and split into as large as possible intervals.
0195       No interval merging is performed.
0196       @param values List of positive integer numbers in arbitrary order
0197     */
0198     void add(const QVector<Id> &values);
0199 
0200     /**
0201       Adds the given ImapInterval to this set.
0202       No interval merging is performed.
0203       @param interval the interval to add
0204     */
0205     void add(const ImapInterval &interval);
0206 
0207     /**
0208       Returns a IMAP-compatible QByteArray representation of this set.
0209     */
0210     QByteArray toImapSequenceSet() const;
0211 
0212     /**
0213       Return the set corresponding to the given IMAP-compatible QByteArray representation
0214     */
0215     static ImapSet fromImapSequenceSet(const QByteArray &sequence);
0216 
0217     /**
0218       Returns the intervals this set consists of.
0219     */
0220     ImapInterval::List intervals() const;
0221 
0222     /**
0223       Returns true if this set doesn't contains any values.
0224     */
0225     bool isEmpty() const;
0226 
0227     /**
0228      * Optimizes the ImapSet by sorting and merging overlapping intervals.
0229      *
0230      * Normally you shouldn't need to call this method. KIMAP will make sure
0231      * to opimize the ImapSet before serializing it to string and sending it
0232      * to the IMAP server.
0233      */
0234     void optimize();
0235 
0236 private:
0237     class Private;
0238     QSharedDataPointer<Private> d;
0239 };
0240 
0241 }
0242 
0243 KIMAP2_EXPORT QDebug &operator<<(QDebug &d, const KIMAP2::ImapInterval &interval);
0244 KIMAP2_EXPORT QDebug &operator<<(QDebug &d, const KIMAP2::ImapSet &set);
0245 
0246 Q_DECLARE_METATYPE(KIMAP2::ImapInterval)
0247 Q_DECLARE_METATYPE(KIMAP2::ImapInterval::List)
0248 Q_DECLARE_METATYPE(KIMAP2::ImapSet)
0249 
0250 #endif