[SHELL32] -Simplify the drop target of CRecycleBin to let the data object generate...
[reactos.git] / reactos / dll / win32 / shell32 / folders / CRecycleBin.cpp
1 /*
2 * Trash virtual folder support. The trashing engine is implemented in trash.c
3 *
4 * Copyright (C) 2006 Mikolaj Zalewski
5 * Copyright (C) 2009 Andrew Hill
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include <precomp.h>
23
24 #include <mmsystem.h>
25 #include <ntquery.h>
26
27 #define MAX_PROPERTY_SHEET_PAGE 32
28
29 WINE_DEFAULT_DEBUG_CHANNEL(CRecycleBin);
30
31 typedef struct
32 {
33 int column_name_id;
34 const GUID *fmtId;
35 DWORD pid;
36 int pcsFlags;
37 int fmt;
38 int cxChars;
39 } columninfo;
40
41 static const columninfo RecycleBinColumns[] =
42 {
43 {IDS_SHV_COLUMN_NAME, &FMTID_Storage, PID_STG_NAME, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_LEFT, 30},
44 {IDS_SHV_COLUMN_DELFROM, &FMTID_Displaced, PID_DISPLACED_FROM, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_LEFT, 30},
45 {IDS_SHV_COLUMN_DELDATE, &FMTID_Displaced, PID_DISPLACED_DATE, SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_LEFT, 20},
46 {IDS_SHV_COLUMN_SIZE, &FMTID_Storage, PID_STG_SIZE, SHCOLSTATE_TYPE_INT | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 20},
47 {IDS_SHV_COLUMN_TYPE, &FMTID_Storage, PID_STG_STORAGETYPE, SHCOLSTATE_TYPE_INT | SHCOLSTATE_ONBYDEFAULT, LVCFMT_LEFT, 20},
48 {IDS_SHV_COLUMN_MODIFIED, &FMTID_Storage, PID_STG_WRITETIME, SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_LEFT, 20},
49 /* {"creation time", &FMTID_Storage, PID_STG_CREATETIME, SHCOLSTATE_TYPE_DATE, LVCFMT_LEFT, 20}, */
50 /* {"attribs", &FMTID_Storage, PID_STG_ATTRIBUTES, SHCOLSTATE_TYPE_STR, LVCFMT_LEFT, 20}, */
51 };
52
53 #define COLUMN_NAME 0
54 #define COLUMN_DELFROM 1
55 #define COLUMN_DATEDEL 2
56 #define COLUMN_SIZE 3
57 #define COLUMN_TYPE 4
58 #define COLUMN_MTIME 5
59
60 #define COLUMNS_COUNT 6
61
62 /*
63 * Recycle Bin folder
64 */
65
66 HRESULT CRecyclerExtractIcon_CreateInstance(LPCITEMIDLIST pidl, REFIID riid, LPVOID * ppvOut)
67 {
68 CComPtr<IDefaultExtractIconInit> initIcon;
69 HRESULT hr = SHCreateDefaultExtractIcon(IID_PPV_ARG(IDefaultExtractIconInit, &initIcon));
70 if (FAILED_UNEXPECTEDLY(hr))
71 return hr;
72
73 /* FIXME: This is completely unimplemented */
74 initIcon->SetNormalIcon(swShell32Name, 0);
75
76 return initIcon->QueryInterface(riid, ppvOut);
77 }
78
79 class CRecycleBinEnum :
80 public CEnumIDListBase
81 {
82 private:
83 public:
84 CRecycleBinEnum();
85 ~CRecycleBinEnum();
86 HRESULT WINAPI Initialize(DWORD dwFlags);
87 static BOOL WINAPI CBEnumRecycleBin(IN PVOID Context, IN HANDLE hDeletedFile);
88 BOOL WINAPI CBEnumRecycleBin(IN HANDLE hDeletedFile);
89
90 BEGIN_COM_MAP(CRecycleBinEnum)
91 COM_INTERFACE_ENTRY_IID(IID_IEnumIDList, IEnumIDList)
92 END_COM_MAP()
93 };
94
95 class CRecycleBinItemContextMenu :
96 public CComObjectRootEx<CComMultiThreadModelNoCS>,
97 public IContextMenu2
98 {
99 private:
100 LPITEMIDLIST apidl;
101 public:
102 CRecycleBinItemContextMenu();
103 ~CRecycleBinItemContextMenu();
104 HRESULT WINAPI Initialize(LPCITEMIDLIST pidl);
105
106 // IContextMenu
107 virtual HRESULT WINAPI QueryContextMenu(HMENU hMenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags);
108 virtual HRESULT WINAPI InvokeCommand(LPCMINVOKECOMMANDINFO lpcmi);
109 virtual HRESULT WINAPI GetCommandString(UINT_PTR idCommand, UINT uFlags, UINT *lpReserved, LPSTR lpszName, UINT uMaxNameLen);
110
111 // IContextMenu2
112 virtual HRESULT WINAPI HandleMenuMsg(UINT uMsg, WPARAM wParam, LPARAM lParam);
113
114 BEGIN_COM_MAP(CRecycleBinItemContextMenu)
115 COM_INTERFACE_ENTRY_IID(IID_IContextMenu, IContextMenu)
116 COM_INTERFACE_ENTRY_IID(IID_IContextMenu2, IContextMenu2)
117 END_COM_MAP()
118 };
119
120 typedef struct
121 {
122 PIDLRecycleStruct *pFileDetails;
123 HANDLE hDeletedFile;
124 BOOL bFound;
125 } SEARCH_CONTEXT, *PSEARCH_CONTEXT;
126
127 BOOL WINAPI CBSearchRecycleBin(IN PVOID Context, IN HANDLE hDeletedFile)
128 {
129 PSEARCH_CONTEXT pContext = (PSEARCH_CONTEXT)Context;
130
131 PDELETED_FILE_DETAILS_W pFileDetails;
132 DWORD dwSize;
133 BOOL ret;
134
135 if (!GetDeletedFileDetailsW(hDeletedFile,
136 0,
137 NULL,
138 &dwSize) &&
139 GetLastError() != ERROR_INSUFFICIENT_BUFFER)
140 {
141 ERR("GetDeletedFileDetailsW failed\n");
142 return FALSE;
143 }
144
145 pFileDetails = (DELETED_FILE_DETAILS_W *)SHAlloc(dwSize);
146 if (!pFileDetails)
147 {
148 ERR("No memory\n");
149 return FALSE;
150 }
151
152 if (!GetDeletedFileDetailsW(hDeletedFile,
153 dwSize,
154 pFileDetails,
155 NULL))
156 {
157 ERR("GetDeletedFileDetailsW failed\n");
158 SHFree(pFileDetails);
159 return FALSE;
160 }
161
162 ret = memcmp(pFileDetails, pContext->pFileDetails, dwSize);
163 if (!ret)
164 {
165 pContext->hDeletedFile = hDeletedFile;
166 pContext->bFound = TRUE;
167 }
168 else
169 CloseRecycleBinHandle(hDeletedFile);
170
171 SHFree(pFileDetails);
172 return ret;
173 }
174
175 static PIDLRecycleStruct * _ILGetRecycleStruct(LPCITEMIDLIST pidl)
176 {
177 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
178
179 if (pdata && pdata->type == 0x00)
180 return (PIDLRecycleStruct*) & (pdata->u.crecycle);
181
182 return NULL;
183 }
184
185 CRecycleBinEnum::CRecycleBinEnum()
186 {
187 }
188
189 CRecycleBinEnum::~CRecycleBinEnum()
190 {
191 }
192
193 HRESULT WINAPI CRecycleBinEnum::Initialize(DWORD dwFlags)
194 {
195 static LPCWSTR szDrive = L"C:\\";
196
197 if (dwFlags & SHCONTF_NONFOLDERS)
198 {
199 TRACE("Starting Enumeration\n");
200
201 if (!EnumerateRecycleBinW(szDrive /* FIXME */ , CBEnumRecycleBin, (PVOID)this))
202 {
203 WARN("Error: EnumerateCRecycleBinW failed\n");
204 return E_FAIL;
205 }
206 }
207 else
208 {
209 // do nothing
210 }
211 return S_OK;
212 }
213
214 static LPITEMIDLIST _ILCreateRecycleItem(PDELETED_FILE_DETAILS_W pFileDetails)
215 {
216 PIDLDATA tmp;
217 LPITEMIDLIST pidl;
218 PIDLRecycleStruct * p;
219 int size0 = (char*)&tmp.u.crecycle.szName - (char*)&tmp.u.crecycle;
220 int size = size0;
221
222 tmp.type = 0x00;
223 size += (wcslen(pFileDetails->FileName) + 1) * sizeof(WCHAR);
224
225 pidl = (LPITEMIDLIST)SHAlloc(size + 4);
226 if (!pidl)
227 return pidl;
228
229 pidl->mkid.cb = size + 2;
230 memcpy(pidl->mkid.abID, &tmp, 2 + size0);
231
232 p = &((PIDLDATA*)pidl->mkid.abID)->u.crecycle;
233 RtlCopyMemory(p, pFileDetails, sizeof(DELETED_FILE_DETAILS_W));
234 wcscpy(p->szName, pFileDetails->FileName);
235 *(WORD*)((char*)pidl + (size + 2)) = 0;
236 return pidl;
237 }
238
239 BOOL WINAPI CRecycleBinEnum::CBEnumRecycleBin(IN PVOID Context, IN HANDLE hDeletedFile)
240 {
241 return static_cast<CRecycleBinEnum *>(Context)->CBEnumRecycleBin(hDeletedFile);
242 }
243
244 BOOL WINAPI CRecycleBinEnum::CBEnumRecycleBin(IN HANDLE hDeletedFile)
245 {
246 PDELETED_FILE_DETAILS_W pFileDetails;
247 DWORD dwSize;
248 LPITEMIDLIST pidl = NULL;
249 BOOL ret;
250
251 if (!GetDeletedFileDetailsW(hDeletedFile,
252 0,
253 NULL,
254 &dwSize) &&
255 GetLastError() != ERROR_INSUFFICIENT_BUFFER)
256 {
257 ERR("GetDeletedFileDetailsW failed\n");
258 return FALSE;
259 }
260
261 pFileDetails = (DELETED_FILE_DETAILS_W *)SHAlloc(dwSize);
262 if (!pFileDetails)
263 {
264 ERR("No memory\n");
265 return FALSE;
266 }
267
268 if (!GetDeletedFileDetailsW(hDeletedFile,
269 dwSize,
270 pFileDetails,
271 NULL))
272 {
273 ERR("GetDeletedFileDetailsW failed\n");
274 SHFree(pFileDetails);
275 return FALSE;
276 }
277
278 pidl = _ILCreateRecycleItem(pFileDetails);
279 if (!pidl)
280 {
281 SHFree(pFileDetails);
282 return FALSE;
283 }
284
285 ret = AddToEnumList(pidl);
286
287 if (!ret)
288 SHFree(pidl);
289 SHFree(pFileDetails);
290 TRACE("Returning %d\n", ret);
291 CloseRecycleBinHandle(hDeletedFile);
292 return ret;
293 }
294
295 /**************************************************************************
296 * IContextMenu2 Bitbucket Item Implementation
297 */
298
299 CRecycleBinItemContextMenu::CRecycleBinItemContextMenu()
300 {
301 apidl = NULL;
302 }
303
304 CRecycleBinItemContextMenu::~CRecycleBinItemContextMenu()
305 {
306 ILFree(apidl);
307 }
308
309 HRESULT WINAPI CRecycleBinItemContextMenu::Initialize(LPCITEMIDLIST pidl)
310 {
311 apidl = ILClone(pidl);
312 if (apidl == NULL)
313 return E_OUTOFMEMORY;
314 return S_OK;
315 }
316
317 HRESULT WINAPI CRecycleBinItemContextMenu::QueryContextMenu(HMENU hMenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags)
318 {
319 WCHAR szBuffer[30] = {0};
320 ULONG Count = 1;
321
322 TRACE("(%p)->(hmenu=%p indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n", this, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
323
324 if (LoadStringW(shell32_hInstance, IDS_RESTORE, szBuffer, sizeof(szBuffer) / sizeof(WCHAR)))
325 {
326 szBuffer[(sizeof(szBuffer)/sizeof(WCHAR))-1] = L'\0';
327 _InsertMenuItemW(hMenu, indexMenu++, TRUE, idCmdFirst + Count, MFT_STRING, szBuffer, MFS_ENABLED);
328 Count++;
329 }
330
331 if (LoadStringW(shell32_hInstance, IDS_CUT, szBuffer, sizeof(szBuffer) / sizeof(WCHAR)))
332 {
333 _InsertMenuItemW(hMenu, indexMenu++, TRUE, idCmdFirst + Count++, MFT_SEPARATOR, NULL, MFS_ENABLED);
334 szBuffer[(sizeof(szBuffer)/sizeof(WCHAR))-1] = L'\0';
335 _InsertMenuItemW(hMenu, indexMenu++, TRUE, idCmdFirst + Count++, MFT_STRING, szBuffer, MFS_ENABLED);
336 }
337
338 if (LoadStringW(shell32_hInstance, IDS_DELETE, szBuffer, sizeof(szBuffer) / sizeof(WCHAR)))
339 {
340 szBuffer[(sizeof(szBuffer)/sizeof(WCHAR))-1] = L'\0';
341 _InsertMenuItemW(hMenu, indexMenu++, TRUE, idCmdFirst + Count++, MFT_SEPARATOR, NULL, MFS_ENABLED);
342 _InsertMenuItemW(hMenu, indexMenu++, TRUE, idCmdFirst + Count++, MFT_STRING, szBuffer, MFS_ENABLED);
343 }
344
345 if (LoadStringW(shell32_hInstance, IDS_PROPERTIES, szBuffer, sizeof(szBuffer) / sizeof(WCHAR)))
346 {
347 szBuffer[(sizeof(szBuffer)/sizeof(WCHAR))-1] = L'\0';
348 _InsertMenuItemW(hMenu, indexMenu++, TRUE, idCmdFirst + Count++, MFT_SEPARATOR, NULL, MFS_ENABLED);
349 _InsertMenuItemW(hMenu, indexMenu++, TRUE, idCmdFirst + Count, MFT_STRING, szBuffer, MFS_DEFAULT);
350 }
351
352 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, Count);
353 }
354
355 HRESULT WINAPI CRecycleBinItemContextMenu::InvokeCommand(LPCMINVOKECOMMANDINFO lpcmi)
356 {
357 SEARCH_CONTEXT Context;
358 static LPCWSTR szDrive = L"C:\\";
359
360 TRACE("(%p)->(invcom=%p verb=%p wnd=%p)\n", this, lpcmi, lpcmi->lpVerb, lpcmi->hwnd);
361
362 if (lpcmi->lpVerb == MAKEINTRESOURCEA(1) || lpcmi->lpVerb == MAKEINTRESOURCEA(5))
363 {
364 Context.pFileDetails = _ILGetRecycleStruct(apidl);
365 Context.bFound = FALSE;
366
367 EnumerateRecycleBinW(szDrive, CBSearchRecycleBin, (PVOID)&Context);
368 if (!Context.bFound)
369 return E_FAIL;
370
371 if (lpcmi->lpVerb == MAKEINTRESOURCEA(1))
372 {
373 /* restore file */
374 if (RestoreFile(Context.hDeletedFile))
375 return S_OK;
376 else
377 return E_FAIL;
378 }
379 else
380 {
381 DeleteFileHandleToRecycleBin(Context.hDeletedFile);
382 return E_NOTIMPL;
383 }
384 }
385 else if (lpcmi->lpVerb == MAKEINTRESOURCEA(3))
386 {
387 FIXME("implement cut\n");
388 return E_NOTIMPL;
389 }
390 else if (lpcmi->lpVerb == MAKEINTRESOURCEA(7))
391 {
392 FIXME("implement properties\n");
393 return E_NOTIMPL;
394 }
395
396 return S_OK;
397 }
398
399 HRESULT WINAPI CRecycleBinItemContextMenu::GetCommandString(UINT_PTR idCommand, UINT uFlags, UINT *lpReserved, LPSTR lpszName, UINT uMaxNameLen)
400 {
401 TRACE("(%p)->(idcom=%lx flags=%x %p name=%p len=%x)\n", this, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
402
403 return E_FAIL;
404 }
405
406 HRESULT WINAPI CRecycleBinItemContextMenu::HandleMenuMsg(UINT uMsg, WPARAM wParam, LPARAM lParam)
407 {
408 TRACE("CRecycleBin_IContextMenu2Item_HandleMenuMsg (%p)->(msg=%x wp=%lx lp=%lx)\n", this, uMsg, wParam, lParam);
409
410 return E_NOTIMPL;
411 }
412
413 /**************************************************************************
414 * registers clipboardformat once
415 */
416 void CRecycleBin::SF_RegisterClipFmt()
417 {
418 TRACE ("(%p)\n", this);
419
420 if (!cfShellIDList)
421 cfShellIDList = RegisterClipboardFormatW(CFSTR_SHELLIDLIST);
422 }
423
424 CRecycleBin::CRecycleBin()
425 {
426 pidl = NULL;
427 iIdEmpty = 0;
428 cfShellIDList = 0;
429 SF_RegisterClipFmt();
430 fAcceptFmt = FALSE;
431 }
432
433 CRecycleBin::~CRecycleBin()
434 {
435 /* InterlockedDecrement(&objCount);*/
436 SHFree(pidl);
437 }
438
439 /*************************************************************************
440 * RecycleBin IPersistFolder2 interface
441 */
442
443 HRESULT WINAPI CRecycleBin::GetClassID(CLSID *pClassID)
444 {
445 TRACE("(%p, %p)\n", this, pClassID);
446 if (pClassID == NULL)
447 return E_INVALIDARG;
448 memcpy(pClassID, &CLSID_RecycleBin, sizeof(CLSID));
449 return S_OK;
450 }
451
452 HRESULT WINAPI CRecycleBin::Initialize(LPCITEMIDLIST pidl)
453 {
454 TRACE("(%p, %p)\n", this, pidl);
455
456 SHFree((LPVOID)this->pidl);
457 this->pidl = ILClone(pidl);
458 if (this->pidl == NULL)
459 return E_OUTOFMEMORY;
460 return S_OK;
461 }
462
463 HRESULT WINAPI CRecycleBin::GetCurFolder(LPITEMIDLIST *ppidl)
464 {
465 TRACE("\n");
466 *ppidl = ILClone(pidl);
467 return S_OK;
468 }
469
470 /*************************************************************************
471 * RecycleBin IShellFolder2 interface
472 */
473
474 HRESULT WINAPI CRecycleBin::ParseDisplayName(HWND hwnd, LPBC pbc,
475 LPOLESTR pszDisplayName, ULONG *pchEaten, PIDLIST_RELATIVE *ppidl,
476 ULONG *pdwAttributes)
477 {
478 FIXME("stub\n");
479 return E_NOTIMPL;
480 }
481
482
483 PDELETED_FILE_DETAILS_W
484 UnpackDetailsFromPidl(LPCITEMIDLIST pidl)
485 {
486 return (PDELETED_FILE_DETAILS_W)&pidl->mkid.abID;
487 }
488
489 HRESULT WINAPI CRecycleBin::EnumObjects(HWND hwndOwner, DWORD dwFlags, LPENUMIDLIST *ppEnumIDList)
490 {
491 return ShellObjectCreatorInit<CRecycleBinEnum>(dwFlags, IID_PPV_ARG(IEnumIDList, ppEnumIDList));
492 }
493
494 HRESULT WINAPI CRecycleBin::BindToObject(PCUIDLIST_RELATIVE pidl, LPBC pbc, REFIID riid, void **ppv)
495 {
496 FIXME("(%p, %p, %p, %s, %p) - stub\n", this, pidl, pbc, debugstr_guid(&riid), ppv);
497 return E_NOTIMPL;
498 }
499
500 HRESULT WINAPI CRecycleBin::BindToStorage(PCUIDLIST_RELATIVE pidl, LPBC pbc, REFIID riid, void **ppv)
501 {
502 FIXME("(%p, %p, %p, %s, %p) - stub\n", this, pidl, pbc, debugstr_guid(&riid), ppv);
503 return E_NOTIMPL;
504 }
505
506 HRESULT WINAPI CRecycleBin::CompareIDs(LPARAM lParam, PCUIDLIST_RELATIVE pidl1, PCUIDLIST_RELATIVE pidl2)
507 {
508 PIDLRecycleStruct* pData1 = _ILGetRecycleStruct(pidl1);
509 PIDLRecycleStruct* pData2 = _ILGetRecycleStruct(pidl2);
510 LPWSTR pName1, pName2;
511
512 if(!pData1 || !pData2 || LOWORD(lParam) >= COLUMNS_COUNT)
513 return E_INVALIDARG;
514
515 SHORT result;
516 LONGLONG diff;
517 switch (LOWORD(lParam))
518 {
519 case 0: /* Name */
520 pName1 = PathFindFileNameW(pData1->szName);
521 pName2 = PathFindFileNameW(pData2->szName);
522 result = wcsicmp(pName1, pName2);
523 break;
524 case 1: /* Orig. Location */
525 result = wcsicmp(pData1->szName, pData2->szName);
526 break;
527 case 2: /* Date Deleted */
528 result = CompareFileTime(&pData1->DeletionTime, &pData2->DeletionTime);
529 break;
530 case 3: /* Size */
531 diff = pData1->FileSize.QuadPart - pData2->FileSize.QuadPart;
532 return MAKE_COMPARE_HRESULT(diff);
533 case 4: /* Type */
534 pName1 = PathFindExtensionW(pData1->szName);
535 pName2 = PathFindExtensionW(pData2->szName);
536 result = wcsicmp(pName1, pName2);
537 break;
538 case 5: /* Modified */
539 result = CompareFileTime(&pData1->LastModification, &pData2->LastModification);
540 break;
541 }
542 return MAKE_COMPARE_HRESULT(result);
543 }
544
545 HRESULT WINAPI CRecycleBin::CreateViewObject(HWND hwndOwner, REFIID riid, void **ppv)
546 {
547 CComPtr<IShellView> pShellView;
548 HRESULT hr = E_NOINTERFACE;
549
550 TRACE("(%p, %p, %s, %p)\n", this, hwndOwner, debugstr_guid(&riid), ppv);
551
552 if (!ppv)
553 return hr;
554
555 *ppv = NULL;
556
557 if (IsEqualIID (riid, IID_IDropTarget))
558 {
559 hr = this->QueryInterface (IID_IDropTarget, ppv);
560 }
561 else if (IsEqualIID (riid, IID_IContextMenu) || IsEqualIID (riid, IID_IContextMenu2))
562 {
563 hr = this->QueryInterface(riid, ppv);
564 }
565 else if (IsEqualIID (riid, IID_IShellView))
566 {
567 SFV_CREATE sfvparams = {sizeof(SFV_CREATE), this};
568 hr = SHCreateShellFolderView(&sfvparams, (IShellView**)ppv);
569 }
570 else
571 return hr;
572 TRACE ("-- (%p)->(interface=%p)\n", this, ppv);
573 return hr;
574
575 }
576
577 HRESULT WINAPI CRecycleBin::GetAttributesOf(UINT cidl, PCUITEMID_CHILD_ARRAY apidl,
578 SFGAOF *rgfInOut)
579 {
580 TRACE("(%p, %d, {%p, ...}, {%x})\n", this, cidl, apidl ? apidl[0] : NULL, (unsigned int)*rgfInOut);
581 *rgfInOut &= SFGAO_FOLDER|SFGAO_DROPTARGET|SFGAO_HASPROPSHEET|SFGAO_CANLINK;
582 return S_OK;
583 }
584
585 HRESULT WINAPI CRecycleBin::GetUIObjectOf(HWND hwndOwner, UINT cidl, PCUITEMID_CHILD_ARRAY apidl,
586 REFIID riid, UINT *prgfInOut, void **ppv)
587 {
588 LPVOID pObj = NULL;
589 HRESULT hr = E_INVALIDARG;
590
591 TRACE ("(%p)->(%p,%u,apidl=%p, %p %p)\n", this,
592 hwndOwner, cidl, apidl, prgfInOut, ppv);
593
594 if (!ppv)
595 return hr;
596
597 *ppv = NULL;
598
599 if ((IsEqualIID (riid, IID_IContextMenu) || IsEqualIID(riid, IID_IContextMenu2)) && (cidl >= 1))
600 {
601 hr = ShellObjectCreatorInit<CRecycleBinItemContextMenu>(apidl[0], riid, &pObj);
602 }
603 else if (IsEqualIID (riid, IID_IDropTarget) && (cidl == 1))
604 {
605 IDropTarget * pDt = NULL;
606 hr = QueryInterface(IID_PPV_ARG(IDropTarget, &pDt));
607 pObj = pDt;
608 }
609 else if((IsEqualIID(riid, IID_IExtractIconA) || IsEqualIID(riid, IID_IExtractIconW)) && (cidl == 1))
610 {
611 hr = CRecyclerExtractIcon_CreateInstance(apidl[0], riid, &pObj);
612 }
613 else
614 hr = E_NOINTERFACE;
615
616 if (SUCCEEDED(hr) && !pObj)
617 hr = E_OUTOFMEMORY;
618
619 *ppv = pObj;
620 TRACE ("(%p)->hr=0x%08x\n", this, hr);
621 return hr;
622 }
623
624 HRESULT WINAPI CRecycleBin::GetDisplayNameOf(PCUITEMID_CHILD pidl, SHGDNF uFlags, STRRET *pName)
625 {
626 PIDLRecycleStruct *pFileDetails;
627 LPWSTR pFileName;
628
629 TRACE("(%p, %p, %x, %p)\n", this, pidl, (unsigned int)uFlags, pName);
630
631 pFileDetails = _ILGetRecycleStruct(pidl);
632 if (!pFileDetails)
633 {
634 pName->cStr[0] = 0;
635 pName->uType = STRRET_CSTR;
636 return E_INVALIDARG;
637 }
638
639 pFileName = wcsrchr(pFileDetails->szName, L'\\');
640 if (!pFileName)
641 {
642 pName->cStr[0] = 0;
643 pName->uType = STRRET_CSTR;
644 return E_UNEXPECTED;
645 }
646
647 pName->pOleStr = StrDupW(pFileName + 1);
648 if (pName->pOleStr == NULL)
649 return E_OUTOFMEMORY;
650
651 pName->uType = STRRET_WSTR;
652 return S_OK;
653 }
654
655 HRESULT WINAPI CRecycleBin::SetNameOf(HWND hwnd, PCUITEMID_CHILD pidl, LPCOLESTR pszName,
656 SHGDNF uFlags, PITEMID_CHILD *ppidlOut)
657 {
658 TRACE("\n");
659 return E_FAIL; /* not supported */
660 }
661
662 HRESULT WINAPI CRecycleBin::GetDefaultSearchGUID(GUID *pguid)
663 {
664 FIXME("stub\n");
665 return E_NOTIMPL;
666 }
667
668 HRESULT WINAPI CRecycleBin::EnumSearches(IEnumExtraSearch **ppEnum)
669 {
670 FIXME("stub\n");
671 *ppEnum = NULL;
672 return E_NOTIMPL;
673 }
674
675 HRESULT WINAPI CRecycleBin::GetDefaultColumn(DWORD dwReserved, ULONG *pSort, ULONG *pDisplay)
676 {
677 TRACE("(%p, %x, %p, %p)\n", this, (unsigned int)dwReserved, pSort, pDisplay);
678 if (pSort)
679 *pSort = 0;
680 if (pDisplay)
681 *pDisplay = 0;
682 return S_OK;
683 }
684
685 HRESULT WINAPI CRecycleBin::GetDefaultColumnState(UINT iColumn, SHCOLSTATEF *pcsFlags)
686 {
687 TRACE("(%p, %d, %p)\n", this, iColumn, pcsFlags);
688 if (iColumn >= COLUMNS_COUNT)
689 return E_INVALIDARG;
690 *pcsFlags = RecycleBinColumns[iColumn].pcsFlags;
691 return S_OK;
692 }
693
694 HRESULT WINAPI CRecycleBin::GetDetailsEx(PCUITEMID_CHILD pidl, const SHCOLUMNID *pscid, VARIANT *pv)
695 {
696 FIXME("stub\n");
697 return E_NOTIMPL;
698 }
699
700 static HRESULT FormatDateTime(LPWSTR buffer, int size, FILETIME * ft)
701 {
702 FILETIME lft;
703 SYSTEMTIME time;
704 int ret;
705
706 FileTimeToLocalFileTime(ft, &lft);
707 FileTimeToSystemTime(&lft, &time);
708
709 ret = GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &time, NULL, buffer, size);
710 if (ret > 0 && ret < size)
711 {
712 /* Append space + time without seconds */
713 buffer[ret-1] = ' ';
714 GetTimeFormatW(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &time, NULL, &buffer[ret], size - ret);
715 }
716
717 return (ret != 0 ? E_FAIL : S_OK);
718 }
719
720 HRESULT WINAPI CRecycleBin::GetDetailsOf(PCUITEMID_CHILD pidl, UINT iColumn, LPSHELLDETAILS pDetails)
721 {
722 PIDLRecycleStruct * pFileDetails;
723 WCHAR buffer[MAX_PATH];
724 WCHAR szTypeName[100];
725 LPWSTR pszBackslash;
726 UINT Length;
727
728 TRACE("(%p, %p, %d, %p)\n", this, pidl, iColumn, pDetails);
729 if (iColumn >= COLUMNS_COUNT)
730 return E_FAIL;
731 pDetails->fmt = RecycleBinColumns[iColumn].fmt;
732 pDetails->cxChar = RecycleBinColumns[iColumn].cxChars;
733 if (pidl == NULL)
734 return SHSetStrRet(&pDetails->str, RecycleBinColumns[iColumn].column_name_id);
735
736 if (iColumn == COLUMN_NAME)
737 return GetDisplayNameOf(pidl, SHGDN_NORMAL, &pDetails->str);
738
739 pFileDetails = _ILGetRecycleStruct(pidl);
740 switch (iColumn)
741 {
742 case COLUMN_DATEDEL:
743 FormatDateTime(buffer, MAX_PATH, &pFileDetails->DeletionTime);
744 break;
745 case COLUMN_DELFROM:
746 pszBackslash = wcsrchr(pFileDetails->szName, L'\\');
747 Length = (pszBackslash - pFileDetails->szName);
748 memcpy((LPVOID)buffer, pFileDetails->szName, Length * sizeof(WCHAR));
749 buffer[Length] = L'\0';
750 break;
751 case COLUMN_SIZE:
752 StrFormatKBSizeW(pFileDetails->FileSize.QuadPart, buffer, MAX_PATH);
753 break;
754 case COLUMN_MTIME:
755 FormatDateTime(buffer, MAX_PATH, &pFileDetails->LastModification);
756 break;
757 case COLUMN_TYPE:
758 // FIXME: We should in fact use a UNICODE version of _ILGetFileType
759 szTypeName[0] = L'\0';
760 wcscpy(buffer, PathFindExtensionW(pFileDetails->szName));
761 if (!( HCR_MapTypeToValueW(buffer, buffer, _countof(buffer), TRUE) &&
762 HCR_MapTypeToValueW(buffer, szTypeName, _countof(szTypeName), FALSE )))
763 {
764 /* load localized file string */
765 szTypeName[0] = '\0';
766 if(LoadStringW(shell32_hInstance, IDS_ANY_FILE, szTypeName, _countof(szTypeName)))
767 {
768 szTypeName[63] = '\0';
769 StringCchPrintfW(buffer, _countof(buffer), szTypeName, PathFindExtensionW(pFileDetails->szName));
770 }
771 }
772 return SHSetStrRet(&pDetails->str, szTypeName);
773 default:
774 return E_FAIL;
775 }
776
777 return SHSetStrRet(&pDetails->str, buffer);
778 }
779
780 HRESULT WINAPI CRecycleBin::MapColumnToSCID(UINT iColumn, SHCOLUMNID *pscid)
781 {
782 TRACE("(%p, %d, %p)\n", this, iColumn, pscid);
783 if (iColumn >= COLUMNS_COUNT)
784 return E_INVALIDARG;
785 pscid->fmtid = *RecycleBinColumns[iColumn].fmtId;
786 pscid->pid = RecycleBinColumns[iColumn].pid;
787 return S_OK;
788 }
789
790 BOOL CRecycleBin::RecycleBinIsEmpty()
791 {
792 CComPtr<IEnumIDList> spEnumFiles;
793 HRESULT hr = EnumObjects(NULL, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &spEnumFiles);
794 if (FAILED(hr))
795 return TRUE;
796 CComHeapPtr<ITEMIDLIST> spPidl;
797 ULONG itemcount;
798 return spEnumFiles->Next(1, &spPidl, &itemcount) != S_OK;
799 }
800
801 /*************************************************************************
802 * RecycleBin IContextMenu interface
803 */
804
805 HRESULT WINAPI CRecycleBin::QueryContextMenu(HMENU hMenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags)
806 {
807 WCHAR szBuffer[100];
808 MENUITEMINFOW mii;
809 int id = 1;
810
811 TRACE("QueryContextMenu %p %p %u %u %u %u\n", this, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags );
812
813 if (!hMenu)
814 return E_INVALIDARG;
815
816 memset(&mii, 0, sizeof(mii));
817 mii.cbSize = sizeof(mii);
818 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
819 mii.fState = RecycleBinIsEmpty() ? MFS_DISABLED : MFS_ENABLED;
820 szBuffer[0] = L'\0';
821 LoadStringW(shell32_hInstance, IDS_EMPTY_BITBUCKET, szBuffer, sizeof(szBuffer) / sizeof(WCHAR));
822 mii.dwTypeData = szBuffer;
823 mii.cch = wcslen(mii.dwTypeData);
824 mii.wID = idCmdFirst + id++;
825 mii.fType = MFT_STRING;
826 iIdEmpty = 1;
827
828 if (!InsertMenuItemW(hMenu, indexMenu, TRUE, &mii))
829 return E_FAIL;
830
831 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, id);
832 }
833
834 HRESULT WINAPI CRecycleBin::InvokeCommand(LPCMINVOKECOMMANDINFO lpcmi)
835 {
836 HRESULT hr;
837 LPSHELLBROWSER lpSB;
838 IShellView * lpSV = NULL;
839
840 TRACE("%p %p verb %p\n", this, lpcmi, lpcmi->lpVerb);
841
842 if (LOWORD(lpcmi->lpVerb) == iIdEmpty)
843 {
844 // FIXME
845 // path & flags
846 hr = SHEmptyRecycleBinW(lpcmi->hwnd, L"C:\\", 0);
847 TRACE("result %x\n", hr);
848 if (hr != S_OK)
849 return hr;
850
851 lpSB = (LPSHELLBROWSER)SendMessageA(lpcmi->hwnd, CWM_GETISHELLBROWSER, 0, 0);
852 if (lpSB && SUCCEEDED(lpSB->QueryActiveShellView(&lpSV)))
853 lpSV->Refresh();
854 }
855 return S_OK;
856 }
857
858 HRESULT WINAPI CRecycleBin::GetCommandString(UINT_PTR idCommand, UINT uFlags, UINT *lpReserved, LPSTR lpszName, UINT uMaxNameLen)
859 {
860 FIXME("%p %lu %u %p %p %u\n", this, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
861
862 return E_NOTIMPL;
863 }
864
865 /*************************************************************************
866 * RecycleBin IShellPropSheetExt interface
867 */
868
869 HRESULT WINAPI CRecycleBin::AddPages(LPFNSVADDPROPSHEETPAGE pfnAddPage, LPARAM lParam)
870 {
871 FIXME("%p %p %lu\n", this, pfnAddPage, lParam);
872
873 return E_NOTIMPL;
874 }
875
876 HRESULT WINAPI CRecycleBin::ReplacePage(EXPPS uPageID, LPFNSVADDPROPSHEETPAGE pfnReplaceWith, LPARAM lParam)
877 {
878 FIXME("%p %lu %p %lu\n", this, uPageID, pfnReplaceWith, lParam);
879
880 return E_NOTIMPL;
881 }
882
883 /*************************************************************************
884 * RecycleBin IShellExtInit interface
885 */
886
887 HRESULT WINAPI CRecycleBin::Initialize(LPCITEMIDLIST pidlFolder, IDataObject *pdtobj, HKEY hkeyProgID)
888 {
889 TRACE("%p %p %p %p\n", this, pidlFolder, pdtobj, hkeyProgID );
890 return S_OK;
891 }
892
893 BOOL
894 TRASH_CanTrashFile(LPCWSTR wszPath)
895 {
896 LONG ret;
897 DWORD dwNukeOnDelete, dwType, VolSerialNumber, MaxComponentLength;
898 DWORD FileSystemFlags, dwSize, dwDisposition;
899 HKEY hKey;
900 WCHAR szBuffer[10];
901 WCHAR szKey[150] = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Bitbucket\\Volume\\";
902
903 if (wszPath[1] != L':')
904 {
905 /* path is UNC */
906 return FALSE;
907 }
908
909 if (GetDriveTypeW(wszPath) != DRIVE_FIXED)
910 {
911 /* no bitbucket on removable media */
912 return FALSE;
913 }
914
915 if (!GetVolumeInformationW(wszPath, NULL, 0, &VolSerialNumber, &MaxComponentLength, &FileSystemFlags, NULL, 0))
916 {
917 ERR("GetVolumeInformationW failed with %u\n", GetLastError());
918 return FALSE;
919 }
920
921 swprintf(szBuffer, L"%04X-%04X", LOWORD(VolSerialNumber), HIWORD(VolSerialNumber));
922 wcscat(szKey, szBuffer);
923
924 if (RegCreateKeyExW(HKEY_CURRENT_USER, szKey, 0, NULL, 0, KEY_WRITE, NULL, &hKey, &dwDisposition) != ERROR_SUCCESS)
925 {
926 ERR("RegCreateKeyExW failed\n");
927 return FALSE;
928 }
929
930 if (dwDisposition & REG_CREATED_NEW_KEY)
931 {
932 /* per default move to bitbucket */
933 dwNukeOnDelete = 0;
934 RegSetValueExW(hKey, L"NukeOnDelete", 0, REG_DWORD, (LPBYTE)&dwNukeOnDelete, sizeof(DWORD));
935 /* per default unlimited size */
936 dwSize = -1;
937 RegSetValueExW(hKey, L"MaxCapacity", 0, REG_DWORD, (LPBYTE)&dwSize, sizeof(DWORD));
938 RegCloseKey(hKey);
939 return TRUE;
940 }
941 else
942 {
943 dwSize = sizeof(dwNukeOnDelete);
944 ret = RegQueryValueExW(hKey, L"NukeOnDelete", NULL, &dwType, (LPBYTE)&dwNukeOnDelete, &dwSize);
945 if (ret != ERROR_SUCCESS)
946 {
947 if (ret == ERROR_FILE_NOT_FOUND)
948 {
949 /* restore key and enable bitbucket */
950 dwNukeOnDelete = 0;
951 RegSetValueExW(hKey, L"NukeOnDelete", 0, REG_DWORD, (LPBYTE)&dwNukeOnDelete, sizeof(DWORD));
952 }
953 RegCloseKey(hKey);
954 return TRUE;
955 }
956 else if (dwNukeOnDelete)
957 {
958 /* do not delete to bitbucket */
959 RegCloseKey(hKey);
960 return FALSE;
961 }
962 /* FIXME
963 * check if bitbucket is full
964 */
965 RegCloseKey(hKey);
966 return TRUE;
967 }
968 }
969
970 BOOL
971 TRASH_TrashFile(LPCWSTR wszPath)
972 {
973 TRACE("(%s)\n", debugstr_w(wszPath));
974 return DeleteFileToRecycleBin(wszPath);
975 }
976
977 /*************************************************************************
978 * SHUpdateCRecycleBinIcon [SHELL32.@]
979 *
980 * Undocumented
981 */
982 EXTERN_C HRESULT WINAPI SHUpdateRecycleBinIcon(void)
983 {
984 FIXME("stub\n");
985
986
987
988 return S_OK;
989 }
990
991 /****************************************************************************
992 * IDropTarget implementation
993 */
994 BOOL CRecycleBin::QueryDrop(DWORD dwKeyState, LPDWORD pdwEffect)
995 {
996 /* TODO on shift we should delete, we should update the cursor manager to show this. */
997
998 DWORD dwEffect = DROPEFFECT_COPY;
999
1000 *pdwEffect = DROPEFFECT_NONE;
1001
1002 if (fAcceptFmt) { /* Does our interpretation of the keystate ... */
1003 *pdwEffect = KeyStateToDropEffect (dwKeyState);
1004
1005 if (*pdwEffect == DROPEFFECT_NONE)
1006 *pdwEffect = dwEffect;
1007
1008 /* ... matches the desired effect ? */
1009 if (dwEffect & *pdwEffect) {
1010 return TRUE;
1011 }
1012 }
1013 return FALSE;
1014 }
1015
1016 HRESULT WINAPI CRecycleBin::DragEnter(IDataObject *pDataObject,
1017 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
1018 {
1019 TRACE("Recycle bin drag over (%p)\n", this);
1020 /* The recycle bin accepts pretty much everything, and sets a CSIDL flag. */
1021 fAcceptFmt = TRUE;
1022
1023 QueryDrop(dwKeyState, pdwEffect);
1024 return S_OK;
1025 }
1026
1027 HRESULT WINAPI CRecycleBin::DragOver(DWORD dwKeyState, POINTL pt,
1028 DWORD *pdwEffect)
1029 {
1030 TRACE("(%p)\n", this);
1031
1032 if (!pdwEffect)
1033 return E_INVALIDARG;
1034
1035 QueryDrop(dwKeyState, pdwEffect);
1036
1037 return S_OK;
1038 }
1039
1040 HRESULT WINAPI CRecycleBin::DragLeave()
1041 {
1042 TRACE("(%p)\n", this);
1043
1044 fAcceptFmt = FALSE;
1045
1046 return S_OK;
1047 }
1048
1049 HRESULT WINAPI CRecycleBin::Drop(IDataObject *pDataObject,
1050 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
1051 {
1052 TRACE("(%p) object dropped on recycle bin, effect %u\n", this, *pdwEffect);
1053
1054 /* TODO: pdwEffect should be read and make the drop object be permanently deleted in the move case (shift held) */
1055
1056 FORMATETC fmt;
1057 TRACE("(%p)->(DataObject=%p)\n", this, pDataObject);
1058 InitFormatEtc (fmt, cfShellIDList, TYMED_HGLOBAL);
1059
1060 /* Handle cfShellIDList Drop objects here, otherwise send the approriate message to other software */
1061 if (SUCCEEDED(pDataObject->QueryGetData(&fmt)))
1062 {
1063 DWORD fMask = 0;
1064
1065 if ((dwKeyState & MK_SHIFT) == MK_SHIFT)
1066 fMask |= CMIC_MASK_SHIFT_DOWN;
1067
1068 DoDeleteAsync(pDataObject, fMask);
1069 }
1070 else
1071 {
1072 /*
1073 * TODO call SetData on the data object with format CFSTR_TARGETCLSID
1074 * set to the Recycle Bin's class identifier CLSID_RecycleBin.
1075 */
1076 }
1077 return S_OK;
1078 }
1079
1080 HRESULT WINAPI DoDeleteDataObject(IDataObject *pda, DWORD fMask)
1081 {
1082 HRESULT hr;
1083 STGMEDIUM medium;
1084 FORMATETC formatetc;
1085 InitFormatEtc (formatetc, CF_HDROP, TYMED_HGLOBAL);
1086 hr = pda->GetData(&formatetc, &medium);
1087 if (FAILED_UNEXPECTEDLY(hr))
1088 return hr;
1089
1090 LPDROPFILES lpdf = (LPDROPFILES) GlobalLock(medium.hGlobal);
1091 if (!lpdf)
1092 {
1093 ERR("Error locking global\n");
1094 return E_FAIL;
1095 }
1096
1097 /* Delete them */
1098 SHFILEOPSTRUCTW FileOp;
1099 ZeroMemory(&FileOp, sizeof(FileOp));
1100 FileOp.wFunc = FO_DELETE;
1101 FileOp.pFrom = (LPWSTR) (((byte*) lpdf) + lpdf->pFiles);;
1102 if ((fMask & CMIC_MASK_SHIFT_DOWN) == 0)
1103 FileOp.fFlags = FOF_ALLOWUNDO;
1104
1105 if (SHFileOperationW(&FileOp) != 0)
1106 {
1107 ERR("SHFileOperation failed with 0x%x\n", GetLastError());
1108 hr = E_FAIL;
1109 }
1110
1111 ReleaseStgMedium(&medium);
1112
1113 return hr;
1114 }
1115
1116 struct DeleteThreadData {
1117 IStream *s;
1118 DWORD fMask;
1119 };
1120
1121 DWORD WINAPI DoDeleteThreadProc(LPVOID lpParameter)
1122 {
1123 DeleteThreadData *data = static_cast<DeleteThreadData*>(lpParameter);
1124 CoInitialize(NULL);
1125 IDataObject *pDataObject;
1126 HRESULT hr = CoGetInterfaceAndReleaseStream (data->s, IID_PPV_ARG(IDataObject, &pDataObject));
1127 if (SUCCEEDED(hr))
1128 {
1129 DoDeleteDataObject(pDataObject, data->fMask);
1130 }
1131 pDataObject->Release();
1132 CoUninitialize();
1133 HeapFree(GetProcessHeap(), 0, data);
1134 return 0;
1135 }
1136
1137 void DoDeleteAsync(IDataObject *pda, DWORD fMask)
1138 {
1139 DeleteThreadData *data = static_cast<DeleteThreadData*>(HeapAlloc(GetProcessHeap(), 0, sizeof(DeleteThreadData)));
1140 data->fMask = fMask;
1141 CoMarshalInterThreadInterfaceInStream(IID_IDataObject, pda, &data->s);
1142 SHCreateThread(DoDeleteThreadProc, data, NULL, NULL);
1143 }
1144
1145 /*************************************************************************
1146 * SHEmptyRecycleBinA (SHELL32.@)
1147 */
1148 HRESULT WINAPI SHEmptyRecycleBinA(HWND hwnd, LPCSTR pszRootPath, DWORD dwFlags)
1149 {
1150 LPWSTR szRootPathW = NULL;
1151 int len;
1152 HRESULT hr;
1153
1154 TRACE("%p, %s, 0x%08x\n", hwnd, debugstr_a(pszRootPath), dwFlags);
1155
1156 if (pszRootPath)
1157 {
1158 len = MultiByteToWideChar(CP_ACP, 0, pszRootPath, -1, NULL, 0);
1159 if (len == 0)
1160 return HRESULT_FROM_WIN32(GetLastError());
1161 szRootPathW = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1162 if (!szRootPathW)
1163 return E_OUTOFMEMORY;
1164 if (MultiByteToWideChar(CP_ACP, 0, pszRootPath, -1, szRootPathW, len) == 0)
1165 {
1166 HeapFree(GetProcessHeap(), 0, szRootPathW);
1167 return HRESULT_FROM_WIN32(GetLastError());
1168 }
1169 }
1170
1171 hr = SHEmptyRecycleBinW(hwnd, szRootPathW, dwFlags);
1172 HeapFree(GetProcessHeap(), 0, szRootPathW);
1173
1174 return hr;
1175 }
1176
1177 HRESULT WINAPI SHEmptyRecycleBinW(HWND hwnd, LPCWSTR pszRootPath, DWORD dwFlags)
1178 {
1179 WCHAR szPath[MAX_PATH] = {0}, szBuffer[MAX_PATH];
1180 DWORD dwSize, dwType, count;
1181 LONG ret;
1182 IShellFolder *pDesktop, *pRecycleBin;
1183 PIDLIST_ABSOLUTE pidlRecycleBin;
1184 PITEMID_CHILD pidl;
1185 HRESULT hr = S_OK;
1186 LPENUMIDLIST penumFiles;
1187 STRRET StrRet;
1188
1189 TRACE("%p, %s, 0x%08x\n", hwnd, debugstr_w(pszRootPath), dwFlags);
1190
1191 if (!(dwFlags & SHERB_NOCONFIRMATION))
1192 {
1193 hr = SHGetDesktopFolder(&pDesktop);
1194 if (FAILED(hr))
1195 return hr;
1196 hr = SHGetFolderLocation(NULL, CSIDL_BITBUCKET, NULL, 0, &pidlRecycleBin);
1197 if (FAILED(hr))
1198 {
1199 pDesktop->Release();
1200 return hr;
1201 }
1202 hr = pDesktop->BindToObject(pidlRecycleBin, NULL, IID_PPV_ARG(IShellFolder, &pRecycleBin));
1203 CoTaskMemFree(pidlRecycleBin);
1204 pDesktop->Release();
1205 if (FAILED(hr))
1206 return hr;
1207 hr = pRecycleBin->EnumObjects(hwnd, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &penumFiles);
1208 if (FAILED(hr))
1209 {
1210 pRecycleBin->Release();
1211 return hr;
1212 }
1213
1214 count = 0;
1215 if (hr != S_FALSE)
1216 {
1217 while (penumFiles->Next(1, &pidl, NULL) == S_OK)
1218 {
1219 count++;
1220 pRecycleBin->GetDisplayNameOf(pidl, SHGDN_NORMAL, &StrRet);
1221 StrRetToBuf(&StrRet, pidl, szBuffer, _countof(szBuffer));
1222 CoTaskMemFree(pidl);
1223 }
1224 penumFiles->Release();
1225 }
1226 pRecycleBin->Release();
1227
1228 switch (count)
1229 {
1230 case 0:
1231 /* no files, don't need confirmation */
1232 break;
1233
1234 case 1:
1235 /* we have only one item inside the bin, so show a message box with its name */
1236 if (ShellMessageBoxW(shell32_hInstance, hwnd, MAKEINTRESOURCEW(IDS_DELETEITEM_TEXT), MAKEINTRESOURCEW(IDS_EMPTY_BITBUCKET),
1237 MB_ICONEXCLAMATION | MB_YESNO | MB_DEFBUTTON2, szBuffer) == IDNO)
1238 {
1239 return S_OK;
1240 }
1241 break;
1242
1243 default:
1244 /* we have more than one item, so show a message box with the count of the items */
1245 StringCbPrintfW(szBuffer, sizeof(szBuffer), L"%u", count);
1246 if (ShellMessageBoxW(shell32_hInstance, hwnd, MAKEINTRESOURCEW(IDS_DELETEMULTIPLE_TEXT), MAKEINTRESOURCEW(IDS_EMPTY_BITBUCKET),
1247 MB_ICONEXCLAMATION | MB_YESNO | MB_DEFBUTTON2, szBuffer) == IDNO)
1248 {
1249 return S_OK;
1250 }
1251 break;
1252 }
1253 }
1254
1255 if (dwFlags & SHERB_NOPROGRESSUI)
1256 {
1257 ret = EmptyRecycleBinW(pszRootPath);
1258 }
1259 else
1260 {
1261 /* FIXME
1262 * show a progress dialog
1263 */
1264 ret = EmptyRecycleBinW(pszRootPath);
1265 }
1266
1267 if (!ret)
1268 return HRESULT_FROM_WIN32(GetLastError());
1269
1270 if (!(dwFlags & SHERB_NOSOUND))
1271 {
1272 dwSize = sizeof(szPath);
1273 ret = RegGetValueW(HKEY_CURRENT_USER,
1274 L"AppEvents\\Schemes\\Apps\\Explorer\\EmptyRecycleBin\\.Current",
1275 NULL,
1276 RRF_RT_REG_SZ,
1277 &dwType,
1278 (PVOID)szPath,
1279 &dwSize);
1280 if (ret != ERROR_SUCCESS)
1281 return S_OK;
1282
1283 if (dwType != REG_EXPAND_SZ) /* type dismatch */
1284 return S_OK;
1285
1286 szPath[(sizeof(szPath)/sizeof(WCHAR))-1] = L'\0';
1287 PlaySoundW(szPath, NULL, SND_FILENAME);
1288 }
1289 return S_OK;
1290 }
1291
1292 HRESULT WINAPI SHQueryRecycleBinA(LPCSTR pszRootPath, LPSHQUERYRBINFO pSHQueryRBInfo)
1293 {
1294 LPWSTR szRootPathW = NULL;
1295 int len;
1296 HRESULT hr;
1297
1298 TRACE("%s, %p\n", debugstr_a(pszRootPath), pSHQueryRBInfo);
1299
1300 if (pszRootPath)
1301 {
1302 len = MultiByteToWideChar(CP_ACP, 0, pszRootPath, -1, NULL, 0);
1303 if (len == 0)
1304 return HRESULT_FROM_WIN32(GetLastError());
1305 szRootPathW = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1306 if (!szRootPathW)
1307 return E_OUTOFMEMORY;
1308 if (MultiByteToWideChar(CP_ACP, 0, pszRootPath, -1, szRootPathW, len) == 0)
1309 {
1310 HeapFree(GetProcessHeap(), 0, szRootPathW);
1311 return HRESULT_FROM_WIN32(GetLastError());
1312 }
1313 }
1314
1315 hr = SHQueryRecycleBinW(szRootPathW, pSHQueryRBInfo);
1316 HeapFree(GetProcessHeap(), 0, szRootPathW);
1317
1318 return hr;
1319 }
1320
1321 HRESULT WINAPI SHQueryRecycleBinW(LPCWSTR pszRootPath, LPSHQUERYRBINFO pSHQueryRBInfo)
1322 {
1323 FIXME("%s, %p - stub\n", debugstr_w(pszRootPath), pSHQueryRBInfo);
1324
1325 if (!(pszRootPath) || (pszRootPath[0] == 0) ||
1326 !(pSHQueryRBInfo) || (pSHQueryRBInfo->cbSize < sizeof(SHQUERYRBINFO)))
1327 {
1328 return E_INVALIDARG;
1329 }
1330
1331 pSHQueryRBInfo->i64Size = 0;
1332 pSHQueryRBInfo->i64NumItems = 0;
1333
1334 return S_OK;
1335 }