SettingHelper.cs 12 KB

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