123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- //
- // DSignatureConfigWindowController.swift
- // PDF Reader Pro Edition
- //
- // Created by Niehaoyu on 2023/9/27.
- //
- import Cocoa
- typealias configWActionInfoBlock = (_ controller: DSignatureConfigWindowController, _ type: DSignatureActionType) -> Void
- @objcMembers class DSignatureConfigWindowController: NSWindowController {
-
- @IBOutlet weak var contendBox: NSBox!
-
- var fileListVC: DSignatureFileListViewController = DSignatureFileListViewController.init()
- var certifyDetailVC: DSignatureCertifyDetailViewController!
- var createVC: DSignatureCreateViewController = DSignatureCreateViewController.init()
- var fromFileVC: DSignatureFromFileViewController = DSignatureFromFileViewController.init()
- var saveTypeVC: DSignatureSaveTypeViewController = DSignatureSaveTypeViewController.init()
- var createInfoVC: DSignatureCreateInfoViewController = DSignatureCreateInfoViewController.init()
- var saveFolderTypeVC: DSignatureSaveFolderViewController = DSignatureSaveFolderViewController.init()
- var certificateCustomVC: CDSignatureCertificateCustomViewController = CDSignatureCertificateCustomViewController.init()
-
- @objc var _viewType: DSignatureViewType = .none
-
- @objc var appearanceWidget: CPDFSignatureWidgetAnnotation!
- @objc var isCreatDS: Bool = false
-
- private var cert: KMDSignatureModel!
-
- private var certDictionary: NSDictionary!
- private var certUsage: CPDFCertUsage!
-
- private var saveType: CDSignatureSaveType = .none
-
- var actionBlock: ((DSignatureConfigWindowController, DSignatureActionType)->Void)?
- var complentionHandle: ((_ isSign: Bool, _ dic: NSDictionary, _ config: CPDFSignatureConfig, _ isLock: Bool) ->Void)?
-
- override func windowDidLoad() {
- super.windowDidLoad()
- // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
-
- self.initFileListVC()
-
- self.initCreateVC()
- self.initFromFileVC()
- self.initSaveTypeVC()
- self.initCreateInfoViewVC()
- self.initSaveFolderVC()
- self.initCertificateCustomVC()
-
- if KMDSignatureManager.default().signatures.count == 0 {
- KMDSignatureManager.default().loadAllKeyChainCertificates()
- }
-
- }
-
- //MARK: Setter
- @objc var viewType: DSignatureViewType {
- set {
- _viewType = newValue;
-
- switch _viewType {
-
- case .none:
-
- break
- case .fileList:
- self.setContendView(self.fileListVC.view)
- self.fileListVC.isFileList = false
-
- break
- case .certifyDetail:
-
- self.initCertifyDetailVC()
- self.setContendView(self.certifyDetailVC.view)
-
- break
- case .createNew:
- self.createVC.isCreatDS = self.isCreatDS
- self.setContendView(self.createVC.view)
-
- break
- case .createFromFile:
- self.setContendView(self.fromFileVC.view)
-
- break
- case .saveType:
- self.setContendView(self.saveTypeVC.view)
-
- break
- case .createInfo:
- self.setContendView(self.createInfoVC.view)
-
- break
- case .saveFolderType:
- self.setContendView(self.saveFolderTypeVC.view)
-
- break
- case .signatureDetail:
- break
-
- case .edit:
- self.certificateCustomVC.signatureWidget = self.appearanceWidget
- self.certificateCustomVC.cert = self.cert
- self.setContendView(self.certificateCustomVC.view)
- break
-
- case .fromFileList:
- self.fileListVC.isFileList = true
-
- self.setContendView(self.fileListVC.view)
- break
-
- }
- }
-
- get {
- return _viewType;
- }
- }
-
-
-
- func setContendView(_ view: NSView) {
-
-
- var frame = self.window?.frame
-
- let originX = (frame?.origin.x)!
- let originY = (frame?.origin.y)!
- let height: CGFloat = (frame?.size.height)!
- let width: CGFloat = (frame?.size.width)!
-
- var viewHeight: CGFloat = 500
- if self.viewType == .fileList ||
- self.viewType == .fromFileList {
- viewHeight = 499 + 22
- } else if self.viewType == .createNew {
- viewHeight = 500 + 22
- } else if self.viewType == .certifyDetail {
- viewHeight = 450 + 22
- } else if self.viewType == .createFromFile {
- viewHeight = 410 + 22
- } else if self.viewType == .edit {
- viewHeight = 530 + 22
- } else if self.viewType == .saveType {
- viewHeight = 430 + 22
- } else if self.viewType == .saveFolderType {
- viewHeight = 450 + 22
- } else {
- frame?.size.height = view.frame.size.height + 22
- }
-
-
- frame?.size.height = viewHeight
- frame?.origin.y = originY + (height - viewHeight)
-
- self.window?.setFrame(frame!, display: true)
- self.contendBox.contentView = view
- }
-
- //MARK: DSignatureFileListViewController
- func initFileListVC () {
- weak var weakSelf = self
- self.fileListVC.actionBlock = { fileListVC, type in
- if type == .cancel {
- if fileListVC.isFileList {
- weakSelf!.viewType = .createFromFile
- } else {
- guard let callback = weakSelf!.actionBlock else {
- return
- }
- callback(weakSelf!, type)
- }
- } else if type == .confirm {
- if fileListVC.isFileList {
- weakSelf!.viewType = .fileList
- } else {
- weakSelf!.cert = fileListVC.currentModel
-
- weakSelf!.viewType = .certifyDetail
- }
- } else if type == .createNewDsign {
- weakSelf!.viewType = .createNew
- }
- }
-
- self.fileListVC.viewCertDetailBlock = {fileListVC, certificate, isDigitalFile in
- DispatchQueue.main.async {
- let signatureDetailVC = DSignatureDetailsViewController.init()
- signatureDetailVC.isDigitalFile = isDigitalFile
- signatureDetailVC.certificates = [certificate]
-
- fileListVC.presentAsModalWindow(signatureDetailVC)
- }
- }
-
- }
-
- //MARK: DSignatureCertifyDetailViewController
- func initCertifyDetailVC () {
- if self.certifyDetailVC != nil {
- self.certifyDetailVC.view.removeFromSuperview()
- self.certifyDetailVC = nil
- }
- self.certifyDetailVC = DSignatureCertifyDetailViewController.init()
- self.certifyDetailVC.cert = self.cert
- self.certifyDetailVC.signatureWidget = self.appearanceWidget
-
- self.certifyDetailVC.actionBlock = {detailVC, actionType, appearanceData, config, password, isLock in
- if actionType == .cancel {
- self.closeAction()
- } else if actionType == .previousStep {
- self.viewType = .fileList
- } else if actionType == .confirm {
- var returnAcType = actionType
-
- let dic = NSMutableDictionary()
- if self.cert.isFormKeyChain {
- var save: Bool = false
- let folderPath = kDSignatureFolderPath
-
- if FileManager.default.fileExists(atPath: folderPath) == false {
- try?FileManager.default.createDirectory(atPath: kDSignatureFolderPath, withIntermediateDirectories: false)
- }
- let filePath = kDSignatureFolderPath.appending("/CDSignatures.p12")
- save = KMDSignatureManager.exportKeyChain(withP12FilePath: filePath, signatureModel: self.cert, passWord: "")
- if save {
- dic.setValue(filePath, forKey: SAVEFILEPATH_KEY)
- dic.setValue("", forKey: PASSWORD_KEY)
- } else {
- return
- }
- if save == false {
- returnAcType = .none
- }
- } else {
- dic.setValue(self.cert.filePath, forKey: SAVEFILEPATH_KEY)
- dic.setValue(password, forKey: PASSWORD_KEY)
- }
- guard let complentionCallBack = self.complentionHandle else {
- return
- }
- complentionCallBack(true, dic, config, isLock)
-
- guard let callback = self.actionBlock else {
- return
- }
- callback(self, returnAcType)
-
- } else if actionType == .create {
- self.certificateCustomVC.isCreat = true
- self.certificateCustomVC.appearanceData = nil
- self.viewType = .edit
- } else if actionType == .edit {
- self.certificateCustomVC.isCreat = false
- self.certificateCustomVC.appearanceData = appearanceData
- self.viewType = .edit
- } else if actionType == .delete {
-
- } else if actionType == .viewDetail {
-
- }
-
- }
-
- }
-
- //MARK: DSignatureCreateViewController
- func initCreateVC () {
- self.createVC.actionBlock = { createVC, actionType, createType in
- if actionType == .previousStep {
- self.viewType = .fileList
- } else if actionType == .confirm {
- if createType == .signCreate {
- self.viewType = .saveType
- } else if createType == .fromFile {
- self.viewType = .createFromFile
- }
- } else if actionType == .cancel {
- guard let callback = self.actionBlock else {
- return
- }
- callback(self, actionType)
- }
-
- }
- }
-
- //MARK: DSignatureFromFileViewController
- func initFromFileVC () {
- self.fromFileVC.actionBlock = {fromFileVC, action, cer in
- if action == .cancel {
- self.closeAction()
- } else if action == .previousStep {
- self.viewType = .createNew
- } else if action == .confirm {
- self.viewType = .fromFileList
- } else if action == .createNewDsign {
- self.viewType = .saveType
- }
- }
- }
-
- //MARK: DSignatureSaveTypeViewController
- func initSaveTypeVC () {
- self.saveTypeVC.actionBlock = { saveTypeVC, actionType, saveType in
- self.saveType = saveType
-
- if actionType == .cancel {
- self.closeAction()
- } else if actionType == .previousStep {
- self.viewType = .createNew
- } else if actionType == .confirm {
- self.viewType = .createInfo
- }
- }
- }
-
- //MARK: DSignatureCreateInfoViewController
- func initCreateInfoViewVC () {
- self.createInfoVC.actionBlock = { createVC, action, cer, certUsage in
- self.certDictionary = cer
- if certUsage == 0 {
- self.certUsage = .digSig
- } else if certUsage == 1 {
- self.certUsage = .dataEnc
- } else if certUsage == 2 {
- self.certUsage = .all
- }
-
- if action == .previousStep {
- self.viewType = .saveType
- } else if action == .confirm {
- if self.saveType == .folder {
- self.viewType = .saveFolderType
- } else {
- let folderPath = kDSignatureFolderPath
- var save: Bool = true
- if FileManager.default.fileExists(atPath: folderPath) == false {
- try?FileManager.default.createDirectory(atPath: folderPath, withIntermediateDirectories: false)
- save = FileManager.default.fileExists(atPath: folderPath)
- }
- let filePath = kDSignatureFolderPath.appending("/KMDSignatures.p12")
- save = CPDFSignature.generatePKCS12Cert(withInfo: cer as? [AnyHashable : Any], password: "1", toPath: filePath, certUsage: self.certUsage)
-
- let success = KMDSignatureManager.default().importKeyChain(withP12FilePath: filePath, passWord: "1")
- if save && success && FileManager.default.fileExists(atPath: filePath) {
- self.viewType = .fileList
- } else {
- let alert = NSAlert.init()
- alert.messageText = NSLocalizedString("Save failed!", comment: "")
- alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
- alert.runModal()
- }
- }
- } else if action == .cancel {
- self.closeAction()
- }
- }
- }
-
- //MARK: DSignatureSaveFolderViewController
- func initSaveFolderVC () {
- self.saveFolderTypeVC.actionBlock = { saveFolderVC, actionType, infoDic in
- if actionType == .cancel {
- self.closeAction()
- } else if actionType == .confirm {
- var save: Bool = CPDFSignature.generatePKCS12Cert(withInfo: self.certDictionary as? [AnyHashable : Any], password: infoDic[PASSWORD_KEY] as? String, toPath: infoDic[SAVEFILEPATH_KEY] as? String, certUsage: self.certUsage)
- KMDSignatureManager.default().moveP12DigitalFile(withFilePath: infoDic[SAVEFILEPATH_KEY] as? String, password: infoDic[PASSWORD_KEY] as? String)
- if save {
- self.viewType = .fileList
- } else {
- let alert = NSAlert.init()
- alert.messageText = NSLocalizedString("Save failed!", comment: "")
- alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
- alert.runModal()
- }
-
-
- } else if actionType == .previousStep {
- self.viewType = .createInfo
- }
- }
- }
-
- //MARK: CDSignatureCertificateCustomViewController
- func initCertificateCustomVC () {
- self.certificateCustomVC.actionBlock = {customVC, type, appearanceData in
- if type == .cancel {
- self.closeAction()
- } else if type == .previousStep {
- self.viewType = .certifyDetail
- } else if type == .confirm {
-
- _ = DSignatureApperanceManager.manager.addDSignatureAppearanceData(watermark: appearanceData)
- DSignatureApperanceManager.manager.saveDSignatureAppearanceData(watermark: appearanceData)
- self.viewType = .certifyDetail
-
- self.certifyDetailVC.cert = self.cert
- self.certifyDetailVC.appearanceData = appearanceData
- self.certifyDetailVC.signatureWidget = self.appearanceWidget
- self.certifyDetailVC.reloadSignatures()
- }
- }
- }
-
- //
- func closeAction() {
- guard let callback = self.actionBlock else {
- return
- }
- callback(self, .cancel)
- }
-
-
- }
|