Logo Search packages:      
Sourcecode: akonadi version File versions  Download package

imapset.cpp

/*
    Copyright (c) 2007 Volker Krause <vkrause@kde.org>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include "imapset_p.h"

#include "imapparser_p.h"

#include <QtCore/QSharedData>

using namespace Akonadi;

class ImapInterval::Private : public QSharedData
{
  public:
    Private() :
      QSharedData(),
      begin( 0 ),
      end( 0 )
    {}

    Private( const Private &other ) :
      QSharedData( other )
    {
      begin = other.begin;
      end = other.end;
    }

    Id begin;
    Id end;
};

class ImapSet::Private : public QSharedData
{
  public:
    Private() : QSharedData() {}
    Private( const Private &other ) :
      QSharedData( other )
    {
    }

    ImapInterval::List intervals;
};


00061 ImapInterval::ImapInterval() :
    d( new Private )
{
}

00066 ImapInterval::ImapInterval(const ImapInterval & other) :
    d( other.d )
{
}

00071 ImapInterval::ImapInterval(Id begin, Id end) :
    d( new Private )
{
  d->begin = begin;
  d->end = end;
}

00078 ImapInterval::~ ImapInterval()
{
}

00082 ImapInterval& ImapInterval::operator =(const ImapInterval & other)
{
  if ( this != & other )
    d = other.d;
  return *this;
}

00089 bool ImapInterval::operator ==(const ImapInterval & other) const
{
  return ( d->begin == other.d->begin && d->end == other.d->end );
}

00094 ImapInterval::Id ImapInterval::size() const
{
  if ( !d->begin && !d->end )
    return 0;
  return d->end - d->begin + 1;
}

00101 bool ImapInterval::hasDefinedBegin() const
{
  return d->begin != 0;
}

00106 ImapInterval::Id ImapInterval::begin() const
{
  return d->begin;
}

00111 bool ImapInterval::hasDefinedEnd() const
{
  return d->end != 0;
}

00116 ImapInterval::Id ImapInterval::end() const
{
  if ( hasDefinedEnd() )
    return d->end;
  return 0xFFFFFFFF; // should be INT_MAX, but where is that defined again?
}

00123 void ImapInterval::setBegin(Id value)
{
  Q_ASSERT( value >= 0 );
  Q_ASSERT( value <= d->end || !hasDefinedEnd() );
  d->begin = value;
}

00130 void ImapInterval::setEnd(Id value)
{
  Q_ASSERT( value >= 0 );
  Q_ASSERT( value >= d->begin || !hasDefinedBegin() );
  d->end = value;
}

00137 QByteArray Akonadi::ImapInterval::toImapSequence() const
{
  if ( size() == 0 )
    return QByteArray();
  if ( size() == 1 )
    return QByteArray::number( d->begin );
  QByteArray rv;
  rv += QByteArray::number( d->begin ) + ':';
  if ( hasDefinedEnd() )
    rv += QByteArray::number( d->end );
  else
    rv += '*';
  return rv;
}


00153 ImapSet::ImapSet() :
    d( new Private )
{
}

00158 ImapSet::ImapSet(const ImapSet & other) :
    d( other.d )
{
}

00163 ImapSet::~ImapSet()
{
}

00167 ImapSet & ImapSet::operator =(const ImapSet & other)
{
  if ( this != &other )
    d = other.d;
  return *this;
}

00174 void ImapSet::add(const QList<Id> & values)
{
  QList<Id> vals = values;
  qSort( vals );
  for( int i = 0; i < vals.count(); ++i ) {
    const int begin = vals[i];
    Q_ASSERT( begin >= 0 );
    if ( i == vals.count() - 1 ) {
      d->intervals << ImapInterval( begin, begin );
      break;
    }
    do {
      ++i;
      Q_ASSERT( vals[i] >= 0 );
      if ( vals[i] != (vals[i - 1] + 1) ) {
        --i;
        break;
      }
    } while ( i < vals.count() - 1 );
    d->intervals << ImapInterval( begin, vals[i] );
  }
}

00197 void ImapSet::add(const ImapInterval & interval)
{
  d->intervals << interval;
}

00202 QByteArray ImapSet::toImapSequenceSet() const
{
  QList<QByteArray> rv;
  foreach ( const ImapInterval interval, d->intervals )
    rv << interval.toImapSequence();
  return ImapParser::join( rv, "," );
}

00210 ImapInterval::List ImapSet::intervals() const
{
  return d->intervals;
}

00215 bool ImapSet::isEmpty() const
{
  return d->intervals.isEmpty();
}

QDebug& operator<<( QDebug &d, const Akonadi::ImapInterval &interval )
{
  d << interval.toImapSequence();
  return d;
}


Generated by  Doxygen 1.6.0   Back to index