[SHELLFIND] Add "Open Containing Folder" to right-click menu
[reactos.git] / dll / win32 / browseui / shellfind / CFindFolder.cpp
1 /*
2 * PROJECT: ReactOS Search Shell Extension
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Search results folder
5 * COPYRIGHT: Copyright 2019 Brock Mammen
6 */
7
8 #include "CFindFolder.h"
9
10 WINE_DEFAULT_DEBUG_CHANNEL(shellfind);
11
12 // FIXME: Remove this declaration after the function has been fully implemented
13 EXTERN_C HRESULT
14 WINAPI
15 SHOpenFolderAndSelectItems(LPITEMIDLIST pidlFolder,
16 UINT cidl,
17 PCUITEMID_CHILD_ARRAY apidl,
18 DWORD dwFlags);
19
20 struct FolderViewColumns
21 {
22 LPCWSTR wzColumnName;
23 DWORD dwDefaultState;
24 int fmt;
25 int cxChar;
26 };
27
28 static FolderViewColumns g_ColumnDefs[] =
29 {
30 {L"Name", SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_LEFT, 30},
31 {L"In Folder", SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_LEFT, 30},
32 {L"Relevance", SHCOLSTATE_TYPE_STR, LVCFMT_LEFT, 0}
33 };
34
35 static LPITEMIDLIST _ILCreate(LPCWSTR lpszPath, LPCITEMIDLIST lpcFindDataPidl)
36 {
37 int pathLen = (wcslen(lpszPath) + 1) * sizeof(WCHAR);
38 int cbData = sizeof(WORD) + pathLen + lpcFindDataPidl->mkid.cb;
39 LPITEMIDLIST pidl = (LPITEMIDLIST) SHAlloc(cbData + sizeof(WORD));
40 if (!pidl)
41 return NULL;
42
43 LPBYTE p = (LPBYTE) pidl;
44 *((WORD *) p) = cbData;
45 p += sizeof(WORD);
46
47 memcpy(p, lpszPath, pathLen);
48 p += pathLen;
49
50 memcpy(p, lpcFindDataPidl, lpcFindDataPidl->mkid.cb);
51 p += lpcFindDataPidl->mkid.cb;
52
53 *((WORD *) p) = 0;
54
55 return pidl;
56 }
57
58 static LPCWSTR _ILGetPath(LPCITEMIDLIST pidl)
59 {
60 if (!pidl || !pidl->mkid.cb)
61 return NULL;
62 return (LPCWSTR) pidl->mkid.abID;
63 }
64
65 static LPCITEMIDLIST _ILGetFSPidl(LPCITEMIDLIST pidl)
66 {
67 if (!pidl || !pidl->mkid.cb)
68 return pidl;
69 return (LPCITEMIDLIST) ((LPBYTE) pidl->mkid.abID
70 + ((wcslen((LPCWSTR) pidl->mkid.abID) + 1) * sizeof(WCHAR)));
71 }
72
73 LRESULT CFindFolder::AddItem(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
74 {
75 if (!lParam)
76 return 0;
77
78 HRESULT hr;
79 LPWSTR path = (LPWSTR) lParam;
80
81 CComPtr<IShellFolder> pShellFolder;
82 hr = SHGetDesktopFolder(&pShellFolder);
83 if (FAILED_UNEXPECTEDLY(hr))
84 {
85 LocalFree(path);
86 return hr;
87 }
88
89 LPITEMIDLIST lpFSPidl;
90 DWORD pchEaten;
91 hr = pShellFolder->ParseDisplayName(NULL, NULL, path, &pchEaten, &lpFSPidl, NULL);
92 if (FAILED_UNEXPECTEDLY(hr))
93 {
94 LocalFree(path);
95 return hr;
96 }
97
98 LPITEMIDLIST lpLastFSPidl = ILFindLastID(lpFSPidl);
99 LPITEMIDLIST lpSearchPidl = _ILCreate(path, lpLastFSPidl);
100 ILFree(lpFSPidl);
101 LocalFree(path);
102 if (!lpSearchPidl)
103 {
104 return E_OUTOFMEMORY;
105 }
106
107 UINT uItemIndex;
108 hr = m_shellFolderView->AddObject(lpSearchPidl, &uItemIndex);
109 ILFree(lpSearchPidl);
110
111 return hr;
112 }
113
114 // *** IShellFolder2 methods ***
115 STDMETHODIMP CFindFolder::GetDefaultSearchGUID(GUID *pguid)
116 {
117 UNIMPLEMENTED;
118 return E_NOTIMPL;
119 }
120
121 STDMETHODIMP CFindFolder::EnumSearches(IEnumExtraSearch **ppenum)
122 {
123 UNIMPLEMENTED;
124 return E_NOTIMPL;
125 }
126
127 STDMETHODIMP CFindFolder::GetDefaultColumn(DWORD, ULONG *pSort, ULONG *pDisplay)
128 {
129 if (pSort)
130 *pSort = 0;
131 if (pDisplay)
132 *pDisplay = 0;
133 return S_OK;
134 }
135
136 STDMETHODIMP CFindFolder::GetDefaultColumnState(UINT iColumn, DWORD *pcsFlags)
137 {
138 if (!pcsFlags)
139 return E_INVALIDARG;
140 if (iColumn >= _countof(g_ColumnDefs))
141 return m_pisfInner->GetDefaultColumnState(iColumn - _countof(g_ColumnDefs) + 1, pcsFlags);
142 *pcsFlags = g_ColumnDefs[iColumn].dwDefaultState;
143 return S_OK;
144 }
145
146 STDMETHODIMP CFindFolder::GetDetailsEx(PCUITEMID_CHILD pidl, const SHCOLUMNID *pscid, VARIANT *pv)
147 {
148 return m_pisfInner->GetDetailsEx(pidl, pscid, pv);
149 }
150
151 STDMETHODIMP CFindFolder::GetDetailsOf(PCUITEMID_CHILD pidl, UINT iColumn, SHELLDETAILS *pDetails)
152 {
153 if (iColumn >= _countof(g_ColumnDefs))
154 return m_pisfInner->GetDetailsOf(_ILGetFSPidl(pidl), iColumn - _countof(g_ColumnDefs) + 1, pDetails);
155
156 pDetails->cxChar = g_ColumnDefs[iColumn].cxChar;
157 pDetails->fmt = g_ColumnDefs[iColumn].fmt;
158
159 if (!pidl)
160 return SHSetStrRet(&pDetails->str, g_ColumnDefs[iColumn].wzColumnName);
161
162 if (iColumn == 1)
163 {
164 WCHAR path[MAX_PATH];
165 wcscpy(path, _ILGetPath(pidl));
166 PathRemoveFileSpecW(path);
167 return SHSetStrRet(&pDetails->str, path);
168 }
169
170 return GetDisplayNameOf(pidl, SHGDN_NORMAL, &pDetails->str);
171 }
172
173 STDMETHODIMP CFindFolder::MapColumnToSCID(UINT iColumn, SHCOLUMNID *pscid)
174 {
175 UNIMPLEMENTED;
176 return E_NOTIMPL;
177 }
178
179 // *** IShellFolder methods ***
180 STDMETHODIMP CFindFolder::ParseDisplayName(HWND hwndOwner, LPBC pbc, LPOLESTR lpszDisplayName, ULONG *pchEaten,
181 PIDLIST_RELATIVE *ppidl, ULONG *pdwAttributes)
182 {
183 UNIMPLEMENTED;
184 return E_NOTIMPL;
185 }
186
187 STDMETHODIMP CFindFolder::EnumObjects(HWND hwndOwner, DWORD dwFlags, LPENUMIDLIST *ppEnumIDList)
188 {
189 *ppEnumIDList = NULL;
190 return S_FALSE;
191 }
192
193 STDMETHODIMP CFindFolder::BindToObject(PCUIDLIST_RELATIVE pidl, LPBC pbcReserved, REFIID riid, LPVOID *ppvOut)
194 {
195 UNIMPLEMENTED;
196 return E_NOTIMPL;
197 }
198
199 STDMETHODIMP CFindFolder::BindToStorage(PCUIDLIST_RELATIVE pidl, LPBC pbcReserved, REFIID riid, LPVOID *ppvOut)
200 {
201 UNIMPLEMENTED;
202 return E_NOTIMPL;
203 }
204
205 STDMETHODIMP CFindFolder::CompareIDs(LPARAM lParam, PCUIDLIST_RELATIVE pidl1, PCUIDLIST_RELATIVE pidl2)
206 {
207 return m_pisfInner->CompareIDs(lParam, _ILGetFSPidl(pidl1), _ILGetFSPidl(pidl2));
208 }
209
210 STDMETHODIMP CFindFolder::CreateViewObject(HWND hwndOwner, REFIID riid, LPVOID *ppvOut)
211 {
212 if (riid == IID_IShellView)
213 {
214 SFV_CREATE sfvparams = {};
215 sfvparams.cbSize = sizeof(SFV_CREATE);
216 sfvparams.pshf = this;
217 sfvparams.psfvcb = this;
218 HRESULT hr = SHCreateShellFolderView(&sfvparams, (IShellView **) ppvOut);
219 if (FAILED_UNEXPECTEDLY(hr))
220 {
221 return hr;
222 }
223
224 return ((IShellView * ) * ppvOut)->QueryInterface(IID_PPV_ARG(IShellFolderView, &m_shellFolderView));
225 }
226 return E_NOINTERFACE;
227 }
228
229 STDMETHODIMP CFindFolder::GetAttributesOf(UINT cidl, PCUITEMID_CHILD_ARRAY apidl, DWORD *rgfInOut)
230 {
231 CComHeapPtr<PCITEMID_CHILD> aFSPidl;
232 aFSPidl.Allocate(cidl);
233 for (UINT i = 0; i < cidl; i++)
234 {
235 aFSPidl[i] = _ILGetFSPidl(apidl[i]);
236 }
237
238 return m_pisfInner->GetAttributesOf(cidl, aFSPidl, rgfInOut);
239 }
240
241 STDMETHODIMP CFindFolder::GetUIObjectOf(HWND hwndOwner, UINT cidl, PCUITEMID_CHILD_ARRAY apidl, REFIID riid,
242 UINT *prgfInOut, LPVOID *ppvOut)
243 {
244 if (riid == IID_IDataObject && cidl == 1)
245 {
246 WCHAR path[MAX_PATH];
247 wcscpy(path, (LPCWSTR) apidl[0]->mkid.abID);
248 PathRemoveFileSpecW(path);
249 LPITEMIDLIST rootPidl = ILCreateFromPathW(path);
250 if (!rootPidl)
251 return E_OUTOFMEMORY;
252 PCITEMID_CHILD aFSPidl[1];
253 aFSPidl[0] = _ILGetFSPidl(apidl[0]);
254 return IDataObject_Constructor(hwndOwner, rootPidl, aFSPidl, cidl, (IDataObject **) ppvOut);
255 }
256
257 if (cidl <= 0)
258 {
259 return m_pisfInner->GetUIObjectOf(hwndOwner, cidl, apidl, riid, prgfInOut, ppvOut);
260 }
261
262 PCITEMID_CHILD *aFSPidl = new PCITEMID_CHILD[cidl];
263 for (UINT i = 0; i < cidl; i++)
264 {
265 aFSPidl[i] = _ILGetFSPidl(apidl[i]);
266 }
267
268 if (riid == IID_IContextMenu)
269 {
270 HKEY hKeys[16];
271 UINT cKeys = 0;
272 AddFSClassKeysToArray(aFSPidl[0], hKeys, &cKeys);
273
274 DEFCONTEXTMENU dcm;
275 dcm.hwnd = hwndOwner;
276 dcm.pcmcb = this;
277 dcm.pidlFolder = m_pidl;
278 dcm.psf = this;
279 dcm.cidl = cidl;
280 dcm.apidl = apidl;
281 dcm.cKeys = cKeys;
282 dcm.aKeys = hKeys;
283 dcm.punkAssociationInfo = NULL;
284 HRESULT hr = SHCreateDefaultContextMenu(&dcm, riid, ppvOut);
285 delete[] aFSPidl;
286
287 return hr;
288 }
289
290 HRESULT hr = m_pisfInner->GetUIObjectOf(hwndOwner, cidl, aFSPidl, riid, prgfInOut, ppvOut);
291 delete[] aFSPidl;
292
293 return hr;
294 }
295
296 STDMETHODIMP CFindFolder::GetDisplayNameOf(PCUITEMID_CHILD pidl, DWORD dwFlags, LPSTRRET pName)
297 {
298 return m_pisfInner->GetDisplayNameOf(_ILGetFSPidl(pidl), dwFlags, pName);
299 }
300
301 STDMETHODIMP CFindFolder::SetNameOf(HWND hwndOwner, PCUITEMID_CHILD pidl, LPCOLESTR lpName, DWORD dwFlags,
302 PITEMID_CHILD *pPidlOut)
303 {
304 UNIMPLEMENTED;
305 return E_NOTIMPL;
306 }
307
308 //// *** IShellFolderViewCB method ***
309 STDMETHODIMP CFindFolder::MessageSFVCB(UINT uMsg, WPARAM wParam, LPARAM lParam)
310 {
311 switch (uMsg)
312 {
313 case SFVM_DEFVIEWMODE:
314 {
315 FOLDERVIEWMODE *pViewMode = (FOLDERVIEWMODE *) lParam;
316 *pViewMode = FVM_DETAILS;
317 return S_OK;
318 }
319 case SFVM_WINDOWCREATED:
320 {
321 SubclassWindow((HWND) wParam);
322 return S_OK;
323 }
324 }
325 return E_NOTIMPL;
326 }
327
328 //// *** IContextMenuCB method ***
329 STDMETHODIMP CFindFolder::CallBack(IShellFolder *psf, HWND hwndOwner, IDataObject *pdtobj, UINT uMsg, WPARAM wParam, LPARAM lParam)
330 {
331 switch (uMsg)
332 {
333 case DFM_MERGECONTEXTMENU:
334 {
335 QCMINFO *pqcminfo = (QCMINFO *) lParam;
336 _InsertMenuItemW(pqcminfo->hmenu, pqcminfo->indexMenu++, TRUE, pqcminfo->idCmdFirst++, MFT_SEPARATOR, NULL, 0);
337 _InsertMenuItemW(pqcminfo->hmenu, pqcminfo->indexMenu++, TRUE, pqcminfo->idCmdFirst++, MFT_STRING, L"Open Containing Folder", MFS_ENABLED);
338 _InsertMenuItemW(pqcminfo->hmenu, pqcminfo->indexMenu++, TRUE, pqcminfo->idCmdFirst++, MFT_SEPARATOR, NULL, 0);
339 return S_OK;
340 }
341 case DFM_INVOKECOMMAND:
342 case DFM_INVOKECOMMANDEX:
343 {
344 if (wParam != 1)
345 break;
346
347 PCUITEMID_CHILD *apidl;
348 UINT cidl;
349 HRESULT hr = m_shellFolderView->GetSelectedObjects(&apidl, &cidl);
350 if (FAILED_UNEXPECTEDLY(hr))
351 return hr;
352
353 for (UINT i = 0; i < cidl; i++)
354 {
355 LPITEMIDLIST pidl;
356 DWORD attrs = 0;
357 hr = SHILCreateFromPathW((LPCWSTR) apidl[i]->mkid.abID, &pidl, &attrs);
358 if (SUCCEEDED(hr))
359 {
360 SHOpenFolderAndSelectItems(NULL, 1, &pidl, 0);
361 }
362 }
363
364 return S_OK;
365 }
366 case DFM_GETDEFSTATICID:
367 return S_FALSE;
368 }
369 return Shell_DefaultContextMenuCallBack(m_pisfInner, pdtobj);
370 }
371
372 //// *** IPersistFolder2 methods ***
373 STDMETHODIMP CFindFolder::GetCurFolder(LPITEMIDLIST *pidl)
374 {
375 *pidl = ILClone(m_pidl);
376 return S_OK;
377 }
378
379 // *** IPersistFolder methods ***
380 STDMETHODIMP CFindFolder::Initialize(LPCITEMIDLIST pidl)
381 {
382 m_pidl = ILClone(pidl);
383 if (!m_pidl)
384 return E_OUTOFMEMORY;
385
386 return SHELL32_CoCreateInitSF(m_pidl,
387 NULL,
388 NULL,
389 &CLSID_ShellFSFolder,
390 IID_PPV_ARG(IShellFolder2, &m_pisfInner));
391 }
392
393 // *** IPersist methods ***
394 STDMETHODIMP CFindFolder::GetClassID(CLSID *pClassId)
395 {
396 if (pClassId == NULL)
397 return E_INVALIDARG;
398 memcpy(pClassId, &CLSID_FindFolder, sizeof(CLSID));
399 return S_OK;
400 }