automatically save settings when chaning vault properties. fixes #446

This commit is contained in:
Sebastian Stenzel
2017-02-02 11:31:00 +01:00
parent 02ae2e7ca0
commit f391a6521d
8 changed files with 42 additions and 34 deletions

View File

@@ -69,6 +69,7 @@ public class MainController extends LocalizedFXMLViewController {
private static final Logger LOG = LoggerFactory.getLogger(MainController.class);
private final Stage mainWindow;
private final Settings settings;
private final VaultFactory vaultFactoy;
private final Lazy<WelcomeController> welcomeController;
private final Lazy<InitializeController> initializeController;
@@ -95,6 +96,7 @@ public class MainController extends LocalizedFXMLViewController {
VaultList vaults) {
super(localization);
this.mainWindow = mainWindow;
this.settings = settings;
this.vaultFactoy = vaultFactoy;
this.welcomeController = welcomeController;
this.initializeController = initializeController;
@@ -231,7 +233,7 @@ public class MainController extends LocalizedFXMLViewController {
return;
}
final VaultSettings vaultSettings = VaultSettings.withRandomId();
final VaultSettings vaultSettings = VaultSettings.withRandomId(settings);
vaultSettings.path().set(vaultPath);
final Vault vault = vaultFactoy.get(vaultSettings);
if (!vaults.contains(vault)) {

View File

@@ -27,7 +27,6 @@ import org.cryptomator.ui.controls.SecPasswordField;
import org.cryptomator.ui.model.Vault;
import org.cryptomator.ui.model.WindowsDriveLetters;
import org.cryptomator.ui.settings.Localization;
import org.cryptomator.ui.settings.Settings;
import org.cryptomator.ui.util.AsyncTaskService;
import org.cryptomator.ui.util.DialogBuilderUtil;
import org.slf4j.Logger;
@@ -59,7 +58,6 @@ public class UnlockController extends LocalizedFXMLViewController {
private final Application app;
private final AsyncTaskService asyncTaskService;
private final Settings settings;
private final WindowsDriveLetters driveLetters;
private final ChangeListener<Character> driveLetterChangeListener = this::winDriveLetterDidChange;
private final Optional<KeychainAccess> keychainAccess;
@@ -67,11 +65,10 @@ public class UnlockController extends LocalizedFXMLViewController {
private Optional<UnlockListener> listener = Optional.empty();
@Inject
public UnlockController(Application app, Localization localization, AsyncTaskService asyncTaskService, Settings settings, WindowsDriveLetters driveLetters, Optional<KeychainAccess> keychainAccess) {
public UnlockController(Application app, Localization localization, AsyncTaskService asyncTaskService, WindowsDriveLetters driveLetters, Optional<KeychainAccess> keychainAccess) {
super(localization);
this.app = app;
this.asyncTaskService = asyncTaskService;
this.settings = settings;
this.driveLetters = driveLetters;
this.keychainAccess = keychainAccess;
}
@@ -256,7 +253,6 @@ public class UnlockController extends LocalizedFXMLViewController {
private void winDriveLetterDidChange(ObservableValue<? extends Character> property, Character oldValue, Character newValue) {
vault.setWinDriveLetter(newValue);
settings.save();
}
private void chooseSelectedDriveLetter() {

View File

@@ -18,7 +18,6 @@ import org.cryptomator.cryptolib.api.CryptoLibVersion.Version;
import org.cryptomator.cryptolib.api.Cryptor;
import org.cryptomator.cryptolib.api.CryptorProvider;
import org.cryptomator.ui.settings.Localization;
import org.cryptomator.ui.settings.Settings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -28,12 +27,10 @@ import javafx.application.Platform;
class UpgradeVersion3DropBundleExtension extends UpgradeStrategy {
private static final Logger LOG = LoggerFactory.getLogger(UpgradeVersion3DropBundleExtension.class);
private final Settings settings;
@Inject
public UpgradeVersion3DropBundleExtension(@CryptoLibVersion(Version.ONE) CryptorProvider version1CryptorProvider, Localization localization, Settings settings) {
public UpgradeVersion3DropBundleExtension(@CryptoLibVersion(Version.ONE) CryptorProvider version1CryptorProvider, Localization localization) {
super(version1CryptorProvider, localization, 3, 3);
this.settings = settings;
}
@Override
@@ -66,7 +63,6 @@ class UpgradeVersion3DropBundleExtension extends UpgradeStrategy {
Files.move(path, path.resolveSibling(newVaultName));
Platform.runLater(() -> {
vault.getVaultSettings().path().set(newPath);
settings.save();
});
} catch (IOException e) {
LOG.error("Vault migration failed", e);

View File

@@ -59,7 +59,7 @@ public class Settings {
this.save();
}
public void save() {
void save() {
if (saveCmd != null) {
saveCmd.accept(this);
}

View File

@@ -60,7 +60,7 @@ public class SettingsJsonAdapter extends TypeAdapter<Settings> {
String name = in.nextName();
switch (name) {
case "directories":
settings.getDirectories().addAll(readVaultSettingsArray(in));
settings.getDirectories().addAll(readVaultSettingsArray(in, settings));
break;
case "checkForUpdatesEnabled":
settings.checkForUpdates().set(in.nextBoolean());
@@ -90,11 +90,11 @@ public class SettingsJsonAdapter extends TypeAdapter<Settings> {
return settings;
}
private List<VaultSettings> readVaultSettingsArray(JsonReader in) throws IOException {
private List<VaultSettings> readVaultSettingsArray(JsonReader in, Settings settings) throws IOException {
List<VaultSettings> result = new ArrayList<>();
in.beginArray();
while (!JsonToken.END_ARRAY.equals(in.peek())) {
result.add(vaultSettingsJsonAdapter.read(in));
result.add(vaultSettingsJsonAdapter.read(in, settings));
}
in.endArray();
return result;

View File

@@ -19,19 +19,28 @@ import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
public class VaultSettings {
private final Settings settings;
private final String id;
private final ObjectProperty<Path> path = new SimpleObjectProperty<>();
private final StringProperty mountName = new SimpleStringProperty();
private final StringProperty winDriveLetter = new SimpleStringProperty();
public VaultSettings(String id) {
public VaultSettings(Settings settings, String id) {
this.settings = settings;
this.id = Objects.requireNonNull(id);
EasyBind.subscribe(path, this::deriveMountNameFromPath);
// TODO: automatically save settings, when chaning vaultSettings
path.addListener(this::somethingChanged);
mountName.addListener(this::somethingChanged);
winDriveLetter.addListener(this::somethingChanged);
}
private void somethingChanged(ObservableValue<?> observable, Object oldValue, Object newValue) {
settings.save();
}
private void deriveMountNameFromPath(Path path) {
@@ -40,8 +49,8 @@ public class VaultSettings {
}
}
public static VaultSettings withRandomId() {
return new VaultSettings(generateId());
public static VaultSettings withRandomId(Settings settings) {
return new VaultSettings(settings, generateId());
}
private static String generateId() {

View File

@@ -11,15 +11,13 @@ import java.nio.file.Paths;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
class VaultSettingsJsonAdapter extends TypeAdapter<VaultSettings> {
class VaultSettingsJsonAdapter {
private static final Logger LOG = LoggerFactory.getLogger(VaultSettingsJsonAdapter.class);
@Override
public void write(JsonWriter out, VaultSettings value) throws IOException {
out.beginObject();
out.name("id").value(value.getId());
@@ -29,8 +27,7 @@ class VaultSettingsJsonAdapter extends TypeAdapter<VaultSettings> {
out.endObject();
}
@Override
public VaultSettings read(JsonReader in) throws IOException {
public VaultSettings read(JsonReader in, Settings settings) throws IOException {
String id = null;
String path = null;
String mountName = null;
@@ -59,11 +56,11 @@ class VaultSettingsJsonAdapter extends TypeAdapter<VaultSettings> {
}
in.endObject();
VaultSettings settings = (id == null) ? VaultSettings.withRandomId() : new VaultSettings(id);
settings.mountName().set(mountName);
settings.path().set(Paths.get(path));
settings.winDriveLetter().set(winDriveLetter);
return settings;
VaultSettings vaultSettings = (id == null) ? VaultSettings.withRandomId(settings) : new VaultSettings(settings, id);
vaultSettings.mountName().set(mountName);
vaultSettings.path().set(Paths.get(path));
vaultSettings.winDriveLetter().set(winDriveLetter);
return vaultSettings;
}
}

View File

@@ -6,10 +6,14 @@
package org.cryptomator.ui.settings;
import java.io.IOException;
import java.io.StringReader;
import java.nio.file.Paths;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import com.google.gson.stream.JsonReader;
public class VaultSettingsJsonAdapterTest {
@@ -17,11 +21,15 @@ public class VaultSettingsJsonAdapterTest {
@Test
public void testDeserialize() throws IOException {
VaultSettings settings = adapter.fromJson("{\"id\": \"foo\", \"path\": \"/foo/bar\", \"mountName\": \"test\", \"winDriveLetter\": \"X\", \"shouldBeIgnored\": true}");
Assert.assertEquals("foo", settings.getId());
Assert.assertEquals(Paths.get("/foo/bar"), settings.path().get());
Assert.assertEquals("test", settings.mountName().get());
Assert.assertEquals("X", settings.winDriveLetter().get());
String json = "{\"id\": \"foo\", \"path\": \"/foo/bar\", \"mountName\": \"test\", \"winDriveLetter\": \"X\", \"shouldBeIgnored\": true}";
JsonReader jsonReader = new JsonReader(new StringReader(json));
Settings settings = Mockito.mock(Settings.class);
VaultSettings vaultSettings = adapter.read(jsonReader, settings);
Assert.assertEquals("foo", vaultSettings.getId());
Assert.assertEquals(Paths.get("/foo/bar"), vaultSettings.path().get());
Assert.assertEquals("test", vaultSettings.mountName().get());
Assert.assertEquals("X", vaultSettings.winDriveLetter().get());
}
}