This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
directory structure setting moved to general settings (#3166)
* directory structure setting moved to general settings * Update app/src/main/java/app/passwordstore/util/settings/PreferenceKeys.kt Co-authored-by: Harsh Shandilya <[email protected]> Signed-off-by: Alexander Grahn <[email protected]> --------- Signed-off-by: Alexander Grahn <[email protected]> Co-authored-by: Harsh Shandilya <[email protected]>
- Loading branch information
Showing
20 changed files
with
145 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,124 +9,15 @@ import app.passwordstore.data.passfile.PasswordEntry | |
import app.passwordstore.util.extensions.getString | ||
import app.passwordstore.util.extensions.sharedPrefs | ||
import app.passwordstore.util.services.getDefaultUsername | ||
import app.passwordstore.util.settings.DirectoryStructure | ||
import app.passwordstore.util.settings.PreferenceKeys | ||
import com.github.androidpasswordstore.autofillparser.Credentials | ||
import java.io.File | ||
import java.nio.file.Paths | ||
|
||
enum class DirectoryStructure(val value: String) { | ||
EncryptedUsername("encrypted_username"), | ||
FileBased("file"), | ||
DirectoryBased("directory"); | ||
|
||
/** | ||
* Returns the username associated to [file], following the convention of the current | ||
* [DirectoryStructure]. | ||
* | ||
* Examples: | ||
* - * --> null (EncryptedUsername) | ||
* - work/example.org/[email protected] --> [email protected] (FileBased) | ||
* - work/example.org/[email protected]/password.gpg --> [email protected] (DirectoryBased) | ||
* - Temporary PIN.gpg --> Temporary PIN (DirectoryBased, fallback) | ||
*/ | ||
fun getUsernameFor(file: File): String? = | ||
when (this) { | ||
EncryptedUsername -> null | ||
FileBased -> file.nameWithoutExtension | ||
DirectoryBased -> file.parentFile?.name ?: file.nameWithoutExtension | ||
} | ||
|
||
/** | ||
* Returns the origin identifier associated to [file], following the convention of the current | ||
* [DirectoryStructure]. | ||
* | ||
* At least one of [DirectoryStructure.getIdentifierFor] and | ||
* [DirectoryStructure.getAccountPartFor] will always return a non-null result. | ||
* | ||
* Examples: | ||
* - work/example.org.gpg --> example.org (EncryptedUsername) | ||
* - work/example.org/[email protected] --> example.org (FileBased) | ||
* - example.org.gpg --> example.org (FileBased, fallback) | ||
* - work/example.org/[email protected]/password.gpg --> example.org (DirectoryBased) | ||
* - Temporary PIN.gpg --> null (DirectoryBased) | ||
*/ | ||
fun getIdentifierFor(file: File): String? = | ||
when (this) { | ||
EncryptedUsername -> file.nameWithoutExtension | ||
FileBased -> file.parentFile?.name ?: file.nameWithoutExtension | ||
DirectoryBased -> file.parentFile?.parent | ||
} | ||
|
||
/** | ||
* Returns the path components of [file] until right before the component that contains the origin | ||
* identifier according to the current [DirectoryStructure]. | ||
* | ||
* Examples: | ||
* - work/example.org.gpg --> work (EncryptedUsername) | ||
* - work/example.org/[email protected] --> work (FileBased) | ||
* - example.org/[email protected] --> null (FileBased) | ||
* - [email protected] --> null (FileBased) | ||
* - work/example.org/[email protected]/password.gpg --> work (DirectoryBased) | ||
* - example.org/[email protected]/password.gpg --> null (DirectoryBased) | ||
*/ | ||
fun getPathToIdentifierFor(file: File): String? = | ||
when (this) { | ||
EncryptedUsername -> file.parent | ||
FileBased -> file.parentFile?.parent | ||
DirectoryBased -> file.parentFile?.parentFile?.parent | ||
} | ||
|
||
/** | ||
* Returns the path component of [file] following the origin identifier according to the current | ||
* [DirectoryStructure](without file extension). | ||
* | ||
* At least one of [DirectoryStructure.getIdentifierFor] and | ||
* [DirectoryStructure.getAccountPartFor] will always return a non-null result. | ||
* | ||
* Examples: | ||
* - * --> null (EncryptedUsername) | ||
* - work/example.org/[email protected] --> [email protected] (FileBased) | ||
* - example.org.gpg --> null (FileBased, fallback) | ||
* - work/example.org/[email protected]/password.gpg --> [email protected]/password (DirectoryBased) | ||
* - Temporary PIN.gpg --> Temporary PIN (DirectoryBased, fallback) | ||
*/ | ||
fun getAccountPartFor(file: File): String? = | ||
when (this) { | ||
EncryptedUsername -> null | ||
FileBased -> file.nameWithoutExtension.takeIf { file.parentFile != null } | ||
DirectoryBased -> | ||
file.parentFile?.let { parentFile -> "${parentFile.name}/${file.nameWithoutExtension}" } | ||
?: file.nameWithoutExtension | ||
} | ||
|
||
fun getSaveFolderName(sanitizedIdentifier: String, username: String?) = | ||
when (this) { | ||
EncryptedUsername -> "/" | ||
FileBased -> sanitizedIdentifier | ||
DirectoryBased -> Paths.get(sanitizedIdentifier, username ?: "username").toString() | ||
} | ||
|
||
fun getSaveFileName(username: String?, identifier: String) = | ||
when (this) { | ||
EncryptedUsername -> identifier | ||
FileBased -> username | ||
DirectoryBased -> "password" | ||
} | ||
|
||
companion object { | ||
|
||
val DEFAULT = FileBased | ||
|
||
private val reverseMap = entries.associateBy { it.value } | ||
|
||
fun fromValue(value: String?) = if (value != null) reverseMap[value] ?: DEFAULT else DEFAULT | ||
} | ||
} | ||
|
||
object AutofillPreferences { | ||
|
||
fun directoryStructure(context: Context): DirectoryStructure { | ||
val value = context.sharedPrefs.getString(PreferenceKeys.OREO_AUTOFILL_DIRECTORY_STRUCTURE) | ||
val value = context.sharedPrefs.getString(PreferenceKeys.DIRECTORY_STRUCTURE) | ||
return DirectoryStructure.fromValue(value) | ||
} | ||
|
||
|
117 changes: 117 additions & 0 deletions
117
app/src/main/java/app/passwordstore/util/settings/DirectoryStructure.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Copyright © 2014-2024 The Android Password Store Authors. All Rights Reserved. | ||
* SPDX-License-Identifier: GPL-3.0-only | ||
*/ | ||
package app.passwordstore.util.settings | ||
|
||
import java.io.File | ||
import java.nio.file.Paths | ||
|
||
enum class DirectoryStructure(val value: String) { | ||
EncryptedUsername("encrypted_username"), | ||
FileBased("file"), | ||
DirectoryBased("directory"); | ||
|
||
/** | ||
* Returns the username associated to [file], following the convention of the current | ||
* [DirectoryStructure]. | ||
* | ||
* Examples: | ||
* - * --> null (EncryptedUsername) | ||
* - work/example.org/[email protected] --> [email protected] (FileBased) | ||
* - work/example.org/[email protected]/password.gpg --> [email protected] (DirectoryBased) | ||
* - Temporary PIN.gpg --> Temporary PIN (DirectoryBased, fallback) | ||
*/ | ||
fun getUsernameFor(file: File): String? = | ||
when (this) { | ||
EncryptedUsername -> null | ||
FileBased -> file.nameWithoutExtension | ||
DirectoryBased -> file.parentFile?.name ?: file.nameWithoutExtension | ||
} | ||
|
||
/** | ||
* Returns the origin identifier associated to [file], following the convention of the current | ||
* [DirectoryStructure]. | ||
* | ||
* At least one of [DirectoryStructure.getIdentifierFor] and | ||
* [DirectoryStructure.getAccountPartFor] will always return a non-null result. | ||
* | ||
* Examples: | ||
* - work/example.org.gpg --> example.org (EncryptedUsername) | ||
* - work/example.org/[email protected] --> example.org (FileBased) | ||
* - example.org.gpg --> example.org (FileBased, fallback) | ||
* - work/example.org/[email protected]/password.gpg --> example.org (DirectoryBased) | ||
* - Temporary PIN.gpg --> null (DirectoryBased) | ||
*/ | ||
fun getIdentifierFor(file: File): String? = | ||
when (this) { | ||
EncryptedUsername -> file.nameWithoutExtension | ||
FileBased -> file.parentFile?.name ?: file.nameWithoutExtension | ||
DirectoryBased -> file.parentFile?.parent | ||
} | ||
|
||
/** | ||
* Returns the path components of [file] until right before the component that contains the origin | ||
* identifier according to the current [DirectoryStructure]. | ||
* | ||
* Examples: | ||
* - work/example.org.gpg --> work (EncryptedUsername) | ||
* - work/example.org/[email protected] --> work (FileBased) | ||
* - example.org/[email protected] --> null (FileBased) | ||
* - [email protected] --> null (FileBased) | ||
* - work/example.org/[email protected]/password.gpg --> work (DirectoryBased) | ||
* - example.org/[email protected]/password.gpg --> null (DirectoryBased) | ||
*/ | ||
fun getPathToIdentifierFor(file: File): String? = | ||
when (this) { | ||
EncryptedUsername -> file.parent | ||
FileBased -> file.parentFile?.parent | ||
DirectoryBased -> file.parentFile?.parentFile?.parent | ||
} | ||
|
||
/** | ||
* Returns the path component of [file] following the origin identifier according to the current | ||
* [DirectoryStructure](without file extension). | ||
* | ||
* At least one of [DirectoryStructure.getIdentifierFor] and | ||
* [DirectoryStructure.getAccountPartFor] will always return a non-null result. | ||
* | ||
* Examples: | ||
* - * --> null (EncryptedUsername) | ||
* - work/example.org/[email protected] --> [email protected] (FileBased) | ||
* - example.org.gpg --> null (FileBased, fallback) | ||
* - work/example.org/[email protected]/password.gpg --> [email protected]/password (DirectoryBased) | ||
* - Temporary PIN.gpg --> Temporary PIN (DirectoryBased, fallback) | ||
*/ | ||
fun getAccountPartFor(file: File): String? = | ||
when (this) { | ||
EncryptedUsername -> null | ||
FileBased -> file.nameWithoutExtension.takeIf { file.parentFile != null } | ||
DirectoryBased -> | ||
file.parentFile?.let { parentFile -> "${parentFile.name}/${file.nameWithoutExtension}" } | ||
?: file.nameWithoutExtension | ||
} | ||
|
||
fun getSaveFolderName(sanitizedIdentifier: String, username: String?) = | ||
when (this) { | ||
EncryptedUsername -> "/" | ||
FileBased -> sanitizedIdentifier | ||
DirectoryBased -> Paths.get(sanitizedIdentifier, username ?: "username").toString() | ||
} | ||
|
||
fun getSaveFileName(username: String?, identifier: String) = | ||
when (this) { | ||
EncryptedUsername -> identifier | ||
FileBased -> username | ||
DirectoryBased -> "password" | ||
} | ||
|
||
companion object { | ||
|
||
val DEFAULT = FileBased | ||
|
||
private val reverseMap = entries.associateBy { it.value } | ||
|
||
fun fromValue(value: String?) = if (value != null) reverseMap[value] ?: DEFAULT else DEFAULT | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.