KMMergeView.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. //
  2. // KMMergeView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/11/8.
  6. //
  7. import Cocoa
  8. typealias KMMergeViewCancelAction = (_ view: KMMergeView) -> Void
  9. typealias KMMergeViewAddFilesAction = (_ view: KMMergeView) -> Void
  10. typealias KMMergeViewMergeAction = (_ view: KMMergeView, _ files: [KMFileAttribute], _ size: CGSize) -> Void
  11. typealias KMMergeViewClearAction = (_ view: KMMergeView) -> Void
  12. enum KMMergeViewType: Int {
  13. case add = 0
  14. case merge
  15. }
  16. class KMMergeView: KMBaseXibView {
  17. @IBOutlet weak var tableview: NSTableView!
  18. @IBOutlet weak var clearButton: NSButton!
  19. @IBOutlet weak var cancelButton: NSButton!
  20. @IBOutlet weak var mergeButton: NSButton!
  21. @IBOutlet weak var addFilesButton: NSButton!
  22. @IBOutlet weak var progress: NSProgressIndicator!
  23. @IBOutlet weak var pageSizeWidthTextField: NSTextField!
  24. @IBOutlet weak var pageSizeWidthHeightConnectorTextField: NSTextField!
  25. @IBOutlet weak var pageSizeHeightTextField: NSTextField!
  26. @IBOutlet weak var pageSizeUnitLabel: NSTextField!
  27. @IBOutlet weak var originalSizeButton: NSButton!
  28. @IBOutlet weak var A4SizeButton: NSButton!
  29. @IBOutlet weak var A3SizeButton: NSButton!
  30. @IBOutlet weak var USLetterSizeButton: NSButton!
  31. @IBOutlet weak var USLegalButton: NSButton!
  32. @IBOutlet weak var customSizeButton: NSButton!
  33. @IBOutlet weak var box: KMBox!
  34. @IBOutlet weak var boxLabel: NSTextField!
  35. @IBOutlet weak var blankView: KMMergeBlankView!
  36. var cancelAction: KMMergeViewCancelAction?
  37. var addFilesAction: KMMergeViewAddFilesAction?
  38. var mergeAction: KMMergeViewMergeAction?
  39. var clearAction: KMMergeViewClearAction?
  40. let MyTableCellViewDataType = NSPasteboard.PasteboardType(rawValue: "KMLocalForDraggedTypes")
  41. var type: KMMergeViewType = .add {
  42. didSet {
  43. self.updateLanguage()
  44. }
  45. }
  46. var files: [KMFileAttribute] = [] {
  47. didSet {
  48. self.reloadData()
  49. }
  50. }
  51. var lockFiles: [KMFileAttribute] = [] {
  52. didSet {
  53. self.reloadData()
  54. }
  55. }//存在密码文件
  56. var lockFilesIndex: Int = 0
  57. var newPageSize: NSSize = .zero
  58. var insertRow: Int = 0
  59. override func draw(_ dirtyRect: NSRect) {
  60. super.draw(dirtyRect)
  61. // Drawing code here.
  62. }
  63. override func setup() {
  64. self.box.cornerRadius = 4
  65. pageSizeWidthTextField.isEnabled = false
  66. pageSizeHeightTextField.isEnabled = false;
  67. tableview.delegate = self
  68. tableview.dataSource = self
  69. tableview.allowsMultipleSelection = true
  70. tableview.registerForDraggedTypes([MyTableCellViewDataType, .fileURL, .string, .pdf])
  71. // tableview.register(NSNib.init(nibNamed: "KMMergeTableViewCell", bundle: nil), forIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMMergeTableViewCell"))
  72. progress.isHidden = true
  73. boxLabel.textColor = KMAppearance.Layout.h0Color()
  74. blankView.dragAction = { [weak self] filePaths in
  75. var theFileUrls: [URL] = []
  76. for fileUrl in filePaths {
  77. if KMTools.isImageType(fileUrl.pathExtension) {
  78. self?.addImageFile(fileUrl: fileUrl)
  79. } else {
  80. theFileUrls.append(fileUrl)
  81. }
  82. }
  83. self?.addFilePaths(urls: theFileUrls)
  84. }
  85. blankView.mouseDownAction = { [unowned self] view in
  86. self.addFilesAction?(self)
  87. }
  88. }
  89. override func updateLanguage() {
  90. originalSizeButton.title = NSLocalizedString("Original Size", comment: "")
  91. A4SizeButton.title = "A4";
  92. A3SizeButton.title = "A3";
  93. USLetterSizeButton.title = NSLocalizedString("U.S.Letter", comment: "");
  94. USLegalButton.title = NSLocalizedString("U.S.Legal", comment: "");
  95. customSizeButton.title = NSLocalizedString("Custom", comment: "");
  96. pageSizeWidthTextField.stringValue = "595";
  97. pageSizeHeightTextField.stringValue = "841";
  98. cancelButton.title = NSLocalizedString("Cancel", comment: "");
  99. clearButton.title = NSLocalizedString("Clear", comment: "");
  100. addFilesButton.title = NSLocalizedString("Add Files", comment: "")
  101. mergeButton.title = self.type == .add ? NSLocalizedString("Append", comment: ""): NSLocalizedString("Merge", comment: "")
  102. boxLabel.stringValue = NSLocalizedString("Page size:", comment: "")
  103. }
  104. override func reloadData() {
  105. self.updateButtonState()
  106. if files.count != 0 {
  107. self.blankView.isHidden = true
  108. } else {
  109. self.blankView.isHidden = false
  110. }
  111. self.tableview.reloadData()
  112. }
  113. func updateButtonState() {
  114. if (files.count >= 1) {
  115. blankView.isHidden = true
  116. clearButton.isEnabled = true
  117. mergeButton.isEnabled = true
  118. } else {
  119. blankView.isHidden = false
  120. clearButton.isEnabled = false
  121. mergeButton.isEnabled = false
  122. }
  123. }
  124. }
  125. extension KMMergeView {
  126. func controlTextDidChange(_ obj: Notification) {
  127. // NSTextField *textField = (NSTextField*)[obj object];
  128. // NSInteger index = textField.tag;
  129. // [[_files objectAtIndex:index] setPagesString:textField.stringValue];
  130. }
  131. }
  132. extension KMMergeView: NSTableViewDataSource {
  133. func numberOfRows(in tableView: NSTableView) -> Int {
  134. return files.count
  135. }
  136. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  137. var cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("KMMergeTableViewCell"), owner: self)
  138. if cell == nil {
  139. cell = KMMergeTableViewCell.init(frame: .zero)
  140. }
  141. let myCellView: KMMergeTableViewCell = cell! as! KMMergeTableViewCell
  142. if row < files.count {
  143. myCellView.fileModel = files[row]
  144. }
  145. myCellView.index = row + 1
  146. // 配置单元格的显示内容
  147. myCellView.removeAction = { [weak self] view, model in
  148. self?.files.removeObject(model)
  149. self?.reloadData()
  150. }
  151. return myCellView
  152. }
  153. func tableView(_ tableView: NSTableView, shouldSelect tableColumn: NSTableColumn?) -> Bool {
  154. return false
  155. }
  156. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  157. return 120
  158. }
  159. }
  160. extension KMMergeView: NSTableViewDelegate {
  161. func tableView(_ tableView: NSTableView, writeRowsWith rowIndexes: IndexSet, to pboard: NSPasteboard) -> Bool {
  162. let indexSetData = try? NSKeyedArchiver.archivedData(withRootObject: rowIndexes, requiringSecureCoding: true)
  163. pboard.declareTypes([NSPasteboard.PasteboardType.string], owner: self)
  164. pboard.setData(indexSetData, forType: MyTableCellViewDataType)
  165. return true
  166. }
  167. func tableView(_ tableView: NSTableView, validateDrop info: NSDraggingInfo, proposedRow row: Int, proposedDropOperation dropOperation: NSTableView.DropOperation) -> NSDragOperation {
  168. if dropOperation == .on {
  169. return []
  170. }
  171. var isCanDrag = false
  172. var result = NSDragOperation.copy
  173. let pboard = info.draggingPasteboard
  174. if pboard.availableType(from: [NSPasteboard.PasteboardType.fileURL]) != nil {
  175. let filePath = pboard.propertyList(forType: NSPasteboard.PasteboardType.fileURL) as? String
  176. let url = URL(string: filePath!)
  177. if url!.path.lowercased().hasSuffix("pdf") {
  178. isCanDrag = true
  179. } else {
  180. if KMTools.isImageType(url?.pathExtension ?? "") {
  181. isCanDrag = true
  182. } else {
  183. isCanDrag = false
  184. }
  185. }
  186. } else if (pboard.availableType(from: [MyTableCellViewDataType]) != nil) {
  187. result = .every
  188. }
  189. if isCanDrag {
  190. result = .copy
  191. }
  192. return result
  193. }
  194. func tableView(_ tableView: NSTableView, acceptDrop info: NSDraggingInfo, row: Int, dropOperation: NSTableView.DropOperation) -> Bool {
  195. var result = false
  196. let pboard = info.draggingPasteboard
  197. insertRow = row
  198. // NSPasteboard.PasteboardType.fileURL
  199. if pboard.availableType(from: [NSPasteboard.PasteboardType.fileURL]) != nil {
  200. let filePath = pboard.propertyList(forType: NSPasteboard.PasteboardType.fileURL) as? String
  201. let url = URL(string: filePath!)
  202. var array = [URL]()
  203. array.append(url!)
  204. var theFileUrls: [URL] = []
  205. for fileUrl in array {
  206. if KMTools.isImageType(fileUrl.pathExtension) {
  207. self.addImageFile(fileUrl: fileUrl)
  208. } else {
  209. theFileUrls.append(fileUrl)
  210. }
  211. }
  212. addFilePaths(urls: theFileUrls)
  213. result = true
  214. } else if pboard.availableType(from: [MyTableCellViewDataType]) != nil {
  215. guard let data = info.draggingPasteboard.data(forType: MyTableCellViewDataType),
  216. let rowIndexes = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? IndexSet else {
  217. return false
  218. }
  219. // 移动数据
  220. var draggedItems: [KMFileAttribute] = []
  221. for index in rowIndexes {
  222. draggedItems.append(files[index])
  223. }
  224. for index in rowIndexes.reversed() {
  225. files.remove(at: index)
  226. }
  227. let insertionIndex = row > rowIndexes.first! ? row - rowIndexes.count : row
  228. for (index, item) in draggedItems.enumerated() {
  229. files.insert(item, at: insertionIndex + index)
  230. }
  231. tableView.reloadData()
  232. return true
  233. } else {
  234. result = false
  235. }
  236. return result
  237. }
  238. }
  239. extension KMMergeView {
  240. @IBAction func clearButtonAction(_ sender: Any) {
  241. self.files.removeAll()
  242. self.reloadData()
  243. guard let callBack = clearAction else { return }
  244. callBack(self)
  245. }
  246. @IBAction func cancelButtonAction(_ sender: Any) {
  247. guard let callBack = cancelAction else { return }
  248. callBack(self)
  249. }
  250. @IBAction func mergeButtonAction(_ sender: Any) {
  251. guard let callBack = mergeAction else { return }
  252. self.reloadData()
  253. callBack(self, self.files, self.newPageSize)
  254. }
  255. @IBAction func addFilesButtonAction(_ sender: Any) {
  256. guard let callBack = addFilesAction else { return }
  257. callBack(self)
  258. }
  259. @IBAction func sizeButtonAction(_ sender: NSButton) {
  260. originalSizeButton.state = .off
  261. A3SizeButton.state = .off
  262. A4SizeButton.state = .off
  263. USLetterSizeButton.state = .off
  264. USLegalButton.state = .off
  265. customSizeButton.state = .off
  266. sender.state = .on
  267. pageSizeHeightTextField.isEnabled = sender.isEqual(customSizeButton)
  268. pageSizeWidthTextField.isEnabled = sender.isEqual(customSizeButton)
  269. var size: NSSize = .zero
  270. switch sender.tag {
  271. case 0:
  272. break
  273. case 1:
  274. size = NSMakeSize(595, 841);
  275. break;
  276. case 2:
  277. size = NSMakeSize(841, 1190);
  278. break;
  279. case 3:
  280. size = NSMakeSize(612, 792);
  281. break;
  282. case 4:
  283. size = NSMakeSize(612, 1108);
  284. break;
  285. case 5:
  286. size = NSMakeSize(595, 841);
  287. pageSizeWidthTextField.stringValue = size.width.description
  288. pageSizeHeightTextField.stringValue = size.height.description
  289. break;
  290. default:
  291. break
  292. }
  293. self.newPageSize = size
  294. }
  295. }
  296. //MARK: public
  297. extension KMMergeView {
  298. func addFilePaths(urls: [URL]) {
  299. lockFiles.removeAll()
  300. // files.removeAll()
  301. for url in urls {
  302. let file = KMFileAttribute()
  303. file.filePath = url.path
  304. if file.isLocked {
  305. lockFiles.append(file)
  306. } else {
  307. var isExist = false
  308. for item in files {
  309. if item.filePath == file.filePath {
  310. isExist = true
  311. break
  312. }
  313. }
  314. if !isExist {
  315. files.append(file)
  316. }
  317. }
  318. }
  319. lockFilesIndex = 0
  320. self.openPasswordFile { [weak self] success, resultPassword in
  321. self?.reloadData()
  322. }
  323. }
  324. func addImageFile(fileUrl: URL) {
  325. if let image = NSImage(contentsOf: fileUrl) {
  326. if let page = PDFPage(image: image) {
  327. let document = PDFDocument()
  328. document.insert(page, at: 0)
  329. var path = self._saveImagePath() + "/" + fileUrl.deletingPathExtension().lastPathComponent + ".pdf"
  330. path = KMTools.getUniqueFilePath(filePath: path)
  331. let result = document.write(toFile: path)
  332. if result {
  333. let file = KMFileAttribute()
  334. file.filePath = path
  335. file.oriFilePath = fileUrl.path
  336. file.myPDFDocument = document
  337. self.files.append(file)
  338. }
  339. }
  340. } else {
  341. Task {
  342. _ = await KMAlertTool.runModel(message: NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: ""))
  343. }
  344. }
  345. }
  346. func openPasswordFile(completion: @escaping ((_ success: Bool, _ resultPassword: String) -> Void)) {
  347. if lockFiles.count != 0 {
  348. let file = lockFiles[lockFilesIndex]
  349. KMBaseWindowController.checkPassword(url: URL(fileURLWithPath: file.filePath), type: .owner) { [unowned self] success, resultPassword in
  350. if success {
  351. file.password = resultPassword
  352. lockFilesIndex = lockFilesIndex + 1
  353. files.append(file)
  354. completion(true, "")
  355. if lockFiles.count > lockFilesIndex {
  356. self.openPasswordFile(completion: completion)
  357. }
  358. } else {
  359. completion(false, "")
  360. }
  361. }
  362. } else {
  363. completion(true, "")
  364. }
  365. }
  366. }
  367. //MARK: private
  368. extension KMMergeView {
  369. private func openFilePath(url: URL) {
  370. NSWorkspace.shared.activateFileViewerSelecting([url])
  371. }
  372. private func isExistAtFilePath(filePath: String) -> Bool{
  373. for file in self.files {
  374. if file.filePath == filePath {
  375. return true
  376. }
  377. }
  378. return false
  379. }
  380. private func _saveImagePath() -> String {
  381. let rootPath = KMDataManager.fetchAppSupportOfBundleIdentifierDirectory()
  382. let path = rootPath.appendingPathComponent("Merge").path
  383. if FileManager.default.fileExists(atPath: path) == false {
  384. try?FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: false)
  385. }
  386. return path
  387. }
  388. }