CDSignatureCertificateCustomViewController.swift 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. //
  2. // CDSignatureCertificateCustomViewController.swift
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by Niehaoyu on 2023/10/12.
  6. //
  7. import Cocoa
  8. class CDSignatureCertificateCustomViewController: NSViewController, NSTextFieldDelegate {
  9. @IBOutlet weak var titleLabel: NSTextField!
  10. @IBOutlet weak var textButton: NSButton!
  11. @IBOutlet weak var textLineBox: NSBox!
  12. @IBOutlet weak var drawButton: NSButton!
  13. @IBOutlet weak var drawLineBox: NSBox!
  14. @IBOutlet weak var imageButton: NSButton!
  15. @IBOutlet weak var imageLineBox: NSBox!
  16. @IBOutlet weak var noneButton: NSButton!
  17. @IBOutlet weak var noneLineBox: NSBox!
  18. @IBOutlet weak var drawBox: NSBox!
  19. @IBOutlet weak var drawImageView: NSImageView!
  20. @IBOutlet weak var showDrawButton: NSButton!
  21. @IBOutlet weak var clearButton: NSButton!
  22. @IBOutlet weak var browseButton: NSButton!
  23. @IBOutlet weak var textLabel: NSTextField!
  24. @IBOutlet weak var nameButton: NSButton!
  25. @IBOutlet weak var dateButton: NSButton!
  26. @IBOutlet weak var logoButton: NSButton!
  27. @IBOutlet weak var reasonButton: NSButton!
  28. @IBOutlet weak var distinguishedButton: NSButton!
  29. @IBOutlet weak var versionButton: NSButton!
  30. @IBOutlet weak var locationButton: NSButton!
  31. @IBOutlet weak var labelsButton: NSButton!
  32. @IBOutlet weak var directionLabel: NSTextField!
  33. @IBOutlet weak var leftButton: NSButton!
  34. @IBOutlet weak var rightButton: NSButton!
  35. @IBOutlet weak var reasonLabel: NSTextField!
  36. @IBOutlet weak var reasonPopUpButton: NSPopUpButton!
  37. @IBOutlet weak var locationLabel: NSTextField!
  38. @IBOutlet weak var locationTextField: NSTextField!
  39. @IBOutlet weak var nameLabel: NSTextField!
  40. @IBOutlet weak var nameTextField: NSTextField!
  41. @IBOutlet weak var cancelButton: NSButton!
  42. @IBOutlet weak var continueButton: NSButton!
  43. @IBOutlet weak var locationTopOffset: NSLayoutConstraint!
  44. var _customType: CDSignatureCustomType = .none
  45. var signatureWidget: CPDFSignatureWidgetAnnotation!
  46. var appearanceData: DSignatureAppearanceData!
  47. var cert: KMDSignatureModel!
  48. var isCreat: Bool = false
  49. var photoImage: NSImage!
  50. var drawImage: NSImage!
  51. var drawText: String!
  52. var drawViewController = CDSignatureDrawViewController.init()
  53. var textViewController = CDSignatureTextViewController.init()
  54. var actionBlock: ((_ customVC: CDSignatureCertificateCustomViewController, _ type: DSignatureActionType, _ appearanceData: DSignatureAppearanceData)->Void)?
  55. override func viewWillAppear() {
  56. super.viewWillAppear()
  57. self.logoButton.state = .off;
  58. self.labelsButton.state = .off
  59. self.distinguishedButton.state = .off
  60. self.nameButton.state = .off
  61. self.reasonButton.state = .off
  62. self.locationButton.state = .off
  63. self.dateButton.state = .off
  64. self.versionButton.state = .off
  65. self.reasonLabel.isHidden = true
  66. self.reasonPopUpButton.isHidden = true
  67. self.locationLabel.isHidden = true
  68. self.locationTextField.isHidden = true
  69. self.locationTextField.delegate = self
  70. if (self.isCreat) {
  71. self.nameTextField.stringValue = "";
  72. self.appearanceData = DSignatureApperanceManager.manager.standardDSignatureAppearanceData()
  73. if self.cert.name.isEmpty == false {
  74. self.appearanceData.signatureConfig.text = self.cert.name
  75. }
  76. } else {
  77. if self.appearanceData.appearanceName.length > 0 {
  78. self.nameTextField.stringValue = self.appearanceData.appearanceName as String
  79. } else {
  80. self.nameTextField.stringValue = ""
  81. }
  82. }
  83. if (self.appearanceData.signatureConfig.isDrawLogo) {
  84. self.logoButton.state = .on;
  85. }
  86. if (self.appearanceData.signatureConfig.isDrawKey) {
  87. self.labelsButton.state = .on;
  88. }
  89. let keys = NSMutableArray()
  90. for item in self.appearanceData.signatureConfig.contents {
  91. keys.add(item.key as String)
  92. }
  93. if keys.contains(DN_KEY) {
  94. self.distinguishedButton.state = .on
  95. }
  96. if keys.contains(NAME_KEY) {
  97. self.nameButton.state = .on
  98. }
  99. if keys.contains(REASON_KEY) {
  100. self.reasonButton.state = .on
  101. self.reasonLabel.isHidden = false
  102. self.reasonPopUpButton.isHidden = false
  103. }
  104. if keys.contains(LOCATION_KEY) {
  105. self.locationButton.state = .on
  106. self.locationTextField.isHidden = false
  107. self.locationLabel.isHidden = false
  108. if self.reasonButton.state == .on {
  109. self.locationTopOffset.constant = 56;
  110. } else {
  111. self.locationTopOffset.constant = 12;
  112. }
  113. var valueString = String()
  114. for item in self.appearanceData.signatureConfig.contents {
  115. if item.key == LOCATION_KEY {
  116. valueString = item.value
  117. break
  118. }
  119. }
  120. self.locationTextField.stringValue = valueString
  121. }
  122. if keys.contains(DATE_KEY) {
  123. self.dateButton.state = .on
  124. }
  125. if keys.contains(VERSION_KEY) {
  126. self.versionButton.state = .on
  127. }
  128. if (self.appearanceData.signatureConfig.isContentAlginLeft) {
  129. self.leftButton.image = NSImage(named: "ImageNamePropertybarTextalignLeftNor")
  130. self.rightButton.image = NSImage(named: "ImageNamePropertybarTextalignRightSel")
  131. } else {
  132. self.leftButton.image = NSImage(named: "ImageNamePropertybarTextalignLeftSel")
  133. self.rightButton.image = NSImage(named: "ImageNamePropertybarTextalignRightNor");
  134. }
  135. if (self.appearanceData.drawType == .image) {
  136. self.photoImage = self.appearanceData.signatureConfig.image
  137. } else if (self.appearanceData.drawType == .draw) {
  138. self.drawImage = self.appearanceData.signatureConfig.image
  139. } else if (self.appearanceData.drawType == .text) {
  140. self.drawText = self.appearanceData.signatureConfig.text
  141. }
  142. self.customType = self.appearanceData.drawType
  143. self.reloadData()
  144. }
  145. override func viewDidLoad() {
  146. super.viewDidLoad()
  147. // Do view setup here.
  148. self.localizedLanguage()
  149. self.reasonLabel.isHidden = true
  150. self.reasonPopUpButton.isHidden = true
  151. self.locationLabel.isHidden = true
  152. self.locationTextField.isHidden = true
  153. self.textLineBox.isHidden = true
  154. self.imageLineBox.isHidden = true
  155. self.drawLineBox.isHidden = true
  156. self.noneLineBox.isHidden = true
  157. self.textLineBox.fillColor = KMAppearance.Layout.mColor()
  158. self.imageLineBox.fillColor = KMAppearance.Layout.mColor()
  159. self.drawLineBox.fillColor = KMAppearance.Layout.mColor()
  160. self.noneLineBox.fillColor = KMAppearance.Layout.mColor()
  161. self.titleLabel.textColor = NSColor.labelColor
  162. self.nameLabel.textColor = NSColor.labelColor
  163. self.drawBox.borderColor = NSColor(red: 165.0/255.0, green: 167.0/255.0, blue: 179.0/255.0, alpha: 1.0)
  164. self.reasonLabel.textColor = NSColor.labelColor
  165. self.locationLabel.textColor = NSColor.labelColor
  166. self.locationTextField.textColor = NSColor.labelColor
  167. self.nameTextField.textColor = NSColor.labelColor
  168. self.leftButton.wantsLayer = true
  169. self.rightButton.wantsLayer = true
  170. self.leftButton.layer?.cornerRadius = 5.0
  171. self.rightButton.layer?.cornerRadius = 5.0
  172. self.locationTextField.delegate = self;
  173. self.textViewController.actionBlock = { textVC, inputText in
  174. if inputText.count > 0 {
  175. self.drawText = inputText
  176. } else {
  177. self.drawText = ""
  178. }
  179. self.appearanceData.isInputDSignatureText = true
  180. self.appearanceData.signatureConfig.text = self.drawText
  181. self.reloadData()
  182. }
  183. self.drawViewController.actionBlock = { drawVC, image in
  184. if image.size.width > 0 &&
  185. image.size.height > 0 {
  186. self.drawImage = image
  187. self.appearanceData.signatureConfig.image = image
  188. }
  189. self.reloadData()
  190. }
  191. }
  192. //MARK: Setter
  193. var customType: CDSignatureCustomType {
  194. set {
  195. _customType = newValue;
  196. let norColor = KMAppearance.Layout.h1Color()
  197. let selColor = KMAppearance.Layout.mColor()
  198. self.appearanceData.drawType = _customType
  199. self.appearanceData.signatureConfig.isDrawOnlyContent = false
  200. self.textLineBox.isHidden = true
  201. self.imageLineBox.isHidden = true
  202. self.noneLineBox.isHidden = true
  203. self.drawLineBox.isHidden = true
  204. self.textButton.setTitleColor(norColor)
  205. self.drawButton.setTitleColor(norColor)
  206. self.imageButton.setTitleColor(norColor)
  207. self.noneButton.setTitleColor(norColor)
  208. if self.customType == .draw {
  209. self.drawLineBox.isHidden = false
  210. self.drawButton.setTitleColor(selColor)
  211. self.textButton.image = NSImage(named: "ImageNameDSignatureCustomText")
  212. self.drawButton.image = NSImage(named: "ImageNameDSignatureCustomSignSelect")
  213. self.imageButton.image = NSImage(named: "ImageNameDSignatureCustomImage")
  214. self.noneButton.image = NSImage(named: "ImageNameDSignatureCustomNone")
  215. self.showDrawButton.isHidden = false
  216. self.browseButton.isHidden = true
  217. self.clearButton.isHidden = true
  218. self.appearanceData.signatureConfig.text = nil;
  219. self.appearanceData.signatureConfig.image = self.drawImage;
  220. } else if self.customType == .image {
  221. self.imageLineBox.isHidden = false;
  222. self.imageButton.setTitleColor(selColor)
  223. self.textButton.image = NSImage(named: "ImageNameDSignatureCustomText")
  224. self.drawButton.image = NSImage(named: "ImageNameDSignatureCustomSign")
  225. self.imageButton.image = NSImage(named: "ImageNameDSignatureCustomImageSelect")
  226. self.noneButton.image = NSImage(named: "ImageNameDSignatureCustomNone")
  227. self.showDrawButton.isHidden = true
  228. self.browseButton.isHidden = false
  229. self.clearButton.isHidden = false
  230. self.appearanceData.signatureConfig.text = nil
  231. self.appearanceData.signatureConfig.image = self.photoImage
  232. } else if self.customType == .none {
  233. self.noneLineBox.isHidden = false
  234. self.noneButton.setTitleColor(selColor)
  235. self.textButton.image = NSImage(named: "ImageNameDSignatureCustomText")
  236. self.drawButton.image = NSImage(named: "ImageNameDSignatureCustomSign")
  237. self.imageButton.image = NSImage(named: "ImageNameDSignatureCustomImage")
  238. self.noneButton.image = NSImage(named: "ImageNameDSignatureCustomNoneSelect")
  239. self.showDrawButton.isHidden = true;
  240. self.browseButton.isHidden = true;
  241. self.clearButton.isHidden = true;
  242. self.appearanceData.signatureConfig.text = nil;
  243. self.appearanceData.signatureConfig.image = nil;
  244. self.appearanceData.signatureConfig.isDrawOnlyContent = true;
  245. } else if self.customType == .text {
  246. self.textLineBox.isHidden = false
  247. self.textButton.setTitleColor(selColor)
  248. self.textButton.image = NSImage(named: "ImageNameDSignatureCustomTextSelect")
  249. self.drawButton.image = NSImage(named: "ImageNameDSignatureCustomSign")
  250. self.imageButton.image = NSImage(named: "ImageNameDSignatureCustomImage")
  251. self.noneButton.image = NSImage(named: "ImageNameDSignatureCustomNone")
  252. self.showDrawButton.isHidden = true;
  253. self.browseButton.isHidden = true;
  254. self.clearButton.isHidden = true;
  255. self.appearanceData.signatureConfig.image = nil;
  256. self.appearanceData.signatureConfig.text = self.drawText
  257. }
  258. self.reloadData()
  259. }
  260. get {
  261. return _customType;
  262. }
  263. }
  264. //MARK: Method
  265. func localizedLanguage() {
  266. self.titleLabel.stringValue = NSLocalizedString("Customize the Signature Appearance", comment: "")
  267. self.nameLabel.stringValue = NSLocalizedString("Preset Name", comment: "") + ":"
  268. self.textButton.title = NSLocalizedString("Text", comment: "");
  269. self.imageButton.title = NSLocalizedString("Image", comment: "");
  270. self.drawButton.title = NSLocalizedString("Draw", comment: "");
  271. self.noneButton.title = NSLocalizedString("None", comment: "");
  272. self.browseButton.title = NSLocalizedString("Browse", comment: "");
  273. self.clearButton.title = NSLocalizedString("Clear", comment: "");
  274. self.showDrawButton.title = NSLocalizedString("Click here to draw", comment: "");
  275. let txtColor = KMAppearance.Interactive.a0Color()
  276. self.browseButton.setTitleColor(txtColor)
  277. self.clearButton.setTitleColor(txtColor)
  278. self.showDrawButton.setTitleColor(txtColor)
  279. self.continueButton.title = NSLocalizedString("Continue", comment: "");
  280. self.cancelButton.title = NSLocalizedString("Cancel", comment: "");
  281. self.textLabel.stringValue = NSLocalizedString("Include Text", comment: "");
  282. self.directionLabel.stringValue = String(format: "%@:",NSLocalizedString("Text Alignment", comment: ""));
  283. self.nameButton.title = NSLocalizedString("Name", comment: "");
  284. self.dateButton.title = NSLocalizedString("Date", comment: "");
  285. self.locationButton.title = NSLocalizedString("Location", comment: "");
  286. self.reasonButton.title = NSLocalizedString("Reason", comment: "");
  287. self.distinguishedButton.title = NSLocalizedString("Distinguished Name", comment: "");
  288. self.versionButton.title = VERSION_KEY
  289. self.logoButton.title = NSLocalizedString("Logo", comment: "");
  290. self.labelsButton.title = NSLocalizedString("Include Text", comment: "");
  291. self.locationLabel.stringValue = String(format:"%@:",NSLocalizedString("Location", comment: ""));
  292. self.reasonLabel.stringValue = String(format:"%@:",NSLocalizedString("Reason", comment: ""));
  293. let titles = [String(format:" %@",NSLocalizedString("none", comment: "")),
  294. String(format:" %@",NSLocalizedString("I am the author of this document", comment: "")),
  295. String(format:" %@",NSLocalizedString("I have reviewed this document", comment: "")),
  296. String(format:" %@",NSLocalizedString("I'm in the process of approving this document", comment: "")),
  297. String(format:" %@",NSLocalizedString("I certify the accuracy and authenticity of this document", comment: "")),
  298. String(format:" %@",NSLocalizedString("I have signed this document to indicate that I agree to the terms defined", comment: "")),
  299. String(format:" %@",NSLocalizedString("I agree with the specified portions of this document", comment: ""))]
  300. self.reasonPopUpButton.addItems(withTitles: titles)
  301. }
  302. func reloadData() {
  303. for item in self.appearanceData.signatureConfig.contents {
  304. if item.key == DATE_KEY {
  305. let dateFormatter = DateFormatter.init()
  306. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  307. let dateString = dateFormatter.string(from: NSDate() as Date)
  308. item.value = dateString
  309. break
  310. }
  311. }
  312. self.signatureWidget.signAppearanceConfig(self.appearanceData.signatureConfig)
  313. let image: NSImage = self.signatureWidget.contentImage()
  314. self.drawImageView.image = image
  315. }
  316. //MARK: IBAction
  317. @IBAction func closeAction(_ sender: Any) {
  318. guard let callBack = self.actionBlock else {
  319. return
  320. }
  321. callBack(self, .cancel, DSignatureAppearanceData())
  322. }
  323. @IBAction func buttonItemClick_CustomType(_ sender: NSButton) {
  324. self.customType = CDSignatureCustomType(rawValue: sender.tag) ?? .none;
  325. if (self.customType == .text) {
  326. // self.textViewController.drawText = self.drawText
  327. // self.presentAsSheet(self.textViewController)
  328. } else if self.customType == .draw {
  329. self.buttonItemClick_ShowDraw(self.drawButton)
  330. }
  331. }
  332. @IBAction func buttonItemClick_ShowDraw(_ sender: NSButton) {
  333. self.drawViewController.drawImage = self.drawImage;
  334. self.presentAsSheet(self.drawViewController)
  335. }
  336. @IBAction func buttonItemClick_Clear(_ sender: Any) {
  337. self.photoImage = nil
  338. self.appearanceData.signatureConfig.image = nil
  339. self.reloadData()
  340. }
  341. @IBAction func buttonItemClick_Browse(_ sender: Any) {
  342. let openPanel = NSOpenPanel()
  343. openPanel.canChooseDirectories = true
  344. openPanel.canChooseFiles = true
  345. openPanel.allowsMultipleSelection = false;
  346. openPanel.allowedFileTypes = KMPDFSignatureImageView().supportedImageTypes()
  347. openPanel.beginSheetModal(for: self.view.window!) { result in
  348. if result == .OK {
  349. let fileURL = openPanel.urls.first
  350. self.appearanceData.signatureConfig.text = ""
  351. self.photoImage = NSImage(contentsOf: fileURL!)
  352. self.appearanceData.signatureConfig.image = self.photoImage
  353. self.reloadData()
  354. }
  355. }
  356. }
  357. @IBAction func buttonItemClick_ShowText(_ sender: Any) {
  358. let contents = NSMutableArray(array: self.appearanceData.signatureConfig.contents)
  359. if contents.count <= 1 && self.customType == .none {
  360. let configItem: CPDFSignatureConfigItem = contents.firstObject as! CPDFSignatureConfigItem
  361. if configItem.key == NAME_KEY && self.appearanceData.signatureConfig.isDrawKey {
  362. if self.nameButton.state == .off {
  363. contents.removeAllObjects()
  364. }
  365. if self.labelsButton.state == .off {
  366. self.appearanceData.signatureConfig.isDrawKey = false
  367. }
  368. }
  369. }
  370. var configItem: CPDFSignatureConfigItem!
  371. if self.nameButton.isEqual(sender) {
  372. if self.nameButton.state == .on {
  373. configItem = CPDFSignatureConfigItem.init()
  374. configItem.key = NAME_KEY
  375. configItem.value = NSLocalizedString("<your common name here>", comment: "")
  376. contents.add(configItem as Any)
  377. } else {
  378. for item in contents {
  379. let exitItem: CPDFSignatureConfigItem = item as! CPDFSignatureConfigItem
  380. if exitItem.key == NAME_KEY {
  381. configItem = item as? CPDFSignatureConfigItem
  382. break
  383. }
  384. }
  385. if configItem != nil {
  386. contents.remove(configItem as Any)
  387. }
  388. }
  389. } else if self.dateButton.isEqual(sender) {
  390. if self.dateButton.state == .on {
  391. configItem = CPDFSignatureConfigItem.init()
  392. configItem.key = DATE_KEY
  393. let dateFormatter = DateFormatter.init()
  394. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  395. configItem.value = dateFormatter.string(from: NSDate() as Date)
  396. contents.add(configItem as Any)
  397. } else {
  398. for item in contents {
  399. let exitItem: CPDFSignatureConfigItem = item as! CPDFSignatureConfigItem
  400. if exitItem.key == DATE_KEY {
  401. configItem = item as? CPDFSignatureConfigItem
  402. break
  403. }
  404. }
  405. if configItem != nil {
  406. contents.remove(configItem as Any)
  407. }
  408. }
  409. } else if self.reasonButton.isEqual(sender) {
  410. if (self.reasonButton.state == .on) {
  411. self.locationTopOffset.constant = 56.0;
  412. } else {
  413. self.locationTopOffset.constant = 12.0;
  414. }
  415. if self.reasonButton.state == .on {
  416. self.reasonPopUpButton.isHidden = false
  417. self.reasonLabel.isHidden = false
  418. self.reasonPopUpButton.selectItem(at: 0)
  419. configItem = CPDFSignatureConfigItem.init()
  420. configItem.key = REASON_KEY
  421. configItem.value = self.reasonPopUpButton.title
  422. if configItem.value == "" ||
  423. configItem.value == String(format: " %@", NSLocalizedString("none", comment: "")) {
  424. configItem.value = NSLocalizedString("<your signing reason here>", comment: "")
  425. }
  426. contents.add(configItem as Any)
  427. } else {
  428. self.reasonPopUpButton.isHidden = true
  429. self.reasonLabel.isHidden = true
  430. for item in contents {
  431. let exitItem: CPDFSignatureConfigItem = item as! CPDFSignatureConfigItem
  432. if exitItem.key == REASON_KEY {
  433. configItem = item as? CPDFSignatureConfigItem
  434. break
  435. }
  436. }
  437. if configItem != nil {
  438. contents.remove(configItem as Any)
  439. }
  440. }
  441. } else if self.locationButton.isEqual(sender) {
  442. if self.locationButton.state == .on {
  443. self.locationLabel.isHidden = false
  444. self.locationTextField.isHidden = false
  445. if (self.reasonButton.state == .on) {
  446. self.locationTopOffset.constant = 56.0;
  447. } else {
  448. self.locationTopOffset.constant = 12.0;
  449. }
  450. configItem = CPDFSignatureConfigItem.init()
  451. configItem.key = LOCATION_KEY
  452. if self.locationTextField.stringValue.isEmpty {
  453. configItem.value = NSLocalizedString("<your signing location here>", comment: "")
  454. } else {
  455. configItem.value = self.locationTextField.stringValue
  456. }
  457. contents.add(configItem as Any)
  458. } else {
  459. self.locationLabel.isHidden = true
  460. self.locationTextField.isHidden = true
  461. for item in contents {
  462. let exitItem: CPDFSignatureConfigItem = item as! CPDFSignatureConfigItem
  463. if exitItem.key == LOCATION_KEY {
  464. configItem = item as? CPDFSignatureConfigItem
  465. break
  466. }
  467. }
  468. if configItem != nil {
  469. contents.remove(configItem as Any)
  470. }
  471. }
  472. } else if self.distinguishedButton.isEqual(sender) {
  473. if self.distinguishedButton.state == .on {
  474. configItem = CPDFSignatureConfigItem.init()
  475. configItem.key = DN_KEY
  476. configItem.value = NSLocalizedString("<your distinguished name here>", comment: "")
  477. contents.add(configItem as Any)
  478. } else {
  479. for item in contents {
  480. let exitItem: CPDFSignatureConfigItem = item as! CPDFSignatureConfigItem
  481. if exitItem.key == DN_KEY {
  482. configItem = item as? CPDFSignatureConfigItem
  483. break
  484. }
  485. }
  486. if configItem != nil {
  487. contents.remove(configItem as Any)
  488. }
  489. }
  490. } else if self.versionButton.isEqual(sender) {
  491. if self.versionButton.state == .on {
  492. configItem = CPDFSignatureConfigItem.init()
  493. configItem.key = VERSION_KEY
  494. let infoDictionary = Bundle.main.infoDictionary!
  495. // CFShow(infoDictionary)
  496. let app_Version = infoDictionary["CFBundleShortVersionString"]
  497. configItem.value = app_Version as? String
  498. contents.add(configItem as Any)
  499. } else {
  500. for item in contents {
  501. let exitItem: CPDFSignatureConfigItem = item as! CPDFSignatureConfigItem
  502. if exitItem.key == VERSION_KEY {
  503. configItem = item as? CPDFSignatureConfigItem
  504. break
  505. }
  506. }
  507. if configItem != nil {
  508. contents.remove(configItem as Any)
  509. }
  510. }
  511. } else if self.logoButton.isEqual(sender) {
  512. if self.logoButton.state == .on {
  513. self.appearanceData.signatureConfig.isDrawLogo = true
  514. } else {
  515. self.appearanceData.signatureConfig.isDrawLogo = false
  516. }
  517. }else if self.labelsButton.isEqual(sender) {
  518. if self.labelsButton.state == .on {
  519. self.appearanceData.signatureConfig.isDrawKey = true
  520. } else {
  521. self.appearanceData.signatureConfig.isDrawKey = false
  522. }
  523. }
  524. if self.customType == .none && contents.count == 0 {
  525. configItem = CPDFSignatureConfigItem.init()
  526. configItem.key = NAME_KEY
  527. configItem.value = NSLocalizedString("<your common name here>", comment: "")
  528. contents.add(configItem as Any)
  529. self.appearanceData.signatureConfig.isDrawKey = true
  530. }
  531. let sortContents:[Any] = KMDSignatureManager.default().sortContens((contents as! [Any]))
  532. self.appearanceData.signatureConfig.contents = (sortContents as! [CPDFSignatureConfigItem])
  533. self.reloadData()
  534. }
  535. @IBAction func buttonItemClick_Algin(_ sender: NSButton) {
  536. if (sender.tag == 0) {
  537. self.appearanceData.signatureConfig.isContentAlginLeft = false;
  538. self.leftButton.image = NSImage(named: "ImageNamePropertybarTextalignLeftSel")
  539. self.rightButton.image = NSImage(named: "ImageNamePropertybarTextalignRightNor")
  540. } else {
  541. self.appearanceData.signatureConfig.isContentAlginLeft = true;
  542. self.leftButton.image = NSImage(named: "ImageNamePropertybarTextalignLeftNor")
  543. self.rightButton.image = NSImage(named: "ImageNamePropertybarTextalignRightSel")
  544. }
  545. self.reloadData()
  546. }
  547. @IBAction func buttonItemClick_Reason(_ sender: Any) {
  548. let contents:[CPDFSignatureConfigItem] = self.appearanceData.signatureConfig.contents
  549. for object in contents {
  550. if object.key == REASON_KEY {
  551. if self.reasonPopUpButton.title.count > 2 {
  552. let fromStr = self.reasonPopUpButton.title.dropFirst(2)
  553. if self.reasonPopUpButton.title == String(format: " %@", NSLocalizedString("none", comment: "")) {
  554. object.value = NSLocalizedString("<your signing reason here>", comment: "")
  555. } else {
  556. object.value = String(fromStr)
  557. }
  558. }
  559. break
  560. }
  561. }
  562. self.reloadData()
  563. }
  564. @IBAction func buttonItemClick_Cancel(_ sender: Any) {
  565. guard let callBack = self.actionBlock else {
  566. return
  567. }
  568. callBack(self, .previousStep, DSignatureAppearanceData())
  569. }
  570. @IBAction func buttonItemClick_Continue(_ sender: Any) {
  571. let name = self.nameTextField.stringValue
  572. if (self.isCreat) {
  573. let at: [DSignatureAppearanceData] = DSignatureApperanceManager.manager.signatures as! [DSignatureAppearanceData]
  574. var names = NSMutableArray()
  575. for data in at {
  576. names.add(data.appearanceName as Any)
  577. }
  578. if names.contains(name) {
  579. let alert = NSAlert.init()
  580. alert.messageText = NSLocalizedString("The name already exists, please enter it again!", comment: "")
  581. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  582. alert.runModal()
  583. return
  584. }
  585. self.appearanceData.tag = DSignatureApperanceManager.manager.tagString()
  586. }
  587. if name.count > 0 {
  588. self.appearanceData.appearanceName = self.nameTextField.stringValue as NSString
  589. } else {
  590. self.appearanceData.appearanceName = DSignatureApperanceManager.manager.tagString()
  591. }
  592. guard let callBack = self.actionBlock else {
  593. return
  594. }
  595. callBack(self, .confirm, self.appearanceData)
  596. }
  597. //MARK: NSTextFieldDelegate
  598. func controlTextDidChange(_ obj: Notification) {
  599. if obj.object == nil {
  600. return
  601. }
  602. let textField = obj.object as! NSTextField
  603. if self.locationTextField.isEqual(textField) {
  604. let contents = self.appearanceData.signatureConfig.contents!
  605. for object in contents {
  606. if object.key == LOCATION_KEY {
  607. if self.locationTextField.stringValue.isEmpty == true {
  608. object.value = NSLocalizedString("<your signing location here>", comment: "")
  609. } else {
  610. object.value = self.locationTextField.stringValue
  611. }
  612. break
  613. }
  614. }
  615. self.reloadData()
  616. }
  617. }
  618. }