classes.c 13.1 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
 * 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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 <stdarg.h>
28
#include <stdio.h>
29 30 31

#define COBJMACROS

32
#include "wine/debug.h"
33
#include "winerror.h"
34 35
#include "windef.h"
#include "winbase.h"
36
#include "winreg.h"
37 38
#include "wingdi.h"
#include "winuser.h"
39

40
#include "shlobj.h"
41
#include "shell32_main.h"
42 43
#include "shlguid.h"
#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 51
#define MAX_EXTENSION_LENGTH 20

Mike McCormack's avatar
Mike McCormack committed
52
BOOL HCR_MapTypeToValueW(LPCWSTR szExtension, LPWSTR szFileType, LONG len, BOOL bPrependDot)
53 54 55 56
{	
	HKEY	hkey;
	WCHAR	szTemp[MAX_EXTENSION_LENGTH + 2];

57
	TRACE("%s %p\n", debugstr_w(szExtension), szFileType);
58 59 60 61 62 63 64 65 66 67

    /* 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);

68
	if (RegOpenKeyExW(HKEY_CLASSES_ROOT, szTemp, 0, KEY_READ, &hkey))
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	{ 
	  return FALSE;
	}

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

	RegCloseKey(hkey);

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

	return TRUE;
}

Mike McCormack's avatar
Mike McCormack committed
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 95
    if (szExtension[0] == '.')
        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, KEY_READ, &hkey))
103 104
	{ 
	  return FALSE;
105 106
	}

107 108 109
	if (RegQueryValueA(hkey, NULL, szFileType, &len))
	{ 
	  RegCloseKey(hkey);
110
	  return FALSE;
111
	}
112 113 114

	RegCloseKey(hkey);

115
	TRACE("--UE;\n} %s\n", szFileType);
116 117 118

	return TRUE;
}
119

120 121 122 123 124 125 126 127 128 129 130 131
static const WCHAR swShell[] = {'s','h','e','l','l','\\',0};
static const WCHAR swOpen[] = {'o','p','e','n',0};
static const WCHAR swCommand[] = {'\\','c','o','m','m','a','n','d',0};

BOOL HCR_GetDefaultVerbW( HKEY hkeyClass, LPCWSTR szVerb, LPWSTR szDest, DWORD len )
{
        WCHAR sTemp[MAX_PATH];
        LONG size;
        HKEY hkey;

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

132
        if (szVerb && *szVerb)
133 134 135 136 137 138 139 140 141 142 143 144 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 172 173 174 175
        {
            lstrcpynW(szDest, szVerb, len);
            return TRUE;
        }

        size=len;
        *szDest='\0';
        if (!RegQueryValueW(hkeyClass, swShell, szDest, &size) && *szDest)
        {
            /* The MSDN says to first try the default verb */
            lstrcpyW(sTemp, swShell);
            lstrcatW(sTemp, szDest);
            lstrcatW(sTemp, swCommand);
            if (!RegOpenKeyExW(hkeyClass, sTemp, 0, 0, &hkey))
            {
                RegCloseKey(hkey);
                TRACE("default verb=%s\n", debugstr_w(szDest));
                return TRUE;
            }
        }

        /* then fallback to 'open' */
        lstrcpyW(sTemp, swShell);
        lstrcatW(sTemp, swOpen);
        lstrcatW(sTemp, swCommand);
        if (!RegOpenKeyExW(hkeyClass, sTemp, 0, 0, &hkey))
        {
            RegCloseKey(hkey);
            lstrcpynW(szDest, swOpen, len);
            TRACE("default verb=open\n");
            return TRUE;
        }

        /* and then just use the first verb on Windows >= 2000 */
        if (!RegEnumKeyW(hkeyClass, 0, szDest, len) && *szDest)
        {
            TRACE("default verb=first verb=%s\n", debugstr_w(szDest));
            return TRUE;
        }

        TRACE("no default verb!\n");
	return FALSE;
}
176

