push a9ea98eed33411fd355fd4e8b78f7880f1dd09a3
[wine/hacks.git] / dlls / shell32 / folders.c
blob8766c50f4c8e58c560f778f2f1c645108ac11211
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 "config.h"
21 #include "wine/port.h"
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <string.h>
28 #define COBJMACROS
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winerror.h"
33 #include "objbase.h"
34 #include "undocshell.h"
35 #include "shlguid.h"
37 #include "wine/debug.h"
39 #include "pidl.h"
40 #include "shell32_main.h"
41 #include "shfldr.h"
42 #include "shresdef.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(shell);
46 /***********************************************************************
47 * IExtractIconW implementation
49 typedef struct
51 const IExtractIconWVtbl *lpVtbl;
52 LONG ref;
53 const IPersistFileVtbl *lpvtblPersistFile;
54 const IExtractIconAVtbl *lpvtblExtractIconA;
55 LPITEMIDLIST pidl;
56 } IExtractIconWImpl;
58 static const IExtractIconAVtbl eiavt;
59 static const IExtractIconWVtbl eivt;
60 static const IPersistFileVtbl pfvt;
62 static inline IExtractIconW *impl_from_IPersistFile( IPersistFile *iface )
64 return (IExtractIconW *)((char*)iface - FIELD_OFFSET(IExtractIconWImpl, lpvtblPersistFile));
67 static inline IExtractIconW *impl_from_IExtractIconA( IExtractIconA *iface )
69 return (IExtractIconW *)((char*)iface - FIELD_OFFSET(IExtractIconWImpl, lpvtblExtractIconA));
73 /**************************************************************************
74 * IExtractIconW_Constructor
76 IExtractIconW* IExtractIconW_Constructor(LPCITEMIDLIST pidl)
78 IExtractIconWImpl* ei;
80 TRACE("%p\n", pidl);
82 ei = HeapAlloc(GetProcessHeap(),0,sizeof(IExtractIconWImpl));
83 ei->ref=1;
84 ei->lpVtbl = &eivt;
85 ei->lpvtblPersistFile = &pfvt;
86 ei->lpvtblExtractIconA = &eiavt;
87 ei->pidl=ILClone(pidl);
89 pdump(pidl);
91 TRACE("(%p)\n", ei);
92 return (IExtractIconW *)ei;
94 /**************************************************************************
95 * IExtractIconW_QueryInterface
97 static HRESULT WINAPI IExtractIconW_fnQueryInterface(IExtractIconW *iface, REFIID riid, LPVOID *ppvObj)
99 IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
101 TRACE("(%p)->(\n\tIID:\t%s,%p)\n", This, debugstr_guid(riid), ppvObj);
103 *ppvObj = NULL;
105 if (IsEqualIID(riid, &IID_IUnknown)) /*IUnknown*/
107 *ppvObj = This;
109 else if (IsEqualIID(riid, &IID_IPersistFile)) /*IExtractIcon*/
111 *ppvObj = (IPersistFile*)&(This->lpvtblPersistFile);
113 else if (IsEqualIID(riid, &IID_IExtractIconA)) /*IExtractIcon*/
115 *ppvObj = (IExtractIconA*)&(This->lpvtblExtractIconA);
117 else if (IsEqualIID(riid, &IID_IExtractIconW)) /*IExtractIcon*/
119 *ppvObj = (IExtractIconW*)This;
122 if(*ppvObj)
124 IExtractIconW_AddRef((IExtractIconW*) *ppvObj);
125 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
126 return S_OK;
128 TRACE("-- Interface: E_NOINTERFACE\n");
129 return E_NOINTERFACE;
132 /**************************************************************************
133 * IExtractIconW_AddRef
135 static ULONG WINAPI IExtractIconW_fnAddRef(IExtractIconW * iface)
137 IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
138 ULONG refCount = InterlockedIncrement(&This->ref);
140 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
142 return refCount;
144 /**************************************************************************
145 * IExtractIconW_Release
147 static ULONG WINAPI IExtractIconW_fnRelease(IExtractIconW * iface)
149 IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
150 ULONG refCount = InterlockedDecrement(&This->ref);
152 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
154 if (!refCount)
156 TRACE(" destroying IExtractIcon(%p)\n",This);
157 SHFree(This->pidl);
158 HeapFree(GetProcessHeap(),0,This);
159 return 0;
161 return refCount;
164 static HRESULT getIconLocationForFolder(IExtractIconW *iface, UINT uFlags,
165 LPWSTR szIconFile, UINT cchMax, int *piIndex, UINT *pwFlags)
167 IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
168 int icon_idx;
169 WCHAR wszPath[MAX_PATH];
170 WCHAR wszCLSIDValue[CHARS_IN_GUID];
171 static const WCHAR shellClassInfo[] = { '.','S','h','e','l','l','C','l','a','s','s','I','n','f','o',0 };
172 static const WCHAR iconFile[] = { 'I','c','o','n','F','i','l','e',0 };
173 static const WCHAR clsid[] = { 'C','L','S','I','D',0 };
174 static const WCHAR clsid2[] = { 'C','L','S','I','D','2',0 };
175 static const WCHAR iconIndex[] = { 'I','c','o','n','I','n','d','e','x',0 };
177 if (SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, iconFile,
178 wszPath, MAX_PATH))
180 WCHAR wszIconIndex[10];
181 SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, iconIndex,
182 wszIconIndex, 10);
183 *piIndex = atoiW(wszIconIndex);
185 else if (SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, clsid,
186 wszCLSIDValue, CHARS_IN_GUID) &&
187 HCR_GetDefaultIconW(wszCLSIDValue, szIconFile, cchMax, &icon_idx))
189 *piIndex = icon_idx;
191 else if (SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, clsid2,
192 wszCLSIDValue, CHARS_IN_GUID) &&
193 HCR_GetDefaultIconW(wszCLSIDValue, szIconFile, cchMax, &icon_idx))
195 *piIndex = icon_idx;
197 else
199 static const WCHAR folder[] = { 'F','o','l','d','e','r',0 };
201 if (!HCR_GetDefaultIconW(folder, szIconFile, cchMax, &icon_idx))
203 lstrcpynW(szIconFile, swShell32Name, cchMax);
204 icon_idx = -IDI_SHELL_FOLDER;
207 if (uFlags & GIL_OPENICON)
208 *piIndex = icon_idx<0? icon_idx-1: icon_idx+1;
209 else
210 *piIndex = icon_idx;
213 return S_OK;
216 WCHAR swShell32Name[MAX_PATH];
218 /**************************************************************************
219 * IExtractIconW_GetIconLocation
221 * mapping filetype to icon
223 static HRESULT WINAPI IExtractIconW_fnGetIconLocation(
224 IExtractIconW * iface,
225 UINT uFlags, /* GIL_ flags */
226 LPWSTR szIconFile,
227 UINT cchMax,
228 int * piIndex,
229 UINT * pwFlags) /* returned GIL_ flags */
231 IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
233 char sTemp[MAX_PATH];
234 int icon_idx;
235 GUID const * riid;
236 LPITEMIDLIST pSimplePidl = ILFindLastID(This->pidl);
238 TRACE("(%p) (flags=%u %p %u %p %p)\n", This, uFlags, szIconFile, cchMax, piIndex, pwFlags);
240 if (pwFlags)
241 *pwFlags = 0;
243 if (_ILIsDesktop(pSimplePidl))
245 lstrcpynW(szIconFile, swShell32Name, cchMax);
246 *piIndex = -IDI_SHELL_DESKTOP;
249 /* my computer and other shell extensions */
250 else if ((riid = _ILGetGUIDPointer(pSimplePidl)))
252 static const WCHAR fmt[] = { 'C','L','S','I','D','\\',
253 '{','%','0','8','l','x','-','%','0','4','x','-','%','0','4','x','-',
254 '%','0','2','x','%','0','2','x','-','%','0','2','x', '%','0','2','x',
255 '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x','}',0 };
256 WCHAR xriid[50];
258 sprintfW(xriid, fmt,
259 riid->Data1, riid->Data2, riid->Data3,
260 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
261 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]);
263 if (HCR_GetDefaultIconW(xriid, szIconFile, cchMax, &icon_idx))
265 *piIndex = icon_idx;
267 else
269 lstrcpynW(szIconFile, swShell32Name, cchMax);
270 if(IsEqualGUID(riid, &CLSID_MyComputer))
271 *piIndex = -IDI_SHELL_MY_COMPUTER;
272 else if(IsEqualGUID(riid, &CLSID_MyDocuments))
273 *piIndex = -IDI_SHELL_MY_DOCUMENTS;
274 else if(IsEqualGUID(riid, &CLSID_NetworkPlaces))
275 *piIndex = -IDI_SHELL_MY_NETWORK_PLACES;
276 else if(IsEqualGUID(riid, &CLSID_UnixFolder) ||
277 IsEqualGUID(riid, &CLSID_UnixDosFolder))
278 *piIndex = -IDI_SHELL_DRIVE;
279 else
280 *piIndex = -IDI_SHELL_FOLDER;
284 else if (_ILIsDrive (pSimplePidl))
286 static const WCHAR drive[] = { 'D','r','i','v','e',0 };
288 int icon_idx = -1;
290 if (_ILGetDrive(pSimplePidl, sTemp, MAX_PATH))
292 switch(GetDriveTypeA(sTemp))
294 case DRIVE_REMOVABLE: icon_idx = IDI_SHELL_FLOPPY; break;
295 case DRIVE_CDROM: icon_idx = IDI_SHELL_CDROM; break;
296 case DRIVE_REMOTE: icon_idx = IDI_SHELL_NETDRIVE; break;
297 case DRIVE_RAMDISK: icon_idx = IDI_SHELL_RAMDISK; break;
301 if (icon_idx != -1)
303 lstrcpynW(szIconFile, swShell32Name, cchMax);
304 *piIndex = -icon_idx;
306 else
308 if (HCR_GetDefaultIconW(drive, szIconFile, cchMax, &icon_idx))
310 *piIndex = icon_idx;
312 else
314 lstrcpynW(szIconFile, swShell32Name, cchMax);
315 *piIndex = -IDI_SHELL_DRIVE;
319 else if (_ILIsFolder (pSimplePidl))
321 getIconLocationForFolder(iface, uFlags, szIconFile, cchMax, piIndex,
322 pwFlags);
324 else
326 BOOL found = FALSE;
328 if (_ILIsCPanelStruct(pSimplePidl))
330 if (SUCCEEDED(CPanel_GetIconLocationW(pSimplePidl, szIconFile, cchMax, piIndex)))
331 found = TRUE;
333 else if (_ILGetExtension(pSimplePidl, sTemp, MAX_PATH))
335 if (HCR_MapTypeToValueA(sTemp, sTemp, MAX_PATH, TRUE)
336 && HCR_GetDefaultIconA(sTemp, sTemp, MAX_PATH, &icon_idx))
338 if (!lstrcmpA("%1", sTemp)) /* icon is in the file */
340 SHGetPathFromIDListW(This->pidl, szIconFile);
341 *piIndex = 0;
343 else
345 MultiByteToWideChar(CP_ACP, 0, sTemp, -1, szIconFile, cchMax);
346 *piIndex = icon_idx;
349 found = TRUE;
351 else if (!lstrcmpiA(sTemp, "lnkfile"))
353 /* extract icon from shell shortcut */
354 IShellFolder* dsf;
355 IShellLinkW* psl;
357 if (SUCCEEDED(SHGetDesktopFolder(&dsf)))
359 HRESULT hr = IShellFolder_GetUIObjectOf(dsf, NULL, 1, (LPCITEMIDLIST*)&This->pidl, &IID_IShellLinkW, NULL, (LPVOID*)&psl);
361 if (SUCCEEDED(hr))
363 hr = IShellLinkW_GetIconLocation(psl, szIconFile, MAX_PATH, piIndex);
365 if (SUCCEEDED(hr) && *szIconFile)
366 found = TRUE;
368 IShellLinkW_Release(psl);
371 IShellFolder_Release(dsf);
376 if (!found) /* default icon */
378 lstrcpynW(szIconFile, swShell32Name, cchMax);
379 *piIndex = 0;
383 TRACE("-- %s %x\n", debugstr_w(szIconFile), *piIndex);
384 return NOERROR;
387 /**************************************************************************
388 * IExtractIconW_Extract
390 static HRESULT WINAPI IExtractIconW_fnExtract(IExtractIconW * iface, LPCWSTR pszFile, UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
392 IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
393 int index;
395 FIXME("(%p) (file=%p index=%d %p %p size=%08x) semi-stub\n", This, debugstr_w(pszFile), (signed)nIconIndex,
396 phiconLarge, phiconSmall, nIconSize);
398 index = SIC_GetIconIndex(pszFile, nIconIndex, 0);
400 if (phiconLarge)
401 *phiconLarge = ImageList_GetIcon(ShellBigIconList, index, ILD_TRANSPARENT);
403 if (phiconSmall)
404 *phiconSmall = ImageList_GetIcon(ShellSmallIconList, index, ILD_TRANSPARENT);
406 return S_OK;
409 static const IExtractIconWVtbl eivt =
411 IExtractIconW_fnQueryInterface,
412 IExtractIconW_fnAddRef,
413 IExtractIconW_fnRelease,
414 IExtractIconW_fnGetIconLocation,
415 IExtractIconW_fnExtract
418 /**************************************************************************
419 * IExtractIconA_Constructor
421 IExtractIconA* IExtractIconA_Constructor(LPCITEMIDLIST pidl)
423 IExtractIconWImpl *This = (IExtractIconWImpl *)IExtractIconW_Constructor(pidl);
424 IExtractIconA *eia = (IExtractIconA *)&This->lpvtblExtractIconA;
426 TRACE("(%p)->(%p)\n", This, eia);
427 return eia;
429 /**************************************************************************
430 * IExtractIconA_QueryInterface
432 static HRESULT WINAPI IExtractIconA_fnQueryInterface(IExtractIconA * iface, REFIID riid, LPVOID *ppvObj)
434 IExtractIconW *This = impl_from_IExtractIconA(iface);
436 return IExtractIconW_QueryInterface(This, riid, ppvObj);
439 /**************************************************************************
440 * IExtractIconA_AddRef
442 static ULONG WINAPI IExtractIconA_fnAddRef(IExtractIconA * iface)
444 IExtractIconW *This = impl_from_IExtractIconA(iface);
446 return IExtractIconW_AddRef(This);
448 /**************************************************************************
449 * IExtractIconA_Release
451 static ULONG WINAPI IExtractIconA_fnRelease(IExtractIconA * iface)
453 IExtractIconW *This = impl_from_IExtractIconA(iface);
455 return IExtractIconW_AddRef(This);
457 /**************************************************************************
458 * IExtractIconA_GetIconLocation
460 * mapping filetype to icon
462 static HRESULT WINAPI IExtractIconA_fnGetIconLocation(
463 IExtractIconA * iface,
464 UINT uFlags,
465 LPSTR szIconFile,
466 UINT cchMax,
467 int * piIndex,
468 UINT * pwFlags)
470 HRESULT ret;
471 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, cchMax * sizeof(WCHAR));
472 IExtractIconW *This = impl_from_IExtractIconA(iface);
474 TRACE("(%p) (flags=%u %p %u %p %p)\n", This, uFlags, szIconFile, cchMax, piIndex, pwFlags);
476 ret = IExtractIconW_GetIconLocation(This, uFlags, lpwstrFile, cchMax, piIndex, pwFlags);
477 WideCharToMultiByte(CP_ACP, 0, lpwstrFile, -1, szIconFile, cchMax, NULL, NULL);
478 HeapFree(GetProcessHeap(), 0, lpwstrFile);
480 TRACE("-- %s %x\n", szIconFile, *piIndex);
481 return ret;
483 /**************************************************************************
484 * IExtractIconA_Extract
486 static HRESULT WINAPI IExtractIconA_fnExtract(IExtractIconA * iface, LPCSTR pszFile, UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
488 HRESULT ret;
489 INT len = MultiByteToWideChar(CP_ACP, 0, pszFile, -1, NULL, 0);
490 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
491 IExtractIconW *This = impl_from_IExtractIconA(iface);
493 TRACE("(%p) (file=%p index=%u %p %p size=%u)\n", This, pszFile, nIconIndex, phiconLarge, phiconSmall, nIconSize);
495 MultiByteToWideChar(CP_ACP, 0, pszFile, -1, lpwstrFile, len);
496 ret = IExtractIconW_Extract(This, lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIconSize);
497 HeapFree(GetProcessHeap(), 0, lpwstrFile);
498 return ret;
501 static const IExtractIconAVtbl eiavt =
503 IExtractIconA_fnQueryInterface,
504 IExtractIconA_fnAddRef,
505 IExtractIconA_fnRelease,
506 IExtractIconA_fnGetIconLocation,
507 IExtractIconA_fnExtract
510 /************************************************************************
511 * IEIPersistFile_QueryInterface (IUnknown)
513 static HRESULT WINAPI IEIPersistFile_fnQueryInterface(
514 IPersistFile *iface,
515 REFIID iid,
516 LPVOID *ppvObj)
518 IExtractIconW *This = impl_from_IPersistFile(iface);
520 return IExtractIconW_QueryInterface(This, iid, ppvObj);
523 /************************************************************************
524 * IEIPersistFile_AddRef (IUnknown)
526 static ULONG WINAPI IEIPersistFile_fnAddRef(
527 IPersistFile *iface)
529 IExtractIconW *This = impl_from_IPersistFile(iface);
531 return IExtractIconW_AddRef(This);
534 /************************************************************************
535 * IEIPersistFile_Release (IUnknown)
537 static ULONG WINAPI IEIPersistFile_fnRelease(
538 IPersistFile *iface)
540 IExtractIconW *This = impl_from_IPersistFile(iface);
542 return IExtractIconW_Release(This);
545 /************************************************************************
546 * IEIPersistFile_GetClassID (IPersist)
548 static HRESULT WINAPI IEIPersistFile_fnGetClassID(
549 IPersistFile *iface,
550 LPCLSID lpClassId)
552 CLSID StdFolderID = { 0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} };
554 if (lpClassId==NULL)
555 return E_POINTER;
557 *lpClassId = StdFolderID;
559 return S_OK;
562 /************************************************************************
563 * IEIPersistFile_Load (IPersistFile)
565 static HRESULT WINAPI IEIPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
567 IExtractIconW *This = impl_from_IPersistFile(iface);
568 FIXME("%p\n", This);
569 return E_NOTIMPL;
573 static const IPersistFileVtbl pfvt =
575 IEIPersistFile_fnQueryInterface,
576 IEIPersistFile_fnAddRef,
577 IEIPersistFile_fnRelease,
578 IEIPersistFile_fnGetClassID,
579 (void *) 0xdeadbeef /* IEIPersistFile_fnIsDirty */,
580 IEIPersistFile_fnLoad,
581 (void *) 0xdeadbeef /* IEIPersistFile_fnSave */,
582 (void *) 0xdeadbeef /* IEIPersistFile_fnSaveCompleted */,
583 (void *) 0xdeadbeef /* IEIPersistFile_fnGetCurFile */