Skip to content

Commit abf9873

Browse files
committed
Created the new NativeStorable
1 parent fdada99 commit abf9873

File tree

4 files changed

+141
-9
lines changed

4 files changed

+141
-9
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) 2024 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
namespace Files.App.Storage.Storables
5+
{
6+
/// <summary>
7+
/// Represents a file object that is natively supported by Windows Shell API.
8+
/// </summary>
9+
public class INativeStorable : NativeStorable
10+
{
11+
/// <summary>
12+
/// Get a property value from this <see cref="INativeStorable"/>.
13+
/// </summary>
14+
/// <param name="id">The property ID (e.g. "System.Image.Dimensions").</param>
15+
/// <returns>Returns a valid value formatted with string; otherwise, returns <see cref="string.Empty"/>.</returns>
16+
public string GetPropertyAsync(string id);
17+
}
18+
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
// Copyright (c) 2024 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4-
using System.IO;
5-
64
namespace Files.App.Storage.Storables
75
{
86
/// <summary>
97
/// Represents a file object that is natively supported by Windows Shell API.
108
/// </summary>
11-
public class NativeFile : NativeStorable
9+
public class NativeFile : NativeStorable/*, IFile*/
1210
{
1311
}
1412
}
Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
11
// Copyright (c) 2024 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4-
using System.IO;
5-
using System.Runtime.CompilerServices;
6-
74
namespace Files.App.Storage.Storables
85
{
96
/// <summary>
107
/// Represents a folder object that is natively supported by Windows Shell API.
118
/// </summary>
12-
public class NativeFolder : NativeStorable
9+
public class NativeFolder : NativeStorable/*, IFolder*/
1310
{
11+
public async IAsyncEnumerable<IStorable> GetChildrenAsync()
12+
{
13+
foreach (var storable in GetChildren())
14+
{
15+
await Task.Yield();
16+
17+
yield return storable;
18+
}
19+
20+
unsafe IEnumerable<NativeStorable> GetChildren()
21+
{
22+
using ComPtr<IEnumShellItems> pEnumShellItems = default;
23+
fixed (Guid* pBHID = PInvoke.BHID_EnumItems)
24+
{
25+
hr = pRecycleBinFolderShellItem.Get()->BindToHandler(
26+
null,
27+
pBHID,
28+
(Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in IEnumShellItems.Guid)),
29+
(void**)pEnumShellItems.GetAddressOf());
30+
31+
ComPtr<IShellItem> pShellItem = default;
32+
while (pEnumShellItems.Get()->Next(1, pShellItem.GetAddressOf()) == HRESULT.S_OK)
33+
yield return NativeStorable(pShellItem);
34+
}
35+
}
36+
}
1437
}
1538
}
Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,107 @@
11
// Copyright (c) 2024 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

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;
59

610
namespace Files.App.Storage.Storables
711
{
812
/// <summary>
913
/// Represents a storable that is natively supported by Windows Shell API.
1014
/// </summary>
11-
public abstract class NativeStorable
15+
public abstract class NativeStorable, INativeStorable
1216
{
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+
}
13106
}
14107
}

0 commit comments

Comments
 (0)