JumpList.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Diagnostics.CodeAnalysis;
  5. using System.IO;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Windows;
  10. using System.Windows.Markup;
  11. using Standard;
  12. namespace Microsoft.Windows.Shell;
  13. [ContentProperty("JumpItems")]
  14. public sealed class JumpList : ISupportInitialize
  15. {
  16. public static void AddToRecentCategory(string itemPath)
  17. {
  18. Verify.FileExists(itemPath, "itemPath");
  19. itemPath = Path.GetFullPath(itemPath);
  20. NativeMethods.SHAddToRecentDocs(itemPath);
  21. }
  22. [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")]
  23. public static void AddToRecentCategory(JumpPath jumpPath)
  24. {
  25. Verify.IsNotNull<JumpPath>(jumpPath, "jumpPath");
  26. JumpList.AddToRecentCategory(jumpPath.Path);
  27. }
  28. public static void AddToRecentCategory(JumpTask jumpTask)
  29. {
  30. Verify.IsNotNull<JumpTask>(jumpTask, "jumpTask");
  31. if (Utility.IsOSWindows7OrNewer)
  32. {
  33. IShellLinkW shellLinkW = JumpList.CreateLinkFromJumpTask(jumpTask, false);
  34. try
  35. {
  36. if (shellLinkW != null)
  37. {
  38. NativeMethods.SHAddToRecentDocs(shellLinkW);
  39. }
  40. }
  41. finally
  42. {
  43. Utility.SafeRelease<IShellLinkW>(ref shellLinkW);
  44. }
  45. }
  46. }
  47. public static void SetJumpList(Application application, JumpList value)
  48. {
  49. Verify.IsNotNull<Application>(application, "application");
  50. lock (JumpList.s_lock)
  51. {
  52. JumpList jumpList;
  53. if (JumpList.s_applicationMap.TryGetValue(application, out jumpList) && jumpList != null)
  54. {
  55. jumpList._application = null;
  56. }
  57. JumpList.s_applicationMap[application] = value;
  58. if (value != null)
  59. {
  60. value._application = application;
  61. }
  62. }
  63. if (value != null)
  64. {
  65. value.ApplyFromApplication();
  66. }
  67. }
  68. public static JumpList GetJumpList(Application application)
  69. {
  70. Verify.IsNotNull<Application>(application, "application");
  71. JumpList result;
  72. JumpList.s_applicationMap.TryGetValue(application, out result);
  73. return result;
  74. }
  75. public JumpList() : this(null, false, false)
  76. {
  77. this._initializing = null;
  78. }
  79. public JumpList(IEnumerable<JumpItem> items, bool showFrequent, bool showRecent)
  80. {
  81. if (items != null)
  82. {
  83. this._jumpItems = new List<JumpItem>(items);
  84. }
  85. else
  86. {
  87. this._jumpItems = new List<JumpItem>();
  88. }
  89. this.ShowFrequentCategory = showFrequent;
  90. this.ShowRecentCategory = showRecent;
  91. this._initializing = new bool?(false);
  92. }
  93. public bool ShowFrequentCategory { get; set; }
  94. public bool ShowRecentCategory { get; set; }
  95. [SuppressMessage("Microsoft.Design", "CA1002:DoNotExposeGenericLists")]
  96. public List<JumpItem> JumpItems
  97. {
  98. get
  99. {
  100. return this._jumpItems;
  101. }
  102. }
  103. private bool _IsUnmodified
  104. {
  105. get
  106. {
  107. return this._initializing == null && this.JumpItems.Count == 0 && !this.ShowRecentCategory && !this.ShowFrequentCategory;
  108. }
  109. }
  110. [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "BeginInit")]
  111. public void BeginInit()
  112. {
  113. if (!this._IsUnmodified)
  114. {
  115. throw new InvalidOperationException("Calls to BeginInit cannot be nested.");
  116. }
  117. this._initializing = new bool?(true);
  118. }
  119. [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "EndInit")]
  120. [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "BeginInit")]
  121. public void EndInit()
  122. {
  123. if (this._initializing != true)
  124. {
  125. throw new NotSupportedException("Can't call EndInit without first calling BeginInit.");
  126. }
  127. this._initializing = new bool?(false);
  128. this.ApplyFromApplication();
  129. }
  130. private static string _RuntimeId
  131. {
  132. get
  133. {
  134. string result;
  135. HRESULT hrLeft = NativeMethods.GetCurrentProcessExplicitAppUserModelID(out result);
  136. if (hrLeft == HRESULT.E_FAIL)
  137. {
  138. hrLeft = HRESULT.S_OK;
  139. result = null;
  140. }
  141. hrLeft.ThrowIfFailed();
  142. return result;
  143. }
  144. }
  145. [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "JumpList")]
  146. [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "EndInit")]
  147. public void Apply()
  148. {
  149. if (this._initializing == true)
  150. {
  151. throw new InvalidOperationException("The JumpList can't be applied until EndInit has been called.");
  152. }
  153. this._initializing = new bool?(false);
  154. this._ApplyList();
  155. }
  156. private void ApplyFromApplication()
  157. {
  158. if (this._initializing != true && !this._IsUnmodified)
  159. {
  160. this._initializing = new bool?(false);
  161. }
  162. if (this._application == Application.Current && this._initializing == false)
  163. {
  164. this._ApplyList();
  165. }
  166. }
  167. [SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "Standard.Verify.IsApartmentState(System.Threading.ApartmentState,System.String)")]
  168. [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
  169. [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "JumpLists")]
  170. private void _ApplyList()
  171. {
  172. Verify.IsApartmentState(ApartmentState.STA, "JumpLists can only be effected on STA threads.");
  173. if (!Utility.IsOSWindows7OrNewer)
  174. {
  175. this.RejectEverything();
  176. return;
  177. }
  178. List<JumpItem> jumpItems;
  179. List<JumpList._RejectedJumpItemPair> list;
  180. List<JumpList._ShellObjectPair> list2;
  181. try
  182. {
  183. this._BuildShellLists(out jumpItems, out list, out list2);
  184. }
  185. catch (Exception)
  186. {
  187. this.RejectEverything();
  188. return;
  189. }
  190. this._jumpItems = jumpItems;
  191. EventHandler<JumpItemsRejectedEventArgs> jumpItemsRejected = this.JumpItemsRejected;
  192. EventHandler<JumpItemsRemovedEventArgs> jumpItemsRemovedByUser = this.JumpItemsRemovedByUser;
  193. if (list.Count > 0 && jumpItemsRejected != null)
  194. {
  195. List<JumpItem> list3 = new List<JumpItem>(list.Count);
  196. List<JumpItemRejectionReason> list4 = new List<JumpItemRejectionReason>(list.Count);
  197. foreach (JumpList._RejectedJumpItemPair rejectedJumpItemPair in list)
  198. {
  199. list3.Add(rejectedJumpItemPair.JumpItem);
  200. list4.Add(rejectedJumpItemPair.Reason);
  201. }
  202. jumpItemsRejected(this, new JumpItemsRejectedEventArgs(list3, list4));
  203. }
  204. if (list2.Count > 0 && jumpItemsRemovedByUser != null)
  205. {
  206. List<JumpItem> list5 = new List<JumpItem>(list2.Count);
  207. foreach (JumpList._ShellObjectPair shellObjectPair in list2)
  208. {
  209. if (shellObjectPair.JumpItem != null)
  210. {
  211. list5.Add(shellObjectPair.JumpItem);
  212. }
  213. }
  214. if (list5.Count > 0)
  215. {
  216. jumpItemsRemovedByUser(this, new JumpItemsRemovedEventArgs(list5));
  217. }
  218. }
  219. }
  220. private void _BuildShellLists(out List<JumpItem> successList, out List<JumpList._RejectedJumpItemPair> rejectedList, out List<JumpList._ShellObjectPair> removedList)
  221. {
  222. List<List<JumpList._ShellObjectPair>> list = null;
  223. removedList = null;
  224. ICustomDestinationList customDestinationList = CLSID.CoCreateInstance<ICustomDestinationList>("77f10cf0-3db5-4966-b520-b7c54fd35ed6");
  225. try
  226. {
  227. string runtimeId = JumpList._RuntimeId;
  228. if (!string.IsNullOrEmpty(runtimeId))
  229. {
  230. customDestinationList.SetAppID(runtimeId);
  231. }
  232. Guid guid = new Guid("92CA9DCD-5622-4bba-A805-5E9F541BD8C9");
  233. uint num;
  234. IObjectArray shellObjects = (IObjectArray) customDestinationList.BeginList(out num, ref guid);
  235. removedList = JumpList.GenerateJumpItems(shellObjects);
  236. successList = new List<JumpItem>(this.JumpItems.Count);
  237. rejectedList = new List<JumpList._RejectedJumpItemPair>(this.JumpItems.Count);
  238. list = new List<List<JumpList._ShellObjectPair>>
  239. {
  240. new List<JumpList._ShellObjectPair>()
  241. };
  242. foreach (JumpItem jumpItem in this.JumpItems)
  243. {
  244. if (jumpItem == null)
  245. {
  246. rejectedList.Add(new JumpList._RejectedJumpItemPair
  247. {
  248. JumpItem = jumpItem,
  249. Reason = JumpItemRejectionReason.InvalidItem
  250. });
  251. }
  252. else
  253. {
  254. object obj = null;
  255. try
  256. {
  257. obj = JumpList.GetShellObjectForJumpItem(jumpItem);
  258. if (obj == null)
  259. {
  260. rejectedList.Add(new JumpList._RejectedJumpItemPair
  261. {
  262. Reason = JumpItemRejectionReason.InvalidItem,
  263. JumpItem = jumpItem
  264. });
  265. }
  266. else if (JumpList.ListContainsShellObject(removedList, obj))
  267. {
  268. rejectedList.Add(new JumpList._RejectedJumpItemPair
  269. {
  270. Reason = JumpItemRejectionReason.RemovedByUser,
  271. JumpItem = jumpItem
  272. });
  273. }
  274. else
  275. {
  276. JumpList._ShellObjectPair item = new JumpList._ShellObjectPair
  277. {
  278. JumpItem = jumpItem,
  279. ShellObject = obj
  280. };
  281. if (string.IsNullOrEmpty(jumpItem.CustomCategory))
  282. {
  283. list[0].Add(item);
  284. }
  285. else
  286. {
  287. bool flag = false;
  288. foreach (List<JumpList._ShellObjectPair> list2 in list)
  289. {
  290. if (list2.Count > 0 && list2[0].JumpItem.CustomCategory == jumpItem.CustomCategory)
  291. {
  292. list2.Add(item);
  293. flag = true;
  294. break;
  295. }
  296. }
  297. if (!flag)
  298. {
  299. list.Add(new List<JumpList._ShellObjectPair>
  300. {
  301. item
  302. });
  303. }
  304. }
  305. obj = null;
  306. }
  307. }
  308. finally
  309. {
  310. Utility.SafeRelease<object>(ref obj);
  311. }
  312. }
  313. }
  314. list.Reverse();
  315. if (this.ShowFrequentCategory)
  316. {
  317. customDestinationList.AppendKnownCategory(KDC.FREQUENT);
  318. }
  319. if (this.ShowRecentCategory)
  320. {
  321. customDestinationList.AppendKnownCategory(KDC.RECENT);
  322. }
  323. foreach (List<JumpList._ShellObjectPair> list3 in list)
  324. {
  325. if (list3.Count > 0)
  326. {
  327. string customCategory = list3[0].JumpItem.CustomCategory;
  328. JumpList.AddCategory(customDestinationList, customCategory, list3, successList, rejectedList);
  329. }
  330. }
  331. customDestinationList.CommitList();
  332. successList.Reverse();
  333. }
  334. finally
  335. {
  336. Utility.SafeRelease<ICustomDestinationList>(ref customDestinationList);
  337. if (list != null)
  338. {
  339. foreach (List<JumpList._ShellObjectPair> list4 in list)
  340. {
  341. JumpList._ShellObjectPair.ReleaseShellObjects(list4);
  342. }
  343. }
  344. JumpList._ShellObjectPair.ReleaseShellObjects(removedList);
  345. }
  346. }
  347. private static bool ListContainsShellObject(List<JumpList._ShellObjectPair> removedList, object shellObject)
  348. {
  349. if (removedList.Count == 0)
  350. {
  351. return false;
  352. }
  353. IShellItem shellItem = shellObject as IShellItem;
  354. if (shellItem != null)
  355. {
  356. foreach (JumpList._ShellObjectPair shellObjectPair in removedList)
  357. {
  358. IShellItem shellItem2 = shellObjectPair.ShellObject as IShellItem;
  359. if (shellItem2 != null && shellItem.Compare(shellItem2, (SICHINT) 805306368u) == 0)
  360. {
  361. return true;
  362. }
  363. }
  364. return false;
  365. }
  366. IShellLinkW shellLinkW = shellObject as IShellLinkW;
  367. if (shellLinkW != null)
  368. {
  369. foreach (JumpList._ShellObjectPair shellObjectPair2 in removedList)
  370. {
  371. IShellLinkW shellLinkW2 = shellObjectPair2.ShellObject as IShellLinkW;
  372. if (shellLinkW2 != null)
  373. {
  374. string a = JumpList.ShellLinkToString(shellLinkW2);
  375. string b = JumpList.ShellLinkToString(shellLinkW);
  376. if (a == b)
  377. {
  378. return true;
  379. }
  380. }
  381. }
  382. return false;
  383. }
  384. return false;
  385. }
  386. private static object GetShellObjectForJumpItem(JumpItem jumpItem)
  387. {
  388. JumpPath jumpPath = jumpItem as JumpPath;
  389. JumpTask jumpTask = jumpItem as JumpTask;
  390. if (jumpPath != null)
  391. {
  392. return JumpList.CreateItemFromJumpPath(jumpPath);
  393. }
  394. if (jumpTask != null)
  395. {
  396. return JumpList.CreateLinkFromJumpTask(jumpTask, true);
  397. }
  398. return null;
  399. }
  400. private static List<JumpList._ShellObjectPair> GenerateJumpItems(IObjectArray shellObjects)
  401. {
  402. List<JumpList._ShellObjectPair> list = new List<JumpList._ShellObjectPair>();
  403. Guid guid = new Guid("00000000-0000-0000-C000-000000000046");
  404. uint count = shellObjects.GetCount();
  405. for (uint num = 0u; num < count; num += 1u)
  406. {
  407. object at = shellObjects.GetAt(num, ref guid);
  408. JumpItem jumpItem = null;
  409. try
  410. {
  411. jumpItem = JumpList.GetJumpItemForShellObject(at);
  412. }
  413. catch (Exception ex)
  414. {
  415. if (ex is NullReferenceException || ex is SEHException)
  416. {
  417. throw;
  418. }
  419. }
  420. list.Add(new JumpList._ShellObjectPair
  421. {
  422. ShellObject = at,
  423. JumpItem = jumpItem
  424. });
  425. }
  426. return list;
  427. }
  428. private static void AddCategory(ICustomDestinationList cdl, string category, List<JumpList._ShellObjectPair> jumpItems, List<JumpItem> successList, List<JumpList._RejectedJumpItemPair> rejectionList)
  429. {
  430. JumpList.AddCategory(cdl, category, jumpItems, successList, rejectionList, true);
  431. }
  432. private static void AddCategory(ICustomDestinationList cdl, string category, List<JumpList._ShellObjectPair> jumpItems, List<JumpItem> successList, List<JumpList._RejectedJumpItemPair> rejectionList, bool isHeterogenous)
  433. {
  434. IObjectCollection objectCollection = (IObjectCollection) Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("2d3468c1-36a7-43b6-ac24-d3f02fd9607a")));
  435. foreach (JumpList._ShellObjectPair shellObjectPair in jumpItems)
  436. {
  437. objectCollection.AddObject(shellObjectPair.ShellObject);
  438. }
  439. HRESULT hrLeft;
  440. if (string.IsNullOrEmpty(category))
  441. {
  442. hrLeft = cdl.AddUserTasks(objectCollection);
  443. }
  444. else
  445. {
  446. hrLeft = cdl.AppendCategory(category, objectCollection);
  447. }
  448. if (hrLeft.Succeeded)
  449. {
  450. int num = jumpItems.Count;
  451. while (--num >= 0)
  452. {
  453. successList.Add(jumpItems[num].JumpItem);
  454. }
  455. return;
  456. }
  457. if (isHeterogenous && hrLeft == HRESULT.DESTS_E_NO_MATCHING_ASSOC_HANDLER)
  458. {
  459. Utility.SafeRelease<IObjectCollection>(ref objectCollection);
  460. List<JumpList._ShellObjectPair> list = new List<JumpList._ShellObjectPair>();
  461. foreach (JumpList._ShellObjectPair shellObjectPair2 in jumpItems)
  462. {
  463. if (shellObjectPair2.JumpItem is JumpPath)
  464. {
  465. rejectionList.Add(new JumpList._RejectedJumpItemPair
  466. {
  467. JumpItem = shellObjectPair2.JumpItem,
  468. Reason = JumpItemRejectionReason.NoRegisteredHandler
  469. });
  470. }
  471. else
  472. {
  473. list.Add(shellObjectPair2);
  474. }
  475. }
  476. if (list.Count > 0)
  477. {
  478. JumpList.AddCategory(cdl, category, list, successList, rejectionList, false);
  479. return;
  480. }
  481. }
  482. else
  483. {
  484. foreach (JumpList._ShellObjectPair shellObjectPair3 in jumpItems)
  485. {
  486. rejectionList.Add(new JumpList._RejectedJumpItemPair
  487. {
  488. JumpItem = shellObjectPair3.JumpItem,
  489. Reason = JumpItemRejectionReason.InvalidItem
  490. });
  491. }
  492. }
  493. }
  494. [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
  495. private static IShellLinkW CreateLinkFromJumpTask(JumpTask jumpTask, bool allowSeparators)
  496. {
  497. if (string.IsNullOrEmpty(jumpTask.Title) && (!allowSeparators || !string.IsNullOrEmpty(jumpTask.CustomCategory)))
  498. {
  499. return null;
  500. }
  501. IShellLinkW shellLinkW = (IShellLinkW) Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("00021401-0000-0000-C000-000000000046")));
  502. IShellLinkW result;
  503. try
  504. {
  505. string path = JumpList._FullName;
  506. if (!string.IsNullOrEmpty(jumpTask.ApplicationPath))
  507. {
  508. path = jumpTask.ApplicationPath;
  509. }
  510. shellLinkW.SetPath(path);
  511. if (!string.IsNullOrEmpty(jumpTask.WorkingDirectory))
  512. {
  513. shellLinkW.SetWorkingDirectory(jumpTask.WorkingDirectory);
  514. }
  515. if (!string.IsNullOrEmpty(jumpTask.Arguments))
  516. {
  517. shellLinkW.SetArguments(jumpTask.Arguments);
  518. }
  519. if (jumpTask.IconResourceIndex != -1)
  520. {
  521. string pszIconPath = JumpList._FullName;
  522. if (!string.IsNullOrEmpty(jumpTask.IconResourcePath))
  523. {
  524. if ((long) jumpTask.IconResourcePath.Length >= 260L)
  525. {
  526. return null;
  527. }
  528. pszIconPath = jumpTask.IconResourcePath;
  529. }
  530. shellLinkW.SetIconLocation(pszIconPath, jumpTask.IconResourceIndex);
  531. }
  532. if (!string.IsNullOrEmpty(jumpTask.Description))
  533. {
  534. shellLinkW.SetDescription(jumpTask.Description);
  535. }
  536. IPropertyStore propertyStore = (IPropertyStore) shellLinkW;
  537. using (PROPVARIANT propvariant = new PROPVARIANT())
  538. {
  539. PKEY pkey = default(PKEY);
  540. if (!string.IsNullOrEmpty(jumpTask.Title))
  541. {
  542. propvariant.SetValue(jumpTask.Title);
  543. pkey = PKEY.Title;
  544. }
  545. else
  546. {
  547. propvariant.SetValue(true);
  548. pkey = PKEY.AppUserModel_IsDestListSeparator;
  549. }
  550. propertyStore.SetValue(ref pkey, propvariant);
  551. }
  552. propertyStore.Commit();
  553. IShellLinkW shellLinkW2 = shellLinkW;
  554. shellLinkW = null;
  555. result = shellLinkW2;
  556. }
  557. catch (Exception)
  558. {
  559. result = null;
  560. }
  561. finally
  562. {
  563. Utility.SafeRelease<IShellLinkW>(ref shellLinkW);
  564. }
  565. return result;
  566. }
  567. private static IShellItem2 GetShellItemForPath(string path)
  568. {
  569. if (string.IsNullOrEmpty(path))
  570. {
  571. return null;
  572. }
  573. Guid guid = new Guid("7e9fb0d3-919f-4307-ab2e-9b1860310c93");
  574. object obj;
  575. HRESULT hrLeft = NativeMethods.SHCreateItemFromParsingName(path, null, ref guid, out obj);
  576. if (hrLeft == (HRESULT) Win32Error.ERROR_FILE_NOT_FOUND || hrLeft == (HRESULT) Win32Error.ERROR_PATH_NOT_FOUND)
  577. {
  578. hrLeft = HRESULT.S_OK;
  579. obj = null;
  580. }
  581. hrLeft.ThrowIfFailed();
  582. return (IShellItem2) obj;
  583. }
  584. [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
  585. private static IShellItem2 CreateItemFromJumpPath(JumpPath jumpPath)
  586. {
  587. try
  588. {
  589. return JumpList.GetShellItemForPath(Path.GetFullPath(jumpPath.Path));
  590. }
  591. catch (Exception)
  592. {
  593. }
  594. return null;
  595. }
  596. private static JumpItem GetJumpItemForShellObject(object shellObject)
  597. {
  598. IShellItem2 shellItem = shellObject as IShellItem2;
  599. IShellLinkW shellLinkW = shellObject as IShellLinkW;
  600. if (shellItem != null)
  601. {
  602. return new JumpPath
  603. {
  604. Path = shellItem.GetDisplayName((SIGDN) 2147647488u)
  605. };
  606. }
  607. if (shellLinkW != null)
  608. {
  609. StringBuilder stringBuilder = new StringBuilder(260);
  610. shellLinkW.GetPath(stringBuilder, stringBuilder.Capacity, null, SLGP.RAWPATH);
  611. StringBuilder stringBuilder2 = new StringBuilder(1024);
  612. shellLinkW.GetArguments(stringBuilder2, stringBuilder2.Capacity);
  613. StringBuilder stringBuilder3 = new StringBuilder(1024);
  614. shellLinkW.GetDescription(stringBuilder3, stringBuilder3.Capacity);
  615. StringBuilder stringBuilder4 = new StringBuilder(260);
  616. int iconResourceIndex;
  617. shellLinkW.GetIconLocation(stringBuilder4, stringBuilder4.Capacity, out iconResourceIndex);
  618. StringBuilder stringBuilder5 = new StringBuilder(260);
  619. shellLinkW.GetWorkingDirectory(stringBuilder5, stringBuilder5.Capacity);
  620. JumpTask jumpTask = new JumpTask
  621. {
  622. ApplicationPath = stringBuilder.ToString(),
  623. Arguments = stringBuilder2.ToString(),
  624. Description = stringBuilder3.ToString(),
  625. IconResourceIndex = iconResourceIndex,
  626. IconResourcePath = stringBuilder4.ToString(),
  627. WorkingDirectory = stringBuilder5.ToString()
  628. };
  629. using (PROPVARIANT propvariant = new PROPVARIANT())
  630. {
  631. IPropertyStore propertyStore = (IPropertyStore) shellLinkW;
  632. PKEY title = PKEY.Title;
  633. propertyStore.GetValue(ref title, propvariant);
  634. jumpTask.Title = (propvariant.GetValue() ?? "");
  635. }
  636. return jumpTask;
  637. }
  638. return null;
  639. }
  640. private static string ShellLinkToString(IShellLinkW shellLink)
  641. {
  642. StringBuilder stringBuilder = new StringBuilder(260);
  643. shellLink.GetPath(stringBuilder, stringBuilder.Capacity, null, SLGP.RAWPATH);
  644. string text = null;
  645. using (PROPVARIANT propvariant = new PROPVARIANT())
  646. {
  647. IPropertyStore propertyStore = (IPropertyStore) shellLink;
  648. PKEY title = PKEY.Title;
  649. propertyStore.GetValue(ref title, propvariant);
  650. text = (propvariant.GetValue() ?? "");
  651. }
  652. StringBuilder stringBuilder2 = new StringBuilder(1024);
  653. shellLink.GetArguments(stringBuilder2, stringBuilder2.Capacity);
  654. return stringBuilder.ToString().ToUpperInvariant() + text.ToUpperInvariant() + stringBuilder2.ToString();
  655. }
  656. private void RejectEverything()
  657. {
  658. EventHandler<JumpItemsRejectedEventArgs> jumpItemsRejected = this.JumpItemsRejected;
  659. if (jumpItemsRejected == null)
  660. {
  661. this._jumpItems.Clear();
  662. return;
  663. }
  664. if (this._jumpItems.Count > 0)
  665. {
  666. List<JumpItemRejectionReason> list = new List<JumpItemRejectionReason>(this.JumpItems.Count);
  667. for (int i = 0; i < this.JumpItems.Count; i++)
  668. {
  669. list.Add(JumpItemRejectionReason.InvalidItem);
  670. }
  671. JumpItemsRejectedEventArgs e = new JumpItemsRejectedEventArgs(this.JumpItems, list);
  672. this._jumpItems.Clear();
  673. jumpItemsRejected(this, e);
  674. }
  675. }
  676. public event EventHandler<JumpItemsRejectedEventArgs> JumpItemsRejected;
  677. public event EventHandler<JumpItemsRemovedEventArgs> JumpItemsRemovedByUser;
  678. private static readonly object s_lock = new object();
  679. private static readonly Dictionary<Application, JumpList> s_applicationMap = new Dictionary<Application, JumpList>();
  680. private Application _application;
  681. private bool? _initializing;
  682. private List<JumpItem> _jumpItems;
  683. private static readonly string _FullName = NativeMethods.GetModuleFileName(IntPtr.Zero);
  684. private class _RejectedJumpItemPair
  685. {
  686. public JumpItem JumpItem { get; set; }
  687. public JumpItemRejectionReason Reason { get; set; }
  688. }
  689. private class _ShellObjectPair
  690. {
  691. public JumpItem JumpItem { get; set; }
  692. public object ShellObject { get; set; }
  693. public static void ReleaseShellObjects(List<JumpList._ShellObjectPair> list)
  694. {
  695. if (list != null)
  696. {
  697. foreach (JumpList._ShellObjectPair shellObjectPair in list)
  698. {
  699. object shellObject = shellObjectPair.ShellObject;
  700. shellObjectPair.ShellObject = null;
  701. Utility.SafeRelease<object>(ref shellObject);
  702. }
  703. }
  704. }
  705. }
  706. }