// // KMBaseWindowController.swift // PDF Master // // Created by tangchao on 2023/5/11. // import Cocoa class KMBaseWindowController: NSWindowController { var cancelAction: KMCommonBlock? var pdfDocument: CPDFDocument? var isBates: Bool = false var isBatch: Bool = false //是否批量模块进入 deinit { Swift.debugPrint(self.className + "已释放") } override func windowDidLoad() { super.windowDidLoad() // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file. self.initSubViews() self.initDefaultValue() } func initSubViews() {} func initDefaultValue() {} } extension KMBaseWindowController { static func checkPassword(url: URL, 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 { 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 let passwordWindowController = PasswordWindowController(windowNibName: "PasswordWindowController") passwordWindowController.fileURL = url let window = NSWindow.currentWindow() window.km_beginSheet(windowC: passwordWindowController) passwordWindowController.closeCallBack = { passwordString in window.km_quick_endSheet() if passwordString.count != 0 { document.unlock(withPassword: passwordString) completion(true, passwordString) } else { completion(false, "") } } } } }