ntoskrnl.exe: Send PnP and Power IRPs to plug and play devices.
[wine.git] / dlls / shell32 / folders.c
blobcf26caf8b0602bc36a3e3ee8c01ed0a24dfca938
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 IExtractIconW IExtractIconW_iface;
52 IExtractIconA IExtractIconA_iface;
53 IPersistFile IPersistFile_iface;
54 LONG ref;
55 LPITEMIDLIST pidl;
56 } IExtractIconWImpl;
58 static inline IExtractIconWImpl *impl_from_IExtractIconW(IExtractIconW *iface)
60 return CONTAINING_RECORD(iface, IExtractIconWImpl, IExtractIconW_iface);
63 static inline IExtractIconWImpl *impl_from_IExtractIconA(IExtractIconA *iface)
65 return CONTAINING_RECORD(iface, IExtractIconWImpl, IExtractIconA_iface);
68 static inline IExtractIconWImpl *impl_from_IPersistFile(IPersistFile *iface)
70 return CONTAINING_RECORD(iface, IExtractIconWImpl, IPersistFile_iface);
74 /**************************************************************************
75 * IExtractIconW::QueryInterface
77 static HRESULT WINAPI IExtractIconW_fnQueryInterface(IExtractIconW *iface, REFIID riid,
78 void **ppv)
80 IExtractIconWImpl *This = impl_from_IExtractIconW(iface);
82 TRACE("(%p)->(\n\tIID:\t%s,%p)\n", This, debugstr_guid(riid), ppv);
84 *ppv = NULL;
85 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IExtractIconW))
86 *ppv = iface;
87 else if (IsEqualIID(riid, &IID_IPersistFile))
88 *ppv = &This->IPersistFile_iface;
89 else if (IsEqualIID(riid, &IID_IExtractIconA))
90 *ppv = &This->IExtractIconA_iface;
92 if(*ppv)
94 IUnknown_AddRef((IUnknown*)*ppv);
95 TRACE("-- Interface: (%p)->(%p)\n", ppv, *ppv);
96 return S_OK;
98 TRACE("-- Interface: E_NOINTERFACE\n");
99 return E_NOINTERFACE;
102 /**************************************************************************
103 * IExtractIconW::AddRef
105 static ULONG WINAPI IExtractIconW_fnAddRef(IExtractIconW * iface)
107 IExtractIconWImpl *This = impl_from_IExtractIconW(iface);
108 ULONG refCount = InterlockedIncrement(&This->ref);
110 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
112 return refCount;
114 /**************************************************************************
115 * IExtractIconW::Release
117 static ULONG WINAPI IExtractIconW_fnRelease(IExtractIconW * iface)
119 IExtractIconWImpl *This = impl_from_IExtractIconW(iface);
120 ULONG refCount = InterlockedDecrement(&This->ref);
122 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
124 if (!refCount)
126 TRACE(" destroying IExtractIcon(%p)\n",This);
127 SHFree(This->pidl);
128 HeapFree(GetProcessHeap(),0,This);
129 return 0;
131 return refCount;
134 static HRESULT getIconLocationForFolder(IExtractIconWImpl *This, UINT uFlags, LPWSTR szIconFile,
135 UINT cchMax, int *piIndex, UINT *pwFlags)
137 int icon_idx;
138 WCHAR wszPath[MAX_PATH];
139 WCHAR wszCLSIDValue[CHARS_IN_GUID];
140 static const WCHAR shellClassInfo[] = { '.','S','h','e','l','l','C','l','a','s','s','I','n','f','o',0 };
141 static const WCHAR iconFile[] = { 'I','c','o','n','F','i','l','e',0 };
142 static const WCHAR clsid[] = { 'C','L','S','I','D',0 };
143 static const WCHAR clsid2[] = { 'C','L','S','I','D','2',0 };
144 static const WCHAR iconIndex[] = { 'I','c','o','n','I','n','d','e','x',0 };
146 if (SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, iconFile,
147 wszPath, MAX_PATH))
149 WCHAR wszIconIndex[10];
150 SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, iconIndex,
151 wszIconIndex, 10);
152 *piIndex = atoiW(wszIconIndex);
154 else if (SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, clsid,
155 wszCLSIDValue, CHARS_IN_GUID) &&
156 HCR_GetDefaultIconW(wszCLSIDValue, szIconFile, cchMax, &icon_idx))
158 *piIndex = icon_idx;
160 else if (SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, clsid2,
161 wszCLSIDValue, CHARS_IN_GUID) &&
162 HCR_GetDefaultIconW(wszCLSIDValue, szIconFile, cchMax, &icon_idx))
164 *piIndex = icon_idx;
166 else
168 static const WCHAR folder[] = { 'F','o','l','d','e','r',0 };
170 if (!HCR_GetDefaultIconW(folder, szIconFile, cchMax, &icon_idx))
172 lstrcpynW(szIconFile, swShell32Name, cchMax);
173 icon_idx = -IDI_SHELL_FOLDER;
176 if (uFlags & GIL_OPENICON)
177 *piIndex = icon_idx<0? icon_idx-1: icon_idx+1;
178 else
179 *piIndex = icon_idx;
182 return S_OK;
185 WCHAR swShell32Name[MAX_PATH];
187 /**************************************************************************
188 * IExtractIconW::GetIconLocation
190 * mapping filetype to icon
192 static HRESULT WINAPI IExtractIconW_fnGetIconLocation(IExtractIconW * iface, UINT uFlags,
193 LPWSTR szIconFile, UINT cchMax, int * piIndex, UINT * pwFlags)
195 IExtractIconWImpl *This = impl_from_IExtractIconW(iface);
196 char sTemp[MAX_PATH];
197 int icon_idx;
198 GUID const * riid;
199 LPITEMIDLIST pSimplePidl = ILFindLastID(This->pidl);
201 TRACE("(%p) (flags=%u, %s, %u, %p, %p)\n", This, uFlags, debugstr_w(szIconFile),
202 cchMax, piIndex, pwFlags);
204 if (pwFlags)
205 *pwFlags = 0;
207 if (_ILIsDesktop(pSimplePidl))
209 lstrcpynW(szIconFile, swShell32Name, cchMax);
210 *piIndex = -IDI_SHELL_DESKTOP;
213 /* my computer and other shell extensions */
214 else if ((riid = _ILGetGUIDPointer(pSimplePidl)))
216 static const WCHAR fmt[] = { 'C','L','S','I','D','\\',
217 '{','%','0','8','l','x','-','%','0','4','x','-','%','0','4','x','-',
218 '%','0','2','x','%','0','2','x','-','%','0','2','x', '%','0','2','x',
219 '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x','}',0 };
220 WCHAR xriid[50];
222 sprintfW(xriid, fmt,
223 riid->Data1, riid->Data2, riid->Data3,
224 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
225 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]);
227 if (HCR_GetDefaultIconW(xriid, szIconFile, cchMax, &icon_idx))
229 *piIndex = icon_idx;
231 else
233 lstrcpynW(szIconFile, swShell32Name, cchMax);
234 if(IsEqualGUID(riid, &CLSID_MyComputer))
235 *piIndex = -IDI_SHELL_MY_COMPUTER;
236 else if(IsEqualGUID(riid, &CLSID_MyDocuments))
237 *piIndex = -IDI_SHELL_MY_DOCUMENTS;
238 else if(IsEqualGUID(riid, &CLSID_NetworkPlaces))
239 *piIndex = -IDI_SHELL_MY_NETWORK_PLACES;
240 else if(IsEqualGUID(riid, &CLSID_UnixFolder) ||
241 IsEqualGUID(riid, &CLSID_UnixDosFolder))
242 *piIndex = -IDI_SHELL_DRIVE;
243 else
244 *piIndex = -IDI_SHELL_FOLDER;
248 else if (_ILIsDrive (pSimplePidl))
250 static const WCHAR drive[] = { 'D','r','i','v','e',0 };
252 int icon_idx = -1;
254 if (_ILGetDrive(pSimplePidl, sTemp, MAX_PATH))
256 switch(GetDriveTypeA(sTemp))
258 case DRIVE_REMOVABLE: icon_idx = IDI_SHELL_FLOPPY; break;
259 case DRIVE_CDROM: icon_idx = IDI_SHELL_CDROM; break;
260 case DRIVE_REMOTE: icon_idx = IDI_SHELL_NETDRIVE; break;
261 case DRIVE_RAMDISK: icon_idx = IDI_SHELL_RAMDISK; break;
265 if (icon_idx != -1)
267 lstrcpynW(szIconFile, swShell32Name, cchMax);
268 *piIndex = -icon_idx;
270 else
272 if (HCR_GetDefaultIconW(drive, szIconFile, cchMax, &icon_idx))
274 *piIndex = icon_idx;
276 else
278 lstrcpynW(szIconFile, swShell32Name, cchMax);
279 *piIndex = -IDI_SHELL_DRIVE;
283 else if (_ILIsFolder (pSimplePidl))
285 getIconLocationForFolder(This, uFlags, szIconFile, cchMax, piIndex, pwFlags);
287 else
289 BOOL found = FALSE;
291 if (_ILIsCPanelStruct(pSimplePidl))
293 if (SUCCEEDED(CPanel_GetIconLocationW(pSimplePidl, szIconFile, cchMax, piIndex)))
294 found = TRUE;
296 else if (_ILGetExtension(pSimplePidl, sTemp, MAX_PATH))
298 if (HCR_MapTypeToValueA(sTemp, sTemp, MAX_PATH, TRUE)
299 && HCR_GetDefaultIconA(sTemp, sTemp, MAX_PATH, &icon_idx))
301 if (!lstrcmpA("%1", sTemp)) /* icon is in the file */
303 SHGetPathFromIDListW(This->pidl, szIconFile);
304 *piIndex = 0;
306 else
308 MultiByteToWideChar(CP_ACP, 0, sTemp, -1, szIconFile, cchMax);
309 *piIndex = icon_idx;
312 found = TRUE;
314 else if (!lstrcmpiA(sTemp, "lnkfile"))
316 /* extract icon from shell shortcut */
317 IShellFolder* dsf;
318 IShellLinkW* psl;
320 if (SUCCEEDED(SHGetDesktopFolder(&dsf)))
322 HRESULT hr = IShellFolder_GetUIObjectOf(dsf, NULL, 1, (LPCITEMIDLIST*)&This->pidl, &IID_IShellLinkW, NULL, (LPVOID*)&psl);
324 if (SUCCEEDED(hr))
326 hr = IShellLinkW_GetIconLocation(psl, szIconFile, MAX_PATH, piIndex);
328 if (SUCCEEDED(hr) && *szIconFile)
329 found = TRUE;
331 IShellLinkW_Release(psl);
334 IShellFolder_Release(dsf);
339 if (!found) /* default icon */
341 lstrcpynW(szIconFile, swShell32Name, cchMax);
342 *piIndex = 0;
346 TRACE("-- %s %x\n", debugstr_w(szIconFile), *piIndex);
347 return S_OK;
350 /**************************************************************************
351 * IExtractIconW::Extract
353 static HRESULT WINAPI IExtractIconW_fnExtract(IExtractIconW * iface, LPCWSTR pszFile,
354 UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
356 IExtractIconWImpl *This = impl_from_IExtractIconW(iface);
357 int index;
358 HIMAGELIST big_icons, small_icons;
360 FIXME("(%p) (file=%s index=%d %p %p size=%08x) semi-stub\n", This, debugstr_w(pszFile),
361 (signed)nIconIndex, phiconLarge, phiconSmall, nIconSize);
363 index = SIC_GetIconIndex(pszFile, nIconIndex, 0);
364 Shell_GetImageLists( &big_icons, &small_icons );
365 if (phiconLarge)
366 *phiconLarge = ImageList_GetIcon(big_icons, index, ILD_TRANSPARENT);
368 if (phiconSmall)
369 *phiconSmall = ImageList_GetIcon(small_icons, index, ILD_TRANSPARENT);
371 return S_OK;
374 static const IExtractIconWVtbl eivt =
376 IExtractIconW_fnQueryInterface,
377 IExtractIconW_fnAddRef,
378 IExtractIconW_fnRelease,
379 IExtractIconW_fnGetIconLocation,
380 IExtractIconW_fnExtract
383 /**************************************************************************
384 * IExtractIconA::QueryInterface
386 static HRESULT WINAPI IExtractIconA_fnQueryInterface(IExtractIconA * iface, REFIID riid,
387 void **ppv)
389 IExtractIconWImpl *This = impl_from_IExtractIconA(iface);
391 return IExtractIconW_QueryInterface(&This->IExtractIconW_iface, riid, ppv);
394 /**************************************************************************
395 * IExtractIconA::AddRef
397 static ULONG WINAPI IExtractIconA_fnAddRef(IExtractIconA * iface)
399 IExtractIconWImpl *This = impl_from_IExtractIconA(iface);
401 return IExtractIconW_AddRef(&This->IExtractIconW_iface);
403 /**************************************************************************
404 * IExtractIconA::Release
406 static ULONG WINAPI IExtractIconA_fnRelease(IExtractIconA * iface)
408 IExtractIconWImpl *This = impl_from_IExtractIconA(iface);
410 return IExtractIconW_Release(&This->IExtractIconW_iface);
412 /**************************************************************************
413 * IExtractIconA::GetIconLocation
415 * mapping filetype to icon
417 static HRESULT WINAPI IExtractIconA_fnGetIconLocation(IExtractIconA * iface, UINT uFlags,
418 LPSTR szIconFile, UINT cchMax, int * piIndex, UINT * pwFlags)
420 IExtractIconWImpl *This = impl_from_IExtractIconA(iface);
421 HRESULT ret;
422 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, cchMax * sizeof(WCHAR));
424 TRACE("(%p) (flags=%u %p %u %p %p)\n", This, uFlags, szIconFile, cchMax, piIndex, pwFlags);
426 ret = IExtractIconW_GetIconLocation(&This->IExtractIconW_iface, uFlags, lpwstrFile, cchMax,
427 piIndex, pwFlags);
428 WideCharToMultiByte(CP_ACP, 0, lpwstrFile, -1, szIconFile, cchMax, NULL, NULL);
429 HeapFree(GetProcessHeap(), 0, lpwstrFile);
431 TRACE("-- %s %x\n", szIconFile, *piIndex);
432 return ret;
434 /**************************************************************************
435 * IExtractIconA::Extract
437 static HRESULT WINAPI IExtractIconA_fnExtract(IExtractIconA * iface, LPCSTR pszFile,
438 UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
440 IExtractIconWImpl *This = impl_from_IExtractIconA(iface);
441 HRESULT ret;
442 INT len = MultiByteToWideChar(CP_ACP, 0, pszFile, -1, NULL, 0);
443 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
445 TRACE("(%p) (file=%p index=%u %p %p size=%u)\n", This, pszFile, nIconIndex, phiconLarge, phiconSmall, nIconSize);
447 MultiByteToWideChar(CP_ACP, 0, pszFile, -1, lpwstrFile, len);
448 ret = IExtractIconW_Extract(&This->IExtractIconW_iface, lpwstrFile, nIconIndex, phiconLarge,
449 phiconSmall, nIconSize);
450 HeapFree(GetProcessHeap(), 0, lpwstrFile);
451 return ret;
454 static const IExtractIconAVtbl eiavt =
456 IExtractIconA_fnQueryInterface,
457 IExtractIconA_fnAddRef,
458 IExtractIconA_fnRelease,
459 IExtractIconA_fnGetIconLocation,
460 IExtractIconA_fnExtract
463 /************************************************************************
464 * IEIPersistFile::QueryInterface
466 static HRESULT WINAPI IEIPersistFile_fnQueryInterface(IPersistFile *iface, REFIID iid,
467 void **ppv)
469 IExtractIconWImpl *This = impl_from_IPersistFile(iface);
471 return IExtractIconW_QueryInterface(&This->IExtractIconW_iface, iid, ppv);
474 /************************************************************************
475 * IEIPersistFile::AddRef
477 static ULONG WINAPI IEIPersistFile_fnAddRef(IPersistFile *iface)
479 IExtractIconWImpl *This = impl_from_IPersistFile(iface);
481 return IExtractIconW_AddRef(&This->IExtractIconW_iface);
484 /************************************************************************
485 * IEIPersistFile::Release
487 static ULONG WINAPI IEIPersistFile_fnRelease(IPersistFile *iface)
489 IExtractIconWImpl *This = impl_from_IPersistFile(iface);
491 return IExtractIconW_Release(&This->IExtractIconW_iface);
494 /************************************************************************
495 * IEIPersistFile::GetClassID
497 static HRESULT WINAPI IEIPersistFile_fnGetClassID(IPersistFile *iface, LPCLSID lpClassId)
499 CLSID StdFolderID = { 0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} };
501 if (lpClassId==NULL)
502 return E_POINTER;
504 *lpClassId = StdFolderID;
506 return S_OK;
509 /************************************************************************
510 * IEIPersistFile_Load
512 static HRESULT WINAPI IEIPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName,
513 DWORD dwMode)
515 IExtractIconWImpl *This = impl_from_IPersistFile(iface);
517 FIXME("%p\n", This);
518 return E_NOTIMPL;
521 static const IPersistFileVtbl pfvt =
523 IEIPersistFile_fnQueryInterface,
524 IEIPersistFile_fnAddRef,
525 IEIPersistFile_fnRelease,
526 IEIPersistFile_fnGetClassID,
527 (void *) 0xdeadbeef /* IEIPersistFile_fnIsDirty */,
528 IEIPersistFile_fnLoad,
529 (void *) 0xdeadbeef /* IEIPersistFile_fnSave */,
530 (void *) 0xdeadbeef /* IEIPersistFile_fnSaveCompleted */,
531 (void *) 0xdeadbeef /* IEIPersistFile_fnGetCurFile */
534 static IExtractIconWImpl *extracticon_create(LPCITEMIDLIST pidl)
536 IExtractIconWImpl *ei;
538 TRACE("%p\n", pidl);
540 ei = HeapAlloc(GetProcessHeap(), 0, sizeof(*ei));
541 ei->ref=1;
542 ei->IExtractIconW_iface.lpVtbl = &eivt;
543 ei->IExtractIconA_iface.lpVtbl = &eiavt;
544 ei->IPersistFile_iface.lpVtbl = &pfvt;
545 ei->pidl=ILClone(pidl);
547 pdump(pidl);
549 TRACE("(%p)\n", ei);
550 return ei;
553 IExtractIconW *IExtractIconW_Constructor(LPCITEMIDLIST pidl)
555 IExtractIconWImpl *ei = extracticon_create(pidl);
557 return &ei->IExtractIconW_iface;
560 IExtractIconA *IExtractIconA_Constructor(LPCITEMIDLIST pidl)
562 IExtractIconWImpl *ei = extracticon_create(pidl);
564 return &ei->IExtractIconA_iface;