/**
* Once the folder structure has been created it copies messages recursively from the root
* folder.
*/
private void copySourceMessages(IMAPFolder sourceFolder) throws MessagingException {
if (sourceFolder != null) {
final String sourceFolderName = sourceFolder.getFullName();
final String targetFolderName = sourceFolderNameToTarget(sourceFolderName, sourceIndex,
targetIndex);
if ((sourceFolder.getType() & Folder.HOLDS_MESSAGES) == Folder.HOLDS_MESSAGES) {
//Manage Servers with public/read only folders.
try {
sourceFolder.open(Folder.READ_WRITE);
} catch (ReadOnlyFolderException ex) {
sourceFolder.open(Folder.READ_ONLY);
}
if (sourceFolder.getMode() != Folder.READ_ONLY) {
sourceFolder.expunge();
}
///////////////////////
final int messageCount = sourceFolder.getMessageCount();
sourceFolder.close(false);
int pos = 1;
while (pos + MNIMAPSync.BATCH_SIZE <= messageCount) {
//Copy messages
service.execute(new MessageCopier(this, sourceFolderName, targetFolderName, pos,
pos + MNIMAPSync.BATCH_SIZE, targetIndex.getFolderMessages(
targetFolderName)));
pos = pos + MNIMAPSync.BATCH_SIZE;
}
service.execute(new MessageCopier(this, sourceFolderName, targetFolderName, pos,
messageCount,
targetIndex.getFolderMessages(targetFolderName)));
}
//Folder recursion. Get all children
if ((sourceFolder.getType() & Folder.HOLDS_FOLDERS) == Folder.HOLDS_FOLDERS) {
for (Folder child : sourceFolder.list()) {
copySourceMessages((IMAPFolder) child);
}
}
}
}