|
1 | 1 | // Copyright (c) 2024 Files Community |
2 | 2 | // Licensed under the MIT License. See the LICENSE. |
3 | 3 |
|
4 | | -using System.IO; |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +using Windows.Win32; |
| 6 | +using Windows.Win32.Foundation; |
| 7 | +using Windows.Win32.System.Com; |
| 8 | +using Windows.Win32.UI.Shell; |
5 | 9 |
|
6 | 10 | namespace Files.App.Storage.Storables |
7 | 11 | { |
8 | 12 | /// <summary> |
9 | 13 | /// Represents a storable that is natively supported by Windows Shell API. |
10 | 14 | /// </summary> |
11 | | - public abstract class NativeStorable |
| 15 | + public abstract class NativeStorable, INativeStorable |
12 | 16 | { |
| 17 | + /// <inheritdoc/> |
| 18 | + /// <remarks> |
| 19 | + /// This must be a path that can be parsed by SHCreateItemFromParsingName. |
| 20 | + /// </remarks> |
| 21 | + public string Path { get; protected set; } |
| 22 | + |
| 23 | + /// <inheritdoc/> |
| 24 | + /// <remarks> |
| 25 | + /// This must be a path that can be parsed by SHParseDisplayName. |
| 26 | + /// </remarks> |
| 27 | + public string Name { get; protected set; } |
| 28 | + |
| 29 | + /// <inheritdoc/> |
| 30 | + public string Id { get; protected set; } // Won't use |
| 31 | + |
| 32 | + protected ComPtr<IShellItem> m_pShellItem { get; private set; } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Initializes an instance of <see cref="NativeStorable"/> class. |
| 36 | + /// </summary> |
| 37 | + /// <param name="path">Win32 file namespace, shell namespace, or UNC path.</param> |
| 38 | + public unsafe NativeFile(string path) |
| 39 | + { |
| 40 | + HRESULT hr = PInvoke.SHCreateItemFromParsingName( |
| 41 | + path, |
| 42 | + null, |
| 43 | + (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in IShellItem.Guid)), |
| 44 | + (void**)m_pShellItem.GetAddressOf()); |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Initializes an instance of <see cref="NativeStorable"/> class. |
| 49 | + /// </summary> |
| 50 | + /// <param name="shellGuid">An instance of GUID that represents a shell folder.</param> |
| 51 | + public unsafe NativeStorable(Guid shellGuid) |
| 52 | + { |
| 53 | + HRESULT hr = default; |
| 54 | + |
| 55 | + // For known folders |
| 56 | + fixed (Guid* pFolderId = shellGuid) |
| 57 | + { |
| 58 | + hr = PInvoke.SHGetKnownFolderItem( |
| 59 | + pFolderId, |
| 60 | + KNOWN_FOLDER_FLAG.KF_FLAG_DEFAULT, |
| 61 | + HANDLE.Null, |
| 62 | + (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in IShellItem.Guid)), |
| 63 | + (void**)m_pShellItem.GetAddressOf()); |
| 64 | + } |
| 65 | + |
| 66 | + if (hr == HRESULT.S_OK) |
| 67 | + return; |
| 68 | + |
| 69 | + string path = $"Shell:::{shellGuid.ToString("B")}"; |
| 70 | + |
| 71 | + hr = PInvoke.SHCreateItemFromParsingName( |
| 72 | + path, |
| 73 | + null, |
| 74 | + (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in IShellItem.Guid)), |
| 75 | + (void**)m_pShellItem.GetAddressOf()); |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Initializes an instance of <see cref="NativeStorable"/> class. |
| 80 | + /// </summary> |
| 81 | + /// <param name="pShellItem">An instance of <see cref="IShellItem"/>.</param> |
| 82 | + public NativeStorable(ComPtr<IShellItem> pShellItem) |
| 83 | + { |
| 84 | + m_pShellItem = pShellItem; |
| 85 | + } |
| 86 | + |
| 87 | + /// <inheritdoc/> |
| 88 | + public string GetPropertyAsync(string id) |
| 89 | + { |
| 90 | + using ComPtr<IShellItem2> pShellItem2 = default; |
| 91 | + hr = pShellItem.Get()->QueryInterface( |
| 92 | + (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in IShellItem2.Guid)), |
| 93 | + (void**)pShellItem2.GetAddressOf()); |
| 94 | + |
| 95 | + hr = PInvoke.PSGetPropertyKeyFromName( |
| 96 | + id, |
| 97 | + out var propertyKey); |
| 98 | + |
| 99 | + using ComHeapPtr<LPWSTR> pPropertyValue; |
| 100 | + hr = pShellItem2.Get()->GetString( |
| 101 | + propertyKey, |
| 102 | + (void**)pPropertyValue.GetAddressOf()); |
| 103 | + |
| 104 | + return szPropertyValue.Get()->ToString(); |
| 105 | + } |
13 | 106 | } |
14 | 107 | } |
0 commit comments