KMCompareWindowController.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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, _ 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. // if pdfDocument!.isLocked && self.pdfOldDocument?.isLocked == true {
  91. // DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) {
  92. // let passwordWC = PasswordWindowController(windowNibName: "PasswordWindowController")
  93. // passwordWC.fileURL = URL(fileURLWithPath: filePath)
  94. // passwordWC.beginSheetModalForWindow(self.window) { password in
  95. // if let password = password {
  96. // pdfDocument.unlock(withPassword: password)
  97. // config.fileOldAttribute.pdfDocument = pdfDocument
  98. // config.fileOldAttribute.password = password
  99. // config.fileOldAttribute.bAllPage = true
  100. // config.fileOldAttribute.pagesType = PDFSeleectPageType_AllPages
  101. // self.pdfOldDocument = PDFDocument(url: URL(fileURLWithPath: filePath))
  102. // if self.pdfOldDocument?.isLocked == true {
  103. // self.pdfOldDocument?.unlock(withPassword: password)
  104. // }
  105. // self.oldPDFView.document = self.pdfOldDocument
  106. // self.oldPDFView.autoScales = true
  107. // self.oldPDFView.delegate = self
  108. // self.updateOldFileQKSelectedPathsWithPath(self.pdfOldDocument?.documentURL?.path ?? "")
  109. // self.reloadOldPDFData()
  110. // }
  111. // }
  112. // }
  113. // return
  114. // }
  115. let filePath1 = config.fileNewAttribute.pdfDocument?.documentURL.path
  116. let pdfDocument1 = CPDFDocument(url: URL(fileURLWithPath: filePath1!))
  117. // if pdfDocument1.isLocked && self.pdfNewDocument?.isLocked == true {
  118. // DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) {
  119. // let passwordWC = PasswordWindowController(windowNibName: "PasswordWindowController")
  120. // passwordWC.fileURL = URL(fileURLWithPath: filePath1)
  121. // passwordWC.beginSheetModalForWindow(self.window) { password in
  122. // if let password = password {
  123. // pdfDocument.unlock(withPassword: password)
  124. // config.fileNewAttribute.pdfDocument = pdfDocument1
  125. // config.fileNewAttribute.password = password
  126. // config.fileNewAttribute.bAllPage = true
  127. // config.fileNewAttribute.pagesType = PDFSeleectPageType_AllPages
  128. // self.pdfNewDocument = PDFDocument(url: URL(fileURLWithPath: filePath1))
  129. // if self.pdfNewDocument?.isLocked == true {
  130. // self.pdfNewDocument?.unlock(withPassword: password)
  131. // }
  132. // self.pdfNewView.document = self.pdfNewDocument
  133. // self.pdfNewView.autoScales = true
  134. // self.pdfNewView.delegate = self
  135. // self.addFileContentView.isHidden = true
  136. // self.updateNewFileQKSelectedPathsWithPath(self.pdfNewDocument?.documentURL?.path ?? "")
  137. // self.reloadNewPDFData()
  138. // }
  139. // }
  140. // }
  141. // return
  142. // }
  143. DispatchQueue.global().async {
  144. let oldDoc = CPDFDocument(url: config.fileOldAttribute.pdfDocument?.documentURL)
  145. oldDoc!.unlock(withPassword: config.fileOldAttribute.password)
  146. let doc = CPDFDocument(url: config.fileNewAttribute.pdfDocument?.documentURL)
  147. doc!.unlock(withPassword: config.fileNewAttribute.password)
  148. // if let compareLoadingWVC = self.compareLoadingWVC {
  149. // compareLoadingWVC.maxPageCount = config.fileOldAttribute.pdfDocument.pageCount + config.fileNewAttribute.pdfDocument.pageCount
  150. // }
  151. // if self._compareCancel {
  152. // return
  153. // }
  154. if self.compareView.fileType == .coverting {
  155. let pdfCompareOverlay = CPDFCompareOverlay(oldDocument: oldDoc, oldPageRange: config.fileOldAttribute.pagesString, newDocument: doc, newPageRange: config.fileNewAttribute.pagesString)
  156. let oldStrokeColor = config.oldStrokeColor
  157. pdfCompareOverlay?.setOldDocumentStroke(oldStrokeColor())
  158. pdfCompareOverlay?.setOldDocumentStrokeOpacity(config.oldStrokeOpacity())
  159. let newStrokeColor = config.newStrokeColor
  160. pdfCompareOverlay?.setNewDocumentStroke(newStrokeColor())
  161. pdfCompareOverlay?.setNewDocumentStrokeOpacity(config.newStrokeOpacity())
  162. pdfCompareOverlay?.setNewDocumentFillOpacity(config.newFillOpacity())
  163. pdfCompareOverlay?.setOldDocumentFillOpacity(config.oldFillOpacity())
  164. pdfCompareOverlay?.setNoFill(config.isNOFill())
  165. pdfCompareOverlay?.setBlendMod(config.blendMod())
  166. if (pdfCompareOverlay?.compare() == true) {
  167. guard let document = pdfCompareOverlay?.comparisonDocument() else {
  168. DispatchQueue.main.async {
  169. let alert = NSAlert()
  170. alert.alertStyle = .critical
  171. alert.messageText = NSLocalizedString("Failure", comment: "")
  172. alert.runModal()
  173. }
  174. return
  175. }
  176. DispatchQueue.main.async {
  177. self.coveringComplete?(self, document)
  178. }
  179. debugPrint("合并成功")
  180. }
  181. } else {
  182. var results: [CPDFCompareResults] = []
  183. var pdfCompareContent: CPDFCompareContent? = nil
  184. // if let _pdfCompareContent = self._pdfCompareContent {
  185. // pdfCompareContent = _pdfCompareContent
  186. // }
  187. //
  188. // if self._compareCancel {
  189. // return
  190. // }
  191. pdfCompareContent = CPDFCompareContent(oldDocument: oldDoc, newDocument: doc)
  192. pdfCompareContent?.setDelete(config.deleteColor())
  193. pdfCompareContent?.setReplace(config.replaceColor())
  194. pdfCompareContent?.setInsert(config.insertColor())
  195. pdfCompareContent?.setDeleteOpacity(config.deleteOpacity())
  196. pdfCompareContent?.setReplaceOpacity(config.replaceOpacity())
  197. pdfCompareContent?.setInsertOpacity(config.insertOpacity())
  198. let maxIndex = max(config.fileOldAttribute.fetchSelectPages().count, config.fileNewAttribute.fetchSelectPages().count)
  199. var compareType: CPDFCompareType = .all
  200. if config.isCompareText() && !config.isCompareImage() {
  201. compareType = .text
  202. } else if !config.isCompareText() && config.isCompareImage() {
  203. compareType = .image
  204. }
  205. // if self._compareCancel {
  206. // return
  207. // }
  208. for i in 0..<maxIndex {
  209. let oldPageIndex: Int
  210. if i >= config.fileOldAttribute.fetchSelectPages().count {
  211. oldPageIndex = Int(oldDoc!.pageCount) + i
  212. } else {
  213. oldPageIndex = Int(truncating: config.fileOldAttribute.fetchSelectPages()[i]) - 1
  214. }
  215. let newPageIndex: Int
  216. if i >= config.fileNewAttribute.fetchSelectPages().count {
  217. newPageIndex = Int(doc!.pageCount) + i
  218. } else {
  219. newPageIndex = Int(truncating: config.fileNewAttribute.fetchSelectPages()[i]) - 1
  220. }
  221. if let compareResults = pdfCompareContent?.compareOldPageIndex(oldPageIndex, newPageIndex: newPageIndex, type: compareType, isDrawHighlight: true) {
  222. results.append(compareResults)
  223. }
  224. // DispatchQueue.main.async {
  225. // if let compareLoadingWVC = self.compareLoadingWVC {
  226. // compareLoadingWVC.progress = CGFloat(i) / CGFloat(maxIndex)
  227. // }
  228. // }
  229. }
  230. // DispatchQueue.main.async {
  231. self.contentComplete?(self, pdfCompareContent!, results, oldDoc!, doc!)
  232. if results.count > 0 {
  233. // Handle success case
  234. } else {
  235. let alert = NSAlert()
  236. alert.alertStyle = .critical
  237. alert.messageText = NSLocalizedString("There is no difference between the two documents.", comment: "")
  238. alert.runModal()
  239. }
  240. // }
  241. }
  242. }
  243. }
  244. }