classes.c 9.71 KB
Newer Older
1
/*
2
 *	file type mapping
3 4
 *	(HKEY_CLASSES_ROOT - Stuff)
 *
5
 * Copyright 1998, 1999, 2000 Juergen Schmied
6
 *
7 8 9 10 11 12 13 14 15 16 17 18 19
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 */
21

Steven Edwards's avatar
Steven Edwards committed
22 23 24
#include "config.h"
#include "wine/port.h"

25 26
#include <stdlib.h>
#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 35
#include "shlguid.h"
#include "shresdef.h"
36
#include "shlwapi.h"
37
#include "wine/unicode.h"
38

39
WINE_DEFAULT_DEBUG_CHANNEL(shell);
40

41 42
#define MAX_EXTENSION_LENGTH 20

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
BOOL HCR_MapTypeToValueW(LPCWSTR szExtension, LPWSTR szFileType, DWORD len, BOOL bPrependDot)
{	
	HKEY	hkey;
	WCHAR	szTemp[MAX_EXTENSION_LENGTH + 2];

	TRACE("%s %p\n", debugstr_w(szExtension), debugstr_w(szFileType));

    /* added because we do not want to have double dots */
    if (szExtension[0] == '.')
        bPrependDot = 0;

	if (bPrependDot)
	  szTemp[0] = '.';

	lstrcpynW(szTemp + (bPrependDot?1:0), szExtension, MAX_EXTENSION_LENGTH);

	if (RegOpenKeyExW(HKEY_CLASSES_ROOT, szTemp, 0, 0x02000000, &hkey))
	{ 
	  return FALSE;
	}

	if (RegQueryValueW(hkey, NULL, szFileType, &len))
	{ 
	  RegCloseKey(hkey);
	  return FALSE;
	}

	RegCloseKey(hkey);

	TRACE("--UE;\n} %s\n", debugstr_w(szFileType));

	return TRUE;
}

BOOL HCR_MapTypeToValueA(LPCSTR szExtension, LPSTR szFileType, DWORD len, BOOL bPrependDot)
{
	HKEY	hkey;
80
	char	szTemp[MAX_EXTENSION_LENGTH + 2];
81

82
	TRACE("%s %p\n", szExtension, szFileType);
83

84
    /* added because we do not want to have double dots */
85 86
    if (szExtension[0] == '.')
        bPrependDot = 0;
87

88
	if (bPrependDot)
89
	  szTemp[0] = '.';
90

91
	lstrcpynA(szTemp + (bPrependDot?1:0), szExtension, MAX_EXTENSION_LENGTH);
92

93 94 95
	if (RegOpenKeyExA(HKEY_CLASSES_ROOT, szTemp, 0, 0x02000000, &hkey))
	{ 
	  return FALSE;
96 97
	}

98 99 100
	if (RegQueryValueA(hkey, NULL, szFileType, &len))
	{ 
	  RegCloseKey(hkey);
101
	  return FALSE;
102
	}
103 104 105

	RegCloseKey(hkey);

106
	TRACE("--UE;\n} %s\n", szFileType);
107 108 109

	return TRUE;
}
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132


BOOL HCR_GetExecuteCommandW(LPCWSTR szClass, LPCWSTR szVerb, LPWSTR szDest, DWORD len)
{
        static const WCHAR swShell[] = {'\\','s','h','e','l','l','\\',0};
        static const WCHAR swCommand[] = {'\\','c','o','m','m','a','n','d',0};
	WCHAR	sTemp[MAX_PATH];

	TRACE("%s %s %p\n",debugstr_w(szClass), debugstr_w(szVerb), szDest);

	lstrcpyW(sTemp, szClass);
	lstrcatW(sTemp, swShell);
	lstrcatW(sTemp, szVerb);
	lstrcatW(sTemp, swCommand);

	if (ERROR_SUCCESS == SHGetValueW(HKEY_CLASSES_ROOT, sTemp, NULL, NULL, szDest, &len)) {
	    TRACE("-- %s\n", debugstr_w(szDest) );
	    return TRUE;
	}
	return FALSE;
}

BOOL HCR_GetExecuteCommandA(LPCSTR szClass, LPCSTR szVerb, LPSTR szDest, DWORD len)
Juergen Schmied's avatar
Juergen Schmied committed
133 134
{
	char	sTemp[MAX_PATH];
135

136
	TRACE("%s %s\n",szClass, szVerb );
137

138
	snprintf(sTemp, MAX_PATH, "%s\\shell\\%s\\command",szClass, szVerb);
139

140 141 142
	if (ERROR_SUCCESS == SHGetValueA(HKEY_CLASSES_ROOT, sTemp, NULL, NULL, szDest, &len)) {
	    TRACE("-- %s\n", debugstr_a(szDest) );
	    return TRUE;
143
	}
144
	return FALSE;
145
}
146

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
BOOL HCR_GetExecuteCommandEx( HKEY hkeyClass, LPCSTR szClass, LPCSTR szVerb, LPSTR szDest, DWORD len )
{
	BOOL	ret = FALSE;

	TRACE("%p %s %s\n", hkeyClass, szClass, szVerb );

	if (szClass)
            RegOpenKeyExA(hkeyClass,szClass,0,0x02000000,&hkeyClass);

        if (hkeyClass)
	{
	    char sTemp[MAX_PATH];

	    snprintf(sTemp, MAX_PATH, "shell\\%s\\command", szVerb);

            ret = (ERROR_SUCCESS == SHGetValueA(hkeyClass, sTemp, NULL, NULL, szDest, &len));

	    if (szClass)
	       RegCloseKey(hkeyClass);
	}

	TRACE("-- %s\n", szDest );
	return ret;
}

