KMCompareWindowController.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //
  2. // KMCompareWindowController.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/11/14.
  6. //
  7. import Cocoa
  8. typealias KMCompareWindowControllerContentComplete = (_ controller: KMCompareWindowController, _ 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 contentComplete: KMCompareWindowControllerContentComplete?
  28. var coveringComplete :KMCompareWindowControllerCoveringComplete?
  29. override func windowDidLoad() {
  30. super.windowDidLoad()
  31. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  32. compareView.filePath = filePath
  33. compareView.fileType = fileType
  34. compareView.cancelAction = { [unowned self] view in
  35. cancelAction?(self)
  36. }
  37. compareView.doneAction = { [unowned self] view, config in
  38. }
  39. }
  40. func compareAction(config: KMCompareFilesConfig) {
  41. guard let pdfOldDocument = config.fileOldAttribute.pdfDocument, let pdfNewDocument = config.fileNewAttribute.pdfDocument else {
  42. let alert = NSAlert()
  43. alert.alertStyle = .critical
  44. alert.messageText = NSLocalizedString("Please select two files to compare", comment: "")
  45. alert.runModal()
  46. return
  47. }
  48. let fileManager = FileManager.default
  49. let fileOldPath = config.fileOldAttribute.pdfDocument?.documentURL.path
  50. let fileNewPath = config.fileNewAttribute.pdfDocument?.documentURL.path
  51. if !fileManager.fileExists(atPath: fileOldPath!) || !fileManager.fileExists(atPath: fileNewPath!){
  52. let alert = NSAlert()
  53. alert.alertStyle = .critical
  54. alert.messageText = NSLocalizedString("The file has been deleted, please reselect a file.", comment: "")
  55. alert.runModal()
  56. return
  57. }
  58. if (config.fileNewAttribute.fetchSelectPages().count == 0) ||
  59. (!config.fileNewAttribute.bAllPage && config.fileNewAttribute.pagesString.count < 1) {
  60. let alert = NSAlert()
  61. alert.alertStyle = .critical
  62. alert.messageText = String(format: "%@ %@", (config.fileNewAttribute.pdfDocument?.documentURL.path.lastPathComponent.lastPathComponent)!, NSLocalizedString("Invalid page range. Please reselect the page range.", comment: ""))
  63. alert.runModal()
  64. config.fileNewAttribute.bAllPage = true
  65. config.fileNewAttribute.pagesType = .all
  66. compareView.reloadData()
  67. return
  68. }
  69. if (config.fileOldAttribute.fetchSelectPages().count == 0) || (!config.fileOldAttribute.bAllPage && config.fileOldAttribute.pagesString.count < 1) {
  70. let alert = NSAlert()
  71. alert.alertStyle = .critical
  72. alert.messageText = String(format: "%@ %@", (config.fileOldAttribute.pdfDocument?.documentURL.path.lastPathComponent.lastPathComponent)!, NSLocalizedString("Invalid page range. Please reselect the page range.", comment: ""))
  73. alert.runModal()
  74. config.fileOldAttribute.bAllPage = true
  75. config.fileOldAttribute.pagesType = .all
  76. compareView.reloadData()
  77. return
  78. }
  79. let filePath = config.fileOldAttribute.pdfDocument?.documentURL.path
  80. let pdfDocument = CPDFDocument(url: URL(fileURLWithPath: filePath!))
  81. // if pdfDocument!.isLocked && self.pdfOldDocument?.isLocked == true {
  82. // DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) {
  83. // let passwordWC = PasswordWindowController(windowNibName: "PasswordWindowController")
  84. // passwordWC.fileURL = URL(fileURLWithPath: filePath)
  85. // passwordWC.beginSheetModalForWindow(self.window) { password in
  86. // if let password = password {
  87. // pdfDocument.unlock(withPassword: password)
  88. // config.fileOldAttribute.pdfDocument = pdfDocument
  89. // config.fileOldAttribute.password = password
  90. // config.fileOldAttribute.bAllPage = true
  91. // config.fileOldAttribute.pagesType = PDFSeleectPageType_AllPages
  92. // self.pdfOldDocument = PDFDocument(url: URL(fileURLWithPath: filePath))
  93. // if self.pdfOldDocument?.isLocked == true {
  94. // self.pdfOldDocument?.unlock(withPassword: password)
  95. // }
  96. // self.oldPDFView.document = self.pdfOldDocument
  97. // self.oldPDFView.autoScales = true
  98. // self.oldPDFView.delegate = self
  99. // self.updateOldFileQKSelectedPathsWithPath(self.pdfOldDocument?.documentURL?.path ?? "")
  100. // self.reloadOldPDFData()
  101. // }
  102. // }
  103. // }
  104. // return
  105. // }
  106. let filePath1 = config.fileNewAttribute.pdfDocument?.documentURL.path
  107. let pdfDocument1 = CPDFDocument(url: URL(fileURLWithPath: filePath1!))
  108. // if pdfDocument1.isLocked && self.pdfNewDocument?.isLocked == true {
  109. // DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) {
  110. // let passwordWC = PasswordWindowController(windowNibName: "PasswordWindowController")
  111. // passwordWC.fileURL = URL(fileURLWithPath: filePath1)
  112. // passwordWC.beginSheetModalForWindow(self.window) { password in
  113. // if let password = password {
  114. // pdfDocument.unlock(withPassword: password)
  115. // config.fileNewAttribute.pdfDocument = pdfDocument1
  116. // config.fileNewAttribute.password = password
  117. // config.fileNewAttribute.bAllPage = true
  118. // config.fileNewAttribute.pagesType = PDFSeleectPageType_AllPages
  119. // self.pdfNewDocument = PDFDocument(url: URL(fileURLWithPath: filePath1))
  120. // if self.pdfNewDocument?.isLocked == true {
  121. // self.pdfNewDocument?.unlock(withPassword: password)
  122. // }
  123. // self.pdfNewView.document = self.pdfNewDocument
  124. // self.pdfNewView.autoScales = true
  125. // self.pdfNewView.delegate = self
  126. // self.addFileContentView.isHidden = true
  127. // self.updateNewFileQKSelectedPathsWithPath(self.pdfNewDocument?.documentURL?.path ?? "")
  128. // self.reloadNewPDFData()
  129. // }
  130. // }
  131. // }
  132. // return
  133. // }
  134. DispatchQueue.global().async {
  135. let oldDoc = CPDFDocument(url: config.fileOldAttribute.pdfDocument?.documentURL)
  136. if let password = config.fileOldAttribute.password {
  137. oldDoc!.unlock(withPassword: password)
  138. }
  139. let doc = CPDFDocument(url: config.fileNewAttribute.pdfDocument?.documentURL)
  140. if let password = config.fileNewAttribute.password {
  141. doc!.unlock(withPassword: password)
  142. }
  143. // if let compareLoadingWVC = self.compareLoadingWVC {
  144. // compareLoadingWVC.maxPageCount = config.fileOldAttribute.pdfDocument.pageCount + config.fileNewAttribute.pdfDocument.pageCount
  145. // }
  146. // if self._compareCancel {
  147. // return
  148. // }
  149. if self.compareView.fileType == .coverting {
  150. let pdfCompareOverlay = CPDFCompareOverlay(oldDocument: oldDoc, oldPageRange: config.fileOldAttribute.pagesString, newDocument: doc, newPageRange: config.fileNewAttribute.pagesString)
  151. let oldStrokeColor = config.oldStrokeColor
  152. pdfCompareOverlay?.setOldDocumentStroke(oldStrokeColor())
  153. pdfCompareOverlay?.setOldDocumentStrokeOpacity(config.oldStrokeOpacity())
  154. let newStrokeColor = config.newStrokeColor
  155. pdfCompareOverlay?.setNewDocumentStroke(newStrokeColor())
  156. pdfCompareOverlay?.setNewDocumentStrokeOpacity(config.newStrokeOpacity())
  157. pdfCompareOverlay?.setNewDocumentFillOpacity(config.newFillOpacity())
  158. pdfCompareOverlay?.setOldDocumentFillOpacity(config.oldFillOpacity())
  159. pdfCompareOverlay?.setNoFill(config.isNOFill())
  160. pdfCompareOverlay?.setBlendMod(config.blendMod())
  161. if ((pdfCompareOverlay?.compare()) != nil) {
  162. let document: CPDFDocument = (pdfCompareOverlay?.comparisonDocument()!)!
  163. if document == nil {
  164. DispatchQueue.main.async {
  165. self.coveringComplete?(self, document)
  166. }
  167. debugPrint("合并成功")
  168. } else {
  169. DispatchQueue.main.async {
  170. let alert = NSAlert()
  171. alert.alertStyle = .critical
  172. alert.messageText = NSLocalizedString("Failure", comment: "")
  173. alert.runModal()
  174. }
  175. }
  176. }
  177. } else {
  178. var results = [CPDFCompareResults]()
  179. var pdfCompareContent: CPDFCompareContent? = nil
  180. // if let _pdfCompareContent = self._pdfCompareContent {
  181. // pdfCompareContent = _pdfCompareContent
  182. // }
  183. //
  184. // if self._compareCancel {
  185. // return
  186. // }
  187. pdfCompareContent = CPDFCompareContent(oldDocument: oldDoc, newDocument: doc)
  188. pdfCompareContent?.setDelete(config.deleteColor())
  189. pdfCompareContent?.setReplace(config.replaceColor())
  190. pdfCompareContent?.setInsert(config.insertColor())
  191. pdfCompareContent?.setDeleteOpacity(config.deleteOpacity())
  192. pdfCompareContent?.setReplaceOpacity(config.replaceOpacity())
  193. pdfCompareContent?.setInsertOpacity(config.insertOpacity())
  194. let maxIndex = max(config.fileOldAttribute.fetchSelectPages().count, config.fileNewAttribute.fetchSelectPages().count)
  195. var compareType: CPDFCompareType = .all
  196. if config.isCompareText() && !config.isCompareImage() {
  197. compareType = .text
  198. } else if !config.isCompareText() && config.isCompareImage() {
  199. compareType = .image
  200. }
  201. // if self._compareCancel {
  202. // return
  203. // }
  204. for i in 0..<maxIndex {
  205. let oldPageIndex: Int
  206. if i >= config.fileOldAttribute.fetchSelectPages().count {
  207. oldPageIndex = Int(oldDoc!.pageCount) + i
  208. } else {
  209. oldPageIndex = Int(truncating: config.fileOldAttribute.fetchSelectPages()[i]) - 1
  210. }
  211. let newPageIndex: Int
  212. if i >= config.fileNewAttribute.fetchSelectPages().count {
  213. newPageIndex = Int(doc!.pageCount) + i
  214. } else {
  215. newPageIndex = Int(truncating: config.fileNewAttribute.fetchSelectPages()[i]) - 1
  216. }
  217. if let compareResults = pdfCompareContent?.compareOldPageIndex(oldPageIndex, newPageIndex: newPageIndex, type: compareType, isDrawHighlight: true) {
  218. results.append(compareResults)
  219. }
  220. // DispatchQueue.main.async {
  221. // if let compareLoadingWVC = self.compareLoadingWVC {
  222. // compareLoadingWVC.progress = CGFloat(i) / CGFloat(maxIndex)
  223. // }
  224. // }
  225. }
  226. DispatchQueue.main.async {
  227. self.contentComplete?(self,results, oldDoc!, doc!)
  228. if results.count > 0 {
  229. // Handle success case
  230. } else {
  231. let alert = NSAlert()
  232. alert.alertStyle = .critical
  233. alert.messageText = NSLocalizedString("There is no difference between the two documents.", comment: "")
  234. alert.runModal()
  235. }
  236. }
  237. }
  238. }
  239. }
  240. }