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