KMBaseWindowController.swift 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. deinit {
  12. Swift.debugPrint(self.className + "已释放")
  13. }
  14. override func windowDidLoad() {
  15. super.windowDidLoad()
  16. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  17. self.initSubViews()
  18. self.initDefaultValue()
  19. }
  20. func initSubViews() {}
  21. func initDefaultValue() {}
  22. }
  23. extension KMBaseWindowController {
  24. static func checkPassword(url: URL, password: String = "", completion: @escaping ((_ success: Bool, _ resultPassword: String) -> Void)) {
  25. let document = CPDFDocument.init(url: url)
  26. if document!.isLocked {
  27. if let unlockSuccess = document?.unlock(withPassword: password) {
  28. completion(true, password)
  29. } else {
  30. DispatchQueue.main.async {
  31. let passwordWindowController = PasswordWindowController(windowNibName: "PasswordWindowController")
  32. passwordWindowController.fileURL = url
  33. NSWindow.currentWindow().km_beginSheet(windowC: passwordWindowController)
  34. passwordWindowController.closeCallBack = { passwordString in
  35. if passwordString.count != 0 {
  36. document?.unlock(withPassword: passwordString)
  37. completion(true, passwordString)
  38. } else {
  39. completion(false, "")
  40. }
  41. }
  42. }
  43. }
  44. } else {
  45. completion(true, "")
  46. }
  47. }
  48. }