Source code of Windows XP (NT5)
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.

1681 lines
57 KiB

  1. #include "stdafx.h"
  2. #include "specfldr.h"
  3. #include "hostutil.h"
  4. #include "rcids.h" // for IDM_PROGRAMS etc.
  5. #include "ras.h"
  6. #include "raserror.h"
  7. #include "netcon.h"
  8. #include "netconp.h"
  9. #include <cowsite.h>
  10. //
  11. // This definition is stolen from shell32\unicpp\dcomp.h
  12. //
  13. #define REGSTR_PATH_HIDDEN_DESKTOP_ICONS_STARTPANEL \
  14. REGSTR_PATH_EXPLORER TEXT("\\HideDesktopIcons\\NewStartPanel")
  15. HRESULT CRecentShellMenuCallback_CreateInstance(IShellMenuCallback **ppsmc);
  16. HRESULT CNoSubdirShellMenuCallback_CreateInstance(IShellMenuCallback **ppsmc);
  17. HRESULT CMyComputerShellMenuCallback_CreateInstance(IShellMenuCallback **ppsmc);
  18. HRESULT CNoFontsShellMenuCallback_CreateInstance(IShellMenuCallback **ppsmc);
  19. HRESULT CConnectToShellMenuCallback_CreateInstance(IShellMenuCallback **ppsmc);
  20. LPTSTR _Static_LoadString(const struct SpecialFolderDesc *pdesc);
  21. BOOL ShouldShowWindowsSecurity();
  22. BOOL ShouldShowOEMLink();
  23. typedef HRESULT (CALLBACK *CREATESHELLMENUCALLBACK)(IShellMenuCallback **ppsmc);
  24. typedef BOOL (CALLBACK *SHOULDSHOWFOLDERCALLBACK)();
  25. EXTERN_C HINSTANCE hinstCabinet;
  26. void ShowFolder(UINT csidl);
  27. BOOL IsNetConPidlRAS(IShellFolder2 *psfNetCon, LPCITEMIDLIST pidlNetConItem);
  28. //****************************************************************************
  29. //
  30. // SpecialFolderDesc
  31. //
  32. // Describes a special folder.
  33. //
  34. #define SFD_SEPARATOR ((LPTSTR)-1)
  35. // Flags for SpecialFolderDesc._uFlags
  36. enum {
  37. // These values are collectively known as the
  38. // "display mode" and match the values set by regtreeop.
  39. SFD_HIDE = 0x0000,
  40. SFD_SHOW = 0x0001,
  41. SFD_CASCADE = 0x0002,
  42. SFD_MODEMASK = 0x0003,
  43. SFD_DROPTARGET = 0x0004,
  44. SFD_CANCASCADE = 0x0008,
  45. SFD_FORCECASCADE = 0x0010,
  46. SFD_BOLD = 0x0020,
  47. SFD_WASSHOWN = 0x0040,
  48. SFD_PREFIX = 0x0080,
  49. SFD_USEBGTHREAD = 0x0100,
  50. };
  51. struct SpecialFolderDesc {
  52. typedef BOOL (SpecialFolderDesc::*CUSTOMFOLDERNAMECALLBACK)(LPTSTR *ppsz) const;
  53. LPCTSTR _pszTarget; // or MAKEINTRESOURCE(csidl)
  54. RESTRICTIONS _rest; // optional restriction
  55. LPCTSTR _pszShow; // REGSTR_EXPLORER_ADVANCED!_pszShow
  56. UINT _uFlags; // SFD_* values
  57. CREATESHELLMENUCALLBACK _CreateShellMenuCallback; // Which IShellMenuCallback do we want?
  58. LPCTSTR _pszCustomizeKey; // Optional location where customizations are saved
  59. DWORD _dwShellFolderFlags; // Optional restrictions for cascading folder
  60. UINT _idsCustomName; // Optional override (CUSTOMFOLDERNAMECALLBACK)
  61. UINT _iToolTip; // Optional resource ID for a custom tooltip
  62. CUSTOMFOLDERNAMECALLBACK _CustomName; // Over-ride the filesys name
  63. SHOULDSHOWFOLDERCALLBACK _ShowFolder;
  64. LPCTSTR _pszCanHideOnDesktop; // Optional {guid} that controls desktop visibility
  65. DWORD GetDisplayMode(BOOL *pbIgnoreRule) const;
  66. void AdjustForSKU();
  67. void SetDefaultDisplayMode(UINT iNewMode)
  68. {
  69. _uFlags = (_uFlags & ~SFD_MODEMASK) | iNewMode;
  70. }
  71. BOOL IsDropTarget() const { return _uFlags & SFD_DROPTARGET; }
  72. BOOL IsCacheable() const { return _uFlags & SFD_USEBGTHREAD; }
  73. int IsCSIDL() const { return IS_INTRESOURCE(_pszTarget); }
  74. BOOL IsBold() const { return _uFlags & SFD_BOLD; }
  75. int IsSeparator() const { return _pszTarget == SFD_SEPARATOR; }
  76. int GetCSIDL() const {
  77. ASSERT(IsCSIDL());
  78. return (short)PtrToLong(_pszTarget);
  79. }
  80. HRESULT CreateShellMenuCallback(IShellMenuCallback **ppsmc) const {
  81. return _CreateShellMenuCallback ? _CreateShellMenuCallback(ppsmc) : S_OK;
  82. }
  83. BOOL GetCustomName(LPTSTR *ppsz) const {
  84. if (_CustomName)
  85. return (this->*_CustomName)(ppsz);
  86. else
  87. return FALSE;
  88. }
  89. LPWSTR GetShowCacheRegName() const;
  90. BOOL LoadStringAsOLESTR(LPTSTR *ppsz) const;
  91. BOOL ConnectToName(LPTSTR *ppsz) const;
  92. };
  93. static SpecialFolderDesc s_rgsfd[] = {
  94. /* My Documents */
  95. {
  96. MAKEINTRESOURCE(CSIDL_PERSONAL), // pszTarget
  97. REST_NOSMMYDOCS, // restriction
  98. REGSTR_VAL_DV2_SHOWMYDOCS,
  99. SFD_SHOW | SFD_DROPTARGET | SFD_CANCASCADE | SFD_BOLD,
  100. // show by default, is drop target
  101. NULL, // no custom cascade
  102. NULL, // no drag/drop customization
  103. 0, // no special flags for cascaded menu
  104. 0, // (no custom name)
  105. IDS_CUSTOMTIP_MYDOCS,
  106. NULL, // (no custom name)
  107. NULL, // (no custom display rule)
  108. TEXT("{450D8FBA-AD25-11D0-98A8-0800361B1103}"), // desktop visibility control
  109. },
  110. /* Recent */
  111. {
  112. MAKEINTRESOURCE(CSIDL_RECENT), // pszTarget
  113. REST_NORECENTDOCSMENU, // restriction
  114. REGSTR_VAL_DV2_SHOWRECDOCS, // customize show
  115. SFD_HIDE | SFD_CANCASCADE | SFD_BOLD | SFD_PREFIX, // hide by default
  116. CRecentShellMenuCallback_CreateInstance, // custom callback
  117. NULL, // no drag/drop customization
  118. SMINIT_RESTRICT_DRAGDROP, // disallow drag/drop in cascaded menu
  119. IDS_STARTPANE_RECENT, // override filesys name
  120. IDS_CUSTOMTIP_RECENT,
  121. &SpecialFolderDesc::LoadStringAsOLESTR, // override filesys name with _idsCustomName
  122. NULL, // (no custom display rule)
  123. NULL, // (no desktop visibility control)
  124. },
  125. /* My Pictures */
  126. {
  127. MAKEINTRESOURCE(CSIDL_MYPICTURES), // pszTarget
  128. REST_NOSMMYPICS, // restriction
  129. REGSTR_VAL_DV2_SHOWMYPICS,
  130. SFD_SHOW | SFD_DROPTARGET | SFD_CANCASCADE | SFD_BOLD,
  131. // show by default, is drop target
  132. NULL, // no custom cascade
  133. NULL, // no drag/drop customization
  134. 0, // no special flags for cascaded menu
  135. 0, // (no custom name)
  136. IDS_CUSTOMTIP_MYPICS,
  137. NULL, // (no custom name)
  138. NULL, // (no custom display rule)
  139. NULL, // (no desktop visibility control)
  140. },
  141. /* My Music */
  142. {
  143. MAKEINTRESOURCE(CSIDL_MYMUSIC), // pszTarget
  144. REST_NOSMMYMUSIC, // restriction
  145. REGSTR_VAL_DV2_SHOWMYMUSIC,
  146. SFD_SHOW | SFD_DROPTARGET | SFD_CANCASCADE | SFD_BOLD,
  147. // show by default, is drop target
  148. NULL, // no custom cascade
  149. NULL, // no drag/drop customization
  150. 0, // no special flags for cascaded menu
  151. 0, // (no custom name)
  152. IDS_CUSTOMTIP_MYMUSIC,
  153. NULL, // (no custom name)
  154. NULL, // (no custom display rule)
  155. NULL, // (no desktop visibility control)
  156. },
  157. /* Favorites */
  158. {
  159. MAKEINTRESOURCE(CSIDL_FAVORITES), // pszTarget
  160. REST_NOFAVORITESMENU, // restriction
  161. REGSTR_VAL_DV2_FAVORITES, // customize show (shared w/classic)
  162. SFD_HIDE | SFD_DROPTARGET |
  163. SFD_CANCASCADE | SFD_FORCECASCADE | SFD_BOLD | SFD_PREFIX,
  164. // hide by default, is drop target
  165. NULL, // unrestricted cascading
  166. STRREG_FAVORITES, // drag/drop customization key
  167. 0, // no special flags for cascaded menu
  168. IDS_STARTPANE_FAVORITES, // override filesys name
  169. 0, // no custom tip
  170. &SpecialFolderDesc::LoadStringAsOLESTR, // override filesys name with _idsCustomName
  171. NULL, // (no custom display rule)
  172. NULL, // (no desktop visibility control)
  173. },
  174. /* My Computer */
  175. {
  176. MAKEINTRESOURCE(CSIDL_DRIVES), // pszTarget
  177. REST_NOMYCOMPUTERICON, // restriction
  178. REGSTR_VAL_DV2_SHOWMC, // customize show
  179. SFD_SHOW | SFD_CANCASCADE | SFD_BOLD, // show by default
  180. CMyComputerShellMenuCallback_CreateInstance, // custom callback
  181. NULL, // no drag/drop customization
  182. 0, // no special flags for cascaded menu
  183. 0, // (no custom name)
  184. IDS_CUSTOMTIP_MYCOMP,
  185. NULL, // (no custom name)
  186. NULL, // (no custom display rule)
  187. TEXT("{20D04FE0-3AEA-1069-A2D8-08002B30309D}"), // desktop visibility control
  188. },
  189. /* My Network Places */
  190. {
  191. MAKEINTRESOURCE(CSIDL_NETWORK), // pszTarget
  192. REST_NOSMNETWORKPLACES, // restriction
  193. REGSTR_VAL_DV2_SHOWNETPL, // customize show
  194. SFD_SHOW | SFD_CANCASCADE | SFD_BOLD | SFD_USEBGTHREAD, // show by default
  195. CNoSubdirShellMenuCallback_CreateInstance, // only cascade one level
  196. NULL, // no drag/drop customization
  197. 0, // no special flags for cascaded menu
  198. 0, // (no custom name)
  199. IDS_CUSTOMTIP_MYNETPLACES,
  200. NULL, // (no custom name)
  201. ShouldShowNetPlaces,
  202. TEXT("{208D2C60-3AEA-1069-A2D7-08002B30309D}"), // desktop visibility control
  203. },
  204. /* Separator line */
  205. {
  206. SFD_SEPARATOR, // separator
  207. REST_NONE, // no restriction
  208. NULL, // no customize show
  209. SFD_SHOW, // show by default
  210. NULL, // (not cascadable)
  211. NULL, // (not cascadable)
  212. 0, // (not cascadable)
  213. 0, // (no custom name)
  214. 0, // no custom tip
  215. NULL, // (no custom name)
  216. NULL, // (no custom display rule)
  217. NULL, // (no desktop visibility control)
  218. },
  219. /* Control Panel */
  220. {
  221. MAKEINTRESOURCE(CSIDL_CONTROLS), // pszTarget
  222. REST_NOCONTROLPANEL, // restriction
  223. REGSTR_VAL_DV2_SHOWCPL,
  224. SFD_SHOW | SFD_CANCASCADE | SFD_PREFIX, // show by default
  225. CNoFontsShellMenuCallback_CreateInstance, // custom callback
  226. NULL, // no drag/drop customization
  227. 0, // no special flags for cascaded menu
  228. IDS_STARTPANE_CONTROLPANEL, // override filesys name
  229. IDS_CUSTOMTIP_CTRLPANEL, // no custom tip
  230. &SpecialFolderDesc::LoadStringAsOLESTR, // override filesys name with _idsCustomName
  231. NULL, // (no custom display rule)
  232. NULL, // (no desktop visibility control)
  233. },
  234. /* Admin Tools */
  235. {
  236. // Using the ::{guid} gets the icon right
  237. TEXT("shell:::{D20EA4E1-3957-11d2-A40B-0C5020524153}"), // pszTarget
  238. REST_NONE, // no restriction
  239. REGSTR_VAL_DV2_ADMINTOOLSROOT,
  240. SFD_HIDE | SFD_CANCASCADE | SFD_FORCECASCADE, // hide by default, force to cascade
  241. NULL, // no custom callback
  242. NULL, // no drag/drop customization
  243. 0, // no special flags for cascaded menu
  244. NULL, // no custom name
  245. NULL, // no custom tip
  246. NULL, // no custom name
  247. NULL, // (no custom display rule)
  248. NULL, // (no desktop visibility control)
  249. },
  250. /* Network Connections */
  251. {
  252. MAKEINTRESOURCE(CSIDL_CONNECTIONS), // pszTarget
  253. REST_NONETWORKCONNECTIONS, // restriction
  254. REGSTR_VAL_DV2_SHOWNETCONN, // customize show
  255. SFD_CASCADE | SFD_CANCASCADE | SFD_PREFIX | SFD_USEBGTHREAD, // cascade by default
  256. CConnectToShellMenuCallback_CreateInstance, // do special Connect To filtering
  257. NULL, // no drag/drop customization
  258. 0, // no special flags for cascaded menu
  259. IDS_STARTPANE_CONNECTTO, // override filesys name
  260. IDS_CUSTOMTIP_CONNECTTO,
  261. &SpecialFolderDesc::ConnectToName, // override filesys name with _idsCustomName
  262. ShouldShowConnectTo, // see if we should be shown
  263. NULL, // (no desktop visibility control)
  264. },
  265. /* Printers */
  266. {
  267. MAKEINTRESOURCE(CSIDL_PRINTERS), // pszTarget
  268. REST_NONE, // no restriction
  269. REGSTR_VAL_DV2_SHOWPRINTERS, // customize show
  270. SFD_HIDE, // hide by default, can't cascade
  271. NULL, // (not cascadable)
  272. NULL, // no drag/drop customization
  273. 0, // no special flags for cascaded menu
  274. 0, // (no custom name)
  275. 0, // no custom tip
  276. NULL, // (no custom name)
  277. NULL, // (no custom display rule)
  278. NULL, // (no desktop visibility control)
  279. },
  280. /* Separator line */
  281. {
  282. SFD_SEPARATOR, // separator
  283. REST_NONE, // no restriction
  284. NULL, // no customize show
  285. SFD_SHOW, // show by default
  286. NULL, // (not cascadable)
  287. NULL, // (not cascadable)
  288. 0, // (not cascadable)
  289. 0, // (no custom name)
  290. 0, // no custom tip
  291. NULL, // (no custom name)
  292. NULL, // (no custom display rule)
  293. NULL, // (no desktop visibility control)
  294. },
  295. /* Help */
  296. {
  297. TEXT("shell:::{2559a1f1-21d7-11d4-bdaf-00c04f60b9f0}"), // pszTarget
  298. REST_NOSMHELP, // restriction
  299. REGSTR_VAL_DV2_SHOWHELP, // customize show
  300. SFD_SHOW | SFD_PREFIX, // show by default, use & prefix
  301. NULL, // (not cascadable)
  302. NULL, // (not cascadable)
  303. 0, // (not cascadable)
  304. 0, // (no custom name)
  305. 0, // no custom tip
  306. NULL, // (no custom name)
  307. NULL, // (no custom display rule)
  308. NULL, // (no desktop visibility control)
  309. },
  310. /* Search */
  311. {
  312. TEXT("shell:::{2559a1f0-21d7-11d4-bdaf-00c04f60b9f0}"), // pszTarget
  313. REST_NOFIND, // restriction
  314. REGSTR_VAL_DV2_SHOWSEARCH, // customize show
  315. SFD_SHOW | SFD_PREFIX, // show by default, use & prefix
  316. NULL, // (not cascadable)
  317. NULL, // (not cascadable)
  318. 0, // (not cascadable)
  319. 0, // (no custom name)
  320. 0, // no custom tip
  321. NULL, // (no custom name)
  322. NULL, // (no custom display rule)
  323. NULL, // (no desktop visibility control)
  324. },
  325. /* Run */
  326. {
  327. TEXT("shell:::{2559a1f3-21d7-11d4-bdaf-00c04f60b9f0}"), // pszTarget
  328. REST_NORUN, // restriction
  329. REGSTR_VAL_DV2_SHOWRUN, // customize show
  330. SFD_SHOW | SFD_PREFIX, // show by default, use & prefix
  331. NULL, // (not cascadable)
  332. NULL, // (not cascadable)
  333. 0, // (not cascadable)
  334. 0, // (no custom name)
  335. 0, // no custom tip
  336. NULL, // (no custom name)
  337. NULL, // (no custom display rule)
  338. NULL, // (no desktop visibility control)
  339. },
  340. /* Separator line */
  341. {
  342. SFD_SEPARATOR, // separator
  343. REST_NONE, // no restriction
  344. NULL, // no customize show
  345. SFD_SHOW, // show by default
  346. NULL, // (not cascadable)
  347. NULL, // (not cascadable)
  348. 0, // (not cascadable)
  349. 0, // (no custom name)
  350. 0, // no custom tip
  351. NULL, // (no custom name)
  352. NULL, // (no custom display rule)
  353. NULL, // (no desktop visibility control)
  354. },
  355. /* Windows Security */
  356. {
  357. TEXT("shell:::{2559a1f2-21d7-11d4-bdaf-00c04f60b9f0}"), // pszTarget
  358. REST_NOSECURITY, // restriction
  359. NULL, // not customizable
  360. SFD_SHOW | SFD_PREFIX, // show by default, use & prefix
  361. NULL, // (not cascadable)
  362. NULL, // (not cascadable)
  363. 0, // (not cascadable)
  364. 0, // (no custom name)
  365. 0, // no custom tip
  366. NULL, // (no custom name)
  367. ShouldShowWindowsSecurity, // custom display rule
  368. NULL, // (no desktop visibility control)
  369. },
  370. /* OEM Command */
  371. {
  372. TEXT("shell:::{2559a1f6-21d7-11d4-bdaf-00c04f60b9f0}"), // pszTarget
  373. REST_NONE, // no restriction
  374. REGSTR_VAL_DV2_SHOWOEM, // customizable
  375. SFD_SHOW | SFD_PREFIX, // show by default, use & prefix
  376. NULL, // (not cascadable)
  377. NULL, // (not cascadable)
  378. 0, // (not cascadable)
  379. 0, // (no custom name)
  380. 0, // no custom tip
  381. NULL, // (no custom name)
  382. ShouldShowOEMLink, // custom display rule
  383. NULL, // (no desktop visibility control)
  384. },
  385. };
  386. void SpecialFolderDesc::AdjustForSKU()
  387. {
  388. if (IsCSIDL())
  389. {
  390. switch (GetCSIDL())
  391. {
  392. case CSIDL_MYPICTURES:
  393. case CSIDL_MYMUSIC:
  394. SetDefaultDisplayMode(IsOS(OS_ANYSERVER) ? SFD_HIDE : SFD_SHOW);
  395. break;
  396. case CSIDL_RECENT:
  397. SetDefaultDisplayMode(IsOS(OS_PERSONAL) ? SFD_HIDE : SFD_CASCADE);
  398. break;
  399. case CSIDL_PRINTERS:
  400. SetDefaultDisplayMode(IsOS(OS_PERSONAL) ? SFD_HIDE : SFD_SHOW);
  401. break;
  402. }
  403. }
  404. }
  405. LPWSTR SpecialFolderDesc::GetShowCacheRegName() const
  406. {
  407. const WCHAR szCached[] = L"_ShouldShow";
  408. WCHAR *pszShowCache = (WCHAR *)LocalAlloc(LPTR, ((lstrlenW(_pszShow)+1) * sizeof (WCHAR)) + sizeof(szCached));
  409. if (pszShowCache)
  410. {
  411. StrCpy(pszShowCache, _pszShow);
  412. StrCat(pszShowCache, szCached);
  413. }
  414. return pszShowCache;
  415. }
  416. //
  417. // First try to read the display mode from the registry.
  418. // Failing that, use the default value.
  419. // Also fill in whether to ignore the custom display rule or not
  420. //
  421. DWORD SpecialFolderDesc::GetDisplayMode(BOOL *pbIgnoreRule) const
  422. {
  423. *pbIgnoreRule = FALSE;
  424. // Restrictions always take top priority
  425. if (SHRestricted(_rest))
  426. {
  427. return SFD_HIDE;
  428. }
  429. DWORD dwMode = _uFlags & SFD_MODEMASK;
  430. // See if there is a user setting to override
  431. if (_pszShow)
  432. {
  433. DWORD dwNewMode, cb = sizeof(DWORD);
  434. if (SHRegGetUSValue(REGSTR_EXPLORER_ADVANCED, _pszShow, NULL, &dwNewMode, &cb, FALSE, NULL, 0) == ERROR_SUCCESS)
  435. {
  436. // User has forced show or forced no-show
  437. // Do not call the custom show logic
  438. dwMode = dwNewMode;
  439. *pbIgnoreRule = TRUE;
  440. }
  441. else
  442. {
  443. WCHAR *pszShowCache = GetShowCacheRegName();
  444. if (pszShowCache)
  445. {
  446. if (SHGetValue(HKEY_CURRENT_USER, REGSTR_EXPLORER_ADVANCED, pszShowCache, NULL, &dwNewMode, &cb) == ERROR_SUCCESS)
  447. {
  448. dwMode = dwNewMode;
  449. }
  450. LocalFree(pszShowCache);
  451. }
  452. }
  453. }
  454. //
  455. // Some items are cascade-only (Favorites).
  456. // Others never cascade (Run).
  457. //
  458. // Enforce those rules here.
  459. //
  460. if (dwMode == SFD_CASCADE && !(_uFlags & SFD_CANCASCADE))
  461. {
  462. dwMode = SFD_SHOW;
  463. }
  464. else if (dwMode == SFD_SHOW && (_uFlags & SFD_FORCECASCADE))
  465. {
  466. dwMode = SFD_CASCADE;
  467. }
  468. return dwMode;
  469. }
  470. //****************************************************************************
  471. //
  472. // SpecialFolderListItem
  473. //
  474. // A PaneItem for the benefit of SFTBarHost.
  475. //
  476. class SpecialFolderListItem : public PaneItem
  477. {
  478. public:
  479. LPITEMIDLIST _pidl; // Full Pidl to each item
  480. const SpecialFolderDesc *_psfd; // Describes this item
  481. TCHAR _chMnem; // Keyboard accelerator
  482. LPTSTR _pszDispName; // Display name
  483. HICON _hIcon; // Icon
  484. SpecialFolderListItem(const SpecialFolderDesc *psfd) : _pidl(NULL), _psfd(psfd)
  485. {
  486. if (_psfd->IsSeparator())
  487. {
  488. // Make sure that SFD_SEPARATOR isn't accidentally recognized
  489. // as a separator.
  490. ASSERT(!_psfd->IsCSIDL());
  491. _iPinPos = PINPOS_SEPARATOR;
  492. }
  493. else if (_psfd->IsCSIDL())
  494. {
  495. SHGetSpecialFolderLocation(NULL, _psfd->GetCSIDL(), &_pidl);
  496. }
  497. else
  498. {
  499. SHILCreateFromPath(_psfd->_pszTarget, &_pidl, NULL);
  500. }
  501. };
  502. ~SpecialFolderListItem()
  503. {
  504. ILFree(_pidl);
  505. if (_hIcon)
  506. {
  507. DestroyIcon(_hIcon);
  508. }
  509. SHFree(_pszDispName);
  510. };
  511. void ReplaceLastPidlElement(LPITEMIDLIST pidlNew)
  512. {
  513. ASSERT(ILFindLastID(pidlNew) == pidlNew); // the ILAppend below won't work otherwise
  514. ILRemoveLastID(_pidl);
  515. LPITEMIDLIST pidlCombined = ILAppendID(_pidl, &pidlNew->mkid, TRUE);
  516. if (pidlCombined)
  517. _pidl = pidlCombined;
  518. }
  519. //
  520. // Values that are derived from CSIDL values need to be revalidated
  521. // because the user can rename a special folder, and we need to track
  522. // it to its new location.
  523. //
  524. BOOL IsStillValid()
  525. {
  526. BOOL fValid = TRUE;
  527. if (_psfd->IsCSIDL())
  528. {
  529. LPITEMIDLIST pidlNew;
  530. if (SHGetSpecialFolderLocation(NULL, _psfd->GetCSIDL(), &pidlNew) == S_OK)
  531. {
  532. UINT cbSizeNew = ILGetSize(pidlNew);
  533. if (cbSizeNew != ILGetSize(_pidl) ||
  534. memcmp(_pidl, pidlNew, cbSizeNew) != 0)
  535. {
  536. fValid = FALSE;
  537. }
  538. ILFree(pidlNew);
  539. }
  540. }
  541. return fValid;
  542. }
  543. };
  544. SpecialFolderList::~SpecialFolderList()
  545. {
  546. }
  547. HRESULT SpecialFolderList::Initialize()
  548. {
  549. for(int i=0;i < ARRAYSIZE(s_rgsfd); i++)
  550. s_rgsfd[i].AdjustForSKU();
  551. return S_OK;
  552. }
  553. // return TRUE if there are the requisite number of kids
  554. BOOL MinKidsHelper(UINT csidl, BOOL bOnlyRASCON, DWORD dwMinKids)
  555. {
  556. DWORD dwCount = 0;
  557. IShellFolder2 *psf;
  558. LPITEMIDLIST pidlBind = NULL;
  559. if (SHGetSpecialFolderLocation(NULL, csidl, &pidlBind) == S_OK)
  560. {
  561. if (SUCCEEDED(SHBindToObjectEx(NULL, pidlBind, NULL, IID_PPV_ARG(IShellFolder2, &psf))))
  562. {
  563. IEnumIDList *penum;
  564. if (S_OK == psf->EnumObjects(NULL, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &penum))
  565. {
  566. LPITEMIDLIST pidl;
  567. ULONG celt;
  568. while (S_OK == penum->Next(1, &pidl, &celt))
  569. {
  570. if (bOnlyRASCON)
  571. {
  572. ASSERT(csidl == CSIDL_CONNECTIONS); // we better be in the net con folder
  573. if (IsNetConPidlRAS(psf, pidl))
  574. dwCount++;
  575. }
  576. else
  577. dwCount++;
  578. SHFree(pidl);
  579. if (dwCount >= dwMinKids)
  580. break;
  581. }
  582. penum->Release();
  583. }
  584. psf->Release();
  585. }
  586. ILFree(pidlBind);
  587. }
  588. return dwCount >= dwMinKids;
  589. }
  590. BOOL ShouldShowNetPlaces()
  591. {
  592. return MinKidsHelper(CSIDL_NETHOOD, FALSE, 1); // see bug 317893 for details on when to show net places
  593. }
  594. BOOL ShouldShowConnectTo()
  595. {
  596. return MinKidsHelper(CSIDL_CONNECTIONS, TRUE, 1); // see bug 226855 (and the associated spec) for when to show Connect To
  597. }
  598. BOOL ShouldShowWindowsSecurity()
  599. {
  600. return SHGetMachineInfo(GMI_TSCLIENT);
  601. }
  602. BOOL ShouldShowOEMLink()
  603. {
  604. // Only show the OEM link if the OPK tool has added the appropriate registry entries...
  605. BOOL bRet = FALSE;
  606. HKEY hk;
  607. if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_CLASSES_ROOT, TEXT("CLSID\\{2559a1f6-21d7-11d4-bdaf-00c04f60b9f0}"), 0, KEY_READ, &hk))
  608. {
  609. DWORD cb;
  610. // Check to make sure its got a name, and a the parameter is registered properly...
  611. if (ERROR_SUCCESS == RegQueryValue(hk, NULL, NULL, (LONG*) &cb) &&
  612. ERROR_SUCCESS == SHGetValue(hk, TEXT("Instance\\InitPropertyBag"), TEXT("Param1"), NULL, NULL, &cb))
  613. {
  614. bRet = TRUE;
  615. }
  616. RegCloseKey(hk);
  617. }
  618. return bRet;
  619. }
  620. DWORD WINAPI SpecialFolderList::_HasEnoughChildrenThreadProc(void *pvData)
  621. {
  622. SpecialFolderList *pThis = reinterpret_cast<SpecialFolderList *>(pvData);
  623. HRESULT hr = SHCoInitialize();
  624. if (SUCCEEDED(hr))
  625. {
  626. DWORD dwIndex;
  627. for (dwIndex = 0; dwIndex < ARRAYSIZE(s_rgsfd); dwIndex++)
  628. {
  629. const SpecialFolderDesc *pdesc = &s_rgsfd[dwIndex];
  630. BOOL bIgnoreRule;
  631. DWORD dwMode = pdesc->GetDisplayMode(&bIgnoreRule);
  632. if (pdesc->IsCacheable() && pdesc->_ShowFolder)
  633. {
  634. ASSERT(pdesc->_pszShow);
  635. // We need to recount now
  636. if (!bIgnoreRule && pdesc->_ShowFolder())
  637. {
  638. // We have enough kids
  639. // Let's see if the state changed from last time
  640. if (!(dwMode & SFD_WASSHOWN))
  641. {
  642. WCHAR *pszShowCache = pdesc->GetShowCacheRegName();
  643. if (pszShowCache)
  644. {
  645. dwMode |= SFD_WASSHOWN;
  646. SHSetValue(HKEY_CURRENT_USER, REGSTR_EXPLORER_ADVANCED, pszShowCache, REG_DWORD, &dwMode, sizeof(dwMode));
  647. pThis->Invalidate();
  648. LocalFree(pszShowCache);
  649. }
  650. }
  651. continue;
  652. }
  653. // just create the item to get a pidl for it....
  654. SpecialFolderListItem *pitem = new SpecialFolderListItem(pdesc);
  655. if (pitem && pitem->_pidl)
  656. {
  657. // We don't have enough kids but we might gain them dynamically.
  658. // Register for notifications that can indicate that there are new
  659. // items.
  660. ASSERT(pThis->_cNotify < SFTHOST_MAXNOTIFY);
  661. if (pThis->RegisterNotify(pThis->_cNotify, SHCNE_CREATE | SHCNE_MKDIR | SHCNE_UPDATEDIR,
  662. pitem->_pidl, FALSE))
  663. {
  664. pThis->_cNotify++;
  665. }
  666. }
  667. delete pitem;
  668. // Let's see if the state changed from last time
  669. if (dwMode & SFD_WASSHOWN)
  670. {
  671. // Reset it to the default
  672. WCHAR *pszShowCache = pdesc->GetShowCacheRegName();
  673. if (pszShowCache)
  674. {
  675. SHDeleteValue(HKEY_CURRENT_USER, REGSTR_EXPLORER_ADVANCED, pszShowCache);
  676. pThis->Invalidate();
  677. LocalFree(pszShowCache);
  678. }
  679. }
  680. }
  681. }
  682. pThis->Release();
  683. }
  684. SHCoUninitialize(hr);
  685. return 0;
  686. }
  687. BOOL ShouldShowItem(const SpecialFolderDesc *pdesc, BOOL bIgnoreRule, DWORD dwMode)
  688. {
  689. if (bIgnoreRule)
  690. return TRUE; // registry is over-riding whatever special rules exist...
  691. // if we've got a special rule, then the background thread will check it, so return false for now unless we showed it last time
  692. if (pdesc->_ShowFolder)
  693. {
  694. if (pdesc->IsCacheable())
  695. {
  696. if (dwMode & SFD_WASSHOWN)
  697. {
  698. // Last time we looked, there were enough kids so let's assume it hasn't changed for now
  699. return TRUE;
  700. }
  701. return FALSE;
  702. }
  703. else
  704. {
  705. return pdesc->_ShowFolder();
  706. }
  707. }
  708. return TRUE;
  709. }
  710. void SpecialFolderList::EnumItems()
  711. {
  712. // Clean out any previous register notifies.
  713. UINT id;
  714. for (id = 0; id < _cNotify; id++)
  715. {
  716. UnregisterNotify(id);
  717. }
  718. _cNotify = 0;
  719. // Start background enum for the MinKids since they can get hung up on the network
  720. AddRef();
  721. if (!SHQueueUserWorkItem(SpecialFolderList::_HasEnoughChildrenThreadProc, this, 0, 0, NULL, NULL, 0))
  722. {
  723. Release();
  724. }
  725. DWORD dwIndex;
  726. // Restrictions may result in an entire section disappearing,
  727. // so don't create two separators in a row. Preinitialize to TRUE
  728. // so we don't get separators at the top of the list.
  729. BOOL fIgnoreSeparators = TRUE;
  730. int iItems=0;
  731. for (dwIndex = 0; dwIndex < ARRAYSIZE(s_rgsfd); dwIndex++)
  732. {
  733. const SpecialFolderDesc *pdesc = &s_rgsfd[dwIndex];
  734. BOOL bIgnoreRule;
  735. DWORD dwMode = pdesc->GetDisplayMode(&bIgnoreRule);
  736. if (dwMode != SFD_HIDE)
  737. {
  738. SpecialFolderListItem *pitem = new SpecialFolderListItem(pdesc);
  739. if (pitem)
  740. {
  741. if ((pitem->IsSeparator() && !fIgnoreSeparators) ||
  742. (pitem->_pidl && ShouldShowItem(pdesc, bIgnoreRule, dwMode)))
  743. {
  744. if ((dwMode & SFD_MODEMASK) == SFD_CASCADE)
  745. {
  746. pitem->EnableCascade();
  747. }
  748. if (pdesc->IsDropTarget())
  749. {
  750. pitem->EnableDropTarget();
  751. }
  752. // Get the icon and display name now.
  753. if (!pitem->IsSeparator())
  754. {
  755. IShellFolder *psf;
  756. LPCITEMIDLIST pidlItem;
  757. HRESULT hr = SHBindToIDListParent(pitem->_pidl, IID_PPV_ARG(IShellFolder, &psf), &pidlItem);
  758. if (SUCCEEDED(hr))
  759. {
  760. if (!pitem->_psfd->GetCustomName(&pitem->_pszDispName))
  761. pitem->_pszDispName = _DisplayNameOf(psf, pidlItem, SHGDN_NORMAL);
  762. pitem->_hIcon = _IconOf(psf, pidlItem, _cxIcon);
  763. psf->Release();
  764. }
  765. }
  766. fIgnoreSeparators = pitem->IsSeparator();
  767. // add the item
  768. AddItem(pitem, NULL, pitem->_pidl);
  769. if (!pitem->IsSeparator())
  770. iItems++;
  771. }
  772. else
  773. delete pitem;
  774. }
  775. }
  776. }
  777. SetDesiredSize(0, iItems);
  778. }
  779. int SpecialFolderList::AddImageForItem(PaneItem *p, IShellFolder *psf, LPCITEMIDLIST pidl, int iPos)
  780. {
  781. int iIcon = -1; // assume no icon
  782. SpecialFolderListItem *pitem = static_cast<SpecialFolderListItem *>(p);
  783. if (pitem->_hIcon)
  784. {
  785. iIcon = AddImage(pitem->_hIcon);
  786. DestroyIcon(pitem->_hIcon);
  787. pitem->_hIcon = NULL;
  788. }
  789. return iIcon;
  790. }
  791. LPTSTR SpecialFolderList::DisplayNameOfItem(PaneItem *p, IShellFolder *psf, LPCITEMIDLIST pidlItem, SHGNO shgno)
  792. {
  793. LPTSTR psz = NULL;
  794. SpecialFolderListItem *pitem = static_cast<SpecialFolderListItem *>(p);
  795. if (shgno == SHGDN_NORMAL && pitem->_pszDispName)
  796. {
  797. // We are going to transfer ownership
  798. psz = pitem->_pszDispName;
  799. pitem->_pszDispName = NULL;
  800. }
  801. else
  802. {
  803. if (!pitem->_psfd->GetCustomName(&psz))
  804. {
  805. psz = SFTBarHost::DisplayNameOfItem(p, psf, pidlItem, shgno);
  806. }
  807. }
  808. if ((pitem->_psfd->_uFlags & SFD_PREFIX) && psz)
  809. {
  810. SHFree(pitem->_pszAccelerator);
  811. pitem->_pszAccelerator = NULL;
  812. SHStrDup(psz, &pitem->_pszAccelerator); // if it fails, then tough, no mnemonic
  813. pitem->_chMnem = CharUpperChar(SHStripMneumonic(psz));
  814. }
  815. return psz;
  816. }
  817. int SpecialFolderList::CompareItems(PaneItem *p1, PaneItem *p2)
  818. {
  819. // SpecialFolderListItem *pitem1 = static_cast<SpecialFolderListItem *>(p1);
  820. // SpecialFolderListItem *pitem2 = static_cast<SpecialFolderListItem *>(p2);
  821. return 0; // we added them in the right order the first time
  822. }
  823. HRESULT SpecialFolderList::GetFolderAndPidl(PaneItem *p,
  824. IShellFolder **ppsfOut, LPCITEMIDLIST *ppidlOut)
  825. {
  826. SpecialFolderListItem *pitem = static_cast<SpecialFolderListItem *>(p);
  827. return SHBindToIDListParent(pitem->_pidl, IID_PPV_ARG(IShellFolder, ppsfOut), ppidlOut);
  828. }
  829. void SpecialFolderList::GetItemInfoTip(PaneItem *p, LPTSTR pszText, DWORD cch)
  830. {
  831. SpecialFolderListItem *pitem = (SpecialFolderListItem*)p;
  832. if (pitem->_psfd->_iToolTip)
  833. LoadString(_Module.GetResourceInstance(), pitem->_psfd->_iToolTip, pszText, cch);
  834. else
  835. SFTBarHost::GetItemInfoTip(p, pszText, cch); // call the base class
  836. }
  837. HRESULT SpecialFolderList::ContextMenuRenameItem(PaneItem *p, LPCTSTR ptszNewName)
  838. {
  839. SpecialFolderListItem *pitem = (SpecialFolderListItem*)p;
  840. IShellFolder *psf;
  841. LPCITEMIDLIST pidlItem;
  842. HRESULT hr = GetFolderAndPidl(pitem, &psf, &pidlItem);
  843. if (SUCCEEDED(hr))
  844. {
  845. LPITEMIDLIST pidlNew;
  846. hr = psf->SetNameOf(_hwnd, pidlItem, ptszNewName, SHGDN_INFOLDER, &pidlNew);
  847. if (SUCCEEDED(hr))
  848. {
  849. pitem->ReplaceLastPidlElement(pidlNew);
  850. }
  851. psf->Release();
  852. }
  853. return hr;
  854. }
  855. //
  856. // If we get any changenotify, it means that somebody added (or thought about
  857. // adding) an item to one of our minkids folders, so we'll have to look to see
  858. // if it crossed the minkids threshold.
  859. //
  860. void SpecialFolderList::OnChangeNotify(UINT id, LONG lEvent, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
  861. {
  862. Invalidate();
  863. for (id = 0; id < _cNotify; id++)
  864. {
  865. UnregisterNotify(id);
  866. }
  867. _cNotify = 0;
  868. PostMessage(_hwnd, SFTBM_REFRESH, TRUE, 0);
  869. }
  870. BOOL SpecialFolderList::IsItemStillValid(PaneItem *p)
  871. {
  872. SpecialFolderListItem *pitem = static_cast<SpecialFolderListItem *>(p);
  873. return pitem->IsStillValid();
  874. }
  875. BOOL SpecialFolderList::IsBold(PaneItem *p)
  876. {
  877. SpecialFolderListItem *pitem = static_cast<SpecialFolderListItem *>(p);
  878. return pitem->_psfd->IsBold();
  879. }
  880. HRESULT SpecialFolderList::GetCascadeMenu(PaneItem *p, IShellMenu **ppsm)
  881. {
  882. SpecialFolderListItem *pitem = static_cast<SpecialFolderListItem *>(p);
  883. IShellFolder *psf;
  884. HRESULT hr = SHBindToObjectEx(NULL, pitem->_pidl, NULL, IID_PPV_ARG(IShellFolder, &psf));
  885. if (SUCCEEDED(hr))
  886. {
  887. IShellMenu *psm;
  888. hr = CoCreateInstance(CLSID_MenuBand, NULL, CLSCTX_INPROC_SERVER,
  889. IID_PPV_ARG(IShellMenu, &psm));
  890. if (SUCCEEDED(hr))
  891. {
  892. //
  893. // Recent Documents requires special treatment.
  894. //
  895. IShellMenuCallback *psmc = NULL;
  896. hr = pitem->_psfd->CreateShellMenuCallback(&psmc);
  897. if (SUCCEEDED(hr))
  898. {
  899. DWORD dwFlags = SMINIT_TOPLEVEL | SMINIT_VERTICAL | pitem->_psfd->_dwShellFolderFlags;
  900. if (IsRestrictedOrUserSetting(HKEY_CURRENT_USER, REST_NOCHANGESTARMENU,
  901. TEXT("Advanced"), TEXT("Start_EnableDragDrop"),
  902. ROUS_DEFAULTALLOW | ROUS_KEYALLOWS))
  903. {
  904. dwFlags |= SMINIT_RESTRICT_DRAGDROP | SMINIT_RESTRICT_CONTEXTMENU;
  905. }
  906. psm->Initialize(psmc, 0, 0, dwFlags);
  907. HKEY hkCustom = NULL;
  908. if (pitem->_psfd->_pszCustomizeKey)
  909. {
  910. RegCreateKeyEx(HKEY_CURRENT_USER, pitem->_psfd->_pszCustomizeKey,
  911. NULL, NULL, REG_OPTION_NON_VOLATILE,
  912. KEY_READ | KEY_WRITE, NULL, &hkCustom, NULL);
  913. }
  914. dwFlags = SMSET_USEBKICONEXTRACTION;
  915. hr = psm->SetShellFolder(psf, pitem->_pidl, hkCustom, dwFlags);
  916. if (SUCCEEDED(hr))
  917. {
  918. // SetShellFolder takes ownership of hkCustom
  919. *ppsm = psm;
  920. psm->AddRef();
  921. }
  922. else
  923. {
  924. // Clean up the registry key since SetShellFolder
  925. // did not take ownership
  926. if (hkCustom)
  927. {
  928. RegCloseKey(hkCustom);
  929. }
  930. }
  931. ATOMICRELEASE(psmc); // psmc can be NULL
  932. }
  933. psm->Release();
  934. }
  935. psf->Release();
  936. }
  937. return hr;
  938. }
  939. TCHAR SpecialFolderList::GetItemAccelerator(PaneItem *p, int iItemStart)
  940. {
  941. SpecialFolderListItem *pitem = static_cast<SpecialFolderListItem *>(p);
  942. if (pitem->_chMnem)
  943. {
  944. return pitem->_chMnem;
  945. }
  946. else
  947. {
  948. // Default: First letter is accelerator.
  949. return SFTBarHost::GetItemAccelerator(p, iItemStart);
  950. }
  951. }
  952. LRESULT SpecialFolderList::OnWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  953. {
  954. switch (uMsg)
  955. {
  956. case WM_NOTIFY:
  957. switch (((NMHDR*)(lParam))->code)
  958. {
  959. // When the user connects/disconnects via TS, we need to recalc
  960. // the "Windows Security" item
  961. case SMN_REFRESHLOGOFF:
  962. Invalidate();
  963. break;
  964. }
  965. }
  966. // Else fall back to parent implementation
  967. return SFTBarHost::OnWndProc(hwnd, uMsg, wParam, lParam);
  968. }
  969. BOOL _IsItemHiddenOnDesktop(LPCTSTR pszGuid)
  970. {
  971. return SHRegGetBoolUSValue(REGSTR_PATH_HIDDEN_DESKTOP_ICONS_STARTPANEL,
  972. pszGuid, FALSE, FALSE);
  973. }
  974. UINT SpecialFolderList::AdjustDeleteMenuItem(PaneItem *p, UINT *puiFlags)
  975. {
  976. SpecialFolderListItem *pitem = static_cast<SpecialFolderListItem *>(p);
  977. if (pitem->_psfd->_pszCanHideOnDesktop)
  978. {
  979. // Set MF_CHECKED if the item is visible on the desktop
  980. if (!_IsItemHiddenOnDesktop(pitem->_psfd->_pszCanHideOnDesktop))
  981. {
  982. // Item is visible - show the checkbox
  983. *puiFlags |= MF_CHECKED;
  984. }
  985. return IDS_SFTHOST_SHOWONDESKTOP;
  986. }
  987. else
  988. {
  989. return 0; // not deletable
  990. }
  991. }
  992. HRESULT SpecialFolderList::ContextMenuInvokeItem(PaneItem *p, IContextMenu *pcm, CMINVOKECOMMANDINFOEX *pici, LPCTSTR pszVerb)
  993. {
  994. SpecialFolderListItem *pitem = static_cast<SpecialFolderListItem *>(p);
  995. HRESULT hr;
  996. if (StrCmpIC(pszVerb, TEXT("delete")) == 0)
  997. {
  998. ASSERT(pitem->_psfd->_pszCanHideOnDesktop);
  999. // Toggle the hide/unhide state
  1000. DWORD dwHide = !_IsItemHiddenOnDesktop(pitem->_psfd->_pszCanHideOnDesktop);
  1001. LONG lErr = SHRegSetUSValue(REGSTR_PATH_HIDDEN_DESKTOP_ICONS_STARTPANEL,
  1002. pitem->_psfd->_pszCanHideOnDesktop,
  1003. REG_DWORD, &dwHide, sizeof(dwHide),
  1004. SHREGSET_FORCE_HKCU);
  1005. hr = HRESULT_FROM_WIN32(lErr);
  1006. if (SUCCEEDED(hr))
  1007. {
  1008. // explorer\rcids.h and shell32\unicpp\resource.h have DIFFERENT
  1009. // VALUES FOR FCIDM_REFRESH! We want the one in unicpp\resource.h
  1010. // because that's the correct one...
  1011. #define FCIDM_REFRESH_REAL 0x0a220
  1012. PostMessage(GetShellWindow(), WM_COMMAND, FCIDM_REFRESH_REAL, 0); // refresh desktop
  1013. }
  1014. }
  1015. else
  1016. {
  1017. hr = SFTBarHost::ContextMenuInvokeItem(pitem, pcm, pici, pszVerb);
  1018. }
  1019. return hr;
  1020. }
  1021. HRESULT SpecialFolderList::_GetUIObjectOfItem(PaneItem *p, REFIID riid, LPVOID *ppv)
  1022. {
  1023. SpecialFolderListItem *pitem = static_cast<SpecialFolderListItem *>(p);
  1024. if (pitem->_psfd->IsCSIDL() && (CSIDL_RECENT == pitem->_psfd->GetCSIDL()))
  1025. {
  1026. *ppv = NULL;
  1027. return E_NOTIMPL;
  1028. }
  1029. return SFTBarHost::_GetUIObjectOfItem(p, riid, ppv);
  1030. }
  1031. //****************************************************************************
  1032. //
  1033. // IShellMenuCallback helper for Recent Documents
  1034. //
  1035. // We want to restrict to the first MAXRECDOCS items.
  1036. //
  1037. class CRecentShellMenuCallback
  1038. : public CUnknown
  1039. , public IShellMenuCallback
  1040. {
  1041. public:
  1042. // *** IUnknown ***
  1043. STDMETHODIMP QueryInterface(REFIID riid, void** ppvObj);
  1044. STDMETHODIMP_(ULONG) AddRef(void) { return CUnknown::AddRef(); }
  1045. STDMETHODIMP_(ULONG) Release(void) { return CUnknown::Release(); }
  1046. // *** IShellMenuCallback ***
  1047. STDMETHODIMP CallbackSM(LPSMDATA psmd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  1048. private:
  1049. friend HRESULT CRecentShellMenuCallback_CreateInstance(IShellMenuCallback **ppsmc);
  1050. HRESULT _FilterRecentPidl(IShellFolder *psf, LPCITEMIDLIST pidlItem);
  1051. int _nShown;
  1052. int _iMaxRecentDocs;
  1053. };
  1054. HRESULT CRecentShellMenuCallback::QueryInterface(REFIID riid, void **ppvObj)
  1055. {
  1056. static const QITAB qit[] =
  1057. {
  1058. QITABENT(CRecentShellMenuCallback, IShellMenuCallback),
  1059. { 0 },
  1060. };
  1061. return QISearch(this, qit, riid, ppvObj);
  1062. }
  1063. HRESULT CRecentShellMenuCallback::CallbackSM(LPSMDATA psmd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  1064. {
  1065. switch (uMsg)
  1066. {
  1067. case SMC_BEGINENUM:
  1068. _nShown = 0;
  1069. _iMaxRecentDocs = SHRestricted(REST_MaxRecentDocs);
  1070. if (_iMaxRecentDocs < 1)
  1071. _iMaxRecentDocs = 15; // default from shell32\recdocs.h
  1072. return S_OK;
  1073. case SMC_FILTERPIDL:
  1074. ASSERT(psmd->dwMask & SMDM_SHELLFOLDER);
  1075. return _FilterRecentPidl(psmd->psf, psmd->pidlItem);
  1076. }
  1077. return S_FALSE;
  1078. }
  1079. //
  1080. // Return S_FALSE to allow the item to show, S_OK to hide it
  1081. //
  1082. HRESULT CRecentShellMenuCallback::_FilterRecentPidl(IShellFolder *psf, LPCITEMIDLIST pidlItem)
  1083. {
  1084. HRESULT hrRc = S_OK; // Assume hidden
  1085. if (_nShown < _iMaxRecentDocs)
  1086. {
  1087. IShellLink *psl;
  1088. if (SUCCEEDED(psf->GetUIObjectOf(NULL, 1, &pidlItem, IID_X_PPV_ARG(IShellLink, NULL, &psl))))
  1089. {
  1090. LPITEMIDLIST pidlTarget;
  1091. if (SUCCEEDED(psl->GetIDList(&pidlTarget)) && pidlTarget)
  1092. {
  1093. DWORD dwAttr = SFGAO_FOLDER;
  1094. if (SUCCEEDED(SHGetAttributesOf(pidlTarget, &dwAttr)) &&
  1095. !(dwAttr & SFGAO_FOLDER))
  1096. {
  1097. // We found a shortcut to a nonfolder - keep it!
  1098. _nShown++;
  1099. hrRc = S_FALSE;
  1100. }
  1101. ILFree(pidlTarget);
  1102. }
  1103. psl->Release();
  1104. }
  1105. }
  1106. return hrRc;
  1107. }
  1108. HRESULT CRecentShellMenuCallback_CreateInstance(IShellMenuCallback **ppsmc)
  1109. {
  1110. *ppsmc = new CRecentShellMenuCallback;
  1111. return *ppsmc ? S_OK : E_OUTOFMEMORY;
  1112. }
  1113. //****************************************************************************
  1114. //
  1115. // IShellMenuCallback helper that disallows cascading into subfolders
  1116. //
  1117. class CNoSubdirShellMenuCallback
  1118. : public CUnknown
  1119. , public IShellMenuCallback
  1120. {
  1121. public:
  1122. // *** IUnknown ***
  1123. STDMETHODIMP QueryInterface(REFIID riid, void** ppvObj);
  1124. STDMETHODIMP_(ULONG) AddRef(void) { return CUnknown::AddRef(); }
  1125. STDMETHODIMP_(ULONG) Release(void) { return CUnknown::Release(); }
  1126. // *** IShellMenuCallback ***
  1127. STDMETHODIMP CallbackSM(LPSMDATA psmd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  1128. private:
  1129. friend HRESULT CNoSubdirShellMenuCallback_CreateInstance(IShellMenuCallback **ppsmc);
  1130. };
  1131. HRESULT CNoSubdirShellMenuCallback::QueryInterface(REFIID riid, void **ppvObj)
  1132. {
  1133. static const QITAB qit[] =
  1134. {
  1135. QITABENT(CNoSubdirShellMenuCallback, IShellMenuCallback),
  1136. { 0 },
  1137. };
  1138. return QISearch(this, qit, riid, ppvObj);
  1139. }
  1140. HRESULT CNoSubdirShellMenuCallback::CallbackSM(LPSMDATA psmd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  1141. {
  1142. switch (uMsg)
  1143. {
  1144. case SMC_GETSFINFO:
  1145. {
  1146. // Turn off the SMIF_SUBMENU flag on everybody. This
  1147. // prevents us from cascading more than one level deel.
  1148. SMINFO *psminfo = reinterpret_cast<SMINFO *>(lParam);
  1149. psminfo->dwFlags &= ~SMIF_SUBMENU;
  1150. return S_OK;
  1151. }
  1152. }
  1153. return S_FALSE;
  1154. }
  1155. HRESULT CNoSubdirShellMenuCallback_CreateInstance(IShellMenuCallback **ppsmc)
  1156. {
  1157. *ppsmc = new CNoSubdirShellMenuCallback;
  1158. return *ppsmc ? S_OK : E_OUTOFMEMORY;
  1159. }
  1160. //****************************************************************************
  1161. //
  1162. // IShellMenuCallback helper for My Computer
  1163. //
  1164. // Disallow cascading into subfolders and also force the default
  1165. // drag/drop effect to DROPEFFECT_LINK.
  1166. //
  1167. class CMyComputerShellMenuCallback
  1168. : public CNoSubdirShellMenuCallback
  1169. {
  1170. public:
  1171. typedef CNoSubdirShellMenuCallback super;
  1172. // *** IShellMenuCallback ***
  1173. STDMETHODIMP CallbackSM(LPSMDATA psmd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  1174. private:
  1175. friend HRESULT CMyComputerShellMenuCallback_CreateInstance(IShellMenuCallback **ppsmc);
  1176. };
  1177. HRESULT CMyComputerShellMenuCallback::CallbackSM(LPSMDATA psmd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  1178. {
  1179. switch (uMsg)
  1180. {
  1181. case SMC_BEGINDRAG:
  1182. *(DWORD*)wParam = DROPEFFECT_LINK;
  1183. return S_OK;
  1184. }
  1185. return super::CallbackSM(psmd, uMsg, wParam, lParam);
  1186. }
  1187. HRESULT CMyComputerShellMenuCallback_CreateInstance(IShellMenuCallback **ppsmc)
  1188. {
  1189. *ppsmc = new CMyComputerShellMenuCallback;
  1190. return *ppsmc ? S_OK : E_OUTOFMEMORY;
  1191. }
  1192. //****************************************************************************
  1193. //
  1194. // IShellMenuCallback helper that prevents Fonts from cascading
  1195. // Used by Control Panel.
  1196. //
  1197. class CNoFontsShellMenuCallback
  1198. : public CUnknown
  1199. , public IShellMenuCallback
  1200. {
  1201. public:
  1202. // *** IUnknown ***
  1203. STDMETHODIMP QueryInterface(REFIID riid, void** ppvObj);
  1204. STDMETHODIMP_(ULONG) AddRef(void) { return CUnknown::AddRef(); }
  1205. STDMETHODIMP_(ULONG) Release(void) { return CUnknown::Release(); }
  1206. // *** IShellMenuCallback ***
  1207. STDMETHODIMP CallbackSM(LPSMDATA psmd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  1208. private:
  1209. friend HRESULT CNoFontsShellMenuCallback_CreateInstance(IShellMenuCallback **ppsmc);
  1210. };
  1211. HRESULT CNoFontsShellMenuCallback::QueryInterface(REFIID riid, void **ppvObj)
  1212. {
  1213. static const QITAB qit[] =
  1214. {
  1215. QITABENT(CNoFontsShellMenuCallback, IShellMenuCallback),
  1216. { 0 },
  1217. };
  1218. return QISearch(this, qit, riid, ppvObj);
  1219. }
  1220. BOOL _IsFontsFolderShortcut(IShellFolder *psf, LPCITEMIDLIST pidl)
  1221. {
  1222. TCHAR sz[MAX_PATH];
  1223. return SUCCEEDED(DisplayNameOf(psf, pidl, SHGDN_FORPARSING | SHGDN_INFOLDER, sz, ARRAYSIZE(sz))) &&
  1224. lstrcmpi(sz, TEXT("::{D20EA4E1-3957-11d2-A40B-0C5020524152}")) == 0;
  1225. }
  1226. HRESULT CNoFontsShellMenuCallback::CallbackSM(LPSMDATA psmd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  1227. {
  1228. switch (uMsg)
  1229. {
  1230. case SMC_GETSFINFO:
  1231. {
  1232. // If this is the Fonts item, then remove the SUBMENU attribute.
  1233. SMINFO *psminfo = reinterpret_cast<SMINFO *>(lParam);
  1234. if ((psminfo->dwMask & SMIM_FLAGS) &&
  1235. (psminfo->dwFlags & SMIF_SUBMENU) &&
  1236. _IsFontsFolderShortcut(psmd->psf, psmd->pidlItem))
  1237. {
  1238. psminfo->dwFlags &= ~SMIF_SUBMENU;
  1239. }
  1240. return S_OK;
  1241. }
  1242. }
  1243. return S_FALSE;
  1244. }
  1245. HRESULT CNoFontsShellMenuCallback_CreateInstance(IShellMenuCallback **ppsmc)
  1246. {
  1247. *ppsmc = new CNoFontsShellMenuCallback;
  1248. return *ppsmc ? S_OK : E_OUTOFMEMORY;
  1249. }
  1250. //****************************************************************************
  1251. //
  1252. // IShellMenuCallback helper that filters the "connect to" menu
  1253. //
  1254. class CConnectToShellMenuCallback
  1255. : public CUnknown
  1256. , public IShellMenuCallback
  1257. , public CObjectWithSite
  1258. {
  1259. public:
  1260. // *** IUnknown ***
  1261. STDMETHODIMP QueryInterface(REFIID riid, void** ppvObj);
  1262. STDMETHODIMP_(ULONG) AddRef(void) { return CUnknown::AddRef(); }
  1263. STDMETHODIMP_(ULONG) Release(void) { return CUnknown::Release(); }
  1264. // *** IShellMenuCallback ***
  1265. STDMETHODIMP CallbackSM(LPSMDATA psmd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  1266. // *** IObjectWithSite ***
  1267. // inherited from CObjectWithSite
  1268. private:
  1269. HRESULT _OnGetSFInfo(SMDATA *psmd, SMINFO *psminfo);
  1270. HRESULT _OnGetInfo(SMDATA *psmd, SMINFO *psminfo);
  1271. HRESULT _OnEndEnum(SMDATA *psmd);
  1272. friend HRESULT CConnectToShellMenuCallback_CreateInstance(IShellMenuCallback **ppsmc);
  1273. BOOL _bAnyRAS;
  1274. };
  1275. #define ICOL_NETCONMEDIATYPE 0x101 // from netshell
  1276. #define ICOL_NETCONSUBMEDIATYPE 0x102 // from netshell
  1277. #define ICOL_NETCONSTATUS 0x103 // from netshell
  1278. #define ICOL_NETCONCHARACTERISTICS 0x104 // from netshell
  1279. BOOL IsMediaRASType(NETCON_MEDIATYPE ncm)
  1280. {
  1281. return (ncm == NCM_DIRECT || ncm == NCM_ISDN || ncm == NCM_PHONE || ncm == NCM_TUNNEL || ncm == NCM_PPPOE); // REVIEW DIRECT correct?
  1282. }
  1283. BOOL IsNetConPidlRAS(IShellFolder2 *psfNetCon, LPCITEMIDLIST pidlNetConItem)
  1284. {
  1285. BOOL bRet = FALSE;
  1286. SHCOLUMNID scidMediaType, scidSubMediaType, scidCharacteristics;
  1287. VARIANT v;
  1288. scidMediaType.fmtid = GUID_NETSHELL_PROPS;
  1289. scidMediaType.pid = ICOL_NETCONMEDIATYPE;
  1290. scidSubMediaType.fmtid = GUID_NETSHELL_PROPS;
  1291. scidSubMediaType.pid = ICOL_NETCONSUBMEDIATYPE;
  1292. scidCharacteristics.fmtid = GUID_NETSHELL_PROPS;
  1293. scidCharacteristics.pid = ICOL_NETCONCHARACTERISTICS;
  1294. if (SUCCEEDED(psfNetCon->GetDetailsEx(pidlNetConItem, &scidMediaType, &v)))
  1295. {
  1296. // Is this a RAS connection
  1297. if (IsMediaRASType((NETCON_MEDIATYPE)v.lVal))
  1298. {
  1299. VariantClear(&v);
  1300. // Make sure it's not incoming
  1301. if (SUCCEEDED(psfNetCon->GetDetailsEx(pidlNetConItem, &scidCharacteristics, &v)))
  1302. {
  1303. if (!(NCCF_INCOMING_ONLY & v.lVal))
  1304. bRet = TRUE;
  1305. }
  1306. }
  1307. // Is this a Wireless LAN connection?
  1308. if (NCM_LAN == (NETCON_MEDIATYPE)v.lVal)
  1309. {
  1310. VariantClear(&v);
  1311. if (SUCCEEDED(psfNetCon->GetDetailsEx(pidlNetConItem, &scidSubMediaType, &v)))
  1312. {
  1313. if (NCSM_WIRELESS == (NETCON_SUBMEDIATYPE)v.lVal)
  1314. bRet = TRUE;
  1315. }
  1316. }
  1317. VariantClear(&v);
  1318. }
  1319. return bRet;
  1320. }
  1321. HRESULT CConnectToShellMenuCallback::_OnGetInfo(SMDATA *psmd, SMINFO *psminfo)
  1322. {
  1323. HRESULT hr = S_FALSE;
  1324. if (psminfo->dwMask & SMIM_ICON)
  1325. {
  1326. if (psmd->uId == IDM_OPENCONFOLDER)
  1327. {
  1328. LPITEMIDLIST pidl = SHCloneSpecialIDList(NULL, CSIDL_CONNECTIONS, FALSE);
  1329. if (pidl)
  1330. {
  1331. LPCITEMIDLIST pidlObject;
  1332. IShellFolder *psf;
  1333. hr = SHBindToParent(pidl, IID_PPV_ARG(IShellFolder, &psf), &pidlObject);
  1334. if (SUCCEEDED(hr))
  1335. {
  1336. SHMapPIDLToSystemImageListIndex(psf, pidlObject, &psminfo->iIcon);
  1337. psminfo->dwFlags |= SMIF_ICON;
  1338. psf->Release();
  1339. }
  1340. ILFree(pidl);
  1341. }
  1342. }
  1343. }
  1344. return hr;
  1345. }
  1346. HRESULT CConnectToShellMenuCallback::_OnGetSFInfo(SMDATA *psmd, SMINFO *psminfo)
  1347. {
  1348. IShellFolder2 *psf2;
  1349. ASSERT(psminfo->dwMask & SMIM_FLAGS); // ??
  1350. psminfo->dwFlags &= ~SMIF_SUBMENU;
  1351. if (SUCCEEDED(psmd->psf->QueryInterface(IID_PPV_ARG(IShellFolder2, &psf2))))
  1352. {
  1353. if (!IsNetConPidlRAS(psf2, psmd->pidlItem))
  1354. psminfo->dwFlags |= SMIF_HIDDEN;
  1355. else
  1356. _bAnyRAS = TRUE;
  1357. psf2->Release();
  1358. }
  1359. return S_OK;
  1360. }
  1361. HRESULT CConnectToShellMenuCallback::_OnEndEnum(SMDATA *psmd)
  1362. {
  1363. HRESULT hr = S_FALSE;
  1364. IShellMenu* psm;
  1365. if (psmd->punk && SUCCEEDED(hr = psmd->punk->QueryInterface(IID_PPV_ARG(IShellMenu, &psm))))
  1366. {
  1367. // load the static portion of the connect to menu, and add it to the bottom
  1368. HMENU hmStatic = LoadMenu(_Module.GetResourceInstance(), MAKEINTRESOURCE(MENU_CONNECTTO));
  1369. if (hmStatic)
  1370. {
  1371. // if there aren't any dynamic items (RAS connections), then delete the separator
  1372. if (!_bAnyRAS)
  1373. DeleteMenu(hmStatic, 0, MF_BYPOSITION);
  1374. HWND hwnd = NULL;
  1375. IUnknown *punk;
  1376. if (SUCCEEDED(IUnknown_QueryService(_punkSite, SID_SMenuPopup, IID_PPV_ARG(IUnknown, &punk))))
  1377. {
  1378. IUnknown_GetWindow(punk, &hwnd);
  1379. punk->Release();
  1380. }
  1381. psm->SetMenu(hmStatic, hwnd, SMSET_NOEMPTY | SMSET_BOTTOM);
  1382. }
  1383. psm->Release();
  1384. }
  1385. return hr;
  1386. }
  1387. HRESULT CConnectToShellMenuCallback::QueryInterface(REFIID riid, void **ppvObj)
  1388. {
  1389. static const QITAB qit[] =
  1390. {
  1391. QITABENT(CConnectToShellMenuCallback, IShellMenuCallback),
  1392. QITABENT(CConnectToShellMenuCallback, IObjectWithSite),
  1393. { 0 },
  1394. };
  1395. return QISearch(this, qit, riid, ppvObj);
  1396. }
  1397. HRESULT CConnectToShellMenuCallback::CallbackSM(LPSMDATA psmd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  1398. {
  1399. switch (uMsg)
  1400. {
  1401. case SMC_GETINFO:
  1402. return _OnGetInfo(psmd, (SMINFO *)lParam);
  1403. case SMC_GETSFINFO:
  1404. return _OnGetSFInfo(psmd, (SMINFO *)lParam);
  1405. case SMC_BEGINENUM:
  1406. _bAnyRAS = FALSE;
  1407. case SMC_ENDENUM:
  1408. return _OnEndEnum(psmd);
  1409. case SMC_EXEC:
  1410. switch (psmd->uId)
  1411. {
  1412. case IDM_OPENCONFOLDER:
  1413. ShowFolder(CSIDL_CONNECTIONS);
  1414. return S_OK;
  1415. }
  1416. break;
  1417. }
  1418. return S_FALSE;
  1419. }
  1420. HRESULT CConnectToShellMenuCallback_CreateInstance(IShellMenuCallback **ppsmc)
  1421. {
  1422. *ppsmc = new CConnectToShellMenuCallback;
  1423. return *ppsmc ? S_OK : E_OUTOFMEMORY;
  1424. }
  1425. BOOL SpecialFolderDesc::LoadStringAsOLESTR(LPTSTR *ppsz) const
  1426. {
  1427. BOOL bRet = FALSE;
  1428. TCHAR szTmp[MAX_PATH];
  1429. if (_idsCustomName && LoadString(_Module.GetResourceInstance(), _idsCustomName, szTmp, ARRAYSIZE(szTmp)))
  1430. {
  1431. if (ppsz)
  1432. SHStrDup(szTmp, ppsz);
  1433. bRet = TRUE;
  1434. }
  1435. return bRet;
  1436. }
  1437. BOOL SpecialFolderDesc::ConnectToName(LPTSTR *ppsz) const
  1438. {
  1439. BOOL bIgnoreRule;
  1440. DWORD dwMode = GetDisplayMode(&bIgnoreRule);
  1441. // if Connect To is displayed as a link, then don't over-ride the name (i.e. use Network Connections)
  1442. if ((dwMode & SFD_MODEMASK) == SFD_SHOW)
  1443. return FALSE;
  1444. else
  1445. return LoadStringAsOLESTR(ppsz);
  1446. }
  1447. void ShowFolder(UINT csidl)
  1448. {
  1449. LPITEMIDLIST pidl;
  1450. if (SUCCEEDED(SHGetFolderLocation(NULL, csidl, NULL, 0, &pidl)))
  1451. {
  1452. SHELLEXECUTEINFO shei = { 0 };
  1453. shei.cbSize = sizeof(shei);
  1454. shei.fMask = SEE_MASK_IDLIST;
  1455. shei.nShow = SW_SHOWNORMAL;
  1456. shei.lpVerb = TEXT("open");
  1457. shei.lpIDList = pidl;
  1458. ShellExecuteEx(&shei);
  1459. ILFree(pidl);
  1460. }
  1461. }