SettingHelper.cs 12 KB

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