using AsyncEvent; using FileTime.Core.Interactions; using FileTime.Core.Models; using FileTime.Core.Providers; namespace FileTime.Providers.Smb { public class SmbContentProvider : IContentProvider { private IContainer? _parent; private readonly IInputInterface _inputInterface; private readonly List _rootContainers; private readonly IReadOnlyList _rootContainersReadOnly; private readonly IReadOnlyList? _items; private readonly IReadOnlyList? _elements = new List().AsReadOnly(); public string Name { get; } = "smb"; public string? FullName { get; } public bool IsHidden => false; public IContentProvider Provider => this; public AsyncEventHandler Refreshed { get; } = new(); public SmbContentProvider(IInputInterface inputInterface) { _rootContainers = new List(); _rootContainersReadOnly = _rootContainers.AsReadOnly(); _inputInterface = inputInterface; } public async Task CreateContainer(string name) { var fullName = "\\\\" + name; var container = _rootContainers.Find(c => c.Name == name); if (container == null) { container = new SmbServer(fullName, this, _inputInterface); _rootContainers.Add(container); } await Refresh(); return container; } public Task CreateElement(string name) { throw new NotSupportedException(); } public Task Delete() { throw new NotSupportedException(); } public Task GetByPath(string path) { throw new NotImplementedException(); } public IContainer? GetParent() => _parent; public async Task IsExists(string name) => (await GetItems())?.Any(i => i.Name == name) ?? false; public async Task Refresh() => await Refreshed.InvokeAsync(this, AsyncEventArgs.Empty); public bool CanHandlePath(string path) => path.StartsWith("smb://") || path.StartsWith(@"\\"); public void SetParent(IContainer container) => _parent = container; public Task> GetRootContainers(CancellationToken token = default) => Task.FromResult(_rootContainersReadOnly); public Task?> GetItems(CancellationToken token = default) => Task.FromResult(_items); public Task?> GetContainers(CancellationToken token = default) => Task.FromResult((IReadOnlyList?)_rootContainersReadOnly); public Task?> GetElements(CancellationToken token = default) => Task.FromResult(_elements); } }