KMMergeView.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 = CGSizeZero
  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 = { [unowned self] filePaths in
  75. self.addFilePaths(urls: filePaths)
  76. }
  77. blankView.mouseDownAction = { [unowned self] view in
  78. self.addFilesAction?(self)
  79. }
  80. }
  81. override func updateLanguage() {
  82. originalSizeButton.title = NSLocalizedString("Original Size", comment: "")
  83. A4SizeButton.title = "A4";
  84. A3SizeButton.title = "A3";
  85. USLetterSizeButton.title = NSLocalizedString("U.S.Letter", comment: "");
  86. USLegalButton.title = NSLocalizedString("U.S.Legal", comment: "");
  87. customSizeButton.title = NSLocalizedString("Custom", comment: "");
  88. pageSizeWidthTextField.stringValue = "595";
  89. pageSizeHeightTextField.stringValue = "841";
  90. cancelButton.title = NSLocalizedString("Cancel", comment: "");
  91. clearButton.title = NSLocalizedString("Clear", comment: "");
  92. addFilesButton.title = NSLocalizedString("Add Files", comment: "")
  93. mergeButton.title = self.type == .add ? NSLocalizedString("Append", comment: ""): NSLocalizedString("Merge", comment: "")
  94. boxLabel.stringValue = NSLocalizedString("Page size:", comment: "")
  95. }
  96. override func reloadData() {
  97. self.updateButtonState()
  98. if files.count != 0 {
  99. self.blankView.isHidden = true
  100. } else {
  101. self.blankView.isHidden = false
  102. }
  103. self.tableview.reloadData()
  104. }
  105. func updateButtonState() {
  106. if (files.count >= 1) {
  107. blankView.isHidden = true
  108. clearButton.isEnabled = true
  109. mergeButton.isEnabled = true
  110. } else {
  111. blankView.isHidden = false
  112. clearButton.isEnabled = false
  113. mergeButton.isEnabled = false
  114. }
  115. }
  116. }
  117. extension KMMergeView {
  118. func controlTextDidChange(_ obj: Notification) {
  119. // NSTextField *textField = (NSTextField*)[obj object];
  120. // NSInteger index = textField.tag;
  121. // [[_files objectAtIndex:index] setPagesString:textField.stringValue];
  122. }
  123. }
  124. extension KMMergeView: NSTableViewDataSource {
  125. func numberOfRows(in tableView: NSTableView) -> Int {
  126. return files.count
  127. }
  128. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  129. var cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("KMMergeTableViewCell"), owner: self)
  130. if cell == nil {
  131. cell = KMMergeTableViewCell.init(frame: CGRectZero)
  132. }
  133. let myCellView: KMMergeTableViewCell = cell! as! KMMergeTableViewCell
  134. if row < files.count {
  135. myCellView.fileModel = files[row]
  136. }
  137. myCellView.index = row + 1
  138. // 配置单元格的显示内容
  139. myCellView.removeAction = { [unowned self] view, model in
  140. self.files.removeObject(model)
  141. self.reloadData()
  142. }
  143. return myCellView
  144. }
  145. func tableView(_ tableView: NSTableView, shouldSelect tableColumn: NSTableColumn?) -> Bool {
  146. return false
  147. }
  148. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  149. return 120
  150. }
  151. }
  152. extension KMMergeView: NSTableViewDelegate {
  153. func tableView(_ tableView: NSTableView, writeRowsWith rowIndexes: IndexSet, to pboard: NSPasteboard) -> Bool {
  154. let indexSetData = try? NSKeyedArchiver.archivedData(withRootObject: rowIndexes, requiringSecureCoding: true)
  155. pboard.declareTypes([NSPasteboard.PasteboardType.string], owner: self)
  156. pboard.setData(indexSetData, forType: MyTableCellViewDataType)
  157. return true
  158. }
  159. func tableView(_ tableView: NSTableView, validateDrop info: NSDraggingInfo, proposedRow row: Int, proposedDropOperation dropOperation: NSTableView.DropOperation) -> NSDragOperation {
  160. if dropOperation == .on {
  161. return []
  162. }
  163. var isCanDrag = false
  164. var result = NSDragOperation.copy
  165. let pboard = info.draggingPasteboard
  166. if pboard.availableType(from: [NSPasteboard.PasteboardType.fileURL]) != nil {
  167. let filePath = pboard.propertyList(forType: NSPasteboard.PasteboardType.fileURL) as? String
  168. let url = URL(string: filePath!)
  169. if url!.path.lowercased().hasSuffix("pdf") {
  170. isCanDrag = true
  171. } else {
  172. isCanDrag = false
  173. }
  174. } else if (pboard.availableType(from: [MyTableCellViewDataType]) != nil) {
  175. result = .every
  176. }
  177. if isCanDrag {
  178. result = .copy
  179. }
  180. return result
  181. }
  182. func tableView(_ tableView: NSTableView, acceptDrop info: NSDraggingInfo, row: Int, dropOperation: NSTableView.DropOperation) -> Bool {
  183. var result = false
  184. let pboard = info.draggingPasteboard
  185. insertRow = row
  186. // NSPasteboard.PasteboardType.fileURL
  187. if pboard.availableType(from: [NSPasteboard.PasteboardType.fileURL]) != nil {
  188. let filePath = pboard.propertyList(forType: NSPasteboard.PasteboardType.fileURL) as? String
  189. let url = URL(string: filePath!)
  190. var array = [URL]()
  191. array.append(url!)
  192. // for path in fileNames {
  193. // if !isExistAtFilePath(filePath: path) {
  194. // continue
  195. // }
  196. //// if let attrib = try? FileManager.default.attributesOfItem(atPath: path),
  197. //// let fileSize = attrib[.size] as? CGFloat {
  198. ////
  199. //// allFileSize += fileSize
  200. ////
  201. //// if !IAPProductsManager.defaultManager.isAvailableAllFunction {
  202. //// // 免费版只支持2个文件做合并,小于20M的文件合并
  203. //// if files.count >= 2 || allFileSize > (20 * 1024 * 1024) {
  204. //// let vc = KMToolCompareWindowController.toolCompare(with: .pageEdit, setSelectIndex: 1)
  205. //// vc.showWindow(nil)
  206. ////
  207. //// allFileSize -= fileSize
  208. //// addFiles(array)
  209. //// result = true
  210. //// return false
  211. //// }
  212. //// }
  213. // array.append(URL(string: path)!)
  214. //// }
  215. // }
  216. addFilePaths(urls: array)
  217. result = true
  218. } else if pboard.availableType(from: [MyTableCellViewDataType]) != nil {
  219. guard let data = info.draggingPasteboard.data(forType: MyTableCellViewDataType),
  220. let rowIndexes = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? IndexSet else {
  221. return false
  222. }
  223. // 移动数据
  224. var draggedItems: [KMFileAttribute] = []
  225. for index in rowIndexes {
  226. draggedItems.append(files[index])
  227. }
  228. for index in rowIndexes.reversed() {
  229. files.remove(at: index)
  230. }
  231. let insertionIndex = row > rowIndexes.first! ? row - rowIndexes.count : row
  232. for (index, item) in draggedItems.enumerated() {
  233. files.insert(item, at: insertionIndex + index)
  234. }
  235. tableView.reloadData()
  236. return true
  237. } else {
  238. result = false
  239. }
  240. return result
  241. }
  242. }
  243. extension KMMergeView {
  244. @IBAction func clearButtonAction(_ sender: Any) {
  245. self.files.removeAll()
  246. self.reloadData()
  247. guard let callBack = clearAction else { return }
  248. callBack(self)
  249. }
  250. @IBAction func cancelButtonAction(_ sender: Any) {
  251. guard let callBack = cancelAction else { return }
  252. callBack(self)
  253. }
  254. @IBAction func mergeButtonAction(_ sender: Any) {
  255. guard let callBack = mergeAction else { return }
  256. self.reloadData()
  257. callBack(self, self.files, self.newPageSize)
  258. }
  259. @IBAction func addFilesButtonAction(_ sender: Any) {
  260. guard let callBack = addFilesAction else { return }
  261. callBack(self)
  262. }
  263. @IBAction func sizeButtonAction(_ sender: NSButton) {
  264. originalSizeButton.state = .off
  265. A3SizeButton.state = .off
  266. A4SizeButton.state = .off
  267. USLetterSizeButton.state = .off
  268. USLegalButton.state = .off
  269. customSizeButton.state = .off
  270. sender.state = .on
  271. pageSizeHeightTextField.isEnabled = sender.isEqual(customSizeButton)
  272. pageSizeWidthTextField.isEnabled = sender.isEqual(customSizeButton)
  273. var size = CGSizeZero
  274. switch sender.tag {
  275. case 0:
  276. break
  277. case 1:
  278. size = CGSizeMake(595, 841);
  279. break;
  280. case 2:
  281. size = CGSizeMake(841, 1190);
  282. break;
  283. case 3:
  284. size = CGSizeMake(612, 792);
  285. break;
  286. case 4:
  287. size = CGSizeMake(612, 1108);
  288. break;
  289. case 5:
  290. size = CGSizeMake(595, 841);
  291. pageSizeWidthTextField.stringValue = size.width.description
  292. pageSizeHeightTextField.stringValue = size.height.description
  293. break;
  294. default:
  295. break
  296. }
  297. self.newPageSize = size
  298. }
  299. }
  300. //MARK: public
  301. extension KMMergeView {
  302. func addFilePaths(urls: [URL]) {
  303. lockFiles.removeAll()
  304. // files.removeAll()
  305. for url in urls {
  306. let file = KMFileAttribute()
  307. file.filePath = url.path
  308. if file.isLocked {
  309. lockFiles.append(file)
  310. } else {
  311. var isExist = false
  312. for item in files {
  313. if item.filePath == file.filePath {
  314. isExist = true
  315. break
  316. }
  317. }
  318. if !isExist {
  319. files.append(file)
  320. }
  321. }
  322. }
  323. lockFilesIndex = 0
  324. self.openPasswordFile { [unowned self] success, resultPassword in
  325. self.reloadData()
  326. }
  327. }
  328. func openPasswordFile(completion: @escaping ((_ success: Bool, _ resultPassword: String) -> Void)) {
  329. if lockFiles.count != 0 {
  330. let file = lockFiles[lockFilesIndex]
  331. KMBaseWindowController.checkPassword(url: URL(fileURLWithPath: file.filePath), type: .owner) { [unowned self] success, resultPassword in
  332. if success {
  333. file.password = resultPassword
  334. lockFilesIndex = lockFilesIndex + 1
  335. files.append(file)
  336. completion(true, "")
  337. if lockFiles.count > lockFilesIndex {
  338. self.openPasswordFile(completion: completion)
  339. }
  340. } else {
  341. completion(false, "")
  342. }
  343. }
  344. } else {
  345. completion(true, "")
  346. }
  347. }
  348. }
  349. //MARK: private
  350. extension KMMergeView {
  351. private func openFilePath(url: URL) {
  352. NSWorkspace.shared.activateFileViewerSelecting([url])
  353. }
  354. private func isExistAtFilePath(filePath: String) -> Bool{
  355. for file in self.files {
  356. if file.filePath == filePath {
  357. return true
  358. }
  359. }
  360. return false
  361. }
  362. }