KMCompareWindowController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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: KMBaseWindowController {
  11. @IBOutlet weak var compareView: KMCompareView!
  12. var pdfCompareContent: CPDFCompareContent?
  13. var filePath: String = "" {
  14. didSet {
  15. if compareView != nil {
  16. compareView.filePath = filePath
  17. }
  18. }
  19. }
  20. var fileType: KMCompareFilesType = .content {
  21. didSet {
  22. if compareView != nil {
  23. compareView.fileType = fileType
  24. }
  25. }
  26. }
  27. var password: String = "" {
  28. didSet {
  29. if compareView != nil {
  30. self.compareView.password = password
  31. }
  32. }
  33. }
  34. var contentComplete: KMCompareWindowControllerContentComplete?
  35. var coveringComplete :KMCompareWindowControllerCoveringComplete?
  36. override func windowDidLoad() {
  37. super.windowDidLoad()
  38. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  39. compareView.password = password
  40. compareView.filePath = filePath
  41. compareView.fileType = fileType
  42. compareView.cancelAction = { [unowned self] view in
  43. cancelAction?(self)
  44. }
  45. compareView.doneAction = { [unowned self] view, config in
  46. self.compareAction(config: config)
  47. }
  48. }
  49. func compareAction(config: KMCompareFilesConfig) {
  50. compareView.doneButton.isEnabled = false
  51. guard let pdfOldDocument = config.fileOldAttribute.pdfDocument, let pdfNewDocument = config.fileNewAttribute.pdfDocument else {
  52. let alert = NSAlert()
  53. alert.alertStyle = .critical
  54. alert.messageText = NSLocalizedString("Please select two files to compare", comment: "")
  55. alert.runModal()
  56. return
  57. }
  58. let fileManager = FileManager.default
  59. let fileOldPath = config.fileOldAttribute.pdfDocument?.documentURL.path
  60. let fileNewPath = config.fileNewAttribute.pdfDocument?.documentURL.path
  61. if !fileManager.fileExists(atPath: fileOldPath!) || !fileManager.fileExists(atPath: fileNewPath!){
  62. let alert = NSAlert()
  63. alert.alertStyle = .critical
  64. alert.messageText = NSLocalizedString("The file has been deleted, please reselect a file.", comment: "")
  65. alert.runModal()
  66. return
  67. }
  68. if (config.fileNewAttribute.fetchSelectPages().count == 0) ||
  69. (!config.fileNewAttribute.bAllPage && config.fileNewAttribute.pagesString.count < 1) {
  70. let alert = NSAlert()
  71. alert.alertStyle = .critical
  72. alert.messageText = String(format: "%@ %@", (config.fileNewAttribute.pdfDocument?.documentURL.path.lastPathComponent.lastPathComponent)!, NSLocalizedString("Invalid page range. Please reselect the page range.", comment: ""))
  73. alert.runModal()
  74. config.fileNewAttribute.bAllPage = true
  75. config.fileNewAttribute.pagesType = .all
  76. compareView.reloadData()
  77. return
  78. }
  79. if (config.fileOldAttribute.fetchSelectPages().count == 0) || (!config.fileOldAttribute.bAllPage && config.fileOldAttribute.pagesString.count < 1) {
  80. let alert = NSAlert()
  81. alert.alertStyle = .critical
  82. alert.messageText = String(format: "%@ %@", (config.fileOldAttribute.pdfDocument?.documentURL.path.lastPathComponent.lastPathComponent)!, NSLocalizedString("Invalid page range. Please reselect the page range.", comment: ""))
  83. alert.runModal()
  84. config.fileOldAttribute.bAllPage = true
  85. config.fileOldAttribute.pagesType = .all
  86. compareView.reloadData()
  87. return
  88. }
  89. let filePath = config.fileOldAttribute.pdfDocument?.documentURL.path
  90. let pdfDocument = CPDFDocument(url: URL(fileURLWithPath: filePath!))
  91. let filePath1 = config.fileNewAttribute.pdfDocument?.documentURL.path
  92. let pdfDocument1 = CPDFDocument(url: URL(fileURLWithPath: filePath1!))
  93. DispatchQueue.global().async {
  94. let oldDoc = CPDFDocument(url: config.fileOldAttribute.pdfDocument?.documentURL)
  95. oldDoc!.unlock(withPassword: config.fileOldAttribute.password)
  96. let doc = CPDFDocument(url: config.fileNewAttribute.pdfDocument?.documentURL)
  97. doc!.unlock(withPassword: config.fileNewAttribute.password)
  98. if self.compareView.fileType == .coverting {
  99. let pdfCompareOverlay = CPDFCompareOverlay(oldDocument: oldDoc, oldPageRange: config.fileOldAttribute.pagesString, newDocument: doc, newPageRange: config.fileNewAttribute.pagesString)
  100. let oldStrokeColor = config.oldStrokeColor
  101. pdfCompareOverlay?.setOldDocumentStroke(oldStrokeColor())
  102. pdfCompareOverlay?.setOldDocumentStrokeOpacity(config.oldStrokeOpacity())
  103. let newStrokeColor = config.newStrokeColor
  104. pdfCompareOverlay?.setNewDocumentStroke(newStrokeColor())
  105. pdfCompareOverlay?.setNewDocumentStrokeOpacity(config.newStrokeOpacity())
  106. pdfCompareOverlay?.setNewDocumentFillOpacity(config.newFillOpacity())
  107. pdfCompareOverlay?.setOldDocumentFillOpacity(config.oldFillOpacity())
  108. pdfCompareOverlay?.setNoFill(config.isNOFill())
  109. pdfCompareOverlay?.setBlendMod(config.blendMod())
  110. if (pdfCompareOverlay?.compare() == true) {
  111. guard let document = pdfCompareOverlay?.comparisonDocument() else {
  112. DispatchQueue.main.async {
  113. let alert = NSAlert()
  114. alert.alertStyle = .critical
  115. alert.messageText = NSLocalizedString("Failure", comment: "")
  116. alert.runModal()
  117. }
  118. return
  119. }
  120. DispatchQueue.main.async {
  121. self.coveringComplete?(self, document)
  122. }
  123. debugPrint("合并成功")
  124. }
  125. } else {
  126. var results: [CPDFCompareResults] = []
  127. var pdfCompareContent: CPDFCompareContent? = nil
  128. // if let _pdfCompareContent = self._pdfCompareContent {
  129. // pdfCompareContent = _pdfCompareContent
  130. // }
  131. //
  132. // if self._compareCancel {
  133. // return
  134. // }
  135. pdfCompareContent = CPDFCompareContent(oldDocument: oldDoc, newDocument: doc)
  136. pdfCompareContent?.setDelete(config.deleteColor())
  137. pdfCompareContent?.setReplace(config.replaceColor())
  138. pdfCompareContent?.setInsert(config.insertColor())
  139. pdfCompareContent?.setDeleteOpacity(config.deleteOpacity())
  140. pdfCompareContent?.setReplaceOpacity(config.replaceOpacity())
  141. pdfCompareContent?.setInsertOpacity(config.insertOpacity())
  142. let maxIndex = max(config.fileOldAttribute.fetchSelectPages().count, config.fileNewAttribute.fetchSelectPages().count)
  143. var compareType: CPDFCompareType = .all
  144. if config.isCompareText() && !config.isCompareImage() {
  145. compareType = .text
  146. } else if !config.isCompareText() && config.isCompareImage() {
  147. compareType = .image
  148. }
  149. // if self._compareCancel {
  150. // return
  151. // }
  152. for i in 0..<maxIndex {
  153. let oldPageIndex: Int
  154. if i >= config.fileOldAttribute.fetchSelectPages().count {
  155. oldPageIndex = Int(oldDoc!.pageCount) + i
  156. } else {
  157. oldPageIndex = Int(truncating: config.fileOldAttribute.fetchSelectPages()[i] as NSNumber) - 1
  158. }
  159. let newPageIndex: Int
  160. if i >= config.fileNewAttribute.fetchSelectPages().count {
  161. newPageIndex = Int(doc!.pageCount) + i
  162. } else {
  163. newPageIndex = Int(truncating: config.fileNewAttribute.fetchSelectPages()[i] as NSNumber) - 1
  164. }
  165. if let compareResults = pdfCompareContent?.compareOldPageIndex(oldPageIndex, newPageIndex: newPageIndex, type: compareType, isDrawHighlight: true) {
  166. results.append(compareResults)
  167. }
  168. // DispatchQueue.main.async {
  169. // if let compareLoadingWVC = self.compareLoadingWVC {
  170. // compareLoadingWVC.progress = CGFloat(i) / CGFloat(maxIndex)
  171. // }
  172. // }
  173. }
  174. DispatchQueue.main.async {
  175. self.contentComplete?(self, pdfCompareContent!, results, oldDoc!, doc!)
  176. if results.count > 0 {
  177. // Handle success case
  178. } else {
  179. let alert = NSAlert()
  180. alert.alertStyle = .critical
  181. alert.messageText = NSLocalizedString("There is no difference between the two documents.", comment: "")
  182. alert.runModal()
  183. }
  184. }
  185. }
  186. }
  187. }
  188. }