KMBaseWindowController.swift 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. let document = CPDFDocument.init(url: url)
  28. if document!.isLocked {
  29. if let unlockSuccess = document?.unlock(withPassword: password) {
  30. completion(true, password)
  31. } else {
  32. DispatchQueue.main.async {
  33. let passwordWindowController = PasswordWindowController(windowNibName: "PasswordWindowController")
  34. passwordWindowController.fileURL = url
  35. NSWindow.currentWindow().km_beginSheet(windowC: passwordWindowController)
  36. passwordWindowController.closeCallBack = { passwordString in
  37. if passwordString.count != 0 {
  38. document?.unlock(withPassword: passwordString)
  39. completion(true, passwordString)
  40. } else {
  41. completion(false, "")
  42. }
  43. }
  44. }
  45. }
  46. } else {
  47. completion(true, "")
  48. }
  49. }
  50. }