KMBaseWindowController.swift 1.7 KB

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