1/*
2 Copyright (c) 2010 Stephen Kelly <steveire@gmail.com>
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
21#include "entitytreecreatejob.h"
22
23
24#include <Akonadi/CollectionCreateJob>
25#include <Akonadi/ItemCreateJob>
26
27using namespace Akonadi;
28
29static const char collectionIdMappingProperty[] = "collectionIdMappingProperty";
30
31EntityTreeCreateJob::EntityTreeCreateJob( const QList< Akonadi::Collection::List > &collections, const Akonadi::Item::List &items, QObject* parent )
32 : Akonadi::TransactionSequence( parent ), m_collections( collections ), m_items( items ), m_pendingJobs( 0 )
33{
34
35}
36
37void EntityTreeCreateJob::doStart()
38{
39 if ( !m_collections.isEmpty() )
40 createNextLevelOfCollections();
41 createReadyItems();
42}
43
44void EntityTreeCreateJob::createNextLevelOfCollections()
45{
46 CollectionCreateJob *job;
47
48 const Collection::List colList = m_collections.takeFirst();
49 foreach ( const Collection &collection, colList ) {
50 ++m_pendingJobs;
51 job = new CollectionCreateJob( collection, this );
52 job->setProperty( collectionIdMappingProperty, collection.id() );
53 connect( job, SIGNAL(result(KJob*)), SLOT(collectionCreateJobDone(KJob*)) );
54 }
55}
56
57void EntityTreeCreateJob::createReadyItems()
58{
59 Item::List::iterator it;
60 for ( it = m_items.begin(); it != m_items.end(); ) {
61 Collection parentCollection = ( *it ).parentCollection();
62 if ( parentCollection.isValid() ) {
63 (void) new ItemCreateJob( *it, parentCollection, this );
64 it = m_items.erase( it );
65 } else {
66 ++it;
67 }
68 }
69 if ( m_items.isEmpty() && m_collections.isEmpty() )
70 commit();
71}
72
73void EntityTreeCreateJob::collectionCreateJobDone( KJob *job )
74{
75 Q_ASSERT( m_pendingJobs > 0 );
76 --m_pendingJobs;
77 CollectionCreateJob *collectionCreateJob = qobject_cast<CollectionCreateJob *>( job );
78 Collection createdCollection = collectionCreateJob->collection();
79
80 if ( job->error() ) {
81 kDebug() << job->errorString();
82 return;
83 }
84
85 const Collection::Id creationId = job->property( collectionIdMappingProperty ).toLongLong();
86
87 Item::List::iterator it;
88 const Item::List::iterator end = m_items.end();
89 for ( it = m_items.begin(); it != end; ++it ) {
90 kDebug() << "updating items";
91 if ( it->parentCollection().id() == creationId ) {
92 it->setParentCollection( createdCollection );
93 }
94 }
95
96 createReadyItems();
97
98 if ( !m_collections.isEmpty() ) {
99 Collection::List::iterator col_it;
100 const Collection::List::iterator col_end = m_collections[0].end();
101 for ( col_it = m_collections[0].begin(); col_it != col_end; ++col_it ) {
102 if ( col_it->parentCollection().id() == creationId ) {
103 col_it->setParentCollection( createdCollection );
104 }
105 }
106 if ( m_pendingJobs == 0 )
107 createNextLevelOfCollections();
108 }
109
110 if ( m_items.isEmpty() && m_collections.isEmpty() )
111 commit();
112}
113
114