SettingHelper.cs 12 KB

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