172 173 174 175 176
/***************************************************************************************
*	HCR_GetDefaultIcon	[internal]
*
* Gets the icon for a filetype
*/
177
static BOOL HCR_RegOpenClassIDKey(REFIID riid, HKEY *hkey)
178 179 180 181 182 183 184 185 186
{
	char	xriid[50];
    sprintf( xriid, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
                 riid->Data1, riid->Data2, riid->Data3,
                 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
                 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7] );

 	TRACE("%s\n",xriid );

187
	return !RegOpenKeyExA(HKEY_CLASSES_ROOT, xriid, 0, KEY_READ, hkey);
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
}

static BOOL HCR_RegGetDefaultIconW(HKEY hkey, LPWSTR szDest, DWORD len, LPDWORD dwNr)
{
	DWORD dwType;
	WCHAR sTemp[MAX_PATH];
	WCHAR sNum[5];

	if (!RegQueryValueExW(hkey, NULL, 0, &dwType, (LPBYTE)szDest, &len))
	{
      if (dwType == REG_EXPAND_SZ)
	  {
	    ExpandEnvironmentStringsW(szDest, sTemp, MAX_PATH);
	    lstrcpynW(szDest, sTemp, len);
	  }
	  if (ParseFieldW (szDest, 2, sNum, 5))
             *dwNr = atoiW(sNum);
          else
             *dwNr=0; /* sometimes the icon number is missing */
	  ParseFieldW (szDest, 1, szDest, len);
	  return TRUE;
	}
	return FALSE;
}

static BOOL HCR_RegGetDefaultIconA(HKEY hkey, LPSTR szDest, DWORD len, LPDWORD dwNr)
{
	DWORD dwType;
	char sTemp[MAX_PATH];
	char  sNum[5];

	if (!RegQueryValueExA(hkey, NULL, 0, &dwType, szDest, &len))
	{
      if (dwType == REG_EXPAND_SZ)
	  {
	    ExpandEnvironmentStringsA(szDest, sTemp, MAX_PATH);
	    lstrcpynA(szDest, sTemp, len);
	  }
	  if (ParseFieldA (szDest, 2, sNum, 5))
             *dwNr=atoi(sNum);
          else
             *dwNr=0; /* sometimes the icon number is missing */
	  ParseFieldA (szDest, 1, szDest, len);
	  return TRUE;
	}
	return FALSE;
}

BOOL HCR_GetDefaultIconW(LPCWSTR szClass, LPWSTR szDest, DWORD len, LPDWORD dwNr)
{
        static const WCHAR swDefaultIcon[] = {'\\','D','e','f','a','u','l','t','I','c','o','n',0};
	HKEY	hkey;
	WCHAR	sTemp[MAX_PATH];
	BOOL	ret = FALSE;

	TRACE("%s\n",debugstr_w(szClass) );

	lstrcpynW(sTemp, szClass, MAX_PATH);
	lstrcatW(sTemp, swDefaultIcon);

	if (!RegOpenKeyExW(HKEY_CLASSES_ROOT, sTemp, 0, 0x02000000, &hkey))
	{
	  ret = HCR_RegGetDefaultIconW(hkey, szDest, len, dwNr);
	  RegCloseKey(hkey);
	}
	TRACE("-- %s %li\n", debugstr_w(szDest), *dwNr );
	return ret;
}

BOOL HCR_GetDefaultIconA(LPCSTR szClass, LPSTR szDest, DWORD len, LPDWORD dwNr)
Juergen Schmied's avatar
Juergen Schmied committed
258 259 260 261
{
	HKEY	hkey;
	char	sTemp[MAX_PATH];
	BOOL	ret = FALSE;
262

263
	TRACE("%s\n",szClass );
264 265 266

	sprintf(sTemp, "%s\\DefaultIcon",szClass);

267
	if (!RegOpenKeyExA(HKEY_CLASSES_ROOT, sTemp, 0, 0x02000000, &hkey))
Juergen Schmied's avatar
Juergen Schmied committed
268
	{
269
	  ret = HCR_RegGetDefaultIconA(hkey, szDest, len, dwNr);
Juergen Schmied's avatar
Juergen Schmied committed
270
	  RegCloseKey(hkey);
271
	}
272
	TRACE("-- %s %li\n", szDest, *dwNr );
Juergen Schmied's avatar
Juergen Schmied committed
273
	return ret;
274
}
275

