• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.3 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  return *this;
86 }
87 
88 bool ImapInterval::operator ==(const ImapInterval & other) const
89 {
90  return ( d->begin == other.d->begin && d->end == other.d->end );
91 }
92 
93 ImapInterval::Id ImapInterval::size() const
94 {
95  if ( !d->begin && !d->end )
96  return 0;
97  if ( d->begin && !d->end )
98  return Q_INT64_C( 0x7FFFFFFFFFFFFFFF ) - d->begin + 1;
99  return d->end - d->begin + 1;
100 }
101 
102 bool ImapInterval::hasDefinedBegin() const
103 {
104  return d->begin != 0;
105 }
106 
107 ImapInterval::Id ImapInterval::begin() const
108 {
109  return d->begin;
110 }
111 
112 bool ImapInterval::hasDefinedEnd() const
113 {
114  return d->end != 0;
115 }
116 
117 ImapInterval::Id ImapInterval::end() const
118 {
119  if ( hasDefinedEnd() )
120  return d->end;
121  return 0xFFFFFFFF; // should be INT_MAX, but where is that defined again?
122 }
123 
124 void ImapInterval::setBegin(Id value)
125 {
126  Q_ASSERT( value >= 0 );
127  Q_ASSERT( value <= d->end || !hasDefinedEnd() );
128  d->begin = value;
129 }
130 
131 void ImapInterval::setEnd(Id value)
132 {
133  Q_ASSERT( value >= 0 );
134  Q_ASSERT( value >= d->begin || !hasDefinedBegin() );
135  d->end = value;
136 }
137 
138 QByteArray ImapInterval::toImapSequence() const
139 {
140  if ( size() == 0 )
141  return QByteArray();
142  if ( size() == 1 )
143  return QByteArray::number( d->begin );
144  QByteArray rv;
145  rv += QByteArray::number( d->begin ) + ':';
146  if ( hasDefinedEnd() )
147  rv += QByteArray::number( d->end );
148  else
149  rv += '*';
150  return rv;
151 }
152 
153 ImapInterval ImapInterval::fromImapSequence( const QByteArray &sequence )
154 {
155  QList<QByteArray> values = sequence.split( ':' );
156  if ( values.isEmpty() || values.size() > 2 ) {
157  return ImapInterval();
158  }
159 
160  bool ok = false;
161  Id begin = values[0].toLongLong(&ok);
162 
163  if ( !ok ) {
164  return ImapInterval();
165  }
166 
167  Id end;
168 
169  if ( values.size() == 1 ) {
170  end = begin;
171  } else if ( values[1] == QByteArray( "*" ) ) {
172  end = 0;
173  } else {
174  ok = false;
175  end = values[1].toLongLong(&ok);
176  if ( !ok ) {
177  return ImapInterval();
178  }
179  }
180 
181  return ImapInterval( begin, end );
182 }
183 
184 ImapSet::ImapSet() :
185  d( new Private )
186 {
187 }
188 
189 ImapSet::ImapSet( Id begin, Id end ) :
190  d( new Private )
191 {
192  add( ImapInterval( begin, end ) );
193 }
194 
195 ImapSet::ImapSet( Id value ) :
196  d( new Private )
197 {
198  add( QList<Id>() << value );
199 }
200 
201 ImapSet::ImapSet(const ImapSet & other) :
202  d( other.d )
203 {
204 }
205 
206 ImapSet::~ImapSet()
207 {
208 }
209 
210 ImapSet & ImapSet::operator =(const ImapSet & other)
211 {
212  if ( this != &other )
213  d = other.d;
214  return *this;
215 }
216 
217 bool ImapSet::operator ==(const ImapSet &other) const
218 {
219  if ( d->intervals.size()!=other.d->intervals.size() ) {
220  return false;
221  }
222 
223  foreach ( const ImapInterval &interval, d->intervals ) {
224  if ( !other.d->intervals.contains( interval ) ) {
225  return false;
226  }
227  }
228 
229  return true;
230 }
231 
232 void ImapSet::add( Id value )
233 {
234  add( QList<Id>() << value );
235 }
236 
237 void ImapSet::add(const QList<Id> & values)
238 {
239  QList<Id> vals = values;
240  qSort( vals );
241  for( int i = 0; i < vals.count(); ++i ) {
242  const int begin = vals[i];
243  Q_ASSERT( begin >= 0 );
244  if ( i == vals.count() - 1 ) {
245  d->intervals << ImapInterval( begin, begin );
246  break;
247  }
248  do {
249  ++i;
250  Q_ASSERT( vals[i] >= 0 );
251  if ( vals[i] != (vals[i - 1] + 1) ) {
252  --i;
253  break;
254  }
255  } while ( i < vals.count() - 1 );
256  d->intervals << ImapInterval( begin, vals[i] );
257  }
258 }
259 
260 void ImapSet::add(const ImapInterval & interval)
261 {
262  d->intervals << interval;
263 }
264 
265 QByteArray ImapSet::toImapSequenceSet() const
266 {
267  QList<QByteArray> rv;
268  foreach ( const ImapInterval &interval, d->intervals ) {
269  rv << interval.toImapSequence();
270  }
271 
272  QByteArray result;
273 
274  if ( !rv.isEmpty() ) {
275  result = rv.first();
276  QList<QByteArray>::ConstIterator it = rv.constBegin();
277  ++it;
278  for ( ; it != rv.constEnd(); ++it ) {
279  result += ',' + (*it);
280  }
281  }
282 
283  return result;
284 }
285 
286 ImapSet ImapSet::fromImapSequenceSet( const QByteArray &sequence )
287 {
288  ImapSet result;
289 
290  QList<QByteArray> intervals = sequence.split( ',' );
291 
292  foreach( const QByteArray &interval, intervals ) {
293  if ( !interval.isEmpty() ) {
294  result.add( ImapInterval::fromImapSequence( interval ) );
295  }
296  }
297 
298  return result;
299 }
300 
301 ImapInterval::List ImapSet::intervals() const
302 {
303  return d->intervals;
304 }
305 
306 bool ImapSet::isEmpty() const
307 {
308  return d->intervals.isEmpty();
309 }
310 
311 QDebug& operator<<( QDebug &d, const ImapInterval &interval )
312 {
313  d << interval.toImapSequence();
314  return d;
315 }
316 
317 QDebug& operator<<( QDebug &d, const ImapSet &set )
318 {
319  d << set.toImapSequenceSet();
320  return d;
321 }
322 
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Wed Nov 28 2012 21:43:26 by doxygen 1.8.1.2 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.9.3 API Reference

Skip menu "kdepimlibs-4.9.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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