SettingHelper.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 DefaultEditProperty GetPDFEditDefaultProperty(ComPDFKit.PDFPage.CPDFEditType editType, string saveKey = "")
  163. {
  164. if (Settings.Default.DefaultEditProperties != null)
  165. {
  166. if (saveKey == "")
  167. {
  168. return Settings.Default.DefaultEditProperties.GetEditProperty(editType);
  169. }
  170. return Settings.Default.DefaultEditProperties.GetEditProperty(editType, saveKey);
  171. }
  172. return null;
  173. }
  174. public static void SetPDFEditProperty(DefaultEditProperty editType)
  175. {
  176. if (Settings.Default.DefaultEditProperties == null)
  177. {
  178. Settings.Default.DefaultEditProperties = new DefaultEditProperties();
  179. }
  180. Settings.Default.DefaultEditProperties.SetEditProperty(editType);
  181. Settings.Default.Save();
  182. }
  183. #endregion 缓存编辑属性
  184. #region 缓存颜色列表
  185. public static void SetColorSelector(ColorSelectorItem selectorItem)
  186. {
  187. if (Settings.Default.ColorSelectors == null)
  188. {
  189. Settings.Default.ColorSelectors = new ColorSelectorList();
  190. }
  191. Settings.Default.ColorSelectors.SetColorSelector(selectorItem);
  192. Settings.Default.Save();
  193. }
  194. public static ColorSelectorItem GetColorSelectorItem(ColorSelectorType selectorType,int Index)
  195. {
  196. if (Settings.Default.ColorSelectors != null)
  197. {
  198. return Settings.Default.ColorSelectors.GetColorSelector(selectorType, Index);
  199. }
  200. return null;
  201. }
  202. #endregion 缓存颜色列表
  203. private static string KeyAlaphabet = "0123456789ABCDEFGHIGKLMNOPQRSTUVWXYZ";
  204. public static char NumberToChar(int num)
  205. {
  206. if (num >= 0 && num <= 35)
  207. {
  208. return KeyAlaphabet[num];
  209. }
  210. throw new ArgumentException("Wrong num value.", "ch");
  211. }
  212. /// <summary>
  213. /// AES加密
  214. /// </summary>
  215. /// <param name="text">加密字符</param>
  216. /// <param name="password">加密的密码</param>
  217. /// <returns></returns>
  218. public static string AESEncrypt(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[] pwdBytes = Encoding.UTF8.GetBytes(password);
  226. byte[] keyBytes = new byte[16];
  227. int len = pwdBytes.Length;
  228. if (len > keyBytes.Length) len = keyBytes.Length;
  229. Array.Copy(pwdBytes, keyBytes, len);
  230. rijndaelCipher.Key = keyBytes;
  231. rijndaelCipher.IV = new byte[16];
  232. ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
  233. byte[] plainText = Encoding.UTF8.GetBytes(text);
  234. byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
  235. return Convert.ToBase64String(cipherBytes);
  236. }
  237. /// <summary>
  238. /// AES解密
  239. /// </summary>
  240. /// <param name="text"></param>
  241. /// <param name="password"></param>
  242. /// <returns></returns>
  243. public static string AESDecrypt(string text, string password)
  244. {
  245. RijndaelManaged rijndaelCipher = new RijndaelManaged();
  246. rijndaelCipher.Mode = CipherMode.CBC;
  247. rijndaelCipher.Padding = PaddingMode.PKCS7;
  248. rijndaelCipher.KeySize = 128;
  249. rijndaelCipher.BlockSize = 128;
  250. byte[] encryptedData = Convert.FromBase64String(text);
  251. byte[] pwdBytes = Encoding.UTF8.GetBytes(password);
  252. byte[] keyBytes = new byte[16];
  253. int len = pwdBytes.Length;
  254. if (len > keyBytes.Length) len = keyBytes.Length;
  255. Array.Copy(pwdBytes, keyBytes, len);
  256. rijndaelCipher.Key = keyBytes;
  257. rijndaelCipher.IV = new byte[16];
  258. ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
  259. byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
  260. return Encoding.UTF8.GetString(plainText);
  261. }
  262. public static ProductActiveInfo GetProductActiveInfo()
  263. {
  264. //if (Settings.Default.ProductActiveStore != null)
  265. //{
  266. // ProductActiveInfo activeInfo = new ProductActiveInfo();
  267. // ProductActiveStore storeInfo= Settings.Default.ProductActiveStore;
  268. // Random random = new Random((int)storeInfo.UpdateTime.Ticks);
  269. // StringBuilder keyBuilder = new StringBuilder();
  270. // for (int i = 0; i < 16; i++)
  271. // {
  272. // keyBuilder.Append(NumberToChar(random.Next(0, 35)));
  273. // }
  274. // string checkData = AESEncrypt(storeInfo.SignData, keyBuilder.ToString());
  275. // MD5 mD5 = MD5.Create();
  276. // byte[] hashCode = mD5.ComputeHash(Encoding.UTF8.GetBytes(checkData));
  277. // StringBuilder checkBuilder = new StringBuilder();
  278. // foreach (byte code in hashCode)
  279. // {
  280. // checkBuilder.Append(code.ToString("X2"));
  281. // }
  282. // if (checkBuilder.ToString() == storeInfo.CheckSum)
  283. // {
  284. // string decryptData = AESDecrypt(storeInfo.SignData, keyBuilder.ToString());
  285. // activeInfo = JsonConvert.DeserializeObject<ProductActiveInfo>(decryptData);
  286. // }
  287. // return activeInfo;
  288. //}
  289. return null;
  290. }
  291. public static void SetProductActiveStore(ProductActiveInfo activeInfo)
  292. {
  293. //ProductActiveStore activeStore = new ProductActiveStore();
  294. //activeStore.UpdateTime = DateTime.Now;
  295. //string rawData = JsonConvert.SerializeObject(activeInfo);
  296. //Random random = new Random((int)activeStore.UpdateTime.Ticks);
  297. //StringBuilder keyBuilder = new StringBuilder();
  298. //for (int i = 0; i < 16; i++)
  299. //{
  300. // keyBuilder.Append(NumberToChar(random.Next(0, 35)));
  301. //}
  302. //activeStore.SignData = AESEncrypt(rawData, keyBuilder.ToString());
  303. //string checkData = AESEncrypt(activeStore.SignData, keyBuilder.ToString());
  304. //MD5 mD5 = MD5.Create();
  305. //byte[] hashCode = mD5.ComputeHash(Encoding.UTF8.GetBytes(checkData));
  306. //StringBuilder checkBuilder = new StringBuilder();
  307. //foreach(byte code in hashCode)
  308. //{
  309. // checkBuilder.Append(code.ToString("X2"));
  310. //}
  311. //activeStore.CheckSum = checkBuilder.ToString();
  312. //Settings.Default.ProductActiveStore = activeStore;
  313. //Settings.Default.Save();
  314. }
  315. }
  316. }