123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- //
- // KMAITranslationConfirmWindowController.swift
- // PDF Master
- //
- // Created by wanjun on 2023/5/25.
- //
- import Cocoa
- class KMAITranslationConfirmWindowController: NSWindowController {
- @IBOutlet weak var label: NSTextField!
- @IBOutlet weak var subLabel: NSTextField!
- @IBOutlet weak var cancelBox: NSBox!
- @IBOutlet weak var cancelLabel: NSTextField!
- @IBOutlet weak var cancelButton: NSButton!
- @IBOutlet weak var translateBox: NSBox!
- @IBOutlet weak var translateLabel: NSTextField!
- @IBOutlet weak var translateButton: NSButton!
-
- var filePath: String = ""
- var progressController: SKProgressController?
- override func windowDidLoad() {
- super.windowDidLoad()
- self.initLocalization()
- self.initializeUI()
- }
-
- func initializeUI() -> Void {
- self.label.textColor = NSColor(hex: "#4D4D4D")
- self.label.font = NSFont.SFProTextSemibold(13.0)
- self.subLabel.textColor = NSColor(hex: "##4D4D4D")
- self.subLabel.font = NSFont.SFProTextRegular(11.0)
- self.cancelBox.fillColor = NSColor(hex: "#F5F5F5")
- self.cancelBox.cornerRadius = 5.0
- self.cancelBox.borderWidth = 0.0
- self.cancelLabel.textColor = NSColor(hex: "#4D4D4D")
- self.cancelLabel.font = NSFont.SFProTextRegular(13.0)
- self.translateBox.fillColor = NSColor(hex: "#4B91F7")
- self.translateBox.cornerRadius = 5.0
- self.translateBox.borderWidth = 0.0
- self.translateLabel.textColor = NSColor(hex: "#FFFFFF")
- self.translateLabel.font = NSFont.SFProTextRegular(13.0)
- }
-
- func initLocalization() -> Void {
- self.label.stringValue = NSLocalizedString("AI Translation", comment: "")
- self.subLabel.stringValue = NSLocalizedString("Intelligent translation of the currently open document, the translated file will be opened as a new file.", comment: "")
- self.cancelLabel.stringValue = NSLocalizedString("Cancel", comment: "")
- self.translateLabel.stringValue = NSLocalizedString("Translate", comment: "")
- }
-
- // MARK: Private Methods
-
-
- // MARK: Action Methods
- @IBAction func translateAction(_ sender: NSButton) {
- if !KMLightMemberManager.manager.isLogin() {
- KMLoginWindowController.show(window: NSApp.mainWindow!)
- return
- }
-
- DispatchQueue.main.async {
- self.showProgressWindow()
- }
-
- let infoDictionary = Bundle .main.infoDictionary!
- let majorVersion = infoDictionary["CFBundleShortVersionString"]
- KMRequestServerManager.manager.aiTranslationFileUpload(file: self.filePath, version: "1.0.1") { [unowned self] success, result in
- if success {
- let result: NSDictionary = result!.result
- let fileKey = result["fileKey"]
- let fileName = result["fileName"]
- let pageCount = result["pageCount"]
- if fileKey != nil {
- self.fileTranslateHandle(fileKey as! String)
- }
- } else {
- let result: String = result!.message
- DispatchQueue.main.async {
- self.hiddenProgressWindow()
-
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = result
- alert.runModal()
- }
- }
- }
- }
-
- @IBAction func cancelAction(_ sender: NSButton) {
- NSApp.mainWindow!.endSheet(self.window!)
- self.window?.orderOut(self)
- }
-
- // MARK: Private Methods
- func showProgressWindow() {
- let progress = SKProgressController()
- progress.message = NSLocalizedString("translation...", comment: "")
- progress.window?.backgroundColor = NSColor(hex: "#36383B")
- progress.window?.contentView?.wantsLayer = true
- progress.window?.contentView?.layer?.backgroundColor = NSColor(hex: "#36383B").cgColor
- progress.progressField.textColor = NSColor.white
-
- progress.closeBlock = { [unowned self] in
-
- }
-
- self.progressController = progress
- self.window?.beginSheet(progress.window!)
- }
-
- func hiddenProgressWindow() {
- if (self.progressController != nil) {
- self.window?.endSheet((self.progressController?.window)!)
- self.progressController = nil
- }
- }
-
- func fileTranslateHandle(_ fileKey: String) -> Void {
- let infoDictionary = Bundle .main.infoDictionary!
- let majorVersion = infoDictionary["CFBundleShortVersionString"]
- KMRequestServerManager.manager.aiTranslationFileTranslateHandle(fileKey: fileKey, from: "auto", to: "en", version: "1.0.1") { success, result in
- if success {
- let result: NSDictionary = result!.result
- let fileUrl: String = result["fileUrl"] as! String
- let downFileUrl: String = result["downFileUrl"] as! String
- let ossDownUrl: String = result["ossDownUrl"] as! String
- let fileName: String = result["fileName"] as! String
- let downFileName: String = result["downFileName"] as! String
- let from: String = result["from"] as! String
- let to: String = result["to"] as! String
-
- self.downloadFile(filePath: ossDownUrl, downFileName: downFileName)
- } else {
- let result: String = result!.message
- DispatchQueue.main.async {
- self.hiddenProgressWindow()
-
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = result
- alert.runModal()
- }
- }
- }
- }
-
- func downloadFile(filePath: String, downFileName: String) -> Void {
- guard let fileURL = URL(string: filePath) else {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = NSLocalizedString("Invalid file link", comment: "")
- alert.runModal()
- return
- }
-
- let fileNameWithoutExtension = fileURL.deletingPathExtension().lastPathComponent
- let fileExtension = fileURL.pathExtension
- let newFileName = fileNameWithoutExtension + "_aiTranslation" + "." + fileExtension
- let newDestinationURL = fileURL.deletingLastPathComponent().appendingPathComponent(newFileName)
-
- let destinationURL = newDestinationURL
- if FileManager.default.fileExists(atPath: destinationURL.path) {
- do {
- try FileManager.default.removeItem(at: destinationURL)
- print("删除旧文件成功")
- } catch {
- print("删除旧文件失败:\(error)")
- }
- }
- let sessionConfiguration = URLSessionConfiguration.default
- let session = URLSession(configuration: sessionConfiguration)
- let downloadTask = session.downloadTask(with: fileURL) { (tempLocalURL, response, error) in
- if let error = error {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = String(format: "%@:\(error)", NSLocalizedString("Download failure", comment: ""))
- alert.runModal()
-
- return
- }
- guard let tempLocalURL = tempLocalURL else {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = NSLocalizedString("The download file temporary path is invalid", comment: "")
- alert.runModal()
- return
- }
-
- DispatchQueue.main.async {
- self.hiddenProgressWindow()
- }
-
- do {
- try FileManager.default.moveItem(at: tempLocalURL, to: destinationURL)
- NSDocumentController.shared.openDocument(withContentsOf: destinationURL, display: true) { document, documentWasAlreadyOpen, error in
- if error != nil {
- NSApp.presentError(error!)
- } else {
-
- }
- }
- } catch {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = String(format: "%@:\(error)", NSLocalizedString("File saving failure", comment: ""))
- alert.runModal()
- }
- }
- downloadTask.resume()
- }
- }
|