• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

KIMAP Library

imapset.cpp

00001 /*
00002     Copyright (c) 2007 Volker Krause <vkrause@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or modify it
00005     under the terms of the GNU Library General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or (at your
00007     option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful, but WITHOUT
00010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00012     License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to the
00016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017     02110-1301, USA.
00018 */
00019 
00020 #include "imapset.h"
00021 
00022 #include <QtCore/QSharedData>
00023 
00024 using namespace KIMAP;
00025 
00026 class ImapInterval::Private : public QSharedData
00027 {
00028   public:
00029     Private() :
00030       QSharedData(),
00031       begin( 0 ),
00032       end( 0 )
00033     {}
00034 
00035     Private( const Private &other ) :
00036       QSharedData( other )
00037     {
00038       begin = other.begin;
00039       end = other.end;
00040     }
00041 
00042     Id begin;
00043     Id end;
00044 };
00045 
00046 class ImapSet::Private : public QSharedData
00047 {
00048   public:
00049     Private() : QSharedData() {}
00050     Private( const Private &other ) :
00051       QSharedData( other )
00052     {
00053     }
00054 
00055     ImapInterval::List intervals;
00056 };
00057 
00058 
00059 ImapInterval::ImapInterval() :
00060     d( new Private )
00061 {
00062 }
00063 
00064 ImapInterval::ImapInterval(const ImapInterval & other) :
00065     d( other.d )
00066 {
00067 }
00068 
00069 ImapInterval::ImapInterval(Id begin, Id end) :
00070     d( new Private )
00071 {
00072   d->begin = begin;
00073   d->end = end;
00074 }
00075 
00076 ImapInterval::~ ImapInterval()
00077 {
00078 }
00079 
00080 ImapInterval& ImapInterval::operator =(const ImapInterval & other)
00081 {
00082   if ( this != & other )
00083     d = other.d;
00084   return *this;
00085 }
00086 
00087 bool ImapInterval::operator ==(const ImapInterval & other) const
00088 {
00089   return ( d->begin == other.d->begin && d->end == other.d->end );
00090 }
00091 
00092 ImapInterval::Id ImapInterval::size() const
00093 {
00094   if ( !d->begin && !d->end )
00095     return 0;
00096   return d->end - d->begin + 1;
00097 }
00098 
00099 bool ImapInterval::hasDefinedBegin() const
00100 {
00101   return d->begin != 0;
00102 }
00103 
00104 ImapInterval::Id ImapInterval::begin() const
00105 {
00106   return d->begin;
00107 }
00108 
00109 bool ImapInterval::hasDefinedEnd() const
00110 {
00111   return d->end != 0;
00112 }
00113 
00114 ImapInterval::Id ImapInterval::end() const
00115 {
00116   if ( hasDefinedEnd() )
00117     return d->end;
00118   return 0xFFFFFFFF; // should be INT_MAX, but where is that defined again?
00119 }
00120 
00121 void ImapInterval::setBegin(Id value)
00122 {
00123   Q_ASSERT( value >= 0 );
00124   Q_ASSERT( value <= d->end || !hasDefinedEnd() );
00125   d->begin = value;
00126 }
00127 
00128 void ImapInterval::setEnd(Id value)
00129 {
00130   Q_ASSERT( value >= 0 );
00131   Q_ASSERT( value >= d->begin || !hasDefinedBegin() );
00132   d->end = value;
00133 }
00134 
00135 QByteArray ImapInterval::toImapSequence() const
00136 {
00137   if ( size() == 0 )
00138     return QByteArray();
00139   if ( size() == 1 )
00140     return QByteArray::number( d->begin );
00141   QByteArray rv;
00142   rv += QByteArray::number( d->begin ) + ':';
00143   if ( hasDefinedEnd() )
00144     rv += QByteArray::number( d->end );
00145   else
00146     rv += '*';
00147   return rv;
00148 }
00149 
00150 
00151 ImapSet::ImapSet() :
00152     d( new Private )
00153 {
00154 }
00155 
00156 ImapSet::ImapSet( Id begin, Id end ) :
00157     d( new Private )
00158 {
00159   add( ImapInterval( begin, end ) );
00160 }
00161 
00162 ImapSet::ImapSet( Id value ) :
00163     d( new Private )
00164 {
00165   add( QList<Id>() << value );
00166 }
00167 
00168 ImapSet::ImapSet(const ImapSet & other) :
00169     d( other.d )
00170 {
00171 }
00172 
00173 ImapSet::~ImapSet()
00174 {
00175 }
00176 
00177 ImapSet & ImapSet::operator =(const ImapSet & other)
00178 {
00179   if ( this != &other )
00180     d = other.d;
00181   return *this;
00182 }
00183 
00184 void ImapSet::add(const QList<Id> & values)
00185 {
00186   QList<Id> vals = values;
00187   qSort( vals );
00188   for( int i = 0; i < vals.count(); ++i ) {
00189     const int begin = vals[i];
00190     Q_ASSERT( begin >= 0 );
00191     if ( i == vals.count() - 1 ) {
00192       d->intervals << ImapInterval( begin, begin );
00193       break;
00194     }
00195     do {
00196       ++i;
00197       Q_ASSERT( vals[i] >= 0 );
00198       if ( vals[i] != (vals[i - 1] + 1) ) {
00199         --i;
00200         break;
00201       }
00202     } while ( i < vals.count() - 1 );
00203     d->intervals << ImapInterval( begin, vals[i] );
00204   }
00205 }
00206 
00207 void ImapSet::add(const ImapInterval & interval)
00208 {
00209   d->intervals << interval;
00210 }
00211 
00212 QByteArray ImapSet::toImapSequenceSet() const
00213 {
00214   QList<QByteArray> rv;
00215   foreach ( const ImapInterval interval, d->intervals ) {
00216     rv << interval.toImapSequence();
00217   }
00218 
00219   QByteArray result = rv.first();
00220   QList<QByteArray>::ConstIterator it = rv.constBegin();
00221   ++it;
00222   for ( ; it != rv.constEnd(); ++it ) {
00223     result += ',' + (*it);
00224   }
00225 
00226   return result;
00227 }
00228 
00229 ImapInterval::List ImapSet::intervals() const
00230 {
00231   return d->intervals;
00232 }
00233 
00234 bool ImapSet::isEmpty() const
00235 {
00236   return d->intervals.isEmpty();
00237 }
00238 
00239 QDebug& operator<<( QDebug &d, const ImapInterval &interval )
00240 {
00241   d << interval.toImapSequence();
00242   return d;
00243 }
00244 

KIMAP Library

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

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.8
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal