Skip to content

Commit

Permalink
Merge pull request #8541 from shamim-emon/fix-issue-8531
Browse files Browse the repository at this point in the history
Fix of "QR code import does not update unified inbox"
  • Loading branch information
cketti authored Nov 16, 2024
2 parents fe05b40 + 6062bf9 commit 6d759cf
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import app.k9mail.feature.account.setup.AccountSetupExternalContract
import app.k9mail.feature.account.setup.AccountSetupExternalContract.AccountCreator.AccountCreatorResult
import app.k9mail.legacy.account.Account.SpecialFolderSelection
import com.fsck.k9.Core
import com.fsck.k9.K9
import com.fsck.k9.Preferences
import com.fsck.k9.controller.MessagingController
import com.fsck.k9.logging.Timber
Expand All @@ -21,6 +20,7 @@ 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 com.fsck.k9.preferences.UnifiedInboxConfigurator
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
Expand All @@ -35,6 +35,7 @@ class AccountCreator(
private val messagingController: MessagingController,
private val deletePolicyProvider: DeletePolicyProvider,
private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO,
private val unifiedInboxConfigurator: UnifiedInboxConfigurator,
) : AccountSetupExternalContract.AccountCreator {

@Suppress("TooGenericExceptionCaught")
Expand Down Expand Up @@ -81,10 +82,7 @@ class AccountCreator(

preferences.saveAccount(newAccount)

if (preferences.getAccounts().size > 1) {
K9.isShowUnifiedInbox = true
K9.saveSettingsAsync()
}
unifiedInboxConfigurator.configureUnifiedInbox()

Core.setServicesEnabled(context)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ val newAccountModule = module {
context = androidApplication(),
deletePolicyProvider = get(),
messagingController = get(),
unifiedInboxConfigurator = get(),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ val preferencesModule = module {
)
}

factory { UnifiedInboxConfigurator(accountManager = get()) }

factory {
SettingsImporter(
settingsFileParser = get(),
Expand All @@ -66,6 +68,7 @@ val preferencesModule = module {
accountSettingsUpgrader = get(),
generalSettingsWriter = get(),
accountSettingsWriter = get(),
unifiedInboxConfigurator = get(),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.fsck.k9.preferences.Settings.InvalidSettingValueException
import java.io.InputStream
import timber.log.Timber

@Suppress("LongParameterList")
class SettingsImporter internal constructor(
private val settingsFileParser: SettingsFileParser,
private val generalSettingsValidator: GeneralSettingsValidator,
Expand All @@ -17,6 +18,7 @@ class SettingsImporter internal constructor(
private val accountSettingsUpgrader: AccountSettingsUpgrader,
private val generalSettingsWriter: GeneralSettingsWriter,
private val accountSettingsWriter: AccountSettingsWriter,
private val unifiedInboxConfigurator: UnifiedInboxConfigurator,
) {
/**
* Parses an import [InputStream] and returns information on whether it contains global settings and/or account
Expand Down Expand Up @@ -100,6 +102,10 @@ class SettingsImporter internal constructor(
}
}

if (!globalSettingsImported) {
unifiedInboxConfigurator.configureUnifiedInbox()
}

return ImportResults(globalSettingsImported, importedAccounts, erroneousAccounts)
} catch (e: SettingsImportExportException) {
throw e
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.fsck.k9.preferences

import app.k9mail.legacy.account.AccountManager
import com.fsck.k9.K9

/**
* Configures the unified inbox after an account has been added.
*/
class UnifiedInboxConfigurator(
private val accountManager: AccountManager,
) {
fun configureUnifiedInbox() {
if (accountManager.getAccounts().size > 1) {
K9.isShowUnifiedInbox = true
K9.saveSettingsAsync()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fsck.k9.preferences

import app.k9mail.legacy.di.DI.get
import assertk.all
import assertk.assertFailure
import assertk.assertThat
Expand All @@ -16,10 +17,23 @@ import com.fsck.k9.Preferences
import java.util.UUID
import org.junit.Before
import org.junit.Test
import org.koin.test.inject
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.times
import org.mockito.kotlin.verify

class SettingsImporterTest : K9RobolectricTest() {
private val settingsImporter: SettingsImporter by inject()
private val unifiedInboxConfigurator = mock<UnifiedInboxConfigurator>()
private val settingsImporter = SettingsImporter(
settingsFileParser = get(),
generalSettingsValidator = get(),
accountSettingsValidator = get(),
generalSettingsUpgrader = get(),
accountSettingsWriter = get(),
accountSettingsUpgrader = get(),
generalSettingsWriter = get(),
unifiedInboxConfigurator = unifiedInboxConfigurator,
)

@Before
fun before() {
Expand Down Expand Up @@ -154,6 +168,93 @@ class SettingsImporterTest : K9RobolectricTest() {
}
}

@Test
fun `importSettings() configures unifiedInbox when globalSettingsImported is false`() {
val accountUuid = UUID.randomUUID().toString()
val inputStream =
"""
<k9settings format="1" version="1">
<accounts>
<account uuid="$accountUuid">
<name>Account</name>
<incoming-server type="IMAP">
<connection-security>SSL_TLS_REQUIRED</connection-security>
<username>[email protected]</username>
<authentication-type>CRAM_MD5</authentication-type>
<host>googlemail.com</host>
</incoming-server>
<outgoing-server type="SMTP">
<connection-security>SSL_TLS_REQUIRED</connection-security>
<username>[email protected]</username>
<authentication-type>CRAM_MD5</authentication-type>
<host>googlemail.com</host>
</outgoing-server>
<settings>
<value key="a">b</value>
</settings>
<identities>
<identity>
<email>[email protected]</email>
</identity>
</identities>
</account>
</accounts>
</k9settings>
""".trimIndent().byteInputStream()
val accountUuids = listOf("uuid-1")

val results = settingsImporter.importSettings(inputStream, globalSettings = false, accountUuids)

assertThat(results.globalSettings).isFalse()
verify(unifiedInboxConfigurator, times(1)).configureUnifiedInbox()
}

@Test
fun `importSettings() does not not configure unifiedInbox when globalSettingsImported is true`() {
val accountUuid = UUID.randomUUID().toString()
val inputStream =
"""
<k9settings format="1" version="101">
<global>
<value key="confirmDelete">false</value>
<value key="changeRegisteredNameColor">false</value>
<value key="confirmSpam">false</value>
</global>
<accounts>
<account uuid="$accountUuid">
<name>Account</name>
<incoming-server type="IMAP">
<connection-security>SSL_TLS_REQUIRED</connection-security>
<username>[email protected]</username>
<authentication-type>CRAM_MD5</authentication-type>
<host>googlemail.com</host>
</incoming-server>
<outgoing-server type="SMTP">
<connection-security>SSL_TLS_REQUIRED</connection-security>
<username>[email protected]</username>
<authentication-type>CRAM_MD5</authentication-type>
<host>googlemail.com</host>
</outgoing-server>
<settings>
<value key="a">b</value>
</settings>
<identities>
<identity>
<email>[email protected]</email>
</identity>
</identities>
</account>
</accounts>
</k9settings>
""".trimIndent().byteInputStream()
val accountUuids = listOf("uuid-1")

val results = settingsImporter.importSettings(inputStream, globalSettings = true, accountUuids)

assertThat(results.globalSettings).isTrue()
verify(unifiedInboxConfigurator, never()).configureUnifiedInbox()
}

@Test
fun `getImportStreamContents() should return list of accounts`() {
val accountUuid = UUID.randomUUID().toString()
Expand Down

0 comments on commit 6d759cf

Please sign in to comment.