Akonadi

imapset.cpp
1 /*
2  Copyright (c) 2007 Volker Krause <[email protected]>
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 #include "imapset_p.h"
21 #include "datastream_p_p.h"
22 
23 #include <QSharedData>
24 
25 #include <limits>
26 
27 namespace Akonadi
28 {
29 
30 class ImapInterval::Private : public QSharedData
31 {
32 public:
33  Private()
34  : QSharedData()
35  , begin(0)
36  , end(0)
37  {
38  }
39 
40  Private(const Private &other)
41  : QSharedData(other)
42  {
43  begin = other.begin;
44  end = other.end;
45  }
46 
47  Id begin;
48  Id end;
49 };
50 
51 class ImapSet::Private : public QSharedData
52 {
53 public:
54  Private()
55  : QSharedData()
56  {
57  }
58 
59  Private(const Private &other)
60  : QSharedData(other)
61  {
62  intervals = other.intervals;
63  }
64 
65  template<typename T>
66  void add(const T &values)
67  {
68  T vals = values;
69  std::sort(vals.begin(), vals.end());
70  for (int i = 0; i < vals.count(); ++i) {
71  const int begin = vals[i];
72  Q_ASSERT(begin >= 0);
73  if (i == vals.count() - 1) {
74  intervals << ImapInterval(begin, begin);
75  break;
76  }
77  do {
78  ++i;
79  Q_ASSERT(vals[i] >= 0);
80  if (vals[i] != (vals[i - 1] + 1)) {
81  --i;
82  break;
83  }
84  } while (i < vals.count() - 1);
85  intervals << ImapInterval(begin, vals[i]);
86  }
87  }
88 
89  ImapInterval::List intervals;
90 };
91 
92 ImapInterval::ImapInterval()
93  : d(new Private)
94 {
95 }
96 
97 ImapInterval::ImapInterval(const ImapInterval &other)
98  : d(other.d)
99 {
100 }
101 
102 ImapInterval::ImapInterval(Id begin, Id end)
103  : d(new Private)
104 {
105  d->begin = begin;
106  d->end = end;
107 }
108 
109 ImapInterval::~ImapInterval()
110 {
111 }
112 
113 ImapInterval &ImapInterval::operator=(const ImapInterval &other)
114 {
115  if (this != &other) {
116  d = other.d;
117  }
118 
119  return *this;
120 }
121 
122 bool ImapInterval::operator==(const ImapInterval &other) const
123 {
124  return (d->begin == other.d->begin && d->end == other.d->end);
125 }
126 
127 ImapInterval::Id ImapInterval::size() const
128 {
129  if (!d->begin && !d->end) {
130  return 0;
131  }
132 
133  return (d->end - d->begin + 1);
134 }
135 
136 bool ImapInterval::hasDefinedBegin() const
137 {
138  return (d->begin != 0);
139 }
140 
141 ImapInterval::Id ImapInterval::begin() const
142 {
143  return d->begin;
144 }
145 
146 bool ImapInterval::hasDefinedEnd() const
147 {
148  return (d->end != 0);
149 }
150 
151 ImapInterval::Id ImapInterval::end() const
152 {
153  if (hasDefinedEnd()) {
154  return d->end;
155  }
156 
157  return std::numeric_limits<Id>::max();
158 }
159 
160 void ImapInterval::setBegin(Id value)
161 {
162  Q_ASSERT(value >= 0);
163  Q_ASSERT(value <= d->end || !hasDefinedEnd());
164  d->begin = value;
165 }
166 
167 void ImapInterval::setEnd(Id value)
168 {
169  Q_ASSERT(value >= 0);
170  Q_ASSERT(value >= d->begin || !hasDefinedBegin());
171  d->end = value;
172 }
173 
174 QByteArray Akonadi::ImapInterval::toImapSequence() const
175 {
176  if (size() == 0) {
177  return QByteArray();
178  }
179 
180  if (size() == 1) {
181  return QByteArray::number(d->begin);
182  }
183 
184  QByteArray rv = QByteArray::number(d->begin) + ':';
185 
186  if (hasDefinedEnd()) {
187  rv += QByteArray::number(d->end);
188  } else {
189  rv += '*';
190  }
191 
192  return rv;
193 }
194 
195 ImapSet::ImapSet()
196  : d(new Private)
197 {
198 }
199 
200 ImapSet::ImapSet(Id id)
201  : d(new Private)
202 {
203  add(QVector<Id>() << id);
204 }
205 
206 ImapSet::ImapSet(const QVector<qint64> &ids)
207  : d(new Private)
208 {
209  add(ids);
210 }
211 
212 ImapSet::ImapSet(const QList<qint64> &ids)
213  : d(new Private)
214 {
215  add(ids);
216 }
217 
218 ImapSet::ImapSet(const ImapInterval &interval)
219  : d(new Private)
220 {
221  add(interval);
222 }
223 
224 ImapSet::ImapSet(const ImapSet &other)
225  : d(other.d)
226 {
227 }
228 
229 ImapSet::~ImapSet()
230 {
231 }
232 
233 ImapSet ImapSet::all()
234 {
235  ImapSet set;
236  set.add(ImapInterval(1, 0));
237  return set;
238 }
239 
240 ImapSet &ImapSet::operator=(const ImapSet &other)
241 {
242  if (this != &other) {
243  d = other.d;
244  }
245 
246  return *this;
247 }
248 
249 bool ImapSet::operator==(const ImapSet &other) const
250 {
251  return d->intervals == other.d->intervals;
252 }
253 
254 void ImapSet::add(const QVector<Id> &values)
255 {
256  d->add(values);
257 }
258 
259 void ImapSet::add(const QList<Id> &values)
260 {
261  d->add(values);
262 }
263 
264 void ImapSet::add(const QSet<Id> &values)
265 {
266  QVector<Id> v;
267  v.reserve(values.size());
268  for (QSet<Id>::ConstIterator iter = values.constBegin(); iter != values.constEnd(); ++iter) {
269  v.push_back(*iter);
270  }
271 
272  add(v);
273 }
274 
275 void ImapSet::add(const ImapInterval &interval)
276 {
277  d->intervals << interval;
278 }
279 
280 QByteArray ImapSet::toImapSequenceSet() const
281 {
282  QByteArray rv;
283  for (auto iter = d->intervals.cbegin(), end = d->intervals.cend(); iter != end; ++iter) {
284  if (iter != d->intervals.cbegin()) {
285  rv += ',';
286  }
287  rv += iter->toImapSequence();
288  }
289 
290  return rv;
291 }
292 
293 ImapInterval::List ImapSet::intervals() const
294 {
295  return d->intervals;
296 }
297 
298 bool ImapSet::isEmpty() const
299 {
300  return d->intervals.isEmpty() || (d->intervals.size() == 1 && d->intervals.at(0).size() == 0);
301 }
302 
303 Protocol::DataStream &operator<<(Protocol::DataStream &stream, const Akonadi::ImapInterval &interval)
304 {
305  return stream << interval.d->begin
306  << interval.d->end;
307 }
308 
309 Protocol::DataStream &operator>>(Protocol::DataStream &stream, Akonadi::ImapInterval &interval)
310 {
311  return stream >> interval.d->begin
312  >> interval.d->end;
313 }
314 
315 Protocol::DataStream &operator<<(Protocol::DataStream &stream, const Akonadi::ImapSet &set)
316 {
317  return stream << set.d->intervals;
318 }
319 
320 Protocol::DataStream &operator>>(Protocol::DataStream &stream, Akonadi::ImapSet &set)
321 {
322  return stream >> set.d->intervals;
323 }
324 
325 } // namespace Akonadi
326 
327 using namespace Akonadi;
328 
329 QDebug operator<<(QDebug d, const Akonadi::ImapInterval &interval)
330 {
331  d << interval.toImapSequence();
332  return d;
333 }
334 
336 {
337  d << set.toImapSequenceSet();
338  return d;
339 }
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
int size() const const
KCALENDARCORE_EXPORT QDataStream & operator>>(QDataStream &in, const KCalendarCore::Alarm::Ptr &)
bool isEmpty() const const
KIOFILEWIDGETS_EXPORT void add(const QString &fileClass, const QString &directory)
const QList< QKeySequence > & begin()
QByteArray number(int n, int base)
QSet::const_iterator constEnd() const const
void reserve(int size)
QSet::const_iterator constBegin() const const
const QList< QKeySequence > & end()
Definition: item.h:44
Helper integration between Akonadi and Qt.
void push_back(const T &value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed May 27 2020 22:43:39 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.