Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
6.4 KiB

  1. //
  2. // native.cpp in shell\lib
  3. //
  4. // common Utility functions that need to be compiled for
  5. // both UNICODE and ANSI
  6. //
  7. #include "stock.h"
  8. #pragma hdrstop
  9. #include "shellp.h"
  10. #include <regstr.h>
  11. // get the name and flags of an absolute IDlist
  12. // in:
  13. // dwFlags SHGDN_ flags as hints to the name space GetDisplayNameOf() function
  14. //
  15. // in/out:
  16. // *pdwAttribs (optional) return flags
  17. STDAPI SHGetNameAndFlags(LPCITEMIDLIST pidl, DWORD dwFlags, LPTSTR pszName, UINT cchName, DWORD *pdwAttribs)
  18. {
  19. if (pszName)
  20. {
  21. *pszName = 0;
  22. }
  23. HRESULT hrInit = SHCoInitialize();
  24. IShellFolder *psf;
  25. LPCITEMIDLIST pidlLast;
  26. HRESULT hr = SHBindToIDListParent(pidl, IID_PPV_ARG(IShellFolder, &psf), &pidlLast);
  27. if (SUCCEEDED(hr))
  28. {
  29. if (pszName)
  30. hr = DisplayNameOf(psf, pidlLast, dwFlags, pszName, cchName);
  31. if (SUCCEEDED(hr) && pdwAttribs)
  32. {
  33. RIP(*pdwAttribs); // this is an in-out param
  34. *pdwAttribs = SHGetAttributes(psf, pidlLast, *pdwAttribs);
  35. }
  36. psf->Release();
  37. }
  38. SHCoUninitialize(hrInit);
  39. return hr;
  40. }
  41. STDAPI_(DWORD) GetUrlScheme(LPCTSTR pszUrl)
  42. {
  43. if (pszUrl)
  44. {
  45. PARSEDURL pu;
  46. pu.cbSize = sizeof(pu);
  47. if (SUCCEEDED(ParseURL(pszUrl, &pu)))
  48. return pu.nScheme;
  49. }
  50. return URL_SCHEME_INVALID;
  51. }
  52. //
  53. // returns
  54. //
  55. // TRUE if the key is present and nonzero.
  56. // FALSE if the key is present and zero.
  57. // -1 if the key is not present.
  58. //
  59. BOOL GetExplorerUserSetting(HKEY hkeyRoot, LPCTSTR pszSubKey, LPCTSTR pszValue)
  60. {
  61. TCHAR szPath[MAX_PATH];
  62. TCHAR szPathExplorer[MAX_PATH];
  63. DWORD cbSize = ARRAYSIZE(szPath);
  64. DWORD dwType;
  65. PathCombine(szPathExplorer, REGSTR_PATH_EXPLORER, pszSubKey);
  66. if (ERROR_SUCCESS == SHGetValue(hkeyRoot, szPathExplorer, pszValue,
  67. &dwType, szPath, &cbSize))
  68. {
  69. // Zero in the DWORD case or NULL in the string case
  70. // indicates that this item is not available.
  71. if (dwType == REG_DWORD)
  72. return *((DWORD*)szPath) != 0;
  73. else
  74. return (TCHAR)szPath[0] != 0;
  75. }
  76. return -1;
  77. }
  78. //
  79. // This function allows a feature to be controlled by both a user setting
  80. // or a policy restriction. The policy restriction is first checked.
  81. //
  82. // If the value is 1, then the action is restricted.
  83. // If the value is 2, then the action is allowed.
  84. // If the value is absent or 0, then we look at the user setting.
  85. //
  86. // If the user setting is present, then ROUS_KEYALLOWS and ROUS_KEYRESTRICTS
  87. // controls the return value. ROUS_KEYALLOWS means that a nonzero user
  88. // setting allows the action. ROUS_KEYRESTRICTS means that a nonzero user
  89. // setting restricts the action.
  90. //
  91. // If the user setting is absent, then ROUS_DEFAULTALLOW and
  92. // ROUS_DESFAULTRESTRICT controls the default return value.
  93. //
  94. STDAPI_(BOOL) IsRestrictedOrUserSetting(HKEY hkeyRoot, RESTRICTIONS rest, LPCTSTR pszSubKey, LPCTSTR pszValue, UINT flags)
  95. {
  96. // See if the system policy restriction trumps
  97. DWORD dwRest = SHRestricted(rest);
  98. if (dwRest == 1)
  99. return TRUE;
  100. if (dwRest == 2)
  101. return FALSE;
  102. //
  103. // Restriction not in place or defers to user setting.
  104. //
  105. BOOL fValidKey = GetExplorerUserSetting(hkeyRoot, pszSubKey, pszValue);
  106. switch (fValidKey)
  107. {
  108. case 0: // Key is present and zero
  109. if (flags & ROUS_KEYRESTRICTS)
  110. return FALSE; // restriction not present
  111. else
  112. return TRUE; // ROUS_KEYALLOWS, value is 0 -> restricted
  113. case 1: // Key is present and nonzero
  114. if (flags & ROUS_KEYRESTRICTS)
  115. return TRUE; // restriction present -> restricted
  116. else
  117. return FALSE; // ROUS_KEYALLOWS, value is 1 -> not restricted
  118. default:
  119. ASSERT(0); // _GetExplorerUserSetting returns exactly 0, 1 or -1.
  120. // Fall through
  121. case -1: // Key is not present
  122. return (flags & ROUS_DEFAULTRESTRICT);
  123. }
  124. /*NOTREACHED*/
  125. }
  126. //
  127. // Repair font attributes that don't work on certain languages.
  128. //
  129. STDAPI_(void) SHAdjustLOGFONT(IN OUT LOGFONT *plf)
  130. {
  131. ASSERT(plf);
  132. //
  133. // FE fonts don't look good in bold since the glyphs are intricate
  134. // and converting them to bold turns them into a black blob.
  135. //
  136. if (plf->lfCharSet == SHIFTJIS_CHARSET||
  137. plf->lfCharSet == HANGEUL_CHARSET ||
  138. plf->lfCharSet == GB2312_CHARSET ||
  139. plf->lfCharSet == CHINESEBIG5_CHARSET)
  140. {
  141. if (plf->lfWeight > FW_NORMAL)
  142. plf->lfWeight = FW_NORMAL;
  143. }
  144. }
  145. //
  146. // Some of our registry keys were used prior to MUI, so for compat
  147. // reasons, apps have to put non-MUI strings there; otherwise,
  148. // downlevel clients will display at-signs which is kinda ugly.
  149. //
  150. // So the solution for these keys is to store the non-MUI string
  151. // in the legacy location, but put the MUI version in the
  152. // "LocalizedString" value.
  153. //
  154. STDAPI SHLoadLegacyRegUIString(HKEY hk, LPCTSTR pszSubkey, LPTSTR pszOutBuf, UINT cchOutBuf)
  155. {
  156. HKEY hkClose = NULL;
  157. ASSERT(cchOutBuf);
  158. pszOutBuf[0] = TEXT('\0');
  159. if (pszSubkey && *pszSubkey)
  160. {
  161. DWORD dwError = RegOpenKeyEx(hk, pszSubkey, 0, KEY_QUERY_VALUE, &hkClose);
  162. if (dwError != ERROR_SUCCESS)
  163. {
  164. return HRESULT_FROM_WIN32(dwError);
  165. }
  166. hk = hkClose;
  167. }
  168. HRESULT hr = SHLoadRegUIString(hk, TEXT("LocalizedString"), pszOutBuf, cchOutBuf);
  169. if (FAILED(hr) || pszOutBuf[0] == TEXT('\0'))
  170. {
  171. hr = SHLoadRegUIString(hk, TEXT(""), pszOutBuf, cchOutBuf);
  172. }
  173. if (hkClose)
  174. {
  175. RegCloseKey(hkClose);
  176. }
  177. return hr;
  178. }
  179. STDAPI_(TCHAR) SHFindMnemonic(LPCTSTR psz)
  180. {
  181. ASSERT(psz);
  182. TCHAR tchDefault = *psz; // Default is first character
  183. LPCTSTR pszAmp;
  184. while ((pszAmp = StrChr(psz, TEXT('&'))) != NULL)
  185. {
  186. switch (pszAmp[1])
  187. {
  188. case TEXT('&'): // Skip over &&
  189. psz = pszAmp + 2;
  190. continue;
  191. case TEXT('\0'): // Ignore trailing ampersand
  192. return tchDefault;
  193. default:
  194. return pszAmp[1];
  195. }
  196. }
  197. return tchDefault;
  198. }