check if new file/dir names are valid

This commit is contained in:
tibbi 2016-11-17 16:23:49 +01:00
parent 83dc85e63b
commit fa4cadebb4
13 changed files with 38 additions and 27 deletions

View file

@ -19,24 +19,23 @@ import java.util.*
class CopyDialog(val activity: SimpleActivity, val files: ArrayList<File>, val copyMoveListener: CopyMoveTask.CopyMoveListener) { class CopyDialog(val activity: SimpleActivity, val files: ArrayList<File>, val copyMoveListener: CopyMoveTask.CopyMoveListener) {
init { init {
val context = activity val view = LayoutInflater.from(activity).inflate(R.layout.dialog_copy_move, null)
val view = LayoutInflater.from(context).inflate(R.layout.dialog_copy_move, null)
val sourcePath = files[0].parent.trimEnd('/') val sourcePath = files[0].parent.trimEnd('/')
var destinationPath = "" var destinationPath = ""
view.source.text = context.humanizePath(sourcePath) view.source.text = activity.humanizePath(sourcePath)
view.destination.setOnClickListener { view.destination.setOnClickListener {
PickAlbumDialog(activity, object : PickAlbumDialog.OnPickAlbumListener { PickAlbumDialog(activity, object : PickAlbumDialog.OnPickAlbumListener {
override fun onSuccess(path: String) { override fun onSuccess(path: String) {
destinationPath = path destinationPath = path
view.destination.text = context.humanizePath(path) view.destination.text = activity.humanizePath(path)
} }
}) })
} }
AlertDialog.Builder(context) AlertDialog.Builder(activity)
.setTitle(context.resources.getString(if (files.size == 1) R.string.copy_item else R.string.copy_items)) .setTitle(activity.resources.getString(if (files.size == 1) R.string.copy_item else R.string.copy_items))
.setView(view) .setView(view)
.setPositiveButton(R.string.ok, null) .setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)

View file

@ -12,23 +12,22 @@ import kotlinx.android.synthetic.main.dialog_album_picker.view.*
import java.util.* import java.util.*
class PickAlbumDialog(val activity: SimpleActivity, val listener: OnPickAlbumListener) : GetDirectoriesAsynctask.GetDirectoriesListener { class PickAlbumDialog(val activity: SimpleActivity, val listener: OnPickAlbumListener) : GetDirectoriesAsynctask.GetDirectoriesListener {
val context = activity.applicationContext
var dialog: AlertDialog var dialog: AlertDialog
var directoriesGrid: RecyclerView var directoriesGrid: RecyclerView
init { init {
val view = LayoutInflater.from(context).inflate(R.layout.dialog_album_picker, null) val view = LayoutInflater.from(activity).inflate(R.layout.dialog_album_picker, null)
directoriesGrid = view.directories_grid directoriesGrid = view.directories_grid
dialog = AlertDialog.Builder(activity) dialog = AlertDialog.Builder(activity)
.setTitle(context.resources.getString(R.string.select_destination)) .setTitle(activity.resources.getString(R.string.select_destination))
.setView(view) .setView(view)
.setPositiveButton(R.string.ok, null) .setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)
.create() .create()
dialog.show() dialog.show()
GetDirectoriesAsynctask(context, false, false, ArrayList<String>(), this).execute() GetDirectoriesAsynctask(activity, false, false, ArrayList<String>(), this).execute()
} }
override fun gotDirectories(dirs: ArrayList<Directory>) { override fun gotDirectories(dirs: ArrayList<Directory>) {

View file

@ -12,16 +12,14 @@ import java.io.File
import java.util.* import java.util.*
class RenameDirectoryDialog(val activity: SimpleActivity, val dir: File, val listener: OnRenameDirListener) { class RenameDirectoryDialog(val activity: SimpleActivity, val dir: File, val listener: OnRenameDirListener) {
val context = activity
init { init {
val view = LayoutInflater.from(context).inflate(R.layout.rename_directory, null) val view = LayoutInflater.from(activity).inflate(R.layout.rename_directory, null)
view.directory_name.setText(dir.name) view.directory_name.setText(dir.name)
view.directory_path.text = "${context.humanizePath(dir.parent)}/" view.directory_path.text = "${activity.humanizePath(dir.parent)}/"
AlertDialog.Builder(context) AlertDialog.Builder(activity)
.setTitle(context.resources.getString(R.string.rename_folder)) .setTitle(activity.resources.getString(R.string.rename_folder))
.setView(view) .setView(view)
.setPositiveButton(R.string.ok, null) .setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)
@ -30,12 +28,16 @@ class RenameDirectoryDialog(val activity: SimpleActivity, val dir: File, val lis
show() show()
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({ getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({
val newDirName = view.directory_name.value val newDirName = view.directory_name.value
if (newDirName.isEmpty()) { if (newDirName.isEmpty()) {
context.toast(R.string.rename_folder_empty) context.toast(R.string.rename_folder_empty)
return@setOnClickListener return@setOnClickListener
} }
if (!newDirName.isAValidFilename()) {
context.toast(R.string.invalid_name)
return@setOnClickListener
}
val updatedFiles = ArrayList<String>() val updatedFiles = ArrayList<String>()
updatedFiles.add(dir.absolutePath) updatedFiles.add(dir.absolutePath)
val newDir = File(dir.parent, newDirName) val newDir = File(dir.parent, newDirName)
@ -65,7 +67,7 @@ class RenameDirectoryDialog(val activity: SimpleActivity, val dir: File, val lis
} }
private fun sendSuccess(updatedFiles: ArrayList<String>, newDir: File) { private fun sendSuccess(updatedFiles: ArrayList<String>, newDir: File) {
context.toast(R.string.renaming_folder) activity.toast(R.string.renaming_folder)
val files = newDir.listFiles() val files = newDir.listFiles()
files.mapTo(updatedFiles) { it.absolutePath } files.mapTo(updatedFiles) { it.absolutePath }

View file

@ -13,8 +13,7 @@ import java.io.File
class RenameFileDialog(val activity: SimpleActivity, val file: File, val listener: OnRenameFileListener) { class RenameFileDialog(val activity: SimpleActivity, val file: File, val listener: OnRenameFileListener) {
init { init {
val context = activity val view = LayoutInflater.from(activity).inflate(R.layout.rename_file, null)
val view = LayoutInflater.from(context).inflate(R.layout.rename_file, null)
val fullName = file.name val fullName = file.name
val dotAt = fullName.lastIndexOf(".") val dotAt = fullName.lastIndexOf(".")
var name = fullName var name = fullName
@ -26,10 +25,10 @@ class RenameFileDialog(val activity: SimpleActivity, val file: File, val listene
} }
view.file_name.setText(name) view.file_name.setText(name)
view.file_path.text = "${context.humanizePath(file.parent)}/" view.file_path.text = "${activity.humanizePath(file.parent)}/"
AlertDialog.Builder(context) AlertDialog.Builder(activity)
.setTitle(context.resources.getString(R.string.rename_file)) .setTitle(activity.resources.getString(R.string.rename_file))
.setView(view) .setView(view)
.setPositiveButton(R.string.ok, null) .setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)
@ -45,6 +44,11 @@ class RenameFileDialog(val activity: SimpleActivity, val file: File, val listene
return@setOnClickListener return@setOnClickListener
} }
if (!fileName.isAValidFilename()) {
context.toast(R.string.invalid_name)
return@setOnClickListener
}
val newFile = File(file.parent, "$fileName.$extension") val newFile = File(file.parent, "$fileName.$extension")
if (context.needsStupidWritePermissions(file.absolutePath)) { if (context.needsStupidWritePermissions(file.absolutePath)) {

View file

@ -14,12 +14,11 @@ import kotlinx.android.synthetic.main.rename_file.view.*
class SaveAsDialog(val activity: Activity, val path: String, val listener: OnSaveAsListener) { class SaveAsDialog(val activity: Activity, val path: String, val listener: OnSaveAsListener) {
init { init {
val context = activity val view = LayoutInflater.from(activity).inflate(R.layout.dialog_save_as, null)
val view = LayoutInflater.from(context).inflate(R.layout.dialog_save_as, null)
view.file_name.setText(path.getFilenameFromPath()) view.file_name.setText(path.getFilenameFromPath())
AlertDialog.Builder(context) AlertDialog.Builder(activity)
.setTitle(context.resources.getString(R.string.save_as)) .setTitle(activity.resources.getString(R.string.save_as))
.setView(view) .setView(view)
.setPositiveButton(R.string.ok, null) .setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)

View file

@ -60,6 +60,7 @@
<string name="already_exists">A file with that name already exists</string> <string name="already_exists">A file with that name already exists</string>
<string name="moving_success_partial">Some files could not be moved</string> <string name="moving_success_partial">Some files could not be moved</string>
<string name="copying_success_partial">Some files could not be copied</string> <string name="copying_success_partial">Some files could not be copied</string>
<string name="invalid_name">Der Name enthält nicht erlaubte Zeichen</string>
<plurals name="folders_deleted"> <plurals name="folders_deleted">
<item quantity="one">1 Ordner gelöscht</item> <item quantity="one">1 Ordner gelöscht</item>

View file

@ -60,6 +60,7 @@
<string name="already_exists">A file with that name already exists</string> <string name="already_exists">A file with that name already exists</string>
<string name="moving_success_partial">Some files could not be moved</string> <string name="moving_success_partial">Some files could not be moved</string>
<string name="copying_success_partial">Some files could not be copied</string> <string name="copying_success_partial">Some files could not be copied</string>
<string name="invalid_name">The name contains invalid characters</string>
<plurals name="folders_deleted"> <plurals name="folders_deleted">
<item quantity="one">1 carpeta eliminada</item> <item quantity="one">1 carpeta eliminada</item>

View file

@ -60,6 +60,7 @@
<string name="already_exists">A file with that name already exists</string> <string name="already_exists">A file with that name already exists</string>
<string name="moving_success_partial">Some files could not be moved</string> <string name="moving_success_partial">Some files could not be moved</string>
<string name="copying_success_partial">Some files could not be copied</string> <string name="copying_success_partial">Some files could not be copied</string>
<string name="invalid_name">Il nome contiene caratteri non validi</string>
<plurals name="folders_deleted"> <plurals name="folders_deleted">
<item quantity="one">1 cartella eliminata</item> <item quantity="one">1 cartella eliminata</item>

View file

@ -60,6 +60,7 @@
<string name="already_exists">A file with that name already exists</string> <string name="already_exists">A file with that name already exists</string>
<string name="moving_success_partial">Some files could not be moved</string> <string name="moving_success_partial">Some files could not be moved</string>
<string name="copying_success_partial">Some files could not be copied</string> <string name="copying_success_partial">Some files could not be copied</string>
<string name="invalid_name">名前に無効な文字が含まれています</string>
<plurals name="folders_deleted"> <plurals name="folders_deleted">
<item quantity="one">1 フォルダーを削除しました</item> <item quantity="one">1 フォルダーを削除しました</item>

View file

@ -60,6 +60,7 @@
<string name="already_exists">Já existe um ficheiro com este nome</string> <string name="already_exists">Já existe um ficheiro com este nome</string>
<string name="moving_success_partial">Alguns ficheiros não foram movidos</string> <string name="moving_success_partial">Alguns ficheiros não foram movidos</string>
<string name="copying_success_partial">Alguns ficheiros não foram copiados</string> <string name="copying_success_partial">Alguns ficheiros não foram copiados</string>
<string name="invalid_name">O nome contém caracteres inválidos</string>
<plurals name="folders_deleted"> <plurals name="folders_deleted">
<item quantity="one">1 pasta apagada</item> <item quantity="one">1 pasta apagada</item>

View file

@ -60,6 +60,7 @@
<string name="already_exists">A file with that name already exists</string> <string name="already_exists">A file with that name already exists</string>
<string name="moving_success_partial">Some files could not be moved</string> <string name="moving_success_partial">Some files could not be moved</string>
<string name="copying_success_partial">Some files could not be copied</string> <string name="copying_success_partial">Some files could not be copied</string>
<string name="invalid_name">Namnet innehåller ogiltiga tecken</string>
<plurals name="folders_deleted"> <plurals name="folders_deleted">
<item quantity="one">1 mapp borttagen</item> <item quantity="one">1 mapp borttagen</item>

View file

@ -61,6 +61,7 @@
<string name="already_exists">相同的文件名已经存在</string> <string name="already_exists">相同的文件名已经存在</string>
<string name="moving_success_partial">相同文件不能移动</string> <string name="moving_success_partial">相同文件不能移动</string>
<string name="copying_success_partial">相同文件不能复制</string> <string name="copying_success_partial">相同文件不能复制</string>
<string name="invalid_name">The name contains invalid characters</string>
<plurals name="folders_deleted"> <plurals name="folders_deleted">
<item quantity="one">1 folder deleted</item> <item quantity="one">1 folder deleted</item>

View file

@ -60,6 +60,7 @@
<string name="already_exists">A file with that name already exists</string> <string name="already_exists">A file with that name already exists</string>
<string name="moving_success_partial">Some files could not be moved</string> <string name="moving_success_partial">Some files could not be moved</string>
<string name="copying_success_partial">Some files could not be copied</string> <string name="copying_success_partial">Some files could not be copied</string>
<string name="invalid_name">The name contains invalid characters</string>
<plurals name="folders_deleted"> <plurals name="folders_deleted">
<item quantity="one">1 folder deleted</item> <item quantity="one">1 folder deleted</item>