cmd: Introduce helpers to handle directory walk.
[wine.git] / dlls / shell32 / folders.c
blob896286c0b6416fe34fd615cc1340f9e5b8aabd14
1 /*
2 * Copyright 1997 Marcus Meissner
3 * Copyright 1998 Juergen Schmied
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <string.h>
25 #define COBJMACROS
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30 #include "objbase.h"
31 #include "shlguid.h"
33 #include "wine/debug.h"
35 #include "pidl.h"
36 #include "shell32_main.h"
37 #include "shfldr.h"
38 #include "shresdef.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(shell);
42 /***********************************************************************
43 * IExtractIconW implementation
45 typedef struct
47 IExtractIconW IExtractIconW_iface;
48 IExtractIconA IExtractIconA_iface;
49 IPersistFile IPersistFile_iface;
50 LONG ref;
51 LPITEMIDLIST pidl;
52 } IExtractIconWImpl;
54 static inline IExtractIconWImpl *impl_from_IExtractIconW(IExtractIconW *iface)
56 return CONTAINING_RECORD(iface, IExtractIconWImpl, IExtractIconW_iface);
59 static inline IExtractIconWImpl *impl_from_IExtractIconA(IExtractIconA *iface)
61 return CONTAINING_RECORD(iface, IExtractIconWImpl, IExtractIconA_iface);
64 static inline IExtractIconWImpl *impl_from_IPersistFile(IPersistFile *iface)
66 return CONTAINING_RECORD(iface, IExtractIconWImpl, IPersistFile_iface);
70 /**************************************************************************
71 * IExtractIconW::QueryInterface
73 static HRESULT WINAPI IExtractIconW_fnQueryInterface(IExtractIconW *iface, REFIID riid,
74 void **ppv)
76 IExtractIconWImpl *This = impl_from_IExtractIconW(iface);
78 TRACE("(%p)->(\n\tIID:\t%s,%p)\n", This, debugstr_guid(riid), ppv);
80 *ppv = NULL;
81 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IExtractIconW))
82 *ppv = iface;
83 else if (IsEqualIID(riid, &IID_IPersistFile))
84 *ppv = &This->IPersistFile_iface;
85 else if (IsEqualIID(riid, &IID_IExtractIconA))
86 *ppv = &This->IExtractIconA_iface;
88 if(*ppv)
90 IUnknown_AddRef((IUnknown*)*ppv);
91 TRACE("-- Interface: (%p)->(%p)\n", ppv, *ppv);
92 return S_OK;
94 TRACE("-- Interface: E_NOINTERFACE\n");
95 return E_NOINTERFACE;
98 /**************************************************************************
99 * IExtractIconW::AddRef
101 static ULONG WINAPI IExtractIconW_fnAddRef(IExtractIconW * iface)
103 IExtractIconWImpl *This = impl_from_IExtractIconW(iface);
104 ULONG refCount = InterlockedIncrement(&This->ref);
106 TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
108 return refCount;
110 /**************************************************************************
111 * IExtractIconW::Release
113 static ULONG WINAPI IExtractIconW_fnRelease(IExtractIconW * iface)
115 IExtractIconWImpl *This = impl_from_IExtractIconW(iface);
116 ULONG refCount = InterlockedDecrement(&This->ref);
118 TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
120 if (!refCount)
122 TRACE(" destroying IExtractIcon(%p)\n",This);
123 SHFree(This->pidl);
124 free(This);
126 return refCount;
129 static HRESULT getIconLocationForFolder(IExtractIconWImpl *This, UINT uFlags, LPWSTR szIconFile,
130 UINT cchMax, int *piIndex, UINT *pwFlags)
132 int icon_idx;
133 WCHAR wszPath[MAX_PATH];
134 WCHAR wszCLSIDValue[CHARS_IN_GUID];
136 if (SHELL32_GetCustomFolderAttribute(This->pidl, L".ShellClassInfo", L"IconFile",
137 wszPath, MAX_PATH))
139 WCHAR wszIconIndex[10];
140 SHELL32_GetCustomFolderAttribute(This->pidl, L".ShellClassInfo", L"IconIndex",
141 wszIconIndex, 10);
142 *piIndex = wcstol(wszIconIndex, NULL, 10);
144 else if (SHELL32_GetCustomFolderAttribute(This->pidl, L".ShellClassInfo", L"CLSID",
145 wszCLSIDValue, CHARS_IN_GUID) &&
146 HCR_GetDefaultIconW(wszCLSIDValue, szIconFile, cchMax, &icon_idx))
148 *piIndex = icon_idx;
150 else if (SHELL32_GetCustomFolderAttribute(This->pidl, L".ShellClassInfo", L"CLSID2",
151 wszCLSIDValue, CHARS_IN_GUID) &&
152 HCR_GetDefaultIconW(wszCLSIDValue, szIconFile, cchMax, &icon_idx))
154 *piIndex = icon_idx;
156 else
158 if (!HCR_GetDefaultIconW(L"Folder", szIconFile, cchMax, &icon_idx))
160 lstrcpynW(szIconFile, swShell32Name, cchMax);
161 icon_idx = -IDI_SHELL_FOLDER;
164 if (uFlags & GIL_OPENICON)
165 *piIndex = icon_idx<0? icon_idx-1: icon_idx+1;
166 else
167 *piIndex = icon_idx;
170 return S_OK;
173 WCHAR swShell32Name[MAX_PATH];
175 /**************************************************************************
176 * IExtractIconW::GetIconLocation
178 * mapping filetype to icon
180 static HRESULT WINAPI IExtractIconW_fnGetIconLocation(IExtractIconW * iface, UINT uFlags,
181 LPWSTR szIconFile, UINT cchMax, int * piIndex, UINT * pwFlags)
183 IExtractIconWImpl *This = impl_from_IExtractIconW(iface);
184 char sTemp[MAX_PATH];
185 int icon_idx;
186 GUID const * riid;
187 LPITEMIDLIST pSimplePidl = ILFindLastID(This->pidl);
189 TRACE("(%p) (flags=%u, %p, %u, %p, %p)\n", This, uFlags, szIconFile,
190 cchMax, piIndex, pwFlags);
192 if (pwFlags)
193 *pwFlags = 0;
195 if (_ILIsDesktop(pSimplePidl))
197 lstrcpynW(szIconFile, swShell32Name, cchMax);
198 *piIndex = -IDI_SHELL_DESKTOP;
201 /* my computer and other shell extensions */
202 else if ((riid = _ILGetGUIDPointer(pSimplePidl)))
204 WCHAR xriid[50];
206 swprintf(xriid, ARRAY_SIZE(xriid), L"CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
207 riid->Data1, riid->Data2, riid->Data3,
208 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
209 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]);
211 if (HCR_GetDefaultIconW(xriid, szIconFile, cchMax, &icon_idx))
213 *piIndex = icon_idx;
215 else
217 lstrcpynW(szIconFile, swShell32Name, cchMax);
218 if(IsEqualGUID(riid, &CLSID_MyComputer))
219 *piIndex = -IDI_SHELL_MY_COMPUTER;
220 else if(IsEqualGUID(riid, &CLSID_MyDocuments))
221 *piIndex = -IDI_SHELL_MY_DOCUMENTS;
222 else if(IsEqualGUID(riid, &CLSID_NetworkPlaces))
223 *piIndex = -IDI_SHELL_MY_NETWORK_PLACES;
224 else if(IsEqualGUID(riid, &CLSID_UnixFolder) ||
225 IsEqualGUID(riid, &CLSID_UnixDosFolder))
226 *piIndex = -IDI_SHELL_DRIVE;
227 else
228 *piIndex = -IDI_SHELL_FOLDER;
232 else if (_ILIsDrive (pSimplePidl))
234 icon_idx = -1;
236 if (_ILGetDrive(pSimplePidl, sTemp, MAX_PATH))
238 switch(GetDriveTypeA(sTemp))
240 case DRIVE_REMOVABLE: icon_idx = IDI_SHELL_FLOPPY; break;
241 case DRIVE_CDROM: icon_idx = IDI_SHELL_OPTICAL_DRIVE; break;
242 case DRIVE_REMOTE: icon_idx = IDI_SHELL_NETDRIVE; break;
243 case DRIVE_RAMDISK: icon_idx = IDI_SHELL_RAMDISK; break;
247 if (icon_idx != -1)
249 lstrcpynW(szIconFile, swShell32Name, cchMax);
250 *piIndex = -icon_idx;
252 else
254 if (HCR_GetDefaultIconW(L"Drive", szIconFile, cchMax, &icon_idx))
256 *piIndex = icon_idx;
258 else
260 lstrcpynW(szIconFile, swShell32Name, cchMax);
261 *piIndex = -IDI_SHELL_DRIVE;
265 else if (_ILIsFolder (pSimplePidl))
267 getIconLocationForFolder(This, uFlags, szIconFile, cchMax, piIndex, pwFlags);
269 else
271 BOOL found = FALSE;
273 if (_ILIsCPanelStruct(pSimplePidl))
275 if (SUCCEEDED(CPanel_GetIconLocationW(pSimplePidl, szIconFile, cchMax, piIndex)))
276 found = TRUE;
278 else if (_ILGetExtension(pSimplePidl, sTemp, MAX_PATH))
280 if (HCR_MapTypeToValueA(sTemp, sTemp, MAX_PATH, TRUE)
281 && HCR_GetDefaultIconA(sTemp, sTemp, MAX_PATH, &icon_idx))
283 if (!lstrcmpA("%1", sTemp)) /* icon is in the file */
285 SHGetPathFromIDListW(This->pidl, szIconFile);
286 *piIndex = 0;
288 else
290 MultiByteToWideChar(CP_ACP, 0, sTemp, -1, szIconFile, cchMax);
291 *piIndex = icon_idx;
294 found = TRUE;
296 else if (!lstrcmpiA(sTemp, "lnkfile"))
298 /* extract icon from shell shortcut */
299 IShellFolder* dsf;
300 IShellLinkW* psl;
302 if (SUCCEEDED(SHGetDesktopFolder(&dsf)))
304 HRESULT hr = IShellFolder_GetUIObjectOf(dsf, NULL, 1, (LPCITEMIDLIST*)&This->pidl, &IID_IShellLinkW, NULL, (LPVOID*)&psl);
306 if (SUCCEEDED(hr))
308 hr = IShellLinkW_GetIconLocation(psl, szIconFile, cchMax, piIndex);
310 if (SUCCEEDED(hr) && *szIconFile)
311 found = TRUE;
313 IShellLinkW_Release(psl);
316 IShellFolder_Release(dsf);
321 if (!found) /* default icon */
323 lstrcpynW(szIconFile, swShell32Name, cchMax);
324 *piIndex = 0;
328 TRACE("-- %s %x\n", debugstr_w(szIconFile), *piIndex);
329 return S_OK;
332 /**************************************************************************
333 * IExtractIconW::Extract
335 static HRESULT WINAPI IExtractIconW_fnExtract(IExtractIconW * iface, LPCWSTR pszFile,
336 UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
338 IExtractIconWImpl *This = impl_from_IExtractIconW(iface);
339 int index;
340 HIMAGELIST big_icons, small_icons;
342 FIXME("(%p) (file=%s index=%d %p %p size=%08x) semi-stub\n", This, debugstr_w(pszFile),
343 (signed)nIconIndex, phiconLarge, phiconSmall, nIconSize);
345 index = SIC_GetIconIndex(pszFile, nIconIndex, 0);
346 Shell_GetImageLists( &big_icons, &small_icons );
347 if (phiconLarge)
348 *phiconLarge = ImageList_GetIcon(big_icons, index, ILD_TRANSPARENT);
350 if (phiconSmall)
351 *phiconSmall = ImageList_GetIcon(small_icons, index, ILD_TRANSPARENT);
353 return S_OK;
356 static const IExtractIconWVtbl eivt =
358 IExtractIconW_fnQueryInterface,
359 IExtractIconW_fnAddRef,
360 IExtractIconW_fnRelease,
361 IExtractIconW_fnGetIconLocation,
362 IExtractIconW_fnExtract
365 /**************************************************************************
366 * IExtractIconA::QueryInterface
368 static HRESULT WINAPI IExtractIconA_fnQueryInterface(IExtractIconA * iface, REFIID riid,
369 void **ppv)
371 IExtractIconWImpl *This = impl_from_IExtractIconA(iface);
373 return IExtractIconW_QueryInterface(&This->IExtractIconW_iface, riid, ppv);
376 /**************************************************************************
377 * IExtractIconA::AddRef
379 static ULONG WINAPI IExtractIconA_fnAddRef(IExtractIconA * iface)
381 IExtractIconWImpl *This = impl_from_IExtractIconA(iface);
383 return IExtractIconW_AddRef(&This->IExtractIconW_iface);
385 /**************************************************************************
386 * IExtractIconA::Release
388 static ULONG WINAPI IExtractIconA_fnRelease(IExtractIconA * iface)
390 IExtractIconWImpl *This = impl_from_IExtractIconA(iface);
392 return IExtractIconW_Release(&This->IExtractIconW_iface);
394 /**************************************************************************
395 * IExtractIconA::GetIconLocation
397 * mapping filetype to icon
399 static HRESULT WINAPI IExtractIconA_fnGetIconLocation(IExtractIconA * iface, UINT uFlags,
400 LPSTR szIconFile, UINT cchMax, int * piIndex, UINT * pwFlags)
402 IExtractIconWImpl *This = impl_from_IExtractIconA(iface);
403 HRESULT ret;
404 WCHAR *lpwstrFile = malloc(cchMax * sizeof(WCHAR));
406 TRACE("(%p) (flags=%u %p %u %p %p)\n", This, uFlags, szIconFile, cchMax, piIndex, pwFlags);
408 ret = IExtractIconW_GetIconLocation(&This->IExtractIconW_iface, uFlags, lpwstrFile, cchMax,
409 piIndex, pwFlags);
410 WideCharToMultiByte(CP_ACP, 0, lpwstrFile, -1, szIconFile, cchMax, NULL, NULL);
411 free(lpwstrFile);
413 TRACE("-- %s %x\n", szIconFile, *piIndex);
414 return ret;
416 /**************************************************************************
417 * IExtractIconA::Extract
419 static HRESULT WINAPI IExtractIconA_fnExtract(IExtractIconA * iface, LPCSTR pszFile,
420 UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
422 IExtractIconWImpl *This = impl_from_IExtractIconA(iface);
423 HRESULT ret;
424 INT len = MultiByteToWideChar(CP_ACP, 0, pszFile, -1, NULL, 0);
425 WCHAR *lpwstrFile = malloc(len * sizeof(WCHAR));
427 TRACE("(%p) (file=%p index=%u %p %p size=%u)\n", This, pszFile, nIconIndex, phiconLarge, phiconSmall, nIconSize);
429 MultiByteToWideChar(CP_ACP, 0, pszFile, -1, lpwstrFile, len);
430 ret = IExtractIconW_Extract(&This->IExtractIconW_iface, lpwstrFile, nIconIndex, phiconLarge,
431 phiconSmall, nIconSize);
432 free(lpwstrFile);
433 return ret;
436 static const IExtractIconAVtbl eiavt =
438 IExtractIconA_fnQueryInterface,
439 IExtractIconA_fnAddRef,
440 IExtractIconA_fnRelease,
441 IExtractIconA_fnGetIconLocation,
442 IExtractIconA_fnExtract
445 /************************************************************************
446 * IEIPersistFile::QueryInterface
448 static HRESULT WINAPI IEIPersistFile_fnQueryInterface(IPersistFile *iface, REFIID iid,
449 void **ppv)
451 IExtractIconWImpl *This = impl_from_IPersistFile(iface);
453 return IExtractIconW_QueryInterface(&This->IExtractIconW_iface, iid, ppv);
456 /************************************************************************
457 * IEIPersistFile::AddRef
459 static ULONG WINAPI IEIPersistFile_fnAddRef(IPersistFile *iface)
461 IExtractIconWImpl *This = impl_from_IPersistFile(iface);
463 return IExtractIconW_AddRef(&This->IExtractIconW_iface);
466 /************************************************************************
467 * IEIPersistFile::Release
469 static ULONG WINAPI IEIPersistFile_fnRelease(IPersistFile *iface)
471 IExtractIconWImpl *This = impl_from_IPersistFile(iface);
473 return IExtractIconW_Release(&This->IExtractIconW_iface);
476 /************************************************************************
477 * IEIPersistFile::GetClassID
479 static HRESULT WINAPI IEIPersistFile_fnGetClassID(IPersistFile *iface, LPCLSID lpClassId)
481 CLSID StdFolderID = { 0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} };
483 if (lpClassId==NULL)
484 return E_POINTER;
486 *lpClassId = StdFolderID;
488 return S_OK;
491 /************************************************************************
492 * IEIPersistFile_Load
494 static HRESULT WINAPI IEIPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName,
495 DWORD dwMode)
497 IExtractIconWImpl *This = impl_from_IPersistFile(iface);
499 FIXME("%p\n", This);
500 return E_NOTIMPL;
503 static const IPersistFileVtbl pfvt =
505 IEIPersistFile_fnQueryInterface,
506 IEIPersistFile_fnAddRef,
507 IEIPersistFile_fnRelease,
508 IEIPersistFile_fnGetClassID,
509 (void *) 0xdeadbeef /* IEIPersistFile_fnIsDirty */,
510 IEIPersistFile_fnLoad,
511 (void *) 0xdeadbeef /* IEIPersistFile_fnSave */,
512 (void *) 0xdeadbeef /* IEIPersistFile_fnSaveCompleted */,
513 (void *) 0xdeadbeef /* IEIPersistFile_fnGetCurFile */
516 static IExtractIconWImpl *extracticon_create(LPCITEMIDLIST pidl)
518 IExtractIconWImpl *ei;
520 TRACE("%p\n", pidl);
522 ei = malloc(sizeof(*ei));
523 ei->ref=1;
524 ei->IExtractIconW_iface.lpVtbl = &eivt;
525 ei->IExtractIconA_iface.lpVtbl = &eiavt;
526 ei->IPersistFile_iface.lpVtbl = &pfvt;
527 ei->pidl=ILClone(pidl);
529 pdump(pidl);
531 TRACE("(%p)\n", ei);
532 return ei;
535 IExtractIconW *IExtractIconW_Constructor(LPCITEMIDLIST pidl)
537 IExtractIconWImpl *ei = extracticon_create(pidl);
539 return &ei->IExtractIconW_iface;
542 IExtractIconA *IExtractIconA_Constructor(LPCITEMIDLIST pidl)
544 IExtractIconWImpl *ei = extracticon_create(pidl);
546 return &ei->IExtractIconA_iface;