Implement CryptRegisterOIDFunction and CryptSIPAddProvider.
[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 #include "wine/debug.h"
30 #include "winerror.h"
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winreg.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36
37 #include "shlobj.h"
38 #include "shell32_main.h"
39 #include "shlguid.h"
40 #include "shresdef.h"
41 #include "shlwapi.h"
42 #include "wine/unicode.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(shell);
45
46 #define MAX_EXTENSION_LENGTH 20
47
48 BOOL HCR_MapTypeToValueW(LPCWSTR szExtension, LPWSTR szFileType, DWORD len, BOOL bPrependDot)
49 {       
50         HKEY    hkey;
51         WCHAR   szTemp[MAX_EXTENSION_LENGTH + 2];
52
53         TRACE("%s %p\n", debugstr_w(szExtension), debugstr_w(szFileType));
54
55     /* added because we do not want to have double dots */
56     if (szExtension[0] == '.')
57         bPrependDot = 0;
58
59         if (bPrependDot)
60           szTemp[0] = '.';
61
62         lstrcpynW(szTemp + (bPrependDot?1:0), szExtension, MAX_EXTENSION_LENGTH);
63
64         if (RegOpenKeyExW(HKEY_CLASSES_ROOT, szTemp, 0, 0x02000000, &hkey))
65         { 
66           return FALSE;
67         }
68
69         if (RegQueryValueW(hkey, NULL, szFileType, &len))
70         { 
71           RegCloseKey(hkey);
72           return FALSE;
73         }
74
75         RegCloseKey(hkey);
76
77         TRACE("--UE;\n} %s\n", debugstr_w(szFileType));
78
79         return TRUE;
80 }
81
82 BOOL HCR_MapTypeToValueA(LPCSTR szExtension, LPSTR szFileType, DWORD len, BOOL bPrependDot)
83 {
84         HKEY    hkey;
85         char    szTemp[MAX_EXTENSION_LENGTH + 2];
86
87         TRACE("%s %p\n", szExtension, szFileType);
88
89     /* added because we do not want to have double dots */
90     if (szExtension[0] == '.')
91         bPrependDot = 0;
92
93         if (bPrependDot)
94           szTemp[0] = '.';
95
96         lstrcpynA(szTemp + (bPrependDot?1:0), szExtension, MAX_EXTENSION_LENGTH);
97
98         if (RegOpenKeyExA(HKEY_CLASSES_ROOT, szTemp, 0, 0x02000000, &hkey))
99         { 
100           return FALSE;
101         }
102
103         if (RegQueryValueA(hkey, NULL, szFileType, &len))
104         { 
105           RegCloseKey(hkey);
106           return FALSE;
107         }
108
109         RegCloseKey(hkey);
110
111         TRACE("--UE;\n} %s\n", szFileType);
112
113         return TRUE;
114 }
115
116
117 BOOL HCR_GetExecuteCommandW(LPCWSTR szClass, LPCWSTR szVerb, LPWSTR szDest, DWORD len)
118 {
119         static const WCHAR swShell[] = {'\\','s','h','e','l','l','\\',0};
120         static const WCHAR swCommand[] = {'\\','c','o','m','m','a','n','d',0};
121         WCHAR   sTemp[MAX_PATH];
122
123         TRACE("%s %s %p\n",debugstr_w(szClass), debugstr_w(szVerb), szDest);
124
125         lstrcpyW(sTemp, szClass);
126         lstrcatW(sTemp, swShell);
127         lstrcatW(sTemp, szVerb);
128         lstrcatW(sTemp, swCommand);
129
130         if (ERROR_SUCCESS == SHGetValueW(HKEY_CLASSES_ROOT, sTemp, NULL, NULL, szDest, &len)) {
131             TRACE("-- %s\n", debugstr_w(szDest) );
132             return TRUE;
133         }
134         return FALSE;
135 }
136
137 BOOL HCR_GetExecuteCommandA(LPCSTR szClass, LPCSTR szVerb, LPSTR szDest, DWORD len)
138 {
139         char    sTemp[MAX_PATH];
140
141         TRACE("%s %s\n",szClass, szVerb );
142
143         snprintf(sTemp, MAX_PATH, "%s\\shell\\%s\\command",szClass, szVerb);
144
145         if (ERROR_SUCCESS == SHGetValueA(HKEY_CLASSES_ROOT, sTemp, NULL, NULL, szDest, &len)) {
146             TRACE("-- %s\n", debugstr_a(szDest) );
147             return TRUE;
148         }
149         return FALSE;
150 }
151
152 BOOL HCR_GetExecuteCommandExA( HKEY hkeyClass, LPCSTR szClass, LPCSTR szVerb, LPSTR szDest, DWORD len )
153 {
154         BOOL    ret = FALSE;
155
156         TRACE("%p %s %s\n", hkeyClass, szClass, szVerb );
157
158         if (szClass)
159             RegOpenKeyExA(hkeyClass,szClass,0,0x02000000,&hkeyClass);
160
161         if (hkeyClass)
162         {
163             char sTemp[MAX_PATH];
164
165             snprintf(sTemp, MAX_PATH, "shell\\%s\\command", szVerb);
166
167             ret = (ERROR_SUCCESS == SHGetValueA(hkeyClass, sTemp, NULL, NULL, szDest, &len));
168
169             if (szClass)
170                RegCloseKey(hkeyClass);
171         }
172
173         TRACE("-- %s\n", szDest );
174         return ret;
175 }
176
177 BOOL HCR_GetExecuteCommandExW( HKEY hkeyClass, LPCWSTR szClass, LPCWSTR szVerb, LPWSTR szDest, DWORD len )
178 {
179         static const WCHAR swShell[] = {'\\','s','h','e','l','l','\\',0};
180         static const WCHAR swCommand[] = {'\\','c','o','m','m','a','n','d',0};
181         BOOL    ret = FALSE;
182
183         TRACE("%p %s %s %p\n", hkeyClass, debugstr_w(szClass), debugstr_w(szVerb), szDest);
184
185         if (szClass)
186             RegOpenKeyExW(hkeyClass,szClass,0,0x02000000,&hkeyClass);
187
188         if (hkeyClass)
189         {
190             WCHAR sTemp[MAX_PATH];
191             lstrcpyW(sTemp, swShell);
192             lstrcatW(sTemp, szVerb);
193             lstrcatW(sTemp, swCommand);
194
195             ret = (ERROR_SUCCESS == SHGetValueW(hkeyClass, sTemp, NULL, NULL, szDest, &len));
196
197             if (szClass)
198                RegCloseKey(hkeyClass);
199         }
200
201         TRACE("-- %s\n", debugstr_w(szDest) );
202         return ret;
203 }
204
205 /***************************************************************************************
206 *       HCR_GetDefaultIcon      [internal]
207 *
208 * Gets the icon for a filetype
209 */
210 static BOOL HCR_RegOpenClassIDKey(REFIID riid, HKEY *hkey)
211 {
212         char    xriid[50];
213     sprintf( xriid, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
214                  riid->Data1, riid->Data2, riid->Data3,
215                  riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
216                  riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7] );
217
218         TRACE("%s\n",xriid );
219
220         return !RegOpenKeyExA(HKEY_CLASSES_ROOT, xriid, 0, KEY_READ, hkey);
221 }
222
223 static BOOL HCR_RegGetDefaultIconW(HKEY hkey, LPWSTR szDest, DWORD len, LPDWORD dwNr)
224 {
225         DWORD dwType;
226         WCHAR sTemp[MAX_PATH];
227         WCHAR sNum[5];
228
229         if (!RegQueryValueExW(hkey, NULL, 0, &dwType, (LPBYTE)szDest, &len))
230         {
231       if (dwType == REG_EXPAND_SZ)
232           {
233             ExpandEnvironmentStringsW(szDest, sTemp, MAX_PATH);
234             lstrcpynW(szDest, sTemp, len);
235           }
236           if (ParseFieldW (szDest, 2, sNum, 5))
237              *dwNr = atoiW(sNum);
238           else
239              *dwNr=0; /* sometimes the icon number is missing */
240           ParseFieldW (szDest, 1, szDest, len);
241           return TRUE;
242         }
243         return FALSE;
244 }
245
246 static BOOL HCR_RegGetDefaultIconA(HKEY hkey, LPSTR szDest, DWORD len, LPDWORD dwNr)
247 {
248         DWORD dwType;
249         char sTemp[MAX_PATH];
250         char  sNum[5];
251
252         if (!RegQueryValueExA(hkey, NULL, 0, &dwType, szDest, &len))
253         {
254       if (dwType == REG_EXPAND_SZ)
255           {
256             ExpandEnvironmentStringsA(szDest, sTemp, MAX_PATH);
257             lstrcpynA(szDest, sTemp, len);
258           }
259           if (ParseFieldA (szDest, 2, sNum, 5))
260              *dwNr=atoi(sNum);
261           else
262              *dwNr=0; /* sometimes the icon number is missing */
263           ParseFieldA (szDest, 1, szDest, len);
264           return TRUE;
265         }
266         return FALSE;
267 }
268
269 BOOL HCR_GetDefaultIconW(LPCWSTR szClass, LPWSTR szDest, DWORD len, LPDWORD dwNr)
270 {
271         static const WCHAR swDefaultIcon[] = {'\\','D','e','f','a','u','l','t','I','c','o','n',0};
272         HKEY    hkey;
273         WCHAR   sTemp[MAX_PATH];
274         BOOL    ret = FALSE;
275
276         TRACE("%s\n",debugstr_w(szClass) );
277
278         lstrcpynW(sTemp, szClass, MAX_PATH);
279         lstrcatW(sTemp, swDefaultIcon);
280
281         if (!RegOpenKeyExW(HKEY_CLASSES_ROOT, sTemp, 0, 0x02000000, &hkey))
282         {
283           ret = HCR_RegGetDefaultIconW(hkey, szDest, len, dwNr);
284           RegCloseKey(hkey);
285         }
286         TRACE("-- %s %li\n", debugstr_w(szDest), *dwNr );
287         return ret;
288 }
289
290 BOOL HCR_GetDefaultIconA(LPCSTR szClass, LPSTR szDest, DWORD len, LPDWORD dwNr)
291 {
292         HKEY    hkey;
293         char    sTemp[MAX_PATH];
294         BOOL    ret = FALSE;
295
296         TRACE("%s\n",szClass );
297
298         sprintf(sTemp, "%s\\DefaultIcon",szClass);
299
300         if (!RegOpenKeyExA(HKEY_CLASSES_ROOT, sTemp, 0, 0x02000000, &hkey))
301         {
302           ret = HCR_RegGetDefaultIconA(hkey, szDest, len, dwNr);
303           RegCloseKey(hkey);
304         }
305         TRACE("-- %s %li\n", szDest, *dwNr );
306         return ret;
307 }
308
309 BOOL HCR_GetDefaultIconFromGUIDW(REFIID riid, LPWSTR szDest, DWORD len, LPDWORD dwNr)
310 {
311         HKEY    hkey;
312         BOOL    ret = FALSE;
313
314         if (HCR_RegOpenClassIDKey(riid, &hkey))
315         {
316           ret = HCR_RegGetDefaultIconW(hkey, szDest, len, dwNr);
317           RegCloseKey(hkey);
318         }
319         TRACE("-- %s %li\n", debugstr_w(szDest), *dwNr );
320         return ret;
321 }
322
323 /***************************************************************************************
324 *       HCR_GetClassName        [internal]
325 *
326 * Gets the name of a registred class
327 */
328 static WCHAR swEmpty[] = {0};
329
330 BOOL HCR_GetClassNameW(REFIID riid, LPWSTR szDest, DWORD len)
331 {       
332         HKEY    hkey;
333         BOOL ret = FALSE;
334         DWORD buflen = len;
335
336         szDest[0] = 0;
337         if (HCR_RegOpenClassIDKey(riid, &hkey))
338         {
339           if (!RegQueryValueExW(hkey, swEmpty, 0, NULL, (LPBYTE)szDest, &len))
340           {
341             ret = TRUE;
342           }
343           RegCloseKey(hkey);
344         }
345
346         if (!ret || !szDest[0])
347         {
348           if(IsEqualIID(riid, &CLSID_ShellDesktop))
349           {
350             if (LoadStringW(shell32_hInstance, IDS_DESKTOP, szDest, buflen))
351               ret = TRUE;
352           }
353           else if (IsEqualIID(riid, &CLSID_MyComputer))
354           {
355             if(LoadStringW(shell32_hInstance, IDS_MYCOMPUTER, szDest, buflen))
356               ret = TRUE;
357           }
358         }
359         TRACE("-- %s\n", debugstr_w(szDest));
360         return ret;
361 }
362
363 BOOL HCR_GetClassNameA(REFIID riid, LPSTR szDest, DWORD len)
364 {       HKEY    hkey;
365         BOOL ret = FALSE;
366         DWORD buflen = len;
367
368         szDest[0] = 0;
369         if (HCR_RegOpenClassIDKey(riid, &hkey))
370         {
371           if (!RegQueryValueExA(hkey,"",0,NULL,szDest,&len))
372           {
373             ret = TRUE;
374           }
375           RegCloseKey(hkey);
376         }
377
378         if (!ret || !szDest[0])
379         {
380           if(IsEqualIID(riid, &CLSID_ShellDesktop))
381           {
382             if (LoadStringA(shell32_hInstance, IDS_DESKTOP, szDest, buflen))
383               ret = TRUE;
384           }
385           else if (IsEqualIID(riid, &CLSID_MyComputer))
386           {
387             if(LoadStringA(shell32_hInstance, IDS_MYCOMPUTER, szDest, buflen))
388               ret = TRUE;
389           }
390         }
391
392         TRACE("-- %s\n", szDest);
393
394         return ret;
395 }
396
397 /***************************************************************************************
398 *       HCR_GetFolderAttributes [internal]
399 *
400 * gets the folder attributes of a class
401 *
402 * FIXME
403 *       verify the defaultvalue for *szDest
404 */
405 BOOL HCR_GetFolderAttributes (REFIID riid, LPDWORD szDest)
406 {       HKEY    hkey;
407         char    xriid[60];
408         DWORD   attributes;
409         DWORD   len = 4;
410
411         sprintf( xriid, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
412                  riid->Data1, riid->Data2, riid->Data3,
413                  riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
414                  riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7] );
415         TRACE("%s\n",xriid );
416
417         if (!szDest) return FALSE;
418         *szDest = SFGAO_FOLDER|SFGAO_FILESYSTEM;
419
420         strcat (xriid, "\\ShellFolder");
421
422         if (RegOpenKeyExA(HKEY_CLASSES_ROOT,xriid,0,KEY_READ,&hkey))
423         {
424           return FALSE;
425         }
426
427         if (RegQueryValueExA(hkey,"Attributes",0,NULL,(LPBYTE)&attributes,&len))
428         {
429           RegCloseKey(hkey);
430           return FALSE;
431         }
432
433         RegCloseKey(hkey);
434
435         TRACE("-- 0x%08lx\n", attributes);
436
437         *szDest = attributes;
438
439         return TRUE;
440 }