1/*
2 Copyright (c) 2007 Volker Krause <vkrause@kde.org>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19
20#ifndef AKONADI_IMAPSET_P_H
21#define AKONADI_IMAPSET_P_H
22
23#include "akonadiprotocolinternals_export.h"
24
25#include <QtCore/QByteArray>
26#include <QtCore/QDebug>
27#include <QtCore/QList>
28#include <QtCore/QMetaType>
29#include <QtCore/QSharedDataPointer>
30
31namespace Akonadi {
32
33/**
34 Represents a single interval in an ImapSet.
35 This class is implicitly shared.
36*/
37class AKONADIPROTOCOLINTERNALS_EXPORT ImapInterval
38{
39public:
40 /**
41 * Describes the ids stored in the interval.
42 */
43 typedef qint64 Id;
44
45 /**
46 A list of ImapInterval objects.
47 */
48 typedef QList<ImapInterval> List;
49
50 /**
51 Constructs an interval that covers all positive numbers.
52 */
53 ImapInterval();
54
55 /**
56 Copy constructor.
57 */
58 ImapInterval(const ImapInterval &other);
59
60 /**
61 Create a new interval.
62 @param begin The begin of the interval.
63 @param end Keep default (0) to just set the interval begin
64 */
65 explicit ImapInterval(Id begin, Id end = 0);
66
67 /**
68 Destructor.
69 */
70 ~ImapInterval();
71
72 /**
73 Assignment operator.
74 */
75 ImapInterval &operator=(const ImapInterval &other);
76
77 /**
78 Comparison operator.
79 */
80 bool operator==(const ImapInterval &other) const;
81
82 /**
83 Returns the size of this interval.
84 Size is only defined for finite intervals.
85 */
86 Id size() const;
87
88 /**
89 Returns true if this interval has a defined begin.
90 */
91 bool hasDefinedBegin() const;
92
93 /**
94 Returns the begin of this interval. The value is the smallest value part of the interval.
95 Only valid if begin is defined.
96 */
97 Id begin() const;
98
99 /**
100 Returns true if this intercal has been defined.
101 */
102 bool hasDefinedEnd() const;
103
104 /**
105 Returns the end of this interval. This value is the largest value part of the interval.
106 Only valid if hasDefinedEnd() returned true.
107 */
108 Id end() const;
109
110 /**
111 Sets the begin of the interval.
112 */
113 void setBegin(Id value);
114
115 /**
116 Sets the end of this interval.
117 */
118 void setEnd(Id value);
119
120 /**
121 Converts this set into an IMAP compatible sequence.
122 */
123 QByteArray toImapSequence() const;
124
125private:
126 class Private;
127 QSharedDataPointer<Private> d;
128};
129
130/**
131 Represents a set of natural numbers (1->\f$\infty\f$) in a as compact as possible form.
132 Used to address Akonadi items via the IMAP protocol or in the database.
133 This class is implicitly shared.
134*/
135class AKONADIPROTOCOLINTERNALS_EXPORT ImapSet
136{
137public:
138 /**
139 * Describes the ids stored in the set.
140 */
141 typedef qint64 Id;
142
143 /**
144 Constructs an empty set.
145 */
146 ImapSet();
147
148 /**
149 Copy constructor.
150 */
151 ImapSet(const ImapSet &other);
152
153 /**
154 Destructor.
155 */
156 ~ImapSet();
157
158 /**
159 Assignment operator.
160 */
161 ImapSet &operator=(const ImapSet &other);
162
163 /**
164 Adds the given list of positive integer numbers to the set.
165 The list is sorted and split into as large as possible intervals.
166 No interval merging is performed.
167 @param values List of positive integer numbers in arbitrary order
168 */
169 void add(const QVector<Id> &values);
170
171 /**
172 @overload
173 @deprecated Use the QVector version instead.
174 */
175 void add(const QList<Id> &values);
176
177 /**
178 * @overload
179 */
180 void add(const QSet<Id> &values);
181
182 /**
183 Adds the given ImapInterval to this set.
184 No interval merging is performed.
185 */
186 void add(const ImapInterval &interval);
187
188 /**
189 Returns a IMAP-compatible QByteArray representation of this set.
190 */
191 QByteArray toImapSequenceSet() const;
192
193 /**
194 Returns the intervals this set consists of.
195 */
196 ImapInterval::List intervals() const;
197
198 /**
199 Returns true if this set doesn't contains any values.
200 */
201 bool isEmpty() const;
202
203private:
204 class Private;
205 QSharedDataPointer<Private> d;
206};
207
208}
209
210AKONADIPROTOCOLINTERNALS_EXPORT QDebug &operator<<(QDebug &d, const Akonadi::ImapInterval &interval);
211AKONADIPROTOCOLINTERNALS_EXPORT QDebug operator<<(QDebug d, const Akonadi::ImapSet &set);
212
213Q_DECLARE_TYPEINFO(Akonadi::ImapInterval, Q_MOVABLE_TYPE);
214Q_DECLARE_TYPEINFO(Akonadi::ImapSet, Q_MOVABLE_TYPE);
215
216Q_DECLARE_METATYPE(Akonadi::ImapInterval)
217Q_DECLARE_METATYPE(Akonadi::ImapInterval::List)
218Q_DECLARE_METATYPE(Akonadi::ImapSet)
219
220#endif
221