DSignatureCreateInfoViewController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. //
  2. // DSignatureCreateInfoViewController.swift
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by Niehaoyu on 2023/9/28.
  6. //
  7. import Cocoa
  8. class DSignatureCreateInfoViewController: NSViewController, NSTextFieldDelegate {
  9. @IBOutlet weak var contendView: NSView!
  10. @IBOutlet weak var titleLabel: NSTextField!
  11. @IBOutlet weak var nameLabel: NSTextField!
  12. @IBOutlet weak var organizationLabel: NSTextField!
  13. @IBOutlet weak var organizationUnitLabel: NSTextField!
  14. @IBOutlet weak var emailLabel: NSTextField!
  15. @IBOutlet weak var countryLabel: NSTextField!
  16. @IBOutlet weak var purposeLabel: NSTextField!
  17. @IBOutlet weak var nameTextFiled: NSTextField!
  18. @IBOutlet weak var organizationTextFiled: NSTextField!
  19. @IBOutlet weak var organizationUnitTextFiled: NSTextField!
  20. @IBOutlet weak var emailTextFiled: NSTextField!
  21. @IBOutlet weak var countryButton: NSPopUpButton!
  22. @IBOutlet weak var purposeButton: NSPopUpButton!
  23. @IBOutlet weak var errorLabel: NSTextField!
  24. @IBOutlet weak var promptBox: NSBox!
  25. @IBOutlet var promptTextView: NSTextView!
  26. @IBOutlet weak var previousStepButton: NSButton!
  27. @IBOutlet weak var continueButton: NSButton!
  28. @IBOutlet weak var textFieldLeftConst: NSLayoutConstraint!
  29. @IBOutlet weak var countryBtnTopConst: NSLayoutConstraint!
  30. // @property(nonatomic) IBOutlet NSLayoutConstraint *bottomOffset;
  31. //
  32. // @property(nonatomic) IBOutlet NSLayoutConstraint *leftBottomOffset;
  33. var codes: [String] = []
  34. var actionBlock: ((_ createVC:DSignatureCreateInfoViewController, _ action:DSignatureActionType, _ cer:NSDictionary, _ certUsage:Int)->Void)?
  35. override func viewWillAppear() {
  36. super.viewWillAppear()
  37. }
  38. override func viewDidLoad() {
  39. super.viewDidLoad()
  40. // Do view setup here.
  41. self.updateButtonState()
  42. self.errorLabel.isHidden = true
  43. self.nameTextFiled.delegate = self
  44. self.emailTextFiled.delegate = self
  45. self.organizationTextFiled.delegate = self
  46. self.organizationUnitTextFiled.delegate = self
  47. self.purposeButton.addItems(withTitles: [NSLocalizedString("Digital Signature", comment: ""),
  48. NSLocalizedString("Data Encryption", comment: ""),
  49. NSLocalizedString("Digital Signatures and Data Encryption", comment: "")])
  50. self.reloadData()
  51. self.nameTextFiled.wantsLayer = true
  52. self.organizationTextFiled.wantsLayer = true
  53. self.organizationUnitTextFiled.wantsLayer = true
  54. self.emailTextFiled.wantsLayer = true
  55. self.nameTextFiled.layer?.borderWidth = 1.0
  56. self.organizationTextFiled.layer?.borderWidth = 1.0
  57. self.organizationUnitTextFiled.layer?.borderWidth = 1.0
  58. self.emailTextFiled.layer?.borderWidth = 1.0
  59. self.nameTextFiled.layer?.cornerRadius = 5.0
  60. self.organizationTextFiled.layer?.cornerRadius = 5.0
  61. self.organizationUnitTextFiled.layer?.cornerRadius = 5.0
  62. self.emailTextFiled.layer?.cornerRadius = 5.0
  63. self.organizationTextFiled.layer?.borderColor = NSColor(red: 2.0/255.0, green: 8.0/255.0, blue: 38.0/255.0, alpha: 0.15).cgColor
  64. self.organizationUnitTextFiled.layer?.borderColor = NSColor(red: 2.0/255.0, green: 8.0/255.0, blue: 38.0/255.0, alpha: 0.15).cgColor
  65. self.localizedLanguage()
  66. self.nameTextFiled.layer?.borderColor = NSColor.clear.cgColor
  67. self.emailTextFiled.layer?.borderColor = NSColor.clear.cgColor
  68. }
  69. func reloadData() {
  70. var countryCodes = NSLocale.isoCountryCodes
  71. self.codes = countryCodes
  72. var codes: [String] = []
  73. let localeID = NSLocale.current.identifier
  74. let components = NSLocale.components(fromLocaleIdentifier: localeID)
  75. let countryCode = components[NSLocale.Key.countryCode.rawValue]
  76. for countryCode in countryCodes {
  77. let identifier = NSLocale.localeIdentifier(fromComponents: [NSLocale.Key.countryCode.rawValue: countryCode])
  78. let local = NSLocale(localeIdentifier: localeID)
  79. let country = local.displayName(forKey: NSLocale.Key.identifier, value: identifier)
  80. codes.append(String(format: "%@ - %@", countryCode, (country ?? "")))
  81. }
  82. self.countryButton.addItems(withTitles: codes)
  83. for index in 0...countryCodes.count-1 {
  84. let cCode = countryCodes[index]
  85. if countryCode == cCode {
  86. self.countryButton.selectItem(at: index)
  87. }
  88. }
  89. let sud = UserDefaults.standard
  90. let loginName = NSFullUserName()
  91. if loginName.isEmpty == false {
  92. self.nameTextFiled.stringValue = loginName
  93. }
  94. self.organizationTextFiled.stringValue = sud.string(forKey: CAuthenticationDepartmentKey) ?? ""
  95. self.organizationUnitTextFiled.stringValue = sud.string(forKey: CAuthenticationCompanyNameKey) ?? ""
  96. self.emailTextFiled.stringValue = sud.string(forKey: CAuthenticationEmailAddressKey) ?? ""
  97. }
  98. func localizedLanguage() {
  99. self.titleLabel.stringValue = NSLocalizedString("Create a Self-signed Digital ID", comment: "")
  100. self.nameLabel.stringValue = NSLocalizedString("Name", comment: "")
  101. self.organizationLabel.stringValue = NSLocalizedString("Organization Name", comment: "")
  102. self.organizationUnitLabel.stringValue = NSLocalizedString("Organization Unit", comment: "")
  103. self.emailLabel.stringValue = NSLocalizedString("Email Address", comment: "")
  104. self.countryLabel.stringValue = NSLocalizedString("Country/Region", comment: "")
  105. self.purposeLabel.stringValue = NSLocalizedString("Purpose", comment: "")
  106. var maxLabelWidth = self.nameLabel.sizeThatFits(CGSize(width: 1000, height: self.nameLabel.frame.size.height)).width
  107. if self.organizationLabel.sizeThatFits(CGSize(width: 1000, height: self.organizationLabel.frame.size.height)).width > maxLabelWidth {
  108. maxLabelWidth = self.organizationLabel.sizeThatFits(CGSize(width: 1000, height: self.organizationLabel.frame.size.height)).width
  109. }
  110. if self.organizationUnitLabel.sizeThatFits(CGSize(width: 1000, height: self.organizationUnitLabel.frame.size.height)).width > maxLabelWidth {
  111. maxLabelWidth = self.organizationUnitLabel.sizeThatFits(CGSize(width: 1000, height: self.organizationUnitLabel.frame.size.height)).width
  112. }
  113. if self.emailLabel.sizeThatFits(CGSize(width: 1000, height: self.emailLabel.frame.size.height)).width > maxLabelWidth {
  114. maxLabelWidth = self.emailLabel.sizeThatFits(CGSize(width: 1000, height: self.emailLabel.frame.size.height)).width
  115. }
  116. if self.countryLabel.sizeThatFits(CGSize(width: 1000, height: self.countryLabel.frame.size.height)).width > maxLabelWidth {
  117. maxLabelWidth = self.countryLabel.sizeThatFits(CGSize(width: 1000, height: self.countryLabel.frame.size.height)).width
  118. }
  119. if self.purposeLabel.sizeThatFits(CGSize(width: 1000, height: self.purposeLabel.frame.size.height)).width > maxLabelWidth {
  120. maxLabelWidth = self.purposeLabel.sizeThatFits(CGSize(width: 1000, height: self.purposeLabel.frame.size.height)).width
  121. }
  122. if maxLabelWidth < 90 {
  123. maxLabelWidth = 90
  124. }
  125. self.textFieldLeftConst.constant = maxLabelWidth + 10
  126. self.promptTextView.string = String(format: "%@\n\n%@",NSLocalizedString("Enter the identity information to be used for creating the self-signed digital ID.", comment: ""), NSLocalizedString("Digital IDs that are self-signed by individuals do not provide the assurance that the identity information is valid. For this reason they may not be accepted in some use cases.", comment: ""))
  127. self.nameTextFiled.placeholderString = NSLocalizedString("Enter name...", comment: "");
  128. self.errorLabel.stringValue = NSLocalizedString("Email address is not valid", comment: "");
  129. self.organizationTextFiled.placeholderString = NSLocalizedString("Enter organization name...", comment: "");
  130. self.organizationUnitTextFiled.placeholderString = NSLocalizedString("Enter organization unit...", comment: "");
  131. self.emailTextFiled.placeholderString = NSLocalizedString("Enter Email", comment: "");
  132. self.previousStepButton.title = NSLocalizedString("Previous Step", comment: "");
  133. self.continueButton.title = NSLocalizedString("Continue", comment: "");
  134. }
  135. func validateEmail(_ strEmail: String) -> Bool {
  136. let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
  137. let emailTest: NSPredicate = NSPredicate(format: "SELF MATCHES %@", emailRegex)
  138. return emailTest.evaluate(with: strEmail)
  139. }
  140. func updateButtonState() {
  141. if self.nameTextFiled.stringValue.isEmpty == false && self.validateEmail(self.emailTextFiled.stringValue) {
  142. self.continueButton.isEnabled = true
  143. } else {
  144. self.continueButton.isEnabled = false
  145. }
  146. }
  147. func updateTextFiedState() {
  148. if self.nameTextFiled.stringValue.isEmpty == false {
  149. self.nameTextFiled.updateState(false)
  150. } else {
  151. self.nameTextFiled.updateState(true)
  152. }
  153. if self.emailTextFiled.stringValue.isEmpty == false {
  154. self.emailTextFiled.updateState(false)
  155. } else {
  156. self.emailTextFiled.updateState(true)
  157. }
  158. }
  159. //MARK: IBAction
  160. @IBAction func closeAction(_ sender: Any) {
  161. guard let callBack = self.actionBlock else {
  162. return
  163. }
  164. callBack(self, .cancel, NSDictionary(), 0)
  165. }
  166. @IBAction func previousAction(_ sender: Any) {
  167. guard let callBack = self.actionBlock else {
  168. return
  169. }
  170. callBack(self, .previousStep, NSDictionary(), 0)
  171. }
  172. @IBAction func continueAction(_ sender: Any) {
  173. self.view.window?.makeFirstResponder(nil)
  174. if self.validateEmail(self.emailTextFiled.stringValue) == false {
  175. return
  176. }
  177. let cer = NSMutableDictionary.init()
  178. cer.setValue(self.nameTextFiled.stringValue, forKey: "CN")
  179. cer.setObject(self.emailTextFiled.stringValue, forKey: "emailAddress" as NSCopying)
  180. if self.codes.count > self.countryButton.indexOfSelectedItem {
  181. let string = self.codes[self.countryButton.indexOfSelectedItem]
  182. cer.setValue(string, forKey: "C")
  183. }
  184. if self.organizationTextFiled.stringValue.isEmpty == false {
  185. cer.setValue(self.organizationTextFiled.stringValue, forKey: "O")
  186. }
  187. if self.organizationUnitTextFiled.stringValue.isEmpty == false {
  188. cer.setValue(self.organizationUnitTextFiled.stringValue, forKey: "OU")
  189. }
  190. let dex = self.purposeButton.index(of: self.purposeButton.selectedItem!)
  191. guard let callBack = self.actionBlock else {
  192. return
  193. }
  194. callBack(self, .confirm, cer, dex)
  195. }
  196. //MARK: NSTextFieldDelegate
  197. func controlTextDidChange(_ obj: Notification) {
  198. self.updateButtonState()
  199. if obj.object == nil {
  200. return
  201. }
  202. let textField = obj.object as! NSTextField
  203. if self.emailTextFiled.isEqual(textField) ||
  204. self.nameTextFiled.isEqual(textField) {
  205. if (textField.stringValue.isEmpty == false) {
  206. textField.updateState(false)
  207. } else {
  208. textField.updateState(true)
  209. }
  210. }
  211. if self.emailTextFiled.isEqual(textField) {
  212. self.errorLabel.isHidden = true
  213. self.countryBtnTopConst.constant = 16.0;
  214. }
  215. }
  216. func controlTextDidEndEditing(_ obj: Notification) {
  217. let fied: NSTextField = obj.object as! NSTextField;
  218. if fied.isEqual(self.emailTextFiled) {
  219. if self.validateEmail(self.emailTextFiled.stringValue) == true {
  220. self.errorLabel.isHidden = true
  221. self.countryBtnTopConst.constant = 16.0;
  222. } else {
  223. self.errorLabel.isHidden = false;
  224. self.countryBtnTopConst.constant = self.errorLabel.frame.size.height + 12;
  225. }
  226. }
  227. }
  228. }