PasswordWindowController.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // PasswordWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by kdanmobile on 2023/11/2.
  6. //
  7. import Cocoa
  8. typealias closePwdCallBack = (_ password: String) -> ()
  9. @objc protocol PasswordWindowDelegate: AnyObject {
  10. @objc optional func didFinshedUnlockFile(_ passwordWindowCtr: PasswordWindowController, password: String)
  11. @objc optional func didCancelUnlockFile(_ passwordWindowCtr: PasswordWindowController)
  12. }
  13. @objc class PasswordWindowController: NSWindowController{
  14. var fileURL: URL?
  15. var delegate: PasswordWindowDelegate?
  16. var tag: Int = 0
  17. var password: String?
  18. var closeCallBack: closePwdCallBack?
  19. @IBOutlet var passwordTextField: NSSecureTextField!
  20. @IBOutlet var titleTextField: NSTextField!
  21. @IBOutlet var textField: NSTextField!
  22. @IBOutlet var openButton: NSButton!
  23. @IBOutlet var cancelButton: NSButton!
  24. override init(window: NSWindow?) {
  25. super.init(window: window)
  26. }
  27. required init?(coder: NSCoder) {
  28. super.init(coder: coder)
  29. }
  30. override func windowDidLoad() {
  31. super.windowDidLoad()
  32. self.titleTextField.stringValue = fileURL?.lastPathComponent ?? ""
  33. self.textField.stringValue = NSLocalizedString("This PDF is password protected. Please enter the password below to access this PDF.", comment: "")
  34. self.openButton.title = NSLocalizedString("Open", comment: "")
  35. self.cancelButton.title = NSLocalizedString("Cancel", comment: "")
  36. }
  37. @IBAction func cancelAction(_ sender: NSButton) {
  38. self.close()
  39. }
  40. @IBAction func OKAction(_ sender: NSButton) {
  41. guard let pdfDoc = CPDFDocument(url: self.fileURL) else {
  42. __NSBeep()
  43. return
  44. }
  45. let isOk = pdfDoc.unlock(withPassword: self.passwordTextField.stringValue)
  46. if isOk {
  47. if pdfDoc.permissionsStatus == .owner {
  48. self.password = self.passwordTextField.stringValue
  49. self.close()
  50. } else{
  51. self.incorrectPasswordAlert()
  52. }
  53. } else {
  54. self.incorrectPasswordAlert()
  55. }
  56. }
  57. func incorrectPasswordAlert() {
  58. let alert = NSAlert()
  59. alert.alertStyle = .critical
  60. alert.messageText = NSLocalizedString("Incorrect password. Please check your password and try again.", comment: "")
  61. alert.runModal()
  62. }
  63. @objc func didEndSheet(_ sheet: NSWindow, returnCode: NSInteger, contextInfo: UnsafeMutableRawPointer?) {
  64. if contextInfo != nil {
  65. let handler = contextInfo!.assumingMemoryBound(to: ((String) -> Void).self).pointee
  66. handler(password ?? "")
  67. }
  68. }
  69. func beginSheetModal(for window: NSWindow, completionHandler handler: ((String) -> Void)?) {
  70. struct Context {
  71. var handler: ((String) -> Void)?
  72. }
  73. var context = Context(handler: handler)
  74. // Pass the address of the struct as contextInfo
  75. let contextPointer = UnsafeMutableRawPointer(&context)
  76. NSApp.beginSheet(self.window!, modalFor: window, modalDelegate: self, didEnd: #selector(didEndSheet(_:returnCode:contextInfo:)), contextInfo: contextPointer)
  77. }
  78. override func close() {
  79. if (self.closeCallBack != nil) {
  80. closeCallBack!(password ?? "")
  81. }
  82. }
  83. }