Skip to content

Commit 6610cbd

Browse files
authored
Merge pull request #9213 from shamim-emon/fix-issue-9134
feat: remove gmail prefix in folder structure
2 parents 15a7aea + fee8797 commit 6610cbd

File tree

5 files changed

+90
-4
lines changed

5 files changed

+90
-4
lines changed

legacy/core/src/test/java/com/fsck/k9/mailstore/K9BackendDefaultStorageTest.kt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import assertk.assertions.isEqualTo
77
import com.fsck.k9.K9RobolectricTest
88
import com.fsck.k9.Preferences
99
import com.fsck.k9.backend.api.BackendStorage
10+
import com.fsck.k9.mail.AuthType
11+
import com.fsck.k9.mail.ConnectionSecurity
12+
import com.fsck.k9.mail.ServerSettings
1013
import net.thunderbird.core.android.account.LegacyAccount
1114
import org.junit.After
1215
import org.junit.Test
@@ -67,14 +70,30 @@ class K9BackendDefaultStorageTest : K9RobolectricTest() {
6770
// FIXME: This is a hack to get Preferences into a state where it's safe to call newAccount()
6871
preferences.clearAccounts()
6972

70-
return preferences.newAccount()
73+
return preferences.newAccount().apply {
74+
incomingServerSettings = SERVER_SETTINGS
75+
outgoingServerSettings = SERVER_SETTINGS
76+
}
7177
}
7278

7379
private fun createBackendStorage(): BackendStorage {
7480
val messageStore = messageStoreManager.getMessageStore(account)
7581
val folderSettingsProvider = createFolderSettingsProvider()
7682
return K9BackendStorage(messageStore, folderSettingsProvider, saveMessageDataCreator, emptyList())
7783
}
84+
85+
companion object {
86+
private val SERVER_SETTINGS = ServerSettings(
87+
type = "irrelevant",
88+
host = "irrelevant",
89+
port = 993,
90+
connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED,
91+
authenticationType = AuthType.PLAIN,
92+
username = "username",
93+
password = null,
94+
clientCertificateAlias = null,
95+
)
96+
}
7897
}
7998

