PasswordWindowController.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // PasswordWindowController.swift
  3. // PDF Master
  4. //
  5. // Created by kdanmobile on 2023/11/2.
  6. //
  7. import Cocoa
  8. @objc(PasswordWindowDelegate)
  9. 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. @IBOutlet var passwordTextField: NSSecureTextField!
  19. @IBOutlet var titleTextField: NSTextField!
  20. @IBOutlet var textField: NSTextField!
  21. @IBOutlet var openButton: NSButton!
  22. @IBOutlet var cancelButton: NSButton!
  23. override init(window: NSWindow?) {
  24. super.init(window: window)
  25. }
  26. required init?(coder: NSCoder) {
  27. super.init(coder: coder)
  28. }
  29. override func windowDidLoad() {
  30. super.windowDidLoad()
  31. titleTextField.stringValue = fileUrl?.lastPathComponent ?? ""
  32. textField.stringValue = NSLocalizedString("This PDF is password protected. Please enter the password below to access this PDF.", comment: "")
  33. openButton.title = NSLocalizedString("Open", comment: "")
  34. cancelButton.title = NSLocalizedString("Cancel", comment: "")
  35. }
  36. @IBAction func cancelAction(_ sender: NSButton) {
  37. self.close()
  38. }
  39. @IBAction func OKAction(_ sender: NSButton) {
  40. let pdfDoc = PDFDocument(url: fileUrl!)
  41. if pdfDoc != nil {
  42. let isOk = pdfDoc?.unlock(withPassword: passwordTextField.stringValue)
  43. if isOk! {
  44. if pdfDoc?.permissionsStatus == .owner {
  45. self.password = passwordTextField.stringValue
  46. self.close()
  47. } else{
  48. self.incorrectPasswordAlert()
  49. }
  50. } else {
  51. self.incorrectPasswordAlert()
  52. }
  53. }
  54. }
  55. func incorrectPasswordAlert() {
  56. let alert = NSAlert()
  57. alert.alertStyle = .critical
  58. alert.messageText = NSLocalizedString("Incorrect password. Please check your password and try again.", comment: "")
  59. alert.runModal()
  60. }
  61. @objc func didEndSheet(_ sheet: NSWindow, returnCode: NSInteger, contextInfo: UnsafeMutableRawPointer?) {
  62. if contextInfo != nil {
  63. let handler = contextInfo!.assumingMemoryBound(to: ((String) -> Void).self).pointee
  64. handler(password ?? "")
  65. }
  66. }
  67. func beginSheetModalForWindow(_ window: NSWindow, completionHandler handler: ((String) -> Void)?) {
  68. // self.window?.beginSheet(window)
  69. NSApp.beginSheet(self.window!,
  70. modalFor: window,
  71. modalDelegate: self,
  72. didEnd: #selector(didEndSheet(_:returnCode:contextInfo:)),
  73. contextInfo: handler != nil ? unsafeBitCast(handler, to: UnsafeMutableRawPointer.self) : nil)
  74. }
  75. override func close() {
  76. NSApp.endSheet(self.window!)
  77. self.window?.orderOut(self)
  78. }
  79. }