177
BOOL HCR_GetExecuteCommandW( HKEY hkeyClass, LPCWSTR szClass, LPCWSTR szVerb, LPWSTR szDest, DWORD len )
178
{
179 180
	WCHAR sTempVerb[MAX_PATH];
	BOOL ret;
181 182 183 184

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

	if (szClass)
185
            RegOpenKeyExW(HKEY_CLASSES_ROOT, szClass, 0, KEY_READ, &hkeyClass);
186 187 188 189
        if (!hkeyClass)
            return FALSE;
        ret = FALSE;

190
        if (HCR_GetDefaultVerbW(hkeyClass, szVerb, sTempVerb, sizeof(sTempVerb)/sizeof(sTempVerb[0])))
191 192 193 194 195 196 197 198 199
        {
            WCHAR sTemp[MAX_PATH];
            lstrcpyW(sTemp, swShell);
            lstrcatW(sTemp, sTempVerb);
            lstrcatW(sTemp, swCommand);
            ret = (ERROR_SUCCESS == SHGetValueW(hkeyClass, sTemp, NULL, NULL, szDest, &len));
        }
        if (szClass)
            RegCloseKey(hkeyClass);
200 201 202 203 204

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

205 206 207 208 209
/***************************************************************************************
*	HCR_GetDefaultIcon	[internal]
*
* Gets the icon for a filetype
*/
210
static BOOL HCR_RegOpenClassIDKey(REFIID riid, HKEY *hkey)
211 212
{
	char	xriid[50];
213
    sprintf( xriid, "CLSID\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
214 215 216 217 218 219
                 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 );

220
	return !RegOpenKeyExA(HKEY_CLASSES_ROOT, xriid, 0, KEY_READ, hkey);
221 222
}

223
static BOOL HCR_RegGetDefaultIconW(HKEY hkey, LPWSTR szDest, DWORD len, int* picon_idx)
224
{
225 226 227
    DWORD dwType;
    WCHAR sTemp[MAX_PATH];
    WCHAR sNum[5];
228

229 230
    if (!RegQueryValueExW(hkey, NULL, 0, &dwType, (LPBYTE)szDest, &len))
    {
231
      if (dwType == REG_EXPAND_SZ)
232 233 234 235 236 237
      {
        ExpandEnvironmentStringsW(szDest, sTemp, MAX_PATH);
        lstrcpynW(szDest, sTemp, len);
      }
      if (ParseFieldW (szDest, 2, sNum, 5))
             *picon_idx = atoiW(sNum);
238
          else
239 240
             *picon_idx=0; /* sometimes the icon number is missing */
      ParseFieldW (szDest, 1, szDest, len);
241
          PathUnquoteSpacesW(szDest);
242 243 244
      return TRUE;
    }
    return FALSE;
245 246
}

247
static BOOL HCR_RegGetDefaultIconA(HKEY hkey, LPSTR szDest, DWORD len, int* picon_idx)
248 249 250 251 252
{
	DWORD dwType;
	char sTemp[MAX_PATH];
	char  sNum[5];

253
	if (!RegQueryValueExA(hkey, NULL, 0, &dwType, (LPBYTE)szDest, &len))
254 255 256 257 258 259 260
	{
      if (dwType == REG_EXPAND_SZ)
	  {
	    ExpandEnvironmentStringsA(szDest, sTemp, MAX_PATH);
	    lstrcpynA(szDest, sTemp, len);
	  }
	  if (ParseFieldA (szDest, 2, sNum, 5))
261
             *picon_idx=atoi(sNum);
262
          else
263
             *picon_idx=0; /* sometimes the icon number is missing */
264
	  ParseFieldA (szDest, 1, szDest, len);
265
          PathUnquoteSpacesA(szDest);
266 267 268 269 270
	  return TRUE;
	}
	return FALSE;
}

271
BOOL HCR_GetDefaultIconW(LPCWSTR szClass, LPWSTR szDest, DWORD len, int* picon_idx)
272 273 274 275 276 277 278 279 280 281 282
{
        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);

283
	if (!RegOpenKeyExW(HKEY_CLASSES_ROOT, sTemp, 0, KEY_READ, &hkey))
284
	{
285
	  ret = HCR_RegGetDefaultIconW(hkey, szDest, len, picon_idx);
286 287
	  RegCloseKey(hkey);
	}
288 289

        if(ret)
290
            TRACE("-- %s %i\n", debugstr_w(szDest), *picon_idx);
291 292 293
        else
            TRACE("-- not found\n");

294 295 296
	return ret;
}

