CDSignatureCertificateCustomViewController.swift 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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(kNAME_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. let document = CPDFDocument(url: fileURL)
  351. if !document!.allowsCopying || !document!.allowsPrinting {
  352. let alert = NSAlert()
  353. alert.alertStyle = .critical
  354. alert.messageText = NSLocalizedString("This is a secured document. Editing is not permitted.", comment: "")
  355. alert.runModal()
  356. return
  357. }
  358. self.appearanceData.signatureConfig.text = ""
  359. self.photoImage = NSImage(contentsOf: fileURL!)
  360. self.appearanceData.signatureConfig.image = self.photoImage
  361. self.reloadData()
  362. }
  363. }
  364. }
  365. @IBAction func buttonItemClick_ShowText(_ sender: Any) {
  366. let contents = NSMutableArray(array: self.appearanceData.signatureConfig.contents)
  367. if contents.count <= 1 && self.customType == .none {
  368. let configItem: CPDFSignatureConfigItem = contents.firstObject as! CPDFSignatureConfigItem
  369. if configItem.key == kNAME_KEY && self.appearanceData.signatureConfig.isDrawKey {
  370. if self.nameButton.state == .off {
  371. contents.removeAllObjects()
  372. }
  373. if self.labelsButton.state == .off {
  374. self.appearanceData.signatureConfig.isDrawKey = false
  375. }
  376. }
  377. }
  378. var configItem: CPDFSignatureConfigItem!
  379. if self.nameButton.isEqual(sender) {
  380. if self.nameButton.state == .on {
  381. configItem = CPDFSignatureConfigItem.init()
  382. configItem.key = kNAME_KEY
  383. configItem.value = NSLocalizedString("<your common name here>", comment: "")
  384. contents.add(configItem as Any)
  385. } else {
  386. for item in contents {
  387. let exitItem: CPDFSignatureConfigItem = item as! CPDFSignatureConfigItem
  388. if exitItem.key == kNAME_KEY {
  389. configItem = item as? CPDFSignatureConfigItem
  390. break
  391. }
  392. }
  393. if configItem != nil {
  394. contents.remove(configItem as Any)
  395. }
  396. }
  397. } else if self.dateButton.isEqual(sender) {
  398. if self.dateButton.state == .on {
  399. configItem = CPDFSignatureConfigItem.init()
  400. configItem.key = DATE_KEY
  401. let dateFormatter = DateFormatter.init()
  402. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  403. configItem.value = dateFormatter.string(from: NSDate() as Date)
  404. contents.add(configItem as Any)
  405. } else {
  406. for item in contents {
  407. let exitItem: CPDFSignatureConfigItem = item as! CPDFSignatureConfigItem
  408. if exitItem.key == DATE_KEY {
  409. configItem = item as? CPDFSignatureConfigItem
  410. break
  411. }
  412. }
  413. if configItem != nil {
  414. contents.remove(configItem as Any)
  415. }
  416. }
  417. } else if self.reasonButton.isEqual(sender) {
  418. if (self.reasonButton.state == .on) {
  419. self.locationTopOffset.constant = 56.0;
  420. } else {
  421. self.locationTopOffset.constant = 12.0;
  422. }
  423. if self.reasonButton.state == .on {
  424. self.reasonPopUpButton.isHidden = false
  425. self.reasonLabel.isHidden = false
  426. self.reasonPopUpButton.selectItem(at: 0)
  427. configItem = CPDFSignatureConfigItem.init()
  428. configItem.key = REASON_KEY
  429. configItem.value = self.reasonPopUpButton.title
  430. if configItem.value == "" ||
  431. configItem.value == String(format: " %@", NSLocalizedString("none", comment: "")) {
  432. configItem.value = NSLocalizedString("<your signing reason here>", comment: "")
  433. }
  434. contents.add(configItem as Any)
  435. } else {
  436. self.reasonPopUpButton.isHidden = true
  437. self.reasonLabel.isHidden = true
  438. for item in contents {
  439. let exitItem: CPDFSignatureConfigItem = item as! CPDFSignatureConfigItem
  440. if exitItem.key == REASON_KEY {
  441. configItem = item as? CPDFSignatureConfigItem
  442. break
  443. }
  444. }
  445. if configItem != nil {
  446. contents.remove(configItem as Any)
  447. }
  448. }
  449. } else if self.locationButton.isEqual(sender) {
  450. if self.locationButton.state == .on {
  451. self.locationLabel.isHidden = false
  452. self.locationTextField.isHidden = false
  453. if (self.reasonButton.state == .on) {
  454. self.locationTopOffset.constant = 56.0;
  455. } else {
  456. self.locationTopOffset.constant = 12.0;
  457. }
  458. configItem = CPDFSignatureConfigItem.init()
  459. configItem.key = LOCATION_KEY
  460. if self.locationTextField.stringValue.isEmpty {
  461. configItem.value = NSLocalizedString("<your signing location here>", comment: "")
  462. } else {
  463. configItem.value = self.locationTextField.stringValue
  464. }
  465. contents.add(configItem as Any)
  466. } else {
  467. self.locationLabel.isHidden = true
  468. self.locationTextField.isHidden = true
  469. for item in contents {
  470. let exitItem: CPDFSignatureConfigItem = item as! CPDFSignatureConfigItem
  471. if exitItem.key == LOCATION_KEY {
  472. configItem = item as? CPDFSignatureConfigItem
  473. break
  474. }
  475. }
  476. if configItem != nil {
  477. contents.remove(configItem as Any)
  478. }
  479. }
  480. } else if self.distinguishedButton.isEqual(sender) {
  481. if self.distinguishedButton.state == .on {
  482. configItem = CPDFSignatureConfigItem.init()
  483. configItem.key = DN_KEY
  484. configItem.value = NSLocalizedString("<your distinguished name here>", comment: "")
  485. contents.add(configItem as Any)
  486. } else {
  487. for item in contents {
  488. let exitItem: CPDFSignatureConfigItem = item as! CPDFSignatureConfigItem
  489. if exitItem.key == DN_KEY {
  490. configItem = item as? CPDFSignatureConfigItem
  491. break
  492. }
  493. }
  494. if configItem != nil {
  495. contents.remove(configItem as Any)
  496. }
  497. }
  498. } else if self.versionButton.isEqual(sender) {
  499. if self.versionButton.state == .on {
  500. configItem = CPDFSignatureConfigItem.init()
  501. configItem.key = VERSION_KEY
  502. let infoDictionary = Bundle.main.infoDictionary!
  503. // CFShow(infoDictionary)
  504. let app_Version = infoDictionary["CFBundleShortVersionString"]
  505. configItem.value = app_Version as? String
  506. contents.add(configItem as Any)
  507. } else {
  508. for item in contents {
  509. let exitItem: CPDFSignatureConfigItem = item as! CPDFSignatureConfigItem
  510. if exitItem.key == VERSION_KEY {
  511. configItem = item as? CPDFSignatureConfigItem
  512. break
  513. }
  514. }
  515. if configItem != nil {
  516. contents.remove(configItem as Any)
  517. }
  518. }
  519. } else if self.logoButton.isEqual(sender) {
  520. if self.logoButton.state == .on {
  521. self.appearanceData.signatureConfig.isDrawLogo = true
  522. } else {
  523. self.appearanceData.signatureConfig.isDrawLogo = false
  524. }
  525. }else if self.labelsButton.isEqual(sender) {
  526. if self.labelsButton.state == .on {
  527. self.appearanceData.signatureConfig.isDrawKey = true
  528. } else {
  529. self.appearanceData.signatureConfig.isDrawKey = false
  530. }
  531. }
  532. if self.customType == .none && contents.count == 0 {
  533. configItem = CPDFSignatureConfigItem.init()
  534. configItem.key = kNAME_KEY
  535. configItem.value = NSLocalizedString("<your common name here>", comment: "")
  536. contents.add(configItem as Any)
  537. self.appearanceData.signatureConfig.isDrawKey = true
  538. }
  539. let sortContents:[Any] = KMDSignatureManager.default().sortContens((contents as! [Any]))
  540. self.appearanceData.signatureConfig.contents = (sortContents as! [CPDFSignatureConfigItem])
  541. self.reloadData()
  542. }
  543. @IBAction func buttonItemClick_Algin(_ sender: NSButton) {
  544. if (sender.tag == 0) {
  545. self.appearanceData.signatureConfig.isContentAlginLeft = false;
  546. self.leftButton.image = NSImage(named: "ImageNamePropertybarTextalignLeftSel")
  547. self.rightButton.image = NSImage(named: "ImageNamePropertybarTextalignRightNor")
  548. } else {
  549. self.appearanceData.signatureConfig.isContentAlginLeft = true;
  550. self.leftButton.image = NSImage(named: "ImageNamePropertybarTextalignLeftNor")
  551. self.rightButton.image = NSImage(named: "ImageNamePropertybarTextalignRightSel")
  552. }
  553. self.reloadData()
  554. }
  555. @IBAction func buttonItemClick_Reason(_ sender: Any) {
  556. let contents:[CPDFSignatureConfigItem] = self.appearanceData.signatureConfig.contents
  557. for object in contents {
  558. if object.key == REASON_KEY {
  559. if self.reasonPopUpButton.title.count > 2 {
  560. let fromStr = self.reasonPopUpButton.title.dropFirst(2)
  561. if self.reasonPopUpButton.title == String(format: " %@", NSLocalizedString("none", comment: "")) {
  562. object.value = NSLocalizedString("<your signing reason here>", comment: "")
  563. } else {
  564. object.value = String(fromStr)
  565. }
  566. }
  567. break
  568. }
  569. }
  570. self.reloadData()
  571. }
  572. @IBAction func buttonItemClick_Cancel(_ sender: Any) {
  573. guard let callBack = self.actionBlock else {
  574. return
  575. }
  576. callBack(self, .previousStep, DSignatureAppearanceData())
  577. }
  578. @IBAction func buttonItemClick_Continue(_ sender: Any) {
  579. let name = self.nameTextField.stringValue
  580. if (self.isCreat) {
  581. let at: [DSignatureAppearanceData] = DSignatureApperanceManager.manager.signatures as! [DSignatureAppearanceData]
  582. var names = NSMutableArray()
  583. for data in at {
  584. names.add(data.appearanceName as Any)
  585. }
  586. if names.contains(name) {
  587. let alert = NSAlert.init()
  588. alert.messageText = NSLocalizedString("The name already exists, please enter it again!", comment: "")
  589. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  590. alert.runModal()
  591. return
  592. }
  593. self.appearanceData.tag = DSignatureApperanceManager.manager.tagString()
  594. }
  595. if name.count > 0 {
  596. self.appearanceData.appearanceName = self.nameTextField.stringValue as NSString
  597. } else {
  598. self.appearanceData.appearanceName = DSignatureApperanceManager.manager.tagString()
  599. }
  600. guard let callBack = self.actionBlock else {
  601. return
  602. }
  603. callBack(self, .confirm, self.appearanceData)
  604. }
  605. //MARK: NSTextFieldDelegate
  606. func controlTextDidChange(_ obj: Notification) {
  607. if obj.object == nil {
  608. return
  609. }
  610. let textField = obj.object as! NSTextField
  611. if self.locationTextField.isEqual(textField) {
  612. let contents = self.appearanceData.signatureConfig.contents!
  613. for object in contents {
  614. if object.key == LOCATION_KEY {
  615. if self.locationTextField.stringValue.isEmpty == true {
  616. object.value = NSLocalizedString("<your signing location here>", comment: "")
  617. } else {
  618. object.value = self.locationTextField.stringValue
  619. }
  620. break
  621. }
  622. }
  623. self.reloadData()
  624. }
  625. }
  626. }