Fix bug found by Piotr Caban, where our function tried to delete a
[wine] / dlls / shell32 / iconcache.c
1 /*
2  *      shell icon cache (SIC)
3  *
4  * Copyright 1998, 1999 Juergen Schmied
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdarg.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "winreg.h"
35 #include "wine/debug.h"
36
37 #include "shellapi.h"
38 #include "objbase.h"
39 #include "shlguid.h"
40 #include "pidl.h"
41 #include "shell32_main.h"
42 #include "undocshell.h"
43 #include "shlwapi.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(shell);
46
47 /********************** THE ICON CACHE ********************************/
48
49 #define INVALID_INDEX -1
50
51 typedef struct
52 {
53         LPWSTR sSourceFile;     /* file (not path!) containing the icon */
54         DWORD dwSourceIndex;    /* index within the file, if it is a resoure ID it will be negated */
55         DWORD dwListIndex;      /* index within the iconlist */
56         DWORD dwFlags;          /* GIL_* flags */
57         DWORD dwAccessTime;
58 } SIC_ENTRY, * LPSIC_ENTRY;
59
60 static HDPA             sic_hdpa = 0;
61
62 static CRITICAL_SECTION SHELL32_SicCS;
63 static CRITICAL_SECTION_DEBUG critsect_debug =
64 {
65     0, 0, &SHELL32_SicCS,
66     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
67       0, 0, { 0, (DWORD)(__FILE__ ": SHELL32_SicCS") }
68 };
69 static CRITICAL_SECTION SHELL32_SicCS = { &critsect_debug, -1, 0, 0, 0, 0 };
70
71 /*****************************************************************************
72  * SIC_CompareEntries
73  *
74  * NOTES
75  *  Callback for DPA_Search
76  */
77 static INT CALLBACK SIC_CompareEntries( LPVOID p1, LPVOID p2, LPARAM lparam)
78 {       TRACE("%p %p %8lx\n", p1, p2, lparam);
79
80         if (((LPSIC_ENTRY)p1)->dwSourceIndex != ((LPSIC_ENTRY)p2)->dwSourceIndex) /* first the faster one*/
81           return 1;
82
83         if (strcmpiW(((LPSIC_ENTRY)p1)->sSourceFile,((LPSIC_ENTRY)p2)->sSourceFile))
84           return 1;
85
86         return 0;
87 }
88 /*****************************************************************************
89  * SIC_IconAppend                       [internal]
90  *
91  * NOTES
92  *  appends an icon pair to the end of the cache
93  */
94 static INT SIC_IconAppend (LPCWSTR sSourceFile, INT dwSourceIndex, HICON hSmallIcon, HICON hBigIcon)
95 {       LPSIC_ENTRY lpsice;
96         INT ret, index, index1;
97         WCHAR path[MAX_PATH];
98         TRACE("%s %i %p %p\n", debugstr_w(sSourceFile), dwSourceIndex, hSmallIcon ,hBigIcon);
99
100         lpsice = (LPSIC_ENTRY) SHAlloc (sizeof (SIC_ENTRY));
101
102        GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
103         lpsice->sSourceFile = HeapAlloc( GetProcessHeap(), 0, (strlenW(path)+1)*sizeof(WCHAR) );
104         strcpyW( lpsice->sSourceFile, path );
105
106         lpsice->dwSourceIndex = dwSourceIndex;
107
108         EnterCriticalSection(&SHELL32_SicCS);
109
110         index = DPA_InsertPtr(sic_hdpa, 0x7fff, lpsice);
111         if ( INVALID_INDEX == index )
112         {
113           HeapFree(GetProcessHeap(), 0, lpsice->sSourceFile);
114           SHFree(lpsice);
115           ret = INVALID_INDEX;
116         }
117         else
118         {
119           index = ImageList_AddIcon (ShellSmallIconList, hSmallIcon);
120           index1= ImageList_AddIcon (ShellBigIconList, hBigIcon);
121
122           if (index!=index1)
123           {
124             FIXME("iconlists out of sync 0x%x 0x%x\n", index, index1);
125           }
126           lpsice->dwListIndex = index;
127           ret = lpsice->dwListIndex;
128         }
129
130         LeaveCriticalSection(&SHELL32_SicCS);
131         return ret;
132 }
133 /****************************************************************************
134  * SIC_LoadIcon                         [internal]
135  *
136  * NOTES
137  *  gets small/big icon by number from a file
138  */
139 static INT SIC_LoadIcon (LPCWSTR sSourceFile, INT dwSourceIndex)
140 {       HICON   hiconLarge=0;
141         HICON   hiconSmall=0;
142
143         PrivateExtractIconsW( sSourceFile, dwSourceIndex, 32, 32, &hiconLarge, 0, 1, 0 );
144         PrivateExtractIconsW( sSourceFile, dwSourceIndex, 16, 16, &hiconSmall, 0, 1, 0 );
145
146         if ( !hiconLarge ||  !hiconSmall)
147         {
148           WARN("failure loading icon %i from %s (%p %p)\n", dwSourceIndex, debugstr_w(sSourceFile), hiconLarge, hiconSmall);
149           return -1;
150         }
151         return SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge);
152 }
153 /*****************************************************************************
154  * SIC_GetIconIndex                     [internal]
155  *
156  * Parameters
157  *      sSourceFile     [IN]    filename of file containing the icon
158  *      index           [IN]    index/resID (negated) in this file
159  *
160  * NOTES
161  *  look in the cache for a proper icon. if not available the icon is taken
162  *  from the file and cached
163  */
164 INT SIC_GetIconIndex (LPCWSTR sSourceFile, INT dwSourceIndex )
165 {
166         SIC_ENTRY sice;
167         INT ret, index = INVALID_INDEX;
168         WCHAR path[MAX_PATH];
169
170         TRACE("%s %i\n", debugstr_w(sSourceFile), dwSourceIndex);
171
172         GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
173         sice.sSourceFile = path;
174         sice.dwSourceIndex = dwSourceIndex;
175
176         EnterCriticalSection(&SHELL32_SicCS);
177
178         if (NULL != DPA_GetPtr (sic_hdpa, 0))
179         {
180           /* search linear from position 0*/
181           index = DPA_Search (sic_hdpa, &sice, 0, SIC_CompareEntries, 0, 0);
182         }
183
184         if ( INVALID_INDEX == index )
185         {
186           ret = SIC_LoadIcon (sSourceFile, dwSourceIndex);
187         }
188         else
189         {
190           TRACE("-- found\n");
191           ret = ((LPSIC_ENTRY)DPA_GetPtr(sic_hdpa, index))->dwListIndex;
192         }
193
194         LeaveCriticalSection(&SHELL32_SicCS);
195         return ret;
196 }
197 /*****************************************************************************
198  * SIC_Initialize                       [internal]
199  *
200  * NOTES
201  *  hack to load the resources from the shell32.dll under a different dll name
202  *  will be removed when the resource-compiler is ready
203  */
204 BOOL SIC_Initialize(void)
205 {
206         HICON           hSm, hLg;
207         UINT            index;
208         int             cx_small, cy_small;
209         int             cx_large, cy_large;
210
211         cx_small = GetSystemMetrics(SM_CXSMICON);
212         cy_small = GetSystemMetrics(SM_CYSMICON);
213         cx_large = GetSystemMetrics(SM_CXICON);
214         cy_large = GetSystemMetrics(SM_CYICON);
215
216         TRACE("\n");
217
218         if (sic_hdpa)   /* already initialized?*/
219           return TRUE;
220
221         sic_hdpa = DPA_Create(16);
222
223         if (!sic_hdpa)
224         {
225           return(FALSE);
226         }
227
228        ShellSmallIconList = ImageList_Create(16,16,ILC_COLOR32|ILC_MASK,0,0x20);
229        ShellBigIconList = ImageList_Create(32,32,ILC_COLOR32|ILC_MASK,0,0x20);
230
231        ImageList_SetBkColor(ShellSmallIconList, CLR_NONE);
232        ImageList_SetBkColor(ShellBigIconList, CLR_NONE);
233
234         for (index=1; index<39; index++)
235         {
236           hSm = (HICON)LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, cx_small, cy_small, LR_SHARED);
237           hLg = (HICON)LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, cx_large, cy_large, LR_SHARED);
238
239           if(!hSm)
240           {
241             hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(1), IMAGE_ICON, cx_small, cy_small, LR_SHARED);
242             hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(1), IMAGE_ICON, cx_large, cy_large, LR_SHARED);
243           }
244          SIC_IconAppend (swShell32Name, index, hSm, hLg);
245         }
246
247         TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
248
249         return TRUE;
250 }
251 /*************************************************************************
252  * SIC_Destroy
253  *
254  * frees the cache
255  */
256 static INT CALLBACK sic_free( LPVOID ptr, LPVOID lparam )
257 {
258         HeapFree(GetProcessHeap(), 0, ((LPSIC_ENTRY)ptr)->sSourceFile);
259         SHFree(ptr);
260         return TRUE;
261 }
262
263 void SIC_Destroy(void)
264 {
265         TRACE("\n");
266
267         EnterCriticalSection(&SHELL32_SicCS);
268
269         if (sic_hdpa) DPA_DestroyCallback(sic_hdpa, sic_free, NULL );
270
271         sic_hdpa = NULL;
272         ImageList_Destroy(ShellSmallIconList);
273         ShellSmallIconList = 0;
274         ImageList_Destroy(ShellBigIconList);
275         ShellBigIconList = 0;
276
277         LeaveCriticalSection(&SHELL32_SicCS);
278         DeleteCriticalSection(&SHELL32_SicCS);
279 }
280
281 /*************************************************************************
282  * Shell_GetImageList                   [SHELL32.71]
283  *
284  * PARAMETERS
285  *  imglist[1|2] [OUT] pointer which receives imagelist handles
286  *
287  */
288 BOOL WINAPI Shell_GetImageList(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
289 {       TRACE("(%p,%p)\n",lpBigList,lpSmallList);
290         if (lpBigList)
291         { *lpBigList = ShellBigIconList;
292         }
293         if (lpSmallList)
294         { *lpSmallList = ShellSmallIconList;
295         }
296
297         return TRUE;
298 }
299 /*************************************************************************
300  * PidlToSicIndex                       [INTERNAL]
301  *
302  * PARAMETERS
303  *      sh      [IN]    IShellFolder
304  *      pidl    [IN]
305  *      bBigIcon [IN]
306  *      uFlags  [IN]    GIL_*
307  *      pIndex  [OUT]   index within the SIC
308  *
309  */
310 BOOL PidlToSicIndex (
311         IShellFolder * sh,
312         LPCITEMIDLIST pidl,
313         BOOL bBigIcon,
314         UINT uFlags,
315         int * pIndex)
316 {
317         IExtractIconW   *ei;
318         WCHAR           szIconFile[MAX_PATH];   /* file containing the icon */
319         INT             iSourceIndex;           /* index or resID(negated) in this file */
320         BOOL            ret = FALSE;
321         UINT            dwFlags = 0;
322
323         TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
324
325         if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconW, 0, (void **)&ei)))
326         {
327           if (SUCCEEDED(IExtractIconW_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
328           {
329             *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex);
330             ret = TRUE;
331           }
332           IExtractIconW_Release(ei);
333         }
334
335         if (INVALID_INDEX == *pIndex)   /* default icon when failed */
336           *pIndex = 0;
337
338         return ret;
339
340 }
341
342 /*************************************************************************
343  * SHMapPIDLToSystemImageListIndex      [SHELL32.77]
344  *
345  * PARAMETERS
346  *      sh      [IN]            pointer to an instance of IShellFolder
347  *      pidl    [IN]
348  *      pIndex  [OUT][OPTIONAL] SIC index for big icon
349  *
350  */
351 int WINAPI SHMapPIDLToSystemImageListIndex(
352         IShellFolder *sh,
353         LPCITEMIDLIST pidl,
354         int *pIndex)
355 {
356         int Index;
357
358         TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
359         pdump(pidl);
360
361         if (pIndex)
362           PidlToSicIndex ( sh, pidl, 1, 0, pIndex);
363         PidlToSicIndex ( sh, pidl, 0, 0, &Index);
364         return Index;
365 }
366
367 /*************************************************************************
368  * Shell_GetCachedImageIndex            [SHELL32.72]
369  *
370  */
371 INT WINAPI Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
372 {
373         INT ret, len;
374         LPWSTR szTemp;
375
376         WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
377
378         len = MultiByteToWideChar( CP_ACP, 0, szPath, -1, NULL, 0 );
379         szTemp = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
380         MultiByteToWideChar( CP_ACP, 0, szPath, -1, szTemp, len );
381
382         ret = SIC_GetIconIndex( szTemp, nIndex );
383
384         HeapFree( GetProcessHeap(), 0, szTemp );
385
386         return ret;
387 }
388
389 INT WINAPI Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
390 {
391         WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
392
393         return SIC_GetIconIndex(szPath, nIndex);
394 }
395
396 INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
397 {       if( SHELL_OsIsUnicode())
398           return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
399         return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
400 }
401
402 /*************************************************************************
403  * ExtractIconEx                        [SHELL32.@]
404  */
405 UINT WINAPI ExtractIconExAW(LPCVOID lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
406 {       if (SHELL_OsIsUnicode())
407           return ExtractIconExW ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
408         return ExtractIconExA ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
409 }
410
411 /*************************************************************************
412  * ExtractIconExW                       [SHELL32.@]
413  * RETURNS
414  *  0 no icon found
415  *  -1 file is not valid
416  *  or number of icons extracted
417  */
418 UINT WINAPI ExtractIconExW(LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
419 {
420         TRACE("%s %i %p %p %i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons);
421
422         return PrivateExtractIconExW(lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
423 }
424
425 /*************************************************************************
426  * ExtractIconExA                       [SHELL32.@]
427  */
428 UINT WINAPI ExtractIconExA(LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
429 {
430     UINT ret;
431     INT len = MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, NULL, 0);
432     LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
433
434     TRACE("%s %i %p %p %i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
435
436     MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, lpwstrFile, len);
437     ret = ExtractIconExW (lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
438     HeapFree(GetProcessHeap(), 0, lpwstrFile);
439     return ret;
440 }
441
442 /*************************************************************************
443  *                              ExtractAssociatedIconA (SHELL32.@)
444  *
445  * Return icon for given file (either from file itself or from associated
446  * executable) and patch parameters if needed.
447  */
448 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
449 {       
450         HICON hIcon;
451         WORD wDummyIcon = 0;
452         
453         TRACE("\n");
454
455         if(lpiIcon == NULL)
456             lpiIcon = &wDummyIcon;
457
458         hIcon = ExtractIconA(hInst, lpIconPath, *lpiIcon);
459
460         if( hIcon < (HICON)2 )
461         { if( hIcon == (HICON)1 ) /* no icons found in given file */
462           { char  tempPath[0x80];
463             HINSTANCE uRet = FindExecutableA(lpIconPath,NULL,tempPath);
464
465             if( uRet > (HINSTANCE)32 && tempPath[0] )
466             { strcpy(lpIconPath,tempPath);
467               hIcon = ExtractIconA(hInst, lpIconPath, *lpiIcon);
468               if( hIcon > (HICON)2 )
469                 return hIcon;
470             }
471             else hIcon = 0;
472           }
473
474           if( hIcon == (HICON)1 )
475             *lpiIcon = 2;   /* MSDOS icon - we found .exe but no icons in it */
476           else
477             *lpiIcon = 6;   /* generic icon - found nothing */
478
479           if (GetModuleFileNameA(hInst, lpIconPath, 0x80))
480           {
481               /* terminate string (GetModuleFileName doesn't do if buffer is too small) */
482               lpIconPath[0x80 - 1] = '\0';
483               hIcon = LoadIconA( hInst, MAKEINTRESOURCEA(*lpiIcon));
484           }
485         }
486         return hIcon;
487 }
488
489 /*************************************************************************
490  *                              ExtractAssociatedIconExW (SHELL32.@)
491  *
492  * Return icon for given file (either from file itself or from associated
493  * executable) and patch parameters if needed.
494  */
495 HICON WINAPI ExtractAssociatedIconExW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
496 {
497   FIXME("%p %s %p %p): stub\n", hInst, debugstr_w(lpIconPath), lpiIconIdx, lpiIconId);
498   return 0;
499 }
500
501 /*************************************************************************
502  *                              ExtractAssociatedIconExA (SHELL32.@)
503  *
504  * Return icon for given file (either from file itself or from associated
505  * executable) and patch parameters if needed.
506  */
507 HICON WINAPI ExtractAssociatedIconExA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
508 {
509   HICON ret;
510   INT len = MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, NULL, 0 );
511   LPWSTR lpwstrFile = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
512
513   TRACE("%p %s %p %p)\n", hInst, lpIconPath, lpiIconIdx, lpiIconId);
514
515   MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, lpwstrFile, len );
516   ret = ExtractAssociatedIconExW(hInst, lpwstrFile, lpiIconIdx, lpiIconId);
517   HeapFree(GetProcessHeap(), 0, lpwstrFile);
518   return ret;
519 }
520
521
522 /****************************************************************************
523  * SHDefExtractIconW            [SHELL32.@]
524  */
525 HRESULT WINAPI SHDefExtractIconW(LPCWSTR pszIconFile, int iIndex, UINT uFlags,
526                                  HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
527 {
528         UINT ret;
529         HICON hIcons[2];
530         WARN("%s %d 0x%08x %p %p %d, semi-stub\n", debugstr_w(pszIconFile), iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
531
532         ret = PrivateExtractIconsW(pszIconFile, iIndex, nIconSize, nIconSize, hIcons, NULL, 2, LR_DEFAULTCOLOR);
533         /* FIXME: deal with uFlags parameter which contains GIL_ flags */
534         if (ret == 0xFFFFFFFF)
535           return E_FAIL;
536         if (ret > 0) {
537           *phiconLarge = hIcons[0];
538           *phiconSmall = hIcons[1];
539           return S_OK;
540         }
541         return S_FALSE;
542 }
543
544 /****************************************************************************
545  * SHDefExtractIconA            [SHELL32.@]
546  */
547 HRESULT WINAPI SHDefExtractIconA(LPCSTR pszIconFile, int iIndex, UINT uFlags,
548                                  HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
549 {
550   HRESULT ret;
551   INT len = MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, NULL, 0);
552   LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
553
554   TRACE("%s %d 0x%08x %p %p %d\n", pszIconFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
555
556   MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, lpwstrFile, len);
557   ret = SHDefExtractIconW(lpwstrFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
558   HeapFree(GetProcessHeap(), 0, lpwstrFile);
559   return ret;
560 }