276 277 278 279 280
BOOL HCR_GetDefaultIconFromGUIDW(REFIID riid, LPWSTR szDest, DWORD len, LPDWORD dwNr)
{
	HKEY	hkey;
	BOOL	ret = FALSE;

281
	if (HCR_RegOpenClassIDKey(riid, &hkey))
282 283 284 285 286 287 288 289
	{
	  ret = HCR_RegGetDefaultIconW(hkey, szDest, len, dwNr);
	  RegCloseKey(hkey);
	}
	TRACE("-- %s %li\n", debugstr_w(szDest), *dwNr );
	return ret;
}

290 291 292 293 294
/***************************************************************************************
*	HCR_GetClassName	[internal]
*
* Gets the name of a registred class
*/
295 296 297 298 299
static WCHAR swEmpty[] = {0};

BOOL HCR_GetClassNameW(REFIID riid, LPWSTR szDest, DWORD len)
{	
	HKEY	hkey;
300 301
	BOOL ret = FALSE;
	DWORD buflen = len;
302

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
 	szDest[0] = 0;
	if (HCR_RegOpenClassIDKey(riid, &hkey))
	{
	  if (!RegQueryValueExW(hkey, swEmpty, 0, NULL, (LPBYTE)szDest, &len))
	  {
	    ret = TRUE;
	  }
	  RegCloseKey(hkey);
	}

	if (!ret || !szDest[0])
	{
	  if(IsEqualIID(riid, &CLSID_ShellDesktop))
	  {
	    if (LoadStringW(shell32_hInstance, IDS_DESKTOP, szDest, buflen))
	      ret = TRUE;
	  }
	  else if (IsEqualIID(riid, &CLSID_MyComputer))
	  {
	    if(LoadStringW(shell32_hInstance, IDS_MYCOMPUTER, szDest, buflen))
	      ret = TRUE;
	  }
	}
	TRACE("-- %s\n", debugstr_w(szDest));
	return ret;
}
329

330 331 332 333
BOOL HCR_GetClassNameA(REFIID riid, LPSTR szDest, DWORD len)
{	HKEY	hkey;
	BOOL ret = FALSE;
	DWORD buflen = len;
334

Juergen Schmied's avatar
Juergen Schmied committed
335
	szDest[0] = 0;
336
	if (HCR_RegOpenClassIDKey(riid, &hkey))
337 338 339 340 341 342
	{
	  if (!RegQueryValueExA(hkey,"",0,NULL,szDest,&len))
	  {
	    ret = TRUE;
	  }
	  RegCloseKey(hkey);
343 344
	}

345
	if (!ret || !szDest[0])
346
	{
347 348
	  if(IsEqualIID(riid, &CLSID_ShellDesktop))
	  {
Juergen Schmied's avatar
Juergen Schmied committed
349 350
	    if (LoadStringA(shell32_hInstance, IDS_DESKTOP, szDest, buflen))
	      ret = TRUE;
351
	  }
352
	  else if (IsEqualIID(riid, &CLSID_MyComputer))
353
	  {
Juergen Schmied's avatar
Juergen Schmied committed
354 355
	    if(LoadStringA(shell32_hInstance, IDS_MYCOMPUTER, szDest, buflen))
	      ret = TRUE;
356
	  }
357
	}
358 359 360

	TRACE("-- %s\n", szDest);

361
	return ret;
362 363 364 365 366 367
}

/***************************************************************************************
*	HCR_GetFolderAttributes	[internal]
*
* gets the folder attributes of a class
368 369 370
*
* FIXME
*	verify the defaultvalue for *szDest
371 372 373 374 375 376 377
*/
BOOL HCR_GetFolderAttributes (REFIID riid, LPDWORD szDest)
{	HKEY	hkey;
	char	xriid[60];
	DWORD	attributes;
	DWORD	len = 4;

378 379 380 381
        sprintf( xriid, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
                 riid->Data1, riid->Data2, riid->Data3,
                 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
                 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7] );
382 383
	TRACE("%s\n",xriid );

384 385
	if (!szDest) return FALSE;
	*szDest = SFGAO_FOLDER|SFGAO_FILESYSTEM;
386

387 388 389 390 391 392 393 394 395 396 397
	strcat (xriid, "\\ShellFolder");

	if (RegOpenKeyExA(HKEY_CLASSES_ROOT,xriid,0,KEY_READ,&hkey))
	{
	  return FALSE;
	}

	if (RegQueryValueExA(hkey,"Attributes",0,NULL,(LPBYTE)&attributes,&len))
	{
	  RegCloseKey(hkey);
	  return FALSE;
398
	}
399 400 401 402 403 404 405 406

	RegCloseKey(hkey);

	TRACE("-- 0x%08lx\n", attributes);

	*szDest = attributes;

	return TRUE;
407
}