• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 API Reference
  • KDE Home
  • Contact Us
 

KIMAP Library

  • kimap
imapset.cpp
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 #include "imapset.h"
21 
22 #include <QtCore/QSharedData>
23 
24 using namespace KIMAP;
25 
26 class ImapInterval::Private : public QSharedData
27 {
28  public:
29  Private() :
30  QSharedData(),
31  begin( 0 ),
32  end( 0 )
33  {}
34 
35  Private( const Private &other ) :
36  QSharedData( other )
37  {
38  begin = other.begin;
39  end = other.end;
40  }
41 
42  Id begin;
43  Id end;
44 };
45 
46 class ImapSet::Private : public QSharedData
47 {
48  public:
49  Private() : QSharedData() {}
50  Private( const Private &other ) :
51  QSharedData( other )
52  {
53  intervals = other.intervals;
54  }
55 
56  ImapInterval::List intervals;
57 };
58 
59 
60 ImapInterval::ImapInterval() :
61  d( new Private )
62 {
63 }
64 
65 ImapInterval::ImapInterval(const ImapInterval & other) :
66  d( other.d )
67 {
68 }
69 
70 ImapInterval::ImapInterval(Id begin, Id end) :
71  d( new Private )
72 {
73  d->begin = begin;
74  d->end = end;
75 }
76 
77 ImapInterval::~ ImapInterval()
78 {
79 }
80 
81 ImapInterval& ImapInterval::operator =(const ImapInterval & other)
82 {
83  if ( this != &other ) {
84  d = other.d;
85  }
86  return *this;
87 }
88 
89 bool ImapInterval::operator ==(const ImapInterval & other) const
90 {
91  return ( d->begin == other.d->begin && d->end == other.d->end );
92 }
93 
94 ImapInterval::Id ImapInterval::size() const
95 {
96  if ( !d->begin && !d->end ) {
97  return 0;
98  }
99  if ( d->begin && !d->end ) {
100  return Q_INT64_C( 0x7FFFFFFFFFFFFFFF ) - d->begin + 1;
101  }
102  return d->end - d->begin + 1;
103 }
104 
105 bool ImapInterval::hasDefinedBegin() const
106 {
107  return d->begin != 0;
108 }
109 
110 ImapInterval::Id ImapInterval::begin() const
111 {
112  return d->begin;
113 }
114 
115 bool ImapInterval::hasDefinedEnd() const
116 {
117  return d->end != 0;
118 }
119 
120 ImapInterval::Id ImapInterval::end() const
121 {
122  if ( hasDefinedEnd() ) {
123  return d->end;
124  }
125  return 0xFFFFFFFF; // should be INT_MAX, but where is that defined again?
126 }
127 
128 void ImapInterval::setBegin(Id value)
129 {
130  Q_ASSERT( value >= 0 );
131  Q_ASSERT( value <= d->end || !hasDefinedEnd() );
132  d->begin = value;
133 }
134 
135 void ImapInterval::setEnd(Id value)
136 {
137  Q_ASSERT( value >= 0 );
138  Q_ASSERT( value >= d->begin || !hasDefinedBegin() );
139  d->end = value;
140 }
141 
142 QByteArray ImapInterval::toImapSequence() const
143 {
144  if ( size() == 0 ) {
145  return QByteArray();
146  }
147  if ( size() == 1 ) {
148  return QByteArray::number( d->begin );
149  }
150  QByteArray rv;
151  rv += QByteArray::number( d->begin ) + ':';
152  if ( hasDefinedEnd() ) {
153  rv += QByteArray::number( d->end );
154  } else {
155  rv += '*';
156  }
157  return rv;
158 }
159 
160 ImapInterval ImapInterval::fromImapSequence( const QByteArray &sequence )
161 {
162  QList<QByteArray> values = sequence.split( ':' );
163  if ( values.isEmpty() || values.size() > 2 ) {
164  return ImapInterval();
165  }
166 
167  bool ok = false;
168  Id begin = values[0].toLongLong( &ok );
169 
170  if ( !ok ) {
171  return ImapInterval();
172  }
173 
174  Id end;
175 
176  if ( values.size() == 1 ) {
177  end = begin;
178  } else if ( values[1] == QByteArray( "*" ) ) {
179  end = 0;
180  } else {
181  ok = false;
182  end = values[1].toLongLong( &ok );
183  if ( !ok ) {
184  return ImapInterval();
185  }
186  }
187 
188  return ImapInterval( begin, end );
189 }
190 
191 ImapSet::ImapSet() :
192  d( new Private )
193 {
194 }
195 
196 ImapSet::ImapSet( Id begin, Id end ) :
197  d( new Private )
198 {
199  add( ImapInterval( begin, end ) );
200 }
201 
202 ImapSet::ImapSet( Id value ) :
203  d( new Private )
204 {
205  add( QList<Id>() << value );
206 }
207 
208 ImapSet::ImapSet(const ImapSet & other) :
209  d( other.d )
210 {
211 }
212 
213 ImapSet::~ImapSet()
214 {
215 }
216 
217 ImapSet & ImapSet::operator =(const ImapSet & other)
218 {
219  if ( this != &other ) {
220  d = other.d;
221  }
222  return *this;
223 }
224 
225 bool ImapSet::operator ==(const ImapSet &other) const
226 {
227  if ( d->intervals.size()!=other.d->intervals.size() ) {
228  return false;
229  }
230 
231  foreach ( const ImapInterval &interval, d->intervals ) {
232  if ( !other.d->intervals.contains( interval ) ) {
233  return false;
234  }
235  }
236 
237  return true;
238 }
239 
240 void ImapSet::add( Id value )
241 {
242  add( QList<Id>() << value );
243 }
244 
245 void ImapSet::add(const QList<Id> & values)
246 {
247  QList<Id> vals = values;
248  qSort( vals );
249  for ( int i = 0; i < vals.count(); ++i ) {
250  const int begin = vals[i];
251  Q_ASSERT( begin >= 0 );
252  if ( i == vals.count() - 1 ) {
253  d->intervals << ImapInterval( begin, begin );
254  break;
255  }
256  do {
257  ++i;
258  Q_ASSERT( vals[i] >= 0 );
259  if ( vals[i] != ( vals[i - 1] + 1 ) ) {
260  --i;
261  break;
262  }
263  } while ( i < vals.count() - 1 );
264  d->intervals << ImapInterval( begin, vals[i] );
265  }
266 }
267 
268 void ImapSet::add(const ImapInterval & interval)
269 {
270  d->intervals << interval;
271 }
272 
273 QByteArray ImapSet::toImapSequenceSet() const
274 {
275  QList<QByteArray> rv;
276  foreach ( const ImapInterval &interval, d->intervals ) {
277  rv << interval.toImapSequence();
278  }
279 
280  QByteArray result;
281 
282  if ( !rv.isEmpty() ) {
283  result = rv.first();
284  QList<QByteArray>::ConstIterator it = rv.constBegin();
285  ++it;
286  for ( ; it != rv.constEnd(); ++it ) {
287  result += ',' + ( *it );
288  }
289  }
290 
291  return result;
292 }
293 
294 ImapSet ImapSet::fromImapSequenceSet( const QByteArray &sequence )
295 {
296  ImapSet result;
297 
298  QList<QByteArray> intervals = sequence.split( ',' );
299 
300  foreach ( const QByteArray &interval, intervals ) {
301  if ( !interval.isEmpty() ) {
302  result.add( ImapInterval::fromImapSequence( interval ) );
303  }
304  }
305 
306  return result;
307 }
308 
309 ImapInterval::List ImapSet::intervals() const
310 {
311  return d->intervals;
312 }
313 
314 bool ImapSet::isEmpty() const
315 {
316  return d->intervals.isEmpty();
317 }
318 
319 QDebug& operator<<( QDebug &d, const ImapInterval &interval )
320 {
321  d << interval.toImapSequence();
322  return d;
323 }
324 
325 QDebug& operator<<( QDebug &d, const ImapSet &set )
326 {
327  d << set.toImapSequenceSet();
328  return d;
329 }
330 
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:25:16 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIMAP Library

Skip menu "KIMAP Library"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal