SettingHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using ComPDFKitViewer.AnnotEvent;
  2. using PDFSettings;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Dynamic;
  8. using System.Windows;
  9. using System.IO;
  10. using PDF_Master.Properties;
  11. using PDF_Master;
  12. namespace PDF_Master.Helper
  13. {
  14. public class SettingHelper
  15. {
  16. public static void SortRecentOpenFiles(string filePath)
  17. {
  18. if (Settings.Default.RecentOpenFiles == null)
  19. Settings.Default.RecentOpenFiles = new RecentOpenFiles();
  20. OpenFileInfo existFile = null;
  21. foreach (var file in Settings.Default.RecentOpenFiles)
  22. {
  23. if (file.FilePath.ToLower() == filePath.ToLower())
  24. {
  25. if(file.FilePath != filePath)
  26. {
  27. file.FilePath = filePath;
  28. file.FileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
  29. }
  30. file.LastOpenTime = DateTime.Now;
  31. existFile = file;
  32. break;
  33. }
  34. }
  35. if (existFile != null)
  36. {
  37. if (Settings.Default.RecentOpenFiles.IndexOf(existFile) != 0)
  38. {
  39. Settings.Default.RecentOpenFiles.Remove(existFile);
  40. Settings.Default.RecentOpenFiles.Insert(0, existFile);
  41. }
  42. }
  43. else
  44. {
  45. OpenFileInfo fileInfo = new OpenFileInfo();
  46. fileInfo.LastFitMode = Settings.Default.AppProperties.InitialVIew.ZoomMode;
  47. fileInfo.LastViewMode = Settings.Default.AppProperties.InitialVIew.PageView;
  48. fileInfo.FileName = Path.GetFileName(filePath);
  49. fileInfo.FilePath = filePath;
  50. fileInfo.IsGuidPDF = string.Equals(filePath, App.GuidPDFPath);
  51. fileInfo.ThumbImgPath = "";
  52. fileInfo.LastOpenTime = DateTime.Now;
  53. PDF_Master.Properties.Settings.Default.RecentOpenFiles.Insert(0, fileInfo);
  54. }
  55. //限制最近文件列表显示的最大条数
  56. if(Settings.Default.RecentOpenFiles.Count>Settings.Default.AppProperties.Description.FileCountInRecentFiles)
  57. {
  58. Settings.Default.RecentOpenFiles.RemoveRange(Settings.Default.AppProperties.Description.FileCountInRecentFiles, Settings.Default.RecentOpenFiles.Count-Settings.Default.AppProperties.Description.FileCountInRecentFiles);
  59. }
  60. Settings.Default.Save();
  61. }
  62. public static void RemoveRecentOpenFile(string filePath)
  63. {
  64. if (Settings.Default.RecentOpenFiles == null || Settings.Default.RecentOpenFiles.Count == 0)
  65. return;
  66. OpenFileInfo deleteItem = null;
  67. foreach(var item in Settings.Default.RecentOpenFiles)
  68. {
  69. if (item.FilePath.ToLower() == filePath.ToLower())
  70. {
  71. deleteItem = item;
  72. break;
  73. }
  74. }
  75. if(deleteItem!=null)
  76. {
  77. DeleteByPath(deleteItem.ThumbImgPath);
  78. Settings.Default.RecentOpenFiles.Remove(deleteItem);
  79. Settings.Default.Save();
  80. }
  81. }
  82. public static void DeleteByPath(string path)
  83. {
  84. try
  85. {
  86. if (!string.IsNullOrEmpty(path) && System.IO.File.Exists(path))
  87. System.IO.File.Delete(path);
  88. }
  89. catch { }
  90. }
  91. public static void RemoveAllRecentOpenFiles()
  92. {
  93. if (Settings.Default.RecentOpenFiles == null || Settings.Default.RecentOpenFiles.Count == 0)
  94. return;
  95. string folderPath = System.IO.Path.Combine(App.CurrentPath, "CoverImage");
  96. DirectoryInfo directoryInfo = new DirectoryInfo(folderPath);
  97. if (directoryInfo.Exists)
  98. {
  99. var files = directoryInfo.GetFiles();
  100. foreach (var file in files)
  101. {
  102. if (file.Exists)
  103. file.Delete();
  104. }
  105. }
  106. Settings.Default.RecentOpenFiles.Clear();
  107. Settings.Default.Save();
  108. }
  109. public static OpenFileInfo GetFileInfo(string filePath)
  110. {
  111. if (Settings.Default.RecentOpenFiles == null || Settings.Default.RecentOpenFiles.Count == 0)
  112. return null;
  113. foreach(var info in Settings.Default.RecentOpenFiles)
  114. {
  115. if (info.FilePath == filePath)
  116. return info;
  117. }
  118. return null;
  119. }
  120. public static void SetFileInfo(OpenFileInfo openFileInfo)
  121. {
  122. try
  123. {
  124. if (Settings.Default.RecentOpenFiles == null || Settings.Default.RecentOpenFiles.Count == 0 || openFileInfo == null)
  125. return;
  126. for (int i = 0; i < Settings.Default.RecentOpenFiles.Count; i++)
  127. {
  128. if (Settings.Default.RecentOpenFiles[i].FilePath == openFileInfo.FilePath)
  129. {
  130. Settings.Default.RecentOpenFiles[i] = openFileInfo;
  131. Settings.Default.Save();//保存时有可能会文件被占用
  132. return;
  133. }
  134. }
  135. }
  136. catch { }
  137. }
  138. #region 缓存注释属性
  139. public static DefaultAnnotProperty GetAnnotDefaultProperty(AnnotArgsType annotToolType, string saveKey = "")
  140. {
  141. if (Settings.Default.DefautAnnotProperties != null)
  142. {
  143. if (saveKey == "")
  144. {
  145. return Settings.Default.DefautAnnotProperties.GetAnnotProperty(annotToolType);
  146. }
  147. return Settings.Default.DefautAnnotProperties.GetAnnotProperty(annotToolType, saveKey);
  148. }
  149. return null;
  150. }
  151. public static void SetAnnotDefaultProperty(DefaultAnnotProperty annotProperty)
  152. {
  153. if (Settings.Default.DefautAnnotProperties == null)
  154. {
  155. Settings.Default.DefautAnnotProperties = new DefaultAnnotProperties();
  156. }
  157. Settings.Default.DefautAnnotProperties.SetAnnotProperty(annotProperty);
  158. Settings.Default.Save();
  159. }
  160. #endregion 缓存注释属性
  161. #region 缓存颜色列表
  162. public static void SetColorSelector(ColorSelectorItem selectorItem)
  163. {
  164. if (Settings.Default.ColorSelectors == null)
  165. {
  166. Settings.Default.ColorSelectors = new ColorSelectorList();
  167. }
  168. Settings.Default.ColorSelectors.SetColorSelector(selectorItem);
  169. Settings.Default.Save();
  170. }
  171. public static ColorSelectorItem GetColorSelectorItem(ColorSelectorType selectorType,int Index)
  172. {
  173. if (Settings.Default.ColorSelectors != null)
  174. {
  175. return Settings.Default.ColorSelectors.GetColorSelector(selectorType, Index);
  176. }
  177. return null;
  178. }
  179. #endregion 缓存颜色列表
  180. private static string KeyAlaphabet = "0123456789ABCDEFGHIGKLMNOPQRSTUVWXYZ";
  181. public static char NumberToChar(int num)
  182. {
  183. if (num >= 0 && num <= 35)
  184. {
  185. return KeyAlaphabet[num];
  186. }
  187. throw new ArgumentException("Wrong num value.", "ch");
  188. }
  189. /// <summary>
  190. /// AES加密
  191. /// </summary>
  192. /// <param name="text">加密字符</param>
  193. /// <param name="password">加密的密码</param>
  194. /// <returns></returns>
  195. public static string AESEncrypt(string text, string password)
  196. {
  197. RijndaelManaged rijndaelCipher = new RijndaelManaged();
  198. rijndaelCipher.Mode = CipherMode.CBC;
  199. rijndaelCipher.Padding = PaddingMode.PKCS7;
  200. rijndaelCipher.KeySize = 128;
  201. rijndaelCipher.BlockSize = 128;
  202. byte[] pwdBytes = Encoding.UTF8.GetBytes(password);
  203. byte[] keyBytes = new byte[16];
  204. int len = pwdBytes.Length;
  205. if (len > keyBytes.Length) len = keyBytes.Length;
  206. Array.Copy(pwdBytes, keyBytes, len);
  207. rijndaelCipher.Key = keyBytes;
  208. rijndaelCipher.IV = new byte[16];
  209. ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
  210. byte[] plainText = Encoding.UTF8.GetBytes(text);
  211. byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
  212. return Convert.ToBase64String(cipherBytes);
  213. }
  214. /// <summary>
  215. /// AES解密
  216. /// </summary>
  217. /// <param name="text"></param>
  218. /// <param name="password"></param>
  219. /// <returns></returns>
  220. public static string AESDecrypt(string text, string password)
  221. {
  222. RijndaelManaged rijndaelCipher = new RijndaelManaged();
  223. rijndaelCipher.Mode = CipherMode.CBC;
  224. rijndaelCipher.Padding = PaddingMode.PKCS7;
  225. rijndaelCipher.KeySize = 128;
  226. rijndaelCipher.BlockSize = 128;
  227. byte[] encryptedData = Convert.FromBase64String(text);
  228. byte[] pwdBytes = Encoding.UTF8.GetBytes(password);
  229. byte[] keyBytes = new byte[16];
  230. int len = pwdBytes.Length;
  231. if (len > keyBytes.Length) len = keyBytes.Length;
  232. Array.Copy(pwdBytes, keyBytes, len);
  233. rijndaelCipher.Key = keyBytes;
  234. rijndaelCipher.IV = new byte[16];
  235. ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
  236. byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
  237. return Encoding.UTF8.GetString(plainText);
  238. }
  239. public static ProductActiveInfo GetProductActiveInfo()
  240. {
  241. //if (Settings.Default.ProductActiveStore != null)
  242. //{
  243. // ProductActiveInfo activeInfo = new ProductActiveInfo();
  244. // ProductActiveStore storeInfo= Settings.Default.ProductActiveStore;
  245. // Random random = new Random((int)storeInfo.UpdateTime.Ticks);
  246. // StringBuilder keyBuilder = new StringBuilder();
  247. // for (int i = 0; i < 16; i++)
  248. // {
  249. // keyBuilder.Append(NumberToChar(random.Next(0, 35)));
  250. // }
  251. // string checkData = AESEncrypt(storeInfo.SignData, keyBuilder.ToString());
  252. // MD5 mD5 = MD5.Create();
  253. // byte[] hashCode = mD5.ComputeHash(Encoding.UTF8.GetBytes(checkData));
  254. // StringBuilder checkBuilder = new StringBuilder();
  255. // foreach (byte code in hashCode)
  256. // {
  257. // checkBuilder.Append(code.ToString("X2"));
  258. // }
  259. // if (checkBuilder.ToString() == storeInfo.CheckSum)
  260. // {
  261. // string decryptData = AESDecrypt(storeInfo.SignData, keyBuilder.ToString());
  262. // activeInfo = JsonConvert.DeserializeObject<ProductActiveInfo>(decryptData);
  263. // }
  264. // return activeInfo;
  265. //}
  266. return null;
  267. }
  268. public static void SetProductActiveStore(ProductActiveInfo activeInfo)
  269. {
  270. //ProductActiveStore activeStore = new ProductActiveStore();
  271. //activeStore.UpdateTime = DateTime.Now;
  272. //string rawData = JsonConvert.SerializeObject(activeInfo);
  273. //Random random = new Random((int)activeStore.UpdateTime.Ticks);
  274. //StringBuilder keyBuilder = new StringBuilder();
  275. //for (int i = 0; i < 16; i++)
  276. //{
  277. // keyBuilder.Append(NumberToChar(random.Next(0, 35)));
  278. //}
  279. //activeStore.SignData = AESEncrypt(rawData, keyBuilder.ToString());
  280. //string checkData = AESEncrypt(activeStore.SignData, keyBuilder.ToString());
  281. //MD5 mD5 = MD5.Create();
  282. //byte[] hashCode = mD5.ComputeHash(Encoding.UTF8.GetBytes(checkData));
  283. //StringBuilder checkBuilder = new StringBuilder();
  284. //foreach(byte code in hashCode)
  285. //{
  286. // checkBuilder.Append(code.ToString("X2"));
  287. //}
  288. //activeStore.CheckSum = checkBuilder.ToString();
  289. //Settings.Default.ProductActiveStore = activeStore;
  290. //Settings.Default.Save();
  291. }
  292. }
  293. }