297
BOOL HCR_GetDefaultIconA(LPCSTR szClass, LPSTR szDest, DWORD len, int* picon_idx)
Juergen Schmied's avatar
Juergen Schmied committed
298 299 300 301
{
	HKEY	hkey;
	char	sTemp[MAX_PATH];
	BOOL	ret = FALSE;
302

303
	TRACE("%s\n",szClass );
304 305 306

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

307
	if (!RegOpenKeyExA(HKEY_CLASSES_ROOT, sTemp, 0, KEY_READ, &hkey))
Juergen Schmied's avatar
Juergen Schmied committed
308
	{
309
	  ret = HCR_RegGetDefaultIconA(hkey, szDest, len, picon_idx);
Juergen Schmied's avatar
Juergen Schmied committed
310
	  RegCloseKey(hkey);
311
	}
312
	TRACE("-- %s %i\n", szDest, *picon_idx);
Juergen Schmied's avatar
Juergen Schmied committed
313
	return ret;
314
}
315

316 317 318
/***************************************************************************************
*	HCR_GetClassName	[internal]
*
319
* Gets the name of a registered class
320
*/
321
static const WCHAR swEmpty[] = {0};
322 323 324 325

BOOL HCR_GetClassNameW(REFIID riid, LPWSTR szDest, DWORD len)
{	
	HKEY	hkey;
326 327
	BOOL ret = FALSE;
	DWORD buflen = len;
328

329 330 331
 	szDest[0] = 0;
	if (HCR_RegOpenClassIDKey(riid, &hkey))
	{
332 333 334 335 336
          static const WCHAR wszLocalizedString[] = 
            { 'L','o','c','a','l','i','z','e','d','S','t','r','i','n','g', 0 };
          if (!RegLoadMUIStringW(hkey, wszLocalizedString, szDest, len, NULL, 0, NULL) ||
              !RegQueryValueExW(hkey, swEmpty, 0, NULL, (LPBYTE)szDest, &len))
          {
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
	    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;
}
358

359 360 361 362
BOOL HCR_GetClassNameA(REFIID riid, LPSTR szDest, DWORD len)
{	HKEY	hkey;
	BOOL ret = FALSE;
	DWORD buflen = len;
363

Juergen Schmied's avatar
Juergen Schmied committed
364
	szDest[0] = 0;
365
	if (HCR_RegOpenClassIDKey(riid, &hkey))
366
	{
367 368 369
          if (!RegLoadMUIStringA(hkey,"LocalizedString",szDest,len,NULL,0,NULL) ||
              !RegQueryValueExA(hkey,"",0,NULL,(LPBYTE)szDest,&len))
          {
370 371 372
	    ret = TRUE;
	  }
	  RegCloseKey(hkey);
373 374
	}

375
	if (!ret || !szDest[0])
376
	{
377 378
	  if(IsEqualIID(riid, &CLSID_ShellDesktop))
	  {
Juergen Schmied's avatar
Juergen Schmied committed
379 380
	    if (LoadStringA(shell32_hInstance, IDS_DESKTOP, szDest, buflen))
	      ret = TRUE;
381
	  }
382
	  else if (IsEqualIID(riid, &CLSID_MyComputer))
383
	  {
Juergen Schmied's avatar
Juergen Schmied committed
384 385
	    if(LoadStringA(shell32_hInstance, IDS_MYCOMPUTER, szDest, buflen))
	      ret = TRUE;
386
	  }
387
	}
388

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

391
	return ret;
392 393
}

394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
/******************************************************************************
 * HCR_GetFolderAttributes [Internal]
 *
 * Query the registry for a shell folders' attributes
 *
 * PARAMS
 *  pidlFolder    [I]  A simple pidl of type PT_GUID. 
 *  pdwAttributes [IO] In: Attributes to be queried, OUT: Resulting attributes.
 *
 * RETURNS
 *  TRUE:  Found information for the attributes in the registry
 *  FALSE: No attribute information found
 *
 * NOTES
 *  If queried for an attribute, which is set in the CallForAttributes registry
 *  value, the function binds to the shellfolder objects and queries it.
 */
BOOL HCR_GetFolderAttributes(LPCITEMIDLIST pidlFolder, LPDWORD pdwAttributes)
{
    HKEY hSFKey;
    LPOLESTR pwszCLSID;
    LONG lResult;
    DWORD dwTemp, dwLen;
    static const WCHAR wszAttributes[] = { 'A','t','t','r','i','b','u','t','e','s',0 };
    static const WCHAR wszCallForAttributes[] = { 
        'C','a','l','l','F','o','r','A','t','t','r','i','b','u','t','e','s',0 };
    WCHAR wszShellFolderKey[] = { 'C','L','S','I','D','\\','{','0','0','0','2','1','4','0','0','-',
        '0','0','0','0','-','0','0','0','0','-','C','0','0','0','-','0','0','0','0','0','0','0',
        '0','0','0','4','6','}','\\','S','h','e','l','l','F','o','l','d','e','r',0 };

    TRACE("(pidlFolder=%p, pdwAttributes=%p)\n", pidlFolder, pdwAttributes);
       
    if (!_ILIsPidlSimple(pidlFolder)) { 
427 428
        static int firstHit = 1;
        if (firstHit) {
429
            ERR("should be called for simple PIDL's only!\n");
430 431
            firstHit = 0;
        }
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
        return FALSE;
    }
    
    if (!_ILIsDesktop(pidlFolder)) {
        if (FAILED(StringFromCLSID(_ILGetGUIDPointer(pidlFolder), &pwszCLSID))) return FALSE;
        memcpy(&wszShellFolderKey[6], pwszCLSID, 38 * sizeof(WCHAR));
        CoTaskMemFree(pwszCLSID);
    }
    
    lResult = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszShellFolderKey, 0, KEY_READ, &hSFKey);
    if (lResult != ERROR_SUCCESS) return FALSE;
    
    dwLen = sizeof(DWORD);
    lResult = RegQueryValueExW(hSFKey, wszCallForAttributes, 0, NULL, (LPBYTE)&dwTemp, &dwLen);
    if ((lResult == ERROR_SUCCESS) && (dwTemp & *pdwAttributes)) {
        LPSHELLFOLDER psfDesktop, psfFolder;
        HRESULT hr;

        RegCloseKey(hSFKey);
        hr = SHGetDesktopFolder(&psfDesktop);
        if (SUCCEEDED(hr)) {
            hr = IShellFolder_BindToObject(psfDesktop, pidlFolder, NULL, &IID_IShellFolder, 
                                           (LPVOID*)&psfFolder);
            if (SUCCEEDED(hr)) { 
                hr = IShellFolder_GetAttributesOf(psfFolder, 0, NULL, pdwAttributes);
457
                IShellFolder_Release(psfFolder);
458
            }
459
            IShellFolder_Release(psfDesktop);
460 461 462 463 464 465 466 467 468 469 470 471
        }
        if (FAILED(hr)) return FALSE;
    } else {
        lResult = RegQueryValueExW(hSFKey, wszAttributes, 0, NULL, (LPBYTE)&dwTemp, &dwLen);
        RegCloseKey(hSFKey);
        if (lResult == ERROR_SUCCESS) {
            *pdwAttributes &= dwTemp;
        } else {
            return FALSE;
        }
    }

472
    TRACE("-- *pdwAttributes == 0x%08x\n", *pdwAttributes);
473 474

    return TRUE;
475
}