PasswordWindowController.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // PasswordWindowController.swift
  3. // PDF Master
  4. //
  5. // Created by kdanmobile on 2023/11/2.
  6. //
  7. import Cocoa
  8. typealias closePwdCallBack = (_ password: String) -> ()
  9. @objc(PasswordWindowDelegate)
  10. protocol PasswordWindowDelegate: AnyObject {
  11. @objc optional func didFinshedUnlockFile(_ passwordWindowCtr: PasswordWindowController, password: String)
  12. @objc optional func didCancelUnlockFile(_ passwordWindowCtr: PasswordWindowController)
  13. }
  14. @objc class PasswordWindowController: NSWindowController{
  15. var fileURL: URL?
  16. var delegate: PasswordWindowDelegate?
  17. var tag: Int = 0
  18. var password: String?
  19. var closeCallBack: closePwdCallBack?
  20. @IBOutlet var passwordTextField: NSSecureTextField!
  21. @IBOutlet var titleTextField: NSTextField!
  22. @IBOutlet var textField: NSTextField!
  23. @IBOutlet var openButton: NSButton!
  24. @IBOutlet var cancelButton: NSButton!
  25. override init(window: NSWindow?) {
  26. super.init(window: window)
  27. }
  28. required init?(coder: NSCoder) {
  29. super.init(coder: coder)
  30. }
  31. override func windowDidLoad() {
  32. super.windowDidLoad()
  33. titleTextField.stringValue = fileURL?.lastPathComponent ?? ""
  34. textField.stringValue = NSLocalizedString("This PDF is password protected. Please enter the password below to access this PDF.", comment: "")
  35. openButton.title = NSLocalizedString("Open", comment: "")
  36. cancelButton.title = NSLocalizedString("Cancel", comment: "")
  37. }
  38. @IBAction func cancelAction(_ sender: NSButton) {
  39. self.close()
  40. }
  41. @IBAction func OKAction(_ sender: NSButton) {
  42. let pdfDoc = CPDFDocument(url: fileURL!)
  43. if pdfDoc != nil {
  44. let isOk = pdfDoc?.unlock(withPassword: passwordTextField.stringValue)
  45. if isOk! {
  46. if pdfDoc?.permissionsStatus == .owner {
  47. self.password = passwordTextField.stringValue
  48. self.close()
  49. } else{
  50. self.incorrectPasswordAlert()
  51. }
  52. } else {
  53. self.incorrectPasswordAlert()
  54. }
  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. NSWindow.currentWindow().endSheet(self.window!)
  83. self.window?.orderOut(self)
  84. }
  85. }