Release 0.9.14.
[wine/multimedia.git] / dlls / shell32 / folders.c
blob6ce7ab7ffae8721940eb38dad131873020ce5c84
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"
36 #include "winreg.h"
38 #include "wine/debug.h"
40 #include "pidl.h"
41 #include "shell32_main.h"
42 #include "shfldr.h"
43 #include "shresdef.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(shell);
47 /***********************************************************************
48 * IExtractIconW implementation
50 typedef struct
52 const IExtractIconWVtbl *lpVtbl;
53 LONG ref;
54 const IPersistFileVtbl *lpvtblPersistFile;
55 const IExtractIconAVtbl *lpvtblExtractIconA;
56 LPITEMIDLIST pidl;
57 } IExtractIconWImpl;
59 static const IExtractIconAVtbl eiavt;
60 static const IExtractIconWVtbl eivt;
61 static const IPersistFileVtbl pfvt;
63 static inline IExtractIconW *impl_from_IPersistFile( IPersistFile *iface )
65 return (IExtractIconW *)((char*)iface - FIELD_OFFSET(IExtractIconWImpl, lpvtblPersistFile));
68 static inline IExtractIconW *impl_from_IExtractIconA( IExtractIconA *iface )
70 return (IExtractIconW *)((char*)iface - FIELD_OFFSET(IExtractIconWImpl, lpvtblExtractIconA));
74 /**************************************************************************
75 * IExtractIconW_Constructor
77 IExtractIconW* IExtractIconW_Constructor(LPCITEMIDLIST pidl)
79 IExtractIconWImpl* ei;
81 TRACE("%p\n", pidl);
83 ei = HeapAlloc(GetProcessHeap(),0,sizeof(IExtractIconWImpl));
84 ei->ref=1;
85 ei->lpVtbl = &eivt;
86 ei->lpvtblPersistFile = &pfvt;
87 ei->lpvtblExtractIconA = &eiavt;
88 ei->pidl=ILClone(pidl);
90 pdump(pidl);
92 TRACE("(%p)\n", ei);
93 return (IExtractIconW *)ei;
95 /**************************************************************************
96 * IExtractIconW_QueryInterface
98 static HRESULT WINAPI IExtractIconW_fnQueryInterface(IExtractIconW *iface, REFIID riid, LPVOID *ppvObj)
100 IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
102 TRACE("(%p)->(\n\tIID:\t%s,%p)\n", This, debugstr_guid(riid), ppvObj);
104 *ppvObj = NULL;
106 if (IsEqualIID(riid, &IID_IUnknown)) /*IUnknown*/
108 *ppvObj = This;
110 else if (IsEqualIID(riid, &IID_IPersistFile)) /*IExtractIcon*/
112 *ppvObj = (IPersistFile*)&(This->lpvtblPersistFile);
114 else if (IsEqualIID(riid, &IID_IExtractIconA)) /*IExtractIcon*/
116 *ppvObj = (IExtractIconA*)&(This->lpvtblExtractIconA);
118 else if (IsEqualIID(riid, &IID_IExtractIconW)) /*IExtractIcon*/
120 *ppvObj = (IExtractIconW*)This;
123 if(*ppvObj)
125 IExtractIconW_AddRef((IExtractIconW*) *ppvObj);
126 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
127 return S_OK;
129 TRACE("-- Interface: E_NOINTERFACE\n");
130 return E_NOINTERFACE;
133 /**************************************************************************
134 * IExtractIconW_AddRef
136 static ULONG WINAPI IExtractIconW_fnAddRef(IExtractIconW * iface)
138 IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
139 ULONG refCount = InterlockedIncrement(&This->ref);
141 TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
143 return refCount;
145 /**************************************************************************
146 * IExtractIconW_Release
148 static ULONG WINAPI IExtractIconW_fnRelease(IExtractIconW * iface)
150 IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
151 ULONG refCount = InterlockedDecrement(&This->ref);
153 TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
155 if (!refCount)
157 TRACE(" destroying IExtractIcon(%p)\n",This);
158 SHFree(This->pidl);
159 HeapFree(GetProcessHeap(),0,This);
160 return 0;
162 return refCount;
165 static HRESULT getIconLocationForFolder(IExtractIconW *iface, UINT uFlags,
166 LPWSTR szIconFile, UINT cchMax, int *piIndex, UINT *pwFlags)
168 IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
169 int icon_idx;
170 WCHAR wszPath[MAX_PATH];
171 WCHAR wszCLSIDValue[CHARS_IN_GUID];
172 static const WCHAR shellClassInfo[] = { '.','S','h','e','l','l','C','l','a','s','s','I','n','f','o',0 };
173 static const WCHAR iconFile[] = { 'I','c','o','n','F','i','l','e',0 };
174 static const WCHAR clsid[] = { 'C','L','S','I','D',0 };
175 static const WCHAR clsid2[] = { 'C','L','S','I','D','2',0 };
176 static const WCHAR iconIndex[] = { 'I','c','o','n','I','n','d','e','x',0 };
178 if (SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, iconFile,
179 wszPath, MAX_PATH))
181 WCHAR wszIconIndex[10];
182 SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, iconIndex,
183 wszIconIndex, 10);
184 *piIndex = atoiW(wszIconIndex);
186 else if (SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, clsid,
187 wszCLSIDValue, CHARS_IN_GUID) &&
188 HCR_GetDefaultIconW(wszCLSIDValue, szIconFile, cchMax, &icon_idx))
190 *piIndex = icon_idx;
192 else if (SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, clsid2,
193 wszCLSIDValue, CHARS_IN_GUID) &&
194 HCR_GetDefaultIconW(wszCLSIDValue, szIconFile, cchMax, &icon_idx))
196 *piIndex = icon_idx;
198 else
200 static const WCHAR folder[] = { 'F','o','l','d','e','r',0 };
202 if (!HCR_GetDefaultIconW(folder, szIconFile, cchMax, &icon_idx))
204 lstrcpynW(szIconFile, swShell32Name, cchMax);
205 icon_idx = -IDI_SHELL_FOLDER;
208 if (uFlags & GIL_OPENICON)
209 *piIndex = icon_idx<0? icon_idx-1: icon_idx+1;
210 else
211 *piIndex = icon_idx;
214 return S_OK;
217 WCHAR swShell32Name[MAX_PATH];
219 /**************************************************************************
220 * IExtractIconW_GetIconLocation
222 * mapping filetype to icon
224 static HRESULT WINAPI IExtractIconW_fnGetIconLocation(
225 IExtractIconW * iface,
226 UINT uFlags, /* GIL_ flags */
227 LPWSTR szIconFile,
228 UINT cchMax,
229 int * piIndex,
230 UINT * pwFlags) /* returned GIL_ flags */
232 IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
234 char sTemp[MAX_PATH];
235 int icon_idx;
236 GUID const * riid;
237 LPITEMIDLIST pSimplePidl = ILFindLastID(This->pidl);
239 TRACE("(%p) (flags=%u %p %u %p %p)\n", This, uFlags, szIconFile, cchMax, piIndex, pwFlags);
241 if (pwFlags)
242 *pwFlags = 0;
244 if (_ILIsDesktop(pSimplePidl))
246 lstrcpynW(szIconFile, swShell32Name, cchMax);
247 *piIndex = -IDI_SHELL_DESKTOP;
250 /* my computer and other shell extensions */
251 else if ((riid = _ILGetGUIDPointer(pSimplePidl)))
253 static const WCHAR fmt[] = { 'C','L','S','I','D','\\',
254 '{','%','0','8','l','x','-','%','0','4','x','-','%','0','4','x','-',
255 '%','0','2','x','%','0','2','x','-','%','0','2','x', '%','0','2','x',
256 '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x','}',0 };
257 WCHAR xriid[50];
259 sprintfW(xriid, fmt,
260 riid->Data1, riid->Data2, riid->Data3,
261 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
262 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]);
264 if (HCR_GetDefaultIconW(xriid, szIconFile, cchMax, &icon_idx))
266 *piIndex = icon_idx;
268 else
270 lstrcpynW(szIconFile, swShell32Name, cchMax);
271 if(IsEqualGUID(riid, &CLSID_MyComputer))
272 *piIndex = -IDI_SHELL_MY_COMPUTER;
273 else if(IsEqualGUID(riid, &CLSID_MyDocuments))
274 *piIndex = -IDI_SHELL_MY_DOCUMENTS;
275 else if(IsEqualGUID(riid, &CLSID_NetworkPlaces))
276 *piIndex = -IDI_SHELL_MY_NETWORK_PLACES;
277 else if(IsEqualGUID(riid, &CLSID_UnixFolder) ||
278 IsEqualGUID(riid, &CLSID_UnixDosFolder))
279 *piIndex = -IDI_SHELL_DRIVE;
280 else
281 *piIndex = -IDI_SHELL_FOLDER;
285 else if (_ILIsDrive (pSimplePidl))
287 static const WCHAR drive[] = { 'D','r','i','v','e',0 };
289 int icon_idx = -1;
291 if (_ILGetDrive(pSimplePidl, sTemp, MAX_PATH))
293 switch(GetDriveTypeA(sTemp))
295 case DRIVE_REMOVABLE: icon_idx = IDI_SHELL_FLOPPY; break;
296 case DRIVE_CDROM: icon_idx = IDI_SHELL_CDROM; break;
297 case DRIVE_REMOTE: icon_idx = IDI_SHELL_NETDRIVE; break;
298 case DRIVE_RAMDISK: icon_idx = IDI_SHELL_RAMDISK; break;
302 if (icon_idx != -1)
304 lstrcpynW(szIconFile, swShell32Name, cchMax);
305 *piIndex = -icon_idx;
307 else
309 if (HCR_GetDefaultIconW(drive, szIconFile, cchMax, &icon_idx))
311 *piIndex = icon_idx;
313 else
315 lstrcpynW(szIconFile, swShell32Name, cchMax);
316 *piIndex = -IDI_SHELL_DRIVE;
320 else if (_ILIsFolder (pSimplePidl))
322 getIconLocationForFolder(iface, uFlags, szIconFile, cchMax, piIndex,
323 pwFlags);
325 else
327 BOOL found = FALSE;
329 if (_ILIsCPanelStruct(pSimplePidl))
331 if (SUCCEEDED(CPanel_GetIconLocationW(pSimplePidl, szIconFile, cchMax, piIndex)))
332 found = TRUE;
334 else if (_ILGetExtension(pSimplePidl, sTemp, MAX_PATH))
336 if (HCR_MapTypeToValueA(sTemp, sTemp, MAX_PATH, TRUE)
337 && HCR_GetDefaultIconA(sTemp, sTemp, MAX_PATH, &icon_idx))
339 if (!lstrcmpA("%1", sTemp)) /* icon is in the file */
341 SHGetPathFromIDListW(This->pidl, szIconFile);
342 *piIndex = 0;
344 else
346 MultiByteToWideChar(CP_ACP, 0, sTemp, -1, szIconFile, cchMax);
347 *piIndex = icon_idx;
350 found = TRUE;
352 else if (!lstrcmpiA(sTemp, "lnkfile"))
354 /* extract icon from shell shortcut */
355 IShellFolder* dsf;
356 IShellLinkW* psl;
358 if (SUCCEEDED(SHGetDesktopFolder(&dsf)))
360 HRESULT hr = IShellFolder_GetUIObjectOf(dsf, NULL, 1, (LPCITEMIDLIST*)&This->pidl, &IID_IShellLinkW, NULL, (LPVOID*)&psl);
362 if (SUCCEEDED(hr))
364 hr = IShellLinkW_GetIconLocation(psl, szIconFile, MAX_PATH, piIndex);
366 if (SUCCEEDED(hr) && *szIconFile)
367 found = TRUE;
369 IShellLinkW_Release(psl);
372 IShellFolder_Release(dsf);
377 if (!found) /* default icon */
379 lstrcpynW(szIconFile, swShell32Name, cchMax);
380 *piIndex = 0;
384 TRACE("-- %s %x\n", debugstr_w(szIconFile), *piIndex);
385 return NOERROR;
388 /**************************************************************************
389 * IExtractIconW_Extract
391 static HRESULT WINAPI IExtractIconW_fnExtract(IExtractIconW * iface, LPCWSTR pszFile, UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
393 IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
394 int index;
396 FIXME("(%p) (file=%p index=%d %p %p size=%08x) semi-stub\n", This, debugstr_w(pszFile), (signed)nIconIndex,
397 phiconLarge, phiconSmall, nIconSize);
399 index = SIC_GetIconIndex(pszFile, nIconIndex, 0);
401 if (phiconLarge)
402 *phiconLarge = ImageList_GetIcon(ShellBigIconList, index, ILD_TRANSPARENT);
404 if (phiconSmall)
405 *phiconSmall = ImageList_GetIcon(ShellSmallIconList, index, ILD_TRANSPARENT);
407 return S_OK;
410 static const IExtractIconWVtbl eivt =
412 IExtractIconW_fnQueryInterface,
413 IExtractIconW_fnAddRef,
414 IExtractIconW_fnRelease,
415 IExtractIconW_fnGetIconLocation,
416 IExtractIconW_fnExtract
419 /**************************************************************************
420 * IExtractIconA_Constructor
422 IExtractIconA* IExtractIconA_Constructor(LPCITEMIDLIST pidl)
424 IExtractIconWImpl *This = (IExtractIconWImpl *)IExtractIconW_Constructor(pidl);
425 IExtractIconA *eia = (IExtractIconA *)&This->lpvtblExtractIconA;
427 TRACE("(%p)->(%p)\n", This, eia);
428 return eia;
430 /**************************************************************************
431 * IExtractIconA_QueryInterface
433 static HRESULT WINAPI IExtractIconA_fnQueryInterface(IExtractIconA * iface, REFIID riid, LPVOID *ppvObj)
435 IExtractIconW *This = impl_from_IExtractIconA(iface);
437 return IExtractIconW_QueryInterface(This, riid, ppvObj);
440 /**************************************************************************
441 * IExtractIconA_AddRef
443 static ULONG WINAPI IExtractIconA_fnAddRef(IExtractIconA * iface)
445 IExtractIconW *This = impl_from_IExtractIconA(iface);
447 return IExtractIconW_AddRef(This);
449 /**************************************************************************
450 * IExtractIconA_Release
452 static ULONG WINAPI IExtractIconA_fnRelease(IExtractIconA * iface)
454 IExtractIconW *This = impl_from_IExtractIconA(iface);
456 return IExtractIconW_AddRef(This);
458 /**************************************************************************
459 * IExtractIconA_GetIconLocation
461 * mapping filetype to icon
463 static HRESULT WINAPI IExtractIconA_fnGetIconLocation(
464 IExtractIconA * iface,
465 UINT uFlags,
466 LPSTR szIconFile,
467 UINT cchMax,
468 int * piIndex,
469 UINT * pwFlags)
471 HRESULT ret;
472 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, cchMax * sizeof(WCHAR));
473 IExtractIconW *This = impl_from_IExtractIconA(iface);
475 TRACE("(%p) (flags=%u %p %u %p %p)\n", This, uFlags, szIconFile, cchMax, piIndex, pwFlags);
477 ret = IExtractIconW_GetIconLocation(This, uFlags, lpwstrFile, cchMax, piIndex, pwFlags);
478 WideCharToMultiByte(CP_ACP, 0, lpwstrFile, -1, szIconFile, cchMax, NULL, NULL);
479 HeapFree(GetProcessHeap(), 0, lpwstrFile);
481 TRACE("-- %s %x\n", szIconFile, *piIndex);
482 return ret;
484 /**************************************************************************
485 * IExtractIconA_Extract
487 static HRESULT WINAPI IExtractIconA_fnExtract(IExtractIconA * iface, LPCSTR pszFile, UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
489 HRESULT ret;
490 INT len = MultiByteToWideChar(CP_ACP, 0, pszFile, -1, NULL, 0);
491 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
492 IExtractIconW *This = impl_from_IExtractIconA(iface);
494 TRACE("(%p) (file=%p index=%u %p %p size=%u)\n", This, pszFile, nIconIndex, phiconLarge, phiconSmall, nIconSize);
496 MultiByteToWideChar(CP_ACP, 0, pszFile, -1, lpwstrFile, len);
497 ret = IExtractIconW_Extract(This, lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIconSize);
498 HeapFree(GetProcessHeap(), 0, lpwstrFile);
499 return ret;
502 static const IExtractIconAVtbl eiavt =
504 IExtractIconA_fnQueryInterface,
505 IExtractIconA_fnAddRef,
506 IExtractIconA_fnRelease,
507 IExtractIconA_fnGetIconLocation,
508 IExtractIconA_fnExtract
511 /************************************************************************
512 * IEIPersistFile_QueryInterface (IUnknown)
514 static HRESULT WINAPI IEIPersistFile_fnQueryInterface(
515 IPersistFile *iface,
516 REFIID iid,
517 LPVOID *ppvObj)
519 IExtractIconW *This = impl_from_IPersistFile(iface);
521 return IExtractIconW_QueryInterface(This, iid, ppvObj);
524 /************************************************************************
525 * IEIPersistFile_AddRef (IUnknown)
527 static ULONG WINAPI IEIPersistFile_fnAddRef(
528 IPersistFile *iface)
530 IExtractIconW *This = impl_from_IPersistFile(iface);
532 return IExtractIconW_AddRef(This);
535 /************************************************************************
536 * IEIPersistFile_Release (IUnknown)
538 static ULONG WINAPI IEIPersistFile_fnRelease(
539 IPersistFile *iface)
541 IExtractIconW *This = impl_from_IPersistFile(iface);
543 return IExtractIconW_Release(This);
546 /************************************************************************
547 * IEIPersistFile_GetClassID (IPersist)
549 static HRESULT WINAPI IEIPersistFile_fnGetClassID(
550 IPersistFile *iface,
551 LPCLSID lpClassId)
553 CLSID StdFolderID = { 0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} };
555 if (lpClassId==NULL)
556 return E_POINTER;
558 memcpy(lpClassId, &StdFolderID, sizeof(StdFolderID));
560 return S_OK;
563 /************************************************************************
564 * IEIPersistFile_Load (IPersistFile)
566 static HRESULT WINAPI IEIPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
568 IExtractIconW *This = impl_from_IPersistFile(iface);
569 FIXME("%p\n", This);
570 return E_NOTIMPL;
574 static const IPersistFileVtbl pfvt =
576 IEIPersistFile_fnQueryInterface,
577 IEIPersistFile_fnAddRef,
578 IEIPersistFile_fnRelease,
579 IEIPersistFile_fnGetClassID,
580 (void *) 0xdeadbeef /* IEIPersistFile_fnIsDirty */,
581 IEIPersistFile_fnLoad,
582 (void *) 0xdeadbeef /* IEIPersistFile_fnSave */,
583 (void *) 0xdeadbeef /* IEIPersistFile_fnSaveCompleted */,
584 (void *) 0xdeadbeef /* IEIPersistFile_fnGetCurFile */