20 #include "recursivemover_p.h"
21 #include "collectionfetchjob.h"
22 #include "itemfetchjob.h"
23 #include "itemfetchscope.h"
24 #include "collectionfetchscope.h"
26 using namespace Akonadi;
28 RecursiveMover::RecursiveMover(
AgentBasePrivate* parent): KCompositeJob(parent),
29 m_agentBase( parent ), m_currentAction( None ), m_runningJobs( 0 ), m_pendingReplay( false )
33 void RecursiveMover::start()
35 Q_ASSERT( receivers(SIGNAL(result(KJob*))) );
37 CollectionFetchJob *job =
new CollectionFetchJob( m_movedCollection, CollectionFetchJob::Recursive,
this );
38 connect( job, SIGNAL(finished(KJob*)), SLOT(collectionListResult(KJob*)) );
45 m_movedCollection = collection;
46 m_collections.insert( collection.
id(), m_movedCollection );
47 m_collections.insert( parentCollection.
id(), parentCollection );
50 void RecursiveMover::collectionListResult( KJob *job )
52 Q_ASSERT( m_pendingCollections.isEmpty() );
61 QHash<Collection::Id, Collection::List> colTree;
64 m_collections.insert( col.id(), col );
68 m_pendingCollections.push_back( m_movedCollection );
69 QQueue<Collection> toBeProcessed;
70 toBeProcessed.enqueue( m_movedCollection );
71 while ( !toBeProcessed.isEmpty() ) {
72 const Collection col = toBeProcessed.dequeue();
73 const Collection::List children = colTree.value( col.id() );
74 if ( children.isEmpty() )
76 m_pendingCollections.append( children );
77 foreach (
const Collection &child, children )
78 toBeProcessed.enqueue( child );
81 replayNextCollection();
84 void RecursiveMover::collectionFetchResult( KJob *job )
86 Q_ASSERT( m_currentCollection.isValid() );
92 CollectionFetchJob *fetchJob = qobject_cast<CollectionFetchJob*>( job );
93 if ( fetchJob->collections().size() == 1 ) {
94 m_currentCollection = fetchJob->
collections().first();
95 m_currentCollection.setParentCollection( m_collections.value( m_currentCollection.parentCollection().id() ) );
96 m_collections.insert( m_currentCollection.id(), m_currentCollection );
101 if ( !m_runningJobs && m_pendingReplay )
105 void RecursiveMover::itemListResult( KJob *job )
112 foreach (
const Item &item, qobject_cast<ItemFetchJob*>( job )->items() ) {
113 if ( item.remoteId().isEmpty() )
114 m_pendingItems.push_back( item );
117 if ( !m_runningJobs && m_pendingReplay )
121 void RecursiveMover::itemFetchResult( KJob *job )
123 Q_ASSERT( m_currentAction == None );
129 ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob*>( job );
130 if ( fetchJob->items().size() == 1 ) {
131 m_currentAction = AddItem;
132 m_agentBase->itemAdded( fetchJob->items().first(), m_currentCollection );
135 m_currentItem = Item();
140 void RecursiveMover::replayNextCollection()
142 if ( !m_pendingCollections.isEmpty() ) {
144 m_currentCollection = m_pendingCollections.takeFirst();
145 ItemFetchJob *job =
new ItemFetchJob( m_currentCollection,
this );
146 connect( job, SIGNAL(result(KJob*)), SLOT(itemListResult(KJob*)));
150 if ( m_currentCollection.remoteId().isEmpty() ) {
151 Q_ASSERT( m_currentAction == None );
152 m_currentAction = AddCollection;
153 m_agentBase->collectionAdded( m_currentCollection, m_collections.value( m_currentCollection.parentCollection().id() ) );
157 m_pendingReplay =
true;
166 void RecursiveMover::replayNextItem()
168 Q_ASSERT( m_currentCollection.isValid() );
169 if ( m_pendingItems.isEmpty() ) {
170 replayNextCollection();
173 Q_ASSERT( m_currentAction == None );
174 m_currentItem = m_pendingItems.takeFirst();
175 ItemFetchJob *job =
new ItemFetchJob( m_currentItem,
this );
176 job->fetchScope().fetchFullPayload();
177 connect( job, SIGNAL(result(KJob*)), SLOT(itemFetchResult(KJob*)) );
183 void RecursiveMover::changeProcessed()
185 Q_ASSERT( m_currentAction != None );
187 if ( m_currentAction == AddCollection ) {
188 Q_ASSERT( m_currentCollection.isValid() );
190 job->fetchScope().setAncestorRetrieval( CollectionFetchScope::All );
191 connect( job, SIGNAL(result(KJob*)), SLOT(collectionFetchResult(KJob*)) );
196 m_currentAction = None;
199 void RecursiveMover::replayNext()
202 if ( m_runningJobs ) {
203 m_pendingReplay =
true;
207 m_pendingReplay =
false;
209 if ( m_currentCollection.isValid() )
212 replayNextCollection();
215 #include "recursivemover_p.moc"