Portability fix.
[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 <stdio.h>
28 #include "wine/debug.h"
29 #include "winerror.h"
30 #include "winreg.h"
31
32 #include "shlobj.h"
33 #include "shell32_main.h"
34 #include "shlguid.h"
35 #include "shresdef.h"
36 #include "shlwapi.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(shell);
39
40 #define MAX_EXTENSION_LENGTH 20
41
42 BOOL HCR_MapTypeToValue ( LPCSTR szExtension, LPSTR szFileType, DWORD len, BOOL bPrependDot)
43 {       HKEY    hkey;
44         char    szTemp[MAX_EXTENSION_LENGTH + 2];
45
46         TRACE("%s %p\n",szExtension, szFileType );
47
48     /* added because we do not want to have double dots */
49     if (szExtension[0]=='.')
50         bPrependDot=0;
51
52         if (bPrependDot)
53           strcpy(szTemp, ".");
54
55         lstrcpynA(szTemp+((bPrependDot)?1:0), szExtension, MAX_EXTENSION_LENGTH);
56
57         if (RegOpenKeyExA(HKEY_CLASSES_ROOT,szTemp,0,0x02000000,&hkey))
58         { return FALSE;
59         }
60
61         if (RegQueryValueA(hkey,NULL,szFileType,&len))
62         { RegCloseKey(hkey);
63           return FALSE;
64         }
65
66         RegCloseKey(hkey);
67
68         TRACE("--UE;
69 } %s\n", szFileType );
70
71         return TRUE;
72 }
73 BOOL HCR_GetExecuteCommand ( LPCSTR szClass, LPCSTR szVerb, LPSTR szDest, DWORD len )
74 {
75         char    sTemp[MAX_PATH];
76
77         TRACE("%s %s\n",szClass, szVerb );
78
79         snprintf(sTemp, MAX_PATH, "%s\\shell\\%s\\command",szClass, szVerb);
80
81         if (ERROR_SUCCESS == SHGetValueA(HKEY_CLASSES_ROOT, sTemp, NULL, NULL, szDest, &len)) {
82             TRACE("-- %s\n", debugstr_a(szDest) );
83             return TRUE;
84         }
85         return FALSE;
86 }
87 /***************************************************************************************
88 *       HCR_GetDefaultIcon      [internal]
89 *
90 * Gets the icon for a filetype
91 */
92 BOOL HCR_GetDefaultIcon (LPCSTR szClass, LPSTR szDest, DWORD len, LPDWORD dwNr)
93 {
94         HKEY    hkey;
95         char    sTemp[MAX_PATH];
96         char    sNum[5];
97         DWORD   dwType;
98         BOOL    ret = FALSE;
99
100         TRACE("%s\n",szClass );
101
102         sprintf(sTemp, "%s\\DefaultIcon",szClass);
103
104         if (!RegOpenKeyExA(HKEY_CLASSES_ROOT,sTemp,0,0x02000000,&hkey))
105         {
106           if (!RegQueryValueExA(hkey, NULL, 0, &dwType, szDest, &len))
107           {
108             if (dwType == REG_EXPAND_SZ)
109             {
110               ExpandEnvironmentStringsA(szDest, sTemp, MAX_PATH);
111               strcpy(szDest, sTemp);
112             }
113             if (ParseFieldA (szDest, 2, sNum, 5))
114                *dwNr=atoi(sNum);
115             else
116                *dwNr=0; /* sometimes the icon number is missing */
117             ParseFieldA (szDest, 1, szDest, len);
118             ret = TRUE;
119           }
120           RegCloseKey(hkey);
121         }
122         TRACE("-- %s %li\n", szDest, *dwNr );
123         return ret;
124 }
125
126 /***************************************************************************************
127 *       HCR_GetClassName        [internal]
128 *
129 * Gets the name of a registred class
130 */
131 BOOL HCR_GetClassName (REFIID riid, LPSTR szDest, DWORD len)
132 {       HKEY    hkey;
133         char    xriid[50];
134         BOOL ret = FALSE;
135         DWORD buflen = len;
136
137         sprintf( xriid, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
138                  riid->Data1, riid->Data2, riid->Data3,
139                  riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
140                  riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7] );
141
142         TRACE("%s\n",xriid );
143
144         szDest[0] = 0;
145         if (!RegOpenKeyExA(HKEY_CLASSES_ROOT,xriid,0,KEY_READ,&hkey))
146         {
147           if (!RegQueryValueExA(hkey,"",0,NULL,szDest,&len))
148           {
149             ret = TRUE;
150           }
151           RegCloseKey(hkey);
152         }
153
154         if (!ret || !szDest[0])
155         {
156           if(IsEqualIID(riid, &CLSID_ShellDesktop))
157           {
158             if (LoadStringA(shell32_hInstance, IDS_DESKTOP, szDest, buflen))
159               ret = TRUE;
160           }
161           else if (IsEqualIID(riid, &CLSID_MyComputer))
162           {
163             if(LoadStringA(shell32_hInstance, IDS_MYCOMPUTER, szDest, buflen))
164               ret = TRUE;
165           }
166         }
167
168         TRACE("-- %s\n", szDest);
169
170         return ret;
171 }
172
173 /***************************************************************************************
174 *       HCR_GetFolderAttributes [internal]
175 *
176 * gets the folder attributes of a class
177 *
178 * FIXME
179 *       verify the defaultvalue for *szDest
180 */
181 BOOL HCR_GetFolderAttributes (REFIID riid, LPDWORD szDest)
182 {       HKEY    hkey;
183         char    xriid[60];
184         DWORD   attributes;
185         DWORD   len = 4;
186
187         sprintf( xriid, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
188                  riid->Data1, riid->Data2, riid->Data3,
189                  riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
190                  riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7] );
191         TRACE("%s\n",xriid );
192
193         if (!szDest) return FALSE;
194         *szDest = SFGAO_FOLDER|SFGAO_FILESYSTEM;
195
196         strcat (xriid, "\\ShellFolder");
197
198         if (RegOpenKeyExA(HKEY_CLASSES_ROOT,xriid,0,KEY_READ,&hkey))
199         {
200           return FALSE;
201         }
202
203         if (RegQueryValueExA(hkey,"Attributes",0,NULL,(LPBYTE)&attributes,&len))
204         {
205           RegCloseKey(hkey);
206           return FALSE;
207         }
208
209         RegCloseKey(hkey);
210
211         TRACE("-- 0x%08lx\n", attributes);
212
213         *szDest = attributes;
214
215         return TRUE;
216 }
217
218 typedef struct
219 {       ICOM_VFIELD(IQueryAssociations);
220         DWORD   ref;
221 } IQueryAssociationsImpl;
222
223 static struct ICOM_VTABLE(IQueryAssociations) qavt;
224
225 /**************************************************************************
226 *  IQueryAssociations_Constructor
227 */
228 IQueryAssociations* IQueryAssociations_Constructor(void)
229 {
230         IQueryAssociationsImpl* ei;
231
232         ei=(IQueryAssociationsImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IQueryAssociationsImpl));
233         ei->ref=1;
234         ICOM_VTBL(ei) = &qavt;
235
236         TRACE("(%p)\n",ei);
237         return (IQueryAssociations *)ei;
238 }
239 /**************************************************************************
240  *  IQueryAssociations_QueryInterface
241  */
242 static HRESULT WINAPI IQueryAssociations_fnQueryInterface(
243         IQueryAssociations * iface,
244         REFIID riid,
245         LPVOID *ppvObj)
246 {
247         ICOM_THIS(IQueryAssociationsImpl,iface);
248
249          TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
250
251         *ppvObj = NULL;
252
253         if(IsEqualIID(riid, &IID_IUnknown))             /*IUnknown*/
254         {
255           *ppvObj = This;
256         }
257         else if(IsEqualIID(riid, &IID_IQueryAssociations))      /*IExtractIcon*/
258         {
259           *ppvObj = (IQueryAssociations*)This;
260         }
261
262         if(*ppvObj)
263         {
264           IQueryAssociations_AddRef((IQueryAssociations*) *ppvObj);
265           TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
266           return S_OK;
267         }
268         TRACE("-- Interface: E_NOINTERFACE\n");
269         return E_NOINTERFACE;
270 }
271
272 /**************************************************************************
273 *  IQueryAssociations_AddRef
274 */
275 static ULONG WINAPI IQueryAssociations_fnAddRef(IQueryAssociations * iface)
276 {
277         ICOM_THIS(IQueryAssociationsImpl,iface);
278
279         TRACE("(%p)->(count=%lu)\n",This, This->ref );
280
281         return ++(This->ref);
282 }
283 /**************************************************************************
284 *  IQueryAssociations_Release
285 */
286 static ULONG WINAPI IQueryAssociations_fnRelease(IQueryAssociations * iface)
287 {
288         ICOM_THIS(IQueryAssociationsImpl,iface);
289
290         TRACE("(%p)->()\n",This);
291
292         if (!--(This->ref))
293         {
294           TRACE(" destroying IExtractIcon(%p)\n",This);
295           HeapFree(GetProcessHeap(),0,This);
296           return 0;
297         }
298         return This->ref;
299 }
300
301 static HRESULT WINAPI IQueryAssociations_fnInit(
302         IQueryAssociations * iface,
303         ASSOCF flags,
304         LPCWSTR pszAssoc,
305         HKEY hkProgid,
306         HWND hwnd)
307 {
308         return E_NOTIMPL;
309 }
310
311 static HRESULT WINAPI IQueryAssociations_fnGetString(
312         IQueryAssociations * iface,
313         ASSOCF flags,
314         ASSOCSTR str,
315         LPCWSTR pszExtra,
316         LPWSTR pszOut,
317         DWORD *pcchOut)
318 {
319         return E_NOTIMPL;
320 }
321
322 static HRESULT WINAPI IQueryAssociations_fnGetKey(
323         IQueryAssociations * iface,
324         ASSOCF flags,
325         ASSOCKEY key,
326         LPCWSTR pszExtra,
327         HKEY *phkeyOut)
328 {
329         return E_NOTIMPL;
330 }
331
332 static HRESULT WINAPI IQueryAssociations_fnGetData(
333         IQueryAssociations * iface,
334         ASSOCF flags,
335         ASSOCDATA data,
336         LPCWSTR pszExtra,
337         LPVOID pvOut,
338         DWORD *pcbOut)
339 {
340         return E_NOTIMPL;
341 }
342 static HRESULT WINAPI IQueryAssociations_fnGetEnum(
343         IQueryAssociations * iface,
344         ASSOCF flags,
345         ASSOCENUM assocenum,
346         LPCWSTR pszExtra,
347         REFIID riid,
348         LPVOID *ppvOut)
349 {
350         return E_NOTIMPL;
351 }
352
353 static struct ICOM_VTABLE(IQueryAssociations) qavt =
354 {
355         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
356         IQueryAssociations_fnQueryInterface,
357         IQueryAssociations_fnAddRef,
358         IQueryAssociations_fnRelease,
359         IQueryAssociations_fnInit,
360         IQueryAssociations_fnGetString,
361         IQueryAssociations_fnGetKey,
362         IQueryAssociations_fnGetData,
363         IQueryAssociations_fnGetEnum
364 };