2 * Trash virtual folder support. The trashing engine is implemented in trash.c
4 * Copyright (C) 2006 Mikolaj Zalewski
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #define NONAMELESSUNION
37 #include "wine/debug.h"
39 #include "shell32_main.h"
40 #include "enumidlist.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(recyclebin);
55 static const columninfo RecycleBinColumns[] =
57 {IDS_SHV_COLUMN1, &FMTID_Storage, PID_STG_NAME, SHCOLSTATE_TYPE_STR|SHCOLSTATE_ONBYDEFAULT, LVCFMT_LEFT, 30},
58 {IDS_SHV_COLUMN_DELFROM, &FMTID_Displaced, PID_DISPLACED_FROM, SHCOLSTATE_TYPE_STR|SHCOLSTATE_ONBYDEFAULT, LVCFMT_LEFT, 30},
59 {IDS_SHV_COLUMN_DELDATE, &FMTID_Displaced, PID_DISPLACED_DATE, SHCOLSTATE_TYPE_DATE|SHCOLSTATE_ONBYDEFAULT, LVCFMT_LEFT, 20},
60 {IDS_SHV_COLUMN2, &FMTID_Storage, PID_STG_SIZE, SHCOLSTATE_TYPE_INT|SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 20},
61 {IDS_SHV_COLUMN3, &FMTID_Storage, PID_STG_STORAGETYPE,SHCOLSTATE_TYPE_INT|SHCOLSTATE_ONBYDEFAULT, LVCFMT_LEFT, 20},
62 {IDS_SHV_COLUMN4, &FMTID_Storage, PID_STG_WRITETIME, SHCOLSTATE_TYPE_DATE|SHCOLSTATE_ONBYDEFAULT, LVCFMT_LEFT, 20},
63 /* {"creation time", &FMTID_Storage, PID_STG_CREATETIME, SHCOLSTATE_TYPE_DATE, LVCFMT_LEFT, 20}, */
64 /* {"attribs", &FMTID_Storage, PID_STG_ATTRIBUTES, SHCOLSTATE_TYPE_STR, LVCFMT_LEFT, 20}, */
68 #define COLUMN_DELFROM 1
69 #define COLUMN_DATEDEL 2
72 #define COLUMN_MTIME 5
74 #define COLUMNS_COUNT 6
76 static HRESULT FormatDateTime(LPWSTR buffer, int size, FILETIME ft)
82 FileTimeToLocalFileTime(&ft, &lft);
83 FileTimeToSystemTime(&lft, &time);
85 ret = GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &time, NULL, buffer, size);
86 if (ret>0 && ret<size)
88 /* Append space + time without seconds */
90 GetTimeFormatW(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &time, NULL, &buffer[ret], size - ret);
93 return (ret!=0 ? E_FAIL : S_OK);
100 typedef struct tagRecycleBin
102 const IShellFolder2Vtbl *lpVtbl;
103 const IPersistFolder2Vtbl *lpPersistFolderVtbl;
109 static const IShellFolder2Vtbl recycleBinVtbl;
110 static const IPersistFolder2Vtbl recycleBinPersistVtbl;
112 static RecycleBin *impl_from_IPersistFolder(IPersistFolder2 *iface)
114 return (RecycleBin *)((char *)iface - FIELD_OFFSET(RecycleBin, lpPersistFolderVtbl));
117 static void RecycleBin_Destructor(RecycleBin *This);
119 HRESULT WINAPI RecycleBin_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppOutput)
124 return CLASS_E_NOAGGREGATION;
126 obj = SHAlloc(sizeof(RecycleBin));
128 return E_OUTOFMEMORY;
129 ZeroMemory(obj, sizeof(RecycleBin));
130 obj->lpVtbl = &recycleBinVtbl;
131 obj->lpPersistFolderVtbl = &recycleBinPersistVtbl;
132 if (FAILED(ret = IUnknown_QueryInterface((IUnknown *)obj, riid, ppOutput)))
134 RecycleBin_Destructor(obj);
137 /* InterlockedIncrement(&objCount);*/
141 static void RecycleBin_Destructor(RecycleBin *This)
143 /* InterlockedDecrement(&objCount);*/
148 static HRESULT WINAPI RecycleBin_QueryInterface(IShellFolder2 *iface, REFIID riid, void **ppvObject)
150 RecycleBin *This = (RecycleBin *)iface;
151 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppvObject);
154 if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_IShellFolder)
155 || IsEqualGUID(riid, &IID_IShellFolder2))
158 if (IsEqualGUID(riid, &IID_IPersist) || IsEqualGUID(riid, &IID_IPersistFolder)
159 || IsEqualGUID(riid, &IID_IPersistFolder2))
160 *ppvObject = &This->lpPersistFolderVtbl;
162 if (*ppvObject != NULL)
164 IUnknown_AddRef((IUnknown *)*ppvObject);
167 WARN("no interface %s\n", debugstr_guid(riid));
168 return E_NOINTERFACE;
171 static ULONG WINAPI RecycleBin_AddRef(IShellFolder2 *iface)
173 RecycleBin *This = (RecycleBin *)iface;
174 TRACE("(%p)\n", This);
175 return InterlockedIncrement(&This->refCount);
178 static ULONG WINAPI RecycleBin_Release(IShellFolder2 *iface)
180 RecycleBin *This = (RecycleBin *)iface;
183 TRACE("(%p)\n", This);
184 result = InterlockedDecrement(&This->refCount);
187 TRACE("Destroy object\n");
188 RecycleBin_Destructor(This);
193 static HRESULT WINAPI RecycleBin_ParseDisplayName(IShellFolder2 *This, HWND hwnd, LPBC pbc,
194 LPOLESTR pszDisplayName, ULONG *pchEaten, LPITEMIDLIST *ppidl,
195 ULONG *pdwAttributes)
201 static HRESULT WINAPI RecycleBin_EnumObjects(IShellFolder2 *iface, HWND hwnd, SHCONTF grfFlags, IEnumIDList **ppenumIDList)
203 RecycleBin *This = (RecycleBin *)iface;
210 TRACE("(%p, %p, %x, %p)\n", This, hwnd, grfFlags, ppenumIDList);
212 if (grfFlags & SHCONTF_NONFOLDERS)
214 *ppenumIDList = NULL;
215 if (FAILED(ret = TRASH_EnumItems(&pidls, &pidls_count)))
218 list = IEnumIDList_Constructor();
221 for (i=0; i<pidls_count; i++)
222 if (!AddToEnumList(list, pidls[i]))
224 *ppenumIDList = list;
228 *ppenumIDList = IEnumIDList_Constructor();
229 if (*ppenumIDList == NULL)
230 return E_OUTOFMEMORY;
237 IEnumIDList_Release(list);
238 for (; i<pidls_count; i++)
241 return E_OUTOFMEMORY;
244 static HRESULT WINAPI RecycleBin_BindToObject(IShellFolder2 *This, LPCITEMIDLIST pidl, LPBC pbc, REFIID riid, void **ppv)
246 FIXME("(%p, %p, %p, %s, %p) - stub\n", This, pidl, pbc, debugstr_guid(riid), ppv);
250 static HRESULT WINAPI RecycleBin_BindToStorage(IShellFolder2 *This, LPCITEMIDLIST pidl, LPBC pbc, REFIID riid, void **ppv)
252 FIXME("(%p, %p, %p, %s, %p) - stub\n", This, pidl, pbc, debugstr_guid(riid), ppv);
256 static HRESULT WINAPI RecycleBin_CompareIDs(IShellFolder2 *iface, LPARAM lParam, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
258 RecycleBin *This = (RecycleBin *)iface;
261 TRACE("(%p, %p, %p, %p)\n", This, (void *)lParam, pidl1, pidl2);
262 if (pidl1->mkid.cb != pidl2->mkid.cb)
263 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, pidl1->mkid.cb - pidl2->mkid.cb);
264 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (unsigned short)memcmp(pidl1->mkid.abID, pidl2->mkid.abID, pidl1->mkid.cb));
267 static HRESULT WINAPI RecycleBin_CreateViewObject(IShellFolder2 *iface, HWND hwndOwner, REFIID riid, void **ppv)
269 RecycleBin *This = (RecycleBin *)iface;
271 TRACE("(%p, %p, %s, %p)\n", This, hwndOwner, debugstr_guid(riid), ppv);
274 if (IsEqualGUID(riid, &IID_IShellView))
279 ZeroMemory(&sfv, sizeof(sfv));
280 sfv.cbSize = sizeof(sfv);
281 sfv.pshf = (IShellFolder *)This;
283 TRACE("Calling SHCreateShellFolderViewEx\n");
284 ret = SHCreateShellFolderViewEx(&sfv, &tmp);
285 TRACE("Result: %08x, output: %p\n", (unsigned int)ret, tmp);
290 return E_NOINTERFACE;
293 static HRESULT WINAPI RecycleBin_GetAttributesOf(IShellFolder2 *This, UINT cidl, LPCITEMIDLIST *apidl,
296 TRACE("(%p, %d, {%p, ...}, {%x})\n", This, cidl, apidl[0], *rgfInOut);
297 *rgfInOut &= SFGAO_CANMOVE|SFGAO_CANDELETE|SFGAO_HASPROPSHEET|SFGAO_FILESYSTEM;
301 static HRESULT WINAPI RecycleBin_GetUIObjectOf(IShellFolder2 *This, HWND hwndOwner, UINT cidl, LPCITEMIDLIST *apidl,
302 REFIID riid, UINT *rgfReserved, void **ppv)
304 FIXME("(%p, %p, %d, {%p, ...}, %s, %p, %p): stub!\n", This, hwndOwner, cidl, apidl[0], debugstr_guid(riid), rgfReserved, ppv);
309 static HRESULT WINAPI RecycleBin_GetDisplayNameOf(IShellFolder2 *This, LPCITEMIDLIST pidl, SHGDNF uFlags, STRRET *pName)
311 WIN32_FIND_DATAW data;
313 TRACE("(%p, %p, %x, %p)\n", This, pidl, uFlags, pName);
314 TRASH_UnpackItemID(&pidl->mkid, &data);
315 pName->uType = STRRET_WSTR;
316 pName->u.pOleStr = StrDupW(PathFindFileNameW(data.cFileName));
317 if (pName->u.pOleStr == NULL)
318 return E_OUTOFMEMORY;
323 static HRESULT WINAPI RecycleBin_SetNameOf(IShellFolder2 *This, HWND hwnd, LPCITEMIDLIST pidl, LPCOLESTR pszName,
324 SHGDNF uFlags, LPITEMIDLIST *ppidlOut)
327 return E_FAIL; /* not supported */
330 static HRESULT WINAPI RecycleBin_GetClassID(IPersistFolder2 *This, CLSID *pClassID)
332 TRACE("(%p, %p)\n", This, pClassID);
333 if (This == NULL || pClassID == NULL)
335 *pClassID = CLSID_RecycleBin;
339 static HRESULT WINAPI RecycleBin_Initialize(IPersistFolder2 *iface, LPCITEMIDLIST pidl)
341 RecycleBin *This = impl_from_IPersistFolder(iface);
342 TRACE("(%p, %p)\n", This, pidl);
344 This->pidl = ILClone(pidl);
345 if (This->pidl == NULL)
346 return E_OUTOFMEMORY;
350 static HRESULT WINAPI RecycleBin_GetCurFolder(IPersistFolder2 *iface, LPITEMIDLIST *ppidl)
352 RecycleBin *This = impl_from_IPersistFolder(iface);
354 *ppidl = ILClone(This->pidl);
358 static HRESULT WINAPI RecycleBin_GetDefaultSearchGUID(IShellFolder2 *iface, GUID *pguid)
364 static HRESULT WINAPI RecycleBin_EnumSearches(IShellFolder2 *iface, IEnumExtraSearch **ppEnum)
371 static HRESULT WINAPI RecycleBin_GetDefaultColumn(IShellFolder2 *iface, DWORD dwReserved, ULONG *pSort, ULONG *pDisplay)
373 RecycleBin *This = (RecycleBin *)iface;
374 TRACE("(%p, %x, %p, %p)\n", This, dwReserved, pSort, pDisplay);
380 static HRESULT WINAPI RecycleBin_GetDefaultColumnState(IShellFolder2 *iface, UINT iColumn, SHCOLSTATEF *pcsFlags)
382 RecycleBin *This = (RecycleBin *)iface;
383 TRACE("(%p, %d, %p)\n", This, iColumn, pcsFlags);
384 if (iColumn >= COLUMNS_COUNT)
386 *pcsFlags = RecycleBinColumns[iColumn].pcsFlags;
390 static HRESULT WINAPI RecycleBin_GetDetailsEx(IShellFolder2 *iface, LPCITEMIDLIST pidl, const SHCOLUMNID *pscid, VARIANT *pv)
396 static HRESULT WINAPI RecycleBin_GetDetailsOf(IShellFolder2 *iface, LPCITEMIDLIST pidl, UINT iColumn, LPSHELLDETAILS pDetails)
398 RecycleBin *This = (RecycleBin *)iface;
399 WIN32_FIND_DATAW data;
400 WCHAR buffer[MAX_PATH];
402 TRACE("(%p, %p, %d, %p)\n", This, pidl, iColumn, pDetails);
403 if (iColumn >= COLUMNS_COUNT)
405 pDetails->fmt = RecycleBinColumns[iColumn].fmt;
406 pDetails->cxChar = RecycleBinColumns[iColumn].cxChars;
409 pDetails->str.uType = STRRET_WSTR;
410 LoadStringW(shell32_hInstance, RecycleBinColumns[iColumn].column_name_id, buffer, MAX_PATH);
411 return SHStrDupW(buffer, &pDetails->str.u.pOleStr);
414 if (iColumn == COLUMN_NAME)
415 return RecycleBin_GetDisplayNameOf(iface, pidl, SHGDN_NORMAL, &pDetails->str);
417 TRASH_UnpackItemID(&pidl->mkid, &data);
421 FormatDateTime(buffer, MAX_PATH, data.ftLastAccessTime);
424 lstrcpyW(buffer, data.cFileName);
425 PathRemoveFileSpecW(buffer);
428 StrFormatKBSizeW(((LONGLONG)data.nFileSizeHigh<<32)|data.nFileSizeLow, buffer, MAX_PATH);
431 FormatDateTime(buffer, MAX_PATH, data.ftLastWriteTime);
441 pDetails->str.uType = STRRET_WSTR;
442 return SHStrDupW(buffer, &pDetails->str.u.pOleStr);
445 static HRESULT WINAPI RecycleBin_MapColumnToSCID(IShellFolder2 *iface, UINT iColumn, SHCOLUMNID *pscid)
447 RecycleBin *This = (RecycleBin *)iface;
448 TRACE("(%p, %d, %p)\n", This, iColumn, pscid);
449 if (iColumn>=COLUMNS_COUNT)
451 pscid->fmtid = *RecycleBinColumns[iColumn].fmtId;
452 pscid->pid = RecycleBinColumns[iColumn].pid;
456 static const IShellFolder2Vtbl recycleBinVtbl =
459 RecycleBin_QueryInterface,
464 RecycleBin_ParseDisplayName,
465 RecycleBin_EnumObjects,
466 RecycleBin_BindToObject,
467 RecycleBin_BindToStorage,
468 RecycleBin_CompareIDs,
469 RecycleBin_CreateViewObject,
470 RecycleBin_GetAttributesOf,
471 RecycleBin_GetUIObjectOf,
472 RecycleBin_GetDisplayNameOf,
473 RecycleBin_SetNameOf,
476 RecycleBin_GetDefaultSearchGUID,
477 RecycleBin_EnumSearches,
478 RecycleBin_GetDefaultColumn,
479 RecycleBin_GetDefaultColumnState,
480 RecycleBin_GetDetailsEx,
481 RecycleBin_GetDetailsOf,
482 RecycleBin_MapColumnToSCID
485 static HRESULT WINAPI RecycleBin_IPersistFolder2_QueryInterface(IPersistFolder2 *This, REFIID riid, void **ppvObject)
487 return RecycleBin_QueryInterface((IShellFolder2 *)impl_from_IPersistFolder(This), riid, ppvObject);
490 static ULONG WINAPI RecycleBin_IPersistFolder2_AddRef(IPersistFolder2 *This)
492 return RecycleBin_AddRef((IShellFolder2 *)impl_from_IPersistFolder(This));
495 static ULONG WINAPI RecycleBin_IPersistFolder2_Release(IPersistFolder2 *This)
497 return RecycleBin_Release((IShellFolder2 *)impl_from_IPersistFolder(This));
500 static const IPersistFolder2Vtbl recycleBinPersistVtbl =
503 RecycleBin_IPersistFolder2_QueryInterface,
504 RecycleBin_IPersistFolder2_AddRef,
505 RecycleBin_IPersistFolder2_Release,
508 RecycleBin_GetClassID,
510 RecycleBin_Initialize,
511 /* IPersistFolder2 */
512 RecycleBin_GetCurFolder
515 /*************************************************************************
516 * SHUpdateRecycleBinIcon [SHELL32.@]
520 HRESULT WINAPI SHUpdateRecycleBinIcon(void)