Modern UI, Loading screen CanRunMessages
This commit is contained in:
157
src/Providers/FileTime.Providers.Local/Interop/WindowsInterop.cs
Normal file
157
src/Providers/FileTime.Providers.Local/Interop/WindowsInterop.cs
Normal file
@@ -0,0 +1,157 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace FileTime.Providers.Local.Interop
|
||||
{
|
||||
//https://stackoverflow.com/questions/3282418/send-a-file-to-the-recycle-bin
|
||||
public static class WindowsInterop
|
||||
{
|
||||
/// <summary>
|
||||
/// Possible flags for the SHFileOperation method.
|
||||
/// </summary>
|
||||
[Flags]
|
||||
public enum FileOperationFlags : ushort
|
||||
{
|
||||
/// <summary>
|
||||
/// Do not show a dialog during the process
|
||||
/// </summary>
|
||||
FOF_SILENT = 0x0004,
|
||||
/// <summary>
|
||||
/// Do not ask the user to confirm selection
|
||||
/// </summary>
|
||||
FOF_NOCONFIRMATION = 0x0010,
|
||||
/// <summary>
|
||||
/// Delete the file to the recycle bin. (Required flag to send a file to the bin
|
||||
/// </summary>
|
||||
FOF_ALLOWUNDO = 0x0040,
|
||||
/// <summary>
|
||||
/// Do not show the names of the files or folders that are being recycled.
|
||||
/// </summary>
|
||||
FOF_SIMPLEPROGRESS = 0x0100,
|
||||
/// <summary>
|
||||
/// Surpress errors, if any occur during the process.
|
||||
/// </summary>
|
||||
FOF_NOERRORUI = 0x0400,
|
||||
/// <summary>
|
||||
/// Warn if files are too big to fit in the recycle bin and will need
|
||||
/// to be deleted completely.
|
||||
/// </summary>
|
||||
FOF_WANTNUKEWARNING = 0x4000,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// File Operation Function Type for SHFileOperation
|
||||
/// </summary>
|
||||
public enum FileOperationType : uint
|
||||
{
|
||||
/// <summary>
|
||||
/// Move the objects
|
||||
/// </summary>
|
||||
FO_MOVE = 0x0001,
|
||||
/// <summary>
|
||||
/// Copy the objects
|
||||
/// </summary>
|
||||
FO_COPY = 0x0002,
|
||||
/// <summary>
|
||||
/// Delete (or recycle) the objects
|
||||
/// </summary>
|
||||
FO_DELETE = 0x0003,
|
||||
/// <summary>
|
||||
/// Rename the object(s)
|
||||
/// </summary>
|
||||
FO_RENAME = 0x0004,
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// SHFILEOPSTRUCT for SHFileOperation from COM
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
|
||||
private struct SHFILEOPSTRUCT
|
||||
{
|
||||
|
||||
public IntPtr hwnd;
|
||||
[MarshalAs(UnmanagedType.U4)]
|
||||
public FileOperationType wFunc;
|
||||
public string pFrom;
|
||||
public string pTo;
|
||||
public FileOperationFlags fFlags;
|
||||
[MarshalAs(UnmanagedType.Bool)]
|
||||
public bool fAnyOperationsAborted;
|
||||
public IntPtr hNameMappings;
|
||||
public string lpszProgressTitle;
|
||||
}
|
||||
|
||||
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
|
||||
private static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
|
||||
|
||||
/// <summary>
|
||||
/// Send file to recycle bin
|
||||
/// </summary>
|
||||
/// <param name="path">Location of directory or file to recycle</param>
|
||||
/// <param name="flags">FileOperationFlags to add in addition to FOF_ALLOWUNDO</param>
|
||||
public static bool Send(string path, FileOperationFlags flags)
|
||||
{
|
||||
try
|
||||
{
|
||||
var fs = new SHFILEOPSTRUCT
|
||||
{
|
||||
wFunc = FileOperationType.FO_DELETE,
|
||||
pFrom = path + '\0' + '\0',
|
||||
fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags
|
||||
};
|
||||
SHFileOperation(ref fs);
|
||||
return true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send file to recycle bin. Display dialog, display warning if files are too big to fit (FOF_WANTNUKEWARNING)
|
||||
/// </summary>
|
||||
/// <param name="path">Location of directory or file to recycle</param>
|
||||
public static bool Send(string path)
|
||||
{
|
||||
return Send(path, FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_WANTNUKEWARNING);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send file silently to recycle bin. Surpress dialog, surpress errors, delete if too large.
|
||||
/// </summary>
|
||||
/// <param name="path">Location of directory or file to recycle</param>
|
||||
public static bool MoveToRecycleBin(string path)
|
||||
{
|
||||
return Send(path, FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_NOERRORUI | FileOperationFlags.FOF_SILENT);
|
||||
|
||||
}
|
||||
|
||||
private static bool deleteFile(string path, FileOperationFlags flags)
|
||||
{
|
||||
try
|
||||
{
|
||||
var fs = new SHFILEOPSTRUCT
|
||||
{
|
||||
wFunc = FileOperationType.FO_DELETE,
|
||||
pFrom = path + '\0' + '\0',
|
||||
fFlags = flags
|
||||
};
|
||||
SHFileOperation(ref fs);
|
||||
return true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool DeleteCompletelySilent(string path)
|
||||
{
|
||||
return deleteFile(path,
|
||||
FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_NOERRORUI |
|
||||
FileOperationFlags.FOF_SILENT);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,10 +27,12 @@ namespace FileTime.Providers.Local
|
||||
public AsyncEventHandler Refreshed { get; } = new();
|
||||
|
||||
public bool IsCaseInsensitive { get; }
|
||||
public bool CanDelete => false;
|
||||
public SupportsDelete CanDelete => SupportsDelete.False;
|
||||
public bool CanRename => false;
|
||||
public IReadOnlyList<Exception> Exceptions { get; } = new List<Exception>().AsReadOnly();
|
||||
|
||||
public bool SupportsDirectoryLevelSoftDelete => false;
|
||||
|
||||
public LocalContentProvider(ILogger<LocalContentProvider> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
@@ -76,7 +78,7 @@ namespace FileTime.Providers.Local
|
||||
public Task<IElement> CreateElement(string name) => throw new NotSupportedException();
|
||||
public Task<bool> IsExists(string name) => Task.FromResult(_rootContainers.Any(i => i.Name == name));
|
||||
|
||||
public Task Delete() => throw new NotSupportedException();
|
||||
public Task Delete(bool hardDelete = false) => throw new NotSupportedException();
|
||||
|
||||
internal string NormalizePath(string path) => IsCaseInsensitive ? path.ToLower() : path;
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ using System.Runtime.InteropServices;
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Core.Providers;
|
||||
using FileTime.Providers.Local.Extensions;
|
||||
using FileTime.Providers.Local.Interop;
|
||||
using Mono.Unix;
|
||||
|
||||
namespace FileTime.Providers.Local
|
||||
@@ -24,7 +25,7 @@ namespace FileTime.Providers.Local
|
||||
public string Attributes => GetAttributes();
|
||||
|
||||
public DateTime CreatedAt => File.CreationTime;
|
||||
public bool CanDelete => true;
|
||||
public SupportsDelete CanDelete => SupportsDelete.True;
|
||||
public bool CanRename => true;
|
||||
|
||||
private readonly LocalFolder _parent;
|
||||
@@ -41,9 +42,16 @@ namespace FileTime.Providers.Local
|
||||
|
||||
public string GetPrimaryAttributeText() => File.Length.ToSizeString();
|
||||
|
||||
public Task Delete()
|
||||
public Task Delete(bool hardDelete = false)
|
||||
{
|
||||
File.Delete();
|
||||
if (hardDelete)
|
||||
{
|
||||
File.Delete();
|
||||
}
|
||||
else
|
||||
{
|
||||
WindowsInterop.MoveToRecycleBin(File.FullName);
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
public async Task Rename(string newName)
|
||||
|
||||
@@ -2,6 +2,7 @@ using System.Runtime.InteropServices;
|
||||
using AsyncEvent;
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Core.Providers;
|
||||
using FileTime.Providers.Local.Interop;
|
||||
|
||||
namespace FileTime.Providers.Local
|
||||
{
|
||||
@@ -23,7 +24,7 @@ namespace FileTime.Providers.Local
|
||||
public string FullName { get; }
|
||||
|
||||
public bool IsLoaded => _items != null;
|
||||
public bool CanDelete => true;
|
||||
public SupportsDelete CanDelete { get; }
|
||||
public bool CanRename => true;
|
||||
|
||||
public AsyncEventHandler Refreshed { get; } = new();
|
||||
@@ -33,6 +34,8 @@ namespace FileTime.Providers.Local
|
||||
public DateTime CreatedAt => Directory.CreationTime;
|
||||
public IReadOnlyList<Exception> Exceptions { get; }
|
||||
|
||||
public bool SupportsDirectoryLevelSoftDelete { get; }
|
||||
|
||||
public LocalFolder(DirectoryInfo directory, LocalContentProvider contentProvider, IContainer? parent)
|
||||
{
|
||||
Directory = directory;
|
||||
@@ -44,6 +47,12 @@ namespace FileTime.Providers.Local
|
||||
Name = directory.Name.TrimEnd(Path.DirectorySeparatorChar);
|
||||
FullName = parent?.FullName == null ? Name : parent.FullName + Constants.SeparatorChar + Name;
|
||||
Provider = contentProvider;
|
||||
|
||||
//TODO: Linux soft delete
|
||||
SupportsDirectoryLevelSoftDelete = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
|
||||
CanDelete = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
|
||||
? SupportsDelete.True
|
||||
: SupportsDelete.HardDeleteOnly;
|
||||
}
|
||||
|
||||
public IContainer? GetParent() => _parent;
|
||||
@@ -124,9 +133,16 @@ namespace FileTime.Providers.Local
|
||||
|
||||
public async Task<bool> IsExists(string name) => (await GetItems())?.Any(i => Provider.NormalizePath(i.Name) == Provider.NormalizePath(name)) ?? false;
|
||||
|
||||
public Task Delete()
|
||||
public Task Delete(bool hardDelete = false)
|
||||
{
|
||||
Directory.Delete(true);
|
||||
if (hardDelete)
|
||||
{
|
||||
Directory.Delete(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
WindowsInterop.MoveToRecycleBin(Directory.FullName);
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
public async Task Rename(string newName)
|
||||
|
||||
Reference in New Issue
Block a user