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