summaryrefslogtreecommitdiff
path: root/app/src/main
diff options
context:
space:
mode:
authorDiogenes Molinares <amolinares19@gmail.com>2020-06-14 11:31:43 +0200
committerGitHub <noreply@github.com>2020-06-14 15:01:43 +0530
commitfaff735a087241d0166e39ef2e39fd3f43305fa9 (patch)
tree3b74cc72107c63d0d4b2b7a73b69668ac1992bd3 /app/src/main
parentcd72d15b328cce62d0a6162533bd868222824f75 (diff)
Properly support password renaming (#852)
Co-authored-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main')
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/crypto/PasswordCreationActivity.kt67
-rw-r--r--app/src/main/res/values/strings.xml4
2 files changed, 52 insertions, 19 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/crypto/PasswordCreationActivity.kt b/app/src/main/java/com/zeapo/pwdstore/crypto/PasswordCreationActivity.kt
index 7216a506..c735659a 100644
--- a/app/src/main/java/com/zeapo/pwdstore/crypto/PasswordCreationActivity.kt
+++ b/app/src/main/java/com/zeapo/pwdstore/crypto/PasswordCreationActivity.kt
@@ -12,6 +12,7 @@ import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
+import androidx.core.view.isVisible
import androidx.core.widget.doOnTextChanged
import androidx.lifecycle.lifecycleScope
import com.github.ajalt.timberkt.e
@@ -29,9 +30,9 @@ import com.zeapo.pwdstore.utils.snackbar
import com.zeapo.pwdstore.utils.viewBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
-import org.eclipse.jgit.api.Git
import me.msfjarvis.openpgpktx.util.OpenPgpApi
import me.msfjarvis.openpgpktx.util.OpenPgpServiceConnection
+import org.eclipse.jgit.api.Git
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.File
@@ -45,12 +46,15 @@ class PasswordCreationActivity : BasePgpActivity(), OpenPgpServiceConnection.OnB
private val suggestedPass by lazy { intent.getStringExtra(EXTRA_PASSWORD) }
private val suggestedExtra by lazy { intent.getStringExtra(EXTRA_EXTRA_CONTENT) }
private val shouldGeneratePassword by lazy { intent.getBooleanExtra(EXTRA_GENERATE_PASSWORD, false) }
+ private val editing by lazy { intent.getBooleanExtra(EXTRA_EDITING, false) }
private val doNothing = registerForActivityResult(StartActivityForResult()) {}
+ private var oldCategory: String? = null
+ private val oldFileName by lazy { intent.getStringExtra(EXTRA_FILE_NAME) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
bindToOpenKeychain(this, doNothing)
- title = if (intent.getBooleanExtra(EXTRA_EDITING, false))
+ title = if (editing)
getString(R.string.edit_password)
else
getString(R.string.new_password_title)
@@ -68,8 +72,10 @@ class PasswordCreationActivity : BasePgpActivity(), OpenPgpServiceConnection.OnB
// Keep empty path field visible if it is editable.
if (path.isEmpty() && !isEnabled)
visibility = View.GONE
- else
+ else {
setText(path)
+ oldCategory = path
+ }
}
suggestedName?.let { filename.setText(it) }
// Allow the user to quickly switch between storing the username as the filename or
@@ -223,6 +229,11 @@ class PasswordCreationActivity : BasePgpActivity(), OpenPgpServiceConnection.OnB
OpenPgpApi.RESULT_CODE_SUCCESS -> {
try {
val file = File(path)
+ // If we're not editing, this file should not already exist!
+ if (!editing && file.exists()) {
+ snackbar(message = getString(R.string.password_creation_duplicate_error))
+ return@executeApiAsync
+ }
try {
file.outputStream().use {
it.write(outputStream.toByteArray())
@@ -231,7 +242,7 @@ class PasswordCreationActivity : BasePgpActivity(), OpenPgpServiceConnection.OnB
e(e) { "Failed to write password file" }
setResult(RESULT_CANCELED)
MaterialAlertDialogBuilder(this@PasswordCreationActivity)
- .setTitle(getString(R.string.password_creation_file_write_fail_title))
+ .setTitle(getString(R.string.password_creation_file_fail_title))
.setMessage(getString(R.string.password_creation_file_write_fail_message))
.setCancelable(false)
.setPositiveButton(android.R.string.ok) { _, _ ->
@@ -255,20 +266,40 @@ class PasswordCreationActivity : BasePgpActivity(), OpenPgpServiceConnection.OnB
returnIntent.putExtra(RETURN_EXTRA_USERNAME, username)
}
- val repo = PasswordRepository.getRepository(null)
- if (repo != null) {
- val status = Git(repo).status().call()
- if (status.modified.isNotEmpty()) {
- commitChange(
- getString(
- R.string.git_commit_edit_text,
- getLongName(fullPath, repoPath, editName)
- )
- )
- }
- }
- setResult(RESULT_OK, returnIntent)
- finish()
+ val repo = PasswordRepository.getRepository(null)
+ if (repo != null) {
+ val status = Git(repo).status().call()
+ if (status.modified.isNotEmpty()) {
+ commitChange(
+ getString(
+ R.string.git_commit_edit_text,
+ getLongName(fullPath, repoPath, editName)
+ )
+ )
+ }
+ }
+
+ if (category.isVisible && category.isEnabled) {
+ val oldFile = File("$repoPath/${oldCategory?.trim('/')}/$oldFileName.gpg")
+ if (oldFile.path != file.path && !oldFile.delete()) {
+ setResult(RESULT_CANCELED)
+ MaterialAlertDialogBuilder(this@PasswordCreationActivity)
+ .setTitle(R.string.password_creation_file_fail_title)
+ .setMessage(getString(R.string.password_creation_file_delete_fail_message, oldFileName))
+ .setCancelable(false)
+ .setPositiveButton(android.R.string.ok) { _, _ ->
+ finish()
+ }
+ .show()
+ } else {
+ setResult(RESULT_OK, returnIntent)
+ finish()
+ }
+ } else {
+ setResult(RESULT_OK, returnIntent)
+ finish()
+ }
+
} catch (e: Exception) {
e(e) { "An Exception occurred" }
}
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 4574c44a..db508815 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -364,6 +364,8 @@
<string name="openpgp_error_unknown">Error from OpenKeyChain : %s</string>
<!-- Password creation failure -->
- <string name="password_creation_file_write_fail_title">Error</string>
+ <string name="password_creation_file_fail_title">Error</string>
<string name="password_creation_file_write_fail_message">Failed to write password file to the store, please try again.</string>
+ <string name="password_creation_file_delete_fail_message">Failed to delete password file %1$s from the store, please delete it manually.</string>
+ <string name="password_creation_duplicate_error">File already exists, please use a different name</string>
</resources>