Przeglądaj źródła

【2025】【Home】快捷工具串接

niehaoyu 2 miesięcy temu
rodzic
commit
dc6551867b

+ 32 - 84
PDF Office/PDF Master/KMClass/KMHomeViewController/KMNHomeViewController.swift

@@ -7,9 +7,9 @@
 
 import Cocoa
 import KMComponentLibrary
- 
-class KMNHomeViewController: NSViewController {
 
+class KMNHomeViewController: NSViewController {
+    
     @IBOutlet var leftContendBox: NSBox!
     @IBOutlet var leftDivider: ComponentDivider!
     @IBOutlet var homeOpenView: KMHomeOpenView!
@@ -19,11 +19,11 @@ class KMNHomeViewController: NSViewController {
     @IBOutlet var rightInfoView: KMHomeRightView!
     
     @IBOutlet var homeDragView: KMHomeDragView!
-     
+    
     var demoVC: WCCompWindowController = WCCompWindowController(windowNibName: "WCCompWindowController")
-  
+    
     var createWC: KMURLCreatePDFWindowController = KMURLCreatePDFWindowController(windowNibName: "KMURLCreatePDFWindowController")
-
+    
     
     //合并
     var mergeWindowController: KMMergeWindowController?
@@ -61,7 +61,7 @@ class KMNHomeViewController: NSViewController {
         homeOpenView.delegate = self
         
         self.homeRecommondView.reloadData()
-
+        
     }
     
     func configRightContendView() {
@@ -139,7 +139,7 @@ extension KMNHomeViewController: KMHomeOpenViewDelegate {
     func homeOpenViewDidChooseFileURL(_ view: KMHomeOpenView?, _ url: URL) {
         self.openFile(withFilePath: url)
     }
-      
+    
 }
 
 //MARK: - KMHomeRightViewDelegate
@@ -158,7 +158,7 @@ extension KMNHomeViewController: KMHomeRightViewDelegate {
         self.quickToolsActionWith(toolType)
         
     }
- 
+    
     //最近文件列表删除更新结束后回调
     func homeRightViewDidRecentFilesUpdated(_ view: KMHomeRightView) {
         
@@ -183,7 +183,7 @@ extension KMNHomeViewController: KMNQuickToolWindowDelegate {
 extension KMNHomeViewController: KMHomeDragViewDelegate {
     func homeDragView(_ viewController: KMHomeDragView, filePath: URL) {
         self.openFile(withFilePath: filePath)
-
+        
     }
     
     func homeDragView(_ viewController: KMHomeDragView, notSupport: Bool) {
@@ -198,19 +198,19 @@ extension KMNHomeViewController: KMHomeDragViewDelegate {
 
 //MARK: - Open Files
 extension KMNHomeViewController {
-  
+    
     func openFile(withFilePath path: URL) -> Void {
         if let _windowC = NSApp.mainWindow?.windowController as? KMBrowserWindowController {
             _windowC.openFile(withFilePath: path)
         }
     }
-       
+    
     func openImageToPdfWindow(urls: Array<URL>) {
         if let _windowC = NSApp.mainWindow?.windowController as? KMBrowserWindowController {
             _windowC.showBatchWindow(type: .imageToPDF, files: urls)
         }
     }
-     
+    
     func showLimitWindowAlert(url: URL?) {
         if !KMDataManager.default.isTabbingWin{
             KMDataManager.default.isTabbingWin = true
@@ -226,7 +226,7 @@ extension KMNHomeViewController {
             self.km_beginSheet(windowC: tabbingWin)
         }
     }
-     
+    
     func reopenDocument(forPaths path: URL?) -> Void {
         if path == nil {
             let browser = KMBrowser.init() as KMBrowser
@@ -256,7 +256,7 @@ extension KMNHomeViewController {
             let originalFullFileName = path.lastPathComponent
             let originalFileName = path.lastPathComponent.deletingPathExtension.lastPathComponent
             let originalExtension = path.extension
-
+            
             let startIndex: Int = 0
             let endIndex: Int = startIndex + originalPath.count - originalFullFileName.count - 1
             let fileLocatePath = originalPath.substring(to: endIndex)
@@ -292,7 +292,7 @@ extension KMNHomeViewController {
             return true
         }
         let addImageAnnotation = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).last!.appendingPathComponent(Bundle.main.bundleIdentifier!).appendingPathComponent("addImageAnnotation")
-         if !FileManager.default.fileExists(atPath: addImageAnnotation.path) {
+        if !FileManager.default.fileExists(atPath: addImageAnnotation.path) {
             try? FileManager.default.createDirectory(atPath: addImageAnnotation.path, withIntermediateDirectories: false, attributes: nil)
         }
         guard let data = image!.tiffRepresentation else { return false }
@@ -426,7 +426,7 @@ extension KMNHomeViewController {
             if result == NSApplication.ModalResponse.OK {
                 guard let weakSelf = self else { return }
                 let urls = openPanel.urls as [URL]
-
+                
                 weakSelf.openImageToPdfWindow(urls: urls)
             }
         }
@@ -458,12 +458,12 @@ extension KMNHomeViewController {
             return
         }
         let openPanel = NSOpenPanel()
-       
+        
         var arr = KMImageToPDFMethod.supportedImageTypes()
         arr.append("pdf")
         openPanel.allowedFileTypes = arr
         openPanel.allowsMultipleSelection = false
-
+        
         openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
             if result == NSApplication.ModalResponse.OK {
                 quickOcr(urls: openPanel.urls)
@@ -472,35 +472,7 @@ extension KMNHomeViewController {
     }
     
     func quickOcr(urls: Array<URL>) {
-        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
-            var document: CPDFDocument?
-            for fileURL in urls {
-                if fileURL.lastPathComponent.pathExtension == "pdf"{
-                    document = CPDFDocument(url: fileURL)
-                }else{
-                    document = CPDFDocument()
-                    let image = NSImage(contentsOfFile: fileURL.path)
-                    _ = document?.km_insertPage(image!.size, withImage: fileURL.path, at: 0)
-                }
-                break
-            }
-            if document == nil{
-                let alert = NSAlert()
-                alert.alertStyle = .critical
-                alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
-                alert.beginSheetModal(for: NSApp.mainWindow!) { (response) in
-                    if response == .alertFirstButtonReturn {
-                        // Handle cancel action
-                    }
-                }
-                return
-            }
-            let com = KMOCRPDFWindowController(cpdfDocument: document!, pwd: document!.password)
-            com.own_beginSheetModal(for: self.view.window) { result in
-                
-            }
-            
-        }
+        showBatchWindow(type: .OCR, files: urls)
     }
     
     func fastTool_ConvertPDF() {    // 转换PDF
@@ -510,7 +482,7 @@ extension KMNHomeViewController {
         openPanel.allowedFileTypes = ["pdf","PDF"]
         //MARK: 允许多选还是单选,如果是付费用户允许多选
         openPanel.allowsMultipleSelection = false
-
+        
         openPanel.beginSheetModal(for: self.view.window!) { [self] (result) in
             if result == NSApplication.ModalResponse.OK {
                 quickConvertPdf(urls: openPanel.urls, type: .WordAdvance)
@@ -520,26 +492,6 @@ extension KMNHomeViewController {
     
     func quickConvertPdf(urls: Array<URL>, type: KMConvertWithPDFType) {
         DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
-//            var arr: Array<KMBatchOperateFile> = Array()
-//            for fileURL in urls {
-//                let img = NSImage(contentsOfFile: fileURL.path)
-//                if self.isDamageImage(image: img, path: fileURL.path) {
-//                    let alert = NSAlert()
-//                    alert.alertStyle = .critical
-//                    alert.messageText = String(format: NSLocalizedString("The file \"%@\" could not be opened.", comment: ""), fileURL.path.lastPathComponent)
-//                    alert.informativeText = NSLocalizedString("It may be damaged or use a file format that PDF Reader Pro doesn’t recognize.", comment: "")
-//                    alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
-//                    alert.beginSheetModal(for: NSApp.mainWindow!) { (response) in
-//                        if response == .alertFirstButtonReturn {
-//                            // Handle cancel action
-//                        }
-//                    }
-//                    continue
-//                }
-//                let file = KMBatchOperateFile(filePath: fileURL.path, type: .Convert)
-//                arr.append(file)
-//            }
-            
             self.showBatchWindow(type: .convertPDF, subType: type.rawValue,files: urls)
         }
     }
@@ -576,7 +528,7 @@ extension KMNHomeViewController {
         openPanel.allowedFileTypes = ["pdf","PDF"]
         //MARK: 允许多选还是单选,如果是付费用户允许多选
         openPanel.allowsMultipleSelection = false
-
+        
         openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
             if result == NSApplication.ModalResponse.OK {
                 quickCompressPdf(urls: openPanel.urls)
@@ -647,15 +599,11 @@ extension KMNHomeViewController {
                         controller.contentComplete = { [unowned self] controller, pdfCompareContent, result, oldDocument, document in
                             DispatchQueue.main.async {
                                 self.view.window?.endSheet((controller.window)!)
-                                
-//                                self.openContentCompareVC(with: pdfCompareContent, results: result, oldDocument: oldDocument, document: document)
                             }
                         }
                         
                         controller.coveringComplete = { [unowned self] controller, document in
                             self.view.window?.endSheet((controller.window)!)
-                            
-//                            self.openCoveringCompareVC(with: document)
                         }
                         controller.fileType = .content
                         
@@ -698,7 +646,7 @@ extension KMNHomeViewController {
             winC?.showWindow(nil)
             return
         }
-
+        
         let openPanel = NSOpenPanel()
         openPanel.allowedFileTypes = ["pdf","PDF"]
         //MARK: 允许多选还是单选,如果是付费用户允许多选
@@ -715,7 +663,7 @@ extension KMNHomeViewController {
             self.showBatchWindow(type: .background, files: urls)
         }
     }
-     
+    
     //MARK: 打印
     func fastTool_Print() {
         let openPanel = NSOpenPanel()
@@ -725,7 +673,7 @@ extension KMNHomeViewController {
             if result == NSApplication.ModalResponse.OK {
                 if let url = openPanel.url {
                     DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
-                         self.showPrintWindowWithURL(url)
+                        self.showPrintWindowWithURL(url)
                     }
                 }
             }
@@ -734,7 +682,7 @@ extension KMNHomeViewController {
     
     func showPrintWindowWithURL(_ url: URL) {
         if let cPDFDocument = CPDFDocument(url: url) {
-             KMPrintWindowController.openDocument(inputDocument: cPDFDocument, inputPageRange: KMPrintPageRange(type: .allPage, selectPages: []))
+            KMPrintWindowController.openDocument(inputDocument: cPDFDocument, inputPageRange: KMPrintPageRange(type: .allPage, selectPages: []))
         }
     }
     
@@ -743,7 +691,7 @@ extension KMNHomeViewController {
             KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
             return
         }
-
+        
         let openPanel = NSOpenPanel()
         openPanel.prompt = NSLocalizedString("Insert", comment: "")
         openPanel.allowsMultipleSelection = false
@@ -838,7 +786,7 @@ extension KMNHomeViewController {
                     DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                         let saveFilePath = outputSavePanel.url?.path
                         DispatchQueue.global().async {
-                            var pdf = CPDFDocument.init()
+                            let pdf = CPDFDocument.init()
                             let success = (pdf!.extractAsOneDocument(withPages: pages, savePath: saveFilePath)) as Bool
                             DispatchQueue.main.async {
                                 if success {
@@ -906,11 +854,11 @@ extension KMNHomeViewController {
         let batchWindowController = KMBatchWindowController.init(windowNibName: "KMBatchWindowController")
         batchWindowController.window?.makeKeyAndOrderFront("")
         
-//        var datas: [KMBatchProcessingTableViewModel] = []
-//        for file in files! {
-//            let data = KMBatchProcessingTableViewModel.initWithFilePath(url: file)
-//            datas.append(data)
-//        }
+        //        var datas: [KMBatchProcessingTableViewModel] = []
+        //        for file in files! {
+        //            let data = KMBatchProcessingTableViewModel.initWithFilePath(url: file)
+        //            datas.append(data)
+        //        }
         batchWindowController.inputData = files ?? []
         batchWindowController.type = type
         batchWindowController.inputSubType = subType