|
| 1 | +// Copyright (c) Files Community |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System.Runtime.CompilerServices; |
| 5 | +using Windows.Win32; |
| 6 | +using Windows.Win32.Foundation; |
| 7 | +using Windows.Win32.System.Com; |
| 8 | +using Windows.Win32.UI.Shell; |
| 9 | + |
| 10 | +namespace Files.App.Storage.Storables |
| 11 | +{ |
| 12 | + public sealed class WindowsBulkOperations : IDisposable |
| 13 | + { |
| 14 | + // Constants |
| 15 | + |
| 16 | + private const FILEOPERATION_FLAGS defaultOptions = FILEOPERATION_FLAGS.FOF_ALLOWUNDO | FILEOPERATION_FLAGS.FOF_NOCONFIRMMKDIR; |
| 17 | + |
| 18 | + // Fields |
| 19 | + |
| 20 | + private readonly ComPtr<IFileOperation> _pFileOperation; |
| 21 | + private readonly ComPtr<IFileOperationProgressSink> _pProgressSink; |
| 22 | + private readonly uint _progressSinkCookie; |
| 23 | + |
| 24 | + private int _disposedValue = 0; |
| 25 | + private FILEOPERATION_FLAGS _operationFlags = defaultOptions; |
| 26 | + private HWND _owner; |
| 27 | + |
| 28 | + // Events |
| 29 | + |
| 30 | + public event EventHandler<WindowsBulkOperationsEventArgs>? FinishOperations; |
| 31 | + public event EventHandler<WindowsBulkOperationsEventArgs>? PostCopyItem; |
| 32 | + public event EventHandler<WindowsBulkOperationsEventArgs>? PostDeleteItem; |
| 33 | + public event EventHandler<WindowsBulkOperationsEventArgs>? PostMoveItem; |
| 34 | + public event EventHandler<WindowsBulkOperationsEventArgs>? PostNewItem; |
| 35 | + public event EventHandler<WindowsBulkOperationsEventArgs>? PostRenameItem; |
| 36 | + public event EventHandler<WindowsBulkOperationsEventArgs>? PreCopyItem; |
| 37 | + public event EventHandler<WindowsBulkOperationsEventArgs>? PreDeleteItem; |
| 38 | + public event EventHandler<WindowsBulkOperationsEventArgs>? PreMoveItem; |
| 39 | + public event EventHandler<WindowsBulkOperationsEventArgs>? PreNewItem; |
| 40 | + public event EventHandler<WindowsBulkOperationsEventArgs>? PreRenameItem; |
| 41 | + public event EventHandler? StartOperations; |
| 42 | + public event ProgressChangedEventHandler? UpdateProgress; |
| 43 | + |
| 44 | + // Constructor |
| 45 | + |
| 46 | + public unsafe WindowsBulkOperations(HWND owner = default) |
| 47 | + { |
| 48 | + var clsid = typeof(FileOperation).GUID; |
| 49 | + var iid = typeof(IFileOperation).GUID; |
| 50 | + void* pObj = default; |
| 51 | + |
| 52 | + PInvoke.CoCreateInstance( |
| 53 | + &clsid, |
| 54 | + null, |
| 55 | + CLSCTX.CLSCTX_LOCAL_SERVER, |
| 56 | + &iid, |
| 57 | + (void**)_pFileOperation.GetAddressOf()); |
| 58 | + |
| 59 | + _owner = owner; |
| 60 | + if (owner != default) |
| 61 | + _pFileOperation.Get()->SetOwnerWindow(owner); |
| 62 | + |
| 63 | + _pProgressSink.Attach((IFileOperationProgressSink*)WindowsBulkOperationsSink.Create(this)); |
| 64 | + _pFileOperation.Get()->Advise(_pProgressSink.Get(), out var progressSinkCookie); |
| 65 | + _progressSinkCookie = progressSinkCookie; |
| 66 | + } |
| 67 | + |
| 68 | + public unsafe HRESULT CopyItem(ComPtr<IShellItem> psiItem, ComPtr<IShellItem> psiDestinationFolder, PCWSTR pszCopyName) |
| 69 | + { |
| 70 | + HRESULT hr = default; |
| 71 | + |
| 72 | + try |
| 73 | + { |
| 74 | + hr = _pFileOperation.Get()->CopyItem(psiItem.Get(), psiDestinationFolder.Get(), pszCopyName, _pProgressSink.Get()); |
| 75 | + hr.ThrowOnFailure(); |
| 76 | + } |
| 77 | + finally |
| 78 | + { |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + return hr; |
| 83 | + } |
| 84 | + |
| 85 | + public unsafe HRESULT DeleteItem(ComPtr<IShellItem> psiItem) |
| 86 | + { |
| 87 | + HRESULT hr = default; |
| 88 | + |
| 89 | + try |
| 90 | + { |
| 91 | + hr = _pFileOperation.Get()->DeleteItem(psiItem.Get(), _pProgressSink.Get()); |
| 92 | + hr.ThrowOnFailure(); |
| 93 | + } |
| 94 | + finally |
| 95 | + { |
| 96 | + } |
| 97 | + |
| 98 | + return hr; |
| 99 | + } |
| 100 | + |
| 101 | + public unsafe HRESULT MoveItem(ComPtr<IShellItem> psiItem, ComPtr<IShellItem> psiDestinationFolder, PCWSTR pszNewName) |
| 102 | + { |
| 103 | + HRESULT hr = default; |
| 104 | + |
| 105 | + try |
| 106 | + { |
| 107 | + hr = _pFileOperation.Get()->MoveItem(psiItem.Get(), psiDestinationFolder.Get(), pszNewName, _pProgressSink.Get()); |
| 108 | + hr.ThrowOnFailure(); |
| 109 | + } |
| 110 | + finally |
| 111 | + { |
| 112 | + } |
| 113 | + |
| 114 | + return hr; |
| 115 | + } |
| 116 | + |
| 117 | + public unsafe HRESULT NewItem(ComPtr<IShellItem> psiDestinationFolder, uint dwFileAttributes, PCWSTR pszName, PCWSTR pszTemplateName) |
| 118 | + { |
| 119 | + HRESULT hr = default; |
| 120 | + |
| 121 | + try |
| 122 | + { |
| 123 | + hr = _pFileOperation.Get()->NewItem(psiDestinationFolder.Get(), dwFileAttributes, pszName, pszTemplateName, _pProgressSink.Get()); |
| 124 | + hr.ThrowOnFailure(); |
| 125 | + } |
| 126 | + finally |
| 127 | + { |
| 128 | + } |
| 129 | + |
| 130 | + return hr; |
| 131 | + } |
| 132 | + |
| 133 | + public unsafe HRESULT RenameItem(ComPtr<IShellItem> psiItem, PCWSTR pszNewName) |
| 134 | + { |
| 135 | + HRESULT hr = default; |
| 136 | + |
| 137 | + try |
| 138 | + { |
| 139 | + hr = _pFileOperation.Get()->RenameItem(psiItem.Get(), pszNewName, _pProgressSink.Get()); |
| 140 | + hr.ThrowOnFailure(); |
| 141 | + } |
| 142 | + finally |
| 143 | + { |
| 144 | + } |
| 145 | + |
| 146 | + return hr; |
| 147 | + } |
| 148 | + |
| 149 | + public void Dispose() |
| 150 | + { |
| 151 | + _pFileOperation.Dispose(); |
| 152 | + _pProgressSink.Dispose(); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments