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