Sync to Wine-20050830:
[reactos.git] / reactos / lib / shell32 / folders.c
1 /*
2 * Copyright 1997 Marcus Meissner
3 * Copyright 1998 Juergen Schmied
4 *
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.
9 *
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.
14 *
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #include "config.h"
21 #include "wine/port.h"
22
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #define COBJMACROS
29
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"
37
38 #include "wine/debug.h"
39
40 #include "pidl.h"
41 #include "shell32_main.h"
42 #include "shfldr.h"
43 #include "shresdef.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(shell);
46
47 /***********************************************************************
48 * IExtractIconW implementation
49 */
50 typedef struct
51 {
52 const IExtractIconWVtbl *lpVtbl;
53 LONG ref;
54 const IPersistFileVtbl *lpvtblPersistFile;
55 const IExtractIconAVtbl *lpvtblExtractIconA;
56 LPITEMIDLIST pidl;
57 } IExtractIconWImpl;
58
59 static const IExtractIconAVtbl eiavt;
60 static const IExtractIconWVtbl eivt;
61 static const IPersistFileVtbl pfvt;
62
63 static inline IExtractIconW *impl_from_IPersistFile( IPersistFile *iface )
64 {
65 return (IExtractIconW *)((char*)iface - FIELD_OFFSET(IExtractIconWImpl, lpvtblPersistFile));
66 }
67
68 static inline IExtractIconW *impl_from_IExtractIconA( IExtractIconA *iface )
69 {
70 return (IExtractIconW *)((char*)iface - FIELD_OFFSET(IExtractIconWImpl, lpvtblExtractIconA));
71 }
72
73
74 /**************************************************************************
75 * IExtractIconW_Constructor
76 */
77 IExtractIconW* IExtractIconW_Constructor(LPCITEMIDLIST pidl)
78 {
79 IExtractIconWImpl* ei;
80
81 TRACE("%p\n", pidl);
82
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);
89
90 pdump(pidl);
91
92 TRACE("(%p)\n", ei);
93 return (IExtractIconW *)ei;
94 }
95 /**************************************************************************
96 * IExtractIconW_QueryInterface
97 */
98 static HRESULT WINAPI IExtractIconW_fnQueryInterface(IExtractIconW *iface, REFIID riid, LPVOID *ppvObj)
99 {
100 IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
101
102 TRACE("(%p)->(\n\tIID:\t%s,%p)\n", This, debugstr_guid(riid), ppvObj);
103
104 *ppvObj = NULL;
105
106 if (IsEqualIID(riid, &IID_IUnknown)) /*IUnknown*/
107 {
108 *ppvObj = This;
109 }
110 else if (IsEqualIID(riid, &IID_IPersistFile)) /*IExtractIcon*/
111 {
112 *ppvObj = (IPersistFile*)&(This->lpvtblPersistFile);
113 }
114 else if (IsEqualIID(riid, &IID_IExtractIconA)) /*IExtractIcon*/
115 {
116 *ppvObj = (IExtractIconA*)&(This->lpvtblExtractIconA);
117 }
118 else if (IsEqualIID(riid, &IID_IExtractIconW)) /*IExtractIcon*/
119 {
120 *ppvObj = (IExtractIconW*)This;
121 }
122
123 if(*ppvObj)
124 {
125 IExtractIconW_AddRef((IExtractIconW*) *ppvObj);
126 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
127 return S_OK;
128 }
129 TRACE("-- Interface: E_NOINTERFACE\n");
130 return E_NOINTERFACE;
131 }
132
133 /**************************************************************************
134 * IExtractIconW_AddRef
135 */
136 static ULONG WINAPI IExtractIconW_fnAddRef(IExtractIconW * iface)
137 {
138 IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
139 ULONG refCount = InterlockedIncrement(&This->ref);
140
141 TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
142
143 return refCount;
144 }
145 /**************************************************************************
146 * IExtractIconW_Release
147 */
148 static ULONG WINAPI IExtractIconW_fnRelease(IExtractIconW * iface)
149 {
150 IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
151 ULONG refCount = InterlockedDecrement(&This->ref);
152
153 TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
154
155 if (!refCount)
156 {
157 TRACE(" destroying IExtractIcon(%p)\n",This);
158 SHFree(This->pidl);
159 HeapFree(GetProcessHeap(),0,This);
160 return 0;
161 }
162 return refCount;
163 }
164
165 static HRESULT getIconLocationForFolder(IExtractIconW *iface, UINT uFlags,
166 LPWSTR szIconFile, UINT cchMax, int *piIndex, UINT *pwFlags)
167 {
168 IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
169 DWORD dwNr;
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 };
177
178 if (SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, iconFile,
179 wszPath, MAX_PATH))
180 {
181 WCHAR wszIconIndex[10];
182 SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, iconIndex,
183 wszIconIndex, 10);
184 *piIndex = atoiW(wszIconIndex);
185 }
186 else if (SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, clsid,
187 wszCLSIDValue, CHARS_IN_GUID) &&
188 HCR_GetDefaultIconW(wszCLSIDValue, szIconFile, cchMax, &dwNr))
189 {
190 *piIndex = dwNr;
191 }
192 else if (SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, clsid2,
193 wszCLSIDValue, CHARS_IN_GUID) &&
194 HCR_GetDefaultIconW(wszCLSIDValue, szIconFile, cchMax, &dwNr))
195 {
196 *piIndex = dwNr;
197 }
198 else
199 {
200 static const WCHAR folder[] = { 'F','o','l','d','e','r',0 };
201
202 if (!HCR_GetDefaultIconW(folder, szIconFile, cchMax, &dwNr))
203 {
204 lstrcpynW(szIconFile, swShell32Name, cchMax);
205 dwNr = IDI_SHELL_FOLDER;
206 }
207 *piIndex = -((uFlags & GIL_OPENICON) ? dwNr + 1 : dwNr);
208 }
209 return S_OK;
210 }
211
212 WCHAR swShell32Name[MAX_PATH];
213
214 /**************************************************************************
215 * IExtractIconW_GetIconLocation
216 *
217 * mapping filetype to icon
218 */
219 static HRESULT WINAPI IExtractIconW_fnGetIconLocation(
220 IExtractIconW * iface,
221 UINT uFlags, /* GIL_ flags */
222 LPWSTR szIconFile,
223 UINT cchMax,
224 int * piIndex,
225 UINT * pwFlags) /* returned GIL_ flags */
226 {
227 IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
228
229 char sTemp[MAX_PATH];
230 DWORD dwNr;
231 GUID const * riid;
232 LPITEMIDLIST pSimplePidl = ILFindLastID(This->pidl);
233
234 TRACE("(%p) (flags=%u %p %u %p %p)\n", This, uFlags, szIconFile, cchMax, piIndex, pwFlags);
235
236 if (pwFlags)
237 *pwFlags = 0;
238
239 if (_ILIsDesktop(pSimplePidl))
240 {
241 lstrcpynW(szIconFile, swShell32Name, cchMax);
242 *piIndex = -IDI_SHELL_DESKTOP;
243 }
244
245 /* my computer and other shell extensions */
246 else if ((riid = _ILGetGUIDPointer(pSimplePidl)))
247 {
248 static const WCHAR fmt[] = { 'C','L','S','I','D','\\',
249 '{','%','0','8','l','x','-','%','0','4','x','-','%','0','4','x','-',
250 '%','0','2','x','%','0','2','x','-','%','0','2','x', '%','0','2','x',
251 '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x','}',0 };
252 WCHAR xriid[50];
253
254 sprintfW(xriid, fmt,
255 riid->Data1, riid->Data2, riid->Data3,
256 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
257 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]);
258
259 if (HCR_GetDefaultIconW(xriid, szIconFile, cchMax, &dwNr))
260 {
261 *piIndex = dwNr;
262 }
263 else
264 {
265 lstrcpynW(szIconFile, swShell32Name, cchMax);
266 if(IsEqualGUID(riid, &CLSID_MyComputer))
267 *piIndex = -IDI_SHELL_MY_COMPUTER;
268 else if(IsEqualGUID(riid, &CLSID_MyDocuments))
269 *piIndex = -IDI_SHELL_MY_DOCUMENTS;
270 else if(IsEqualGUID(riid, &CLSID_NetworkPlaces))
271 *piIndex = -IDI_SHELL_MY_NETWORK_PLACES;
272 else
273 *piIndex = -IDI_SHELL_FOLDER;
274 }
275 }
276
277 else if (_ILIsDrive (pSimplePidl))
278 {
279 static const WCHAR drive[] = { 'D','r','i','v','e',0 };
280
281 int icon_idx = -1;
282
283 if (_ILGetDrive(pSimplePidl, sTemp, MAX_PATH))
284 {
285 switch(GetDriveTypeA(sTemp))
286 {
287 case DRIVE_REMOVABLE: icon_idx = IDI_SHELL_FLOPPY; break;
288 case DRIVE_CDROM: icon_idx = IDI_SHELL_CDROM; break;
289 case DRIVE_REMOTE: icon_idx = IDI_SHELL_NETDRIVE; break;
290 case DRIVE_RAMDISK: icon_idx = IDI_SHELL_RAMDISK; break;
291 }
292 }
293
294 if (icon_idx != -1)
295 {
296 lstrcpynW(szIconFile, swShell32Name, cchMax);
297 *piIndex = -icon_idx;
298 }
299 else
300 {
301 if (HCR_GetDefaultIconW(drive, szIconFile, cchMax, &dwNr))
302 {
303 *piIndex = dwNr;
304 }
305 else
306 {
307 lstrcpynW(szIconFile, swShell32Name, cchMax);
308 *piIndex = -IDI_SHELL_DRIVE;
309 }
310 }
311 }
312 else if (_ILIsFolder (pSimplePidl))
313 {
314 getIconLocationForFolder(iface, uFlags, szIconFile, cchMax, piIndex,
315 pwFlags);
316 }
317 else
318 {
319 BOOL found = FALSE;
320
321 if (_ILIsCPanelStruct(pSimplePidl))
322 {
323 if (SUCCEEDED(CPanel_GetIconLocationW(pSimplePidl, szIconFile, cchMax, piIndex)))
324 found = TRUE;
325 }
326 else if (_ILGetExtension(pSimplePidl, sTemp, MAX_PATH))
327 {
328 if (HCR_MapTypeToValueA(sTemp, sTemp, MAX_PATH, TRUE)
329 && HCR_GetDefaultIconA(sTemp, sTemp, MAX_PATH, &dwNr))
330 {
331 if (!lstrcmpA("%1", sTemp)) /* icon is in the file */
332 {
333 SHGetPathFromIDListW(This->pidl, szIconFile);
334 *piIndex = 0;
335 }
336 else
337 {
338 MultiByteToWideChar(CP_ACP, 0, sTemp, -1, szIconFile, cchMax);
339 *piIndex = dwNr;
340 }
341
342 found = TRUE;
343 }
344 else if (!lstrcmpiA(sTemp, "lnkfile"))
345 {
346 /* extract icon from shell shortcut */
347 IShellFolder* dsf;
348 IShellLinkW* psl;
349
350 if (SUCCEEDED(SHGetDesktopFolder(&dsf)))
351 {
352 HRESULT hr = IShellFolder_GetUIObjectOf(dsf, NULL, 1, (LPCITEMIDLIST*)&This->pidl, &IID_IShellLinkW, NULL, (LPVOID*)&psl);
353
354 if (SUCCEEDED(hr))
355 {
356 hr = IShellLinkW_GetIconLocation(psl, szIconFile, MAX_PATH, piIndex);
357
358 if (SUCCEEDED(hr) && *szIconFile)
359 found = TRUE;
360
361 IShellLinkW_Release(psl);
362 }
363
364 IShellFolder_Release(dsf);
365 }
366 }
367 }
368
369 if (!found) /* default icon */
370 {
371 lstrcpynW(szIconFile, swShell32Name, cchMax);
372 *piIndex = 0;
373 }
374 }
375
376 TRACE("-- %s %x\n", debugstr_w(szIconFile), *piIndex);
377 return NOERROR;
378 }
379
380 /**************************************************************************
381 * IExtractIconW_Extract
382 */
383 static HRESULT WINAPI IExtractIconW_fnExtract(IExtractIconW * iface, LPCWSTR pszFile, UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
384 {
385 IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
386 int index;
387
388 FIXME("(%p) (file=%p index=%d %p %p size=%08x) semi-stub\n", This, debugstr_w(pszFile), (signed)nIconIndex,
389 phiconLarge, phiconSmall, nIconSize);
390
391 index = SIC_GetIconIndex(pszFile, nIconIndex, 0);
392
393 if (phiconLarge)
394 *phiconLarge = ImageList_GetIcon(ShellBigIconList, index, ILD_TRANSPARENT);
395
396 if (phiconSmall)
397 *phiconSmall = ImageList_GetIcon(ShellSmallIconList, index, ILD_TRANSPARENT);
398
399 return S_OK;
400 }
401
402 static const IExtractIconWVtbl eivt =
403 {
404 IExtractIconW_fnQueryInterface,
405 IExtractIconW_fnAddRef,
406 IExtractIconW_fnRelease,
407 IExtractIconW_fnGetIconLocation,
408 IExtractIconW_fnExtract
409 };
410
411 /**************************************************************************
412 * IExtractIconA_Constructor
413 */
414 IExtractIconA* IExtractIconA_Constructor(LPCITEMIDLIST pidl)
415 {
416 IExtractIconWImpl *This = (IExtractIconWImpl *)IExtractIconW_Constructor(pidl);
417 IExtractIconA *eia = (IExtractIconA *)&This->lpvtblExtractIconA;
418
419 TRACE("(%p)->(%p)\n", This, eia);
420 return eia;
421 }
422 /**************************************************************************
423 * IExtractIconA_QueryInterface
424 */
425 static HRESULT WINAPI IExtractIconA_fnQueryInterface(IExtractIconA * iface, REFIID riid, LPVOID *ppvObj)
426 {
427 IExtractIconW *This = impl_from_IExtractIconA(iface);
428
429 return IExtractIconW_QueryInterface(This, riid, ppvObj);
430 }
431
432 /**************************************************************************
433 * IExtractIconA_AddRef
434 */
435 static ULONG WINAPI IExtractIconA_fnAddRef(IExtractIconA * iface)
436 {
437 IExtractIconW *This = impl_from_IExtractIconA(iface);
438
439 return IExtractIconW_AddRef(This);
440 }
441 /**************************************************************************
442 * IExtractIconA_Release
443 */
444 static ULONG WINAPI IExtractIconA_fnRelease(IExtractIconA * iface)
445 {
446 IExtractIconW *This = impl_from_IExtractIconA(iface);
447
448 return IExtractIconW_AddRef(This);
449 }
450 /**************************************************************************
451 * IExtractIconA_GetIconLocation
452 *
453 * mapping filetype to icon
454 */
455 static HRESULT WINAPI IExtractIconA_fnGetIconLocation(
456 IExtractIconA * iface,
457 UINT uFlags,
458 LPSTR szIconFile,
459 UINT cchMax,
460 int * piIndex,
461 UINT * pwFlags)
462 {
463 HRESULT ret;
464 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, cchMax * sizeof(WCHAR));
465 IExtractIconW *This = impl_from_IExtractIconA(iface);
466
467 TRACE("(%p) (flags=%u %p %u %p %p)\n", This, uFlags, szIconFile, cchMax, piIndex, pwFlags);
468
469 ret = IExtractIconW_GetIconLocation(This, uFlags, lpwstrFile, cchMax, piIndex, pwFlags);
470 WideCharToMultiByte(CP_ACP, 0, lpwstrFile, -1, szIconFile, cchMax, NULL, NULL);
471 HeapFree(GetProcessHeap(), 0, lpwstrFile);
472
473 TRACE("-- %s %x\n", szIconFile, *piIndex);
474 return ret;
475 }
476 /**************************************************************************
477 * IExtractIconA_Extract
478 */
479 static HRESULT WINAPI IExtractIconA_fnExtract(IExtractIconA * iface, LPCSTR pszFile, UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
480 {
481 HRESULT ret;
482 INT len = MultiByteToWideChar(CP_ACP, 0, pszFile, -1, NULL, 0);
483 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
484 IExtractIconW *This = impl_from_IExtractIconA(iface);
485
486 TRACE("(%p) (file=%p index=%u %p %p size=%u)\n", This, pszFile, nIconIndex, phiconLarge, phiconSmall, nIconSize);
487
488 MultiByteToWideChar(CP_ACP, 0, pszFile, -1, lpwstrFile, len);
489 ret = IExtractIconW_Extract(This, lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIconSize);
490 HeapFree(GetProcessHeap(), 0, lpwstrFile);
491 return ret;
492 }
493
494 static const IExtractIconAVtbl eiavt =
495 {
496 IExtractIconA_fnQueryInterface,
497 IExtractIconA_fnAddRef,
498 IExtractIconA_fnRelease,
499 IExtractIconA_fnGetIconLocation,
500 IExtractIconA_fnExtract
501 };
502
503 /************************************************************************
504 * IEIPersistFile_QueryInterface (IUnknown)
505 */
506 static HRESULT WINAPI IEIPersistFile_fnQueryInterface(
507 IPersistFile *iface,
508 REFIID iid,
509 LPVOID *ppvObj)
510 {
511 IExtractIconW *This = impl_from_IPersistFile(iface);
512
513 return IExtractIconW_QueryInterface(This, iid, ppvObj);
514 }
515
516 /************************************************************************
517 * IEIPersistFile_AddRef (IUnknown)
518 */
519 static ULONG WINAPI IEIPersistFile_fnAddRef(
520 IPersistFile *iface)
521 {
522 IExtractIconW *This = impl_from_IPersistFile(iface);
523
524 return IExtractIconW_AddRef(This);
525 }
526
527 /************************************************************************
528 * IEIPersistFile_Release (IUnknown)
529 */
530 static ULONG WINAPI IEIPersistFile_fnRelease(
531 IPersistFile *iface)
532 {
533 IExtractIconW *This = impl_from_IPersistFile(iface);
534
535 return IExtractIconW_Release(This);
536 }
537
538 /************************************************************************
539 * IEIPersistFile_GetClassID (IPersist)
540 */
541 static HRESULT WINAPI IEIPersistFile_fnGetClassID(
542 IPersistFile *iface,
543 LPCLSID lpClassId)
544 {
545 CLSID StdFolderID = { 0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} };
546
547 if (lpClassId==NULL)
548 return E_POINTER;
549
550 memcpy(lpClassId, &StdFolderID, sizeof(StdFolderID));
551
552 return S_OK;
553 }
554
555 /************************************************************************
556 * IEIPersistFile_Load (IPersistFile)
557 */
558 static HRESULT WINAPI IEIPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
559 {
560 IExtractIconW *This = impl_from_IPersistFile(iface);
561 FIXME("%p\n", This);
562 return E_NOTIMPL;
563
564 }
565
566 static const IPersistFileVtbl pfvt =
567 {
568 IEIPersistFile_fnQueryInterface,
569 IEIPersistFile_fnAddRef,
570 IEIPersistFile_fnRelease,
571 IEIPersistFile_fnGetClassID,
572 (void *) 0xdeadbeef /* IEIPersistFile_fnIsDirty */,
573 IEIPersistFile_fnLoad,
574 (void *) 0xdeadbeef /* IEIPersistFile_fnSave */,
575 (void *) 0xdeadbeef /* IEIPersistFile_fnSaveCompleted */,
576 (void *) 0xdeadbeef /* IEIPersistFile_fnGetCurFile */
577 };