shell32: Fix folder icon index when read from registry.
[wine] / dlls / shell32 / classes.c
1 /*
2  *      file type mapping
3  *      (HKEY_CLASSES_ROOT - Stuff)
4  *
5  * Copyright 1998, 1999, 2000 Juergen Schmied
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29
30 #define COBJMACROS
31
32 #include "wine/debug.h"
33 #include "winerror.h"
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winreg.h"
37 #include "wingdi.h"
38 #include "winuser.h"
39
40 #include "shlobj.h"
41 #include "shell32_main.h"
42 #include "shlguid.h"
43 #include "shresdef.h"
44 #include "shlwapi.h"
45 #include "pidl.h"
46 #include "wine/unicode.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(shell);
49
50 #define MAX_EXTENSION_LENGTH 20
51
52 BOOL HCR_MapTypeToValueW(LPCWSTR szExtension, LPWSTR szFileType, LONG len, BOOL bPrependDot)
53 {       
54         HKEY    hkey;
55         WCHAR   szTemp[MAX_EXTENSION_LENGTH + 2];
56
57         TRACE("%s %p\n", debugstr_w(szExtension), debugstr_w(szFileType));
58
59     /* added because we do not want to have double dots */
60     if (szExtension[0] == '.')
61         bPrependDot = 0;
62
63         if (bPrependDot)
64           szTemp[0] = '.';
65
66         lstrcpynW(szTemp + (bPrependDot?1:0), szExtension, MAX_EXTENSION_LENGTH);
67
68         if (RegOpenKeyExW(HKEY_CLASSES_ROOT, szTemp, 0, 0x02000000, &hkey))
69         { 
70           return FALSE;
71         }
72
73         if (RegQueryValueW(hkey, NULL, szFileType, &len))
74         { 
75           RegCloseKey(hkey);
76           return FALSE;
77         }
78
79         RegCloseKey(hkey);
80
81         TRACE("--UE;\n} %s\n", debugstr_w(szFileType));
82
83         return TRUE;
84 }
85
86 BOOL HCR_MapTypeToValueA(LPCSTR szExtension, LPSTR szFileType, LONG len, BOOL bPrependDot)
87 {
88         HKEY    hkey;
89         char    szTemp[MAX_EXTENSION_LENGTH + 2];
90
91         TRACE("%s %p\n", szExtension, szFileType);
92
93     /* added because we do not want to have double dots */
94     if (szExtension[0] == '.')
95         bPrependDot = 0;
96
97         if (bPrependDot)
98           szTemp[0] = '.';
99
100         lstrcpynA(szTemp + (bPrependDot?1:0), szExtension, MAX_EXTENSION_LENGTH);
101
102         if (RegOpenKeyExA(HKEY_CLASSES_ROOT, szTemp, 0, 0x02000000, &hkey))
103         { 
104           return FALSE;
105         }
106
107         if (RegQueryValueA(hkey, NULL, szFileType, &len))
108         { 
109           RegCloseKey(hkey);
110           return FALSE;
111         }
112
113         RegCloseKey(hkey);
114
115         TRACE("--UE;\n} %s\n", szFileType);
116
117         return TRUE;
118 }
119
120
121 BOOL HCR_GetExecuteCommandW( HKEY hkeyClass, LPCWSTR szClass, LPCWSTR szVerb, LPWSTR szDest, DWORD len )
122 {
123         static const WCHAR swShell[] = {'s','h','e','l','l','\\',0};
124         static const WCHAR swCommand[] = {'\\','c','o','m','m','a','n','d',0};
125         BOOL    ret = FALSE;
126
127         TRACE("%p %s %s %p\n", hkeyClass, debugstr_w(szClass), debugstr_w(szVerb), szDest);
128
129         if (szClass)
130             RegOpenKeyExW(HKEY_CLASSES_ROOT, szClass, 0, 0x02000000, &hkeyClass);
131
132         if (hkeyClass)
133         {
134             WCHAR sTemp[MAX_PATH];
135             lstrcpyW(sTemp, swShell);
136             lstrcatW(sTemp, szVerb);
137             lstrcatW(sTemp, swCommand);
138
139             ret = (ERROR_SUCCESS == SHGetValueW(hkeyClass, sTemp, NULL, NULL, szDest, &len));
140
141             if (szClass)
142                RegCloseKey(hkeyClass);
143         }
144
145         TRACE("-- %s\n", debugstr_w(szDest) );
146         return ret;
147 }
148
149 /***************************************************************************************
150 *       HCR_GetDefaultIcon      [internal]
151 *
152 * Gets the icon for a filetype
153 */
154 static BOOL HCR_RegOpenClassIDKey(REFIID riid, HKEY *hkey)
155 {
156         char    xriid[50];
157     sprintf( xriid, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
158                  riid->Data1, riid->Data2, riid->Data3,
159                  riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
160                  riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7] );
161
162         TRACE("%s\n",xriid );
163
164         return !RegOpenKeyExA(HKEY_CLASSES_ROOT, xriid, 0, KEY_READ, hkey);
165 }
166
167 static BOOL HCR_RegGetDefaultIconW(HKEY hkey, LPWSTR szDest, DWORD len, int* picon_idx)
168 {
169     DWORD dwType;
170     WCHAR sTemp[MAX_PATH];
171     WCHAR sNum[5];
172
173     if (!RegQueryValueExW(hkey, NULL, 0, &dwType, (LPBYTE)szDest, &len))
174     {
175       if (dwType == REG_EXPAND_SZ)
176       {
177         ExpandEnvironmentStringsW(szDest, sTemp, MAX_PATH);
178         lstrcpynW(szDest, sTemp, len);
179       }
180       if (ParseFieldW (szDest, 2, sNum, 5))
181              *picon_idx = atoiW(sNum);
182           else
183              *picon_idx=0; /* sometimes the icon number is missing */
184       ParseFieldW (szDest, 1, szDest, len);
185           PathUnquoteSpacesW(szDest);
186       return TRUE;
187     }
188     return FALSE;
189 }
190
191 static BOOL HCR_RegGetDefaultIconA(HKEY hkey, LPSTR szDest, DWORD len, int* picon_idx)
192 {
193         DWORD dwType;
194         char sTemp[MAX_PATH];
195         char  sNum[5];
196
197         if (!RegQueryValueExA(hkey, NULL, 0, &dwType, (LPBYTE)szDest, &len))
198         {
199       if (dwType == REG_EXPAND_SZ)
200           {
201             ExpandEnvironmentStringsA(szDest, sTemp, MAX_PATH);
202             lstrcpynA(szDest, sTemp, len);
203           }
204           if (ParseFieldA (szDest, 2, sNum, 5))
205              *picon_idx=atoi(sNum);
206           else
207              *picon_idx=0; /* sometimes the icon number is missing */
208           ParseFieldA (szDest, 1, szDest, len);
209           PathUnquoteSpacesA(szDest);
210           return TRUE;
211         }
212         return FALSE;
213 }
214
215 BOOL HCR_GetDefaultIconW(LPCWSTR szClass, LPWSTR szDest, DWORD len, int* picon_idx)
216 {
217         static const WCHAR swDefaultIcon[] = {'\\','D','e','f','a','u','l','t','I','c','o','n',0};
218         HKEY    hkey;
219         WCHAR   sTemp[MAX_PATH];
220         BOOL    ret = FALSE;
221
222         TRACE("%s\n",debugstr_w(szClass) );
223
224         lstrcpynW(sTemp, szClass, MAX_PATH);
225         lstrcatW(sTemp, swDefaultIcon);
226
227         if (!RegOpenKeyExW(HKEY_CLASSES_ROOT, sTemp, 0, 0x02000000, &hkey))
228         {
229           ret = HCR_RegGetDefaultIconW(hkey, szDest, len, picon_idx);
230           RegCloseKey(hkey);
231         }
232
233         if(ret)
234             TRACE("-- %s %i\n", debugstr_w(szDest), *picon_idx);
235         else
236             TRACE("-- not found\n");
237
238         return ret;
239 }
240
241 BOOL HCR_GetDefaultIconA(LPCSTR szClass, LPSTR szDest, DWORD len, int* picon_idx)
242 {
243         HKEY    hkey;
244         char    sTemp[MAX_PATH];
245         BOOL    ret = FALSE;
246
247         TRACE("%s\n",szClass );
248
249         sprintf(sTemp, "%s\\DefaultIcon",szClass);
250
251         if (!RegOpenKeyExA(HKEY_CLASSES_ROOT, sTemp, 0, 0x02000000, &hkey))
252         {
253           ret = HCR_RegGetDefaultIconA(hkey, szDest, len, picon_idx);
254           RegCloseKey(hkey);
255         }
256         TRACE("-- %s %i\n", szDest, *picon_idx);
257         return ret;
258 }
259
260 BOOL HCR_GetDefaultIconFromGUIDW(REFIID riid, LPWSTR szDest, DWORD len, int* picon_idx)
261 {
262         HKEY    hkey;
263         BOOL    ret = FALSE;
264
265         if (HCR_RegOpenClassIDKey(riid, &hkey))
266         {
267           ret = HCR_RegGetDefaultIconW(hkey, szDest, len, picon_idx);
268           RegCloseKey(hkey);
269         }
270         TRACE("-- %s %i\n", debugstr_w(szDest), *picon_idx);
271         return ret;
272 }
273
274 /***************************************************************************************
275 *       HCR_GetClassName        [internal]
276 *
277 * Gets the name of a registered class
278 */
279 static const WCHAR swEmpty[] = {0};
280
281 BOOL HCR_GetClassNameW(REFIID riid, LPWSTR szDest, DWORD len)
282 {       
283         HKEY    hkey;
284         BOOL ret = FALSE;
285         DWORD buflen = len;
286
287         szDest[0] = 0;
288         if (HCR_RegOpenClassIDKey(riid, &hkey))
289         {
290           if (!RegQueryValueExW(hkey, swEmpty, 0, NULL, (LPBYTE)szDest, &len))
291           {
292             ret = TRUE;
293           }
294           RegCloseKey(hkey);
295         }
296
297         if (!ret || !szDest[0])
298         {
299           if(IsEqualIID(riid, &CLSID_ShellDesktop))
300           {
301             if (LoadStringW(shell32_hInstance, IDS_DESKTOP, szDest, buflen))
302               ret = TRUE;
303           }
304           else if (IsEqualIID(riid, &CLSID_MyComputer))
305           {
306             if(LoadStringW(shell32_hInstance, IDS_MYCOMPUTER, szDest, buflen))
307               ret = TRUE;
308           }
309         }
310         TRACE("-- %s\n", debugstr_w(szDest));
311         return ret;
312 }
313
314 BOOL HCR_GetClassNameA(REFIID riid, LPSTR szDest, DWORD len)
315 {       HKEY    hkey;
316         BOOL ret = FALSE;
317         DWORD buflen = len;
318
319         szDest[0] = 0;
320         if (HCR_RegOpenClassIDKey(riid, &hkey))
321         {
322           if (!RegQueryValueExA(hkey,"",0,NULL,(LPBYTE)szDest,&len))
323           {
324             ret = TRUE;
325           }
326           RegCloseKey(hkey);
327         }
328
329         if (!ret || !szDest[0])
330         {
331           if(IsEqualIID(riid, &CLSID_ShellDesktop))
332           {
333             if (LoadStringA(shell32_hInstance, IDS_DESKTOP, szDest, buflen))
334               ret = TRUE;
335           }
336           else if (IsEqualIID(riid, &CLSID_MyComputer))
337           {
338             if(LoadStringA(shell32_hInstance, IDS_MYCOMPUTER, szDest, buflen))
339               ret = TRUE;
340           }
341         }
342
343         TRACE("-- %s\n", szDest);
344
345         return ret;
346 }
347
348 /******************************************************************************
349  * HCR_GetFolderAttributes [Internal]
350  *
351  * Query the registry for a shell folders' attributes
352  *
353  * PARAMS
354  *  pidlFolder    [I]  A simple pidl of type PT_GUID. 
355  *  pdwAttributes [IO] In: Attributes to be queried, OUT: Resulting attributes.
356  *
357  * RETURNS
358  *  TRUE:  Found information for the attributes in the registry
359  *  FALSE: No attribute information found
360  *
361  * NOTES
362  *  If queried for an attribute, which is set in the CallForAttributes registry
363  *  value, the function binds to the shellfolder objects and queries it.
364  */
365 BOOL HCR_GetFolderAttributes(LPCITEMIDLIST pidlFolder, LPDWORD pdwAttributes)
366 {
367     HKEY hSFKey;
368     LPOLESTR pwszCLSID;
369     LONG lResult;
370     DWORD dwTemp, dwLen;
371     static const WCHAR wszAttributes[] = { 'A','t','t','r','i','b','u','t','e','s',0 };
372     static const WCHAR wszCallForAttributes[] = { 
373         'C','a','l','l','F','o','r','A','t','t','r','i','b','u','t','e','s',0 };
374     WCHAR wszShellFolderKey[] = { 'C','L','S','I','D','\\','{','0','0','0','2','1','4','0','0','-',
375         '0','0','0','0','-','0','0','0','0','-','C','0','0','0','-','0','0','0','0','0','0','0',
376         '0','0','0','4','6','}','\\','S','h','e','l','l','F','o','l','d','e','r',0 };
377
378     TRACE("(pidlFolder=%p, pdwAttributes=%p)\n", pidlFolder, pdwAttributes);
379        
380     if (!_ILIsPidlSimple(pidlFolder)) { 
381         ERR("HCR_GetFolderAttributes should be called for simple PIDL's only!\n");    
382         return FALSE;
383     }
384     
385     if (!_ILIsDesktop(pidlFolder)) {
386         if (FAILED(StringFromCLSID(_ILGetGUIDPointer(pidlFolder), &pwszCLSID))) return FALSE;
387         memcpy(&wszShellFolderKey[6], pwszCLSID, 38 * sizeof(WCHAR));
388         CoTaskMemFree(pwszCLSID);
389     }
390     
391     lResult = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszShellFolderKey, 0, KEY_READ, &hSFKey);
392     if (lResult != ERROR_SUCCESS) return FALSE;
393     
394     dwLen = sizeof(DWORD);
395     lResult = RegQueryValueExW(hSFKey, wszCallForAttributes, 0, NULL, (LPBYTE)&dwTemp, &dwLen);
396     if ((lResult == ERROR_SUCCESS) && (dwTemp & *pdwAttributes)) {
397         LPSHELLFOLDER psfDesktop, psfFolder;
398         HRESULT hr;
399
400         RegCloseKey(hSFKey);
401         hr = SHGetDesktopFolder(&psfDesktop);
402         if (SUCCEEDED(hr)) {
403             hr = IShellFolder_BindToObject(psfDesktop, pidlFolder, NULL, &IID_IShellFolder, 
404                                            (LPVOID*)&psfFolder);
405             if (SUCCEEDED(hr)) { 
406                 hr = IShellFolder_GetAttributesOf(psfFolder, 0, NULL, pdwAttributes);
407                 IShellFolder_Release(psfFolder);
408             }
409             IShellFolder_Release(psfDesktop);
410         }
411         if (FAILED(hr)) return FALSE;
412     } else {
413         lResult = RegQueryValueExW(hSFKey, wszAttributes, 0, NULL, (LPBYTE)&dwTemp, &dwLen);
414         RegCloseKey(hSFKey);
415         if (lResult == ERROR_SUCCESS) {
416             *pdwAttributes &= dwTemp;
417         } else {
418             return FALSE;
419         }
420     }
421
422     TRACE("-- *pdwAttributes == 0x%08lx\n", *pdwAttributes);
423
424     return TRUE;
425 }