MostRecentFiles.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using PaintDotNet.SystemLayer;
  2. using System;
  3. using System.Collections;
  4. using System.Drawing;
  5. namespace PaintDotNet
  6. {
  7. /// <summary>
  8. /// 用于管理“最近使用的”文件列表
  9. /// </summary>
  10. internal class MostRecentFiles
  11. {
  12. /// <summary>
  13. /// 表示先进先出的最近的文件实例
  14. /// </summary>
  15. private Queue files;
  16. private int maxCount;
  17. private const int iconSize = 56;
  18. private bool loaded = false;
  19. public MostRecentFiles(int maxCount)
  20. {
  21. this.maxCount = maxCount;
  22. this.files = new Queue();
  23. }
  24. public bool Loaded
  25. {
  26. get
  27. {
  28. return this.loaded;
  29. }
  30. }
  31. public int Count
  32. {
  33. get
  34. {
  35. if (!this.loaded)
  36. {
  37. LoadMruList();
  38. }
  39. return this.files.Count;
  40. }
  41. }
  42. public int MaxCount
  43. {
  44. get
  45. {
  46. return this.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. MostRecentFile[] 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. foreach (MostRecentFile mrf in files)
  75. {
  76. string lcMrf = mrf.FileName.ToLower();
  77. if (0 == String.Compare(lcMrf, lcFileName))
  78. {
  79. return true;
  80. }
  81. }
  82. return false;
  83. }
  84. public void Add(MostRecentFile mrf)
  85. {
  86. if (!Loaded)
  87. {
  88. LoadMruList();
  89. }
  90. if (!Contains(mrf.FileName))
  91. {
  92. files.Enqueue(mrf);
  93. while (files.Count > maxCount)
  94. {
  95. files.Dequeue();
  96. }
  97. }
  98. }
  99. public void Remove(string fileName)
  100. {
  101. if (!Loaded)
  102. {
  103. LoadMruList();
  104. }
  105. if (!Contains(fileName))
  106. {
  107. return;
  108. }
  109. Queue newQueue = new Queue();
  110. foreach (MostRecentFile mrf in files)
  111. {
  112. if (0 != string.Compare(mrf.FileName, fileName, true))
  113. {
  114. newQueue.Enqueue(mrf);
  115. }
  116. }
  117. this.files = newQueue;
  118. }
  119. public void Clear()
  120. {
  121. if (!Loaded)
  122. {
  123. LoadMruList();
  124. }
  125. foreach (MostRecentFile mrf in this.GetFileList())
  126. {
  127. Remove(mrf.FileName);
  128. }
  129. }
  130. public void LoadMruList()
  131. {
  132. try
  133. {
  134. this.loaded = true;
  135. Clear();
  136. for (int i = 0; i < MaxCount; ++i)
  137. {
  138. try
  139. {
  140. string mruName = "MRU" + i.ToString();
  141. string fileName = (string)Settings.CurrentUser.GetString(mruName);
  142. if (fileName != null)
  143. {
  144. //Image thumb = Settings.CurrentUser.GetImage(mruName + "Thumb");
  145. if (fileName != null)// && thumb != null
  146. {
  147. MostRecentFile mrf = new MostRecentFile(fileName, null);//thumb
  148. Add(mrf);
  149. }
  150. }
  151. }
  152. catch
  153. {
  154. break;
  155. }
  156. }
  157. }
  158. catch (Exception ex)
  159. {
  160. Tracing.Ping("Exception when loading MRU list: " + ex.ToString());
  161. Clear();
  162. }
  163. }
  164. public void SaveMruList()
  165. {
  166. if (Loaded)
  167. {
  168. MostRecentFile[] mrfArray = GetFileList();
  169. for (int i = 0; i < MaxCount; ++i)
  170. {
  171. string mruName = "MRU" + i.ToString();
  172. string mruThumbName = mruName + "Thumb";
  173. if (i >= mrfArray.Length)
  174. {
  175. Settings.CurrentUser.Delete(mruName);
  176. Settings.CurrentUser.Delete(mruThumbName);
  177. }
  178. else
  179. {
  180. MostRecentFile mrf = mrfArray[i];
  181. Settings.CurrentUser.SetString(mruName, mrf.FileName);
  182. //Settings.CurrentUser.SetImage(mruThumbName, mrf.Thumb);
  183. }
  184. }
  185. }
  186. }
  187. }
  188. }