Commit 5b5505ef authored by Volker Krause's avatar Volker Krause

Refactor itemSetToByteArray() to be usable for collections as well.

svn path=/trunk/KDE/kdepimlibs/; revision=1020872
parent 5b708d9f
......@@ -91,7 +91,7 @@ void ItemDeleteJob::doStart()
if ( !d->mItems.isEmpty() ) {
QByteArray command = d->newTag();
try {
command += ProtocolHelper::itemSetToByteArray( d->mItems, AKONADI_CMD_ITEMDELETE );
command += ProtocolHelper::entitySetToByteArray( d->mItems, AKONADI_CMD_ITEMDELETE );
} catch ( const std::exception &e ) {
setError( Unknown );
setErrorText( QString::fromUtf8( e.what() ) );
......
......@@ -77,7 +77,7 @@ void ItemMoveJob::doStart()
QByteArray command = d->newTag();
try {
command += ProtocolHelper::itemSetToByteArray( d->mItems, "MOVE" );
command += ProtocolHelper::entitySetToByteArray( d->mItems, "MOVE" );
} catch ( const std::exception &e ) {
setError( Unknown );
setErrorText( QString::fromUtf8( e.what() ) );
......
......@@ -25,8 +25,6 @@
#include "exception.h"
#include "itemserializer_p.h"
#include "itemserializerplugin.h"
#include <akonadi/private/imapparser_p.h>
#include <akonadi/private/protocol_p.h>
#include <QtCore/QDateTime>
#include <QtCore/QFile>
......@@ -35,9 +33,6 @@
#include <kdebug.h>
#include <klocale.h>
#include <boost/bind.hpp>
#include <algorithm>
using namespace Akonadi;
int ProtocolHelper::parseCachePolicy(const QByteArray & data, CachePolicy & policy, int start)
......@@ -219,44 +214,6 @@ QByteArray ProtocolHelper::decodePartIdentifier( const QByteArray &data, PartNam
}
}
QByteArray ProtocolHelper::itemSetToByteArray( const Item::List &_items, const QByteArray &command )
{
if ( _items.isEmpty() )
throw Exception( "No items specified" );
Item::List items( _items );
QByteArray rv;
std::sort( items.begin(), items.end(), boost::bind( &Item::id, _1 ) < boost::bind( &Item::id, _2 ) );
if ( items.first().isValid() ) {
// all items have a uid set
rv += " " AKONADI_CMD_UID " ";
rv += command;
rv += ' ';
QList<Item::Id> uids;
foreach ( const Item &item, items )
uids << item.id();
ImapSet set;
set.add( uids );
rv += set.toImapSequenceSet();
} else {
// check if all items have a remote id
QList<QByteArray> rids;
foreach ( const Item &item, items ) {
if ( item.remoteId().isEmpty() )
throw Exception( i18n( "No remote identifier specified" ) );
rids << ImapParser::quote( item.remoteId().toUtf8() );
}
rv += " " AKONADI_CMD_RID " ";
rv += command;
rv += " (";
rv += ImapParser::join( rids, " " );
rv += ')';
}
return rv;
}
QByteArray ProtocolHelper::hierarchicalRidToByteArray( const Collection &col )
{
if ( col == Collection::root() )
......
......@@ -25,6 +25,12 @@
#include <akonadi/item.h>
#include <akonadi/itemfetchscope.h>
#include <akonadi/private/imapparser_p.h>
#include <akonadi/private/protocol_p.h>
#include <boost/bind.hpp>
#include <algorithm>
namespace Akonadi {
/**
......@@ -92,7 +98,44 @@ class ProtocolHelper
Converts the given set of items into a protocol representation.
@throws A Akonadi::Exception if the item set contains items with missing/invalid identifiers.
*/
static QByteArray itemSetToByteArray( const Item::List &items, const QByteArray &command );
template <typename T>
static QByteArray entitySetToByteArray( const QList<T> &_objects, const QByteArray &command )
{
if ( _objects.isEmpty() )
throw Exception( "No objects specified" );
typename T::List objects( _objects );
QByteArray rv;
std::sort( objects.begin(), objects.end(), boost::bind( &T::id, _1 ) < boost::bind( &T::id, _2 ) );
if ( objects.first().isValid() ) {
// all items have a uid set
rv += " " AKONADI_CMD_UID " ";
rv += command;
rv += ' ';
QList<typename T::Id> uids;
foreach ( const T &object, objects )
uids << object.id();
ImapSet set;
set.add( uids );
rv += set.toImapSequenceSet();
} else {
// check if all items have a remote id
QList<QByteArray> rids;
foreach ( const T &object, objects ) {
if ( object.remoteId().isEmpty() )
throw Exception( "No remote identifier specified" );
rids << ImapParser::quote( object.remoteId().toUtf8() );
}
rv += " " AKONADI_CMD_RID " ";
rv += command;
rv += " (";
rv += ImapParser::join( rids, " " );
rv += ')';
}
return rv;
}
/**
Converts the given collection's hierarchical RID into a protocol representation.
......
......@@ -56,7 +56,7 @@ class ProtocolHelperTest : public QObject
bool didThrow = false;
try {
const QByteArray r = ProtocolHelper::itemSetToByteArray( items, "CMD" );
const QByteArray r = ProtocolHelper::entitySetToByteArray( items, "CMD" );
QCOMPARE( r, result );
} catch ( const std::exception &e ) {
qDebug() << e.what();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment