// // DSignatureCertifyDetailViewController.swift // PDF Reader Pro Edition // // Created by Niehaoyu on 2023/9/28. // import Cocoa class DSignatureCertifyDetailViewController: NSViewController, NSTextFieldDelegate { @IBOutlet weak var contendView: NSView! @IBOutlet weak var titleLabel: NSTextField! @IBOutlet weak var closeButton: NSButton! @IBOutlet weak var appearanceLabel: NSTextField! @IBOutlet weak var appearanceButton: NSPopUpButton! @IBOutlet weak var createButton: KMButton! @IBOutlet weak var editButton: KMButton! @IBOutlet weak var deleteButton: KMButton! @IBOutlet weak var lockDocButton: NSButton! @IBOutlet weak var errorLabel: NSTextField! @IBOutlet weak var drawBox: NSBox! @IBOutlet weak var drawImageView: NSImageView! @IBOutlet weak var infoButton: NSButton! @IBOutlet weak var passwordBox: NSBox! @IBOutlet weak var passwordLabel: NSTextField! @IBOutlet weak var passwordTextField: NSSecureTextField! @IBOutlet weak var cancelButton: NSButton! @IBOutlet weak var continueButton: NSButton! //证书+外观配置信息 var _cert: KMDSignatureModel! var _appearanceData: DSignatureAppearanceData! var config: CPDFSignatureConfig! var _signatureWidget: CPDFSignatureWidgetAnnotation! var isLock: Bool = false var actionBlock: ((_ detailVC: DSignatureCertifyDetailViewController, _ actionType: DSignatureActionType, _ appearanceData: DSignatureAppearanceData, _ config: CPDFSignatureConfig, _ password: String, _ isLock: Bool)->Void)? override func viewDidLoad() { super.viewDidLoad() // Do view setup here. self.passwordTextField.delegate = self; self.localizedLanguage() if self.appearanceData == nil { self.appearanceData = DSignatureApperanceManager.manager.getLastDSignatureAppearanceData() } self.reloadSignatures() } //MARK: Setter var cert: KMDSignatureModel? { set { _cert = newValue } get { return _cert } } var appearanceData: DSignatureAppearanceData? { set { _appearanceData = newValue if _appearanceData.appearanceName.length > 0 && _appearanceData.appearanceName as String == NSLocalizedString("Standard Text", comment: "") { self.deleteButton.isEnabled = false self.editButton.isEnabled = false } else { self.deleteButton.isEnabled = true self.editButton.isEnabled = true } } get { return _appearanceData } } var signatureWidget: CPDFSignatureWidgetAnnotation? { set { _signatureWidget = newValue } get { return _signatureWidget } } //MARK: Method func reloadSignatures () { self.appearanceButton.removeAllItems() let datas = DSignatureApperanceManager.manager.loadDSignatureAppearanceDatas() var dex = 0; let titles = NSMutableArray.init() if datas.count > 0 { for i in 0...datas.count-1 { let data = datas[i] as! DSignatureAppearanceData if data.appearanceName.isEqual(to: self.appearanceData?.appearanceName) { dex = i } titles.add(data.appearanceName as Any) } } self.errorLabel.isHidden = true self.appearanceButton.addItems(withTitles: titles as! [String]) self.appearanceButton.selectItem(at: dex) self.appearanceData = datas[dex] as? DSignatureAppearanceData if self.appearanceData?.isInputDSignatureText == false { self.appearanceData?.signatureConfig.text = _cert.name; } if self.cert?.isFormKeyChain == true { self.passwordBox.isHidden = true; } else { self.passwordBox.isHidden = false; } self.reloadData() } func reloadData() { self.config = CPDFSignatureConfig.init() self.config.isDrawKey = self.appearanceData!.signatureConfig.isDrawKey self.config.isDrawLogo = self.appearanceData!.signatureConfig.isDrawLogo self.config.isContentAlginLeft = self.appearanceData!.signatureConfig.isContentAlginLeft var textStr = "" if self.appearanceData?.signatureConfig.text != nil { if (self.appearanceData?.signatureConfig.text.count)! > 0 { textStr = (self.appearanceData?.signatureConfig.text)! } } if (textStr.count > 0) { self.config.text = self.cert?.name } else if (self.appearanceData!.signatureConfig.image != nil) { self.config.image = self.appearanceData!.signatureConfig.image } else { self.config.text = nil self.config.image = nil self.config.isDrawOnlyContent = true } self.config.logo = self.appearanceData!.signatureConfig.logo; let contens = NSMutableArray.init() for configItem in self.appearanceData!.signatureConfig.contents { let cerName = _cert.name ?? "" if configItem.key == NAME_KEY && !cerName.isEmpty { let tConfigItem = CPDFSignatureConfigItem.init() tConfigItem.key = configItem.key tConfigItem.value = self.cert?.name contens.add(tConfigItem) } else if configItem.key == DN_KEY && !_cert.dn.isEmpty { let tConfigItem = CPDFSignatureConfigItem.init() tConfigItem.key = configItem.key tConfigItem.value = _cert.dn contens.add(tConfigItem) } else if configItem.key == REASON_KEY { let tConfigItem = CPDFSignatureConfigItem.init() tConfigItem.key = configItem.key tConfigItem.value = configItem.value contens.add(tConfigItem) } else if configItem.key == LOCATION_KEY { let tConfigItem = CPDFSignatureConfigItem.init() tConfigItem.key = configItem.key tConfigItem.value = configItem.value contens.add(tConfigItem) } else if configItem.key == NSLocalizedString(DATE_KEY, comment: "") { let tConfigItem = CPDFSignatureConfigItem.init() tConfigItem.key = configItem.key let dateFormatter = DateFormatter.init() dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" tConfigItem.value = dateFormatter.string(from: NSDate() as Date) contens.add(tConfigItem) } else if configItem.key == VERSION_KEY && _cert.name.isEmpty == false { let tConfigItem = CPDFSignatureConfigItem.init() tConfigItem.key = configItem.key let infoDictionary = NSMutableDictionary(dictionary: Bundle.main.infoDictionary!) CFShow(infoDictionary) let app_Version = infoDictionary.object(forKey: "CFBundleShortVersionString") tConfigItem.value = app_Version as? String contens.add(tConfigItem) } } self.config.contents = contens as? [CPDFSignatureConfigItem] self.signatureWidget?.signAppearanceConfig(self.config) self.drawImageView.image = self.signatureWidget?.contentImage() } func updateTextFiedState () { self.passwordTextField.layer?.borderColor = NSColor.systemRed.cgColor; } func localizedLanguage () { self.contendView.wantsLayer = true self.contendView.layer?.backgroundColor = NSColor.clear.cgColor if self.cert?.name != nil { self.titleLabel.stringValue = NSLocalizedString("Sign as", comment: "") + " " + (self.cert?.name)! } else { self.titleLabel.stringValue = NSLocalizedString("Sign as", comment: "") } self.appearanceLabel.stringValue = NSLocalizedString("Appearance", comment: "") + ":" self.passwordTextField.placeholderString = NSLocalizedString("Enter the digital signature ID card PIN or password", comment: "") self.passwordLabel.stringValue = NSLocalizedString("Password", comment: "") + ":" self.editButton.toolTip = NSLocalizedString("Edit", comment: "") self.createButton.toolTip = NSLocalizedString("Create", comment: "") self.deleteButton.toolTip = NSLocalizedString("Delete", comment: "") self.continueButton.title = NSLocalizedString("Continue", comment: ""); self.cancelButton.title = NSLocalizedString("Previous Step",comment: ""); self.infoButton.title = NSLocalizedString("View Certificate Details",comment: ""); self.lockDocButton.title = NSLocalizedString("Lock document after signing", comment: ""); let txtColor = KMAppearance.Interactive.a0Color() self.infoButton.setTitleColor(txtColor) self.lockDocButton.isHidden = true self.createButton.wantsLayer = true self.editButton.wantsLayer = true self.deleteButton.wantsLayer = true self.createButton.layer?.borderWidth = 0.5 self.editButton.layer?.borderWidth = 0.5 self.deleteButton.layer?.borderWidth = 0.5 self.createButton.layer?.cornerRadius = 5 self.editButton.layer?.cornerRadius = 5 self.deleteButton.layer?.cornerRadius = 5 self.createButton.layer?.borderColor = NSColor(red: 165.0/255.0, green: 167.0/255.0, blue: 179.0/255.0, alpha: 1).cgColor self.editButton.layer?.borderColor = NSColor(red: 165.0/255.0, green: 167.0/255.0, blue: 179.0/255.0, alpha: 1).cgColor self.deleteButton.layer?.borderColor = NSColor(red: 165.0/255.0, green: 167.0/255.0, blue: 179.0/255.0, alpha: 1).cgColor let creatFileLayer = CALayer() creatFileLayer.frame = NSMakeRect(0, 0, NSWidth(self.createButton.bounds), NSHeight(self.createButton.bounds)); self.createButton.layer?.addSublayer(creatFileLayer) let editFileLayer = CALayer() editFileLayer.frame = NSMakeRect(0, 0, NSWidth(self.editButton.bounds), NSHeight(self.editButton.bounds)); self.editButton.layer?.addSublayer(editFileLayer) let deleteFileLayer = CALayer() deleteFileLayer.frame = NSMakeRect(0, 0, NSWidth(self.deleteButton.bounds), NSHeight(self.deleteButton.bounds)); self.deleteButton.layer?.addSublayer(deleteFileLayer) self.titleLabel.textColor = NSColor.labelColor self.appearanceLabel.textColor = NSColor.labelColor self.passwordLabel.textColor = NSColor.labelColor self.passwordTextField.textColor = NSColor.labelColor self.drawBox.borderColor = NSColor(red: 165.0/255.0, green: 167.0/255.0, blue: 179.0/255.0, alpha: 1) self.passwordTextField.wantsLayer = true self.passwordTextField.layer?.borderWidth = 1.0 self.passwordTextField.layer?.cornerRadius = 5.0 self.passwordTextField.placeholderString = NSLocalizedString("Enter the Digital ID PIN or Password...", comment: "") self.passwordTextField.updateState(false) self.errorLabel.stringValue = NSLocalizedString("Password is incorrect. Please re-enter the password.", comment: "") } //MARK: IBAction @IBAction func buttonItemClick_Appearence(_ sender: NSPopUpButton) { self.appearanceButton.title = sender.title; let datas = DSignatureApperanceManager.manager.loadDSignatureAppearanceDatas() self.appearanceData = datas.object(at: self.appearanceButton.indexOfSelectedItem) as? DSignatureAppearanceData if self.appearanceData?.isInputDSignatureText == false { self.appearanceData?.signatureConfig.text = _cert.name } self.reloadData() } @IBAction func buttonItemClick_Creat(_ sender: Any) { guard let callBack = self.actionBlock else { return } callBack(self, .create, self.appearanceData!, self.config, "", self.isLock) } @IBAction func buttonItemClick_Edit(_ sender: Any) { guard let callBack = self.actionBlock else { return } callBack(self, .edit, self.appearanceData!, self.config, "", self.isLock) } @IBAction func buttonItemClick_Delete(_ sender: Any) { _ = DSignatureApperanceManager.manager.removeDSignatureAppearanceData(watermark: self.appearanceData!) let datas = DSignatureApperanceManager.manager.loadDSignatureAppearanceDatas() as! [DSignatureAppearanceData] let titles = NSMutableArray() for i in 0.. 0 { titles.add(data.appearanceName as Any) } } self.appearanceButton.removeAllItems() self.appearanceButton.addItems(withTitles: titles as! [String]) self.appearanceButton.selectItem(at: 0) self.appearanceData = datas.first self.reloadData() } @IBAction func lockDocumentButtonAction(_ sender: NSButton) { self.isLock = true } @IBAction func buttonItemViewDetail(_ sender: NSButton) { var filePath: String = "" var save: Bool = true var password: String = "" if self.cert!.isFormKeyChain == true { let folderPath = kDSignatureFolderPath if FileManager.default.fileExists(atPath: folderPath) == false { let success: ()? = try?FileManager.default.createDirectory(atPath: folderPath, withIntermediateDirectories: false) if success == nil { save = false } } filePath = kDSignatureFolderPath.appending("/KMDSignatures.p12") as String save = KMDSignatureManager.exportKeyChain(withP12FilePath: filePath as String?, signatureModel: self.cert, passWord: password as String?) self.cert?.filePath = filePath as String } else { password = self.passwordTextField.stringValue filePath = (self.cert?.filePath)! } if save { let certificate = CPDFSignatureCertificate(pkcs12Path: filePath, password: password) if certificate != nil { let signatureDetailVC = DSignatureDetailsViewController.init() signatureDetailVC.isDigitalFile = ((self.cert?.isFormKeyChain) == nil) signatureDetailVC.certificates = [certificate!] self.presentAsSheet(signatureDetailVC) return } } let alert = NSAlert.init() alert.messageText = NSLocalizedString("Failed to get certificate details!", comment: "") alert.addButton(withTitle: NSLocalizedString("OK", comment: "")) alert.runModal() } @IBAction func closeAction(_ sender: Any) { guard let callBack = self.actionBlock else { return } callBack(self, .cancel, self.appearanceData!, self.config, "", self.isLock) } @IBAction func cancelAction(_ sender: Any) { guard let callBack = self.actionBlock else { return } callBack(self, .previousStep, self.appearanceData!, self.config, "", self.isLock) } @IBAction func continueAction(_ sender: Any) { guard let callBack = self.actionBlock else { return } if self.cert?.isFormKeyChain == true { DSignatureApperanceManager.manager.saveDSignatureAppearanceData(watermark: self.appearanceData!) callBack(self, .confirm, self.appearanceData!, self.config, self.passwordTextField.stringValue, self.isLock) } else { let certificate = CPDFSignatureCertificate(pkcs12Path: self.cert?.filePath, password: self.passwordTextField.stringValue) if certificate == nil { self.errorLabel.isHidden = false self.passwordTextField.updateState(true) } else { DSignatureApperanceManager.manager.saveDSignatureAppearanceData(watermark: self.appearanceData!) callBack(self, .confirm, self.appearanceData!, self.config, self.passwordTextField.stringValue, self.isLock) } } } //MARK: NSTextFieldDelegate func controlTextDidChange(_ obj: Notification) { if obj.object == nil { return } let textField = obj.object as! NSTextField if self.passwordTextField.isEqual(textField) { self.errorLabel.isHidden = true } self.passwordTextField.updateState(false) } }