KMPasswordInputWindowController.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //
  2. // KMPasswordInputWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/12/31.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMPasswordInputWindowController: NSWindowController {
  10. @IBOutlet weak var titleLabel: NSTextField!
  11. @IBOutlet weak var despLabel: NSTextField!
  12. @IBOutlet var passwordInputView: ComponentPasswordView!
  13. @IBOutlet var passwordErrorLabel: NSTextField!
  14. @IBOutlet var cancelButton: ComponentButton!
  15. @IBOutlet var confirmButton: ComponentButton!
  16. static var permissionsStatus: CPDFDocumentPermissions = .none
  17. var confirmButtonVC: KMDesignButton?
  18. var documentURL: URL?
  19. var itemClick: KMPasswordInputWindowItemClick?
  20. var canEncrpty = false
  21. var type: KMPasswordInputWindowType = .open {
  22. didSet {
  23. var fileName = KMLocalizedString("")
  24. if (self.documentURL != nil) {
  25. fileName.append("\(self.documentURL!.lastPathComponent)")
  26. }
  27. titleLabel?.stringValue = KMLocalizedString("Permission Password")
  28. despLabel?.maximumNumberOfLines = 3
  29. despLabel?.lineBreakMode = .byTruncatingTail
  30. despLabel?.cell?.truncatesLastVisibleLine = true
  31. let ps = NSMutableParagraphStyle()
  32. ps.lineSpacing = 5
  33. let despLabelString = "\"\(fileName)\"\(KMLocalizedString("This PDF is password protected. Please enter the password below to access this PDF."))"
  34. despLabel?.attributedStringValue = NSAttributedString(string: despLabelString, attributes: [.foregroundColor : ComponentLibrary.shared.getComponentColorFromKey("colorText/1"), .font : ComponentLibrary.shared.getFontFromKey("mac/body-m-bold"), .paragraphStyle : ps])
  35. }
  36. }
  37. override func windowDidLoad() {
  38. super.windowDidLoad()
  39. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  40. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorPicture/empty-textDefault")
  41. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  42. despLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorPicture/empty-textSecondary")
  43. despLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular")
  44. despLabel.isSelectable = false
  45. passwordInputView.properties = ComponentPasswordProperty(size: .m,
  46. state: .normal,
  47. isError: false,
  48. placeholderString: "Please enter password",
  49. text: "",
  50. isDisabled: false,
  51. errorText: "Here is the error message description.")
  52. passwordInputView.delegate = self
  53. passwordErrorLabel.stringValue = KMLocalizedString("Incorrect password. Please try again.")
  54. passwordErrorLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-xs-regular")
  55. passwordErrorLabel.wantsLayer = true
  56. passwordErrorLabel.isHidden = true
  57. cancelButton.properties = ComponentButtonProperty(type: .default_tertiary,
  58. size: .s,
  59. buttonText: KMLocalizedString("Cancel"),
  60. keepPressState: false)
  61. cancelButton.setTarget(self, action: #selector(cancelButtonAction(_:)))
  62. confirmButton.properties = ComponentButtonProperty(type: .primary,
  63. size: .s,
  64. buttonText: KMLocalizedString("Unlock"),
  65. keepPressState: false)
  66. confirmButton.setTarget(self, action: #selector(confirmButtonAction(_:)))
  67. dealConfirmButtonEnabledState(enabled: true)
  68. }
  69. // MARK: - Actions
  70. @objc func cancelButtonAction(_ sender: NSView) {
  71. // guard let callback = self.itemClick else {
  72. // return
  73. // }
  74. //
  75. // callback(self, 1, "")
  76. }
  77. @objc func confirmButtonAction(_ sender: NSView) {
  78. if (!self.canEncrpty) {
  79. return
  80. }
  81. guard let documentURL = self.documentURL else {
  82. return
  83. }
  84. if (self.type == .open) {
  85. let document: CPDFDocument = CPDFDocument(url: documentURL)
  86. if document.permissionsStatus == .none {
  87. let reuslt = document.unlock(withPassword: passwordInputView.properties.text)
  88. /// CPDFDocumentPermissionsNone 解锁失败
  89. /// CPDFDocumentPermissionsUser 输入的开启密码
  90. /// CPDFDocumentPermissionsOwner 输入的权限密码
  91. KMPasswordInputWindow.permissionsStatus = document.permissionsStatus
  92. if document.permissionsStatus != CPDFDocumentPermissions.none { /// 密码正确
  93. // guard let callback = self.itemClick else {
  94. // return
  95. // }
  96. //
  97. // callback(self ,2, passwordInputView.properties.text)
  98. } else { /// 密码错误
  99. passwordErrorLabel.isHidden = false
  100. passwordErrorLabel.stringValue = KMLocalizedString("Incorrect password. Please try again.")
  101. }
  102. }
  103. return
  104. }
  105. /// 权限密码类型
  106. let document: CPDFDocument = CPDFDocument(url: documentURL)
  107. if (document.isLocked) {
  108. if document.permissionsStatus == CPDFDocumentPermissions.none {
  109. let reuslt = document.unlock(withPassword: passwordInputView.properties.text)
  110. KMPasswordInputWindow.permissionsStatus = document.permissionsStatus
  111. if document.permissionsStatus == .owner { /// 密码正确
  112. // guard let callback = self.itemClick else {
  113. // return
  114. // }
  115. //
  116. // callback(self, 2, passwordInputView.properties.text)
  117. } else { /// 密码错误
  118. passwordErrorLabel.isHidden = false
  119. passwordErrorLabel.stringValue = KMLocalizedString("Incorrect password. Please try again.")
  120. }
  121. }
  122. } else {
  123. if document.permissionsStatus == CPDFDocumentPermissions.user {
  124. document.unlock(withPassword: passwordInputView.properties.text)
  125. KMPasswordInputWindow.permissionsStatus = document.permissionsStatus
  126. if document.permissionsStatus == .owner { /// 密码正确
  127. // guard let callback = self.itemClick else {
  128. // return
  129. // }
  130. //
  131. // callback(self, 2, passwordInputView.properties.text)
  132. } else { /// 密码错误
  133. passwordErrorLabel.isHidden = false
  134. passwordErrorLabel.stringValue = KMLocalizedString("Incorrect password. Please try again.")
  135. }
  136. }
  137. }
  138. }
  139. func dealConfirmButtonEnabledState(enabled: Bool) {
  140. canEncrpty = enabled
  141. confirmButton.properties.isDisabled = enabled ? false : true
  142. confirmButton.reloadData()
  143. }
  144. func updatePasswordState() {
  145. passwordErrorLabel.isHidden = true
  146. if passwordInputView.properties.text.isEmpty {
  147. dealConfirmButtonEnabledState(enabled: false)
  148. } else {
  149. dealConfirmButtonEnabledState(enabled: true)
  150. }
  151. }
  152. override func mouseUp(with event: NSEvent) {
  153. super.mouseUp(with: event)
  154. self.window?.makeFirstResponder(nil)
  155. passwordErrorLabel.isHidden = true
  156. }
  157. }
  158. extension KMPasswordInputWindowController {
  159. @objc class func openWindow(window: NSWindow, type: KMPasswordInputWindowType = .open, url: URL, callback: @escaping (KMPasswordInputWindowResult, String?)->Void) -> KMPasswordInputWindowController {
  160. let passwordWindow = KMPasswordInputWindowController.init(windowNibName: "KMPasswordInputWindowController")
  161. passwordWindow.documentURL = url
  162. passwordWindow.type = type
  163. passwordWindow.itemClick = { pwdWin, index, string in
  164. if let sheetParent = pwdWin.sheetParent {
  165. sheetParent.endSheet(pwdWin)
  166. }
  167. if index == 1 { /// 关闭
  168. callback(.cancel, "")
  169. return
  170. }
  171. /// 解密成功
  172. callback(.success, string)
  173. }
  174. window.beginSheet(passwordWindow.window!)
  175. return passwordWindow
  176. }
  177. }
  178. extension KMPasswordInputWindowController: ComponentPasswordViewDelegate {
  179. func componentPasswordViewDidChanged(inputView: ComponentPasswordView) {
  180. self.updatePasswordState()
  181. }
  182. func componentPasswordViewDidEndEditing(inputView: ComponentPasswordView) {
  183. self.updatePasswordState()
  184. }
  185. }