KMBaseWindowController.swift 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. NSWindow.currentWindow().km_beginSheet(windowC: passwordWindowController)
  32. passwordWindowController.closeCallBack = { passwordString in
  33. if passwordString.count != 0 {
  34. document?.unlock(withPassword: passwordString)
  35. completion(true, passwordString)
  36. } else {
  37. completion(false, "")
  38. }
  39. }
  40. }
  41. }
  42. } else {
  43. completion(true, "")
  44. }
  45. }
  46. }