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 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 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 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 _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; }