Skip to content

Commit 90b072a

Browse files
committed
Refactor PullJumpListFromExplorer logic
1 parent a8c65d4 commit 90b072a

File tree

1 file changed

+68
-69
lines changed

1 file changed

+68
-69
lines changed

src/Files.App.Storage/Windows/Managers/JumpListManager.cs

Lines changed: 68 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -51,28 +51,28 @@ private JumpListManager() { }
5151

5252
public HRESULT PullJumpListFromExplorer(int maxCount = 200)
5353
{
54-
// This method changes the jump list of Files, so we disable the watcher temporarily
55-
if (_filesADLStoreFileWatcher is not null && _filesADLStoreFileWatcher.EnableRaisingEvents)
56-
_filesADLStoreFileWatcher.EnableRaisingEvents = false;
57-
if (_filesCDLStoreFileWatcher is not null && _filesCDLStoreFileWatcher.EnableRaisingEvents)
58-
_filesCDLStoreFileWatcher.EnableRaisingEvents = false;
54+
try
55+
{
56+
// This method changes the jump list of Files, so we disable the watcher temporarily
57+
if (_filesADLStoreFileWatcher is not null && _filesADLStoreFileWatcher.EnableRaisingEvents)
58+
_filesADLStoreFileWatcher.EnableRaisingEvents = false;
59+
if (_filesCDLStoreFileWatcher is not null && _filesCDLStoreFileWatcher.EnableRaisingEvents)
60+
_filesCDLStoreFileWatcher.EnableRaisingEvents = false;
5961

60-
HRESULT hr;
62+
HRESULT hr;
6163

62-
using ComPtr<IAutomaticDestinationList> pExplorerADL = default;
63-
hr = PInvoke.CoCreateInstance(CLSID.CLSID_AutomaticDestinationList, null, CLSCTX.CLSCTX_INPROC_SERVER, IID.IID_IAutomaticDestinationList, (void**)pExplorerADL.GetAddressOf());
64-
fixed (char* pAumid = "Microsoft.Windows.Explorer") pExplorerADL.Get()->Initialize(pAumid, default, default);
64+
using ComPtr<IAutomaticDestinationList> pExplorerADL = default;
65+
hr = PInvoke.CoCreateInstance(CLSID.CLSID_AutomaticDestinationList, null, CLSCTX.CLSCTX_INPROC_SERVER, IID.IID_IAutomaticDestinationList, (void**)pExplorerADL.GetAddressOf());
66+
fixed (char* pAumid = "Microsoft.Windows.Explorer") pExplorerADL.Get()->Initialize(pAumid, default, default);
6567

66-
using ComPtr<IAutomaticDestinationList> pFilesADL = default;
67-
hr = PInvoke.CoCreateInstance(CLSID.CLSID_AutomaticDestinationList, null, CLSCTX.CLSCTX_INPROC_SERVER, IID.IID_IAutomaticDestinationList, (void**)pFilesADL.GetAddressOf());
68-
fixed (char* pAumid = _aumid) pFilesADL.Get()->Initialize(pAumid, default, default);
68+
using ComPtr<IAutomaticDestinationList> pFilesADL = default;
69+
hr = PInvoke.CoCreateInstance(CLSID.CLSID_AutomaticDestinationList, null, CLSCTX.CLSCTX_INPROC_SERVER, IID.IID_IAutomaticDestinationList, (void**)pFilesADL.GetAddressOf());
70+
fixed (char* pAumid = _aumid) pFilesADL.Get()->Initialize(pAumid, default, default);
6971

70-
using ComPtr<ICustomDestinationList> pFilesCDL = default;
71-
hr = PInvoke.CoCreateInstance(CLSID.CLSID_DestinationList, null, CLSCTX.CLSCTX_INPROC_SERVER, IID.IID_ICustomDestinationList, (void**)pFilesCDL.GetAddressOf());
72-
fixed (char* pAumid = _aumid) pFilesCDL.Get()->SetAppID(pAumid);
72+
using ComPtr<ICustomDestinationList> pFilesCDL = default;
73+
hr = PInvoke.CoCreateInstance(CLSID.CLSID_DestinationList, null, CLSCTX.CLSCTX_INPROC_SERVER, IID.IID_ICustomDestinationList, (void**)pFilesCDL.GetAddressOf());
74+
fixed (char* pAumid = _aumid) pFilesCDL.Get()->SetAppID(pAumid);
7375

74-
try
75-
{
7676
// Get whether the Files's Automatic Destination has items
7777
BOOL hasList = default;
7878
hr = pFilesADL.Get()->HasList(&hasList);
@@ -88,44 +88,12 @@ public HRESULT PullJumpListFromExplorer(int maxCount = 200)
8888
// Clear the Files' Custom Destination
8989
hr = pFilesCDL.Get()->DeleteList((PCWSTR)Unsafe.AsPointer(ref Unsafe.AsRef(in _aumid.GetPinnableReference())));
9090

91-
// Get the Explorer's Pinned items from its Automatic Destination
9291
using ComPtr<IObjectCollection> poc = default;
93-
hr = pExplorerADL.Get()->GetList(DESTLISTTYPE.PINNED, maxCount, GETDESTLISTFLAGS.NONE, IID.IID_IObjectCollection, (void**)poc.GetAddressOf());
94-
if (FAILED(hr)) return hr;
95-
96-
// Get the count of the Explorer's Pinned items
97-
uint dwItemsCount = 0U;
98-
hr = poc.Get()->GetCount(&dwItemsCount);
99-
if (FAILED(hr)) return hr;
100-
101-
for (uint dwIndex = 0; dwIndex < dwItemsCount; dwIndex++)
102-
{
103-
// Get an instance of IShellItem
104-
using ComPtr<IShellItem> psi = default;
105-
hr = poc.Get()->GetAt(dwIndex, IID.IID_IShellItem, (void**)psi.GetAddressOf());
106-
if (FAILED(hr)) continue;
107-
108-
// Get its pin index
109-
int pinIndex = 0;
110-
hr = pExplorerADL.Get()->GetPinIndex((IUnknown*)psi.Get(), &pinIndex);
111-
if (FAILED(hr)) continue;
112-
113-
// Get an instance of IShellLinkW from the IShellItem instance
114-
IShellLinkW* psl = default;
115-
hr = CreateLinkFromItem(psi.Get(), &psl);
116-
if (FAILED(hr)) continue;
117-
118-
// Pin it to the Files' Automatic Destinations
119-
hr = pFilesADL.Get()->PinItem((IUnknown*)psl, pinIndex);
120-
if (FAILED(hr)) continue;
121-
}
122-
123-
// Get the Explorer's Recent items from its Automatic Destination
124-
poc.Dispose();
12592
hr = pExplorerADL.Get()->GetList(DESTLISTTYPE.RECENT, maxCount, GETDESTLISTFLAGS.NONE, IID.IID_IObjectCollection, (void**)poc.GetAddressOf());
12693
if (FAILED(hr)) return hr;
12794

12895
// Get the count of the Explorer's Recent items
96+
uint dwItemsCount = 0U;
12997
hr = poc.Get()->GetCount(&dwItemsCount);
13098
if (FAILED(hr)) return hr;
13199

@@ -141,11 +109,6 @@ public HRESULT PullJumpListFromExplorer(int maxCount = 200)
141109
hr = poc.Get()->GetAt(dwIndex, IID.IID_IShellItem, (void**)psi.GetAddressOf());
142110
if (FAILED(hr)) continue;
143111

144-
// Try to get the pin index of the item. If it is not pinned, keep going
145-
int pinIndex = 0;
146-
hr = pExplorerADL.Get()->GetPinIndex((IUnknown*)psi.Get(), &pinIndex);
147-
if (SUCCEEDED(hr)) continue; // If not pinned, HRESULT is E_NOT_SET
148-
149112
// Get an instance of IShellLinkW from the IShellItem instance
150113
IShellLinkW* psl = default;
151114
hr = CreateLinkFromItem(psi.Get(), &psl);
@@ -178,6 +141,39 @@ public HRESULT PullJumpListFromExplorer(int maxCount = 200)
178141
hr = pFilesCDL.Get()->CommitList();
179142
if (FAILED(hr)) return hr;
180143

144+
// Get the Explorer's Recent items from its Automatic Destination
145+
poc.Dispose();
146+
147+
// Get the Explorer's Pinned items from its Automatic Destination
148+
hr = pExplorerADL.Get()->GetList(DESTLISTTYPE.PINNED, maxCount, GETDESTLISTFLAGS.NONE, IID.IID_IObjectCollection, (void**)poc.GetAddressOf());
149+
if (FAILED(hr)) return hr;
150+
151+
// Get the count of the Explorer's Pinned items
152+
hr = poc.Get()->GetCount(&dwItemsCount);
153+
if (FAILED(hr)) return hr;
154+
155+
for (uint dwIndex = 0; dwIndex < dwItemsCount; dwIndex++)
156+
{
157+
// Get an instance of IShellItem
158+
using ComPtr<IShellItem> psi = default;
159+
hr = poc.Get()->GetAt(dwIndex, IID.IID_IShellItem, (void**)psi.GetAddressOf());
160+
if (FAILED(hr)) continue;
161+
162+
// Get its pin index
163+
int pinIndex = 0;
164+
hr = pExplorerADL.Get()->GetPinIndex((IUnknown*)psi.Get(), &pinIndex);
165+
if (FAILED(hr)) continue;
166+
167+
// Get an instance of IShellLinkW from the IShellItem instance
168+
IShellLinkW* psl = default;
169+
hr = CreateLinkFromItem(psi.Get(), &psl);
170+
if (FAILED(hr)) continue;
171+
172+
// Pin it to the Files' Automatic Destinations
173+
hr = pFilesADL.Get()->PinItem((IUnknown*)psl, pinIndex);
174+
if (FAILED(hr)) continue;
175+
}
176+
181177
return hr;
182178
}
183179
finally
@@ -191,25 +187,25 @@ public HRESULT PullJumpListFromExplorer(int maxCount = 200)
191187

192188
public HRESULT PushJumpListToExplorer(int maxCount = 200)
193189
{
194-
if (_explorerADLStoreFileWatcher is not null && _explorerADLStoreFileWatcher.EnableRaisingEvents)
195-
_explorerADLStoreFileWatcher.EnableRaisingEvents = false;
190+
try
191+
{
192+
if (_explorerADLStoreFileWatcher is not null && _explorerADLStoreFileWatcher.EnableRaisingEvents)
193+
_explorerADLStoreFileWatcher.EnableRaisingEvents = false;
196194

197-
HRESULT hr;
195+
HRESULT hr;
198196

199-
using ComPtr<IAutomaticDestinationList> pExplorerADL = default;
200-
hr = PInvoke.CoCreateInstance(CLSID.CLSID_AutomaticDestinationList, null, CLSCTX.CLSCTX_INPROC_SERVER, IID.IID_IAutomaticDestinationList, (void**)pExplorerADL.GetAddressOf());
201-
fixed (char* pAumid = "Microsoft.Windows.Explorer") pExplorerADL.Get()->Initialize(pAumid, default, default);
197+
using ComPtr<IAutomaticDestinationList> pExplorerADL = default;
198+
hr = PInvoke.CoCreateInstance(CLSID.CLSID_AutomaticDestinationList, null, CLSCTX.CLSCTX_INPROC_SERVER, IID.IID_IAutomaticDestinationList, (void**)pExplorerADL.GetAddressOf());
199+
fixed (char* pAumid = "Microsoft.Windows.Explorer") pExplorerADL.Get()->Initialize(pAumid, default, default);
202200

203-
using ComPtr<IAutomaticDestinationList> pFilesADL = default;
204-
hr = PInvoke.CoCreateInstance(CLSID.CLSID_AutomaticDestinationList, null, CLSCTX.CLSCTX_INPROC_SERVER, IID.IID_IAutomaticDestinationList, (void**)pFilesADL.GetAddressOf());
205-
fixed (char* pAumid = _aumid) pFilesADL.Get()->Initialize(pAumid, default, default);
201+
using ComPtr<IAutomaticDestinationList> pFilesADL = default;
202+
hr = PInvoke.CoCreateInstance(CLSID.CLSID_AutomaticDestinationList, null, CLSCTX.CLSCTX_INPROC_SERVER, IID.IID_IAutomaticDestinationList, (void**)pFilesADL.GetAddressOf());
203+
fixed (char* pAumid = _aumid) pFilesADL.Get()->Initialize(pAumid, default, default);
206204

207-
using ComPtr<IInternalCustomDestinationList> pFilesICDL = default;
208-
hr = PInvoke.CoCreateInstance(CLSID.CLSID_DestinationList, null, CLSCTX.CLSCTX_INPROC_SERVER, IID.IID_IInternalCustomDestinationList, (void**)pFilesICDL.GetAddressOf());
209-
fixed (char* pAumid = _aumid) pFilesICDL.Get()->SetApplicationID(pAumid);
205+
using ComPtr<IInternalCustomDestinationList> pFilesICDL = default;
206+
hr = PInvoke.CoCreateInstance(CLSID.CLSID_DestinationList, null, CLSCTX.CLSCTX_INPROC_SERVER, IID.IID_IInternalCustomDestinationList, (void**)pFilesICDL.GetAddressOf());
207+
fixed (char* pAumid = _aumid) pFilesICDL.Get()->SetApplicationID(pAumid);
210208

211-
try
212-
{
213209
// Get whether the Explorer's Automatic Destination has items
214210
BOOL hasList = default;
215211
hr = pExplorerADL.Get()->HasList(&hasList);
@@ -254,6 +250,7 @@ category.Type is not APPDESTCATEGORYTYPE.CUSTOM ||
254250
// Get the items in the "Recent" category
255251
using ComPtr<IObjectCollection> poc = default;
256252
hr = pFilesICDL.Get()->EnumerateCategoryDestinations(indexOfRecentCategory, IID.IID_IObjectCollection, (void**)poc.GetAddressOf());
253+
if (FAILED(hr)) return hr;
257254

258255
// Get the count of items in the "Recent" category
259256
uint countOfItems = 0U;
@@ -274,6 +271,7 @@ category.Type is not APPDESTCATEGORYTYPE.CUSTOM ||
274271
using ComHeapPtr<char> pszParseablePath = default;
275272
pszParseablePath.Allocate(PInvoke.MAX_PATH);
276273
hr = psl.Get()->GetArguments(pszParseablePath.Get(), (int)PInvoke.MAX_PATH);
274+
if (FAILED(hr)) return hr;
277275

278276
using ComHeapPtr<IShellItem> psi = default;
279277
hr = PInvoke.SHCreateItemFromParsingName(pszParseablePath.Get(), null, IID.IID_IShellItem, (void**)psi.GetAddressOf());
@@ -308,6 +306,7 @@ category.Type is not APPDESTCATEGORYTYPE.CUSTOM ||
308306
using ComHeapPtr<char> pszParseablePath = default;
309307
pszParseablePath.Allocate(PInvoke.MAX_PATH);
310308
hr = psl.Get()->GetArguments(pszParseablePath.Get(), (int)PInvoke.MAX_PATH);
309+
if (FAILED(hr)) return hr;
311310

312311
using ComHeapPtr<IShellItem> psi = default;
313312
hr = PInvoke.SHCreateItemFromParsingName(pszParseablePath.Get(), null, IID.IID_IShellItem, (void**)psi.GetAddressOf());

0 commit comments

Comments
 (0)