Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1f240455 authored by cketti's avatar cketti
Browse files

Migrate storeUri/transportUri to new server settings format

parent 08d6d9be
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ import timber.log.Timber;


public class K9StoragePersister implements StoragePersister {
    private static final int DB_VERSION = 11;
    private static final int DB_VERSION = 12;
    private static final String DB_NAME = "preferences_storage";

    private final Context context;
+65 −0
Original line number Diff line number Diff line
package com.fsck.k9.preferences.migrations

import android.database.sqlite.SQLiteDatabase
import com.fsck.k9.ServerSettingsSerializer
import com.fsck.k9.mail.filter.Base64
import com.fsck.k9.preferences.migrations.migration12.ImapStoreUriDecoder
import com.fsck.k9.preferences.migrations.migration12.Pop3StoreUriDecoder
import com.fsck.k9.preferences.migrations.migration12.SmtpTransportUriDecoder
import com.fsck.k9.preferences.migrations.migration12.WebDavStoreUriDecoder

/**
 * Convert server settings from the old URI format to the new JSON format
 */
class StorageMigrationTo12(
    private val db: SQLiteDatabase,
    private val migrationsHelper: StorageMigrationsHelper
) {
    private val serverSettingsSerializer = ServerSettingsSerializer()

    fun removeStoreAndTransportUri() {
        val accountUuidsListValue = migrationsHelper.readValue(db, "accountUuids")
        if (accountUuidsListValue == null || accountUuidsListValue.isEmpty()) {
            return
        }

        val accountUuids = accountUuidsListValue.split(",")
        for (accountUuid in accountUuids) {
            convertStoreUri(accountUuid)
            convertTransportUri(accountUuid)
        }
    }

    private fun convertStoreUri(accountUuid: String) {
        val storeUri = migrationsHelper.readValue(db, "$accountUuid.storeUri")?.base64Decode() ?: return

        val serverSettings = when {
            storeUri.startsWith("imap") -> ImapStoreUriDecoder.decode(storeUri)
            storeUri.startsWith("pop3") -> Pop3StoreUriDecoder.decode(storeUri)
            storeUri.startsWith("webdav") -> WebDavStoreUriDecoder.decode(storeUri)
            else -> error("Unsupported account type")
        }

        val json = serverSettingsSerializer.serialize(serverSettings)

        migrationsHelper.insertValue(db, "$accountUuid.incomingServerSettings", json)
        migrationsHelper.writeValue(db, "$accountUuid.storeUri", null)
    }

    private fun convertTransportUri(accountUuid: String) {
        val transportUri = migrationsHelper.readValue(db, "$accountUuid.transportUri")?.base64Decode() ?: return

        val serverSettings = when {
            transportUri.startsWith("smtp") -> SmtpTransportUriDecoder.decodeSmtpUri(transportUri)
            transportUri.startsWith("webdav") -> WebDavStoreUriDecoder.decode(transportUri)
            else -> error("Unsupported account type")
        }

        val json = serverSettingsSerializer.serialize(serverSettings)

        migrationsHelper.insertValue(db, "$accountUuid.outgoingServerSettings", json)
        migrationsHelper.writeValue(db, "$accountUuid.transportUri", null)
    }

    private fun String.base64Decode() = Base64.decode(this)
}
+1 −0
Original line number Diff line number Diff line
@@ -17,5 +17,6 @@ internal object StorageMigrations {
        if (oldVersion < 9) StorageMigrationTo9(db, migrationsHelper).disablePush()
        if (oldVersion < 10) StorageMigrationTo10(db, migrationsHelper).removeSavedFolderSettings()
        if (oldVersion < 11) StorageMigrationTo11(db, migrationsHelper).upgradeMessageViewContentFontSize()
        if (oldVersion < 12) StorageMigrationTo12(db, migrationsHelper).removeStoreAndTransportUri()
    }
}
+5 −3
Original line number Diff line number Diff line
package com.fsck.k9.backend.imap;
package com.fsck.k9.preferences.migrations.migration12;


import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

import com.fsck.k9.mail.AuthType;
import com.fsck.k9.mail.ConnectionSecurity;
import com.fsck.k9.mail.ServerSettings;
import com.fsck.k9.mail.store.imap.ImapStoreSettings;

import static com.fsck.k9.mail.helper.UrlEncodingHelper.decodeUtf8;

@@ -136,7 +136,9 @@ public class ImapStoreUriDecoder {
            }
        }

        Map<String, String> extra = ImapStoreSettings.createExtra(autoDetectNamespace, pathPrefix);
        Map<String, String> extra = new HashMap<>();
        extra.put("autoDetectNamespace", Boolean.toString(autoDetectNamespace));
        extra.put("pathPrefix", pathPrefix);

        return new ServerSettings("imap", host, port, connectionSecurity, authenticationType, username,
                password, clientCertificateAlias, extra);
+1 −1
Original line number Diff line number Diff line
package com.fsck.k9.backend.pop3;
package com.fsck.k9.preferences.migrations.migration12;


import java.net.URI;
Loading