ChatGPTAITranslationContent.xaml.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using ComPDFKitViewer;
  2. using PDF_Master.Helper;
  3. using System.Data;
  4. using System.Windows;
  5. using System;
  6. using System.Windows.Controls;
  7. using System.Windows.Interop;
  8. using PDF_Master.ViewModels.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
  9. using PDF_Master.ViewModels.HomePanel.ChatGPTAI;
  10. using Microsoft.Office.Core;
  11. using PDF_Master.ViewModels;
  12. using System.IO;
  13. using Point = System.Windows.Point;
  14. using Window = System.Windows.Window;
  15. using System.Threading.Tasks;
  16. using PDF_Master.Model;
  17. using PDF_Master.Properties;
  18. using Prism.Services.Dialogs;
  19. using ComPDFKit.PDFDocument;
  20. using Microsoft.Office.Interop.Word;
  21. using PDFReader_WPF.Helper;
  22. namespace PDF_Master.Views.HomePanel.ChatGPTAI
  23. {
  24. /// <summary>
  25. /// Interaction logic for ChatGPTAITranslationContent
  26. /// </summary>
  27. public partial class ChatGPTAITranslationContent : UserControl
  28. {
  29. private ChatGPTAITranslationContentViewModel viewModel;
  30. private bool isFirst = false;
  31. private IDropTargetHelper dropHelper;
  32. public ChatGPTAITranslationContent()
  33. {
  34. InitializeComponent();
  35. viewModel = this.DataContext as ChatGPTAITranslationContentViewModel;
  36. }
  37. private async void Grid_Drop(object sender, DragEventArgs e)
  38. {
  39. DropColorBorder.Visibility = Visibility.Hidden;
  40. NormalColorBorder.Visibility = Visibility.Visible;
  41. try
  42. {
  43. Point iconPoint = e.GetPosition(this);
  44. dropHelper?.Drop((System.Runtime.InteropServices.ComTypes.IDataObject)e.Data, ref iconPoint, e.Effects);
  45. }
  46. catch
  47. {
  48. }
  49. string dropFile = "Drop";
  50. if (e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop))
  51. {
  52. //BtnBlank.IsEnabled = true;
  53. int count = ((System.Array)e.Data.GetData(System.Windows.DataFormats.FileDrop)).Length;
  54. for (int i = 0; i < count; i++)
  55. {
  56. dropFile = ((System.Array)e.Data.GetData(System.Windows.DataFormats.FileDrop)).GetValue(i).ToString();
  57. if (dropFile.ToLower().EndsWith("pdf") || dropFile.ToLower().EndsWith(".doc") || dropFile.ToLower().EndsWith(".docx") || dropFile.ToLower().EndsWith(".docm") || dropFile.ToLower().EndsWith(".dot") || dropFile.ToLower().EndsWith(".dotx") || dropFile.ToLower().EndsWith(".dotm"))
  58. {
  59. if (!isFirst)
  60. {
  61. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_AI, DataTrackingHelper.EntryPathKeyType.Home_Tools);
  62. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Home, DataTrackingHelper.SecondaryPath.AITranslate);
  63. //权益弹窗
  64. bool flg = false;
  65. if (!App.IsLogin || Settings.Default.UserDate.subscribestatus != 1)
  66. {
  67. DialogParameters value = new DialogParameters();
  68. value.Add(ParameterNames.Open, "AI");
  69. App.mainWindowViewModel.dialogs.ShowDialog(DialogNames.SubscriptionDialog, value, dialogResult =>
  70. {
  71. if (dialogResult.Result == ButtonResult.OK)
  72. {
  73. flg = true;
  74. }
  75. else
  76. {
  77. flg = false;
  78. }
  79. });
  80. if (flg == false)
  81. {
  82. return;
  83. }
  84. }
  85. isFirst = true;
  86. viewModel.ErrorTipVisible = Visibility.Collapsed;
  87. FileInfo fileInfo = new FileInfo(dropFile);
  88. //判断文件大小
  89. if ((((float)fileInfo.Length) / 1024 / 1024) > 10)
  90. {
  91. viewModel.ErrorTipText = "The uploaded file cannot exceed 10MB";
  92. viewModel.ErrorTipVisible = Visibility.Visible;
  93. isFirst = false;
  94. return;
  95. }
  96. //判断文件页数
  97. if (fileInfo.Extension.ToLower() == ".pdf")
  98. {
  99. CPDFDocument cPDFDocument = CPDFDocument.InitWithFilePath(dropFile);
  100. if (cPDFDocument.PageCount > 30)
  101. {
  102. viewModel.ErrorTipText = "The uploaded file cannot exceed 30Pages";
  103. viewModel.ErrorTipVisible = Visibility.Visible;
  104. isFirst = false;
  105. return;
  106. }
  107. }
  108. string newfile = "";
  109. await System.Threading.Tasks.Task.Run(async delegate
  110. {
  111. newfile = await ChatGTPAIHelper.fileTranslate(dropFile, viewModel.fromlanguage, viewModel.tolanguage);
  112. });
  113. if (!string.IsNullOrEmpty(newfile))
  114. {
  115. if (File.Exists(newfile))
  116. {
  117. if (viewModel.homeContentViewModel != null)
  118. {
  119. viewModel.homeContentViewModel.OpenFile(new string[] { newfile });
  120. isFirst = false;
  121. return;
  122. }
  123. }
  124. }
  125. isFirst = false;
  126. if (newfile == "-1") { return; }
  127. viewModel.ErrorTipText = ChatGTPAIHelper.GetBaiduTranslationCode(newfile);
  128. viewModel.ErrorTipVisible = Visibility.Visible;
  129. }
  130. }
  131. }
  132. }
  133. }
  134. private void MainPage_DragEnter(object sender, DragEventArgs e)
  135. {
  136. DropColorBorder.Visibility = Visibility.Visible;
  137. NormalColorBorder.Visibility = Visibility.Hidden;
  138. //BtnBlank.IsEnabled = false;
  139. //页面编辑和缩略图
  140. if ((e.OriginalSource as Image) != null)
  141. return;
  142. try
  143. {
  144. if (dropHelper == null)
  145. {
  146. dropHelper = Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("4657278A-411B-11D2-839A-00C04FD918D0"))) as IDropTargetHelper;
  147. }
  148. Point iconPoint = e.GetPosition(this);
  149. dropHelper?.DragEnter(new WindowInteropHelper(Window.GetWindow(this)).Handle, (System.Runtime.InteropServices.ComTypes.IDataObject)e.Data, ref iconPoint, e.Effects);
  150. }
  151. catch
  152. {
  153. }
  154. }
  155. private void MainPage_DragOver(object sender, DragEventArgs e)
  156. {
  157. if ((e.OriginalSource as Image) != null)
  158. return;
  159. try
  160. {
  161. Point iconPoint = e.GetPosition(this);
  162. dropHelper?.DragOver(ref iconPoint, e.Effects);
  163. }
  164. catch
  165. {
  166. }
  167. }
  168. private void MainPage_DragLeave(object sender, DragEventArgs e)
  169. {
  170. DropColorBorder.Visibility = Visibility.Hidden;
  171. NormalColorBorder.Visibility = Visibility.Visible;
  172. // BtnBlank.IsEnabled = true;
  173. if ((e.OriginalSource as Image) != null)
  174. return;
  175. try
  176. {
  177. dropHelper?.DragLeave();
  178. }
  179. catch
  180. {
  181. }
  182. }
  183. }
  184. }