4733d4b615
Every row and tile menu gains Rename…, Move… and Delete… (folders now have a menu too). Rename prefills the name with the file's stem selected and applies the server's name rules inline. Move reuses the chat folder picker through a FolderPickerPurpose: titled for the entry, confirmed with Move here, never offering the entry's current folder and keeping a moved folder's own subtree out of reach. Delete confirms, and when the server reports a folder with contents asks once more before deleting recursively. Holding an entry and dropping it on a folder row or tile moves it there; the target highlights and refuses itself and ancestors. FilesController performs each request through files_mutations.dart, shows Renaming…/Moving…/Deleting… progress, reloads the shown folder afterwards and keeps a refusal in the server's words in the panel's notice until dismissed.
55 lines
2.1 KiB
Dart
55 lines
2.1 KiB
Dart
import '../../../core/api_gateway.dart';
|
|
|
|
/// The phrase a `delete_path` refusal carries when the folder has contents and
|
|
/// the request was not recursive (the server's `protocol.FolderNotEmpty`).
|
|
const folderNotEmptyPhrase = 'is not empty';
|
|
|
|
bool isFolderNotEmptyError(Object error) =>
|
|
'$error'.contains(folderNotEmptyPhrase);
|
|
|
|
/// How one delete ended, so the panel can offer the recursive confirmation
|
|
/// only when the server asked for it.
|
|
enum FilesDeleteResult { deleted, notEmpty, failed }
|
|
|
|
/// Sends `rename_path`; resolves to the entry's new path.
|
|
Future<String> renamePath(ApiGateway gateway, String path, String name) =>
|
|
_mutate(gateway, 'rename_path', {'path': path, 'name': name});
|
|
|
|
/// Sends `move_path`; the entry keeps its name inside [into].
|
|
Future<String> movePath(ApiGateway gateway, String path, String into) =>
|
|
_mutate(gateway, 'move_path', {'path': path, 'into': into});
|
|
|
|
/// Sends `delete_path`; a folder with contents is refused unless [recursive].
|
|
Future<String> deletePath(
|
|
ApiGateway gateway,
|
|
String path, {
|
|
bool recursive = false,
|
|
}) => _mutate(gateway, 'delete_path', {
|
|
'path': path,
|
|
if (recursive) 'recursive': true,
|
|
});
|
|
|
|
/// Every mutation replies `path_mutated: {path}`; a refusal arrives as the
|
|
/// gateway's [ProtocolError] with the server's words and is left to the caller.
|
|
Future<String> _mutate(ApiGateway gateway, String type, JsonMap body) async {
|
|
final reply = await gateway.call(type, {type: body});
|
|
final result = reply['path_mutated'];
|
|
if (result is! Map || result['path'] is! String) {
|
|
throw StateError('The server did not confirm the $type request.');
|
|
}
|
|
return result['path'] as String;
|
|
}
|
|
|
|
/// Why [name] cannot be an entry's new name, or null when it can. The same
|
|
/// rules the server applies, so a refused name never leaves the dialog.
|
|
String? renameNameError(String name) {
|
|
final trimmed = name.trim();
|
|
if (trimmed.isEmpty) return 'Enter a name.';
|
|
if (trimmed == '.' || trimmed == '..') return 'That name is reserved.';
|
|
if (trimmed.contains('/') || trimmed.contains('\\')) {
|
|
return r'A name cannot contain / or \.';
|
|
}
|
|
if (trimmed.length > 255) return 'That name is too long.';
|
|
return null;
|
|
}
|