From 98002f63dc7c5109cc736da6400a799e2b85b71b Mon Sep 17 00:00:00 2001 From: Armin Schrenk Date: Thu, 12 May 2022 08:45:03 +0200 Subject: [PATCH] Use enum for well-known local cloud storage paths and move class to commons package --- .../cryptomator/common/LocationPreset.java | 60 +++++++++++++++++++ .../CreateNewVaultLocationController.java | 6 +- ...sets.java => ObservedLocationPresets.java} | 46 ++++---------- .../resources/fxml/addvault_new_location.fxml | 12 ++-- 4 files changed, 79 insertions(+), 45 deletions(-) create mode 100644 src/main/java/org/cryptomator/common/LocationPreset.java rename src/main/java/org/cryptomator/ui/addvaultwizard/{LocationPresets.java => ObservedLocationPresets.java} (64%) diff --git a/src/main/java/org/cryptomator/common/LocationPreset.java b/src/main/java/org/cryptomator/common/LocationPreset.java new file mode 100644 index 000000000..1c984160c --- /dev/null +++ b/src/main/java/org/cryptomator/common/LocationPreset.java @@ -0,0 +1,60 @@ +package org.cryptomator.common; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.List; + +/** + * Enum of common cloud providers and their default local storage location path. + */ +public enum LocationPreset { + + DROPBOX("Dropbox", "~/Dropbox"), + ICLOUD("iCloud Drive", "~/Library/Mobile Documents/com~apple~CloudDocs", "~/iCloudDrive"), + GDRIVE("Google Drive", "~/Google Drive/My Drive", "~/Google Drive"), + MEGA("MEGA", "~/MEGA"), + ONEDRIVE("OneDrive", "~/OneDrive"), + PCLOUD("pCloud", "~/pCloudDrive"), + + LOCAL("local"); + + final String name; + final List candidates; + + LocationPreset(String name, String... candidates) { + this.name = name; + + String userHome = System.getProperty("user.home"); + this.candidates = Arrays.stream(candidates).map(c -> LocationPreset.resolveHomePath(userHome, c)).map(Path::of).toList(); + } + + private static String resolveHomePath(String home, String path) { + if (path.startsWith("~/")) { + return home + path.substring(1); + } else { + return path; + } + } + + /** + * Checks for this LocationPreset if any of the associated paths exist. + * + * @return the first existing path or null, if none exists. + */ + public Path existingPath() { + for (Path candidate : candidates) { + if (Files.isDirectory(candidate)) { + return candidate; + } + } + return null; + } + + @Override + public String toString() { + return name; + } + +} + diff --git a/src/main/java/org/cryptomator/ui/addvaultwizard/CreateNewVaultLocationController.java b/src/main/java/org/cryptomator/ui/addvaultwizard/CreateNewVaultLocationController.java index 1fd463432..cadccc091 100644 --- a/src/main/java/org/cryptomator/ui/addvaultwizard/CreateNewVaultLocationController.java +++ b/src/main/java/org/cryptomator/ui/addvaultwizard/CreateNewVaultLocationController.java @@ -46,7 +46,7 @@ public class CreateNewVaultLocationController implements FxController { private final Stage window; private final Lazy chooseNameScene; private final Lazy choosePasswordScene; - private final LocationPresets locationPresets; + private final ObservedLocationPresets locationPresets; private final ObjectProperty vaultPath; private final StringProperty vaultName; private final ResourceBundle resourceBundle; @@ -71,7 +71,7 @@ public class CreateNewVaultLocationController implements FxController { public FontAwesome5IconView badLocation; @Inject - CreateNewVaultLocationController(@AddVaultWizardWindow Stage window, @FxmlScene(FxmlFile.ADDVAULT_NEW_NAME) Lazy chooseNameScene, @FxmlScene(FxmlFile.ADDVAULT_NEW_PASSWORD) Lazy choosePasswordScene, LocationPresets locationPresets, ObjectProperty vaultPath, @Named("vaultName") StringProperty vaultName, ResourceBundle resourceBundle) { + CreateNewVaultLocationController(@AddVaultWizardWindow Stage window, @FxmlScene(FxmlFile.ADDVAULT_NEW_NAME) Lazy chooseNameScene, @FxmlScene(FxmlFile.ADDVAULT_NEW_PASSWORD) Lazy choosePasswordScene, ObservedLocationPresets locationPresets, ObjectProperty vaultPath, @Named("vaultName") StringProperty vaultName, ResourceBundle resourceBundle) { this.window = window; this.chooseNameScene = chooseNameScene; this.choosePasswordScene = choosePasswordScene; @@ -197,7 +197,7 @@ public class CreateNewVaultLocationController implements FxController { return validVaultPath.get(); } - public LocationPresets getLocationPresets() { + public ObservedLocationPresets getObservedLocationPresets() { return locationPresets; } diff --git a/src/main/java/org/cryptomator/ui/addvaultwizard/LocationPresets.java b/src/main/java/org/cryptomator/ui/addvaultwizard/ObservedLocationPresets.java similarity index 64% rename from src/main/java/org/cryptomator/ui/addvaultwizard/LocationPresets.java rename to src/main/java/org/cryptomator/ui/addvaultwizard/ObservedLocationPresets.java index 313a31dc9..0a057845b 100644 --- a/src/main/java/org/cryptomator/ui/addvaultwizard/LocationPresets.java +++ b/src/main/java/org/cryptomator/ui/addvaultwizard/ObservedLocationPresets.java @@ -1,23 +1,15 @@ package org.cryptomator.ui.addvaultwizard; +import org.cryptomator.common.LocationPreset; + import javax.inject.Inject; import javafx.beans.binding.BooleanBinding; import javafx.beans.property.ReadOnlyObjectProperty; import javafx.beans.property.SimpleObjectProperty; -import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; @AddVaultWizardScoped -public class LocationPresets { - - private static final String USER_HOME = System.getProperty("user.home"); - private static final String[] ICLOUDDRIVE_LOCATIONS = {"~/Library/Mobile Documents/iCloud~com~setolabs~Cryptomator/Documents", "~/iCloudDrive/iCloud~com~setolabs~Cryptomator"}; - private static final String[] DROPBOX_LOCATIONS = {"~/Dropbox"}; - private static final String[] GDRIVE_LOCATIONS = {"~/Google Drive/My Drive", "~/Google Drive"}; - private static final String[] ONEDRIVE_LOCATIONS = {"~/OneDrive"}; - private static final String[] MEGA_LOCATIONS = {"~/MEGA"}; - private static final String[] PCLOUD_LOCATIONS = {"~/pCloudDrive"}; +public class ObservedLocationPresets { private final ReadOnlyObjectProperty iclouddriveLocation; private final ReadOnlyObjectProperty dropboxLocation; @@ -33,13 +25,13 @@ public class LocationPresets { private final BooleanBinding foundPcloud; @Inject - public LocationPresets() { - this.iclouddriveLocation = new SimpleObjectProperty<>(existingWritablePath(ICLOUDDRIVE_LOCATIONS)); - this.dropboxLocation = new SimpleObjectProperty<>(existingWritablePath(DROPBOX_LOCATIONS)); - this.gdriveLocation = new SimpleObjectProperty<>(existingWritablePath(GDRIVE_LOCATIONS)); - this.onedriveLocation = new SimpleObjectProperty<>(existingWritablePath(ONEDRIVE_LOCATIONS)); - this.megaLocation = new SimpleObjectProperty<>(existingWritablePath(MEGA_LOCATIONS)); - this.pcloudLocation = new SimpleObjectProperty<>(existingWritablePath(PCLOUD_LOCATIONS)); + public ObservedLocationPresets() { + this.iclouddriveLocation = new SimpleObjectProperty<>(LocationPreset.ICLOUD.existingPath()); + this.dropboxLocation = new SimpleObjectProperty<>(LocationPreset.DROPBOX.existingPath()); + this.gdriveLocation = new SimpleObjectProperty<>(LocationPreset.GDRIVE.existingPath()); + this.onedriveLocation = new SimpleObjectProperty<>(LocationPreset.ONEDRIVE.existingPath()); + this.megaLocation = new SimpleObjectProperty<>(LocationPreset.MEGA.existingPath()); + this.pcloudLocation = new SimpleObjectProperty<>(LocationPreset.PCLOUD.existingPath()); this.foundIclouddrive = iclouddriveLocation.isNotNull(); this.foundDropbox = dropboxLocation.isNotNull(); this.foundGdrive = gdriveLocation.isNotNull(); @@ -48,24 +40,6 @@ public class LocationPresets { this.foundPcloud = pcloudLocation.isNotNull(); } - private static Path existingWritablePath(String... candidates) { - for (String candidate : candidates) { - Path path = Paths.get(resolveHomePath(candidate)); - if (Files.isDirectory(path)) { - return path; - } - } - return null; - } - - private static String resolveHomePath(String path) { - if (path.startsWith("~/")) { - return USER_HOME + path.substring(1); - } else { - return path; - } - } - /* Observables */ public ReadOnlyObjectProperty iclouddriveLocationProperty() { diff --git a/src/main/resources/fxml/addvault_new_location.fxml b/src/main/resources/fxml/addvault_new_location.fxml index 3c25ba569..343a2d580 100644 --- a/src/main/resources/fxml/addvault_new_location.fxml +++ b/src/main/resources/fxml/addvault_new_location.fxml @@ -31,12 +31,12 @@