8099
internal fun createFolderSettingsProvider(): FolderSettingsProvider {

legacy/core/src/test/java/com/fsck/k9/mailstore/K9BackendFolderTest.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ import com.fsck.k9.backend.api.BackendFolder
1616
import com.fsck.k9.backend.api.FolderInfo
1717
import com.fsck.k9.backend.api.updateFolders
1818
import com.fsck.k9.mail.Address
19+
import com.fsck.k9.mail.AuthType
20+
import com.fsck.k9.mail.ConnectionSecurity
1921
import com.fsck.k9.mail.Flag
2022
import com.fsck.k9.mail.FolderType
2123
import com.fsck.k9.mail.Message
2224
import com.fsck.k9.mail.MessageDownloadState
25+
import com.fsck.k9.mail.ServerSettings
2326
import com.fsck.k9.mail.internet.MimeMessage
2427
import com.fsck.k9.mail.internet.MimeMessageHelper
2528
import com.fsck.k9.mail.internet.TextBody
@@ -97,8 +100,10 @@ class K9BackendFolderTest : K9RobolectricTest() {
97100
fun createAccount(): LegacyAccount {
98101
// FIXME: This is a hack to get Preferences into a state where it's safe to call newAccount()
99102
preferences.clearAccounts()
100-
101-
return preferences.newAccount()
103+
return preferences.newAccount().apply {
104+
incomingServerSettings = SERVER_SETTINGS
105+
outgoingServerSettings = SERVER_SETTINGS
106+
}
102107
}
103108

104109
fun createBackendFolder(): BackendFolder {
@@ -158,5 +163,16 @@ class K9BackendFolderTest : K9RobolectricTest() {
158163
const val FOLDER_NAME = "Test Folder"
159164
val FOLDER_TYPE = FolderType.INBOX
160165
const val MESSAGE_SERVER_ID = "msg001"
166+
167+
private val SERVER_SETTINGS = ServerSettings(
168+
type = "irrelevant",
169+
host = "irrelevant",
170+
port = 993,
171+
connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED,
172+
authenticationType = AuthType.PLAIN,
173+
username = "username",
174+
password = null,
175+
clientCertificateAlias = null,
176+
)
161177
}
162178
}

legacy/storage/src/main/java/com/fsck/k9/storage/messages/CreateFolderOperations.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal class CreateFolderOperations(private val lockableDatabase: LockableData
1111
for (folder in folders) {
1212
val folderSettings = folder.settings
1313
val values = ContentValues().apply {
14-
put("name", folder.name)
14+
put("name", folder.name.replace("\\[(Gmail|Google Mail)]/".toRegex(), ""))
1515
put("visible_limit", folderSettings.visibleLimit)
1616
put("integrate", folderSettings.integrate)
1717
put("top_group", folderSettings.inTopGroup)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fsck.k9.storage.messages
2+
3+
import android.content.ContentValues
4+
import com.fsck.k9.mailstore.LockableDatabase
5+
6+
internal class FolderNameSanitizer(private val lockableDatabase: LockableDatabase) {
7+
fun removeGmailPrefixFromFolders() {
8+
lockableDatabase.execute(false) { db ->
9+
val cursor = db.query(
10+
"folders",
11+
arrayOf("id", "name"),
12+
"name LIKE ? OR name LIKE ?",
13+
arrayOf("%[Gmail]/%", "%[Google Mail]/%"),
14+
null,
15+
null,
16+
null,
17+
)
18+
19+
while (cursor.moveToNext()) {
20+
val id = cursor.getLong(cursor.getColumnIndexOrThrow("id"))
21+
val name = cursor.getString(cursor.getColumnIndexOrThrow("name"))
22+
val updatedName = name
23+
.replace("[Gmail]/", "")
24+
.replace("[Google Mail]/", "")
25+
26+
val values = ContentValues().apply {
27+
put("name", updatedName)
28+
}
29+
30+
db.update("folders", values, "id = ?", arrayOf(id.toString()))
31+
}
32+
33+
cursor.close()
34+
}
35+
}
36+
}

legacy/storage/src/main/java/com/fsck/k9/storage/messages/K9MessageStoreFactory.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,18 @@ class K9MessageStoreFactory(
1313
private val storageFilesProviderFactory: StorageFilesProviderFactory,
1414
private val basicPartInfoExtractor: BasicPartInfoExtractor,
1515
) : MessageStoreFactory {
16+
private lateinit var folderNameSanitizer: FolderNameSanitizer
17+
1618
override fun create(account: LegacyAccount): ListenableMessageStore {
1719
val localStore = localStoreProvider.getInstance(account)
20+
if (account.incomingServerSettings.host.isGoogle() ||
21+
account.outgoingServerSettings.host.isGoogle()
22+
) {
23+
if (!this::folderNameSanitizer.isInitialized) {
24+
folderNameSanitizer = FolderNameSanitizer(lockableDatabase = localStore.database)
25+
}
26+
folderNameSanitizer.removeGmailPrefixFromFolders()
27+
}
1828
val storageFilesProvider = storageFilesProviderFactory.createStorageFilesProvider(account.uuid)
1929
val messageStore = K9MessageStore(
2030
localStore.database,
@@ -25,3 +35,8 @@ class K9MessageStoreFactory(
2535
return ListenableMessageStore(notifierMessageStore)
2636
}
2737
}
38+
39+
private fun String.isGoogle(): Boolean {
40+
val domains = listOf(".gmail.com", ".googlemail.com")
41+
return domains.any { this.endsWith(it, ignoreCase = true) }
42+
}

0 commit comments

Comments
 (0)