KMCompareWindowController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //
  2. // KMCompareWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/11/14.
  6. //
  7. import Cocoa
  8. typealias KMCompareWindowControllerContentComplete = (_ controller: KMCompareWindowController, _ pdfCompareContent: CPDFCompareContent, _ results: [CPDFCompareResults] ,_ oldDocument: CPDFDocument, _ document: CPDFDocument) -> Void
  9. typealias KMCompareWindowControllerCoveringComplete = (_ controller: KMCompareWindowController, _ document: CPDFDocument) -> Void
  10. class KMCompareWindowController: KMNBaseWindowController {
  11. @IBOutlet weak var compareView: KMCompareView!
  12. var cancelAction: KMCommonBlock?
  13. var pdfCompareContent: CPDFCompareContent?
  14. var maskView: KMBookletMaskView?
  15. var progressController: SKProgressController?
  16. var filePath: String = "" {
  17. didSet {
  18. if compareView != nil {
  19. compareView.filePath = filePath
  20. }
  21. }
  22. }
  23. var fileType: KMCompareFilesType = .content {
  24. didSet {
  25. if compareView != nil {
  26. compareView.fileType = fileType
  27. }
  28. }
  29. }
  30. var password: String = "" {
  31. didSet {
  32. if compareView != nil {
  33. self.compareView.password = password
  34. }
  35. }
  36. }
  37. var contentComplete: KMCompareWindowControllerContentComplete?
  38. var coveringComplete :KMCompareWindowControllerCoveringComplete?
  39. override func windowDidLoad() {
  40. super.windowDidLoad()
  41. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  42. compareView.password = password
  43. compareView.filePath = filePath
  44. compareView.fileType = fileType
  45. compareView.cancelAction = { [unowned self] view in
  46. cancelAction?(self)
  47. }
  48. compareView.doneAction = { [weak self] view, config in
  49. self?.compareAction(config: config)
  50. }
  51. }
  52. func compareAction(config: KMCompareFilesConfig) {
  53. self.showWaitting()
  54. guard let pdfOldDocument = config.fileOldAttribute.pdfDocument, let pdfNewDocument = config.fileNewAttribute.pdfDocument else {
  55. let alert = NSAlert()
  56. alert.alertStyle = .critical
  57. alert.messageText = NSLocalizedString("Please select two files to compare", comment: "")
  58. alert.runModal()
  59. self.hideWaitting()
  60. return
  61. }
  62. let fileManager = FileManager.default
  63. let fileOldPath = config.fileOldAttribute.pdfDocument?.documentURL.path
  64. let fileNewPath = config.fileNewAttribute.pdfDocument?.documentURL.path
  65. if !fileManager.fileExists(atPath: fileOldPath!) || !fileManager.fileExists(atPath: fileNewPath!){
  66. let alert = NSAlert()
  67. alert.alertStyle = .critical
  68. alert.messageText = NSLocalizedString("The file has been deleted, please reselect a file.", comment: "")
  69. alert.runModal()
  70. self.hideWaitting()
  71. return
  72. }
  73. if (config.fileNewAttribute.fetchSelectPages().count == 0) ||
  74. (!config.fileNewAttribute.bAllPage && config.fileNewAttribute.pagesString.count < 1) {
  75. let alert = NSAlert()
  76. alert.alertStyle = .critical
  77. alert.messageText = String(format: "%@ %@", (config.fileNewAttribute.pdfDocument?.documentURL.path.lastPathComponent.lastPathComponent)!, NSLocalizedString("Invalid page range. Please reselect the page range.", comment: ""))
  78. alert.runModal()
  79. config.fileNewAttribute.bAllPage = true
  80. config.fileNewAttribute.pagesType = .all
  81. compareView.reloadData()
  82. self.hideWaitting()
  83. return
  84. }
  85. if (config.fileOldAttribute.fetchSelectPages().count == 0) || (!config.fileOldAttribute.bAllPage && config.fileOldAttribute.pagesString.count < 1) {
  86. let alert = NSAlert()
  87. alert.alertStyle = .critical
  88. alert.messageText = String(format: "%@ %@", (config.fileOldAttribute.pdfDocument?.documentURL.path.lastPathComponent.lastPathComponent)!, NSLocalizedString("Invalid page range. Please reselect the page range.", comment: ""))
  89. alert.runModal()
  90. config.fileOldAttribute.bAllPage = true
  91. config.fileOldAttribute.pagesType = .all
  92. compareView.reloadData()
  93. self.hideWaitting()
  94. return
  95. }
  96. let filePath = config.fileOldAttribute.pdfDocument?.documentURL.path
  97. let pdfDocument = CPDFDocument(url: URL(fileURLWithPath: filePath!))
  98. let filePath1 = config.fileNewAttribute.pdfDocument?.documentURL.path
  99. let pdfDocument1 = CPDFDocument(url: URL(fileURLWithPath: filePath1!))
  100. DispatchQueue.global().async {
  101. let oldDoc = CPDFDocument(url: config.fileOldAttribute.pdfDocument?.documentURL)
  102. oldDoc!.unlock(withPassword: config.fileOldAttribute.password)
  103. let doc = CPDFDocument(url: config.fileNewAttribute.pdfDocument?.documentURL)
  104. doc!.unlock(withPassword: config.fileNewAttribute.password)
  105. if self.compareView.fileType == .coverting {
  106. let pdfCompareOverlay = CPDFCompareOverlay(oldDocument: oldDoc, oldPageRange: config.fileOldAttribute.pagesString, newDocument: doc, newPageRange: config.fileNewAttribute.pagesString)
  107. let oldStrokeColor = config.oldStrokeColor
  108. pdfCompareOverlay?.setOldDocumentStroke(oldStrokeColor())
  109. pdfCompareOverlay?.setOldDocumentStrokeOpacity(config.oldStrokeOpacity())
  110. let newStrokeColor = config.newStrokeColor
  111. pdfCompareOverlay?.setNewDocumentStroke(newStrokeColor())
  112. pdfCompareOverlay?.setNewDocumentStrokeOpacity(config.newStrokeOpacity())
  113. pdfCompareOverlay?.setNewDocumentFillOpacity(config.newFillOpacity())
  114. pdfCompareOverlay?.setOldDocumentFillOpacity(config.oldFillOpacity())
  115. pdfCompareOverlay?.setNoFill(config.isNOFill())
  116. pdfCompareOverlay?.setBlendMod(config.blendMod())
  117. if (pdfCompareOverlay?.compare() == true) {
  118. DispatchQueue.main.async {
  119. self.progressController?.doubleValue = 30.0
  120. }
  121. guard let document = pdfCompareOverlay?.comparisonDocument() else {
  122. DispatchQueue.main.async {
  123. let alert = NSAlert()
  124. alert.alertStyle = .critical
  125. alert.messageText = NSLocalizedString("Failure", comment: "")
  126. alert.runModal()
  127. self.hideWaitting()
  128. }
  129. return
  130. }
  131. DispatchQueue.main.async {
  132. self.hideWaitting()
  133. self.coveringComplete?(self, document)
  134. }
  135. debugPrint("合并成功")
  136. }
  137. } else {
  138. var results: [CPDFCompareResults] = []
  139. var pdfCompareContent: CPDFCompareContent? = nil
  140. pdfCompareContent = CPDFCompareContent(oldDocument: oldDoc, newDocument: doc)
  141. pdfCompareContent?.setDelete(config.deleteColor())
  142. pdfCompareContent?.setReplace(config.replaceColor())
  143. pdfCompareContent?.setInsert(config.insertColor())
  144. pdfCompareContent?.setDeleteOpacity(config.deleteOpacity())
  145. pdfCompareContent?.setReplaceOpacity(config.replaceOpacity())
  146. pdfCompareContent?.setInsertOpacity(config.insertOpacity())
  147. let maxIndex = max(config.fileOldAttribute.fetchSelectPages().count, config.fileNewAttribute.fetchSelectPages().count)
  148. var compareType: CPDFCompareType = .all
  149. if config.isCompareText() && !config.isCompareImage() {
  150. compareType = .text
  151. } else if !config.isCompareText() && config.isCompareImage() {
  152. compareType = .image
  153. }
  154. DispatchQueue.main.async {
  155. self.progressController?.doubleValue = 30.0
  156. }
  157. for i in 0..<maxIndex {
  158. let oldPageIndex: Int
  159. if i >= config.fileOldAttribute.fetchSelectPages().count {
  160. oldPageIndex = Int(oldDoc!.pageCount) + i
  161. } else {
  162. oldPageIndex = Int(truncating: config.fileOldAttribute.fetchSelectPages()[i] as NSNumber) - 1
  163. }
  164. let newPageIndex: Int
  165. if i >= config.fileNewAttribute.fetchSelectPages().count {
  166. newPageIndex = Int(doc!.pageCount) + i
  167. } else {
  168. newPageIndex = Int(truncating: config.fileNewAttribute.fetchSelectPages()[i] as NSNumber) - 1
  169. }
  170. if let compareResults = pdfCompareContent?.compareOldPageIndex(oldPageIndex, newPageIndex: newPageIndex, type: compareType, isDrawHighlight: true) {
  171. results.append(compareResults)
  172. }
  173. }
  174. DispatchQueue.main.async {
  175. self.hideWaitting()
  176. self.contentComplete?(self, pdfCompareContent!, results, oldDoc!, doc!)
  177. if results.count > 0 {
  178. // Handle success case
  179. } else {
  180. let alert = NSAlert()
  181. alert.alertStyle = .critical
  182. alert.messageText = NSLocalizedString("There is no difference between the two documents.", comment: "")
  183. alert.runModal()
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }
  190. extension KMCompareWindowController {
  191. func showWaitting() {
  192. compareView.doneButton.isEnabled = false
  193. if self.maskView == nil {
  194. self.maskView = KMBookletMaskView(frame: CGRect(x: 0, y: 0, width: self.window?.frame.size.width ?? 0, height: self.window?.frame.size.height ?? 0))
  195. }
  196. self.window?.contentView?.addSubview(self.maskView!)
  197. let progress = SKProgressController()
  198. progress.window?.backgroundColor = NSColor.km_init(hex: "#36383B")
  199. progress.window?.contentView?.wantsLayer = true
  200. progress.window?.contentView?.layer?.backgroundColor = NSColor.km_init(hex: "#36383B").cgColor
  201. progress.progressField.textColor = NSColor.white
  202. progress.message = NSLocalizedString("Comparing documents...", comment: "")
  203. progress.closeBlock = { [weak self] in
  204. }
  205. self.progressController = progress
  206. self.window?.beginSheet(progress.window!)
  207. }
  208. func hideWaitting() {
  209. compareView.doneButton.isEnabled = true
  210. DispatchQueue.main.async {
  211. self.progressController?.doubleValue = 99.0
  212. }
  213. self.maskView?.removeFromSuperview()
  214. if (self.progressController != nil) {
  215. self.window?.endSheet((self.progressController?.window)!)
  216. self.progressController = nil
  217. }
  218. }
  219. }