KMBaseWindowController.swift 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. if document.isLocked == false && (document.allowsCopying && document.allowsPrinting) {
  37. completion(true, "")
  38. return
  39. }
  40. // 加密文件,尝试解锁
  41. if password.isEmpty == false {
  42. let preStatus = document.permissionsStatus
  43. document.unlock(withPassword: password)
  44. if document.permissionsStatus.rawValue > preStatus.rawValue { // 解密成功
  45. completion(true, password)
  46. return
  47. }
  48. }
  49. // 弹密码弹窗
  50. Task { @MainActor in
  51. let passwordWindowController = PasswordWindowController(windowNibName: "PasswordWindowController")
  52. passwordWindowController.fileURL = url
  53. let window = NSWindow.currentWindow()
  54. window.km_beginSheet(windowC: passwordWindowController)
  55. passwordWindowController.closeCallBack = { passwordString in
  56. window.km_quick_endSheet()
  57. if passwordString.count != 0 {
  58. document.unlock(withPassword: passwordString)
  59. completion(true, passwordString)
  60. } else {
  61. completion(false, "")
  62. }
  63. }
  64. }
  65. }
  66. }