From ae831c0c33626022ed6f228e071215572d5f22aa Mon Sep 17 00:00:00 2001 From: M3DZIK Date: Sun, 29 Oct 2023 21:08:44 +0100 Subject: [PATCH] ktlint: Format --- .../medzik/librepass/desktop/config/Config.kt | 39 +++++++------ .../librepass/desktop/gui/Controller.kt | 1 + .../desktop/gui/WelcomeController.kt | 9 +-- .../desktop/gui/auth/LoginController.kt | 57 ++++++++++--------- .../desktop/gui/auth/RegisterController.kt | 14 +++-- .../desktop/gui/components/CipherListItem.kt | 14 +++-- .../desktop/gui/dashboard/CipherView.kt | 10 +++- .../gui/dashboard/DashboardController.kt | 57 +++++++++++-------- .../librepass/desktop/locale/LangManager.kt | 1 - .../librepass/desktop/state/StateManager.kt | 33 ++++++----- .../medzik/librepass/desktop/utils/Fxml.kt | 13 ++++- .../medzik/librepass/desktop/utils/Utils.kt | 6 +- 12 files changed, 150 insertions(+), 104 deletions(-) diff --git a/src/main/kotlin/dev/medzik/librepass/desktop/config/Config.kt b/src/main/kotlin/dev/medzik/librepass/desktop/config/Config.kt index 6aae56c..d36ae05 100644 --- a/src/main/kotlin/dev/medzik/librepass/desktop/config/Config.kt +++ b/src/main/kotlin/dev/medzik/librepass/desktop/config/Config.kt @@ -8,6 +8,7 @@ import java.io.File import java.nio.file.Files import java.nio.file.Paths import java.util.* + object Config { val workDir = File(getWorkDir("librepass")) @@ -31,21 +32,27 @@ object Config { private fun getWorkDir(name: String): String { val osname = System.getProperty("os.name").lowercase(Locale.getDefault()) - if (osname.startsWith("windows")) return Paths.get(System.getenv("APPDATA"), name) - .toString() else if (osname.contains("nux") || osname.contains("freebsd")) return Paths.get( - System.getProperty("user.home"), - ".config", - name - ).toString() else if (osname.contains("mac") || osname.contains("darwin")) return Paths.get( - System.getProperty("user.home"), - "Library", - "Application Support", - name - ).toString() + if (osname.startsWith("windows")) + return Paths.get(System.getenv("APPDATA"), name) + .toString() else if (osname.contains("nux") || osname.contains("freebsd")) + return Paths.get( + System.getProperty("user.home"), + ".config", + name + ).toString() else if (osname.contains("mac") || osname.contains("darwin")) + return Paths.get( + System.getProperty("user.home"), + "Library", + "Application Support", + name + ).toString() return "" } - fun writeObject(name: String, obj: Any): Any { + fun writeObject( + name: String, + obj: Any + ): Any { val file = File(workDir, "$name.json") val serialized = Gson().toJson(obj) @@ -60,7 +67,10 @@ object Config { return Gson().fromJson(json, object : TypeToken() {}.type) } - inline fun overrideObject(name: String, obj: (T) -> T): T { + inline fun overrideObject( + name: String, + obj: (T) -> T + ): T { val readed = readObject(name) val ret = obj.invoke(readed) writeObject(name, ret) @@ -79,13 +89,10 @@ object Config { data class Credentials( val userId: UUID, val email: String, - val apiUrl: String? = null, val apiKey: String, val publicKey: String, - val lastSync: Long? = null, - // argon2id parameters val memory: Int, val iterations: Int, diff --git a/src/main/kotlin/dev/medzik/librepass/desktop/gui/Controller.kt b/src/main/kotlin/dev/medzik/librepass/desktop/gui/Controller.kt index a7e903d..3412dc8 100644 --- a/src/main/kotlin/dev/medzik/librepass/desktop/gui/Controller.kt +++ b/src/main/kotlin/dev/medzik/librepass/desktop/gui/Controller.kt @@ -2,5 +2,6 @@ package dev.medzik.librepass.desktop.gui open class Controller { open fun onStart() {} + open fun onStop() {} } diff --git a/src/main/kotlin/dev/medzik/librepass/desktop/gui/WelcomeController.kt b/src/main/kotlin/dev/medzik/librepass/desktop/gui/WelcomeController.kt index 07549d2..c1af0fe 100644 --- a/src/main/kotlin/dev/medzik/librepass/desktop/gui/WelcomeController.kt +++ b/src/main/kotlin/dev/medzik/librepass/desktop/gui/WelcomeController.kt @@ -24,10 +24,11 @@ class WelcomeController : Controller() { @FXML private fun initialize() { - fadeInTransition = FadeTransition(Duration.seconds(1.6), logo).apply { - fromValue = 0.0 - toValue = 1.0 - } + fadeInTransition = + FadeTransition(Duration.seconds(1.6), logo).apply { + fromValue = 0.0 + toValue = 1.0 + } // reading theme val theme = Config.readObject("settings").theme diff --git a/src/main/kotlin/dev/medzik/librepass/desktop/gui/auth/LoginController.kt b/src/main/kotlin/dev/medzik/librepass/desktop/gui/auth/LoginController.kt index fe832a9..3570700 100644 --- a/src/main/kotlin/dev/medzik/librepass/desktop/gui/auth/LoginController.kt +++ b/src/main/kotlin/dev/medzik/librepass/desktop/gui/auth/LoginController.kt @@ -20,7 +20,6 @@ import javafx.scene.control.TextField import java.util.concurrent.CompletableFuture class LoginController : Controller() { - @FXML private lateinit var email: TextField @@ -34,50 +33,56 @@ class LoginController : Controller() { @FXML private fun initialize() { - val listener = ChangeListener { _, _, _ -> - login.isDisable = !email.text.contains("@") || password.text.isEmpty() - } + val listener = + ChangeListener { _, _, _ -> + login.isDisable = !email.text.contains("@") || password.text.isEmpty() + } email.textProperty().addListener(listener) password.textProperty().addListener(listener) login.isDisable = true } @FXML - fun onBack() = - StateManager.setState(State.WELCOME) + fun onBack() = StateManager.setState(State.WELCOME) @FXML fun onLogin() { submit(email.text, password.text) } - private fun submit(email: String, password: String) = CompletableFuture.runAsync { + private fun submit( + email: String, + password: String + ) = CompletableFuture.runAsync { login.isDisable = true try { val preLogin = authClient.preLogin(email) // authenticate user and get credentials - val loginCredentials = authClient.login( - email = email, - password = password - ) - - val credentials = Credentials( - userId = loginCredentials.userId, - email = email, - apiKey = loginCredentials.apiKey, - publicKey = Hex.encode(loginCredentials.publicKey), - // Argon2id parameters - memory = preLogin.memory, - iterations = preLogin.iterations, - parallelism = preLogin.parallelism - ) + val loginCredentials = + authClient.login( + email = email, + password = password + ) + + val credentials = + Credentials( + userId = loginCredentials.userId, + email = email, + apiKey = loginCredentials.apiKey, + publicKey = Hex.encode(loginCredentials.publicKey), + // Argon2id parameters + memory = preLogin.memory, + iterations = preLogin.iterations, + parallelism = preLogin.parallelism + ) Config.writeObject("credentials", credentials) - val userSecrets = UserSecrets( - privateKey = Hex.encode(loginCredentials.privateKey), - secretKey = loginCredentials.secretKey - ) + val userSecrets = + UserSecrets( + privateKey = Hex.encode(loginCredentials.privateKey), + secretKey = loginCredentials.secretKey + ) Config.writeObject("user_secrets", userSecrets) // Perform GC to flush a lot of memory allocated by argon2 generation diff --git a/src/main/kotlin/dev/medzik/librepass/desktop/gui/auth/RegisterController.kt b/src/main/kotlin/dev/medzik/librepass/desktop/gui/auth/RegisterController.kt index dbf124f..b49883e 100644 --- a/src/main/kotlin/dev/medzik/librepass/desktop/gui/auth/RegisterController.kt +++ b/src/main/kotlin/dev/medzik/librepass/desktop/gui/auth/RegisterController.kt @@ -13,7 +13,6 @@ import javafx.scene.control.TextField import java.util.concurrent.CompletableFuture class RegisterController { - @FXML private lateinit var email: TextField @@ -33,9 +32,10 @@ class RegisterController { @FXML private fun initialize() { - val listener = ChangeListener { _, _, _ -> - register.isDisable = !email.text.contains("@") || password.text.isEmpty() || password.text != retypePassword.text - } + val listener = + ChangeListener { _, _, _ -> + register.isDisable = !email.text.contains("@") || password.text.isEmpty() || password.text != retypePassword.text + } email.textProperty().addListener(listener) password.textProperty().addListener(listener) retypePassword.textProperty().addListener(listener) @@ -52,7 +52,11 @@ class RegisterController { hint.clear() } - private fun submit(email: String, password: String, passwordHint: String) { + private fun submit( + email: String, + password: String, + passwordHint: String + ) { CompletableFuture.runAsync { authClient.register(email, password, passwordHint) Utils.dialog("Registered!", "Registered!", Alert.AlertType.INFORMATION) diff --git a/src/main/kotlin/dev/medzik/librepass/desktop/gui/components/CipherListItem.kt b/src/main/kotlin/dev/medzik/librepass/desktop/gui/components/CipherListItem.kt index bd4cb68..0549f54 100644 --- a/src/main/kotlin/dev/medzik/librepass/desktop/gui/components/CipherListItem.kt +++ b/src/main/kotlin/dev/medzik/librepass/desktop/gui/components/CipherListItem.kt @@ -49,7 +49,10 @@ class CipherListItem : ListCell() { } } - override fun updateItem(cipher: Cipher?, empty: Boolean) { + override fun updateItem( + cipher: Cipher?, + empty: Boolean + ) { super.updateItem(cipher, empty) if (empty) { graphic = null @@ -77,9 +80,10 @@ class CipherListItem : ListCell() { private fun updateIcon(cipher: Cipher) { val urls = cipher.loginData?.uris - icon.image = if (!urls.isNullOrEmpty()) - getIcon(CipherClient.getFavicon(domain = urls[0])) - else - userIcon + icon.image = + if (!urls.isNullOrEmpty()) + getIcon(CipherClient.getFavicon(domain = urls[0])) + else + userIcon } } diff --git a/src/main/kotlin/dev/medzik/librepass/desktop/gui/dashboard/CipherView.kt b/src/main/kotlin/dev/medzik/librepass/desktop/gui/dashboard/CipherView.kt index 68ee046..7eaa19e 100644 --- a/src/main/kotlin/dev/medzik/librepass/desktop/gui/dashboard/CipherView.kt +++ b/src/main/kotlin/dev/medzik/librepass/desktop/gui/dashboard/CipherView.kt @@ -15,7 +15,6 @@ private const val BULLET = '\u2022' // TODO: Split into components class CipherView : AnchorPane() { - @FXML private lateinit var name: Label @@ -60,13 +59,18 @@ class CipherView : AnchorPane() { passwordToggleShow.isSelected = false } - private fun setPassword(text: String?, mask: Boolean) { + private fun setPassword( + text: String?, + mask: Boolean + ) { password.text = "" if (!text.isNullOrEmpty()) { if (mask) { for (i in text) password.text += BULLET - } else password.text = text + } else { + password.text = text + } } } diff --git a/src/main/kotlin/dev/medzik/librepass/desktop/gui/dashboard/DashboardController.kt b/src/main/kotlin/dev/medzik/librepass/desktop/gui/dashboard/DashboardController.kt index 4914e94..e9ff446 100644 --- a/src/main/kotlin/dev/medzik/librepass/desktop/gui/dashboard/DashboardController.kt +++ b/src/main/kotlin/dev/medzik/librepass/desktop/gui/dashboard/DashboardController.kt @@ -51,10 +51,13 @@ class DashboardController : Controller() { cipherList.items = list cipherList.setCellFactory { CipherListItem() } cipherList.selectionModel.selectedItemProperty().addListener { _, _, cipher -> - rootBorderPane.center = if (cipher != null) { - cipherView.setCipher(cipher) - cipherView - } else emptyView + rootBorderPane.center = + if (cipher != null) { + cipherView.setCipher(cipher) + cipherView + } else { + emptyView + } } rootBorderPane.center = emptyView @@ -69,28 +72,32 @@ class DashboardController : Controller() { // get ciphers from local repository and update UI // TODO: Add more database like storage private fun updateLocalCiphers() { - val ciphers = if (Config.isObjectExists("ciphers")) - Config.readObject>("ciphers") - else { - val list = mutableListOf() - Config.writeObject("ciphers", list) - list - } + val ciphers = + if (Config.isObjectExists("ciphers")) { + Config.readObject>("ciphers") + } else { + val list = mutableListOf() + Config.writeObject("ciphers", list) + list + } - val decryptedCiphers = ciphers.map { - try { - Cipher(it.encryptedCipher, userSecrets.secretKey) - } catch (e: Exception) { // TODO - Cipher( - id = it.encryptedCipher.id, - owner = it.owner, - type = CipherType.Login, - loginData = CipherLoginData( - name = "Encryption error" + val decryptedCiphers = + ciphers.map { + try { + Cipher(it.encryptedCipher, userSecrets.secretKey) + } catch (e: Exception) { + // TODO + Cipher( + id = it.encryptedCipher.id, + owner = it.owner, + type = CipherType.Login, + loginData = + CipherLoginData( + name = "Encryption error" + ) ) - ) + } } - } Platform.runLater { list.clear() // sort ciphers by name and update UI @@ -155,9 +162,9 @@ class DashboardController : Controller() { } resetStatus() } catch (e: Exception) { - if (e is ClientException) + if (e is ClientException) { setStatus(tr("status.error.network")) - else { + } else { setStatus("Error: ${e.message}") throw RuntimeException(e) } diff --git a/src/main/kotlin/dev/medzik/librepass/desktop/locale/LangManager.kt b/src/main/kotlin/dev/medzik/librepass/desktop/locale/LangManager.kt index a43b2ba..96e81e8 100644 --- a/src/main/kotlin/dev/medzik/librepass/desktop/locale/LangManager.kt +++ b/src/main/kotlin/dev/medzik/librepass/desktop/locale/LangManager.kt @@ -3,7 +3,6 @@ package dev.medzik.librepass.desktop.locale import java.util.* object LangManager { - private lateinit var currentLocale: ResourceBundle fun init(): ResourceBundle { diff --git a/src/main/kotlin/dev/medzik/librepass/desktop/state/StateManager.kt b/src/main/kotlin/dev/medzik/librepass/desktop/state/StateManager.kt index 807ea88..6436880 100644 --- a/src/main/kotlin/dev/medzik/librepass/desktop/state/StateManager.kt +++ b/src/main/kotlin/dev/medzik/librepass/desktop/state/StateManager.kt @@ -9,7 +9,10 @@ object StateManager { private lateinit var scene: Scene private var currentState: State? = null - fun init(scene: Scene, resources: ResourceBundle) { + fun init( + scene: Scene, + resources: ResourceBundle + ) { StateManager.scene = scene for (state in State.entries) state.load(resources) @@ -19,21 +22,21 @@ object StateManager { return state } - fun applyState(state: State) = Platform.runLater { - if (currentState != null) { - val currentController = currentState!!.getController() - if (currentController is Controller) - currentController.onStop() - } + fun applyState(state: State) = + Platform.runLater { + if (currentState != null) { + val currentController = currentState!!.getController() + if (currentController is Controller) + currentController.onStop() + } - currentState = state - scene.root = state.rootPane + currentState = state + scene.root = state.rootPane - val controller = state.getController() - if (controller is Controller) - controller.onStart() - } + val controller = state.getController() + if (controller is Controller) + controller.onStart() + } - fun setState(state: State) = - applyState(state) + fun setState(state: State) = applyState(state) } diff --git a/src/main/kotlin/dev/medzik/librepass/desktop/utils/Fxml.kt b/src/main/kotlin/dev/medzik/librepass/desktop/utils/Fxml.kt index afec186..b23e0a6 100644 --- a/src/main/kotlin/dev/medzik/librepass/desktop/utils/Fxml.kt +++ b/src/main/kotlin/dev/medzik/librepass/desktop/utils/Fxml.kt @@ -5,10 +5,13 @@ import dev.medzik.librepass.desktop.locale.LangManager import javafx.fxml.FXMLLoader object Fxml { - fun getLoader(path: String) = FXMLLoader(CipherListItem::class.java.getResource(path)) - fun load(path: String, controller: Any?, rootNode: Any? = controller) { + fun load( + path: String, + controller: Any?, + rootNode: Any? = controller + ) { val loader = FXMLLoader(CipherListItem::class.java.getResource(path)) loader.resources = LangManager.getLocale() loader.setController(controller) @@ -16,7 +19,11 @@ object Fxml { loader.load() } - fun loadComponent(path: String, controller: Any?, rootNode: Any? = controller) { + fun loadComponent( + path: String, + controller: Any?, + rootNode: Any? = controller + ) { val loader = FXMLLoader(CipherListItem::class.java.getResource(path)) loader.resources = LangManager.getLocale() loader.setControllerFactory { controller } diff --git a/src/main/kotlin/dev/medzik/librepass/desktop/utils/Utils.kt b/src/main/kotlin/dev/medzik/librepass/desktop/utils/Utils.kt index 349f499..5a545a6 100644 --- a/src/main/kotlin/dev/medzik/librepass/desktop/utils/Utils.kt +++ b/src/main/kotlin/dev/medzik/librepass/desktop/utils/Utils.kt @@ -6,7 +6,11 @@ import javafx.scene.control.Alert import javafx.scene.control.Alert.AlertType object Utils { - fun dialog(title: String, text: String, type: AlertType) { + fun dialog( + title: String, + text: String, + type: AlertType + ) { Platform.runLater { val alert = Alert(type) alert.title = title