Merge pull request #7443 from thunderbird/fetch_folder_list_after_creating_account

Save special folder mapping and fetch folder list after creating account
This commit is contained in:
cketti 2024-01-03 13:14:12 +01:00 committed by GitHub
commit aae41a903f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 7 deletions

View file

@ -151,23 +151,18 @@ class Account(override val uuid: String) : BaseAccount {
@get:Synchronized
var draftsFolderSelection = SpecialFolderSelection.AUTOMATIC
private set
@get:Synchronized
var sentFolderSelection = SpecialFolderSelection.AUTOMATIC
private set
@get:Synchronized
var trashFolderSelection = SpecialFolderSelection.AUTOMATIC
private set
@get:Synchronized
var archiveFolderSelection = SpecialFolderSelection.AUTOMATIC
private set
@get:Synchronized
var spamFolderSelection = SpecialFolderSelection.AUTOMATIC
private set
@get:Synchronized
@set:Synchronized

View file

@ -364,8 +364,24 @@ public class MessagingController {
put("refreshFolderList", null, () -> refreshFolderListSynchronous(account));
}
@Deprecated
public void refreshFolderListSynchronous(Account account) {
public void refreshFolderListBlocking(Account account) {
final CountDownLatch latch = new CountDownLatch(1);
putBackground("refreshFolderListBlocking", null, () -> {
try {
refreshFolderListSynchronous(account);
} finally {
latch.countDown();
}
});
try {
latch.await();
} catch (Exception e) {
Timber.e(e, "Interrupted while awaiting latch release");
}
}
void refreshFolderListSynchronous(Account account) {
try {
if (isAuthenticationProblem(account, true)) {
Timber.d("Authentication will fail. Skip refreshing the folder list.");

View file

@ -3,11 +3,15 @@ package com.fsck.k9.account
import android.content.Context
import app.k9mail.core.common.mail.Protocols
import app.k9mail.feature.account.common.domain.entity.Account
import app.k9mail.feature.account.common.domain.entity.SpecialFolderOption
import app.k9mail.feature.account.common.domain.entity.SpecialFolderSettings
import app.k9mail.feature.account.setup.AccountSetupExternalContract
import app.k9mail.feature.account.setup.AccountSetupExternalContract.AccountCreator.AccountCreatorResult
import com.fsck.k9.Account.FolderMode
import com.fsck.k9.Account.SpecialFolderSelection
import com.fsck.k9.Core
import com.fsck.k9.Preferences
import com.fsck.k9.controller.MessagingController
import com.fsck.k9.logging.Timber
import com.fsck.k9.mail.ServerSettings
import com.fsck.k9.mail.store.imap.ImapStoreSettings.autoDetectNamespace
@ -15,6 +19,7 @@ import com.fsck.k9.mail.store.imap.ImapStoreSettings.createExtra
import com.fsck.k9.mail.store.imap.ImapStoreSettings.isSendClientId
import com.fsck.k9.mail.store.imap.ImapStoreSettings.isUseCompression
import com.fsck.k9.mail.store.imap.ImapStoreSettings.pathPrefix
import com.fsck.k9.mailstore.SpecialFolderUpdater
import com.fsck.k9.mailstore.SpecialLocalFoldersCreator
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
@ -27,6 +32,7 @@ class AccountCreator(
private val localFoldersCreator: SpecialLocalFoldersCreator,
private val preferences: Preferences,
private val context: Context,
private val messagingController: MessagingController,
private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO,
) : AccountSetupExternalContract.AccountCreator {
@ -67,14 +73,65 @@ class AccountCreator(
localFoldersCreator.createSpecialLocalFolders(newAccount)
account.specialFolderSettings?.let { specialFolderSettings ->
newAccount.setSpecialFolders(specialFolderSettings)
}
newAccount.markSetupFinished()
preferences.saveAccount(newAccount)
Core.setServicesEnabled(context)
messagingController.refreshFolderListBlocking(newAccount)
return newAccount.uuid
}
/**
* Set special folders by name.
*
* Since the folder list hasn't been synced yet, we don't have database IDs for the folders. So we use the same
* mechanism that is used when importing settings. See [SpecialFolderUpdater] for details.
*/
private fun K9Account.setSpecialFolders(specialFolders: SpecialFolderSettings) {
importedArchiveFolder = specialFolders.archiveSpecialFolderOption.toFolderServerId()
archiveFolderSelection = specialFolders.archiveSpecialFolderOption.toFolderSelection()
importedDraftsFolder = specialFolders.draftsSpecialFolderOption.toFolderServerId()
draftsFolderSelection = specialFolders.draftsSpecialFolderOption.toFolderSelection()
importedSentFolder = specialFolders.sentSpecialFolderOption.toFolderServerId()
sentFolderSelection = specialFolders.sentSpecialFolderOption.toFolderSelection()
importedSpamFolder = specialFolders.spamSpecialFolderOption.toFolderServerId()
spamFolderSelection = specialFolders.spamSpecialFolderOption.toFolderSelection()
importedTrashFolder = specialFolders.trashSpecialFolderOption.toFolderServerId()
trashFolderSelection = specialFolders.trashSpecialFolderOption.toFolderSelection()
}
private fun SpecialFolderOption.toFolderServerId(): String? {
return when (this) {
is SpecialFolderOption.None -> null
is SpecialFolderOption.Regular -> remoteFolder.serverId.serverId
is SpecialFolderOption.Special -> remoteFolder.serverId.serverId
}
}
private fun SpecialFolderOption.toFolderSelection(): SpecialFolderSelection {
return when (this) {
is SpecialFolderOption.None -> {
if (isAutomatic) SpecialFolderSelection.AUTOMATIC else SpecialFolderSelection.MANUAL
}
is SpecialFolderOption.Regular -> {
SpecialFolderSelection.MANUAL
}
is SpecialFolderOption.Special -> {
if (isAutomatic) SpecialFolderSelection.AUTOMATIC else SpecialFolderSelection.MANUAL
}
}
}
}
private fun K9Account.setIncomingServerSettings(serverSettings: ServerSettings) {

View file

@ -26,6 +26,7 @@ val newAccountModule = module {
localFoldersCreator = get(),
preferences = get(),
context = androidApplication(),
messagingController = get(),
)
}