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*))) );
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();
74 if ( children.isEmpty() )
76 m_pendingCollections.append( children );
78 toBeProcessed.enqueue( child );
81 replayNextCollection();
84 void RecursiveMover::collectionFetchResult( KJob *job )
86 Q_ASSERT( m_currentCollection.
isValid() );
94 m_currentCollection = fetchJob->
collections().first();
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() ) {
114 m_pendingItems.push_back( item );
117 if ( !m_runningJobs && m_pendingReplay )
121 void RecursiveMover::itemFetchResult( KJob *job )
123 Q_ASSERT( m_currentAction == None );
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();
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();
177 connect( job, SIGNAL(result(KJob*)), SLOT(itemFetchResult(KJob*)) );
185 Q_ASSERT( m_currentAction != None );
187 if ( m_currentAction == AddCollection ) {
188 Q_ASSERT( m_currentCollection.
isValid() );
191 connect( job, SIGNAL(result(KJob*)), SLOT(collectionFetchResult(KJob*)) );
196 m_currentAction = None;
202 if ( m_runningJobs ) {
203 m_pendingReplay =
true;
207 m_pendingReplay =
false;
209 if ( m_currentCollection.
isValid() )
212 replayNextCollection();
215 #include "moc_recursivemover_p.cpp"