diff --git a/akonadi/itemdeletejob.cpp b/akonadi/itemdeletejob.cpp index a89464a8b..9821fe751 100644 --- a/akonadi/itemdeletejob.cpp +++ b/akonadi/itemdeletejob.cpp @@ -1,110 +1,110 @@ /* Copyright (c) 2006 - 2007 Volker Krause 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 "itemdeletejob.h" #include "collection.h" #include "collectionselectjob_p.h" #include "item.h" #include "job_p.h" #include "protocolhelper_p.h" #include #include #include #include using namespace Akonadi; class Akonadi::ItemDeleteJobPrivate : public JobPrivate { public: ItemDeleteJobPrivate( ItemDeleteJob *parent ) : JobPrivate( parent ) { } void selectResult( KJob *job ); Q_DECLARE_PUBLIC( ItemDeleteJob ) Item::List mItems; Collection mCollection; }; void ItemDeleteJobPrivate::selectResult( KJob *job ) { if ( job->error() ) return; // KCompositeJob takes care of errors const QByteArray command = newTag() + " " AKONADI_CMD_ITEMDELETE " 1:*\n"; writeData( command ); } ItemDeleteJob::ItemDeleteJob( const Item & item, QObject * parent ) : Job( new ItemDeleteJobPrivate( this ), parent ) { Q_D( ItemDeleteJob ); d->mItems << item; } ItemDeleteJob::ItemDeleteJob(const Item::List& items, QObject* parent) : Job( new ItemDeleteJobPrivate( this ), parent ) { Q_D( ItemDeleteJob ); d->mItems = items; } ItemDeleteJob::ItemDeleteJob(const Collection& collection, QObject* parent) : Job( new ItemDeleteJobPrivate( this ), parent ) { Q_D( ItemDeleteJob ); d->mCollection = collection; } ItemDeleteJob::~ItemDeleteJob() { } void ItemDeleteJob::doStart() { Q_D( ItemDeleteJob ); if ( !d->mItems.isEmpty() ) { QByteArray command = d->newTag(); try { command += ProtocolHelper::itemSetToByteArray( d->mItems, AKONADI_CMD_ITEMDELETE ); } catch ( const std::exception &e ) { setError( Unknown ); setErrorText( QString::fromUtf8( e.what() ) ); emitResult(); return; } command += '\n'; d->writeData( command ); } else { CollectionSelectJob *job = new CollectionSelectJob( d->mCollection, this ); - connect( job, SIGNAL(result(KJob*)), SLOT(selectDone(KJob*)) ); + connect( job, SIGNAL(result(KJob*)), SLOT(selectResult(KJob*)) ); addSubjob( job ); } } #include "itemdeletejob.moc" diff --git a/akonadi/tests/itemdeletetest.cpp b/akonadi/tests/itemdeletetest.cpp index 810af8775..5230cca4d 100644 --- a/akonadi/tests/itemdeletetest.cpp +++ b/akonadi/tests/itemdeletetest.cpp @@ -1,113 +1,137 @@ /* Copyright (c) 20089 Volker Krause 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 #include #include #include #include #include #include #include #include "test_utils.h" #include #include using namespace Akonadi; class ItemDeleteTest : public QObject { Q_OBJECT private slots: void initTestCase() { Control::start(); } void testIllegalDelete() { ItemDeleteJob *djob = new ItemDeleteJob( Item( INT_MAX ), this ); QVERIFY( !djob->exec() ); // make sure a failed delete doesn't leave a transaction open (the kpilot bug) TransactionRollbackJob *tjob = new TransactionRollbackJob( this ); QVERIFY( !tjob->exec() ); } void testDelete() { ItemDeleteJob *djob = new ItemDeleteJob( Item( 1 ), this ); AKVERIFYEXEC( djob ); ItemFetchJob *fjob = new ItemFetchJob( Item( 1 ), this ); AKVERIFYEXEC( fjob ); QCOMPARE( fjob->items().count(), 0 ); } void testDeleteFromUnselectedCollection() { const QString path = QLatin1String( "res1" ) + CollectionPathResolver::pathDelimiter() + QLatin1String( "foo" ); CollectionPathResolver *rjob = new CollectionPathResolver( path, this ); AKVERIFYEXEC( rjob ); ItemFetchJob *fjob = new ItemFetchJob( Collection( rjob->collection() ), this ); AKVERIFYEXEC( fjob ); const Item::List items = fjob->items(); QVERIFY( items.count() > 0 ); CollectionSelectJob *sjob = new CollectionSelectJob( Collection( 2 ), this ); AKVERIFYEXEC( sjob ); ItemDeleteJob *djob = new ItemDeleteJob( items[ 0 ], this ); AKVERIFYEXEC( djob ); fjob = new ItemFetchJob( items[ 0 ], this ); AKVERIFYEXEC( fjob ); QCOMPARE( fjob->items().count(), 0 ); } void testRidDelete() { const Collection col ( collectionIdFromPath( "res1/foo" ) ); QVERIFY( col.isValid() ); CollectionSelectJob *sel = new CollectionSelectJob( col ); AKVERIFYEXEC( sel ); Item i; i.setRemoteId( "C" ); ItemDeleteJob *djob = new ItemDeleteJob( i, this ); AKVERIFYEXEC( djob ); ItemFetchJob *fjob = new ItemFetchJob( i, this ); fjob->setCollection( col ); AKVERIFYEXEC( fjob ); QCOMPARE( fjob->items().count(), 0 ); } + void testCollectionDelete() + { + const Collection col( collectionIdFromPath( "res1/foo" ) ); + ItemFetchJob *fjob = new ItemFetchJob( col, this ); + AKVERIFYEXEC( fjob ); + QVERIFY( fjob->items().count() > 0 ); + + // delete from non-empty collection + ItemDeleteJob *djob = new ItemDeleteJob( col, this ); + AKVERIFYEXEC( djob ); + + fjob = new ItemFetchJob( col, this ); + AKVERIFYEXEC( fjob ); + QCOMPARE( fjob->items().count(), 0 ); + + // delete from empty collection + djob = new ItemDeleteJob( col, this ); + QVERIFY( !djob->exec() ); // error: no items found + + fjob = new ItemFetchJob( col, this ); + AKVERIFYEXEC( fjob ); + QCOMPARE( fjob->items().count(), 0 ); + } + }; QTEST_AKONADIMAIN( ItemDeleteTest, NoGUI ) #include "itemdeletetest.moc"