KMBaseWindowController.swift 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // KMBaseWindowController.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/5/11.
  6. //
  7. import Cocoa
  8. class KMBaseWindowController: NSWindowController {
  9. var cancelAction: KMCommonBlock?
  10. var pdfDocument: CPDFDocument?
  11. var isBates: Bool = false
  12. var isBatch: Bool = false //是否批量模块进入
  13. deinit {
  14. Swift.debugPrint(self.className + "已释放")
  15. }
  16. override func windowDidLoad() {
  17. super.windowDidLoad()
  18. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  19. self.initSubViews()
  20. self.initDefaultValue()
  21. }
  22. func initSubViews() {}
  23. func initDefaultValue() {}
  24. }
  25. extension KMBaseWindowController {
  26. static func checkPassword(url: URL, password: String = "", completion: @escaping ((_ success: Bool, _ resultPassword: String) -> Void)) {
  27. // 判断路径 + document
  28. guard let document = CPDFDocument.init(url: url) else {
  29. return completion(false, "")
  30. }
  31. // 判断是否为加密文档
  32. if document.isLocked == false {
  33. completion(true, "")
  34. return
  35. }
  36. // 加密文件,尝试解锁
  37. if password.isEmpty == false {
  38. let preStatus = document.permissionsStatus
  39. document.unlock(withPassword: password)
  40. if document.permissionsStatus.rawValue > preStatus.rawValue { // 解密成功
  41. completion(true, password)
  42. return
  43. }
  44. }
  45. // 弹密码弹窗
  46. Task { @MainActor in
  47. let passwordWindowController = PasswordWindowController(windowNibName: "PasswordWindowController")
  48. passwordWindowController.fileURL = url
  49. let window = NSWindow.currentWindow()
  50. window.km_beginSheet(windowC: passwordWindowController)
  51. passwordWindowController.closeCallBack = { passwordString in
  52. window.km_quick_endSheet()
  53. if passwordString.count != 0 {
  54. document.unlock(withPassword: passwordString)
  55. completion(true, passwordString)
  56. } else {
  57. completion(false, "")
  58. }
  59. }
  60. }
  61. }
  62. }