CDSignatureCertificateCustomViewController.swift 31 KB

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