KMAITranslationConfirmWindowController.swift 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // KMAITranslationConfirmWindowController.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2023/5/25.
  6. //
  7. import Cocoa
  8. class KMAITranslationConfirmWindowController: NSWindowController {
  9. @IBOutlet weak var label: NSTextField!
  10. @IBOutlet weak var subLabel: NSTextField!
  11. @IBOutlet weak var cancelBox: NSBox!
  12. @IBOutlet weak var cancelLabel: NSTextField!
  13. @IBOutlet weak var cancelButton: NSButton!
  14. @IBOutlet weak var translateBox: NSBox!
  15. @IBOutlet weak var translateLabel: NSTextField!
  16. @IBOutlet weak var translateButton: NSButton!
  17. var filePath: String = ""
  18. var progressController: SKProgressController?
  19. override func windowDidLoad() {
  20. super.windowDidLoad()
  21. self.initLocalization()
  22. self.initializeUI()
  23. }
  24. func initializeUI() -> Void {
  25. self.label.textColor = NSColor(hex: "#4D4D4D")
  26. self.label.font = NSFont.SFProTextSemibold(13.0)
  27. self.subLabel.textColor = NSColor(hex: "##4D4D4D")
  28. self.subLabel.font = NSFont.SFProTextRegular(11.0)
  29. self.cancelBox.fillColor = NSColor(hex: "#F5F5F5")
  30. self.cancelBox.cornerRadius = 5.0
  31. self.cancelBox.borderWidth = 0.0
  32. self.cancelLabel.textColor = NSColor(hex: "#4D4D4D")
  33. self.cancelLabel.font = NSFont.SFProTextRegular(13.0)
  34. self.translateBox.fillColor = NSColor(hex: "#4B91F7")
  35. self.translateBox.cornerRadius = 5.0
  36. self.translateBox.borderWidth = 0.0
  37. self.translateLabel.textColor = NSColor(hex: "#FFFFFF")
  38. self.translateLabel.font = NSFont.SFProTextRegular(13.0)
  39. }
  40. func initLocalization() -> Void {
  41. self.label.stringValue = NSLocalizedString("AI Translation", comment: "")
  42. self.subLabel.stringValue = NSLocalizedString("Intelligent translation of the currently open document, the translated file will be opened as a new file.", comment: "")
  43. self.cancelLabel.stringValue = NSLocalizedString("Cancel", comment: "")
  44. self.translateLabel.stringValue = NSLocalizedString("Translate", comment: "")
  45. }
  46. // MARK: Private Methods
  47. // MARK: Action Methods
  48. @IBAction func translateAction(_ sender: NSButton) {
  49. if !KMLightMemberManager.manager.isLogin() {
  50. KMLoginWindowController.show(window: NSApp.mainWindow!)
  51. return
  52. }
  53. DispatchQueue.main.async {
  54. self.showProgressWindow()
  55. }
  56. let infoDictionary = Bundle .main.infoDictionary!
  57. let majorVersion = infoDictionary["CFBundleShortVersionString"]
  58. KMRequestServerManager.manager.aiTranslationFileUpload(file: self.filePath, version: "1.0.1") { [unowned self] success, result in
  59. if success {
  60. let result: NSDictionary = result!.result
  61. let fileKey = result["fileKey"]
  62. let fileName = result["fileName"]
  63. let pageCount = result["pageCount"]
  64. if fileKey != nil {
  65. self.fileTranslateHandle(fileKey as! String)
  66. }
  67. } else {
  68. let result: String = result!.message
  69. DispatchQueue.main.async {
  70. self.hiddenProgressWindow()
  71. let alert = NSAlert()
  72. alert.alertStyle = .critical
  73. alert.messageText = result
  74. alert.runModal()
  75. }
  76. }
  77. }
  78. }
  79. @IBAction func cancelAction(_ sender: NSButton) {
  80. NSApp.mainWindow!.endSheet(self.window!)
  81. self.window?.orderOut(self)
  82. }
  83. // MARK: Private Methods
  84. func showProgressWindow() {
  85. let progress = SKProgressController()
  86. progress.message = NSLocalizedString("translation...", comment: "")
  87. progress.window?.backgroundColor = NSColor(hex: "#36383B")
  88. progress.window?.contentView?.wantsLayer = true
  89. progress.window?.contentView?.layer?.backgroundColor = NSColor(hex: "#36383B").cgColor
  90. progress.progressField.textColor = NSColor.white
  91. progress.closeBlock = { [unowned self] in
  92. }
  93. self.progressController = progress
  94. self.window?.beginSheet(progress.window!)
  95. }
  96. func hiddenProgressWindow() {
  97. if (self.progressController != nil) {
  98. self.window?.endSheet((self.progressController?.window)!)
  99. self.progressController = nil
  100. }
  101. }
  102. func fileTranslateHandle(_ fileKey: String) -> Void {
  103. let infoDictionary = Bundle .main.infoDictionary!
  104. let majorVersion = infoDictionary["CFBundleShortVersionString"]
  105. KMRequestServerManager.manager.aiTranslationFileTranslateHandle(fileKey: fileKey, from: "auto", to: "en", version: "1.0.1") { success, result in
  106. if success {
  107. let result: NSDictionary = result!.result
  108. let fileUrl: String = result["fileUrl"] as! String
  109. let downFileUrl: String = result["downFileUrl"] as! String
  110. let ossDownUrl: String = result["ossDownUrl"] as! String
  111. let fileName: String = result["fileName"] as! String
  112. let downFileName: String = result["downFileName"] as! String
  113. let from: String = result["from"] as! String
  114. let to: String = result["to"] as! String
  115. self.downloadFile(filePath: ossDownUrl, downFileName: downFileName)
  116. } else {
  117. let result: String = result!.message
  118. DispatchQueue.main.async {
  119. self.hiddenProgressWindow()
  120. let alert = NSAlert()
  121. alert.alertStyle = .critical
  122. alert.messageText = result
  123. alert.runModal()
  124. }
  125. }
  126. }
  127. }
  128. func downloadFile(filePath: String, downFileName: String) -> Void {
  129. guard let fileURL = URL(string: filePath) else {
  130. let alert = NSAlert()
  131. alert.alertStyle = .critical
  132. alert.messageText = NSLocalizedString("Invalid file link", comment: "")
  133. alert.runModal()
  134. return
  135. }
  136. let fileNameWithoutExtension = fileURL.deletingPathExtension().lastPathComponent
  137. let fileExtension = fileURL.pathExtension
  138. let newFileName = fileNameWithoutExtension + "_aiTranslation" + "." + fileExtension
  139. let newDestinationURL = fileURL.deletingLastPathComponent().appendingPathComponent(newFileName)
  140. let destinationURL = newDestinationURL
  141. if FileManager.default.fileExists(atPath: destinationURL.path) {
  142. do {
  143. try FileManager.default.removeItem(at: destinationURL)
  144. print("删除旧文件成功")
  145. } catch {
  146. print("删除旧文件失败:\(error)")
  147. }
  148. }
  149. let sessionConfiguration = URLSessionConfiguration.default
  150. let session = URLSession(configuration: sessionConfiguration)
  151. let downloadTask = session.downloadTask(with: fileURL) { (tempLocalURL, response, error) in
  152. if let error = error {
  153. let alert = NSAlert()
  154. alert.alertStyle = .critical
  155. alert.messageText = String(format: "%@:\(error)", NSLocalizedString("Download failure", comment: ""))
  156. alert.runModal()
  157. return
  158. }
  159. guard let tempLocalURL = tempLocalURL else {
  160. let alert = NSAlert()
  161. alert.alertStyle = .critical
  162. alert.messageText = NSLocalizedString("The download file temporary path is invalid", comment: "")
  163. alert.runModal()
  164. return
  165. }
  166. DispatchQueue.main.async {
  167. self.hiddenProgressWindow()
  168. }
  169. do {
  170. try FileManager.default.moveItem(at: tempLocalURL, to: destinationURL)
  171. NSDocumentController.shared.openDocument(withContentsOf: destinationURL, display: true) { document, documentWasAlreadyOpen, error in
  172. if error != nil {
  173. NSApp.presentError(error!)
  174. } else {
  175. }
  176. }
  177. } catch {
  178. let alert = NSAlert()
  179. alert.alertStyle = .critical
  180. alert.messageText = String(format: "%@:\(error)", NSLocalizedString("File saving failure", comment: ""))
  181. alert.runModal()
  182. }
  183. }
  184. downloadTask.resume()
  185. }
  186. }