PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/MarkPad/Infrastructure/JumpListIntegration.cs

https://github.com/bcott/DownmarkerWPF
C# | 160 lines | 124 code | 28 blank | 8 comment | 19 complexity | 66d1c1b8107283dae1de189d0c97c623 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Threading;
  7. using System.Windows;
  8. using System.Windows.Shell;
  9. using Caliburn.Micro;
  10. using MarkPad.Events;
  11. using MarkPad.Settings;
  12. using MarkPad.Settings.Models;
  13. namespace MarkPad.Infrastructure
  14. {
  15. /// <summary>
  16. /// Class for interacting with the Windows7 JumpList
  17. /// </summary>
  18. public class JumpListIntegration : IHandle<FileOpenEvent>, IHandle<AppReadyEvent>, IDisposable
  19. {
  20. readonly ISettingsProvider settingsService;
  21. JumpList jumpList;
  22. public JumpListIntegration(ISettingsProvider settingsService)
  23. {
  24. this.settingsService = settingsService;
  25. }
  26. public void Handle(FileOpenEvent message)
  27. {
  28. var x = new Thread(new ParameterizedThreadStart(delegate { OpenFileAsync(message.Path); }));
  29. x.SetApartmentState(ApartmentState.STA);
  30. x.Start();
  31. }
  32. public void OpenFileAsync(string openedFile)
  33. {
  34. if (!IsWin7OrAbove())
  35. return;
  36. var currentFiles = jumpList.JumpItems.OfType<JumpTask>().Select(t => t.Arguments).ToList();
  37. if (currentFiles.Contains(openedFile))
  38. {
  39. // find file in list
  40. var settings = settingsService.GetSettings<MarkPadSettings>();
  41. var index = settings.RecentFiles.IndexOf(openedFile);
  42. if (index >= 0)
  43. settings.RecentFiles.RemoveAt(index);
  44. settings.RecentFiles.Insert(0, openedFile);
  45. settingsService.SaveSettings(settings);
  46. // Sometimes the settings and the jumplist can get out of sequence.
  47. index = currentFiles.IndexOf(openedFile);
  48. if (index >= 0) jumpList.JumpItems.RemoveAt(index);
  49. InsertFileFirst(openedFile);
  50. }
  51. else
  52. {
  53. // update settings
  54. var settings = settingsService.GetSettings<MarkPadSettings>();
  55. settings.RecentFiles.Insert(0, openedFile);
  56. if (settings.RecentFiles.Count > 5)
  57. settings.RecentFiles.RemoveAt(5);
  58. settingsService.SaveSettings(settings);
  59. InsertFileFirst(openedFile);
  60. }
  61. }
  62. private void InsertFileFirst(string openedFile)
  63. {
  64. if (!IsWin7OrAbove())
  65. return;
  66. if (jumpList != null)
  67. {
  68. var item = CreateJumpListItem(openedFile);
  69. jumpList.JumpItems.Insert(0, item);
  70. jumpList.Apply();
  71. }
  72. }
  73. public void Handle(AppReadyEvent message)
  74. {
  75. if (!IsWin7OrAbove())
  76. return;
  77. jumpList = GetJumpList();
  78. var x = new Thread(new ParameterizedThreadStart(delegate { PopulateJumpList(settingsService.GetSettings<MarkPadSettings>().RecentFiles); }));
  79. x.SetApartmentState(ApartmentState.STA);
  80. x.Start();
  81. }
  82. public void Dispose()
  83. {
  84. //settingsService.Save();
  85. }
  86. private void PopulateJumpList(IEnumerable<string> recentFiles)
  87. {
  88. if (!IsWin7OrAbove())
  89. return;
  90. if (recentFiles == null) return;
  91. foreach (var file in recentFiles.Distinct())
  92. {
  93. if (!File.Exists(file)) continue;
  94. var item = CreateJumpListItem(file);
  95. jumpList.JumpItems.Add(item);
  96. }
  97. jumpList.Apply();
  98. }
  99. private static JumpItem CreateJumpListItem(string file)
  100. {
  101. if (!IsWin7OrAbove())
  102. return null;
  103. var path = Assembly.GetEntryAssembly().CodeBase;
  104. return new JumpTask
  105. {
  106. Arguments = file,
  107. ApplicationPath = path,
  108. IconResourcePath = Path.Combine(Constants.IconDir, Constants.Icons[0]),
  109. Title = new FileInfo(file).Name,
  110. CustomCategory = "Recent Files"
  111. };
  112. }
  113. private static bool IsWin7OrAbove()
  114. {
  115. // check for Windows7
  116. var os = Environment.OSVersion.Version;
  117. if (os.Major < 6) return false;
  118. if (os.Minor < 1) return false;
  119. return true;
  120. }
  121. private static JumpList GetJumpList()
  122. {
  123. if (!IsWin7OrAbove())
  124. return null;
  125. var list = JumpList.GetJumpList(Application.Current);
  126. if (list != null) return list;
  127. list = new JumpList { ShowFrequentCategory = false, ShowRecentCategory = false };
  128. JumpList.SetJumpList(Application.Current, list);
  129. return list;
  130. }
  131. }
  132. }