KMBaseWindowController.swift 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. NSWindow.currentWindow().km_beginSheet(windowC: passwordWindowController)
  50. passwordWindowController.closeCallBack = { passwordString in
  51. if passwordString.count != 0 {
  52. document.unlock(withPassword: passwordString)
  53. completion(true, passwordString)
  54. } else {
  55. completion(false, "")
  56. }
  57. }
  58. }
  59. }
  60. }