[SHELL32]
[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 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(hr))
71 return NULL;
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_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 hr = CDefView_Constructor(this, riid, ppv);
568 }
569 else
570 return hr;
571 TRACE ("-- (%p)->(interface=%p)\n", this, ppv);
572 return hr;
573
574 }
575
576 HRESULT WINAPI CRecycleBin::GetAttributesOf(UINT cidl, PCUITEMID_CHILD_ARRAY apidl,
577 SFGAOF *rgfInOut)
578 {
579 TRACE("(%p, %d, {%p, ...}, {%x})\n", this, cidl, apidl ? apidl[0] : NULL, (unsigned int)*rgfInOut);
580 *rgfInOut &= SFGAO_FOLDER|SFGAO_DROPTARGET|SFGAO_HASPROPSHEET|SFGAO_CANLINK;
581 return S_OK;
582 }
583
584 HRESULT WINAPI CRecycleBin::GetUIObjectOf(HWND hwndOwner, UINT cidl, PCUITEMID_CHILD_ARRAY apidl,
585 REFIID riid, UINT *prgfInOut, void **ppv)
586 {
587 LPVOID pObj = NULL;
588 HRESULT hr = E_INVALIDARG;
589
590 TRACE ("(%p)->(%p,%u,apidl=%p, %p %p)\n", this,
591 hwndOwner, cidl, apidl, prgfInOut, ppv);
592
593 if (!ppv)
594 return hr;
595
596 *ppv = NULL;
597
598 if ((IsEqualIID (riid, IID_IContextMenu) || IsEqualIID(riid, IID_IContextMenu2)) && (cidl >= 1))
599 {
600 hr = ShellObjectCreatorInit<CRecycleBinItemContextMenu>(apidl[0], riid, &pObj);
601 }
602 else if (IsEqualIID (riid, IID_IDropTarget) && (cidl == 1))
603 {
604 IDropTarget * pDt = NULL;
605 hr = QueryInterface(IID_PPV_ARG(IDropTarget, &pDt));
606 pObj = pDt;
607 }
608 else if((IsEqualIID(riid, IID_IExtractIconA) || IsEqualIID(riid, IID_IExtractIconW)) && (cidl == 1))
609 {
610 hr = CRecyclerExtractIcon_CreateInstance(apidl[0], riid, &pObj);
611 }
612 else
613 hr = E_NOINTERFACE;
614
615 if (SUCCEEDED(hr) && !pObj)
616 hr = E_OUTOFMEMORY;
617
618 *ppv = pObj;
619 TRACE ("(%p)->hr=0x%08x\n", this, hr);
620 return hr;
621 }
622
623 HRESULT WINAPI CRecycleBin::GetDisplayNameOf(PCUITEMID_CHILD pidl, SHGDNF uFlags, STRRET *pName)
624 {
625 PIDLRecycleStruct *pFileDetails;
626 LPWSTR pFileName;
627
628 TRACE("(%p, %p, %x, %p)\n", this, pidl, (unsigned int)uFlags, pName);
629
630 pFileDetails = _ILGetRecycleStruct(pidl);
631 if (!pFileDetails)
632 {
633 pName->cStr[0] = 0;
634 pName->uType = STRRET_CSTR;
635 return E_INVALIDARG;
636 }
637
638 pFileName = wcsrchr(pFileDetails->szName, L'\\');
639 if (!pFileName)
640 {
641 pName->cStr[0] = 0;
642 pName->uType = STRRET_CSTR;
643 return E_UNEXPECTED;
644 }
645
646 pName->pOleStr = StrDupW(pFileName + 1);
647 if (pName->pOleStr == NULL)
648 return E_OUTOFMEMORY;
649
650 pName->uType = STRRET_WSTR;
651 return S_OK;
652 }
653
654 HRESULT WINAPI CRecycleBin::SetNameOf(HWND hwnd, PCUITEMID_CHILD pidl, LPCOLESTR pszName,
655 SHGDNF uFlags, PITEMID_CHILD *ppidlOut)
656 {
657 TRACE("\n");
658 return E_FAIL; /* not supported */
659 }
660
661 HRESULT WINAPI CRecycleBin::GetDefaultSearchGUID(GUID *pguid)
662 {
663 FIXME("stub\n");
664 return E_NOTIMPL;
665 }
666
667 HRESULT WINAPI CRecycleBin::EnumSearches(IEnumExtraSearch **ppEnum)
668 {
669 FIXME("stub\n");
670 *ppEnum = NULL;
671 return E_NOTIMPL;
672 }
673
674 HRESULT WINAPI CRecycleBin::GetDefaultColumn(DWORD dwReserved, ULONG *pSort, ULONG *pDisplay)
675 {
676 TRACE("(%p, %x, %p, %p)\n", this, (unsigned int)dwReserved, pSort, pDisplay);
677 if (pSort)
678 *pSort = 0;
679 if (pDisplay)
680 *pDisplay = 0;
681 return S_OK;
682 }
683
684 HRESULT WINAPI CRecycleBin::GetDefaultColumnState(UINT iColumn, SHCOLSTATEF *pcsFlags)
685 {
686 TRACE("(%p, %d, %p)\n", this, iColumn, pcsFlags);
687 if (iColumn >= COLUMNS_COUNT)
688 return E_INVALIDARG;
689 *pcsFlags = RecycleBinColumns[iColumn].pcsFlags;
690 return S_OK;
691 }
692
693 HRESULT WINAPI CRecycleBin::GetDetailsEx(PCUITEMID_CHILD pidl, const SHCOLUMNID *pscid, VARIANT *pv)
694 {
695 FIXME("stub\n");
696 return E_NOTIMPL;
697 }
698
699 static HRESULT FormatDateTime(LPWSTR buffer, int size, FILETIME * ft)
700 {
701 FILETIME lft;
702 SYSTEMTIME time;
703 int ret;
704
705 FileTimeToLocalFileTime(ft, &lft);
706 FileTimeToSystemTime(&lft, &time);
707
708 ret = GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &time, NULL, buffer, size);
709 if (ret > 0 && ret < size)
710 {
711 /* Append space + time without seconds */
712 buffer[ret-1] = ' ';
713 GetTimeFormatW(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &time, NULL, &buffer[ret], size - ret);
714 }
715
716 return (ret != 0 ? E_FAIL : S_OK);
717 }
718
719 HRESULT WINAPI CRecycleBin::GetDetailsOf(PCUITEMID_CHILD pidl, UINT iColumn, LPSHELLDETAILS pDetails)
720 {
721 PIDLRecycleStruct * pFileDetails;
722 WCHAR buffer[MAX_PATH];
723 WCHAR szTypeName[100];
724 LPWSTR pszBackslash;
725 UINT Length;
726
727 TRACE("(%p, %p, %d, %p)\n", this, pidl, iColumn, pDetails);
728 if (iColumn >= COLUMNS_COUNT)
729 return E_FAIL;
730 pDetails->fmt = RecycleBinColumns[iColumn].fmt;
731 pDetails->cxChar = RecycleBinColumns[iColumn].cxChars;
732 if (pidl == NULL)
733 return SHSetStrRet(&pDetails->str, RecycleBinColumns[iColumn].column_name_id);
734
735 if (iColumn == COLUMN_NAME)
736 return GetDisplayNameOf(pidl, SHGDN_NORMAL, &pDetails->str);
737
738 pFileDetails = _ILGetRecycleStruct(pidl);
739 switch (iColumn)
740 {
741 case COLUMN_DATEDEL:
742 FormatDateTime(buffer, MAX_PATH, &pFileDetails->DeletionTime);
743 break;
744 case COLUMN_DELFROM:
745 pszBackslash = wcsrchr(pFileDetails->szName, L'\\');
746 Length = (pszBackslash - pFileDetails->szName);
747 memcpy((LPVOID)buffer, pFileDetails->szName, Length * sizeof(WCHAR));
748 buffer[Length] = L'\0';
749 break;
750 case COLUMN_SIZE:
751 StrFormatKBSizeW(pFileDetails->FileSize.QuadPart, buffer, MAX_PATH);
752 break;
753 case COLUMN_MTIME:
754 FormatDateTime(buffer, MAX_PATH, &pFileDetails->LastModification);
755 break;
756 case COLUMN_TYPE:
757 // FIXME: We should in fact use a UNICODE version of _ILGetFileType
758 szTypeName[0] = L'\0';
759 wcscpy(buffer, PathFindExtensionW(pFileDetails->szName));
760 if (!( HCR_MapTypeToValueW(buffer, buffer, sizeof(buffer) / sizeof(WCHAR), TRUE) &&
761 HCR_MapTypeToValueW(buffer, szTypeName, sizeof(szTypeName) / sizeof(WCHAR), FALSE )))
762 {
763 wcscpy (szTypeName, PathFindExtensionW(pFileDetails->szName));
764 wcscat(szTypeName, L"-");
765 Length = wcslen(szTypeName);
766 if (LoadStringW(shell32_hInstance, IDS_SHV_COLUMN1, &szTypeName[Length], (sizeof(szTypeName) / sizeof(WCHAR)) - Length))
767 szTypeName[(sizeof(szTypeName)/sizeof(WCHAR))-1] = L'\0';
768 }
769 return SHSetStrRet(&pDetails->str, szTypeName);
770 default:
771 return E_FAIL;
772 }
773
774 return SHSetStrRet(&pDetails->str, buffer);
775 }
776
777 HRESULT WINAPI CRecycleBin::MapColumnToSCID(UINT iColumn, SHCOLUMNID *pscid)
778 {
779 TRACE("(%p, %d, %p)\n", this, iColumn, pscid);
780 if (iColumn >= COLUMNS_COUNT)
781 return E_INVALIDARG;
782 pscid->fmtid = *RecycleBinColumns[iColumn].fmtId;
783 pscid->pid = RecycleBinColumns[iColumn].pid;
784 return S_OK;
785 }
786
787 BOOL CRecycleBin::RecycleBinIsEmpty()
788 {
789 CComPtr<IEnumIDList> spEnumFiles;
790 HRESULT hr = EnumObjects(NULL, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &spEnumFiles);
791 if (FAILED(hr))
792 return TRUE;
793 CComHeapPtr<ITEMIDLIST> spPidl;
794 ULONG itemcount;
795 return spEnumFiles->Next(1, &spPidl, &itemcount) != S_OK;
796 }
797
798 /*************************************************************************
799 * RecycleBin IContextMenu interface
800 */
801
802 HRESULT WINAPI CRecycleBin::QueryContextMenu(HMENU hMenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags)
803 {
804 WCHAR szBuffer[100];
805 MENUITEMINFOW mii;
806 int id = 1;
807
808 TRACE("QueryContextMenu %p %p %u %u %u %u\n", this, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags );
809
810 if (!hMenu)
811 return E_INVALIDARG;
812
813 memset(&mii, 0, sizeof(mii));
814 mii.cbSize = sizeof(mii);
815 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
816 mii.fState = RecycleBinIsEmpty() ? MFS_DISABLED : MFS_ENABLED;
817 szBuffer[0] = L'\0';
818 LoadStringW(shell32_hInstance, IDS_EMPTY_BITBUCKET, szBuffer, sizeof(szBuffer) / sizeof(WCHAR));
819 mii.dwTypeData = szBuffer;
820 mii.cch = wcslen(mii.dwTypeData);
821 mii.wID = idCmdFirst + id++;
822 mii.fType = MFT_STRING;
823 iIdEmpty = 1;
824
825 if (!InsertMenuItemW(hMenu, indexMenu, TRUE, &mii))
826 return E_FAIL;
827
828 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, id);
829 }
830
831 HRESULT WINAPI CRecycleBin::InvokeCommand(LPCMINVOKECOMMANDINFO lpcmi)
832 {
833 HRESULT hr;
834 LPSHELLBROWSER lpSB;
835 IShellView * lpSV = NULL;
836
837 TRACE("%p %p verb %p\n", this, lpcmi, lpcmi->lpVerb);
838
839 if (LOWORD(lpcmi->lpVerb) == iIdEmpty)
840 {
841 // FIXME
842 // path & flags
843 hr = SHEmptyRecycleBinW(lpcmi->hwnd, L"C:\\", 0);
844 TRACE("result %x\n", hr);
845 if (hr != S_OK)
846 return hr;
847
848 lpSB = (LPSHELLBROWSER)SendMessageA(lpcmi->hwnd, CWM_GETISHELLBROWSER, 0, 0);
849 if (lpSB && SUCCEEDED(lpSB->QueryActiveShellView(&lpSV)))
850 lpSV->Refresh();
851 }
852 return S_OK;
853 }
854
855 HRESULT WINAPI CRecycleBin::GetCommandString(UINT_PTR idCommand, UINT uFlags, UINT *lpReserved, LPSTR lpszName, UINT uMaxNameLen)
856 {
857 FIXME("%p %lu %u %p %p %u\n", this, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
858
859 return E_NOTIMPL;
860 }
861
862 /*************************************************************************
863 * RecycleBin IShellPropSheetExt interface
864 */
865
866 HRESULT WINAPI CRecycleBin::AddPages(LPFNSVADDPROPSHEETPAGE pfnAddPage, LPARAM lParam)
867 {
868 FIXME("%p %p %lu\n", this, pfnAddPage, lParam);
869
870 return E_NOTIMPL;
871 }
872
873 HRESULT WINAPI CRecycleBin::ReplacePage(EXPPS uPageID, LPFNSVADDPROPSHEETPAGE pfnReplaceWith, LPARAM lParam)
874 {
875 FIXME("%p %lu %p %lu\n", this, uPageID, pfnReplaceWith, lParam);
876
877 return E_NOTIMPL;
878 }
879
880 /*************************************************************************
881 * RecycleBin IShellExtInit interface
882 */
883
884 HRESULT WINAPI CRecycleBin::Initialize(LPCITEMIDLIST pidlFolder, IDataObject *pdtobj, HKEY hkeyProgID)
885 {
886 TRACE("%p %p %p %p\n", this, pidlFolder, pdtobj, hkeyProgID );
887 return S_OK;
888 }
889
890 BOOL
891 TRASH_CanTrashFile(LPCWSTR wszPath)
892 {
893 LONG ret;
894 DWORD dwNukeOnDelete, dwType, VolSerialNumber, MaxComponentLength;
895 DWORD FileSystemFlags, dwSize, dwDisposition;
896 HKEY hKey;
897 WCHAR szBuffer[10];
898 WCHAR szKey[150] = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Bitbucket\\Volume\\";
899
900 if (wszPath[1] != L':')
901 {
902 /* path is UNC */
903 return FALSE;
904 }
905
906 if (GetDriveTypeW(wszPath) != DRIVE_FIXED)
907 {
908 /* no bitbucket on removable media */
909 return FALSE;
910 }
911
912 if (!GetVolumeInformationW(wszPath, NULL, 0, &VolSerialNumber, &MaxComponentLength, &FileSystemFlags, NULL, 0))
913 {
914 ERR("GetVolumeInformationW failed with %u\n", GetLastError());
915 return FALSE;
916 }
917
918 swprintf(szBuffer, L"%04X-%04X", LOWORD(VolSerialNumber), HIWORD(VolSerialNumber));
919 wcscat(szKey, szBuffer);
920
921 if (RegCreateKeyExW(HKEY_CURRENT_USER, szKey, 0, NULL, 0, KEY_WRITE, NULL, &hKey, &dwDisposition) != ERROR_SUCCESS)
922 {
923 ERR("RegCreateKeyExW failed\n");
924 return FALSE;
925 }
926
927 if (dwDisposition & REG_CREATED_NEW_KEY)
928 {
929 /* per default move to bitbucket */
930 dwNukeOnDelete = 0;
931 RegSetValueExW(hKey, L"NukeOnDelete", 0, REG_DWORD, (LPBYTE)&dwNukeOnDelete, sizeof(DWORD));
932 /* per default unlimited size */
933 dwSize = -1;
934 RegSetValueExW(hKey, L"MaxCapacity", 0, REG_DWORD, (LPBYTE)&dwSize, sizeof(DWORD));
935 RegCloseKey(hKey);
936 return TRUE;
937 }
938 else
939 {
940 dwSize = sizeof(dwNukeOnDelete);
941 ret = RegQueryValueExW(hKey, L"NukeOnDelete", NULL, &dwType, (LPBYTE)&dwNukeOnDelete, &dwSize);
942 if (ret != ERROR_SUCCESS)
943 {
944 if (ret == ERROR_FILE_NOT_FOUND)
945 {
946 /* restore key and enable bitbucket */
947 dwNukeOnDelete = 0;
948 RegSetValueExW(hKey, L"NukeOnDelete", 0, REG_DWORD, (LPBYTE)&dwNukeOnDelete, sizeof(DWORD));
949 }
950 RegCloseKey(hKey);
951 return TRUE;
952 }
953 else if (dwNukeOnDelete)
954 {
955 /* do not delete to bitbucket */
956 RegCloseKey(hKey);
957 return FALSE;
958 }
959 /* FIXME
960 * check if bitbucket is full
961 */
962 RegCloseKey(hKey);
963 return TRUE;
964 }
965 }
966
967 BOOL
968 TRASH_TrashFile(LPCWSTR wszPath)
969 {
970 TRACE("(%s)\n", debugstr_w(wszPath));
971 return DeleteFileToRecycleBin(wszPath);
972 }
973
974 /*************************************************************************
975 * SHUpdateCRecycleBinIcon [SHELL32.@]
976 *
977 * Undocumented
978 */
979 EXTERN_C HRESULT WINAPI SHUpdateRecycleBinIcon(void)
980 {
981 FIXME("stub\n");
982
983
984
985 return S_OK;
986 }
987
988 /****************************************************************************
989 * IDropTarget implementation
990 */
991 BOOL CRecycleBin::QueryDrop(DWORD dwKeyState, LPDWORD pdwEffect)
992 {
993 /* TODO on shift we should delete, we should update the cursor manager to show this. */
994
995 DWORD dwEffect = DROPEFFECT_COPY;
996
997 *pdwEffect = DROPEFFECT_NONE;
998
999 if (fAcceptFmt) { /* Does our interpretation of the keystate ... */
1000 *pdwEffect = KeyStateToDropEffect (dwKeyState);
1001
1002 if (*pdwEffect == DROPEFFECT_NONE)
1003 *pdwEffect = dwEffect;
1004
1005 /* ... matches the desired effect ? */
1006 if (dwEffect & *pdwEffect) {
1007 return TRUE;
1008 }
1009 }
1010 return FALSE;
1011 }
1012
1013 HRESULT WINAPI CRecycleBin::DragEnter(IDataObject *pDataObject,
1014 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
1015 {
1016 TRACE("Recycle bin drag over (%p)\n", this);
1017 /* The recycle bin accepts pretty much everything, and sets a CSIDL flag. */
1018 fAcceptFmt = TRUE;
1019
1020 QueryDrop(dwKeyState, pdwEffect);
1021 return S_OK;
1022 }
1023
1024 HRESULT WINAPI CRecycleBin::DragOver(DWORD dwKeyState, POINTL pt,
1025 DWORD *pdwEffect)
1026 {
1027 TRACE("(%p)\n", this);
1028
1029 if (!pdwEffect)
1030 return E_INVALIDARG;
1031
1032 QueryDrop(dwKeyState, pdwEffect);
1033
1034 return S_OK;
1035 }
1036
1037 HRESULT WINAPI CRecycleBin::DragLeave()
1038 {
1039 TRACE("(%p)\n", this);
1040
1041 fAcceptFmt = FALSE;
1042
1043 return S_OK;
1044 }
1045
1046 HRESULT WINAPI CRecycleBin::Drop(IDataObject *pDataObject,
1047 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
1048 {
1049 TRACE("(%p) object dropped on recycle bin, effect %u\n", this, *pdwEffect);
1050
1051 /* TODO: pdwEffect should be read and make the drop object be permanently deleted in the move case (shift held) */
1052
1053 FORMATETC fmt;
1054 TRACE("(%p)->(DataObject=%p)\n", this, pDataObject);
1055 InitFormatEtc (fmt, cfShellIDList, TYMED_HGLOBAL);
1056
1057 /* Handle cfShellIDList Drop objects here, otherwise send the approriate message to other software */
1058 if (SUCCEEDED(pDataObject->QueryGetData(&fmt)))
1059 {
1060 DWORD fMask = 0;
1061
1062 if ((dwKeyState & MK_SHIFT) == MK_SHIFT)
1063 fMask |= CMIC_MASK_SHIFT_DOWN;
1064
1065 DoDeleteAsync(pDataObject, fMask);
1066 }
1067 else
1068 {
1069 /*
1070 * TODO call SetData on the data object with format CFSTR_TARGETCLSID
1071 * set to the Recycle Bin's class identifier CLSID_RecycleBin.
1072 */
1073 }
1074 return S_OK;
1075 }
1076
1077 HRESULT WINAPI DoDeleteDataObject(IDataObject *pda, DWORD fMask)
1078 {
1079 TRACE("performing delete");
1080 HRESULT hr;
1081
1082 STGMEDIUM medium;
1083 FORMATETC formatetc;
1084 InitFormatEtc(formatetc, RegisterClipboardFormatW(CFSTR_SHELLIDLIST), TYMED_HGLOBAL);
1085 hr = pda->GetData(&formatetc, &medium);
1086 if (FAILED(hr))
1087 return hr;
1088
1089 /* lock the handle */
1090 LPIDA lpcida = (LPIDA)GlobalLock(medium.hGlobal);
1091 if (!lpcida)
1092 {
1093 ReleaseStgMedium(&medium);
1094 return E_FAIL;
1095 }
1096
1097 /* convert the data into pidl */
1098 LPITEMIDLIST pidl;
1099 LPITEMIDLIST *apidl = _ILCopyCidaToaPidl(&pidl, lpcida);
1100 if (!apidl)
1101 {
1102 ReleaseStgMedium(&medium);
1103 return E_FAIL;
1104 }
1105
1106 CComPtr<IShellFolder> psfDesktop;
1107 CComPtr<IShellFolder> psfFrom = NULL;
1108
1109 /* Grab the desktop shell folder */
1110 hr = SHGetDesktopFolder(&psfDesktop);
1111 if (FAILED(hr))
1112 {
1113 ERR("SHGetDesktopFolder failed\n");
1114 SHFree(pidl);
1115 _ILFreeaPidl(apidl, lpcida->cidl);
1116 ReleaseStgMedium(&medium);
1117 return E_FAIL;
1118 }
1119
1120 /* Find source folder, this is where the clipboard data was copied from */
1121 if (_ILIsDesktop(pidl))
1122 {
1123 psfFrom = psfDesktop;
1124 }
1125 else
1126 {
1127 hr = psfDesktop->BindToObject(pidl, NULL, IID_PPV_ARG(IShellFolder, &psfFrom));
1128 if (FAILED(hr))
1129 {
1130 ERR("no IShellFolder\n");
1131 SHFree(pidl);
1132 _ILFreeaPidl(apidl, lpcida->cidl);
1133 ReleaseStgMedium(&medium);
1134 return E_FAIL;
1135 }
1136 }
1137
1138 STRRET strTemp;
1139 hr = psfFrom->GetDisplayNameOf(apidl[0], SHGDN_FORPARSING, &strTemp);
1140 if (FAILED(hr))
1141 {
1142 ERR("IShellFolder_GetDisplayNameOf failed with %x\n", hr);
1143 SHFree(pidl);
1144 _ILFreeaPidl(apidl, lpcida->cidl);
1145 ReleaseStgMedium(&medium);
1146 return hr;
1147 }
1148
1149 WCHAR wszPath[MAX_PATH];
1150 hr = StrRetToBufW(&strTemp, apidl[0], wszPath, _countof(wszPath));
1151 if (FAILED(hr))
1152 {
1153 ERR("StrRetToBufW failed with %x\n", hr);
1154 SHFree(pidl);
1155 _ILFreeaPidl(apidl, lpcida->cidl);
1156 ReleaseStgMedium(&medium);
1157 return hr;
1158 }
1159
1160 /* Only keep the base path */
1161 LPWSTR pwszFilename = PathFindFileNameW(wszPath);
1162 *pwszFilename = L'\0';
1163
1164 /* Build paths list */
1165 LPWSTR pwszPaths = BuildPathsList(wszPath, lpcida->cidl, (LPCITEMIDLIST*) apidl);
1166 if (!pwszPaths)
1167 {
1168 SHFree(pidl);
1169 _ILFreeaPidl(apidl, lpcida->cidl);
1170 ReleaseStgMedium(&medium);
1171 return E_FAIL;
1172 }
1173
1174 /* Delete them */
1175 SHFILEOPSTRUCTW FileOp;
1176 ZeroMemory(&FileOp, sizeof(FileOp));
1177 FileOp.wFunc = FO_DELETE;
1178 FileOp.pFrom = pwszPaths;
1179 if ((fMask & CMIC_MASK_SHIFT_DOWN) == 0)
1180 FileOp.fFlags = FOF_ALLOWUNDO;
1181
1182 if (SHFileOperationW(&FileOp) != 0)
1183 {
1184 ERR("SHFileOperation failed with 0x%x for %s\n", GetLastError(), debugstr_w(pwszPaths));
1185 hr = E_FAIL;
1186 }
1187
1188 HeapFree(GetProcessHeap(), 0, pwszPaths);
1189 SHFree(pidl);
1190 _ILFreeaPidl(apidl, lpcida->cidl);
1191 ReleaseStgMedium(&medium);
1192
1193 return hr;
1194 }
1195
1196 struct DeleteThreadData {
1197 IStream *s;
1198 DWORD fMask;
1199 };
1200
1201 DWORD WINAPI DoDeleteThreadProc(LPVOID lpParameter)
1202 {
1203 DeleteThreadData *data = static_cast<DeleteThreadData*>(lpParameter);
1204 CoInitialize(NULL);
1205 IDataObject *pDataObject;
1206 HRESULT hr = CoGetInterfaceAndReleaseStream (data->s, IID_PPV_ARG(IDataObject, &pDataObject));
1207 if (SUCCEEDED(hr))
1208 {
1209 DoDeleteDataObject(pDataObject, data->fMask);
1210 }
1211 pDataObject->Release();
1212 CoUninitialize();
1213 HeapFree(GetProcessHeap(), 0, data);
1214 return 0;
1215 }
1216
1217 void DoDeleteAsync(IDataObject *pda, DWORD fMask)
1218 {
1219 DeleteThreadData *data = static_cast<DeleteThreadData*>(HeapAlloc(GetProcessHeap(), 0, sizeof(DeleteThreadData)));
1220 data->fMask = fMask;
1221 CoMarshalInterThreadInterfaceInStream(IID_IDataObject, pda, &data->s);
1222 SHCreateThread(DoDeleteThreadProc, data, NULL, NULL);
1223 }
1224
1225 /*************************************************************************
1226 * SHEmptyRecycleBinA (SHELL32.@)
1227 */
1228 HRESULT WINAPI SHEmptyRecycleBinA(HWND hwnd, LPCSTR pszRootPath, DWORD dwFlags)
1229 {
1230 LPWSTR szRootPathW = NULL;
1231 int len;
1232 HRESULT hr;
1233
1234 TRACE("%p, %s, 0x%08x\n", hwnd, debugstr_a(pszRootPath), dwFlags);
1235
1236 if (pszRootPath)
1237 {
1238 len = MultiByteToWideChar(CP_ACP, 0, pszRootPath, -1, NULL, 0);
1239 if (len == 0)
1240 return HRESULT_FROM_WIN32(GetLastError());
1241 szRootPathW = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1242 if (!szRootPathW)
1243 return E_OUTOFMEMORY;
1244 if (MultiByteToWideChar(CP_ACP, 0, pszRootPath, -1, szRootPathW, len) == 0)
1245 {
1246 HeapFree(GetProcessHeap(), 0, szRootPathW);
1247 return HRESULT_FROM_WIN32(GetLastError());
1248 }
1249 }
1250
1251 hr = SHEmptyRecycleBinW(hwnd, szRootPathW, dwFlags);
1252 HeapFree(GetProcessHeap(), 0, szRootPathW);
1253
1254 return hr;
1255 }
1256
1257 HRESULT WINAPI SHEmptyRecycleBinW(HWND hwnd, LPCWSTR pszRootPath, DWORD dwFlags)
1258 {
1259 WCHAR szPath[MAX_PATH] = {0}, szBuffer[MAX_PATH];
1260 DWORD dwSize, dwType, count;
1261 LONG ret;
1262 IShellFolder *pDesktop, *pRecycleBin;
1263 PIDLIST_ABSOLUTE pidlRecycleBin;
1264 PITEMID_CHILD pidl;
1265 HRESULT hr = S_OK;
1266 LPENUMIDLIST penumFiles;
1267 STRRET StrRet;
1268
1269 TRACE("%p, %s, 0x%08x\n", hwnd, debugstr_w(pszRootPath), dwFlags);
1270
1271 if (!(dwFlags & SHERB_NOCONFIRMATION))
1272 {
1273 hr = SHGetDesktopFolder(&pDesktop);
1274 if (FAILED(hr))
1275 return hr;
1276 hr = SHGetFolderLocation(NULL, CSIDL_BITBUCKET, NULL, 0, &pidlRecycleBin);
1277 if (FAILED(hr))
1278 {
1279 pDesktop->Release();
1280 return hr;
1281 }
1282 hr = pDesktop->BindToObject(pidlRecycleBin, NULL, IID_PPV_ARG(IShellFolder, &pRecycleBin));
1283 CoTaskMemFree(pidlRecycleBin);
1284 pDesktop->Release();
1285 if (FAILED(hr))
1286 return hr;
1287 hr = pRecycleBin->EnumObjects(hwnd, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &penumFiles);
1288 if (FAILED(hr))
1289 {
1290 pRecycleBin->Release();
1291 return hr;
1292 }
1293
1294 count = 0;
1295 if (hr != S_FALSE)
1296 {
1297 while (penumFiles->Next(1, &pidl, NULL) == S_OK)
1298 {
1299 count++;
1300 pRecycleBin->GetDisplayNameOf(pidl, SHGDN_NORMAL, &StrRet);
1301 StrRetToBuf(&StrRet, pidl, szBuffer, _countof(szBuffer));
1302 CoTaskMemFree(pidl);
1303 }
1304 penumFiles->Release();
1305 }
1306 pRecycleBin->Release();
1307
1308 switch (count)
1309 {
1310 case 0:
1311 /* no files, don't need confirmation */
1312 break;
1313
1314 case 1:
1315 /* we have only one item inside the bin, so show a message box with its name */
1316 if (ShellMessageBoxW(shell32_hInstance, hwnd, MAKEINTRESOURCEW(IDS_DELETEITEM_TEXT), MAKEINTRESOURCEW(IDS_EMPTY_BITBUCKET),
1317 MB_ICONEXCLAMATION | MB_YESNO | MB_DEFBUTTON2, szBuffer) == IDNO)
1318 {
1319 return S_OK;
1320 }
1321 break;
1322
1323 default:
1324 /* we have more than one item, so show a message box with the count of the items */
1325 StringCbPrintfW(szBuffer, sizeof(szBuffer), L"%u", count);
1326 if (ShellMessageBoxW(shell32_hInstance, hwnd, MAKEINTRESOURCEW(IDS_DELETEMULTIPLE_TEXT), MAKEINTRESOURCEW(IDS_EMPTY_BITBUCKET),
1327 MB_ICONEXCLAMATION | MB_YESNO | MB_DEFBUTTON2, szBuffer) == IDNO)
1328 {
1329 return S_OK;
1330 }
1331 break;
1332 }
1333 }
1334
1335 if (dwFlags & SHERB_NOPROGRESSUI)
1336 {
1337 ret = EmptyRecycleBinW(pszRootPath);
1338 }
1339 else
1340 {
1341 /* FIXME
1342 * show a progress dialog
1343 */
1344 ret = EmptyRecycleBinW(pszRootPath);
1345 }
1346
1347 if (!ret)
1348 return HRESULT_FROM_WIN32(GetLastError());
1349
1350 if (!(dwFlags & SHERB_NOSOUND))
1351 {
1352 dwSize = sizeof(szPath);
1353 ret = RegGetValueW(HKEY_CURRENT_USER,
1354 L"AppEvents\\Schemes\\Apps\\Explorer\\EmptyRecycleBin\\.Current",
1355 NULL,
1356 RRF_RT_REG_SZ,
1357 &dwType,
1358 (PVOID)szPath,
1359 &dwSize);
1360 if (ret != ERROR_SUCCESS)
1361 return S_OK;
1362
1363 if (dwType != REG_EXPAND_SZ) /* type dismatch */
1364 return S_OK;
1365
1366 szPath[(sizeof(szPath)/sizeof(WCHAR))-1] = L'\0';
1367 PlaySoundW(szPath, NULL, SND_FILENAME);
1368 }
1369 return S_OK;
1370 }
1371
1372 HRESULT WINAPI SHQueryRecycleBinA(LPCSTR pszRootPath, LPSHQUERYRBINFO pSHQueryRBInfo)
1373 {
1374 LPWSTR szRootPathW = NULL;
1375 int len;
1376 HRESULT hr;
1377
1378 TRACE("%s, %p\n", debugstr_a(pszRootPath), pSHQueryRBInfo);
1379
1380 if (pszRootPath)
1381 {
1382 len = MultiByteToWideChar(CP_ACP, 0, pszRootPath, -1, NULL, 0);
1383 if (len == 0)
1384 return HRESULT_FROM_WIN32(GetLastError());
1385 szRootPathW = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1386 if (!szRootPathW)
1387 return E_OUTOFMEMORY;
1388 if (MultiByteToWideChar(CP_ACP, 0, pszRootPath, -1, szRootPathW, len) == 0)
1389 {
1390 HeapFree(GetProcessHeap(), 0, szRootPathW);
1391 return HRESULT_FROM_WIN32(GetLastError());
1392 }
1393 }
1394
1395 hr = SHQueryRecycleBinW(szRootPathW, pSHQueryRBInfo);
1396 HeapFree(GetProcessHeap(), 0, szRootPathW);
1397
1398 return hr;
1399 }
1400
1401 HRESULT WINAPI SHQueryRecycleBinW(LPCWSTR pszRootPath, LPSHQUERYRBINFO pSHQueryRBInfo)
1402 {
1403 FIXME("%s, %p - stub\n", debugstr_w(pszRootPath), pSHQueryRBInfo);
1404
1405 if (!(pszRootPath) || (pszRootPath[0] == 0) ||
1406 !(pSHQueryRBInfo) || (pSHQueryRBInfo->cbSize < sizeof(SHQUERYRBINFO)))
1407 {
1408 return E_INVALIDARG;
1409 }
1410
1411 pSHQueryRBInfo->i64Size = 0;
1412 pSHQueryRBInfo->i64NumItems = 0;
1413
1414 return S_OK;
1415 }