Skip to content

Commit

Permalink
Merge pull request #7240 from thundernest/backport_identity_fixes
Browse files Browse the repository at this point in the history
Backport settings import/export fixes
  • Loading branch information
cketti authored Oct 12, 2023
2 parents 315cabd + d8c7ff0 commit b1e9ca6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class SettingsExporter(
if (hasThirdPart) {
val secondPart = keyPart.substring(0, indexOfLastDot)
val thirdPart = keyPart.substring(indexOfLastDot + 1)
if (secondPart == IDENTITY_DESCRIPTION_KEY) {
if (secondPart == IDENTITY_EMAIL_KEY) {
// This is an identity key. Save identity index for later...
thirdPart.toIntOrNull()?.let {
identities.add(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,19 +540,6 @@ private static void importIdentities(StorageEditor editor, int contentVersion, S
nextIdentityIndex++;
}

String identityDescription = (identity.description == null) ? "Imported" : identity.description;
if (isIdentityDescriptionUsed(identityDescription, existingIdentities)) {
// Identity description is already in use. So generate a new one by appending
// " (x)", where x is the first number >= 1 that results in an unused identity
// description.
for (int i = 1; i <= existingIdentities.size(); i++) {
identityDescription = identity.description + " (" + i + ")";
if (!isIdentityDescriptionUsed(identityDescription, existingIdentities)) {
break;
}
}
}

String identitySuffix = "." + writeIdentityIndex;

// Write name used in identity
Expand All @@ -568,8 +555,10 @@ private static void importIdentities(StorageEditor editor, int contentVersion, S
putString(editor, accountKeyPrefix + AccountPreferenceSerializer.IDENTITY_EMAIL_KEY + identitySuffix, identity.email);

// Write identity description
putString(editor, accountKeyPrefix + AccountPreferenceSerializer.IDENTITY_DESCRIPTION_KEY + identitySuffix,
identityDescription);
if (identity.description != null) {
putString(editor, accountKeyPrefix + AccountPreferenceSerializer.IDENTITY_DESCRIPTION_KEY + identitySuffix,
identity.description);
}

if (identity.settings != null) {
// Validate identity settings
Expand Down Expand Up @@ -617,15 +606,6 @@ private static boolean isAccountNameUsed(String name, List<Account> accounts) {
return false;
}

private static boolean isIdentityDescriptionUsed(String description, List<Identity> identities) {
for (Identity identity : identities) {
if (identity.getDescription().equals(description)) {
return true;
}
}
return false;
}

private static int findIdentity(ImportedIdentity identity, List<Identity> identities) {
for (int i = 0; i < identities.size(); i++) {
Identity existingIdentity = identities.get(i);
Expand Down
3 changes: 1 addition & 2 deletions config/detekt/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
<ID>LoopWithTooManyJumpStatements:HttpUriParser.kt$HttpUriParser$while (currentPos &lt; text.length) { val c = text[currentPos] if (isHexDigit(c)) { shouldBeHex = (shouldBeHex - 1).coerceAtLeast(0) } else if (shouldBeHex == 0) { if (c in allowedCharacters) { // Everything ok here :) } else if (c == '%') { shouldBeHex = 2 } else { break } } else { break } currentPos++ }</ID>
<ID>LoopWithTooManyJumpStatements:RealImapStore.kt$RealImapStore$for (listResponse in listResponses) { val serverId = listResponse.name if (pathDelimiter == null) { pathDelimiter = listResponse.hierarchyDelimiter combinedPrefix = null } if (RealImapFolder.INBOX.equals(serverId, ignoreCase = true)) { continue } else if (listResponse.hasAttribute("\\NoSelect")) { continue } val name = getFolderDisplayName(serverId) val oldServerId = getOldServerId(serverId) val type = when { listResponse.hasAttribute("\\Archive") -&gt; FolderType.ARCHIVE listResponse.hasAttribute("\\All") -&gt; FolderType.ARCHIVE listResponse.hasAttribute("\\Drafts") -&gt; FolderType.DRAFTS listResponse.hasAttribute("\\Sent") -&gt; FolderType.SENT listResponse.hasAttribute("\\Junk") -&gt; FolderType.SPAM listResponse.hasAttribute("\\Trash") -&gt; FolderType.TRASH else -&gt; FolderType.REGULAR } val existingItem = folderMap[serverId] if (existingItem == null || existingItem.type == FolderType.REGULAR) { folderMap[serverId] = FolderListItem(serverId, name, type, oldServerId) } }</ID>
<ID>LoopWithTooManyJumpStatements:SettingsExporter.kt$SettingsExporter$for ((key, value) in prefs) { val valueString = value.toString() val comps = key.split(".") if (comps.size &lt; 3) { // Skip non-identity config entries continue } val keyUuid = comps[0] val identityKey = comps[1] val identityIndex = comps[2] if (keyUuid != accountUuid || identityIndex != identity) { // Skip entries that belong to another identity continue } val versionedSetting = IdentitySettingsDescriptions.SETTINGS[identityKey] if (versionedSetting != null) { val highestVersion = versionedSetting.lastKey() val setting = versionedSetting[highestVersion] if (setting != null) { // Only write settings that have an entry in IdentitySettings.SETTINGS try { writeKeyAndPrettyValueFromSetting(serializer, identityKey, setting, valueString) } catch (e: InvalidSettingValueException) { Timber.w( "Identity setting \"%s\" has invalid value \"%s\" in preference storage. " + "This shouldn't happen!", identityKey, valueString, ) } } } }</ID>
<ID>LoopWithTooManyJumpStatements:SettingsExporter.kt$SettingsExporter$for ((key, value) in prefs) { val valueString = value.toString() val comps = key.split(".", limit = 2) if (comps.size &lt; 2) { // Skip global settings continue } val keyUuid = comps[0] val keyPart = comps[1] if (keyUuid != accountUuid) { // Setting doesn't belong to the account we're currently writing. continue } val indexOfLastDot = keyPart.lastIndexOf(".") val hasThirdPart = indexOfLastDot != -1 &amp;&amp; indexOfLastDot &lt; keyPart.length - 1 if (hasThirdPart) { val secondPart = keyPart.substring(0, indexOfLastDot) val thirdPart = keyPart.substring(indexOfLastDot + 1) if (secondPart == IDENTITY_DESCRIPTION_KEY) { // This is an identity key. Save identity index for later... thirdPart.toIntOrNull()?.let { identities.add(it) } // ... but don't write it now. continue } if (FolderSettingsDescriptions.SETTINGS.containsKey(thirdPart)) { // This is a folder key. Ignore it. continue } } if (keyPart !in FOLDER_NAME_KEYS) { writeAccountSettingIfValid(serializer, keyPart, valueString, account) } }</ID>
<ID>LoopWithTooManyJumpStatements:SettingsExporter.kt$SettingsExporter$for ((key, value) in prefs) { val valueString = value.toString() val comps = key.split(".", limit = 2) if (comps.size &lt; 2) { // Skip global settings continue } val keyUuid = comps[0] val keyPart = comps[1] if (keyUuid != accountUuid) { // Setting doesn't belong to the account we're currently writing. continue } val indexOfLastDot = keyPart.lastIndexOf(".") val hasThirdPart = indexOfLastDot != -1 &amp;&amp; indexOfLastDot &lt; keyPart.length - 1 if (hasThirdPart) { val secondPart = keyPart.substring(0, indexOfLastDot) val thirdPart = keyPart.substring(indexOfLastDot + 1) if (secondPart == IDENTITY_EMAIL_KEY) { // This is an identity key. Save identity index for later... thirdPart.toIntOrNull()?.let { identities.add(it) } // ... but don't write it now. continue } if (FolderSettingsDescriptions.SETTINGS.containsKey(thirdPart)) { // This is a folder key. Ignore it. continue } } if (keyPart !in FOLDER_NAME_KEYS) { writeAccountSettingIfValid(serializer, keyPart, valueString, account) } }</ID>
<ID>LoopWithTooManyJumpStatements:UnreadWidgetMigrations.kt$UnreadWidgetMigrations$for (widgetId in widgetIds) { val accountUuid = preferences.getString("unread_widget.$widgetId", null) ?: continue val account = accountRepository.getAccount(accountUuid) ?: continue val folderServerId = preferences.getString("unread_widget.$widgetId.folder_name", null) if (folderServerId != null) { val folderId = folderRepository.getFolderId(account, folderServerId) putString("unread_widget.$widgetId.folder_id", folderId?.toString()) } remove("unread_widget.$widgetId.folder_name") }</ID>
<ID>MagicNumber:Account.kt$Account$168</ID>
<ID>MagicNumber:Account.kt$Account$2</ID>
Expand Down Expand Up @@ -802,7 +802,6 @@
<ID>UnusedPrivateMember:HttpUriParser.kt$HttpUriParser$i</ID>
<ID>UnusedPrivateMember:MessageListLoader.kt$MessageListLoader$account: Account</ID>
<ID>UnusedPrivateMember:MessageViewFragment.kt$MessageViewFragment$requestKey: String</ID>
<ID>UnusedPrivateMember:NewSetupUiHack.kt$NewSetupUiHack$builder: SettingsListBuilder</ID>
<ID>UseCheckOrError:OutboxStateRepository.kt$OutboxStateRepository$throw IllegalStateException("No outbox_state entry for message with id $messageId")</ID>
<ID>UseCheckOrError:ThemeExtensions.kt$throw IllegalStateException("Couldn't resolve attribute ($attrId)")</ID>
<ID>UseRequire:MimeParameterEncoder.kt$MimeParameterEncoder$throw IllegalArgumentException("Unsupported character: $c")</ID>
Expand Down

0 comments on commit b1e9ca6

Please sign in to comment.