Return detailed information on imported accounts

This commit is contained in:
cketti 2011-04-29 04:29:16 +02:00
parent 71f423d029
commit 7e24377bb8
2 changed files with 82 additions and 20 deletions

View file

@ -73,6 +73,7 @@ import com.fsck.k9.preferences.StorageImportExportException;
import com.fsck.k9.preferences.StorageImporter;
import com.fsck.k9.preferences.StorageImporter.AccountDescription;
import com.fsck.k9.preferences.StorageImporter.ImportContents;
import com.fsck.k9.preferences.StorageImporter.ImportResults;
public class Accounts extends K9ListActivity implements OnItemClickListener, OnClickListener {
@ -1166,6 +1167,7 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
private boolean mOverwrite;
private String mEncryptionKey;
private InputStream mInputStream;
private ImportResults mImportResults;
private ImportAsyncTask(boolean includeGlobals, List<String> accountUuids,
boolean overwrite, String encryptionKey, InputStream is) {
@ -1187,8 +1189,8 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
@Override
protected Boolean doInBackground(Void... params) {
try {
StorageImporter.importSettings(Accounts.this, mInputStream, mEncryptionKey,
mIncludeGlobals, mAccountUuids, mOverwrite);
mImportResults = StorageImporter.importSettings(Accounts.this, mInputStream,
mEncryptionKey, mIncludeGlobals, mAccountUuids, mOverwrite);
} catch (StorageImportExportException e) {
Log.w(K9.LOG_TAG, "Exception during export", e);
return false;
@ -1199,9 +1201,13 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
@Override
protected void onPostExecute(Boolean success) {
if (success) {
int imported = mImportResults.importedAccounts.size();
//TODO: display names of imported accounts (name from file *and* possibly new name)
showDialog(Accounts.this, R.string.settings_import_success_header,
//FIXME: use correct number of imported accounts
Accounts.this.getString(R.string.settings_import_success, 3, "unknown"));
//FIXME: use correct file name
Accounts.this.getString(R.string.settings_import_success, imported, "filename"));
refresh();
} else {
//TODO: make the importer return an error code; translate that error code to a localized string here

View file

@ -70,6 +70,30 @@ public class StorageImporter {
}
}
public static class AccountDescriptionPair {
public final AccountDescription original;
public final AccountDescription imported;
private AccountDescriptionPair(AccountDescription original, AccountDescription imported) {
this.original = original;
this.imported = imported;
}
}
public static class ImportResults {
public final boolean globalSettings;
public final List<AccountDescriptionPair> importedAccounts;
public final List<AccountDescription> errorneousAccounts;
private ImportResults(boolean globalSettings,
List<AccountDescriptionPair> importedAccounts,
List<AccountDescription> errorneousAccounts) {
this.globalSettings = globalSettings;
this.importedAccounts = importedAccounts;
this.errorneousAccounts = errorneousAccounts;
}
}
public static boolean isImportStreamEncrypted(Context context, InputStream inputStream) {
return false;
}
@ -127,23 +151,34 @@ public class StorageImporter {
* @param overwrite
* @throws StorageImportExportException
*/
public static void importSettings(Context context, InputStream inputStream, String encryptionKey,
public static ImportResults importSettings(Context context, InputStream inputStream, String encryptionKey,
boolean globalSettings, List<String> accountUuids, boolean overwrite)
throws StorageImportExportException {
try
{
boolean globalSettingsImported = false;
List<AccountDescriptionPair> importedAccounts = new ArrayList<AccountDescriptionPair>();
List<AccountDescription> errorneousAccounts = new ArrayList<AccountDescription>();
Imported imported = parseSettings(inputStream, globalSettings, accountUuids, overwrite, false);
Preferences preferences = Preferences.getPreferences(context);
SharedPreferences storage = preferences.getPreferences();
SharedPreferences.Editor editor = storage.edit();
if (globalSettings) {
if (imported.globalSettings != null) {
importGlobalSettings(editor, imported.globalSettings);
} else {
Log.w(K9.LOG_TAG, "Was asked to import global settings but none found.");
try {
SharedPreferences.Editor editor = storage.edit();
if (imported.globalSettings != null) {
importGlobalSettings(editor, imported.globalSettings);
} else {
Log.w(K9.LOG_TAG, "Was asked to import global settings but none found.");
}
if (editor.commit()) {
globalSettingsImported = true;
}
} catch (Exception e) {
Log.e(K9.LOG_TAG, "Exception while importing global settings", e);
}
}
@ -152,9 +187,24 @@ public class StorageImporter {
List<String> newUuids = new ArrayList<String>();
for (String accountUuid : accountUuids) {
if (imported.accounts.containsKey(accountUuid)) {
String newUuid = importAccount(context, editor, imported.accounts.get(accountUuid), overwrite);
if (newUuid != null) {
newUuids.add(newUuid);
ImportedAccount account = imported.accounts.get(accountUuid);
try {
SharedPreferences.Editor editor = storage.edit();
AccountDescriptionPair importResult = importAccount(context,
editor, account, overwrite);
String newUuid = importResult.imported.uuid;
if (!newUuid.equals(importResult.original.uuid)) {
newUuids.add(newUuid);
}
if (editor.commit()) {
importedAccounts.add(importResult);
} else {
errorneousAccounts.add(importResult.original);
}
} catch (Exception e) {
errorneousAccounts.add(new AccountDescription(account.name, account.uuid));
}
} else {
Log.w(K9.LOG_TAG, "Was asked to import account with UUID " +
@ -162,6 +212,8 @@ public class StorageImporter {
}
}
SharedPreferences.Editor editor = storage.edit();
if (newUuids.size() > 0) {
String oldAccountUuids = storage.getString("accountUuids", "");
String appendUuids = Utility.combine(newUuids.toArray(new String[0]), ',');
@ -177,20 +229,21 @@ public class StorageImporter {
editor.putString("defaultAccountUuid", accountUuids.get(0));
}
if (!editor.commit()) {
throw new StorageImportExportException("Failed to set default account");
}
} else {
Log.w(K9.LOG_TAG, "Was asked to import at least one account but none found.");
}
}
if (!editor.commit()) {
throw new StorageImportExportException("Couldn't save imported settings");
}
preferences.loadAccounts();
DateFormatter.clearChosenFormat();
K9.loadPrefs(preferences);
K9.setServicesEnabled(context);
return new ImportResults(globalSettingsImported, importedAccounts, errorneousAccounts);
} catch (StorageImportExportException e) {
throw e;
} catch (Exception e) {
@ -211,8 +264,10 @@ public class StorageImporter {
}
}
private static String importAccount(Context context, SharedPreferences.Editor editor,
ImportedAccount account, boolean overwrite) {
private static AccountDescriptionPair importAccount(Context context,
SharedPreferences.Editor editor, ImportedAccount account, boolean overwrite) {
AccountDescription original = new AccountDescription(account.name, account.uuid);
// Validate input and ignore malformed values when possible
Map<String, String> validatedSettings =
@ -280,7 +335,8 @@ public class StorageImporter {
//TODO: sync folder settings with localstore?
return (overwrite && existingAccount != null) ? null : uuid;
AccountDescription imported = new AccountDescription(accountName, uuid);
return new AccountDescriptionPair(original, imported);
}
private static void importIdentities(SharedPreferences.Editor editor, String uuid,