123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- using ComPDFKitViewer.AnnotEvent;
- using PDFSettings;
- using System;
- using System.Collections.Generic;
- using System.Security.Cryptography;
- using System.Text;
- using System.Dynamic;
- using System.Windows;
- using System.IO;
- using PDF_Master.Properties;
- using PDF_Master;
- namespace PDF_Master.Helper
- {
- public class SettingHelper
- {
- public static void SortRecentOpenFiles(string filePath)
- {
- if (Settings.Default.RecentOpenFiles == null)
- Settings.Default.RecentOpenFiles = new RecentOpenFiles();
- OpenFileInfo existFile = null;
- foreach (var file in Settings.Default.RecentOpenFiles)
- {
- if (file.FilePath.ToLower() == filePath.ToLower())
- {
- if(file.FilePath != filePath)
- {
- file.FilePath = filePath;
- file.FileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
- }
- file.LastOpenTime = DateTime.Now;
- existFile = file;
- break;
- }
- }
- if (existFile != null)
- {
- if (Settings.Default.RecentOpenFiles.IndexOf(existFile) != 0)
- {
- Settings.Default.RecentOpenFiles.Remove(existFile);
- Settings.Default.RecentOpenFiles.Insert(0, existFile);
- }
- }
- else
- {
- OpenFileInfo fileInfo = new OpenFileInfo();
- fileInfo.LastFitMode = Settings.Default.AppProperties.InitialVIew.ZoomMode;
- fileInfo.LastViewMode = Settings.Default.AppProperties.InitialVIew.PageView;
- fileInfo.FileName = Path.GetFileName(filePath);
- fileInfo.FilePath = filePath;
- fileInfo.IsGuidPDF = string.Equals(filePath, App.GuidPDFPath);
- fileInfo.ThumbImgPath = "";
- fileInfo.LastOpenTime = DateTime.Now;
- PDF_Master.Properties.Settings.Default.RecentOpenFiles.Insert(0, fileInfo);
- }
- //限制最近文件列表显示的最大条数
- if(Settings.Default.RecentOpenFiles.Count>Settings.Default.AppProperties.Description.FileCountInRecentFiles)
- {
- Settings.Default.RecentOpenFiles.RemoveRange(Settings.Default.AppProperties.Description.FileCountInRecentFiles, Settings.Default.RecentOpenFiles.Count-Settings.Default.AppProperties.Description.FileCountInRecentFiles);
- }
- Settings.Default.Save();
- }
- public static void RemoveRecentOpenFile(string filePath)
- {
- if (Settings.Default.RecentOpenFiles == null || Settings.Default.RecentOpenFiles.Count == 0)
- return;
- OpenFileInfo deleteItem = null;
- foreach(var item in Settings.Default.RecentOpenFiles)
- {
- if (item.FilePath.ToLower() == filePath.ToLower())
- {
- deleteItem = item;
- break;
- }
- }
- if(deleteItem!=null)
- {
- DeleteByPath(deleteItem.ThumbImgPath);
- Settings.Default.RecentOpenFiles.Remove(deleteItem);
- Settings.Default.Save();
- }
- }
- public static void DeleteByPath(string path)
- {
- try
- {
- if (!string.IsNullOrEmpty(path) && System.IO.File.Exists(path))
- System.IO.File.Delete(path);
- }
- catch { }
- }
- public static void RemoveAllRecentOpenFiles()
- {
- if (Settings.Default.RecentOpenFiles == null || Settings.Default.RecentOpenFiles.Count == 0)
- return;
- string folderPath = System.IO.Path.Combine(App.CurrentPath, "CoverImage");
- DirectoryInfo directoryInfo = new DirectoryInfo(folderPath);
- if (directoryInfo.Exists)
- {
- var files = directoryInfo.GetFiles();
- foreach (var file in files)
- {
- if (file.Exists)
- file.Delete();
- }
- }
- Settings.Default.RecentOpenFiles.Clear();
- Settings.Default.Save();
- }
- public static OpenFileInfo GetFileInfo(string filePath)
- {
- if (Settings.Default.RecentOpenFiles == null || Settings.Default.RecentOpenFiles.Count == 0)
- return null;
- foreach(var info in Settings.Default.RecentOpenFiles)
- {
- if (info.FilePath == filePath)
- return info;
- }
- return null;
- }
- public static void SetFileInfo(OpenFileInfo openFileInfo)
- {
- try
- {
- if (Settings.Default.RecentOpenFiles == null || Settings.Default.RecentOpenFiles.Count == 0 || openFileInfo == null)
- return;
- for (int i = 0; i < Settings.Default.RecentOpenFiles.Count; i++)
- {
- if (Settings.Default.RecentOpenFiles[i].FilePath == openFileInfo.FilePath)
- {
- Settings.Default.RecentOpenFiles[i] = openFileInfo;
- Settings.Default.Save();//保存时有可能会文件被占用
- return;
- }
- }
- }
- catch { }
- }
- #region 缓存注释属性
- public static DefaultAnnotProperty GetAnnotDefaultProperty(AnnotArgsType annotToolType, string saveKey = "")
- {
- if (Settings.Default.DefautAnnotProperties != null)
- {
- if (saveKey == "")
- {
- return Settings.Default.DefautAnnotProperties.GetAnnotProperty(annotToolType);
- }
- return Settings.Default.DefautAnnotProperties.GetAnnotProperty(annotToolType, saveKey);
- }
- return null;
- }
- public static void SetAnnotDefaultProperty(DefaultAnnotProperty annotProperty)
- {
- if (Settings.Default.DefautAnnotProperties == null)
- {
- Settings.Default.DefautAnnotProperties = new DefaultAnnotProperties();
- }
- Settings.Default.DefautAnnotProperties.SetAnnotProperty(annotProperty);
- Settings.Default.Save();
- }
- #endregion 缓存注释属性
- #region 缓存编辑属性
- public static DefaultEditProperty GetPDFEditDefaultProperty(ComPDFKit.PDFPage.CPDFEditType editType, string saveKey = "")
- {
- if (Settings.Default.DefaultEditProperties != null)
- {
- if (saveKey == "")
- {
- return Settings.Default.DefaultEditProperties.GetEditProperty(editType);
- }
- return Settings.Default.DefaultEditProperties.GetEditProperty(editType, saveKey);
- }
- return null;
- }
- public static void SetPDFEditProperty(DefaultEditProperty editType)
- {
- if (Settings.Default.DefaultEditProperties == null)
- {
- Settings.Default.DefaultEditProperties = new DefaultEditProperties();
- }
- Settings.Default.DefaultEditProperties.SetEditProperty(editType);
- Settings.Default.Save();
- }
- #endregion 缓存编辑属性
- #region 缓存颜色列表
- public static void SetColorSelector(ColorSelectorItem selectorItem)
- {
- if (Settings.Default.ColorSelectors == null)
- {
- Settings.Default.ColorSelectors = new ColorSelectorList();
- }
- Settings.Default.ColorSelectors.SetColorSelector(selectorItem);
- Settings.Default.Save();
- }
- public static ColorSelectorItem GetColorSelectorItem(ColorSelectorType selectorType,int Index)
- {
- if (Settings.Default.ColorSelectors != null)
- {
- return Settings.Default.ColorSelectors.GetColorSelector(selectorType, Index);
- }
- return null;
- }
- #endregion 缓存颜色列表
- private static string KeyAlaphabet = "0123456789ABCDEFGHIGKLMNOPQRSTUVWXYZ";
- public static char NumberToChar(int num)
- {
- if (num >= 0 && num <= 35)
- {
- return KeyAlaphabet[num];
- }
- throw new ArgumentException("Wrong num value.", "ch");
- }
- /// <summary>
- /// AES加密
- /// </summary>
- /// <param name="text">加密字符</param>
- /// <param name="password">加密的密码</param>
- /// <returns></returns>
- public static string AESEncrypt(string text, string password)
- {
- RijndaelManaged rijndaelCipher = new RijndaelManaged();
- rijndaelCipher.Mode = CipherMode.CBC;
- rijndaelCipher.Padding = PaddingMode.PKCS7;
- rijndaelCipher.KeySize = 128;
- rijndaelCipher.BlockSize = 128;
- byte[] pwdBytes = Encoding.UTF8.GetBytes(password);
- byte[] keyBytes = new byte[16];
- int len = pwdBytes.Length;
- if (len > keyBytes.Length) len = keyBytes.Length;
- Array.Copy(pwdBytes, keyBytes, len);
- rijndaelCipher.Key = keyBytes;
- rijndaelCipher.IV = new byte[16];
- ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
- byte[] plainText = Encoding.UTF8.GetBytes(text);
- byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
- return Convert.ToBase64String(cipherBytes);
- }
- /// <summary>
- /// AES解密
- /// </summary>
- /// <param name="text"></param>
- /// <param name="password"></param>
- /// <returns></returns>
- public static string AESDecrypt(string text, string password)
- {
- RijndaelManaged rijndaelCipher = new RijndaelManaged();
- rijndaelCipher.Mode = CipherMode.CBC;
- rijndaelCipher.Padding = PaddingMode.PKCS7;
- rijndaelCipher.KeySize = 128;
- rijndaelCipher.BlockSize = 128;
- byte[] encryptedData = Convert.FromBase64String(text);
- byte[] pwdBytes = Encoding.UTF8.GetBytes(password);
- byte[] keyBytes = new byte[16];
- int len = pwdBytes.Length;
- if (len > keyBytes.Length) len = keyBytes.Length;
- Array.Copy(pwdBytes, keyBytes, len);
- rijndaelCipher.Key = keyBytes;
- rijndaelCipher.IV = new byte[16];
- ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
- byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
- return Encoding.UTF8.GetString(plainText);
- }
- public static ProductActiveInfo GetProductActiveInfo()
- {
- //if (Settings.Default.ProductActiveStore != null)
- //{
- // ProductActiveInfo activeInfo = new ProductActiveInfo();
- // ProductActiveStore storeInfo= Settings.Default.ProductActiveStore;
- // Random random = new Random((int)storeInfo.UpdateTime.Ticks);
- // StringBuilder keyBuilder = new StringBuilder();
- // for (int i = 0; i < 16; i++)
- // {
- // keyBuilder.Append(NumberToChar(random.Next(0, 35)));
- // }
- // string checkData = AESEncrypt(storeInfo.SignData, keyBuilder.ToString());
- // MD5 mD5 = MD5.Create();
- // byte[] hashCode = mD5.ComputeHash(Encoding.UTF8.GetBytes(checkData));
- // StringBuilder checkBuilder = new StringBuilder();
- // foreach (byte code in hashCode)
- // {
- // checkBuilder.Append(code.ToString("X2"));
- // }
- // if (checkBuilder.ToString() == storeInfo.CheckSum)
- // {
- // string decryptData = AESDecrypt(storeInfo.SignData, keyBuilder.ToString());
- // activeInfo = JsonConvert.DeserializeObject<ProductActiveInfo>(decryptData);
- // }
- // return activeInfo;
- //}
- return null;
- }
- public static void SetProductActiveStore(ProductActiveInfo activeInfo)
- {
- //ProductActiveStore activeStore = new ProductActiveStore();
- //activeStore.UpdateTime = DateTime.Now;
- //string rawData = JsonConvert.SerializeObject(activeInfo);
- //Random random = new Random((int)activeStore.UpdateTime.Ticks);
- //StringBuilder keyBuilder = new StringBuilder();
- //for (int i = 0; i < 16; i++)
- //{
- // keyBuilder.Append(NumberToChar(random.Next(0, 35)));
- //}
- //activeStore.SignData = AESEncrypt(rawData, keyBuilder.ToString());
- //string checkData = AESEncrypt(activeStore.SignData, keyBuilder.ToString());
- //MD5 mD5 = MD5.Create();
- //byte[] hashCode = mD5.ComputeHash(Encoding.UTF8.GetBytes(checkData));
- //StringBuilder checkBuilder = new StringBuilder();
- //foreach(byte code in hashCode)
- //{
- // checkBuilder.Append(code.ToString("X2"));
- //}
- //activeStore.CheckSum = checkBuilder.ToString();
- //Settings.Default.ProductActiveStore = activeStore;
- //Settings.Default.Save();
- }
- }
- }
|