KMNAlertTipViewController.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. //
  2. // KMNAlertTipViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by 丁林圭 on 2024/12/18.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. typealias FormFieldHighlightCallback = () -> ()
  10. typealias EnterPasswordCallback = () -> ()
  11. typealias DigitalDetailsCallback = () -> ()
  12. typealias RedactApplyCallback = () -> ()
  13. typealias OCRApplyCallback = () -> ()
  14. typealias viewFrameHeightChange = (_ height:CGFloat) -> ()
  15. @objc public enum CPromptDignSignaturesState: NSInteger {
  16. case failure = 0
  17. case Unknown = 1
  18. case Success = 2
  19. }
  20. class KMNAlertTipViewController: KMNBaseViewController {
  21. //Form提示
  22. @IBOutlet var formComponentAlert: ComponentAlert!
  23. //Secure提示
  24. @IBOutlet var secureComponentAlert: ComponentAlert!
  25. //数字签名提示
  26. @IBOutlet var digitalComponentAlert: ComponentAlert!
  27. //密文提示
  28. @IBOutlet var redactComponentAlert: ComponentAlert!
  29. //OCR提示
  30. @IBOutlet var OCRComponentAlert: ComponentAlert!
  31. var type:CPromptDignSignaturesState = .failure
  32. var isShowFormAlert = true
  33. var isShowDigitalAlert = true
  34. var isShowSecureAlert = true
  35. var isShowRedactAlert = true
  36. var isShowOCRAlert = true
  37. weak var listView:CPDFListView?
  38. weak var subView:NSView?
  39. var formFieldHighlightCallback: FormFieldHighlightCallback?
  40. var enterPasswordCallback: EnterPasswordCallback?
  41. var digitalDetailsCallback: DigitalDetailsCallback?
  42. var redactApplyCallback: RedactApplyCallback?
  43. var OCRApplyCallback: OCRApplyCallback?
  44. var heightCallback: viewFrameHeightChange?
  45. override func viewDidLoad() {
  46. super.viewDidLoad()
  47. // Do view setup here.
  48. }
  49. override func initContentView() {
  50. super.initContentView()
  51. }
  52. override func updateUIThemeColor() {
  53. super.updateUIThemeColor()
  54. }
  55. override func updateUILanguage() {
  56. super.updateUILanguage()
  57. let highlightFormFiled = CPDFKitConfig.sharedInstance().enableFormFieldHighlight()
  58. var detailButtonString = ""
  59. if highlightFormFiled {
  60. detailButtonString = KMLocalizedString("Disable Highlight Effect")
  61. } else {
  62. detailButtonString = KMLocalizedString("Highlight Form Fields")
  63. }
  64. formComponentAlert.properties = ComponentAlertProperty(subTitle: KMLocalizedString("This document contains interactive form fields."),detailButtonString: detailButtonString)
  65. formComponentAlert.delegate = self
  66. secureComponentAlert.properties = ComponentAlertProperty(subTitle: KMLocalizedString("This document has a permission password."),detailButtonString: KMLocalizedString("Enter Password"))
  67. secureComponentAlert.delegate = self
  68. digitalComponentAlert.properties = ComponentAlertProperty(subTitle: KMLocalizedString("Signature is valid."),detailButtonString:nil)
  69. digitalComponentAlert.delegate = self
  70. redactComponentAlert.properties = ComponentAlertProperty(subTitle: KMLocalizedString("There are unapplied redactions in this file"),detailButtonString:KMLocalizedString("Apply"))
  71. redactComponentAlert.delegate = self
  72. OCRComponentAlert.properties = ComponentAlertProperty(subTitle: KMLocalizedString("This document contains scanned pages and requires OCR recognition before editing."),detailButtonString:KMLocalizedString("OCR"))
  73. OCRComponentAlert.delegate = self
  74. }
  75. public func reloadFormAlertUI() {
  76. if isShowFormAlert == true {
  77. var isContainsForm = false
  78. for i in 0..<(listView?.document?.pageCount ?? 0) {
  79. guard i < listView?.document?.pageCount ?? 0 else { continue }
  80. let page = listView?.document.page(at: i)
  81. if let annotations = page?.annotations, !annotations.isEmpty {
  82. for annotation in annotations {
  83. if annotation is CPDFWidgetAnnotation {
  84. isContainsForm = true
  85. break
  86. }
  87. }
  88. }
  89. if isContainsForm {
  90. break
  91. }
  92. }
  93. if isContainsForm {
  94. if formComponentAlert.superview == nil {
  95. formComponentAlert.frame = CGRect(x: 0, y: 0, width: subView?.frame.width ?? 0, height: 32.0)
  96. formComponentAlert.autoresizingMask = .width
  97. self.view.addSubview(formComponentAlert)
  98. }
  99. let highlightFormFiled = CPDFKitConfig.sharedInstance().enableFormFieldHighlight()
  100. var detailButtonString = ""
  101. if highlightFormFiled {
  102. detailButtonString = KMLocalizedString("Disable Highlight Effect")
  103. } else {
  104. detailButtonString = KMLocalizedString("Highlight Form Fields")
  105. }
  106. formComponentAlert.properties.detailButtonString = detailButtonString
  107. formComponentAlert.reloadData()
  108. } else {
  109. if formComponentAlert.superview != nil {
  110. formComponentAlert.removeFromSuperview()
  111. }
  112. }
  113. } else {
  114. if formComponentAlert.superview != nil {
  115. formComponentAlert.removeFromSuperview()
  116. }
  117. }
  118. }
  119. public func reloadSecureAlertUI() {
  120. if isShowSecureAlert == true {
  121. if listView?.document.permissionsStatus == .user {
  122. if secureComponentAlert.superview == nil {
  123. secureComponentAlert.frame = CGRect(x: 0, y: 0, width: subView?.frame.width ?? 0, height: 32.0)
  124. secureComponentAlert.autoresizingMask = .width
  125. self.view.addSubview(secureComponentAlert)
  126. }
  127. secureComponentAlert.reloadData()
  128. } else {
  129. if secureComponentAlert.superview != nil {
  130. secureComponentAlert.removeFromSuperview()
  131. }
  132. }
  133. } else {
  134. if secureComponentAlert.superview != nil {
  135. secureComponentAlert.removeFromSuperview()
  136. }
  137. }
  138. }
  139. public func reloadDigitalAlertUI() {
  140. if isShowDigitalAlert == true {
  141. let signatures:[CPDFSignature] = listView?.signatures as? [CPDFSignature] ?? []
  142. if signatures.count > 0 {
  143. if digitalComponentAlert.superview == nil {
  144. digitalComponentAlert.frame = CGRect(x: 0, y: 0, width: subView?.frame.width ?? 0, height: 32.0)
  145. digitalComponentAlert.autoresizingMask = .width
  146. self.view.addSubview(digitalComponentAlert)
  147. }
  148. var isSignVerified = false
  149. var isCertTrusted = false
  150. for item in signatures {
  151. let signature: CPDFSignature = item
  152. if signature.signers != nil {
  153. let signer = signature.signers.first
  154. if signer?.isCertTrusted == false {
  155. isCertTrusted = false
  156. break
  157. } else {
  158. isCertTrusted = true
  159. }
  160. }
  161. }
  162. for item in signatures {
  163. let signature: CPDFSignature = item
  164. if signature.signers != nil {
  165. let signer = signature.signers.first
  166. if signer?.isSignVerified == false {
  167. isSignVerified = false
  168. break
  169. } else {
  170. isSignVerified = true
  171. }
  172. }
  173. }
  174. if (isSignVerified && isCertTrusted) {
  175. self.type = .Success;
  176. } else if(isSignVerified && !isCertTrusted) {
  177. self.type = .Unknown;
  178. } else {
  179. self.type = .failure;
  180. }
  181. if (.Success == self.type) {
  182. digitalComponentAlert.properties.messageType = .success
  183. digitalComponentAlert.properties.iconImage = NSImage(named: "KMNImageNameSigntureVerifySuccess")
  184. digitalComponentAlert.properties.subTitle = KMLocalizedString("Signature is valid.")
  185. } else if(.Unknown == self.type) {
  186. digitalComponentAlert.properties.messageType = .warning
  187. digitalComponentAlert.properties.iconImage = NSImage(named: "KMNImageNameSigntureTrustedFailure")
  188. if(signatures.count > 1) {
  189. digitalComponentAlert.properties.subTitle = KMLocalizedString("At least one signature is invalid.")
  190. } else {
  191. digitalComponentAlert.properties.subTitle = KMLocalizedString("Signature is invalid")
  192. }
  193. } else {
  194. digitalComponentAlert.properties.messageType = .error
  195. digitalComponentAlert.properties.iconImage = NSImage(named: "KMNImageNameSigntureVerifyFailures")
  196. if(signatures.count > 1) {
  197. digitalComponentAlert.properties.subTitle = KMLocalizedString("At least one signature is invalid.")
  198. } else {
  199. digitalComponentAlert.properties.subTitle = KMLocalizedString("Signature is invalid")
  200. }
  201. }
  202. digitalComponentAlert.reloadData()
  203. } else {
  204. if digitalComponentAlert.superview != nil {
  205. digitalComponentAlert.removeFromSuperview()
  206. }
  207. }
  208. } else {
  209. if digitalComponentAlert.superview != nil {
  210. digitalComponentAlert.removeFromSuperview()
  211. }
  212. }
  213. }
  214. public func reloadRedactAlertUI() {
  215. if isShowRedactAlert == true {
  216. var isContainsRedact = false
  217. for i in 0..<(listView?.document?.pageCount ?? 0) {
  218. guard i < listView?.document?.pageCount ?? 0 else { continue }
  219. let page = listView?.document.page(at: i)
  220. if let annotations = page?.annotations, !annotations.isEmpty {
  221. for annotation in annotations {
  222. if annotation is CPDFRedactAnnotation {
  223. isContainsRedact = true
  224. break
  225. }
  226. }
  227. }
  228. if isContainsRedact {
  229. break
  230. }
  231. }
  232. if isContainsRedact {
  233. if redactComponentAlert.superview == nil {
  234. redactComponentAlert.frame = CGRect(x: 0, y: 0, width: subView?.frame.width ?? 0, height: 32.0)
  235. redactComponentAlert.autoresizingMask = .width
  236. self.view.addSubview(redactComponentAlert)
  237. }
  238. redactComponentAlert.reloadData()
  239. } else {
  240. if redactComponentAlert.superview != nil {
  241. redactComponentAlert.removeFromSuperview()
  242. }
  243. }
  244. } else {
  245. if redactComponentAlert.superview != nil {
  246. redactComponentAlert.removeFromSuperview()
  247. }
  248. }
  249. }
  250. public func reloadOCRAlertUI() {
  251. if isShowOCRAlert == true && self.listView?.toolMode == .CEditPDFToolMode {
  252. var isContainsImagePage = false
  253. for i in 0..<(listView?.document?.pageCount ?? 0) {
  254. guard i < listView?.document?.pageCount ?? 0 else { continue }
  255. let page = listView?.document.page(at: i)
  256. if (page?.isImagePage() == true) {
  257. isContainsImagePage = true
  258. break
  259. }
  260. }
  261. if isContainsImagePage {
  262. if OCRComponentAlert.superview == nil {
  263. OCRComponentAlert.frame = CGRect(x: 0, y: 0, width: subView?.frame.width ?? 0, height: 40.0)
  264. OCRComponentAlert.autoresizingMask = .width
  265. self.view.addSubview(OCRComponentAlert)
  266. }
  267. OCRComponentAlert.reloadData()
  268. } else {
  269. if OCRComponentAlert.superview != nil {
  270. OCRComponentAlert.removeFromSuperview()
  271. }
  272. }
  273. } else {
  274. if OCRComponentAlert.superview != nil {
  275. OCRComponentAlert.removeFromSuperview()
  276. }
  277. }
  278. }
  279. public func reloadAlertUIFrame() {
  280. var alertHeight = 0.0
  281. if digitalComponentAlert.superview != nil {
  282. digitalComponentAlert.frame = CGRect(x: 0, y: alertHeight, width: subView?.frame.width ?? 0, height: 32.0)
  283. alertHeight += 32.0
  284. }
  285. if formComponentAlert.superview != nil {
  286. if alertHeight > 0 {
  287. alertHeight += 1
  288. }
  289. formComponentAlert.frame = CGRect(x: 0, y: alertHeight, width: subView?.frame.width ?? 0, height: 32.0)
  290. alertHeight += 32.0
  291. }
  292. if secureComponentAlert.superview != nil {
  293. if alertHeight > 0 {
  294. alertHeight += 1
  295. }
  296. secureComponentAlert.frame = CGRect(x: 0, y: alertHeight, width: subView?.frame.width ?? 0, height: 32.0)
  297. alertHeight += 32.0
  298. }
  299. if redactComponentAlert.superview != nil {
  300. if alertHeight > 0 {
  301. alertHeight += 1
  302. }
  303. redactComponentAlert.frame = CGRect(x: 0, y: alertHeight, width: subView?.frame.width ?? 0, height: 32.0)
  304. alertHeight += 32.0
  305. }
  306. if OCRComponentAlert.superview != nil {
  307. if alertHeight > 0 {
  308. alertHeight += 1
  309. }
  310. OCRComponentAlert.frame = CGRect(x: 0, y: alertHeight, width: subView?.frame.width ?? 0, height: 40.0)
  311. alertHeight += 40.0
  312. }
  313. if(alertHeight > 0) {
  314. if(listView?.toolMode == .CRedactToolMode) {
  315. if self.view.superview != nil {
  316. self.view.removeFromSuperview()
  317. heightCallback?(0.0)
  318. }
  319. } else {
  320. var rect = self.view.frame
  321. rect.size.height = alertHeight
  322. rect.size.width = subView?.frame.size.width ?? 0
  323. rect.origin.y = (subView?.frame.size.height ?? 0) - alertHeight
  324. self.view.frame = rect
  325. if self.view.superview == nil {
  326. subView?.addSubview(self.view)
  327. self.view.autoresizingMask = [.width, .minYMargin]
  328. }
  329. heightCallback?(alertHeight)
  330. }
  331. } else {
  332. if self.view.superview != nil {
  333. self.view.removeFromSuperview()
  334. heightCallback?(0)
  335. }
  336. }
  337. }
  338. func showInView(listView: CPDFListView?,subView:NSView) {
  339. self.listView = listView
  340. self.subView = subView
  341. self.view.frame = CGRect(x: 0, y: 0, width: subView.frame.size.width , height: 0)
  342. self.view.autoresizingMask = [.width, .minYMargin]
  343. subView.addSubview(self.view)
  344. reloadSecureAlertUI()
  345. reloadFormAlertUI()
  346. reloadDigitalAlertUI()
  347. reloadRedactAlertUI()
  348. reloadFormAlertUI()
  349. reloadOCRAlertUI()
  350. }
  351. }
  352. //MARK: - ComponentAlertDelegate
  353. extension KMNAlertTipViewController: ComponentAlertDelegate {
  354. func componentAlertDidCloseButtonClick(inputView: ComponentAlert) {
  355. if inputView == formComponentAlert {
  356. isShowFormAlert = false
  357. reloadFormAlertUI()
  358. } else if inputView == secureComponentAlert {
  359. isShowSecureAlert = false
  360. reloadSecureAlertUI()
  361. } else if inputView == digitalComponentAlert {
  362. isShowDigitalAlert = false
  363. reloadDigitalAlertUI()
  364. } else if inputView == redactComponentAlert {
  365. isShowRedactAlert = false
  366. reloadRedactAlertUI()
  367. } else if inputView == OCRComponentAlert {
  368. isShowOCRAlert = false
  369. reloadOCRAlertUI()
  370. }
  371. reloadAlertUIFrame()
  372. }
  373. func componentAlertDidButtonClick(inputView: ComponentAlert, buttonIndex: Int) {
  374. if buttonIndex == 1 {
  375. if inputView == formComponentAlert {
  376. formFieldHighlightCallback?()
  377. let highlightFormFiled = CPDFKitConfig.sharedInstance().enableFormFieldHighlight()
  378. var detailButtonString = ""
  379. if highlightFormFiled {
  380. detailButtonString = KMLocalizedString("Disable Highlight Effect")
  381. } else {
  382. detailButtonString = KMLocalizedString("Highlight Form Fields")
  383. }
  384. formComponentAlert.properties.detailButtonString = detailButtonString
  385. formComponentAlert.reloadData()
  386. } else if inputView == secureComponentAlert {
  387. enterPasswordCallback?()
  388. } else if inputView == digitalComponentAlert {
  389. digitalDetailsCallback?()
  390. } else if inputView == redactComponentAlert {
  391. redactApplyCallback?()
  392. } else if inputView == OCRComponentAlert {
  393. OCRApplyCallback?()
  394. }
  395. }
  396. }
  397. }