KMCompareWindowController.swift 10.0 KB

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