KMMergeView.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. // for path in fileNames {
  205. // if !isExistAtFilePath(filePath: path) {
  206. // continue
  207. // }
  208. //// if let attrib = try? FileManager.default.attributesOfItem(atPath: path),
  209. //// let fileSize = attrib[.size] as? CGFloat {
  210. ////
  211. //// allFileSize += fileSize
  212. ////
  213. //// if !IAPProductsManager.defaultManager.isAvailableAllFunction {
  214. //// // 免费版只支持2个文件做合并,小于20M的文件合并
  215. //// if files.count >= 2 || allFileSize > (20 * 1024 * 1024) {
  216. //// let vc = KMToolCompareWindowController.toolCompare(with: .pageEdit, setSelectIndex: 1)
  217. //// vc.showWindow(nil)
  218. ////
  219. //// allFileSize -= fileSize
  220. //// addFiles(array)
  221. //// result = true
  222. //// return false
  223. //// }
  224. //// }
  225. // array.append(URL(string: path)!)
  226. //// }
  227. // }
  228. var theFileUrls: [URL] = []
  229. for fileUrl in array {
  230. if KMTools.isImageType(fileUrl.pathExtension) {
  231. self.addImageFile(fileUrl: fileUrl)
  232. } else {
  233. theFileUrls.append(fileUrl)
  234. }
  235. }
  236. addFilePaths(urls: theFileUrls)
  237. result = true
  238. } else if pboard.availableType(from: [MyTableCellViewDataType]) != nil {
  239. guard let data = info.draggingPasteboard.data(forType: MyTableCellViewDataType),
  240. let rowIndexes = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? IndexSet else {
  241. return false
  242. }
  243. // 移动数据
  244. var draggedItems: [KMFileAttribute] = []
  245. for index in rowIndexes {
  246. draggedItems.append(files[index])
  247. }
  248. for index in rowIndexes.reversed() {
  249. files.remove(at: index)
  250. }
  251. let insertionIndex = row > rowIndexes.first! ? row - rowIndexes.count : row
  252. for (index, item) in draggedItems.enumerated() {
  253. files.insert(item, at: insertionIndex + index)
  254. }
  255. tableView.reloadData()
  256. return true
  257. } else {
  258. result = false
  259. }
  260. return result
  261. }
  262. }
  263. extension KMMergeView {
  264. @IBAction func clearButtonAction(_ sender: Any) {
  265. self.files.removeAll()
  266. self.reloadData()
  267. guard let callBack = clearAction else { return }
  268. callBack(self)
  269. }
  270. @IBAction func cancelButtonAction(_ sender: Any) {
  271. guard let callBack = cancelAction else { return }
  272. callBack(self)
  273. }
  274. @IBAction func mergeButtonAction(_ sender: Any) {
  275. guard let callBack = mergeAction else { return }
  276. self.reloadData()
  277. callBack(self, self.files, self.newPageSize)
  278. }
  279. @IBAction func addFilesButtonAction(_ sender: Any) {
  280. guard let callBack = addFilesAction else { return }
  281. callBack(self)
  282. }
  283. @IBAction func sizeButtonAction(_ sender: NSButton) {
  284. originalSizeButton.state = .off
  285. A3SizeButton.state = .off
  286. A4SizeButton.state = .off
  287. USLetterSizeButton.state = .off
  288. USLegalButton.state = .off
  289. customSizeButton.state = .off
  290. sender.state = .on
  291. pageSizeHeightTextField.isEnabled = sender.isEqual(customSizeButton)
  292. pageSizeWidthTextField.isEnabled = sender.isEqual(customSizeButton)
  293. var size: NSSize = .zero
  294. switch sender.tag {
  295. case 0:
  296. break
  297. case 1:
  298. size = NSMakeSize(595, 841);
  299. break;
  300. case 2:
  301. size = NSMakeSize(841, 1190);
  302. break;
  303. case 3:
  304. size = NSMakeSize(612, 792);
  305. break;
  306. case 4:
  307. size = NSMakeSize(612, 1108);
  308. break;
  309. case 5:
  310. size = NSMakeSize(595, 841);
  311. pageSizeWidthTextField.stringValue = size.width.description
  312. pageSizeHeightTextField.stringValue = size.height.description
  313. break;
  314. default:
  315. break
  316. }
  317. self.newPageSize = size
  318. }
  319. }
  320. //MARK: public
  321. extension KMMergeView {
  322. func addFilePaths(urls: [URL]) {
  323. lockFiles.removeAll()
  324. // files.removeAll()
  325. for url in urls {
  326. let file = KMFileAttribute()
  327. file.filePath = url.path
  328. if file.isLocked {
  329. lockFiles.append(file)
  330. } else {
  331. var isExist = false
  332. for item in files {
  333. if item.filePath == file.filePath {
  334. isExist = true
  335. break
  336. }
  337. }
  338. if !isExist {
  339. files.append(file)
  340. }
  341. }
  342. }
  343. lockFilesIndex = 0
  344. self.openPasswordFile { [weak self] success, resultPassword in
  345. self?.reloadData()
  346. }
  347. }
  348. func addImageFile(fileUrl: URL) {
  349. if let image = NSImage(contentsOf: fileUrl) {
  350. if let page = PDFPage(image: image) {
  351. let document = PDFDocument()
  352. document.insert(page, at: 0)
  353. var path = self._saveImagePath() + "/" + fileUrl.deletingPathExtension().lastPathComponent + ".pdf"
  354. path = KMTools.getUniqueFilePath(filePath: path)
  355. let result = document.write(toFile: path)
  356. if result {
  357. let file = KMFileAttribute()
  358. file.filePath = path
  359. file.oriFilePath = fileUrl.path
  360. file.myPDFDocument = document
  361. // let attribe = try?FileManager.default.attributesOfItem(atPath: fileURL.path)
  362. // let fileSize = attribe?[FileAttributeKey.size] as? CGFloat ?? 0
  363. // size = fileSize + size
  364. //
  365. // if !IAPProductsManager.default().isAvailableAllFunction() && (files.count >= 2 || size > 20 * 1024 * 1024) {
  366. // KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  367. // return
  368. // }
  369. self.files.append(file)
  370. }
  371. }
  372. } else {
  373. Task {
  374. _ = await KMAlertTool.runModel(message: NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: ""))
  375. }
  376. }
  377. }
  378. func openPasswordFile(completion: @escaping ((_ success: Bool, _ resultPassword: String) -> Void)) {
  379. if lockFiles.count != 0 {
  380. let file = lockFiles[lockFilesIndex]
  381. KMBaseWindowController.checkPassword(url: URL(fileURLWithPath: file.filePath), type: .owner) { [unowned self] success, resultPassword in
  382. if success {
  383. file.password = resultPassword
  384. lockFilesIndex = lockFilesIndex + 1
  385. files.append(file)
  386. completion(true, "")
  387. if lockFiles.count > lockFilesIndex {
  388. self.openPasswordFile(completion: completion)
  389. }
  390. } else {
  391. completion(false, "")
  392. }
  393. }
  394. } else {
  395. completion(true, "")
  396. }
  397. }
  398. }
  399. //MARK: private
  400. extension KMMergeView {
  401. private func openFilePath(url: URL) {
  402. NSWorkspace.shared.activateFileViewerSelecting([url])
  403. }
  404. private func isExistAtFilePath(filePath: String) -> Bool{
  405. for file in self.files {
  406. if file.filePath == filePath {
  407. return true
  408. }
  409. }
  410. return false
  411. }
  412. private func _saveImagePath() -> String {
  413. let rootPath = KMDataManager.fetchAppSupportOfBundleIdentifierDirectory()
  414. let path = rootPath.appendingPathComponent("Merge").path
  415. if FileManager.default.fileExists(atPath: path) == false {
  416. try?FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: false)
  417. }
  418. return path
  419. }
  420. }