Browse Source

【综合】缩略图列表拖拽插入特定文档crash修复

tangchao 8 months ago
parent
commit
5b0d0253b8

+ 44 - 0
PDF Office/PDF Master/Class/Common/Base/KMBaseWindowController.swift

@@ -63,6 +63,50 @@ extension KMBaseWindowController {
     }
 }
 
+extension NSWindowController {
+    func kCheckPassword(url: URL, type: KMPasswordInputWindowType, password: String = "", completion: @escaping ((_ success: Bool, _ resultPassword: String) -> Void)) {
+        // 判断路径 + document
+        guard let document = CPDFDocument.init(url: url) else {
+            return completion(false, "")
+        }
+
+        // 判断是否为加密文档
+        if document.isLocked == false {
+            if type == .open {
+                completion(true, "")
+                return
+            }
+        }
+        
+        if document.isLocked == false && (document.allowsCopying && document.allowsPrinting) {
+            completion(true, "")
+            return
+        }
+        
+        // 加密文件,尝试解锁
+        if password.isEmpty == false {
+            let preStatus = document.permissionsStatus
+            document.unlock(withPassword: password)
+            if document.permissionsStatus.rawValue > preStatus.rawValue { // 解密成功
+                completion(true, password)
+                return
+            }
+        }
+        
+        // 弹密码弹窗
+        Task { @MainActor in
+            KMPasswordInputWindow.openWindow(window: self.window!, type: type, url: url) { result , password in
+                if (result == .cancel) {
+                    completion(false, "")
+                    return
+                } else {
+                    completion(true, password ?? "")
+                }
+            }
+        }
+    }
+}
+
 extension KMBaseWindowController {
     static func checkPassword(url: URL, type: KMPasswordInputWindowType, password: String = "", completion: @escaping ((_ success: Bool, _ resultPassword: String) -> Void)) {
         // 判断路径 + document

+ 12 - 14
PDF Office/PDF Master/Class/PDFWindowController/Side/LeftSide/KMLeftSideViewController.swift

@@ -1299,6 +1299,7 @@ extension KMLeftSideViewController: NSTableViewDelegate, NSTableViewDataSource {
                             if let data = pdf?.isEncrypted, data {
                                 KMBaseWindowController.checkPassword(url: URL(fileURLWithPath: path), type: .owner) { success, resultPassword in
                                     if success && resultPassword.isEmpty == false {
+                                        self.model.insertedDocumentSet.insert(pdf!)
                                         for i in 0 ..< (pdf?.pageCount ?? 0) {
                                             if let page = pdf?.page(at: i).copy() as? CPDFPage {
                                                 self.pdfDocument()?.insertPageObject(page, at: UInt(index))
@@ -1310,6 +1311,9 @@ extension KMLeftSideViewController: NSTableViewDelegate, NSTableViewDataSource {
                                     }
                                 }
                             } else {
+                                if let data = pdf {
+                                    self.model.insertedDocumentSet.insert(data)
+                                }
                                 for i in 0 ..< (pdf?.pageCount ?? 0) {
                                     let page = pdf?.page(at: i)
                                     self.pdfDocument()?.insertPageObject(page, at: UInt(index))
@@ -1382,32 +1386,32 @@ extension KMLeftSideViewController: NSTableViewDelegate, NSTableViewDataSource {
             var insertIndexSet = IndexSet()
             var index = row
 
-//            if (fileNames.count == 1) {
             if fileNames != nil {
-//                var path = fileNames.first as? String ?? ""
                 let path = fileNames as? String ?? ""
                 let url = URL(string: path)
                 let pathExtension = url?.pathExtension.lowercased() ?? ""
                 if pathExtension == "pdf" {
                     let pdf = CPDFDocument(url: url!)
                     if let data = pdf?.isEncrypted, data {
-                        KMBaseWindowController.checkPassword(url: url!, type: .owner) { success, resultPassword in
+                        self.view.window?.windowController?.kCheckPassword(url: url!, type: .owner, completion: { [weak self] success, resultPassword in
                             if success && resultPassword.isEmpty == false {
                                 pdf?.unlock(withPassword: resultPassword)
+                                self?.model.insertedDocumentSet.insert(pdf!)
                                 for i in 0 ..< (pdf?.pageCount ?? 0) {
-                                    //                                    let page = pdf?.page(at: i).copy() as? CPDFPage
                                     if let page = pdf?.page(at: i) {
-                                        self.pdfDocument()?.insertPageObject(page, at: UInt(index))
+                                        self?.pdfDocument()?.insertPageObject(page, at: UInt(index))
                                         insertIndexSet.insert(index)
                                         index += 1
                                     }
                                 }
-                                self.insertPages(insertIndexSet, pageAt: index-1)
+                                self?.insertPages(insertIndexSet, pageAt: index-1)
                             }
-                        }
+                        })
                     } else {
+                        if let data = pdf {
+                            self.model.insertedDocumentSet.insert(data)
+                        }
                         for i in 0 ..< (pdf?.pageCount ?? 0) {
-//                            let page = pdf?.page(at: i).copy() as? CPDFPage
                             if let page = pdf?.page(at: i) {
                                 self.pdfDocument()?.insertPageObject(page, at: UInt(index))
                                 insertIndexSet.insert(index)
@@ -1445,11 +1449,6 @@ extension KMLeftSideViewController: NSTableViewDelegate, NSTableViewDataSource {
                     fileExt = "pdf"
                     // filenames
                     pboard.declareTypes([.pdf, .tiff, .fileURL, .filePromise, .localDraggedTypes, KPDFThumbnailDoucumentURLForDraggedTypes], owner: self)
-//                    let newDoc = CPDFDocument()
-//                    newDoc?.insertPageObject(page, at: 0)
-//                    let data = newDoc?.dataRepresentation()
-            
-//                    [pboard setData:pdfData forType:NSPasteboardTypePDF];
                     pboard.setData(tiffData, forType: .pdf)
                     
 //                    NSData *zNSIndexSetData = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
@@ -1622,7 +1621,6 @@ extension KMLeftSideViewController: NSTableViewDelegate, NSTableViewDataSource {
                 let pdf = CPDFDocument()
                 for idx in indexSet {
                     if idx != NSNotFound && self.isLocked() == false {
-//                        let copyPage = self.listView.document.page(at: UInt(idx)).copy() as? CPDFPage
                         let copyPage = self.pdfDocument()?.page(at: UInt(idx))
                         pdf?.insertPageObject(copyPage, at: pdf?.pageCount ?? 0)
                     }