|
| 1 | +// Copyright (c) Files Community |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using Windows.Win32; |
| 5 | +using Windows.Win32.Foundation; |
| 6 | +using Windows.Win32.Storage.FileSystem; |
| 7 | +using Windows.Win32.System.Com; |
| 8 | +using Windows.Win32.UI.Shell; |
| 9 | + |
| 10 | +namespace Files.App.Storage.Storables |
| 11 | +{ |
| 12 | + public sealed partial class WindowsBulkOperations : IDisposable |
| 13 | + { |
| 14 | + // Fields |
| 15 | + |
| 16 | + private readonly ComPtr<IFileOperation> _pFileOperation; |
| 17 | + private readonly ComPtr<IFileOperationProgressSink> _pProgressSink; |
| 18 | + private readonly uint _progressSinkCookie; |
| 19 | + |
| 20 | + private int _disposedValue = 0; |
| 21 | + |
| 22 | + // Events |
| 23 | + |
| 24 | + public event EventHandler<WindowsBulkOperationsEventArgs>? OperationsFinished; |
| 25 | + public event EventHandler<WindowsBulkOperationsEventArgs>? ItemCopied; |
| 26 | + public event EventHandler<WindowsBulkOperationsEventArgs>? ItemDeleted; |
| 27 | + public event EventHandler<WindowsBulkOperationsEventArgs>? ItemMoved; |
| 28 | + public event EventHandler<WindowsBulkOperationsEventArgs>? ItemCreated; |
| 29 | + public event EventHandler<WindowsBulkOperationsEventArgs>? ItemRenamed; |
| 30 | + public event EventHandler<WindowsBulkOperationsEventArgs>? ItemCopying; |
| 31 | + public event EventHandler<WindowsBulkOperationsEventArgs>? ItemDeleting; |
| 32 | + public event EventHandler<WindowsBulkOperationsEventArgs>? ItemMoving; |
| 33 | + public event EventHandler<WindowsBulkOperationsEventArgs>? ItemCreating; |
| 34 | + public event EventHandler<WindowsBulkOperationsEventArgs>? ItemRenaming; |
| 35 | + public event EventHandler? OperationsStarted; |
| 36 | + public event ProgressChangedEventHandler? ProgressUpdated; |
| 37 | + |
| 38 | + // Constructor |
| 39 | + |
| 40 | + public unsafe WindowsBulkOperations(HWND owner = default, FILEOPERATION_FLAGS flags = FILEOPERATION_FLAGS.FOF_ALLOWUNDO | FILEOPERATION_FLAGS.FOF_NOCONFIRMMKDIR) |
| 41 | + { |
| 42 | + var clsid = typeof(FileOperation).GUID; |
| 43 | + var iid = typeof(IFileOperation).GUID; |
| 44 | + |
| 45 | + HRESULT hr = PInvoke.CoCreateInstance( |
| 46 | + &clsid, |
| 47 | + null, |
| 48 | + CLSCTX.CLSCTX_LOCAL_SERVER, |
| 49 | + &iid, |
| 50 | + (void**)_pFileOperation.GetAddressOf()); |
| 51 | + |
| 52 | + if (owner != default) |
| 53 | + hr = _pFileOperation.Get()->SetOwnerWindow(owner); |
| 54 | + |
| 55 | + hr = _pFileOperation.Get()->SetOperationFlags(flags); |
| 56 | + |
| 57 | + _pProgressSink.Attach((IFileOperationProgressSink*)WindowsBulkOperationsSink.Create(this)); |
| 58 | + hr = _pFileOperation.Get()->Advise(_pProgressSink.Get(), out var progressSinkCookie); |
| 59 | + _progressSinkCookie = progressSinkCookie; |
| 60 | + } |
| 61 | + |
| 62 | + public unsafe HRESULT QueueCopyOperation(WindowsStorable targetItem, WindowsFolder destinationFolder, string? copyName) |
| 63 | + { |
| 64 | + fixed (char* pszCopyName = copyName) |
| 65 | + return _pFileOperation.Get()->CopyItem(targetItem.ThisPtr.Get(), destinationFolder.ThisPtr.Get(), pszCopyName, _pProgressSink.Get()); |
| 66 | + } |
| 67 | + |
| 68 | + public unsafe HRESULT QueueDeleteOperation(WindowsStorable targetItem) |
| 69 | + { |
| 70 | + return _pFileOperation.Get()->DeleteItem(targetItem.ThisPtr.Get(), _pProgressSink.Get()); |
| 71 | + } |
| 72 | + |
| 73 | + public unsafe HRESULT QueueMoveOperation(WindowsStorable targetItem, WindowsFolder destinationFolder, string? newName) |
| 74 | + { |
| 75 | + fixed (char* pszNewName = newName) |
| 76 | + return _pFileOperation.Get()->MoveItem(targetItem.ThisPtr.Get(), destinationFolder.ThisPtr.Get(), pszNewName, null); |
| 77 | + } |
| 78 | + |
| 79 | + public unsafe HRESULT QueueCreateOperation(WindowsFolder destinationFolder, FILE_FLAGS_AND_ATTRIBUTES fileAttributes, string name, string? templateName) |
| 80 | + { |
| 81 | + fixed (char* pszName = name, pszTemplateName = templateName) |
| 82 | + return _pFileOperation.Get()->NewItem(destinationFolder.ThisPtr.Get(), (uint)fileAttributes, pszName, pszTemplateName, _pProgressSink.Get()); |
| 83 | + } |
| 84 | + |
| 85 | + public unsafe HRESULT QueueRenameOperation(WindowsStorable targetItem, string newName) |
| 86 | + { |
| 87 | + fixed (char* pszNewName = newName) |
| 88 | + return _pFileOperation.Get()->RenameItem(targetItem.ThisPtr.Get(), pszNewName, _pProgressSink.Get()); |
| 89 | + } |
| 90 | + |
| 91 | + public unsafe HRESULT PerformAllOperations() |
| 92 | + { |
| 93 | + return _pFileOperation.Get()->PerformOperations(); |
| 94 | + } |
| 95 | + |
| 96 | + public unsafe void Dispose() |
| 97 | + { |
| 98 | + _pFileOperation.Get()->Unadvise(_progressSinkCookie); |
| 99 | + _pFileOperation.Dispose(); |
| 100 | + _pProgressSink.Dispose(); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments