PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src/MostRecentFiles.cs

https://bitbucket.org/tuldok89/openpdn
C# | 207 lines | 164 code | 32 blank | 11 comment | 19 complexity | 9aee391a77dccad079bfe6bb4b679eda MD5 | raw file
  1. /////////////////////////////////////////////////////////////////////////////////
  2. // Paint.NET //
  3. // Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors. //
  4. // Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
  5. // See src/Resources/Files/License.txt for full licensing and attribution //
  6. // details. //
  7. // . //
  8. /////////////////////////////////////////////////////////////////////////////////
  9. using System.Linq;
  10. using PaintDotNet.SystemLayer;
  11. using System;
  12. using System.Collections;
  13. using System.Drawing;
  14. namespace PaintDotNet
  15. {
  16. /// <summary>
  17. /// Data structure to manage the Most Recently Used list of files.
  18. /// </summary>
  19. internal class MostRecentFiles
  20. {
  21. private Queue _files; // contains MostRecentFile instances
  22. private readonly int _maxCount;
  23. private const int iconSize = 56;
  24. public MostRecentFiles(int maxCount)
  25. {
  26. Loaded = false;
  27. _maxCount = maxCount;
  28. _files = new Queue();
  29. }
  30. public bool Loaded { get; private set; }
  31. public int Count
  32. {
  33. get
  34. {
  35. if (!Loaded)
  36. {
  37. LoadMruList();
  38. }
  39. return _files.Count;
  40. }
  41. }
  42. public int MaxCount
  43. {
  44. get
  45. {
  46. return _maxCount;
  47. }
  48. }
  49. public int IconSize
  50. {
  51. get
  52. {
  53. return UI.ScaleWidth(iconSize);
  54. }
  55. }
  56. public MostRecentFile[] GetFileList()
  57. {
  58. if (!Loaded)
  59. {
  60. LoadMruList();
  61. }
  62. object[] array = _files.ToArray();
  63. var mrfArray = new MostRecentFile[array.Length];
  64. array.CopyTo(mrfArray, 0);
  65. return mrfArray;
  66. }
  67. public bool Contains(string fileName)
  68. {
  69. if (!Loaded)
  70. {
  71. LoadMruList();
  72. }
  73. string lcFileName = fileName.ToLower();
  74. return (from MostRecentFile mrf in _files select mrf.FileName.ToLower()).Any(lcMrf => 0 == String.Compare(lcMrf, lcFileName));
  75. }
  76. public void Add(MostRecentFile mrf)
  77. {
  78. if (!Loaded)
  79. {
  80. LoadMruList();
  81. }
  82. if (Contains(mrf.FileName)) return;
  83. _files.Enqueue(mrf);
  84. while (_files.Count > _maxCount)
  85. {
  86. _files.Dequeue();
  87. }
  88. }
  89. public void Remove(string fileName)
  90. {
  91. if (!Loaded)
  92. {
  93. LoadMruList();
  94. }
  95. if (!Contains(fileName))
  96. {
  97. return;
  98. }
  99. var newQueue = new Queue();
  100. foreach (MostRecentFile mrf in
  101. _files.Cast<MostRecentFile>().Where(mrf => 0 != string.Compare(mrf.FileName, fileName, true)))
  102. {
  103. newQueue.Enqueue(mrf);
  104. }
  105. _files = newQueue;
  106. }
  107. public void Clear()
  108. {
  109. if (!Loaded)
  110. {
  111. LoadMruList();
  112. }
  113. foreach (MostRecentFile mrf in GetFileList())
  114. {
  115. Remove(mrf.FileName);
  116. }
  117. }
  118. public void LoadMruList()
  119. {
  120. try
  121. {
  122. Loaded = true;
  123. Clear();
  124. for (int i = 0; i < MaxCount; ++i)
  125. {
  126. try
  127. {
  128. string mruName = "MRU" + i;
  129. var fileName = Settings.CurrentUser.GetString(mruName);
  130. if (fileName != null)
  131. {
  132. Image thumb = Settings.CurrentUser.GetImage(mruName + "Thumb");
  133. if (thumb != null)
  134. {
  135. var mrf = new MostRecentFile(fileName, thumb);
  136. Add(mrf);
  137. }
  138. }
  139. }
  140. catch
  141. {
  142. break;
  143. }
  144. }
  145. }
  146. catch (Exception ex)
  147. {
  148. Tracing.Ping("Exception when loading MRU list: " + ex.ToString());
  149. Clear();
  150. }
  151. }
  152. public void SaveMruList()
  153. {
  154. if (!Loaded) return;
  155. Settings.CurrentUser.SetInt32(SettingNames.MruMax, MaxCount);
  156. MostRecentFile[] mrfArray = GetFileList();
  157. for (int i = 0; i < MaxCount; ++i)
  158. {
  159. string mruName = "MRU" + i;
  160. string mruThumbName = mruName + "Thumb";
  161. if (i >= mrfArray.Length)
  162. {
  163. Settings.CurrentUser.Delete(mruName);
  164. Settings.CurrentUser.Delete(mruThumbName);
  165. }
  166. else
  167. {
  168. MostRecentFile mrf = mrfArray[i];
  169. Settings.CurrentUser.SetString(mruName, mrf.FileName);
  170. Settings.CurrentUser.SetImage(mruThumbName, mrf.Thumb);
  171. }
  172. }
  173. }
  174. }
  175. }