diff --git a/src/com/fsck/k9/mail/store/LocalStore.java b/src/com/fsck/k9/mail/store/LocalStore.java index 3b0137a00..d742c6a8b 100644 --- a/src/com/fsck/k9/mail/store/LocalStore.java +++ b/src/com/fsck/k9/mail/store/LocalStore.java @@ -88,15 +88,6 @@ public class LocalStore extends Store implements Serializable { HEADERS_TO_SAVE.add("User-Agent"); } - public static final Set FOLDER_SETTINGS_KEYS = new HashSet(); - static { - FOLDER_SETTINGS_KEYS.add("displayMode"); - FOLDER_SETTINGS_KEYS.add("syncMode"); - FOLDER_SETTINGS_KEYS.add("pushMode"); - FOLDER_SETTINGS_KEYS.add("inTopGroup"); - FOLDER_SETTINGS_KEYS.add("integrate"); - } - /* * a String containing the columns getMessages expects to work with * in the correct order. diff --git a/src/com/fsck/k9/preferences/FolderSettings.java b/src/com/fsck/k9/preferences/FolderSettings.java new file mode 100644 index 000000000..1a2af5502 --- /dev/null +++ b/src/com/fsck/k9/preferences/FolderSettings.java @@ -0,0 +1,44 @@ +package com.fsck.k9.preferences; + +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; +import android.content.SharedPreferences; + +import com.fsck.k9.mail.Folder.FolderClass; +import com.fsck.k9.preferences.Settings.*; + +public class FolderSettings { + public static final Map SETTINGS; + + static { + Map s = new LinkedHashMap(); + + s.put("displayMode", new EnumSetting(FolderClass.class, FolderClass.NO_CLASS)); + s.put("syncMode", new EnumSetting(FolderClass.class, FolderClass.INHERITED)); + s.put("pushMode", new EnumSetting(FolderClass.class, FolderClass.INHERITED)); + s.put("inTopGroup", new BooleanSetting(false)); + s.put("integrate", new BooleanSetting(false)); + + SETTINGS = Collections.unmodifiableMap(s); + } + + public static Map validate(Map importedSettings, + boolean useDefaultValues) { + return Settings.validate(SETTINGS, importedSettings, useDefaultValues); + } + + public static Map getFolderSettings(SharedPreferences storage, String uuid, + String folderName) { + Map result = new HashMap(); + String prefix = uuid + "." + folderName + "."; + for (String key : SETTINGS.keySet()) { + String value = storage.getString(prefix + key, null); + if (value != null) { + result.put(key, value); + } + } + return result; + } +} diff --git a/src/com/fsck/k9/preferences/StorageExporter.java b/src/com/fsck/k9/preferences/StorageExporter.java index 0134e947c..b9de982e7 100644 --- a/src/com/fsck/k9/preferences/StorageExporter.java +++ b/src/com/fsck/k9/preferences/StorageExporter.java @@ -28,7 +28,6 @@ import com.fsck.k9.helper.Utility; import com.fsck.k9.mail.Store; import com.fsck.k9.mail.ServerSettings; import com.fsck.k9.mail.Transport; -import com.fsck.k9.mail.store.LocalStore; import com.fsck.k9.preferences.Settings.InvalidSettingValueException; import com.fsck.k9.preferences.Settings.SettingsDescription; @@ -304,7 +303,7 @@ public class StorageExporter { continue; } - if (LocalStore.FOLDER_SETTINGS_KEYS.contains(thirdPart)) { + if (FolderSettings.SETTINGS.containsKey(thirdPart)) { // This is a folder key. Save folder name for later... folders.add(secondPart); // ... but don't write it now. @@ -429,25 +428,42 @@ public class StorageExporter { serializer.startTag(null, FOLDER_ELEMENT); serializer.attribute(null, NAME_ATTRIBUTE, folder); + + // Write folder settings for (Map.Entry entry : prefs.entrySet()) { String key = entry.getKey(); - String value = entry.getValue().toString(); + String valueString = entry.getValue().toString(); String[] comps = key.split("\\."); - if (comps.length >= 3) { - String keyUuid = comps[0]; - String folderName = comps[1]; - String folderKey = comps[2]; - if (!keyUuid.equals(accountUuid) || !folderName.equals(folder) - || !LocalStore.FOLDER_SETTINGS_KEYS.contains(folderKey)) { - continue; - } - } else { + + if (comps.length < 3) { // Skip non-folder config entries continue; } - writeKeyValue(serializer, comps[2], value); + String keyUuid = comps[0]; + String folderName = comps[1]; + String folderKey = comps[2]; + + if (!keyUuid.equals(accountUuid) || !folderName.equals(folder)) { + // Skip entries that belong to another folder + continue; + } + + SettingsDescription setting = FolderSettings.SETTINGS.get(folderKey); + if (setting != null) { + // Only write settings that have an entry in FolderSettings.SETTINGS + try { + Object value = setting.fromString(valueString); + String outputValue = setting.toPrettyString(value); + writeKeyValue(serializer, folderKey, outputValue); + } catch (InvalidSettingValueException e) { + Log.w(K9.LOG_TAG, "Folder setting \"" + folderKey + + "\" has invalid value \"" + valueString + + "\" in preference storage. This shouldn't happen!"); + } + } } + serializer.endTag(null, FOLDER_ELEMENT); } diff --git a/src/com/fsck/k9/preferences/StorageImporter.java b/src/com/fsck/k9/preferences/StorageImporter.java index 3fff2fea1..8e415df7d 100644 --- a/src/com/fsck/k9/preferences/StorageImporter.java +++ b/src/com/fsck/k9/preferences/StorageImporter.java @@ -311,8 +311,6 @@ public class StorageImporter { //TODO: validate account name //TODO: validate server settings - //TODO: validate identity settings - //TODO: validate folder settings String accountName = account.name; @@ -360,12 +358,7 @@ public class StorageImporter { // Write folder settings if (account.folders != null) { for (ImportedFolder folder : account.folders) { - String folderKeyPrefix = uuid + "." + folder.name + "."; - for (Map.Entry setting : folder.settings.settings.entrySet()) { - String key = folderKeyPrefix + setting.getKey(); - String value = setting.getValue(); - editor.putString(key, value); - } + importFolder(editor, uuid, folder, mergeImportedAccount, prefs); } } @@ -375,6 +368,33 @@ public class StorageImporter { return new AccountDescriptionPair(original, imported); } + private static void importFolder(SharedPreferences.Editor editor, String uuid, + ImportedFolder folder, boolean overwrite, Preferences prefs) { + + // Validate folder settings + Map validatedFolderSettings = + FolderSettings.validate(folder.settings.settings, !overwrite); + + // Merge folder settings if necessary + Map writeFolderSettings; + if (overwrite) { + writeFolderSettings = FolderSettings.getFolderSettings(prefs.getPreferences(), + uuid, folder.name); + writeFolderSettings.putAll(validatedFolderSettings); + } else { + writeFolderSettings = new HashMap(validatedFolderSettings); + } + + // Write folder settings + String folderKeyPrefix = uuid + "." + folder.name + "."; + for (Map.Entry setting : writeFolderSettings.entrySet()) { + String key = folderKeyPrefix + setting.getKey(); + String value = setting.getValue(); + Log.v(K9.LOG_TAG, "Writing " + key + "=" + value); + editor.putString(key, value); + } + } + private static void importIdentities(SharedPreferences.Editor editor, String uuid, ImportedAccount account, boolean overwrite, Account existingAccount, Preferences prefs) throws InvalidSettingValueException { @@ -440,25 +460,27 @@ public class StorageImporter { editor.putString(accountKeyPrefix + Account.IDENTITY_DESCRIPTION_KEY + identitySuffix, identityDescription); - // Validate identity settings - Map validatedSettings = IdentitySettings.validate( - identity.settings.settings, !mergeSettings); + if (identity.settings != null) { + // Validate identity settings + Map validatedSettings = IdentitySettings.validate( + identity.settings.settings, !mergeSettings); - // Merge identity settings if necessary - Map writeSettings; - if (mergeSettings) { - writeSettings = new HashMap(IdentitySettings.getIdentitySettings( - prefs.getPreferences(), uuid, writeIdentityIndex)); - writeSettings.putAll(validatedSettings); - } else { - writeSettings = new HashMap(validatedSettings); - } + // Merge identity settings if necessary + Map writeSettings; + if (mergeSettings) { + writeSettings = new HashMap(IdentitySettings.getIdentitySettings( + prefs.getPreferences(), uuid, writeIdentityIndex)); + writeSettings.putAll(validatedSettings); + } else { + writeSettings = new HashMap(validatedSettings); + } - // Write identity settings - for (Map.Entry setting : writeSettings.entrySet()) { - String key = accountKeyPrefix + setting.getKey() + identitySuffix; - String value = setting.getValue(); - editor.putString(key, value); + // Write identity settings + for (Map.Entry setting : writeSettings.entrySet()) { + String key = accountKeyPrefix + setting.getKey() + identitySuffix; + String value = setting.getValue(); + editor.putString(key, value); + } } } }